From 1e2c8fea4cfcc20561b65f6ff659d05f0eacdfc3 Mon Sep 17 00:00:00 2001 From: Mateusz Juzwiak Date: Fri, 19 Jul 2024 05:37:53 -0400 Subject: [PATCH 1/2] isotpsniffer: option for no quitting on invalid message received --- isotpsniffer.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/isotpsniffer.c b/isotpsniffer.c index 6ca29ae6..2cd06c49 100644 --- a/isotpsniffer.c +++ b/isotpsniffer.c @@ -79,6 +79,7 @@ void print_usage(char *prg) fprintf(stderr, " -f (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT); fprintf(stderr, " -L (set link layer options for CAN FD)\n"); fprintf(stderr, " -h (head: print only first bytes)\n"); + fprintf(stderr, " -q (don't quit on read error, allows to receive malformed frames)\n"); fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n"); fprintf(stderr, "\n"); } @@ -189,6 +190,7 @@ int main(int argc, char **argv) int head = 0; int timestamp = 0; int format = FORMAT_DEFAULT; + int noquit = 0; canid_t src = NO_CAN_ID; canid_t dst = NO_CAN_ID; extern int optind, opterr, optopt; @@ -197,7 +199,7 @@ int main(int argc, char **argv) unsigned char buffer[4096]; int nbytes; - while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?")) != -1) { + while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?q")) != -1) { switch (opt) { case 's': src = strtoul(optarg, NULL, 16); @@ -249,6 +251,10 @@ int main(int argc, char **argv) } break; + case 'q': + noquit = 1; + break; + case '?': print_usage(basename(argv[0])); goto out; @@ -371,14 +377,17 @@ int main(int argc, char **argv) if (nbytes < 0) { perror("read socket s"); r = 1; - goto out; + if(!noquit) + goto out; } if (nbytes > 4095) { r = 1; + perror("read socket s too much data"); goto out; } - printbuf(buffer, nbytes, color?2:0, timestamp, format, - &tv, &last_tv, dst, s, if_name, head); + if(nbytes > 0) + printbuf(buffer, nbytes, color?2:0, timestamp, format, + &tv, &last_tv, dst, s, if_name, head); } if (FD_ISSET(t, &rdfs)) { @@ -386,14 +395,17 @@ int main(int argc, char **argv) if (nbytes < 0) { perror("read socket t"); r = 1; - goto out; + if(!noquit) + goto out; } if (nbytes > 4095) { r = 1; + perror("read socket t too much data"); goto out; } - printbuf(buffer, nbytes, color?1:0, timestamp, format, - &tv, &last_tv, src, t, if_name, head); + if(nbytes > 0) + printbuf(buffer, nbytes, color?1:0, timestamp, format, + &tv, &last_tv, src, t, if_name, head); } } From 0cfff2e5da50aead50157fff7fcfcd245a5d2d91 Mon Sep 17 00:00:00 2001 From: Mateusz Juzwiak Date: Thu, 26 Sep 2024 14:19:31 +0200 Subject: [PATCH 2/2] isotpsniffer: change -q to -i parameter, const buffer size --- isotpsniffer.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/isotpsniffer.c b/isotpsniffer.c index 2cd06c49..1cbb0950 100644 --- a/isotpsniffer.c +++ b/isotpsniffer.c @@ -59,6 +59,7 @@ #include #include #include +#include #define NO_CAN_ID 0xFFFFFFFFU @@ -66,6 +67,8 @@ #define FORMAT_ASCII 2 #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX) +#define PDU_BUF_SIZE 4096 + void print_usage(char *prg) { fprintf(stderr, "\nUsage: %s [options] \n", prg); @@ -79,7 +82,7 @@ void print_usage(char *prg) fprintf(stderr, " -f (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT); fprintf(stderr, " -L (set link layer options for CAN FD)\n"); fprintf(stderr, " -h (head: print only first bytes)\n"); - fprintf(stderr, " -q (don't quit on read error, allows to receive malformed frames)\n"); + fprintf(stderr, " -i (ignore syscall errors to receive malformed PDUs)\n"); fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n"); fprintf(stderr, "\n"); } @@ -190,16 +193,16 @@ int main(int argc, char **argv) int head = 0; int timestamp = 0; int format = FORMAT_DEFAULT; - int noquit = 0; + int ignore_errors = 0; canid_t src = NO_CAN_ID; canid_t dst = NO_CAN_ID; extern int optind, opterr, optopt; static struct timeval tv, last_tv; - unsigned char buffer[4096]; + unsigned char buffer[PDU_BUF_SIZE]; int nbytes; - while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?q")) != -1) { + while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?i")) != -1) { switch (opt) { case 's': src = strtoul(optarg, NULL, 16); @@ -251,8 +254,8 @@ int main(int argc, char **argv) } break; - case 'q': - noquit = 1; + case 'i': + ignore_errors = 1; break; case '?': @@ -373,16 +376,16 @@ int main(int argc, char **argv) } if (FD_ISSET(s, &rdfs)) { - nbytes = read(s, buffer, 4096); + nbytes = read(s, buffer, PDU_BUF_SIZE); if (nbytes < 0) { perror("read socket s"); r = 1; - if(!noquit) + if(!ignore_errors) goto out; } - if (nbytes > 4095) { + if (nbytes > (PDU_BUF_SIZE - 1)) { r = 1; - perror("read socket s too much data"); + fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno)); goto out; } if(nbytes > 0) @@ -391,16 +394,16 @@ int main(int argc, char **argv) } if (FD_ISSET(t, &rdfs)) { - nbytes = read(t, buffer, 4096); + nbytes = read(t, buffer, PDU_BUF_SIZE); if (nbytes < 0) { perror("read socket t"); r = 1; - if(!noquit) + if(!ignore_errors) goto out; } - if (nbytes > 4095) { + if (nbytes > (PDU_BUF_SIZE - 1)) { r = 1; - perror("read socket t too much data"); + fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno)); goto out; } if(nbytes > 0)