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