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