]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/getifaddrs.c
Libinfo-278.tar.gz
[apple/libinfo.git] / gen.subproj / getifaddrs.c
1 /*
2 * Copyright (c) 1995, 1999
3 * Berkeley Software Design, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
12 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
15 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
21 * SUCH DAMAGE.
22 *
23 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
24 */
25 /*
26 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
27 * try-and-error for region size.
28 */
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <net/if.h>
33 #ifdef NET_RT_IFLIST
34 #include <sys/param.h>
35 #include <net/route.h>
36 #include <sys/sysctl.h>
37 #include <net/if_dl.h>
38 #endif
39
40 #include <errno.h>
41 #include <ifaddrs.h>
42 #include <netinet/in.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stdint.h>
46
47 #if !defined(AF_LINK)
48 #define SA_LEN(sa) sizeof(struct sockaddr)
49 #endif
50
51 #if !defined(SA_LEN)
52 #define SA_LEN(sa) (sa)->sa_len
53 #endif
54
55 #define SALIGN (sizeof(int32_t) - 1)
56 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
57
58 #ifndef ALIGNBYTES
59 /*
60 * On systems with a routing socket, ALIGNBYTES should match the value
61 * that the kernel uses when building the messages.
62 */
63 #define ALIGNBYTES XXX
64 #endif
65 #ifndef ALIGN
66 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
67 #endif
68
69 #if _BSDI_VERSION >= 199701
70 #define HAVE_IFM_DATA
71 #endif
72
73 #if _BSDI_VERSION >= 199802
74 /* ifam_data is very specific to recent versions of bsdi */
75 #define HAVE_IFAM_DATA
76 #endif
77
78 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
79 #define HAVE_IFM_DATA
80 #endif
81
82 #ifdef __APPLE__
83 #define HAVE_IFM_DATA
84 #endif
85
86 #define MEMORY_MIN 2048
87 #define MEMORY_MAX 4194304
88
89 int
90 getifaddrs(struct ifaddrs **pif)
91 {
92 int icnt = 1;
93 int dcnt = 0;
94 int ncnt = 0;
95 struct ifaddrs *ifa, *ift;
96 struct sockaddr_in6 *sin6;
97 uint16_t esid;
98 #ifdef NET_RT_IFLIST
99 int mib[6];
100 size_t needed;
101 char *buf;
102 char *next;
103 struct ifaddrs *cif = 0;
104 char *p, *p0;
105 struct rt_msghdr *rtm;
106 struct if_msghdr *ifm;
107 struct ifa_msghdr *ifam;
108 struct sockaddr_dl *dl;
109 struct sockaddr *sa;
110 u_short index = 0;
111 #else /* NET_RT_IFLIST */
112 char buf[1024];
113 int m, sock;
114 struct ifconf ifc;
115 struct ifreq *ifr;
116 struct ifreq *lifr;
117 #endif /* NET_RT_IFLIST */
118 int i, status;
119 size_t len, alen;
120 char *data;
121 char *names;
122
123 #ifdef NET_RT_IFLIST
124 mib[0] = CTL_NET;
125 mib[1] = PF_ROUTE;
126 mib[2] = 0; /* protocol */
127 mib[3] = 0; /* wildcard address family */
128 mib[4] = NET_RT_IFLIST;
129 mib[5] = 0; /* no flags */
130 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) return (-1);
131
132 if (needed < MEMORY_MIN) needed = MEMORY_MIN;
133 needed *= 2;
134
135 while (needed <= MEMORY_MAX)
136 {
137 buf = malloc(needed);
138 if (buf == NULL) return (-1);
139
140 status = sysctl(mib, 6, buf, &needed, NULL, 0);
141 if (status >= 0) break;
142
143 free(buf);
144 buf = NULL;
145 needed *= 2;
146 }
147
148 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
149 rtm = (struct rt_msghdr *)next;
150 if (rtm->rtm_version != RTM_VERSION)
151 continue;
152 switch (rtm->rtm_type) {
153 case RTM_IFINFO:
154 ifm = (struct if_msghdr *)rtm;
155 if (ifm->ifm_addrs & RTA_IFP) {
156 index = ifm->ifm_index;
157 ++icnt;
158 dl = (struct sockaddr_dl *)(ifm + 1);
159 dcnt += SA_RLEN((struct sockaddr *)dl) +
160 ALIGNBYTES;
161 #ifdef HAVE_IFM_DATA
162 dcnt += sizeof(ifm->ifm_data);
163 #endif /* HAVE_IFM_DATA */
164 ncnt += dl->sdl_nlen + 1;
165 } else
166 index = 0;
167 break;
168
169 case RTM_NEWADDR:
170 ifam = (struct ifa_msghdr *)rtm;
171 if (index && ifam->ifam_index != index)
172 abort(); /* this cannot happen */
173
174 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
175 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
176 break;
177 p = (char *)(ifam + 1);
178 ++icnt;
179 #ifdef HAVE_IFAM_DATA
180 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
181 #endif /* HAVE_IFAM_DATA */
182 /* Scan to look for length of address */
183 alen = 0;
184 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
185 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
186 == 0)
187 continue;
188 sa = (struct sockaddr *)p;
189 len = SA_RLEN(sa);
190 if (i == RTAX_IFA) {
191 alen = len;
192 break;
193 }
194 p += len;
195 }
196 for (p = p0, i = 0; i < RTAX_MAX; i++) {
197 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
198 == 0)
199 continue;
200 sa = (struct sockaddr *)p;
201 len = SA_RLEN(sa);
202 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
203 dcnt += alen;
204 else
205 dcnt += len;
206 p += len;
207 }
208 break;
209 }
210 }
211 #else /* NET_RT_IFLIST */
212 ifc.ifc_buf = buf;
213 ifc.ifc_len = sizeof(buf);
214
215 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
216 free(buf);
217 return (-1);
218 }
219
220 i = ioctl(sock, SIOCGIFCONF, (char *)&ifc);
221 close(sock);
222 if (i < 0) {
223 free(buf);
224 return (-1);
225 }
226
227 ifr = ifc.ifc_req;
228 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
229
230 while (ifr < lifr) {
231 struct sockaddr *sa;
232
233 sa = &ifr->ifr_addr;
234 ++icnt;
235 dcnt += SA_RLEN(sa);
236 ncnt += sizeof(ifr->ifr_name) + 1;
237
238 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
239 }
240 #endif /* NET_RT_IFLIST */
241
242 if (icnt + dcnt + ncnt == 1) {
243 *pif = NULL;
244 free(buf);
245 return (0);
246 }
247 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
248 if (data == NULL) {
249 free(buf);
250 return(-1);
251 }
252
253 ifa = (struct ifaddrs *)data;
254 data += sizeof(struct ifaddrs) * icnt;
255 names = data + dcnt;
256
257 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
258 ift = ifa;
259
260 #ifdef NET_RT_IFLIST
261 index = 0;
262 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
263 rtm = (struct rt_msghdr *)next;
264 if (rtm->rtm_version != RTM_VERSION)
265 continue;
266 switch (rtm->rtm_type) {
267 case RTM_IFINFO:
268 ifm = (struct if_msghdr *)rtm;
269 if (ifm->ifm_addrs & RTA_IFP) {
270 index = ifm->ifm_index;
271 dl = (struct sockaddr_dl *)(ifm + 1);
272
273 cif = ift;
274 ift->ifa_name = names;
275 ift->ifa_flags = (int)ifm->ifm_flags;
276 memcpy(names, dl->sdl_data, dl->sdl_nlen);
277 names[dl->sdl_nlen] = 0;
278 names += dl->sdl_nlen + 1;
279
280 ift->ifa_addr = (struct sockaddr *)data;
281 memcpy(data, dl, SA_LEN((struct sockaddr *)dl));
282 data += SA_RLEN((struct sockaddr *)dl);
283
284 #ifdef HAVE_IFM_DATA
285 /* ifm_data needs to be aligned */
286 ift->ifa_data = data = (void *)ALIGN(data);
287 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
288 data += sizeof(ifm->ifm_data);
289 #else /* HAVE_IFM_DATA */
290 ift->ifa_data = NULL;
291 #endif /* HAVE_IFM_DATA */
292
293 ift = (ift->ifa_next = ift + 1);
294 } else
295 index = 0;
296 break;
297
298 case RTM_NEWADDR:
299 ifam = (struct ifa_msghdr *)rtm;
300 if (index && ifam->ifam_index != index)
301 abort(); /* this cannot happen */
302
303 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
304 break;
305 ift->ifa_name = cif->ifa_name;
306 ift->ifa_flags = cif->ifa_flags;
307 ift->ifa_data = NULL;
308 p = (char *)(ifam + 1);
309 /* Scan to look for length of address */
310 alen = 0;
311 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
312 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
313 == 0)
314 continue;
315 sa = (struct sockaddr *)p;
316 len = SA_RLEN(sa);
317 if (i == RTAX_IFA) {
318 alen = len;
319 break;
320 }
321 p += len;
322 }
323 for (p = p0, i = 0; i < RTAX_MAX; i++) {
324 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
325 == 0)
326 continue;
327 sa = (struct sockaddr *)p;
328 len = SA_RLEN(sa);
329 switch (i) {
330 case RTAX_IFA:
331 ift->ifa_addr = (struct sockaddr *)data;
332 memcpy(data, p, len);
333 data += len;
334 break;
335
336 case RTAX_NETMASK:
337 ift->ifa_netmask =
338 (struct sockaddr *)data;
339 if (SA_LEN(sa) == 0) {
340 memset(data, 0, alen);
341 data += alen;
342 break;
343 }
344 memcpy(data, p, len);
345 data += len;
346 break;
347
348 case RTAX_BRD:
349 ift->ifa_broadaddr =
350 (struct sockaddr *)data;
351 memcpy(data, p, len);
352 data += len;
353 break;
354 }
355 p += len;
356 }
357
358 #ifdef HAVE_IFAM_DATA
359 /* ifam_data needs to be aligned */
360 ift->ifa_data = data = (void *)ALIGN(data);
361 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
362 data += sizeof(ifam->ifam_data);
363 #endif /* HAVE_IFAM_DATA */
364
365 ift = (ift->ifa_next = ift + 1);
366 break;
367 }
368 }
369
370 free(buf);
371 #else /* NET_RT_IFLIST */
372 ifr = ifc.ifc_req;
373 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
374
375 while (ifr < lifr) {
376 struct sockaddr *sa;
377
378 ift->ifa_name = names;
379 names[sizeof(ifr->ifr_name)] = 0;
380 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
381 while (*names++)
382 ;
383
384 ift->ifa_addr = (struct sockaddr *)data;
385 sa = &ifr->ifr_addr;
386 memcpy(data, sa, SA_LEN(sa));
387 data += SA_RLEN(sa);
388
389 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
390 ift = (ift->ifa_next = ift + 1);
391 }
392 #endif /* NET_RT_IFLIST */
393 if (--ift >= ifa) {
394 ift->ifa_next = NULL;
395 *pif = ifa;
396 } else {
397 *pif = NULL;
398 free(ifa);
399 }
400
401 for (ift = ifa; ift != NULL; ift = ift->ifa_next)
402 {
403 if (ift->ifa_addr->sa_family == AF_INET6)
404 {
405 sin6 = (struct sockaddr_in6 *)ift->ifa_addr;
406 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) || IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
407 {
408 esid = ntohs(sin6->sin6_addr.__u6_addr.__u6_addr16[1]);
409 sin6->sin6_addr.__u6_addr.__u6_addr16[1] = 0;
410 if (sin6->sin6_scope_id == 0) sin6->sin6_scope_id = esid;
411 }
412 }
413 }
414
415 return (0);
416 }
417
418 void
419 freeifaddrs(struct ifaddrs *ifp)
420 {
421 free(ifp);
422 }