diff --git a/lib/libc/net/ifname.c b/lib/libc/net/ifname.c index 3d3916a458ac..94fb9513a747 100644 --- a/lib/libc/net/ifname.c +++ b/lib/libc/net/ifname.c @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -48,8 +49,8 @@ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) -unsigned int -if_nametoindex(ifname) +static unsigned int +if_onametoindex(ifname) const char *ifname; { struct if_nameindex *iff = if_nameindex(), *ifx; @@ -69,6 +70,25 @@ if_nametoindex(ifname) errno = ENXIO; return 0; } + +unsigned int +if_nametoindex(ifname) + const char *ifname; +{ + int s; + struct ifreq ifr; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) + return (0); + strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); + if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) { + close (s); + return (if_onametoindex(ifname)); + } + close(s); + return (ifr.ifr_index); +} char * if_indextoname(ifindex, ifname)