cansequence: main(): allow to bind on "any" interface

This is handy for running cansequence during an ifdown/ifup or a
rmmod/insmod cycle.
This commit is contained in:
Marc Kleine-Budde
2023-07-07 08:34:11 +02:00
committed by Oliver Hartkopp
parent 87def9b1fa
commit 0be066bee6
+7 -6
View File
@@ -28,6 +28,7 @@
#include <linux/can/raw.h> #include <linux/can/raw.h>
#define CAN_ID_DEFAULT (2) #define CAN_ID_DEFAULT (2)
#define ANYDEV "any" /* name of interface to receive from any CAN interface */
extern int optind, opterr, optopt; extern int optind, opterr, optopt;
@@ -248,7 +249,6 @@ int main(int argc, char **argv)
struct sigaction act = { struct sigaction act = {
.sa_handler = sig_handler, .sa_handler = sig_handler,
}; };
struct ifreq ifr;
struct sockaddr_can addr = { struct sockaddr_can addr = {
.can_family = AF_CAN, .can_family = AF_CAN,
}; };
@@ -345,12 +345,13 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); if (strcmp(ANYDEV, interface)) {
if (ioctl(s, SIOCGIFINDEX, &ifr)) { addr.can_ifindex = if_nametoindex(interface);
perror("ioctl()"); if (!addr.can_ifindex) {
exit(EXIT_FAILURE); perror("if_nametoindex");
exit(EXIT_FAILURE);
}
} }
addr.can_ifindex = ifr.ifr_ifindex;
/* first don't recv. any msgs */ /* first don't recv. any msgs */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) { if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0)) {