isotpsniffer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * isotpsniffer.c - dump ISO15765-2 datagrams using PF_CAN isotp protocol
  3. *
  4. * Copyright (c) 2008 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <unistd.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <libgen.h>
  48. #include <time.h>
  49. #include <net/if.h>
  50. #include <sys/types.h>
  51. #include <sys/socket.h>
  52. #include <sys/ioctl.h>
  53. #include <linux/can.h>
  54. #include <linux/can/isotp.h>
  55. #include "terminal.h"
  56. #define NO_CAN_ID 0xFFFFFFFFU
  57. #define FORMAT_HEX 1
  58. #define FORMAT_ASCII 2
  59. #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
  60. void print_usage(char *prg)
  61. {
  62. fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
  63. fprintf(stderr, "Options: -s <can_id> (source can_id. Use 8 digits for extended IDs)\n");
  64. fprintf(stderr, " -d <can_id> (destination can_id. Use 8 digits for extended IDs)\n");
  65. fprintf(stderr, " -x <addr> (extended addressing mode)\n");
  66. fprintf(stderr, " -X <addr> (extended addressing mode - rx addr)\n");
  67. fprintf(stderr, " -c (color mode)\n");
  68. fprintf(stderr, " -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
  69. fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
  70. fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
  71. fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
  72. fprintf(stderr, "\n");
  73. }
  74. void printbuf(unsigned char *buffer, int nbytes, int color, int timestamp,
  75. int format, struct timeval *tv, struct timeval *last_tv,
  76. canid_t src, int socket, char *candevice, int head)
  77. {
  78. int i;
  79. if (color == 1)
  80. printf("%s", FGRED);
  81. if (color == 2)
  82. printf("%s", FGBLUE);
  83. if (timestamp) {
  84. ioctl(socket, SIOCGSTAMP, tv);
  85. switch (timestamp) {
  86. case 'a': /* absolute with timestamp */
  87. printf("(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
  88. break;
  89. case 'A': /* absolute with date */
  90. {
  91. struct tm tm;
  92. char timestring[25];
  93. tm = *localtime(&tv->tv_sec);
  94. strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
  95. printf("(%s.%06ld) ", timestring, tv->tv_usec);
  96. }
  97. break;
  98. case 'd': /* delta */
  99. case 'z': /* starting with zero */
  100. {
  101. struct timeval diff;
  102. if (last_tv->tv_sec == 0) /* first init */
  103. *last_tv = *tv;
  104. diff.tv_sec = tv->tv_sec - last_tv->tv_sec;
  105. diff.tv_usec = tv->tv_usec - last_tv->tv_usec;
  106. if (diff.tv_usec < 0)
  107. diff.tv_sec--, diff.tv_usec += 1000000;
  108. if (diff.tv_sec < 0)
  109. diff.tv_sec = diff.tv_usec = 0;
  110. printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
  111. if (timestamp == 'd')
  112. *last_tv = *tv; /* update for delta calculation */
  113. }
  114. break;
  115. default: /* no timestamp output */
  116. break;
  117. }
  118. }
  119. /* the source socket gets pdu data from the detination id */
  120. printf(" %s %03X [%d] ", candevice, src & CAN_EFF_MASK, nbytes);
  121. if (format & FORMAT_HEX) {
  122. for (i=0; i<nbytes; i++) {
  123. printf("%02X ", buffer[i]);
  124. if (head && i+1 >= head) {
  125. printf("... ");
  126. break;
  127. }
  128. }
  129. if (format & FORMAT_ASCII)
  130. printf(" - ");
  131. }
  132. if (format & FORMAT_ASCII) {
  133. printf("'");
  134. for (i=0; i<nbytes; i++) {
  135. if ((buffer[i] > 0x1F) && (buffer[i] < 0x7F))
  136. printf("%c", buffer[i]);
  137. else
  138. printf(".");
  139. if (head && i+1 >= head)
  140. break;
  141. }
  142. printf("'");
  143. if (head && i+1 >= head)
  144. printf(" ... ");
  145. }
  146. if (color)
  147. printf("%s", ATTRESET);
  148. printf("\n");
  149. fflush(stdout);
  150. }
  151. int main(int argc, char **argv)
  152. {
  153. fd_set rdfs;
  154. int s, t;
  155. struct sockaddr_can addr;
  156. char if_name[IFNAMSIZ];
  157. static struct can_isotp_options opts;
  158. int opt, quit = 0;
  159. int color = 0;
  160. int head = 0;
  161. int timestamp = 0;
  162. int format = FORMAT_DEFAULT;
  163. canid_t src = NO_CAN_ID;
  164. canid_t dst = NO_CAN_ID;
  165. extern int optind, opterr, optopt;
  166. static struct timeval tv, last_tv;
  167. unsigned char buffer[4096];
  168. int nbytes;
  169. while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:?")) != -1) {
  170. switch (opt) {
  171. case 's':
  172. src = strtoul(optarg, (char **)NULL, 16);
  173. if (strlen(optarg) > 7)
  174. src |= CAN_EFF_FLAG;
  175. break;
  176. case 'd':
  177. dst = strtoul(optarg, (char **)NULL, 16);
  178. if (strlen(optarg) > 7)
  179. dst |= CAN_EFF_FLAG;
  180. break;
  181. case 'x':
  182. opts.flags |= CAN_ISOTP_EXTEND_ADDR;
  183. opts.ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  184. break;
  185. case 'X':
  186. opts.flags |= CAN_ISOTP_RX_EXT_ADDR;
  187. opts.rx_ext_address = strtoul(optarg, (char **)NULL, 16) & 0xFF;
  188. break;
  189. case 'f':
  190. format = (atoi(optarg) & (FORMAT_ASCII | FORMAT_HEX));
  191. break;
  192. case 'h':
  193. head = atoi(optarg);
  194. break;
  195. case 'c':
  196. color = 1;
  197. break;
  198. case 't':
  199. timestamp = optarg[0];
  200. if ((timestamp != 'a') && (timestamp != 'A') &&
  201. (timestamp != 'd') && (timestamp != 'z')) {
  202. printf("%s: unknown timestamp mode '%c' - ignored\n",
  203. basename(argv[0]), optarg[0]);
  204. timestamp = 0;
  205. }
  206. break;
  207. case '?':
  208. print_usage(basename(argv[0]));
  209. exit(0);
  210. break;
  211. default:
  212. fprintf(stderr, "Unknown option %c\n", opt);
  213. print_usage(basename(argv[0]));
  214. exit(1);
  215. break;
  216. }
  217. }
  218. if ((argc - optind) != 1 || src == NO_CAN_ID || dst == NO_CAN_ID) {
  219. print_usage(basename(argv[0]));
  220. exit(1);
  221. }
  222. if ((opts.flags & CAN_ISOTP_RX_EXT_ADDR) && (!(opts.flags & CAN_ISOTP_EXTEND_ADDR))) {
  223. print_usage(basename(argv[0]));
  224. exit(1);
  225. }
  226. if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  227. perror("socket");
  228. exit(1);
  229. }
  230. if ((t = socket(PF_CAN, SOCK_DGRAM, CAN_ISOTP)) < 0) {
  231. perror("socket");
  232. exit(1);
  233. }
  234. opts.flags |= CAN_ISOTP_LISTEN_MODE;
  235. strncpy(if_name, argv[optind], IFNAMSIZ - 1);
  236. if_name[IFNAMSIZ - 1] = '\0';
  237. addr.can_family = AF_CAN;
  238. addr.can_ifindex = if_nametoindex(if_name);
  239. setsockopt(s, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  240. addr.can_addr.tp.tx_id = src;
  241. addr.can_addr.tp.rx_id = dst;
  242. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  243. perror("bind");
  244. close(s);
  245. exit(1);
  246. }
  247. if (opts.flags & CAN_ISOTP_RX_EXT_ADDR) {
  248. /* flip extended address info due to separate rx ext addr */
  249. __u8 tmpext;
  250. tmpext = opts.ext_address;
  251. opts.ext_address = opts.rx_ext_address;
  252. opts.rx_ext_address = tmpext;
  253. }
  254. setsockopt(t, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, &opts, sizeof(opts));
  255. addr.can_addr.tp.tx_id = dst;
  256. addr.can_addr.tp.rx_id = src;
  257. if (bind(t, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  258. perror("bind");
  259. close(s);
  260. exit(1);
  261. }
  262. while (!quit) {
  263. FD_ZERO(&rdfs);
  264. FD_SET(s, &rdfs);
  265. FD_SET(t, &rdfs);
  266. FD_SET(0, &rdfs);
  267. if ((nbytes = select(t+1, &rdfs, NULL, NULL, NULL)) < 0) {
  268. perror("select");
  269. continue;
  270. }
  271. if (FD_ISSET(0, &rdfs)) {
  272. getchar();
  273. quit = 1;
  274. printf("quit due to keyboard input.\n");
  275. }
  276. if (FD_ISSET(s, &rdfs)) {
  277. nbytes = read(s, buffer, 4096);
  278. if (nbytes < 0) {
  279. perror("read socket s");
  280. return -1;
  281. }
  282. if (nbytes > 4095)
  283. return -1;
  284. printbuf(buffer, nbytes, color?2:0, timestamp, format,
  285. &tv, &last_tv, dst, s, if_name, head);
  286. }
  287. if (FD_ISSET(t, &rdfs)) {
  288. nbytes = read(t, buffer, 4096);
  289. if (nbytes < 0) {
  290. perror("read socket t");
  291. return -1;
  292. }
  293. if (nbytes > 4095)
  294. return -1;
  295. printbuf(buffer, nbytes, color?1:0, timestamp, format,
  296. &tv, &last_tv, src, t, if_name, head);
  297. }
  298. }
  299. close(s);
  300. return 0;
  301. }