1 /* $FreeBSD: src/lib/libc/net/getifaddrs.c,v 1.1.2.4 2002/08/01 19:31:06 ume Exp $ */
2 /* $KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $ */
5 * Copyright (c) 1995, 1999
6 * Berkeley Software Design, Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
29 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
30 * try-and-error for region size.
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <sys/param.h>
38 #include <net/route.h>
39 #include <sys/sysctl.h>
40 #include <net/if_dl.h>
49 #define SA_LEN(sa) sizeof(struct sockaddr)
53 #define SA_LEN(sa) (sa)->sa_len
56 #define SALIGN (sizeof(long) - 1)
57 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
61 * On systems with a routing socket, ALIGNBYTES should match the value
62 * that the kernel uses when building the messages.
64 #define ALIGNBYTES XXX
67 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
70 #if _BSDI_VERSION >= 199701
74 #if _BSDI_VERSION >= 199802
75 /* ifam_data is very specific to recent versions of bsdi */
76 #define HAVE_IFAM_DATA
79 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
83 #define MAX_SYSCTL_TRY 5
86 getifaddrs(struct ifaddrs **pif)
97 struct ifaddrs *cif = 0;
99 struct rt_msghdr *rtm;
100 struct if_msghdr *ifm;
101 struct ifa_msghdr *ifam;
102 struct sockaddr_dl *dl;
104 struct ifaddrs *ifa, *ift;
106 #else /* NET_RT_IFLIST */
112 #endif /* NET_RT_IFLIST */
121 mib[2] = 0; /* protocol */
122 mib[3] = 0; /* wildcard address family */
123 mib[4] = NET_RT_IFLIST;
124 mib[5] = 0; /* no flags */
127 * We'll try to get addresses several times in case that
128 * the number of addresses is unexpectedly increased during
129 * the two sysctl calls. This should rarely happen, but we'll
130 * try to do our best for applications that assume success of
131 * this library (which should usually be the case).
132 * Portability note: since FreeBSD does not add margin of
133 * memory at the first sysctl, the possibility of failure on
134 * the second sysctl call is a bit higher.
137 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
139 if ((buf = malloc(needed)) == NULL)
141 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
142 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
149 } while (buf == NULL);
151 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
152 rtm = (struct rt_msghdr *)(void *)next;
153 if (rtm->rtm_version != RTM_VERSION)
155 switch (rtm->rtm_type) {
157 ifm = (struct if_msghdr *)(void *)rtm;
158 if (ifm->ifm_addrs & RTA_IFP) {
159 idx = ifm->ifm_index;
161 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
162 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
165 dcnt += sizeof(ifm->ifm_data);
166 #endif /* HAVE_IFM_DATA */
167 ncnt += dl->sdl_nlen + 1;
173 ifam = (struct ifa_msghdr *)(void *)rtm;
174 if (idx && ifam->ifam_index != idx)
175 abort(); /* this cannot happen */
177 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
178 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
180 p = (char *)(void *)(ifam + 1);
182 #ifdef HAVE_IFAM_DATA
183 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
184 #endif /* HAVE_IFAM_DATA */
185 /* Scan to look for length of address */
187 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
188 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
191 sa = (struct sockaddr *)(void *)p;
199 for (p = p0, i = 0; i < RTAX_MAX; i++) {
200 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
203 sa = (struct sockaddr *)(void *)p;
205 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
214 #else /* NET_RT_IFLIST */
216 ifc.ifc_len = sizeof(buf);
218 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
220 i = ioctl(sock, SIOCGIFCONF, (char *)&ifc);
226 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
234 ncnt += sizeof(ifr->ifr_name) + 1;
236 if (SA_LEN(sa) < sizeof(*sa))
237 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
239 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
241 #endif /* NET_RT_IFLIST */
243 if (icnt + dcnt + ncnt == 1) {
248 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
254 ifa = (struct ifaddrs *)(void *)data;
255 data += sizeof(struct ifaddrs) * icnt;
258 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
263 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
264 rtm = (struct rt_msghdr *)(void *)next;
265 if (rtm->rtm_version != RTM_VERSION)
267 switch (rtm->rtm_type) {
269 ifm = (struct if_msghdr *)(void *)rtm;
270 if (ifm->ifm_addrs & RTA_IFP) {
271 idx = ifm->ifm_index;
272 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
275 ift->ifa_name = names;
276 ift->ifa_flags = (int)ifm->ifm_flags;
277 memcpy(names, dl->sdl_data,
278 (size_t)dl->sdl_nlen);
279 names[dl->sdl_nlen] = 0;
280 names += dl->sdl_nlen + 1;
282 ift->ifa_addr = (struct sockaddr *)(void *)data;
284 (size_t)SA_LEN((struct sockaddr *)
286 data += SA_RLEN((struct sockaddr *)(void *)dl);
289 /* ifm_data needs to be aligned */
290 ift->ifa_data = data = (void *)ALIGN(data);
291 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
292 data += sizeof(ifm->ifm_data);
293 #else /* HAVE_IFM_DATA */
294 ift->ifa_data = NULL;
295 #endif /* HAVE_IFM_DATA */
297 ift = (ift->ifa_next = ift + 1);
303 ifam = (struct ifa_msghdr *)(void *)rtm;
304 if (idx && ifam->ifam_index != idx)
305 abort(); /* this cannot happen */
307 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
309 ift->ifa_name = cif->ifa_name;
310 ift->ifa_flags = cif->ifa_flags;
311 ift->ifa_data = NULL;
312 p = (char *)(void *)(ifam + 1);
313 /* Scan to look for length of address */
315 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
316 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
319 sa = (struct sockaddr *)(void *)p;
327 for (p = p0, i = 0; i < RTAX_MAX; i++) {
328 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
331 sa = (struct sockaddr *)(void *)p;
336 (struct sockaddr *)(void *)data;
337 memcpy(data, p, len);
343 (struct sockaddr *)(void *)data;
344 if (SA_LEN(sa) == 0) {
345 memset(data, 0, alen);
349 memcpy(data, p, len);
355 (struct sockaddr *)(void *)data;
356 memcpy(data, p, len);
363 #ifdef HAVE_IFAM_DATA
364 /* ifam_data needs to be aligned */
365 ift->ifa_data = data = (void *)ALIGN(data);
366 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
367 data += sizeof(ifam->ifam_data);
368 #endif /* HAVE_IFAM_DATA */
370 ift = (ift->ifa_next = ift + 1);
376 #else /* NET_RT_IFLIST */
378 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
383 ift->ifa_name = names;
384 names[sizeof(ifr->ifr_name)] = 0;
385 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
389 ift->ifa_addr = (struct sockaddr *)data;
391 memcpy(data, sa, SA_LEN(sa));
394 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
395 ift = (ift->ifa_next = ift + 1);
397 #endif /* NET_RT_IFLIST */
399 ift->ifa_next = NULL;
409 freeifaddrs(struct ifaddrs *ifp)