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