tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / getifaddrs.c
blobbe276c4960ecd263d4c4a282ded8e4193b256bae
1 /* $KAME: getifaddrs.c,v 1.9 2005/11/14 16:53:20 sonic Exp $ */
3 /*
4 * Copyright (c) 1995, 1999
5 * Berkeley Software Design, Inc. All rights reserved.
6 * Copyright (c) 2005 - 2006
7 * Pavel Fedin
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
30 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
31 * try-and-error for region size.
34 #include <conf.h>
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/sockio.h>
38 #include <sys/socket.h>
39 #include <net/if.h>
40 #ifndef ENABLE_SYSCTL
41 #undef NET_RT_IFLIST
42 #endif
43 #ifdef NET_RT_IFLIST
44 #include <sys/param.h>
45 #include <net/route.h>
46 #include <sys/sysctl.h>
47 #include <net/if_dl.h>
48 #endif
50 #include <errno.h>
51 #include <api/amiga_api.h>
52 #include <api/ifaddrs.h>
53 #include <string.h>
55 #include "apicalls.h"
57 #if !defined(AF_LINK)
58 #define SA_LEN(sa) sizeof(struct sockaddr)
59 #endif
61 #if !defined(SA_LEN)
62 #define SA_LEN(sa) (sa)->sa_len
63 #endif
65 #define SALIGN (sizeof(long) - 1)
66 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
68 #ifndef ALIGNBYTES
70 * On systems with a routing socket, ALIGNBYTES should match the value
71 * that the kernel uses when building the messages.
73 #define ALIGNBYTES XXX
74 #endif
75 #ifndef ALIGN
76 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
77 #endif
79 #if _BSDI_VERSION >= 199701
80 #define HAVE_IFM_DATA
81 #endif
83 #if _BSDI_VERSION >= 199802
84 /* ifam_data is very specific to recent versions of bsdi */
85 #define HAVE_IFAM_DATA
86 #endif
88 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
89 #define HAVE_IFM_DATA
90 #endif
92 #define MAX_SYSCTL_TRY 5
94 int
95 getifaddrs(struct ifaddrs **pif, struct SocketBase *SocketBase)
97 int icnt = 1;
98 int dcnt = 0;
99 int ncnt = 0;
100 #ifdef NET_RT_IFLIST
101 int ntry = 0;
102 int mib[6];
103 size_t needed;
104 char *buf;
105 char *next;
106 struct ifaddrs *cif = 0;
107 char *p, *p0;
108 struct rt_msghdr *rtm;
109 struct if_msghdr *ifm;
110 struct ifa_msghdr *ifam;
111 struct sockaddr_dl *dl;
112 struct sockaddr *sa;
113 u_short idx = 0;
114 int errno;
115 #else /* NET_RT_IFLIST */
116 char buf[1024];
117 __unused int m;
118 int sock;
119 struct ifconf ifc;
120 struct ifreq *ifr;
121 struct ifreq *lifr;
122 #endif /* NET_RT_IFLIST */
123 struct ifaddrs *ifa, *ift;
124 int i;
125 __unused size_t len, alen;
126 char *data;
127 char *names;
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 do {
138 * We'll try to get addresses several times in case that
139 * the number of addresses is unexpectedly increased during
140 * the two sysctl calls. This should rarely happen, but we'll
141 * try to do our best for applications that assume success of
142 * this library (which should usually be the case).
143 * Portability note: since FreeBSD does not add margin of
144 * memory at the first sysctl, the possibility of failure on
145 * the second sysctl call is a bit higher.
148 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
149 return (-1);
150 if ((buf = bsd_malloc(needed, NULL, NULL)) == NULL)
151 return (-1);
152 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
153 errno = readErrnoValue(SocketBase);
154 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
155 bsd_free(buf, NULL);
156 return (-1);
158 bsd_free(buf, NULL);
159 buf = NULL;
161 } while (buf == NULL);
163 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
164 rtm = (struct rt_msghdr *)(void *)next;
165 if (rtm->rtm_version != RTM_VERSION)
166 continue;
167 switch (rtm->rtm_type) {
168 case RTM_IFINFO:
169 ifm = (struct if_msghdr *)(void *)rtm;
170 if (ifm->ifm_addrs & RTA_IFP) {
171 idx = ifm->ifm_index;
172 ++icnt;
173 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
174 dcnt += SA_RLEN((struct sockaddr *)(void*)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 idx = 0;
182 break;
184 case RTM_NEWADDR:
185 ifam = (struct ifa_msghdr *)(void *)rtm;
186 if (idx && ifam->ifam_index != idx)
187 abort(); /* this cannot happen */
189 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
190 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
191 break;
192 p = (char *)(void *)(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 *)(void *)p;
204 len = SA_RLEN(sa);
205 if (i == RTAX_IFA) {
206 alen = len;
207 break;
209 p += len;
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 *)(void *)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;
223 break;
226 #else /* NET_RT_IFLIST */
227 ifc.ifc_buf = buf;
228 ifc.ifc_len = sizeof(buf);
230 if ((sock = __socket(AF_INET, SOCK_STREAM, 0, SocketBase)) < 0)
231 return (-1);
232 i = __IoctlSocket(sock, SIOCGIFCONF, (char *)&ifc, SocketBase);
233 __CloseSocket(sock, SocketBase);
234 if (i < 0)
235 return (-1);
237 ifr = ifc.ifc_req;
238 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
240 while (ifr < lifr) {
241 struct sockaddr *sa;
243 sa = &ifr->ifr_addr;
244 ++icnt;
245 dcnt += SA_RLEN(sa);
246 ncnt += sizeof(ifr->ifr_name) + 1;
248 if (SA_LEN(sa) < sizeof(*sa))
249 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
250 else
251 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
253 #endif /* NET_RT_IFLIST */
255 if (icnt + dcnt + ncnt == 1) {
256 *pif = NULL;
257 bsd_free(buf, NULL);
258 return (0);
260 data = bsd_malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt, NULL, NULL);
261 if (data == NULL) {
262 bsd_free(buf, NULL);
263 return(-1);
266 ifa = (struct ifaddrs *)(void *)data;
267 data += sizeof(struct ifaddrs) * icnt;
268 names = data + dcnt;
270 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
271 ift = ifa;
273 #ifdef NET_RT_IFLIST
274 idx = 0;
275 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
276 rtm = (struct rt_msghdr *)(void *)next;
277 if (rtm->rtm_version != RTM_VERSION)
278 continue;
279 switch (rtm->rtm_type) {
280 case RTM_IFINFO:
281 ifm = (struct if_msghdr *)(void *)rtm;
282 if (ifm->ifm_addrs & RTA_IFP) {
283 idx = ifm->ifm_index;
284 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
286 cif = ift;
287 ift->ifa_name = names;
288 ift->ifa_flags = (int)ifm->ifm_flags;
289 memcpy(names, dl->sdl_data,
290 (size_t)dl->sdl_nlen);
291 names[dl->sdl_nlen] = 0;
292 names += dl->sdl_nlen + 1;
294 ift->ifa_addr = (struct sockaddr *)(void *)data;
295 memcpy(data, dl,
296 (size_t)SA_LEN((struct sockaddr *)
297 (void *)dl));
298 data += SA_RLEN((struct sockaddr *)(void *)dl);
300 #ifdef HAVE_IFM_DATA
301 /* ifm_data needs to be aligned */
302 ift->ifa_data = data = (void *)ALIGN(data);
303 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
304 data += sizeof(ifm->ifm_data);
305 #else /* HAVE_IFM_DATA */
306 ift->ifa_data = NULL;
307 #endif /* HAVE_IFM_DATA */
309 ift = (ift->ifa_next = ift + 1);
310 } else
311 idx = 0;
312 break;
314 case RTM_NEWADDR:
315 ifam = (struct ifa_msghdr *)(void *)rtm;
316 if (idx && ifam->ifam_index != idx)
317 abort(); /* this cannot happen */
319 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
320 break;
321 ift->ifa_name = cif->ifa_name;
322 ift->ifa_flags = cif->ifa_flags;
323 ift->ifa_data = NULL;
324 p = (char *)(void *)(ifam + 1);
325 /* Scan to look for length of address */
326 alen = 0;
327 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
328 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
329 == 0)
330 continue;
331 sa = (struct sockaddr *)(void *)p;
332 len = SA_RLEN(sa);
333 if (i == RTAX_IFA) {
334 alen = len;
335 break;
337 p += len;
339 for (p = p0, i = 0; i < RTAX_MAX; i++) {
340 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
341 == 0)
342 continue;
343 sa = (struct sockaddr *)(void *)p;
344 len = SA_RLEN(sa);
345 switch (i) {
346 case RTAX_IFA:
347 ift->ifa_addr =
348 (struct sockaddr *)(void *)data;
349 memcpy(data, p, len);
350 data += len;
351 break;
353 case RTAX_NETMASK:
354 ift->ifa_netmask =
355 (struct sockaddr *)(void *)data;
356 if (SA_LEN(sa) == 0) {
357 memset(data, 0, alen);
358 data += alen;
359 break;
361 memcpy(data, p, len);
362 data += len;
363 break;
365 case RTAX_BRD:
366 ift->ifa_broadaddr =
367 (struct sockaddr *)(void *)data;
368 memcpy(data, p, len);
369 data += len;
370 break;
372 p += len;
375 #ifdef HAVE_IFAM_DATA
376 /* ifam_data needs to be aligned */
377 ift->ifa_data = data = (void *)ALIGN(data);
378 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
379 data += sizeof(ifam->ifam_data);
380 #endif /* HAVE_IFAM_DATA */
382 ift = (ift->ifa_next = ift + 1);
383 break;
387 bsd_free(buf, NULL);
388 #else /* NET_RT_IFLIST */
389 ifr = ifc.ifc_req;
390 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
392 while (ifr < lifr) {
393 struct sockaddr *sa;
395 ift->ifa_name = names;
396 names[sizeof(ifr->ifr_name)] = 0;
397 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
398 while (*names++)
401 ift->ifa_addr = (struct sockaddr *)data;
402 sa = &ifr->ifr_addr;
403 memcpy(data, sa, SA_LEN(sa));
404 data += SA_RLEN(sa);
406 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
407 ift = (ift->ifa_next = ift + 1);
409 #endif /* NET_RT_IFLIST */
410 if (--ift >= ifa) {
411 ift->ifa_next = NULL;
412 *pif = ifa;
413 } else {
414 *pif = NULL;
415 bsd_free(ifa, NULL);
417 return (0);
420 void
421 freeifaddrs(struct ifaddrs *ifp)
424 bsd_free(ifp, NULL);