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.3 2003/07/13 05:45:05 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__) || defined(__FreeBSD__)
84 #define MAX_SYSCTL_TRY 5
87 getifaddrs(struct ifaddrs **pif)
98 struct ifaddrs *cif = 0;
100 struct rt_msghdr *rtm;
101 struct if_msghdr *ifm;
102 struct ifa_msghdr *ifam;
103 struct sockaddr_dl *dl;
105 struct ifaddrs *ifa, *ift;
107 #else /* NET_RT_IFLIST */
113 #endif /* NET_RT_IFLIST */
122 mib[2] = 0; /* protocol */
123 mib[3] = 0; /* wildcard address family */
124 mib[4] = NET_RT_IFLIST;
125 mib[5] = 0; /* no flags */
128 * We'll try to get addresses several times in case that
129 * the number of addresses is unexpectedly increased during
130 * the two sysctl calls. This should rarely happen, but we'll
131 * try to do our best for applications that assume success of
132 * this library (which should usually be the case).
133 * Portability note: since FreeBSD does not add margin of
134 * memory at the first sysctl, the possibility of failure on
135 * the second sysctl call is a bit higher.
138 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
140 if ((buf = malloc(needed)) == NULL)
142 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
143 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
150 } while (buf == NULL);
152 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
153 rtm = (struct rt_msghdr *)(void *)next;
154 if (rtm->rtm_version != RTM_VERSION)
156 switch (rtm->rtm_type) {
158 ifm = (struct if_msghdr *)(void *)rtm;
159 if (ifm->ifm_addrs & RTA_IFP) {
160 idx = ifm->ifm_index;
162 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
163 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
166 dcnt += sizeof(ifm->ifm_data);
167 #endif /* HAVE_IFM_DATA */
168 ncnt += dl->sdl_nlen + 1;
174 ifam = (struct ifa_msghdr *)(void *)rtm;
175 if (idx && ifam->ifam_index != idx)
176 abort(); /* this cannot happen */
178 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
179 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
181 p = (char *)(void *)(ifam + 1);
183 #ifdef HAVE_IFAM_DATA
184 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
185 #endif /* HAVE_IFAM_DATA */
186 /* Scan to look for length of address */
188 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
189 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
192 sa = (struct sockaddr *)(void *)p;
200 for (p = p0, i = 0; i < RTAX_MAX; i++) {
201 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
204 sa = (struct sockaddr *)(void *)p;
206 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
215 #else /* NET_RT_IFLIST */
217 ifc.ifc_len = sizeof(buf);
219 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
221 i = ioctl(sock, SIOCGIFCONF, (char *)&ifc);
227 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
235 ncnt += sizeof(ifr->ifr_name) + 1;
237 if (SA_LEN(sa) < sizeof(*sa))
238 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
240 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
242 #endif /* NET_RT_IFLIST */
244 if (icnt + dcnt + ncnt == 1) {
249 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
255 ifa = (struct ifaddrs *)(void *)data;
256 data += sizeof(struct ifaddrs) * icnt;
259 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
264 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
265 rtm = (struct rt_msghdr *)(void *)next;
266 if (rtm->rtm_version != RTM_VERSION)
268 switch (rtm->rtm_type) {
270 ifm = (struct if_msghdr *)(void *)rtm;
271 if (ifm->ifm_addrs & RTA_IFP) {
272 idx = ifm->ifm_index;
273 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
276 ift->ifa_name = names;
277 ift->ifa_flags = (int)ifm->ifm_flags;
278 memcpy(names, dl->sdl_data,
279 (size_t)dl->sdl_nlen);
280 names[dl->sdl_nlen] = 0;
281 names += dl->sdl_nlen + 1;
283 ift->ifa_addr = (struct sockaddr *)(void *)data;
285 (size_t)SA_LEN((struct sockaddr *)
287 data += SA_RLEN((struct sockaddr *)(void *)dl);
290 /* ifm_data needs to be aligned */
291 ift->ifa_data = data = (void *)ALIGN(data);
292 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
293 data += sizeof(ifm->ifm_data);
294 #else /* HAVE_IFM_DATA */
295 ift->ifa_data = NULL;
296 #endif /* HAVE_IFM_DATA */
298 ift = (ift->ifa_next = ift + 1);
304 ifam = (struct ifa_msghdr *)(void *)rtm;
305 if (idx && ifam->ifam_index != idx)
306 abort(); /* this cannot happen */
308 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
310 ift->ifa_name = cif->ifa_name;
311 ift->ifa_flags = cif->ifa_flags;
312 ift->ifa_data = NULL;
313 p = (char *)(void *)(ifam + 1);
314 /* Scan to look for length of address */
316 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
317 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
320 sa = (struct sockaddr *)(void *)p;
328 for (p = p0, i = 0; i < RTAX_MAX; i++) {
329 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
332 sa = (struct sockaddr *)(void *)p;
337 (struct sockaddr *)(void *)data;
338 memcpy(data, p, len);
344 (struct sockaddr *)(void *)data;
345 if (SA_LEN(sa) == 0) {
346 memset(data, 0, alen);
350 memcpy(data, p, len);
356 (struct sockaddr *)(void *)data;
357 memcpy(data, p, len);
364 #ifdef HAVE_IFAM_DATA
365 /* ifam_data needs to be aligned */
366 ift->ifa_data = data = (void *)ALIGN(data);
367 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
368 data += sizeof(ifam->ifam_data);
369 #endif /* HAVE_IFAM_DATA */
371 ift = (ift->ifa_next = ift + 1);
377 #else /* NET_RT_IFLIST */
379 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
384 ift->ifa_name = names;
385 names[sizeof(ifr->ifr_name)] = 0;
386 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
390 ift->ifa_addr = (struct sockaddr *)data;
392 memcpy(data, sa, SA_LEN(sa));
395 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
396 ift = (ift->ifa_next = ift + 1);
398 #endif /* NET_RT_IFLIST */
400 ift->ifa_next = NULL;
410 freeifaddrs(struct ifaddrs *ifp)