Home | History | Annotate | Line # | Download | only in traceroute
ifaddrlist.c revision 1.11
      1  1.11     kamil /*	$NetBSD: ifaddrlist.c,v 1.11 2019/08/18 04:12:07 kamil Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4  1.10  christos  * Copyright (c) 1997, 1998, 1999, 2000
      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.10  christos     "@(#) Id: ifaddrlist.c,v 1.9 2000/11/23 20:01:55 leres Exp  (LBL)";
     42   1.1  christos #else
     43  1.11     kamil __RCSID("$NetBSD: ifaddrlist.c,v 1.11 2019/08/18 04:12:07 kamil Exp $");
     44   1.1  christos #endif
     45   1.1  christos #endif
     46   1.1  christos 
     47   1.1  christos #include <sys/param.h>
     48   1.1  christos #include <sys/file.h>
     49   1.1  christos #include <sys/ioctl.h>
     50   1.1  christos #include <sys/socket.h>
     51   1.1  christos #ifdef HAVE_SYS_SOCKIO_H
     52   1.1  christos #include <sys/sockio.h>
     53   1.1  christos #endif
     54   1.1  christos #include <sys/time.h>				/* concession to AIX */
     55   1.1  christos 
     56   1.1  christos struct mbuf;
     57   1.1  christos struct rtentry;
     58   1.1  christos 
     59   1.1  christos #include <net/if.h>
     60   1.1  christos #include <netinet/in.h>
     61   1.3  explorer #include <arpa/inet.h>
     62   1.1  christos 
     63   1.1  christos #include <ctype.h>
     64   1.1  christos #include <errno.h>
     65   1.1  christos #include <memory.h>
     66   1.1  christos #include <stdio.h>
     67   1.1  christos #include <stdlib.h>
     68   1.1  christos #include <string.h>
     69   1.1  christos #include <unistd.h>
     70   1.4    itojun #include <ifaddrs.h>
     71   1.1  christos 
     72   1.1  christos #include "gnuc.h"
     73   1.1  christos #ifdef HAVE_OS_PROTO_H
     74   1.1  christos #include "os-proto.h"
     75   1.1  christos #endif
     76   1.1  christos 
     77   1.1  christos #include "ifaddrlist.h"
     78  1.11     kamil #include "prog_ops.h"
     79   1.1  christos 
     80   1.1  christos /* Not all systems have IFF_LOOPBACK */
     81   1.4    itojun #ifdef IFF_LOOPBACK
     82   1.4    itojun #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
     83   1.4    itojun #else
     84   1.4    itojun #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
     85   1.4    itojun #endif
     86   1.1  christos 
     87   1.1  christos /*
     88   1.1  christos  * Return the interface list
     89   1.1  christos  */
     90   1.9  christos ssize_t
     91   1.9  christos ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, size_t buflen)
     92   1.1  christos {
     93   1.4    itojun 	struct sockaddr_in *sin;
     94   1.9  christos 	struct ifaddrs *ifap = NULL, *ifa;
     95   1.9  christos 	struct ifaddrlist *al = NULL, *nal;
     96   1.9  christos 	size_t i = 0, maxal = 10;
     97   1.9  christos 
     98  1.11     kamil 	if (prog_getifaddrs(&ifap) != 0)
     99   1.9  christos 		goto out;
    100   1.9  christos 
    101   1.9  christos 	if ((al = malloc(maxal * sizeof(*al))) == NULL)
    102   1.9  christos 		goto out;
    103   1.9  christos 
    104   1.4    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    105   1.4    itojun 		if (ifa->ifa_addr->sa_family != AF_INET)
    106   1.4    itojun 			continue;
    107   1.4    itojun 
    108   1.4    itojun 		/* Must be up */
    109   1.4    itojun 		if ((ifa->ifa_flags & IFF_UP) == 0)
    110   1.4    itojun 			continue;
    111   1.4    itojun 
    112   1.4    itojun 		/*
    113   1.4    itojun 		 * Must not be a loopback address (127/8)
    114   1.4    itojun 		 */
    115   1.4    itojun 		sin = (struct sockaddr_in *)ifa->ifa_addr;
    116   1.4    itojun 		if (ISLOOPBACK(ifa))
    117   1.4    itojun 			if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
    118   1.4    itojun 				continue;
    119   1.4    itojun 
    120   1.9  christos 		if (i == maxal) {
    121   1.9  christos 			maxal <<= 1;
    122   1.9  christos 			if ((nal = realloc(al, maxal * sizeof(*al))) == NULL)
    123   1.9  christos 				goto out;
    124   1.9  christos 			al = nal;
    125   1.9  christos 		}
    126   1.9  christos 
    127   1.9  christos 		al[i].addr = sin->sin_addr.s_addr;
    128   1.9  christos 		if ((al[i].device = strdup(ifa->ifa_name)) == NULL)
    129   1.9  christos 			goto out;
    130   1.9  christos 		i++;
    131   1.4    itojun 	}
    132   1.9  christos 	if ((nal = realloc(al, i * sizeof(*al))) == NULL)
    133   1.9  christos 		goto out;
    134   1.4    itojun 	freeifaddrs(ifap);
    135   1.9  christos 	*ipaddrp = nal;
    136   1.9  christos 	return (ssize_t)i;
    137   1.9  christos out:
    138   1.9  christos 	if (ifap)
    139   1.9  christos 		freeifaddrs(ifap);
    140   1.9  christos 	if (al) {
    141   1.9  christos 		while (i > 0)
    142   1.9  christos 			free(al[--i].device);
    143   1.9  christos 		free(al);
    144   1.9  christos 	}
    145   1.9  christos 	(void)snprintf(errbuf, buflen, "%s: %s", __func__, strerror(errno));
    146   1.9  christos 	return -1;
    147   1.1  christos }
    148