1 /* $FreeBSD: src/lib/libc/net/getifaddrs.c,v 1.6 2002/07/25 08:08:30 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 "namespace.h"
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
38 #include <sys/param.h>
39 #include <net/route.h>
40 #include <sys/sysctl.h>
41 #include <net/if_dl.h>
48 #include "un-namespace.h"
51 #define SA_LEN(sa) sizeof(struct sockaddr)
55 #define SA_LEN(sa) (sa)->sa_len
58 #define SALIGN (sizeof(long) - 1)
59 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
63 * On systems with a routing socket, ALIGNBYTES should match the value
64 * that the kernel uses when building the messages.
66 #define ALIGNBYTES XXX
69 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
72 #if _BSDI_VERSION >= 199701
76 #if _BSDI_VERSION >= 199802
77 /* ifam_data is very specific to recent versions of bsdi */
78 #define HAVE_IFAM_DATA
81 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
82 defined(__FreeBSD__) || defined(__DragonFly__)
86 #define MAX_SYSCTL_TRY 5
89 getifaddrs(struct ifaddrs **pif)
100 struct ifaddrs *cif = NULL;
102 struct rt_msghdr *rtm;
103 struct if_msghdr *ifm;
104 struct ifa_msghdr *ifam;
105 struct sockaddr_dl *dl;
107 struct ifaddrs *ifa, *ift;
109 #else /* NET_RT_IFLIST */
115 #endif /* NET_RT_IFLIST */
124 mib[2] = 0; /* protocol */
125 mib[3] = 0; /* wildcard address family */
126 mib[4] = NET_RT_IFLIST;
127 mib[5] = 0; /* no flags */
130 * We'll try to get addresses several times in case that
131 * the number of addresses is unexpectedly increased during
132 * the two sysctl calls. This should rarely happen, but we'll
133 * try to do our best for applications that assume success of
134 * this library (which should usually be the case).
135 * Portability note: since FreeBSD does not add margin of
136 * memory at the first sysctl, the possibility of failure on
137 * the second sysctl call is a bit higher.
140 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
142 if ((buf = malloc(needed)) == NULL)
144 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
145 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
152 } while (buf == NULL);
154 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
155 rtm = (struct rt_msghdr *)(void *)next;
156 if (rtm->rtm_version != RTM_VERSION)
158 switch (rtm->rtm_type) {
160 ifm = (struct if_msghdr *)(void *)rtm;
161 if (ifm->ifm_addrs & RTA_IFP) {
162 idx = ifm->ifm_index;
164 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
165 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
168 dcnt += sizeof(ifm->ifm_data);
169 #endif /* HAVE_IFM_DATA */
170 ncnt += dl->sdl_nlen + 1;
176 ifam = (struct ifa_msghdr *)(void *)rtm;
177 if (idx && ifam->ifam_index != idx)
178 abort(); /* this cannot happen */
180 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
181 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
183 p = (char *)(void *)(ifam + 1);
185 #ifdef HAVE_IFAM_DATA
186 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
187 #endif /* HAVE_IFAM_DATA */
188 /* Scan to look for length of address */
190 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
191 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
194 sa = (struct sockaddr *)(void *)p;
202 for (p = p0, i = 0; i < RTAX_MAX; i++) {
203 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
206 sa = (struct sockaddr *)(void *)p;
208 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
217 #else /* NET_RT_IFLIST */
219 ifc.ifc_len = sizeof(buf);
221 if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
223 i = _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
229 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
237 ncnt += sizeof(ifr->ifr_name) + 1;
239 if (SA_LEN(sa) < sizeof(*sa))
240 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
242 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
244 #endif /* NET_RT_IFLIST */
246 if (icnt + dcnt + ncnt == 1) {
251 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
257 ifa = (struct ifaddrs *)(void *)data;
258 data += sizeof(struct ifaddrs) * icnt;
261 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
266 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
267 rtm = (struct rt_msghdr *)(void *)next;
268 if (rtm->rtm_version != RTM_VERSION)
270 switch (rtm->rtm_type) {
272 ifm = (struct if_msghdr *)(void *)rtm;
273 if (ifm->ifm_addrs & RTA_IFP) {
274 idx = ifm->ifm_index;
275 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
278 ift->ifa_name = names;
279 ift->ifa_flags = (int)ifm->ifm_flags;
280 memcpy(names, dl->sdl_data,
281 (size_t)dl->sdl_nlen);
282 names[dl->sdl_nlen] = 0;
283 names += dl->sdl_nlen + 1;
285 ift->ifa_addr = (struct sockaddr *)(void *)data;
287 (size_t)SA_LEN((struct sockaddr *)
289 data += SA_RLEN((struct sockaddr *)(void *)dl);
292 /* ifm_data needs to be aligned */
293 ift->ifa_data = data = (void *)ALIGN(data);
294 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
295 data += sizeof(ifm->ifm_data);
296 #else /* HAVE_IFM_DATA */
297 ift->ifa_data = NULL;
298 #endif /* HAVE_IFM_DATA */
300 ift = (ift->ifa_next = ift + 1);
306 ifam = (struct ifa_msghdr *)(void *)rtm;
307 if (idx && ifam->ifam_index != idx)
308 abort(); /* this cannot happen */
310 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
312 ift->ifa_name = cif->ifa_name;
313 ift->ifa_flags = cif->ifa_flags;
314 ift->ifa_data = NULL;
315 p = (char *)(void *)(ifam + 1);
316 /* Scan to look for length of address */
318 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
319 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
322 sa = (struct sockaddr *)(void *)p;
330 for (p = p0, i = 0; i < RTAX_MAX; i++) {
331 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
334 sa = (struct sockaddr *)(void *)p;
339 (struct sockaddr *)(void *)data;
340 memcpy(data, p, len);
346 (struct sockaddr *)(void *)data;
347 if (SA_LEN(sa) == 0) {
348 memset(data, 0, alen);
352 memcpy(data, p, len);
358 (struct sockaddr *)(void *)data;
359 memcpy(data, p, len);
366 #ifdef HAVE_IFAM_DATA
367 /* ifam_data needs to be aligned */
368 ift->ifa_data = data = (void *)ALIGN(data);
369 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
370 data += sizeof(ifam->ifam_data);
371 #endif /* HAVE_IFAM_DATA */
373 ift = (ift->ifa_next = ift + 1);
379 #else /* NET_RT_IFLIST */
381 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
386 ift->ifa_name = names;
387 names[sizeof(ifr->ifr_name)] = 0;
388 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
392 ift->ifa_addr = (struct sockaddr *)data;
394 memcpy(data, sa, SA_LEN(sa));
397 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
398 ift = (ift->ifa_next = ift + 1);
400 #endif /* NET_RT_IFLIST */
402 ift->ifa_next = NULL;
412 freeifaddrs(struct ifaddrs *ifp)