diff options
| author | itojun <itojun@NetBSD.org> | 2003-08-18 05:39:52 +0000 |
|---|---|---|
| committer | itojun <itojun@NetBSD.org> | 2003-08-18 05:39:52 +0000 |
| commit | 757e4585598c300e1fa666b5ac8595efc705d43e (patch) | |
| tree | 387e7bfc7da22cfd53a252c0555eab4e3c730851 /usr.sbin | |
| parent | dc4bf37abfea2d129cbcc62b42e0046c8ea4e1b8 (diff) | |
SIOCGIFCONF -> getifaddrs conversion
Diffstat (limited to 'usr.sbin')
| -rw-r--r-- | usr.sbin/bootp/common/getif.c | 16 | ||||
| -rw-r--r-- | usr.sbin/mopd/common/device.c | 96 | ||||
| -rw-r--r-- | usr.sbin/mopd/common/os.h | 5 | ||||
| -rw-r--r-- | usr.sbin/mrouted/cfparse.y | 62 | ||||
| -rw-r--r-- | usr.sbin/timed/timed/timed.c | 58 |
5 files changed, 81 insertions, 156 deletions
diff --git a/usr.sbin/bootp/common/getif.c b/usr.sbin/bootp/common/getif.c index fad3686dc26..a6b570d3227 100644 --- a/usr.sbin/bootp/common/getif.c +++ b/usr.sbin/bootp/common/getif.c @@ -1,8 +1,8 @@ -/* $NetBSD: getif.c,v 1.5 2002/07/14 00:26:17 wiz Exp $ */ +/* $NetBSD: getif.c,v 1.6 2003/08/18 05:39:52 itojun Exp $ */ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: getif.c,v 1.5 2002/07/14 00:26:17 wiz Exp $"); +__RCSID("$NetBSD: getif.c,v 1.6 2003/08/18 05:39:52 itojun Exp $"); #endif /* @@ -90,11 +90,13 @@ getif(int s, struct in_addr *addrp) len = ifconf.ifc_len; while (len > 0) { ifrq = (struct ifreq *) p; - sip = (struct sockaddr_in *) &ifrq->ifr_addr; - m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr)); - if (m > maxmatch) { - maxmatch = m; - ifrmax = ifrq; + if (ifrq->ifr_addr.sa_family == AF_INET) { + sip = (struct sockaddr_in *) &ifrq->ifr_addr; + m = nmatch((u_char *)addrp, (u_char *)&(sip->sin_addr)); + if (m > maxmatch) { + maxmatch = m; + ifrmax = ifrq; + } } /* XXX - Could this be just #ifndef IFNAMSIZ instead? -gwr */ #if (BSD - 0) < 43 diff --git a/usr.sbin/mopd/common/device.c b/usr.sbin/mopd/common/device.c index 05d968f91bf..bb8f9615c67 100644 --- a/usr.sbin/mopd/common/device.c +++ b/usr.sbin/mopd/common/device.c @@ -1,4 +1,4 @@ -/* $NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $ */ +/* $NetBSD: device.c,v 1.8 2003/08/18 05:39:52 itojun Exp $ */ /* * Copyright (c) 1993-95 Mats O Jansson. All rights reserved. @@ -31,7 +31,7 @@ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $"); +__RCSID("$NetBSD: device.c,v 1.8 2003/08/18 05:39:52 itojun Exp $"); #endif #include "os.h" @@ -55,40 +55,25 @@ deviceEthAddr(ifname, eaddr) char *ifname; u_char *eaddr; { - char inbuf[8192]; - struct ifconf ifc; - struct ifreq *ifr; 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. */ + if (getifaddrs(&ifap) != 0) + mopLogErr("deviceEthAddr: getifaddrs"); - /* Use datagram socket to get Ethernet address. */ - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) - mopLogErr("deviceEthAddr: socket"); - - ifc.ifc_len = sizeof(inbuf); - ifc.ifc_buf = inbuf; - if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) - mopLogErr("deviceEthAddr: SIOGIFCONF"); - 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)) { memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6); + freeifaddrs(ifap); return; } } + freeifaddrs(ifap); mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname); } #endif /* DEV_NEW_CONF */ @@ -247,65 +232,38 @@ void deviceInitAll() { #ifdef DEV_NEW_CONF - char inbuf[8192]; - struct ifconf ifc; - struct ifreq *ifr; struct sockaddr_dl *sdl; - int fd; - int i, len; + struct ifaddrs *ifap, *ifa; - if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) + if (getifaddrs(&ifap) != 0) mopLogErr("deviceInitAll: socket"); - ifc.ifc_len = sizeof(inbuf); - ifc.ifc_buf = inbuf; - if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) - mopLogErr("deviceInitAll: SIOCGIFCONF"); - 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 (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) { - mopLogWarn("deviceInitAll: SIOCGIFFLAGS"); - 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) + if (getifaddrs(&ifap) != 0) mopLogErr("deviceInitAll: old socket"); - ifc.ifc_len = sizeof ibuf; - ifc.ifc_buf = (caddr_t)ibuf; - if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) - mopLogErr("deviceInitAll: old SIOCGIFCONF"); - 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); + freeifaddrs(ifap); #endif /* DEV_NEW_CONF */ } diff --git a/usr.sbin/mopd/common/os.h b/usr.sbin/mopd/common/os.h index c4e5a7cee53..80e705746fb 100644 --- a/usr.sbin/mopd/common/os.h +++ b/usr.sbin/mopd/common/os.h @@ -1,4 +1,4 @@ -/* $NetBSD: os.h,v 1.4 2001/01/16 02:50:31 cgd Exp $ */ +/* $NetBSD: os.h,v 1.5 2003/08/18 05:39:52 itojun Exp $ */ /* * Copyright (c) 1994-95 Mats O Jansson. All rights reserved. @@ -28,7 +28,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $NetBSD: os.h,v 1.4 2001/01/16 02:50:31 cgd Exp $ + * $NetBSD: os.h,v 1.5 2003/08/18 05:39:52 itojun Exp $ * */ @@ -55,6 +55,7 @@ #include <syslog.h> #include <time.h> #include <unistd.h> +#include <ifaddrs.h> #define DEV_NEW_CONF diff --git a/usr.sbin/mrouted/cfparse.y b/usr.sbin/mrouted/cfparse.y index 664a00bb18b..f2a4b9ed7ca 100644 --- a/usr.sbin/mrouted/cfparse.y +++ b/usr.sbin/mrouted/cfparse.y @@ -1,5 +1,5 @@ %{ -/* $NetBSD: cfparse.y,v 1.14 2003/07/13 12:40:17 itojun Exp $ */ +/* $NetBSD: cfparse.y,v 1.15 2003/08/18 05:39:53 itojun Exp $ */ /* * Configuration file parser for mrouted. @@ -26,6 +26,7 @@ #include <stdarg.h> #include "defs.h" #include <netdb.h> +#include <ifaddrs.h> /* * Local function declarations @@ -38,7 +39,7 @@ static void yyerror(char *s); static char * next_word(void); static int yylex(void); static u_int32_t valid_if(char *s); -static struct ifreq * ifconfaddr(struct ifconf *ifcp, u_int32_t a); +static const char * ifconfaddr(u_int32_t a); int yyparse(void); static FILE *f __attribute__((__unused__)); /* XXX egcs */ @@ -49,8 +50,6 @@ extern int cache_lifetime; extern int max_prune_lifetime; static int lineno; -static struct ifreq ifbuf[32]; -static struct ifconf ifc; static struct uvif *v; @@ -128,26 +127,25 @@ stmt : error } ifmods | TUNNEL interface addrname { - - struct ifreq *ifr; + const char *ifname; struct ifreq ffr; vifi_t vifi; order++; - ifr = ifconfaddr(&ifc, $2); - if (ifr == 0) + ifname = ifconfaddr($2); + if (ifname == 0) fatal("Tunnel local address %s is not mine", inet_fmt($2)); - strncpy(ffr.ifr_name, ifr->ifr_name, IFNAMSIZ); + strncpy(ffr.ifr_name, ifname, sizeof(ffr.ifr_name)); if (ioctl(udp_socket, SIOCGIFFLAGS, (char *)&ffr)<0) fatal("ioctl SIOCGIFFLAGS on %s",ffr.ifr_name); if (ffr.ifr_flags & IFF_LOOPBACK) fatal("Tunnel local address %s is a loopback interface", inet_fmt($2)); - if (ifconfaddr(&ifc, $3) != 0) + if (ifconfaddr($3) != 0) fatal("Tunnel remote address %s is one of mine", inet_fmt($3)); @@ -569,11 +567,6 @@ config_vifs_from_file() return; } - ifc.ifc_buf = (char *)ifbuf; - ifc.ifc_len = sizeof(ifbuf); - if (ioctl(udp_socket, SIOCGIFCONF, (char *)&ifc) < 0) - logit(LOG_ERR, errno, "ioctl SIOCGIFCONF"); - yyparse(); fclose(f); @@ -593,28 +586,25 @@ char *s; return 0; } -static struct ifreq * -ifconfaddr(ifcp, a) - struct ifconf *ifcp; +static const char * +ifconfaddr(a) u_int32_t a; { - int n; - struct ifreq *ifrp = (struct ifreq *)ifcp->ifc_buf; - struct ifreq *ifend = (struct ifreq *)((char *)ifrp + ifcp->ifc_len); - - while (ifrp < ifend) { - if (ifrp->ifr_addr.sa_family == AF_INET && - ((struct sockaddr_in *)&ifrp->ifr_addr)->sin_addr.s_addr == a) - return (ifrp); -#if (defined(BSD) && (BSD >= 199006)) - n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name); - if (n < sizeof(*ifrp)) - ++ifrp; - else - ifrp = (struct ifreq *)((char *)ifrp + n); -#else - ++ifrp; -#endif + static char ifname[IFNAMSIZ]; + struct ifaddrs *ifap, *ifa; + + if (getifaddrs(&ifap) != 0) + return (NULL); + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family == AF_INET && + ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == a) { + strlcpy(ifname, ifa->ifa_name, sizeof(ifname)); + freeifaddrs(ifap); + return (ifname); + } } - return (0); + + freeifaddrs(ifap); + return (NULL); } diff --git a/usr.sbin/timed/timed/timed.c b/usr.sbin/timed/timed/timed.c index 17f39c3efb4..cbb025b18b5 100644 --- a/usr.sbin/timed/timed/timed.c +++ b/usr.sbin/timed/timed/timed.c @@ -1,4 +1,4 @@ -/* $NetBSD: timed.c,v 1.18 2003/08/07 11:25:47 agc Exp $ */ +/* $NetBSD: timed.c,v 1.19 2003/08/18 05:39:53 itojun Exp $ */ /*- * Copyright (c) 1985, 1993 The Regents of the University of California. @@ -40,7 +40,7 @@ __COPYRIGHT( #if 0 static char sccsid[] = "@(#)timed.c 8.2 (Berkeley) 3/26/95"; #else -__RCSID("$NetBSD: timed.c,v 1.18 2003/08/07 11:25:47 agc Exp $"); +__RCSID("$NetBSD: timed.c,v 1.19 2003/08/18 05:39:53 itojun Exp $"); #endif #endif /* not lint */ @@ -55,6 +55,7 @@ __RCSID("$NetBSD: timed.c,v 1.18 2003/08/07 11:25:47 agc Exp $"); #include <sys/types.h> #include <sys/times.h> #include <util.h> +#include <ifaddrs.h> #ifdef HAVENIS #include <netgroup.h> @@ -124,9 +125,6 @@ main(int argc, char *argv[]) int nflag, iflag; struct timeval ntime; struct servent *srvp; - char buf[BUFSIZ], *cp, *cplim; - struct ifconf ifc; - struct ifreq ifreq, ifreqf, *ifr; register struct netinfo *ntp; struct netinfo *ntip; struct netinfo *savefromnet; @@ -137,6 +135,7 @@ main(int argc, char *argv[]) int c; extern char *optarg; extern int optind, opterr; + struct ifaddrs *ifap, *ifa; #define IN_MSG "timed: -i and -n make no sense together\n" #ifdef HAVENIS @@ -296,64 +295,38 @@ main(int argc, char *argv[]) if (0 == (nt->net & 0xff000000)) nt->net <<= 8; } - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { + if (getifaddrs(&ifap) != 0) { perror("timed: get interface configuration"); exit(1); } ntp = NULL; -#define size(p) max((p).sa_len, sizeof(p)) - cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ - for (cp = buf; cp < cplim; - cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { - ifr = (struct ifreq *)cp; - if (ifr->ifr_addr.sa_family != AF_INET) + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_INET) continue; if (!ntp) ntp = (struct netinfo*)malloc(sizeof(struct netinfo)); bzero(ntp,sizeof(*ntp)); - ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; + ntp->my_addr=((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; ntp->status = NOMASTER; - ifreq = *ifr; - ifreqf = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) { - perror("get interface flags"); + if ((ifa->ifa_flags & IFF_UP) == 0) continue; - } - if ((ifreqf.ifr_flags & IFF_UP) == 0) - continue; - if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 && - (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) { + if ((ifa->ifa_flags & IFF_BROADCAST) == 0 && + (ifa->ifa_flags & IFF_POINTOPOINT) == 0) { continue; } - - if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) { - perror("get netmask"); - continue; - } ntp->mask = ((struct sockaddr_in *) - &ifreq.ifr_addr)->sin_addr.s_addr; + ifa->ifa_netmask)->sin_addr.s_addr; - if (ifreqf.ifr_flags & IFF_BROADCAST) { - if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { - perror("get broadaddr"); - continue; - } - ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr; + if (ifa->ifa_flags & IFF_BROADCAST) { + ntp->dest_addr = *(struct sockaddr_in *)ifa->ifa_broadaddr; /* What if the broadcast address is all ones? * So we cannot just mask ntp->dest_addr. */ ntp->net = ntp->my_addr; ntp->net.s_addr &= ntp->mask; } else { - if (ioctl(sock, SIOCGIFDSTADDR, - (char *)&ifreq) < 0) { - perror("get destaddr"); - continue; - } - ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr; + ntp->dest_addr = *(struct sockaddr_in *)ifa->ifa_dstaddr; ntp->net = ntp->dest_addr.sin_addr; } @@ -375,6 +348,7 @@ main(int argc, char *argv[]) ntip = ntp; ntp = NULL; } + freeifaddrs(ifap); if (ntp) (void) free((char *)ntp); if (nettab == NULL) { |
