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