/
proxy.go
382 lines (323 loc) · 10.4 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package cmd
import (
"github.com/spf13/cobra"
"golang.org/x/net/ipv4"
"github.com/BurntSushi/toml"
"github.com/linklayer/go-socketcan/pkg/socketcan"
"github.com/karlding/tritiumbridgetools/pkg/tritium"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
)
// BridgeConfig contains all the configuration for a particular Tritium Bridge
type BridgeConfig struct {
ID uint8 `toml:"id"`
IP string `toml:"ip"`
NetworkInterface string `toml:"network_interface"`
SocketCANInterface string `toml:"vcan"`
}
// Config contains the representation of a TOML file describing the network
type Config struct {
Bridge []BridgeConfig `toml:"bridge"`
}
// Transport is the type of network transport (TCP/UDP) used to communicate
// with the Tritium CAN-Ethernet bridge
var Transport string
var tomlFile string
func init() {
log.SetOutput(os.Stdout)
rootCmd.AddCommand(proxyCommand)
proxyCommand.Flags().StringVarP(&Transport, "transport", "t", "", "Transport source [tcp,udp]")
proxyCommand.MarkFlagRequired("transport")
proxyCommand.Flags().StringVarP(&tomlFile, "config", "f", "", "TOML file")
proxyCommand.MarkFlagRequired("config")
}
var proxyCommand = &cobra.Command{
Use: "proxy",
Short: "",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
doStuff()
},
}
func handleUDPPackets(packetConn *ipv4.PacketConn, socketMap map[uint8]socketcan.Interface) {
// (64 + 8 + 8 + 32 + 56 + 8 + 56 + 8) bits = 30 bytes
b := make([]byte, 30)
for {
numBytes, _, _, err := packetConn.ReadFrom(b)
if err != nil {
// error handling
continue
}
if numBytes != 30 {
log.Println(b)
panic("Failed")
}
tritiumPacket := new(tritium.Packet)
tritium.ByteArrayToTritiumMessage(b, tritiumPacket)
// Now forward onto SocketCAN interface if it isn't a Heartbeat or
// Settings frame
if !tritiumPacket.FlagHeartbeat {
// Find the socket by bus number
if vcan, ok := socketMap[tritiumPacket.BusNumber]; ok {
tmp := make([]byte, 8)
binary.BigEndian.PutUint64(tmp[:], tritiumPacket.Data)
vcan.SendFrame(socketcan.CanFrame{
ArbId: tritiumPacket.CanID,
Dlc: tritiumPacket.Length,
Data: tmp[0:8],
Extended: tritiumPacket.FlagExtendedID,
})
}
}
}
}
func doStuffOverUDP(conf Config) {
// The Group Address is 239.255.60.60
group := net.IPv4(239, 255, 60, 60)
// socketMap[Bus Number] = SocketCAN file descriptor
socketMap := make(map[uint8]socketcan.Interface)
// Start a UDP connection
// The Tritium CAN-Ethernet bridge always broadcasts on port 4876
// TODO: Should we only bind on a single interface?
c, err := net.ListenPacket("udp4", "0.0.0.0:4876")
if err != nil {
// error handling
return
}
defer c.Close()
// Join UDP Multicast group
// This can be verified by checking the groups you belong to:
// netstat -gn | grep '239.255.60.60'
p := ipv4.NewPacketConn(c)
for _, bridge := range conf.Bridge {
vcan, err := socketcan.NewRawInterface(bridge.SocketCANInterface)
if err != nil {
log.Fatal(err)
return
}
socketMap[bridge.ID] = vcan
networkInterface, err := net.InterfaceByName(bridge.NetworkInterface)
if err != nil {
log.Fatal(err)
return
}
// TODO: Error handling for this..
// Probably handle failing case with random padded
macAddressBuffer := networkInterface.HardwareAddr
macAddress := binary.LittleEndian.Uint64(macAddressBuffer[0:8])
if err := p.JoinGroup(networkInterface, &net.UDPAddr{IP: group}); err != nil {
// error handling
return
}
// Start a goroutine for each SocketCAN interface to forward over UDP
go func(bridgeID uint8, packetConn *ipv4.PacketConn, macAddress uint64) {
txBuff := make([]byte, 30)
for {
canFrame, err := socketMap[bridgeID].RecvFrame()
if err != nil {
panic("oof")
}
log.Println("Received SocketCAN frame")
// TODO: Write a proper conversion function here
tritiumPacket := new(tritium.Packet)
tritiumPacket.VersionIdentifier = uint64(0x5472697469756)
tritiumPacket.BusNumber = bridgeID
tritiumPacket.ClientIdentifier = macAddress
tritiumPacket.CanID = canFrame.ArbId
tritiumPacket.FlagExtendedID = canFrame.Extended
tritiumPacket.Length = canFrame.Dlc
tritiumPacket.Data = binary.BigEndian.Uint64(canFrame.Data[0:8])
tritium.PacketToNetworkByteArray(tritiumPacket, txBuff)
// Send multicast packet to group
// TODO: is it necessary to specify a control message?
bytes, err := packetConn.WriteTo(txBuff, &ipv4.ControlMessage{IfIndex: networkInterface.Index}, &net.UDPAddr{IP: group, Port: 4876})
if err != nil {
panic(err)
}
if bytes != 30 {
log.Printf("Only wrote %d bytes\n", bytes)
}
}
}(bridge.ID, p, macAddress)
}
go handleUDPPackets(p, socketMap)
select {}
}
func doStuffOverTCP(conf Config) {
// In TCP mode, we need to send:
//
// +-----------------------------+
// | Fwd Identifier (32 bits) |
// +-----------------------------+
// | Fwd range (32 bits) |
// +-----------------------------+
// | Padding (8 bits) |
// +-----------------------------+
// | Bus Identifier (56 bits) |
// +-----------------------------+
// | Padding (8 bits) |
// +-----------------------------+
// | Client Identifier (56 bits) |
// +-----------------------------+
for _, bridge := range conf.Bridge {
vcan, err := socketcan.NewRawInterface(bridge.SocketCANInterface)
if err != nil {
log.Fatal(err)
return
}
networkInterface, err := net.InterfaceByName(bridge.NetworkInterface)
if err != nil {
log.Fatal(err)
return
}
setupBuffer := make([]byte, (4 + 4 + 1 + 7 + 1 + 7))
// The bridge will forward any packet matching:
//
// fwdIdentifier <= CAN arbitration id < (fwdIdentifier + fwdRange)
//
// So we set the bridge to forward all valid CAN IDs (including extended),
// which is the range [0, 2^29 - 1]
fwdIdentifier := uint32(0)
fwdRange := uint32(536870911)
// The Bus Number must match the Bus Number specified in the Tritium
// CAN-Ethernet Bridge Configuration tool, otherwise the TCP connection is
// terminated.
// TODO: Take these as configuration options?
busIdentifier := (uint64(0x5472697469756) << 4) | uint64(bridge.ID)
binary.BigEndian.PutUint32(setupBuffer[0:4], fwdIdentifier)
binary.BigEndian.PutUint32(setupBuffer[4:8], fwdRange)
binary.BigEndian.PutUint64(setupBuffer[8:16], busIdentifier)
// TODO: We probably need error checking here in case the MAC address
// is not 8 bytes?
copy(setupBuffer[16:24], networkInterface.HardwareAddr[0:8])
macAddressBuffer := networkInterface.HardwareAddr
macAddress := binary.LittleEndian.Uint64(macAddressBuffer[0:8])
// Establish a TCP connection
// TODO: Handle multiple Tritium bridges on the same subnet.
bridgeIPAddress := net.ParseIP(bridge.IP)
conn, err := net.Dial("tcp4", fmt.Sprintf("%s:4876", bridgeIPAddress.String()))
if err != nil {
log.Println(err)
return
}
// Send fwd identifiers for every message
// TODO: Maybe we want to support selectively sending fwd identifiers?
bytes, err := conn.Write(setupBuffer)
log.Println(bytes)
if err != nil {
log.Println(err)
return
}
// Start a goroutine for each bridge we're receiving from
go func(conn net.Conn, vcan socketcan.Interface) {
b := make([]byte, 30)
// (64 + 8 + 8 + 32 + 56 + 8 + 56 + 8) bits = 30 bytes
numBytes, err := io.ReadFull(conn, b[:])
if err != nil {
// error handling
log.Println(err)
return
}
if numBytes != 30 {
log.Println(b)
panic("Failed")
}
tritiumPacket := new(tritium.Packet)
tritium.ByteArrayToTritiumMessage(b, tritiumPacket)
// Now forward onto SocketCAN interface if it isn't a Heartbeat or
// Settings frame
if !tritiumPacket.FlagHeartbeat && !tritiumPacket.FlagSettings {
tmp := make([]byte, 8)
binary.BigEndian.PutUint64(tmp[:], tritiumPacket.Data)
vcan.SendFrame(socketcan.CanFrame{
ArbId: tritiumPacket.CanID,
Dlc: tritiumPacket.Length,
Data: tmp[0:8],
Extended: tritiumPacket.FlagExtendedID,
})
}
// Subsequent packets are 14 bytes
buff := make([]byte, 14)
for {
// (64 + 8 + 8 + 32 + 56 + 8 + 56 + 8) bits = 30 bytes
numBytes, err := io.ReadFull(conn, buff[:])
log.Printf("Received %d bytes\n", numBytes)
for i, val := range buff {
log.Printf("buff[%d] = 0x%x\n", i, val)
}
if err != nil {
// error handling
log.Println(err)
continue
panic("Failed")
}
if numBytes != 14 {
log.Println(buff)
panic("Failed")
}
tritiumPacket := new(tritium.Packet)
tritium.ByteArrayTCPToTritiumMessage(buff, tritiumPacket)
// Now forward onto SocketCAN interface if it isn't a Heartbeat
// or Settings frame
if !tritiumPacket.FlagHeartbeat && !tritiumPacket.FlagSettings {
tmp := make([]byte, 8)
binary.BigEndian.PutUint64(tmp[:], tritiumPacket.Data)
vcan.SendFrame(socketcan.CanFrame{
ArbId: tritiumPacket.CanID,
Dlc: tritiumPacket.Length,
Data: tmp[0:8],
Extended: tritiumPacket.FlagExtendedID,
})
}
}
}(conn, vcan)
// Forward from SocketCAN interface over TCP
go func(vcan socketcan.Interface, bridgeID uint8, packetConn net.Conn, macAddress uint64) {
txBuff := make([]byte, 30)
for {
canFrame, err := vcan.RecvFrame()
if err != nil {
continue
}
// TODO: Write a proper conversion function here
tritiumPacket := new(tritium.Packet)
tritiumPacket.VersionIdentifier = uint64(0x5472697469756)
tritiumPacket.BusNumber = bridgeID
tritiumPacket.ClientIdentifier = macAddress
tritiumPacket.CanID = canFrame.ArbId
tritiumPacket.FlagExtendedID = canFrame.Extended
tritiumPacket.Length = canFrame.Dlc
tritiumPacket.Data = binary.BigEndian.Uint64(canFrame.Data[0:8])
tritium.PacketToNetworkByteArray(tritiumPacket, txBuff)
// We skip the headers that aren't needed:
//
// * Bus Identifier: 8 bytes
// * Client Identifier: 8 bytes
bytes, err := packetConn.Write(txBuff[16:])
if err != nil {
panic(err)
}
if bytes != 30 {
log.Printf("Only wrote %d bytes\n", bytes)
}
}
}(vcan, bridge.ID, conn, macAddress)
}
select {}
}
func doStuff() {
// Set GitCommit and Version
var conf Config
// TOML parsing
toml.DecodeFile(tomlFile, &conf)
if Transport == "udp" {
doStuffOverUDP(conf)
} else if Transport == "tcp" {
doStuffOverTCP(conf)
}
}