Compare commits

...

5 Commits

Author SHA1 Message Date
Mathew 7abf53c4d0
Merge 0cfff2e5da into 287469245c 2025-04-09 05:30:12 -04:00
Marc Kleine-Budde 287469245c
Merge pull request #123 from twasilczyk/slcand-android
Don't fail when it wasn't possible to fetch the interface name.
2025-04-06 10:11:38 +02:00
Tomasz Wasilczyk 7a318636e7 slcand: Don't fail when it wasn't possible to fetch the interface name.
On some systems SIOCGIFNAME may fail with ENOTTY, but the actual
slcanX interface gets properly configured. Instead of crashing hard on
such case, let's gracefuly degrade and just not display the interface
name.
2025-04-06 10:03:36 +02:00
Mateusz Juzwiak 0cfff2e5da isotpsniffer: change -q to -i parameter, const buffer size 2024-09-26 14:31:06 +02:00
Mateusz Juzwiak 1e2c8fea4c isotpsniffer: option for no quitting on invalid message received 2024-07-19 05:37:53 -04:00
2 changed files with 34 additions and 14 deletions

View File

@ -59,6 +59,7 @@
#include <linux/can.h>
#include <linux/can/isotp.h>
#include <linux/sockios.h>
#include <errno.h>
#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] <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, " -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");
}
@ -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);
@ -249,6 +254,10 @@ int main(int argc, char **argv)
}
break;
case 'i':
ignore_errors = 1;
break;
case '?':
print_usage(basename(argv[0]));
goto out;
@ -367,31 +376,37 @@ 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(!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;
}
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;
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;
}
if(nbytes > 0)
printbuf(buffer, nbytes, color?1:0, timestamp, format,
&tv, &last_tv, src, t, if_name, head);
}

View File

@ -370,8 +370,13 @@ int main(int argc, char *argv[])
/* retrieve the name of the created CAN netdevice */
if (ioctl(fd, SIOCGIFNAME, ifr.ifr_name) < 0) {
if (name) {
perror("ioctl SIOCGIFNAME");
exit(EXIT_FAILURE);
} else {
/* Graceful degradation: we only needed the name for display. */
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "<unknown>");
}
}
syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, ifr.ifr_name);