]> rtime.felk.cvut.cz Git - can-utils.git/blob - cansniffer.c
Added -f option to filter only on the CAN-ID and not on the content.
[can-utils.git] / cansniffer.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * can-sniffer.c
7  *
8  * Copyright (c) 2002-2007 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 <fcntl.h>
53 #include <signal.h>
54 #include <ctype.h>
55 #include <libgen.h>
56 #include <time.h>
57
58 #include <sys/time.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/socket.h>
62 #include <sys/ioctl.h>
63 #include <sys/uio.h>
64 #include <net/if.h>
65
66 #include <linux/can.h>
67 #include <linux/can/bcm.h>
68
69 #include "terminal.h"
70
71 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
72
73 #define SETFNAME "sniffset."
74 #define ANYDEV   "any"
75
76 /* flags */
77
78 #define ENABLE  1 /* by filter or user */
79 #define DISPLAY 2 /* is on the screen */
80 #define UPDATE  4 /* needs to be printed on the screen */
81 #define CLRSCR  8 /* clear screen in next loop */
82
83 /* flags testing & setting */
84
85 #define is_set(id, flag) (sniftab[id].flags & flag)
86 #define is_clr(id, flag) (!(sniftab[id].flags & flag))
87
88 #define do_set(id, flag) (sniftab[id].flags |= flag)
89 #define do_clr(id, flag) (sniftab[id].flags &= ~flag)
90
91 /* time defaults */
92
93 #define TIMEOUT 50 /* in 100ms */
94 #define HOLD    10 /* in 100ms */
95 #define LOOP     2 /* in 100ms */
96
97 #define MAXANI 8
98 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
99
100 #define ATTCOLOR ATTBOLD FGRED
101
102 #define STARTLINESTR "X  time    ID  data ... "
103
104 struct snif {
105         int flags;
106         long hold;
107         long timeout;
108         struct timeval laststamp;
109         struct timeval currstamp;
110         struct can_frame last;
111         struct can_frame current;
112         struct can_frame marker;
113         struct can_frame notch;
114 } sniftab[2048];
115
116
117 extern int optind, opterr, optopt;
118
119 static int running = 1;
120 static int clearscreen = 1;
121 static int notch;
122 static int filter_id_only;
123 static long timeout = TIMEOUT;
124 static long hold = HOLD;
125 static long loop = LOOP;
126 static unsigned char binary;
127 static unsigned char binary_gap;
128 static unsigned char color;
129
130 void rx_setup (int fd, int id);
131 void rx_delete (int fd, int id);
132 void print_snifline(int id);
133 int handle_keyb(int fd);
134 int handle_bcm(int fd, long currcms);
135 int handle_timeo(int fd, long currcms);
136 void writesettings(char* name);
137 void readsettings(char* name, int sockfd);
138
139 void print_usage(char *prg)
140 {
141         const char manual [] = {
142                 "commands that can be entered at runtime:\n"
143                 "\n"
144                 "q<ENTER>       - quit\n"
145                 "b<ENTER>       - toggle binary / HEX-ASCII output\n"
146                 "B<ENTER>       - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
147                 "c<ENTER>       - toggle color mode\n"
148                 "#<ENTER>       - notch currently marked/changed bits (can be used repeatedly)\n"
149                 "*<ENTER>       - clear notched marked\n"
150                 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
151                 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
152                 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
153                 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
154                 "\n"
155                 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
156                 "+1F5<ENTER>    - add CAN-ID 0x1F5\n"
157                 "-42E<ENTER>    - remove CAN-ID 0x42E\n"
158                 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
159                 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
160                 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
161                 "+000000<ENTER> - add all CAN-IDs\n"
162                 "-000000<ENTER> - remove all CAN-IDs\n"
163                 "\n"
164                 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
165                 "which is quite easy when the filter is 000\n"
166                 "\n"
167         };
168
169         fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
170         fprintf(stderr, "Options: -m <mask>  (initial FILTER default 0x00000000)\n");
171         fprintf(stderr, "         -v <value> (initial FILTER default 0x00000000)\n");
172         fprintf(stderr, "         -q         (quiet - all IDs deactivated)\n");
173         fprintf(stderr, "         -r <name>  (read %sname from file)\n", SETFNAME);
174         fprintf(stderr, "         -b         (start with binary mode)\n");
175         fprintf(stderr, "         -B         (start with binary mode with gap - exceeds 80 chars!)\n");
176         fprintf(stderr, "         -c         (color changes)\n");
177         fprintf(stderr, "         -t <time>  (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
178         fprintf(stderr, "         -h <time>  (hold marker on changes [x100ms] default: %d)\n", HOLD);
179         fprintf(stderr, "         -l <time>  (loop time (display) [x100ms] default: %d)\n", LOOP);
180         fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
181         fprintf(stderr, "\n");
182         fprintf(stderr, "%s", manual);
183 }
184
185 void sigterm(int signo)
186 {
187         running = 0;
188 }
189
190 int main(int argc, char **argv)
191 {
192         fd_set rdfs;
193         int s;
194         canid_t mask = 0;
195         canid_t value = 0;
196         long currcms = 0;
197         long lastcms = 0;
198         unsigned char quiet = 0;
199         int opt, ret;
200         struct timeval timeo, start_tv, tv;
201         struct sockaddr_can addr;
202         struct ifreq ifr;
203         int i;
204
205
206         signal(SIGTERM, sigterm);
207         signal(SIGHUP, sigterm);
208         signal(SIGINT, sigterm);
209
210         for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
211                 do_set(i, ENABLE);
212
213         while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf")) != -1) {
214                 switch (opt) {
215                 case 'm':
216                         sscanf(optarg, "%x", &mask);
217                         break;
218
219                 case 'v':
220                         sscanf(optarg, "%x", &value);
221                         break;
222
223                 case 'r':
224                         readsettings(optarg, 0); /* no BCM-setting here */
225                         break;
226
227                 case 't':
228                         sscanf(optarg, "%ld", &timeout);
229                         break;
230
231                 case 'h':
232                         sscanf(optarg, "%ld", &hold);
233                         break;
234
235                 case 'l':
236                         sscanf(optarg, "%ld", &loop);
237                         break;
238
239                 case 'q':
240                         quiet = 1;
241                         break;
242
243                 case 'b':
244                         binary = 1;
245                         binary_gap = 0;
246                         break;
247
248                 case 'B':
249                         binary = 1;
250                         binary_gap = 1;
251                         break;
252
253                 case 'c':
254                         color = 1;
255                         break;
256
257                 case 'f':
258                         filter_id_only = 1;
259                         break;
260
261                 case '?':
262                         break;
263
264                 default:
265                         fprintf(stderr, "Unknown option %c\n", opt);
266                         break;
267                 }
268         }
269
270         if (optind == argc) {
271                 print_usage(basename(argv[0]));
272                 exit(0);
273         }
274         
275         if (mask || value) {
276                 for (i=0; i < 2048 ;i++) {
277                         if ((i & mask) ==  (value & mask))
278                                 do_set(i, ENABLE);
279                         else
280                                 do_clr(i, ENABLE);
281                 }
282         }
283
284         if (quiet)
285                 for (i=0; i < 2048 ;i++)
286                         do_clr(i, ENABLE);
287
288         if (strlen(argv[optind]) >= IFNAMSIZ) {
289                 printf("name of CAN device '%s' is too long!\n", argv[optind]);
290                 return 1;
291         }
292
293         if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) {
294                 perror("socket");
295                 return 1;
296         }
297
298         addr.can_family = AF_CAN;
299
300         if (strcmp(ANYDEV, argv[optind])) {
301                 strcpy(ifr.ifr_name, argv[optind]);
302                 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
303                         perror("SIOCGIFINDEX");
304                         exit(1);
305                 }
306                 addr.can_ifindex = ifr.ifr_ifindex;
307         }
308         else
309                 addr.can_ifindex = 0; /* any can interface */
310
311         if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
312                 perror("connect");
313                 return 1;
314         }
315
316         for (i=0; i < 2048 ;i++) /* initial BCM setup */
317                 if (is_set(i, ENABLE))
318                         rx_setup(s, i);
319
320         gettimeofday(&start_tv, NULL);
321         tv.tv_sec = tv.tv_usec = 0;
322
323         printf("%s", CSR_HIDE); /* hide cursor */
324
325         while (running) {
326
327                 FD_ZERO(&rdfs);
328                 FD_SET(0, &rdfs);
329                 FD_SET(s, &rdfs);
330
331                 timeo.tv_sec  = 0;
332                 timeo.tv_usec = 100000 * loop;
333
334                 if ((ret = select(s+1, &rdfs, NULL, NULL, &timeo)) < 0) {
335                         //perror("select");
336                         running = 0;
337                         continue;
338                 }
339
340                 gettimeofday(&tv, NULL);
341                 currcms = (tv.tv_sec - start_tv.tv_sec) * 10 + (tv.tv_usec / 100000);
342
343                 if (FD_ISSET(0, &rdfs))
344                         running &= handle_keyb(s);
345
346                 if (FD_ISSET(s, &rdfs))
347                         running &= handle_bcm(s, currcms);
348
349                 if (currcms - lastcms >= loop) {
350                         running &= handle_timeo(s, currcms);
351                         lastcms = currcms;
352                 }
353         }
354
355         printf("%s", CSR_SHOW); /* show cursor */
356
357         close(s);
358         return 0;
359 }
360
361 void rx_setup (int fd, int id){
362
363         struct {
364                 struct bcm_msg_head msg_head;
365                 struct can_frame frame;
366         } txmsg;
367
368         txmsg.msg_head.opcode  = RX_SETUP;
369         txmsg.msg_head.can_id  = id;
370         txmsg.msg_head.flags   = RX_CHECK_DLC;
371         txmsg.msg_head.ival1.tv_sec  = 0;
372         txmsg.msg_head.ival1.tv_usec = 0;
373         txmsg.msg_head.ival2.tv_sec  = 0;
374         txmsg.msg_head.ival2.tv_usec = 0;
375         txmsg.msg_head.nframes = 1;
376         U64_DATA(&txmsg.frame) = (__u64) 0xFFFFFFFFFFFFFFFFULL;
377
378         if (filter_id_only)
379                 txmsg.msg_head.flags |= RX_FILTER_ID;
380
381         if (write(fd, &txmsg, sizeof(txmsg)) < 0)
382                 perror("write");
383 };
384
385 void rx_delete (int fd, int id){
386
387         struct bcm_msg_head msg_head;
388
389         msg_head.opcode  = RX_DELETE;
390         msg_head.can_id  = id;
391         msg_head.nframes = 0;
392
393         if (write(fd, &msg_head, sizeof(msg_head)) < 0)
394                 perror("write");
395 }
396
397 int handle_keyb(int fd){
398
399         char cmd [20] = {0};
400         int i;
401         unsigned int mask;
402         unsigned int value;
403
404         if (read(0, cmd, 19) > strlen("+123456\n"))
405                 return 1; /* ignore */
406
407         if (strlen(cmd) > 0)
408                 cmd[strlen(cmd)-1] = 0; /* chop off trailing newline */
409
410         switch (cmd[0]) {
411
412         case '+':
413         case '-':
414                 sscanf(&cmd[1], "%x", &value);
415                 if (strlen(&cmd[1]) > 3) {
416                         mask = value & 0xFFF;
417                         value >>= 12;
418                 }
419                 else
420                         mask = 0x7FF;
421
422                 if (cmd[0] == '+') {
423                         for (i=0; i < 2048 ;i++) {
424                                 if (((i & mask) == (value & mask)) && (is_clr(i, ENABLE))) {
425                                         do_set(i, ENABLE);
426                                         rx_setup(fd, i);
427                                 }
428                         }
429                 }
430                 else { /* '-' */
431                         for (i=0; i < 2048 ;i++) {
432                                 if (((i & mask) == (value & mask)) && (is_set(i, ENABLE))) {
433                                         do_clr(i, ENABLE);
434                                         rx_delete(fd, i);
435                                 }
436                         }
437                 }
438                 break;
439
440         case 'w' :
441                 writesettings(&cmd[1]);
442                 break;
443
444         case 'r' :
445                 readsettings(&cmd[1], fd);
446                 break;
447
448         case 'q' :
449                 running = 0;
450                 break;
451
452         case 'B' :
453                 binary_gap = 1;
454                 if (binary)
455                         binary = 0;
456                 else
457                         binary = 1;
458
459                 break;
460
461         case 'b' :
462                 binary_gap = 0;
463                 if (binary)
464                         binary = 0;
465                 else
466                         binary = 1;
467
468                 break;
469
470         case 'c' :
471                 if (color)
472                         color = 0;
473                 else
474                         color = 1;
475
476                 break;
477
478         case '#' :
479                 notch = 1;
480                 break;
481
482         case '*' :
483                 for (i=0; i < 2048; i++)
484                         U64_DATA(&sniftab[i].notch) = (__u64) 0;
485                 break;
486
487         default:
488                 break;
489         }
490
491         clearscreen = 1;
492
493         return 1; /* ok */
494 };
495
496 int handle_bcm(int fd, long currcms){
497
498         int nbytes, id;
499
500         struct {
501                 struct bcm_msg_head msg_head;
502                 struct can_frame frame;
503         } bmsg;
504
505         if ((nbytes = read(fd, &bmsg, sizeof(bmsg))) < 0) {
506                 perror("bcm read");
507                 return 0; /* quit */
508         }
509
510         id = bmsg.msg_head.can_id;
511         ioctl(fd, SIOCGSTAMP, &sniftab[id].currstamp);
512
513         if (bmsg.msg_head.opcode != RX_CHANGED) {
514                 printf("received strange BCM opcode %d!\n", bmsg.msg_head.opcode);
515                 return 0; /* quit */
516         }
517
518         if (nbytes != sizeof(bmsg)) {
519                 printf("received strange BCM data length %d!\n", nbytes);
520                 return 0; /* quit */
521         }
522
523         sniftab[id].current = bmsg.frame;
524         U64_DATA(&sniftab[id].marker) |= 
525                 U64_DATA(&sniftab[id].current) ^ U64_DATA(&sniftab[id].last);
526         sniftab[id].timeout = (timeout)?(currcms + timeout):0;
527
528         if (is_clr(id, DISPLAY))
529                 clearscreen = 1; /* new entry -> new drawing */
530
531         do_set(id, DISPLAY);
532         do_set(id, UPDATE);
533         
534         return 1; /* ok */
535 };
536
537 int handle_timeo(int fd, long currcms){
538
539         int i;
540         int force_redraw = 0;
541
542         if (clearscreen) {
543                 char startline[80];
544                 printf("%s%s", CLR_SCREEN, CSR_HOME);
545                 snprintf(startline, 79, "< can-sniffer parameters: l=%ld h=%ld t=%ld >", loop, hold, timeout);
546                 printf("%s%*s",STARTLINESTR, 79-(int)strlen(STARTLINESTR), startline);
547                 force_redraw = 1;
548                 clearscreen = 0;
549         }
550
551         if (notch) {
552                 for (i=0; i < 2048; i++)
553                         U64_DATA(&sniftab[i].notch) |= U64_DATA(&sniftab[i].marker);
554                 notch = 0;
555         }
556
557         printf("%s", CSR_HOME);
558         printf("%c\n", anichar[currcms % MAXANI]); /* funny animation */
559
560         for (i=0; i < 2048; i++) {
561
562                 if is_set(i, ENABLE) {
563
564                                 if is_set(i, DISPLAY) {
565
566                                                 if (is_set(i, UPDATE) || (force_redraw)){
567                                                         print_snifline(i);
568                                                         sniftab[i].hold = currcms + hold;
569                                                         do_clr(i, UPDATE);
570                                                 }
571                                                 else
572                                                         if ((sniftab[i].hold) && (sniftab[i].hold < currcms)) {
573                                                                 U64_DATA(&sniftab[i].marker) = (__u64) 0;
574                                                                 print_snifline(i);
575                                                                 sniftab[i].hold = 0; /* disable update by hold */
576                                                         }
577                                                         else
578                                                                 printf("%s", CSR_DOWN); /* skip my line */
579
580                                                 if (sniftab[i].timeout && sniftab[i].timeout < currcms) {
581                                                         do_clr(i, DISPLAY);
582                                                         do_clr(i, UPDATE);
583                                                         clearscreen = 1; /* removed entry -> new drawing next time */
584                                                 }
585                                         }
586                                 sniftab[i].last      = sniftab[i].current;
587                                 sniftab[i].laststamp = sniftab[i].currstamp;
588                         }
589         }
590
591         return 1; /* ok */
592
593 };
594
595 void print_snifline(int id){
596
597         long diffsec  = sniftab[id].currstamp.tv_sec  - sniftab[id].laststamp.tv_sec;
598         long diffusec = sniftab[id].currstamp.tv_usec - sniftab[id].laststamp.tv_usec;
599         int dlc_diff  = sniftab[id].last.can_dlc - sniftab[id].current.can_dlc;
600         int i,j;
601
602         if (diffusec < 0)
603                 diffsec--, diffusec += 1000000;
604
605         if (diffsec < 0)
606                 diffsec = diffusec = 0;
607
608         if (diffsec > 10)
609                 diffsec = 9, diffusec = 999999;
610
611         printf("%ld.%06ld  %3x  ", diffsec, diffusec, id);
612
613         if (binary) {
614
615                 for (i=0; i<sniftab[id].current.can_dlc; i++) {
616                         for (j=7; j>=0; j--) {
617                                 if ((color) && (sniftab[id].marker.data[i] & 1<<j) &&
618                                     (!(sniftab[id].notch.data[i] & 1<<j)))
619                                         if (sniftab[id].current.data[i] & 1<<j)
620                                                 printf("%s1%s", ATTCOLOR, ATTRESET);
621                                         else
622                                                 printf("%s0%s", ATTCOLOR, ATTRESET);
623                                 else
624                                         if (sniftab[id].current.data[i] & 1<<j)
625                                                 putchar('1');
626                                         else
627                                                 putchar('0');
628                         }
629                         if (binary_gap)
630                                 putchar(' ');
631                 }
632
633                 /*
634                  * when the can_dlc decreased (dlc_diff > 0),
635                  * we need to blank the former data printout
636                  */
637                 for (i=0; i<dlc_diff; i++) {
638                         printf("        ");
639                         if (binary_gap)
640                                 putchar(' ');
641                 }
642         }
643         else {
644
645                 for (i=0; i<sniftab[id].current.can_dlc; i++)
646                         if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
647                                 printf("%s%02X%s ", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
648                         else
649                                 printf("%02X ", sniftab[id].current.data[i]);
650
651                 if (sniftab[id].current.can_dlc < 8)
652                         printf("%*s", (8 - sniftab[id].current.can_dlc) * 3, "");
653
654                 for (i=0; i<sniftab[id].current.can_dlc; i++)
655                         if ((sniftab[id].current.data[i] > 0x1F) && 
656                             (sniftab[id].current.data[i] < 0x7F))
657                                 if ((color) && (sniftab[id].marker.data[i]) && (!(sniftab[id].notch.data[i])))
658                                         printf("%s%c%s", ATTCOLOR, sniftab[id].current.data[i], ATTRESET);
659                                 else
660                                         putchar(sniftab[id].current.data[i]);
661                         else
662                                 putchar('.');
663
664                 /*
665                  * when the can_dlc decreased (dlc_diff > 0),
666                  * we need to blank the former data printout
667                  */
668                 for (i=0; i<dlc_diff; i++)
669                         putchar(' ');
670         }
671
672         putchar('\n');
673
674         U64_DATA(&sniftab[id].marker) = (__u64) 0;
675
676 };
677
678
679 void writesettings(char* name){
680
681         int fd;
682         char fname[30] = SETFNAME;
683         int i,j;
684         char buf[8]= {0};
685
686         strncat(fname, name, 29 - strlen(fname)); 
687         fd = open(fname,  O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
688     
689         if (fd > 0) {
690
691                 for (i=0; i < 2048 ;i++) {
692                         sprintf(buf, "<%03X>%c.", i, (is_set(i, ENABLE))?'1':'0');
693                         write(fd, buf, 7);
694                         for (j=0; j<8 ; j++){
695                                 sprintf(buf, "%02X", sniftab[i].notch.data[j]);
696                                 write(fd, buf, 2);
697                         }
698                         write(fd, "\n", 1);
699                         /* 7 + 16 + 1 = 24 bytes per entry */ 
700                 }
701                 close(fd);
702         }
703         else
704                 printf("unable to write setting file '%s'!\n", fname);
705 };
706
707 void readsettings(char* name, int sockfd){
708
709         int fd;
710         char fname[30] = SETFNAME;
711         char buf[25] = {0};
712         int i,j;
713
714         strncat(fname, name, 29 - strlen(fname)); 
715         fd = open(fname, O_RDONLY);
716     
717         if (fd > 0) {
718                 if (!sockfd)
719                         printf("reading setting file '%s' ... ", fname);
720
721                 for (i=0; i < 2048 ;i++) {
722                         if (read(fd, &buf, 24) == 24) {
723                                 if (buf[5] & 1) {
724                                         if (is_clr(i, ENABLE)) {
725                                                 do_set(i, ENABLE);
726                                                 if (sockfd)
727                                                         rx_setup(sockfd, i);
728                                         }
729                                 }
730                                 else
731                                         if (is_set(i, ENABLE)) {
732                                                 do_clr(i, ENABLE);
733                                                 if (sockfd)
734                                                         rx_delete(sockfd, i);
735                                         }
736                                 for (j=7; j>=0 ; j--){
737                                         sniftab[i].notch.data[j] =
738                                                 (__u8) strtoul(&buf[2*j+7], (char **)NULL, 16) & 0xFF;
739                                         buf[2*j+7] = 0; /* cut off each time */
740                                 }
741                         }
742                         else {
743                                 if (!sockfd)
744                                         printf("was only able to read until index %d from setting file '%s'!\n",
745                                                i, fname);
746                         }
747                 }
748     
749                 if (!sockfd)
750                         printf("done\n");
751
752                 close(fd);
753         }
754         else
755                 printf("unable to read setting file '%s'!\n", fname);
756 };