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