canlogserver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * canlogserver.c
  3. *
  4. * Copyright (c) 2002-2007 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 <unistd.h>
  46. #include <string.h>
  47. #include <signal.h>
  48. #include <ctype.h>
  49. #include <libgen.h>
  50. #include <time.h>
  51. #include <sys/time.h>
  52. #include <sys/wait.h>
  53. #include <sys/types.h>
  54. #include <sys/socket.h>
  55. #include <sys/ioctl.h>
  56. #include <sys/uio.h>
  57. #include <net/if.h>
  58. #include <netinet/in.h>
  59. #include <linux/can.h>
  60. #include <linux/can/raw.h>
  61. #include <signal.h>
  62. #include <errno.h>
  63. #include "lib.h"
  64. #define MAXDEV 6 /* change sscanf()'s manually if changed here */
  65. #define ANYDEV "any"
  66. #define ANL "\r\n" /* newline in ASC mode */
  67. #define COMMENTSZ 200
  68. #define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */
  69. #define DEFPORT 28700
  70. static char devname[MAXDEV][IFNAMSIZ+1];
  71. static int dindex[MAXDEV];
  72. static int max_devname_len;
  73. extern int optind, opterr, optopt;
  74. static volatile int running = 1;
  75. void print_usage(char *prg)
  76. {
  77. fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
  78. fprintf(stderr, " (use CTRL-C to terminate %s)\n\n", prg);
  79. fprintf(stderr, "Options: -m <mask> (ID filter mask. Default 0x00000000) *\n");
  80. fprintf(stderr, " -v <value> (ID filter value. Default 0x00000000) *\n");
  81. fprintf(stderr, " -i <0|1> (invert the specified ID filter) *\n");
  82. fprintf(stderr, " -e <emask> (mask for error frames)\n");
  83. fprintf(stderr, " -p <port> (listen on port <port>. Default: %d)\n", DEFPORT);
  84. fprintf(stderr, "\n");
  85. fprintf(stderr, "* The CAN ID filter matches, when ...\n");
  86. fprintf(stderr, " <received_can_id> & mask == value & mask\n");
  87. fprintf(stderr, "\n");
  88. fprintf(stderr, "When using more than one CAN interface the options\n");
  89. fprintf(stderr, "m/v/i/e have comma separated values e.g. '-m 0,7FF,0'\n");
  90. fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n\n", ANYDEV);
  91. }
  92. int idx2dindex(int ifidx, int socket)
  93. {
  94. int i;
  95. struct ifreq ifr;
  96. for (i=0; i<MAXDEV; i++) {
  97. if (dindex[i] == ifidx)
  98. return i;
  99. }
  100. /* create new interface index cache entry */
  101. /* remove index cache zombies first */
  102. for (i=0; i < MAXDEV; i++) {
  103. if (dindex[i]) {
  104. ifr.ifr_ifindex = dindex[i];
  105. if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
  106. dindex[i] = 0;
  107. }
  108. }
  109. for (i=0; i < MAXDEV; i++)
  110. if (!dindex[i]) /* free entry */
  111. break;
  112. if (i == MAXDEV) {
  113. printf("Interface index cache only supports %d interfaces.\n", MAXDEV);
  114. exit(1);
  115. }
  116. dindex[i] = ifidx;
  117. ifr.ifr_ifindex = ifidx;
  118. if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
  119. perror("SIOCGIFNAME");
  120. if (max_devname_len < strlen(ifr.ifr_name))
  121. max_devname_len = strlen(ifr.ifr_name);
  122. strcpy(devname[i], ifr.ifr_name);
  123. #ifdef DEBUG
  124. printf("new index %d (%s)\n", i, devname[i]);
  125. #endif
  126. return i;
  127. }
  128. /*
  129. * This is a Signalhandler. When we get a signal, that a child
  130. * terminated, we wait for it, so the zombie will disappear.
  131. */
  132. void childdied(int i)
  133. {
  134. wait(NULL);
  135. }
  136. /*
  137. * This is a Signalhandler for a cought SIGTERM
  138. */
  139. void shutdown_gra(int i)
  140. {
  141. exit(0);
  142. }
  143. int main(int argc, char **argv)
  144. {
  145. struct sigaction signalaction;
  146. sigset_t sigset;
  147. fd_set rdfs;
  148. int s[MAXDEV];
  149. int socki, accsocket;
  150. canid_t mask[MAXDEV] = {0};
  151. canid_t value[MAXDEV] = {0};
  152. int inv_filter[MAXDEV] = {0};
  153. can_err_mask_t err_mask[MAXDEV] = {0};
  154. int opt, ret;
  155. int currmax = 1; /* we assume at least one can bus ;-) */
  156. struct sockaddr_can addr;
  157. struct can_filter rfilter;
  158. struct canfd_frame frame;
  159. const int canfd_on = 1;
  160. int nbytes, i, j, maxdlen;
  161. struct ifreq ifr;
  162. struct timeval tv;
  163. int port = DEFPORT;
  164. struct sockaddr_in inaddr;
  165. struct sockaddr_in clientaddr;
  166. socklen_t sin_size = sizeof(clientaddr);
  167. char temp[BUFSZ];
  168. sigemptyset(&sigset);
  169. signalaction.sa_handler = &childdied;
  170. signalaction.sa_mask = sigset;
  171. signalaction.sa_flags = 0;
  172. sigaction(SIGCHLD, &signalaction, NULL); /* install signal for dying child */
  173. signalaction.sa_handler = &shutdown_gra;
  174. signalaction.sa_mask = sigset;
  175. signalaction.sa_flags = 0;
  176. sigaction(SIGTERM, &signalaction, NULL); /* install Signal for termination */
  177. sigaction(SIGINT, &signalaction, NULL); /* install Signal for termination */
  178. while ((opt = getopt(argc, argv, "m:v:i:e:p:?")) != -1) {
  179. switch (opt) {
  180. case 'm':
  181. i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
  182. &mask[0], &mask[1], &mask[2],
  183. &mask[3], &mask[4], &mask[5]);
  184. if (i > currmax)
  185. currmax = i;
  186. break;
  187. case 'v':
  188. i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
  189. &value[0], &value[1], &value[2],
  190. &value[3], &value[4], &value[5]);
  191. if (i > currmax)
  192. currmax = i;
  193. break;
  194. case 'i':
  195. i = sscanf(optarg, "%d,%d,%d,%d,%d,%d",
  196. &inv_filter[0], &inv_filter[1], &inv_filter[2],
  197. &inv_filter[3], &inv_filter[4], &inv_filter[5]);
  198. if (i > currmax)
  199. currmax = i;
  200. break;
  201. case 'e':
  202. i = sscanf(optarg, "%x,%x,%x,%x,%x,%x",
  203. &err_mask[0], &err_mask[1], &err_mask[2],
  204. &err_mask[3], &err_mask[4], &err_mask[5]);
  205. if (i > currmax)
  206. currmax = i;
  207. break;
  208. case 'p':
  209. port = atoi(optarg);
  210. break;
  211. default:
  212. print_usage(basename(argv[0]));
  213. exit(1);
  214. break;
  215. }
  216. }
  217. if (optind == argc) {
  218. print_usage(basename(argv[0]));
  219. exit(0);
  220. }
  221. /* count in options higher than device count ? */
  222. if (optind + currmax > argc) {
  223. printf("low count of CAN devices!\n");
  224. return 1;
  225. }
  226. currmax = argc - optind; /* find real number of CAN devices */
  227. if (currmax > MAXDEV) {
  228. printf("More than %d CAN devices!\n", MAXDEV);
  229. return 1;
  230. }
  231. socki = socket(PF_INET, SOCK_STREAM, 0);
  232. if (socki < 0) {
  233. perror("socket");
  234. exit(1);
  235. }
  236. inaddr.sin_family = AF_INET;
  237. inaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  238. inaddr.sin_port = htons(port);
  239. while(bind(socki, (struct sockaddr*)&inaddr, sizeof(inaddr)) < 0) {
  240. printf(".");fflush(NULL);
  241. usleep(100000);
  242. }
  243. if (listen(socki, 3) != 0) {
  244. perror("listen");
  245. exit(1);
  246. }
  247. while(1) {
  248. accsocket = accept(socki, (struct sockaddr*)&clientaddr, &sin_size);
  249. if (accsocket > 0) {
  250. //printf("accepted\n");
  251. if (!fork())
  252. break;
  253. else
  254. close(accsocket);
  255. }
  256. else if (errno != EINTR) {
  257. perror("accept");
  258. exit(1);
  259. }
  260. }
  261. for (i=0; i<currmax; i++) {
  262. #ifdef DEBUG
  263. printf("open %d '%s' m%08X v%08X i%d e%d.\n",
  264. i, argv[optind+i], mask[i], value[i],
  265. inv_filter[i], err_mask[i]);
  266. #endif
  267. if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
  268. perror("socket");
  269. return 1;
  270. }
  271. if (mask[i] || value[i]) {
  272. printf("CAN ID filter[%d] for %s set to "
  273. "mask = %08X, value = %08X %s\n",
  274. i, argv[optind+i], mask[i], value[i],
  275. (inv_filter[i]) ? "(inv_filter)" : "");
  276. rfilter.can_id = value[i];
  277. rfilter.can_mask = mask[i];
  278. if (inv_filter[i])
  279. rfilter.can_id |= CAN_INV_FILTER;
  280. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
  281. &rfilter, sizeof(rfilter));
  282. }
  283. if (err_mask[i])
  284. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
  285. &err_mask[i], sizeof(err_mask[i]));
  286. /* try to switch the socket into CAN FD mode */
  287. setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
  288. j = strlen(argv[optind+i]);
  289. if (!(j < IFNAMSIZ)) {
  290. printf("name of CAN device '%s' is too long!\n", argv[optind+i]);
  291. return 1;
  292. }
  293. if (j > max_devname_len)
  294. max_devname_len = j; /* for nice printing */
  295. addr.can_family = AF_CAN;
  296. if (strcmp(ANYDEV, argv[optind+i])) {
  297. strcpy(ifr.ifr_name, argv[optind+i]);
  298. if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
  299. perror("SIOCGIFINDEX");
  300. exit(1);
  301. }
  302. addr.can_ifindex = ifr.ifr_ifindex;
  303. }
  304. else
  305. addr.can_ifindex = 0; /* any can interface */
  306. if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  307. perror("bindcan");
  308. return 1;
  309. }
  310. }
  311. while (running) {
  312. FD_ZERO(&rdfs);
  313. for (i=0; i<currmax; i++)
  314. FD_SET(s[i], &rdfs);
  315. if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
  316. //perror("select");
  317. running = 0;
  318. continue;
  319. }
  320. for (i=0; i<currmax; i++) { /* check all CAN RAW sockets */
  321. if (FD_ISSET(s[i], &rdfs)) {
  322. socklen_t len = sizeof(addr);
  323. int idx;
  324. if ((nbytes = recvfrom(s[i], &frame, CANFD_MTU, 0,
  325. (struct sockaddr*)&addr, &len)) < 0) {
  326. perror("read");
  327. return 1;
  328. }
  329. if ((size_t)nbytes == CAN_MTU)
  330. maxdlen = CAN_MAX_DLEN;
  331. else if ((size_t)nbytes == CANFD_MTU)
  332. maxdlen = CANFD_MAX_DLEN;
  333. else {
  334. fprintf(stderr, "read: incomplete CAN frame\n");
  335. return 1;
  336. }
  337. if (ioctl(s[i], SIOCGSTAMP, &tv) < 0)
  338. perror("SIOCGSTAMP");
  339. idx = idx2dindex(addr.can_ifindex, s[i]);
  340. sprintf(temp, "(%ld.%06ld) %*s ",
  341. tv.tv_sec, tv.tv_usec, max_devname_len, devname[idx]);
  342. sprint_canframe(temp+strlen(temp), &frame, 0, maxdlen);
  343. strcat(temp, "\n");
  344. if (write(accsocket, temp, strlen(temp)) < 0) {
  345. perror("writeaccsock");
  346. return 1;
  347. }
  348. #if 0
  349. /* print CAN frame in log file style to stdout */
  350. printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
  351. printf("%*s ", max_devname_len, devname[idx]);
  352. fprint_canframe(stdout, &frame, "\n", 0, maxdlen);
  353. #endif
  354. }
  355. }
  356. }
  357. for (i=0; i<currmax; i++)
  358. close(s[i]);
  359. close(accsocket);
  360. return 0;
  361. }