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