ifaddrlist.c revision 1.4 1 /* $NetBSD: ifaddrlist.c,v 1.4 2000/04/13 07:53:29 itojun 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.4 2000/04/13 07:53:29 itojun 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 #ifdef HAVE_IFADDRS_H
72 #include <ifaddrs.h>
73 #endif
74
75 #include "gnuc.h"
76 #ifdef HAVE_OS_PROTO_H
77 #include "os-proto.h"
78 #endif
79
80 #include "ifaddrlist.h"
81 #include "savestr.h"
82
83
84 /* Not all systems have IFF_LOOPBACK */
85 #ifdef HAVE_IFADDRS_H
86 #ifdef IFF_LOOPBACK
87 #define ISLOOPBACK(p) ((p)->ifa_flags & IFF_LOOPBACK)
88 #else
89 #define ISLOOPBACK(p) (strcmp((p)->ifa_name, "lo0") == 0)
90 #endif
91 #else
92 #ifdef IFF_LOOPBACK
93 #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
94 #else
95 #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
96 #endif
97 #endif
98
99 #define MAX_IPADDR 256
100
101 /*
102 * Return the interface list
103 */
104 int
105 ifaddrlist(struct ifaddrlist **ipaddrp, char *errbuf, int buflen)
106 {
107 #ifdef HAVE_IFADDRS_H
108 int nipaddr;
109 struct sockaddr_in *sin;
110 struct ifaddrs *ifap, *ifa;
111 struct ifaddrlist *al;
112 static struct ifaddrlist ifaddrlist[MAX_IPADDR];
113
114 al = ifaddrlist;
115 nipaddr = 0;
116
117 if (getifaddrs(&ifap) != 0) {
118 (void)snprintf(errbuf, buflen, "getifaddrs: %s",
119 strerror(errno));
120 return (-1);
121 }
122
123 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
124 if (ifa->ifa_addr->sa_family != AF_INET)
125 continue;
126
127 /* Must be up */
128 if ((ifa->ifa_flags & IFF_UP) == 0)
129 continue;
130
131 /*
132 * Must not be a loopback address (127/8)
133 */
134 sin = (struct sockaddr_in *)ifa->ifa_addr;
135 if (ISLOOPBACK(ifa))
136 if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
137 continue;
138
139 al->addr = sin->sin_addr.s_addr;
140 al->device = savestr(ifa->ifa_name);
141 ++al;
142 ++nipaddr;
143 }
144 *ipaddrp = ifaddrlist;
145 freeifaddrs(ifap);
146 return (nipaddr);
147 #else
148 int fd, nipaddr;
149 #ifdef HAVE_SOCKADDR_SA_LEN
150 int n;
151 #endif
152 struct ifreq *ifrp, *ifend, *ifnext, *mp;
153 struct sockaddr_in *sin;
154 struct ifaddrlist *al;
155 struct ifconf ifc;
156 struct ifreq ibuf[MAX_IPADDR], ifr;
157 char device[sizeof(ifr.ifr_name) + 1];
158 static struct ifaddrlist ifaddrlist[MAX_IPADDR];
159
160 fd = socket(AF_INET, SOCK_DGRAM, 0);
161 if (fd < 0) {
162 (void)snprintf(errbuf, buflen, "socket: %s", strerror(errno));
163 return (-1);
164 }
165 ifc.ifc_len = sizeof(ibuf);
166 ifc.ifc_buf = (caddr_t)ibuf;
167
168 if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
169 ifc.ifc_len < sizeof(struct ifreq)) {
170 (void)snprintf(errbuf, buflen, "SIOCGIFCONF: %s", strerror(errno));
171 (void)close(fd);
172 return (-1);
173 }
174 ifrp = ibuf;
175 ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
176
177 al = ifaddrlist;
178 mp = NULL;
179 nipaddr = 0;
180 for (; ifrp < ifend; ifrp = ifnext) {
181 #ifdef HAVE_SOCKADDR_SA_LEN
182 n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
183 if (n < sizeof(*ifrp))
184 ifnext = ifrp + 1;
185 else
186 ifnext = (struct ifreq *)((char *)ifrp + n);
187 if (ifrp->ifr_addr.sa_family != AF_INET)
188 continue;
189 #else
190 ifnext = ifrp + 1;
191 #endif
192 /*
193 * Need a template to preserve address info that is
194 * used below to locate the next entry. (Otherwise,
195 * SIOCGIFFLAGS stomps over it because the requests
196 * are returned in a union.)
197 */
198 strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
199 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
200 if (errno == ENXIO)
201 continue;
202 (void)snprintf(errbuf, buflen, "SIOCGIFFLAGS: %.*s: %s",
203 (int)sizeof(ifr.ifr_name), ifr.ifr_name,
204 strerror(errno));
205 (void)close(fd);
206 return (-1);
207 }
208
209 /* Must be up */
210 if ((ifr.ifr_flags & IFF_UP) == 0)
211 continue;
212
213 /*
214 * Must not be a loopback address (127/8)
215 */
216 sin = (struct sockaddr_in *)&ifrp->ifr_addr;
217 if (ISLOOPBACK(&ifr))
218 if (ntohl(sin->sin_addr.s_addr) == INADDR_LOOPBACK)
219 continue;
220
221 (void)strncpy(device, ifrp->ifr_name, sizeof(ifrp->ifr_name));
222 device[sizeof(device) - 1] = '\0';
223
224 al->addr = sin->sin_addr.s_addr;
225 al->device = savestr(device);
226 ++al;
227 ++nipaddr;
228 }
229 (void)close(fd);
230
231 *ipaddrp = ifaddrlist;
232 return (nipaddr);
233 #endif
234 }
235