Home | History | Annotate | Line # | Download | only in traceroute
ifaddrlist.c revision 1.6
      1 /*	$NetBSD: ifaddrlist.c,v 1.6 2003/05/15 14:34:39 itojun 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.6 2003/05/15 14:34:39 itojun 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 struct mbuf;
     56 struct rtentry;
     57 
     58 #include <net/if.h>
     59 #include <netinet/in.h>
     60 #include <arpa/inet.h>
     61 
     62 #include <ctype.h>
     63 #include <errno.h>
     64 #include <memory.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 #ifdef HAVE_IFADDRS_H
     70 #include <ifaddrs.h>
     71 #endif
     72 
     73 #include "gnuc.h"
     74 #ifdef HAVE_OS_PROTO_H
     75 #include "os-proto.h"
     76 #endif
     77 
     78 #include "ifaddrlist.h"
     79 
     80 
     81 /* Not all systems have IFF_LOOPBACK */
     82 #ifdef HAVE_IFADDRS_H
     83 #ifdef IFF_LOOPBACK
     84 #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
     85 #else
     86 #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
     87 #endif
     88 #else
     89 #ifdef IFF_LOOPBACK
     90 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
     91 #else
     92 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
     93 #endif
     94 #endif
     95 
     96 #define MAX_IPADDR 256
     97 
     98 /*
     99  * Return the interface list
    100  */
    101 int
    102 ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, int buflen)
    103 {
    104 #ifdef HAVE_IFADDRS_H
    105 	int nipaddr;
    106 	struct sockaddr_in *sin;
    107 	struct ifaddrs *ifap, *ifa;
    108 	struct ifaddrlist *al;
    109 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
    110 
    111 	al = ifaddrlist;
    112 	nipaddr = 0;
    113 
    114 	if (getifaddrs(&ifap) != 0) {
    115 		(void)snprintf(errbuf, buflen, "getifaddrs: %s",
    116 		    strerror(errno));
    117 		return (-1);
    118 	}
    119 
    120 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    121 		if (ifa->ifa_addr->sa_family != AF_INET)
    122 			continue;
    123 
    124 		/* Must be up */
    125 		if ((ifa->ifa_flags & IFF_UP) == 0)
    126 			continue;
    127 
    128 		/*
    129 		 * Must not be a loopback address (127/8)
    130 		 */
    131 		sin = (struct sockaddr_in *)ifa->ifa_addr;
    132 		if (ISLOOPBACK(ifa))
    133 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
    134 				continue;
    135 
    136 		al->addr = sin->sin_addr.s_addr;
    137 		al->device = strdup(ifa->ifa_name);
    138 		++al;
    139 		++nipaddr;
    140 	}
    141 	*ipaddrp = ifaddrlist;
    142 	freeifaddrs(ifap);
    143 	return (nipaddr);
    144 #else
    145 	int fd, nipaddr;
    146 #ifdef HAVE_SOCKADDR_SA_LEN
    147 	int n;
    148 #endif
    149 	struct ifreq *ifrp, *ifend, *ifnext, *mp;
    150 	struct sockaddr_in *sin;
    151 	struct ifaddrlist *al;
    152 	struct ifconf ifc;
    153 	struct ifreq ibuf[MAX_IPADDR], ifr;
    154 	char device[sizeof(ifr.ifr_name) + 1];
    155 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
    156 
    157 	fd = socket(AF_INET, SOCK_DGRAM, 0);
    158 	if (fd < 0) {
    159 		(void)snprintf(errbuf, buflen, "socket: %s", strerror(errno));
    160 		return (-1);
    161 	}
    162 	ifc.ifc_len = sizeof(ibuf);
    163 	ifc.ifc_buf = (caddr_t)ibuf;
    164 
    165 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
    166 	    ifc.ifc_len < sizeof(struct ifreq)) {
    167 		(void)snprintf(errbuf, buflen, "SIOCGIFCONF: %s", strerror(errno));
    168 		(void)close(fd);
    169 		return (-1);
    170 	}
    171 	ifrp = ibuf;
    172 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
    173 
    174 	al = ifaddrlist;
    175 	mp = NULL;
    176 	nipaddr = 0;
    177 	for (; ifrp < ifend; ifrp = ifnext) {
    178 #ifdef HAVE_SOCKADDR_SA_LEN
    179 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    180 		if (n < sizeof(*ifrp))
    181 			ifnext = ifrp + 1;
    182 		else
    183 			ifnext = (struct ifreq *)((char *)ifrp + n);
    184 		if (ifrp->ifr_addr.sa_family != AF_INET)
    185 			continue;
    186 #else
    187 		ifnext = ifrp + 1;
    188 #endif
    189 		/*
    190 		 * Need a template to preserve address info that is
    191 		 * used below to locate the next entry.  (Otherwise,
    192 		 * SIOCGIFFLAGS stomps over it because the requests
    193 		 * are returned in a union.)
    194 		 */
    195 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
    196 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
    197 			if (errno == ENXIO)
    198 				continue;
    199 			(void)snprintf(errbuf, buflen, "SIOCGIFFLAGS: %.*s: %s",
    200 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
    201 			    strerror(errno));
    202 			(void)close(fd);
    203 			return (-1);
    204 		}
    205 
    206 		/* Must be up */
    207 		if ((ifr.ifr_flags & IFF_UP) == 0)
    208 			continue;
    209 
    210 		/*
    211 		 * Must not be a loopback address (127/8)
    212 		 */
    213 		sin = (struct sockaddr_in *)&ifrp->ifr_addr;
    214 		if (ISLOOPBACK(&ifr))
    215 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
    216 				continue;
    217 
    218 		(void)strncpy(device, ifrp->ifr_name, sizeof(ifrp->ifr_name));
    219 		device[sizeof(device) - 1] = '\0';
    220 
    221 		al->addr = sin->sin_addr.s_addr;
    222 		al->device = strdup(device);
    223 		++al;
    224 		++nipaddr;
    225 	}
    226 	(void)close(fd);
    227 
    228 	*ipaddrp = ifaddrlist;
    229 	return (nipaddr);
    230 #endif
    231 }
    232