diff options
Diffstat (limited to 'src/lib/libc/net/getifaddrs.c')
-rw-r--r-- | src/lib/libc/net/getifaddrs.c | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/src/lib/libc/net/getifaddrs.c b/src/lib/libc/net/getifaddrs.c new file mode 100644 index 0000000000..0db89f6c19 --- /dev/null +++ b/src/lib/libc/net/getifaddrs.c | |||
@@ -0,0 +1,277 @@ | |||
1 | /* $OpenBSD: getifaddrs.c,v 1.9 2002/08/09 06:12:25 itojun 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/types.h> | ||
29 | #include <sys/ioctl.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <sys/param.h> | ||
33 | #include <net/route.h> | ||
34 | #include <sys/sysctl.h> | ||
35 | #include <net/if_dl.h> | ||
36 | |||
37 | #include <errno.h> | ||
38 | #include <ifaddrs.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <string.h> | ||
41 | #include <unistd.h> | ||
42 | |||
43 | #define SALIGN (sizeof(long) - 1) | ||
44 | #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1)) | ||
45 | |||
46 | int | ||
47 | getifaddrs(struct ifaddrs **pif) | ||
48 | { | ||
49 | int icnt = 1; | ||
50 | int dcnt = 0; | ||
51 | int ncnt = 0; | ||
52 | int mib[6]; | ||
53 | size_t needed; | ||
54 | char *buf; | ||
55 | char *next; | ||
56 | struct ifaddrs *cif = 0; | ||
57 | char *p, *p0; | ||
58 | struct rt_msghdr *rtm; | ||
59 | struct if_msghdr *ifm; | ||
60 | struct ifa_msghdr *ifam; | ||
61 | struct sockaddr_dl *dl; | ||
62 | struct sockaddr *sa; | ||
63 | u_short index = 0; | ||
64 | size_t len, alen; | ||
65 | struct ifaddrs *ifa, *ift; | ||
66 | int i; | ||
67 | char *data; | ||
68 | char *names; | ||
69 | |||
70 | mib[0] = CTL_NET; | ||
71 | mib[1] = PF_ROUTE; | ||
72 | mib[2] = 0; /* protocol */ | ||
73 | mib[3] = 0; /* wildcard address family */ | ||
74 | mib[4] = NET_RT_IFLIST; | ||
75 | mib[5] = 0; /* no flags */ | ||
76 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) | ||
77 | return (-1); | ||
78 | if ((buf = malloc(needed)) == NULL) | ||
79 | return (-1); | ||
80 | if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { | ||
81 | free(buf); | ||
82 | return (-1); | ||
83 | } | ||
84 | |||
85 | for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { | ||
86 | rtm = (struct rt_msghdr *)next; | ||
87 | if (rtm->rtm_version != RTM_VERSION) | ||
88 | continue; | ||
89 | switch (rtm->rtm_type) { | ||
90 | case RTM_IFINFO: | ||
91 | ifm = (struct if_msghdr *)rtm; | ||
92 | if (ifm->ifm_addrs & RTA_IFP) { | ||
93 | index = ifm->ifm_index; | ||
94 | ++icnt; | ||
95 | dl = (struct sockaddr_dl *)(ifm + 1); | ||
96 | dcnt += SA_RLEN((struct sockaddr *)dl) + | ||
97 | ALIGNBYTES; | ||
98 | dcnt += sizeof(ifm->ifm_data); | ||
99 | ncnt += dl->sdl_nlen + 1; | ||
100 | } else | ||
101 | index = 0; | ||
102 | break; | ||
103 | |||
104 | case RTM_NEWADDR: | ||
105 | ifam = (struct ifa_msghdr *)rtm; | ||
106 | if (index && ifam->ifam_index != index) | ||
107 | abort(); /* XXX abort illegal in library */ | ||
108 | |||
109 | #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD) | ||
110 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
111 | break; | ||
112 | p = (char *)(ifam + 1); | ||
113 | ++icnt; | ||
114 | /* Scan to look for length of address */ | ||
115 | alen = 0; | ||
116 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
117 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
118 | == 0) | ||
119 | continue; | ||
120 | sa = (struct sockaddr *)p; | ||
121 | len = SA_RLEN(sa); | ||
122 | if (i == RTAX_IFA) { | ||
123 | alen = len; | ||
124 | break; | ||
125 | } | ||
126 | p += len; | ||
127 | } | ||
128 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
129 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
130 | == 0) | ||
131 | continue; | ||
132 | sa = (struct sockaddr *)p; | ||
133 | len = SA_RLEN(sa); | ||
134 | if (i == RTAX_NETMASK && sa->sa_len == 0) | ||
135 | dcnt += alen; | ||
136 | else | ||
137 | dcnt += len; | ||
138 | p += len; | ||
139 | } | ||
140 | break; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | if (icnt + dcnt + ncnt == 1) { | ||
145 | *pif = NULL; | ||
146 | free(buf); | ||
147 | return (0); | ||
148 | } | ||
149 | data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt); | ||
150 | if (data == NULL) { | ||
151 | free(buf); | ||
152 | return(-1); | ||
153 | } | ||
154 | |||
155 | ifa = (struct ifaddrs *)data; | ||
156 | data += sizeof(struct ifaddrs) * icnt; | ||
157 | names = data + dcnt; | ||
158 | |||
159 | memset(ifa, 0, sizeof(struct ifaddrs) * icnt); | ||
160 | ift = ifa; | ||
161 | |||
162 | index = 0; | ||
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 | dl = (struct sockaddr_dl *)(ifm + 1); | ||
173 | |||
174 | cif = ift; | ||
175 | ift->ifa_name = names; | ||
176 | ift->ifa_flags = (int)ifm->ifm_flags; | ||
177 | memcpy(names, dl->sdl_data, dl->sdl_nlen); | ||
178 | names[dl->sdl_nlen] = 0; | ||
179 | names += dl->sdl_nlen + 1; | ||
180 | |||
181 | ift->ifa_addr = (struct sockaddr *)data; | ||
182 | memcpy(data, dl, | ||
183 | ((struct sockaddr *)dl)->sa_len); | ||
184 | data += SA_RLEN((struct sockaddr *)dl); | ||
185 | |||
186 | /* ifm_data needs to be aligned */ | ||
187 | ift->ifa_data = data = (void *)ALIGN(data); | ||
188 | memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data)); | ||
189 | data += sizeof(ifm->ifm_data); | ||
190 | |||
191 | ift = (ift->ifa_next = ift + 1); | ||
192 | } else | ||
193 | index = 0; | ||
194 | break; | ||
195 | |||
196 | case RTM_NEWADDR: | ||
197 | ifam = (struct ifa_msghdr *)rtm; | ||
198 | if (index && ifam->ifam_index != index) | ||
199 | abort(); /* XXX abort illegal in library */ | ||
200 | |||
201 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
202 | break; | ||
203 | ift->ifa_name = cif->ifa_name; | ||
204 | ift->ifa_flags = cif->ifa_flags; | ||
205 | ift->ifa_data = NULL; | ||
206 | p = (char *)(ifam + 1); | ||
207 | /* Scan to look for length of address */ | ||
208 | alen = 0; | ||
209 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
210 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
211 | == 0) | ||
212 | continue; | ||
213 | sa = (struct sockaddr *)p; | ||
214 | len = SA_RLEN(sa); | ||
215 | if (i == RTAX_IFA) { | ||
216 | alen = len; | ||
217 | break; | ||
218 | } | ||
219 | p += len; | ||
220 | } | ||
221 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
222 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
223 | == 0) | ||
224 | continue; | ||
225 | sa = (struct sockaddr *)p; | ||
226 | len = SA_RLEN(sa); | ||
227 | switch (i) { | ||
228 | case RTAX_IFA: | ||
229 | ift->ifa_addr = (struct sockaddr *)data; | ||
230 | memcpy(data, p, len); | ||
231 | data += len; | ||
232 | break; | ||
233 | |||
234 | case RTAX_NETMASK: | ||
235 | ift->ifa_netmask = | ||
236 | (struct sockaddr *)data; | ||
237 | if (sa->sa_len == 0) { | ||
238 | memset(data, 0, alen); | ||
239 | data += alen; | ||
240 | break; | ||
241 | } | ||
242 | memcpy(data, p, len); | ||
243 | data += len; | ||
244 | break; | ||
245 | |||
246 | case RTAX_BRD: | ||
247 | ift->ifa_broadaddr = | ||
248 | (struct sockaddr *)data; | ||
249 | memcpy(data, p, len); | ||
250 | data += len; | ||
251 | break; | ||
252 | } | ||
253 | p += len; | ||
254 | } | ||
255 | |||
256 | |||
257 | ift = (ift->ifa_next = ift + 1); | ||
258 | break; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | free(buf); | ||
263 | if (--ift >= ifa) { | ||
264 | ift->ifa_next = NULL; | ||
265 | *pif = ifa; | ||
266 | } else { | ||
267 | *pif = NULL; | ||
268 | free(ifa); | ||
269 | } | ||
270 | return (0); | ||
271 | } | ||
272 | |||
273 | void | ||
274 | freeifaddrs(struct ifaddrs *ifp) | ||
275 | { | ||
276 | free(ifp); | ||
277 | } | ||