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