1 /* $FreeBSD: src/lib/libc/net/getifaddrs.c,v 1.1.2.4 2002/08/01 19:31:06 ume Exp $ */
2 /* $DragonFly: src/lib/libcr/net/Attic/getifaddrs.c,v 1.4 2004/02/03 07:34:10 dillon Exp $ */
3 /* $KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $ */
6 * Copyright (c) 1995, 1999
7 * Berkeley Software Design, Inc. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
30 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
31 * try-and-error for region size.
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
39 #include <net/route.h>
40 #include <sys/sysctl.h>
41 #include <net/if_dl.h>
50 #define SA_LEN(sa) sizeof(struct sockaddr)
54 #define SA_LEN(sa) (sa)->sa_len
57 #define SALIGN (sizeof(long) - 1)
58 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
62 * On systems with a routing socket, ALIGNBYTES should match the value
63 * that the kernel uses when building the messages.
65 #define ALIGNBYTES XXX
68 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
71 #if _BSDI_VERSION >= 199701
75 #if _BSDI_VERSION >= 199802
76 /* ifam_data is very specific to recent versions of bsdi */
77 #define HAVE_IFAM_DATA
80 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
81 defined(__FreeBSD__) || defined(__DragonFly__)
85 #define MAX_SYSCTL_TRY 5
88 getifaddrs(struct ifaddrs **pif)
99 struct ifaddrs *cif = 0;
101 struct rt_msghdr *rtm;
102 struct if_msghdr *ifm;
103 struct ifa_msghdr *ifam;
104 struct sockaddr_dl *dl;
106 struct ifaddrs *ifa, *ift;
108 #else /* NET_RT_IFLIST */
114 #endif /* NET_RT_IFLIST */
123 mib[2] = 0; /* protocol */
124 mib[3] = 0; /* wildcard address family */
125 mib[4] = NET_RT_IFLIST;
126 mib[5] = 0; /* no flags */
129 * We'll try to get addresses several times in case that
130 * the number of addresses is unexpectedly increased during
131 * the two sysctl calls. This should rarely happen, but we'll
132 * try to do our best for applications that assume success of
133 * this library (which should usually be the case).
134 * Portability note: since FreeBSD does not add margin of
135 * memory at the first sysctl, the possibility of failure on
136 * the second sysctl call is a bit higher.
139 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
141 if ((buf = malloc(needed)) == NULL)
143 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
144 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
151 } while (buf == NULL);
153 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
154 rtm = (struct rt_msghdr *)(void *)next;
155 if (rtm->rtm_version != RTM_VERSION)
157 switch (rtm->rtm_type) {
159 ifm = (struct if_msghdr *)(void *)rtm;
160 if (ifm->ifm_addrs & RTA_IFP) {
161 idx = ifm->ifm_index;
163 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
164 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
167 dcnt += sizeof(ifm->ifm_data);
168 #endif /* HAVE_IFM_DATA */
169 ncnt += dl->sdl_nlen + 1;
175 ifam = (struct ifa_msghdr *)(void *)rtm;
176 if (idx && ifam->ifam_index != idx)
177 abort(); /* this cannot happen */
179 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
180 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
182 p = (char *)(void *)(ifam + 1);
184 #ifdef HAVE_IFAM_DATA
185 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
186 #endif /* HAVE_IFAM_DATA */
187 /* Scan to look for length of address */
189 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
190 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
193 sa = (struct sockaddr *)(void *)p;
201 for (p = p0, i = 0; i < RTAX_MAX; i++) {
202 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
205 sa = (struct sockaddr *)(void *)p;
207 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
216 #else /* NET_RT_IFLIST */
218 ifc.ifc_len = sizeof(buf);
220 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
222 i = ioctl(sock, SIOCGIFCONF, (char *)&ifc);
228 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
236 ncnt += sizeof(ifr->ifr_name) + 1;
238 if (SA_LEN(sa) < sizeof(*sa))
239 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
241 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
243 #endif /* NET_RT_IFLIST */
245 if (icnt + dcnt + ncnt == 1) {
250 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
256 ifa = (struct ifaddrs *)(void *)data;
257 data += sizeof(struct ifaddrs) * icnt;
260 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
265 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
266 rtm = (struct rt_msghdr *)(void *)next;
267 if (rtm->rtm_version != RTM_VERSION)
269 switch (rtm->rtm_type) {
271 ifm = (struct if_msghdr *)(void *)rtm;
272 if (ifm->ifm_addrs & RTA_IFP) {
273 idx = ifm->ifm_index;
274 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
277 ift->ifa_name = names;
278 ift->ifa_flags = (int)ifm->ifm_flags;
279 memcpy(names, dl->sdl_data,
280 (size_t)dl->sdl_nlen);
281 names[dl->sdl_nlen] = 0;
282 names += dl->sdl_nlen + 1;
284 ift->ifa_addr = (struct sockaddr *)(void *)data;
286 (size_t)SA_LEN((struct sockaddr *)
288 data += SA_RLEN((struct sockaddr *)(void *)dl);
291 /* ifm_data needs to be aligned */
292 ift->ifa_data = data = (void *)ALIGN(data);
293 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
294 data += sizeof(ifm->ifm_data);
295 #else /* HAVE_IFM_DATA */
296 ift->ifa_data = NULL;
297 #endif /* HAVE_IFM_DATA */
299 ift = (ift->ifa_next = ift + 1);
305 ifam = (struct ifa_msghdr *)(void *)rtm;
306 if (idx && ifam->ifam_index != idx)
307 abort(); /* this cannot happen */
309 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
311 ift->ifa_name = cif->ifa_name;
312 ift->ifa_flags = cif->ifa_flags;
313 ift->ifa_data = NULL;
314 p = (char *)(void *)(ifam + 1);
315 /* Scan to look for length of address */
317 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
318 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
321 sa = (struct sockaddr *)(void *)p;
329 for (p = p0, i = 0; i < RTAX_MAX; i++) {
330 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
333 sa = (struct sockaddr *)(void *)p;
338 (struct sockaddr *)(void *)data;
339 memcpy(data, p, len);
345 (struct sockaddr *)(void *)data;
346 if (SA_LEN(sa) == 0) {
347 memset(data, 0, alen);
351 memcpy(data, p, len);
357 (struct sockaddr *)(void *)data;
358 memcpy(data, p, len);
365 #ifdef HAVE_IFAM_DATA
366 /* ifam_data needs to be aligned */
367 ift->ifa_data = data = (void *)ALIGN(data);
368 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
369 data += sizeof(ifam->ifam_data);
370 #endif /* HAVE_IFAM_DATA */
372 ift = (ift->ifa_next = ift + 1);
378 #else /* NET_RT_IFLIST */
380 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
385 ift->ifa_name = names;
386 names[sizeof(ifr->ifr_name)] = 0;
387 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
391 ift->ifa_addr = (struct sockaddr *)data;
393 memcpy(data, sa, SA_LEN(sa));
396 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
397 ift = (ift->ifa_next = ift + 1);
399 #endif /* NET_RT_IFLIST */
401 ift->ifa_next = NULL;
411 freeifaddrs(struct ifaddrs *ifp)