xref: /minix3/lib/libc/net/getaddrinfo.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: getaddrinfo.c,v 1.109 2015/09/10 14:05:06 christos Exp $	*/
22fe8fb19SBen Gras /*	$KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $	*/
32fe8fb19SBen Gras 
42fe8fb19SBen Gras /*
52fe8fb19SBen Gras  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
62fe8fb19SBen Gras  * All rights reserved.
72fe8fb19SBen Gras  *
82fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras  * are met:
112fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras  * 3. Neither the name of the project nor the names of its contributors
172fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
182fe8fb19SBen Gras  *    without specific prior written permission.
192fe8fb19SBen Gras  *
202fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
212fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
242fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302fe8fb19SBen Gras  * SUCH DAMAGE.
312fe8fb19SBen Gras  */
322fe8fb19SBen Gras 
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras  * Issues to be discussed:
352fe8fb19SBen Gras  * - Return values.  There are nonstandard return values defined and used
362fe8fb19SBen Gras  *   in the source code.  This is because RFC2553 is silent about which error
372fe8fb19SBen Gras  *   code must be returned for which situation.
382fe8fb19SBen Gras  * - IPv4 classful (shortened) form.  RFC2553 is silent about it.  XNET 5.2
392fe8fb19SBen Gras  *   says to use inet_aton() to convert IPv4 numeric to binary (alows
402fe8fb19SBen Gras  *   classful form as a result).
412fe8fb19SBen Gras  *   current code - disallow classful form for IPv4 (due to use of inet_pton).
422fe8fb19SBen Gras  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
432fe8fb19SBen Gras  *   invalid.
442fe8fb19SBen Gras  *   current code - SEGV on freeaddrinfo(NULL)
452fe8fb19SBen Gras  * Note:
462fe8fb19SBen Gras  * - The code filters out AFs that are not supported by the kernel,
472fe8fb19SBen Gras  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
482fe8fb19SBen Gras  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
492fe8fb19SBen Gras  *   in ai_flags?
502fe8fb19SBen Gras  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
512fe8fb19SBen Gras  *   (1) what should we do against numeric hostname (2) what should we do
522fe8fb19SBen Gras  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
532fe8fb19SBen Gras  *   non-loopback address configured?  global address configured?
542fe8fb19SBen Gras  */
552fe8fb19SBen Gras 
562fe8fb19SBen Gras #include <sys/cdefs.h>
572fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
58*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: getaddrinfo.c,v 1.109 2015/09/10 14:05:06 christos Exp $");
592fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
602fe8fb19SBen Gras 
61*0a6a1f1dSLionel Sambuc #ifndef RUMP_ACTION
622fe8fb19SBen Gras #include "namespace.h"
63*0a6a1f1dSLionel Sambuc #endif
642fe8fb19SBen Gras #include <sys/types.h>
652fe8fb19SBen Gras #include <sys/param.h>
662fe8fb19SBen Gras #include <sys/socket.h>
672fe8fb19SBen Gras #include <net/if.h>
682fe8fb19SBen Gras #include <netinet/in.h>
692fe8fb19SBen Gras #include <arpa/inet.h>
702fe8fb19SBen Gras #include <arpa/nameser.h>
712fe8fb19SBen Gras #include <assert.h>
722fe8fb19SBen Gras #include <ctype.h>
732fe8fb19SBen Gras #include <errno.h>
742fe8fb19SBen Gras #include <netdb.h>
752fe8fb19SBen Gras #include <resolv.h>
762fe8fb19SBen Gras #include <stddef.h>
772fe8fb19SBen Gras #include <stdio.h>
782fe8fb19SBen Gras #include <stdlib.h>
792fe8fb19SBen Gras #include <string.h>
802fe8fb19SBen Gras #include <unistd.h>
81f14fb602SLionel Sambuc #include <ifaddrs.h>
822fe8fb19SBen Gras 
832fe8fb19SBen Gras #include <syslog.h>
842fe8fb19SBen Gras #include <stdarg.h>
852fe8fb19SBen Gras #include <nsswitch.h>
862fe8fb19SBen Gras 
872fe8fb19SBen Gras #ifdef YP
882fe8fb19SBen Gras #include <rpc/rpc.h>
892fe8fb19SBen Gras #include <rpcsvc/yp_prot.h>
902fe8fb19SBen Gras #include <rpcsvc/ypclnt.h>
912fe8fb19SBen Gras #endif
922fe8fb19SBen Gras 
932fe8fb19SBen Gras #include "servent.h"
942fe8fb19SBen Gras 
95*0a6a1f1dSLionel Sambuc #ifndef RUMP_ACTION
962fe8fb19SBen Gras #ifdef __weak_alias
972fe8fb19SBen Gras __weak_alias(getaddrinfo,_getaddrinfo)
98*0a6a1f1dSLionel Sambuc __weak_alias(allocaddrinfo,_allocaddrinfo)
992fe8fb19SBen Gras __weak_alias(freeaddrinfo,_freeaddrinfo)
1002fe8fb19SBen Gras __weak_alias(gai_strerror,_gai_strerror)
1012fe8fb19SBen Gras #endif
102*0a6a1f1dSLionel Sambuc #endif
1032fe8fb19SBen Gras 
1042fe8fb19SBen Gras #define SUCCESS 0
1052fe8fb19SBen Gras #define ANY 0
1062fe8fb19SBen Gras #define YES 1
1072fe8fb19SBen Gras #define NO  0
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras static const char in_addrany[] = { 0, 0, 0, 0 };
1102fe8fb19SBen Gras static const char in_loopback[] = { 127, 0, 0, 1 };
1112fe8fb19SBen Gras #ifdef INET6
1122fe8fb19SBen Gras static const char in6_addrany[] = {
1132fe8fb19SBen Gras 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1142fe8fb19SBen Gras };
1152fe8fb19SBen Gras static const char in6_loopback[] = {
1162fe8fb19SBen Gras 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
1172fe8fb19SBen Gras };
1182fe8fb19SBen Gras #endif
1192fe8fb19SBen Gras 
1202fe8fb19SBen Gras static const struct afd {
1212fe8fb19SBen Gras 	int a_af;
1222fe8fb19SBen Gras 	int a_addrlen;
1232fe8fb19SBen Gras 	int a_socklen;
1242fe8fb19SBen Gras 	int a_off;
1252fe8fb19SBen Gras 	const char *a_addrany;
1262fe8fb19SBen Gras 	const char *a_loopback;
1272fe8fb19SBen Gras 	int a_scoped;
1282fe8fb19SBen Gras } afdl [] = {
1292fe8fb19SBen Gras #ifdef INET6
1302fe8fb19SBen Gras 	{PF_INET6, sizeof(struct in6_addr),
1312fe8fb19SBen Gras 	 sizeof(struct sockaddr_in6),
1322fe8fb19SBen Gras 	 offsetof(struct sockaddr_in6, sin6_addr),
1332fe8fb19SBen Gras 	 in6_addrany, in6_loopback, 1},
1342fe8fb19SBen Gras #endif
1352fe8fb19SBen Gras 	{PF_INET, sizeof(struct in_addr),
1362fe8fb19SBen Gras 	 sizeof(struct sockaddr_in),
1372fe8fb19SBen Gras 	 offsetof(struct sockaddr_in, sin_addr),
1382fe8fb19SBen Gras 	 in_addrany, in_loopback, 0},
1392fe8fb19SBen Gras 	{0, 0, 0, 0, NULL, NULL, 0},
1402fe8fb19SBen Gras };
1412fe8fb19SBen Gras 
1422fe8fb19SBen Gras struct explore {
1432fe8fb19SBen Gras 	int e_af;
1442fe8fb19SBen Gras 	int e_socktype;
1452fe8fb19SBen Gras 	int e_protocol;
1462fe8fb19SBen Gras 	const char *e_protostr;
1472fe8fb19SBen Gras 	int e_wild;
1482fe8fb19SBen Gras #define WILD_AF(ex)		((ex)->e_wild & 0x01)
1492fe8fb19SBen Gras #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
1502fe8fb19SBen Gras #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
1512fe8fb19SBen Gras };
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras static const struct explore explore[] = {
1542fe8fb19SBen Gras #if 0
1552fe8fb19SBen Gras 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
1562fe8fb19SBen Gras #endif
1572fe8fb19SBen Gras #ifdef INET6
1582fe8fb19SBen Gras 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
1592fe8fb19SBen Gras 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
1602fe8fb19SBen Gras 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
1612fe8fb19SBen Gras #endif
1622fe8fb19SBen Gras 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
1632fe8fb19SBen Gras 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
1642fe8fb19SBen Gras 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
1652fe8fb19SBen Gras 	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
1662fe8fb19SBen Gras 	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
1672fe8fb19SBen Gras 	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
1682fe8fb19SBen Gras 	{ -1, 0, 0, NULL, 0 },
1692fe8fb19SBen Gras };
1702fe8fb19SBen Gras 
1712fe8fb19SBen Gras #ifdef INET6
1722fe8fb19SBen Gras #define PTON_MAX	16
1732fe8fb19SBen Gras #else
1742fe8fb19SBen Gras #define PTON_MAX	4
1752fe8fb19SBen Gras #endif
1762fe8fb19SBen Gras 
1772fe8fb19SBen Gras static const ns_src default_dns_files[] = {
1782fe8fb19SBen Gras 	{ NSSRC_FILES,	NS_SUCCESS },
1792fe8fb19SBen Gras 	{ NSSRC_DNS,	NS_SUCCESS },
1802fe8fb19SBen Gras 	{ 0, 0 }
1812fe8fb19SBen Gras };
1822fe8fb19SBen Gras 
1832fe8fb19SBen Gras #define MAXPACKET	(64*1024)
1842fe8fb19SBen Gras 
1852fe8fb19SBen Gras typedef union {
1862fe8fb19SBen Gras 	HEADER hdr;
1872fe8fb19SBen Gras 	u_char buf[MAXPACKET];
1882fe8fb19SBen Gras } querybuf;
1892fe8fb19SBen Gras 
1902fe8fb19SBen Gras struct res_target {
1912fe8fb19SBen Gras 	struct res_target *next;
1922fe8fb19SBen Gras 	const char *name;	/* domain name */
1932fe8fb19SBen Gras 	int qclass, qtype;	/* class and type of query */
1942fe8fb19SBen Gras 	u_char *answer;		/* buffer to put answer */
1952fe8fb19SBen Gras 	int anslen;		/* size of answer buffer */
1962fe8fb19SBen Gras 	int n;			/* result length */
1972fe8fb19SBen Gras };
1982fe8fb19SBen Gras 
19984d9c625SLionel Sambuc struct srvinfo {
20084d9c625SLionel Sambuc        struct srvinfo *next;
20184d9c625SLionel Sambuc        char name[MAXDNAME];
20284d9c625SLionel Sambuc        int port, pri, weight;
20384d9c625SLionel Sambuc };
20484d9c625SLionel Sambuc 
20584d9c625SLionel Sambuc static int gai_srvok(const char *);
2062fe8fb19SBen Gras static int str2number(const char *);
2072fe8fb19SBen Gras static int explore_fqdn(const struct addrinfo *, const char *,
2082fe8fb19SBen Gras     const char *, struct addrinfo **, struct servent_data *);
2092fe8fb19SBen Gras static int explore_null(const struct addrinfo *,
2102fe8fb19SBen Gras     const char *, struct addrinfo **, struct servent_data *);
2112fe8fb19SBen Gras static int explore_numeric(const struct addrinfo *, const char *,
2122fe8fb19SBen Gras     const char *, struct addrinfo **, const char *, struct servent_data *);
2132fe8fb19SBen Gras static int explore_numeric_scope(const struct addrinfo *, const char *,
2142fe8fb19SBen Gras     const char *, struct addrinfo **, struct servent_data *);
2152fe8fb19SBen Gras static int get_canonname(const struct addrinfo *,
2162fe8fb19SBen Gras     struct addrinfo *, const char *);
2172fe8fb19SBen Gras static struct addrinfo *get_ai(const struct addrinfo *,
2182fe8fb19SBen Gras     const struct afd *, const char *);
2192fe8fb19SBen Gras static int get_portmatch(const struct addrinfo *, const char *,
2202fe8fb19SBen Gras     struct servent_data *);
2212fe8fb19SBen Gras static int get_port(const struct addrinfo *, const char *, int,
2222fe8fb19SBen Gras     struct servent_data *);
2232fe8fb19SBen Gras static const struct afd *find_afd(int);
224f14fb602SLionel Sambuc static int addrconfig(uint64_t *);
2252fe8fb19SBen Gras #ifdef INET6
2262fe8fb19SBen Gras static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
2272fe8fb19SBen Gras #endif
2282fe8fb19SBen Gras 
2292fe8fb19SBen Gras static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
2302fe8fb19SBen Gras     const struct addrinfo *);
2312fe8fb19SBen Gras static void aisort(struct addrinfo *s, res_state res);
23284d9c625SLionel Sambuc static struct addrinfo * _dns_query(struct res_target *,
23384d9c625SLionel Sambuc     const struct addrinfo *, res_state, int);
23484d9c625SLionel Sambuc static struct addrinfo * _dns_srv_lookup(const char *, const char *,
23584d9c625SLionel Sambuc     const struct addrinfo *);
23684d9c625SLionel Sambuc static struct addrinfo * _dns_host_lookup(const char *,
23784d9c625SLionel Sambuc     const struct addrinfo *);
2382fe8fb19SBen Gras static int _dns_getaddrinfo(void *, void *, va_list);
2392fe8fb19SBen Gras static void _sethtent(FILE **);
2402fe8fb19SBen Gras static void _endhtent(FILE **);
2412fe8fb19SBen Gras static struct addrinfo *_gethtent(FILE **, const char *,
2422fe8fb19SBen Gras     const struct addrinfo *);
2432fe8fb19SBen Gras static int _files_getaddrinfo(void *, void *, va_list);
2442fe8fb19SBen Gras #ifdef YP
2452fe8fb19SBen Gras static struct addrinfo *_yphostent(char *, const struct addrinfo *);
2462fe8fb19SBen Gras static int _yp_getaddrinfo(void *, void *, va_list);
2472fe8fb19SBen Gras #endif
2482fe8fb19SBen Gras 
2492fe8fb19SBen Gras static int res_queryN(const char *, struct res_target *, res_state);
2502fe8fb19SBen Gras static int res_searchN(const char *, struct res_target *, res_state);
2512fe8fb19SBen Gras static int res_querydomainN(const char *, const char *,
2522fe8fb19SBen Gras     struct res_target *, res_state);
2532fe8fb19SBen Gras 
2542fe8fb19SBen Gras static const char * const ai_errlist[] = {
2552fe8fb19SBen Gras 	"Success",
2562fe8fb19SBen Gras 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
2572fe8fb19SBen Gras 	"Temporary failure in name resolution",		/* EAI_AGAIN	  */
2582fe8fb19SBen Gras 	"Invalid value for ai_flags",			/* EAI_BADFLAGS	  */
2592fe8fb19SBen Gras 	"Non-recoverable failure in name resolution",	/* EAI_FAIL	  */
2602fe8fb19SBen Gras 	"ai_family not supported",			/* EAI_FAMILY	  */
2612fe8fb19SBen Gras 	"Memory allocation failure",			/* EAI_MEMORY	  */
2622fe8fb19SBen Gras 	"No address associated with hostname",		/* EAI_NODATA	  */
2632fe8fb19SBen Gras 	"hostname nor servname provided, or not known", /* EAI_NONAME	  */
2642fe8fb19SBen Gras 	"servname not supported for ai_socktype",	/* EAI_SERVICE	  */
2652fe8fb19SBen Gras 	"ai_socktype not supported",			/* EAI_SOCKTYPE	  */
2662fe8fb19SBen Gras 	"System error returned in errno",		/* EAI_SYSTEM	  */
2672fe8fb19SBen Gras 	"Invalid value for hints",			/* EAI_BADHINTS	  */
2682fe8fb19SBen Gras 	"Resolved protocol is unknown",			/* EAI_PROTOCOL	  */
2692fe8fb19SBen Gras 	"Argument buffer overflow",			/* EAI_OVERFLOW	  */
2702fe8fb19SBen Gras 	"Unknown error",				/* EAI_MAX	  */
2712fe8fb19SBen Gras };
2722fe8fb19SBen Gras 
2732fe8fb19SBen Gras /* XXX macros that make external reference is BAD. */
2742fe8fb19SBen Gras 
2752fe8fb19SBen Gras #define GET_AI(ai, afd, addr)					\
2762fe8fb19SBen Gras do {								\
2772fe8fb19SBen Gras 	/* external reference: pai, error, and label free */	\
2782fe8fb19SBen Gras 	(ai) = get_ai(pai, (afd), (addr));			\
2792fe8fb19SBen Gras 	if ((ai) == NULL) {					\
2802fe8fb19SBen Gras 		error = EAI_MEMORY;				\
2812fe8fb19SBen Gras 		goto free;					\
2822fe8fb19SBen Gras 	}							\
2832fe8fb19SBen Gras } while (/*CONSTCOND*/0)
2842fe8fb19SBen Gras 
2852fe8fb19SBen Gras #define GET_PORT(ai, serv, svd)					\
2862fe8fb19SBen Gras do {								\
2872fe8fb19SBen Gras 	/* external reference: error and label free */		\
2882fe8fb19SBen Gras 	error = get_port((ai), (serv), 0, (svd));		\
2892fe8fb19SBen Gras 	if (error != 0)						\
2902fe8fb19SBen Gras 		goto free;					\
2912fe8fb19SBen Gras } while (/*CONSTCOND*/0)
2922fe8fb19SBen Gras 
2932fe8fb19SBen Gras #define GET_CANONNAME(ai, str)					\
2942fe8fb19SBen Gras do {								\
2952fe8fb19SBen Gras 	/* external reference: pai, error and label free */	\
2962fe8fb19SBen Gras 	error = get_canonname(pai, (ai), (str));		\
2972fe8fb19SBen Gras 	if (error != 0)						\
2982fe8fb19SBen Gras 		goto free;					\
2992fe8fb19SBen Gras } while (/*CONSTCOND*/0)
3002fe8fb19SBen Gras 
3012fe8fb19SBen Gras #define ERR(err)						\
3022fe8fb19SBen Gras do {								\
3032fe8fb19SBen Gras 	/* external reference: error, and label bad */		\
3042fe8fb19SBen Gras 	error = (err);						\
3052fe8fb19SBen Gras 	goto bad;						\
3062fe8fb19SBen Gras 	/*NOTREACHED*/						\
3072fe8fb19SBen Gras } while (/*CONSTCOND*/0)
3082fe8fb19SBen Gras 
3092fe8fb19SBen Gras #define MATCH_FAMILY(x, y, w)						\
3102fe8fb19SBen Gras 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC ||	\
3112fe8fb19SBen Gras 	    (y) == PF_UNSPEC)))
3122fe8fb19SBen Gras #define MATCH(x, y, w)							\
3132fe8fb19SBen Gras 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
3142fe8fb19SBen Gras 
3152fe8fb19SBen Gras const char *
gai_strerror(int ecode)3162fe8fb19SBen Gras gai_strerror(int ecode)
3172fe8fb19SBen Gras {
3182fe8fb19SBen Gras 	if (ecode < 0 || ecode > EAI_MAX)
3192fe8fb19SBen Gras 		ecode = EAI_MAX;
3202fe8fb19SBen Gras 	return ai_errlist[ecode];
3212fe8fb19SBen Gras }
3222fe8fb19SBen Gras 
3232fe8fb19SBen Gras void
freeaddrinfo(struct addrinfo * ai)3242fe8fb19SBen Gras freeaddrinfo(struct addrinfo *ai)
3252fe8fb19SBen Gras {
3262fe8fb19SBen Gras 	struct addrinfo *next;
3272fe8fb19SBen Gras 
3282fe8fb19SBen Gras 	_DIAGASSERT(ai != NULL);
3292fe8fb19SBen Gras 
3302fe8fb19SBen Gras 	do {
3312fe8fb19SBen Gras 		next = ai->ai_next;
3322fe8fb19SBen Gras 		if (ai->ai_canonname)
3332fe8fb19SBen Gras 			free(ai->ai_canonname);
3342fe8fb19SBen Gras 		/* no need to free(ai->ai_addr) */
3352fe8fb19SBen Gras 		free(ai);
3362fe8fb19SBen Gras 		ai = next;
3372fe8fb19SBen Gras 	} while (ai);
3382fe8fb19SBen Gras }
3392fe8fb19SBen Gras 
34084d9c625SLionel Sambuc /*
34184d9c625SLionel Sambuc  * We don't want localization to affect us
34284d9c625SLionel Sambuc  */
34384d9c625SLionel Sambuc #define PERIOD '.'
34484d9c625SLionel Sambuc #define hyphenchar(c) ((c) == '-')
34584d9c625SLionel Sambuc #define periodchar(c) ((c) == PERIOD)
34684d9c625SLionel Sambuc #define underschar(c) ((c) == '_')
34784d9c625SLionel Sambuc #define alphachar(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
34884d9c625SLionel Sambuc #define digitchar(c) ((c) >= '0' && (c) <= '9')
34984d9c625SLionel Sambuc 
35084d9c625SLionel Sambuc #define firstchar(c)  (alphachar(c) || digitchar(c) || underschar(c))
35184d9c625SLionel Sambuc #define lastchar(c)   (alphachar(c) || digitchar(c))
35284d9c625SLionel Sambuc #define middlechar(c) (lastchar(c) || hyphenchar(c))
35384d9c625SLionel Sambuc 
35484d9c625SLionel Sambuc static int
gai_srvok(const char * dn)35584d9c625SLionel Sambuc gai_srvok(const char *dn)
35684d9c625SLionel Sambuc {
35784d9c625SLionel Sambuc 	int nch, pch, ch;
35884d9c625SLionel Sambuc 
35984d9c625SLionel Sambuc 	for (pch = PERIOD, nch = ch = *dn++; ch != '\0'; pch = ch, ch = nch) {
36084d9c625SLionel Sambuc 		if (periodchar(ch))
36184d9c625SLionel Sambuc 			continue;
36284d9c625SLionel Sambuc 		if (periodchar(pch)) {
36384d9c625SLionel Sambuc 			if (!firstchar(ch))
36484d9c625SLionel Sambuc 				return 0;
36584d9c625SLionel Sambuc 		} else if (periodchar(nch) || nch == '\0') {
36684d9c625SLionel Sambuc 			if (!lastchar(ch))
36784d9c625SLionel Sambuc 				return 0;
36884d9c625SLionel Sambuc 		} else if (!middlechar(ch))
36984d9c625SLionel Sambuc 			return 0;
37084d9c625SLionel Sambuc        }
37184d9c625SLionel Sambuc        return 1;
37284d9c625SLionel Sambuc }
37384d9c625SLionel Sambuc 
37484d9c625SLionel Sambuc static in_port_t *
getport(struct addrinfo * ai)37584d9c625SLionel Sambuc getport(struct addrinfo *ai) {
37684d9c625SLionel Sambuc 	static in_port_t p;
37784d9c625SLionel Sambuc 
37884d9c625SLionel Sambuc 	switch (ai->ai_family) {
37984d9c625SLionel Sambuc 	case AF_INET:
38084d9c625SLionel Sambuc 		return &((struct sockaddr_in *)(void *)ai->ai_addr)->sin_port;
38184d9c625SLionel Sambuc #ifdef INET6
38284d9c625SLionel Sambuc 	case AF_INET6:
38384d9c625SLionel Sambuc 		return &((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_port;
38484d9c625SLionel Sambuc #endif
38584d9c625SLionel Sambuc 	default:
38684d9c625SLionel Sambuc 		p = 0;
38784d9c625SLionel Sambuc 		/* XXX: abort()? */
38884d9c625SLionel Sambuc 		return &p;
38984d9c625SLionel Sambuc 	}
39084d9c625SLionel Sambuc }
39184d9c625SLionel Sambuc 
3922fe8fb19SBen Gras static int
str2number(const char * p)3932fe8fb19SBen Gras str2number(const char *p)
3942fe8fb19SBen Gras {
3952fe8fb19SBen Gras 	char *ep;
3962fe8fb19SBen Gras 	unsigned long v;
3972fe8fb19SBen Gras 
3982fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
3992fe8fb19SBen Gras 
4002fe8fb19SBen Gras 	if (*p == '\0')
4012fe8fb19SBen Gras 		return -1;
4022fe8fb19SBen Gras 	ep = NULL;
4032fe8fb19SBen Gras 	errno = 0;
4042fe8fb19SBen Gras 	v = strtoul(p, &ep, 10);
405f14fb602SLionel Sambuc 	if (errno == 0 && ep && *ep == '\0' && v <= INT_MAX)
406f14fb602SLionel Sambuc 		return (int)v;
4072fe8fb19SBen Gras 	else
4082fe8fb19SBen Gras 		return -1;
4092fe8fb19SBen Gras }
4102fe8fb19SBen Gras 
4112fe8fb19SBen Gras int
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)4122fe8fb19SBen Gras getaddrinfo(const char *hostname, const char *servname,
4132fe8fb19SBen Gras     const struct addrinfo *hints, struct addrinfo **res)
4142fe8fb19SBen Gras {
4152fe8fb19SBen Gras 	struct addrinfo sentinel;
4162fe8fb19SBen Gras 	struct addrinfo *cur;
4172fe8fb19SBen Gras 	int error = 0;
4182fe8fb19SBen Gras 	struct addrinfo ai;
4192fe8fb19SBen Gras 	struct addrinfo ai0;
4202fe8fb19SBen Gras 	struct addrinfo *pai;
4212fe8fb19SBen Gras 	const struct explore *ex;
4222fe8fb19SBen Gras 	struct servent_data svd;
423f14fb602SLionel Sambuc 	uint64_t mask = (uint64_t)~0ULL;
4242fe8fb19SBen Gras 
4252fe8fb19SBen Gras 	/* hostname is allowed to be NULL */
4262fe8fb19SBen Gras 	/* servname is allowed to be NULL */
4272fe8fb19SBen Gras 	/* hints is allowed to be NULL */
4282fe8fb19SBen Gras 	_DIAGASSERT(res != NULL);
4292fe8fb19SBen Gras 
4302fe8fb19SBen Gras 	(void)memset(&svd, 0, sizeof(svd));
4312fe8fb19SBen Gras 	memset(&sentinel, 0, sizeof(sentinel));
4322fe8fb19SBen Gras 	cur = &sentinel;
4332fe8fb19SBen Gras 	memset(&ai, 0, sizeof(ai));
4342fe8fb19SBen Gras 	pai = &ai;
4352fe8fb19SBen Gras 	pai->ai_flags = 0;
4362fe8fb19SBen Gras 	pai->ai_family = PF_UNSPEC;
4372fe8fb19SBen Gras 	pai->ai_socktype = ANY;
4382fe8fb19SBen Gras 	pai->ai_protocol = ANY;
4392fe8fb19SBen Gras 	pai->ai_addrlen = 0;
4402fe8fb19SBen Gras 	pai->ai_canonname = NULL;
4412fe8fb19SBen Gras 	pai->ai_addr = NULL;
4422fe8fb19SBen Gras 	pai->ai_next = NULL;
4432fe8fb19SBen Gras 
4442fe8fb19SBen Gras 	if (hostname == NULL && servname == NULL)
4452fe8fb19SBen Gras 		return EAI_NONAME;
4462fe8fb19SBen Gras 	if (hints) {
4472fe8fb19SBen Gras 		/* error check for hints */
4482fe8fb19SBen Gras 		if (hints->ai_addrlen || hints->ai_canonname ||
4492fe8fb19SBen Gras 		    hints->ai_addr || hints->ai_next)
4502fe8fb19SBen Gras 			ERR(EAI_BADHINTS); /* xxx */
4512fe8fb19SBen Gras 		if (hints->ai_flags & ~AI_MASK)
4522fe8fb19SBen Gras 			ERR(EAI_BADFLAGS);
4532fe8fb19SBen Gras 		switch (hints->ai_family) {
4542fe8fb19SBen Gras 		case PF_UNSPEC:
4552fe8fb19SBen Gras 		case PF_INET:
4562fe8fb19SBen Gras #ifdef INET6
4572fe8fb19SBen Gras 		case PF_INET6:
4582fe8fb19SBen Gras #endif
4592fe8fb19SBen Gras 			break;
4602fe8fb19SBen Gras 		default:
4612fe8fb19SBen Gras 			ERR(EAI_FAMILY);
4622fe8fb19SBen Gras 		}
4632fe8fb19SBen Gras 		memcpy(pai, hints, sizeof(*pai));
4642fe8fb19SBen Gras 
4652fe8fb19SBen Gras 		/*
4662fe8fb19SBen Gras 		 * if both socktype/protocol are specified, check if they
4672fe8fb19SBen Gras 		 * are meaningful combination.
4682fe8fb19SBen Gras 		 */
4692fe8fb19SBen Gras 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
4702fe8fb19SBen Gras 			for (ex = explore; ex->e_af >= 0; ex++) {
4712fe8fb19SBen Gras 				if (pai->ai_family != ex->e_af)
4722fe8fb19SBen Gras 					continue;
4732fe8fb19SBen Gras 				if (ex->e_socktype == ANY)
4742fe8fb19SBen Gras 					continue;
4752fe8fb19SBen Gras 				if (ex->e_protocol == ANY)
4762fe8fb19SBen Gras 					continue;
4772fe8fb19SBen Gras 				if (pai->ai_socktype == ex->e_socktype
4782fe8fb19SBen Gras 				 && pai->ai_protocol != ex->e_protocol) {
4792fe8fb19SBen Gras 					ERR(EAI_BADHINTS);
4802fe8fb19SBen Gras 				}
4812fe8fb19SBen Gras 			}
4822fe8fb19SBen Gras 		}
4832fe8fb19SBen Gras 	}
4842fe8fb19SBen Gras 
485f14fb602SLionel Sambuc 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && addrconfig(&mask) == -1)
486f14fb602SLionel Sambuc 		ERR(EAI_FAIL);
487f14fb602SLionel Sambuc 
4882fe8fb19SBen Gras 	/*
4892fe8fb19SBen Gras 	 * check for special cases.  (1) numeric servname is disallowed if
4902fe8fb19SBen Gras 	 * socktype/protocol are left unspecified. (2) servname is disallowed
4912fe8fb19SBen Gras 	 * for raw and other inet{,6} sockets.
4922fe8fb19SBen Gras 	 */
4932fe8fb19SBen Gras 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
4942fe8fb19SBen Gras #ifdef PF_INET6
4952fe8fb19SBen Gras 	 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
4962fe8fb19SBen Gras #endif
4972fe8fb19SBen Gras 	    ) {
4982fe8fb19SBen Gras 		ai0 = *pai;	/* backup *pai */
4992fe8fb19SBen Gras 
5002fe8fb19SBen Gras 		if (pai->ai_family == PF_UNSPEC) {
5012fe8fb19SBen Gras #ifdef PF_INET6
5022fe8fb19SBen Gras 			pai->ai_family = PF_INET6;
5032fe8fb19SBen Gras #else
5042fe8fb19SBen Gras 			pai->ai_family = PF_INET;
5052fe8fb19SBen Gras #endif
5062fe8fb19SBen Gras 		}
5072fe8fb19SBen Gras 		error = get_portmatch(pai, servname, &svd);
5082fe8fb19SBen Gras 		if (error)
509f14fb602SLionel Sambuc 			goto bad;
5102fe8fb19SBen Gras 
5112fe8fb19SBen Gras 		*pai = ai0;
5122fe8fb19SBen Gras 	}
5132fe8fb19SBen Gras 
5142fe8fb19SBen Gras 	ai0 = *pai;
5152fe8fb19SBen Gras 
5162fe8fb19SBen Gras 	/* NULL hostname, or numeric hostname */
5172fe8fb19SBen Gras 	for (ex = explore; ex->e_af >= 0; ex++) {
5182fe8fb19SBen Gras 		*pai = ai0;
5192fe8fb19SBen Gras 
520f14fb602SLionel Sambuc 		/* ADDRCONFIG check */
521f14fb602SLionel Sambuc 		if ((((uint64_t)1 << ex->e_af) & mask) == 0)
522f14fb602SLionel Sambuc 			continue;
523f14fb602SLionel Sambuc 
5242fe8fb19SBen Gras 		/* PF_UNSPEC entries are prepared for DNS queries only */
5252fe8fb19SBen Gras 		if (ex->e_af == PF_UNSPEC)
5262fe8fb19SBen Gras 			continue;
5272fe8fb19SBen Gras 
5282fe8fb19SBen Gras 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
5292fe8fb19SBen Gras 			continue;
5302fe8fb19SBen Gras 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
5312fe8fb19SBen Gras 			continue;
5322fe8fb19SBen Gras 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
5332fe8fb19SBen Gras 			continue;
5342fe8fb19SBen Gras 		if (pai->ai_family == PF_UNSPEC)
5352fe8fb19SBen Gras 			pai->ai_family = ex->e_af;
5362fe8fb19SBen Gras 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
5372fe8fb19SBen Gras 			pai->ai_socktype = ex->e_socktype;
5382fe8fb19SBen Gras 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
5392fe8fb19SBen Gras 			pai->ai_protocol = ex->e_protocol;
5402fe8fb19SBen Gras 
5412fe8fb19SBen Gras 		if (hostname == NULL)
5422fe8fb19SBen Gras 			error = explore_null(pai, servname, &cur->ai_next,
5432fe8fb19SBen Gras 			    &svd);
5442fe8fb19SBen Gras 		else
5452fe8fb19SBen Gras 			error = explore_numeric_scope(pai, hostname, servname,
5462fe8fb19SBen Gras 			    &cur->ai_next, &svd);
5472fe8fb19SBen Gras 
5482fe8fb19SBen Gras 		if (error)
5492fe8fb19SBen Gras 			goto free;
5502fe8fb19SBen Gras 
5512fe8fb19SBen Gras 		while (cur->ai_next)
5522fe8fb19SBen Gras 			cur = cur->ai_next;
5532fe8fb19SBen Gras 	}
5542fe8fb19SBen Gras 
5552fe8fb19SBen Gras 	/*
5562fe8fb19SBen Gras 	 * XXX
5572fe8fb19SBen Gras 	 * If numeric representation of AF1 can be interpreted as FQDN
5582fe8fb19SBen Gras 	 * representation of AF2, we need to think again about the code below.
5592fe8fb19SBen Gras 	 */
5602fe8fb19SBen Gras 	if (sentinel.ai_next)
5612fe8fb19SBen Gras 		goto good;
5622fe8fb19SBen Gras 
5632fe8fb19SBen Gras 	if (hostname == NULL)
5642fe8fb19SBen Gras 		ERR(EAI_NODATA);
5652fe8fb19SBen Gras 	if (pai->ai_flags & AI_NUMERICHOST)
5662fe8fb19SBen Gras 		ERR(EAI_NONAME);
5672fe8fb19SBen Gras 
5682fe8fb19SBen Gras 	/*
5692fe8fb19SBen Gras 	 * hostname as alphabetical name.
5702fe8fb19SBen Gras 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
5712fe8fb19SBen Gras 	 * outer loop by AFs.
5722fe8fb19SBen Gras 	 */
5732fe8fb19SBen Gras 	for (ex = explore; ex->e_af >= 0; ex++) {
5742fe8fb19SBen Gras 		*pai = ai0;
5752fe8fb19SBen Gras 
576f14fb602SLionel Sambuc 
577f14fb602SLionel Sambuc 		/* ADDRCONFIG check */
578f14fb602SLionel Sambuc 		/* PF_UNSPEC entries are prepared for DNS queries only */
579f14fb602SLionel Sambuc 		if (ex->e_af != PF_UNSPEC &&
580f14fb602SLionel Sambuc 		    (((uint64_t)1 << ex->e_af) & mask) == 0)
581f14fb602SLionel Sambuc 			continue;
582f14fb602SLionel Sambuc 
5832fe8fb19SBen Gras 		/* require exact match for family field */
5842fe8fb19SBen Gras 		if (pai->ai_family != ex->e_af)
5852fe8fb19SBen Gras 			continue;
5862fe8fb19SBen Gras 
5872fe8fb19SBen Gras 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
5882fe8fb19SBen Gras 				WILD_SOCKTYPE(ex))) {
5892fe8fb19SBen Gras 			continue;
5902fe8fb19SBen Gras 		}
5912fe8fb19SBen Gras 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
5922fe8fb19SBen Gras 				WILD_PROTOCOL(ex))) {
5932fe8fb19SBen Gras 			continue;
5942fe8fb19SBen Gras 		}
5952fe8fb19SBen Gras 
5962fe8fb19SBen Gras 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
5972fe8fb19SBen Gras 			pai->ai_socktype = ex->e_socktype;
5982fe8fb19SBen Gras 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
5992fe8fb19SBen Gras 			pai->ai_protocol = ex->e_protocol;
6002fe8fb19SBen Gras 
6012fe8fb19SBen Gras 		error = explore_fqdn(pai, hostname, servname, &cur->ai_next,
6022fe8fb19SBen Gras 		    &svd);
6032fe8fb19SBen Gras 
6042fe8fb19SBen Gras 		while (cur && cur->ai_next)
6052fe8fb19SBen Gras 			cur = cur->ai_next;
6062fe8fb19SBen Gras 	}
6072fe8fb19SBen Gras 
6082fe8fb19SBen Gras 	/* XXX */
6092fe8fb19SBen Gras 	if (sentinel.ai_next)
6102fe8fb19SBen Gras 		error = 0;
6112fe8fb19SBen Gras 
6122fe8fb19SBen Gras 	if (error)
6132fe8fb19SBen Gras 		goto free;
6142fe8fb19SBen Gras 
6152fe8fb19SBen Gras 	if (sentinel.ai_next) {
6162fe8fb19SBen Gras  good:
6172fe8fb19SBen Gras 		endservent_r(&svd);
6182fe8fb19SBen Gras 		*res = sentinel.ai_next;
6192fe8fb19SBen Gras 		return SUCCESS;
6202fe8fb19SBen Gras 	} else
6212fe8fb19SBen Gras 		error = EAI_FAIL;
6222fe8fb19SBen Gras  free:
6232fe8fb19SBen Gras  bad:
6242fe8fb19SBen Gras 	endservent_r(&svd);
6252fe8fb19SBen Gras 	if (sentinel.ai_next)
6262fe8fb19SBen Gras 		freeaddrinfo(sentinel.ai_next);
6272fe8fb19SBen Gras 	*res = NULL;
6282fe8fb19SBen Gras 	return error;
6292fe8fb19SBen Gras }
6302fe8fb19SBen Gras 
6312fe8fb19SBen Gras /*
6322fe8fb19SBen Gras  * FQDN hostname, DNS lookup
6332fe8fb19SBen Gras  */
6342fe8fb19SBen Gras static int
explore_fqdn(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res,struct servent_data * svd)6352fe8fb19SBen Gras explore_fqdn(const struct addrinfo *pai, const char *hostname,
6362fe8fb19SBen Gras     const char *servname, struct addrinfo **res, struct servent_data *svd)
6372fe8fb19SBen Gras {
6382fe8fb19SBen Gras 	struct addrinfo *result;
6392fe8fb19SBen Gras 	struct addrinfo *cur;
6402fe8fb19SBen Gras 	int error = 0;
6412fe8fb19SBen Gras 	static const ns_dtab dtab[] = {
6422fe8fb19SBen Gras 		NS_FILES_CB(_files_getaddrinfo, NULL)
6432fe8fb19SBen Gras 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
6442fe8fb19SBen Gras 		NS_NIS_CB(_yp_getaddrinfo, NULL)
6452fe8fb19SBen Gras 		NS_NULL_CB
6462fe8fb19SBen Gras 	};
6472fe8fb19SBen Gras 
6482fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
6492fe8fb19SBen Gras 	/* hostname may be NULL */
6502fe8fb19SBen Gras 	/* servname may be NULL */
6512fe8fb19SBen Gras 	_DIAGASSERT(res != NULL);
6522fe8fb19SBen Gras 
6532fe8fb19SBen Gras 	result = NULL;
6542fe8fb19SBen Gras 
6552fe8fb19SBen Gras 	/*
6562fe8fb19SBen Gras 	 * if the servname does not match socktype/protocol, ignore it.
6572fe8fb19SBen Gras 	 */
6582fe8fb19SBen Gras 	if (get_portmatch(pai, servname, svd) != 0)
6592fe8fb19SBen Gras 		return 0;
6602fe8fb19SBen Gras 
6612fe8fb19SBen Gras 	switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
66284d9c625SLionel Sambuc 	    default_dns_files, hostname, pai, servname)) {
6632fe8fb19SBen Gras 	case NS_TRYAGAIN:
6642fe8fb19SBen Gras 		error = EAI_AGAIN;
6652fe8fb19SBen Gras 		goto free;
6662fe8fb19SBen Gras 	case NS_UNAVAIL:
6672fe8fb19SBen Gras 		error = EAI_FAIL;
6682fe8fb19SBen Gras 		goto free;
6692fe8fb19SBen Gras 	case NS_NOTFOUND:
6702fe8fb19SBen Gras 		error = EAI_NODATA;
6712fe8fb19SBen Gras 		goto free;
6722fe8fb19SBen Gras 	case NS_SUCCESS:
6732fe8fb19SBen Gras 		error = 0;
6742fe8fb19SBen Gras 		for (cur = result; cur; cur = cur->ai_next) {
67584d9c625SLionel Sambuc 			/* Check for already filled port. */
67684d9c625SLionel Sambuc 			if (*getport(cur))
67784d9c625SLionel Sambuc 				continue;
6782fe8fb19SBen Gras 			GET_PORT(cur, servname, svd);
6792fe8fb19SBen Gras 			/* canonname should be filled already */
6802fe8fb19SBen Gras 		}
6812fe8fb19SBen Gras 		break;
6822fe8fb19SBen Gras 	}
6832fe8fb19SBen Gras 
6842fe8fb19SBen Gras 	*res = result;
6852fe8fb19SBen Gras 
6862fe8fb19SBen Gras 	return 0;
6872fe8fb19SBen Gras 
6882fe8fb19SBen Gras free:
6892fe8fb19SBen Gras 	if (result)
6902fe8fb19SBen Gras 		freeaddrinfo(result);
6912fe8fb19SBen Gras 	return error;
6922fe8fb19SBen Gras }
6932fe8fb19SBen Gras 
6942fe8fb19SBen Gras /*
6952fe8fb19SBen Gras  * hostname == NULL.
6962fe8fb19SBen Gras  * passive socket -> anyaddr (0.0.0.0 or ::)
6972fe8fb19SBen Gras  * non-passive socket -> localhost (127.0.0.1 or ::1)
6982fe8fb19SBen Gras  */
6992fe8fb19SBen Gras static int
explore_null(const struct addrinfo * pai,const char * servname,struct addrinfo ** res,struct servent_data * svd)7002fe8fb19SBen Gras explore_null(const struct addrinfo *pai, const char *servname,
7012fe8fb19SBen Gras     struct addrinfo **res, struct servent_data *svd)
7022fe8fb19SBen Gras {
7032fe8fb19SBen Gras 	int s;
7042fe8fb19SBen Gras 	const struct afd *afd;
7052fe8fb19SBen Gras 	struct addrinfo *cur;
7062fe8fb19SBen Gras 	struct addrinfo sentinel;
7072fe8fb19SBen Gras 	int error;
7082fe8fb19SBen Gras 
7092fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
7102fe8fb19SBen Gras 	/* servname may be NULL */
7112fe8fb19SBen Gras 	_DIAGASSERT(res != NULL);
7122fe8fb19SBen Gras 
7132fe8fb19SBen Gras 	*res = NULL;
7142fe8fb19SBen Gras 	sentinel.ai_next = NULL;
7152fe8fb19SBen Gras 	cur = &sentinel;
7162fe8fb19SBen Gras 
7172fe8fb19SBen Gras 	/*
7182fe8fb19SBen Gras 	 * filter out AFs that are not supported by the kernel
7192fe8fb19SBen Gras 	 * XXX errno?
7202fe8fb19SBen Gras 	 */
7212fe8fb19SBen Gras 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
7222fe8fb19SBen Gras 	if (s < 0) {
7232fe8fb19SBen Gras 		if (errno != EMFILE)
7242fe8fb19SBen Gras 			return 0;
7252fe8fb19SBen Gras 	} else
7262fe8fb19SBen Gras 		close(s);
7272fe8fb19SBen Gras 
7282fe8fb19SBen Gras 	/*
7292fe8fb19SBen Gras 	 * if the servname does not match socktype/protocol, ignore it.
7302fe8fb19SBen Gras 	 */
7312fe8fb19SBen Gras 	if (get_portmatch(pai, servname, svd) != 0)
7322fe8fb19SBen Gras 		return 0;
7332fe8fb19SBen Gras 
7342fe8fb19SBen Gras 	afd = find_afd(pai->ai_family);
7352fe8fb19SBen Gras 	if (afd == NULL)
7362fe8fb19SBen Gras 		return 0;
7372fe8fb19SBen Gras 
7382fe8fb19SBen Gras 	if (pai->ai_flags & AI_PASSIVE) {
7392fe8fb19SBen Gras 		GET_AI(cur->ai_next, afd, afd->a_addrany);
7402fe8fb19SBen Gras 		/* xxx meaningless?
7412fe8fb19SBen Gras 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
7422fe8fb19SBen Gras 		 */
7432fe8fb19SBen Gras 		GET_PORT(cur->ai_next, servname, svd);
7442fe8fb19SBen Gras 	} else {
7452fe8fb19SBen Gras 		GET_AI(cur->ai_next, afd, afd->a_loopback);
7462fe8fb19SBen Gras 		/* xxx meaningless?
7472fe8fb19SBen Gras 		 * GET_CANONNAME(cur->ai_next, "localhost");
7482fe8fb19SBen Gras 		 */
7492fe8fb19SBen Gras 		GET_PORT(cur->ai_next, servname, svd);
7502fe8fb19SBen Gras 	}
7512fe8fb19SBen Gras 	cur = cur->ai_next;
7522fe8fb19SBen Gras 
7532fe8fb19SBen Gras 	*res = sentinel.ai_next;
7542fe8fb19SBen Gras 	return 0;
7552fe8fb19SBen Gras 
7562fe8fb19SBen Gras free:
7572fe8fb19SBen Gras 	if (sentinel.ai_next)
7582fe8fb19SBen Gras 		freeaddrinfo(sentinel.ai_next);
7592fe8fb19SBen Gras 	return error;
7602fe8fb19SBen Gras }
7612fe8fb19SBen Gras 
7622fe8fb19SBen Gras /*
7632fe8fb19SBen Gras  * numeric hostname
7642fe8fb19SBen Gras  */
7652fe8fb19SBen Gras static int
explore_numeric(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res,const char * canonname,struct servent_data * svd)7662fe8fb19SBen Gras explore_numeric(const struct addrinfo *pai, const char *hostname,
7672fe8fb19SBen Gras     const char *servname, struct addrinfo **res, const char *canonname,
7682fe8fb19SBen Gras     struct servent_data *svd)
7692fe8fb19SBen Gras {
7702fe8fb19SBen Gras 	const struct afd *afd;
7712fe8fb19SBen Gras 	struct addrinfo *cur;
7722fe8fb19SBen Gras 	struct addrinfo sentinel;
7732fe8fb19SBen Gras 	int error;
7742fe8fb19SBen Gras 	char pton[PTON_MAX];
7752fe8fb19SBen Gras 
7762fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
7772fe8fb19SBen Gras 	/* hostname may be NULL */
7782fe8fb19SBen Gras 	/* servname may be NULL */
7792fe8fb19SBen Gras 	_DIAGASSERT(res != NULL);
7802fe8fb19SBen Gras 
7812fe8fb19SBen Gras 	*res = NULL;
7822fe8fb19SBen Gras 	sentinel.ai_next = NULL;
7832fe8fb19SBen Gras 	cur = &sentinel;
7842fe8fb19SBen Gras 
7852fe8fb19SBen Gras 	/*
7862fe8fb19SBen Gras 	 * if the servname does not match socktype/protocol, ignore it.
7872fe8fb19SBen Gras 	 */
7882fe8fb19SBen Gras 	if (get_portmatch(pai, servname, svd) != 0)
7892fe8fb19SBen Gras 		return 0;
7902fe8fb19SBen Gras 
7912fe8fb19SBen Gras 	afd = find_afd(pai->ai_family);
7922fe8fb19SBen Gras 	if (afd == NULL)
7932fe8fb19SBen Gras 		return 0;
7942fe8fb19SBen Gras 
7952fe8fb19SBen Gras 	switch (afd->a_af) {
7962fe8fb19SBen Gras #if 0 /*X/Open spec*/
7972fe8fb19SBen Gras 	case AF_INET:
7982fe8fb19SBen Gras 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
7992fe8fb19SBen Gras 			if (pai->ai_family == afd->a_af ||
8002fe8fb19SBen Gras 			    pai->ai_family == PF_UNSPEC /*?*/) {
8012fe8fb19SBen Gras 				GET_AI(cur->ai_next, afd, pton);
8022fe8fb19SBen Gras 				GET_PORT(cur->ai_next, servname, svd);
8032fe8fb19SBen Gras 				if ((pai->ai_flags & AI_CANONNAME)) {
8042fe8fb19SBen Gras 					/*
8052fe8fb19SBen Gras 					 * Set the numeric address itself as
8062fe8fb19SBen Gras 					 * the canonical name, based on a
8072fe8fb19SBen Gras 					 * clarification in rfc2553bis-03.
8082fe8fb19SBen Gras 					 */
8092fe8fb19SBen Gras 					GET_CANONNAME(cur->ai_next, canonname);
8102fe8fb19SBen Gras 				}
8112fe8fb19SBen Gras 				while (cur && cur->ai_next)
8122fe8fb19SBen Gras 					cur = cur->ai_next;
8132fe8fb19SBen Gras 			} else
8142fe8fb19SBen Gras 				ERR(EAI_FAMILY);	/*xxx*/
8152fe8fb19SBen Gras 		}
8162fe8fb19SBen Gras 		break;
8172fe8fb19SBen Gras #endif
8182fe8fb19SBen Gras 	default:
8192fe8fb19SBen Gras 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
8202fe8fb19SBen Gras 			if (pai->ai_family == afd->a_af ||
8212fe8fb19SBen Gras 			    pai->ai_family == PF_UNSPEC /*?*/) {
8222fe8fb19SBen Gras 				GET_AI(cur->ai_next, afd, pton);
8232fe8fb19SBen Gras 				GET_PORT(cur->ai_next, servname, svd);
8242fe8fb19SBen Gras 				if ((pai->ai_flags & AI_CANONNAME)) {
8252fe8fb19SBen Gras 					/*
8262fe8fb19SBen Gras 					 * Set the numeric address itself as
8272fe8fb19SBen Gras 					 * the canonical name, based on a
8282fe8fb19SBen Gras 					 * clarification in rfc2553bis-03.
8292fe8fb19SBen Gras 					 */
8302fe8fb19SBen Gras 					GET_CANONNAME(cur->ai_next, canonname);
8312fe8fb19SBen Gras 				}
8322fe8fb19SBen Gras 				while (cur->ai_next)
8332fe8fb19SBen Gras 					cur = cur->ai_next;
8342fe8fb19SBen Gras 			} else
8352fe8fb19SBen Gras 				ERR(EAI_FAMILY);	/*xxx*/
8362fe8fb19SBen Gras 		}
8372fe8fb19SBen Gras 		break;
8382fe8fb19SBen Gras 	}
8392fe8fb19SBen Gras 
8402fe8fb19SBen Gras 	*res = sentinel.ai_next;
8412fe8fb19SBen Gras 	return 0;
8422fe8fb19SBen Gras 
8432fe8fb19SBen Gras free:
8442fe8fb19SBen Gras bad:
8452fe8fb19SBen Gras 	if (sentinel.ai_next)
8462fe8fb19SBen Gras 		freeaddrinfo(sentinel.ai_next);
8472fe8fb19SBen Gras 	return error;
8482fe8fb19SBen Gras }
8492fe8fb19SBen Gras 
8502fe8fb19SBen Gras /*
8512fe8fb19SBen Gras  * numeric hostname with scope
8522fe8fb19SBen Gras  */
8532fe8fb19SBen Gras static int
explore_numeric_scope(const struct addrinfo * pai,const char * hostname,const char * servname,struct addrinfo ** res,struct servent_data * svd)8542fe8fb19SBen Gras explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
8552fe8fb19SBen Gras     const char *servname, struct addrinfo **res, struct servent_data *svd)
8562fe8fb19SBen Gras {
8572fe8fb19SBen Gras #if !defined(SCOPE_DELIMITER) || !defined(INET6)
8582fe8fb19SBen Gras 	return explore_numeric(pai, hostname, servname, res, hostname, svd);
8592fe8fb19SBen Gras #else
8602fe8fb19SBen Gras 	const struct afd *afd;
8612fe8fb19SBen Gras 	struct addrinfo *cur;
8622fe8fb19SBen Gras 	int error;
8632fe8fb19SBen Gras 	char *cp, *hostname2 = NULL, *scope, *addr;
8642fe8fb19SBen Gras 	struct sockaddr_in6 *sin6;
8652fe8fb19SBen Gras 
8662fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
8672fe8fb19SBen Gras 	/* hostname may be NULL */
8682fe8fb19SBen Gras 	/* servname may be NULL */
8692fe8fb19SBen Gras 	_DIAGASSERT(res != NULL);
8702fe8fb19SBen Gras 
8712fe8fb19SBen Gras 	/*
8722fe8fb19SBen Gras 	 * if the servname does not match socktype/protocol, ignore it.
8732fe8fb19SBen Gras 	 */
8742fe8fb19SBen Gras 	if (get_portmatch(pai, servname, svd) != 0)
8752fe8fb19SBen Gras 		return 0;
8762fe8fb19SBen Gras 
8772fe8fb19SBen Gras 	afd = find_afd(pai->ai_family);
8782fe8fb19SBen Gras 	if (afd == NULL)
8792fe8fb19SBen Gras 		return 0;
8802fe8fb19SBen Gras 
8812fe8fb19SBen Gras 	if (!afd->a_scoped)
8822fe8fb19SBen Gras 		return explore_numeric(pai, hostname, servname, res, hostname,
8832fe8fb19SBen Gras 		    svd);
8842fe8fb19SBen Gras 
8852fe8fb19SBen Gras 	cp = strchr(hostname, SCOPE_DELIMITER);
8862fe8fb19SBen Gras 	if (cp == NULL)
8872fe8fb19SBen Gras 		return explore_numeric(pai, hostname, servname, res, hostname,
8882fe8fb19SBen Gras 		    svd);
8892fe8fb19SBen Gras 
8902fe8fb19SBen Gras 	/*
8912fe8fb19SBen Gras 	 * Handle special case of <scoped_address><delimiter><scope id>
8922fe8fb19SBen Gras 	 */
8932fe8fb19SBen Gras 	hostname2 = strdup(hostname);
8942fe8fb19SBen Gras 	if (hostname2 == NULL)
8952fe8fb19SBen Gras 		return EAI_MEMORY;
8962fe8fb19SBen Gras 	/* terminate at the delimiter */
8972fe8fb19SBen Gras 	hostname2[cp - hostname] = '\0';
8982fe8fb19SBen Gras 	addr = hostname2;
8992fe8fb19SBen Gras 	scope = cp + 1;
9002fe8fb19SBen Gras 
9012fe8fb19SBen Gras 	error = explore_numeric(pai, addr, servname, res, hostname, svd);
9022fe8fb19SBen Gras 	if (error == 0) {
9032fe8fb19SBen Gras 		u_int32_t scopeid;
9042fe8fb19SBen Gras 
9052fe8fb19SBen Gras 		for (cur = *res; cur; cur = cur->ai_next) {
9062fe8fb19SBen Gras 			if (cur->ai_family != AF_INET6)
9072fe8fb19SBen Gras 				continue;
9082fe8fb19SBen Gras 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
9092fe8fb19SBen Gras 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
9102fe8fb19SBen Gras 				free(hostname2);
9112fe8fb19SBen Gras 				return(EAI_NODATA); /* XXX: is return OK? */
9122fe8fb19SBen Gras 			}
9132fe8fb19SBen Gras 			sin6->sin6_scope_id = scopeid;
9142fe8fb19SBen Gras 		}
9152fe8fb19SBen Gras 	}
9162fe8fb19SBen Gras 
9172fe8fb19SBen Gras 	free(hostname2);
9182fe8fb19SBen Gras 
9192fe8fb19SBen Gras 	return error;
9202fe8fb19SBen Gras #endif
9212fe8fb19SBen Gras }
9222fe8fb19SBen Gras 
9232fe8fb19SBen Gras static int
get_canonname(const struct addrinfo * pai,struct addrinfo * ai,const char * str)9242fe8fb19SBen Gras get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
9252fe8fb19SBen Gras {
9262fe8fb19SBen Gras 
9272fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
9282fe8fb19SBen Gras 	_DIAGASSERT(ai != NULL);
9292fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
9302fe8fb19SBen Gras 
9312fe8fb19SBen Gras 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
9322fe8fb19SBen Gras 		ai->ai_canonname = strdup(str);
9332fe8fb19SBen Gras 		if (ai->ai_canonname == NULL)
9342fe8fb19SBen Gras 			return EAI_MEMORY;
9352fe8fb19SBen Gras 	}
9362fe8fb19SBen Gras 	return 0;
9372fe8fb19SBen Gras }
9382fe8fb19SBen Gras 
9392fe8fb19SBen Gras struct addrinfo *
allocaddrinfo(socklen_t addrlen)9402fe8fb19SBen Gras allocaddrinfo(socklen_t addrlen)
9412fe8fb19SBen Gras {
9422fe8fb19SBen Gras 	struct addrinfo *ai;
9432fe8fb19SBen Gras 
9442fe8fb19SBen Gras 	ai = calloc(sizeof(struct addrinfo) + addrlen, 1);
9452fe8fb19SBen Gras 	if (ai) {
9462fe8fb19SBen Gras 		ai->ai_addr = (void *)(ai+1);
9472fe8fb19SBen Gras 		ai->ai_addrlen = ai->ai_addr->sa_len = addrlen;
9482fe8fb19SBen Gras 	}
9492fe8fb19SBen Gras 
9502fe8fb19SBen Gras 	return ai;
9512fe8fb19SBen Gras }
9522fe8fb19SBen Gras 
9532fe8fb19SBen Gras static struct addrinfo *
get_ai(const struct addrinfo * pai,const struct afd * afd,const char * addr)9542fe8fb19SBen Gras get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
9552fe8fb19SBen Gras {
9562fe8fb19SBen Gras 	char *p;
9572fe8fb19SBen Gras 	struct addrinfo *ai;
9582fe8fb19SBen Gras 	struct sockaddr *save;
9592fe8fb19SBen Gras 
9602fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
9612fe8fb19SBen Gras 	_DIAGASSERT(afd != NULL);
9622fe8fb19SBen Gras 	_DIAGASSERT(addr != NULL);
9632fe8fb19SBen Gras 
9642fe8fb19SBen Gras 	ai = allocaddrinfo((socklen_t)afd->a_socklen);
9652fe8fb19SBen Gras 	if (ai == NULL)
9662fe8fb19SBen Gras 		return NULL;
9672fe8fb19SBen Gras 
9682fe8fb19SBen Gras 	save = ai->ai_addr;
9692fe8fb19SBen Gras 	memcpy(ai, pai, sizeof(struct addrinfo));
9702fe8fb19SBen Gras 
9712fe8fb19SBen Gras 	/* since we just overwrote all of ai, we have
9722fe8fb19SBen Gras 	   to restore ai_addr and ai_addrlen */
9732fe8fb19SBen Gras 	ai->ai_addr = save;
9742fe8fb19SBen Gras 	ai->ai_addrlen = (socklen_t)afd->a_socklen;
9752fe8fb19SBen Gras 
9762fe8fb19SBen Gras 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
9772fe8fb19SBen Gras 	p = (char *)(void *)(ai->ai_addr);
9782fe8fb19SBen Gras 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
9792fe8fb19SBen Gras 	return ai;
9802fe8fb19SBen Gras }
9812fe8fb19SBen Gras 
9822fe8fb19SBen Gras static int
get_portmatch(const struct addrinfo * ai,const char * servname,struct servent_data * svd)9832fe8fb19SBen Gras get_portmatch(const struct addrinfo *ai, const char *servname,
9842fe8fb19SBen Gras     struct servent_data *svd)
9852fe8fb19SBen Gras {
9862fe8fb19SBen Gras 
9872fe8fb19SBen Gras 	_DIAGASSERT(ai != NULL);
9882fe8fb19SBen Gras 	/* servname may be NULL */
9892fe8fb19SBen Gras 
9902fe8fb19SBen Gras 	return get_port(ai, servname, 1, svd);
9912fe8fb19SBen Gras }
9922fe8fb19SBen Gras 
9932fe8fb19SBen Gras static int
get_port(const struct addrinfo * ai,const char * servname,int matchonly,struct servent_data * svd)9942fe8fb19SBen Gras get_port(const struct addrinfo *ai, const char *servname, int matchonly,
9952fe8fb19SBen Gras     struct servent_data *svd)
9962fe8fb19SBen Gras {
9972fe8fb19SBen Gras 	const char *proto;
9982fe8fb19SBen Gras 	struct servent *sp;
9992fe8fb19SBen Gras 	int port;
10002fe8fb19SBen Gras 	int allownumeric;
10012fe8fb19SBen Gras 
10022fe8fb19SBen Gras 	_DIAGASSERT(ai != NULL);
10032fe8fb19SBen Gras 	/* servname may be NULL */
10042fe8fb19SBen Gras 
10052fe8fb19SBen Gras 	if (servname == NULL)
10062fe8fb19SBen Gras 		return 0;
10072fe8fb19SBen Gras 	switch (ai->ai_family) {
10082fe8fb19SBen Gras 	case AF_INET:
10092fe8fb19SBen Gras #ifdef AF_INET6
10102fe8fb19SBen Gras 	case AF_INET6:
10112fe8fb19SBen Gras #endif
10122fe8fb19SBen Gras 		break;
10132fe8fb19SBen Gras 	default:
10142fe8fb19SBen Gras 		return 0;
10152fe8fb19SBen Gras 	}
10162fe8fb19SBen Gras 
10172fe8fb19SBen Gras 	switch (ai->ai_socktype) {
10182fe8fb19SBen Gras 	case SOCK_RAW:
10192fe8fb19SBen Gras 		return EAI_SERVICE;
10202fe8fb19SBen Gras 	case SOCK_DGRAM:
10212fe8fb19SBen Gras 	case SOCK_STREAM:
10222fe8fb19SBen Gras 		allownumeric = 1;
10232fe8fb19SBen Gras 		break;
10242fe8fb19SBen Gras 	case ANY:
10252fe8fb19SBen Gras 		/*
10262fe8fb19SBen Gras 		 * This was 0.	It is now 1 so that queries specifying
10272fe8fb19SBen Gras 		 * a NULL hint, or hint without socktype (but, hopefully,
10282fe8fb19SBen Gras 		 * with protocol) and numeric address actually work.
10292fe8fb19SBen Gras 		 */
10302fe8fb19SBen Gras 		allownumeric = 1;
10312fe8fb19SBen Gras 		break;
10322fe8fb19SBen Gras 	default:
10332fe8fb19SBen Gras 		return EAI_SOCKTYPE;
10342fe8fb19SBen Gras 	}
10352fe8fb19SBen Gras 
10362fe8fb19SBen Gras 	port = str2number(servname);
10372fe8fb19SBen Gras 	if (port >= 0) {
10382fe8fb19SBen Gras 		if (!allownumeric)
10392fe8fb19SBen Gras 			return EAI_SERVICE;
10402fe8fb19SBen Gras 		if (port < 0 || port > 65535)
10412fe8fb19SBen Gras 			return EAI_SERVICE;
10422fe8fb19SBen Gras 		port = htons(port);
10432fe8fb19SBen Gras 	} else {
10442fe8fb19SBen Gras 		struct servent sv;
10452fe8fb19SBen Gras 		if (ai->ai_flags & AI_NUMERICSERV)
10462fe8fb19SBen Gras 			return EAI_NONAME;
10472fe8fb19SBen Gras 
10482fe8fb19SBen Gras 		switch (ai->ai_socktype) {
10492fe8fb19SBen Gras 		case SOCK_DGRAM:
10502fe8fb19SBen Gras 			proto = "udp";
10512fe8fb19SBen Gras 			break;
10522fe8fb19SBen Gras 		case SOCK_STREAM:
10532fe8fb19SBen Gras 			proto = "tcp";
10542fe8fb19SBen Gras 			break;
10552fe8fb19SBen Gras 		default:
10562fe8fb19SBen Gras 			proto = NULL;
10572fe8fb19SBen Gras 			break;
10582fe8fb19SBen Gras 		}
10592fe8fb19SBen Gras 
10602fe8fb19SBen Gras 		sp = getservbyname_r(servname, proto, &sv, svd);
10612fe8fb19SBen Gras 		if (sp == NULL)
10622fe8fb19SBen Gras 			return EAI_SERVICE;
10632fe8fb19SBen Gras 		port = sp->s_port;
10642fe8fb19SBen Gras 	}
10652fe8fb19SBen Gras 
106684d9c625SLionel Sambuc 	if (!matchonly)
106784d9c625SLionel Sambuc 		*getport(__UNCONST(ai)) = port;
10682fe8fb19SBen Gras 	return 0;
10692fe8fb19SBen Gras }
10702fe8fb19SBen Gras 
10712fe8fb19SBen Gras static const struct afd *
find_afd(int af)10722fe8fb19SBen Gras find_afd(int af)
10732fe8fb19SBen Gras {
10742fe8fb19SBen Gras 	const struct afd *afd;
10752fe8fb19SBen Gras 
10762fe8fb19SBen Gras 	if (af == PF_UNSPEC)
10772fe8fb19SBen Gras 		return NULL;
10782fe8fb19SBen Gras 	for (afd = afdl; afd->a_af; afd++) {
10792fe8fb19SBen Gras 		if (afd->a_af == af)
10802fe8fb19SBen Gras 			return afd;
10812fe8fb19SBen Gras 	}
10822fe8fb19SBen Gras 	return NULL;
10832fe8fb19SBen Gras }
10842fe8fb19SBen Gras 
1085f14fb602SLionel Sambuc /*
1086f14fb602SLionel Sambuc  * AI_ADDRCONFIG check: Build a mask containing a bit set for each address
1087f14fb602SLionel Sambuc  * family configured in the system.
1088f14fb602SLionel Sambuc  *
1089f14fb602SLionel Sambuc  */
1090f14fb602SLionel Sambuc static int
addrconfig(uint64_t * mask)1091f14fb602SLionel Sambuc addrconfig(uint64_t *mask)
1092f14fb602SLionel Sambuc {
1093f14fb602SLionel Sambuc 	struct ifaddrs *ifaddrs, *ifa;
1094f14fb602SLionel Sambuc 
1095f14fb602SLionel Sambuc 	if (getifaddrs(&ifaddrs) == -1)
1096f14fb602SLionel Sambuc 		return -1;
1097f14fb602SLionel Sambuc 
1098f14fb602SLionel Sambuc 	*mask = 0;
1099f14fb602SLionel Sambuc 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
1100f14fb602SLionel Sambuc 		if (ifa->ifa_addr && (ifa->ifa_flags & IFF_UP)) {
1101f14fb602SLionel Sambuc 			_DIAGASSERT(ifa->ifa_addr->sa_family < 64);
1102f14fb602SLionel Sambuc 			*mask |= (uint64_t)1 << ifa->ifa_addr->sa_family;
1103f14fb602SLionel Sambuc 		}
1104f14fb602SLionel Sambuc 
1105f14fb602SLionel Sambuc 	freeifaddrs(ifaddrs);
1106f14fb602SLionel Sambuc 	return 0;
1107f14fb602SLionel Sambuc }
1108f14fb602SLionel Sambuc 
11092fe8fb19SBen Gras #ifdef INET6
11102fe8fb19SBen Gras /* convert a string to a scope identifier. XXX: IPv6 specific */
11112fe8fb19SBen Gras static int
ip6_str2scopeid(char * scope,struct sockaddr_in6 * sin6,u_int32_t * scopeid)11122fe8fb19SBen Gras ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
11132fe8fb19SBen Gras {
11142fe8fb19SBen Gras 	u_long lscopeid;
11152fe8fb19SBen Gras 	struct in6_addr *a6;
11162fe8fb19SBen Gras 	char *ep;
11172fe8fb19SBen Gras 
11182fe8fb19SBen Gras 	_DIAGASSERT(scope != NULL);
11192fe8fb19SBen Gras 	_DIAGASSERT(sin6 != NULL);
11202fe8fb19SBen Gras 	_DIAGASSERT(scopeid != NULL);
11212fe8fb19SBen Gras 
11222fe8fb19SBen Gras 	a6 = &sin6->sin6_addr;
11232fe8fb19SBen Gras 
11242fe8fb19SBen Gras 	/* empty scopeid portion is invalid */
11252fe8fb19SBen Gras 	if (*scope == '\0')
11262fe8fb19SBen Gras 		return -1;
11272fe8fb19SBen Gras 
11282fe8fb19SBen Gras 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
11292fe8fb19SBen Gras 		/*
11302fe8fb19SBen Gras 		 * We currently assume a one-to-one mapping between links
11312fe8fb19SBen Gras 		 * and interfaces, so we simply use interface indices for
11322fe8fb19SBen Gras 		 * like-local scopes.
11332fe8fb19SBen Gras 		 */
11342fe8fb19SBen Gras 		*scopeid = if_nametoindex(scope);
11352fe8fb19SBen Gras 		if (*scopeid == 0)
11362fe8fb19SBen Gras 			goto trynumeric;
11372fe8fb19SBen Gras 		return 0;
11382fe8fb19SBen Gras 	}
11392fe8fb19SBen Gras 
11402fe8fb19SBen Gras 	/* still unclear about literal, allow numeric only - placeholder */
11412fe8fb19SBen Gras 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
11422fe8fb19SBen Gras 		goto trynumeric;
11432fe8fb19SBen Gras 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
11442fe8fb19SBen Gras 		goto trynumeric;
11452fe8fb19SBen Gras 	else
11462fe8fb19SBen Gras 		goto trynumeric;	/* global */
11472fe8fb19SBen Gras 
11482fe8fb19SBen Gras 	/* try to convert to a numeric id as a last resort */
11492fe8fb19SBen Gras   trynumeric:
11502fe8fb19SBen Gras 	errno = 0;
11512fe8fb19SBen Gras 	lscopeid = strtoul(scope, &ep, 10);
11522fe8fb19SBen Gras 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
11532fe8fb19SBen Gras 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
11542fe8fb19SBen Gras 		return 0;
11552fe8fb19SBen Gras 	else
11562fe8fb19SBen Gras 		return -1;
11572fe8fb19SBen Gras }
11582fe8fb19SBen Gras #endif
11592fe8fb19SBen Gras 
11602fe8fb19SBen Gras /* code duplicate with gethnamaddr.c */
11612fe8fb19SBen Gras 
11622fe8fb19SBen Gras static const char AskedForGot[] =
11632fe8fb19SBen Gras 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
11642fe8fb19SBen Gras 
11652fe8fb19SBen Gras static struct addrinfo *
getanswer(const querybuf * answer,int anslen,const char * qname,int qtype,const struct addrinfo * pai)11662fe8fb19SBen Gras getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
11672fe8fb19SBen Gras     const struct addrinfo *pai)
11682fe8fb19SBen Gras {
11692fe8fb19SBen Gras 	struct addrinfo sentinel, *cur;
117084d9c625SLionel Sambuc 	struct addrinfo ai, *aip;
11712fe8fb19SBen Gras 	const struct afd *afd;
11722fe8fb19SBen Gras 	char *canonname;
11732fe8fb19SBen Gras 	const HEADER *hp;
11742fe8fb19SBen Gras 	const u_char *cp;
11752fe8fb19SBen Gras 	int n;
11762fe8fb19SBen Gras 	const u_char *eom;
11772fe8fb19SBen Gras 	char *bp, *ep;
11782fe8fb19SBen Gras 	int type, class, ancount, qdcount;
11792fe8fb19SBen Gras 	int haveanswer, had_error;
11802fe8fb19SBen Gras 	char tbuf[MAXDNAME];
11812fe8fb19SBen Gras 	int (*name_ok) (const char *);
11822fe8fb19SBen Gras 	char hostbuf[8*1024];
118384d9c625SLionel Sambuc 	int port, pri, weight;
118484d9c625SLionel Sambuc 	struct srvinfo *srvlist, *srv, *csrv;
11852fe8fb19SBen Gras 
11862fe8fb19SBen Gras 	_DIAGASSERT(answer != NULL);
11872fe8fb19SBen Gras 	_DIAGASSERT(qname != NULL);
11882fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
11892fe8fb19SBen Gras 
11902fe8fb19SBen Gras 	memset(&sentinel, 0, sizeof(sentinel));
11912fe8fb19SBen Gras 	cur = &sentinel;
11922fe8fb19SBen Gras 
11932fe8fb19SBen Gras 	canonname = NULL;
11942fe8fb19SBen Gras 	eom = answer->buf + anslen;
11952fe8fb19SBen Gras 	switch (qtype) {
11962fe8fb19SBen Gras 	case T_A:
11972fe8fb19SBen Gras 	case T_AAAA:
11982fe8fb19SBen Gras 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
11992fe8fb19SBen Gras 		name_ok = res_hnok;
12002fe8fb19SBen Gras 		break;
120184d9c625SLionel Sambuc 	case T_SRV:
120284d9c625SLionel Sambuc 		name_ok = gai_srvok;
120384d9c625SLionel Sambuc 		break;
12042fe8fb19SBen Gras 	default:
12052fe8fb19SBen Gras 		return NULL;	/* XXX should be abort(); */
12062fe8fb19SBen Gras 	}
12072fe8fb19SBen Gras 	/*
12082fe8fb19SBen Gras 	 * find first satisfactory answer
12092fe8fb19SBen Gras 	 */
12102fe8fb19SBen Gras 	hp = &answer->hdr;
12112fe8fb19SBen Gras 	ancount = ntohs(hp->ancount);
12122fe8fb19SBen Gras 	qdcount = ntohs(hp->qdcount);
12132fe8fb19SBen Gras 	bp = hostbuf;
12142fe8fb19SBen Gras 	ep = hostbuf + sizeof hostbuf;
12152fe8fb19SBen Gras 	cp = answer->buf + HFIXEDSZ;
12162fe8fb19SBen Gras 	if (qdcount != 1) {
12172fe8fb19SBen Gras 		h_errno = NO_RECOVERY;
12182fe8fb19SBen Gras 		return (NULL);
12192fe8fb19SBen Gras 	}
1220f14fb602SLionel Sambuc 	n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
12212fe8fb19SBen Gras 	if ((n < 0) || !(*name_ok)(bp)) {
12222fe8fb19SBen Gras 		h_errno = NO_RECOVERY;
12232fe8fb19SBen Gras 		return (NULL);
12242fe8fb19SBen Gras 	}
12252fe8fb19SBen Gras 	cp += n + QFIXEDSZ;
12262fe8fb19SBen Gras 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
12272fe8fb19SBen Gras 		/* res_send() has already verified that the query name is the
12282fe8fb19SBen Gras 		 * same as the one we sent; this just gets the expanded name
12292fe8fb19SBen Gras 		 * (i.e., with the succeeding search-domain tacked on).
12302fe8fb19SBen Gras 		 */
1231f14fb602SLionel Sambuc 		n = (int)strlen(bp) + 1;		/* for the \0 */
12322fe8fb19SBen Gras 		if (n >= MAXHOSTNAMELEN) {
12332fe8fb19SBen Gras 			h_errno = NO_RECOVERY;
12342fe8fb19SBen Gras 			return (NULL);
12352fe8fb19SBen Gras 		}
12362fe8fb19SBen Gras 		canonname = bp;
12372fe8fb19SBen Gras 		bp += n;
12382fe8fb19SBen Gras 		/* The qname can be abbreviated, but h_name is now absolute. */
12392fe8fb19SBen Gras 		qname = canonname;
12402fe8fb19SBen Gras 	}
12412fe8fb19SBen Gras 	haveanswer = 0;
12422fe8fb19SBen Gras 	had_error = 0;
124384d9c625SLionel Sambuc 	srvlist = NULL;
12442fe8fb19SBen Gras 	while (ancount-- > 0 && cp < eom && !had_error) {
1245f14fb602SLionel Sambuc 		n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
12462fe8fb19SBen Gras 		if ((n < 0) || !(*name_ok)(bp)) {
12472fe8fb19SBen Gras 			had_error++;
12482fe8fb19SBen Gras 			continue;
12492fe8fb19SBen Gras 		}
12502fe8fb19SBen Gras 		cp += n;			/* name */
12512fe8fb19SBen Gras 		type = _getshort(cp);
12522fe8fb19SBen Gras 		cp += INT16SZ;			/* type */
12532fe8fb19SBen Gras 		class = _getshort(cp);
12542fe8fb19SBen Gras 		cp += INT16SZ + INT32SZ;	/* class, TTL */
12552fe8fb19SBen Gras 		n = _getshort(cp);
12562fe8fb19SBen Gras 		cp += INT16SZ;			/* len */
12572fe8fb19SBen Gras 		if (class != C_IN) {
12582fe8fb19SBen Gras 			/* XXX - debug? syslog? */
12592fe8fb19SBen Gras 			cp += n;
12602fe8fb19SBen Gras 			continue;		/* XXX - had_error++ ? */
12612fe8fb19SBen Gras 		}
12622fe8fb19SBen Gras 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
12632fe8fb19SBen Gras 		    type == T_CNAME) {
1264f14fb602SLionel Sambuc 			n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
12652fe8fb19SBen Gras 			if ((n < 0) || !(*name_ok)(tbuf)) {
12662fe8fb19SBen Gras 				had_error++;
12672fe8fb19SBen Gras 				continue;
12682fe8fb19SBen Gras 			}
12692fe8fb19SBen Gras 			cp += n;
12702fe8fb19SBen Gras 			/* Get canonical name. */
1271f14fb602SLionel Sambuc 			n = (int)strlen(tbuf) + 1;	/* for the \0 */
12722fe8fb19SBen Gras 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
12732fe8fb19SBen Gras 				had_error++;
12742fe8fb19SBen Gras 				continue;
12752fe8fb19SBen Gras 			}
12762fe8fb19SBen Gras 			strlcpy(bp, tbuf, (size_t)(ep - bp));
12772fe8fb19SBen Gras 			canonname = bp;
12782fe8fb19SBen Gras 			bp += n;
12792fe8fb19SBen Gras 			continue;
12802fe8fb19SBen Gras 		}
12812fe8fb19SBen Gras 		if (qtype == T_ANY) {
12822fe8fb19SBen Gras 			if (!(type == T_A || type == T_AAAA)) {
12832fe8fb19SBen Gras 				cp += n;
12842fe8fb19SBen Gras 				continue;
12852fe8fb19SBen Gras 			}
12862fe8fb19SBen Gras 		} else if (type != qtype) {
12872fe8fb19SBen Gras 			if (type != T_KEY && type != T_SIG) {
12882fe8fb19SBen Gras 				struct syslog_data sd = SYSLOG_DATA_INIT;
12892fe8fb19SBen Gras 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
12902fe8fb19SBen Gras 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
12912fe8fb19SBen Gras 				       qname, p_class(C_IN), p_type(qtype),
12922fe8fb19SBen Gras 				       p_type(type));
12932fe8fb19SBen Gras 			}
12942fe8fb19SBen Gras 			cp += n;
12952fe8fb19SBen Gras 			continue;		/* XXX - had_error++ ? */
12962fe8fb19SBen Gras 		}
12972fe8fb19SBen Gras 		switch (type) {
12982fe8fb19SBen Gras 		case T_A:
12992fe8fb19SBen Gras 		case T_AAAA:
13002fe8fb19SBen Gras 			if (strcasecmp(canonname, bp) != 0) {
13012fe8fb19SBen Gras 				struct syslog_data sd = SYSLOG_DATA_INIT;
13022fe8fb19SBen Gras 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
13032fe8fb19SBen Gras 				       AskedForGot, canonname, bp);
13042fe8fb19SBen Gras 				cp += n;
13052fe8fb19SBen Gras 				continue;	/* XXX - had_error++ ? */
13062fe8fb19SBen Gras 			}
13072fe8fb19SBen Gras 			if (type == T_A && n != INADDRSZ) {
13082fe8fb19SBen Gras 				cp += n;
13092fe8fb19SBen Gras 				continue;
13102fe8fb19SBen Gras 			}
13112fe8fb19SBen Gras 			if (type == T_AAAA && n != IN6ADDRSZ) {
13122fe8fb19SBen Gras 				cp += n;
13132fe8fb19SBen Gras 				continue;
13142fe8fb19SBen Gras 			}
13152fe8fb19SBen Gras 			if (type == T_AAAA) {
13162fe8fb19SBen Gras 				struct in6_addr in6;
13172fe8fb19SBen Gras 				memcpy(&in6, cp, IN6ADDRSZ);
13182fe8fb19SBen Gras 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
13192fe8fb19SBen Gras 					cp += n;
13202fe8fb19SBen Gras 					continue;
13212fe8fb19SBen Gras 				}
13222fe8fb19SBen Gras 			}
13232fe8fb19SBen Gras 			if (!haveanswer) {
13242fe8fb19SBen Gras 				int nn;
13252fe8fb19SBen Gras 
13262fe8fb19SBen Gras 				canonname = bp;
1327f14fb602SLionel Sambuc 				nn = (int)strlen(bp) + 1;	/* for the \0 */
13282fe8fb19SBen Gras 				bp += nn;
13292fe8fb19SBen Gras 			}
13302fe8fb19SBen Gras 
13312fe8fb19SBen Gras 			/* don't overwrite pai */
13322fe8fb19SBen Gras 			ai = *pai;
13332fe8fb19SBen Gras 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
13342fe8fb19SBen Gras 			afd = find_afd(ai.ai_family);
13352fe8fb19SBen Gras 			if (afd == NULL) {
13362fe8fb19SBen Gras 				cp += n;
13372fe8fb19SBen Gras 				continue;
13382fe8fb19SBen Gras 			}
13392fe8fb19SBen Gras 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
13402fe8fb19SBen Gras 			if (cur->ai_next == NULL)
13412fe8fb19SBen Gras 				had_error++;
13422fe8fb19SBen Gras 			while (cur && cur->ai_next)
13432fe8fb19SBen Gras 				cur = cur->ai_next;
13442fe8fb19SBen Gras 			cp += n;
13452fe8fb19SBen Gras 			break;
134684d9c625SLionel Sambuc 		case T_SRV:
134784d9c625SLionel Sambuc 			/* Add to SRV list. Insertion sort on priority. */
134884d9c625SLionel Sambuc 			pri = _getshort(cp);
134984d9c625SLionel Sambuc 			cp += INT16SZ;
135084d9c625SLionel Sambuc 			weight = _getshort(cp);
135184d9c625SLionel Sambuc 			cp += INT16SZ;
135284d9c625SLionel Sambuc 			port = _getshort(cp);
135384d9c625SLionel Sambuc 			cp += INT16SZ;
135484d9c625SLionel Sambuc 			n = dn_expand(answer->buf, eom, cp, tbuf,
135584d9c625SLionel Sambuc 			    (int)sizeof(tbuf));
135684d9c625SLionel Sambuc 			if ((n < 0) || !res_hnok(tbuf)) {
135784d9c625SLionel Sambuc 				had_error++;
135884d9c625SLionel Sambuc 				continue;
135984d9c625SLionel Sambuc 			}
136084d9c625SLionel Sambuc 			cp += n;
136184d9c625SLionel Sambuc 			if (strlen(tbuf) + 1 >= MAXDNAME) {
136284d9c625SLionel Sambuc 				had_error++;
136384d9c625SLionel Sambuc 				continue;
136484d9c625SLionel Sambuc 			}
136584d9c625SLionel Sambuc 			srv = malloc(sizeof(*srv));
136684d9c625SLionel Sambuc 			if (!srv) {
136784d9c625SLionel Sambuc 				had_error++;
136884d9c625SLionel Sambuc 				continue;
136984d9c625SLionel Sambuc 			}
137084d9c625SLionel Sambuc 			strlcpy(srv->name, tbuf, sizeof(srv->name));
137184d9c625SLionel Sambuc 			srv->pri = pri;
137284d9c625SLionel Sambuc 			srv->weight = weight;
137384d9c625SLionel Sambuc 			srv->port = port;
137484d9c625SLionel Sambuc 			/* Weight 0 is sorted before other weights. */
137584d9c625SLionel Sambuc 			if (!srvlist
137684d9c625SLionel Sambuc 			    || srv->pri < srvlist->pri
137784d9c625SLionel Sambuc 			    || (srv->pri == srvlist->pri &&
137884d9c625SLionel Sambuc 			    (!srv->weight || srvlist->weight))) {
137984d9c625SLionel Sambuc 				srv->next = srvlist;
138084d9c625SLionel Sambuc 				srvlist = srv;
138184d9c625SLionel Sambuc 			} else {
138284d9c625SLionel Sambuc 				for (csrv = srvlist;
138384d9c625SLionel Sambuc 				    csrv->next && csrv->next->pri <= srv->pri;
138484d9c625SLionel Sambuc 				    csrv = csrv->next) {
138584d9c625SLionel Sambuc 					if (csrv->next->pri == srv->pri
138684d9c625SLionel Sambuc 					    && (!srv->weight ||
138784d9c625SLionel Sambuc 					    csrv->next->weight))
138884d9c625SLionel Sambuc 						break;
138984d9c625SLionel Sambuc 				}
139084d9c625SLionel Sambuc 				srv->next = csrv->next;
139184d9c625SLionel Sambuc 				csrv->next = srv;
139284d9c625SLionel Sambuc 			}
139384d9c625SLionel Sambuc 			continue; /* Don't add to haveanswer yet. */
13942fe8fb19SBen Gras 		default:
13952fe8fb19SBen Gras 			abort();
13962fe8fb19SBen Gras 		}
13972fe8fb19SBen Gras 		if (!had_error)
13982fe8fb19SBen Gras 			haveanswer++;
13992fe8fb19SBen Gras 	}
140084d9c625SLionel Sambuc 
140184d9c625SLionel Sambuc 	if (srvlist) {
140284d9c625SLionel Sambuc 		res_state res;
140384d9c625SLionel Sambuc 		/*
140484d9c625SLionel Sambuc 		 * Check for explicit rejection.
140584d9c625SLionel Sambuc 		 */
140684d9c625SLionel Sambuc 		if (!srvlist->next && !srvlist->name[0]) {
140784d9c625SLionel Sambuc 			free(srvlist);
140884d9c625SLionel Sambuc 			h_errno = HOST_NOT_FOUND;
140984d9c625SLionel Sambuc 			return NULL;
141084d9c625SLionel Sambuc 		}
141184d9c625SLionel Sambuc 		res = __res_get_state();
141284d9c625SLionel Sambuc 		if (res == NULL) {
141384d9c625SLionel Sambuc 			while (srvlist != NULL) {
141484d9c625SLionel Sambuc 				srv = srvlist;
141584d9c625SLionel Sambuc 				srvlist = srvlist->next;
141684d9c625SLionel Sambuc 				free(srv);
141784d9c625SLionel Sambuc 			}
141884d9c625SLionel Sambuc 			h_errno = NETDB_INTERNAL;
141984d9c625SLionel Sambuc 			return NULL;
142084d9c625SLionel Sambuc 		}
142184d9c625SLionel Sambuc 
142284d9c625SLionel Sambuc 		while (srvlist) {
142384d9c625SLionel Sambuc 			struct res_target q, q2;
142484d9c625SLionel Sambuc 
142584d9c625SLionel Sambuc 			srv = srvlist;
142684d9c625SLionel Sambuc 			srvlist = srvlist->next;
142784d9c625SLionel Sambuc 
142884d9c625SLionel Sambuc 			/*
142984d9c625SLionel Sambuc 			 * Since res_* doesn't give the additional
143084d9c625SLionel Sambuc 			 * section, we always look up.
143184d9c625SLionel Sambuc 			 */
143284d9c625SLionel Sambuc 			memset(&q, 0, sizeof(q));
143384d9c625SLionel Sambuc 			memset(&q2, 0, sizeof(q2));
143484d9c625SLionel Sambuc 
143584d9c625SLionel Sambuc 			q.name = srv->name;
143684d9c625SLionel Sambuc 			q.qclass = C_IN;
143784d9c625SLionel Sambuc 			q.qtype = T_AAAA;
143884d9c625SLionel Sambuc 			q.next = &q2;
143984d9c625SLionel Sambuc 			q2.name = srv->name;
144084d9c625SLionel Sambuc 			q2.qclass = C_IN;
144184d9c625SLionel Sambuc 			q2.qtype = T_A;
144284d9c625SLionel Sambuc 
144384d9c625SLionel Sambuc 			aip = _dns_query(&q, pai, res, 0);
144484d9c625SLionel Sambuc 
144584d9c625SLionel Sambuc 			if (aip != NULL) {
144684d9c625SLionel Sambuc 				cur->ai_next = aip;
144784d9c625SLionel Sambuc 				while (cur && cur->ai_next) {
144884d9c625SLionel Sambuc 					cur = cur->ai_next;
144984d9c625SLionel Sambuc 					*getport(cur) = htons(srv->port);
145084d9c625SLionel Sambuc 					haveanswer++;
145184d9c625SLionel Sambuc 				}
145284d9c625SLionel Sambuc 			}
145384d9c625SLionel Sambuc 			free(srv);
145484d9c625SLionel Sambuc 		}
145584d9c625SLionel Sambuc 		__res_put_state(res);
145684d9c625SLionel Sambuc 	}
14572fe8fb19SBen Gras 	if (haveanswer) {
145884d9c625SLionel Sambuc 		if (!sentinel.ai_next->ai_canonname)
145984d9c625SLionel Sambuc 		       (void)get_canonname(pai, sentinel.ai_next,
146084d9c625SLionel Sambuc 			   canonname ? canonname : qname);
14612fe8fb19SBen Gras 		h_errno = NETDB_SUCCESS;
14622fe8fb19SBen Gras 		return sentinel.ai_next;
14632fe8fb19SBen Gras 	}
14642fe8fb19SBen Gras 
14652fe8fb19SBen Gras 	h_errno = NO_RECOVERY;
14662fe8fb19SBen Gras 	return NULL;
14672fe8fb19SBen Gras }
14682fe8fb19SBen Gras 
14692fe8fb19SBen Gras #define SORTEDADDR(p)	(((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
14702fe8fb19SBen Gras #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
14712fe8fb19SBen Gras 
14722fe8fb19SBen Gras static void
aisort(struct addrinfo * s,res_state res)14732fe8fb19SBen Gras aisort(struct addrinfo *s, res_state res)
14742fe8fb19SBen Gras {
14752fe8fb19SBen Gras 	struct addrinfo head, *t, *p;
14762fe8fb19SBen Gras 	int i;
14772fe8fb19SBen Gras 
14782fe8fb19SBen Gras 	head.ai_next = NULL;
14792fe8fb19SBen Gras 	t = &head;
14802fe8fb19SBen Gras 
14812fe8fb19SBen Gras 	for (i = 0; i < res->nsort; i++) {
14822fe8fb19SBen Gras 		p = s;
14832fe8fb19SBen Gras 		while (p->ai_next) {
14842fe8fb19SBen Gras 			if ((p->ai_next->ai_family != AF_INET)
14852fe8fb19SBen Gras 			|| SORTMATCH(p, res->sort_list[i])) {
14862fe8fb19SBen Gras 				t->ai_next = p->ai_next;
14872fe8fb19SBen Gras 				t = t->ai_next;
14882fe8fb19SBen Gras 				p->ai_next = p->ai_next->ai_next;
14892fe8fb19SBen Gras 			} else {
14902fe8fb19SBen Gras 				p = p->ai_next;
14912fe8fb19SBen Gras 			}
14922fe8fb19SBen Gras 		}
14932fe8fb19SBen Gras 	}
14942fe8fb19SBen Gras 
14952fe8fb19SBen Gras 	/* add rest of list and reset s to the new list*/
14962fe8fb19SBen Gras 	t->ai_next = s->ai_next;
14972fe8fb19SBen Gras 	s->ai_next = head.ai_next;
14982fe8fb19SBen Gras }
14992fe8fb19SBen Gras 
150084d9c625SLionel Sambuc static struct addrinfo *
_dns_query(struct res_target * q,const struct addrinfo * pai,res_state res,int dosearch)150184d9c625SLionel Sambuc _dns_query(struct res_target *q, const struct addrinfo *pai,
150284d9c625SLionel Sambuc     res_state res, int dosearch)
15032fe8fb19SBen Gras {
150484d9c625SLionel Sambuc 	struct res_target *q2 = q->next;
15052fe8fb19SBen Gras  	querybuf *buf, *buf2;
150684d9c625SLionel Sambuc 	struct addrinfo sentinel, *cur, *ai;
15072fe8fb19SBen Gras 
150884d9c625SLionel Sambuc #ifdef DNS_DEBUG
150984d9c625SLionel Sambuc 	struct res_target *iter;
151084d9c625SLionel Sambuc 	for (iter = q; iter; iter = iter->next)
151184d9c625SLionel Sambuc 		printf("Query type %d for %s\n", iter->qtype, iter->name);
151284d9c625SLionel Sambuc #endif
15132fe8fb19SBen Gras 
15142fe8fb19SBen Gras  	buf = malloc(sizeof(*buf));
15152fe8fb19SBen Gras  	if (buf == NULL) {
15162fe8fb19SBen Gras  		h_errno = NETDB_INTERNAL;
151784d9c625SLionel Sambuc 		return NULL;
15182fe8fb19SBen Gras  	}
15192fe8fb19SBen Gras  	buf2 = malloc(sizeof(*buf2));
15202fe8fb19SBen Gras  	if (buf2 == NULL) {
15212fe8fb19SBen Gras  		free(buf);
15222fe8fb19SBen Gras  		h_errno = NETDB_INTERNAL;
152384d9c625SLionel Sambuc 		return NULL;
15242fe8fb19SBen Gras 	}
15252fe8fb19SBen Gras 
152684d9c625SLionel Sambuc 	memset(&sentinel, 0, sizeof(sentinel));
152784d9c625SLionel Sambuc 	cur = &sentinel;
152884d9c625SLionel Sambuc 
152984d9c625SLionel Sambuc 	q->answer = buf->buf;
153084d9c625SLionel Sambuc 	q->anslen = sizeof(buf->buf);
153184d9c625SLionel Sambuc 	if (q2) {
153284d9c625SLionel Sambuc 		q2->answer = buf2->buf;
153384d9c625SLionel Sambuc 		q2->anslen = sizeof(buf2->buf);
153484d9c625SLionel Sambuc 	}
153584d9c625SLionel Sambuc 
153684d9c625SLionel Sambuc 	if (dosearch) {
153784d9c625SLionel Sambuc 		if (res_searchN(q->name, q, res) < 0)
153884d9c625SLionel Sambuc 			goto out;
153984d9c625SLionel Sambuc 	} else {
154084d9c625SLionel Sambuc 		if (res_queryN(q->name, q, res) < 0)
154184d9c625SLionel Sambuc 			goto out;
154284d9c625SLionel Sambuc 	}
154384d9c625SLionel Sambuc 
154484d9c625SLionel Sambuc 	ai = getanswer(buf, q->n, q->name, q->qtype, pai);
154584d9c625SLionel Sambuc 	if (ai) {
154684d9c625SLionel Sambuc 		cur->ai_next = ai;
154784d9c625SLionel Sambuc 		while (cur && cur->ai_next)
154884d9c625SLionel Sambuc 			cur = cur->ai_next;
154984d9c625SLionel Sambuc 	}
155084d9c625SLionel Sambuc 	if (q2) {
155184d9c625SLionel Sambuc 		ai = getanswer(buf2, q2->n, q2->name, q2->qtype, pai);
155284d9c625SLionel Sambuc 		if (ai)
155384d9c625SLionel Sambuc 			cur->ai_next = ai;
155484d9c625SLionel Sambuc  	}
155584d9c625SLionel Sambuc 	free(buf);
155684d9c625SLionel Sambuc 	free(buf2);
155784d9c625SLionel Sambuc 	return sentinel.ai_next;
155884d9c625SLionel Sambuc out:
155984d9c625SLionel Sambuc 	free(buf);
156084d9c625SLionel Sambuc 	free(buf2);
156184d9c625SLionel Sambuc 	return NULL;
156284d9c625SLionel Sambuc }
156384d9c625SLionel Sambuc 
156484d9c625SLionel Sambuc /*ARGSUSED*/
156584d9c625SLionel Sambuc static struct addrinfo *
_dns_srv_lookup(const char * name,const char * servname,const struct addrinfo * pai)156684d9c625SLionel Sambuc _dns_srv_lookup(const char *name, const char *servname,
156784d9c625SLionel Sambuc     const struct addrinfo *pai)
156884d9c625SLionel Sambuc {
156984d9c625SLionel Sambuc 	static const char * const srvprotos[] = { "tcp", "udp" };
157084d9c625SLionel Sambuc 	static const int srvnottype[] = { SOCK_DGRAM, SOCK_STREAM };
157184d9c625SLionel Sambuc 	static const int nsrvprotos = 2;
157284d9c625SLionel Sambuc 	struct addrinfo sentinel, *cur, *ai;
157384d9c625SLionel Sambuc 	struct servent *serv, sv;
157484d9c625SLionel Sambuc 	struct servent_data svd;
157584d9c625SLionel Sambuc 	struct res_target q;
157684d9c625SLionel Sambuc 	res_state res;
157784d9c625SLionel Sambuc 	char *tname;
157884d9c625SLionel Sambuc 	int i;
157984d9c625SLionel Sambuc 
158084d9c625SLionel Sambuc 	res = __res_get_state();
158184d9c625SLionel Sambuc 	if (res == NULL)
158284d9c625SLionel Sambuc 		return NULL;
158384d9c625SLionel Sambuc 
158484d9c625SLionel Sambuc 	memset(&svd, 0, sizeof(svd));
158584d9c625SLionel Sambuc 	memset(&sentinel, 0, sizeof(sentinel));
158684d9c625SLionel Sambuc 	cur = &sentinel;
158784d9c625SLionel Sambuc 
158884d9c625SLionel Sambuc 	/*
158984d9c625SLionel Sambuc 	 * Iterate over supported SRV protocols.
159084d9c625SLionel Sambuc 	 * (currently UDP and TCP only)
159184d9c625SLionel Sambuc 	 */
159284d9c625SLionel Sambuc 	for (i = 0; i < nsrvprotos; i++) {
159384d9c625SLionel Sambuc 		/*
159484d9c625SLionel Sambuc 		 * Check that the caller didn't specify a hint
159584d9c625SLionel Sambuc 		 * which precludes this protocol.
159684d9c625SLionel Sambuc 		 */
159784d9c625SLionel Sambuc 		if (pai->ai_socktype == srvnottype[i])
159884d9c625SLionel Sambuc 			continue;
159984d9c625SLionel Sambuc 		/*
160084d9c625SLionel Sambuc 		 * If the caller specified a port,
160184d9c625SLionel Sambuc 		 * then lookup the database for the
160284d9c625SLionel Sambuc 		 * official service name.
160384d9c625SLionel Sambuc 		 */
160484d9c625SLionel Sambuc 		serv = getservbyname_r(servname, srvprotos[i], &sv, &svd);
160584d9c625SLionel Sambuc 		if (serv == NULL)
160684d9c625SLionel Sambuc 			continue;
160784d9c625SLionel Sambuc 
160884d9c625SLionel Sambuc 		/*
160984d9c625SLionel Sambuc 		 * Construct service DNS name.
161084d9c625SLionel Sambuc 		 */
161184d9c625SLionel Sambuc 		if (asprintf(&tname, "_%s._%s.%s", serv->s_name, serv->s_proto,
161284d9c625SLionel Sambuc 		    name) < 0)
161384d9c625SLionel Sambuc 			continue;
161484d9c625SLionel Sambuc 
161584d9c625SLionel Sambuc 		memset(&q, 0, sizeof(q));
161684d9c625SLionel Sambuc 		q.name = tname;
161784d9c625SLionel Sambuc 		q.qclass = C_IN;
161884d9c625SLionel Sambuc 		q.qtype = T_SRV;
161984d9c625SLionel Sambuc 
162084d9c625SLionel Sambuc 		/*
162184d9c625SLionel Sambuc 		 * Do SRV query.
162284d9c625SLionel Sambuc 		 */
162384d9c625SLionel Sambuc 		ai = _dns_query(&q, pai, res, 1);
162484d9c625SLionel Sambuc 		if (ai) {
162584d9c625SLionel Sambuc 			cur->ai_next = ai;
162684d9c625SLionel Sambuc 			while (cur && cur->ai_next)
162784d9c625SLionel Sambuc 				cur = cur->ai_next;
162884d9c625SLionel Sambuc 		}
162984d9c625SLionel Sambuc 		free(tname);
163084d9c625SLionel Sambuc 	}
163184d9c625SLionel Sambuc 
163284d9c625SLionel Sambuc 	if (res->nsort)
163384d9c625SLionel Sambuc 		aisort(&sentinel, res);
163484d9c625SLionel Sambuc 
163584d9c625SLionel Sambuc 	__res_put_state(res);
163684d9c625SLionel Sambuc 
163784d9c625SLionel Sambuc 	return sentinel.ai_next;
163884d9c625SLionel Sambuc }
163984d9c625SLionel Sambuc 
164084d9c625SLionel Sambuc /*ARGSUSED*/
164184d9c625SLionel Sambuc static struct addrinfo *
_dns_host_lookup(const char * name,const struct addrinfo * pai)164284d9c625SLionel Sambuc _dns_host_lookup(const char *name, const struct addrinfo *pai)
164384d9c625SLionel Sambuc {
164484d9c625SLionel Sambuc 	struct res_target q, q2;
164584d9c625SLionel Sambuc 	struct addrinfo sentinel, *ai;
164684d9c625SLionel Sambuc 	res_state res;
164784d9c625SLionel Sambuc 
164884d9c625SLionel Sambuc 	res = __res_get_state();
164984d9c625SLionel Sambuc 	if (res == NULL)
165084d9c625SLionel Sambuc 		return NULL;
165184d9c625SLionel Sambuc 
165284d9c625SLionel Sambuc 	memset(&q, 0, sizeof(q2));
165384d9c625SLionel Sambuc 	memset(&q2, 0, sizeof(q2));
165484d9c625SLionel Sambuc 
16552fe8fb19SBen Gras 	switch (pai->ai_family) {
16562fe8fb19SBen Gras 	case AF_UNSPEC:
16572fe8fb19SBen Gras 		/* prefer IPv6 */
16582fe8fb19SBen Gras 		q.name = name;
16592fe8fb19SBen Gras 		q.qclass = C_IN;
16602fe8fb19SBen Gras 		q.qtype = T_AAAA;
16612fe8fb19SBen Gras 		q.next = &q2;
16622fe8fb19SBen Gras 		q2.name = name;
16632fe8fb19SBen Gras 		q2.qclass = C_IN;
16642fe8fb19SBen Gras 		q2.qtype = T_A;
16652fe8fb19SBen Gras 		break;
16662fe8fb19SBen Gras 	case AF_INET:
16672fe8fb19SBen Gras 		q.name = name;
16682fe8fb19SBen Gras 		q.qclass = C_IN;
16692fe8fb19SBen Gras 		q.qtype = T_A;
16702fe8fb19SBen Gras 		break;
16712fe8fb19SBen Gras 	case AF_INET6:
16722fe8fb19SBen Gras 		q.name = name;
16732fe8fb19SBen Gras 		q.qclass = C_IN;
16742fe8fb19SBen Gras 		q.qtype = T_AAAA;
16752fe8fb19SBen Gras 		break;
16762fe8fb19SBen Gras 	default:
167784d9c625SLionel Sambuc 		__res_put_state(res);
167884d9c625SLionel Sambuc 		h_errno = NETDB_INTERNAL;
167984d9c625SLionel Sambuc 		return NULL;
16802fe8fb19SBen Gras 	}
16812fe8fb19SBen Gras 
168284d9c625SLionel Sambuc 	ai = _dns_query(&q, pai, res, 1);
168384d9c625SLionel Sambuc 
168484d9c625SLionel Sambuc 	memset(&sentinel, 0, sizeof(sentinel));
168584d9c625SLionel Sambuc 	sentinel.ai_next = ai;
168684d9c625SLionel Sambuc 
168784d9c625SLionel Sambuc 	if (ai != NULL && res->nsort)
168884d9c625SLionel Sambuc 		aisort(&sentinel, res);
168984d9c625SLionel Sambuc 
169084d9c625SLionel Sambuc 	__res_put_state(res);
169184d9c625SLionel Sambuc 
169284d9c625SLionel Sambuc 	return sentinel.ai_next;
16932fe8fb19SBen Gras }
16942fe8fb19SBen Gras 
169584d9c625SLionel Sambuc /*ARGSUSED*/
169684d9c625SLionel Sambuc static int
_dns_getaddrinfo(void * rv,void * cb_data,va_list ap)169784d9c625SLionel Sambuc _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
169884d9c625SLionel Sambuc {
169984d9c625SLionel Sambuc 	struct addrinfo *ai = NULL;
170084d9c625SLionel Sambuc 	const char *name, *servname;
170184d9c625SLionel Sambuc 	const struct addrinfo *pai;
170284d9c625SLionel Sambuc 
170384d9c625SLionel Sambuc 	name = va_arg(ap, char *);
170484d9c625SLionel Sambuc 	pai = va_arg(ap, const struct addrinfo *);
170584d9c625SLionel Sambuc 	servname = va_arg(ap, char *);
170684d9c625SLionel Sambuc 
170784d9c625SLionel Sambuc 	/*
170884d9c625SLionel Sambuc 	 * Try doing SRV lookup on service first.
170984d9c625SLionel Sambuc 	 */
171084d9c625SLionel Sambuc 	if (servname
171184d9c625SLionel Sambuc #ifdef AI_SRV
171284d9c625SLionel Sambuc 	    && (pai->ai_flags & AI_SRV)
171384d9c625SLionel Sambuc #endif
171484d9c625SLionel Sambuc 	    && !(pai->ai_flags & AI_NUMERICSERV)
171584d9c625SLionel Sambuc 	    && str2number(servname) == -1) {
171684d9c625SLionel Sambuc 
171784d9c625SLionel Sambuc #ifdef DNS_DEBUG
171884d9c625SLionel Sambuc 		printf("%s: try SRV lookup\n", __func__);
171984d9c625SLionel Sambuc #endif
172084d9c625SLionel Sambuc 		ai = _dns_srv_lookup(name, servname, pai);
17212fe8fb19SBen Gras 	}
172284d9c625SLionel Sambuc 
172384d9c625SLionel Sambuc 	/*
172484d9c625SLionel Sambuc 	 * Do lookup on name.
172584d9c625SLionel Sambuc 	 */
172684d9c625SLionel Sambuc 	if (ai == NULL) {
172784d9c625SLionel Sambuc 
172884d9c625SLionel Sambuc #ifdef DNS_DEBUG
172984d9c625SLionel Sambuc 		printf("%s: try HOST lookup\n", __func__);
173084d9c625SLionel Sambuc #endif
173184d9c625SLionel Sambuc 		ai = _dns_host_lookup(name, pai);
173284d9c625SLionel Sambuc 
173384d9c625SLionel Sambuc 		if (ai == NULL) {
17342fe8fb19SBen Gras 			switch (h_errno) {
17352fe8fb19SBen Gras 			case HOST_NOT_FOUND:
17362fe8fb19SBen Gras 				return NS_NOTFOUND;
17372fe8fb19SBen Gras 			case TRY_AGAIN:
17382fe8fb19SBen Gras 				return NS_TRYAGAIN;
17392fe8fb19SBen Gras 			default:
17402fe8fb19SBen Gras 				return NS_UNAVAIL;
17412fe8fb19SBen Gras 			}
17422fe8fb19SBen Gras 		}
174384d9c625SLionel Sambuc 	}
17442fe8fb19SBen Gras 
174584d9c625SLionel Sambuc 	*((struct addrinfo **)rv) = ai;
17462fe8fb19SBen Gras 	return NS_SUCCESS;
17472fe8fb19SBen Gras }
17482fe8fb19SBen Gras 
17492fe8fb19SBen Gras static void
_sethtent(FILE ** hostf)17502fe8fb19SBen Gras _sethtent(FILE **hostf)
17512fe8fb19SBen Gras {
17522fe8fb19SBen Gras 
17532fe8fb19SBen Gras 	if (!*hostf)
1754f14fb602SLionel Sambuc 		*hostf = fopen(_PATH_HOSTS, "re");
17552fe8fb19SBen Gras 	else
17562fe8fb19SBen Gras 		rewind(*hostf);
17572fe8fb19SBen Gras }
17582fe8fb19SBen Gras 
17592fe8fb19SBen Gras static void
_endhtent(FILE ** hostf)17602fe8fb19SBen Gras _endhtent(FILE **hostf)
17612fe8fb19SBen Gras {
17622fe8fb19SBen Gras 
17632fe8fb19SBen Gras 	if (*hostf) {
17642fe8fb19SBen Gras 		(void) fclose(*hostf);
17652fe8fb19SBen Gras 		*hostf = NULL;
17662fe8fb19SBen Gras 	}
17672fe8fb19SBen Gras }
17682fe8fb19SBen Gras 
17692fe8fb19SBen Gras static struct addrinfo *
_gethtent(FILE ** hostf,const char * name,const struct addrinfo * pai)17702fe8fb19SBen Gras _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
17712fe8fb19SBen Gras {
17722fe8fb19SBen Gras 	char *p;
17732fe8fb19SBen Gras 	char *cp, *tname, *cname;
17742fe8fb19SBen Gras 	struct addrinfo hints, *res0, *res;
17752fe8fb19SBen Gras 	int error;
17762fe8fb19SBen Gras 	const char *addr;
17772fe8fb19SBen Gras 	char hostbuf[8*1024];
17782fe8fb19SBen Gras 
17792fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
17802fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
17812fe8fb19SBen Gras 
1782f14fb602SLionel Sambuc 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
17832fe8fb19SBen Gras 		return (NULL);
17842fe8fb19SBen Gras  again:
1785f14fb602SLionel Sambuc 	if (!(p = fgets(hostbuf, (int)sizeof hostbuf, *hostf)))
17862fe8fb19SBen Gras 		return (NULL);
17872fe8fb19SBen Gras 	if (*p == '#')
17882fe8fb19SBen Gras 		goto again;
17892fe8fb19SBen Gras 	if (!(cp = strpbrk(p, "#\n")))
17902fe8fb19SBen Gras 		goto again;
17912fe8fb19SBen Gras 	*cp = '\0';
17922fe8fb19SBen Gras 	if (!(cp = strpbrk(p, " \t")))
17932fe8fb19SBen Gras 		goto again;
17942fe8fb19SBen Gras 	*cp++ = '\0';
17952fe8fb19SBen Gras 	addr = p;
17962fe8fb19SBen Gras 	/* if this is not something we're looking for, skip it. */
17972fe8fb19SBen Gras 	cname = NULL;
17982fe8fb19SBen Gras 	while (cp && *cp) {
17992fe8fb19SBen Gras 		if (*cp == ' ' || *cp == '\t') {
18002fe8fb19SBen Gras 			cp++;
18012fe8fb19SBen Gras 			continue;
18022fe8fb19SBen Gras 		}
18032fe8fb19SBen Gras 		if (!cname)
18042fe8fb19SBen Gras 			cname = cp;
18052fe8fb19SBen Gras 		tname = cp;
18062fe8fb19SBen Gras 		if ((cp = strpbrk(cp, " \t")) != NULL)
18072fe8fb19SBen Gras 			*cp++ = '\0';
18082fe8fb19SBen Gras 		if (strcasecmp(name, tname) == 0)
18092fe8fb19SBen Gras 			goto found;
18102fe8fb19SBen Gras 	}
18112fe8fb19SBen Gras 	goto again;
18122fe8fb19SBen Gras 
18132fe8fb19SBen Gras found:
18142fe8fb19SBen Gras 	hints = *pai;
18152fe8fb19SBen Gras 	hints.ai_flags = AI_NUMERICHOST;
18162fe8fb19SBen Gras 	error = getaddrinfo(addr, NULL, &hints, &res0);
18172fe8fb19SBen Gras 	if (error)
18182fe8fb19SBen Gras 		goto again;
18192fe8fb19SBen Gras 	for (res = res0; res; res = res->ai_next) {
18202fe8fb19SBen Gras 		/* cover it up */
18212fe8fb19SBen Gras 		res->ai_flags = pai->ai_flags;
18222fe8fb19SBen Gras 
18232fe8fb19SBen Gras 		if (pai->ai_flags & AI_CANONNAME) {
18242fe8fb19SBen Gras 			if (get_canonname(pai, res, cname) != 0) {
18252fe8fb19SBen Gras 				freeaddrinfo(res0);
18262fe8fb19SBen Gras 				goto again;
18272fe8fb19SBen Gras 			}
18282fe8fb19SBen Gras 		}
18292fe8fb19SBen Gras 	}
18302fe8fb19SBen Gras 	return res0;
18312fe8fb19SBen Gras }
18322fe8fb19SBen Gras 
18332fe8fb19SBen Gras /*ARGSUSED*/
18342fe8fb19SBen Gras static int
_files_getaddrinfo(void * rv,void * cb_data,va_list ap)18352fe8fb19SBen Gras _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
18362fe8fb19SBen Gras {
18372fe8fb19SBen Gras 	const char *name;
18382fe8fb19SBen Gras 	const struct addrinfo *pai;
18392fe8fb19SBen Gras 	struct addrinfo sentinel, *cur;
18402fe8fb19SBen Gras 	struct addrinfo *p;
18412fe8fb19SBen Gras #ifndef _REENTRANT
18422fe8fb19SBen Gras 	static
18432fe8fb19SBen Gras #endif
18442fe8fb19SBen Gras 	FILE *hostf = NULL;
18452fe8fb19SBen Gras 
18462fe8fb19SBen Gras 	name = va_arg(ap, char *);
18472fe8fb19SBen Gras 	pai = va_arg(ap, const struct addrinfo *);
18482fe8fb19SBen Gras 
18492fe8fb19SBen Gras 	memset(&sentinel, 0, sizeof(sentinel));
18502fe8fb19SBen Gras 	cur = &sentinel;
18512fe8fb19SBen Gras 
18522fe8fb19SBen Gras 	_sethtent(&hostf);
18532fe8fb19SBen Gras 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
18542fe8fb19SBen Gras 		cur->ai_next = p;
18552fe8fb19SBen Gras 		while (cur && cur->ai_next)
18562fe8fb19SBen Gras 			cur = cur->ai_next;
18572fe8fb19SBen Gras 	}
18582fe8fb19SBen Gras 	_endhtent(&hostf);
18592fe8fb19SBen Gras 
18602fe8fb19SBen Gras 	*((struct addrinfo **)rv) = sentinel.ai_next;
18612fe8fb19SBen Gras 	if (sentinel.ai_next == NULL)
18622fe8fb19SBen Gras 		return NS_NOTFOUND;
18632fe8fb19SBen Gras 	return NS_SUCCESS;
18642fe8fb19SBen Gras }
18652fe8fb19SBen Gras 
18662fe8fb19SBen Gras #ifdef YP
18672fe8fb19SBen Gras /*ARGSUSED*/
18682fe8fb19SBen Gras static struct addrinfo *
_yphostent(char * line,const struct addrinfo * pai)18692fe8fb19SBen Gras _yphostent(char *line, const struct addrinfo *pai)
18702fe8fb19SBen Gras {
18712fe8fb19SBen Gras 	struct addrinfo sentinel, *cur;
18722fe8fb19SBen Gras 	struct addrinfo hints, *res, *res0;
18732fe8fb19SBen Gras 	int error;
18742fe8fb19SBen Gras 	char *p;
18752fe8fb19SBen Gras 	const char *addr, *canonname;
18762fe8fb19SBen Gras 	char *nextline;
18772fe8fb19SBen Gras 	char *cp;
18782fe8fb19SBen Gras 
18792fe8fb19SBen Gras 	_DIAGASSERT(line != NULL);
18802fe8fb19SBen Gras 	_DIAGASSERT(pai != NULL);
18812fe8fb19SBen Gras 
18822fe8fb19SBen Gras 	p = line;
18832fe8fb19SBen Gras 	addr = canonname = NULL;
18842fe8fb19SBen Gras 
18852fe8fb19SBen Gras 	memset(&sentinel, 0, sizeof(sentinel));
18862fe8fb19SBen Gras 	cur = &sentinel;
18872fe8fb19SBen Gras 
18882fe8fb19SBen Gras nextline:
18892fe8fb19SBen Gras 	/* terminate line */
18902fe8fb19SBen Gras 	cp = strchr(p, '\n');
18912fe8fb19SBen Gras 	if (cp) {
18922fe8fb19SBen Gras 		*cp++ = '\0';
18932fe8fb19SBen Gras 		nextline = cp;
18942fe8fb19SBen Gras 	} else
18952fe8fb19SBen Gras 		nextline = NULL;
18962fe8fb19SBen Gras 
18972fe8fb19SBen Gras 	cp = strpbrk(p, " \t");
18982fe8fb19SBen Gras 	if (cp == NULL) {
18992fe8fb19SBen Gras 		if (canonname == NULL)
19002fe8fb19SBen Gras 			return (NULL);
19012fe8fb19SBen Gras 		else
19022fe8fb19SBen Gras 			goto done;
19032fe8fb19SBen Gras 	}
19042fe8fb19SBen Gras 	*cp++ = '\0';
19052fe8fb19SBen Gras 
19062fe8fb19SBen Gras 	addr = p;
19072fe8fb19SBen Gras 
19082fe8fb19SBen Gras 	while (cp && *cp) {
19092fe8fb19SBen Gras 		if (*cp == ' ' || *cp == '\t') {
19102fe8fb19SBen Gras 			cp++;
19112fe8fb19SBen Gras 			continue;
19122fe8fb19SBen Gras 		}
19132fe8fb19SBen Gras 		if (!canonname)
19142fe8fb19SBen Gras 			canonname = cp;
19152fe8fb19SBen Gras 		if ((cp = strpbrk(cp, " \t")) != NULL)
19162fe8fb19SBen Gras 			*cp++ = '\0';
19172fe8fb19SBen Gras 	}
19182fe8fb19SBen Gras 
19192fe8fb19SBen Gras 	hints = *pai;
19202fe8fb19SBen Gras 	hints.ai_flags = AI_NUMERICHOST;
19212fe8fb19SBen Gras 	error = getaddrinfo(addr, NULL, &hints, &res0);
19222fe8fb19SBen Gras 	if (error == 0) {
19232fe8fb19SBen Gras 		for (res = res0; res; res = res->ai_next) {
19242fe8fb19SBen Gras 			/* cover it up */
19252fe8fb19SBen Gras 			res->ai_flags = pai->ai_flags;
19262fe8fb19SBen Gras 
19272fe8fb19SBen Gras 			if (pai->ai_flags & AI_CANONNAME)
19282fe8fb19SBen Gras 				(void)get_canonname(pai, res, canonname);
19292fe8fb19SBen Gras 		}
19302fe8fb19SBen Gras 	} else
19312fe8fb19SBen Gras 		res0 = NULL;
19322fe8fb19SBen Gras 	if (res0) {
19332fe8fb19SBen Gras 		cur->ai_next = res0;
19342fe8fb19SBen Gras 		while (cur->ai_next)
19352fe8fb19SBen Gras 			cur = cur->ai_next;
19362fe8fb19SBen Gras 	}
19372fe8fb19SBen Gras 
19382fe8fb19SBen Gras 	if (nextline) {
19392fe8fb19SBen Gras 		p = nextline;
19402fe8fb19SBen Gras 		goto nextline;
19412fe8fb19SBen Gras 	}
19422fe8fb19SBen Gras 
19432fe8fb19SBen Gras done:
19442fe8fb19SBen Gras 	return sentinel.ai_next;
19452fe8fb19SBen Gras }
19462fe8fb19SBen Gras 
19472fe8fb19SBen Gras /*ARGSUSED*/
19482fe8fb19SBen Gras static int
_yp_getaddrinfo(void * rv,void * cb_data,va_list ap)19492fe8fb19SBen Gras _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
19502fe8fb19SBen Gras {
19512fe8fb19SBen Gras 	struct addrinfo sentinel, *cur;
19522fe8fb19SBen Gras 	struct addrinfo *ai = NULL;
19532fe8fb19SBen Gras 	char *ypbuf;
19542fe8fb19SBen Gras 	int ypbuflen, r;
19552fe8fb19SBen Gras 	const char *name;
19562fe8fb19SBen Gras 	const struct addrinfo *pai;
19572fe8fb19SBen Gras 	char *ypdomain;
19582fe8fb19SBen Gras 
19592fe8fb19SBen Gras 	if (_yp_check(&ypdomain) == 0)
19602fe8fb19SBen Gras 		return NS_UNAVAIL;
19612fe8fb19SBen Gras 
19622fe8fb19SBen Gras 	name = va_arg(ap, char *);
19632fe8fb19SBen Gras 	pai = va_arg(ap, const struct addrinfo *);
19642fe8fb19SBen Gras 
19652fe8fb19SBen Gras 	memset(&sentinel, 0, sizeof(sentinel));
19662fe8fb19SBen Gras 	cur = &sentinel;
19672fe8fb19SBen Gras 
19682fe8fb19SBen Gras 	/* hosts.byname is only for IPv4 (Solaris8) */
19692fe8fb19SBen Gras 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
19702fe8fb19SBen Gras 		r = yp_match(ypdomain, "hosts.byname", name,
19712fe8fb19SBen Gras 			(int)strlen(name), &ypbuf, &ypbuflen);
19722fe8fb19SBen Gras 		if (r == 0) {
19732fe8fb19SBen Gras 			struct addrinfo ai4;
19742fe8fb19SBen Gras 
19752fe8fb19SBen Gras 			ai4 = *pai;
19762fe8fb19SBen Gras 			ai4.ai_family = AF_INET;
19772fe8fb19SBen Gras 			ai = _yphostent(ypbuf, &ai4);
19782fe8fb19SBen Gras 			if (ai) {
19792fe8fb19SBen Gras 				cur->ai_next = ai;
19802fe8fb19SBen Gras 				while (cur && cur->ai_next)
19812fe8fb19SBen Gras 					cur = cur->ai_next;
19822fe8fb19SBen Gras 			}
19832fe8fb19SBen Gras 		}
19842fe8fb19SBen Gras 		free(ypbuf);
19852fe8fb19SBen Gras 	}
19862fe8fb19SBen Gras 
19872fe8fb19SBen Gras 	/* ipnodes.byname can hold both IPv4/v6 */
19882fe8fb19SBen Gras 	r = yp_match(ypdomain, "ipnodes.byname", name,
19892fe8fb19SBen Gras 		(int)strlen(name), &ypbuf, &ypbuflen);
19902fe8fb19SBen Gras 	if (r == 0) {
19912fe8fb19SBen Gras 		ai = _yphostent(ypbuf, pai);
19922fe8fb19SBen Gras 		if (ai)
19932fe8fb19SBen Gras 			cur->ai_next = ai;
19942fe8fb19SBen Gras 		free(ypbuf);
19952fe8fb19SBen Gras 	}
19962fe8fb19SBen Gras 
19972fe8fb19SBen Gras 	if (sentinel.ai_next == NULL) {
19982fe8fb19SBen Gras 		h_errno = HOST_NOT_FOUND;
19992fe8fb19SBen Gras 		return NS_NOTFOUND;
20002fe8fb19SBen Gras 	}
20012fe8fb19SBen Gras 	*((struct addrinfo **)rv) = sentinel.ai_next;
20022fe8fb19SBen Gras 	return NS_SUCCESS;
20032fe8fb19SBen Gras }
20042fe8fb19SBen Gras #endif
20052fe8fb19SBen Gras 
20062fe8fb19SBen Gras /* resolver logic */
20072fe8fb19SBen Gras 
20082fe8fb19SBen Gras /*
20092fe8fb19SBen Gras  * Formulate a normal query, send, and await answer.
20102fe8fb19SBen Gras  * Returned answer is placed in supplied buffer "answer".
20112fe8fb19SBen Gras  * Perform preliminary check of answer, returning success only
20122fe8fb19SBen Gras  * if no error is indicated and the answer count is nonzero.
20132fe8fb19SBen Gras  * Return the size of the response on success, -1 on error.
20142fe8fb19SBen Gras  * Error number is left in h_errno.
20152fe8fb19SBen Gras  *
20162fe8fb19SBen Gras  * Caller must parse answer and determine whether it answers the question.
20172fe8fb19SBen Gras  */
20182fe8fb19SBen Gras static int
res_queryN(const char * name,struct res_target * target,res_state res)20192fe8fb19SBen Gras res_queryN(const char *name, /* domain name */ struct res_target *target,
20202fe8fb19SBen Gras     res_state res)
20212fe8fb19SBen Gras {
20222fe8fb19SBen Gras 	u_char buf[MAXPACKET];
20232fe8fb19SBen Gras 	HEADER *hp;
20242fe8fb19SBen Gras 	int n;
20252fe8fb19SBen Gras 	struct res_target *t;
20262fe8fb19SBen Gras 	int rcode;
20272fe8fb19SBen Gras 	int ancount;
20282fe8fb19SBen Gras 
20292fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
20302fe8fb19SBen Gras 	/* XXX: target may be NULL??? */
20312fe8fb19SBen Gras 
20322fe8fb19SBen Gras 	rcode = NOERROR;
20332fe8fb19SBen Gras 	ancount = 0;
20342fe8fb19SBen Gras 
20352fe8fb19SBen Gras 	for (t = target; t; t = t->next) {
20362fe8fb19SBen Gras 		int class, type;
20372fe8fb19SBen Gras 		u_char *answer;
20382fe8fb19SBen Gras 		int anslen;
20392fe8fb19SBen Gras 
20402fe8fb19SBen Gras 		hp = (HEADER *)(void *)t->answer;
20412fe8fb19SBen Gras 		hp->rcode = NOERROR;	/* default */
20422fe8fb19SBen Gras 
20432fe8fb19SBen Gras 		/* make it easier... */
20442fe8fb19SBen Gras 		class = t->qclass;
20452fe8fb19SBen Gras 		type = t->qtype;
20462fe8fb19SBen Gras 		answer = t->answer;
20472fe8fb19SBen Gras 		anslen = t->anslen;
20482fe8fb19SBen Gras #ifdef DEBUG
20492fe8fb19SBen Gras 		if (res->options & RES_DEBUG)
20502fe8fb19SBen Gras 			printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
20512fe8fb19SBen Gras #endif
20522fe8fb19SBen Gras 
20532fe8fb19SBen Gras 		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
2054f14fb602SLionel Sambuc 		    buf, (int)sizeof(buf));
20552fe8fb19SBen Gras #ifdef RES_USE_EDNS0
20562fe8fb19SBen Gras 		if (n > 0 && (res->options & RES_USE_EDNS0) != 0)
2057f14fb602SLionel Sambuc 			n = res_nopt(res, n, buf, (int)sizeof(buf), anslen);
20582fe8fb19SBen Gras #endif
20592fe8fb19SBen Gras 		if (n <= 0) {
20602fe8fb19SBen Gras #ifdef DEBUG
20612fe8fb19SBen Gras 			if (res->options & RES_DEBUG)
20622fe8fb19SBen Gras 				printf(";; res_nquery: mkquery failed\n");
20632fe8fb19SBen Gras #endif
20642fe8fb19SBen Gras 			h_errno = NO_RECOVERY;
20652fe8fb19SBen Gras 			return n;
20662fe8fb19SBen Gras 		}
20672fe8fb19SBen Gras 		n = res_nsend(res, buf, n, answer, anslen);
20682fe8fb19SBen Gras #if 0
20692fe8fb19SBen Gras 		if (n < 0) {
20702fe8fb19SBen Gras #ifdef DEBUG
20712fe8fb19SBen Gras 			if (res->options & RES_DEBUG)
20722fe8fb19SBen Gras 				printf(";; res_query: send error\n");
20732fe8fb19SBen Gras #endif
20742fe8fb19SBen Gras 			h_errno = TRY_AGAIN;
20752fe8fb19SBen Gras 			return n;
20762fe8fb19SBen Gras 		}
20772fe8fb19SBen Gras #endif
20782fe8fb19SBen Gras 
20792fe8fb19SBen Gras 		if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
20802fe8fb19SBen Gras 			rcode = hp->rcode;	/* record most recent error */
20812fe8fb19SBen Gras #ifdef DEBUG
20822fe8fb19SBen Gras 			if (res->options & RES_DEBUG)
20832fe8fb19SBen Gras 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
20842fe8fb19SBen Gras 				    ntohs(hp->ancount));
20852fe8fb19SBen Gras #endif
20862fe8fb19SBen Gras 			continue;
20872fe8fb19SBen Gras 		}
20882fe8fb19SBen Gras 
20892fe8fb19SBen Gras 		ancount += ntohs(hp->ancount);
20902fe8fb19SBen Gras 
20912fe8fb19SBen Gras 		t->n = n;
20922fe8fb19SBen Gras 	}
20932fe8fb19SBen Gras 
20942fe8fb19SBen Gras 	if (ancount == 0) {
20952fe8fb19SBen Gras 		switch (rcode) {
20962fe8fb19SBen Gras 		case NXDOMAIN:
20972fe8fb19SBen Gras 			h_errno = HOST_NOT_FOUND;
20982fe8fb19SBen Gras 			break;
20992fe8fb19SBen Gras 		case SERVFAIL:
21002fe8fb19SBen Gras 			h_errno = TRY_AGAIN;
21012fe8fb19SBen Gras 			break;
21022fe8fb19SBen Gras 		case NOERROR:
21032fe8fb19SBen Gras 			h_errno = NO_DATA;
21042fe8fb19SBen Gras 			break;
21052fe8fb19SBen Gras 		case FORMERR:
21062fe8fb19SBen Gras 		case NOTIMP:
21072fe8fb19SBen Gras 		case REFUSED:
21082fe8fb19SBen Gras 		default:
21092fe8fb19SBen Gras 			h_errno = NO_RECOVERY;
21102fe8fb19SBen Gras 			break;
21112fe8fb19SBen Gras 		}
21122fe8fb19SBen Gras 		return -1;
21132fe8fb19SBen Gras 	}
21142fe8fb19SBen Gras 	return ancount;
21152fe8fb19SBen Gras }
21162fe8fb19SBen Gras 
21172fe8fb19SBen Gras /*
21182fe8fb19SBen Gras  * Formulate a normal query, send, and retrieve answer in supplied buffer.
21192fe8fb19SBen Gras  * Return the size of the response on success, -1 on error.
21202fe8fb19SBen Gras  * If enabled, implement search rules until answer or unrecoverable failure
21212fe8fb19SBen Gras  * is detected.	 Error code, if any, is left in h_errno.
21222fe8fb19SBen Gras  */
21232fe8fb19SBen Gras static int
res_searchN(const char * name,struct res_target * target,res_state res)21242fe8fb19SBen Gras res_searchN(const char *name, struct res_target *target, res_state res)
21252fe8fb19SBen Gras {
21262fe8fb19SBen Gras 	const char *cp, * const *domain;
21272fe8fb19SBen Gras 	HEADER *hp;
21282fe8fb19SBen Gras 	u_int dots;
2129*0a6a1f1dSLionel Sambuc 	char buf[MAXHOSTNAMELEN];
21302fe8fb19SBen Gras 	int trailing_dot, ret, saved_herrno;
21312fe8fb19SBen Gras 	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
21322fe8fb19SBen Gras 
21332fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
21342fe8fb19SBen Gras 	_DIAGASSERT(target != NULL);
21352fe8fb19SBen Gras 
21362fe8fb19SBen Gras 	hp = (HEADER *)(void *)target->answer;	/*XXX*/
21372fe8fb19SBen Gras 
21382fe8fb19SBen Gras 	errno = 0;
21392fe8fb19SBen Gras 	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
21402fe8fb19SBen Gras 	dots = 0;
21412fe8fb19SBen Gras 	for (cp = name; *cp; cp++)
21422fe8fb19SBen Gras 		dots += (*cp == '.');
21432fe8fb19SBen Gras 	trailing_dot = 0;
21442fe8fb19SBen Gras 	if (cp > name && *--cp == '.')
21452fe8fb19SBen Gras 		trailing_dot++;
21462fe8fb19SBen Gras 
21472fe8fb19SBen Gras 	/*
21482fe8fb19SBen Gras 	 * if there aren't any dots, it could be a user-level alias
21492fe8fb19SBen Gras 	 */
2150*0a6a1f1dSLionel Sambuc 	if (!dots && (cp = res_hostalias(res, name, buf, sizeof(buf))) != NULL) {
21512fe8fb19SBen Gras 		ret = res_queryN(cp, target, res);
21522fe8fb19SBen Gras 		return ret;
21532fe8fb19SBen Gras 	}
21542fe8fb19SBen Gras 
21552fe8fb19SBen Gras 	/*
21562fe8fb19SBen Gras 	 * If there are dots in the name already, let's just give it a try
21572fe8fb19SBen Gras 	 * 'as is'.  The threshold can be set with the "ndots" option.
21582fe8fb19SBen Gras 	 */
21592fe8fb19SBen Gras 	saved_herrno = -1;
21602fe8fb19SBen Gras 	if (dots >= res->ndots) {
21612fe8fb19SBen Gras 		ret = res_querydomainN(name, NULL, target, res);
21622fe8fb19SBen Gras 		if (ret > 0)
21632fe8fb19SBen Gras 			return (ret);
21642fe8fb19SBen Gras 		saved_herrno = h_errno;
21652fe8fb19SBen Gras 		tried_as_is++;
21662fe8fb19SBen Gras 	}
21672fe8fb19SBen Gras 
21682fe8fb19SBen Gras 	/*
21692fe8fb19SBen Gras 	 * We do at least one level of search if
21702fe8fb19SBen Gras 	 *	- there is no dot and RES_DEFNAME is set, or
21712fe8fb19SBen Gras 	 *	- there is at least one dot, there is no trailing dot,
21722fe8fb19SBen Gras 	 *	  and RES_DNSRCH is set.
21732fe8fb19SBen Gras 	 */
21742fe8fb19SBen Gras 	if ((!dots && (res->options & RES_DEFNAMES)) ||
21752fe8fb19SBen Gras 	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
21762fe8fb19SBen Gras 		int done = 0;
21772fe8fb19SBen Gras 
21782fe8fb19SBen Gras 		for (domain = (const char * const *)res->dnsrch;
21792fe8fb19SBen Gras 		   *domain && !done;
21802fe8fb19SBen Gras 		   domain++) {
21812fe8fb19SBen Gras 
21822fe8fb19SBen Gras 			ret = res_querydomainN(name, *domain, target, res);
21832fe8fb19SBen Gras 			if (ret > 0)
21842fe8fb19SBen Gras 				return ret;
21852fe8fb19SBen Gras 
21862fe8fb19SBen Gras 			/*
21872fe8fb19SBen Gras 			 * If no server present, give up.
21882fe8fb19SBen Gras 			 * If name isn't found in this domain,
21892fe8fb19SBen Gras 			 * keep trying higher domains in the search list
21902fe8fb19SBen Gras 			 * (if that's enabled).
21912fe8fb19SBen Gras 			 * On a NO_DATA error, keep trying, otherwise
21922fe8fb19SBen Gras 			 * a wildcard entry of another type could keep us
21932fe8fb19SBen Gras 			 * from finding this entry higher in the domain.
21942fe8fb19SBen Gras 			 * If we get some other error (negative answer or
21952fe8fb19SBen Gras 			 * server failure), then stop searching up,
21962fe8fb19SBen Gras 			 * but try the input name below in case it's
21972fe8fb19SBen Gras 			 * fully-qualified.
21982fe8fb19SBen Gras 			 */
21992fe8fb19SBen Gras 			if (errno == ECONNREFUSED) {
22002fe8fb19SBen Gras 				h_errno = TRY_AGAIN;
22012fe8fb19SBen Gras 				return -1;
22022fe8fb19SBen Gras 			}
22032fe8fb19SBen Gras 
22042fe8fb19SBen Gras 			switch (h_errno) {
22052fe8fb19SBen Gras 			case NO_DATA:
22062fe8fb19SBen Gras 				got_nodata++;
22072fe8fb19SBen Gras 				/* FALLTHROUGH */
22082fe8fb19SBen Gras 			case HOST_NOT_FOUND:
22092fe8fb19SBen Gras 				/* keep trying */
22102fe8fb19SBen Gras 				break;
22112fe8fb19SBen Gras 			case TRY_AGAIN:
22122fe8fb19SBen Gras 				if (hp->rcode == SERVFAIL) {
22132fe8fb19SBen Gras 					/* try next search element, if any */
22142fe8fb19SBen Gras 					got_servfail++;
22152fe8fb19SBen Gras 					break;
22162fe8fb19SBen Gras 				}
22172fe8fb19SBen Gras 				/* FALLTHROUGH */
22182fe8fb19SBen Gras 			default:
22192fe8fb19SBen Gras 				/* anything else implies that we're done */
22202fe8fb19SBen Gras 				done++;
22212fe8fb19SBen Gras 			}
22222fe8fb19SBen Gras 			/*
22232fe8fb19SBen Gras 			 * if we got here for some reason other than DNSRCH,
22242fe8fb19SBen Gras 			 * we only wanted one iteration of the loop, so stop.
22252fe8fb19SBen Gras 			 */
22262fe8fb19SBen Gras 			if (!(res->options & RES_DNSRCH))
22272fe8fb19SBen Gras 				done++;
22282fe8fb19SBen Gras 		}
22292fe8fb19SBen Gras 	}
22302fe8fb19SBen Gras 
22312fe8fb19SBen Gras 	/*
22322fe8fb19SBen Gras 	 * if we have not already tried the name "as is", do that now.
22332fe8fb19SBen Gras 	 * note that we do this regardless of how many dots were in the
22342fe8fb19SBen Gras 	 * name or whether it ends with a dot.
22352fe8fb19SBen Gras 	 */
22362fe8fb19SBen Gras 	if (!tried_as_is) {
22372fe8fb19SBen Gras 		ret = res_querydomainN(name, NULL, target, res);
22382fe8fb19SBen Gras 		if (ret > 0)
22392fe8fb19SBen Gras 			return ret;
22402fe8fb19SBen Gras 	}
22412fe8fb19SBen Gras 
22422fe8fb19SBen Gras 	/*
22432fe8fb19SBen Gras 	 * if we got here, we didn't satisfy the search.
22442fe8fb19SBen Gras 	 * if we did an initial full query, return that query's h_errno
22452fe8fb19SBen Gras 	 * (note that we wouldn't be here if that query had succeeded).
22462fe8fb19SBen Gras 	 * else if we ever got a nodata, send that back as the reason.
22472fe8fb19SBen Gras 	 * else send back meaningless h_errno, that being the one from
22482fe8fb19SBen Gras 	 * the last DNSRCH we did.
22492fe8fb19SBen Gras 	 */
22502fe8fb19SBen Gras 	if (saved_herrno != -1)
22512fe8fb19SBen Gras 		h_errno = saved_herrno;
22522fe8fb19SBen Gras 	else if (got_nodata)
22532fe8fb19SBen Gras 		h_errno = NO_DATA;
22542fe8fb19SBen Gras 	else if (got_servfail)
22552fe8fb19SBen Gras 		h_errno = TRY_AGAIN;
22562fe8fb19SBen Gras 	return -1;
22572fe8fb19SBen Gras }
22582fe8fb19SBen Gras 
22592fe8fb19SBen Gras /*
22602fe8fb19SBen Gras  * Perform a call on res_query on the concatenation of name and domain,
22612fe8fb19SBen Gras  * removing a trailing dot from name if domain is NULL.
22622fe8fb19SBen Gras  */
22632fe8fb19SBen Gras static int
res_querydomainN(const char * name,const char * domain,struct res_target * target,res_state res)22642fe8fb19SBen Gras res_querydomainN(const char *name, const char *domain,
22652fe8fb19SBen Gras     struct res_target *target, res_state res)
22662fe8fb19SBen Gras {
22672fe8fb19SBen Gras 	char nbuf[MAXDNAME];
22682fe8fb19SBen Gras 	const char *longname = nbuf;
22692fe8fb19SBen Gras 	size_t n, d;
22702fe8fb19SBen Gras 
22712fe8fb19SBen Gras 	_DIAGASSERT(name != NULL);
22722fe8fb19SBen Gras 	/* XXX: target may be NULL??? */
22732fe8fb19SBen Gras 
22742fe8fb19SBen Gras #ifdef DEBUG
22752fe8fb19SBen Gras 	if (res->options & RES_DEBUG)
22762fe8fb19SBen Gras 		printf(";; res_querydomain(%s, %s)\n",
22772fe8fb19SBen Gras 			name, domain?domain:"<Nil>");
22782fe8fb19SBen Gras #endif
22792fe8fb19SBen Gras 	if (domain == NULL) {
22802fe8fb19SBen Gras 		/*
22812fe8fb19SBen Gras 		 * Check for trailing '.';
22822fe8fb19SBen Gras 		 * copy without '.' if present.
22832fe8fb19SBen Gras 		 */
22842fe8fb19SBen Gras 		n = strlen(name);
22852fe8fb19SBen Gras 		if (n + 1 > sizeof(nbuf)) {
22862fe8fb19SBen Gras 			h_errno = NO_RECOVERY;
22872fe8fb19SBen Gras 			return -1;
22882fe8fb19SBen Gras 		}
22892fe8fb19SBen Gras 		if (n > 0 && name[--n] == '.') {
22902fe8fb19SBen Gras 			strncpy(nbuf, name, n);
22912fe8fb19SBen Gras 			nbuf[n] = '\0';
22922fe8fb19SBen Gras 		} else
22932fe8fb19SBen Gras 			longname = name;
22942fe8fb19SBen Gras 	} else {
22952fe8fb19SBen Gras 		n = strlen(name);
22962fe8fb19SBen Gras 		d = strlen(domain);
22972fe8fb19SBen Gras 		if (n + 1 + d + 1 > sizeof(nbuf)) {
22982fe8fb19SBen Gras 			h_errno = NO_RECOVERY;
22992fe8fb19SBen Gras 			return -1;
23002fe8fb19SBen Gras 		}
23012fe8fb19SBen Gras 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
23022fe8fb19SBen Gras 	}
23032fe8fb19SBen Gras 	return res_queryN(longname, target, res);
23042fe8fb19SBen Gras }
230584d9c625SLionel Sambuc 
230684d9c625SLionel Sambuc #ifdef TEST
230784d9c625SLionel Sambuc int
main(int argc,char * argv[])230884d9c625SLionel Sambuc main(int argc, char *argv[]) {
230984d9c625SLionel Sambuc 	struct addrinfo *ai, *sai;
231084d9c625SLionel Sambuc 	int i, e;
231184d9c625SLionel Sambuc 	char buf[1024];
231284d9c625SLionel Sambuc 
231384d9c625SLionel Sambuc 	for (i = 1; i < argc; i++) {
231484d9c625SLionel Sambuc 		if ((e = getaddrinfo(argv[i], NULL, NULL, &sai)) != 0)
231584d9c625SLionel Sambuc 			warnx("%s: %s", argv[i], gai_strerror(e));
231684d9c625SLionel Sambuc 		for (ai = sai; ai; ai = ai->ai_next) {
231784d9c625SLionel Sambuc 			sockaddr_snprintf(buf, sizeof(buf), "%a", ai->ai_addr);
231884d9c625SLionel Sambuc              		printf("flags=0x%x family=%d socktype=%d protocol=%d "
231984d9c625SLionel Sambuc 			    "addrlen=%zu addr=%s canonname=%s next=%p\n",
232084d9c625SLionel Sambuc 			    ai->ai_flags,
232184d9c625SLionel Sambuc              		    ai->ai_family,
232284d9c625SLionel Sambuc              		    ai->ai_socktype,
232384d9c625SLionel Sambuc              		    ai->ai_protocol,
232484d9c625SLionel Sambuc              		    (size_t)ai->ai_addrlen,
232584d9c625SLionel Sambuc 			    buf,
232684d9c625SLionel Sambuc 			    ai->ai_canonname,
232784d9c625SLionel Sambuc 			    ai->ai_next);
232884d9c625SLionel Sambuc 		}
232984d9c625SLionel Sambuc 		if (sai)
233084d9c625SLionel Sambuc 			freeaddrinfo(sai);
233184d9c625SLionel Sambuc 	}
233284d9c625SLionel Sambuc 	return 0;
233384d9c625SLionel Sambuc }
233484d9c625SLionel Sambuc #endif
2335