]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/getifaddrs.c
9522032c1db2043b7267486f2f9ba50e976dc262
[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 <stdlib.h>
43 #include <string.h>
44
45 #if !defined(AF_LINK)
46 #define SA_LEN(sa) sizeof(struct sockaddr)
47 #endif
48
49 #if !defined(SA_LEN)
50 #define SA_LEN(sa) (sa)->sa_len
51 #endif
52
53 #define SALIGN (sizeof(long) - 1)
54 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
55
56 #ifndef ALIGNBYTES
57 /*
58 * On systems with a routing socket, ALIGNBYTES should match the value
59 * that the kernel uses when building the messages.
60 */
61 #define ALIGNBYTES XXX
62 #endif
63 #ifndef ALIGN
64 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
65 #endif
66
67 #if _BSDI_VERSION >= 199701
68 #define HAVE_IFM_DATA
69 #endif
70
71 #if _BSDI_VERSION >= 199802
72 /* ifam_data is very specific to recent versions of bsdi */
73 #define HAVE_IFAM_DATA
74 #endif
75
76 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
77 #define HAVE_IFM_DATA
78 #endif
79
80 int
81 getifaddrs(struct ifaddrs **pif)
82 {
83 int icnt = 1;
84 int dcnt = 0;
85 int ncnt = 0;
86 #ifdef NET_RT_IFLIST
87 int mib[6];
88 size_t needed;
89 char *buf;
90 char *next;
91 struct ifaddrs *cif = 0;
92 char *p, *p0;
93 struct rt_msghdr *rtm;
94 struct if_msghdr *ifm;
95 struct ifa_msghdr *ifam;
96 struct sockaddr_dl *dl;
97 struct sockaddr *sa;
98 struct ifaddrs *ifa, *ift;
99 u_short index = 0;
100 #else /* NET_RT_IFLIST */
101 char buf[1024];
102 int m, sock;
103 struct ifconf ifc;
104 struct ifreq *ifr;
105 struct ifreq *lifr;
106 #endif /* NET_RT_IFLIST */
107 int i;
108 size_t len, alen;
109 char *data;
110 char *names;
111
112 #ifdef NET_RT_IFLIST
113 mib[0] = CTL_NET;
114 mib[1] = PF_ROUTE;
115 mib[2] = 0; /* protocol */
116 mib[3] = 0; /* wildcard address family */
117 mib[4] = NET_RT_IFLIST;
118 mib[5] = 0; /* no flags */
119 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
120 return (-1);
121 if ((buf = malloc(needed)) == NULL)
122 return (-1);
123 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
124 free(buf);
125 return (-1);
126 }
127
128 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
129 rtm = (struct rt_msghdr *)next;
130 if (rtm->rtm_version != RTM_VERSION)
131 continue;
132 switch (rtm->rtm_type) {
133 case RTM_IFINFO:
134 ifm = (struct if_msghdr *)rtm;
135 if (ifm->ifm_addrs & RTA_IFP) {
136 index = ifm->ifm_index;
137 ++icnt;
138 dl = (struct sockaddr_dl *)(ifm + 1);
139 dcnt += SA_RLEN((struct sockaddr *)dl) +
140 ALIGNBYTES;
141 #ifdef HAVE_IFM_DATA
142 dcnt += sizeof(ifm->ifm_data);
143 #endif /* HAVE_IFM_DATA */
144 ncnt += dl->sdl_nlen + 1;
145 } else
146 index = 0;
147 break;
148
149 case RTM_NEWADDR:
150 ifam = (struct ifa_msghdr *)rtm;
151 if (index && ifam->ifam_index != index)
152 abort(); /* this cannot happen */
153
154 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
155 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
156 break;
157 p = (char *)(ifam + 1);
158 ++icnt;
159 #ifdef HAVE_IFAM_DATA
160 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
161 #endif /* HAVE_IFAM_DATA */
162 /* Scan to look for length of address */
163 alen = 0;
164 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
165 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
166 == 0)
167 continue;
168 sa = (struct sockaddr *)p;
169 len = SA_RLEN(sa);
170 if (i == RTAX_IFA) {
171 alen = len;
172 break;
173 }
174 p += len;
175 }
176 for (p = p0, i = 0; i < RTAX_MAX; i++) {
177 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
178 == 0)
179 continue;
180 sa = (struct sockaddr *)p;
181 len = SA_RLEN(sa);
182 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
183 dcnt += alen;
184 else
185 dcnt += len;
186 p += len;
187 }
188 break;
189 }
190 }
191 #else /* NET_RT_IFLIST */
192 ifc.ifc_buf = buf;
193 ifc.ifc_len = sizeof(buf);
194
195 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
196 return (-1);
197 i = ioctl(sock, SIOCGIFCONF, (char *)&ifc);
198 close(sock);
199 if (i < 0)
200 return (-1);
201
202 ifr = ifc.ifc_req;
203 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
204
205 while (ifr < lifr) {
206 struct sockaddr *sa;
207
208 sa = &ifr->ifr_addr;
209 ++icnt;
210 dcnt += SA_RLEN(sa);
211 ncnt += sizeof(ifr->ifr_name) + 1;
212
213 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
214 }
215 #endif /* NET_RT_IFLIST */
216
217 if (icnt + dcnt + ncnt == 1) {
218 *pif = NULL;
219 free(buf);
220 return (0);
221 }
222 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
223 if (data == NULL) {
224 free(buf);
225 return(-1);
226 }
227
228 ifa = (struct ifaddrs *)data;
229 data += sizeof(struct ifaddrs) * icnt;
230 names = data + dcnt;
231
232 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
233 ift = ifa;
234
235 #ifdef NET_RT_IFLIST
236 index = 0;
237 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
238 rtm = (struct rt_msghdr *)next;
239 if (rtm->rtm_version != RTM_VERSION)
240 continue;
241 switch (rtm->rtm_type) {
242 case RTM_IFINFO:
243 ifm = (struct if_msghdr *)rtm;
244 if (ifm->ifm_addrs & RTA_IFP) {
245 index = ifm->ifm_index;
246 dl = (struct sockaddr_dl *)(ifm + 1);
247
248 cif = ift;
249 ift->ifa_name = names;
250 ift->ifa_flags = (int)ifm->ifm_flags;
251 memcpy(names, dl->sdl_data, dl->sdl_nlen);
252 names[dl->sdl_nlen] = 0;
253 names += dl->sdl_nlen + 1;
254
255 ift->ifa_addr = (struct sockaddr *)data;
256 memcpy(data, dl, SA_LEN((struct sockaddr *)dl));
257 data += SA_RLEN((struct sockaddr *)dl);
258
259 #ifdef HAVE_IFM_DATA
260 /* ifm_data needs to be aligned */
261 ift->ifa_data = data = (void *)ALIGN(data);
262 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
263 data += sizeof(ifm->ifm_data);
264 #else /* HAVE_IFM_DATA */
265 ift->ifa_data = NULL;
266 #endif /* HAVE_IFM_DATA */
267
268 ift = (ift->ifa_next = ift + 1);
269 } else
270 index = 0;
271 break;
272
273 case RTM_NEWADDR:
274 ifam = (struct ifa_msghdr *)rtm;
275 if (index && ifam->ifam_index != index)
276 abort(); /* this cannot happen */
277
278 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
279 break;
280 ift->ifa_name = cif->ifa_name;
281 ift->ifa_flags = cif->ifa_flags;
282 ift->ifa_data = NULL;
283 p = (char *)(ifam + 1);
284 /* Scan to look for length of address */
285 alen = 0;
286 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
287 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
288 == 0)
289 continue;
290 sa = (struct sockaddr *)p;
291 len = SA_RLEN(sa);
292 if (i == RTAX_IFA) {
293 alen = len;
294 break;
295 }
296 p += len;
297 }
298 for (p = p0, i = 0; i < RTAX_MAX; i++) {
299 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
300 == 0)
301 continue;
302 sa = (struct sockaddr *)p;
303 len = SA_RLEN(sa);
304 switch (i) {
305 case RTAX_IFA:
306 ift->ifa_addr = (struct sockaddr *)data;
307 memcpy(data, p, len);
308 data += len;
309 break;
310
311 case RTAX_NETMASK:
312 ift->ifa_netmask =
313 (struct sockaddr *)data;
314 if (SA_LEN(sa) == 0) {
315 memset(data, 0, alen);
316 data += alen;
317 break;
318 }
319 memcpy(data, p, len);
320 data += len;
321 break;
322
323 case RTAX_BRD:
324 ift->ifa_broadaddr =
325 (struct sockaddr *)data;
326 memcpy(data, p, len);
327 data += len;
328 break;
329 }
330 p += len;
331 }
332
333 #ifdef HAVE_IFAM_DATA
334 /* ifam_data needs to be aligned */
335 ift->ifa_data = data = (void *)ALIGN(data);
336 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
337 data += sizeof(ifam->ifam_data);
338 #endif /* HAVE_IFAM_DATA */
339
340 ift = (ift->ifa_next = ift + 1);
341 break;
342 }
343 }
344
345 free(buf);
346 #else /* NET_RT_IFLIST */
347 ifr = ifc.ifc_req;
348 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
349
350 while (ifr < lifr) {
351 struct sockaddr *sa;
352
353 ift->ifa_name = names;
354 names[sizeof(ifr->ifr_name)] = 0;
355 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
356 while (*names++)
357 ;
358
359 ift->ifa_addr = (struct sockaddr *)data;
360 sa = &ifr->ifr_addr;
361 memcpy(data, sa, SA_LEN(sa));
362 data += SA_RLEN(sa);
363
364 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
365 ift = (ift->ifa_next = ift + 1);
366 }
367 #endif /* NET_RT_IFLIST */
368 if (--ift >= ifa) {
369 ift->ifa_next = NULL;
370 *pif = ifa;
371 } else {
372 *pif = NULL;
373 free(ifa);
374 }
375 return (0);
376 }
377
378 void
379 freeifaddrs(struct ifaddrs *ifp)
380 {
381 free(ifp);
382 }