Files
semi-libc/src/network/if_nametoindex.c
T
Ron YorstonandRich Felker 3cdbfb99c3 fix if_nametoindex return value when socket open fails
The return value of if_nametoindex is unsigned; it should return 0
on error.
2016-01-17 17:33:49 -05:00

19 lines
429 B
C

#define _GNU_SOURCE
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#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 0;
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;
}