4 * Implements a socket server which understands ASCII HEX
5 * messages for simple TCP/IP <-> ISO 15765-2 bridging.
7 * General message format: <[data]+>
9 * e.g. for an eight bytes PDU
13 * Valid ISO 15625-2 PDUs have a length from 1-4095 bytes.
16 * Andre Naujoks (the socket server stuff)
17 * Oliver Hartkopp (the rest)
19 * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
20 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of Volkswagen nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * Alternatively, provided that this notice is retained in full, this
35 * software may be distributed under the terms of the GNU General
36 * Public License ("GPL") version 2, in which case the provisions of the
37 * GPL apply INSTEAD OF those given above.
39 * The provided data structures and external interfaces from this code
40 * are not restricted to be used by modules with a GPL compatible license.
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
55 * Send feedback to <linux-can@vger.kernel.org>
67 #include <sys/types.h>
69 #include <sys/socket.h>
70 #include <sys/ioctl.h>
73 #include <netinet/in.h>
75 #include <linux/can.h>
76 #include <linux/can/isotp.h>
78 #define NO_CAN_ID 0xFFFFFFFFU
80 /* allow PDUs greater 4095 bytes according ISO 15765-2:2015 */
81 #define MAX_PDU_LENGTH 6000
83 int b64hex(char *asc, unsigned char *bin, int len)
87 for (i = 0; i < len; i++) {
88 if (!sscanf(asc+(i*2), "%2hhx", bin+i))
99 void print_usage(char *prg)
101 fprintf(stderr, "\nUsage: %s -l <port> -s <can_id> -d <can_id> [options] <CAN interface>\n", prg);
102 fprintf(stderr, "Options: (* = mandatory)\n");
103 fprintf(stderr, "\n");
104 fprintf(stderr, "ip adressing:\n");
105 fprintf(stderr, " * -l <port> (local port for the server)\n");
106 fprintf(stderr, "\n");
107 fprintf(stderr, "isotp adressing:\n");
108 fprintf(stderr, " * -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
109 fprintf(stderr, " * -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
110 fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
111 fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
112 fprintf(stderr, "\n");
113 fprintf(stderr, "padding:\n");
114 fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
115 fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
116 fprintf(stderr, "\n");
117 fprintf(stderr, "rx path: (config, which is sent to the sender / data source)\n");
118 fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
119 fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
120 fprintf(stderr, " -w <num> (max. wait frame transmissions)\n");
121 fprintf(stderr, "\n");
122 fprintf(stderr, "tx path: (config, which changes local tx settings)\n");
123 fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
124 fprintf(stderr, "\n");
125 fprintf(stderr, "All values except for '-l' and '-t' are expected in hexadecimal values.\n");
126 fprintf(stderr, "\n");
129 int main(int argc, char **argv)
131 extern int optind, opterr, optopt;
134 int sl, sa, sc; /* (L)isten, (A)ccept, (C)AN sockets */
135 struct sockaddr_in saddr, clientaddr;
136 struct sockaddr_can caddr;
137 static struct can_isotp_options opts;
138 static struct can_isotp_fc_options fcopts;
139 static struct can_isotp_ll_options llopts;
141 socklen_t sin_size = sizeof(clientaddr);
142 socklen_t caddrlen = sizeof(caddr);
144 struct sigaction signalaction;
155 int idx = 0; /* index in txmsg[] */
157 unsigned char msg[MAX_PDU_LENGTH + 1]; /* isotp socket message buffer (4095 + test_for_too_long_byte)*/
158 char rxmsg[MAX_PDU_LENGTH * 2 + 4]; /* isotp->tcp ASCII message buffer (4095*2 + < > \n null) */
159 char txmsg[MAX_PDU_LENGTH * 2 + 3]; /* tcp->isotp ASCII message buffer (4095*2 + < > null) */
161 /* mark missing mandatory commandline options as missing */
162 caddr.can_addr.tp.tx_id = caddr.can_addr.tp.rx_id = NO_CAN_ID;
164 while ((opt = getopt(argc, argv, "l:s:d:x:p:P:b:m:w:t:L:v?")) != -1) {
167 local_port = strtoul(optarg, (char **)NULL, 10);
171 caddr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
172 if (strlen(optarg) > 7)
173 caddr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
177 caddr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
178 if (strlen(optarg) > 7)
179 caddr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
184 int elements = sscanf(optarg, "%hhx:%hhx",
186 &opts.rx_ext_address);
189 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
190 else if (elements == 2)
191 opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
193 printf("incorrect extended addr values '%s'.\n", optarg);
194 print_usage(basename(argv[0]));
202 int elements = sscanf(optarg, "%hhx:%hhx",
204 &opts.rxpad_content);
207 opts.flags |= CAN_ISOTP_TX_PADDING;
208 else if (elements == 2)
209 opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
210 else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
211 opts.flags |= CAN_ISOTP_RX_PADDING;
213 printf("incorrect padding values '%s'.\n", optarg);
214 print_usage(basename(argv[0]));
221 if (optarg[0] == 'l')
222 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
223 else if (optarg[0] == 'c')
224 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
225 else if (optarg[0] == 'a')
226 opts.flags |= (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA);
228 printf("unknown padding check option '%c'.\n", optarg[0]);
229 print_usage(basename(argv[0]));
235 fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
239 fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
243 fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
247 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
251 if (sscanf(optarg, "%hhu:%hhu:%hhu",
254 &llopts.tx_flags) != 3) {
255 printf("unknown link layer options '%s'.\n", optarg);
256 print_usage(basename(argv[0]));
266 print_usage(basename(argv[0]));
271 fprintf(stderr, "Unknown option %c\n", opt);
272 print_usage(basename(argv[0]));
278 if ((argc - optind != 1) || (local_port == 0) ||
279 (caddr.can_addr.tp.tx_id == NO_CAN_ID) ||
280 (caddr.can_addr.tp.rx_id == NO_CAN_ID)) {
281 print_usage(basename(argv[0]));
285 sigemptyset(&sigset);
286 signalaction.sa_handler = &childdied;
287 signalaction.sa_mask = sigset;
288 signalaction.sa_flags = 0;
289 sigaction(SIGCHLD, &signalaction, NULL); /* signal for dying child */
291 if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
292 perror("inetsocket");
296 saddr.sin_family = AF_INET;
297 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
298 saddr.sin_port = htons(local_port);
300 while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
306 if (listen(sl, 3) != 0) {
312 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
321 if (errno != EINTR) {
323 * If the cause for the error was NOT the
324 * signal from a dying child => give an error
332 if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
337 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
338 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
341 if (setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
342 perror("link layer sockopt");
347 caddr.can_family = AF_CAN;
348 strcpy(ifr.ifr_name, argv[optind]);
349 if (ioctl(sc, SIOCGIFINDEX, &ifr) < 0) {
350 perror("SIOCGIFINDEX");
353 caddr.can_ifindex = ifr.ifr_ifindex;
355 if (bind(sc, (struct sockaddr *)&caddr, caddrlen) < 0) {
363 FD_SET(sc, &readfds);
364 FD_SET(sa, &readfds);
366 select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
368 if (FD_ISSET(sc, &readfds)) {
371 nbytes = read(sc, &msg, MAX_PDU_LENGTH + 1);
373 if (nbytes < 1 || nbytes > MAX_PDU_LENGTH) {
374 perror("read from isotp socket");
380 for ( i = 0; i < nbytes; i++)
381 sprintf(rxmsg + 1 + 2*i, "%02X", msg[i]);
383 /* finalize string for sending */
384 strcat(rxmsg, ">\n");
387 printf("CAN>TCP %s", rxmsg);
389 send(sa, rxmsg, strlen(rxmsg), 0);
393 if (FD_ISSET(sa, &readfds)) {
395 if (read(sa, txmsg+idx, 1) < 1) {
396 perror("read from tcp/ip socket");
407 /* max len is 4095*2 + '<' + '>' = 8192. The buffer index starts with 0 */
408 if (idx > MAX_PDU_LENGTH * 2 + 1) {
413 if (txmsg[idx] != '>') {
421 /* must be an even number of bytes and at least one data byte <XX> */
422 if (strlen(txmsg) < 4 || strlen(txmsg) % 2)
426 printf("TCP>CAN %s\n", txmsg);
428 nbytes = (strlen(txmsg)-2)/2;
429 if (b64hex(txmsg+1, msg, nbytes) == 0)
430 send(sc, msg, nbytes, 0);