try SIOCGIFINDEX 1st to be able to use network aliasing.

Submitted by:	jlemon
Approved by:	re
This commit is contained in:
Hajimu UMEMOTO 2002-11-28 17:46:40 +00:00
parent af084166b5
commit c86d6b6cb7

View File

@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
@ -59,9 +60,21 @@ __FBSDID("$FreeBSD$");
unsigned int
if_nametoindex(const char *ifname)
{
int s;
struct ifreq ifr;
struct ifaddrs *ifaddrs, *ifa;
unsigned int ni;
s = _socket(AF_INET, SOCK_DGRAM, 0);
if (s != -1) {
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (_ioctl(s, SIOCGIFINDEX, &ifr) != -1) {
_close(s);
return (ifr.ifr_index);
}
_close(s);
}
if (getifaddrs(&ifaddrs) < 0)
return(0);