You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
23 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * candump.c
  3. *
  4. * Copyright (c) 2002-2009 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 <stdlib.h>
  45. #include <stdint.h>
  46. #include <unistd.h>
  47. #include <string.h>
  48. #include <signal.h>
  49. #include <ctype.h>
  50. #include <libgen.h>
  51. #include <time.h>
  52. #include <errno.h>
  53. #include <sys/time.h>
  54. #include <sys/types.h>
  55. #include <sys/socket.h>
  56. #include <sys/ioctl.h>
  57. #include <sys/uio.h>
  58. #include <net/if.h>
  59. #include <linux/can.h>
  60. #include <linux/can/raw.h>
  61. #include "terminal.h"
  62. #include "lib.h"
  63. #define MAXSOCK 16 /* max. number of CAN interfaces given on the cmdline */
  64. #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
  65. #define MAXCOL 6 /* number of different colors for colorized output */
  66. #define ANYDEV "any" /* name of interface to receive from any CAN interface */
  67. #define ANL "\r\n" /* newline in ASC mode */
  68. #define SILENT_INI 42 /* detect user setting on commandline */
  69. #define SILENT_OFF 0 /* no silent mode */
  70. #define SILENT_ANI 1 /* silent mode with animation */
  71. #define SILENT_ON 2 /* silent mode (completely silent) */
  72. #define BOLD ATTBOLD
  73. #define RED ATTBOLD FGRED
  74. #define GREEN ATTBOLD FGGREEN
  75. #define YELLOW ATTBOLD FGYELLOW
  76. #define BLUE ATTBOLD FGBLUE
  77. #define MAGENTA ATTBOLD FGMAGENTA
  78. #define CYAN ATTBOLD FGCYAN
  79. const char col_on [MAXCOL][19] = {BLUE, RED, GREEN, BOLD, MAGENTA, CYAN};
  80. const char col_off [] = ATTRESET;
  81. static char *cmdlinename[MAXSOCK];
  82. static __u32 dropcnt[MAXSOCK];
  83. static __u32 last_dropcnt[MAXSOCK];
  84. static char devname[MAXIFNAMES][IFNAMSIZ+1];
  85. static int dindex[MAXIFNAMES];
  86. static int max_devname_len; /* to prevent frazzled device name output */
  87. const int canfd_on = 1;
  88. #define MAXANI 4
  89. const char anichar[MAXANI] = {'|', '/', '-', '\\'};
  90. const char extra_m_info[4][4] = {"- -", "B -", "- E", "B E"};
  91. extern int optind, opterr, optopt;
  92. static volatile int running = 1;
  93. void print_usage(char *prg)
  94. {
  95. fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
  96. fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
  97. fprintf(stderr, "Options: -t <type> (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
  98. fprintf(stderr, " -c (increment color mode level)\n");
  99. fprintf(stderr, " -i (binary output - may exceed 80 chars/line)\n");
  100. fprintf(stderr, " -a (enable additional ASCII output)\n");
  101. fprintf(stderr, " -S (swap byte order in printed CAN data[] - marked with '%c' )\n", SWAP_DELIMITER);
  102. fprintf(stderr, " -s <level> (silent mode - %d: off (default) %d: animation %d: silent)\n", SILENT_OFF, SILENT_ANI, SILENT_ON);
  103. fprintf(stderr, " -b <can> (bridge mode - send received frames to <can>)\n");
  104. fprintf(stderr, " -B <can> (bridge mode - like '-b' with disabled loopback)\n");
  105. fprintf(stderr, " -u <usecs> (delay bridge forwarding by <usecs> microseconds)\n");
  106. fprintf(stderr, " -l (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON);
  107. fprintf(stderr, " -L (use log file format on stdout)\n");
  108. fprintf(stderr, " -n <count> (terminate after receiption of <count> CAN frames)\n");
  109. fprintf(stderr, " -r <size> (set socket receive buffer to <size>)\n");
  110. fprintf(stderr, " -D (Don't exit if a \"detected\" can device goes down.\n");
  111. fprintf(stderr, " -d (monitor dropped CAN frames)\n");
  112. fprintf(stderr, " -e (dump CAN error frames in human-readable format)\n");
  113. fprintf(stderr, " -x (print extra message infos, rx/tx brs esi)\n");
  114. fprintf(stderr, " -T <msecs> (terminate after <msecs> without any reception)\n");
  115. fprintf(stderr, "\n");
  116. fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
  117. fprintf(stderr, "on the commandline in the form: <ifname>[,filter]*\n");
  118. fprintf(stderr, "\nComma separated filters can be specified for each given CAN interface:\n");
  119. fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
  120. fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
  121. fprintf(stderr, " #<error_mask> (set error frame filter, see include/linux/can/error.h)\n");
  122. fprintf(stderr, " [j|J] (join the given CAN filters - logical AND semantic)\n");
  123. fprintf(stderr, "\nCAN IDs, masks and data content are given and expected in hexadecimal values.\n");
  124. fprintf(stderr, "When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.\n");
  125. fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n");
  126. fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n", ANYDEV);
  127. fprintf(stderr, "\nExamples:\n");
  128. fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n", prg);
  129. fprintf(stderr, "%s -l any,0~0,#FFFFFFFF (log only error frames but no(!) data frames)\n", prg);
  130. fprintf(stderr, "%s -l any,0:0,#FFFFFFFF (log error frames and also all data frames)\n", prg);
  131. fprintf(stderr, "%s vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)\n", prg);
  132. fprintf(stderr, "%s vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)\n", prg);
  133. fprintf(stderr, "%s vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)\n", prg);
  134. fprintf(stderr, "\n");
  135. }
  136. void sigterm(int signo)
  137. {
  138. running = 0;
  139. }
  140. int idx2dindex(int ifidx, int socket) {
  141. int i;
  142. struct ifreq ifr;
  143. for (i=0; i < MAXIFNAMES; i++) {
  144. if (dindex[i] == ifidx)
  145. return i;
  146. }
  147. /* create new interface index cache entry */
  148. /* remove index cache zombies first */
  149. for (i=0; i < MAXIFNAMES; i++) {
  150. if (dindex[i]) {
  151. ifr.ifr_ifindex = dindex[i];
  152. if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
  153. dindex[i] = 0;
  154. }
  155. }
  156. for (i=0; i < MAXIFNAMES; i++)
  157. if (!dindex[i]) /* free entry */
  158. break;
  159. if (i == MAXIFNAMES) {
  160. fprintf(stderr, "Interface index cache only supports %d interfaces.\n",
  161. MAXIFNAMES);
  162. exit(1);
  163. }
  164. dindex[i] = ifidx;
  165. ifr.ifr_ifindex = ifidx;
  166. if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
  167. perror("SIOCGIFNAME");
  168. if (max_devname_len < strlen(ifr.ifr_name))
  169. max_devname_len = strlen(ifr.ifr_name);
  170. strcpy(devname[i], ifr.ifr_name);
  171. #ifdef DEBUG
  172. printf("new index %d (%s)\n", i, devname[i]);
  173. #endif
  174. return i;
  175. }
  176. int main(int argc, char **argv)
  177. {
  178. fd_set rdfs;
  179. int s[MAXSOCK];
  180. int bridge = 0;
  181. useconds_t bridge_delay = 0;
  182. unsigned char timestamp = 0;
  183. unsigned char down_causes_exit = 1;
  184. unsigned char dropmonitor = 0;
  185. unsigned char extra_msg_info = 0;
  186. unsigned char silent = SILENT_INI;
  187. unsigned char silentani = 0;
  188. unsigned char color = 0;
  189. unsigned char view = 0;
  190. unsigned char log = 0;
  191. unsigned char logfrmt = 0;
  192. int count = 0;
  193. int rcvbuf_size = 0;
  194. int opt, ret;
  195. int currmax, numfilter;
  196. int join_filter;
  197. char *ptr, *nptr;
  198. struct sockaddr_can addr;
  199. char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
  200. struct iovec iov;
  201. struct msghdr msg;
  202. struct cmsghdr *cmsg;
  203. struct can_filter *rfilter;
  204. can_err_mask_t err_mask;
  205. struct canfd_frame frame;
  206. int nbytes, i, maxdlen;
  207. struct ifreq ifr;
  208. struct timeval tv, last_tv;
  209. struct timeval timeout, timeout_config = { 0, 0 }, *timeout_current = NULL;
  210. FILE *logfile = NULL;
  211. signal(SIGTERM, sigterm);
  212. signal(SIGHUP, sigterm);
  213. signal(SIGINT, sigterm);
  214. last_tv.tv_sec = 0;
  215. last_tv.tv_usec = 0;
  216. while ((opt = getopt(argc, argv, "t:ciaSs:b:B:u:lDdxLn:r:heT:?")) != -1) {
  217. switch (opt) {
  218. case 't':
  219. timestamp = optarg[0];
  220. if ((timestamp != 'a') && (timestamp != 'A') &&
  221. (timestamp != 'd') && (timestamp != 'z')) {
  222. fprintf(stderr, "%s: unknown timestamp mode '%c' - ignored\n",
  223. basename(argv[0]), optarg[0]);
  224. timestamp = 0;
  225. }
  226. break;
  227. case 'c':
  228. color++;
  229. break;
  230. case 'i':
  231. view |= CANLIB_VIEW_BINARY;
  232. break;
  233. case 'a':
  234. view |= CANLIB_VIEW_ASCII;
  235. break;
  236. case 'S':
  237. view |= CANLIB_VIEW_SWAP;
  238. break;
  239. case 'e':
  240. view |= CANLIB_VIEW_ERROR;
  241. break;
  242. case 's':
  243. silent = atoi(optarg);
  244. if (silent > SILENT_ON) {
  245. print_usage(basename(argv[0]));
  246. exit(1);
  247. }
  248. break;
  249. case 'b':
  250. case 'B':
  251. if (strlen(optarg) >= IFNAMSIZ) {
  252. fprintf(stderr, "Name of CAN device '%s' is too long!\n\n", optarg);
  253. return 1;
  254. } else {
  255. bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  256. if (bridge < 0) {
  257. perror("bridge socket");
  258. return 1;
  259. }
  260. addr.can_family = AF_CAN;
  261. strcpy(ifr.ifr_name, optarg);
  262. if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
  263. perror("SIOCGIFINDEX");
  264. addr.can_ifindex = ifr.ifr_ifindex;
  265. if (!addr.can_ifindex) {
  266. perror("invalid bridge interface");
  267. return 1;
  268. }
  269. /* disable default receive filter on this write-only RAW socket */
  270. setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  271. if (opt == 'B') {
  272. const int loopback = 0;
  273. setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
  274. &loopback, sizeof(loopback));
  275. }
  276. if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  277. perror("bridge bind");
  278. return 1;
  279. }
  280. }
  281. break;
  282. case 'u':
  283. bridge_delay = (useconds_t)strtoul(optarg, (char **)NULL, 10);
  284. break;
  285. case 'l':
  286. log = 1;
  287. break;
  288. case 'D':
  289. down_causes_exit = 0;
  290. break;
  291. case 'd':
  292. dropmonitor = 1;
  293. break;
  294. case 'x':
  295. extra_msg_info = 1;
  296. break;
  297. case 'L':
  298. logfrmt = 1;
  299. break;
  300. case 'n':
  301. count = atoi(optarg);
  302. if (count < 1) {
  303. print_usage(basename(argv[0]));
  304. exit(1);
  305. }
  306. break;
  307. case 'r':
  308. rcvbuf_size = atoi(optarg);
  309. if (rcvbuf_size < 1) {
  310. print_usage(basename(argv[0]));
  311. exit(1);
  312. }
  313. break;
  314. case 'T':
  315. errno = 0;
  316. timeout_config.tv_usec = strtol(optarg, NULL, 0);
  317. if (errno != 0) {
  318. print_usage(basename(argv[0]));
  319. exit(1);
  320. }
  321. timeout_config.tv_sec = timeout_config.tv_usec / 1000;
  322. timeout_config.tv_usec = (timeout_config.tv_usec % 1000) * 1000;
  323. timeout_current = &timeout;
  324. break;
  325. default:
  326. print_usage(basename(argv[0]));
  327. exit(1);
  328. break;
  329. }
  330. }
  331. if (optind == argc) {
  332. print_usage(basename(argv[0]));
  333. exit(0);
  334. }
  335. if (logfrmt && view) {
  336. fprintf(stderr, "Log file format selected: Please disable ASCII/BINARY/SWAP options!\n");
  337. exit(0);
  338. }
  339. if (silent == SILENT_INI) {
  340. if (log) {
  341. fprintf(stderr, "Disabled standard output while logging.\n");
  342. silent = SILENT_ON; /* disable output on stdout */
  343. } else
  344. silent = SILENT_OFF; /* default output */
  345. }
  346. currmax = argc - optind; /* find real number of CAN devices */
  347. if (currmax > MAXSOCK) {
  348. fprintf(stderr, "More than %d CAN devices given on commandline!\n", MAXSOCK);
  349. return 1;
  350. }
  351. for (i=0; i < currmax; i++) {
  352. ptr = argv[optind+i];
  353. nptr = strchr(ptr, ',');
  354. #ifdef DEBUG
  355. printf("open %d '%s'.\n", i, ptr);
  356. #endif
  357. s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  358. if (s[i] < 0) {
  359. perror("socket");
  360. return 1;
  361. }
  362. cmdlinename[i] = ptr; /* save pointer to cmdline name of this socket */
  363. if (nptr)
  364. nbytes = nptr - ptr; /* interface name is up the first ',' */
  365. else
  366. nbytes = strlen(ptr); /* no ',' found => no filter definitions */
  367. if (nbytes >= IFNAMSIZ) {
  368. fprintf(stderr, "name of CAN device '%s' is too long!\n", ptr);
  369. return 1;
  370. }
  371. if (nbytes > max_devname_len)
  372. max_devname_len = nbytes; /* for nice printing */
  373. addr.can_family = AF_CAN;
  374. memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
  375. strncpy(ifr.ifr_name, ptr, nbytes);
  376. #ifdef DEBUG
  377. printf("using interface name '%s'.\n", ifr.ifr_name);
  378. #endif
  379. if (strcmp(ANYDEV, ifr.ifr_name)) {
  380. if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
  381. perror("SIOCGIFINDEX");
  382. exit(1);
  383. }
  384. addr.can_ifindex = ifr.ifr_ifindex;
  385. } else
  386. addr.can_ifindex = 0; /* any can interface */
  387. if (nptr) {
  388. /* found a ',' after the interface name => check for filters */
  389. /* determine number of filters to alloc the filter space */
  390. numfilter = 0;
  391. ptr = nptr;
  392. while (ptr) {
  393. numfilter++;
  394. ptr++; /* hop behind the ',' */
  395. ptr = strchr(ptr, ','); /* exit condition */
  396. }
  397. rfilter = malloc(sizeof(struct can_filter) * numfilter);
  398. if (!rfilter) {
  399. fprintf(stderr, "Failed to create filter space!\n");
  400. return 1;
  401. }
  402. numfilter = 0;
  403. err_mask = 0;
  404. join_filter = 0;
  405. 599147048, Гиорги Циклаури....
  406. while (nptr) {
  407. ptr = nptr+1; /* hop behind the ',' */
  408. nptr = strchr(ptr, ','); /* update exit condition */
  409. if (sscanf(ptr, "%x:%x",
  410. &rfilter[numfilter].can_id,
  411. &rfilter[numfilter].can_mask) == 2) {
  412. rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
  413. numfilter++;
  414. } else if (sscanf(ptr, "%x~%x",
  415. &rfilter[numfilter].can_id,
  416. &rfilter[numfilter].can_mask) == 2) {
  417. rfilter[numfilter].can_id |= CAN_INV_FILTER;
  418. rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
  419. numfilter++;
  420. } else if (*ptr == 'j' || *ptr == 'J') {
  421. join_filter = 1;
  422. } else if (sscanf(ptr, "#%x", &err_mask) != 1) {
  423. fprintf(stderr, "Error in filter option parsing: '%s'\n", ptr);
  424. return 1;
  425. }
  426. }
  427. if (err_mask)
  428. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
  429. &err_mask, sizeof(err_mask));
  430. if (join_filter && setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_JOIN_FILTERS,
  431. &join_filter, sizeof(join_filter)) < 0) {
  432. perror("setsockopt CAN_RAW_JOIN_FILTERS not supported by your Linux Kernel");
  433. return 1;
  434. }
  435. if (numfilter)
  436. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
  437. rfilter, numfilter * sizeof(struct can_filter));
  438. free(rfilter);
  439. } /* if (nptr) */
  440. /* try to switch the socket into CAN FD mode */
  441. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  442. if (rcvbuf_size) {
  443. int curr_rcvbuf_size;
  444. socklen_t curr_rcvbuf_size_len = sizeof(curr_rcvbuf_size);
  445. /* try SO_RCVBUFFORCE first, if we run with CAP_NET_ADMIN */
  446. if (setsockopt(s[i], SOL_SOCKET, SO_RCVBUFFORCE,
  447. &rcvbuf_size, sizeof(rcvbuf_size)) < 0) {
  448. #ifdef DEBUG
  449. printf("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n");
  450. #endif
  451. if (setsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
  452. &rcvbuf_size, sizeof(rcvbuf_size)) < 0) {
  453. perror("setsockopt SO_RCVBUF");
  454. return 1;
  455. }
  456. if (getsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
  457. &curr_rcvbuf_size, &curr_rcvbuf_size_len) < 0) {
  458. perror("getsockopt SO_RCVBUF");
  459. return 1;
  460. }
  461. /* Only print a warning the first time we detect the adjustment */
  462. /* n.b.: The wanted size is doubled in Linux in net/sore/sock.c */
  463. if (!i && curr_rcvbuf_size < rcvbuf_size*2)
  464. fprintf(stderr, "The socket receive buffer size was "
  465. "adjusted due to /proc/sys/net/core/rmem_max.\n");
  466. }
  467. }
  468. if (timestamp || log || logfrmt) {
  469. const int timestamp_on = 1;
  470. if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
  471. &timestamp_on, sizeof(timestamp_on)) < 0) {
  472. perror("setsockopt SO_TIMESTAMP");
  473. return 1;
  474. }
  475. }
  476. if (dropmonitor) {
  477. const int dropmonitor_on = 1;
  478. if (setsockopt(s[i], SOL_SOCKET, SO_RXQ_OVFL,
  479. &dropmonitor_on, sizeof(dropmonitor_on)) < 0) {
  480. perror("setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
  481. return 1;
  482. }
  483. }
  484. if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  485. perror("bind");
  486. return 1;
  487. }
  488. }
  489. if (log) {
  490. time_t currtime;
  491. struct tm now;
  492. char fname[sizeof("candump-2006-11-20_202026.log")+1];
  493. if (time(&currtime) == (time_t)-1) {
  494. perror("time");
  495. return 1;
  496. }
  497. localtime_r(&currtime, &now);
  498. sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
  499. now.tm_year + 1900,
  500. now.tm_mon + 1,
  501. now.tm_mday,
  502. now.tm_hour,
  503. now.tm_min,
  504. now.tm_sec);
  505. if (silent != SILENT_ON)
  506. printf("\nWarning: console output active while logging!");
  507. fprintf(stderr, "\nEnabling Logfile '%s'\n\n", fname);
  508. logfile = fopen(fname, "w");
  509. if (!logfile) {
  510. perror("logfile");
  511. return 1;
  512. }
  513. }
  514. /* these settings are static and can be held out of the hot path */
  515. iov.iov_base = &frame;
  516. msg.msg_name = &addr;
  517. msg.msg_iov = &iov;
  518. msg.msg_iovlen = 1;
  519. msg.msg_control = &ctrlmsg;
  520. while (running) {
  521. FD_ZERO(&rdfs);
  522. for (i=0; i<currmax; i++)
  523. FD_SET(s[i], &rdfs);
  524. if (timeout_current)
  525. *timeout_current = timeout_config;
  526. if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, timeout_current)) <= 0) {
  527. //perror("select");
  528. running = 0;
  529. continue;
  530. }
  531. for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
  532. if (FD_ISSET(s[i], &rdfs)) {
  533. int idx;
  534. /* these settings may be modified by recvmsg() */
  535. iov.iov_len = sizeof(frame);
  536. msg.msg_namelen = sizeof(addr);
  537. msg.msg_controllen = sizeof(ctrlmsg);
  538. msg.msg_flags = 0;
  539. nbytes = recvmsg(s[i], &msg, 0);
  540. idx = idx2dindex(addr.can_ifindex, s[i]);
  541. if (nbytes < 0) {
  542. if ((errno == ENETDOWN) && !down_causes_exit) {
  543. fprintf(stderr, "%s: interface down\n", devname[idx]);
  544. continue;
  545. }
  546. perror("read");
  547. return 1;
  548. }
  549. if ((size_t)nbytes == CAN_MTU)
  550. maxdlen = CAN_MAX_DLEN;
  551. else if ((size_t)nbytes == CANFD_MTU)
  552. maxdlen = CANFD_MAX_DLEN;
  553. else {
  554. fprintf(stderr, "read: incomplete CAN frame\n");
  555. return 1;
  556. }
  557. if (count && (--count == 0))
  558. running = 0;
  559. if (bridge) {
  560. if (bridge_delay)
  561. usleep(bridge_delay);
  562. nbytes = write(bridge, &frame, nbytes);
  563. if (nbytes < 0) {
  564. perror("bridge write");
  565. return 1;
  566. } else if ((size_t)nbytes != CAN_MTU && (size_t)nbytes != CANFD_MTU) {
  567. fprintf(stderr,"bridge write: incomplete CAN frame\n");
  568. return 1;
  569. }
  570. }
  571. for (cmsg = CMSG_FIRSTHDR(&msg);
  572. cmsg && (cmsg->cmsg_level == SOL_SOCKET);
  573. cmsg = CMSG_NXTHDR(&msg,cmsg)) {
  574. if (cmsg->cmsg_type == SO_TIMESTAMP)
  575. memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
  576. else if (cmsg->cmsg_type == SO_RXQ_OVFL)
  577. memcpy(&dropcnt[i], CMSG_DATA(cmsg), sizeof(__u32));
  578. }
  579. /* check for (unlikely) dropped frames on this specific socket */
  580. if (dropcnt[i] != last_dropcnt[i]) {
  581. __u32 frames = dropcnt[i] - last_dropcnt[i];
  582. if (silent != SILENT_ON)
  583. printf("DROPCOUNT: dropped %d CAN frame%s on '%s' socket (total drops %d)\n",
  584. frames, (frames > 1)?"s":"", devname[idx], dropcnt[i]);
  585. if (log)
  586. fprintf(logfile, "DROPCOUNT: dropped %d CAN frame%s on '%s' socket (total drops %d)\n",
  587. frames, (frames > 1)?"s":"", devname[idx], dropcnt[i]);
  588. last_dropcnt[i] = dropcnt[i];
  589. }
  590. /* once we detected a EFF frame indent SFF frames accordingly */
  591. if (frame.can_id & CAN_EFF_FLAG)
  592. view |= CANLIB_VIEW_INDENT_SFF;
  593. if (log) {
  594. char buf[CL_CFSZ]; /* max length */
  595. /* log CAN frame with absolute timestamp & device */
  596. sprint_canframe(buf, &frame, 0, maxdlen);
  597. fprintf(logfile, "(%010ld.%06ld) %*s %s\n",
  598. tv.tv_sec, tv.tv_usec,
  599. max_devname_len, devname[idx], buf);
  600. }
  601. if (logfrmt) {
  602. char buf[CL_CFSZ]; /* max length */
  603. /* print CAN frame in log file style to stdout */
  604. sprint_canframe(buf, &frame, 0, maxdlen);
  605. printf("(%010ld.%06ld) %*s %s\n",
  606. tv.tv_sec, tv.tv_usec,
  607. max_devname_len, devname[idx], buf);
  608. goto out_fflush; /* no other output to stdout */
  609. }
  610. if (silent != SILENT_OFF){
  611. if (silent == SILENT_ANI) {
  612. printf("%c\b", anichar[silentani%=MAXANI]);
  613. silentani++;
  614. }
  615. goto out_fflush; /* no other output to stdout */
  616. }
  617. printf(" %s", (color>2)?col_on[idx%MAXCOL]:"");
  618. switch (timestamp) {
  619. case 'a': /* absolute with timestamp */
  620. printf("(%010ld.%06ld) ", tv.tv_sec, tv.tv_usec);
  621. break;
  622. case 'A': /* absolute with date */
  623. {
  624. struct tm tm;
  625. char timestring[25];
  626. tm = *localtime(&tv.tv_sec);
  627. strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
  628. printf("(%s.%06ld) ", timestring, tv.tv_usec);
  629. }
  630. break;
  631. case 'd': /* delta */
  632. case 'z': /* starting with zero */
  633. {
  634. struct timeval diff;
  635. if (last_tv.tv_sec == 0) /* first init */
  636. last_tv = tv;
  637. diff.tv_sec = tv.tv_sec - last_tv.tv_sec;
  638. diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
  639. if (diff.tv_usec < 0)
  640. diff.tv_sec--, diff.tv_usec += 1000000;
  641. if (diff.tv_sec < 0)
  642. diff.tv_sec = diff.tv_usec = 0;
  643. printf("(%03ld.%06ld) ", diff.tv_sec, diff.tv_usec);
  644. if (timestamp == 'd')
  645. last_tv = tv; /* update for delta calculation */
  646. }
  647. break;
  648. default: /* no timestamp output */
  649. break;
  650. }
  651. printf(" %s", (color && (color<3))?col_on[idx%MAXCOL]:"");
  652. printf("%*s", max_devname_len, devname[idx]);
  653. if (extra_msg_info) {
  654. if (msg.msg_flags & MSG_DONTROUTE)
  655. printf (" TX %s", extra_m_info[frame.flags & 3]);
  656. else
  657. printf (" RX %s", extra_m_info[frame.flags & 3]);
  658. }
  659. printf("%s ", (color==1)?col_off:"");
  660. fprint_long_canframe(stdout, &frame, NULL, view, maxdlen);
  661. printf("%s", (color>1)?col_off:"");
  662. printf("\n");
  663. }
  664. out_fflush:
  665. fflush(stdout);
  666. }
  667. }
  668. for (i=0; i<currmax; i++)
  669. close(s[i]);
  670. if (bridge)
  671. close(bridge);
  672. if (log)
  673. fclose(logfile);
  674. return 0;
  675. }