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