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