From 384ed8d9191df4f9518de890e3c81350f029fe7b Mon Sep 17 00:00:00 2001 From: Cory Date: Sun, 29 Jul 2018 10:10:12 -0400 Subject: [PATCH 1/2] Added Extended ID support Moved from CAN_BCM to CAN_RAW. Restored basic functionality that BCM had natively. There are likely bugs due to some non functionality between how the original program operated with CAN_BCM and now, but it is working on my bench with a NMEA 2000 GPS. Update README.md Restored add/remove ID Restored ability to remove or add active CAN frames. The CAN_ID must already exist in sniftab for this to work. Update cansniffer.c Fixed a potential for a segmentation fault to occur with the sniftab array. Changed arbitrary max array value to defined name for flexibility with increasing the array size. Other polish I forgot about. Settings file operations work Aligned settings file operations for use with 29bit IDs. Instead of saving all 800 "slots" (or 11 bits IDs for the program as originally written), it now saves in use slots based on CAN ID and will load that ID into a slot for the program to potentially match upon. Truncate when overwritting settings Need to truncate settings file... Update README.md Update cansniffer.c Cleaned up some unused commented code. --- README.md | 2 +- cansniffer.c | 404 ++++++++++++++++++++++++++------------------------- 2 files changed, 207 insertions(+), 199 deletions(-) diff --git a/README.md b/README.md index d1de3662..a46d0694 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ subsystem (aka SocketCAN): * canplayer : replay CAN logfiles * cansend : send a single frame * cangen : generate (random) CAN traffic -* cansniffer : display CAN data content differences (just 11bit CAN IDs) +* cansniffer : display CAN data content differences ~~(just 11bit CAN IDs)~~ #### CAN access via IP sockets * canlogserver : log CAN frames from a remote/local host diff --git a/cansniffer.c b/cansniffer.c index b95f11b4..e59811e4 100644 --- a/cansniffer.c +++ b/cansniffer.c @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -60,13 +61,14 @@ #include #include -#include #include "terminal.h" #define SETFNAME "sniffset." #define ANYDEV "any" +#define MAX_SLOTS 2048 + /* flags */ #define ENABLE 1 /* by filter or user */ @@ -102,7 +104,7 @@ struct snif { struct can_frame current; struct can_frame marker; struct can_frame notch; -} sniftab[2048]; +} sniftab[MAX_SLOTS]; extern int optind, opterr, optopt; @@ -110,7 +112,6 @@ extern int optind, opterr, optopt; static int running = 1; static int clearscreen = 1; static int notch; -static int filter_id_only; static long timeout = TIMEOUT; static long hold = HOLD; static long loop = LOOP; @@ -119,54 +120,69 @@ static unsigned char binary_gap; static unsigned char color; static char *interface; -void rx_setup (int fd, int id); -void rx_delete (int fd, int id); -void print_snifline(int id); +void print_snifline(int slot); int handle_keyb(int fd); -int handle_bcm(int fd, long currcms); +int handle_frame(int fd, long currcms); int handle_timeo(int fd, long currcms); +int sniftab_index(canid_t id); void writesettings(char* name); void readsettings(char* name, int sockfd); +int idx = 0; + + +int comp (const void * elem1, const void * elem2) +{ + unsigned long f = ((struct snif*)elem1)->current.can_id; + unsigned long s = ((struct snif*)elem2)->current.can_id; + if (f > s) return 1; + if (f < s) return -1; + return 0; +} + + void print_usage(char *prg) { const char manual [] = { "commands that can be entered at runtime:\n" "\n" - "q - quit\n" - "b - toggle binary / HEX-ASCII output\n" - "B - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n" - "c - toggle color mode\n" - "# - notch currently marked/changed bits (can be used repeatedly)\n" - "* - clear notched marked\n" - "rMYNAME - read settings file (filter/notch)\n" - "wMYNAME - write settings file (filter/notch)\n" - "+FILTER - add CAN-IDs to sniff\n" - "-FILTER - remove CAN-IDs to sniff\n" + "q - quit\n" + "b - toggle binary / HEX-ASCII output\n" + "B - toggle binary with gap / HEX-ASCII output (exceeds 80 chars!)\n" + "c - toggle color mode\n" + "# - notch currently marked/changed bits (can be used repeatedly)\n" + "* - clear notched marked\n" + "rMYNAME - read settings file (filter/notch)\n" + "wMYNAME - write settings file (filter/notch)\n" + "+FILTER - add CAN-IDs to sniff\n" + "-FILTER - remove CAN-IDs to sniff\n" "\n" - "FILTER can be a single CAN-ID or a CAN-ID/Bitmask:\n" - "+1F5 - add CAN-ID 0x1F5\n" - "-42E - remove CAN-ID 0x42E\n" - "-42E7FF - remove CAN-ID 0x42E (using Bitmask)\n" - "-500700 - remove CAN-IDs 0x500 - 0x5FF\n" - "+400600 - add CAN-IDs 0x400 - 0x5FF\n" - "+000000 - add all CAN-IDs\n" - "-000000 - remove all CAN-IDs\n" + "FILTER must be a single CAN-ID:\n"/* "or a CAN-ID/Bitmask:\n"*/ + "+1F5 - add CAN-ID 0x1F5\n" + "-42E - remove CAN-ID 0x42E\n" "\n" - "if (id & filter) == (sniff-id & filter) the action (+/-) is performed,\n" - "which is quite easy when the filter is 000\n" + "29 bit IDs:\n" + "+18FEDF55 - add CAN-ID 0x18FEDF55\n" + "-00000090 - remove CAN-ID 0x00000090\n" +/* "-42E7FF - remove CAN-ID 0x42E (using Bitmask)\N"*/ +/* "-500700 - remove CAN-IDs 0x500 - 0x5FF\n"*/ +/* "+400600 - add CAN-IDs 0x400 - 0x5FF\n"*/ +/* "+000000 - add all CAN-IDs\n"*/ +/* "-000000 - remove all CAN-IDs\n"*/ +/* "\n"*/ +/* "if (id & filter) == (sniff-id & filter) the action (+/-) IS PERFORMED,\N" */ +/* "which is quite easy when the filter is 000\n"*/ "\n" }; fprintf(stderr, "\nUsage: %s [can-interface]\n", prg); - fprintf(stderr, "Options: -m (initial FILTER default 0x00000000)\n"); - fprintf(stderr, " -v (initial FILTER default 0x00000000)\n"); - fprintf(stderr, " -q (quiet - all IDs deactivated)\n"); +// fprintf(stderr, "Options: -m (initial FILTER default 0x00000000)\n"); +// fprintf(stderr, " -v (initial FILTER default 0x00000000)\n"); + fprintf(stderr, "Options: -q (quiet - all slots are deactivated)\n"); fprintf(stderr, " -r (read %sname from file)\n", SETFNAME); fprintf(stderr, " -b (start with binary mode)\n"); fprintf(stderr, " -B (start with binary mode with gap - exceeds 80 chars!)\n"); fprintf(stderr, " -c (color changes)\n"); - fprintf(stderr, " -f (filter on CAN-ID only)\n"); fprintf(stderr, " -t