diff options
author | jlemon <jlemon@FreeBSD.org> | 2001-10-17 20:08:15 +0000 |
---|---|---|
committer | jlemon <jlemon@FreeBSD.org> | 2001-10-17 20:08:15 +0000 |
commit | f8c01ba3e2f3a2fa18c54b073092e62c01979de5 (patch) | |
tree | 85e95bff7a8fc4d6e1dd7cf22d7764636155ecb3 /lib/libc/net/ifname.c | |
parent | 85e1c0879143bd206c275035a79c73a212654cfa (diff) | |
download | FreeBSD-src-f8c01ba3e2f3a2fa18c54b073092e62c01979de5.zip FreeBSD-src-f8c01ba3e2f3a2fa18c54b073092e62c01979de5.tar.gz |
Use the new SIOCGIFINDEX ioctl to efficiently map a name to an index.
If the syscall fails, fall back on the old method as a compatability
measure.
Diffstat (limited to 'lib/libc/net/ifname.c')
-rw-r--r-- | lib/libc/net/ifname.c | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/libc/net/ifname.c b/lib/libc/net/ifname.c index 3d3916a..94fb951 100644 --- a/lib/libc/net/ifname.c +++ b/lib/libc/net/ifname.c @@ -35,6 +35,7 @@ #include <sys/param.h> #include <sys/socket.h> +#include <sys/sockio.h> #include <sys/sysctl.h> #include <net/if.h> #include <net/route.h> @@ -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) |