Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isotpsniffer: option for no quitting on invalid message received #550

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions isotpsniffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@
#include <linux/can.h>
#include <linux/can/isotp.h>
#include <linux/sockios.h>
#include <errno.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please sort alphabetically


#define NO_CAN_ID 0xFFFFFFFFU

#define FORMAT_HEX 1
#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] <CAN interface>\n", prg);
Expand All @@ -79,6 +82,7 @@ void print_usage(char *prg)
fprintf(stderr, " -f <format> (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 <len> (head: print only first <len> bytes)\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");
}
Expand Down Expand Up @@ -189,15 +193,16 @@ int main(int argc, char **argv)
int head = 0;
int timestamp = 0;
int format = FORMAT_DEFAULT;
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?")) != -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);
Expand Down Expand Up @@ -249,6 +254,10 @@ int main(int argc, char **argv)
}
break;

case 'i':
ignore_errors = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

break;

case '?':
print_usage(basename(argv[0]));
goto out;
Expand Down Expand Up @@ -367,33 +376,39 @@ 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;
goto out;
if(!ignore_errors)
goto out;
}
if (nbytes > 4095) {
if (nbytes > (PDU_BUF_SIZE - 1)) {
r = 1;
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fprintf(stderr, "PDU length %d longer than PDU buffer size %d\n", nbytes, PDU_BUF_SIZE - 1);

(same applies for line 406)

Additionally please set PDU_BUF_SIZE to 8300 as suggested in my previous review.

Can you finally please put all these things into one single patch for the PR.
If this turns out to be too complicated, feel free to create a new PR.

Many thanks!

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)) {
nbytes = read(t, buffer, 4096);
nbytes = read(t, buffer, PDU_BUF_SIZE);
if (nbytes < 0) {
perror("read socket t");
r = 1;
goto out;
if(!ignore_errors)
goto out;
}
if (nbytes > 4095) {
if (nbytes > (PDU_BUF_SIZE - 1)) {
r = 1;
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
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);
}
}

Expand Down
Loading