]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - candump.c
slcan_attach: Added new commandline option to specify the created netdevice name.
[sojka/can-utils.git] / candump.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * candump.c
7  *
8  * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <signal.h>
53 #include <ctype.h>
54 #include <libgen.h>
55 #include <time.h>
56
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <sys/uio.h>
62 #include <net/if.h>
63
64 #include <linux/can.h>
65 #include <linux/can/raw.h>
66
67 #include "terminal.h"
68 #include "lib.h"
69
70 #define MAXSOCK 16    /* max. number of CAN interfaces given on the cmdline */
71 #define MAXIFNAMES 30 /* size of receive name index to omit ioctls */
72 #define MAXCOL 6      /* number of different colors for colorized output */
73 #define ANYDEV "any"  /* name of interface to receive from any CAN interface */
74 #define ANL "\r\n"    /* newline in ASC mode */
75
76 #define SILENT_INI 42 /* detect user setting on commandline */
77 #define SILENT_OFF 0  /* no silent mode */
78 #define SILENT_ANI 1  /* silent mode with animation */
79 #define SILENT_ON  2  /* silent mode (completely silent) */
80
81 #define BOLD    ATTBOLD
82 #define RED     ATTBOLD FGRED
83 #define GREEN   ATTBOLD FGGREEN
84 #define YELLOW  ATTBOLD FGYELLOW
85 #define BLUE    ATTBOLD FGBLUE
86 #define MAGENTA ATTBOLD FGMAGENTA
87 #define CYAN    ATTBOLD FGCYAN
88
89 const char col_on [MAXCOL][19] = {BLUE, RED, GREEN, BOLD, MAGENTA, CYAN};
90 const char col_off [] = ATTRESET;
91
92 static char *cmdlinename[MAXSOCK];
93 static __u32 dropcnt[MAXSOCK];
94 static __u32 last_dropcnt[MAXSOCK];
95 static char devname[MAXIFNAMES][IFNAMSIZ+1];
96 static int  dindex[MAXIFNAMES];
97 static int  max_devname_len; /* to prevent frazzled device name output */ 
98
99 #define MAXANI 4
100 const char anichar[MAXANI] = {'|', '/', '-', '\\'};
101
102 extern int optind, opterr, optopt;
103
104 static volatile int running = 1;
105
106 void print_usage(char *prg)
107 {
108         fprintf(stderr, "\nUsage: %s [options] <CAN interface>+\n", prg);
109         fprintf(stderr, "  (use CTRL-C to terminate %s)\n\n", prg);
110         fprintf(stderr, "Options: -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)\n");
111         fprintf(stderr, "         -c          (increment color mode level)\n");
112         fprintf(stderr, "         -i          (binary output - may exceed 80 chars/line)\n");
113         fprintf(stderr, "         -a          (enable additional ASCII output)\n");
114         fprintf(stderr, "         -S          (swap byte order in printed CAN data[] - marked with '%c' )\n", SWAP_DELIMITER);
115         fprintf(stderr, "         -s <level>  (silent mode - %d: off (default) %d: animation %d: silent)\n", SILENT_OFF, SILENT_ANI, SILENT_ON);
116         fprintf(stderr, "         -b <can>    (bridge mode - send received frames to <can>)\n");
117         fprintf(stderr, "         -B <can>    (bridge mode - like '-b' with disabled loopback)\n");
118         fprintf(stderr, "         -l          (log CAN-frames into file. Sets '-s %d' by default)\n", SILENT_ON);
119         fprintf(stderr, "         -L          (use log file format on stdout)\n");
120         fprintf(stderr, "         -n <count>  (terminate after receiption of <count> CAN frames)\n");
121         fprintf(stderr, "         -r <size>   (set socket receive buffer to <size>)\n");
122         fprintf(stderr, "         -d          (monitor dropped CAN frames)\n");
123         fprintf(stderr, "\n");
124         fprintf(stderr, "Up to %d CAN interfaces with optional filter sets can be specified\n", MAXSOCK);
125         fprintf(stderr, "on the commandline in the form: <ifname>[,filter]*\n");
126         fprintf(stderr, "\nComma separated filters can be specified for each given CAN interface:\n");
127         fprintf(stderr, " <can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)\n");
128         fprintf(stderr, " <can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)\n");
129         fprintf(stderr, " #<error_mask>       (set error frame filter, see include/linux/can/error.h)\n");
130         fprintf(stderr, "\nCAN IDs, masks and data content are given and expected in hexadecimal values.\n");
131         fprintf(stderr, "When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.\n");
132         fprintf(stderr, "Without any given filter all data frames are received ('0:0' default filter).\n");
133         fprintf(stderr, "\nUse interface name '%s' to receive from all CAN interfaces.\n", ANYDEV);
134         fprintf(stderr, "\nExamples:\n");
135         fprintf(stderr, "%s -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8\n", prg);
136         fprintf(stderr, "%s -l any,0~0,#FFFFFFFF    (log only error frames but no(!) data frames)\n", prg);
137         fprintf(stderr, "%s -l any,0:0,#FFFFFFFF    (log error frames and also all data frames)\n", prg);
138         fprintf(stderr, "%s vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)\n", prg);
139         fprintf(stderr, "%s vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)\n", prg);
140         fprintf(stderr, "%s vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)\n", prg);
141         fprintf(stderr, "\n");
142 }
143
144 void sigterm(int signo)
145 {
146         running = 0;
147 }
148
149 int idx2dindex(int ifidx, int socket) {
150
151         int i;
152         struct ifreq ifr;
153
154         for (i=0; i < MAXIFNAMES; i++) {
155                 if (dindex[i] == ifidx)
156                         return i;
157         }
158
159         /* create new interface index cache entry */
160
161         /* remove index cache zombies first */
162         for (i=0; i < MAXIFNAMES; i++) {
163                 if (dindex[i]) {
164                         ifr.ifr_ifindex = dindex[i];
165                         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
166                                 dindex[i] = 0;
167                 }
168         }
169
170         for (i=0; i < MAXIFNAMES; i++)
171                 if (!dindex[i]) /* free entry */
172                         break;
173
174         if (i == MAXIFNAMES) {
175                 fprintf(stderr, "Interface index cache only supports %d interfaces.\n",
176                        MAXIFNAMES);
177                 exit(1);
178         }
179
180         dindex[i] = ifidx;
181
182         ifr.ifr_ifindex = ifidx;
183         if (ioctl(socket, SIOCGIFNAME, &ifr) < 0)
184                 perror("SIOCGIFNAME");
185
186         if (max_devname_len < strlen(ifr.ifr_name))
187                 max_devname_len = strlen(ifr.ifr_name);
188
189         strcpy(devname[i], ifr.ifr_name);
190
191 #ifdef DEBUG
192         printf("new index %d (%s)\n", i, devname[i]);
193 #endif
194
195         return i;
196 }
197
198 int main(int argc, char **argv)
199 {
200         fd_set rdfs;
201         int s[MAXSOCK];
202         int bridge = 0;
203         unsigned char timestamp = 0;
204         unsigned char dropmonitor = 0;
205         unsigned char silent = SILENT_INI;
206         unsigned char silentani = 0;
207         unsigned char color = 0;
208         unsigned char view = 0;
209         unsigned char log = 0;
210         unsigned char logfrmt = 0;
211         int count = 0;
212         int rcvbuf_size = 0;
213         int opt, ret;
214         int currmax, numfilter;
215         char *ptr, *nptr;
216         struct sockaddr_can addr;
217         char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))];
218         struct iovec iov;
219         struct msghdr msg;
220         struct cmsghdr *cmsg;
221         struct can_filter *rfilter;
222         can_err_mask_t err_mask;
223         struct can_frame frame;
224         int nbytes, i;
225         struct ifreq ifr;
226         struct timeval tv, last_tv;
227         FILE *logfile = NULL;
228
229         signal(SIGTERM, sigterm);
230         signal(SIGHUP, sigterm);
231         signal(SIGINT, sigterm);
232
233         last_tv.tv_sec  = 0;
234         last_tv.tv_usec = 0;
235
236         while ((opt = getopt(argc, argv, "t:ciaSs:b:B:ldLn:r:h?")) != -1) {
237                 switch (opt) {
238                 case 't':
239                         timestamp = optarg[0];
240                         if ((timestamp != 'a') && (timestamp != 'A') &&
241                             (timestamp != 'd') && (timestamp != 'z')) {
242                                 fprintf(stderr, "%s: unknown timestamp mode '%c' - ignored\n",
243                                        basename(argv[0]), optarg[0]);
244                                 timestamp = 0;
245                         }
246                         break;
247
248                 case 'c':
249                         color++;
250                         break;
251
252                 case 'i':
253                         view |= CANLIB_VIEW_BINARY;
254                         break;
255
256                 case 'a':
257                         view |= CANLIB_VIEW_ASCII;
258                         break;
259
260                 case 'S':
261                         view |= CANLIB_VIEW_SWAP;
262                         break;
263
264                 case 's':
265                         silent = atoi(optarg);
266                         if (silent > SILENT_ON) {
267                                 print_usage(basename(argv[0]));
268                                 exit(1);
269                         }
270                         break;
271
272                 case 'b':
273                 case 'B':
274                         if (strlen(optarg) >= IFNAMSIZ) {
275                                 fprintf(stderr, "Name of CAN device '%s' is too long!\n\n", optarg);
276                                 return 1;
277                         } else {
278                                 bridge = socket(PF_CAN, SOCK_RAW, CAN_RAW);
279                                 if (bridge < 0) {
280                                         perror("bridge socket");
281                                         return 1;
282                                 }
283                                 addr.can_family = AF_CAN;
284                                 strcpy(ifr.ifr_name, optarg);
285                                 if (ioctl(bridge, SIOCGIFINDEX, &ifr) < 0)
286                                         perror("SIOCGIFINDEX");
287                                 addr.can_ifindex = ifr.ifr_ifindex;
288                 
289                                 if (!addr.can_ifindex) {
290                                         perror("invalid bridge interface");
291                                         return 1;
292                                 }
293
294                                 /* disable default receive filter on this write-only RAW socket */
295                                 setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
296
297                                 if (opt == 'B') {
298                                         const int loopback = 0;
299
300                                         setsockopt(bridge, SOL_CAN_RAW, CAN_RAW_LOOPBACK,
301                                                    &loopback, sizeof(loopback));
302                                 }
303
304                                 if (bind(bridge, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
305                                         perror("bridge bind");
306                                         return 1;
307                                 }
308                         }
309                         break;
310             
311                 case 'l':
312                         log = 1;
313                         break;
314
315                 case 'd':
316                         dropmonitor = 1;
317                         break;
318
319                 case 'L':
320                         logfrmt = 1;
321                         break;
322
323                 case 'n':
324                         count = atoi(optarg);
325                         if (count < 1) {
326                                 print_usage(basename(argv[0]));
327                                 exit(1);
328                         }
329                         break;
330
331                 case 'r':
332                         rcvbuf_size = atoi(optarg);
333                         if (rcvbuf_size < 1) {
334                                 print_usage(basename(argv[0]));
335                                 exit(1);
336                         }
337                         break;
338
339                 default:
340                         print_usage(basename(argv[0]));
341                         exit(1);
342                         break;
343                 }
344         }
345
346         if (optind == argc) {
347                 print_usage(basename(argv[0]));
348                 exit(0);
349         }
350         
351         if (logfrmt && view) {
352                 fprintf(stderr, "Log file format selected: Please disable ASCII/BINARY/SWAP options!\n");
353                 exit(0);
354         }
355
356         if (silent == SILENT_INI) {
357                 if (log) {
358                         fprintf(stderr, "Disabled standard output while logging.\n");
359                         silent = SILENT_ON; /* disable output on stdout */
360                 } else
361                         silent = SILENT_OFF; /* default output */
362         }
363
364         currmax = argc - optind; /* find real number of CAN devices */
365
366         if (currmax > MAXSOCK) {
367                 fprintf(stderr, "More than %d CAN devices given on commandline!\n", MAXSOCK);
368                 return 1;
369         }
370
371         for (i=0; i < currmax; i++) {
372
373                 ptr = argv[optind+i];
374                 nptr = strchr(ptr, ',');
375
376 #ifdef DEBUG
377                 printf("open %d '%s'.\n", i, ptr);
378 #endif
379
380                 s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW);
381                 if (s[i] < 0) {
382                         perror("socket");
383                         return 1;
384                 }
385
386                 cmdlinename[i] = ptr; /* save pointer to cmdline name of this socket */
387
388                 if (nptr)
389                         nbytes = nptr - ptr;  /* interface name is up the first ',' */
390                 else
391                         nbytes = strlen(ptr); /* no ',' found => no filter definitions */
392
393                 if (nbytes >= IFNAMSIZ) {
394                         fprintf(stderr, "name of CAN device '%s' is too long!\n", ptr);
395                         return 1;
396                 }
397
398                 if (nbytes > max_devname_len)
399                         max_devname_len = nbytes; /* for nice printing */
400
401                 addr.can_family = AF_CAN;
402
403                 memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name));
404                 strncpy(ifr.ifr_name, ptr, nbytes);
405
406 #ifdef DEBUG
407                 printf("using interface name '%s'.\n", ifr.ifr_name);
408 #endif
409
410                 if (strcmp(ANYDEV, ifr.ifr_name)) {
411                         if (ioctl(s[i], SIOCGIFINDEX, &ifr) < 0) {
412                                 perror("SIOCGIFINDEX");
413                                 exit(1);
414                         }
415                         addr.can_ifindex = ifr.ifr_ifindex;
416                 } else
417                         addr.can_ifindex = 0; /* any can interface */
418
419                 if (nptr) {
420
421                         /* found a ',' after the interface name => check for filters */
422
423                         /* determine number of filters to alloc the filter space */
424                         numfilter = 0;
425                         ptr = nptr;
426                         while (ptr) {
427                                 numfilter++;
428                                 ptr++; /* hop behind the ',' */
429                                 ptr = strchr(ptr, ','); /* exit condition */
430                         }
431
432                         rfilter = malloc(sizeof(struct can_filter) * numfilter);
433                         if (!rfilter) {
434                                 fprintf(stderr, "Failed to create filter space!\n");
435                                 return 1;
436                         }
437
438                         numfilter = 0;
439                         err_mask = 0;
440
441                         while (nptr) {
442
443                                 ptr = nptr+1; /* hop behind the ',' */
444                                 nptr = strchr(ptr, ','); /* update exit condition */
445
446                                 if (sscanf(ptr, "%x:%x",
447                                            &rfilter[numfilter].can_id, 
448                                            &rfilter[numfilter].can_mask) == 2) {
449                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
450                                         numfilter++;
451                                 } else if (sscanf(ptr, "%x~%x",
452                                                   &rfilter[numfilter].can_id, 
453                                                   &rfilter[numfilter].can_mask) == 2) {
454                                         rfilter[numfilter].can_id |= CAN_INV_FILTER;
455                                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
456                                         numfilter++;
457                                 } else if (sscanf(ptr, "#%x", &err_mask) != 1) { 
458                                         fprintf(stderr, "Error in filter option parsing: '%s'\n", ptr);
459                                         return 1;
460                                 }
461                         }
462
463                         if (err_mask)
464                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
465                                            &err_mask, sizeof(err_mask));
466
467                         if (numfilter)
468                                 setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
469                                            rfilter, numfilter * sizeof(struct can_filter));
470
471                         free(rfilter);
472
473                 } /* if (nptr) */
474
475                 if (rcvbuf_size) {
476
477                         int curr_rcvbuf_size;
478                         socklen_t curr_rcvbuf_size_len = sizeof(curr_rcvbuf_size);
479
480                         /* try SO_RCVBUFFORCE first, if we run with CAP_NET_ADMIN */
481                         if (setsockopt(s[i], SOL_SOCKET, SO_RCVBUFFORCE,
482                                        &rcvbuf_size, sizeof(rcvbuf_size)) < 0) {
483 #ifdef DEBUG
484                                 printf("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n");
485 #endif
486                                 if (setsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
487                                                &rcvbuf_size, sizeof(rcvbuf_size)) < 0) {
488                                         perror("setsockopt SO_RCVBUF");
489                                         return 1;
490                                 }
491
492                                 if (getsockopt(s[i], SOL_SOCKET, SO_RCVBUF,
493                                                &curr_rcvbuf_size, &curr_rcvbuf_size_len) < 0) {
494                                         perror("getsockopt SO_RCVBUF");
495                                         return 1;
496                                 }
497
498                                 /* Only print a warning the first time we detect the adjustment */
499                                 /* n.b.: The wanted size is doubled in Linux in net/sore/sock.c */
500                                 if (!i && curr_rcvbuf_size < rcvbuf_size*2)
501                                         fprintf(stderr, "The socket receive buffer size was "
502                                                 "adjusted due to /proc/sys/net/core/rmem_max.\n");
503                         }
504                 }
505
506                 if (timestamp || log || logfrmt) {
507
508                         const int timestamp_on = 1;
509
510                         if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
511                                        &timestamp_on, sizeof(timestamp_on)) < 0) {
512                                 perror("setsockopt SO_TIMESTAMP");
513                                 return 1;
514                         }
515                 }
516
517                 if (dropmonitor) {
518
519                         const int dropmonitor_on = 1;
520
521                         if (setsockopt(s[i], SOL_SOCKET, SO_RXQ_OVFL,
522                                        &dropmonitor_on, sizeof(dropmonitor_on)) < 0) {
523                                 perror("setsockopt SO_RXQ_OVFL not supported by your Linux Kernel");
524                                 return 1;
525                         }
526                 }
527
528                 if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
529                         perror("bind");
530                         return 1;
531                 }
532         }
533
534         if (log) {
535                 time_t currtime;
536                 struct tm now;
537                 char fname[sizeof("candump-2006-11-20_202026.log")+1];
538
539                 if (time(&currtime) == (time_t)-1) {
540                         perror("time");
541                         return 1;
542                 }
543
544                 localtime_r(&currtime, &now);
545
546                 sprintf(fname, "candump-%04d-%02d-%02d_%02d%02d%02d.log",
547                         now.tm_year + 1900,
548                         now.tm_mon + 1,
549                         now.tm_mday,
550                         now.tm_hour,
551                         now.tm_min,
552                         now.tm_sec);
553
554                 if (silent != SILENT_ON)
555                         printf("\nWarning: console output active while logging!");
556
557                 fprintf(stderr, "\nEnabling Logfile '%s'\n\n", fname);
558
559                 logfile = fopen(fname, "w");
560                 if (!logfile) {
561                         perror("logfile");
562                         return 1;
563                 }
564         }
565
566         /* these settings are static and can be held out of the hot path */
567         iov.iov_base = &frame;
568         msg.msg_name = &addr;
569         msg.msg_iov = &iov;
570         msg.msg_iovlen = 1;
571         msg.msg_control = &ctrlmsg;
572
573         while (running) {
574
575                 FD_ZERO(&rdfs);
576                 for (i=0; i<currmax; i++)
577                         FD_SET(s[i], &rdfs);
578
579                 if ((ret = select(s[currmax-1]+1, &rdfs, NULL, NULL, NULL)) < 0) {
580                         //perror("select");
581                         running = 0;
582                         continue;
583                 }
584
585                 for (i=0; i<currmax; i++) {  /* check all CAN RAW sockets */
586
587                         if (FD_ISSET(s[i], &rdfs)) {
588
589                                 int idx;
590
591                                 /* these settings may be modified by recvmsg() */
592                                 iov.iov_len = sizeof(frame);
593                                 msg.msg_namelen = sizeof(addr);
594                                 msg.msg_controllen = sizeof(ctrlmsg);  
595                                 msg.msg_flags = 0;
596
597                                 nbytes = recvmsg(s[i], &msg, 0);
598                                 if (nbytes < 0) {
599                                         perror("read");
600                                         return 1;
601                                 }
602
603                                 if (nbytes < sizeof(struct can_frame)) {
604                                         fprintf(stderr, "read: incomplete CAN frame\n");
605                                         return 1;
606                                 }
607
608                                 if (count && (--count == 0))
609                                         running = 0;
610
611                                 if (bridge) {
612                                         nbytes = write(bridge, &frame, sizeof(struct can_frame));
613                                         if (nbytes < 0) {
614                                                 perror("bridge write");
615                                                 return 1;
616                                         } else if (nbytes < sizeof(struct can_frame)) {
617                                                 fprintf(stderr,"bridge write: incomplete CAN frame\n");
618                                                 return 1;
619                                         }
620                                 }
621                     
622                                 for (cmsg = CMSG_FIRSTHDR(&msg);
623                                      cmsg && (cmsg->cmsg_level == SOL_SOCKET);
624                                      cmsg = CMSG_NXTHDR(&msg,cmsg)) {
625                                         if (cmsg->cmsg_type == SO_TIMESTAMP)
626                                                 tv = *(struct timeval *)CMSG_DATA(cmsg);
627                                         else if (cmsg->cmsg_type == SO_RXQ_OVFL)
628                                                 dropcnt[i] = *(__u32 *)CMSG_DATA(cmsg);
629                                 }
630
631                                 /* check for (unlikely) dropped frames on this specific socket */
632                                 if (dropcnt[i] != last_dropcnt[i]) {
633
634                                         __u32 frames;
635
636                                         if (dropcnt[i] > last_dropcnt[i])
637                                                 frames = dropcnt[i] - last_dropcnt[i];
638                                         else
639                                                 frames = 4294967295U - last_dropcnt[i] + dropcnt[i]; /* 4294967295U == UINT32_MAX */
640
641                                         if (silent != SILENT_ON)
642                                                 printf("DROPCOUNT: dropped %d CAN frame%s on '%s' socket (total drops %d)\n",
643                                                        frames, (frames > 1)?"s":"", cmdlinename[i], dropcnt[i]);
644
645                                         if (log)
646                                                 fprintf(logfile, "DROPCOUNT: dropped %d CAN frame%s on '%s' socket (total drops %d)\n",
647                                                         frames, (frames > 1)?"s":"", cmdlinename[i], dropcnt[i]);
648
649                                         last_dropcnt[i] = dropcnt[i];
650                                 }
651
652                                 idx = idx2dindex(addr.can_ifindex, s[i]);
653
654                                 if (log) {
655                                         /* log CAN frame with absolute timestamp & device */
656                                         fprintf(logfile, "(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
657                                         fprintf(logfile, "%*s ", max_devname_len, devname[idx]);
658                                         /* without seperator as logfile use-case is parsing */
659                                         fprint_canframe(logfile, &frame, "\n", 0);
660                                 }
661
662                                 if (logfrmt) {
663                                         /* print CAN frame in log file style to stdout */
664                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
665                                         printf("%*s ", max_devname_len, devname[idx]);
666                                         fprint_canframe(stdout, &frame, "\n", 0);
667                                         goto out_fflush; /* no other output to stdout */
668                                 }
669
670                                 if (silent != SILENT_OFF){
671                                         if (silent == SILENT_ANI) {
672                                                 printf("%c\b", anichar[silentani%=MAXANI]);
673                                                 silentani++;
674                                         }
675                                         goto out_fflush; /* no other output to stdout */
676                                 }
677                       
678                                 printf(" %s", (color>2)?col_on[idx%MAXCOL]:"");
679
680                                 switch (timestamp) {
681
682                                 case 'a': /* absolute with timestamp */
683                                         printf("(%ld.%06ld) ", tv.tv_sec, tv.tv_usec);
684                                         break;
685
686                                 case 'A': /* absolute with date */
687                                 {
688                                         struct tm tm;
689                                         char timestring[25];
690
691                                         tm = *localtime(&tv.tv_sec);
692                                         strftime(timestring, 24, "%Y-%m-%d %H:%M:%S", &tm);
693                                         printf("(%s.%06ld) ", timestring, tv.tv_usec);
694                                 }
695                                 break;
696
697                                 case 'd': /* delta */
698                                 case 'z': /* starting with zero */
699                                 {
700                                         struct timeval diff;
701
702                                         if (last_tv.tv_sec == 0)   /* first init */
703                                                 last_tv = tv;
704                                         diff.tv_sec  = tv.tv_sec  - last_tv.tv_sec;
705                                         diff.tv_usec = tv.tv_usec - last_tv.tv_usec;
706                                         if (diff.tv_usec < 0)
707                                                 diff.tv_sec--, diff.tv_usec += 1000000;
708                                         if (diff.tv_sec < 0)
709                                                 diff.tv_sec = diff.tv_usec = 0;
710                                         printf("(%ld.%06ld) ", diff.tv_sec, diff.tv_usec);
711                                 
712                                         if (timestamp == 'd')
713                                                 last_tv = tv; /* update for delta calculation */
714                                 }
715                                 break;
716
717                                 default: /* no timestamp output */
718                                         break;
719                                 }
720
721                                 printf(" %s", (color && (color<3))?col_on[idx%MAXCOL]:"");
722                                 printf("%*s", max_devname_len, devname[idx]);
723                                 printf("%s  ", (color==1)?col_off:"");
724
725                                 fprint_long_canframe(stdout, &frame, NULL, view);
726
727                                 printf("%s", (color>1)?col_off:"");
728                                 printf("\n");
729                         }
730
731                 out_fflush:
732                         fflush(stdout);
733                 }
734         }
735
736         for (i=0; i<currmax; i++)
737                 close(s[i]);
738
739         if (bridge)
740                 close(bridge);
741
742         if (log)
743                 fclose(logfile);
744
745         return 0;
746 }