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 int b64hex(char *asc, unsigned char *bin, int len)
84 for (i = 0; i < len; i++) {
85 if (!sscanf(asc+(i*2), "%2hhx", bin+i))
96 void print_usage(char *prg)
98 fprintf(stderr, "\nUsage: %s -l <port> -s <can_id> -d <can_id> [options] <CAN interface>\n", prg);
99 fprintf(stderr, "Options: (* = mandatory)\n");
100 fprintf(stderr, "\n");
101 fprintf(stderr, "ip adressing:\n");
102 fprintf(stderr, " * -l <port> (local port for the server)\n");
103 fprintf(stderr, "\n");
104 fprintf(stderr, "isotp adressing:\n");
105 fprintf(stderr, " * -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
106 fprintf(stderr, " * -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
107 fprintf(stderr, " -x <addr> (extended addressing mode)\n");
108 fprintf(stderr, "\n");
109 fprintf(stderr, "padding:\n");
110 fprintf(stderr, " -p <byte> (set and enable tx padding byte)\n");
111 fprintf(stderr, " -r <byte> (set and enable rx padding byte)\n");
112 fprintf(stderr, " -P <mode> (check padding in SF/CF. (l)ength (c)ontent (a)ll)\n");
113 fprintf(stderr, "\n");
114 fprintf(stderr, "rx path: (config, which is sent to the sender / data source)\n");
115 fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
116 fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
117 fprintf(stderr, " -w <num> (max. wait frame transmissions)\n");
118 fprintf(stderr, "\n");
119 fprintf(stderr, "tx path: (config, which changes local tx settings)\n");
120 fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
121 fprintf(stderr, "\n");
122 fprintf(stderr, "All values except for '-l' and '-t' are expected in hexadecimal values.\n");
123 fprintf(stderr, "\n");
126 int main(int argc, char **argv)
128 extern int optind, opterr, optopt;
131 int sl, sa, sc; /* (L)isten, (A)ccept, (C)AN sockets */
132 struct sockaddr_in saddr, clientaddr;
133 struct sockaddr_can caddr;
134 static struct can_isotp_options opts;
135 static struct can_isotp_fc_options fcopts;
137 socklen_t sin_size = sizeof(clientaddr);
138 socklen_t caddrlen = sizeof(caddr);
140 struct sigaction signalaction;
151 int idx = 0; /* index in txmsg[] */
153 unsigned char msg[4096]; /* isotp socket message buffer (4095 + test_for_too_long_byte)*/
154 char rxmsg[8194]; /* isotp->tcp ASCII message buffer (4095*2 + < > \n null) */
155 char txmsg[8193]; /* tcp->isotp ASCII message buffer (4095*2 + < > null) */
157 /* mark missing mandatory commandline options as missing */
158 caddr.can_addr.tp.tx_id = caddr.can_addr.tp.rx_id = NO_CAN_ID;
160 while ((opt = getopt(argc, argv, "l:s:d:x:p:r:P:b:m:w:t:v?")) != -1) {
163 local_port = strtoul(optarg, (char **)NULL, 10);
167 caddr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
168 if (strlen(optarg) > 7)
169 caddr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
173 caddr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
174 if (strlen(optarg) > 7)
175 caddr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
179 opts.flags |= CAN_ISOTP_EXTEND_ADDR;
180 opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
184 opts.flags |= CAN_ISOTP_TX_PADDING;
185 opts.txpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
189 opts.flags |= CAN_ISOTP_RX_PADDING;
190 opts.rxpad_content = strtoul(optarg, (char **)NULL, 16) & 0xFF;
194 if (optarg[0] == 'l')
195 opts.flags |= CAN_ISOTP_CHK_PAD_LEN;
196 else if (optarg[0] == 'c')
197 opts.flags |= CAN_ISOTP_CHK_PAD_DATA;
198 else if (optarg[0] == 'a')
199 opts.flags |= (CAN_ISOTP_CHK_PAD_DATA | CAN_ISOTP_CHK_PAD_DATA);
201 printf("unknown padding check option '%c'.\n", optarg[0]);
202 print_usage(basename(argv[0]));
208 fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
212 fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
216 fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
220 opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
228 print_usage(basename(argv[0]));
233 fprintf(stderr, "Unknown option %c\n", opt);
234 print_usage(basename(argv[0]));
240 if ((argc - optind != 1) || (local_port == 0) ||
241 (caddr.can_addr.tp.tx_id == NO_CAN_ID) ||
242 (caddr.can_addr.tp.rx_id == NO_CAN_ID)) {
243 print_usage(basename(argv[0]));
247 sigemptyset(&sigset);
248 signalaction.sa_handler = &childdied;
249 signalaction.sa_mask = sigset;
250 signalaction.sa_flags = 0;
251 sigaction(SIGCHLD, &signalaction, NULL); /* signal for dying child */
253 if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
254 perror("inetsocket");
258 saddr.sin_family = AF_INET;
259 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
260 saddr.sin_port = htons(local_port);
262 while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
268 if (listen(sl, 3) != 0) {
274 sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
283 if (errno != EINTR) {
285 * If the cause for the error was NOT the
286 * signal from a dying child => give an error
294 if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
299 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
300 setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
302 caddr.can_family = AF_CAN;
303 strcpy(ifr.ifr_name, argv[optind]);
304 if (ioctl(sc, SIOCGIFINDEX, &ifr) < 0) {
305 perror("SIOCGIFINDEX");
308 caddr.can_ifindex = ifr.ifr_ifindex;
310 if (bind(sc, (struct sockaddr *)&caddr, caddrlen) < 0) {
318 FD_SET(sc, &readfds);
319 FD_SET(sa, &readfds);
321 select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
323 if (FD_ISSET(sc, &readfds)) {
326 nbytes = read(sc, &msg, 4096);
328 if (nbytes < 1 || nbytes > 4095) {
329 perror("read from isotp socket");
335 for ( i = 0; i < nbytes; i++)
336 sprintf(rxmsg + 1 + 2*i, "%02X", msg[i]);
338 /* finalize string for sending */
339 strcat(rxmsg, ">\n");
342 printf("CAN>TCP %s", rxmsg);
344 send(sa, rxmsg, strlen(rxmsg), 0);
348 if (FD_ISSET(sa, &readfds)) {
350 if (read(sa, txmsg+idx, 1) < 1) {
351 perror("read from tcp/ip socket");
362 /* max len is 4095*2 + '<' + '>' = 8192. The buffer index starts with 0 */
368 if (txmsg[idx] != '>') {
376 /* must be an even number of bytes and at least one data byte <XX> */
377 if (strlen(txmsg) < 4 || strlen(txmsg) % 2)
381 printf("TCP>CAN %s\n", txmsg);
383 nbytes = (strlen(txmsg)-2)/2;
384 if (b64hex(txmsg+1, msg, nbytes) == 0)
385 send(sc, msg, nbytes, 0);