1*276d6b79Snia /* $NetBSD: ifaddrlist.c,v 1.12 2021/10/30 09:26:11 nia Exp $ */
2313a9cffSchristos
3313a9cffSchristos /*
49167e176Schristos * Copyright (c) 1997, 1998, 1999, 2000
5313a9cffSchristos * The Regents of the University of California. All rights reserved.
6313a9cffSchristos *
7313a9cffSchristos * Redistribution and use in source and binary forms, with or without
8313a9cffSchristos * modification, are permitted provided that the following conditions
9313a9cffSchristos * are met:
10313a9cffSchristos * 1. Redistributions of source code must retain the above copyright
11313a9cffSchristos * notice, this list of conditions and the following disclaimer.
12313a9cffSchristos * 2. Redistributions in binary form must reproduce the above copyright
13313a9cffSchristos * notice, this list of conditions and the following disclaimer in the
14313a9cffSchristos * documentation and/or other materials provided with the distribution.
15313a9cffSchristos * 3. All advertising materials mentioning features or use of this software
16313a9cffSchristos * must display the following acknowledgement:
17313a9cffSchristos * This product includes software developed by the Computer Systems
18313a9cffSchristos * Engineering Group at Lawrence Berkeley Laboratory.
19313a9cffSchristos * 4. Neither the name of the University nor of the Laboratory may be used
20313a9cffSchristos * to endorse or promote products derived from this software without
21313a9cffSchristos * specific prior written permission.
22313a9cffSchristos *
23313a9cffSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24313a9cffSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25313a9cffSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26313a9cffSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27313a9cffSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28313a9cffSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29313a9cffSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30313a9cffSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31313a9cffSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32313a9cffSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33313a9cffSchristos * SUCH DAMAGE.
34313a9cffSchristos */
35313a9cffSchristos
36313a9cffSchristos #include <sys/cdefs.h>
37313a9cffSchristos #ifndef lint
38313a9cffSchristos #if 0
39313a9cffSchristos static const char rcsid[] =
40313a9cffSchristos "@(#) Header: ifaddrlist.c,v 1.2 97/04/22 13:31:05 leres Exp (LBL)";
419167e176Schristos "@(#) Id: ifaddrlist.c,v 1.9 2000/11/23 20:01:55 leres Exp (LBL)";
42313a9cffSchristos #else
43*276d6b79Snia __RCSID("$NetBSD: ifaddrlist.c,v 1.12 2021/10/30 09:26:11 nia Exp $");
44313a9cffSchristos #endif
45313a9cffSchristos #endif
46313a9cffSchristos
47313a9cffSchristos #include <sys/param.h>
48313a9cffSchristos #include <sys/file.h>
49313a9cffSchristos #include <sys/ioctl.h>
50313a9cffSchristos #include <sys/socket.h>
51313a9cffSchristos #ifdef HAVE_SYS_SOCKIO_H
52313a9cffSchristos #include <sys/sockio.h>
53313a9cffSchristos #endif
54313a9cffSchristos #include <sys/time.h> /* concession to AIX */
55313a9cffSchristos
56313a9cffSchristos struct mbuf;
57313a9cffSchristos struct rtentry;
58313a9cffSchristos
59313a9cffSchristos #include <net/if.h>
60313a9cffSchristos #include <netinet/in.h>
613004dacaSexplorer #include <arpa/inet.h>
62313a9cffSchristos
63313a9cffSchristos #include <ctype.h>
64313a9cffSchristos #include <errno.h>
65313a9cffSchristos #include <memory.h>
66313a9cffSchristos #include <stdio.h>
67313a9cffSchristos #include <stdlib.h>
68313a9cffSchristos #include <string.h>
69313a9cffSchristos #include <unistd.h>
704e4447b1Sitojun #include <ifaddrs.h>
71313a9cffSchristos
72313a9cffSchristos #include "gnuc.h"
73313a9cffSchristos #ifdef HAVE_OS_PROTO_H
74313a9cffSchristos #include "os-proto.h"
75313a9cffSchristos #endif
76313a9cffSchristos
77313a9cffSchristos #include "ifaddrlist.h"
7850105555Skamil #include "prog_ops.h"
79313a9cffSchristos
80313a9cffSchristos /* Not all systems have IFF_LOOPBACK */
814e4447b1Sitojun #ifdef IFF_LOOPBACK
824e4447b1Sitojun #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
834e4447b1Sitojun #else
844e4447b1Sitojun #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
854e4447b1Sitojun #endif
86313a9cffSchristos
87313a9cffSchristos /*
88313a9cffSchristos * Return the interface list
89313a9cffSchristos */
90865746afSchristos ssize_t
ifaddrlist(struct ifaddrlist ** ipaddrp,char * errbuf,size_t buflen)91865746afSchristos ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, size_t buflen)
92313a9cffSchristos {
934e4447b1Sitojun struct sockaddr_in *sin;
94865746afSchristos struct ifaddrs *ifap = NULL, *ifa;
95*276d6b79Snia struct ifaddrlist *al = NULL;
96865746afSchristos size_t i = 0, maxal = 10;
974e4447b1Sitojun
9850105555Skamil if (prog_getifaddrs(&ifap) != 0)
99865746afSchristos goto out;
1004e4447b1Sitojun
101*276d6b79Snia if (reallocarr(&al, maxal, sizeof(*al)) != 0)
102865746afSchristos goto out;
1034e4447b1Sitojun
1044e4447b1Sitojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1054e4447b1Sitojun if (ifa->ifa_addr->sa_family != AF_INET)
1064e4447b1Sitojun continue;
1074e4447b1Sitojun
1084e4447b1Sitojun /* Must be up */
1094e4447b1Sitojun if ((ifa->ifa_flags & IFF_UP) == 0)
1104e4447b1Sitojun continue;
1114e4447b1Sitojun
1124e4447b1Sitojun /*
1134e4447b1Sitojun * Must not be a loopback address (127/8)
1144e4447b1Sitojun */
1154e4447b1Sitojun sin = (struct sockaddr_in *)ifa->ifa_addr;
1164e4447b1Sitojun if (ISLOOPBACK(ifa))
1174e4447b1Sitojun if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
1184e4447b1Sitojun continue;
1194e4447b1Sitojun
120865746afSchristos if (i == maxal) {
121865746afSchristos maxal <<= 1;
122*276d6b79Snia if (reallocarr(&al, maxal, sizeof(*al)) != 0)
123865746afSchristos goto out;
1244e4447b1Sitojun }
125865746afSchristos
126865746afSchristos al[i].addr = sin->sin_addr.s_addr;
127865746afSchristos if ((al[i].device = strdup(ifa->ifa_name)) == NULL)
128865746afSchristos goto out;
129865746afSchristos i++;
130865746afSchristos }
131*276d6b79Snia if (reallocarr(&al, i, sizeof(*al)) != 0)
132865746afSchristos goto out;
1334e4447b1Sitojun freeifaddrs(ifap);
134*276d6b79Snia *ipaddrp = al;
135865746afSchristos return (ssize_t)i;
136865746afSchristos out:
137865746afSchristos if (ifap)
138865746afSchristos freeifaddrs(ifap);
139865746afSchristos if (al) {
140865746afSchristos while (i > 0)
141865746afSchristos free(al[--i].device);
142865746afSchristos free(al);
143865746afSchristos }
144865746afSchristos (void)snprintf(errbuf, buflen, "%s: %s", __func__, strerror(errno));
145865746afSchristos return -1;
146313a9cffSchristos }
147