cansniffer: make use of if_nametoindex()

Simplify the code as in other can-utils applications.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
pull/200/head
Oliver Hartkopp 2020-04-29 06:57:16 +02:00
parent 507ff82d81
commit 6f44582c19
1 changed files with 4 additions and 10 deletions

View File

@ -188,7 +188,6 @@ int main(int argc, char **argv)
int opt, ret; int opt, ret;
struct timeval timeo, start_tv, tv; struct timeval timeo, start_tv, tv;
struct sockaddr_can addr; struct sockaddr_can addr;
struct ifreq ifr;
int i; int i;
@ -260,21 +259,16 @@ int main(int argc, char **argv)
interface = argv[optind]; interface = argv[optind];
if ((s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM)) < 0) { s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
if (s < 0) {
perror("socket"); perror("socket");
return 1; return 1;
} }
addr.can_family = AF_CAN; addr.can_family = AF_CAN;
if (strcmp(ANYDEV, argv[optind])) { if (strcmp(ANYDEV, argv[optind]))
strcpy(ifr.ifr_name, argv[optind]); addr.can_ifindex = if_nametoindex(argv[optind]);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
exit(1);
}
addr.can_ifindex = ifr.ifr_ifindex;
}
else else
addr.can_ifindex = 0; /* any can interface */ addr.can_ifindex = 0; /* any can interface */