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>
37 #include <sys/param.h>
38 #include <net/route.h>
39 #include <sys/sysctl.h>
40 #include <net/if_dl.h>
46 #include "un-namespace.h"
49 #define SA_LEN(sa) sizeof(struct sockaddr)
53 #define SA_LEN(sa) (sa)->sa_len
56 #define SA_RLEN(sa) RT_ROUNDUP((sa)->sa_len)
60 * On systems with a routing socket, ALIGNBYTES should match the value
61 * that the kernel uses when building the messages.
63 #define ALIGNBYTES XXX
66 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
69 #define MAX_SYSCTL_TRY 5
72 getifaddrs(struct ifaddrs
**pif
)
82 struct ifaddrs
*cif
= NULL
;
84 struct rt_msghdr
*rtm
;
85 struct if_msghdr
*ifm
;
86 struct ifa_msghdr
*ifam
;
87 struct sockaddr_dl
*dl
;
89 struct ifaddrs
*ifa
, *ift
;
98 mib
[2] = 0; /* protocol */
99 mib
[3] = 0; /* wildcard address family */
100 mib
[4] = NET_RT_IFLIST
;
101 mib
[5] = 0; /* no flags */
104 * We'll try to get addresses several times in case that
105 * the number of addresses is unexpectedly increased during
106 * the two sysctl calls. This should rarely happen, but we'll
107 * try to do our best for applications that assume success of
108 * this library (which should usually be the case).
109 * Portability note: since FreeBSD does not add margin of
110 * memory at the first sysctl, the possibility of failure on
111 * the second sysctl call is a bit higher.
114 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
116 if ((buf
= malloc(needed
)) == NULL
)
118 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0) {
119 if (errno
!= ENOMEM
|| ++ntry
>= MAX_SYSCTL_TRY
) {
126 } while (buf
== NULL
);
128 for (next
= buf
; next
< buf
+ needed
; next
+= rtm
->rtm_msglen
) {
129 rtm
= (struct rt_msghdr
*)(void *)next
;
130 if (rtm
->rtm_version
!= RTM_VERSION
)
132 switch (rtm
->rtm_type
) {
134 ifm
= (struct if_msghdr
*)(void *)rtm
;
135 if (ifm
->ifm_addrs
& RTA_IFP
) {
136 idx
= ifm
->ifm_index
;
138 dl
= (struct sockaddr_dl
*)(void *)(ifm
+ 1);
139 dcnt
+= SA_RLEN((struct sockaddr
*)(void*)dl
) +
141 dcnt
+= sizeof(ifm
->ifm_data
);
142 ncnt
+= dl
->sdl_nlen
+ 1;
148 ifam
= (struct ifa_msghdr
*)(void *)rtm
;
149 if (idx
&& ifam
->ifam_index
!= idx
)
150 abort(); /* this cannot happen */
152 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
153 if (idx
== 0 || (ifam
->ifam_addrs
& RTA_MASKS
) == 0)
155 p
= (char *)(void *)(ifam
+ 1);
157 /* Scan to look for length of address */
159 for (p0
= p
, i
= 0; i
< RTAX_MAX
; i
++) {
160 if ((RTA_MASKS
& ifam
->ifam_addrs
& (1 << i
))
163 sa
= (struct sockaddr
*)(void *)p
;
171 for (p
= p0
, i
= 0; i
< RTAX_MAX
; i
++) {
172 if ((RTA_MASKS
& ifam
->ifam_addrs
& (1 << i
))
175 sa
= (struct sockaddr
*)(void *)p
;
177 if (i
== RTAX_NETMASK
&& SA_LEN(sa
) == 0)
187 if (icnt
+ dcnt
+ ncnt
== 1) {
192 data
= malloc(sizeof(struct ifaddrs
) * icnt
+ dcnt
+ ncnt
);
198 ifa
= (struct ifaddrs
*)(void *)data
;
199 data
+= sizeof(struct ifaddrs
) * icnt
;
202 memset(ifa
, 0, sizeof(struct ifaddrs
) * icnt
);
206 for (next
= buf
; next
< buf
+ needed
; next
+= rtm
->rtm_msglen
) {
207 rtm
= (struct rt_msghdr
*)(void *)next
;
208 if (rtm
->rtm_version
!= RTM_VERSION
)
210 switch (rtm
->rtm_type
) {
212 ifm
= (struct if_msghdr
*)(void *)rtm
;
213 if (ifm
->ifm_addrs
& RTA_IFP
) {
214 idx
= ifm
->ifm_index
;
215 dl
= (struct sockaddr_dl
*)(void *)(ifm
+ 1);
218 ift
->ifa_name
= names
;
219 ift
->ifa_flags
= (int)ifm
->ifm_flags
;
220 memcpy(names
, dl
->sdl_data
,
221 (size_t)dl
->sdl_nlen
);
222 names
[dl
->sdl_nlen
] = 0;
223 names
+= dl
->sdl_nlen
+ 1;
225 ift
->ifa_addr
= (struct sockaddr
*)(void *)data
;
227 (size_t)SA_LEN((struct sockaddr
*)
229 data
+= SA_RLEN((struct sockaddr
*)(void *)dl
);
231 /* ifm_data needs to be aligned */
232 ift
->ifa_data
= data
= (void *)ALIGN(data
);
233 memcpy(data
, &ifm
->ifm_data
, sizeof(ifm
->ifm_data
));
234 data
+= sizeof(ifm
->ifm_data
);
235 ift
= (ift
->ifa_next
= ift
+ 1);
241 ifam
= (struct ifa_msghdr
*)(void *)rtm
;
242 if (idx
&& ifam
->ifam_index
!= idx
)
243 abort(); /* this cannot happen */
245 if (idx
== 0 || (ifam
->ifam_addrs
& RTA_MASKS
) == 0)
247 ift
->ifa_name
= cif
->ifa_name
;
248 ift
->ifa_flags
= cif
->ifa_flags
;
249 ift
->ifa_data
= NULL
;
250 p
= (char *)(void *)(ifam
+ 1);
251 /* Scan to look for length of address */
253 for (p0
= p
, i
= 0; i
< RTAX_MAX
; i
++) {
254 if ((RTA_MASKS
& ifam
->ifam_addrs
& (1 << i
))
257 sa
= (struct sockaddr
*)(void *)p
;
265 for (p
= p0
, i
= 0; i
< RTAX_MAX
; i
++) {
266 if ((RTA_MASKS
& ifam
->ifam_addrs
& (1 << i
))
269 sa
= (struct sockaddr
*)(void *)p
;
274 (struct sockaddr
*)(void *)data
;
275 memcpy(data
, p
, len
);
281 (struct sockaddr
*)(void *)data
;
282 if (SA_LEN(sa
) == 0) {
283 memset(data
, 0, alen
);
287 memcpy(data
, p
, len
);
293 (struct sockaddr
*)(void *)data
;
294 memcpy(data
, p
, len
);
301 ift
= (ift
->ifa_next
= ift
+ 1);
308 ift
->ifa_next
= NULL
;
318 freeifaddrs(struct ifaddrs
*ifp
)