Home | History | Annotate | Line # | Download | only in traceroute
ifaddrlist.c revision 1.3
      1 /*	$NetBSD: ifaddrlist.c,v 1.3 1999/02/24 21:21:24 explorer 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.3 1999/02/24 21:21:24 explorer 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 #include <arpa/inet.h>
     63 
     64 #include <ctype.h>
     65 #include <errno.h>
     66 #include <memory.h>
     67 #include <stdio.h>
     68 #include <stdlib.h>
     69 #include <string.h>
     70 #include <unistd.h>
     71 
     72 #include "gnuc.h"
     73 #ifdef HAVE_OS_PROTO_H
     74 #include "os-proto.h"
     75 #endif
     76 
     77 #include "ifaddrlist.h"
     78 #include "savestr.h"
     79 
     80 
     81 /* Not all systems have IFF_LOOPBACK */
     82 #ifdef IFF_LOOPBACK
     83 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
     84 #else
     85 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
     86 #endif
     87 
     88 #define MAX_IPADDR 256
     89 
     90 /*
     91  * Return the interface list
     92  */
     93 int
     94 ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, int buflen)
     95 {
     96 	int fd, nipaddr;
     97 #ifdef HAVE_SOCKADDR_SA_LEN
     98 	int n;
     99 #endif
    100 	struct ifreq *ifrp, *ifend, *ifnext, *mp;
    101 	struct sockaddr_in *sin;
    102 	struct ifaddrlist *al;
    103 	struct ifconf ifc;
    104 	struct ifreq ibuf[MAX_IPADDR], ifr;
    105 	char device[sizeof(ifr.ifr_name) + 1];
    106 	static struct ifaddrlist ifaddrlist[MAX_IPADDR];
    107 
    108 	fd = socket(AF_INET, SOCK_DGRAM, 0);
    109 	if (fd < 0) {
    110 		(void)snprintf(errbuf, buflen, "socket: %s", strerror(errno));
    111 		return (-1);
    112 	}
    113 	ifc.ifc_len = sizeof(ibuf);
    114 	ifc.ifc_buf = (caddr_t)ibuf;
    115 
    116 	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
    117 	    ifc.ifc_len < sizeof(struct ifreq)) {
    118 		(void)snprintf(errbuf, buflen, "SIOCGIFCONF: %s", strerror(errno));
    119 		(void)close(fd);
    120 		return (-1);
    121 	}
    122 	ifrp = ibuf;
    123 	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
    124 
    125 	al = ifaddrlist;
    126 	mp = NULL;
    127 	nipaddr = 0;
    128 	for (; ifrp < ifend; ifrp = ifnext) {
    129 #ifdef HAVE_SOCKADDR_SA_LEN
    130 		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
    131 		if (n < sizeof(*ifrp))
    132 			ifnext = ifrp + 1;
    133 		else
    134 			ifnext = (struct ifreq *)((char *)ifrp + n);
    135 		if (ifrp->ifr_addr.sa_family != AF_INET)
    136 			continue;
    137 #else
    138 		ifnext = ifrp + 1;
    139 #endif
    140 		/*
    141 		 * Need a template to preserve address info that is
    142 		 * used below to locate the next entry.  (Otherwise,
    143 		 * SIOCGIFFLAGS stomps over it because the requests
    144 		 * are returned in a union.)
    145 		 */
    146 		strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
    147 		if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
    148 			if (errno == ENXIO)
    149 				continue;
    150 			(void)snprintf(errbuf, buflen, "SIOCGIFFLAGS: %.*s: %s",
    151 			    (int)sizeof(ifr.ifr_name), ifr.ifr_name,
    152 			    strerror(errno));
    153 			(void)close(fd);
    154 			return (-1);
    155 		}
    156 
    157 		/* Must be up */
    158 		if ((ifr.ifr_flags & IFF_UP) == 0)
    159 			continue;
    160 
    161 		/*
    162 		 * Must not be a loopback address (127/8)
    163 		 */
    164 		sin = (struct sockaddr_in *)&ifrp->ifr_addr;
    165 		if (ISLOOPBACK(&ifr))
    166 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
    167 				continue;
    168 
    169 		(void)strncpy(device, ifrp->ifr_name, sizeof(ifrp->ifr_name));
    170 		device[sizeof(device) - 1] = '\0';
    171 
    172 		al->addr = sin->sin_addr.s_addr;
    173 		al->device = savestr(device);
    174 		++al;
    175 		++nipaddr;
    176 	}
    177 	(void)close(fd);
    178 
    179 	*ipaddrp = ifaddrlist;
    180 	return (nipaddr);
    181 }
    182