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