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