diff options
author | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2003-08-19 22:19:09 +0000 |
---|---|---|
committer | Jun-ichiro itojun Hagino <itojun@cvs.openbsd.org> | 2003-08-19 22:19:09 +0000 |
commit | f0cf3adcad5d0247891eddb49c1e8ab30f13e513 (patch) | |
tree | b38fd2a0d84ace93f340f64d6f2676d91900add3 /usr.sbin/mopd/common/device.c | |
parent | 97545fe4089995279b1270673dd8950368a985e5 (diff) |
rewrite SIOCGIFCONF into getifaddrs. deraadt ok
Diffstat (limited to 'usr.sbin/mopd/common/device.c')
-rw-r--r-- | usr.sbin/mopd/common/device.c | 104 |
1 files changed, 29 insertions, 75 deletions
diff --git a/usr.sbin/mopd/common/device.c b/usr.sbin/mopd/common/device.c index 30e6b5b0c09..a6b289ac53e 100644 --- a/usr.sbin/mopd/common/device.c +++ b/usr.sbin/mopd/common/device.c @@ -1,4 +1,4 @@ -/* $OpenBSD: device.c,v 1.7 2003/06/02 21:38:39 maja Exp $ */ +/* $OpenBSD: device.c,v 1.8 2003/08/19 22:19:08 itojun Exp $ */ /* * Copyright (c) 1993-95 Mats O Jansson. All rights reserved. @@ -25,7 +25,7 @@ */ #ifndef LINT -static char rcsid[] = "$OpenBSD: device.c,v 1.7 2003/06/02 21:38:39 maja Exp $"; +static char rcsid[] = "$OpenBSD: device.c,v 1.8 2003/08/19 22:19:08 itojun Exp $"; #endif #include "os.h" @@ -58,34 +58,21 @@ deviceEthAddr(ifname, eaddr) struct sockaddr_dl *sdl; int fd; int i, len; + struct ifaddrs *ifap, *ifa; - /* We cannot use SIOCGIFADDR on the BPF descriptor. - We must instead get all the interfaces with SIOCGIFCONF - and find the right one. */ - - /* Use datagram socket to get Ethernet address. */ - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - syslog(LOG_ERR, "deviceEthAddr: socket: %m"); + if (getifaddrs(&ifap) != 0) { + syslog(LOG_ERR, "deviceEthAddr: getifaddrs: %m"); exit(1); } - ifc.ifc_len = sizeof(inbuf); - ifc.ifc_buf = inbuf; - if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) { - syslog(LOG_ERR, "deviceEthAddr: SIOGIFCONF: %m"); - exit(1); - } - ifr = ifc.ifc_req; - for (i = 0; i < ifc.ifc_len; - i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) { - len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len; - sdl = (struct sockaddr_dl *)&ifr->ifr_addr; + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + sdl = (struct sockaddr_dl *)ifa->ifa_addr; if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || sdl->sdl_alen != 6) continue; - if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) { + if (!strcmp(ifa->ifa_name, ifname)) { bcopy((caddr_t)LLADDR(sdl), (caddr_t)eaddr, 6); + freeifaddrs(ifap); return; } } @@ -262,74 +249,41 @@ deviceInitOne(ifname) void deviceInitAll() { -#ifdef DEV_NEW_CONF - char inbuf[8192]; - struct ifconf ifc; - struct ifreq *ifr; +#ifdef DEV_NEW_CONF struct sockaddr_dl *sdl; - int fd; - int i, len; + struct ifaddrs *ifap, *ifa; - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - syslog(LOG_ERR, "deviceInitAll: socket: %m"); + if (getifaddrs(&ifap) != 0) { + syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m"); exit(1); } - ifc.ifc_len = sizeof(inbuf); - ifc.ifc_buf = inbuf; - if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) { - syslog(LOG_ERR, "deviceInitAll: SIOCGIFCONF: %m"); - exit(1); - } - ifr = ifc.ifc_req; - for (i = 0; i < ifc.ifc_len; - i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) { - len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len; - sdl = (struct sockaddr_dl *)&ifr->ifr_addr; + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + sdl = (struct sockaddr_dl *)ifa->ifa_next; if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || sdl->sdl_alen != 6) continue; - if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) { - syslog(LOG_ERR, "deviceInitAll: SIOCGIFFLAGS: %m"); - continue; - } - if ((ifr->ifr_flags & + if ((ifa->ifa_flags & (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP) continue; - deviceInitOne(ifr->ifr_name); + deviceInitOne(ifa->ifa_name); } - (void) close(fd); + freeifaddrs(ifap); #else - int fd; - int n; - struct ifreq ibuf[8], *ifrp; - struct ifconf ifc; + struct ifaddrs *ifap, *ifa; - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - syslog(LOG_ERR, "deviceInitAll: old socket: %m"); - exit(1); - } - ifc.ifc_len = sizeof ibuf; - ifc.ifc_buf = (caddr_t)ibuf; - if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) { - syslog(LOG_ERR, "deviceInitAll: old SIOCGIFCONF: %m"); + if (getifaddrs(&ifap) != 0) { + syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m"); exit(1); } - ifrp = ibuf; - n = ifc.ifc_len / sizeof(*ifrp); - for (; --n >= 0; ++ifrp) { - if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) { - continue; - } - if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/ - ifrp->ifr_flags & IFF_LOOPBACK || - ifrp->ifr_flags & IFF_POINTOPOINT) + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/ + ifa->ifa_flags & IFF_LOOPBACK || + ifa->ifa_flags & IFF_POINTOPOINT) continue; - deviceInitOne(ifrp->ifr_name); + deviceInitOne(ifa->ifa_name); } - - (void) close(fd); -#endif /* DEV_NEW_CONF */ + freeifaddrs(ifap); +#endif } |