getaddrinfo.c revision 160552
11558Srgrimes/*	$KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $	*/
21558Srgrimes
31558Srgrimes/*
41558Srgrimes * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
51558Srgrimes * All rights reserved.
61558Srgrimes *
71558Srgrimes * Redistribution and use in source and binary forms, with or without
81558Srgrimes * modification, are permitted provided that the following conditions
91558Srgrimes * are met:
101558Srgrimes * 1. Redistributions of source code must retain the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer.
121558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131558Srgrimes *    notice, this list of conditions and the following disclaimer in the
141558Srgrimes *    documentation and/or other materials provided with the distribution.
151558Srgrimes * 3. Neither the name of the project nor the names of its contributors
161558Srgrimes *    may be used to endorse or promote products derived from this software
171558Srgrimes *    without specific prior written permission.
181558Srgrimes *
191558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
201558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
231558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291558Srgrimes * SUCH DAMAGE.
30114589Sobrien */
311558Srgrimes
3238039Scharnier/*
331558Srgrimes * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
341558Srgrimes *
351558Srgrimes * Issues to be discussed:
361558Srgrimes * - Thread safe-ness must be checked.
371558Srgrimes * - Return values.  There are nonstandard return values defined and used
381558Srgrimes *   in the source code.  This is because RFC2553 is silent about which error
39114589Sobrien *   code must be returned for which situation.
4038039Scharnier * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
41114589Sobrien *   invalid.  current code - SEGV on freeaddrinfo(NULL)
42114589Sobrien *
431558Srgrimes * Note:
44252310Shrs * - The code filters out AFs that are not supported by the kernel,
45252310Shrs *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
46252310Shrs *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
47108375Sdillon *   in ai_flags?
48108375Sdillon * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
49252310Shrs *   (1) what should we do against numeric hostname (2) what should we do
50138129Sdas *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
51108375Sdillon *   non-loopback address configured?  global address configured?
5238039Scharnier *
5338039Scharnier * OS specific notes for netbsd/openbsd/freebsd4/bsdi4:
54252310Shrs * - To avoid search order issue, we have a big amount of code duplicate
55252310Shrs *   from gethnamaddr.c and some other places.  The issues that there's no
561558Srgrimes *   lower layer function to lookup "IPv4 or IPv6" record.  Calling
57252310Shrs *   gethostbyname2 from getaddrinfo will end up in wrong search order, as
58252310Shrs *   presented above.
59252310Shrs *
60252310Shrs * OS specific notes for freebsd4:
61252310Shrs * - FreeBSD supported $GAI.  The code does not.
621558Srgrimes */
6378732Sdd
6438039Scharnier#include <sys/cdefs.h>
656661Sphk__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 160552 2006-07-21 19:00:22Z ume $");
661558Srgrimes
67108375Sdillon#include "namespace.h"
68252310Shrs#include <sys/types.h>
69252310Shrs#include <sys/param.h>
70252310Shrs#include <sys/socket.h>
71252310Shrs#include <net/if.h>
72252310Shrs#include <netinet/in.h>
73108375Sdillon#include <sys/queue.h>
74252310Shrs#ifdef INET6
756661Sphk#include <net/if_var.h>
76227081Sed#include <sys/sysctl.h>
77108375Sdillon#include <sys/ioctl.h>
78252310Shrs#include <netinet6/in6_var.h>	/* XXX */
79252310Shrs#endif
806661Sphk#include <arpa/inet.h>
816661Sphk#include <arpa/nameser.h>
821558Srgrimes#include <rpc/rpc.h>
8392806Sobrien#include <rpcsvc/yp_prot.h>
84252310Shrs#include <rpcsvc/ypclnt.h>
85108375Sdillon#include <netdb.h>
86226742Sed#include <resolv.h>
871558Srgrimes#include <string.h>
88252310Shrs#include <stdlib.h>
89226712Ssobomax#include <stddef.h>
901558Srgrimes#include <ctype.h>
91108375Sdillon#include <unistd.h>
92108375Sdillon#include <stdio.h>
93108375Sdillon#include <errno.h>
94108375Sdillon
95108375Sdillon#include "res_config.h"
96108375Sdillon
97108375Sdillon#ifdef DEBUG
98108375Sdillon#include <syslog.h>
991558Srgrimes#endif
100226712Ssobomax
101252310Shrs#include <stdarg.h>
102108375Sdillon#include <nsswitch.h>
103108375Sdillon#include "un-namespace.h"
104108375Sdillon#include "libc_private.h"
105108375Sdillon#ifdef NS_CACHING
106108375Sdillon#include "nscache.h"
107108375Sdillon#endif
108108375Sdillon
109108375Sdillon#if defined(__KAME__) && defined(INET6)
110108375Sdillon# define FAITH
1111558Srgrimes#endif
112108375Sdillon
113108375Sdillon#define SUCCESS 0
114108375Sdillon#define ANY 0
115108375Sdillon#define YES 1
1161558Srgrimes#define NO  0
117108375Sdillon
118108375Sdillonstatic const char in_addrany[] = { 0, 0, 0, 0 };
119108375Sdillonstatic const char in_loopback[] = { 127, 0, 0, 1 };
120108375Sdillon#ifdef INET6
121108375Sdillonstatic const char in6_addrany[] = {
122108375Sdillon	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
123179154Spjd};
124179154Spjdstatic const char in6_loopback[] = {
125108375Sdillon	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
126179154Spjd};
127179154Spjd#endif
128179154Spjd
129179154Spjdstruct policyqueue {
130179154Spjd	TAILQ_ENTRY(policyqueue) pc_entry;
131179154Spjd#ifdef INET6
132108375Sdillon	struct in6_addrpolicy pc_policy;
133108375Sdillon#endif
134108375Sdillon};
135252310ShrsTAILQ_HEAD(policyhead, policyqueue);
136252310Shrs
137252310Shrsstatic const struct afd {
138179154Spjd	int a_af;
139108375Sdillon	int a_addrlen;
140108375Sdillon	socklen_t a_socklen;
141179966Smtm	int a_off;
142179966Smtm	const char *a_addrany;
143179966Smtm	const char *a_loopback;
144179966Smtm	int a_scoped;
145179154Spjd} afdl [] = {
146179154Spjd#ifdef INET6
147108375Sdillon#define	N_INET6 0
148108375Sdillon	{PF_INET6, sizeof(struct in6_addr),
149108375Sdillon	 sizeof(struct sockaddr_in6),
150108375Sdillon	 offsetof(struct sockaddr_in6, sin6_addr),
151108375Sdillon	 in6_addrany, in6_loopback, 1},
152108375Sdillon#define	N_INET 1
153108375Sdillon#else
154108375Sdillon#define	N_INET 0
155108375Sdillon#endif
156226712Ssobomax	{PF_INET, sizeof(struct in_addr),
157226712Ssobomax	 sizeof(struct sockaddr_in),
158226712Ssobomax	 offsetof(struct sockaddr_in, sin_addr),
1591558Srgrimes	 in_addrany, in_loopback, 0},
1601558Srgrimes	{0, 0, 0, 0, NULL, NULL, 0},
161108375Sdillon};
1621558Srgrimes
163108375Sdillonstruct explore {
1641558Srgrimes	int e_af;
1651558Srgrimes	int e_socktype;
166226742Sed	int e_protocol;
167252310Shrs	const char *e_protostr;
168226712Ssobomax	int e_wild;
169226712Ssobomax#define WILD_AF(ex)		((ex)->e_wild & 0x01)
170108375Sdillon#define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
171108375Sdillon#define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
172108375Sdillon};
173108375Sdillon
174108375Sdillonstatic const struct explore explore[] = {
175108375Sdillon#if 0
176108375Sdillon	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
177252310Shrs#endif
178252310Shrs#ifdef INET6
179252310Shrs	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
180252310Shrs	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
181252310Shrs	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
182252310Shrs#endif
183252310Shrs	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
184226742Sed	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
185252310Shrs	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
186108375Sdillon	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
187252310Shrs	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
188252310Shrs	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
189252310Shrs	{ -1, 0, 0, NULL, 0 },
190252310Shrs};
191252310Shrs
192252310Shrs#ifdef INET6
193108375Sdillon#define PTON_MAX	16
194108375Sdillon#else
195108375Sdillon#define PTON_MAX	4
196108375Sdillon#endif
197108375Sdillon
198252310Shrs#define AIO_SRCFLAG_DEPRECATED	0x1
199252310Shrs
200226742Sedstruct ai_order {
201252310Shrs	union {
202252310Shrs		struct sockaddr_storage aiou_ss;
203252310Shrs		struct sockaddr aiou_sa;
204107913Sdillon	} aio_src_un;
205252310Shrs#define aio_srcsa aio_src_un.aiou_sa
206252310Shrs	u_int32_t aio_srcflag;
207252310Shrs	int aio_srcscope;
208108375Sdillon	int aio_dstscope;
2091558Srgrimes	struct policyqueue *aio_srcpolicy;
210108375Sdillon	struct policyqueue *aio_dstpolicy;
211108375Sdillon	struct addrinfo *aio_ai;
212108375Sdillon	int aio_matchlen;
213108375Sdillon};
214108375Sdillon
215108375Sdillonstatic const ns_src default_dns_files[] = {
216226742Sed	{ NSSRC_FILES, 	NS_SUCCESS },
2171558Srgrimes	{ NSSRC_DNS, 	NS_SUCCESS },
2181558Srgrimes	{ 0 }
219252310Shrs};
220252310Shrs
221252310Shrsstruct res_target {
222252310Shrs	struct res_target *next;
223252310Shrs	const char *name;	/* domain name */
224252310Shrs	int qclass, qtype;	/* class and type of query */
225252310Shrs	u_char *answer;		/* buffer to put answer */
226252310Shrs	int anslen;		/* size of answer buffer */
227252310Shrs	int n;			/* result length */
228252310Shrs};
229252310Shrs
230252310Shrs#define MAXPACKET	(64*1024)
231252310Shrs
232252310Shrstypedef union {
233252310Shrs	HEADER hdr;
234252310Shrs	u_char buf[MAXPACKET];
235252310Shrs} querybuf;
236252310Shrs
237252310Shrsstatic int str2number(const char *);
238252310Shrsstatic int explore_null(const struct addrinfo *,
239252310Shrs	const char *, struct addrinfo **);
240252310Shrsstatic int explore_numeric(const struct addrinfo *, const char *,
241252310Shrs	const char *, struct addrinfo **, const char *);
242252310Shrsstatic int explore_numeric_scope(const struct addrinfo *, const char *,
243252310Shrs	const char *, struct addrinfo **);
244252310Shrsstatic int get_canonname(const struct addrinfo *,
245252310Shrs	struct addrinfo *, const char *);
246252310Shrsstatic struct addrinfo *get_ai(const struct addrinfo *,
247252310Shrs	const struct afd *, const char *);
248252310Shrsstatic int get_portmatch(const struct addrinfo *, const char *);
249252310Shrsstatic int get_port(struct addrinfo *, const char *, int);
250252310Shrsstatic const struct afd *find_afd(int);
251252332Shrsstatic int addrconfig(struct addrinfo *);
252252310Shrsstatic void set_source(struct ai_order *, struct policyhead *);
253252310Shrsstatic int comp_dst(const void *, const void *);
254252332Shrs#ifdef INET6
255252332Shrsstatic int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
256252310Shrs#endif
257252310Shrsstatic int gai_addr2scopetype(struct sockaddr *);
258252310Shrs
259252310Shrsstatic int explore_fqdn(const struct addrinfo *, const char *,
260252310Shrs	const char *, struct addrinfo **);
261252310Shrs
262252310Shrsstatic int reorder(struct addrinfo *);
263252310Shrsstatic int get_addrselectpolicy(struct policyhead *);
264252310Shrsstatic void free_addrselectpolicy(struct policyhead *);
265252310Shrsstatic struct policyqueue *match_addrselectpolicy(struct sockaddr *,
266252310Shrs	struct policyhead *);
267252310Shrsstatic int matchlen(struct sockaddr *, struct sockaddr *);
268252310Shrs
269252310Shrsstatic struct addrinfo *getanswer(const querybuf *, int, const char *, int,
270252310Shrs	const struct addrinfo *, res_state);
271252332Shrs#if defined(RESOLVSORT)
272252310Shrsstatic int addr4sort(struct addrinfo *, res_state);
273252310Shrs#endif
274252310Shrsstatic int _dns_getaddrinfo(void *, void *, va_list);
275252310Shrsstatic void _sethtent(FILE **);
276252310Shrsstatic void _endhtent(FILE **);
277252310Shrsstatic struct addrinfo *_gethtent(FILE **, const char *,
278252310Shrs	const struct addrinfo *);
279252310Shrsstatic int _files_getaddrinfo(void *, void *, va_list);
280252310Shrs#ifdef YP
281252332Shrsstatic struct addrinfo *_yphostent(char *, const struct addrinfo *);
282252310Shrsstatic int _yp_getaddrinfo(void *, void *, va_list);
283252310Shrs#endif
284252310Shrs#ifdef NS_CACHING
285252310Shrsstatic int addrinfo_id_func(char *, size_t *, va_list, void *);
286252310Shrsstatic int addrinfo_marshal_func(char *, size_t *, void *, va_list, void *);
287252310Shrsstatic int addrinfo_unmarshal_func(char *, size_t, void *, va_list, void *);
288252310Shrs#endif
289252310Shrs
290252310Shrsstatic int res_queryN(const char *, struct res_target *, res_state);
291252310Shrsstatic int res_searchN(const char *, struct res_target *, res_state);
292252310Shrsstatic int res_querydomainN(const char *, const char *,
293252310Shrs	struct res_target *, res_state);
294252310Shrs
295252310Shrs/* XXX macros that make external reference is BAD. */
296252310Shrs
297252310Shrs#define GET_AI(ai, afd, addr) \
298252310Shrsdo { \
299252310Shrs	/* external reference: pai, error, and label free */ \
300252310Shrs	(ai) = get_ai(pai, (afd), (addr)); \
301252332Shrs	if ((ai) == NULL) { \
302252310Shrs		error = EAI_MEMORY; \
303252310Shrs		goto free; \
304252310Shrs	} \
305252332Shrs} while (/*CONSTCOND*/0)
306252310Shrs
307252310Shrs#define GET_PORT(ai, serv) \
308252310Shrsdo { \
309252310Shrs	/* external reference: error and label free */ \
310252310Shrs	error = get_port((ai), (serv), 0); \
311252310Shrs	if (error != 0) \
312252310Shrs		goto free; \
313252310Shrs} while (/*CONSTCOND*/0)
314252310Shrs
315252310Shrs#define GET_CANONNAME(ai, str) \
316252310Shrsdo { \
317252310Shrs	/* external reference: pai, error and label free */ \
318252310Shrs	error = get_canonname(pai, (ai), (str)); \
319252310Shrs	if (error != 0) \
320252310Shrs		goto free; \
321252310Shrs} while (/*CONSTCOND*/0)
322252310Shrs
323252332Shrs#define ERR(err) \
324252310Shrsdo { \
325252310Shrs	/* external reference: error, and label bad */ \
326252310Shrs	error = (err); \
327252310Shrs	goto bad; \
328252310Shrs	/*NOTREACHED*/ \
329252332Shrs} while (/*CONSTCOND*/0)
330252332Shrs
331252310Shrs#define MATCH_FAMILY(x, y, w) \
332252310Shrs	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
333252310Shrs#define MATCH(x, y, w) \
334252310Shrs	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
335252310Shrs
336252310Shrsvoid
337252310Shrsfreeaddrinfo(struct addrinfo *ai)
338252310Shrs{
339252310Shrs	struct addrinfo *next;
340252310Shrs
341252310Shrs	do {
342252310Shrs		next = ai->ai_next;
343252310Shrs		if (ai->ai_canonname)
344252310Shrs			free(ai->ai_canonname);
345252310Shrs		/* no need to free(ai->ai_addr) */
346252310Shrs		free(ai);
347252310Shrs		ai = next;
348252310Shrs	} while (ai);
349252310Shrs}
350252310Shrs
351252310Shrsstatic int
352252310Shrsstr2number(const char *p)
353252310Shrs{
354252310Shrs	char *ep;
355252310Shrs	unsigned long v;
356252310Shrs
357252310Shrs	if (*p == '\0')
358252310Shrs		return -1;
359252310Shrs	ep = NULL;
360252310Shrs	errno = 0;
361252310Shrs	v = strtoul(p, &ep, 10);
362252310Shrs	if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)
363252310Shrs		return v;
364252310Shrs	else
365252310Shrs		return -1;
366252310Shrs}
367252310Shrs
368252310Shrsint
369252310Shrsgetaddrinfo(const char *hostname, const char *servname,
370252310Shrs    const struct addrinfo *hints, struct addrinfo **res)
371252310Shrs{
372252310Shrs	struct addrinfo sentinel;
373252310Shrs	struct addrinfo *cur;
374252310Shrs	int error = 0;
375252310Shrs	struct addrinfo ai;
376252310Shrs	struct addrinfo ai0;
377252310Shrs	struct addrinfo *pai;
378252310Shrs	const struct explore *ex;
379252310Shrs	int numeric = 0;
380252310Shrs
381252310Shrs	memset(&sentinel, 0, sizeof(sentinel));
382252310Shrs	cur = &sentinel;
383252310Shrs	pai = &ai;
384252310Shrs	pai->ai_flags = 0;
385252310Shrs	pai->ai_family = PF_UNSPEC;
386252310Shrs	pai->ai_socktype = ANY;
387252310Shrs	pai->ai_protocol = ANY;
388252310Shrs	pai->ai_addrlen = 0;
389252310Shrs	pai->ai_canonname = NULL;
390252310Shrs	pai->ai_addr = NULL;
391252310Shrs	pai->ai_next = NULL;
392252310Shrs
393252310Shrs	if (hostname == NULL && servname == NULL)
394252310Shrs		return EAI_NONAME;
395252310Shrs	if (hints) {
396252310Shrs		/* error check for hints */
397252310Shrs		if (hints->ai_addrlen || hints->ai_canonname ||
398252310Shrs		    hints->ai_addr || hints->ai_next)
399252310Shrs			ERR(EAI_BADHINTS); /* xxx */
400252332Shrs		if (hints->ai_flags & ~AI_MASK)
401252310Shrs			ERR(EAI_BADFLAGS);
402252310Shrs		switch (hints->ai_family) {
403252310Shrs		case PF_UNSPEC:
404252310Shrs		case PF_INET:
405252310Shrs#ifdef INET6
406252310Shrs		case PF_INET6:
407252310Shrs#endif
408252310Shrs			break;
409252310Shrs		default:
410252310Shrs			ERR(EAI_FAMILY);
411252310Shrs		}
412252310Shrs		memcpy(pai, hints, sizeof(*pai));
413252310Shrs
414252310Shrs		/*
415252310Shrs		 * if both socktype/protocol are specified, check if they
416252310Shrs		 * are meaningful combination.
417252310Shrs		 */
418252310Shrs		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
419252310Shrs			for (ex = explore; ex->e_af >= 0; ex++) {
420252310Shrs				if (pai->ai_family != ex->e_af)
421252310Shrs					continue;
422252310Shrs				if (ex->e_socktype == ANY)
423252310Shrs					continue;
424252310Shrs				if (ex->e_protocol == ANY)
425252310Shrs					continue;
426252310Shrs				if (pai->ai_socktype == ex->e_socktype &&
427252310Shrs				    pai->ai_protocol != ex->e_protocol) {
428252310Shrs					ERR(EAI_BADHINTS);
429252310Shrs				}
430252310Shrs			}
431252310Shrs		}
432252310Shrs	}
433252310Shrs
434252310Shrs	/*
435252310Shrs	 * post-2553: AI_ALL and AI_V4MAPPED are effective only against
436252310Shrs	 * AF_INET6 query.  They need to be ignored if specified in other
437252310Shrs	 * occassions.
438252310Shrs	 */
439252310Shrs	switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) {
440252310Shrs	case AI_V4MAPPED:
441252310Shrs	case AI_ALL | AI_V4MAPPED:
442252310Shrs		if (pai->ai_family != AF_INET6)
443252310Shrs			pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
444252310Shrs		break;
445252310Shrs	case AI_ALL:
446252310Shrs#if 1
447252310Shrs		/* illegal */
448252310Shrs		ERR(EAI_BADFLAGS);
449252310Shrs#else
450252310Shrs		pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED);
451252310Shrs#endif
452252310Shrs		break;
453252310Shrs	}
454252310Shrs
455252310Shrs	/*
456252310Shrs	 * check for special cases.  (1) numeric servname is disallowed if
457252310Shrs	 * socktype/protocol are left unspecified. (2) servname is disallowed
458252310Shrs	 * for raw and other inet{,6} sockets.
459252310Shrs	 */
460252310Shrs	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
461252310Shrs#ifdef PF_INET6
462252310Shrs	    || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
463252310Shrs#endif
464252310Shrs	    ) {
465252310Shrs		ai0 = *pai;	/* backup *pai */
466252310Shrs
467252310Shrs		if (pai->ai_family == PF_UNSPEC) {
468252310Shrs#ifdef PF_INET6
469252310Shrs			pai->ai_family = PF_INET6;
470252310Shrs#else
471252310Shrs			pai->ai_family = PF_INET;
472252310Shrs#endif
473252310Shrs		}
474252310Shrs		error = get_portmatch(pai, servname);
475252310Shrs		if (error)
476252310Shrs			ERR(error);
477252310Shrs
478252310Shrs		*pai = ai0;
479252310Shrs	}
480252310Shrs
481252310Shrs	ai0 = *pai;
482252310Shrs
483252310Shrs	/* NULL hostname, or numeric hostname */
484252310Shrs	for (ex = explore; ex->e_af >= 0; ex++) {
485252310Shrs		*pai = ai0;
486252310Shrs
487252310Shrs		/* PF_UNSPEC entries are prepared for DNS queries only */
488252310Shrs		if (ex->e_af == PF_UNSPEC)
489252310Shrs			continue;
490252310Shrs
491252310Shrs		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
492252310Shrs			continue;
493252310Shrs		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
494252310Shrs			continue;
495252310Shrs		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
496252310Shrs			continue;
497252310Shrs
498252310Shrs		if (pai->ai_family == PF_UNSPEC)
499252310Shrs			pai->ai_family = ex->e_af;
500252310Shrs		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
501252310Shrs			pai->ai_socktype = ex->e_socktype;
502252310Shrs		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
503252310Shrs			pai->ai_protocol = ex->e_protocol;
504252310Shrs
505252310Shrs		if (hostname == NULL)
506252310Shrs			error = explore_null(pai, servname, &cur->ai_next);
507252310Shrs		else
508252310Shrs			error = explore_numeric_scope(pai, hostname, servname,
509252310Shrs			    &cur->ai_next);
510252310Shrs
511252310Shrs		if (error)
512252310Shrs			goto free;
513252310Shrs
514252310Shrs		while (cur && cur->ai_next)
515252310Shrs			cur = cur->ai_next;
516252310Shrs	}
517252310Shrs
518252310Shrs	/*
519252310Shrs	 * XXX
520252310Shrs	 * If numreic representation of AF1 can be interpreted as FQDN
521252310Shrs	 * representation of AF2, we need to think again about the code below.
522252310Shrs	 */
523252310Shrs	if (sentinel.ai_next) {
524252310Shrs		numeric = 1;
525252310Shrs		goto good;
526252310Shrs	}
527252310Shrs
528252310Shrs	if (hostname == NULL)
529252310Shrs		ERR(EAI_NONAME);	/* used to be EAI_NODATA */
530252310Shrs	if (pai->ai_flags & AI_NUMERICHOST)
531252310Shrs		ERR(EAI_NONAME);
532252310Shrs
533252310Shrs	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0))
534252310Shrs		ERR(EAI_FAIL);
535252310Shrs
536252310Shrs	/*
537252310Shrs	 * hostname as alphabetical name.
538252310Shrs	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
539252310Shrs	 * outer loop by AFs.
540252310Shrs	 */
541252310Shrs	for (ex = explore; ex->e_af >= 0; ex++) {
542252310Shrs		*pai = ai0;
543252310Shrs
544252310Shrs		/* require exact match for family field */
545252310Shrs		if (pai->ai_family != ex->e_af)
546252310Shrs			continue;
547252310Shrs
548252310Shrs		if (!MATCH(pai->ai_socktype, ex->e_socktype,
549252310Shrs				WILD_SOCKTYPE(ex))) {
550252310Shrs			continue;
551252310Shrs		}
552252310Shrs		if (!MATCH(pai->ai_protocol, ex->e_protocol,
553252310Shrs				WILD_PROTOCOL(ex))) {
554252310Shrs			continue;
555252310Shrs		}
556252310Shrs
557252310Shrs		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
558252310Shrs			pai->ai_socktype = ex->e_socktype;
559252310Shrs		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
560252310Shrs			pai->ai_protocol = ex->e_protocol;
561252310Shrs
562252310Shrs		error = explore_fqdn(pai, hostname, servname,
563252310Shrs			&cur->ai_next);
564252310Shrs
565252310Shrs		while (cur && cur->ai_next)
566252310Shrs			cur = cur->ai_next;
567252310Shrs	}
568252310Shrs
569252310Shrs	/* XXX inhibit errors if we have the result */
570252310Shrs	if (sentinel.ai_next)
571252310Shrs		error = 0;
572252310Shrs
573252310Shrsgood:
574252310Shrs	/*
575252310Shrs	 * ensure we return either:
576252310Shrs	 * - error == 0, non-NULL *res
577252310Shrs	 * - error != 0, NULL *res
578252310Shrs	 */
579252310Shrs	if (error == 0) {
580252310Shrs		if (sentinel.ai_next) {
581252310Shrs			/*
582252310Shrs			 * If the returned entry is for an active connection,
583252310Shrs			 * and the given name is not numeric, reorder the
584252310Shrs			 * list, so that the application would try the list
585252310Shrs			 * in the most efficient order.
586252310Shrs			 */
587252310Shrs			if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) {
588252310Shrs				if (!numeric)
589252310Shrs					(void)reorder(&sentinel);
590252310Shrs			}
591252310Shrs			*res = sentinel.ai_next;
592252310Shrs			return SUCCESS;
593252310Shrs		} else
594252310Shrs			error = EAI_FAIL;
595252310Shrs	}
596252310Shrsfree:
597252310Shrsbad:
598252310Shrs	if (sentinel.ai_next)
599252310Shrs		freeaddrinfo(sentinel.ai_next);
600252310Shrs	*res = NULL;
601252310Shrs	return error;
602252310Shrs}
603252310Shrs
604252310Shrsstatic int
605252310Shrsreorder(struct addrinfo *sentinel)
606252310Shrs{
607252310Shrs	struct addrinfo *ai, **aip;
608252310Shrs	struct ai_order *aio;
609252310Shrs	int i, n;
610252310Shrs	struct policyhead policyhead;
611252310Shrs
612252310Shrs	/* count the number of addrinfo elements for sorting. */
613252310Shrs	for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++)
614252310Shrs		;
615252310Shrs
616252310Shrs	/*
617252310Shrs	 * If the number is small enough, we can skip the reordering process.
618252310Shrs	 */
619252310Shrs	if (n <= 1)
620252310Shrs		return(n);
621108375Sdillon
622252310Shrs	/* allocate a temporary array for sort and initialization of it. */
6231558Srgrimes	if ((aio = malloc(sizeof(*aio) * n)) == NULL)
624252310Shrs		return(n);	/* give up reordering */
625252310Shrs	memset(aio, 0, sizeof(*aio) * n);
626252310Shrs
627252310Shrs	/* retrieve address selection policy from the kernel */
628252310Shrs	TAILQ_INIT(&policyhead);
629252310Shrs	if (!get_addrselectpolicy(&policyhead)) {
630252310Shrs		/* no policy is installed into kernel, we don't sort. */
631252310Shrs		free(aio);
632252310Shrs		return (n);
633252310Shrs	}
634252310Shrs
635252310Shrs	for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) {
636252310Shrs		aio[i].aio_ai = ai;
637252310Shrs		aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr);
638252310Shrs		aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr,
639252310Shrs							      &policyhead);
640252310Shrs		set_source(&aio[i], &policyhead);
641252310Shrs	}
642252310Shrs
643252310Shrs	/* perform sorting. */
644252310Shrs	qsort(aio, n, sizeof(*aio), comp_dst);
645252310Shrs
646252310Shrs	/* reorder the addrinfo chain. */
647252310Shrs	for (i = 0, aip = &sentinel->ai_next; i < n; i++) {
648252310Shrs		*aip = aio[i].aio_ai;
649252310Shrs		aip = &aio[i].aio_ai->ai_next;
650252310Shrs	}
651252310Shrs	*aip = NULL;
652252310Shrs
653252310Shrs	/* cleanup and return */
654252310Shrs	free(aio);
655252310Shrs	free_addrselectpolicy(&policyhead);
656252310Shrs	return(n);
657252310Shrs}
658252310Shrs
659252310Shrsstatic int
660252310Shrsget_addrselectpolicy(struct policyhead *head)
661252310Shrs{
662252310Shrs#ifdef INET6
663252310Shrs	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
664252310Shrs	size_t l;
665252310Shrs	char *buf;
666252310Shrs	struct in6_addrpolicy *pol, *ep;
667252310Shrs
668252310Shrs	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0)
669252310Shrs		return (0);
670252310Shrs	if ((buf = malloc(l)) == NULL)
671252310Shrs		return (0);
672252310Shrs	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
673252310Shrs		free(buf);
674252310Shrs		return (0);
675252310Shrs	}
676252310Shrs
677252310Shrs	ep = (struct in6_addrpolicy *)(buf + l);
678252310Shrs	for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) {
679252310Shrs		struct policyqueue *new;
680252310Shrs
681252310Shrs		if ((new = malloc(sizeof(*new))) == NULL) {
682252310Shrs			free_addrselectpolicy(head); /* make the list empty */
683252310Shrs			break;
684252310Shrs		}
685252310Shrs		new->pc_policy = *pol;
686252310Shrs		TAILQ_INSERT_TAIL(head, new, pc_entry);
687252310Shrs	}
688252310Shrs
689252310Shrs	free(buf);
690252310Shrs	return (1);
691252310Shrs#else
692252310Shrs	return (0);
693252310Shrs#endif
694252310Shrs}
695252310Shrs
696252310Shrsstatic void
697252310Shrsfree_addrselectpolicy(struct policyhead *head)
698252310Shrs{
699252310Shrs	struct policyqueue *ent, *nent;
700252310Shrs
701252310Shrs	for (ent = TAILQ_FIRST(head); ent; ent = nent) {
702252310Shrs		nent = TAILQ_NEXT(ent, pc_entry);
703252310Shrs		TAILQ_REMOVE(head, ent, pc_entry);
704252310Shrs		free(ent);
705252310Shrs	}
706252310Shrs}
707252310Shrs
708252310Shrsstatic struct policyqueue *
709252310Shrsmatch_addrselectpolicy(struct sockaddr *addr, struct policyhead *head)
710252310Shrs{
711252310Shrs#ifdef INET6
7121558Srgrimes	struct policyqueue *ent, *bestent = NULL;
7131558Srgrimes	struct in6_addrpolicy *pol;
714111424Sdas	int matchlen, bestmatchlen = -1;
715252310Shrs	u_char *mp, *ep, *k, *p, m;
7161558Srgrimes	struct sockaddr_in6 key;
717111424Sdas
718111424Sdas	switch(addr->sa_family) {
719111424Sdas	case AF_INET6:
720111424Sdas		key = *(struct sockaddr_in6 *)addr;
721111424Sdas		break;
722111424Sdas	case AF_INET:
7231558Srgrimes		/* convert the address into IPv4-mapped IPv6 address. */
72426740Scharnier		memset(&key, 0, sizeof(key));
7251558Srgrimes		key.sin6_family = AF_INET6;
7261558Srgrimes		key.sin6_len = sizeof(key);
727252310Shrs		key.sin6_addr.s6_addr[10] = 0xff;
7281558Srgrimes		key.sin6_addr.s6_addr[11] = 0xff;
729252310Shrs		memcpy(&key.sin6_addr.s6_addr[12],
7301558Srgrimes		       &((struct sockaddr_in *)addr)->sin_addr, 4);
7311558Srgrimes		break;
73226740Scharnier	default:
733108375Sdillon		return(NULL);
7341558Srgrimes	}
735108375Sdillon
736108375Sdillon	for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) {
737141611Sru		pol = &ent->pc_policy;
738108375Sdillon		matchlen = 0;
739252310Shrs
740108375Sdillon		mp = (u_char *)&pol->addrmask.sin6_addr;
741108375Sdillon		ep = mp + 16;	/* XXX: scope field? */
742179154Spjd		k = (u_char *)&key.sin6_addr;
743108375Sdillon		p = (u_char *)&pol->addr.sin6_addr;
744108375Sdillon		for (; mp < ep && *mp; mp++, k++, p++) {
7451558Srgrimes			m = *mp;
7461558Srgrimes			if ((*k & m) != *p)
747107913Sdillon				goto next; /* not match */
748108375Sdillon			if (m == 0xff) /* short cut for a typical case */
749179154Spjd				matchlen += 8;
750179154Spjd			else {
751179154Spjd				while (m >= 0x80) {
752179154Spjd					matchlen++;
753179154Spjd					m <<= 1;
754179154Spjd				}
755179154Spjd			}
756179154Spjd		}
757179154Spjd
758179154Spjd		/* matched.  check if this is better than the current best. */
759179154Spjd		if (matchlen > bestmatchlen) {
760179154Spjd			bestent = ent;
761179154Spjd			bestmatchlen = matchlen;
762179154Spjd		}
763179154Spjd
764179154Spjd	  next:
765108375Sdillon		continue;
766107913Sdillon	}
767108375Sdillon
768108375Sdillon	return(bestent);
769108425Smike#else
770108375Sdillon	return(NULL);
771108375Sdillon#endif
772108375Sdillon
773108375Sdillon}
774108375Sdillon
775179154Spjdstatic void
776108375Sdillonset_source(struct ai_order *aio, struct policyhead *ph)
777108375Sdillon{
778108375Sdillon	struct addrinfo ai = *aio->aio_ai;
779179154Spjd	struct sockaddr_storage ss;
780179154Spjd	socklen_t srclen;
781179154Spjd	int s;
782179154Spjd
783179154Spjd	/* set unspec ("no source is available"), just in case */
784179154Spjd	aio->aio_srcsa.sa_family = AF_UNSPEC;
785179154Spjd	aio->aio_srcscope = -1;
786179154Spjd
787179154Spjd	switch(ai.ai_family) {
788179154Spjd	case AF_INET:
789108375Sdillon#ifdef INET6
790108375Sdillon	case AF_INET6:
791179154Spjd#endif
792108375Sdillon		break;
793108375Sdillon	default:		/* ignore unsupported AFs explicitly */
794108375Sdillon		return;
795108375Sdillon	}
796179154Spjd
797108375Sdillon	/* XXX: make a dummy addrinfo to call connect() */
798108375Sdillon	ai.ai_socktype = SOCK_DGRAM;
799108375Sdillon	ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */
800108459Smike	ai.ai_next = NULL;
801179154Spjd	memset(&ss, 0, sizeof(ss));
802108375Sdillon	memcpy(&ss, ai.ai_addr, ai.ai_addrlen);
803108375Sdillon	ai.ai_addr = (struct sockaddr *)&ss;
804108375Sdillon	get_port(&ai, "1", 0);
805108375Sdillon
806108375Sdillon	/* open a socket to get the source address for the given dst */
807108375Sdillon	if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0)
808108375Sdillon		return;		/* give up */
809108375Sdillon	if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0)
810108375Sdillon		goto cleanup;
811108375Sdillon	srclen = ai.ai_addrlen;
812108375Sdillon	if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) {
813108375Sdillon		aio->aio_srcsa.sa_family = AF_UNSPEC;
814108375Sdillon		goto cleanup;
815108375Sdillon	}
816108375Sdillon	aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa);
817108375Sdillon	aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph);
818108375Sdillon	aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr);
819126643Smarkm#ifdef INET6
820108375Sdillon	if (ai.ai_family == AF_INET6) {
821108375Sdillon		struct in6_ifreq ifr6;
822108375Sdillon		u_int32_t flags6;
823108375Sdillon
824179154Spjd		/* XXX: interface name should not be hardcoded */
825179154Spjd		strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name));
826108375Sdillon		memset(&ifr6, 0, sizeof(ifr6));
827108375Sdillon		memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen);
828108375Sdillon		if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) {
829179154Spjd			flags6 = ifr6.ifr_ifru.ifru_flags6;
830179154Spjd			if ((flags6 & IN6_IFF_DEPRECATED))
831179154Spjd				aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED;
832179154Spjd		}
833179154Spjd	}
834179154Spjd#endif
835179154Spjd
836108375Sdillon  cleanup:
837108375Sdillon	_close(s);
838108375Sdillon	return;
839108375Sdillon}
840108375Sdillon
841108375Sdillonstatic int
842179154Spjdmatchlen(struct sockaddr *src, struct sockaddr *dst)
843179154Spjd{
844179154Spjd	int match = 0;
845179154Spjd	u_char *s, *d;
846108375Sdillon	u_char *lim, r;
847108375Sdillon	int addrlen;
848107913Sdillon
849	switch (src->sa_family) {
850#ifdef INET6
851	case AF_INET6:
852		s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr;
853		d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr;
854		addrlen = sizeof(struct in6_addr);
855		lim = s + addrlen;
856		break;
857#endif
858	case AF_INET:
859		s = (u_char *)&((struct sockaddr_in *)src)->sin_addr;
860		d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr;
861		addrlen = sizeof(struct in_addr);
862		lim = s + addrlen;
863		break;
864	default:
865		return(0);
866	}
867
868	while (s < lim)
869		if ((r = (*d++ ^ *s++)) != 0) {
870			while (r < addrlen * 8) {
871				match++;
872				r <<= 1;
873			}
874			break;
875		} else
876			match += 8;
877	return(match);
878}
879
880static int
881comp_dst(const void *arg1, const void *arg2)
882{
883	const struct ai_order *dst1 = arg1, *dst2 = arg2;
884
885	/*
886	 * Rule 1: Avoid unusable destinations.
887	 * XXX: we currently do not consider if an appropriate route exists.
888	 */
889	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
890	    dst2->aio_srcsa.sa_family == AF_UNSPEC) {
891		return(-1);
892	}
893	if (dst1->aio_srcsa.sa_family == AF_UNSPEC &&
894	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
895		return(1);
896	}
897
898	/* Rule 2: Prefer matching scope. */
899	if (dst1->aio_dstscope == dst1->aio_srcscope &&
900	    dst2->aio_dstscope != dst2->aio_srcscope) {
901		return(-1);
902	}
903	if (dst1->aio_dstscope != dst1->aio_srcscope &&
904	    dst2->aio_dstscope == dst2->aio_srcscope) {
905		return(1);
906	}
907
908	/* Rule 3: Avoid deprecated addresses. */
909	if (dst1->aio_srcsa.sa_family != AF_UNSPEC &&
910	    dst2->aio_srcsa.sa_family != AF_UNSPEC) {
911		if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
912		    (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
913			return(-1);
914		}
915		if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) &&
916		    !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) {
917			return(1);
918		}
919	}
920
921	/* Rule 4: Prefer home addresses. */
922	/* XXX: not implemented yet */
923
924	/* Rule 5: Prefer matching label. */
925#ifdef INET6
926	if (dst1->aio_srcpolicy && dst1->aio_dstpolicy &&
927	    dst1->aio_srcpolicy->pc_policy.label ==
928	    dst1->aio_dstpolicy->pc_policy.label &&
929	    (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL ||
930	     dst2->aio_srcpolicy->pc_policy.label !=
931	     dst2->aio_dstpolicy->pc_policy.label)) {
932		return(-1);
933	}
934	if (dst2->aio_srcpolicy && dst2->aio_dstpolicy &&
935	    dst2->aio_srcpolicy->pc_policy.label ==
936	    dst2->aio_dstpolicy->pc_policy.label &&
937	    (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL ||
938	     dst1->aio_srcpolicy->pc_policy.label !=
939	     dst1->aio_dstpolicy->pc_policy.label)) {
940		return(1);
941	}
942#endif
943
944	/* Rule 6: Prefer higher precedence. */
945#ifdef INET6
946	if (dst1->aio_dstpolicy &&
947	    (dst2->aio_dstpolicy == NULL ||
948	     dst1->aio_dstpolicy->pc_policy.preced >
949	     dst2->aio_dstpolicy->pc_policy.preced)) {
950		return(-1);
951	}
952	if (dst2->aio_dstpolicy &&
953	    (dst1->aio_dstpolicy == NULL ||
954	     dst2->aio_dstpolicy->pc_policy.preced >
955	     dst1->aio_dstpolicy->pc_policy.preced)) {
956		return(1);
957	}
958#endif
959
960	/* Rule 7: Prefer native transport. */
961	/* XXX: not implemented yet */
962
963	/* Rule 8: Prefer smaller scope. */
964	if (dst1->aio_dstscope >= 0 &&
965	    dst1->aio_dstscope < dst2->aio_dstscope) {
966		return(-1);
967	}
968	if (dst2->aio_dstscope >= 0 &&
969	    dst2->aio_dstscope < dst1->aio_dstscope) {
970		return(1);
971	}
972
973	/*
974	 * Rule 9: Use longest matching prefix.
975	 * We compare the match length in a same AF only.
976	 */
977	if (dst1->aio_ai->ai_addr->sa_family ==
978	    dst2->aio_ai->ai_addr->sa_family) {
979		if (dst1->aio_matchlen > dst2->aio_matchlen) {
980			return(-1);
981		}
982		if (dst1->aio_matchlen < dst2->aio_matchlen) {
983			return(1);
984		}
985	}
986
987	/* Rule 10: Otherwise, leave the order unchanged. */
988	return(-1);
989}
990
991/*
992 * Copy from scope.c.
993 * XXX: we should standardize the functions and link them as standard
994 * library.
995 */
996static int
997gai_addr2scopetype(struct sockaddr *sa)
998{
999#ifdef INET6
1000	struct sockaddr_in6 *sa6;
1001#endif
1002	struct sockaddr_in *sa4;
1003
1004	switch(sa->sa_family) {
1005#ifdef INET6
1006	case AF_INET6:
1007		sa6 = (struct sockaddr_in6 *)sa;
1008		if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
1009			/* just use the scope field of the multicast address */
1010			return(sa6->sin6_addr.s6_addr[2] & 0x0f);
1011		}
1012		/*
1013		 * Unicast addresses: map scope type to corresponding scope
1014		 * value defined for multcast addresses.
1015		 * XXX: hardcoded scope type values are bad...
1016		 */
1017		if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
1018			return(1); /* node local scope */
1019		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
1020			return(2); /* link-local scope */
1021		if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr))
1022			return(5); /* site-local scope */
1023		return(14);	/* global scope */
1024		break;
1025#endif
1026	case AF_INET:
1027		/*
1028		 * IPv4 pseudo scoping according to RFC 3484.
1029		 */
1030		sa4 = (struct sockaddr_in *)sa;
1031		/* IPv4 autoconfiguration addresses have link-local scope. */
1032		if (((u_char *)&sa4->sin_addr)[0] == 169 &&
1033		    ((u_char *)&sa4->sin_addr)[1] == 254)
1034			return(2);
1035		/* Private addresses have site-local scope. */
1036		if (((u_char *)&sa4->sin_addr)[0] == 10 ||
1037		    (((u_char *)&sa4->sin_addr)[0] == 172 &&
1038		     (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) ||
1039		    (((u_char *)&sa4->sin_addr)[0] == 192 &&
1040		     ((u_char *)&sa4->sin_addr)[1] == 168))
1041			return(14);	/* XXX: It should be 5 unless NAT */
1042		/* Loopback addresses have link-local scope. */
1043		if (((u_char *)&sa4->sin_addr)[0] == 127)
1044			return(2);
1045		return(14);
1046		break;
1047	default:
1048		errno = EAFNOSUPPORT; /* is this a good error? */
1049		return(-1);
1050	}
1051}
1052
1053/*
1054 * hostname == NULL.
1055 * passive socket -> anyaddr (0.0.0.0 or ::)
1056 * non-passive socket -> localhost (127.0.0.1 or ::1)
1057 */
1058static int
1059explore_null(const struct addrinfo *pai, const char *servname,
1060    struct addrinfo **res)
1061{
1062	int s;
1063	const struct afd *afd;
1064	struct addrinfo *ai;
1065	int error;
1066
1067	*res = NULL;
1068	ai = NULL;
1069
1070	/*
1071	 * filter out AFs that are not supported by the kernel
1072	 * XXX errno?
1073	 */
1074	s = _socket(pai->ai_family, SOCK_DGRAM, 0);
1075	if (s < 0) {
1076		if (errno != EMFILE)
1077			return 0;
1078	} else
1079		_close(s);
1080
1081	/*
1082	 * if the servname does not match socktype/protocol, ignore it.
1083	 */
1084	if (get_portmatch(pai, servname) != 0)
1085		return 0;
1086
1087	afd = find_afd(pai->ai_family);
1088	if (afd == NULL)
1089		return 0;
1090
1091	if (pai->ai_flags & AI_PASSIVE) {
1092		GET_AI(ai, afd, afd->a_addrany);
1093		GET_PORT(ai, servname);
1094	} else {
1095		GET_AI(ai, afd, afd->a_loopback);
1096		GET_PORT(ai, servname);
1097	}
1098
1099	*res = ai;
1100	return 0;
1101
1102free:
1103	if (ai != NULL)
1104		freeaddrinfo(ai);
1105	return error;
1106}
1107
1108/*
1109 * numeric hostname
1110 */
1111static int
1112explore_numeric(const struct addrinfo *pai, const char *hostname,
1113    const char *servname, struct addrinfo **res, const char *canonname)
1114{
1115	const struct afd *afd;
1116	struct addrinfo *ai;
1117	int error;
1118	char pton[PTON_MAX];
1119
1120	*res = NULL;
1121	ai = NULL;
1122
1123	/*
1124	 * if the servname does not match socktype/protocol, ignore it.
1125	 */
1126	if (get_portmatch(pai, servname) != 0)
1127		return 0;
1128
1129	afd = find_afd(pai->ai_family);
1130	if (afd == NULL)
1131		return 0;
1132
1133	switch (afd->a_af) {
1134	case AF_INET:
1135		/*
1136		 * RFC3493 requires getaddrinfo() to accept AF_INET formats
1137		 * that are accepted by inet_addr() and its family.  The
1138		 * accepted forms includes the "classful" one, which inet_pton
1139		 * does not accept.  So we need to separate the case for
1140		 * AF_INET.
1141		 */
1142		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
1143			if (pai->ai_family == afd->a_af ||
1144			    pai->ai_family == PF_UNSPEC /*?*/) {
1145				GET_AI(ai, afd, pton);
1146				GET_PORT(ai, servname);
1147				if ((pai->ai_flags & AI_CANONNAME)) {
1148					/*
1149					 * Set the numeric address itself as
1150					 * the canonical name, based on a
1151					 * clarification in rfc3493.
1152					 */
1153					GET_CANONNAME(ai, canonname);
1154				}
1155			} else
1156				ERR(EAI_FAMILY);	/*xxx*/
1157		}
1158		break;
1159	default:
1160		if (inet_pton(afd->a_af, hostname, pton) == 1) {
1161			if (pai->ai_family == afd->a_af ||
1162			    pai->ai_family == PF_UNSPEC /*?*/) {
1163				GET_AI(ai, afd, pton);
1164				GET_PORT(ai, servname);
1165				if ((pai->ai_flags & AI_CANONNAME)) {
1166					/*
1167					 * Set the numeric address itself as
1168					 * the canonical name, based on a
1169					 * clarification in rfc3493.
1170					 */
1171					GET_CANONNAME(ai, canonname);
1172				}
1173			} else
1174				ERR(EAI_FAMILY);	/* XXX */
1175		}
1176		break;
1177	}
1178
1179	*res = ai;
1180	return 0;
1181
1182free:
1183bad:
1184	if (ai != NULL)
1185		freeaddrinfo(ai);
1186	return error;
1187}
1188
1189/*
1190 * numeric hostname with scope
1191 */
1192static int
1193explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
1194    const char *servname, struct addrinfo **res)
1195{
1196#if !defined(SCOPE_DELIMITER) || !defined(INET6)
1197	return explore_numeric(pai, hostname, servname, res, hostname);
1198#else
1199	const struct afd *afd;
1200	struct addrinfo *cur;
1201	int error;
1202	char *cp, *hostname2 = NULL, *scope, *addr;
1203	struct sockaddr_in6 *sin6;
1204
1205	/*
1206	 * if the servname does not match socktype/protocol, ignore it.
1207	 */
1208	if (get_portmatch(pai, servname) != 0)
1209		return 0;
1210
1211	afd = find_afd(pai->ai_family);
1212	if (afd == NULL)
1213		return 0;
1214
1215	if (!afd->a_scoped)
1216		return explore_numeric(pai, hostname, servname, res, hostname);
1217
1218	cp = strchr(hostname, SCOPE_DELIMITER);
1219	if (cp == NULL)
1220		return explore_numeric(pai, hostname, servname, res, hostname);
1221
1222	/*
1223	 * Handle special case of <scoped_address><delimiter><scope id>
1224	 */
1225	hostname2 = strdup(hostname);
1226	if (hostname2 == NULL)
1227		return EAI_MEMORY;
1228	/* terminate at the delimiter */
1229	hostname2[cp - hostname] = '\0';
1230	addr = hostname2;
1231	scope = cp + 1;
1232
1233	error = explore_numeric(pai, addr, servname, res, hostname);
1234	if (error == 0) {
1235		u_int32_t scopeid;
1236
1237		for (cur = *res; cur; cur = cur->ai_next) {
1238			if (cur->ai_family != AF_INET6)
1239				continue;
1240			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
1241			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
1242				free(hostname2);
1243				return(EAI_NONAME); /* XXX: is return OK? */
1244			}
1245			sin6->sin6_scope_id = scopeid;
1246		}
1247	}
1248
1249	free(hostname2);
1250
1251	return error;
1252#endif
1253}
1254
1255static int
1256get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
1257{
1258	if ((pai->ai_flags & AI_CANONNAME) != 0) {
1259		ai->ai_canonname = strdup(str);
1260		if (ai->ai_canonname == NULL)
1261			return EAI_MEMORY;
1262	}
1263	return 0;
1264}
1265
1266static struct addrinfo *
1267get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
1268{
1269	char *p;
1270	struct addrinfo *ai;
1271#ifdef FAITH
1272	struct in6_addr faith_prefix;
1273	char *fp_str;
1274	int translate = 0;
1275#endif
1276
1277#ifdef FAITH
1278	/*
1279	 * Transfrom an IPv4 addr into a special IPv6 addr format for
1280	 * IPv6->IPv4 translation gateway. (only TCP is supported now)
1281	 *
1282	 * +-----------------------------------+------------+
1283	 * | faith prefix part (12 bytes)      | embedded   |
1284	 * |                                   | IPv4 addr part (4 bytes)
1285	 * +-----------------------------------+------------+
1286	 *
1287	 * faith prefix part is specified as ascii IPv6 addr format
1288	 * in environmental variable GAI.
1289	 * For FAITH to work correctly, routing to faith prefix must be
1290	 * setup toward a machine where a FAITH daemon operates.
1291	 * Also, the machine must enable some mechanizm
1292	 * (e.g. faith interface hack) to divert those packet with
1293	 * faith prefixed destination addr to user-land FAITH daemon.
1294	 */
1295	fp_str = getenv("GAI");
1296	if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 &&
1297	    afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) {
1298		u_int32_t v4a;
1299		u_int8_t v4a_top;
1300
1301		memcpy(&v4a, addr, sizeof v4a);
1302		v4a_top = v4a >> IN_CLASSA_NSHIFT;
1303		if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) &&
1304		    v4a_top != 0 && v4a != IN_LOOPBACKNET) {
1305			afd = &afdl[N_INET6];
1306			memcpy(&faith_prefix.s6_addr[12], addr,
1307			       sizeof(struct in_addr));
1308			translate = 1;
1309		}
1310	}
1311#endif
1312
1313	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
1314		+ (afd->a_socklen));
1315	if (ai == NULL)
1316		return NULL;
1317
1318	memcpy(ai, pai, sizeof(struct addrinfo));
1319	ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);
1320	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
1321	ai->ai_addr->sa_len = afd->a_socklen;
1322	ai->ai_addrlen = afd->a_socklen;
1323	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
1324	p = (char *)(void *)(ai->ai_addr);
1325#ifdef FAITH
1326	if (translate == 1)
1327		memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen);
1328	else
1329#endif
1330	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
1331	return ai;
1332}
1333
1334static int
1335get_portmatch(const struct addrinfo *ai, const char *servname)
1336{
1337
1338	/* get_port does not touch first argument when matchonly == 1. */
1339	/* LINTED const cast */
1340	return get_port((struct addrinfo *)ai, servname, 1);
1341}
1342
1343static int
1344get_port(struct addrinfo *ai, const char *servname, int matchonly)
1345{
1346	const char *proto;
1347	struct servent *sp;
1348	int port;
1349	int allownumeric;
1350
1351	if (servname == NULL)
1352		return 0;
1353	switch (ai->ai_family) {
1354	case AF_INET:
1355#ifdef AF_INET6
1356	case AF_INET6:
1357#endif
1358		break;
1359	default:
1360		return 0;
1361	}
1362
1363	switch (ai->ai_socktype) {
1364	case SOCK_RAW:
1365		return EAI_SERVICE;
1366	case SOCK_DGRAM:
1367	case SOCK_STREAM:
1368		allownumeric = 1;
1369		break;
1370	case ANY:
1371		allownumeric = 0;
1372		break;
1373	default:
1374		return EAI_SOCKTYPE;
1375	}
1376
1377	port = str2number(servname);
1378	if (port >= 0) {
1379		if (!allownumeric)
1380			return EAI_SERVICE;
1381		if (port < 0 || port > 65535)
1382			return EAI_SERVICE;
1383		port = htons(port);
1384	} else {
1385		if (ai->ai_flags & AI_NUMERICSERV)
1386			return EAI_NONAME;
1387		switch (ai->ai_socktype) {
1388		case SOCK_DGRAM:
1389			proto = "udp";
1390			break;
1391		case SOCK_STREAM:
1392			proto = "tcp";
1393			break;
1394		default:
1395			proto = NULL;
1396			break;
1397		}
1398
1399		if ((sp = getservbyname(servname, proto)) == NULL)
1400			return EAI_SERVICE;
1401		port = sp->s_port;
1402	}
1403
1404	if (!matchonly) {
1405		switch (ai->ai_family) {
1406		case AF_INET:
1407			((struct sockaddr_in *)(void *)
1408			    ai->ai_addr)->sin_port = port;
1409			break;
1410#ifdef INET6
1411		case AF_INET6:
1412			((struct sockaddr_in6 *)(void *)
1413			    ai->ai_addr)->sin6_port = port;
1414			break;
1415#endif
1416		}
1417	}
1418
1419	return 0;
1420}
1421
1422static const struct afd *
1423find_afd(int af)
1424{
1425	const struct afd *afd;
1426
1427	if (af == PF_UNSPEC)
1428		return NULL;
1429	for (afd = afdl; afd->a_af; afd++) {
1430		if (afd->a_af == af)
1431			return afd;
1432	}
1433	return NULL;
1434}
1435
1436/*
1437 * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
1438 * will take care of it.
1439 * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
1440 * if the code is right or not.
1441 *
1442 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with
1443 * _dns_getaddrinfo.
1444 */
1445static int
1446addrconfig(struct addrinfo *pai)
1447{
1448	int s, af;
1449
1450	/*
1451	 * TODO:
1452	 * Note that implementation dependent test for address
1453	 * configuration should be done everytime called
1454	 * (or apropriate interval),
1455	 * because addresses will be dynamically assigned or deleted.
1456	 */
1457	af = pai->ai_family;
1458	if (af == AF_UNSPEC) {
1459		if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1460			af = AF_INET;
1461		else {
1462			_close(s);
1463			if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1464				af = AF_INET6;
1465			else
1466				_close(s);
1467		}
1468	}
1469	if (af != AF_UNSPEC) {
1470		if ((s = _socket(af, SOCK_DGRAM, 0)) < 0)
1471			return 0;
1472		_close(s);
1473	}
1474	pai->ai_family = af;
1475	return 1;
1476}
1477
1478#ifdef INET6
1479/* convert a string to a scope identifier. XXX: IPv6 specific */
1480static int
1481ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
1482{
1483	u_long lscopeid;
1484	struct in6_addr *a6;
1485	char *ep;
1486
1487	a6 = &sin6->sin6_addr;
1488
1489	/* empty scopeid portion is invalid */
1490	if (*scope == '\0')
1491		return -1;
1492
1493	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
1494		/*
1495		 * We currently assume a one-to-one mapping between links
1496		 * and interfaces, so we simply use interface indices for
1497		 * like-local scopes.
1498		 */
1499		*scopeid = if_nametoindex(scope);
1500		if (*scopeid == 0)
1501			goto trynumeric;
1502		return 0;
1503	}
1504
1505	/* still unclear about literal, allow numeric only - placeholder */
1506	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
1507		goto trynumeric;
1508	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
1509		goto trynumeric;
1510	else
1511		goto trynumeric;	/* global */
1512
1513	/* try to convert to a numeric id as a last resort */
1514  trynumeric:
1515	errno = 0;
1516	lscopeid = strtoul(scope, &ep, 10);
1517	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
1518	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
1519		return 0;
1520	else
1521		return -1;
1522}
1523#endif
1524
1525
1526#ifdef NS_CACHING
1527static int
1528addrinfo_id_func(char *buffer, size_t *buffer_size, va_list ap,
1529    void *cache_mdata)
1530{
1531	res_state statp;
1532	u_long res_options;
1533
1534	const int op_id = 0;	/* identifies the getaddrinfo for the cache */
1535	char *hostname;
1536	struct addrinfo *hints;
1537
1538	char *p;
1539	int ai_flags, ai_family, ai_socktype, ai_protocol;
1540	size_t desired_size, size;
1541
1542	statp = __res_state();
1543	res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
1544	    RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
1545
1546	hostname = va_arg(ap, char *);
1547	hints = va_arg(ap, struct addrinfo *);
1548
1549	desired_size = sizeof(res_options) + sizeof(int) + sizeof(int) * 4;
1550	if (hostname != NULL) {
1551		size = strlen(hostname);
1552		desired_size += size + 1;
1553	} else
1554		size = 0;
1555
1556	if (desired_size > *buffer_size) {
1557		*buffer_size = desired_size;
1558		return (NS_RETURN);
1559	}
1560
1561	if (hints == NULL)
1562		ai_flags = ai_family = ai_socktype = ai_protocol = 0;
1563	else {
1564		ai_flags = hints->ai_flags;
1565		ai_family = hints->ai_family;
1566		ai_socktype = hints->ai_socktype;
1567		ai_protocol = hints->ai_protocol;
1568	}
1569
1570	p = buffer;
1571	memcpy(p, &res_options, sizeof(res_options));
1572	p += sizeof(res_options);
1573
1574	memcpy(p, &op_id, sizeof(int));
1575	p += sizeof(int);
1576
1577	memcpy(p, &ai_flags, sizeof(int));
1578	p += sizeof(int);
1579
1580	memcpy(p, &ai_family, sizeof(int));
1581	p += sizeof(int);
1582
1583	memcpy(p, &ai_socktype, sizeof(int));
1584	p += sizeof(int);
1585
1586	memcpy(p, &ai_protocol, sizeof(int));
1587	p += sizeof(int);
1588
1589	if (hostname != NULL)
1590		memcpy(p, hostname, size);
1591
1592	*buffer_size = desired_size;
1593	return (NS_SUCCESS);
1594}
1595
1596static int
1597addrinfo_marshal_func(char *buffer, size_t *buffer_size, void *retval,
1598    va_list ap, void *cache_mdata)
1599{
1600	struct addrinfo	*ai, *cai;
1601	char *p;
1602	size_t desired_size, size, ai_size;
1603
1604	ai = *((struct addrinfo **)retval);
1605
1606	desired_size = sizeof(size_t);
1607	ai_size = 0;
1608	for (cai = ai; cai != NULL; cai = cai->ai_next) {
1609		desired_size += sizeof(struct addrinfo) + cai->ai_addrlen;
1610		if (cai->ai_canonname != NULL)
1611			desired_size += sizeof(size_t) +
1612			    strlen(cai->ai_canonname);
1613		++ai_size;
1614	}
1615
1616	if (desired_size > *buffer_size) {
1617		/* this assignment is here for future use */
1618		errno = ERANGE;
1619		*buffer_size = desired_size;
1620		return (NS_RETURN);
1621	}
1622
1623	memset(buffer, 0, desired_size);
1624	p = buffer;
1625
1626	memcpy(p, &ai_size, sizeof(size_t));
1627	p += sizeof(size_t);
1628	for (cai = ai; cai != NULL; cai = cai->ai_next) {
1629		memcpy(p, cai, sizeof(struct addrinfo));
1630		p += sizeof(struct addrinfo);
1631
1632		memcpy(p, cai->ai_addr, cai->ai_addrlen);
1633		p += cai->ai_addrlen;
1634
1635		if (cai->ai_canonname != NULL) {
1636			size = strlen(cai->ai_canonname);
1637			memcpy(p, &size, sizeof(size_t));
1638			p += sizeof(size_t);
1639
1640			memcpy(p, cai->ai_canonname, size);
1641			p += size;
1642		}
1643	}
1644
1645	return (NS_SUCCESS);
1646}
1647
1648static int
1649addrinfo_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
1650    va_list ap, void *cache_mdata)
1651{
1652	struct addrinfo	new_ai, *result, *sentinel, *lasts;
1653
1654	char *p;
1655	size_t ai_size, ai_i, size;
1656
1657	p = buffer;
1658	memcpy(&ai_size, p, sizeof(size_t));
1659	p += sizeof(size_t);
1660
1661	result = NULL;
1662	lasts = NULL;
1663	for (ai_i = 0; ai_i < ai_size; ++ai_i) {
1664		memcpy(&new_ai, p, sizeof(struct addrinfo));
1665		p += sizeof(struct addrinfo);
1666		size = new_ai.ai_addrlen + sizeof(struct addrinfo) +
1667			_ALIGNBYTES;
1668
1669		sentinel = (struct addrinfo *)malloc(size);
1670		memset(sentinel, 0, size);
1671
1672		memcpy(sentinel, &new_ai, sizeof(struct addrinfo));
1673		sentinel->ai_addr = (struct sockaddr *)_ALIGN((char *)sentinel +
1674		    sizeof(struct addrinfo));
1675
1676		memcpy(sentinel->ai_addr, p, new_ai.ai_addrlen);
1677		p += new_ai.ai_addrlen;
1678
1679		if (new_ai.ai_canonname != NULL) {
1680			memcpy(&size, p, sizeof(size_t));
1681			p += sizeof(size_t);
1682
1683			sentinel->ai_canonname = (char *)malloc(size + 1);
1684			memset(sentinel->ai_canonname, 0, size + 1);
1685
1686			memcpy(sentinel->ai_canonname, p, size);
1687			p += size;
1688		}
1689
1690		if (result == NULL) {
1691			result = sentinel;
1692			lasts = sentinel;
1693		} else {
1694			lasts->ai_next = sentinel;
1695			lasts = sentinel;
1696		}
1697	}
1698
1699	*((struct addrinfo **)retval) = result;
1700	return (NS_SUCCESS);
1701}
1702#endif /* NS_CACHING */
1703
1704/*
1705 * FQDN hostname, DNS lookup
1706 */
1707static int
1708explore_fqdn(const struct addrinfo *pai, const char *hostname,
1709    const char *servname, struct addrinfo **res)
1710{
1711	struct addrinfo *result;
1712	struct addrinfo *cur;
1713	int error = 0;
1714
1715#ifdef NS_CACHING
1716	static const nss_cache_info cache_info =
1717	NS_COMMON_CACHE_INFO_INITIALIZER(
1718		hosts, NULL, addrinfo_id_func, addrinfo_marshal_func,
1719		addrinfo_unmarshal_func);
1720#endif
1721	static const ns_dtab dtab[] = {
1722		NS_FILES_CB(_files_getaddrinfo, NULL)
1723		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
1724		NS_NIS_CB(_yp_getaddrinfo, NULL)
1725#ifdef NS_CACHING
1726		NS_CACHE_CB(&cache_info)
1727#endif
1728		{ 0 }
1729	};
1730
1731	result = NULL;
1732
1733	/*
1734	 * if the servname does not match socktype/protocol, ignore it.
1735	 */
1736	if (get_portmatch(pai, servname) != 0)
1737		return 0;
1738
1739	switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
1740			default_dns_files, hostname, pai)) {
1741	case NS_TRYAGAIN:
1742		error = EAI_AGAIN;
1743		goto free;
1744	case NS_UNAVAIL:
1745		error = EAI_FAIL;
1746		goto free;
1747	case NS_NOTFOUND:
1748		error = EAI_NONAME;
1749		goto free;
1750	case NS_SUCCESS:
1751		error = 0;
1752		for (cur = result; cur; cur = cur->ai_next) {
1753			GET_PORT(cur, servname);
1754			/* canonname should be filled already */
1755		}
1756		break;
1757	}
1758
1759	*res = result;
1760
1761	return 0;
1762
1763free:
1764	if (result)
1765		freeaddrinfo(result);
1766	return error;
1767}
1768
1769#ifdef DEBUG
1770static const char AskedForGot[] =
1771	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
1772#endif
1773
1774static struct addrinfo *
1775getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
1776    const struct addrinfo *pai, res_state res)
1777{
1778	struct addrinfo sentinel, *cur;
1779	struct addrinfo ai;
1780	const struct afd *afd;
1781	char *canonname;
1782	const HEADER *hp;
1783	const u_char *cp;
1784	int n;
1785	const u_char *eom;
1786	char *bp, *ep;
1787	int type, class, ancount, qdcount;
1788	int haveanswer, had_error;
1789	char tbuf[MAXDNAME];
1790	int (*name_ok)(const char *);
1791	char hostbuf[8*1024];
1792
1793	memset(&sentinel, 0, sizeof(sentinel));
1794	cur = &sentinel;
1795
1796	canonname = NULL;
1797	eom = answer->buf + anslen;
1798	switch (qtype) {
1799	case T_A:
1800	case T_AAAA:
1801	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
1802		name_ok = res_hnok;
1803		break;
1804	default:
1805		return (NULL);	/* XXX should be abort(); */
1806	}
1807	/*
1808	 * find first satisfactory answer
1809	 */
1810	hp = &answer->hdr;
1811	ancount = ntohs(hp->ancount);
1812	qdcount = ntohs(hp->qdcount);
1813	bp = hostbuf;
1814	ep = hostbuf + sizeof hostbuf;
1815	cp = answer->buf + HFIXEDSZ;
1816	if (qdcount != 1) {
1817		RES_SET_H_ERRNO(res, NO_RECOVERY);
1818		return (NULL);
1819	}
1820	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1821	if ((n < 0) || !(*name_ok)(bp)) {
1822		RES_SET_H_ERRNO(res, NO_RECOVERY);
1823		return (NULL);
1824	}
1825	cp += n + QFIXEDSZ;
1826	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
1827		/* res_send() has already verified that the query name is the
1828		 * same as the one we sent; this just gets the expanded name
1829		 * (i.e., with the succeeding search-domain tacked on).
1830		 */
1831		n = strlen(bp) + 1;		/* for the \0 */
1832		if (n >= MAXHOSTNAMELEN) {
1833			RES_SET_H_ERRNO(res, NO_RECOVERY);
1834			return (NULL);
1835		}
1836		canonname = bp;
1837		bp += n;
1838		/* The qname can be abbreviated, but h_name is now absolute. */
1839		qname = canonname;
1840	}
1841	haveanswer = 0;
1842	had_error = 0;
1843	while (ancount-- > 0 && cp < eom && !had_error) {
1844		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
1845		if ((n < 0) || !(*name_ok)(bp)) {
1846			had_error++;
1847			continue;
1848		}
1849		cp += n;			/* name */
1850		type = _getshort(cp);
1851 		cp += INT16SZ;			/* type */
1852		class = _getshort(cp);
1853 		cp += INT16SZ + INT32SZ;	/* class, TTL */
1854		n = _getshort(cp);
1855		cp += INT16SZ;			/* len */
1856		if (class != C_IN) {
1857			/* XXX - debug? syslog? */
1858			cp += n;
1859			continue;		/* XXX - had_error++ ? */
1860		}
1861		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
1862		    type == T_CNAME) {
1863			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
1864			if ((n < 0) || !(*name_ok)(tbuf)) {
1865				had_error++;
1866				continue;
1867			}
1868			cp += n;
1869			/* Get canonical name. */
1870			n = strlen(tbuf) + 1;	/* for the \0 */
1871			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
1872				had_error++;
1873				continue;
1874			}
1875			strlcpy(bp, tbuf, ep - bp);
1876			canonname = bp;
1877			bp += n;
1878			continue;
1879		}
1880		if (qtype == T_ANY) {
1881			if (!(type == T_A || type == T_AAAA)) {
1882				cp += n;
1883				continue;
1884			}
1885		} else if (type != qtype) {
1886#ifdef DEBUG
1887			if (type != T_KEY && type != T_SIG)
1888				syslog(LOG_NOTICE|LOG_AUTH,
1889	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
1890				       qname, p_class(C_IN), p_type(qtype),
1891				       p_type(type));
1892#endif
1893			cp += n;
1894			continue;		/* XXX - had_error++ ? */
1895		}
1896		switch (type) {
1897		case T_A:
1898		case T_AAAA:
1899			if (strcasecmp(canonname, bp) != 0) {
1900#ifdef DEBUG
1901				syslog(LOG_NOTICE|LOG_AUTH,
1902				       AskedForGot, canonname, bp);
1903#endif
1904				cp += n;
1905				continue;	/* XXX - had_error++ ? */
1906			}
1907			if (type == T_A && n != INADDRSZ) {
1908				cp += n;
1909				continue;
1910			}
1911			if (type == T_AAAA && n != IN6ADDRSZ) {
1912				cp += n;
1913				continue;
1914			}
1915#ifdef FILTER_V4MAPPED
1916			if (type == T_AAAA) {
1917				struct in6_addr in6;
1918				memcpy(&in6, cp, sizeof(in6));
1919				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
1920					cp += n;
1921					continue;
1922				}
1923			}
1924#endif
1925			if (!haveanswer) {
1926				int nn;
1927
1928				canonname = bp;
1929				nn = strlen(bp) + 1;	/* for the \0 */
1930				bp += nn;
1931			}
1932
1933			/* don't overwrite pai */
1934			ai = *pai;
1935			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
1936			afd = find_afd(ai.ai_family);
1937			if (afd == NULL) {
1938				cp += n;
1939				continue;
1940			}
1941			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
1942			if (cur->ai_next == NULL)
1943				had_error++;
1944			while (cur && cur->ai_next)
1945				cur = cur->ai_next;
1946			cp += n;
1947			break;
1948		default:
1949			abort();
1950		}
1951		if (!had_error)
1952			haveanswer++;
1953	}
1954	if (haveanswer) {
1955#if defined(RESOLVSORT)
1956		/*
1957		 * We support only IPv4 address for backward
1958		 * compatibility against gethostbyname(3).
1959		 */
1960		if (res->nsort && qtype == T_A) {
1961			if (addr4sort(&sentinel, res) < 0) {
1962				freeaddrinfo(sentinel.ai_next);
1963				RES_SET_H_ERRNO(res, NO_RECOVERY);
1964				return NULL;
1965			}
1966		}
1967#endif /*RESOLVSORT*/
1968		if (!canonname)
1969			(void)get_canonname(pai, sentinel.ai_next, qname);
1970		else
1971			(void)get_canonname(pai, sentinel.ai_next, canonname);
1972		RES_SET_H_ERRNO(res, NETDB_SUCCESS);
1973		return sentinel.ai_next;
1974	}
1975
1976	RES_SET_H_ERRNO(res, NO_RECOVERY);
1977	return NULL;
1978}
1979
1980#ifdef RESOLVSORT
1981struct addr_ptr {
1982	struct addrinfo *ai;
1983	int aval;
1984};
1985
1986static int
1987addr4sort(struct addrinfo *sentinel, res_state res)
1988{
1989	struct addrinfo *ai;
1990	struct addr_ptr *addrs, addr;
1991	struct sockaddr_in *sin;
1992	int naddrs, i, j;
1993	int needsort = 0;
1994
1995	if (!sentinel)
1996		return -1;
1997	naddrs = 0;
1998	for (ai = sentinel->ai_next; ai; ai = ai->ai_next)
1999		naddrs++;
2000	if (naddrs < 2)
2001		return 0;		/* We don't need sorting. */
2002	if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL)
2003		return -1;
2004	i = 0;
2005	for (ai = sentinel->ai_next; ai; ai = ai->ai_next) {
2006		sin = (struct sockaddr_in *)ai->ai_addr;
2007		for (j = 0; (unsigned)j < res->nsort; j++) {
2008			if (res->sort_list[j].addr.s_addr ==
2009			    (sin->sin_addr.s_addr & res->sort_list[j].mask))
2010				break;
2011		}
2012		addrs[i].ai = ai;
2013		addrs[i].aval = j;
2014		if (needsort == 0 && i > 0 && j < addrs[i - 1].aval)
2015			needsort = i;
2016		i++;
2017	}
2018	if (!needsort) {
2019		free(addrs);
2020		return 0;
2021	}
2022
2023	while (needsort < naddrs) {
2024		for (j = needsort - 1; j >= 0; j--) {
2025			if (addrs[j].aval > addrs[j+1].aval) {
2026				addr = addrs[j];
2027				addrs[j] = addrs[j + 1];
2028				addrs[j + 1] = addr;
2029			} else
2030				break;
2031		}
2032		needsort++;
2033	}
2034
2035	ai = sentinel;
2036	for (i = 0; i < naddrs; ++i) {
2037		ai->ai_next = addrs[i].ai;
2038		ai = ai->ai_next;
2039	}
2040	ai->ai_next = NULL;
2041	free(addrs);
2042	return 0;
2043}
2044#endif /*RESOLVSORT*/
2045
2046/*ARGSUSED*/
2047static int
2048_dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
2049{
2050	struct addrinfo *ai;
2051	querybuf *buf, *buf2;
2052	const char *hostname;
2053	const struct addrinfo *pai;
2054	struct addrinfo sentinel, *cur;
2055	struct res_target q, q2;
2056	res_state res;
2057
2058	hostname = va_arg(ap, char *);
2059	pai = va_arg(ap, const struct addrinfo *);
2060
2061	memset(&q, 0, sizeof(q));
2062	memset(&q2, 0, sizeof(q2));
2063	memset(&sentinel, 0, sizeof(sentinel));
2064	cur = &sentinel;
2065
2066	buf = malloc(sizeof(*buf));
2067	if (!buf) {
2068		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2069		return NS_NOTFOUND;
2070	}
2071	buf2 = malloc(sizeof(*buf2));
2072	if (!buf2) {
2073		free(buf);
2074		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2075		return NS_NOTFOUND;
2076	}
2077
2078	switch (pai->ai_family) {
2079	case AF_UNSPEC:
2080		q.name = hostname;
2081		q.qclass = C_IN;
2082		q.qtype = T_A;
2083		q.answer = buf->buf;
2084		q.anslen = sizeof(buf->buf);
2085		q.next = &q2;
2086		q2.name = hostname;
2087		q2.qclass = C_IN;
2088		q2.qtype = T_AAAA;
2089		q2.answer = buf2->buf;
2090		q2.anslen = sizeof(buf2->buf);
2091		break;
2092	case AF_INET:
2093		q.name = hostname;
2094		q.qclass = C_IN;
2095		q.qtype = T_A;
2096		q.answer = buf->buf;
2097		q.anslen = sizeof(buf->buf);
2098		break;
2099	case AF_INET6:
2100		q.name = hostname;
2101		q.qclass = C_IN;
2102		q.qtype = T_AAAA;
2103		q.answer = buf->buf;
2104		q.anslen = sizeof(buf->buf);
2105		break;
2106	default:
2107		free(buf);
2108		free(buf2);
2109		return NS_UNAVAIL;
2110	}
2111
2112	res = __res_state();
2113	if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) {
2114		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2115		free(buf);
2116		free(buf2);
2117		return NS_NOTFOUND;
2118	}
2119
2120	if (res_searchN(hostname, &q, res) < 0) {
2121		free(buf);
2122		free(buf2);
2123		return NS_NOTFOUND;
2124	}
2125	/* prefer IPv6 */
2126	if (q.next) {
2127		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res);
2128		if (ai) {
2129			cur->ai_next = ai;
2130			while (cur && cur->ai_next)
2131				cur = cur->ai_next;
2132		}
2133	}
2134	ai = getanswer(buf, q.n, q.name, q.qtype, pai, res);
2135	if (ai)
2136		cur->ai_next = ai;
2137	free(buf);
2138	free(buf2);
2139	if (sentinel.ai_next == NULL)
2140		switch (res->res_h_errno) {
2141		case HOST_NOT_FOUND:
2142			return NS_NOTFOUND;
2143		case TRY_AGAIN:
2144			return NS_TRYAGAIN;
2145		default:
2146			return NS_UNAVAIL;
2147		}
2148	*((struct addrinfo **)rv) = sentinel.ai_next;
2149	return NS_SUCCESS;
2150}
2151
2152static void
2153_sethtent(FILE **hostf)
2154{
2155	if (!*hostf)
2156		*hostf = fopen(_PATH_HOSTS, "r");
2157	else
2158		rewind(*hostf);
2159}
2160
2161static void
2162_endhtent(FILE **hostf)
2163{
2164	if (*hostf) {
2165		(void) fclose(*hostf);
2166		*hostf = NULL;
2167	}
2168}
2169
2170static struct addrinfo *
2171_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
2172{
2173	char *p;
2174	char *cp, *tname, *cname;
2175	struct addrinfo hints, *res0, *res;
2176	int error;
2177	const char *addr;
2178	char hostbuf[8*1024];
2179
2180	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r")))
2181		return (NULL);
2182again:
2183	if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf)))
2184		return (NULL);
2185	if (*p == '#')
2186		goto again;
2187	cp = strpbrk(p, "#\n");
2188	if (cp != NULL)
2189		*cp = '\0';
2190	if (!(cp = strpbrk(p, " \t")))
2191		goto again;
2192	*cp++ = '\0';
2193	addr = p;
2194	cname = NULL;
2195	/* if this is not something we're looking for, skip it. */
2196	while (cp && *cp) {
2197		if (*cp == ' ' || *cp == '\t') {
2198			cp++;
2199			continue;
2200		}
2201		tname = cp;
2202		if (cname == NULL)
2203			cname = cp;
2204		if ((cp = strpbrk(cp, " \t")) != NULL)
2205			*cp++ = '\0';
2206		if (strcasecmp(name, tname) == 0)
2207			goto found;
2208	}
2209	goto again;
2210
2211found:
2212	/* we should not glob socktype/protocol here */
2213	memset(&hints, 0, sizeof(hints));
2214	hints.ai_family = pai->ai_family;
2215	hints.ai_socktype = SOCK_DGRAM;
2216	hints.ai_protocol = 0;
2217	hints.ai_flags = AI_NUMERICHOST;
2218	error = getaddrinfo(addr, "0", &hints, &res0);
2219	if (error)
2220		goto again;
2221#ifdef FILTER_V4MAPPED
2222	/* XXX should check all items in the chain */
2223	if (res0->ai_family == AF_INET6 &&
2224	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) {
2225		freeaddrinfo(res0);
2226		goto again;
2227	}
2228#endif
2229	for (res = res0; res; res = res->ai_next) {
2230		/* cover it up */
2231		res->ai_flags = pai->ai_flags;
2232		res->ai_socktype = pai->ai_socktype;
2233		res->ai_protocol = pai->ai_protocol;
2234
2235		if (pai->ai_flags & AI_CANONNAME) {
2236			if (get_canonname(pai, res, cname) != 0) {
2237				freeaddrinfo(res0);
2238				goto again;
2239			}
2240		}
2241	}
2242	return res0;
2243}
2244
2245/*ARGSUSED*/
2246static int
2247_files_getaddrinfo(void *rv, void *cb_data, va_list ap)
2248{
2249	const char *name;
2250	const struct addrinfo *pai;
2251	struct addrinfo sentinel, *cur;
2252	struct addrinfo *p;
2253	FILE *hostf = NULL;
2254
2255	name = va_arg(ap, char *);
2256	pai = va_arg(ap, struct addrinfo *);
2257
2258	memset(&sentinel, 0, sizeof(sentinel));
2259	cur = &sentinel;
2260
2261	_sethtent(&hostf);
2262	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
2263		cur->ai_next = p;
2264		while (cur && cur->ai_next)
2265			cur = cur->ai_next;
2266	}
2267	_endhtent(&hostf);
2268
2269	*((struct addrinfo **)rv) = sentinel.ai_next;
2270	if (sentinel.ai_next == NULL)
2271		return NS_NOTFOUND;
2272	return NS_SUCCESS;
2273}
2274
2275#ifdef YP
2276/*ARGSUSED*/
2277static struct addrinfo *
2278_yphostent(char *line, const struct addrinfo *pai)
2279{
2280	struct addrinfo sentinel, *cur;
2281	struct addrinfo hints, *res, *res0;
2282	int error;
2283	char *p = line;
2284	const char *addr, *canonname;
2285	char *nextline;
2286	char *cp;
2287
2288	addr = canonname = NULL;
2289
2290	memset(&sentinel, 0, sizeof(sentinel));
2291	cur = &sentinel;
2292
2293nextline:
2294	/* terminate line */
2295	cp = strchr(p, '\n');
2296	if (cp) {
2297		*cp++ = '\0';
2298		nextline = cp;
2299	} else
2300		nextline = NULL;
2301
2302	cp = strpbrk(p, " \t");
2303	if (cp == NULL) {
2304		if (canonname == NULL)
2305			return (NULL);
2306		else
2307			goto done;
2308	}
2309	*cp++ = '\0';
2310
2311	addr = p;
2312
2313	while (cp && *cp) {
2314		if (*cp == ' ' || *cp == '\t') {
2315			cp++;
2316			continue;
2317		}
2318		if (!canonname)
2319			canonname = cp;
2320		if ((cp = strpbrk(cp, " \t")) != NULL)
2321			*cp++ = '\0';
2322	}
2323
2324	hints = *pai;
2325	hints.ai_flags = AI_NUMERICHOST;
2326	error = getaddrinfo(addr, NULL, &hints, &res0);
2327	if (error == 0) {
2328		for (res = res0; res; res = res->ai_next) {
2329			/* cover it up */
2330			res->ai_flags = pai->ai_flags;
2331
2332			if (pai->ai_flags & AI_CANONNAME)
2333				(void)get_canonname(pai, res, canonname);
2334		}
2335	} else
2336		res0 = NULL;
2337	if (res0) {
2338		cur->ai_next = res0;
2339		while (cur && cur->ai_next)
2340			cur = cur->ai_next;
2341	}
2342
2343	if (nextline) {
2344		p = nextline;
2345		goto nextline;
2346	}
2347
2348done:
2349	return sentinel.ai_next;
2350}
2351
2352/*ARGSUSED*/
2353static int
2354_yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
2355{
2356	struct addrinfo sentinel, *cur;
2357	struct addrinfo *ai = NULL;
2358	char *ypbuf;
2359	int ypbuflen, r;
2360	const char *name;
2361	const struct addrinfo *pai;
2362	char *ypdomain;
2363
2364	if (_yp_check(&ypdomain) == 0)
2365		return NS_UNAVAIL;
2366
2367	name = va_arg(ap, char *);
2368	pai = va_arg(ap, const struct addrinfo *);
2369
2370	memset(&sentinel, 0, sizeof(sentinel));
2371	cur = &sentinel;
2372
2373	/* hosts.byname is only for IPv4 (Solaris8) */
2374	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
2375		r = yp_match(ypdomain, "hosts.byname", name,
2376			(int)strlen(name), &ypbuf, &ypbuflen);
2377		if (r == 0) {
2378			struct addrinfo ai4;
2379
2380			ai4 = *pai;
2381			ai4.ai_family = AF_INET;
2382			ai = _yphostent(ypbuf, &ai4);
2383			if (ai) {
2384				cur->ai_next = ai;
2385				while (cur && cur->ai_next)
2386					cur = cur->ai_next;
2387			}
2388			free(ypbuf);
2389		}
2390	}
2391
2392	/* ipnodes.byname can hold both IPv4/v6 */
2393	r = yp_match(ypdomain, "ipnodes.byname", name,
2394		(int)strlen(name), &ypbuf, &ypbuflen);
2395	if (r == 0) {
2396		ai = _yphostent(ypbuf, pai);
2397		if (ai)
2398			cur->ai_next = ai;
2399		free(ypbuf);
2400	}
2401
2402	if (sentinel.ai_next == NULL) {
2403		RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND);
2404		return NS_NOTFOUND;
2405	}
2406	*((struct addrinfo **)rv) = sentinel.ai_next;
2407	return NS_SUCCESS;
2408}
2409#endif
2410
2411/* resolver logic */
2412
2413/*
2414 * Formulate a normal query, send, and await answer.
2415 * Returned answer is placed in supplied buffer "answer".
2416 * Perform preliminary check of answer, returning success only
2417 * if no error is indicated and the answer count is nonzero.
2418 * Return the size of the response on success, -1 on error.
2419 * Error number is left in h_errno.
2420 *
2421 * Caller must parse answer and determine whether it answers the question.
2422 */
2423static int
2424res_queryN(const char *name, struct res_target *target, res_state res)
2425{
2426	u_char *buf;
2427	HEADER *hp;
2428	int n;
2429	u_int oflags;
2430	struct res_target *t;
2431	int rcode;
2432	int ancount;
2433
2434	rcode = NOERROR;
2435	ancount = 0;
2436
2437	buf = malloc(MAXPACKET);
2438	if (!buf) {
2439		RES_SET_H_ERRNO(res, NETDB_INTERNAL);
2440		return -1;
2441	}
2442
2443	for (t = target; t; t = t->next) {
2444		int class, type;
2445		u_char *answer;
2446		int anslen;
2447
2448		hp = (HEADER *)(void *)t->answer;
2449
2450		/* make it easier... */
2451		class = t->qclass;
2452		type = t->qtype;
2453		answer = t->answer;
2454		anslen = t->anslen;
2455
2456		oflags = res->_flags;
2457
2458again:
2459		hp->rcode = NOERROR;	/* default */
2460
2461#ifdef DEBUG
2462		if (res->options & RES_DEBUG)
2463			printf(";; res_query(%s, %d, %d)\n", name, class, type);
2464#endif
2465
2466		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
2467		    buf, MAXPACKET);
2468		if (n > 0 && (res->_flags & RES_F_EDNS0ERR) == 0 &&
2469		    (res->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U)
2470			n = res_nopt(res, n, buf, MAXPACKET, anslen);
2471		if (n <= 0) {
2472#ifdef DEBUG
2473			if (res->options & RES_DEBUG)
2474				printf(";; res_query: mkquery failed\n");
2475#endif
2476			free(buf);
2477			RES_SET_H_ERRNO(res, NO_RECOVERY);
2478			return (n);
2479		}
2480		n = res_nsend(res, buf, n, answer, anslen);
2481		if (n < 0) {
2482			/*
2483			 * if the query choked with EDNS0, retry
2484			 * without EDNS0
2485			 */
2486			if ((res->options & (RES_USE_EDNS0|RES_USE_DNSSEC))
2487			    != 0U &&
2488			    ((oflags ^ res->_flags) & RES_F_EDNS0ERR) != 0) {
2489				res->_flags |= RES_F_EDNS0ERR;
2490				if (res->options & RES_DEBUG)
2491					printf(";; res_nquery: retry without EDNS0\n");
2492				goto again;
2493			}
2494			rcode = hp->rcode;	/* record most recent error */
2495#ifdef DEBUG
2496			if (res->options & RES_DEBUG)
2497				printf(";; res_query: send error\n");
2498#endif
2499			continue;
2500		}
2501
2502		if (n > anslen)
2503			hp->rcode = FORMERR; /* XXX not very informative */
2504		if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2505			rcode = hp->rcode;	/* record most recent error */
2506#ifdef DEBUG
2507			if (res->options & RES_DEBUG)
2508				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
2509				    ntohs(hp->ancount));
2510#endif
2511			continue;
2512		}
2513
2514		ancount += ntohs(hp->ancount);
2515
2516		t->n = n;
2517	}
2518
2519	free(buf);
2520
2521	if (ancount == 0) {
2522		switch (rcode) {
2523		case NXDOMAIN:
2524			RES_SET_H_ERRNO(res, HOST_NOT_FOUND);
2525			break;
2526		case SERVFAIL:
2527			RES_SET_H_ERRNO(res, TRY_AGAIN);
2528			break;
2529		case NOERROR:
2530			RES_SET_H_ERRNO(res, NO_DATA);
2531			break;
2532		case FORMERR:
2533		case NOTIMP:
2534		case REFUSED:
2535		default:
2536			RES_SET_H_ERRNO(res, NO_RECOVERY);
2537			break;
2538		}
2539		return (-1);
2540	}
2541	return (ancount);
2542}
2543
2544/*
2545 * Formulate a normal query, send, and retrieve answer in supplied buffer.
2546 * Return the size of the response on success, -1 on error.
2547 * If enabled, implement search rules until answer or unrecoverable failure
2548 * is detected.  Error code, if any, is left in h_errno.
2549 */
2550static int
2551res_searchN(const char *name, struct res_target *target, res_state res)
2552{
2553	const char *cp, * const *domain;
2554	HEADER *hp = (HEADER *)(void *)target->answer;	/*XXX*/
2555	u_int dots;
2556	int trailing_dot, ret, saved_herrno;
2557	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2558	int tried_as_is = 0;
2559	int searched = 0;
2560	char abuf[MAXDNAME];
2561
2562	errno = 0;
2563	RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */
2564	dots = 0;
2565	for (cp = name; *cp; cp++)
2566		dots += (*cp == '.');
2567	trailing_dot = 0;
2568	if (cp > name && *--cp == '.')
2569		trailing_dot++;
2570
2571	/*
2572	 * if there aren't any dots, it could be a user-level alias
2573	 */
2574	if (!dots &&
2575	    (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL)
2576		return (res_queryN(cp, target, res));
2577
2578	/*
2579	 * If there are enough dots in the name, let's just give it a
2580	 * try 'as is'. The threshold can be set with the "ndots" option.
2581	 * Also, query 'as is', if there is a trailing dot in the name.
2582	 */
2583	saved_herrno = -1;
2584	if (dots >= res->ndots || trailing_dot) {
2585		ret = res_querydomainN(name, NULL, target, res);
2586		if (ret > 0 || trailing_dot)
2587			return (ret);
2588		if (errno == ECONNREFUSED) {
2589			RES_SET_H_ERRNO(res, TRY_AGAIN);
2590			return (-1);
2591		}
2592		switch (res->res_h_errno) {
2593		case NO_DATA:
2594		case HOST_NOT_FOUND:
2595			break;
2596		case TRY_AGAIN:
2597			if (hp->rcode == SERVFAIL)
2598				break;
2599			/* FALLTHROUGH */
2600		default:
2601			return (-1);
2602		}
2603		saved_herrno = res->res_h_errno;
2604		tried_as_is++;
2605	}
2606
2607	/*
2608	 * We do at least one level of search if
2609	 *	- there is no dot and RES_DEFNAME is set, or
2610	 *	- there is at least one dot, there is no trailing dot,
2611	 *	  and RES_DNSRCH is set.
2612	 */
2613	if ((!dots && (res->options & RES_DEFNAMES)) ||
2614	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
2615		int done = 0;
2616
2617		for (domain = (const char * const *)res->dnsrch;
2618		   *domain && !done;
2619		   domain++) {
2620			searched = 1;
2621
2622			if (domain[0][0] == '\0' ||
2623			    (domain[0][0] == '.' && domain[0][1] == '\0'))
2624				root_on_list++;
2625
2626			if (root_on_list && tried_as_is)
2627				continue;
2628
2629			ret = res_querydomainN(name, *domain, target, res);
2630			if (ret > 0)
2631				return (ret);
2632
2633			/*
2634			 * If no server present, give up.
2635			 * If name isn't found in this domain,
2636			 * keep trying higher domains in the search list
2637			 * (if that's enabled).
2638			 * On a NO_DATA error, keep trying, otherwise
2639			 * a wildcard entry of another type could keep us
2640			 * from finding this entry higher in the domain.
2641			 * If we get some other error (negative answer or
2642			 * server failure), then stop searching up,
2643			 * but try the input name below in case it's
2644			 * fully-qualified.
2645			 */
2646			if (errno == ECONNREFUSED) {
2647				RES_SET_H_ERRNO(res, TRY_AGAIN);
2648				return (-1);
2649			}
2650
2651			switch (res->res_h_errno) {
2652			case NO_DATA:
2653				got_nodata++;
2654				/* FALLTHROUGH */
2655			case HOST_NOT_FOUND:
2656				/* keep trying */
2657				break;
2658			case TRY_AGAIN:
2659				got_servfail++;
2660				if (hp->rcode == SERVFAIL) {
2661					/* try next search element, if any */
2662					break;
2663				}
2664				/* FALLTHROUGH */
2665			default:
2666				/* anything else implies that we're done */
2667				done++;
2668			}
2669			/*
2670			 * if we got here for some reason other than DNSRCH,
2671			 * we only wanted one iteration of the loop, so stop.
2672			 */
2673			if (!(res->options & RES_DNSRCH))
2674			        done++;
2675		}
2676	}
2677
2678	switch (res->res_h_errno) {
2679	case NO_DATA:
2680	case HOST_NOT_FOUND:
2681		break;
2682	case TRY_AGAIN:
2683		if (hp->rcode == SERVFAIL)
2684			break;
2685		/* FALLTHROUGH */
2686	default:
2687		goto giveup;
2688	}
2689
2690	/*
2691	 * If the query has not already been tried as is then try it
2692	 * unless RES_NOTLDQUERY is set and there were no dots.
2693	 */
2694	if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) &&
2695	    !(tried_as_is || root_on_list)) {
2696		ret = res_querydomainN(name, NULL, target, res);
2697		if (ret > 0)
2698			return (ret);
2699	}
2700
2701	/*
2702	 * if we got here, we didn't satisfy the search.
2703	 * if we did an initial full query, return that query's h_errno
2704	 * (note that we wouldn't be here if that query had succeeded).
2705	 * else if we ever got a nodata, send that back as the reason.
2706	 * else send back meaningless h_errno, that being the one from
2707	 * the last DNSRCH we did.
2708	 */
2709giveup:
2710	if (saved_herrno != -1)
2711		RES_SET_H_ERRNO(res, saved_herrno);
2712	else if (got_nodata)
2713		RES_SET_H_ERRNO(res, NO_DATA);
2714	else if (got_servfail)
2715		RES_SET_H_ERRNO(res, TRY_AGAIN);
2716	return (-1);
2717}
2718
2719/*
2720 * Perform a call on res_query on the concatenation of name and domain,
2721 * removing a trailing dot from name if domain is NULL.
2722 */
2723static int
2724res_querydomainN(const char *name, const char *domain,
2725    struct res_target *target, res_state res)
2726{
2727	char nbuf[MAXDNAME];
2728	const char *longname = nbuf;
2729	size_t n, d;
2730
2731#ifdef DEBUG
2732	if (res->options & RES_DEBUG)
2733		printf(";; res_querydomain(%s, %s)\n",
2734			name, domain?domain:"<Nil>");
2735#endif
2736	if (domain == NULL) {
2737		/*
2738		 * Check for trailing '.';
2739		 * copy without '.' if present.
2740		 */
2741		n = strlen(name);
2742		if (n >= MAXDNAME) {
2743			RES_SET_H_ERRNO(res, NO_RECOVERY);
2744			return (-1);
2745		}
2746		if (n > 0 && name[--n] == '.') {
2747			strncpy(nbuf, name, n);
2748			nbuf[n] = '\0';
2749		} else
2750			longname = name;
2751	} else {
2752		n = strlen(name);
2753		d = strlen(domain);
2754		if (n + d + 1 >= MAXDNAME) {
2755			RES_SET_H_ERRNO(res, NO_RECOVERY);
2756			return (-1);
2757		}
2758		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
2759	}
2760	return (res_queryN(longname, target, res));
2761}
2762