-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
cannelloni.cpp
540 lines (494 loc) · 16.3 KB
/
cannelloni.cpp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
* This file is part of cannelloni, a SocketCAN over Ethernet tunnel.
*
* Copyright (C) 2014-2023 Maximilian Güntner <code@mguentner.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <cstdlib>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iomanip>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/signalfd.h>
#include "cannelloni.h"
#include "config.h"
#include "connection.h"
#include "inet_address.h"
#include "tcpthread.h"
#include "udpthread.h"
#ifdef SCTP_SUPPORT
#include "sctpthread.h"
#endif
#include "canthread.h"
#include "csvmapparser.h"
#include "framebuffer.h"
#include "logging.h"
#include "make_unique.h"
#include <memory>
#define MIN_LINK_MTU_SIZE 100
#define CANNELLONI_VERSION "1.1.0"
using namespace cannelloni;
void printUsage() {
std::cout << "cannelloni Release: " << CANNELLONI_VERSION << std::endl;
std::cout << "Usage: cannelloni OPTIONS" << std::endl;
std::cout << "Available options:" << std::endl;
#ifdef SCTP_SUPPORT
std::cout << "\t -S [cs] \t\t enable SCTP transport." << std::endl;
std::cout << "\t\t\t c : act as client" << std::endl;
std::cout << "\t\t\t s : act as server" << std::endl;
#endif
std::cout << "\t -C [cs] \t\t enable TCP transport." << std::endl;
std::cout << "\t\t\t c : act as client" << std::endl;
std::cout << "\t\t\t s : act as server" << std::endl;
std::cout << "\t -l PORT \t\t listening port, default: 20000" << std::endl;
std::cout << "\t -L ADDRESS \t\t listening ADDRESS, default: 0.0.0.0" << std::endl;
std::cout << "\t -r PORT \t\t remote port, default: 20000" << std::endl;
std::cout << "\t -R ADDRESS \t\t remote ADDRESS (mandatory for UDP), default: 127.0.0.1" << std::endl;
std::cout << "\t -I INTERFACE \t\t can interface, default: vcan0" << std::endl;
std::cout << "\t -t timeout \t\t buffer timeout for can messages (us), default: 100000" << std::endl;
std::cout << "\t -T table.csv \t\t path to csv with individual timeouts" << std::endl;
std::cout << "\t -s \t\t enable frame sorting" << std::endl;
std::cout << "\t -p \t\t no peer checking" << std::endl;
std::cout << "\t -d [cubt]\t\t enable debug, can be any of these: " << std::endl;
std::cout << "\t\t\t c : enable debugging of can frames" << std::endl;
#ifdef SCTP_SUPPORT
std::cout << "\t\t\t u : enable debugging of udp/tcp/sctp frames" << std::endl;
#else
std::cout << "\t\t\t u : enable debugging of udp/tcp frames" << std::endl;
#endif
std::cout << "\t\t\t b : enable debugging of internal buffer structures" << std::endl;
std::cout << "\t\t\t t : enable debugging of internal timers" << std::endl;
std::cout << "\t -4 \t\t\t use IPv4 (default)" << std::endl;
std::cout << "\t -6 \t\t\t use IPv6" << std::endl;
std::cout << "\t -m \t\t\t set MTU, default: 1500 bytes" << std::endl;
std::cout << "\t -f \t\t\t fork into background / daemon mode" << std::endl;
std::cout << "\t -P \t\t\t pid file path (only in daemon mode), default: /var/run/cannelloni.pid" << std::endl;
std::cout << "\t -h \t\t\t display this help text" << std::endl;
}
void daemonize(std::string pidFilePath) {
pid_t pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS); // exit parent
}
if (setsid() < 0) {
exit(EXIT_FAILURE);
}
// fork again
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS); // exit parent
}
std::cout << "pid: " << getpid() << std::endl;
std::ofstream pidFile;
pidFile.open(pidFilePath, std::ofstream::out);
if (pidFile.fail()) {
std::cerr << "could not write pid file" << std::endl;
exit(EXIT_FAILURE);
}
pidFile << getpid() << std::endl;
pidFile.flush();
pidFile.close();
// change to root, cannelloni only may read the
// timeoutTableFile which has already happend
// by the time this function is called
if (chdir("/") < 0) {
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
int null_fd = open("/dev/null", O_RDWR);
if (null_fd == -1) {
exit(EXIT_FAILURE);
}
dup2(null_fd, STDIN_FILENO);
dup2(null_fd, STDOUT_FILENO);
dup2(null_fd, STDERR_FILENO);
if (null_fd > STDERR_FILENO) {
close(null_fd);
}
}
int main(int argc, char **argv) {
int opt;
bool remoteIPSupplied = false;
bool sortUDP = false;
bool checkPeer = true;
bool useTCP = false;
bool useSCTP = false;
bool useIPv4 = true;
bool useIPv6 = false;
bool forkIntoBackground = false;
uint16_t linkMtuSize = 1500;
TCPThreadRole tcpRole = TCP_CLIENT;
#ifdef SCTP_SUPPORT
SCTPThreadRole sctpRole = SCTP_CLIENT;
#endif
char remoteIP[INET6_ADDRSTRLEN] = "";
uint16_t remotePort = 20000;
char localIP[INET6_ADDRSTRLEN] = "";
uint16_t localPort = 20000;
std::string canInterfaceName = "vcan0";
uint32_t bufferTimeout = 100000;
std::string timeoutTableFile;
std::string pidFilePath = "/var/run/cannelloni.pid";
/* Key is CAN ID, Value is timeout in us */
std::map<uint32_t, uint32_t> timeoutTable;
struct debugOptions_t debugOptions = { /* can */ 0, /* udp */ 0, /* buffer */ 0, /* timer */ 0 };
const std::string argument_options = "C:l:L:r:R:I:t:T:d:m:P:hsp46f"
#ifdef SCTP_SUPPORT
"S:";
#else
;
#endif
while ((opt = getopt(argc, argv, argument_options.c_str())) != -1) {
switch(opt) {
case 'C':
switch (optarg[0]) {
case 's':
case 'S':
tcpRole = TCP_SERVER;
useTCP = true;
break;
case 'c':
case 'C':
tcpRole = TCP_CLIENT;
useTCP = true;
break;
default:
std::cout << "Usage Error: " << std::endl
<< "-C only accepts [s]erver or [c]lient" << std::endl;
printUsage();
return -1;
}
break;
#ifdef SCTP_SUPPORT
case 'S':
switch (optarg[0]) {
case 's':
case 'S':
sctpRole = SCTP_SERVER;
useSCTP = true;
break;
case 'c':
case 'C':
sctpRole = SCTP_CLIENT;
useSCTP = true;
break;
default:
std::cout << "Usage Error: " << std::endl
<< "-S only accepts [s]erver or [c]lient" << std::endl;
printUsage();
return -1;
}
break;
#else
case'S':
std::cout << "Usage Error: " << std::endl
<< "SCTP Transport is not supported in this build." << std::endl
<< std::endl;
printUsage();
return -1;
#endif
case 'l':
localPort = strtoul(optarg, NULL, 10);
break;
case 'L':
strncpy(localIP, optarg, INET6_ADDRSTRLEN-1);
localIP[INET6_ADDRSTRLEN-1] = '\0';
break;
case 'r':
remotePort = strtoul(optarg, NULL, 10);
remotePort = strtoul(optarg, NULL, 10);
break;
case 'R':
strncpy(remoteIP, optarg, INET6_ADDRSTRLEN-1);
remoteIP[INET6_ADDRSTRLEN-1] = '\0';
remoteIPSupplied = true;
break;
case 'I':
canInterfaceName = std::string(optarg);
break;
case 't':
bufferTimeout = static_cast<uint32_t>(strtoul(optarg, NULL, 10));
break;
case 'T':
timeoutTableFile = std::string(optarg);
break;
case 'd':
if (strchr(optarg, 'c'))
debugOptions.can = 1;
if (strchr(optarg, 'u'))
debugOptions.udp = 1;
if (strchr(optarg, 'b'))
debugOptions.buffer = 1;
if (strchr(optarg, 't'))
debugOptions.timer = 1;
break;
case 'h':
printUsage();
return 0;
case 's':
sortUDP = true;
break;
case 'p':
checkPeer = false;
break;
case '4':
useIPv4 = true;
break;
case '6':
useIPv6 = true;
useIPv4 = false;
break;
case 'm':
linkMtuSize = static_cast<uint16_t>(strtol(optarg, NULL, 10));
break;
case 'f':
forkIntoBackground = true;
break;
case 'P':
pidFilePath = std::string(optarg);
break;
default:
printUsage();
return -1;
}
}
if (useIPv4 && useIPv6) {
std::cout << "Usage Error: " << std::endl
<< "Can't use IPv4 and IPv6 simultaneously" << std::endl
<< std::endl;
printUsage();
return -1;
}
if (useTCP && useSCTP) {
std::cout << "Usage Error: " << std::endl
<< "Can't use TCP and SCTP simultaneously" << std::endl
<< std::endl;
printUsage();
return -1;
}
if (!remoteIPSupplied && !useSCTP && !useTCP) {
std::cout << "Usage Error: " << std::endl
<< "Remote IP not supplied" << std::endl
<< std::endl;
printUsage();
return -1;
}
if (bufferTimeout == 0) {
std::cout << "Usage Error: " << std::endl
<< "Only non-zero timeouts are allowed" << std::endl
<< std::endl;
printUsage();
return -1;
}
if (linkMtuSize < MIN_LINK_MTU_SIZE) {
std::cout << "Usage Error: " << std::endl
<< "Specify a link mtu size greater than " << MIN_LINK_MTU_SIZE << std::endl
<< std::endl;
printUsage();
return -1;
}
// set default values if no IPs have been provided
if (strlen(localIP) == 0) {
if (useIPv4) {
strcpy(localIP, "0.0.0.0");
} else {
strcpy(localIP, "::");
}
}
if (!timeoutTableFile.empty()) {
CSVMapParser<uint32_t,uint32_t> mapParser;
if(!mapParser.open(timeoutTableFile)) {
lerror << "Unable to open " << timeoutTableFile << "." << std::endl;
return -1;
}
if(!mapParser.parse()) {
lerror << "Error while parsing " << timeoutTableFile << "." << std::endl;
return -1;
}
if(!mapParser.close()) {
lerror << "Error while closing" << timeoutTableFile << "." << std::endl;
return -1;
}
timeoutTable = mapParser.read();
}
if (debugOptions.timer) {
if (timeoutTable.empty()) {
linfo << "No custom timeout table specified, using "
<< bufferTimeout << " us for all frames." << std::endl;
} else {
linfo << "Custom timeout table loaded: " << std::endl;
linfo << "*---------------------*" << std::endl;
linfo << "| ID | Timeout (us) |" << std::endl;
std::map<uint32_t,uint32_t>::iterator it;
for (it=timeoutTable.begin(); it!=timeoutTable.end(); ++it)
linfo << "|" << std::setw(6) << it->first << "|" << std::setw(14) << it->second << "| " << std::endl;
linfo << "*---------------------*" << std::endl;
linfo << "Other Frames:" << bufferTimeout << " us." << std::endl;
}
}
/* We use the signalfd() system call to create a
* file descriptor to receive signals */
sigset_t signalMask;
struct signalfd_siginfo signalFdInfo;
int signalFD;
/* Prepare the signalMask */
sigemptyset(&signalMask);
sigaddset(&signalMask, SIGTERM);
sigaddset(&signalMask, SIGINT);
/* Block these signals... */
if (sigprocmask(SIG_BLOCK, &signalMask, NULL) == -1) {
lerror << "sigprocmask error" << std::endl;
return -1;
}
/* ...since we want to receive them through signalFD */
signalFD = signalfd(-1, &signalMask, 0);
if (signalFD == -1) {
lerror << "signalfd error" << std::endl;
return -1;
}
struct sockaddr_storage remoteAddr;
struct sockaddr_storage localAddr;
memset(&remoteAddr, 0, sizeof(sockaddr_storage));
memset(&localAddr, 0, sizeof(sockaddr_storage));
int addressFamily = AF_INET;
if (useIPv6) {
addressFamily = AF_INET6;
}
if (remoteIPSupplied && !parseAddress(remoteIP, (struct sockaddr *) &remoteAddr, addressFamily)) {
lerror << "Invalid remote address";
return -1;
}
if (!parseAddress(localIP, (struct sockaddr *) &localAddr, addressFamily)) {
lerror << "Invalid listen address";
return -1;
}
if (addressFamily == AF_INET) {
((struct sockaddr_in *) &remoteAddr)->sin_port = htons(remotePort);
((struct sockaddr_in *) &localAddr)->sin_port = htons(localPort);
} else if (addressFamily == AF_INET6) {
((struct sockaddr_in6 *) &remoteAddr)->sin6_port = htons(remotePort);
((struct sockaddr_in6 *) &localAddr)->sin6_port = htons(localPort);
}
if (forkIntoBackground) {
std::cout << "cannelloni is forking into background." << std::endl;
daemonize(pidFilePath);
}
std::unique_ptr<ConnectionThread> netThread;
if (useTCP && tcpRole == TCP_SERVER) {
netThread = std::make_unique<TCPServerThread>(debugOptions, TCPServerThreadParams {
.remoteAddr = remoteAddr,
.localAddr = localAddr,
.addressFamily = addressFamily,
.checkPeer = checkPeer
});
} else if (useTCP && tcpRole == TCP_CLIENT) {
netThread = std::make_unique<TCPClientThread>(debugOptions, TCPThreadParams {
.remoteAddr = remoteAddr,
.localAddr = localAddr,
.addressFamily = addressFamily,
});
} else if (useSCTP) {
#ifdef SCTP_SUPPORT
auto sctpThread = std::make_unique<SCTPThread>(debugOptions, SCTPThreadParams {
.remoteAddr = remoteAddr,
.localAddr = localAddr,
.addressFamily = addressFamily,
.sortFrames = sortUDP,
.checkPeer = checkPeer,
.linkMtuSize = linkMtuSize,
.role = sctpRole,
});
sctpThread.get()->setTimeout(bufferTimeout);
sctpThread.get()->setTimeoutTable(timeoutTable);
netThread = std::move(sctpThread);
#endif
} else {
auto udpThread = std::make_unique<UDPThread>(debugOptions, UDPThreadParams{
.remoteAddr = remoteAddr,
.localAddr = localAddr,
.addressFamily = addressFamily,
.sortFrames = sortUDP,
.checkPeer = checkPeer,
.linkMtuSize = linkMtuSize,
});
udpThread.get()->setTimeout(bufferTimeout);
udpThread.get()->setTimeoutTable(timeoutTable);
netThread = std::move(udpThread);
}
auto canThread = std::make_unique<CANThread>(debugOptions, canInterfaceName);
auto netFrameBuffer = std::make_unique<FrameBuffer>(1000,16000);
auto canFrameBuffer = std::make_unique<FrameBuffer>(1000,16000);
netThread->setPeerThread(canThread.get());
netThread->setFrameBuffer(netFrameBuffer.get());
canThread->setPeerThread(netThread.get());
canThread->setFrameBuffer(canFrameBuffer.get());
int netStartReturn = netThread->start();
int canStartReturn = canThread->start();
while (netStartReturn == 0 && canStartReturn == 0) {
struct timeval timeout;
fd_set set;
FD_ZERO(&set);
FD_SET(signalFD, &set);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int ret = select(signalFD + 1, &set, NULL, NULL, &timeout);
if (ret == -1) {
lerror << "select error" << std::endl;
break;
} else if (ret == 0) {
if (!(netThread->isRunning() && canThread->isRunning())) {
break;
}
} else {
ssize_t receivedBytes = read(signalFD, &signalFdInfo, sizeof(struct signalfd_siginfo));
if (receivedBytes != sizeof(struct signalfd_siginfo)) {
lerror << "signalfd read error" << std::endl;
break;
}
/* Currently we only receive SIGTERM and SIGINT but we check nonetheless */
if (signalFdInfo.ssi_signo == SIGTERM || signalFdInfo.ssi_signo == SIGINT) {
linfo << "Received signal " << signalFdInfo.ssi_signo << ": Exiting" << std::endl;
break;
}
}
}
netThread->stop();
netThread->join();
canThread->stop();
canThread->join();
/* Clear/free pools once all threads are joined */
netFrameBuffer->clearPool();
canFrameBuffer->clearPool();
close(signalFD);
return 0;
}