#define _GNU_SOURCE #include #include #include #include #include "syscall.h" unsigned if_nametoindex(const char *name) { struct ifreq ifr; int fd, r; if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return -1; strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); r = ioctl(fd, SIOCGIFINDEX, &ifr); __syscall(SYS_close, fd); return r < 0 ? 0 : ifr.ifr_ifindex; }