xref: /freebsd/lib/libc/net/getaddrinfo.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*	$KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * 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  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
34  *
35  * Issues to be discussed:
36  * - Thread safe-ness must be checked.
37  * - Return values.  There are nonstandard return values defined and used
38  *   in the source code.  This is because RFC2553 is silent about which error
39  *   code must be returned for which situation.
40  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
41  *   invalid.  current code - SEGV on freeaddrinfo(NULL)
42  *
43  * Note:
44  * - The code filters out AFs that are not supported by the kernel,
45  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
46  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
47  *   in ai_flags?
48  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
49  *   (1) what should we do against numeric hostname (2) what should we do
50  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
51  *   non-loopback address configured?  global address configured?
52  *
53  * OS specific notes for netbsd/openbsd/freebsd4/bsdi4:
54  * - To avoid search order issue, we have a big amount of code duplicate
55  *   from gethnamaddr.c and some other places.  The issues that there's no
56  *   lower layer function to lookup "IPv4 or IPv6" record.  Calling
57  *   gethostbyname2 from getaddrinfo will end up in wrong search order, as
58  *   presented above.
59  *
60  * OS specific notes for freebsd4:
61  * - FreeBSD supported $GAI.  The code does not.
62  * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not.
63  */
64 
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67 
68 #include "namespace.h"
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/socket.h>
72 #include <net/if.h>
73 #include <netinet/in.h>
74 #include <sys/queue.h>
75 #ifdef INET6
76 #include <net/if_var.h>
77 #include <sys/sysctl.h>
78 #include <sys/ioctl.h>
79 #include <netinet6/in6_var.h>	/* XXX */
80 #endif
81 #include <arpa/inet.h>
82 #include <arpa/nameser.h>
83 #include <rpc/rpc.h>
84 #include <rpcsvc/yp_prot.h>
85 #include <rpcsvc/ypclnt.h>
86 #include <netdb.h>
87 #include <pthread.h>
88 #include <resolv.h>
89 #include <string.h>
90 #include <stdlib.h>
91 #include <stddef.h>
92 #include <ctype.h>
93 #include <unistd.h>
94 #include <stdio.h>
95 #include <errno.h>
96 
97 #include "res_config.h"
98 
99 #ifdef DEBUG
100 #include <syslog.h>
101 #endif
102 
103 #include <stdarg.h>
104 #include <nsswitch.h>
105 #include "un-namespace.h"
106 #include "libc_private.h"
107 
108 #if defined(__KAME__) && defined(INET6)
109 # define FAITH
110 #endif
111 
112 #define SUCCESS 0
113 #define ANY 0
114 #define YES 1
115 #define NO  0
116 
117 static const char in_addrany[] = { 0, 0, 0, 0 };
118 static const char in_loopback[] = { 127, 0, 0, 1 };
119 #ifdef INET6
120 static const char in6_addrany[] = {
121 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
122 };
123 static const char in6_loopback[] = {
124 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
125 };
126 #endif
127 
128 struct policyqueue {
129 	TAILQ_ENTRY(policyqueue) pc_entry;
130 #ifdef INET6
131 	struct in6_addrpolicy pc_policy;
132 #endif
133 };
134 TAILQ_HEAD(policyhead, policyqueue);
135 
136 static const struct afd {
137 	int a_af;
138 	int a_addrlen;
139 	int a_socklen;
140 	int a_off;
141 	const char *a_addrany;
142 	const char *a_loopback;
143 	int a_scoped;
144 } afdl [] = {
145 #ifdef INET6
146 #define	N_INET6 0
147 	{PF_INET6, sizeof(struct in6_addr),
148 	 sizeof(struct sockaddr_in6),
149 	 offsetof(struct sockaddr_in6, sin6_addr),
150 	 in6_addrany, in6_loopback, 1},
151 #define	N_INET 1
152 #else
153 #define	N_INET 0
154 #endif
155 	{PF_INET, sizeof(struct in_addr),
156 	 sizeof(struct sockaddr_in),
157 	 offsetof(struct sockaddr_in, sin_addr),
158 	 in_addrany, in_loopback, 0},
159 	{0, 0, 0, 0, NULL, NULL, 0},
160 };
161 
162 struct explore {
163 	int e_af;
164 	int e_socktype;
165 	int e_protocol;
166 	const char *e_protostr;
167 	int e_wild;
168 #define WILD_AF(ex)		((ex)->e_wild & 0x01)
169 #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
170 #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
171 };
172 
173 static const struct explore explore[] = {
174 #if 0
175 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
176 #endif
177 #ifdef INET6
178 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
179 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
180 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
181 #endif
182 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
183 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
184 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
185 	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
186 	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
187 	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
188 	{ -1, 0, 0, NULL, 0 },
189 };
190 
191 #ifdef INET6
192 #define PTON_MAX	16
193 #else
194 #define PTON_MAX	4
195 #endif
196 
197 #define AIO_SRCFLAG_DEPRECATED	0x1
198 
199 struct ai_order {
200 	union {
201 		struct sockaddr_storage aiou_ss;
202 		struct sockaddr aiou_sa;
203 	} aio_src_un;
204 #define aio_srcsa aio_src_un.aiou_sa
205 	u_int32_t aio_srcflag;
206 	int aio_srcscope;
207 	int aio_dstscope;
208 	struct policyqueue *aio_srcpolicy;
209 	struct policyqueue *aio_dstpolicy;
210 	struct addrinfo *aio_ai;
211 	int aio_matchlen;
212 };
213 
214 static const ns_src default_dns_files[] = {
215 	{ NSSRC_FILES, 	NS_SUCCESS },
216 	{ NSSRC_DNS, 	NS_SUCCESS },
217 	{ 0 }
218 };
219 
220 struct res_target {
221 	struct res_target *next;
222 	const char *name;	/* domain name */
223 	int qclass, qtype;	/* class and type of query */
224 	u_char *answer;		/* buffer to put answer */
225 	int anslen;		/* size of answer buffer */
226 	int n;			/* result length */
227 };
228 
229 #define MAXPACKET	(64*1024)
230 
231 typedef union {
232 	HEADER hdr;
233 	u_char buf[MAXPACKET];
234 } querybuf;
235 
236 static int str2number(const char *);
237 static int explore_null(const struct addrinfo *,
238 	const char *, struct addrinfo **);
239 static int explore_numeric(const struct addrinfo *, const char *,
240 	const char *, struct addrinfo **, const char *);
241 static int explore_numeric_scope(const struct addrinfo *, const char *,
242 	const char *, struct addrinfo **);
243 static int get_canonname(const struct addrinfo *,
244 	struct addrinfo *, const char *);
245 static struct addrinfo *get_ai(const struct addrinfo *,
246 	const struct afd *, const char *);
247 static int get_portmatch(const struct addrinfo *, const char *);
248 static int get_port(struct addrinfo *, const char *, int);
249 static const struct afd *find_afd(int);
250 static int addrconfig(struct addrinfo *);
251 static void set_source(struct ai_order *, struct policyhead *);
252 static int comp_dst(const void *, const void *);
253 #ifdef INET6
254 static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
255 #endif
256 static int gai_addr2scopetype(struct sockaddr *);
257 
258 static int explore_fqdn(const struct addrinfo *, const char *,
259 	const char *, struct addrinfo **);
260 
261 static int reorder(struct addrinfo *);
262 static int get_addrselectpolicy(struct policyhead *);
263 static void free_addrselectpolicy(struct policyhead *);
264 static struct policyqueue *match_addrselectpolicy(struct sockaddr *,
265 	struct policyhead *);
266 static int matchlen(struct sockaddr *, struct sockaddr *);
267 
268 static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
269 	const struct addrinfo *);
270 #if defined(RESOLVSORT)
271 static int addr4sort(struct addrinfo *);
272 #endif
273 static int _dns_getaddrinfo(void *, void *, va_list);
274 static void _sethtent(void);
275 static void _endhtent(void);
276 static struct addrinfo *_gethtent(const char *, const struct addrinfo *);
277 static int _files_getaddrinfo(void *, void *, va_list);
278 #ifdef YP
279 static struct addrinfo *_yphostent(char *, const struct addrinfo *);
280 static int _yp_getaddrinfo(void *, void *, va_list);
281 #endif
282 
283 static int res_queryN(const char *, struct res_target *);
284 static int res_searchN(const char *, struct res_target *);
285 static int res_querydomainN(const char *, const char *,
286 	struct res_target *);
287 
288 static struct ai_errlist {
289 	const char *str;
290 	int code;
291 } ai_errlist[] = {
292 	{ "Success",					0, },
293 	{ "Temporary failure in name resolution",	EAI_AGAIN, },
294 	{ "Invalid value for ai_flags",		       	EAI_BADFLAGS, },
295 	{ "Non-recoverable failure in name resolution", EAI_FAIL, },
296 	{ "ai_family not supported",			EAI_FAMILY, },
297 	{ "Memory allocation failure", 			EAI_MEMORY, },
298 	{ "hostname nor servname provided, or not known", EAI_NONAME, },
299 	{ "servname not supported for ai_socktype",	EAI_SERVICE, },
300 	{ "ai_socktype not supported", 			EAI_SOCKTYPE, },
301 	{ "System error returned in errno", 		EAI_SYSTEM, },
302 	{ "Invalid value for hints",			EAI_BADHINTS, },
303 	{ "Resolved protocol is unknown",		EAI_PROTOCOL, },
304 	/* backward compatibility with userland code prior to 2553bis-02 */
305 	{ "Address family for hostname not supported",	1, },
306 	{ "No address associated with hostname", 	7, },
307 	{ NULL,						-1, },
308 };
309 
310 /*
311  * XXX: Many dependencies are not thread-safe.  So, we share lock between
312  * getaddrinfo() and getipnodeby*().  Still, we cannot use
313  * getaddrinfo() and getipnodeby*() in conjunction with other
314  * functions which call them.
315  */
316 pthread_mutex_t __getaddrinfo_thread_lock = PTHREAD_MUTEX_INITIALIZER;
317 #define THREAD_LOCK() \
318 	if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock);
319 #define THREAD_UNLOCK() \
320 	if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock);
321 
322 /* XXX macros that make external reference is BAD. */
323 
324 #define GET_AI(ai, afd, addr) \
325 do { \
326 	/* external reference: pai, error, and label free */ \
327 	(ai) = get_ai(pai, (afd), (addr)); \
328 	if ((ai) == NULL) { \
329 		error = EAI_MEMORY; \
330 		goto free; \
331 	} \
332 } while (/*CONSTCOND*/0)
333 
334 #define GET_PORT(ai, serv) \
335 do { \
336 	/* external reference: error and label free */ \
337 	error = get_port((ai), (serv), 0); \
338 	if (error != 0) \
339 		goto free; \
340 } while (/*CONSTCOND*/0)
341 
342 #define GET_CANONNAME(ai, str) \
343 do { \
344 	/* external reference: pai, error and label free */ \
345 	error = get_canonname(pai, (ai), (str)); \
346 	if (error != 0) \
347 		goto free; \
348 } while (/*CONSTCOND*/0)
349 
350 #define ERR(err) \
351 do { \
352 	/* external reference: error, and label bad */ \
353 	error = (err); \
354 	goto bad; \
355 	/*NOTREACHED*/ \
356 } while (/*CONSTCOND*/0)
357 
358 #define MATCH_FAMILY(x, y, w) \
359 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
360 #define MATCH(x, y, w) \
361 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
362 
363 char *
364 gai_strerror(ecode)
365 	int ecode;
366 {
367 	struct ai_errlist *p;
368 
369 	for (p = ai_errlist; p->str; p++) {
370 		if (p->code == ecode)
371 			return (char *)p->str;
372 	}
373 	return "Unknown error";
374 }
375 
376 void
377 freeaddrinfo(ai)
378 	struct addrinfo *ai;
379 {
380 	struct addrinfo *next;
381 
382 	do {
383 		next = ai->ai_next;
384 		if (ai->ai_canonname)
385 			free(ai->ai_canonname);
386 		/* no need to free(ai->ai_addr) */
387 		free(ai);
388 		ai = next;
389 	} while (ai);
390 }
391 
392 static int
393 str2number(p)
394 	const char *p;
395 {
396 	char *ep;
397 	unsigned long v;
398 
399 	if (*p == '\0')
400 		return -1;
401 	ep = NULL;
402 	errno = 0;
403 	v = strtoul(p, &ep, 10);
404 	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
405 		return v;
406 	else
407 		return -1;
408 }
409 
410 int
411 getaddrinfo(hostname, servname, hints, res)
412 	const char *hostname, *servname;
413 	const struct addrinfo *hints;
414 	struct addrinfo **res;
415 {
416 	struct addrinfo sentinel;
417 	struct addrinfo *cur;
418 	int error = 0;
419 	struct addrinfo ai;
420 	struct addrinfo ai0;
421 	struct addrinfo *pai;
422 	const struct explore *ex;
423 	int numeric = 0;
424 
425 	memset(&sentinel, 0, sizeof(sentinel));
426 	cur = &sentinel;
427 	pai = &ai;
428 	pai->ai_flags = 0;
429 	pai->ai_family = PF_UNSPEC;
430 	pai->ai_socktype = ANY;
431 	pai->ai_protocol = ANY;
432 	pai->ai_addrlen = 0;
433 	pai->ai_canonname = NULL;
434 	pai->ai_addr = NULL;
435 	pai->ai_next = NULL;
436 
437 	if (hostname == NULL && servname == NULL)
438 		return EAI_NONAME;
439 	if (hints) {
440 		/* error check for hints */
441 		if (hints->ai_addrlen || hints->ai_canonname ||
442 		    hints->ai_addr || hints->ai_next)
443 			ERR(EAI_BADHINTS); /* xxx */
444 		if (hints->ai_flags & ~AI_MASK)
445 			ERR(EAI_BADFLAGS);
446 		switch (hints->ai_family) {
447 		case PF_UNSPEC:
448 		case PF_INET:
449 #ifdef INET6
450 		case PF_INET6:
451 #endif
452 			break;
453 		default:
454 			ERR(EAI_FAMILY);
455 		}
456 		memcpy(pai, hints, sizeof(*pai));
457 
458 		/*
459 		 * if both socktype/protocol are specified, check if they
460 		 * are meaningful combination.
461 		 */
462 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
463 			for (ex = explore; ex->e_af >= 0; ex++) {
464 				if (pai->ai_family != ex->e_af)
465 					continue;
466 				if (ex->e_socktype == ANY)
467 					continue;
468 				if (ex->e_protocol == ANY)
469 					continue;
470 				if (pai->ai_socktype == ex->e_socktype &&
471 				    pai->ai_protocol != ex->e_protocol) {
472 					ERR(EAI_BADHINTS);
473 				}
474 			}
475 		}
476 	}
477 
478 	/*
479 	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
480 	 * AF_INET6 query.  They need to be ignored if specified in other
481 	 * occassions.
482 	 */
483 	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
484 	case AI_V4MAPPED:
485 	case AI_ALL | AI_V4MAPPED:
486 		if (pai->ai_family != AF_INET6)
487 			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
488 		break;
489 	case AI_ALL:
490 #if 1
491 		/* illegal */
492 		ERR(EAI_BADFLAGS);
493 #else
494 		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
495 #endif
496 		break;
497 	}
498 
499 	/*
500 	 * check for special cases.  (1) numeric servname is disallowed if
501 	 * socktype/protocol are left unspecified. (2) servname is disallowed
502 	 * for raw and other inet{,6} sockets.
503 	 */
504 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
505 #ifdef PF_INET6
506 	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
507 #endif
508 	    ) {
509 		ai0 = *pai;	/* backup *pai */
510 
511 		if (pai->ai_family == PF_UNSPEC) {
512 #ifdef PF_INET6
513 			pai->ai_family = PF_INET6;
514 #else
515 			pai->ai_family = PF_INET;
516 #endif
517 		}
518 		error = get_portmatch(pai, servname);
519 		if (error)
520 			ERR(error);
521 
522 		*pai = ai0;
523 	}
524 
525 	ai0 = *pai;
526 
527 	/* NULL hostname, or numeric hostname */
528 	for (ex = explore; ex->e_af >= 0; ex++) {
529 		*pai = ai0;
530 
531 		/* PF_UNSPEC entries are prepared for DNS queries only */
532 		if (ex->e_af == PF_UNSPEC)
533 			continue;
534 
535 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
536 			continue;
537 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
538 			continue;
539 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
540 			continue;
541 
542 		if (pai->ai_family == PF_UNSPEC)
543 			pai->ai_family = ex->e_af;
544 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
545 			pai->ai_socktype = ex->e_socktype;
546 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
547 			pai->ai_protocol = ex->e_protocol;
548 
549 		if (hostname == NULL)
550 			error = explore_null(pai, servname, &cur->ai_next);
551 		else
552 			error = explore_numeric_scope(pai, hostname, servname,
553 			    &cur->ai_next);
554 
555 		if (error)
556 			goto free;
557 
558 		while (cur && cur->ai_next)
559 			cur = cur->ai_next;
560 	}
561 
562 	/*
563 	 * XXX
564 	 * If numreic representation of AF1 can be interpreted as FQDN
565 	 * representation of AF2, we need to think again about the code below.
566 	 */
567 	if (sentinel.ai_next) {
568 		numeric = 1;
569 		goto good;
570 	}
571 
572 	if (hostname == NULL)
573 		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
574 	if (pai->ai_flags & AI_NUMERICHOST)
575 		ERR(EAI_NONAME);
576 
577 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
578 		ERR(EAI_FAIL);
579 
580 	/*
581 	 * hostname as alphabetical name.
582 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
583 	 * outer loop by AFs.
584 	 */
585 	for (ex = explore; ex->e_af >= 0; ex++) {
586 		*pai = ai0;
587 
588 		/* require exact match for family field */
589 		if (pai->ai_family != ex->e_af)
590 			continue;
591 
592 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
593 				WILD_SOCKTYPE(ex))) {
594 			continue;
595 		}
596 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
597 				WILD_PROTOCOL(ex))) {
598 			continue;
599 		}
600 
601 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
602 			pai->ai_socktype = ex->e_socktype;
603 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
604 			pai->ai_protocol = ex->e_protocol;
605 
606 		error = explore_fqdn(pai, hostname, servname,
607 			&cur->ai_next);
608 
609 		while (cur && cur->ai_next)
610 			cur = cur->ai_next;
611 	}
612 
613 	/* XXX inhibit errors if we have the result */
614 	if (sentinel.ai_next)
615 		error = 0;
616 
617 good:
618 	/*
619 	 * ensure we return either:
620 	 * - error == 0, non-NULL *res
621 	 * - error != 0, NULL *res
622 	 */
623 	if (error == 0) {
624 		if (sentinel.ai_next) {
625 			/*
626 			 * If the returned entry is for an active connection,
627 			 * and the given name is not numeric, reorder the
628 			 * list, so that the application would try the list
629 			 * in the most efficient order.
630 			 */
631 			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
632 				if (!numeric)
633 					(void)reorder(&sentinel);
634 			}
635 			*res = sentinel.ai_next;
636 			return SUCCESS;
637 		} else
638 			error = EAI_FAIL;
639 	}
640 free:
641 bad:
642 	if (sentinel.ai_next)
643 		freeaddrinfo(sentinel.ai_next);
644 	*res = NULL;
645 	return error;
646 }
647 
648 static int
649 reorder(sentinel)
650 	struct addrinfo *sentinel;
651 {
652 	struct addrinfo *ai, **aip;
653 	struct ai_order *aio;
654 	int i, n;
655 	struct policyhead policyhead;
656 
657 	/* count the number of addrinfo elements for sorting. */
658 	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
659 		;
660 
661 	/*
662 	 * If the number is small enough, we can skip the reordering process.
663 	 */
664 	if (n <= 1)
665 		return(n);
666 
667 	/* allocate a temporary array for sort and initialization of it. */
668 	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
669 		return(n);	/* give up reordering */
670 	memset(aio, 0, sizeof(*aio) * n);
671 
672 	/* retrieve address selection policy from the kernel */
673 	TAILQ_INIT(&policyhead);
674 	if (!get_addrselectpolicy(&policyhead)) {
675 		/* no policy is installed into kernel, we don't sort. */
676 		free(aio);
677 		return (n);
678 	}
679 
680 	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
681 		aio[i].aio_ai = ai;
682 		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
683 		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
684 							      &policyhead);
685 		set_source(&aio[i], &policyhead);
686 	}
687 
688 	/* perform sorting. */
689 	qsort(aio, n, sizeof(*aio), comp_dst);
690 
691 	/* reorder the addrinfo chain. */
692 	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
693 		*aip = aio[i].aio_ai;
694 		aip = &aio[i].aio_ai->ai_next;
695 	}
696 	*aip = NULL;
697 
698 	/* cleanup and return */
699 	free(aio);
700 	free_addrselectpolicy(&policyhead);
701 	return(n);
702 }
703 
704 static int
705 get_addrselectpolicy(head)
706 	struct policyhead *head;
707 {
708 #ifdef INET6
709 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
710 	size_t l;
711 	char *buf;
712 	struct in6_addrpolicy *pol, *ep;
713 
714 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
715 		return (0);
716 	if ((buf = malloc(l)) == NULL)
717 		return (0);
718 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
719 		free(buf);
720 		return (0);
721 	}
722 
723 	ep = (struct in6_addrpolicy *)(buf + l);
724 	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
725 		struct policyqueue *new;
726 
727 		if ((new = malloc(sizeof(*new))) == NULL) {
728 			free_addrselectpolicy(head); /* make the list empty */
729 			break;
730 		}
731 		new->pc_policy = *pol;
732 		TAILQ_INSERT_TAIL(head, new, pc_entry);
733 	}
734 
735 	free(buf);
736 	return (1);
737 #else
738 	return (0);
739 #endif
740 }
741 
742 static void
743 free_addrselectpolicy(head)
744 	struct policyhead *head;
745 {
746 	struct policyqueue *ent, *nent;
747 
748 	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
749 		nent = TAILQ_NEXT(ent, pc_entry);
750 		TAILQ_REMOVE(head, ent, pc_entry);
751 		free(ent);
752 	}
753 }
754 
755 static struct policyqueue *
756 match_addrselectpolicy(addr, head)
757 	struct sockaddr *addr;
758 	struct policyhead *head;
759 {
760 #ifdef INET6
761 	struct policyqueue *ent, *bestent = NULL;
762 	struct in6_addrpolicy *pol;
763 	int matchlen, bestmatchlen = -1;
764 	u_char *mp, *ep, *k, *p, m;
765 	struct sockaddr_in6 key;
766 
767 	switch(addr->sa_family) {
768 	case AF_INET6:
769 		key = *(struct sockaddr_in6 *)addr;
770 		break;
771 	case AF_INET:
772 		/* convert the address into IPv4-mapped IPv6 address. */
773 		memset(&key, 0, sizeof(key));
774 		key.sin6_family = AF_INET6;
775 		key.sin6_len = sizeof(key);
776 		key.sin6_addr.s6_addr[10] = 0xff;
777 		key.sin6_addr.s6_addr[11] = 0xff;
778 		memcpy(&key.sin6_addr.s6_addr[12],
779 		       &((struct sockaddr_in *)addr)->sin_addr, 4);
780 		break;
781 	default:
782 		return(NULL);
783 	}
784 
785 	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
786 		pol = &ent->pc_policy;
787 		matchlen = 0;
788 
789 		mp = (u_char *)&pol->addrmask.sin6_addr;
790 		ep = mp + 16;	/* XXX: scope field? */
791 		k = (u_char *)&key.sin6_addr;
792 		p = (u_char *)&pol->addr.sin6_addr;
793 		for (; mp < ep && *mp; mp++, k++, p++) {
794 			m = *mp;
795 			if ((*k & m) != *p)
796 				goto next; /* not match */
797 			if (m == 0xff) /* short cut for a typical case */
798 				matchlen += 8;
799 			else {
800 				while (m >= 0x80) {
801 					matchlen++;
802 					m <<= 1;
803 				}
804 			}
805 		}
806 
807 		/* matched.  check if this is better than the current best. */
808 		if (matchlen > bestmatchlen) {
809 			bestent = ent;
810 			bestmatchlen = matchlen;
811 		}
812 
813 	  next:
814 		continue;
815 	}
816 
817 	return(bestent);
818 #else
819 	return(NULL);
820 #endif
821 
822 }
823 
824 static void
825 set_source(aio, ph)
826 	struct ai_order *aio;
827 	struct policyhead *ph;
828 {
829 	struct addrinfo ai = *aio->aio_ai;
830 	struct sockaddr_storage ss;
831 	int s, srclen;
832 
833 	/* set unspec ("no source is available"), just in case */
834 	aio->aio_srcsa.sa_family = AF_UNSPEC;
835 	aio->aio_srcscope = -1;
836 
837 	switch(ai.ai_family) {
838 	case AF_INET:
839 #ifdef INET6
840 	case AF_INET6:
841 #endif
842 		break;
843 	default:		/* ignore unsupported AFs explicitly */
844 		return;
845 	}
846 
847 	/* XXX: make a dummy addrinfo to call connect() */
848 	ai.ai_socktype = SOCK_DGRAM;
849 	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
850 	ai.ai_next = NULL;
851 	memset(&ss, 0, sizeof(ss));
852 	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
853 	ai.ai_addr = (struct sockaddr *)&ss;
854 	get_port(&ai, "1", 0);
855 
856 	/* open a socket to get the source address for the given dst */
857 	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
858 		return;		/* give up */
859 	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
860 		goto cleanup;
861 	srclen = ai.ai_addrlen;
862 	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
863 		aio->aio_srcsa.sa_family = AF_UNSPEC;
864 		goto cleanup;
865 	}
866 	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
867 	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
868 	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
869 #ifdef INET6
870 	if (ai.ai_family == AF_INET6) {
871 		struct in6_ifreq ifr6;
872 		u_int32_t flags6;
873 
874 		/* XXX: interface name should not be hardcoded */
875 		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
876 		memset(&ifr6, 0, sizeof(ifr6));
877 		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
878 		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
879 			flags6 = ifr6.ifr_ifru.ifru_flags6;
880 			if ((flags6 & IN6_IFF_DEPRECATED))
881 				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
882 		}
883 	}
884 #endif
885 
886   cleanup:
887 	_close(s);
888 	return;
889 }
890 
891 static int
892 matchlen(src, dst)
893 	struct sockaddr *src, *dst;
894 {
895 	int match = 0;
896 	u_char *s, *d;
897 	u_char *lim, r;
898 	int addrlen;
899 
900 	switch (src->sa_family) {
901 #ifdef INET6
902 	case AF_INET6:
903 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
904 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
905 		addrlen = sizeof(struct in6_addr);
906 		lim = s + addrlen;
907 		break;
908 #endif
909 	case AF_INET:
910 		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
911 		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
912 		addrlen = sizeof(struct in_addr);
913 		lim = s + addrlen;
914 		break;
915 	default:
916 		return(0);
917 	}
918 
919 	while (s < lim)
920 		if ((r = (*d++ ^ *s++)) != 0) {
921 			while (r < addrlen * 8) {
922 				match++;
923 				r <<= 1;
924 			}
925 			break;
926 		} else
927 			match += 8;
928 	return(match);
929 }
930 
931 static int
932 comp_dst(arg1, arg2)
933 	const void *arg1, *arg2;
934 {
935 	const struct ai_order *dst1 = arg1, *dst2 = arg2;
936 
937 	/*
938 	 * Rule 1: Avoid unusable destinations.
939 	 * XXX: we currently do not consider if an appropriate route exists.
940 	 */
941 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
942 	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
943 		return(-1);
944 	}
945 	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
946 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
947 		return(1);
948 	}
949 
950 	/* Rule 2: Prefer matching scope. */
951 	if (dst1->aio_dstscope == dst1->aio_srcscope &&
952 	    dst2->aio_dstscope != dst2->aio_srcscope) {
953 		return(-1);
954 	}
955 	if (dst1->aio_dstscope != dst1->aio_srcscope &&
956 	    dst2->aio_dstscope == dst2->aio_srcscope) {
957 		return(1);
958 	}
959 
960 	/* Rule 3: Avoid deprecated addresses. */
961 	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
962 	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
963 		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
964 		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
965 			return(-1);
966 		}
967 		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
968 		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
969 			return(1);
970 		}
971 	}
972 
973 	/* Rule 4: Prefer home addresses. */
974 	/* XXX: not implemented yet */
975 
976 	/* Rule 5: Prefer matching label. */
977 #ifdef INET6
978 	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
979 	    dst1->aio_srcpolicy->pc_policy.label ==
980 	    dst1->aio_dstpolicy->pc_policy.label &&
981 	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
982 	     dst2->aio_srcpolicy->pc_policy.label !=
983 	     dst2->aio_dstpolicy->pc_policy.label)) {
984 		return(-1);
985 	}
986 	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
987 	    dst2->aio_srcpolicy->pc_policy.label ==
988 	    dst2->aio_dstpolicy->pc_policy.label &&
989 	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
990 	     dst1->aio_srcpolicy->pc_policy.label !=
991 	     dst1->aio_dstpolicy->pc_policy.label)) {
992 		return(1);
993 	}
994 #endif
995 
996 	/* Rule 6: Prefer higher precedence. */
997 #ifdef INET6
998 	if (dst1->aio_dstpolicy &&
999 	    (dst2->aio_dstpolicy == NULL ||
1000 	     dst1->aio_dstpolicy->pc_policy.preced >
1001 	     dst2->aio_dstpolicy->pc_policy.preced)) {
1002 		return(-1);
1003 	}
1004 	if (dst2->aio_dstpolicy &&
1005 	    (dst1->aio_dstpolicy == NULL ||
1006 	     dst2->aio_dstpolicy->pc_policy.preced >
1007 	     dst1->aio_dstpolicy->pc_policy.preced)) {
1008 		return(1);
1009 	}
1010 #endif
1011 
1012 	/* Rule 7: Prefer native transport. */
1013 	/* XXX: not implemented yet */
1014 
1015 	/* Rule 8: Prefer smaller scope. */
1016 	if (dst1->aio_dstscope >= 0 &&
1017 	    dst1->aio_dstscope < dst2->aio_dstscope) {
1018 		return(-1);
1019 	}
1020 	if (dst2->aio_dstscope >= 0 &&
1021 	    dst2->aio_dstscope < dst1->aio_dstscope) {
1022 		return(1);
1023 	}
1024 
1025 	/*
1026 	 * Rule 9: Use longest matching prefix.
1027 	 * We compare the match length in a same AF only.
1028 	 */
1029 	if (dst1->aio_ai->ai_addr->sa_family ==
1030 	    dst2->aio_ai->ai_addr->sa_family) {
1031 		if (dst1->aio_matchlen > dst2->aio_matchlen) {
1032 			return(-1);
1033 		}
1034 		if (dst1->aio_matchlen < dst2->aio_matchlen) {
1035 			return(1);
1036 		}
1037 	}
1038 
1039 	/* Rule 10: Otherwise, leave the order unchanged. */
1040 	return(-1);
1041 }
1042 
1043 /*
1044  * Copy from scope.c.
1045  * XXX: we should standardize the functions and link them as standard
1046  * library.
1047  */
1048 static int
1049 gai_addr2scopetype(sa)
1050 	struct sockaddr *sa;
1051 {
1052 #ifdef INET6
1053 	struct sockaddr_in6 *sa6;
1054 #endif
1055 	struct sockaddr_in *sa4;
1056 
1057 	switch(sa->sa_family) {
1058 #ifdef INET6
1059 	case AF_INET6:
1060 		sa6 = (struct sockaddr_in6 *)sa;
1061 		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1062 			/* just use the scope field of the multicast address */
1063 			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1064 		}
1065 		/*
1066 		 * Unicast addresses: map scope type to corresponding scope
1067 		 * value defined for multcast addresses.
1068 		 * XXX: hardcoded scope type values are bad...
1069 		 */
1070 		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1071 			return(1); /* node local scope */
1072 		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1073 			return(2); /* link-local scope */
1074 		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1075 			return(5); /* site-local scope */
1076 		return(14);	/* global scope */
1077 		break;
1078 #endif
1079 	case AF_INET:
1080 		/*
1081 		 * IPv4 pseudo scoping according to RFC 3484.
1082 		 */
1083 		sa4 = (struct sockaddr_in *)sa;
1084 		/* IPv4 autoconfiguration addresses have link-local scope. */
1085 		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1086 		    ((u_char *)&sa4->sin_addr)[1] == 254)
1087 			return(2);
1088 		/* Private addresses have site-local scope. */
1089 		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1090 		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
1091 		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1092 		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
1093 		     ((u_char *)&sa4->sin_addr)[1] == 168))
1094 			return(14);	/* XXX: It should be 5 unless NAT */
1095 		/* Loopback addresses have link-local scope. */
1096 		if (((u_char *)&sa4->sin_addr)[0] == 127)
1097 			return(2);
1098 		return(14);
1099 		break;
1100 	default:
1101 		errno = EAFNOSUPPORT; /* is this a good error? */
1102 		return(-1);
1103 	}
1104 }
1105 
1106 /*
1107  * hostname == NULL.
1108  * passive socket -> anyaddr (0.0.0.0 or ::)
1109  * non-passive socket -> localhost (127.0.0.1 or ::1)
1110  */
1111 static int
1112 explore_null(pai, servname, res)
1113 	const struct addrinfo *pai;
1114 	const char *servname;
1115 	struct addrinfo **res;
1116 {
1117 	int s;
1118 	const struct afd *afd;
1119 	struct addrinfo *cur;
1120 	struct addrinfo sentinel;
1121 	int error;
1122 
1123 	*res = NULL;
1124 	sentinel.ai_next = NULL;
1125 	cur = &sentinel;
1126 
1127 	/*
1128 	 * filter out AFs that are not supported by the kernel
1129 	 * XXX errno?
1130 	 */
1131 	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1132 	if (s < 0) {
1133 		if (errno != EMFILE)
1134 			return 0;
1135 	} else
1136 		_close(s);
1137 
1138 	/*
1139 	 * if the servname does not match socktype/protocol, ignore it.
1140 	 */
1141 	if (get_portmatch(pai, servname) != 0)
1142 		return 0;
1143 
1144 	afd = find_afd(pai->ai_family);
1145 	if (afd == NULL)
1146 		return 0;
1147 
1148 	if (pai->ai_flags & AI_PASSIVE) {
1149 		GET_AI(cur->ai_next, afd, afd->a_addrany);
1150 		/* xxx meaningless?
1151 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
1152 		 */
1153 		GET_PORT(cur->ai_next, servname);
1154 	} else {
1155 		GET_AI(cur->ai_next, afd, afd->a_loopback);
1156 		/* xxx meaningless?
1157 		 * GET_CANONNAME(cur->ai_next, "localhost");
1158 		 */
1159 		GET_PORT(cur->ai_next, servname);
1160 	}
1161 	cur = cur->ai_next;
1162 
1163 	*res = sentinel.ai_next;
1164 	return 0;
1165 
1166 free:
1167 	if (sentinel.ai_next)
1168 		freeaddrinfo(sentinel.ai_next);
1169 	return error;
1170 }
1171 
1172 /*
1173  * numeric hostname
1174  */
1175 static int
1176 explore_numeric(pai, hostname, servname, res, canonname)
1177 	const struct addrinfo *pai;
1178 	const char *hostname;
1179 	const char *servname;
1180 	struct addrinfo **res;
1181 	const char *canonname;
1182 {
1183 	const struct afd *afd;
1184 	struct addrinfo *cur;
1185 	struct addrinfo sentinel;
1186 	int error;
1187 	char pton[PTON_MAX];
1188 
1189 	*res = NULL;
1190 	sentinel.ai_next = NULL;
1191 	cur = &sentinel;
1192 
1193 	/*
1194 	 * if the servname does not match socktype/protocol, ignore it.
1195 	 */
1196 	if (get_portmatch(pai, servname) != 0)
1197 		return 0;
1198 
1199 	afd = find_afd(pai->ai_family);
1200 	if (afd == NULL)
1201 		return 0;
1202 
1203 	switch (afd->a_af) {
1204 #if 1 /*X/Open spec*/
1205 	case AF_INET:
1206 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
1207 			if (pai->ai_family == afd->a_af ||
1208 			    pai->ai_family == PF_UNSPEC /*?*/) {
1209 				GET_AI(cur->ai_next, afd, pton);
1210 				GET_PORT(cur->ai_next, servname);
1211 				if ((pai->ai_flags & AI_CANONNAME)) {
1212 					/*
1213 					 * Set the numeric address itself as
1214 					 * the canonical name, based on a
1215 					 * clarification in rfc3493.
1216 					 */
1217 					GET_CANONNAME(cur->ai_next, canonname);
1218 				}
1219 				while (cur && cur->ai_next)
1220 					cur = cur->ai_next;
1221 			} else
1222 				ERR(EAI_FAMILY);	/*xxx*/
1223 		}
1224 		break;
1225 #endif
1226 	default:
1227 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1228 			if (pai->ai_family == afd->a_af ||
1229 			    pai->ai_family == PF_UNSPEC /*?*/) {
1230 				GET_AI(cur->ai_next, afd, pton);
1231 				GET_PORT(cur->ai_next, servname);
1232 				if ((pai->ai_flags & AI_CANONNAME)) {
1233 					/*
1234 					 * Set the numeric address itself as
1235 					 * the canonical name, based on a
1236 					 * clarification in rfc3493.
1237 					 */
1238 					GET_CANONNAME(cur->ai_next, canonname);
1239 				}
1240 				while (cur && cur->ai_next)
1241 					cur = cur->ai_next;
1242 			} else
1243 				ERR(EAI_FAMILY);	/* XXX */
1244 		}
1245 		break;
1246 	}
1247 
1248 	*res = sentinel.ai_next;
1249 	return 0;
1250 
1251 free:
1252 bad:
1253 	if (sentinel.ai_next)
1254 		freeaddrinfo(sentinel.ai_next);
1255 	return error;
1256 }
1257 
1258 /*
1259  * numeric hostname with scope
1260  */
1261 static int
1262 explore_numeric_scope(pai, hostname, servname, res)
1263 	const struct addrinfo *pai;
1264 	const char *hostname;
1265 	const char *servname;
1266 	struct addrinfo **res;
1267 {
1268 #if !defined(SCOPE_DELIMITER) || !defined(INET6)
1269 	return explore_numeric(pai, hostname, servname, res, hostname);
1270 #else
1271 	const struct afd *afd;
1272 	struct addrinfo *cur;
1273 	int error;
1274 	char *cp, *hostname2 = NULL, *scope, *addr;
1275 	struct sockaddr_in6 *sin6;
1276 
1277 	/*
1278 	 * if the servname does not match socktype/protocol, ignore it.
1279 	 */
1280 	if (get_portmatch(pai, servname) != 0)
1281 		return 0;
1282 
1283 	afd = find_afd(pai->ai_family);
1284 	if (afd == NULL)
1285 		return 0;
1286 
1287 	if (!afd->a_scoped)
1288 		return explore_numeric(pai, hostname, servname, res, hostname);
1289 
1290 	cp = strchr(hostname, SCOPE_DELIMITER);
1291 	if (cp == NULL)
1292 		return explore_numeric(pai, hostname, servname, res, hostname);
1293 
1294 	/*
1295 	 * Handle special case of <scoped_address><delimiter><scope id>
1296 	 */
1297 	hostname2 = strdup(hostname);
1298 	if (hostname2 == NULL)
1299 		return EAI_MEMORY;
1300 	/* terminate at the delimiter */
1301 	hostname2[cp - hostname] = '\0';
1302 	addr = hostname2;
1303 	scope = cp + 1;
1304 
1305 	error = explore_numeric(pai, addr, servname, res, hostname);
1306 	if (error == 0) {
1307 		u_int32_t scopeid;
1308 
1309 		for (cur = *res; cur; cur = cur->ai_next) {
1310 			if (cur->ai_family != AF_INET6)
1311 				continue;
1312 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1313 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1314 				free(hostname2);
1315 				return(EAI_NONAME); /* XXX: is return OK? */
1316 			}
1317 			sin6->sin6_scope_id = scopeid;
1318 		}
1319 	}
1320 
1321 	free(hostname2);
1322 
1323 	return error;
1324 #endif
1325 }
1326 
1327 static int
1328 get_canonname(pai, ai, str)
1329 	const struct addrinfo *pai;
1330 	struct addrinfo *ai;
1331 	const char *str;
1332 {
1333 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
1334 		ai->ai_canonname = strdup(str);
1335 		if (ai->ai_canonname == NULL)
1336 			return EAI_MEMORY;
1337 	}
1338 	return 0;
1339 }
1340 
1341 static struct addrinfo *
1342 get_ai(pai, afd, addr)
1343 	const struct addrinfo *pai;
1344 	const struct afd *afd;
1345 	const char *addr;
1346 {
1347 	char *p;
1348 	struct addrinfo *ai;
1349 #ifdef FAITH
1350 	struct in6_addr faith_prefix;
1351 	char *fp_str;
1352 	int translate = 0;
1353 #endif
1354 
1355 #ifdef FAITH
1356 	/*
1357 	 * Transfrom an IPv4 addr into a special IPv6 addr format for
1358 	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
1359 	 *
1360 	 * +-----------------------------------+------------+
1361 	 * | faith prefix part (12 bytes)      | embedded   |
1362 	 * |                                   | IPv4 addr part (4 bytes)
1363 	 * +-----------------------------------+------------+
1364 	 *
1365 	 * faith prefix part is specified as ascii IPv6 addr format
1366 	 * in environmental variable GAI.
1367 	 * For FAITH to work correctly, routing to faith prefix must be
1368 	 * setup toward a machine where a FAITH daemon operates.
1369 	 * Also, the machine must enable some mechanizm
1370 	 * (e.g. faith interface hack) to divert those packet with
1371 	 * faith prefixed destination addr to user-land FAITH daemon.
1372 	 */
1373 	fp_str = getenv("GAI");
1374 	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
1375 	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
1376 		u_int32_t v4a;
1377 		u_int8_t v4a_top;
1378 
1379 		memcpy(&v4a, addr, sizeof v4a);
1380 		v4a_top = v4a >> IN_CLASSA_NSHIFT;
1381 		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
1382 		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
1383 			afd = &afdl[N_INET6];
1384 			memcpy(&faith_prefix.s6_addr[12], addr,
1385 			       sizeof(struct in_addr));
1386 			translate = 1;
1387 		}
1388 	}
1389 #endif
1390 
1391 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
1392 		+ (afd->a_socklen));
1393 	if (ai == NULL)
1394 		return NULL;
1395 
1396 	memcpy(ai, pai, sizeof(struct addrinfo));
1397 	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1398 	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
1399 	ai->ai_addr->sa_len = afd->a_socklen;
1400 	ai->ai_addrlen = afd->a_socklen;
1401 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1402 	p = (char *)(void *)(ai->ai_addr);
1403 #ifdef FAITH
1404 	if (translate == 1)
1405 		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
1406 	else
1407 #endif
1408 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1409 	return ai;
1410 }
1411 
1412 static int
1413 get_portmatch(ai, servname)
1414 	const struct addrinfo *ai;
1415 	const char *servname;
1416 {
1417 
1418 	/* get_port does not touch first argument when matchonly == 1. */
1419 	/* LINTED const cast */
1420 	return get_port((struct addrinfo *)ai, servname, 1);
1421 }
1422 
1423 static int
1424 get_port(ai, servname, matchonly)
1425 	struct addrinfo *ai;
1426 	const char *servname;
1427 	int matchonly;
1428 {
1429 	const char *proto;
1430 	struct servent *sp;
1431 	int port;
1432 	int allownumeric;
1433 
1434 	if (servname == NULL)
1435 		return 0;
1436 	switch (ai->ai_family) {
1437 	case AF_INET:
1438 #ifdef AF_INET6
1439 	case AF_INET6:
1440 #endif
1441 		break;
1442 	default:
1443 		return 0;
1444 	}
1445 
1446 	switch (ai->ai_socktype) {
1447 	case SOCK_RAW:
1448 		return EAI_SERVICE;
1449 	case SOCK_DGRAM:
1450 	case SOCK_STREAM:
1451 		allownumeric = 1;
1452 		break;
1453 	case ANY:
1454 		allownumeric = 0;
1455 		break;
1456 	default:
1457 		return EAI_SOCKTYPE;
1458 	}
1459 
1460 	port = str2number(servname);
1461 	if (port >= 0) {
1462 		if (!allownumeric)
1463 			return EAI_SERVICE;
1464 		if (port < 0 || port > 65535)
1465 			return EAI_SERVICE;
1466 		port = htons(port);
1467 	} else {
1468 		if (ai->ai_flags & AI_NUMERICSERV)
1469 			return EAI_NONAME;
1470 		switch (ai->ai_socktype) {
1471 		case SOCK_DGRAM:
1472 			proto = "udp";
1473 			break;
1474 		case SOCK_STREAM:
1475 			proto = "tcp";
1476 			break;
1477 		default:
1478 			proto = NULL;
1479 			break;
1480 		}
1481 
1482 		THREAD_LOCK();
1483 		if ((sp = getservbyname(servname, proto)) == NULL) {
1484 			THREAD_UNLOCK();
1485 			return EAI_SERVICE;
1486 		}
1487 		port = sp->s_port;
1488 		THREAD_UNLOCK();
1489 	}
1490 
1491 	if (!matchonly) {
1492 		switch (ai->ai_family) {
1493 		case AF_INET:
1494 			((struct sockaddr_in *)(void *)
1495 			    ai->ai_addr)->sin_port = port;
1496 			break;
1497 #ifdef INET6
1498 		case AF_INET6:
1499 			((struct sockaddr_in6 *)(void *)
1500 			    ai->ai_addr)->sin6_port = port;
1501 			break;
1502 #endif
1503 		}
1504 	}
1505 
1506 	return 0;
1507 }
1508 
1509 static const struct afd *
1510 find_afd(af)
1511 	int af;
1512 {
1513 	const struct afd *afd;
1514 
1515 	if (af == PF_UNSPEC)
1516 		return NULL;
1517 	for (afd = afdl; afd->a_af; afd++) {
1518 		if (afd->a_af == af)
1519 			return afd;
1520 	}
1521 	return NULL;
1522 }
1523 
1524 /*
1525  * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1526  * will take care of it.
1527  * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1528  * if the code is right or not.
1529  *
1530  * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1531  * _dns_getaddrinfo.
1532  */
1533 static int
1534 addrconfig(pai)
1535 	struct addrinfo *pai;
1536 {
1537 	int s, af;
1538 
1539 	/*
1540 	 * TODO:
1541 	 * Note that implementation dependent test for address
1542 	 * configuration should be done everytime called
1543 	 * (or apropriate interval),
1544 	 * because addresses will be dynamically assigned or deleted.
1545 	 */
1546 	af = pai->ai_family;
1547 	if (af == AF_UNSPEC) {
1548 		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1549 			af = AF_INET;
1550 		else {
1551 			_close(s);
1552 			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1553 				af = AF_INET6;
1554 			else
1555 				_close(s);
1556 		}
1557 	}
1558 	if (af != AF_UNSPEC) {
1559 		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1560 			return 0;
1561 		_close(s);
1562 	}
1563 	pai->ai_family = af;
1564 	return 1;
1565 }
1566 
1567 #ifdef INET6
1568 /* convert a string to a scope identifier. XXX: IPv6 specific */
1569 static int
1570 ip6_str2scopeid(scope, sin6, scopeid)
1571 	char *scope;
1572 	struct sockaddr_in6 *sin6;
1573 	u_int32_t *scopeid;
1574 {
1575 	u_long lscopeid;
1576 	struct in6_addr *a6;
1577 	char *ep;
1578 
1579 	a6 = &sin6->sin6_addr;
1580 
1581 	/* empty scopeid portion is invalid */
1582 	if (*scope == '\0')
1583 		return -1;
1584 
1585 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1586 		/*
1587 		 * We currently assume a one-to-one mapping between links
1588 		 * and interfaces, so we simply use interface indices for
1589 		 * like-local scopes.
1590 		 */
1591 		*scopeid = if_nametoindex(scope);
1592 		if (*scopeid == 0)
1593 			goto trynumeric;
1594 		return 0;
1595 	}
1596 
1597 	/* still unclear about literal, allow numeric only - placeholder */
1598 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1599 		goto trynumeric;
1600 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1601 		goto trynumeric;
1602 	else
1603 		goto trynumeric;	/* global */
1604 
1605 	/* try to convert to a numeric id as a last resort */
1606   trynumeric:
1607 	errno = 0;
1608 	lscopeid = strtoul(scope, &ep, 10);
1609 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1610 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1611 		return 0;
1612 	else
1613 		return -1;
1614 }
1615 #endif
1616 
1617 /*
1618  * FQDN hostname, DNS lookup
1619  */
1620 static int
1621 explore_fqdn(pai, hostname, servname, res)
1622 	const struct addrinfo *pai;
1623 	const char *hostname;
1624 	const char *servname;
1625 	struct addrinfo **res;
1626 {
1627 	struct addrinfo *result;
1628 	struct addrinfo *cur;
1629 	int error = 0;
1630 	static const ns_dtab dtab[] = {
1631 		NS_FILES_CB(_files_getaddrinfo, NULL)
1632 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1633 		NS_NIS_CB(_yp_getaddrinfo, NULL)
1634 		{ 0 }
1635 	};
1636 
1637 	result = NULL;
1638 
1639 	/*
1640 	 * if the servname does not match socktype/protocol, ignore it.
1641 	 */
1642 	if (get_portmatch(pai, servname) != 0)
1643 		return 0;
1644 
1645 	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1646 			default_dns_files, hostname, pai)) {
1647 	case NS_TRYAGAIN:
1648 		error = EAI_AGAIN;
1649 		goto free;
1650 	case NS_UNAVAIL:
1651 		error = EAI_FAIL;
1652 		goto free;
1653 	case NS_NOTFOUND:
1654 		error = EAI_NONAME;
1655 		goto free;
1656 	case NS_SUCCESS:
1657 		error = 0;
1658 		for (cur = result; cur; cur = cur->ai_next) {
1659 			GET_PORT(cur, servname);
1660 			/* canonname should be filled already */
1661 		}
1662 		break;
1663 	}
1664 
1665 	*res = result;
1666 
1667 	return 0;
1668 
1669 free:
1670 	if (result)
1671 		freeaddrinfo(result);
1672 	return error;
1673 }
1674 
1675 #ifdef DEBUG
1676 static const char AskedForGot[] =
1677 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1678 #endif
1679 static FILE *hostf = NULL;
1680 
1681 static struct addrinfo *
1682 getanswer(answer, anslen, qname, qtype, pai)
1683 	const querybuf *answer;
1684 	int anslen;
1685 	const char *qname;
1686 	int qtype;
1687 	const struct addrinfo *pai;
1688 {
1689 	struct addrinfo sentinel, *cur;
1690 	struct addrinfo ai;
1691 	const struct afd *afd;
1692 	char *canonname;
1693 	const HEADER *hp;
1694 	const u_char *cp;
1695 	int n;
1696 	const u_char *eom;
1697 	char *bp, *ep;
1698 	int type, class, ancount, qdcount;
1699 	int haveanswer, had_error;
1700 	char tbuf[MAXDNAME];
1701 	int (*name_ok)(const char *);
1702 	char hostbuf[8*1024];
1703 
1704 	memset(&sentinel, 0, sizeof(sentinel));
1705 	cur = &sentinel;
1706 
1707 	canonname = NULL;
1708 	eom = answer->buf + anslen;
1709 	switch (qtype) {
1710 	case T_A:
1711 	case T_AAAA:
1712 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1713 		name_ok = res_hnok;
1714 		break;
1715 	default:
1716 		return (NULL);	/* XXX should be abort(); */
1717 	}
1718 	/*
1719 	 * find first satisfactory answer
1720 	 */
1721 	hp = &answer->hdr;
1722 	ancount = ntohs(hp->ancount);
1723 	qdcount = ntohs(hp->qdcount);
1724 	bp = hostbuf;
1725 	ep = hostbuf + sizeof hostbuf;
1726 	cp = answer->buf + HFIXEDSZ;
1727 	if (qdcount != 1) {
1728 		h_errno = NO_RECOVERY;
1729 		return (NULL);
1730 	}
1731 	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1732 	if ((n < 0) || !(*name_ok)(bp)) {
1733 		h_errno = NO_RECOVERY;
1734 		return (NULL);
1735 	}
1736 	cp += n + QFIXEDSZ;
1737 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1738 		/* res_send() has already verified that the query name is the
1739 		 * same as the one we sent; this just gets the expanded name
1740 		 * (i.e., with the succeeding search-domain tacked on).
1741 		 */
1742 		n = strlen(bp) + 1;		/* for the \0 */
1743 		if (n >= MAXHOSTNAMELEN) {
1744 			h_errno = NO_RECOVERY;
1745 			return (NULL);
1746 		}
1747 		canonname = bp;
1748 		bp += n;
1749 		/* The qname can be abbreviated, but h_name is now absolute. */
1750 		qname = canonname;
1751 	}
1752 	haveanswer = 0;
1753 	had_error = 0;
1754 	while (ancount-- > 0 && cp < eom && !had_error) {
1755 		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1756 		if ((n < 0) || !(*name_ok)(bp)) {
1757 			had_error++;
1758 			continue;
1759 		}
1760 		cp += n;			/* name */
1761 		type = _getshort(cp);
1762  		cp += INT16SZ;			/* type */
1763 		class = _getshort(cp);
1764  		cp += INT16SZ + INT32SZ;	/* class, TTL */
1765 		n = _getshort(cp);
1766 		cp += INT16SZ;			/* len */
1767 		if (class != C_IN) {
1768 			/* XXX - debug? syslog? */
1769 			cp += n;
1770 			continue;		/* XXX - had_error++ ? */
1771 		}
1772 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1773 		    type == T_CNAME) {
1774 			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1775 			if ((n < 0) || !(*name_ok)(tbuf)) {
1776 				had_error++;
1777 				continue;
1778 			}
1779 			cp += n;
1780 			/* Get canonical name. */
1781 			n = strlen(tbuf) + 1;	/* for the \0 */
1782 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1783 				had_error++;
1784 				continue;
1785 			}
1786 			strlcpy(bp, tbuf, ep - bp);
1787 			canonname = bp;
1788 			bp += n;
1789 			continue;
1790 		}
1791 		if (qtype == T_ANY) {
1792 			if (!(type == T_A || type == T_AAAA)) {
1793 				cp += n;
1794 				continue;
1795 			}
1796 		} else if (type != qtype) {
1797 #ifdef DEBUG
1798 			if (type != T_KEY && type != T_SIG)
1799 				syslog(LOG_NOTICE|LOG_AUTH,
1800 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1801 				       qname, p_class(C_IN), p_type(qtype),
1802 				       p_type(type));
1803 #endif
1804 			cp += n;
1805 			continue;		/* XXX - had_error++ ? */
1806 		}
1807 		switch (type) {
1808 		case T_A:
1809 		case T_AAAA:
1810 			if (strcasecmp(canonname, bp) != 0) {
1811 #ifdef DEBUG
1812 				syslog(LOG_NOTICE|LOG_AUTH,
1813 				       AskedForGot, canonname, bp);
1814 #endif
1815 				cp += n;
1816 				continue;	/* XXX - had_error++ ? */
1817 			}
1818 			if (type == T_A && n != INADDRSZ) {
1819 				cp += n;
1820 				continue;
1821 			}
1822 			if (type == T_AAAA && n != IN6ADDRSZ) {
1823 				cp += n;
1824 				continue;
1825 			}
1826 #ifdef FILTER_V4MAPPED
1827 			if (type == T_AAAA) {
1828 				struct in6_addr in6;
1829 				memcpy(&in6, cp, sizeof(in6));
1830 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1831 					cp += n;
1832 					continue;
1833 				}
1834 			}
1835 #endif
1836 			if (!haveanswer) {
1837 				int nn;
1838 
1839 				canonname = bp;
1840 				nn = strlen(bp) + 1;	/* for the \0 */
1841 				bp += nn;
1842 			}
1843 
1844 			/* don't overwrite pai */
1845 			ai = *pai;
1846 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1847 			afd = find_afd(ai.ai_family);
1848 			if (afd == NULL) {
1849 				cp += n;
1850 				continue;
1851 			}
1852 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1853 			if (cur->ai_next == NULL)
1854 				had_error++;
1855 			while (cur && cur->ai_next)
1856 				cur = cur->ai_next;
1857 			cp += n;
1858 			break;
1859 		default:
1860 			abort();
1861 		}
1862 		if (!had_error)
1863 			haveanswer++;
1864 	}
1865 	if (haveanswer) {
1866 #if defined(RESOLVSORT)
1867 		/*
1868 		 * We support only IPv4 address for backward
1869 		 * compatibility against gethostbyname(3).
1870 		 */
1871 		if (_res.nsort && qtype == T_A) {
1872 			if (addr4sort(&sentinel) < 0) {
1873 				freeaddrinfo(sentinel.ai_next);
1874 				h_errno = NO_RECOVERY;
1875 				return NULL;
1876 			}
1877 		}
1878 #endif /*RESOLVSORT*/
1879 		if (!canonname)
1880 			(void)get_canonname(pai, sentinel.ai_next, qname);
1881 		else
1882 			(void)get_canonname(pai, sentinel.ai_next, canonname);
1883 		h_errno = NETDB_SUCCESS;
1884 		return sentinel.ai_next;
1885 	}
1886 
1887 	h_errno = NO_RECOVERY;
1888 	return NULL;
1889 }
1890 
1891 #ifdef RESOLVSORT
1892 struct addr_ptr {
1893 	struct addrinfo *ai;
1894 	int aval;
1895 };
1896 
1897 static int
1898 addr4sort(struct addrinfo *sentinel)
1899 {
1900 	struct addrinfo *ai;
1901 	struct addr_ptr *addrs, addr;
1902 	struct sockaddr_in *sin;
1903 	int naddrs, i, j;
1904 	int needsort = 0;
1905 
1906 	if (!sentinel)
1907 		return -1;
1908 	naddrs = 0;
1909 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
1910 		naddrs++;
1911 	if (naddrs < 2)
1912 		return 0;		/* We don't need sorting. */
1913 	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
1914 		return -1;
1915 	i = 0;
1916 	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
1917 		sin = (struct sockaddr_in *)ai->ai_addr;
1918 		for (j = 0; (unsigned)j < _res.nsort; j++) {
1919 			if (_res.sort_list[j].addr.s_addr ==
1920 			    (sin->sin_addr.s_addr & _res.sort_list[j].mask))
1921 				break;
1922 		}
1923 		addrs[i].ai = ai;
1924 		addrs[i].aval = j;
1925 		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
1926 			needsort = i;
1927 		i++;
1928 	}
1929 	if (!needsort) {
1930 		free(addrs);
1931 		return 0;
1932 	}
1933 
1934 	while (needsort < naddrs) {
1935 	    for (j = needsort - 1; j >= 0; j--) {
1936 		if (addrs[j].aval > addrs[j+1].aval) {
1937 		    addr = addrs[j];
1938 		    addrs[j] = addrs[j + 1];
1939 		    addrs[j + 1] = addr;
1940 		} else
1941 		    break;
1942 	    }
1943 	    needsort++;
1944 	}
1945 
1946 	ai = sentinel;
1947 	for (i = 0; i < naddrs; ++i) {
1948 		ai->ai_next = addrs[i].ai;
1949 		ai = ai->ai_next;
1950 	}
1951 	ai->ai_next = NULL;
1952 	free(addrs);
1953 	return 0;
1954 }
1955 #endif /*RESOLVSORT*/
1956 
1957 /*ARGSUSED*/
1958 static int
1959 _dns_getaddrinfo(rv, cb_data, ap)
1960 	void	*rv;
1961 	void	*cb_data;
1962 	va_list	 ap;
1963 {
1964 	struct addrinfo *ai;
1965 	querybuf *buf, *buf2;
1966 	const char *hostname;
1967 	const struct addrinfo *pai;
1968 	struct addrinfo sentinel, *cur;
1969 	struct res_target q, q2;
1970 
1971 	hostname = va_arg(ap, char *);
1972 	pai = va_arg(ap, const struct addrinfo *);
1973 
1974 	memset(&q, 0, sizeof(q2));
1975 	memset(&q2, 0, sizeof(q2));
1976 	memset(&sentinel, 0, sizeof(sentinel));
1977 	cur = &sentinel;
1978 
1979 	buf = malloc(sizeof(*buf));
1980 	if (!buf) {
1981 		h_errno = NETDB_INTERNAL;
1982 		return NS_NOTFOUND;
1983 	}
1984 	buf2 = malloc(sizeof(*buf2));
1985 	if (!buf2) {
1986 		free(buf);
1987 		h_errno = NETDB_INTERNAL;
1988 		return NS_NOTFOUND;
1989 	}
1990 
1991 	switch (pai->ai_family) {
1992 	case AF_UNSPEC:
1993 		q.name = hostname;
1994 		q.qclass = C_IN;
1995 		q.qtype = T_A;
1996 		q.answer = buf->buf;
1997 		q.anslen = sizeof(buf->buf);
1998 		q.next = &q2;
1999 		q2.name = hostname;
2000 		q2.qclass = C_IN;
2001 		q2.qtype = T_AAAA;
2002 		q2.answer = buf2->buf;
2003 		q2.anslen = sizeof(buf2->buf);
2004 		break;
2005 	case AF_INET:
2006 		q.name = hostname;
2007 		q.qclass = C_IN;
2008 		q.qtype = T_A;
2009 		q.answer = buf->buf;
2010 		q.anslen = sizeof(buf->buf);
2011 		break;
2012 	case AF_INET6:
2013 		q.name = hostname;
2014 		q.qclass = C_IN;
2015 		q.qtype = T_AAAA;
2016 		q.answer = buf->buf;
2017 		q.anslen = sizeof(buf->buf);
2018 		break;
2019 	default:
2020 		free(buf);
2021 		free(buf2);
2022 		return NS_UNAVAIL;
2023 	}
2024 	if (res_searchN(hostname, &q) < 0) {
2025 		free(buf);
2026 		free(buf2);
2027 		return NS_NOTFOUND;
2028 	}
2029 	/* prefer IPv6 */
2030 	if (q.next) {
2031 		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
2032 		if (ai) {
2033 			cur->ai_next = ai;
2034 			while (cur && cur->ai_next)
2035 				cur = cur->ai_next;
2036 		}
2037 	}
2038 	ai = getanswer(buf, q.n, q.name, q.qtype, pai);
2039 	if (ai)
2040 		cur->ai_next = ai;
2041 	free(buf);
2042 	free(buf2);
2043 	if (sentinel.ai_next == NULL)
2044 		switch (h_errno) {
2045 		case HOST_NOT_FOUND:
2046 			return NS_NOTFOUND;
2047 		case TRY_AGAIN:
2048 			return NS_TRYAGAIN;
2049 		default:
2050 			return NS_UNAVAIL;
2051 		}
2052 	*((struct addrinfo **)rv) = sentinel.ai_next;
2053 	return NS_SUCCESS;
2054 }
2055 
2056 static void
2057 _sethtent()
2058 {
2059 	if (!hostf)
2060 		hostf = fopen(_PATH_HOSTS, "r" );
2061 	else
2062 		rewind(hostf);
2063 }
2064 
2065 static void
2066 _endhtent()
2067 {
2068 	if (hostf) {
2069 		(void) fclose(hostf);
2070 		hostf = NULL;
2071 	}
2072 }
2073 
2074 static struct addrinfo *
2075 _gethtent(name, pai)
2076 	const char *name;
2077 	const struct addrinfo *pai;
2078 {
2079 	char *p;
2080 	char *cp, *tname, *cname;
2081 	struct addrinfo hints, *res0, *res;
2082 	int error;
2083 	const char *addr;
2084 	char hostbuf[8*1024];
2085 
2086 	if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" )))
2087 		return (NULL);
2088 again:
2089 	if (!(p = fgets(hostbuf, sizeof hostbuf, hostf)))
2090 		return (NULL);
2091 	if (*p == '#')
2092 		goto again;
2093 	cp = strpbrk(p, "#\n");
2094 	if (cp != NULL)
2095 		*cp = '\0';
2096 	if (!(cp = strpbrk(p, " \t")))
2097 		goto again;
2098 	*cp++ = '\0';
2099 	addr = p;
2100 	cname = NULL;
2101 	/* if this is not something we're looking for, skip it. */
2102 	while (cp && *cp) {
2103 		if (*cp == ' ' || *cp == '\t') {
2104 			cp++;
2105 			continue;
2106 		}
2107 		tname = cp;
2108 		if (cname == NULL)
2109 			cname = cp;
2110 		if ((cp = strpbrk(cp, " \t")) != NULL)
2111 			*cp++ = '\0';
2112 		if (strcasecmp(name, tname) == 0)
2113 			goto found;
2114 	}
2115 	goto again;
2116 
2117 found:
2118 	/* we should not glob socktype/protocol here */
2119 	memset(&hints, 0, sizeof(hints));
2120 	hints.ai_family = pai->ai_family;
2121 	hints.ai_socktype = SOCK_DGRAM;
2122 	hints.ai_protocol = 0;
2123 	hints.ai_flags = AI_NUMERICHOST;
2124 	error = getaddrinfo(addr, "0", &hints, &res0);
2125 	if (error)
2126 		goto again;
2127 #ifdef FILTER_V4MAPPED
2128 	/* XXX should check all items in the chain */
2129 	if (res0->ai_family == AF_INET6 &&
2130 	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2131 		freeaddrinfo(res0);
2132 		goto again;
2133 	}
2134 #endif
2135 	for (res = res0; res; res = res->ai_next) {
2136 		/* cover it up */
2137 		res->ai_flags = pai->ai_flags;
2138 		res->ai_socktype = pai->ai_socktype;
2139 		res->ai_protocol = pai->ai_protocol;
2140 
2141 		if (pai->ai_flags & AI_CANONNAME) {
2142 			if (get_canonname(pai, res, cname) != 0) {
2143 				freeaddrinfo(res0);
2144 				goto again;
2145 			}
2146 		}
2147 	}
2148 	return res0;
2149 }
2150 
2151 /*ARGSUSED*/
2152 static int
2153 _files_getaddrinfo(rv, cb_data, ap)
2154 	void	*rv;
2155 	void	*cb_data;
2156 	va_list	 ap;
2157 {
2158 	const char *name;
2159 	const struct addrinfo *pai;
2160 	struct addrinfo sentinel, *cur;
2161 	struct addrinfo *p;
2162 
2163 	name = va_arg(ap, char *);
2164 	pai = va_arg(ap, struct addrinfo *);
2165 
2166 	memset(&sentinel, 0, sizeof(sentinel));
2167 	cur = &sentinel;
2168 
2169 	THREAD_LOCK();
2170 	_sethtent();
2171 	while ((p = _gethtent(name, pai)) != NULL) {
2172 		cur->ai_next = p;
2173 		while (cur && cur->ai_next)
2174 			cur = cur->ai_next;
2175 	}
2176 	_endhtent();
2177 	THREAD_UNLOCK();
2178 
2179 	*((struct addrinfo **)rv) = sentinel.ai_next;
2180 	if (sentinel.ai_next == NULL)
2181 		return NS_NOTFOUND;
2182 	return NS_SUCCESS;
2183 }
2184 
2185 #ifdef YP
2186 static char *__ypdomain;
2187 
2188 /*ARGSUSED*/
2189 static struct addrinfo *
2190 _yphostent(line, pai)
2191 	char *line;
2192 	const struct addrinfo *pai;
2193 {
2194 	struct addrinfo sentinel, *cur;
2195 	struct addrinfo hints, *res, *res0;
2196 	int error;
2197 	char *p = line;
2198 	const char *addr, *canonname;
2199 	char *nextline;
2200 	char *cp;
2201 
2202 	addr = canonname = NULL;
2203 
2204 	memset(&sentinel, 0, sizeof(sentinel));
2205 	cur = &sentinel;
2206 
2207 nextline:
2208 	/* terminate line */
2209 	cp = strchr(p, '\n');
2210 	if (cp) {
2211 		*cp++ = '\0';
2212 		nextline = cp;
2213 	} else
2214 		nextline = NULL;
2215 
2216 	cp = strpbrk(p, " \t");
2217 	if (cp == NULL) {
2218 		if (canonname == NULL)
2219 			return (NULL);
2220 		else
2221 			goto done;
2222 	}
2223 	*cp++ = '\0';
2224 
2225 	addr = p;
2226 
2227 	while (cp && *cp) {
2228 		if (*cp == ' ' || *cp == '\t') {
2229 			cp++;
2230 			continue;
2231 		}
2232 		if (!canonname)
2233 			canonname = cp;
2234 		if ((cp = strpbrk(cp, " \t")) != NULL)
2235 			*cp++ = '\0';
2236 	}
2237 
2238 	hints = *pai;
2239 	hints.ai_flags = AI_NUMERICHOST;
2240 	error = getaddrinfo(addr, NULL, &hints, &res0);
2241 	if (error == 0) {
2242 		for (res = res0; res; res = res->ai_next) {
2243 			/* cover it up */
2244 			res->ai_flags = pai->ai_flags;
2245 
2246 			if (pai->ai_flags & AI_CANONNAME)
2247 				(void)get_canonname(pai, res, canonname);
2248 		}
2249 	} else
2250 		res0 = NULL;
2251 	if (res0) {
2252 		cur->ai_next = res0;
2253 		while (cur && cur->ai_next)
2254 			cur = cur->ai_next;
2255 	}
2256 
2257 	if (nextline) {
2258 		p = nextline;
2259 		goto nextline;
2260 	}
2261 
2262 done:
2263 	return sentinel.ai_next;
2264 }
2265 
2266 /*ARGSUSED*/
2267 static int
2268 _yp_getaddrinfo(rv, cb_data, ap)
2269 	void	*rv;
2270 	void	*cb_data;
2271 	va_list	 ap;
2272 {
2273 	struct addrinfo sentinel, *cur;
2274 	struct addrinfo *ai = NULL;
2275 	static char *__ypcurrent;
2276 	int __ypcurrentlen, r;
2277 	const char *name;
2278 	const struct addrinfo *pai;
2279 
2280 	name = va_arg(ap, char *);
2281 	pai = va_arg(ap, const struct addrinfo *);
2282 
2283 	memset(&sentinel, 0, sizeof(sentinel));
2284 	cur = &sentinel;
2285 
2286 	THREAD_LOCK();
2287 	if (!__ypdomain) {
2288 		if (_yp_check(&__ypdomain) == 0) {
2289 			THREAD_UNLOCK();
2290 			return NS_UNAVAIL;
2291 		}
2292 	}
2293 	if (__ypcurrent)
2294 		free(__ypcurrent);
2295 	__ypcurrent = NULL;
2296 
2297 	/* hosts.byname is only for IPv4 (Solaris8) */
2298 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2299 		r = yp_match(__ypdomain, "hosts.byname", name,
2300 			(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
2301 		if (r == 0) {
2302 			struct addrinfo ai4;
2303 
2304 			ai4 = *pai;
2305 			ai4.ai_family = AF_INET;
2306 			ai = _yphostent(__ypcurrent, &ai4);
2307 			if (ai) {
2308 				cur->ai_next = ai;
2309 				while (cur && cur->ai_next)
2310 					cur = cur->ai_next;
2311 			}
2312 		}
2313 	}
2314 
2315 	/* ipnodes.byname can hold both IPv4/v6 */
2316 	r = yp_match(__ypdomain, "ipnodes.byname", name,
2317 		(int)strlen(name), &__ypcurrent, &__ypcurrentlen);
2318 	if (r == 0) {
2319 		ai = _yphostent(__ypcurrent, pai);
2320 		if (ai) {
2321 			cur->ai_next = ai;
2322 			while (cur && cur->ai_next)
2323 				cur = cur->ai_next;
2324 		}
2325 	}
2326 	THREAD_UNLOCK();
2327 
2328 	if (sentinel.ai_next == NULL) {
2329 		h_errno = HOST_NOT_FOUND;
2330 		return NS_NOTFOUND;
2331 	}
2332 	*((struct addrinfo **)rv) = sentinel.ai_next;
2333 	return NS_SUCCESS;
2334 }
2335 #endif
2336 
2337 /* resolver logic */
2338 
2339 extern const char *__hostalias(const char *);
2340 
2341 /*
2342  * Formulate a normal query, send, and await answer.
2343  * Returned answer is placed in supplied buffer "answer".
2344  * Perform preliminary check of answer, returning success only
2345  * if no error is indicated and the answer count is nonzero.
2346  * Return the size of the response on success, -1 on error.
2347  * Error number is left in h_errno.
2348  *
2349  * Caller must parse answer and determine whether it answers the question.
2350  */
2351 static int
2352 res_queryN(name, target)
2353 	const char *name;	/* domain name */
2354 	struct res_target *target;
2355 {
2356 	u_char *buf;
2357 	HEADER *hp;
2358 	int n;
2359 	struct res_target *t;
2360 	int rcode;
2361 	int ancount;
2362 
2363 	rcode = NOERROR;
2364 	ancount = 0;
2365 
2366 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2367 		h_errno = NETDB_INTERNAL;
2368 		return (-1);
2369 	}
2370 
2371 	buf = malloc(MAXPACKET);
2372 	if (!buf) {
2373 		h_errno = NETDB_INTERNAL;
2374 		return -1;
2375 	}
2376 
2377 	for (t = target; t; t = t->next) {
2378 		int class, type;
2379 		u_char *answer;
2380 		int anslen;
2381 
2382 		hp = (HEADER *)(void *)t->answer;
2383 		hp->rcode = NOERROR;	/* default */
2384 
2385 		/* make it easier... */
2386 		class = t->qclass;
2387 		type = t->qtype;
2388 		answer = t->answer;
2389 		anslen = t->anslen;
2390 #ifdef DEBUG
2391 		if (_res.options & RES_DEBUG)
2392 			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2393 #endif
2394 
2395 		n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
2396 		    buf, MAXPACKET);
2397 		if (n > 0 && (_res.options & RES_USE_EDNS0) != 0)
2398 			n = res_opt(n, buf, MAXPACKET, anslen);
2399 		if (n <= 0) {
2400 #ifdef DEBUG
2401 			if (_res.options & RES_DEBUG)
2402 				printf(";; res_query: mkquery failed\n");
2403 #endif
2404 			free(buf);
2405 			h_errno = NO_RECOVERY;
2406 			return (n);
2407 		}
2408 		n = res_send(buf, n, answer, anslen);
2409 #if 0
2410 		if (n < 0) {
2411 #ifdef DEBUG
2412 			if (_res.options & RES_DEBUG)
2413 				printf(";; res_query: send error\n");
2414 #endif
2415 			free(buf);
2416 			h_errno = TRY_AGAIN;
2417 			return (n);
2418 		}
2419 #endif
2420 
2421 		if (n < 0 || n > anslen)
2422 			hp->rcode = FORMERR; /* XXX not very informative */
2423 		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2424 			rcode = hp->rcode;	/* record most recent error */
2425 #ifdef DEBUG
2426 			if (_res.options & RES_DEBUG)
2427 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2428 				    ntohs(hp->ancount));
2429 #endif
2430 			continue;
2431 		}
2432 
2433 		ancount += ntohs(hp->ancount);
2434 
2435 		t->n = n;
2436 	}
2437 
2438 	free(buf);
2439 
2440 	if (ancount == 0) {
2441 		switch (rcode) {
2442 		case NXDOMAIN:
2443 			h_errno = HOST_NOT_FOUND;
2444 			break;
2445 		case SERVFAIL:
2446 			h_errno = TRY_AGAIN;
2447 			break;
2448 		case NOERROR:
2449 			h_errno = NO_DATA;
2450 			break;
2451 		case FORMERR:
2452 		case NOTIMP:
2453 		case REFUSED:
2454 		default:
2455 			h_errno = NO_RECOVERY;
2456 			break;
2457 		}
2458 		return (-1);
2459 	}
2460 	return (ancount);
2461 }
2462 
2463 /*
2464  * Formulate a normal query, send, and retrieve answer in supplied buffer.
2465  * Return the size of the response on success, -1 on error.
2466  * If enabled, implement search rules until answer or unrecoverable failure
2467  * is detected.  Error code, if any, is left in h_errno.
2468  */
2469 static int
2470 res_searchN(name, target)
2471 	const char *name;	/* domain name */
2472 	struct res_target *target;
2473 {
2474 	const char *cp, * const *domain;
2475 	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2476 	u_int dots;
2477 	int trailing_dot, ret, saved_herrno;
2478 	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
2479 
2480 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2481 		h_errno = NETDB_INTERNAL;
2482 		return (-1);
2483 	}
2484 
2485 	errno = 0;
2486 	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
2487 	dots = 0;
2488 	for (cp = name; *cp; cp++)
2489 		dots += (*cp == '.');
2490 	trailing_dot = 0;
2491 	if (cp > name && *--cp == '.')
2492 		trailing_dot++;
2493 
2494 	/*
2495 	 * if there aren't any dots, it could be a user-level alias
2496 	 */
2497 	if (!dots && (cp = __hostalias(name)) != NULL)
2498 		return (res_queryN(cp, target));
2499 
2500 	/*
2501 	 * If there are dots in the name already, let's just give it a try
2502 	 * 'as is'.  The threshold can be set with the "ndots" option.
2503 	 */
2504 	saved_herrno = -1;
2505 	if (dots >= _res.ndots) {
2506 		ret = res_querydomainN(name, NULL, target);
2507 		if (ret > 0)
2508 			return (ret);
2509 		saved_herrno = h_errno;
2510 		tried_as_is++;
2511 	}
2512 
2513 	/*
2514 	 * We do at least one level of search if
2515 	 *	- there is no dot and RES_DEFNAME is set, or
2516 	 *	- there is at least one dot, there is no trailing dot,
2517 	 *	  and RES_DNSRCH is set.
2518 	 */
2519 	if ((!dots && (_res.options & RES_DEFNAMES)) ||
2520 	    (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
2521 		int done = 0;
2522 
2523 		for (domain = (const char * const *)_res.dnsrch;
2524 		   *domain && !done;
2525 		   domain++) {
2526 
2527 			ret = res_querydomainN(name, *domain, target);
2528 			if (ret > 0)
2529 				return (ret);
2530 
2531 			/*
2532 			 * If no server present, give up.
2533 			 * If name isn't found in this domain,
2534 			 * keep trying higher domains in the search list
2535 			 * (if that's enabled).
2536 			 * On a NO_DATA error, keep trying, otherwise
2537 			 * a wildcard entry of another type could keep us
2538 			 * from finding this entry higher in the domain.
2539 			 * If we get some other error (negative answer or
2540 			 * server failure), then stop searching up,
2541 			 * but try the input name below in case it's
2542 			 * fully-qualified.
2543 			 */
2544 			if (errno == ECONNREFUSED) {
2545 				h_errno = TRY_AGAIN;
2546 				return (-1);
2547 			}
2548 
2549 			switch (h_errno) {
2550 			case NO_DATA:
2551 				got_nodata++;
2552 				/* FALLTHROUGH */
2553 			case HOST_NOT_FOUND:
2554 				/* keep trying */
2555 				break;
2556 			case TRY_AGAIN:
2557 				if (hp->rcode == SERVFAIL) {
2558 					/* try next search element, if any */
2559 					got_servfail++;
2560 					break;
2561 				}
2562 				/* FALLTHROUGH */
2563 			default:
2564 				/* anything else implies that we're done */
2565 				done++;
2566 			}
2567 			/*
2568 			 * if we got here for some reason other than DNSRCH,
2569 			 * we only wanted one iteration of the loop, so stop.
2570 			 */
2571 			if (!(_res.options & RES_DNSRCH))
2572 			        done++;
2573 		}
2574 	}
2575 
2576 	/*
2577 	 * if we have not already tried the name "as is", do that now.
2578 	 * note that we do this regardless of how many dots were in the
2579 	 * name or whether it ends with a dot.
2580 	 */
2581 	if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) {
2582 		ret = res_querydomainN(name, NULL, target);
2583 		if (ret > 0)
2584 			return (ret);
2585 	}
2586 
2587 	/*
2588 	 * if we got here, we didn't satisfy the search.
2589 	 * if we did an initial full query, return that query's h_errno
2590 	 * (note that we wouldn't be here if that query had succeeded).
2591 	 * else if we ever got a nodata, send that back as the reason.
2592 	 * else send back meaningless h_errno, that being the one from
2593 	 * the last DNSRCH we did.
2594 	 */
2595 	if (saved_herrno != -1)
2596 		h_errno = saved_herrno;
2597 	else if (got_nodata)
2598 		h_errno = NO_DATA;
2599 	else if (got_servfail)
2600 		h_errno = TRY_AGAIN;
2601 	return (-1);
2602 }
2603 
2604 /*
2605  * Perform a call on res_query on the concatenation of name and domain,
2606  * removing a trailing dot from name if domain is NULL.
2607  */
2608 static int
2609 res_querydomainN(name, domain, target)
2610 	const char *name, *domain;
2611 	struct res_target *target;
2612 {
2613 	char nbuf[MAXDNAME];
2614 	const char *longname = nbuf;
2615 	size_t n, d;
2616 
2617 	if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
2618 		h_errno = NETDB_INTERNAL;
2619 		return (-1);
2620 	}
2621 #ifdef DEBUG
2622 	if (_res.options & RES_DEBUG)
2623 		printf(";; res_querydomain(%s, %s)\n",
2624 			name, domain?domain:"<Nil>");
2625 #endif
2626 	if (domain == NULL) {
2627 		/*
2628 		 * Check for trailing '.';
2629 		 * copy without '.' if present.
2630 		 */
2631 		n = strlen(name);
2632 		if (n >= MAXDNAME) {
2633 			h_errno = NO_RECOVERY;
2634 			return (-1);
2635 		}
2636 		if (n > 0 && name[--n] == '.') {
2637 			strncpy(nbuf, name, n);
2638 			nbuf[n] = '\0';
2639 		} else
2640 			longname = name;
2641 	} else {
2642 		n = strlen(name);
2643 		d = strlen(domain);
2644 		if (n + d + 1 >= MAXDNAME) {
2645 			h_errno = NO_RECOVERY;
2646 			return (-1);
2647 		}
2648 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2649 	}
2650 	return (res_queryN(longname, target));
2651 }
2652