From 0be066bee617f004ef91aa42374d43a2100872cc Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 6 Jul 2023 15:49:39 +0200 Subject: [PATCH] cansequence: main(): allow to bind on "any" interface This is handy for running cansequence during an ifdown/ifup or a rmmod/insmod cycle. --- cansequence.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cansequence.c b/cansequence.c index 06da7a6..85444d3 100644 --- a/cansequence.c +++ b/cansequence.c @@ -28,6 +28,7 @@ #include #define CAN_ID_DEFAULT (2) +#define ANYDEV "any" /* name of interface to receive from any CAN interface */ extern int optind, opterr, optopt; @@ -248,7 +249,6 @@ int main(int argc, char **argv) struct sigaction act = { .sa_handler = sig_handler, }; - struct ifreq ifr; struct sockaddr_can addr = { .can_family = AF_CAN, }; @@ -345,12 +345,13 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); - if (ioctl(s, SIOCGIFINDEX, &ifr)) { - perror("ioctl()"); - exit(EXIT_FAILURE); + if (strcmp(ANYDEV, interface)) { + addr.can_ifindex = if_nametoindex(interface); + if (!addr.can_ifindex) { + perror("if_nametoindex"); + exit(EXIT_FAILURE); + } } - addr.can_ifindex = ifr.ifr_ifindex; /* first don't recv. any msgs */ if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) {