pull/550/merge
Mathew 2025-11-27 18:24:41 +01:00 committed by GitHub
commit dacd9da569
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 27 additions and 12 deletions

View File

@ -59,6 +59,7 @@
#include <linux/can.h> #include <linux/can.h>
#include <linux/can/isotp.h> #include <linux/can/isotp.h>
#include <linux/sockios.h> #include <linux/sockios.h>
#include <errno.h>
#define NO_CAN_ID 0xFFFFFFFFU #define NO_CAN_ID 0xFFFFFFFFU
@ -66,6 +67,8 @@
#define FORMAT_ASCII 2 #define FORMAT_ASCII 2
#define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX) #define FORMAT_DEFAULT (FORMAT_ASCII | FORMAT_HEX)
#define PDU_BUF_SIZE 4096
void print_usage(char *prg) void print_usage(char *prg)
{ {
fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg); fprintf(stderr, "\nUsage: %s [options] <CAN interface>\n", prg);
@ -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, " -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, " -L (set link layer options for CAN FD)\n");
fprintf(stderr, " -h <len> (head: print only first <len> bytes)\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, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@ -189,15 +193,16 @@ int main(int argc, char **argv)
int head = 0; int head = 0;
int timestamp = 0; int timestamp = 0;
int format = FORMAT_DEFAULT; int format = FORMAT_DEFAULT;
int ignore_errors = 0;
canid_t src = NO_CAN_ID; canid_t src = NO_CAN_ID;
canid_t dst = NO_CAN_ID; canid_t dst = NO_CAN_ID;
extern int optind, opterr, optopt; extern int optind, opterr, optopt;
static struct timeval tv, last_tv; static struct timeval tv, last_tv;
unsigned char buffer[4096]; unsigned char buffer[PDU_BUF_SIZE];
int nbytes; 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) { switch (opt) {
case 's': case 's':
src = strtoul(optarg, NULL, 16); src = strtoul(optarg, NULL, 16);
@ -249,6 +254,10 @@ int main(int argc, char **argv)
} }
break; break;
case 'i':
ignore_errors = 1;
break;
case '?': case '?':
print_usage(basename(argv[0])); print_usage(basename(argv[0]));
goto out; goto out;
@ -367,33 +376,39 @@ int main(int argc, char **argv)
} }
if (FD_ISSET(s, &rdfs)) { if (FD_ISSET(s, &rdfs)) {
nbytes = read(s, buffer, 4096); nbytes = read(s, buffer, PDU_BUF_SIZE);
if (nbytes < 0) { if (nbytes < 0) {
perror("read socket s"); perror("read socket s");
r = 1; r = 1;
goto out; if(!ignore_errors)
goto out;
} }
if (nbytes > 4095) { if (nbytes > (PDU_BUF_SIZE - 1)) {
r = 1; r = 1;
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
goto out; goto out;
} }
printbuf(buffer, nbytes, color?2:0, timestamp, format, if(nbytes > 0)
&tv, &last_tv, dst, s, if_name, head); printbuf(buffer, nbytes, color?2:0, timestamp, format,
&tv, &last_tv, dst, s, if_name, head);
} }
if (FD_ISSET(t, &rdfs)) { if (FD_ISSET(t, &rdfs)) {
nbytes = read(t, buffer, 4096); nbytes = read(t, buffer, PDU_BUF_SIZE);
if (nbytes < 0) { if (nbytes < 0) {
perror("read socket t"); perror("read socket t");
r = 1; r = 1;
goto out; if(!ignore_errors)
goto out;
} }
if (nbytes > 4095) { if (nbytes > (PDU_BUF_SIZE - 1)) {
r = 1; r = 1;
fprintf(stderr, "PDU length %d longer than PDU buffer: %s\n", nbytes, strerror(errno));
goto out; goto out;
} }
printbuf(buffer, nbytes, color?1:0, timestamp, format, if(nbytes > 0)
&tv, &last_tv, src, t, if_name, head); printbuf(buffer, nbytes, color?1:0, timestamp, format,
&tv, &last_tv, src, t, if_name, head);
} }
} }