1*7f5f010bSBen Gras /* $NetBSD: ifaddrlist.c,v 1.10 2011/09/11 01:06:26 christos Exp $ */
2*7f5f010bSBen Gras
3*7f5f010bSBen Gras /*
4*7f5f010bSBen Gras * Copyright (c) 1997, 1998, 1999, 2000
5*7f5f010bSBen Gras * The Regents of the University of California. All rights reserved.
6*7f5f010bSBen Gras *
7*7f5f010bSBen Gras * Redistribution and use in source and binary forms, with or without
8*7f5f010bSBen Gras * modification, are permitted provided that the following conditions
9*7f5f010bSBen Gras * are met:
10*7f5f010bSBen Gras * 1. Redistributions of source code must retain the above copyright
11*7f5f010bSBen Gras * notice, this list of conditions and the following disclaimer.
12*7f5f010bSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13*7f5f010bSBen Gras * notice, this list of conditions and the following disclaimer in the
14*7f5f010bSBen Gras * documentation and/or other materials provided with the distribution.
15*7f5f010bSBen Gras * 3. All advertising materials mentioning features or use of this software
16*7f5f010bSBen Gras * must display the following acknowledgement:
17*7f5f010bSBen Gras * This product includes software developed by the Computer Systems
18*7f5f010bSBen Gras * Engineering Group at Lawrence Berkeley Laboratory.
19*7f5f010bSBen Gras * 4. Neither the name of the University nor of the Laboratory may be used
20*7f5f010bSBen Gras * to endorse or promote products derived from this software without
21*7f5f010bSBen Gras * specific prior written permission.
22*7f5f010bSBen Gras *
23*7f5f010bSBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*7f5f010bSBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*7f5f010bSBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*7f5f010bSBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*7f5f010bSBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*7f5f010bSBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*7f5f010bSBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*7f5f010bSBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*7f5f010bSBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*7f5f010bSBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*7f5f010bSBen Gras * SUCH DAMAGE.
34*7f5f010bSBen Gras */
35*7f5f010bSBen Gras
36*7f5f010bSBen Gras #include <sys/cdefs.h>
37*7f5f010bSBen Gras #ifndef lint
38*7f5f010bSBen Gras #if 0
39*7f5f010bSBen Gras static const char rcsid[] =
40*7f5f010bSBen Gras "@(#) Header: ifaddrlist.c,v 1.2 97/04/22 13:31:05 leres Exp (LBL)";
41*7f5f010bSBen Gras "@(#) Id: ifaddrlist.c,v 1.9 2000/11/23 20:01:55 leres Exp (LBL)";
42*7f5f010bSBen Gras #else
43*7f5f010bSBen Gras __RCSID("$NetBSD: ifaddrlist.c,v 1.10 2011/09/11 01:06:26 christos Exp $");
44*7f5f010bSBen Gras #endif
45*7f5f010bSBen Gras #endif
46*7f5f010bSBen Gras
47*7f5f010bSBen Gras #include <sys/param.h>
48*7f5f010bSBen Gras #include <sys/file.h>
49*7f5f010bSBen Gras #include <sys/ioctl.h>
50*7f5f010bSBen Gras #include <sys/socket.h>
51*7f5f010bSBen Gras #ifdef HAVE_SYS_SOCKIO_H
52*7f5f010bSBen Gras #include <sys/sockio.h>
53*7f5f010bSBen Gras #endif
54*7f5f010bSBen Gras #include <sys/time.h> /* concession to AIX */
55*7f5f010bSBen Gras
56*7f5f010bSBen Gras struct mbuf;
57*7f5f010bSBen Gras struct rtentry;
58*7f5f010bSBen Gras
59*7f5f010bSBen Gras #include <net/if.h>
60*7f5f010bSBen Gras #include <netinet/in.h>
61*7f5f010bSBen Gras #include <arpa/inet.h>
62*7f5f010bSBen Gras
63*7f5f010bSBen Gras #include <ctype.h>
64*7f5f010bSBen Gras #include <errno.h>
65*7f5f010bSBen Gras #include <memory.h>
66*7f5f010bSBen Gras #include <stdio.h>
67*7f5f010bSBen Gras #include <stdlib.h>
68*7f5f010bSBen Gras #include <string.h>
69*7f5f010bSBen Gras #include <unistd.h>
70*7f5f010bSBen Gras #include <ifaddrs.h>
71*7f5f010bSBen Gras
72*7f5f010bSBen Gras #include "gnuc.h"
73*7f5f010bSBen Gras #ifdef HAVE_OS_PROTO_H
74*7f5f010bSBen Gras #include "os-proto.h"
75*7f5f010bSBen Gras #endif
76*7f5f010bSBen Gras
77*7f5f010bSBen Gras #include "ifaddrlist.h"
78*7f5f010bSBen Gras
79*7f5f010bSBen Gras /* Not all systems have IFF_LOOPBACK */
80*7f5f010bSBen Gras #ifdef IFF_LOOPBACK
81*7f5f010bSBen Gras #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
82*7f5f010bSBen Gras #else
83*7f5f010bSBen Gras #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
84*7f5f010bSBen Gras #endif
85*7f5f010bSBen Gras
86*7f5f010bSBen Gras /*
87*7f5f010bSBen Gras * Return the interface list
88*7f5f010bSBen Gras */
89*7f5f010bSBen Gras ssize_t
ifaddrlist(struct ifaddrlist ** ipaddrp,char * errbuf,size_t buflen)90*7f5f010bSBen Gras ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, size_t buflen)
91*7f5f010bSBen Gras {
92*7f5f010bSBen Gras struct sockaddr_in *sin;
93*7f5f010bSBen Gras struct ifaddrs *ifap = NULL, *ifa;
94*7f5f010bSBen Gras struct ifaddrlist *al = NULL, *nal;
95*7f5f010bSBen Gras size_t i = 0, maxal = 10;
96*7f5f010bSBen Gras
97*7f5f010bSBen Gras if (getifaddrs(&ifap) != 0)
98*7f5f010bSBen Gras goto out;
99*7f5f010bSBen Gras
100*7f5f010bSBen Gras if ((al = malloc(maxal * sizeof(*al))) == NULL)
101*7f5f010bSBen Gras goto out;
102*7f5f010bSBen Gras
103*7f5f010bSBen Gras for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
104*7f5f010bSBen Gras if (ifa->ifa_addr->sa_family != AF_INET)
105*7f5f010bSBen Gras continue;
106*7f5f010bSBen Gras
107*7f5f010bSBen Gras /* Must be up */
108*7f5f010bSBen Gras if ((ifa->ifa_flags & IFF_UP) == 0)
109*7f5f010bSBen Gras continue;
110*7f5f010bSBen Gras
111*7f5f010bSBen Gras /*
112*7f5f010bSBen Gras * Must not be a loopback address (127/8)
113*7f5f010bSBen Gras */
114*7f5f010bSBen Gras sin = (struct sockaddr_in *)ifa->ifa_addr;
115*7f5f010bSBen Gras if (ISLOOPBACK(ifa))
116*7f5f010bSBen Gras if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
117*7f5f010bSBen Gras continue;
118*7f5f010bSBen Gras
119*7f5f010bSBen Gras if (i == maxal) {
120*7f5f010bSBen Gras maxal <<= 1;
121*7f5f010bSBen Gras if ((nal = realloc(al, maxal * sizeof(*al))) == NULL)
122*7f5f010bSBen Gras goto out;
123*7f5f010bSBen Gras al = nal;
124*7f5f010bSBen Gras }
125*7f5f010bSBen Gras
126*7f5f010bSBen Gras al[i].addr = sin->sin_addr.s_addr;
127*7f5f010bSBen Gras if ((al[i].device = strdup(ifa->ifa_name)) == NULL)
128*7f5f010bSBen Gras goto out;
129*7f5f010bSBen Gras i++;
130*7f5f010bSBen Gras }
131*7f5f010bSBen Gras if ((nal = realloc(al, i * sizeof(*al))) == NULL)
132*7f5f010bSBen Gras goto out;
133*7f5f010bSBen Gras freeifaddrs(ifap);
134*7f5f010bSBen Gras *ipaddrp = nal;
135*7f5f010bSBen Gras return (ssize_t)i;
136*7f5f010bSBen Gras out:
137*7f5f010bSBen Gras if (ifap)
138*7f5f010bSBen Gras freeifaddrs(ifap);
139*7f5f010bSBen Gras if (al) {
140*7f5f010bSBen Gras while (i > 0)
141*7f5f010bSBen Gras free(al[--i].device);
142*7f5f010bSBen Gras free(al);
143*7f5f010bSBen Gras }
144*7f5f010bSBen Gras (void)snprintf(errbuf, buflen, "%s: %s", __func__, strerror(errno));
145*7f5f010bSBen Gras return -1;
146*7f5f010bSBen Gras }
147