1 | n/a | /* |
---|
2 | n/a | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
---|
3 | n/a | * All rights reserved. |
---|
4 | n/a | * |
---|
5 | n/a | * Redistribution and use in source and binary forms, with or without |
---|
6 | n/a | * modification, are permitted provided that the following conditions |
---|
7 | n/a | * are met: |
---|
8 | n/a | * 1. Redistributions of source code must retain the above copyright |
---|
9 | n/a | * notice, this list of conditions and the following disclaimer. |
---|
10 | n/a | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | n/a | * notice, this list of conditions and the following disclaimer in the |
---|
12 | n/a | * documentation and/or other materials provided with the distribution. |
---|
13 | n/a | * 3. Neither the name of the project nor the names of its contributors |
---|
14 | n/a | * may be used to endorse or promote products derived from this software |
---|
15 | n/a | * without specific prior written permission. |
---|
16 | n/a | * |
---|
17 | n/a | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | n/a | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
---|
21 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | n/a | * SUCH DAMAGE. |
---|
28 | n/a | */ |
---|
29 | n/a | |
---|
30 | n/a | /* |
---|
31 | n/a | * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. |
---|
32 | n/a | * |
---|
33 | n/a | * Issues to be discussed: |
---|
34 | n/a | * - Thread safe-ness must be checked. |
---|
35 | n/a | * - Return values. There are nonstandard return values defined and used |
---|
36 | n/a | * in the source code. This is because RFC2133 is silent about which error |
---|
37 | n/a | * code must be returned for which situation. |
---|
38 | n/a | * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag. |
---|
39 | n/a | */ |
---|
40 | n/a | |
---|
41 | n/a | #if 0 |
---|
42 | n/a | #include <sys/types.h> |
---|
43 | n/a | #include <sys/param.h> |
---|
44 | n/a | #include <sys/sysctl.h> |
---|
45 | n/a | #include <sys/socket.h> |
---|
46 | n/a | #include <netinet/in.h> |
---|
47 | n/a | #include <arpa/inet.h> |
---|
48 | n/a | #include <arpa/nameser.h> |
---|
49 | n/a | #include <netdb.h> |
---|
50 | n/a | #include <resolv.h> |
---|
51 | n/a | #include <string.h> |
---|
52 | n/a | #include <stdlib.h> |
---|
53 | n/a | #include <stddef.h> |
---|
54 | n/a | #include <ctype.h> |
---|
55 | n/a | #include <unistd.h> |
---|
56 | n/a | |
---|
57 | n/a | #include "addrinfo.h" |
---|
58 | n/a | #endif |
---|
59 | n/a | |
---|
60 | n/a | #if defined(__KAME__) && defined(ENABLE_IPV6) |
---|
61 | n/a | # define FAITH |
---|
62 | n/a | #endif |
---|
63 | n/a | |
---|
64 | n/a | #define SUCCESS 0 |
---|
65 | n/a | #define GAI_ANY 0 |
---|
66 | n/a | #define YES 1 |
---|
67 | n/a | #define NO 0 |
---|
68 | n/a | |
---|
69 | n/a | #ifdef FAITH |
---|
70 | n/a | static int translate = NO; |
---|
71 | n/a | static struct in6_addr faith_prefix = IN6ADDR_GAI_ANY_INIT; |
---|
72 | n/a | #endif |
---|
73 | n/a | |
---|
74 | n/a | static const char in_addrany[] = { 0, 0, 0, 0 }; |
---|
75 | n/a | static const char in6_addrany[] = { |
---|
76 | n/a | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
---|
77 | n/a | }; |
---|
78 | n/a | static const char in_loopback[] = { 127, 0, 0, 1 }; |
---|
79 | n/a | static const char in6_loopback[] = { |
---|
80 | n/a | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 |
---|
81 | n/a | }; |
---|
82 | n/a | |
---|
83 | n/a | struct sockinet { |
---|
84 | n/a | u_char si_len; |
---|
85 | n/a | u_char si_family; |
---|
86 | n/a | u_short si_port; |
---|
87 | n/a | }; |
---|
88 | n/a | |
---|
89 | n/a | static struct gai_afd { |
---|
90 | n/a | int a_af; |
---|
91 | n/a | int a_addrlen; |
---|
92 | n/a | int a_socklen; |
---|
93 | n/a | int a_off; |
---|
94 | n/a | const char *a_addrany; |
---|
95 | n/a | const char *a_loopback; |
---|
96 | n/a | } gai_afdl [] = { |
---|
97 | n/a | #ifdef ENABLE_IPV6 |
---|
98 | n/a | #define N_INET6 0 |
---|
99 | n/a | {PF_INET6, sizeof(struct in6_addr), |
---|
100 | n/a | sizeof(struct sockaddr_in6), |
---|
101 | n/a | offsetof(struct sockaddr_in6, sin6_addr), |
---|
102 | n/a | in6_addrany, in6_loopback}, |
---|
103 | n/a | #define N_INET 1 |
---|
104 | n/a | #else |
---|
105 | n/a | #define N_INET 0 |
---|
106 | n/a | #endif |
---|
107 | n/a | {PF_INET, sizeof(struct in_addr), |
---|
108 | n/a | sizeof(struct sockaddr_in), |
---|
109 | n/a | offsetof(struct sockaddr_in, sin_addr), |
---|
110 | n/a | in_addrany, in_loopback}, |
---|
111 | n/a | {0, 0, 0, 0, NULL, NULL}, |
---|
112 | n/a | }; |
---|
113 | n/a | |
---|
114 | n/a | #ifdef ENABLE_IPV6 |
---|
115 | n/a | #define PTON_MAX 16 |
---|
116 | n/a | #else |
---|
117 | n/a | #define PTON_MAX 4 |
---|
118 | n/a | #endif |
---|
119 | n/a | |
---|
120 | n/a | #ifndef IN_MULTICAST |
---|
121 | n/a | #define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U) |
---|
122 | n/a | #endif |
---|
123 | n/a | |
---|
124 | n/a | #ifndef IN_EXPERIMENTAL |
---|
125 | n/a | #define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U) |
---|
126 | n/a | #endif |
---|
127 | n/a | |
---|
128 | n/a | #ifndef IN_LOOPBACKNET |
---|
129 | n/a | #define IN_LOOPBACKNET 127 |
---|
130 | n/a | #endif |
---|
131 | n/a | |
---|
132 | n/a | static int get_name(const char *, struct gai_afd *, |
---|
133 | n/a | struct addrinfo **, char *, struct addrinfo *, |
---|
134 | n/a | int); |
---|
135 | n/a | static int get_addr(const char *, int, struct addrinfo **, |
---|
136 | n/a | struct addrinfo *, int); |
---|
137 | n/a | static int str_isnumber(const char *); |
---|
138 | n/a | |
---|
139 | n/a | static const char * const ai_errlist[] = { |
---|
140 | n/a | "success.", |
---|
141 | n/a | "address family for hostname not supported.", /* EAI_ADDRFAMILY */ |
---|
142 | n/a | "temporary failure in name resolution.", /* EAI_AGAIN */ |
---|
143 | n/a | "invalid value for ai_flags.", /* EAI_BADFLAGS */ |
---|
144 | n/a | "non-recoverable failure in name resolution.", /* EAI_FAIL */ |
---|
145 | n/a | "ai_family not supported.", /* EAI_FAMILY */ |
---|
146 | n/a | "memory allocation failure.", /* EAI_MEMORY */ |
---|
147 | n/a | "no address associated with hostname.", /* EAI_NODATA */ |
---|
148 | n/a | "hostname nor servname provided, or not known.",/* EAI_NONAME */ |
---|
149 | n/a | "servname not supported for ai_socktype.", /* EAI_SERVICE */ |
---|
150 | n/a | "ai_socktype not supported.", /* EAI_SOCKTYPE */ |
---|
151 | n/a | "system error returned in errno.", /* EAI_SYSTEM */ |
---|
152 | n/a | "invalid value for hints.", /* EAI_BADHINTS */ |
---|
153 | n/a | "resolved protocol is unknown.", /* EAI_PROTOCOL */ |
---|
154 | n/a | "unknown error.", /* EAI_MAX */ |
---|
155 | n/a | }; |
---|
156 | n/a | |
---|
157 | n/a | #define GET_CANONNAME(ai, str) \ |
---|
158 | n/a | if (pai->ai_flags & AI_CANONNAME) {\ |
---|
159 | n/a | if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\ |
---|
160 | n/a | strcpy((ai)->ai_canonname, (str));\ |
---|
161 | n/a | } else {\ |
---|
162 | n/a | error = EAI_MEMORY;\ |
---|
163 | n/a | goto free;\ |
---|
164 | n/a | }\ |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | #ifdef HAVE_SOCKADDR_SA_LEN |
---|
168 | n/a | #define GET_AI(ai, gai_afd, addr, port) {\ |
---|
169 | n/a | char *p;\ |
---|
170 | n/a | if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ |
---|
171 | n/a | ((gai_afd)->a_socklen)))\ |
---|
172 | n/a | == NULL) goto free;\ |
---|
173 | n/a | memcpy(ai, pai, sizeof(struct addrinfo));\ |
---|
174 | n/a | (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ |
---|
175 | n/a | memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ |
---|
176 | n/a | (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\ |
---|
177 | n/a | (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ |
---|
178 | n/a | ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ |
---|
179 | n/a | p = (char *)((ai)->ai_addr);\ |
---|
180 | n/a | memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ |
---|
181 | n/a | } |
---|
182 | n/a | #else |
---|
183 | n/a | #define GET_AI(ai, gai_afd, addr, port) {\ |
---|
184 | n/a | char *p;\ |
---|
185 | n/a | if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ |
---|
186 | n/a | ((gai_afd)->a_socklen)))\ |
---|
187 | n/a | == NULL) goto free;\ |
---|
188 | n/a | memcpy(ai, pai, sizeof(struct addrinfo));\ |
---|
189 | n/a | (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ |
---|
190 | n/a | memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ |
---|
191 | n/a | (ai)->ai_addrlen = (gai_afd)->a_socklen;\ |
---|
192 | n/a | (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ |
---|
193 | n/a | ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ |
---|
194 | n/a | p = (char *)((ai)->ai_addr);\ |
---|
195 | n/a | memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ |
---|
196 | n/a | } |
---|
197 | n/a | #endif |
---|
198 | n/a | |
---|
199 | n/a | #define ERR(err) { error = (err); goto bad; } |
---|
200 | n/a | |
---|
201 | n/a | const char * |
---|
202 | n/a | gai_strerror(int ecode) |
---|
203 | n/a | { |
---|
204 | n/a | if (ecode < 0 || ecode > EAI_MAX) |
---|
205 | n/a | ecode = EAI_MAX; |
---|
206 | n/a | return ai_errlist[ecode]; |
---|
207 | n/a | } |
---|
208 | n/a | |
---|
209 | n/a | void |
---|
210 | n/a | freeaddrinfo(struct addrinfo *ai) |
---|
211 | n/a | { |
---|
212 | n/a | struct addrinfo *next; |
---|
213 | n/a | |
---|
214 | n/a | do { |
---|
215 | n/a | next = ai->ai_next; |
---|
216 | n/a | if (ai->ai_canonname) |
---|
217 | n/a | free(ai->ai_canonname); |
---|
218 | n/a | /* no need to free(ai->ai_addr) */ |
---|
219 | n/a | free(ai); |
---|
220 | n/a | } while ((ai = next) != NULL); |
---|
221 | n/a | } |
---|
222 | n/a | |
---|
223 | n/a | static int |
---|
224 | n/a | str_isnumber(const char *p) |
---|
225 | n/a | { |
---|
226 | n/a | unsigned char *q = (unsigned char *)p; |
---|
227 | n/a | while (*q) { |
---|
228 | n/a | if (! isdigit(*q)) |
---|
229 | n/a | return NO; |
---|
230 | n/a | q++; |
---|
231 | n/a | } |
---|
232 | n/a | return YES; |
---|
233 | n/a | } |
---|
234 | n/a | |
---|
235 | n/a | int |
---|
236 | n/a | getaddrinfo(const char*hostname, const char*servname, |
---|
237 | n/a | const struct addrinfo *hints, struct addrinfo **res) |
---|
238 | n/a | { |
---|
239 | n/a | struct addrinfo sentinel; |
---|
240 | n/a | struct addrinfo *top = NULL; |
---|
241 | n/a | struct addrinfo *cur; |
---|
242 | n/a | int i, error = 0; |
---|
243 | n/a | char pton[PTON_MAX]; |
---|
244 | n/a | struct addrinfo ai; |
---|
245 | n/a | struct addrinfo *pai; |
---|
246 | n/a | u_short port; |
---|
247 | n/a | |
---|
248 | n/a | #ifdef FAITH |
---|
249 | n/a | static int firsttime = 1; |
---|
250 | n/a | |
---|
251 | n/a | if (firsttime) { |
---|
252 | n/a | /* translator hack */ |
---|
253 | n/a | { |
---|
254 | n/a | char *q = getenv("GAI"); |
---|
255 | n/a | if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1) |
---|
256 | n/a | translate = YES; |
---|
257 | n/a | } |
---|
258 | n/a | firsttime = 0; |
---|
259 | n/a | } |
---|
260 | n/a | #endif |
---|
261 | n/a | |
---|
262 | n/a | /* initialize file static vars */ |
---|
263 | n/a | sentinel.ai_next = NULL; |
---|
264 | n/a | cur = &sentinel; |
---|
265 | n/a | pai = &ai; |
---|
266 | n/a | pai->ai_flags = 0; |
---|
267 | n/a | pai->ai_family = PF_UNSPEC; |
---|
268 | n/a | pai->ai_socktype = GAI_ANY; |
---|
269 | n/a | pai->ai_protocol = GAI_ANY; |
---|
270 | n/a | pai->ai_addrlen = 0; |
---|
271 | n/a | pai->ai_canonname = NULL; |
---|
272 | n/a | pai->ai_addr = NULL; |
---|
273 | n/a | pai->ai_next = NULL; |
---|
274 | n/a | port = GAI_ANY; |
---|
275 | n/a | |
---|
276 | n/a | if (hostname == NULL && servname == NULL) |
---|
277 | n/a | return EAI_NONAME; |
---|
278 | n/a | if (hints) { |
---|
279 | n/a | /* error check for hints */ |
---|
280 | n/a | if (hints->ai_addrlen || hints->ai_canonname || |
---|
281 | n/a | hints->ai_addr || hints->ai_next) |
---|
282 | n/a | ERR(EAI_BADHINTS); /* xxx */ |
---|
283 | n/a | if (hints->ai_flags & ~AI_MASK) |
---|
284 | n/a | ERR(EAI_BADFLAGS); |
---|
285 | n/a | switch (hints->ai_family) { |
---|
286 | n/a | case PF_UNSPEC: |
---|
287 | n/a | case PF_INET: |
---|
288 | n/a | #ifdef ENABLE_IPV6 |
---|
289 | n/a | case PF_INET6: |
---|
290 | n/a | #endif |
---|
291 | n/a | break; |
---|
292 | n/a | default: |
---|
293 | n/a | ERR(EAI_FAMILY); |
---|
294 | n/a | } |
---|
295 | n/a | memcpy(pai, hints, sizeof(*pai)); |
---|
296 | n/a | switch (pai->ai_socktype) { |
---|
297 | n/a | case GAI_ANY: |
---|
298 | n/a | switch (pai->ai_protocol) { |
---|
299 | n/a | case GAI_ANY: |
---|
300 | n/a | break; |
---|
301 | n/a | case IPPROTO_UDP: |
---|
302 | n/a | pai->ai_socktype = SOCK_DGRAM; |
---|
303 | n/a | break; |
---|
304 | n/a | case IPPROTO_TCP: |
---|
305 | n/a | pai->ai_socktype = SOCK_STREAM; |
---|
306 | n/a | break; |
---|
307 | n/a | default: |
---|
308 | n/a | pai->ai_socktype = SOCK_RAW; |
---|
309 | n/a | break; |
---|
310 | n/a | } |
---|
311 | n/a | break; |
---|
312 | n/a | case SOCK_RAW: |
---|
313 | n/a | break; |
---|
314 | n/a | case SOCK_DGRAM: |
---|
315 | n/a | if (pai->ai_protocol != IPPROTO_UDP && |
---|
316 | n/a | pai->ai_protocol != GAI_ANY) |
---|
317 | n/a | ERR(EAI_BADHINTS); /*xxx*/ |
---|
318 | n/a | pai->ai_protocol = IPPROTO_UDP; |
---|
319 | n/a | break; |
---|
320 | n/a | case SOCK_STREAM: |
---|
321 | n/a | if (pai->ai_protocol != IPPROTO_TCP && |
---|
322 | n/a | pai->ai_protocol != GAI_ANY) |
---|
323 | n/a | ERR(EAI_BADHINTS); /*xxx*/ |
---|
324 | n/a | pai->ai_protocol = IPPROTO_TCP; |
---|
325 | n/a | break; |
---|
326 | n/a | default: |
---|
327 | n/a | ERR(EAI_SOCKTYPE); |
---|
328 | n/a | /* unreachable */ |
---|
329 | n/a | } |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | /* |
---|
333 | n/a | * service port |
---|
334 | n/a | */ |
---|
335 | n/a | if (servname) { |
---|
336 | n/a | if (str_isnumber(servname)) { |
---|
337 | n/a | if (pai->ai_socktype == GAI_ANY) { |
---|
338 | n/a | /* caller accept *GAI_ANY* socktype */ |
---|
339 | n/a | pai->ai_socktype = SOCK_DGRAM; |
---|
340 | n/a | pai->ai_protocol = IPPROTO_UDP; |
---|
341 | n/a | } |
---|
342 | n/a | port = htons((u_short)atoi(servname)); |
---|
343 | n/a | } else { |
---|
344 | n/a | struct servent *sp; |
---|
345 | n/a | char *proto; |
---|
346 | n/a | |
---|
347 | n/a | proto = NULL; |
---|
348 | n/a | switch (pai->ai_socktype) { |
---|
349 | n/a | case GAI_ANY: |
---|
350 | n/a | proto = NULL; |
---|
351 | n/a | break; |
---|
352 | n/a | case SOCK_DGRAM: |
---|
353 | n/a | proto = "udp"; |
---|
354 | n/a | break; |
---|
355 | n/a | case SOCK_STREAM: |
---|
356 | n/a | proto = "tcp"; |
---|
357 | n/a | break; |
---|
358 | n/a | default: |
---|
359 | n/a | fprintf(stderr, "panic!\n"); |
---|
360 | n/a | break; |
---|
361 | n/a | } |
---|
362 | n/a | if ((sp = getservbyname(servname, proto)) == NULL) |
---|
363 | n/a | ERR(EAI_SERVICE); |
---|
364 | n/a | port = sp->s_port; |
---|
365 | n/a | if (pai->ai_socktype == GAI_ANY) { |
---|
366 | n/a | if (strcmp(sp->s_proto, "udp") == 0) { |
---|
367 | n/a | pai->ai_socktype = SOCK_DGRAM; |
---|
368 | n/a | pai->ai_protocol = IPPROTO_UDP; |
---|
369 | n/a | } else if (strcmp(sp->s_proto, "tcp") == 0) { |
---|
370 | n/a | pai->ai_socktype = SOCK_STREAM; |
---|
371 | n/a | pai->ai_protocol = IPPROTO_TCP; |
---|
372 | n/a | } else |
---|
373 | n/a | ERR(EAI_PROTOCOL); /*xxx*/ |
---|
374 | n/a | } |
---|
375 | n/a | } |
---|
376 | n/a | } |
---|
377 | n/a | |
---|
378 | n/a | /* |
---|
379 | n/a | * hostname == NULL. |
---|
380 | n/a | * passive socket -> anyaddr (0.0.0.0 or ::) |
---|
381 | n/a | * non-passive socket -> localhost (127.0.0.1 or ::1) |
---|
382 | n/a | */ |
---|
383 | n/a | if (hostname == NULL) { |
---|
384 | n/a | struct gai_afd *gai_afd; |
---|
385 | n/a | |
---|
386 | n/a | for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) { |
---|
387 | n/a | if (!(pai->ai_family == PF_UNSPEC |
---|
388 | n/a | || pai->ai_family == gai_afd->a_af)) { |
---|
389 | n/a | continue; |
---|
390 | n/a | } |
---|
391 | n/a | |
---|
392 | n/a | if (pai->ai_flags & AI_PASSIVE) { |
---|
393 | n/a | GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port); |
---|
394 | n/a | /* xxx meaningless? |
---|
395 | n/a | * GET_CANONNAME(cur->ai_next, "anyaddr"); |
---|
396 | n/a | */ |
---|
397 | n/a | } else { |
---|
398 | n/a | GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback, |
---|
399 | n/a | port); |
---|
400 | n/a | /* xxx meaningless? |
---|
401 | n/a | * GET_CANONNAME(cur->ai_next, "localhost"); |
---|
402 | n/a | */ |
---|
403 | n/a | } |
---|
404 | n/a | cur = cur->ai_next; |
---|
405 | n/a | } |
---|
406 | n/a | top = sentinel.ai_next; |
---|
407 | n/a | if (top) |
---|
408 | n/a | goto good; |
---|
409 | n/a | else |
---|
410 | n/a | ERR(EAI_FAMILY); |
---|
411 | n/a | } |
---|
412 | n/a | |
---|
413 | n/a | /* hostname as numeric name */ |
---|
414 | n/a | for (i = 0; gai_afdl[i].a_af; i++) { |
---|
415 | n/a | if (inet_pton(gai_afdl[i].a_af, hostname, pton)) { |
---|
416 | n/a | u_long v4a; |
---|
417 | n/a | #ifdef ENABLE_IPV6 |
---|
418 | n/a | u_char pfx; |
---|
419 | n/a | #endif |
---|
420 | n/a | |
---|
421 | n/a | switch (gai_afdl[i].a_af) { |
---|
422 | n/a | case AF_INET: |
---|
423 | n/a | v4a = ((struct in_addr *)pton)->s_addr; |
---|
424 | n/a | v4a = ntohl(v4a); |
---|
425 | n/a | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) |
---|
426 | n/a | pai->ai_flags &= ~AI_CANONNAME; |
---|
427 | n/a | v4a >>= IN_CLASSA_NSHIFT; |
---|
428 | n/a | if (v4a == 0 || v4a == IN_LOOPBACKNET) |
---|
429 | n/a | pai->ai_flags &= ~AI_CANONNAME; |
---|
430 | n/a | break; |
---|
431 | n/a | #ifdef ENABLE_IPV6 |
---|
432 | n/a | case AF_INET6: |
---|
433 | n/a | pfx = ((struct in6_addr *)pton)->s6_addr[0]; |
---|
434 | n/a | if (pfx == 0 || pfx == 0xfe || pfx == 0xff) |
---|
435 | n/a | pai->ai_flags &= ~AI_CANONNAME; |
---|
436 | n/a | break; |
---|
437 | n/a | #endif |
---|
438 | n/a | } |
---|
439 | n/a | |
---|
440 | n/a | if (pai->ai_family == gai_afdl[i].a_af || |
---|
441 | n/a | pai->ai_family == PF_UNSPEC) { |
---|
442 | n/a | if (! (pai->ai_flags & AI_CANONNAME)) { |
---|
443 | n/a | GET_AI(top, &gai_afdl[i], pton, port); |
---|
444 | n/a | goto good; |
---|
445 | n/a | } |
---|
446 | n/a | /* |
---|
447 | n/a | * if AI_CANONNAME and if reverse lookup |
---|
448 | n/a | * fail, return ai anyway to pacify |
---|
449 | n/a | * calling application. |
---|
450 | n/a | * |
---|
451 | n/a | * XXX getaddrinfo() is a name->address |
---|
452 | n/a | * translation function, and it looks strange |
---|
453 | n/a | * that we do addr->name translation here. |
---|
454 | n/a | */ |
---|
455 | n/a | get_name(pton, &gai_afdl[i], &top, pton, pai, port); |
---|
456 | n/a | goto good; |
---|
457 | n/a | } else |
---|
458 | n/a | ERR(EAI_FAMILY); /*xxx*/ |
---|
459 | n/a | } |
---|
460 | n/a | } |
---|
461 | n/a | |
---|
462 | n/a | if (pai->ai_flags & AI_NUMERICHOST) |
---|
463 | n/a | ERR(EAI_NONAME); |
---|
464 | n/a | |
---|
465 | n/a | /* hostname as alphabetical name */ |
---|
466 | n/a | error = get_addr(hostname, pai->ai_family, &top, pai, port); |
---|
467 | n/a | if (error == 0) { |
---|
468 | n/a | if (top) { |
---|
469 | n/a | good: |
---|
470 | n/a | *res = top; |
---|
471 | n/a | return SUCCESS; |
---|
472 | n/a | } else |
---|
473 | n/a | error = EAI_FAIL; |
---|
474 | n/a | } |
---|
475 | n/a | free: |
---|
476 | n/a | if (top) |
---|
477 | n/a | freeaddrinfo(top); |
---|
478 | n/a | bad: |
---|
479 | n/a | *res = NULL; |
---|
480 | n/a | return error; |
---|
481 | n/a | } |
---|
482 | n/a | |
---|
483 | n/a | static int |
---|
484 | n/a | get_name(addr, gai_afd, res, numaddr, pai, port0) |
---|
485 | n/a | const char *addr; |
---|
486 | n/a | struct gai_afd *gai_afd; |
---|
487 | n/a | struct addrinfo **res; |
---|
488 | n/a | char *numaddr; |
---|
489 | n/a | struct addrinfo *pai; |
---|
490 | n/a | int port0; |
---|
491 | n/a | { |
---|
492 | n/a | u_short port = port0 & 0xffff; |
---|
493 | n/a | struct hostent *hp; |
---|
494 | n/a | struct addrinfo *cur; |
---|
495 | n/a | int error = 0; |
---|
496 | n/a | #ifdef ENABLE_IPV6 |
---|
497 | n/a | int h_error; |
---|
498 | n/a | #endif |
---|
499 | n/a | |
---|
500 | n/a | #ifdef ENABLE_IPV6 |
---|
501 | n/a | hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error); |
---|
502 | n/a | #else |
---|
503 | n/a | hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET); |
---|
504 | n/a | #endif |
---|
505 | n/a | if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { |
---|
506 | n/a | GET_AI(cur, gai_afd, hp->h_addr_list[0], port); |
---|
507 | n/a | GET_CANONNAME(cur, hp->h_name); |
---|
508 | n/a | } else |
---|
509 | n/a | GET_AI(cur, gai_afd, numaddr, port); |
---|
510 | n/a | |
---|
511 | n/a | #ifdef ENABLE_IPV6 |
---|
512 | n/a | if (hp) |
---|
513 | n/a | freehostent(hp); |
---|
514 | n/a | #endif |
---|
515 | n/a | *res = cur; |
---|
516 | n/a | return SUCCESS; |
---|
517 | n/a | free: |
---|
518 | n/a | if (cur) |
---|
519 | n/a | freeaddrinfo(cur); |
---|
520 | n/a | #ifdef ENABLE_IPV6 |
---|
521 | n/a | if (hp) |
---|
522 | n/a | freehostent(hp); |
---|
523 | n/a | #endif |
---|
524 | n/a | /* bad: */ |
---|
525 | n/a | *res = NULL; |
---|
526 | n/a | return error; |
---|
527 | n/a | } |
---|
528 | n/a | |
---|
529 | n/a | static int |
---|
530 | n/a | get_addr(hostname, af, res, pai, port0) |
---|
531 | n/a | const char *hostname; |
---|
532 | n/a | int af; |
---|
533 | n/a | struct addrinfo **res; |
---|
534 | n/a | struct addrinfo *pai; |
---|
535 | n/a | int port0; |
---|
536 | n/a | { |
---|
537 | n/a | u_short port = port0 & 0xffff; |
---|
538 | n/a | struct addrinfo sentinel; |
---|
539 | n/a | struct hostent *hp; |
---|
540 | n/a | struct addrinfo *top, *cur; |
---|
541 | n/a | struct gai_afd *gai_afd; |
---|
542 | n/a | int i, error = 0, h_error; |
---|
543 | n/a | char *ap; |
---|
544 | n/a | |
---|
545 | n/a | top = NULL; |
---|
546 | n/a | sentinel.ai_next = NULL; |
---|
547 | n/a | cur = &sentinel; |
---|
548 | n/a | #ifdef ENABLE_IPV6 |
---|
549 | n/a | if (af == AF_UNSPEC) { |
---|
550 | n/a | hp = getipnodebyname(hostname, AF_INET6, |
---|
551 | n/a | AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error); |
---|
552 | n/a | } else |
---|
553 | n/a | hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error); |
---|
554 | n/a | #else |
---|
555 | n/a | hp = gethostbyname(hostname); |
---|
556 | n/a | h_error = h_errno; |
---|
557 | n/a | #endif |
---|
558 | n/a | if (hp == NULL) { |
---|
559 | n/a | switch (h_error) { |
---|
560 | n/a | case HOST_NOT_FOUND: |
---|
561 | n/a | case NO_DATA: |
---|
562 | n/a | error = EAI_NODATA; |
---|
563 | n/a | break; |
---|
564 | n/a | case TRY_AGAIN: |
---|
565 | n/a | error = EAI_AGAIN; |
---|
566 | n/a | break; |
---|
567 | n/a | case NO_RECOVERY: |
---|
568 | n/a | default: |
---|
569 | n/a | error = EAI_FAIL; |
---|
570 | n/a | break; |
---|
571 | n/a | } |
---|
572 | n/a | goto free; |
---|
573 | n/a | } |
---|
574 | n/a | |
---|
575 | n/a | if ((hp->h_name == NULL) || (hp->h_name[0] == 0) || |
---|
576 | n/a | (hp->h_addr_list[0] == NULL)) { |
---|
577 | n/a | error = EAI_FAIL; |
---|
578 | n/a | goto free; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) { |
---|
582 | n/a | switch (af) { |
---|
583 | n/a | #ifdef ENABLE_IPV6 |
---|
584 | n/a | case AF_INET6: |
---|
585 | n/a | gai_afd = &gai_afdl[N_INET6]; |
---|
586 | n/a | break; |
---|
587 | n/a | #endif |
---|
588 | n/a | #ifndef ENABLE_IPV6 |
---|
589 | n/a | default: /* AF_UNSPEC */ |
---|
590 | n/a | #endif |
---|
591 | n/a | case AF_INET: |
---|
592 | n/a | gai_afd = &gai_afdl[N_INET]; |
---|
593 | n/a | break; |
---|
594 | n/a | #ifdef ENABLE_IPV6 |
---|
595 | n/a | default: /* AF_UNSPEC */ |
---|
596 | n/a | if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { |
---|
597 | n/a | ap += sizeof(struct in6_addr) - |
---|
598 | n/a | sizeof(struct in_addr); |
---|
599 | n/a | gai_afd = &gai_afdl[N_INET]; |
---|
600 | n/a | } else |
---|
601 | n/a | gai_afd = &gai_afdl[N_INET6]; |
---|
602 | n/a | break; |
---|
603 | n/a | #endif |
---|
604 | n/a | } |
---|
605 | n/a | #ifdef FAITH |
---|
606 | n/a | if (translate && gai_afd->a_af == AF_INET) { |
---|
607 | n/a | struct in6_addr *in6; |
---|
608 | n/a | |
---|
609 | n/a | GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port); |
---|
610 | n/a | in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr; |
---|
611 | n/a | memcpy(&in6->s6_addr32[0], &faith_prefix, |
---|
612 | n/a | sizeof(struct in6_addr) - sizeof(struct in_addr)); |
---|
613 | n/a | memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr)); |
---|
614 | n/a | } else |
---|
615 | n/a | #endif /* FAITH */ |
---|
616 | n/a | GET_AI(cur->ai_next, gai_afd, ap, port); |
---|
617 | n/a | if (cur == &sentinel) { |
---|
618 | n/a | top = cur->ai_next; |
---|
619 | n/a | GET_CANONNAME(top, hp->h_name); |
---|
620 | n/a | } |
---|
621 | n/a | cur = cur->ai_next; |
---|
622 | n/a | } |
---|
623 | n/a | #ifdef ENABLE_IPV6 |
---|
624 | n/a | freehostent(hp); |
---|
625 | n/a | #endif |
---|
626 | n/a | *res = top; |
---|
627 | n/a | return SUCCESS; |
---|
628 | n/a | free: |
---|
629 | n/a | if (top) |
---|
630 | n/a | freeaddrinfo(top); |
---|
631 | n/a | #ifdef ENABLE_IPV6 |
---|
632 | n/a | if (hp) |
---|
633 | n/a | freehostent(hp); |
---|
634 | n/a | #endif |
---|
635 | n/a | /* bad: */ |
---|
636 | n/a | *res = NULL; |
---|
637 | n/a | return error; |
---|
638 | n/a | } |
---|