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