Home | History | Annotate | Line # | Download | only in traceroute
ifaddrlist.c revision 1.1
      1 /*	$NetBSD: ifaddrlist.c,v 1.1 1997/10/03 22:25:12 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the Computer Systems
     18  *	Engineering Group at Lawrence Berkeley Laboratory.
     19  * 4. Neither the name of the University nor of the Laboratory may be used
     20  *    to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static const char rcsid[] =
     40     "@(#) Header: ifaddrlist.c,v 1.2 97/04/22 13:31:05 leres Exp  (LBL)";
     41 #else
     42 __RCSID("$NetBSD: ifaddrlist.c,v 1.1 1997/10/03 22:25:12 christos Exp $");
     43 #endif
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/file.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/socket.h>
     50 #ifdef HAVE_SYS_SOCKIO_H
     51 #include <sys/sockio.h>
     52 #endif
     53 #include <sys/time.h>				/* concession to AIX */
     54 
     55 #if __STDC__
     56 struct mbuf;
     57 struct rtentry;
     58 #endif
     59 
     60 #include <net/if.h>
     61 #include <netinet/in.h>
     62 
     63 #include <ctype.h>
     64 #include <errno.h>
     65 #include <memory.h>
     66 #include <stdio.h>
     67 #include <stdlib.h>
     68 #include <string.h>
     69 #include <unistd.h>
     70 
     71 #include "gnuc.h"
     72 #ifdef HAVE_OS_PROTO_H
     73 #include "os-proto.h"
     74 #endif
     75 
     76 #include "ifaddrlist.h"
     77 #include "savestr.h"
     78 
     79 
     80 /* Not all systems have IFF_LOOPBACK */
     81 #ifdef IFF_LOOPBACK
     82 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
     83 #else
     84 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
     85 #endif
     86 
     87 #define MAX_IPADDR 32
     88 
     89 /*
     90  * Return the interface list
     91  */
     92 int
     93 ifaddrlist(register struct ifaddrlist **ipaddrp, register char *errbuf)
     94 {
     95 	register int fd, nipaddr;
     96 #ifdef HAVE_SOCKADDR_SA_LEN
     97 	register int n;
     98 #endif
     99 	register struct ifreq *ifrp, *ifend, *ifnext, *mp;
    100 	register struct sockaddr_in *sin;
    101 	register struct ifaddrlist *al;
    102 	struct ifconf ifc;
    103 	struct ifreq ibuf[MAX_IPADDR], ifr;
    104 	char device[sizeof(ifr.ifr_name) + 1];
    105 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
    106 
    107 	fd = socket(AF_INET, SOCK_DGRAM, 0);
    108 	if (fd < 0) {
    109 		(void)sprintf(errbuf, "socket: %s", strerror(errno));
    110 		return (-1);
    111 	}
    112 	ifc.ifc_len = sizeof(ibuf);
    113 	ifc.ifc_buf = (caddr_t)ibuf;
    114 
    115 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
    116 	    ifc.ifc_len < sizeof(struct ifreq)) {
    117 		(void)sprintf(errbuf, "SIOCGIFCONF: %s", strerror(errno));
    118 		(void)close(fd);
    119 		return (-1);
    120 	}
    121 	ifrp = ibuf;
    122 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
    123 
    124 	al = ifaddrlist;
    125 	mp = NULL;
    126 	nipaddr = 0;
    127 	for (; ifrp < ifend; ifrp = ifnext) {
    128 #ifdef HAVE_SOCKADDR_SA_LEN
    129 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    130 		if (n < sizeof(*ifrp))
    131 			ifnext = ifrp + 1;
    132 		else
    133 			ifnext = (struct ifreq *)((char *)ifrp + n);
    134 		if (ifrp->ifr_addr.sa_family != AF_INET)
    135 			continue;
    136 #else
    137 		ifnext = ifrp + 1;
    138 #endif
    139 		/*
    140 		 * Need a template to preserve address info that is
    141 		 * used below to locate the next entry.  (Otherwise,
    142 		 * SIOCGIFFLAGS stomps over it because the requests
    143 		 * are returned in a union.)
    144 		 */
    145 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
    146 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
    147 			if (errno == ENXIO)
    148 				continue;
    149 			(void)sprintf(errbuf, "SIOCGIFFLAGS: %.*s: %s",
    150 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
    151 			    strerror(errno));
    152 			(void)close(fd);
    153 			return (-1);
    154 		}
    155 
    156 		/* Must be up and not the loopback */
    157 		if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr))
    158 			continue;
    159 
    160 		(void)strncpy(device, ifr.ifr_name, sizeof(ifr.ifr_name));
    161 		device[sizeof(device) - 1] = '\0';
    162 		if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
    163 			(void)sprintf(errbuf, "SIOCGIFADDR: %s: %s",
    164 			    device, strerror(errno));
    165 			(void)close(fd);
    166 			return (-1);
    167 		}
    168 
    169 		sin = (struct sockaddr_in *)&ifr.ifr_addr;
    170 		al->addr = sin->sin_addr.s_addr;
    171 		al->device = savestr(device);
    172 		++al;
    173 		++nipaddr;
    174 	}
    175 	(void)close(fd);
    176 
    177 	*ipaddrp = ifaddrlist;
    178 	return (nipaddr);
    179 }
    180