cansniffer.c 17 KB

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