Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin SIOCGIFCONF -> getifaddrs conversion



details:   https://anonhg.NetBSD.org/src/rev/407e44bcd6ab
branches:  trunk
changeset: 550702:407e44bcd6ab
user:      itojun <itojun%NetBSD.org@localhost>
date:      Mon Aug 18 05:39:52 2003 +0000

description:
SIOCGIFCONF -> getifaddrs conversion

diffstat:

 usr.sbin/bootp/common/getif.c |  16 ++++---
 usr.sbin/mopd/common/device.c |  96 ++++++++++++------------------------------
 usr.sbin/mopd/common/os.h     |   5 +-
 usr.sbin/mrouted/cfparse.y    |  60 +++++++++++---------------
 usr.sbin/timed/timed/timed.c  |  60 +++++++-------------------
 5 files changed, 81 insertions(+), 156 deletions(-)

diffs (truncated from 464 to 300 lines):

diff -r 40f781a789c2 -r 407e44bcd6ab usr.sbin/bootp/common/getif.c
--- a/usr.sbin/bootp/common/getif.c     Mon Aug 18 05:39:07 2003 +0000
+++ b/usr.sbin/bootp/common/getif.c     Mon Aug 18 05:39:52 2003 +0000
@@ -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 @@
        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 -r 40f781a789c2 -r 407e44bcd6ab usr.sbin/mopd/common/device.c
--- a/usr.sbin/mopd/common/device.c     Mon Aug 18 05:39:07 2003 +0000
+++ b/usr.sbin/mopd/common/device.c     Mon Aug 18 05:39:52 2003 +0000
@@ -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 @@
        char *ifname;
         u_char *eaddr;
 {
-       char inbuf[8192];
-       struct ifconf ifc;
-       struct ifreq *ifr;
        struct sockaddr_dl *sdl;
-       int fd;
-       int i, len;
-
-       /* We cannot use SIOCGIFADDR on the BPF descriptor.
-          We must instead get all the interfaces with SIOCGIFCONF
-          and find the right one.  */
+       struct ifaddrs *ifap, *ifa;
 
-       /* Use datagram socket to get Ethernet address. */
-       if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
-               mopLogErr("deviceEthAddr: socket");
+       if (getifaddrs(&ifap) != 0)
+               mopLogErr("deviceEthAddr: getifaddrs");
 
-       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 @@
 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) {
+
+       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;
-               }
-               if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
-                   ifrp->ifr_flags & IFF_LOOPBACK ||
-                   ifrp->ifr_flags & IFF_POINTOPOINT)
-                       continue;
-               deviceInitOne(ifrp->ifr_name);
+               deviceInitOne(ifa->ifa_name);
        }
        
-       (void) close(fd);
+       freeifaddrs(ifap);
 #endif /* DEV_NEW_CONF */
 }
diff -r 40f781a789c2 -r 407e44bcd6ab usr.sbin/mopd/common/os.h
--- a/usr.sbin/mopd/common/os.h Mon Aug 18 05:39:07 2003 +0000
+++ b/usr.sbin/mopd/common/os.h Mon Aug 18 05:39:52 2003 +0000
@@ -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 -r 40f781a789c2 -r 407e44bcd6ab usr.sbin/mrouted/cfparse.y
--- a/usr.sbin/mrouted/cfparse.y        Mon Aug 18 05:39:07 2003 +0000
+++ b/usr.sbin/mrouted/cfparse.y        Mon Aug 18 05:39:52 2003 +0000
@@ -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 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 max_prune_lifetime;
 
 static int lineno;
-static struct ifreq ifbuf[32];
-static struct ifconf ifc;
 
 static struct uvif *v;
 
@@ -128,26 +127,25 @@
                                        }
                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 @@
            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 @@
        return 0;
 }
 
-static struct ifreq *
-ifconfaddr(ifcp, a)
-    struct ifconf *ifcp;
+static const char *
+ifconfaddr(a)
     u_int32_t a;
 {



Home | Main Index | Thread Index | Old Index