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