Skip to content

Commit 0cfff2e

Browse files
committed
isotpsniffer: change -q to -i parameter, const buffer size
1 parent 1e2c8fe commit 0cfff2e

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

isotpsniffer.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@
5959
#include <linux/can.h>
6060
#include <linux/can/isotp.h>
6161
#include <linux/sockios.h>
62+
#include <errno.h>
6263

6364
#define NO_CAN_ID 0xFFFFFFFFU
6465

6566
#define FORMAT_HEX 1
6667
#define FORMAT_ASCII 2
6768
#define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
6869

70+
#define PDU_BUF_SIZE 4096
71+
6972
void print_usage(char *prg)
7073
{
7174
fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
@@ -79,7 +82,7 @@ void print_usage(char *prg)
7982
fprintf(stderr, " -f <format> (1 = HEX, 2 = ASCII, 3 = HEX & ASCII - default: %d)\n", FORMAT_DEFAULT);
8083
fprintf(stderr, " -L (set link layer options for CAN FD)\n");
8184
fprintf(stderr, " -h <len> (head: print only first <len> bytes)\n");
82-
fprintf(stderr, " -q (don't quit on read error, allows to receive malformed frames)\n");
85+
fprintf(stderr, " -i (ignore syscall errors to receive malformed PDUs)\n");
8386
fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
8487
fprintf(stderr, "\n");
8588
}
@@ -190,16 +193,16 @@ int main(int argc, char **argv)
190193
int head = 0;
191194
int timestamp = 0;
192195
int format = FORMAT_DEFAULT;
193-
int noquit = 0;
196+
int ignore_errors = 0;
194197
canid_t src = NO_CAN_ID;
195198
canid_t dst = NO_CAN_ID;
196199
extern int optind, opterr, optopt;
197200
static struct timeval tv, last_tv;
198201

199-
unsigned char buffer[4096];
202+
unsigned char buffer[PDU_BUF_SIZE];
200203
int nbytes;
201204

202-
while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?q")) != -1) {
205+
while ((opt = getopt(argc, argv, "s:d:x:X:h:ct:f:L?i")) != -1) {
203206
switch (opt) {
204207
case 's':
205208
src = strtoul(optarg, NULL, 16);
@@ -251,8 +254,8 @@ int main(int argc, char **argv)
251254
}
252255
break;
253256

254-
case 'q':
255-
noquit = 1;
257+
case 'i':
258+
ignore_errors = 1;
256259
break;
257260

258261
case '?':
@@ -373,16 +376,16 @@ int main(int argc, char **argv)
373376
}
374377

375378
if (FD_ISSET(s, &rdfs)) {
376-
nbytes = read(s, buffer, 4096);
379+
nbytes = read(s, buffer, PDU_BUF_SIZE);
377380
if (nbytes < 0) {
378381
perror("read socket s");
379382
r = 1;
380-
if(!noquit)
383+
if(!ignore_errors)
381384
goto out;
382385
}
383-
if (nbytes > 4095) {
386+
if (nbytes > (PDU_BUF_SIZE - 1)) {
384387
r = 1;
385-
perror("read socket s too much data");
388+
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
386389
goto out;
387390
}
388391
if(nbytes > 0)
@@ -391,16 +394,16 @@ int main(int argc, char **argv)
391394
}
392395

393396
if (FD_ISSET(t, &rdfs)) {
394-
nbytes = read(t, buffer, 4096);
397+
nbytes = read(t, buffer, PDU_BUF_SIZE);
395398
if (nbytes < 0) {
396399
perror("read socket t");
397400
r = 1;
398-
if(!noquit)
401+
if(!ignore_errors)
399402
goto out;
400403
}
401-
if (nbytes > 4095) {
404+
if (nbytes > (PDU_BUF_SIZE - 1)) {
402405
r = 1;
403-
perror("read socket t too much data");
406+
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
404407
goto out;
405408
}
406409
if(nbytes > 0)

0 commit comments

Comments
 (0)