isotpserver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * isotpserver.c
  3. *
  4. * Implements a socket server which understands ASCII HEX
  5. * messages for simple TCP/IP <-> ISO 15765-2 bridging.
  6. *
  7. * General message format: <[data]+>
  8. *
  9. * e.g. for an eight bytes PDU
  10. *
  11. * <1122334455667788>
  12. *
  13. * Valid ISO 15625-2 PDUs have a length from 1-4095 bytes.
  14. *
  15. * Authors:
  16. * Andre Naujoks (the socket server stuff)
  17. * Oliver Hartkopp (the rest)
  18. *
  19. * Copyright (c) 2002-2010 Volkswagen Group Electronic Research
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  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.
  33. *
  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.
  38. *
  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.
  41. *
  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
  53. * DAMAGE.
  54. *
  55. * Send feedback to <linux-can@vger.kernel.org>
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <libgen.h>
  61. #include <unistd.h>
  62. #include <string.h>
  63. #include <signal.h>
  64. #include <errno.h>
  65. #include <sys/types.h>
  66. #include <sys/wait.h>
  67. #include <sys/socket.h>
  68. #include <sys/ioctl.h>
  69. #include <sys/uio.h>
  70. #include <net/if.h>
  71. #include <netinet/in.h>
  72. #include <linux/can.h>
  73. #include <linux/can/isotp.h>
  74. #define NO_CAN_ID 0xFFFFFFFFU
  75. /* allow PDUs greater 4095 bytes according ISO 15765-2:2015 */
  76. #define MAX_PDU_LENGTH 6000
  77. int b64hex(char *asc, unsigned char *bin, int len)
  78. {
  79. int i;
  80. for (i = 0; i < len; i++) {
  81. if (!sscanf(asc+(i*2), "%2hhx", bin+i))
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. void childdied(int i)
  87. {
  88. wait(NULL);
  89. }
  90. void print_usage(char *prg)
  91. {
  92. fprintf(stderr, "\nUsage: %s -l <port> -s <can_id> -d <can_id> [options] <CAN interface>\n", prg);
  93. fprintf(stderr, "Options: (* = mandatory)\n");
  94. fprintf(stderr, "\n");
  95. fprintf(stderr, "ip adressing:\n");
  96. fprintf(stderr, " * -l <port> (local port for the server)\n");
  97. fprintf(stderr, "\n");
  98. fprintf(stderr, "isotp adressing:\n");
  99. fprintf(stderr, " * -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
  100. fprintf(stderr, " * -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
  101. fprintf(stderr, " -x <addr>[:<rxaddr>] (extended addressing / opt. separate rxaddr)\n");
  102. fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
  103. fprintf(stderr, "\n");
  104. fprintf(stderr, "padding:\n");
  105. fprintf(stderr, " -p [tx]:[rx] (set and enable tx/rx padding bytes)\n");
  106. fprintf(stderr, " -P <mode> (check rx padding for (l)ength (c)ontent (a)ll)\n");
  107. fprintf(stderr, "\n");
  108. fprintf(stderr, "rx path: (config, which is sent to the sender / data source)\n");
  109. fprintf(stderr, " -b <bs> (blocksize. 0 = off)\n");
  110. fprintf(stderr, " -m <val> (STmin in ms/ns. See spec.)\n");
  111. fprintf(stderr, " -w <num> (max. wait frame transmissions)\n");
  112. fprintf(stderr, "\n");
  113. fprintf(stderr, "tx path: (config, which changes local tx settings)\n");
  114. fprintf(stderr, " -t <time ns> (transmit time in nanosecs)\n");
  115. fprintf(stderr, "\n");
  116. fprintf(stderr, "All values except for '-l' and '-t' are expected in hexadecimal values.\n");
  117. fprintf(stderr, "\n");
  118. }
  119. int main(int argc, char **argv)
  120. {
  121. extern int optind, opterr, optopt;
  122. int opt;
  123. int sl, sa, sc; /* (L)isten, (A)ccept, (C)AN sockets */
  124. struct sockaddr_in saddr, clientaddr;
  125. struct sockaddr_can caddr;
  126. static struct can_isotp_options opts;
  127. static struct can_isotp_fc_options fcopts;
  128. static struct can_isotp_ll_options llopts;
  129. socklen_t sin_size = sizeof(clientaddr);
  130. socklen_t caddrlen = sizeof(caddr);
  131. struct sigaction signalaction;
  132. sigset_t sigset;
  133. fd_set readfds;
  134. int i;
  135. int nbytes;
  136. int local_port = 0;
  137. int verbose = 0;
  138. int idx = 0; /* index in txmsg[] */
  139. unsigned char msg[MAX_PDU_LENGTH + 1]; /* isotp socket message buffer (4095 + test_for_too_long_byte)*/
  140. char rxmsg[MAX_PDU_LENGTH * 2 + 4]; /* isotp->tcp ASCII message buffer (4095*2 + < > \n null) */
  141. char txmsg[MAX_PDU_LENGTH * 2 + 3]; /* tcp->isotp ASCII message buffer (4095*2 + < > null) */
  142. /* mark missing mandatory commandline options as missing */
  143. caddr.can_addr.tp.tx_id = caddr.can_addr.tp.rx_id = NO_CAN_ID;
  144. while ((opt = getopt(argc, argv, "l:s:d:x:p:P:b:m:w:t:L:v?")) != -1) {
  145. switch (opt) {
  146. case 'l':
  147. local_port = strtoul(optarg, (char **)NULL, 10);
  148. break;
  149. case 's':
  150. caddr.can_addr.tp.tx_id = strtoul(optarg, (char **)NULL, 16);
  151. if (strlen(optarg) > 7)
  152. caddr.can_addr.tp.tx_id |= CAN_EFF_FLAG;
  153. break;
  154. case 'd':
  155. caddr.can_addr.tp.rx_id = strtoul(optarg, (char **)NULL, 16);
  156. if (strlen(optarg) > 7)
  157. caddr.can_addr.tp.rx_id |= CAN_EFF_FLAG;
  158. break;
  159. case 'x':
  160. {
  161. int elements = sscanf(optarg, "%hhx:%hhx",
  162. &opts.ext_address,
  163. &opts.rx_ext_address);
  164. if (elements == 1)
  165. opts.flags |= CAN_ISOTP_EXTEND_ADDR;
  166. else if (elements == 2)
  167. opts.flags |= (CAN_ISOTP_EXTEND_ADDR | CAN_ISOTP_RX_EXT_ADDR);
  168. else {
  169. printf("incorrect extended addr values '%s'.\n", optarg);
  170. print_usage(basename(argv[0]));
  171. exit(0);
  172. }
  173. break;
  174. }
  175. case 'p':
  176. {
  177. int elements = sscanf(optarg, "%hhx:%hhx",
  178. &opts.txpad_content,
  179. &opts.rxpad_content);
  180. if (elements == 1)
  181. opts.flags |= CAN_ISOTP_TX_PADDING;
  182. else if (elements == 2)
  183. opts.flags |= (CAN_ISOTP_TX_PADDING | CAN_ISOTP_RX_PADDING);
  184. else if (sscanf(optarg, ":%hhx", &opts.rxpad_content) == 1)
  185. opts.flags |= CAN_ISOTP_RX_PADDING;
  186. else {
  187. printf("incorrect padding values '%s'.\n", optarg);
  188. print_usage(basename(argv[0]));
  189. exit(0);
  190. }
  191. break;
  192. }
  193. case 'P':
  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_LEN | CAN_ISOTP_CHK_PAD_DATA);
  200. else {
  201. printf("unknown padding check option '%c'.\n", optarg[0]);
  202. print_usage(basename(argv[0]));
  203. exit(0);
  204. }
  205. break;
  206. case 'b':
  207. fcopts.bs = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  208. break;
  209. case 'm':
  210. fcopts.stmin = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  211. break;
  212. case 'w':
  213. fcopts.wftmax = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  214. break;
  215. case 't':
  216. opts.frame_txtime = strtoul(optarg, (char **)NULL, 10);
  217. break;
  218. case 'L':
  219. if (sscanf(optarg, "%hhu:%hhu:%hhu",
  220. &llopts.mtu,
  221. &llopts.tx_dl,
  222. &llopts.tx_flags) != 3) {
  223. printf("unknown link layer options '%s'.\n", optarg);
  224. print_usage(basename(argv[0]));
  225. exit(0);
  226. }
  227. break;
  228. case 'v':
  229. verbose = 1;
  230. break;
  231. case '?':
  232. print_usage(basename(argv[0]));
  233. exit(0);
  234. break;
  235. default:
  236. fprintf(stderr, "Unknown option %c\n", opt);
  237. print_usage(basename(argv[0]));
  238. exit(1);
  239. break;
  240. }
  241. }
  242. if ((argc - optind != 1) || (local_port == 0) ||
  243. (caddr.can_addr.tp.tx_id == NO_CAN_ID) ||
  244. (caddr.can_addr.tp.rx_id == NO_CAN_ID)) {
  245. print_usage(basename(argv[0]));
  246. exit(1);
  247. }
  248. sigemptyset(&sigset);
  249. signalaction.sa_handler = &childdied;
  250. signalaction.sa_mask = sigset;
  251. signalaction.sa_flags = 0;
  252. sigaction(SIGCHLD, &signalaction, NULL); /* signal for dying child */
  253. if((sl = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
  254. perror("inetsocket");
  255. exit(1);
  256. }
  257. saddr.sin_family = AF_INET;
  258. saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  259. saddr.sin_port = htons(local_port);
  260. while(bind(sl,(struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
  261. printf(".");
  262. fflush(NULL);
  263. usleep(100000);
  264. }
  265. if (listen(sl, 3) != 0) {
  266. perror("listen");
  267. exit(1);
  268. }
  269. while (1) {
  270. sa = accept(sl,(struct sockaddr *)&clientaddr, &sin_size);
  271. if (sa > 0 ){
  272. if (fork())
  273. close(sa);
  274. else
  275. break;
  276. }
  277. else {
  278. if (errno != EINTR) {
  279. /*
  280. * If the cause for the error was NOT the
  281. * signal from a dying child => give an error
  282. */
  283. perror("accept");
  284. exit(1);
  285. }
  286. }
  287. }
  288. if ((sc = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  289. perror("socket");
  290. exit(1);
  291. }
  292. setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  293. setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts));
  294. if (llopts.tx_dl) {
  295. if (setsockopt(sc, SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, &llopts, sizeof(llopts)) < 0) {
  296. perror("link layer sockopt");
  297. exit(1);
  298. }
  299. }
  300. caddr.can_family = AF_CAN;
  301. caddr.can_ifindex = if_nametoindex(argv[optind]);
  302. if (bind(sc, (struct sockaddr *)&caddr, caddrlen) < 0) {
  303. perror("bind");
  304. exit(1);
  305. }
  306. while (1) {
  307. FD_ZERO(&readfds);
  308. FD_SET(sc, &readfds);
  309. FD_SET(sa, &readfds);
  310. select((sc > sa)?sc+1:sa+1, &readfds, NULL, NULL, NULL);
  311. if (FD_ISSET(sc, &readfds)) {
  312. nbytes = read(sc, &msg, MAX_PDU_LENGTH + 1);
  313. if (nbytes < 1 || nbytes > MAX_PDU_LENGTH) {
  314. perror("read from isotp socket");
  315. exit(1);
  316. }
  317. rxmsg[0] = '<';
  318. for ( i = 0; i < nbytes; i++)
  319. sprintf(rxmsg + 1 + 2*i, "%02X", msg[i]);
  320. /* finalize string for sending */
  321. strcat(rxmsg, ">\n");
  322. if (verbose)
  323. printf("CAN>TCP %s", rxmsg);
  324. send(sa, rxmsg, strlen(rxmsg), 0);
  325. }
  326. if (FD_ISSET(sa, &readfds)) {
  327. if (read(sa, txmsg+idx, 1) < 1) {
  328. perror("read from tcp/ip socket");
  329. exit(1);
  330. }
  331. if (!idx) {
  332. if (txmsg[0] == '<')
  333. idx = 1;
  334. continue;
  335. }
  336. /* max len is 4095*2 + '<' + '>' = 8192. The buffer index starts with 0 */
  337. if (idx > MAX_PDU_LENGTH * 2 + 1) {
  338. idx = 0;
  339. continue;
  340. }
  341. if (txmsg[idx] != '>') {
  342. idx++;
  343. continue;
  344. }
  345. txmsg[idx+1] = 0;
  346. idx = 0;
  347. /* must be an even number of bytes and at least one data byte <XX> */
  348. if (strlen(txmsg) < 4 || strlen(txmsg) % 2)
  349. continue;
  350. if (verbose)
  351. printf("TCP>CAN %s\n", txmsg);
  352. nbytes = (strlen(txmsg)-2)/2;
  353. if (b64hex(txmsg+1, msg, nbytes) == 0)
  354. send(sc, msg, nbytes, 0);
  355. }
  356. }
  357. close(sc);
  358. close(sa);
  359. return 0;
  360. }