]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - cansniffer.c
can-utils: AOSP build clean up
[sojka/can-utils.git] / cansniffer.c
1 /*
2  * can-sniffer.c
3  *
4  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of Volkswagen nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * Alternatively, provided that this notice is retained in full, this
20  * software may be distributed under the terms of the GNU General
21  * Public License ("GPL") version 2, in which case the provisions of the
22  * GPL apply INSTEAD OF those given above.
23  *
24  * The provided data structures and external interfaces from this code
25  * are not restricted to be used by modules with a GPL compatible license.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  *
40  * Send feedback to <linux-can@vger.kernel.org>
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <signal.h>
50 #include <ctype.h>
51 #include <libgen.h>
52 #include <time.h>
53
54 #include <sys/time.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <sys/uio.h>
60 #include <net/if.h>
61
62 #include <linux/can.h>
63 #include <linux/can/bcm.h>
64
65 #include "terminal.h"
66
67 #define U64_DATA(p) (*(unsigned long long*)(p)->data)
68
69 #define SETFNAME "sniffset."
70 #define ANYDEV   "any"
71
72 /* flags */
73
74 #define ENABLE  1 /* by filter or user */
75 #define DISPLAY 2 /* is on the screen */
76 #define UPDATE  4 /* needs to be printed on the screen */
77 #define CLRSCR  8 /* clear screen in next loop */
78
79 /* flags testing & setting */
80
81 #define is_set(id, flag) (sniftab[id].flags & flag)
82 #define is_clr(id, flag) (!(sniftab[id].flags & flag))
83
84 #define do_set(id, flag) (sniftab[id].flags |= flag)
85 #define do_clr(id, flag) (sniftab[id].flags &= ~flag)
86
87 /* time defaults */
88
89 #define TIMEOUT 50 /* in 100ms */
90 #define HOLD    10 /* in 100ms */
91 #define LOOP     2 /* in 100ms */
92
93 #define MAXANI 8
94 const char anichar[MAXANI] = {'|', '/', '-', '\\', '|', '/', '-', '\\'};
95
96 #define ATTCOLOR ATTBOLD FGRED
97
98 #define STARTLINESTR "X  time    ID  data ... "
99
100 struct snif {
101         int flags;
102         long hold;
103         long timeout;
104         struct timeval laststamp;
105         struct timeval currstamp;
106         struct can_frame last;
107         struct can_frame current;
108         struct can_frame marker;
109         struct can_frame notch;
110 } sniftab[2048];
111
112
113 extern int optind, opterr, optopt;
114
115 static int running = 1;
116 static int clearscreen = 1;
117 static int notch;
118 static int filter_id_only;
119 static long timeout = TIMEOUT;
120 static long hold = HOLD;
121 static long loop = LOOP;
122 static unsigned char binary;
123 static unsigned char binary_gap;
124 static unsigned char color;
125 static char *interface;
126
127 void rx_setup (int fd, int id);
128 void rx_delete (int fd, int id);
129 void print_snifline(int id);
130 int handle_keyb(int fd);
131 int handle_bcm(int fd, long currcms);
132 int handle_timeo(int fd, long currcms);
133 void writesettings(char* name);
134 void readsettings(char* name, int sockfd);
135
136 void print_usage(char *prg)
137 {
138         const char manual [] = {
139                 "commands that can be entered at runtime:\n"
140                 "\n"
141                 "q<ENTER>       - quit\n"
142                 "b<ENTER>       - toggle binary / HEX-ASCII output\n"
143                 "B<ENTER>       - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n"
144                 "c<ENTER>       - toggle color mode\n"
145                 "#<ENTER>       - notch currently marked/changed bits (can be used repeatedly)\n"
146                 "*<ENTER>       - clear notched marked\n"
147                 "rMYNAME<ENTER> - read settings file (filter/notch)\n"
148                 "wMYNAME<ENTER> - write settings file (filter/notch)\n"
149                 "+FILTER<ENTER> - add CAN-IDs to sniff\n"
150                 "-FILTER<ENTER> - remove CAN-IDs to sniff\n"
151                 "\n"
152                 "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n"
153                 "+1F5<ENTER>    - add CAN-ID 0x1F5\n"
154                 "-42E<ENTER>    - remove CAN-ID 0x42E\n"
155                 "-42E7FF<ENTER> - remove CAN-ID 0x42E (using Bitmask)\n"
156                 "-500700<ENTER> - remove CAN-IDs 0x500 - 0x5FF\n"
157                 "+400600<ENTER> - add CAN-IDs 0x400 - 0x5FF\n"
158                 "+000000<ENTER> - add all CAN-IDs\n"
159                 "-000000<ENTER> - remove all CAN-IDs\n"
160                 "\n"
161                 "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n"
162                 "which is quite easy when the filter is 000\n"
163                 "\n"
164         };
165
166         fprintf(stderr, "\nUsage: %s [can-interface]\n", prg);
167         fprintf(stderr, "Options: -m <mask>  (initial FILTER default 0x00000000)\n");
168         fprintf(stderr, "         -v <value> (initial FILTER default 0x00000000)\n");
169         fprintf(stderr, "         -q         (quiet - all IDs deactivated)\n");
170         fprintf(stderr, "         -r <name>  (read %sname from file)\n", SETFNAME);
171         fprintf(stderr, "         -b         (start with binary mode)\n");
172         fprintf(stderr, "         -B         (start with binary mode with gap - exceeds 80 chars!)\n");
173         fprintf(stderr, "         -c         (color changes)\n");
174         fprintf(stderr, "         -f         (filter on CAN-ID only)\n");
175         fprintf(stderr, "         -t <time>  (timeout for ID display [x100ms] default: %d, 0 = OFF)\n", TIMEOUT);
176         fprintf(stderr, "         -h <time>  (hold marker on changes [x100ms] default: %d)\n", HOLD);
177         fprintf(stderr, "         -l <time>  (loop time (display) [x100ms] default: %d)\n", LOOP);
178         fprintf(stderr, "Use interface name '%s' to receive from all can-interfaces\n", ANYDEV);
179         fprintf(stderr, "\n");
180         fprintf(stderr, "%s", manual);
181 }
182
183 void sigterm(int signo)
184 {
185         running = 0;
186 }
187
188 int main(int argc, char **argv)
189 {
190         fd_set rdfs;
191         int s;
192         canid_t mask = 0;
193         canid_t value = 0;
194         long currcms = 0;
195         long lastcms = 0;
196         unsigned char quiet = 0;
197         int opt, ret;
198         struct timeval timeo, start_tv, tv;
199         struct sockaddr_can addr;
200         struct ifreq ifr;
201         int i;
202
203
204         signal(SIGTERM, sigterm);
205         signal(SIGHUP, sigterm);
206         signal(SIGINT, sigterm);
207
208         for (i=0; i < 2048 ;i++) /* default: check all CAN-IDs */
209                 do_set(i, ENABLE);
210
211         while ((opt = getopt(argc, argv, "m:v:r:t:h:l:qbBcf?")) != -1) {
212                 switch (opt) {
213                 case 'm':
214                         sscanf(optarg, "%x", &mask);
215                         break;
216
217                 case 'v':
218                         sscanf(optarg, "%x", &value);
219                         break;
220
221                 case 'r':
222                         readsettings(optarg, 0); /* no BCM-setting here */
223                         break;
224
225                 case 't':
226                         sscanf(optarg, "%ld", &timeout);
227                         break;
228
229                 case 'h':
230                         sscanf(optarg, "%ld", &hold);
231                         break;
232
233                 case 'l':
234                         sscanf(optarg, "%ld", &loop);
235                         break;
236
237                 case 'q':
238                         quiet = 1;
239                         break;
240
241                 case 'b':
242                         binary = 1;
243                         binary_gap = 0;
244                         break;
245
246                 case 'B':
247                         binary = 1;
248                         binary_gap = 1;
249                         break;
250
251                 case 'c':
252                         color = 1;
253                         break;
254
255                 case 'f':
256                         filter_id_only = 1;
257                         break;
258
259                 case '?':
260                         break;
261
262                 default:
263                         fprintf(stderr, "Unknown option %c\n", opt);
264                         break;
265                 }
266         }
267
268         if (optind == argc) {
269                 print_usage(basename(argv[0]));
270                 exit(0);
271         }
272         
273         if (mask || value) {
274                 for (i=0; i < 2048 ;i++) {
275                         if ((i & mask) ==  (value & mask))
276                                 do_set(i, ENABLE);
277                         else
278                                 do_clr(i, ENABLE);
279                 }
280         }
281
282         if (quiet)
283                 for (i=0; i < 2048 ;i++)
284                         do_clr(i, ENABLE);
285
286         if (strlen(argv[optind]) >= IFNAMSIZ) {
287                 printf("name of CAN device '%s' is too long!\n", argv[optind]);
288                 return 1;
289         }
290
291         interface = argv[optind];
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, "< cansniffer %s # l=%ld h=%ld t=%ld >", interface, 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 };