Home | History | Annotate | Line # | Download | only in net
getaddrinfo.c revision 1.106.4.1
      1  1.106.4.1  sborrill /*	$NetBSD: getaddrinfo.c,v 1.106.4.1 2016/01/15 17:37:45 sborrill Exp $	*/
      2       1.48    itojun /*	$KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $	*/
      3        1.6    itojun 
      4        1.1    itojun /*
      5        1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6        1.1    itojun  * All rights reserved.
      7       1.42    itojun  *
      8        1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9        1.1    itojun  * modification, are permitted provided that the following conditions
     10        1.1    itojun  * are met:
     11        1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15        1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16        1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17        1.1    itojun  *    may be used to endorse or promote products derived from this software
     18        1.1    itojun  *    without specific prior written permission.
     19       1.42    itojun  *
     20        1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21        1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24        1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.1    itojun  * SUCH DAMAGE.
     31        1.1    itojun  */
     32        1.1    itojun 
     33        1.1    itojun /*
     34        1.1    itojun  * Issues to be discussed:
     35        1.1    itojun  * - Return values.  There are nonstandard return values defined and used
     36       1.14    itojun  *   in the source code.  This is because RFC2553 is silent about which error
     37        1.1    itojun  *   code must be returned for which situation.
     38       1.27    itojun  * - IPv4 classful (shortened) form.  RFC2553 is silent about it.  XNET 5.2
     39       1.27    itojun  *   says to use inet_aton() to convert IPv4 numeric to binary (alows
     40       1.27    itojun  *   classful form as a result).
     41       1.27    itojun  *   current code - disallow classful form for IPv4 (due to use of inet_pton).
     42       1.27    itojun  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
     43       1.27    itojun  *   invalid.
     44       1.27    itojun  *   current code - SEGV on freeaddrinfo(NULL)
     45       1.14    itojun  * Note:
     46       1.14    itojun  * - The code filters out AFs that are not supported by the kernel,
     47       1.22    itojun  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
     48       1.14    itojun  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
     49       1.14    itojun  *   in ai_flags?
     50       1.30    itojun  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
     51       1.30    itojun  *   (1) what should we do against numeric hostname (2) what should we do
     52       1.30    itojun  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
     53       1.30    itojun  *   non-loopback address configured?  global address configured?
     54        1.1    itojun  */
     55       1.39    itojun 
     56       1.39    itojun #include <sys/cdefs.h>
     57       1.39    itojun #if defined(LIBC_SCCS) && !defined(lint)
     58  1.106.4.1  sborrill __RCSID("$NetBSD: getaddrinfo.c,v 1.106.4.1 2016/01/15 17:37:45 sborrill Exp $");
     59       1.39    itojun #endif /* LIBC_SCCS and not lint */
     60        1.1    itojun 
     61       1.38    itojun #include "namespace.h"
     62       1.14    itojun #include <sys/types.h>
     63        1.1    itojun #include <sys/param.h>
     64        1.1    itojun #include <sys/socket.h>
     65       1.14    itojun #include <net/if.h>
     66        1.1    itojun #include <netinet/in.h>
     67        1.1    itojun #include <arpa/inet.h>
     68        1.1    itojun #include <arpa/nameser.h>
     69       1.51     lukem #include <assert.h>
     70       1.51     lukem #include <ctype.h>
     71       1.51     lukem #include <errno.h>
     72        1.1    itojun #include <netdb.h>
     73        1.1    itojun #include <resolv.h>
     74       1.51     lukem #include <stddef.h>
     75       1.51     lukem #include <stdio.h>
     76       1.51     lukem #include <stdlib.h>
     77       1.14    itojun #include <string.h>
     78        1.1    itojun #include <unistd.h>
     79       1.98  christos #include <ifaddrs.h>
     80       1.14    itojun 
     81       1.32    itojun #include <syslog.h>
     82       1.32    itojun #include <stdarg.h>
     83       1.32    itojun #include <nsswitch.h>
     84       1.32    itojun 
     85       1.32    itojun #ifdef YP
     86       1.32    itojun #include <rpc/rpc.h>
     87       1.32    itojun #include <rpcsvc/yp_prot.h>
     88       1.32    itojun #include <rpcsvc/ypclnt.h>
     89       1.38    itojun #endif
     90       1.38    itojun 
     91       1.84  christos #include "servent.h"
     92       1.84  christos 
     93       1.38    itojun #ifdef __weak_alias
     94       1.38    itojun __weak_alias(getaddrinfo,_getaddrinfo)
     95       1.38    itojun __weak_alias(freeaddrinfo,_freeaddrinfo)
     96       1.38    itojun __weak_alias(gai_strerror,_gai_strerror)
     97       1.32    itojun #endif
     98       1.32    itojun 
     99        1.1    itojun #define SUCCESS 0
    100        1.1    itojun #define ANY 0
    101        1.1    itojun #define YES 1
    102        1.1    itojun #define NO  0
    103        1.1    itojun 
    104        1.1    itojun static const char in_addrany[] = { 0, 0, 0, 0 };
    105       1.49     lukem static const char in_loopback[] = { 127, 0, 0, 1 };
    106       1.49     lukem #ifdef INET6
    107        1.1    itojun static const char in6_addrany[] = {
    108        1.1    itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    109        1.1    itojun };
    110        1.1    itojun static const char in6_loopback[] = {
    111        1.1    itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
    112        1.1    itojun };
    113       1.49     lukem #endif
    114        1.1    itojun 
    115       1.14    itojun static const struct afd {
    116        1.1    itojun 	int a_af;
    117        1.1    itojun 	int a_addrlen;
    118        1.1    itojun 	int a_socklen;
    119        1.1    itojun 	int a_off;
    120        1.1    itojun 	const char *a_addrany;
    121      1.102  christos 	const char *a_loopback;
    122       1.14    itojun 	int a_scoped;
    123        1.1    itojun } afdl [] = {
    124        1.1    itojun #ifdef INET6
    125        1.1    itojun 	{PF_INET6, sizeof(struct in6_addr),
    126        1.1    itojun 	 sizeof(struct sockaddr_in6),
    127        1.1    itojun 	 offsetof(struct sockaddr_in6, sin6_addr),
    128       1.14    itojun 	 in6_addrany, in6_loopback, 1},
    129        1.1    itojun #endif
    130        1.1    itojun 	{PF_INET, sizeof(struct in_addr),
    131        1.1    itojun 	 sizeof(struct sockaddr_in),
    132        1.1    itojun 	 offsetof(struct sockaddr_in, sin_addr),
    133       1.14    itojun 	 in_addrany, in_loopback, 0},
    134       1.14    itojun 	{0, 0, 0, 0, NULL, NULL, 0},
    135       1.14    itojun };
    136       1.14    itojun 
    137       1.14    itojun struct explore {
    138       1.14    itojun 	int e_af;
    139       1.14    itojun 	int e_socktype;
    140       1.14    itojun 	int e_protocol;
    141       1.14    itojun 	const char *e_protostr;
    142       1.14    itojun 	int e_wild;
    143       1.14    itojun #define WILD_AF(ex)		((ex)->e_wild & 0x01)
    144       1.14    itojun #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
    145       1.14    itojun #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
    146       1.14    itojun };
    147       1.14    itojun 
    148       1.14    itojun static const struct explore explore[] = {
    149       1.14    itojun #if 0
    150       1.14    itojun 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
    151       1.14    itojun #endif
    152       1.14    itojun #ifdef INET6
    153       1.14    itojun 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    154       1.14    itojun 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    155       1.14    itojun 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
    156       1.14    itojun #endif
    157       1.14    itojun 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    158       1.14    itojun 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    159       1.14    itojun 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
    160       1.32    itojun 	{ PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    161       1.32    itojun 	{ PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    162       1.32    itojun 	{ PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },
    163       1.14    itojun 	{ -1, 0, 0, NULL, 0 },
    164        1.1    itojun };
    165        1.1    itojun 
    166        1.1    itojun #ifdef INET6
    167        1.1    itojun #define PTON_MAX	16
    168        1.1    itojun #else
    169        1.1    itojun #define PTON_MAX	4
    170        1.1    itojun #endif
    171        1.1    itojun 
    172       1.32    itojun static const ns_src default_dns_files[] = {
    173      1.102  christos 	{ NSSRC_FILES,	NS_SUCCESS },
    174      1.102  christos 	{ NSSRC_DNS,	NS_SUCCESS },
    175       1.87  christos 	{ 0, 0 }
    176       1.32    itojun };
    177       1.32    itojun 
    178       1.63    itojun #define MAXPACKET	(64*1024)
    179       1.32    itojun 
    180       1.32    itojun typedef union {
    181       1.32    itojun 	HEADER hdr;
    182       1.32    itojun 	u_char buf[MAXPACKET];
    183       1.32    itojun } querybuf;
    184       1.32    itojun 
    185       1.32    itojun struct res_target {
    186       1.32    itojun 	struct res_target *next;
    187       1.32    itojun 	const char *name;	/* domain name */
    188       1.43    itojun 	int qclass, qtype;	/* class and type of query */
    189       1.32    itojun 	u_char *answer;		/* buffer to put answer */
    190       1.32    itojun 	int anslen;		/* size of answer buffer */
    191       1.32    itojun 	int n;			/* result length */
    192       1.32    itojun };
    193        1.1    itojun 
    194      1.103  christos struct srvinfo {
    195      1.103  christos        struct srvinfo *next;
    196      1.103  christos        char name[MAXDNAME];
    197      1.103  christos        int port, pri, weight;
    198      1.103  christos };
    199      1.103  christos 
    200      1.103  christos static int gai_srvok(const char *);
    201       1.70  christos static int str2number(const char *);
    202       1.70  christos static int explore_fqdn(const struct addrinfo *, const char *,
    203      1.102  christos     const char *, struct addrinfo **, struct servent_data *);
    204       1.70  christos static int explore_null(const struct addrinfo *,
    205      1.102  christos     const char *, struct addrinfo **, struct servent_data *);
    206       1.70  christos static int explore_numeric(const struct addrinfo *, const char *,
    207      1.102  christos     const char *, struct addrinfo **, const char *, struct servent_data *);
    208       1.70  christos static int explore_numeric_scope(const struct addrinfo *, const char *,
    209      1.102  christos     const char *, struct addrinfo **, struct servent_data *);
    210       1.70  christos static int get_canonname(const struct addrinfo *,
    211      1.102  christos     struct addrinfo *, const char *);
    212       1.70  christos static struct addrinfo *get_ai(const struct addrinfo *,
    213      1.102  christos     const struct afd *, const char *);
    214       1.89  christos static int get_portmatch(const struct addrinfo *, const char *,
    215       1.89  christos     struct servent_data *);
    216       1.89  christos static int get_port(const struct addrinfo *, const char *, int,
    217       1.89  christos     struct servent_data *);
    218       1.70  christos static const struct afd *find_afd(int);
    219       1.98  christos static int addrconfig(uint64_t *);
    220       1.27    itojun #ifdef INET6
    221       1.70  christos static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
    222       1.42    itojun #endif
    223       1.14    itojun 
    224  1.106.4.1  sborrill static struct addrinfo *getanswer(res_state, const querybuf *, int,
    225  1.106.4.1  sborrill     const char *, int, const struct addrinfo *);
    226       1.73    tsarna static void aisort(struct addrinfo *s, res_state res);
    227      1.103  christos static struct addrinfo * _dns_query(struct res_target *,
    228      1.103  christos     const struct addrinfo *, res_state, int);
    229      1.103  christos static struct addrinfo * _dns_srv_lookup(const char *, const char *,
    230      1.103  christos     const struct addrinfo *);
    231      1.103  christos static struct addrinfo * _dns_host_lookup(const char *,
    232      1.103  christos     const struct addrinfo *);
    233       1.70  christos static int _dns_getaddrinfo(void *, void *, va_list);
    234       1.70  christos static void _sethtent(FILE **);
    235       1.70  christos static void _endhtent(FILE **);
    236       1.70  christos static struct addrinfo *_gethtent(FILE **, const char *,
    237       1.70  christos     const struct addrinfo *);
    238       1.70  christos static int _files_getaddrinfo(void *, void *, va_list);
    239       1.32    itojun #ifdef YP
    240       1.70  christos static struct addrinfo *_yphostent(char *, const struct addrinfo *);
    241       1.70  christos static int _yp_getaddrinfo(void *, void *, va_list);
    242       1.32    itojun #endif
    243       1.32    itojun 
    244       1.70  christos static int res_queryN(const char *, struct res_target *, res_state);
    245       1.73    tsarna static int res_searchN(const char *, struct res_target *, res_state);
    246       1.70  christos static int res_querydomainN(const char *, const char *,
    247      1.102  christos     struct res_target *, res_state);
    248       1.32    itojun 
    249       1.53  jdolecek static const char * const ai_errlist[] = {
    250        1.7     lukem 	"Success",
    251        1.7     lukem 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
    252      1.102  christos 	"Temporary failure in name resolution",		/* EAI_AGAIN	  */
    253      1.102  christos 	"Invalid value for ai_flags",			/* EAI_BADFLAGS	  */
    254      1.102  christos 	"Non-recoverable failure in name resolution",	/* EAI_FAIL	  */
    255      1.102  christos 	"ai_family not supported",			/* EAI_FAMILY	  */
    256      1.102  christos 	"Memory allocation failure",			/* EAI_MEMORY	  */
    257      1.102  christos 	"No address associated with hostname",		/* EAI_NODATA	  */
    258      1.102  christos 	"hostname nor servname provided, or not known", /* EAI_NONAME	  */
    259      1.102  christos 	"servname not supported for ai_socktype",	/* EAI_SERVICE	  */
    260      1.102  christos 	"ai_socktype not supported",			/* EAI_SOCKTYPE	  */
    261      1.102  christos 	"System error returned in errno",		/* EAI_SYSTEM	  */
    262        1.7     lukem 	"Invalid value for hints",			/* EAI_BADHINTS	  */
    263      1.102  christos 	"Resolved protocol is unknown",			/* EAI_PROTOCOL	  */
    264      1.102  christos 	"Argument buffer overflow",			/* EAI_OVERFLOW	  */
    265      1.102  christos 	"Unknown error",				/* EAI_MAX	  */
    266        1.1    itojun };
    267        1.1    itojun 
    268       1.14    itojun /* XXX macros that make external reference is BAD. */
    269       1.14    itojun 
    270      1.102  christos #define GET_AI(ai, afd, addr)					\
    271      1.102  christos do {								\
    272      1.102  christos 	/* external reference: pai, error, and label free */	\
    273      1.102  christos 	(ai) = get_ai(pai, (afd), (addr));			\
    274      1.102  christos 	if ((ai) == NULL) {					\
    275      1.102  christos 		error = EAI_MEMORY;				\
    276      1.102  christos 		goto free;					\
    277      1.102  christos 	}							\
    278       1.20   mycroft } while (/*CONSTCOND*/0)
    279       1.14    itojun 
    280      1.102  christos #define GET_PORT(ai, serv, svd)					\
    281      1.102  christos do {								\
    282      1.102  christos 	/* external reference: error and label free */		\
    283      1.102  christos 	error = get_port((ai), (serv), 0, (svd));		\
    284      1.102  christos 	if (error != 0)						\
    285      1.102  christos 		goto free;					\
    286       1.20   mycroft } while (/*CONSTCOND*/0)
    287       1.14    itojun 
    288      1.102  christos #define GET_CANONNAME(ai, str)					\
    289      1.102  christos do {								\
    290      1.102  christos 	/* external reference: pai, error and label free */	\
    291      1.102  christos 	error = get_canonname(pai, (ai), (str));		\
    292      1.102  christos 	if (error != 0)						\
    293      1.102  christos 		goto free;					\
    294       1.20   mycroft } while (/*CONSTCOND*/0)
    295       1.14    itojun 
    296      1.102  christos #define ERR(err)						\
    297      1.102  christos do {								\
    298      1.102  christos 	/* external reference: error, and label bad */		\
    299      1.102  christos 	error = (err);						\
    300      1.102  christos 	goto bad;						\
    301      1.102  christos 	/*NOTREACHED*/						\
    302       1.20   mycroft } while (/*CONSTCOND*/0)
    303       1.14    itojun 
    304      1.102  christos #define MATCH_FAMILY(x, y, w)						\
    305      1.102  christos 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC ||	\
    306      1.102  christos 	    (y) == PF_UNSPEC)))
    307      1.102  christos #define MATCH(x, y, w)							\
    308       1.20   mycroft 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
    309        1.1    itojun 
    310       1.70  christos const char *
    311       1.70  christos gai_strerror(int ecode)
    312        1.1    itojun {
    313        1.1    itojun 	if (ecode < 0 || ecode > EAI_MAX)
    314        1.1    itojun 		ecode = EAI_MAX;
    315       1.70  christos 	return ai_errlist[ecode];
    316        1.1    itojun }
    317        1.1    itojun 
    318        1.1    itojun void
    319       1.70  christos freeaddrinfo(struct addrinfo *ai)
    320        1.1    itojun {
    321        1.1    itojun 	struct addrinfo *next;
    322        1.1    itojun 
    323       1.51     lukem 	_DIAGASSERT(ai != NULL);
    324       1.51     lukem 
    325       1.26    itojun 	do {
    326        1.1    itojun 		next = ai->ai_next;
    327        1.1    itojun 		if (ai->ai_canonname)
    328        1.1    itojun 			free(ai->ai_canonname);
    329        1.1    itojun 		/* no need to free(ai->ai_addr) */
    330        1.1    itojun 		free(ai);
    331       1.27    itojun 		ai = next;
    332       1.27    itojun 	} while (ai);
    333        1.1    itojun }
    334        1.1    itojun 
    335      1.103  christos /*
    336      1.103  christos  * We don't want localization to affect us
    337      1.103  christos  */
    338      1.103  christos #define PERIOD '.'
    339      1.103  christos #define hyphenchar(c) ((c) == '-')
    340      1.103  christos #define periodchar(c) ((c) == PERIOD)
    341      1.103  christos #define underschar(c) ((c) == '_')
    342      1.103  christos #define alphachar(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
    343      1.103  christos #define digitchar(c) ((c) >= '0' && (c) <= '9')
    344      1.103  christos 
    345      1.103  christos #define firstchar(c)  (alphachar(c) || digitchar(c) || underschar(c))
    346      1.103  christos #define lastchar(c)   (alphachar(c) || digitchar(c))
    347      1.103  christos #define middlechar(c) (lastchar(c) || hyphenchar(c))
    348      1.103  christos 
    349      1.103  christos static int
    350      1.103  christos gai_srvok(const char *dn)
    351      1.103  christos {
    352      1.103  christos 	int nch, pch, ch;
    353      1.103  christos 
    354      1.103  christos 	for (pch = PERIOD, nch = ch = *dn++; ch != '\0'; pch = ch, ch = nch) {
    355      1.103  christos 		if (periodchar(ch))
    356      1.103  christos 			continue;
    357      1.103  christos 		if (periodchar(pch)) {
    358      1.103  christos 			if (!firstchar(ch))
    359      1.103  christos 				return 0;
    360      1.103  christos 		} else if (periodchar(nch) || nch == '\0') {
    361      1.103  christos 			if (!lastchar(ch))
    362      1.103  christos 				return 0;
    363      1.103  christos 		} else if (!middlechar(ch))
    364      1.103  christos 			return 0;
    365      1.103  christos        }
    366      1.103  christos        return 1;
    367      1.103  christos }
    368      1.103  christos 
    369      1.103  christos static in_port_t *
    370      1.103  christos getport(struct addrinfo *ai) {
    371      1.103  christos 	static in_port_t p;
    372      1.103  christos 
    373      1.103  christos 	switch (ai->ai_family) {
    374      1.103  christos 	case AF_INET:
    375      1.103  christos 		return &((struct sockaddr_in *)(void *)ai->ai_addr)->sin_port;
    376      1.103  christos #ifdef INET6
    377      1.103  christos 	case AF_INET6:
    378      1.103  christos 		return &((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_port;
    379      1.103  christos #endif
    380      1.103  christos 	default:
    381      1.103  christos 		p = 0;
    382      1.103  christos 		/* XXX: abort()? */
    383      1.103  christos 		return &p;
    384      1.103  christos 	}
    385      1.103  christos }
    386      1.103  christos 
    387        1.1    itojun static int
    388       1.70  christos str2number(const char *p)
    389        1.1    itojun {
    390       1.45    itojun 	char *ep;
    391       1.67    itojun 	unsigned long v;
    392       1.45    itojun 
    393       1.51     lukem 	_DIAGASSERT(p != NULL);
    394       1.51     lukem 
    395       1.46    itojun 	if (*p == '\0')
    396       1.67    itojun 		return -1;
    397       1.45    itojun 	ep = NULL;
    398       1.59    itojun 	errno = 0;
    399       1.67    itojun 	v = strtoul(p, &ep, 10);
    400       1.97  christos 	if (errno == 0 && ep && *ep == '\0' && v <= INT_MAX)
    401       1.97  christos 		return (int)v;
    402       1.45    itojun 	else
    403       1.67    itojun 		return -1;
    404        1.1    itojun }
    405        1.1    itojun 
    406        1.1    itojun int
    407       1.70  christos getaddrinfo(const char *hostname, const char *servname,
    408       1.70  christos     const struct addrinfo *hints, struct addrinfo **res)
    409        1.1    itojun {
    410        1.1    itojun 	struct addrinfo sentinel;
    411        1.1    itojun 	struct addrinfo *cur;
    412       1.14    itojun 	int error = 0;
    413        1.1    itojun 	struct addrinfo ai;
    414       1.14    itojun 	struct addrinfo ai0;
    415        1.1    itojun 	struct addrinfo *pai;
    416       1.14    itojun 	const struct explore *ex;
    417       1.89  christos 	struct servent_data svd;
    418       1.98  christos 	uint64_t mask = (uint64_t)~0ULL;
    419        1.1    itojun 
    420       1.51     lukem 	/* hostname is allowed to be NULL */
    421       1.51     lukem 	/* servname is allowed to be NULL */
    422       1.51     lukem 	/* hints is allowed to be NULL */
    423       1.51     lukem 	_DIAGASSERT(res != NULL);
    424       1.51     lukem 
    425       1.89  christos 	(void)memset(&svd, 0, sizeof(svd));
    426       1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
    427        1.1    itojun 	cur = &sentinel;
    428       1.86  christos 	memset(&ai, 0, sizeof(ai));
    429        1.1    itojun 	pai = &ai;
    430        1.1    itojun 	pai->ai_flags = 0;
    431        1.1    itojun 	pai->ai_family = PF_UNSPEC;
    432        1.1    itojun 	pai->ai_socktype = ANY;
    433        1.1    itojun 	pai->ai_protocol = ANY;
    434        1.1    itojun 	pai->ai_addrlen = 0;
    435        1.1    itojun 	pai->ai_canonname = NULL;
    436        1.1    itojun 	pai->ai_addr = NULL;
    437        1.1    itojun 	pai->ai_next = NULL;
    438      1.102  christos 
    439        1.1    itojun 	if (hostname == NULL && servname == NULL)
    440        1.1    itojun 		return EAI_NONAME;
    441        1.1    itojun 	if (hints) {
    442        1.1    itojun 		/* error check for hints */
    443        1.1    itojun 		if (hints->ai_addrlen || hints->ai_canonname ||
    444        1.1    itojun 		    hints->ai_addr || hints->ai_next)
    445        1.1    itojun 			ERR(EAI_BADHINTS); /* xxx */
    446        1.1    itojun 		if (hints->ai_flags & ~AI_MASK)
    447        1.1    itojun 			ERR(EAI_BADFLAGS);
    448        1.1    itojun 		switch (hints->ai_family) {
    449        1.1    itojun 		case PF_UNSPEC:
    450        1.1    itojun 		case PF_INET:
    451        1.1    itojun #ifdef INET6
    452        1.1    itojun 		case PF_INET6:
    453        1.1    itojun #endif
    454        1.1    itojun 			break;
    455        1.1    itojun 		default:
    456        1.1    itojun 			ERR(EAI_FAMILY);
    457        1.1    itojun 		}
    458        1.1    itojun 		memcpy(pai, hints, sizeof(*pai));
    459       1.14    itojun 
    460       1.14    itojun 		/*
    461       1.14    itojun 		 * if both socktype/protocol are specified, check if they
    462       1.14    itojun 		 * are meaningful combination.
    463       1.14    itojun 		 */
    464       1.14    itojun 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
    465       1.14    itojun 			for (ex = explore; ex->e_af >= 0; ex++) {
    466       1.14    itojun 				if (pai->ai_family != ex->e_af)
    467       1.14    itojun 					continue;
    468       1.14    itojun 				if (ex->e_socktype == ANY)
    469       1.14    itojun 					continue;
    470       1.14    itojun 				if (ex->e_protocol == ANY)
    471       1.14    itojun 					continue;
    472       1.14    itojun 				if (pai->ai_socktype == ex->e_socktype
    473       1.14    itojun 				 && pai->ai_protocol != ex->e_protocol) {
    474       1.14    itojun 					ERR(EAI_BADHINTS);
    475       1.14    itojun 				}
    476        1.1    itojun 			}
    477        1.1    itojun 		}
    478        1.1    itojun 	}
    479        1.1    itojun 
    480       1.98  christos 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && addrconfig(&mask) == -1)
    481       1.98  christos 		ERR(EAI_FAIL);
    482       1.98  christos 
    483        1.1    itojun 	/*
    484       1.14    itojun 	 * check for special cases.  (1) numeric servname is disallowed if
    485       1.14    itojun 	 * socktype/protocol are left unspecified. (2) servname is disallowed
    486       1.14    itojun 	 * for raw and other inet{,6} sockets.
    487        1.1    itojun 	 */
    488       1.14    itojun 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
    489       1.27    itojun #ifdef PF_INET6
    490       1.27    itojun 	 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
    491       1.27    itojun #endif
    492       1.27    itojun 	    ) {
    493       1.23    itojun 		ai0 = *pai;	/* backup *pai */
    494       1.21   mycroft 
    495       1.27    itojun 		if (pai->ai_family == PF_UNSPEC) {
    496       1.27    itojun #ifdef PF_INET6
    497       1.14    itojun 			pai->ai_family = PF_INET6;
    498       1.27    itojun #else
    499       1.27    itojun 			pai->ai_family = PF_INET;
    500       1.27    itojun #endif
    501       1.27    itojun 		}
    502       1.89  christos 		error = get_portmatch(pai, servname, &svd);
    503       1.14    itojun 		if (error)
    504      1.101    martin 			goto bad;
    505       1.21   mycroft 
    506       1.21   mycroft 		*pai = ai0;
    507       1.14    itojun 	}
    508       1.14    itojun 
    509       1.14    itojun 	ai0 = *pai;
    510       1.14    itojun 
    511       1.14    itojun 	/* NULL hostname, or numeric hostname */
    512       1.14    itojun 	for (ex = explore; ex->e_af >= 0; ex++) {
    513       1.14    itojun 		*pai = ai0;
    514       1.14    itojun 
    515       1.98  christos 		/* ADDRCONFIG check */
    516       1.98  christos 		if ((((uint64_t)1 << ex->e_af) & mask) == 0)
    517       1.98  christos 			continue;
    518       1.98  christos 
    519       1.32    itojun 		/* PF_UNSPEC entries are prepared for DNS queries only */
    520       1.32    itojun 		if (ex->e_af == PF_UNSPEC)
    521       1.32    itojun 			continue;
    522       1.32    itojun 
    523       1.14    itojun 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    524       1.14    itojun 			continue;
    525       1.14    itojun 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
    526       1.14    itojun 			continue;
    527       1.14    itojun 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
    528       1.14    itojun 			continue;
    529       1.14    itojun 		if (pai->ai_family == PF_UNSPEC)
    530       1.14    itojun 			pai->ai_family = ex->e_af;
    531       1.14    itojun 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    532       1.14    itojun 			pai->ai_socktype = ex->e_socktype;
    533       1.14    itojun 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    534       1.14    itojun 			pai->ai_protocol = ex->e_protocol;
    535       1.14    itojun 
    536       1.14    itojun 		if (hostname == NULL)
    537       1.89  christos 			error = explore_null(pai, servname, &cur->ai_next,
    538       1.89  christos 			    &svd);
    539       1.14    itojun 		else
    540       1.65    itojun 			error = explore_numeric_scope(pai, hostname, servname,
    541       1.89  christos 			    &cur->ai_next, &svd);
    542       1.14    itojun 
    543       1.14    itojun 		if (error)
    544       1.14    itojun 			goto free;
    545       1.14    itojun 
    546       1.79  christos 		while (cur->ai_next)
    547       1.14    itojun 			cur = cur->ai_next;
    548       1.14    itojun 	}
    549       1.14    itojun 
    550       1.14    itojun 	/*
    551       1.14    itojun 	 * XXX
    552       1.76  ginsbach 	 * If numeric representation of AF1 can be interpreted as FQDN
    553       1.14    itojun 	 * representation of AF2, we need to think again about the code below.
    554       1.14    itojun 	 */
    555       1.14    itojun 	if (sentinel.ai_next)
    556       1.14    itojun 		goto good;
    557       1.14    itojun 
    558       1.14    itojun 	if (hostname == NULL)
    559       1.43    itojun 		ERR(EAI_NODATA);
    560       1.64    itojun 	if (pai->ai_flags & AI_NUMERICHOST)
    561       1.64    itojun 		ERR(EAI_NONAME);
    562       1.14    itojun 
    563       1.14    itojun 	/*
    564       1.14    itojun 	 * hostname as alphabetical name.
    565       1.14    itojun 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
    566       1.14    itojun 	 * outer loop by AFs.
    567       1.14    itojun 	 */
    568       1.32    itojun 	for (ex = explore; ex->e_af >= 0; ex++) {
    569       1.14    itojun 		*pai = ai0;
    570       1.14    itojun 
    571      1.100    kardel 
    572       1.98  christos 		/* ADDRCONFIG check */
    573      1.100    kardel 		/* PF_UNSPEC entries are prepared for DNS queries only */
    574      1.100    kardel 		if (ex->e_af != PF_UNSPEC &&
    575      1.100    kardel 		    (((uint64_t)1 << ex->e_af) & mask) == 0)
    576       1.98  christos 			continue;
    577       1.98  christos 
    578       1.32    itojun 		/* require exact match for family field */
    579       1.32    itojun 		if (pai->ai_family != ex->e_af)
    580       1.14    itojun 			continue;
    581       1.14    itojun 
    582       1.32    itojun 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
    583       1.32    itojun 				WILD_SOCKTYPE(ex))) {
    584       1.32    itojun 			continue;
    585       1.32    itojun 		}
    586       1.32    itojun 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
    587       1.32    itojun 				WILD_PROTOCOL(ex))) {
    588       1.32    itojun 			continue;
    589       1.32    itojun 		}
    590       1.14    itojun 
    591       1.32    itojun 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    592       1.32    itojun 			pai->ai_socktype = ex->e_socktype;
    593       1.32    itojun 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    594       1.32    itojun 			pai->ai_protocol = ex->e_protocol;
    595        1.1    itojun 
    596       1.89  christos 		error = explore_fqdn(pai, hostname, servname, &cur->ai_next,
    597       1.89  christos 		    &svd);
    598       1.14    itojun 
    599       1.32    itojun 		while (cur && cur->ai_next)
    600       1.32    itojun 			cur = cur->ai_next;
    601        1.1    itojun 	}
    602       1.14    itojun 
    603       1.14    itojun 	/* XXX */
    604       1.14    itojun 	if (sentinel.ai_next)
    605       1.14    itojun 		error = 0;
    606       1.14    itojun 
    607       1.14    itojun 	if (error)
    608       1.14    itojun 		goto free;
    609       1.89  christos 
    610       1.89  christos 	if (sentinel.ai_next) {
    611       1.14    itojun  good:
    612       1.89  christos 		endservent_r(&svd);
    613       1.89  christos 		*res = sentinel.ai_next;
    614       1.89  christos 		return SUCCESS;
    615       1.89  christos 	} else
    616       1.89  christos 		error = EAI_FAIL;
    617       1.14    itojun  free:
    618       1.14    itojun  bad:
    619       1.89  christos 	endservent_r(&svd);
    620       1.14    itojun 	if (sentinel.ai_next)
    621       1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    622       1.14    itojun 	*res = NULL;
    623       1.14    itojun 	return error;
    624       1.14    itojun }
    625       1.14    itojun 
    626       1.14    itojun /*
    627       1.14    itojun  * FQDN hostname, DNS lookup
    628       1.14    itojun  */
    629       1.14    itojun static int
    630       1.70  christos explore_fqdn(const struct addrinfo *pai, const char *hostname,
    631       1.89  christos     const char *servname, struct addrinfo **res, struct servent_data *svd)
    632       1.14    itojun {
    633       1.32    itojun 	struct addrinfo *result;
    634       1.32    itojun 	struct addrinfo *cur;
    635       1.27    itojun 	int error = 0;
    636       1.32    itojun 	static const ns_dtab dtab[] = {
    637       1.32    itojun 		NS_FILES_CB(_files_getaddrinfo, NULL)
    638       1.32    itojun 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
    639       1.32    itojun 		NS_NIS_CB(_yp_getaddrinfo, NULL)
    640       1.87  christos 		NS_NULL_CB
    641       1.32    itojun 	};
    642       1.14    itojun 
    643       1.51     lukem 	_DIAGASSERT(pai != NULL);
    644       1.51     lukem 	/* hostname may be NULL */
    645       1.51     lukem 	/* servname may be NULL */
    646       1.51     lukem 	_DIAGASSERT(res != NULL);
    647       1.51     lukem 
    648       1.32    itojun 	result = NULL;
    649       1.14    itojun 
    650       1.14    itojun 	/*
    651       1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    652       1.14    itojun 	 */
    653       1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    654       1.14    itojun 		return 0;
    655       1.14    itojun 
    656       1.32    itojun 	switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
    657      1.103  christos 	    default_dns_files, hostname, pai, servname)) {
    658       1.32    itojun 	case NS_TRYAGAIN:
    659       1.32    itojun 		error = EAI_AGAIN;
    660       1.32    itojun 		goto free;
    661       1.32    itojun 	case NS_UNAVAIL:
    662       1.14    itojun 		error = EAI_FAIL;
    663       1.14    itojun 		goto free;
    664       1.32    itojun 	case NS_NOTFOUND:
    665       1.32    itojun 		error = EAI_NODATA;
    666       1.15    itojun 		goto free;
    667       1.32    itojun 	case NS_SUCCESS:
    668       1.32    itojun 		error = 0;
    669       1.32    itojun 		for (cur = result; cur; cur = cur->ai_next) {
    670      1.103  christos 			/* Check for already filled port. */
    671      1.103  christos 			if (*getport(cur))
    672      1.103  christos 				continue;
    673       1.89  christos 			GET_PORT(cur, servname, svd);
    674       1.32    itojun 			/* canonname should be filled already */
    675       1.16    itojun 		}
    676       1.32    itojun 		break;
    677       1.16    itojun 	}
    678       1.15    itojun 
    679       1.32    itojun 	*res = result;
    680       1.14    itojun 
    681       1.14    itojun 	return 0;
    682       1.14    itojun 
    683       1.14    itojun free:
    684       1.34    itojun 	if (result)
    685       1.34    itojun 		freeaddrinfo(result);
    686       1.14    itojun 	return error;
    687       1.14    itojun }
    688       1.14    itojun 
    689       1.14    itojun /*
    690       1.14    itojun  * hostname == NULL.
    691       1.14    itojun  * passive socket -> anyaddr (0.0.0.0 or ::)
    692       1.14    itojun  * non-passive socket -> localhost (127.0.0.1 or ::1)
    693       1.14    itojun  */
    694       1.14    itojun static int
    695       1.70  christos explore_null(const struct addrinfo *pai, const char *servname,
    696       1.89  christos     struct addrinfo **res, struct servent_data *svd)
    697       1.14    itojun {
    698       1.14    itojun 	int s;
    699       1.14    itojun 	const struct afd *afd;
    700       1.14    itojun 	struct addrinfo *cur;
    701       1.14    itojun 	struct addrinfo sentinel;
    702       1.14    itojun 	int error;
    703       1.14    itojun 
    704       1.51     lukem 	_DIAGASSERT(pai != NULL);
    705       1.51     lukem 	/* servname may be NULL */
    706       1.51     lukem 	_DIAGASSERT(res != NULL);
    707       1.51     lukem 
    708       1.14    itojun 	*res = NULL;
    709       1.14    itojun 	sentinel.ai_next = NULL;
    710       1.14    itojun 	cur = &sentinel;
    711       1.14    itojun 
    712       1.14    itojun 	/*
    713       1.14    itojun 	 * filter out AFs that are not supported by the kernel
    714       1.14    itojun 	 * XXX errno?
    715       1.14    itojun 	 */
    716       1.14    itojun 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    717       1.14    itojun 	if (s < 0) {
    718       1.14    itojun 		if (errno != EMFILE)
    719       1.14    itojun 			return 0;
    720       1.14    itojun 	} else
    721       1.14    itojun 		close(s);
    722       1.14    itojun 
    723       1.14    itojun 	/*
    724       1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    725       1.14    itojun 	 */
    726       1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    727       1.14    itojun 		return 0;
    728       1.14    itojun 
    729       1.14    itojun 	afd = find_afd(pai->ai_family);
    730       1.42    itojun 	if (afd == NULL)
    731       1.42    itojun 		return 0;
    732       1.14    itojun 
    733       1.14    itojun 	if (pai->ai_flags & AI_PASSIVE) {
    734       1.14    itojun 		GET_AI(cur->ai_next, afd, afd->a_addrany);
    735       1.14    itojun 		/* xxx meaningless?
    736       1.14    itojun 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
    737       1.14    itojun 		 */
    738       1.89  christos 		GET_PORT(cur->ai_next, servname, svd);
    739       1.14    itojun 	} else {
    740       1.14    itojun 		GET_AI(cur->ai_next, afd, afd->a_loopback);
    741       1.14    itojun 		/* xxx meaningless?
    742       1.14    itojun 		 * GET_CANONNAME(cur->ai_next, "localhost");
    743       1.14    itojun 		 */
    744       1.89  christos 		GET_PORT(cur->ai_next, servname, svd);
    745       1.14    itojun 	}
    746       1.14    itojun 	cur = cur->ai_next;
    747       1.14    itojun 
    748       1.14    itojun 	*res = sentinel.ai_next;
    749       1.14    itojun 	return 0;
    750       1.14    itojun 
    751       1.14    itojun free:
    752       1.14    itojun 	if (sentinel.ai_next)
    753       1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    754       1.14    itojun 	return error;
    755       1.14    itojun }
    756       1.14    itojun 
    757       1.14    itojun /*
    758       1.14    itojun  * numeric hostname
    759       1.14    itojun  */
    760       1.14    itojun static int
    761       1.70  christos explore_numeric(const struct addrinfo *pai, const char *hostname,
    762       1.89  christos     const char *servname, struct addrinfo **res, const char *canonname,
    763       1.89  christos     struct servent_data *svd)
    764       1.14    itojun {
    765       1.14    itojun 	const struct afd *afd;
    766       1.14    itojun 	struct addrinfo *cur;
    767       1.14    itojun 	struct addrinfo sentinel;
    768       1.14    itojun 	int error;
    769       1.14    itojun 	char pton[PTON_MAX];
    770       1.14    itojun 
    771       1.51     lukem 	_DIAGASSERT(pai != NULL);
    772       1.51     lukem 	/* hostname may be NULL */
    773       1.51     lukem 	/* servname may be NULL */
    774       1.51     lukem 	_DIAGASSERT(res != NULL);
    775       1.51     lukem 
    776       1.14    itojun 	*res = NULL;
    777       1.14    itojun 	sentinel.ai_next = NULL;
    778       1.14    itojun 	cur = &sentinel;
    779       1.14    itojun 
    780       1.14    itojun 	/*
    781       1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    782       1.14    itojun 	 */
    783       1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    784       1.14    itojun 		return 0;
    785       1.14    itojun 
    786       1.14    itojun 	afd = find_afd(pai->ai_family);
    787       1.42    itojun 	if (afd == NULL)
    788       1.42    itojun 		return 0;
    789       1.14    itojun 
    790       1.27    itojun 	switch (afd->a_af) {
    791       1.27    itojun #if 0 /*X/Open spec*/
    792       1.27    itojun 	case AF_INET:
    793       1.27    itojun 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
    794       1.27    itojun 			if (pai->ai_family == afd->a_af ||
    795       1.27    itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    796       1.27    itojun 				GET_AI(cur->ai_next, afd, pton);
    797       1.89  christos 				GET_PORT(cur->ai_next, servname, svd);
    798       1.65    itojun 				if ((pai->ai_flags & AI_CANONNAME)) {
    799       1.65    itojun 					/*
    800       1.65    itojun 					 * Set the numeric address itself as
    801       1.65    itojun 					 * the canonical name, based on a
    802       1.65    itojun 					 * clarification in rfc2553bis-03.
    803       1.65    itojun 					 */
    804       1.65    itojun 					GET_CANONNAME(cur->ai_next, canonname);
    805       1.65    itojun 				}
    806       1.27    itojun 				while (cur && cur->ai_next)
    807       1.27    itojun 					cur = cur->ai_next;
    808       1.42    itojun 			} else
    809       1.27    itojun 				ERR(EAI_FAMILY);	/*xxx*/
    810       1.27    itojun 		}
    811       1.27    itojun 		break;
    812       1.27    itojun #endif
    813       1.27    itojun 	default:
    814       1.27    itojun 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
    815       1.27    itojun 			if (pai->ai_family == afd->a_af ||
    816       1.27    itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    817       1.27    itojun 				GET_AI(cur->ai_next, afd, pton);
    818       1.89  christos 				GET_PORT(cur->ai_next, servname, svd);
    819       1.65    itojun 				if ((pai->ai_flags & AI_CANONNAME)) {
    820       1.65    itojun 					/*
    821       1.65    itojun 					 * Set the numeric address itself as
    822       1.65    itojun 					 * the canonical name, based on a
    823       1.65    itojun 					 * clarification in rfc2553bis-03.
    824       1.65    itojun 					 */
    825       1.65    itojun 					GET_CANONNAME(cur->ai_next, canonname);
    826       1.65    itojun 				}
    827       1.81  christos 				while (cur->ai_next)
    828       1.27    itojun 					cur = cur->ai_next;
    829       1.42    itojun 			} else
    830       1.27    itojun 				ERR(EAI_FAMILY);	/*xxx*/
    831       1.27    itojun 		}
    832       1.27    itojun 		break;
    833        1.1    itojun 	}
    834        1.1    itojun 
    835       1.14    itojun 	*res = sentinel.ai_next;
    836       1.14    itojun 	return 0;
    837       1.14    itojun 
    838       1.14    itojun free:
    839       1.14    itojun bad:
    840       1.14    itojun 	if (sentinel.ai_next)
    841       1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    842       1.14    itojun 	return error;
    843       1.14    itojun }
    844       1.14    itojun 
    845       1.14    itojun /*
    846       1.14    itojun  * numeric hostname with scope
    847       1.14    itojun  */
    848       1.14    itojun static int
    849       1.70  christos explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
    850       1.89  christos     const char *servname, struct addrinfo **res, struct servent_data *svd)
    851       1.14    itojun {
    852       1.29    itojun #if !defined(SCOPE_DELIMITER) || !defined(INET6)
    853       1.90  christos 	return explore_numeric(pai, hostname, servname, res, hostname, svd);
    854       1.14    itojun #else
    855       1.14    itojun 	const struct afd *afd;
    856       1.14    itojun 	struct addrinfo *cur;
    857       1.14    itojun 	int error;
    858       1.30    itojun 	char *cp, *hostname2 = NULL, *scope, *addr;
    859       1.14    itojun 	struct sockaddr_in6 *sin6;
    860       1.14    itojun 
    861       1.51     lukem 	_DIAGASSERT(pai != NULL);
    862       1.51     lukem 	/* hostname may be NULL */
    863       1.51     lukem 	/* servname may be NULL */
    864       1.51     lukem 	_DIAGASSERT(res != NULL);
    865       1.51     lukem 
    866       1.14    itojun 	/*
    867       1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    868       1.14    itojun 	 */
    869       1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    870       1.14    itojun 		return 0;
    871       1.14    itojun 
    872       1.14    itojun 	afd = find_afd(pai->ai_family);
    873       1.42    itojun 	if (afd == NULL)
    874       1.42    itojun 		return 0;
    875       1.42    itojun 
    876       1.14    itojun 	if (!afd->a_scoped)
    877       1.89  christos 		return explore_numeric(pai, hostname, servname, res, hostname,
    878       1.89  christos 		    svd);
    879       1.14    itojun 
    880       1.14    itojun 	cp = strchr(hostname, SCOPE_DELIMITER);
    881       1.14    itojun 	if (cp == NULL)
    882       1.89  christos 		return explore_numeric(pai, hostname, servname, res, hostname,
    883       1.89  christos 		    svd);
    884       1.14    itojun 
    885       1.30    itojun 	/*
    886       1.30    itojun 	 * Handle special case of <scoped_address><delimiter><scope id>
    887       1.30    itojun 	 */
    888       1.30    itojun 	hostname2 = strdup(hostname);
    889       1.30    itojun 	if (hostname2 == NULL)
    890       1.30    itojun 		return EAI_MEMORY;
    891       1.30    itojun 	/* terminate at the delimiter */
    892       1.30    itojun 	hostname2[cp - hostname] = '\0';
    893       1.30    itojun 	addr = hostname2;
    894       1.30    itojun 	scope = cp + 1;
    895        1.1    itojun 
    896       1.89  christos 	error = explore_numeric(pai, addr, servname, res, hostname, svd);
    897        1.1    itojun 	if (error == 0) {
    898       1.59    itojun 		u_int32_t scopeid;
    899       1.27    itojun 
    900       1.14    itojun 		for (cur = *res; cur; cur = cur->ai_next) {
    901       1.14    itojun 			if (cur->ai_family != AF_INET6)
    902       1.14    itojun 				continue;
    903       1.36  christos 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
    904       1.60    itojun 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
    905       1.27    itojun 				free(hostname2);
    906       1.43    itojun 				return(EAI_NODATA); /* XXX: is return OK? */
    907       1.27    itojun 			}
    908       1.27    itojun 			sin6->sin6_scope_id = scopeid;
    909       1.14    itojun 		}
    910        1.1    itojun 	}
    911       1.14    itojun 
    912       1.14    itojun 	free(hostname2);
    913       1.14    itojun 
    914        1.1    itojun 	return error;
    915       1.14    itojun #endif
    916        1.1    itojun }
    917        1.1    itojun 
    918        1.1    itojun static int
    919       1.70  christos get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
    920        1.1    itojun {
    921       1.51     lukem 
    922       1.51     lukem 	_DIAGASSERT(pai != NULL);
    923       1.51     lukem 	_DIAGASSERT(ai != NULL);
    924       1.51     lukem 	_DIAGASSERT(str != NULL);
    925       1.51     lukem 
    926       1.14    itojun 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
    927       1.66    itojun 		ai->ai_canonname = strdup(str);
    928       1.14    itojun 		if (ai->ai_canonname == NULL)
    929       1.14    itojun 			return EAI_MEMORY;
    930       1.14    itojun 	}
    931       1.14    itojun 	return 0;
    932       1.14    itojun }
    933        1.1    itojun 
    934       1.93    tsarna struct addrinfo *
    935       1.93    tsarna allocaddrinfo(socklen_t addrlen)
    936       1.93    tsarna {
    937       1.93    tsarna 	struct addrinfo *ai;
    938       1.93    tsarna 
    939       1.93    tsarna 	ai = calloc(sizeof(struct addrinfo) + addrlen, 1);
    940       1.93    tsarna 	if (ai) {
    941       1.93    tsarna 		ai->ai_addr = (void *)(ai+1);
    942       1.93    tsarna 		ai->ai_addrlen = ai->ai_addr->sa_len = addrlen;
    943       1.93    tsarna 	}
    944       1.93    tsarna 
    945       1.93    tsarna 	return ai;
    946       1.93    tsarna }
    947       1.93    tsarna 
    948       1.14    itojun static struct addrinfo *
    949       1.70  christos get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
    950       1.14    itojun {
    951       1.14    itojun 	char *p;
    952       1.14    itojun 	struct addrinfo *ai;
    953       1.93    tsarna 	struct sockaddr *save;
    954       1.12     lukem 
    955       1.51     lukem 	_DIAGASSERT(pai != NULL);
    956       1.51     lukem 	_DIAGASSERT(afd != NULL);
    957       1.51     lukem 	_DIAGASSERT(addr != NULL);
    958       1.51     lukem 
    959       1.93    tsarna 	ai = allocaddrinfo((socklen_t)afd->a_socklen);
    960       1.14    itojun 	if (ai == NULL)
    961       1.14    itojun 		return NULL;
    962       1.14    itojun 
    963      1.102  christos 	save = ai->ai_addr;
    964       1.14    itojun 	memcpy(ai, pai, sizeof(struct addrinfo));
    965       1.93    tsarna 
    966      1.102  christos 	/* since we just overwrote all of ai, we have
    967      1.102  christos 	   to restore ai_addr and ai_addrlen */
    968      1.102  christos 	ai->ai_addr = save;
    969      1.102  christos 	ai->ai_addrlen = (socklen_t)afd->a_socklen;
    970      1.102  christos 
    971       1.14    itojun 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
    972       1.36  christos 	p = (char *)(void *)(ai->ai_addr);
    973       1.20   mycroft 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
    974       1.14    itojun 	return ai;
    975       1.14    itojun }
    976        1.1    itojun 
    977       1.14    itojun static int
    978       1.89  christos get_portmatch(const struct addrinfo *ai, const char *servname,
    979       1.89  christos     struct servent_data *svd)
    980       1.14    itojun {
    981        1.4    itojun 
    982       1.51     lukem 	_DIAGASSERT(ai != NULL);
    983       1.51     lukem 	/* servname may be NULL */
    984       1.51     lukem 
    985       1.89  christos 	return get_port(ai, servname, 1, svd);
    986        1.1    itojun }
    987        1.1    itojun 
    988        1.1    itojun static int
    989       1.89  christos get_port(const struct addrinfo *ai, const char *servname, int matchonly,
    990       1.89  christos     struct servent_data *svd)
    991        1.1    itojun {
    992       1.14    itojun 	const char *proto;
    993       1.14    itojun 	struct servent *sp;
    994       1.14    itojun 	int port;
    995       1.14    itojun 	int allownumeric;
    996       1.12     lukem 
    997       1.51     lukem 	_DIAGASSERT(ai != NULL);
    998       1.51     lukem 	/* servname may be NULL */
    999       1.51     lukem 
   1000       1.14    itojun 	if (servname == NULL)
   1001       1.14    itojun 		return 0;
   1002       1.23    itojun 	switch (ai->ai_family) {
   1003       1.23    itojun 	case AF_INET:
   1004       1.27    itojun #ifdef AF_INET6
   1005       1.23    itojun 	case AF_INET6:
   1006       1.27    itojun #endif
   1007       1.23    itojun 		break;
   1008       1.23    itojun 	default:
   1009       1.14    itojun 		return 0;
   1010       1.23    itojun 	}
   1011        1.1    itojun 
   1012       1.14    itojun 	switch (ai->ai_socktype) {
   1013       1.14    itojun 	case SOCK_RAW:
   1014       1.14    itojun 		return EAI_SERVICE;
   1015       1.14    itojun 	case SOCK_DGRAM:
   1016       1.14    itojun 	case SOCK_STREAM:
   1017       1.14    itojun 		allownumeric = 1;
   1018       1.14    itojun 		break;
   1019       1.14    itojun 	case ANY:
   1020       1.92       tls 		/*
   1021      1.102  christos 		 * This was 0.	It is now 1 so that queries specifying
   1022       1.92       tls 		 * a NULL hint, or hint without socktype (but, hopefully,
   1023       1.92       tls 		 * with protocol) and numeric address actually work.
   1024       1.92       tls 		 */
   1025       1.92       tls 		allownumeric = 1;
   1026       1.14    itojun 		break;
   1027       1.14    itojun 	default:
   1028       1.14    itojun 		return EAI_SOCKTYPE;
   1029        1.1    itojun 	}
   1030       1.14    itojun 
   1031       1.67    itojun 	port = str2number(servname);
   1032       1.67    itojun 	if (port >= 0) {
   1033       1.14    itojun 		if (!allownumeric)
   1034       1.14    itojun 			return EAI_SERVICE;
   1035       1.14    itojun 		if (port < 0 || port > 65535)
   1036       1.14    itojun 			return EAI_SERVICE;
   1037       1.58    itojun 		port = htons(port);
   1038       1.14    itojun 	} else {
   1039       1.84  christos 		struct servent sv;
   1040       1.67    itojun 		if (ai->ai_flags & AI_NUMERICSERV)
   1041       1.67    itojun 			return EAI_NONAME;
   1042       1.67    itojun 
   1043       1.14    itojun 		switch (ai->ai_socktype) {
   1044       1.14    itojun 		case SOCK_DGRAM:
   1045       1.14    itojun 			proto = "udp";
   1046        1.1    itojun 			break;
   1047       1.14    itojun 		case SOCK_STREAM:
   1048       1.14    itojun 			proto = "tcp";
   1049        1.1    itojun 			break;
   1050        1.1    itojun 		default:
   1051       1.14    itojun 			proto = NULL;
   1052        1.1    itojun 			break;
   1053        1.1    itojun 		}
   1054       1.14    itojun 
   1055       1.89  christos 		sp = getservbyname_r(servname, proto, &sv, svd);
   1056       1.85  christos 		if (sp == NULL)
   1057       1.14    itojun 			return EAI_SERVICE;
   1058       1.14    itojun 		port = sp->s_port;
   1059        1.1    itojun 	}
   1060        1.1    itojun 
   1061      1.103  christos 	if (!matchonly)
   1062      1.103  christos 		*getport(__UNCONST(ai)) = port;
   1063       1.14    itojun 	return 0;
   1064       1.14    itojun }
   1065       1.14    itojun 
   1066       1.14    itojun static const struct afd *
   1067       1.70  christos find_afd(int af)
   1068       1.14    itojun {
   1069       1.14    itojun 	const struct afd *afd;
   1070        1.1    itojun 
   1071       1.14    itojun 	if (af == PF_UNSPEC)
   1072       1.14    itojun 		return NULL;
   1073       1.14    itojun 	for (afd = afdl; afd->a_af; afd++) {
   1074       1.14    itojun 		if (afd->a_af == af)
   1075       1.14    itojun 			return afd;
   1076        1.1    itojun 	}
   1077       1.14    itojun 	return NULL;
   1078        1.1    itojun }
   1079       1.27    itojun 
   1080       1.98  christos /*
   1081       1.98  christos  * AI_ADDRCONFIG check: Build a mask containing a bit set for each address
   1082       1.98  christos  * family configured in the system.
   1083       1.98  christos  *
   1084       1.98  christos  */
   1085       1.98  christos static int
   1086       1.98  christos addrconfig(uint64_t *mask)
   1087       1.98  christos {
   1088       1.98  christos 	struct ifaddrs *ifaddrs, *ifa;
   1089       1.98  christos 
   1090       1.98  christos 	if (getifaddrs(&ifaddrs) == -1)
   1091       1.98  christos 		return -1;
   1092       1.98  christos 
   1093       1.99    kardel 	*mask = 0;
   1094       1.98  christos 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
   1095       1.98  christos 		if (ifa->ifa_addr && (ifa->ifa_flags & IFF_UP)) {
   1096       1.98  christos 			_DIAGASSERT(ifa->ifa_addr->sa_family < 64);
   1097       1.98  christos 			*mask |= (uint64_t)1 << ifa->ifa_addr->sa_family;
   1098       1.98  christos 		}
   1099       1.98  christos 
   1100       1.98  christos 	freeifaddrs(ifaddrs);
   1101       1.98  christos 	return 0;
   1102       1.98  christos }
   1103       1.98  christos 
   1104       1.27    itojun #ifdef INET6
   1105       1.27    itojun /* convert a string to a scope identifier. XXX: IPv6 specific */
   1106       1.60    itojun static int
   1107       1.70  christos ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
   1108       1.27    itojun {
   1109       1.59    itojun 	u_long lscopeid;
   1110       1.51     lukem 	struct in6_addr *a6;
   1111       1.27    itojun 	char *ep;
   1112       1.46    itojun 
   1113       1.51     lukem 	_DIAGASSERT(scope != NULL);
   1114       1.51     lukem 	_DIAGASSERT(sin6 != NULL);
   1115       1.60    itojun 	_DIAGASSERT(scopeid != NULL);
   1116       1.51     lukem 
   1117       1.51     lukem 	a6 = &sin6->sin6_addr;
   1118       1.51     lukem 
   1119       1.46    itojun 	/* empty scopeid portion is invalid */
   1120       1.46    itojun 	if (*scope == '\0')
   1121       1.46    itojun 		return -1;
   1122       1.27    itojun 
   1123       1.27    itojun 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
   1124       1.27    itojun 		/*
   1125       1.95       wiz 		 * We currently assume a one-to-one mapping between links
   1126       1.27    itojun 		 * and interfaces, so we simply use interface indices for
   1127       1.27    itojun 		 * like-local scopes.
   1128       1.27    itojun 		 */
   1129       1.60    itojun 		*scopeid = if_nametoindex(scope);
   1130       1.60    itojun 		if (*scopeid == 0)
   1131       1.27    itojun 			goto trynumeric;
   1132       1.60    itojun 		return 0;
   1133       1.27    itojun 	}
   1134       1.27    itojun 
   1135       1.27    itojun 	/* still unclear about literal, allow numeric only - placeholder */
   1136       1.27    itojun 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
   1137       1.27    itojun 		goto trynumeric;
   1138       1.27    itojun 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
   1139       1.27    itojun 		goto trynumeric;
   1140       1.27    itojun 	else
   1141       1.27    itojun 		goto trynumeric;	/* global */
   1142       1.27    itojun 
   1143       1.27    itojun 	/* try to convert to a numeric id as a last resort */
   1144       1.27    itojun   trynumeric:
   1145       1.59    itojun 	errno = 0;
   1146       1.59    itojun 	lscopeid = strtoul(scope, &ep, 10);
   1147       1.61    itojun 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
   1148       1.60    itojun 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
   1149       1.60    itojun 		return 0;
   1150       1.27    itojun 	else
   1151       1.27    itojun 		return -1;
   1152       1.27    itojun }
   1153       1.42    itojun #endif
   1154       1.32    itojun 
   1155       1.32    itojun /* code duplicate with gethnamaddr.c */
   1156       1.32    itojun 
   1157       1.32    itojun static const char AskedForGot[] =
   1158       1.32    itojun 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
   1159       1.32    itojun 
   1160  1.106.4.1  sborrill #define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
   1161  1.106.4.1  sborrill                                (ok)(nm) != 0)
   1162       1.32    itojun static struct addrinfo *
   1163  1.106.4.1  sborrill getanswer(res_state res, const querybuf *answer, int anslen, const char *qname,
   1164  1.106.4.1  sborrill     int qtype, const struct addrinfo *pai)
   1165       1.32    itojun {
   1166       1.32    itojun 	struct addrinfo sentinel, *cur;
   1167      1.103  christos 	struct addrinfo ai, *aip;
   1168       1.32    itojun 	const struct afd *afd;
   1169       1.32    itojun 	char *canonname;
   1170       1.32    itojun 	const HEADER *hp;
   1171       1.32    itojun 	const u_char *cp;
   1172       1.32    itojun 	int n;
   1173       1.32    itojun 	const u_char *eom;
   1174       1.56    itojun 	char *bp, *ep;
   1175       1.56    itojun 	int type, class, ancount, qdcount;
   1176       1.32    itojun 	int haveanswer, had_error;
   1177       1.32    itojun 	char tbuf[MAXDNAME];
   1178       1.70  christos 	int (*name_ok) (const char *);
   1179       1.32    itojun 	char hostbuf[8*1024];
   1180      1.103  christos 	int port, pri, weight;
   1181      1.103  christos 	struct srvinfo *srvlist, *srv, *csrv;
   1182       1.32    itojun 
   1183       1.51     lukem 	_DIAGASSERT(answer != NULL);
   1184       1.51     lukem 	_DIAGASSERT(qname != NULL);
   1185       1.51     lukem 	_DIAGASSERT(pai != NULL);
   1186  1.106.4.1  sborrill 	_DIAGASSERT(res != NULL);
   1187       1.51     lukem 
   1188       1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1189       1.32    itojun 	cur = &sentinel;
   1190       1.32    itojun 
   1191       1.32    itojun 	canonname = NULL;
   1192       1.32    itojun 	eom = answer->buf + anslen;
   1193       1.32    itojun 	switch (qtype) {
   1194       1.32    itojun 	case T_A:
   1195       1.32    itojun 	case T_AAAA:
   1196       1.32    itojun 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
   1197       1.32    itojun 		name_ok = res_hnok;
   1198       1.32    itojun 		break;
   1199      1.103  christos 	case T_SRV:
   1200      1.103  christos 		name_ok = gai_srvok;
   1201      1.103  christos 		break;
   1202       1.32    itojun 	default:
   1203       1.70  christos 		return NULL;	/* XXX should be abort(); */
   1204       1.32    itojun 	}
   1205       1.32    itojun 	/*
   1206       1.32    itojun 	 * find first satisfactory answer
   1207       1.32    itojun 	 */
   1208       1.32    itojun 	hp = &answer->hdr;
   1209       1.32    itojun 	ancount = ntohs(hp->ancount);
   1210       1.32    itojun 	qdcount = ntohs(hp->qdcount);
   1211       1.32    itojun 	bp = hostbuf;
   1212       1.56    itojun 	ep = hostbuf + sizeof hostbuf;
   1213       1.32    itojun 	cp = answer->buf + HFIXEDSZ;
   1214       1.32    itojun 	if (qdcount != 1) {
   1215       1.32    itojun 		h_errno = NO_RECOVERY;
   1216       1.32    itojun 		return (NULL);
   1217       1.32    itojun 	}
   1218       1.97  christos 	n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
   1219  1.106.4.1  sborrill 	if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
   1220       1.32    itojun 		h_errno = NO_RECOVERY;
   1221       1.32    itojun 		return (NULL);
   1222       1.32    itojun 	}
   1223       1.32    itojun 	cp += n + QFIXEDSZ;
   1224       1.32    itojun 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
   1225       1.32    itojun 		/* res_send() has already verified that the query name is the
   1226       1.32    itojun 		 * same as the one we sent; this just gets the expanded name
   1227       1.32    itojun 		 * (i.e., with the succeeding search-domain tacked on).
   1228       1.32    itojun 		 */
   1229       1.97  christos 		n = (int)strlen(bp) + 1;		/* for the \0 */
   1230       1.32    itojun 		if (n >= MAXHOSTNAMELEN) {
   1231       1.32    itojun 			h_errno = NO_RECOVERY;
   1232       1.32    itojun 			return (NULL);
   1233       1.32    itojun 		}
   1234       1.32    itojun 		canonname = bp;
   1235       1.32    itojun 		bp += n;
   1236       1.32    itojun 		/* The qname can be abbreviated, but h_name is now absolute. */
   1237       1.32    itojun 		qname = canonname;
   1238       1.32    itojun 	}
   1239       1.32    itojun 	haveanswer = 0;
   1240       1.32    itojun 	had_error = 0;
   1241      1.103  christos 	srvlist = NULL;
   1242       1.32    itojun 	while (ancount-- > 0 && cp < eom && !had_error) {
   1243       1.97  christos 		n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
   1244  1.106.4.1  sborrill 		if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
   1245       1.32    itojun 			had_error++;
   1246       1.32    itojun 			continue;
   1247       1.32    itojun 		}
   1248       1.32    itojun 		cp += n;			/* name */
   1249       1.32    itojun 		type = _getshort(cp);
   1250      1.102  christos 		cp += INT16SZ;			/* type */
   1251       1.32    itojun 		class = _getshort(cp);
   1252      1.102  christos 		cp += INT16SZ + INT32SZ;	/* class, TTL */
   1253       1.32    itojun 		n = _getshort(cp);
   1254       1.32    itojun 		cp += INT16SZ;			/* len */
   1255       1.32    itojun 		if (class != C_IN) {
   1256       1.32    itojun 			/* XXX - debug? syslog? */
   1257       1.32    itojun 			cp += n;
   1258       1.32    itojun 			continue;		/* XXX - had_error++ ? */
   1259       1.32    itojun 		}
   1260       1.32    itojun 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
   1261       1.32    itojun 		    type == T_CNAME) {
   1262       1.97  christos 			n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
   1263  1.106.4.1  sborrill 			if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
   1264       1.32    itojun 				had_error++;
   1265       1.32    itojun 				continue;
   1266       1.32    itojun 			}
   1267       1.32    itojun 			cp += n;
   1268       1.32    itojun 			/* Get canonical name. */
   1269       1.97  christos 			n = (int)strlen(tbuf) + 1;	/* for the \0 */
   1270       1.56    itojun 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
   1271       1.32    itojun 				had_error++;
   1272       1.32    itojun 				continue;
   1273       1.32    itojun 			}
   1274       1.66    itojun 			strlcpy(bp, tbuf, (size_t)(ep - bp));
   1275       1.32    itojun 			canonname = bp;
   1276       1.32    itojun 			bp += n;
   1277       1.32    itojun 			continue;
   1278       1.32    itojun 		}
   1279       1.32    itojun 		if (qtype == T_ANY) {
   1280       1.32    itojun 			if (!(type == T_A || type == T_AAAA)) {
   1281       1.32    itojun 				cp += n;
   1282       1.32    itojun 				continue;
   1283       1.32    itojun 			}
   1284       1.32    itojun 		} else if (type != qtype) {
   1285       1.88  christos 			if (type != T_KEY && type != T_SIG) {
   1286       1.88  christos 				struct syslog_data sd = SYSLOG_DATA_INIT;
   1287       1.88  christos 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
   1288       1.32    itojun 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
   1289       1.32    itojun 				       qname, p_class(C_IN), p_type(qtype),
   1290       1.32    itojun 				       p_type(type));
   1291       1.88  christos 			}
   1292       1.32    itojun 			cp += n;
   1293       1.32    itojun 			continue;		/* XXX - had_error++ ? */
   1294       1.32    itojun 		}
   1295       1.32    itojun 		switch (type) {
   1296       1.32    itojun 		case T_A:
   1297       1.32    itojun 		case T_AAAA:
   1298       1.32    itojun 			if (strcasecmp(canonname, bp) != 0) {
   1299       1.88  christos 				struct syslog_data sd = SYSLOG_DATA_INIT;
   1300       1.88  christos 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
   1301       1.32    itojun 				       AskedForGot, canonname, bp);
   1302       1.32    itojun 				cp += n;
   1303       1.32    itojun 				continue;	/* XXX - had_error++ ? */
   1304       1.32    itojun 			}
   1305       1.32    itojun 			if (type == T_A && n != INADDRSZ) {
   1306       1.32    itojun 				cp += n;
   1307       1.32    itojun 				continue;
   1308       1.32    itojun 			}
   1309       1.32    itojun 			if (type == T_AAAA && n != IN6ADDRSZ) {
   1310       1.32    itojun 				cp += n;
   1311       1.32    itojun 				continue;
   1312       1.62    itojun 			}
   1313       1.62    itojun 			if (type == T_AAAA) {
   1314       1.62    itojun 				struct in6_addr in6;
   1315       1.62    itojun 				memcpy(&in6, cp, IN6ADDRSZ);
   1316       1.62    itojun 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
   1317       1.62    itojun 					cp += n;
   1318       1.62    itojun 					continue;
   1319       1.62    itojun 				}
   1320       1.32    itojun 			}
   1321       1.32    itojun 			if (!haveanswer) {
   1322       1.32    itojun 				int nn;
   1323       1.32    itojun 
   1324       1.32    itojun 				canonname = bp;
   1325       1.97  christos 				nn = (int)strlen(bp) + 1;	/* for the \0 */
   1326       1.32    itojun 				bp += nn;
   1327       1.32    itojun 			}
   1328       1.32    itojun 
   1329       1.32    itojun 			/* don't overwrite pai */
   1330       1.32    itojun 			ai = *pai;
   1331       1.32    itojun 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
   1332       1.32    itojun 			afd = find_afd(ai.ai_family);
   1333       1.32    itojun 			if (afd == NULL) {
   1334       1.32    itojun 				cp += n;
   1335       1.32    itojun 				continue;
   1336       1.32    itojun 			}
   1337       1.36  christos 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
   1338       1.32    itojun 			if (cur->ai_next == NULL)
   1339       1.32    itojun 				had_error++;
   1340       1.32    itojun 			while (cur && cur->ai_next)
   1341       1.32    itojun 				cur = cur->ai_next;
   1342       1.32    itojun 			cp += n;
   1343       1.32    itojun 			break;
   1344      1.103  christos 		case T_SRV:
   1345      1.103  christos 			/* Add to SRV list. Insertion sort on priority. */
   1346      1.103  christos 			pri = _getshort(cp);
   1347      1.103  christos 			cp += INT16SZ;
   1348      1.103  christos 			weight = _getshort(cp);
   1349      1.103  christos 			cp += INT16SZ;
   1350      1.103  christos 			port = _getshort(cp);
   1351      1.103  christos 			cp += INT16SZ;
   1352      1.103  christos 			n = dn_expand(answer->buf, eom, cp, tbuf,
   1353      1.103  christos 			    (int)sizeof(tbuf));
   1354  1.106.4.1  sborrill 			if ((n < 0) || !maybe_ok(res, tbuf, res_hnok)) {
   1355      1.103  christos 				had_error++;
   1356      1.103  christos 				continue;
   1357      1.103  christos 			}
   1358      1.103  christos 			cp += n;
   1359      1.103  christos 			if (strlen(tbuf) + 1 >= MAXDNAME) {
   1360      1.103  christos 				had_error++;
   1361      1.103  christos 				continue;
   1362      1.103  christos 			}
   1363      1.103  christos 			srv = malloc(sizeof(*srv));
   1364      1.103  christos 			if (!srv) {
   1365      1.103  christos 				had_error++;
   1366      1.103  christos 				continue;
   1367      1.103  christos 			}
   1368      1.103  christos 			strlcpy(srv->name, tbuf, sizeof(srv->name));
   1369      1.103  christos 			srv->pri = pri;
   1370      1.103  christos 			srv->weight = weight;
   1371      1.103  christos 			srv->port = port;
   1372      1.103  christos 			/* Weight 0 is sorted before other weights. */
   1373      1.103  christos 			if (!srvlist
   1374      1.103  christos 			    || srv->pri < srvlist->pri
   1375      1.103  christos 			    || (srv->pri == srvlist->pri &&
   1376      1.103  christos 			    (!srv->weight || srvlist->weight))) {
   1377      1.103  christos 				srv->next = srvlist;
   1378      1.103  christos 				srvlist = srv;
   1379      1.103  christos 			} else {
   1380      1.103  christos 				for (csrv = srvlist;
   1381      1.103  christos 				    csrv->next && csrv->next->pri <= srv->pri;
   1382      1.103  christos 				    csrv = csrv->next) {
   1383      1.103  christos 					if (csrv->next->pri == srv->pri
   1384      1.103  christos 					    && (!srv->weight ||
   1385      1.103  christos 					    csrv->next->weight))
   1386      1.103  christos 						break;
   1387      1.103  christos 				}
   1388      1.103  christos 				srv->next = csrv->next;
   1389      1.103  christos 				csrv->next = srv;
   1390      1.103  christos 			}
   1391      1.103  christos 			continue; /* Don't add to haveanswer yet. */
   1392       1.32    itojun 		default:
   1393       1.32    itojun 			abort();
   1394       1.32    itojun 		}
   1395       1.32    itojun 		if (!had_error)
   1396       1.32    itojun 			haveanswer++;
   1397       1.32    itojun 	}
   1398      1.103  christos 
   1399      1.103  christos 	if (srvlist) {
   1400      1.103  christos 		/*
   1401      1.103  christos 		 * Check for explicit rejection.
   1402      1.103  christos 		 */
   1403      1.103  christos 		if (!srvlist->next && !srvlist->name[0]) {
   1404      1.103  christos 			free(srvlist);
   1405      1.103  christos 			h_errno = HOST_NOT_FOUND;
   1406      1.103  christos 			return NULL;
   1407      1.103  christos 		}
   1408      1.103  christos 
   1409      1.103  christos 		while (srvlist) {
   1410      1.103  christos 			struct res_target q, q2;
   1411      1.103  christos 
   1412      1.103  christos 			srv = srvlist;
   1413      1.103  christos 			srvlist = srvlist->next;
   1414      1.103  christos 
   1415      1.103  christos 			/*
   1416      1.103  christos 			 * Since res_* doesn't give the additional
   1417      1.103  christos 			 * section, we always look up.
   1418      1.103  christos 			 */
   1419      1.103  christos 			memset(&q, 0, sizeof(q));
   1420      1.103  christos 			memset(&q2, 0, sizeof(q2));
   1421      1.103  christos 
   1422      1.103  christos 			q.name = srv->name;
   1423      1.103  christos 			q.qclass = C_IN;
   1424      1.103  christos 			q.qtype = T_AAAA;
   1425      1.103  christos 			q.next = &q2;
   1426      1.103  christos 			q2.name = srv->name;
   1427      1.103  christos 			q2.qclass = C_IN;
   1428      1.103  christos 			q2.qtype = T_A;
   1429      1.103  christos 
   1430      1.103  christos 			aip = _dns_query(&q, pai, res, 0);
   1431      1.103  christos 
   1432      1.103  christos 			if (aip != NULL) {
   1433      1.103  christos 				cur->ai_next = aip;
   1434      1.103  christos 				while (cur && cur->ai_next) {
   1435      1.103  christos 					cur = cur->ai_next;
   1436      1.103  christos 					*getport(cur) = htons(srv->port);
   1437      1.103  christos 					haveanswer++;
   1438      1.103  christos 				}
   1439      1.103  christos 			}
   1440      1.103  christos 			free(srv);
   1441      1.103  christos 		}
   1442      1.103  christos 	}
   1443       1.32    itojun 	if (haveanswer) {
   1444      1.103  christos 		if (!sentinel.ai_next->ai_canonname)
   1445      1.103  christos 		       (void)get_canonname(pai, sentinel.ai_next,
   1446      1.103  christos 			   canonname ? canonname : qname);
   1447       1.32    itojun 		h_errno = NETDB_SUCCESS;
   1448       1.32    itojun 		return sentinel.ai_next;
   1449       1.32    itojun 	}
   1450       1.32    itojun 
   1451       1.32    itojun 	h_errno = NO_RECOVERY;
   1452       1.32    itojun 	return NULL;
   1453       1.32    itojun }
   1454       1.32    itojun 
   1455       1.73    tsarna #define SORTEDADDR(p)	(((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
   1456       1.73    tsarna #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
   1457       1.73    tsarna 
   1458       1.73    tsarna static void
   1459       1.73    tsarna aisort(struct addrinfo *s, res_state res)
   1460       1.73    tsarna {
   1461       1.73    tsarna 	struct addrinfo head, *t, *p;
   1462       1.73    tsarna 	int i;
   1463       1.73    tsarna 
   1464       1.73    tsarna 	head.ai_next = NULL;
   1465       1.73    tsarna 	t = &head;
   1466       1.73    tsarna 
   1467       1.73    tsarna 	for (i = 0; i < res->nsort; i++) {
   1468       1.73    tsarna 		p = s;
   1469       1.73    tsarna 		while (p->ai_next) {
   1470       1.73    tsarna 			if ((p->ai_next->ai_family != AF_INET)
   1471       1.73    tsarna 			|| SORTMATCH(p, res->sort_list[i])) {
   1472       1.73    tsarna 				t->ai_next = p->ai_next;
   1473       1.73    tsarna 				t = t->ai_next;
   1474       1.73    tsarna 				p->ai_next = p->ai_next->ai_next;
   1475       1.73    tsarna 			} else {
   1476       1.73    tsarna 				p = p->ai_next;
   1477       1.73    tsarna 			}
   1478       1.73    tsarna 		}
   1479       1.73    tsarna 	}
   1480       1.73    tsarna 
   1481       1.73    tsarna 	/* add rest of list and reset s to the new list*/
   1482       1.73    tsarna 	t->ai_next = s->ai_next;
   1483       1.73    tsarna 	s->ai_next = head.ai_next;
   1484       1.73    tsarna }
   1485       1.73    tsarna 
   1486      1.103  christos static struct addrinfo *
   1487      1.103  christos _dns_query(struct res_target *q, const struct addrinfo *pai,
   1488      1.103  christos     res_state res, int dosearch)
   1489      1.103  christos {
   1490      1.103  christos 	struct res_target *q2 = q->next;
   1491      1.103  christos  	querybuf *buf, *buf2;
   1492      1.103  christos 	struct addrinfo sentinel, *cur, *ai;
   1493      1.103  christos 
   1494      1.103  christos #ifdef DNS_DEBUG
   1495      1.103  christos 	struct res_target *iter;
   1496      1.103  christos 	for (iter = q; iter; iter = iter->next)
   1497      1.103  christos 		printf("Query type %d for %s\n", iter->qtype, iter->name);
   1498      1.103  christos #endif
   1499      1.103  christos 
   1500      1.103  christos  	buf = malloc(sizeof(*buf));
   1501      1.103  christos  	if (buf == NULL) {
   1502      1.103  christos  		h_errno = NETDB_INTERNAL;
   1503      1.103  christos 		return NULL;
   1504      1.103  christos  	}
   1505      1.103  christos  	buf2 = malloc(sizeof(*buf2));
   1506      1.103  christos  	if (buf2 == NULL) {
   1507      1.103  christos  		free(buf);
   1508      1.103  christos  		h_errno = NETDB_INTERNAL;
   1509      1.103  christos 		return NULL;
   1510      1.103  christos 	}
   1511      1.103  christos 
   1512      1.103  christos 	memset(&sentinel, 0, sizeof(sentinel));
   1513      1.103  christos 	cur = &sentinel;
   1514      1.103  christos 
   1515      1.103  christos 	q->answer = buf->buf;
   1516      1.103  christos 	q->anslen = sizeof(buf->buf);
   1517      1.103  christos 	if (q2) {
   1518      1.103  christos 		q2->answer = buf2->buf;
   1519      1.103  christos 		q2->anslen = sizeof(buf2->buf);
   1520      1.103  christos 	}
   1521      1.103  christos 
   1522      1.103  christos 	if (dosearch) {
   1523      1.103  christos 		if (res_searchN(q->name, q, res) < 0)
   1524      1.103  christos 			goto out;
   1525      1.103  christos 	} else {
   1526      1.103  christos 		if (res_queryN(q->name, q, res) < 0)
   1527      1.103  christos 			goto out;
   1528      1.103  christos 	}
   1529      1.103  christos 
   1530  1.106.4.1  sborrill 	ai = getanswer(res, buf, q->n, q->name, q->qtype, pai);
   1531      1.103  christos 	if (ai) {
   1532      1.103  christos 		cur->ai_next = ai;
   1533      1.103  christos 		while (cur && cur->ai_next)
   1534      1.103  christos 			cur = cur->ai_next;
   1535      1.103  christos 	}
   1536      1.103  christos 	if (q2) {
   1537  1.106.4.1  sborrill 		ai = getanswer(res, buf2, q2->n, q2->name, q2->qtype, pai);
   1538      1.103  christos 		if (ai)
   1539      1.103  christos 			cur->ai_next = ai;
   1540      1.103  christos  	}
   1541      1.103  christos 	free(buf);
   1542      1.103  christos 	free(buf2);
   1543      1.103  christos 	return sentinel.ai_next;
   1544      1.103  christos out:
   1545      1.103  christos 	free(buf);
   1546      1.103  christos 	free(buf2);
   1547      1.103  christos 	return NULL;
   1548      1.103  christos }
   1549      1.103  christos 
   1550       1.32    itojun /*ARGSUSED*/
   1551      1.103  christos static struct addrinfo *
   1552      1.103  christos _dns_srv_lookup(const char *name, const char *servname,
   1553      1.103  christos     const struct addrinfo *pai)
   1554       1.32    itojun {
   1555      1.103  christos 	static const char * const srvprotos[] = { "tcp", "udp" };
   1556      1.103  christos 	static const int srvnottype[] = { SOCK_DGRAM, SOCK_STREAM };
   1557      1.103  christos 	static const int nsrvprotos = 2;
   1558      1.103  christos 	struct addrinfo sentinel, *cur, *ai;
   1559      1.103  christos 	struct servent *serv, sv;
   1560      1.103  christos 	struct servent_data svd;
   1561      1.103  christos 	struct res_target q;
   1562       1.73    tsarna 	res_state res;
   1563      1.103  christos 	char *tname;
   1564      1.103  christos 	int i;
   1565       1.32    itojun 
   1566      1.103  christos 	res = __res_get_state();
   1567      1.103  christos 	if (res == NULL)
   1568      1.103  christos 		return NULL;
   1569       1.32    itojun 
   1570      1.103  christos 	memset(&svd, 0, sizeof(svd));
   1571       1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1572       1.32    itojun 	cur = &sentinel;
   1573       1.32    itojun 
   1574      1.103  christos 	/*
   1575      1.103  christos 	 * Iterate over supported SRV protocols.
   1576      1.103  christos 	 * (currently UDP and TCP only)
   1577      1.103  christos 	 */
   1578      1.103  christos 	for (i = 0; i < nsrvprotos; i++) {
   1579      1.103  christos 		/*
   1580      1.103  christos 		 * Check that the caller didn't specify a hint
   1581      1.103  christos 		 * which precludes this protocol.
   1582      1.103  christos 		 */
   1583      1.103  christos 		if (pai->ai_socktype == srvnottype[i])
   1584      1.103  christos 			continue;
   1585      1.103  christos 		/*
   1586      1.103  christos 		 * If the caller specified a port,
   1587      1.103  christos 		 * then lookup the database for the
   1588      1.103  christos 		 * official service name.
   1589      1.103  christos 		 */
   1590      1.103  christos 		serv = getservbyname_r(servname, srvprotos[i], &sv, &svd);
   1591      1.103  christos 		if (serv == NULL)
   1592      1.103  christos 			continue;
   1593      1.103  christos 
   1594      1.103  christos 		/*
   1595      1.103  christos 		 * Construct service DNS name.
   1596      1.103  christos 		 */
   1597      1.103  christos 		if (asprintf(&tname, "_%s._%s.%s", serv->s_name, serv->s_proto,
   1598      1.103  christos 		    name) < 0)
   1599      1.103  christos 			continue;
   1600      1.103  christos 
   1601      1.103  christos 		memset(&q, 0, sizeof(q));
   1602      1.103  christos 		q.name = tname;
   1603      1.103  christos 		q.qclass = C_IN;
   1604      1.103  christos 		q.qtype = T_SRV;
   1605      1.103  christos 
   1606      1.103  christos 		/*
   1607      1.103  christos 		 * Do SRV query.
   1608      1.103  christos 		 */
   1609      1.103  christos 		ai = _dns_query(&q, pai, res, 1);
   1610      1.103  christos 		if (ai) {
   1611      1.103  christos 			cur->ai_next = ai;
   1612      1.103  christos 			while (cur && cur->ai_next)
   1613      1.103  christos 				cur = cur->ai_next;
   1614      1.103  christos 		}
   1615      1.103  christos 		free(tname);
   1616       1.63    itojun 	}
   1617      1.103  christos 
   1618      1.103  christos 	if (res->nsort)
   1619      1.103  christos 		aisort(&sentinel, res);
   1620      1.103  christos 
   1621      1.103  christos 	__res_put_state(res);
   1622      1.103  christos 
   1623      1.103  christos 	return sentinel.ai_next;
   1624      1.103  christos }
   1625      1.103  christos 
   1626      1.103  christos /*ARGSUSED*/
   1627      1.103  christos static struct addrinfo *
   1628      1.103  christos _dns_host_lookup(const char *name, const struct addrinfo *pai)
   1629      1.103  christos {
   1630      1.103  christos 	struct res_target q, q2;
   1631      1.103  christos 	struct addrinfo sentinel, *ai;
   1632      1.103  christos 	res_state res;
   1633      1.103  christos 
   1634      1.103  christos 	res = __res_get_state();
   1635      1.103  christos 	if (res == NULL)
   1636      1.103  christos 		return NULL;
   1637      1.103  christos 
   1638      1.103  christos 	memset(&q, 0, sizeof(q2));
   1639      1.103  christos 	memset(&q2, 0, sizeof(q2));
   1640       1.63    itojun 
   1641       1.32    itojun 	switch (pai->ai_family) {
   1642       1.32    itojun 	case AF_UNSPEC:
   1643       1.32    itojun 		/* prefer IPv6 */
   1644       1.52    itojun 		q.name = name;
   1645       1.43    itojun 		q.qclass = C_IN;
   1646       1.43    itojun 		q.qtype = T_AAAA;
   1647       1.32    itojun 		q.next = &q2;
   1648       1.52    itojun 		q2.name = name;
   1649       1.43    itojun 		q2.qclass = C_IN;
   1650       1.43    itojun 		q2.qtype = T_A;
   1651       1.32    itojun 		break;
   1652       1.32    itojun 	case AF_INET:
   1653       1.52    itojun 		q.name = name;
   1654       1.43    itojun 		q.qclass = C_IN;
   1655       1.43    itojun 		q.qtype = T_A;
   1656       1.32    itojun 		break;
   1657       1.32    itojun 	case AF_INET6:
   1658       1.52    itojun 		q.name = name;
   1659       1.43    itojun 		q.qclass = C_IN;
   1660       1.43    itojun 		q.qtype = T_AAAA;
   1661       1.32    itojun 		break;
   1662       1.32    itojun 	default:
   1663      1.105  christos 		__res_put_state(res);
   1664      1.103  christos 		h_errno = NETDB_INTERNAL;
   1665      1.103  christos 		return NULL;
   1666       1.32    itojun 	}
   1667       1.73    tsarna 
   1668      1.103  christos 	ai = _dns_query(&q, pai, res, 1);
   1669       1.73    tsarna 
   1670      1.103  christos 	memset(&sentinel, 0, sizeof(sentinel));
   1671      1.103  christos 	sentinel.ai_next = ai;
   1672       1.73    tsarna 
   1673      1.103  christos 	if (ai != NULL && res->nsort)
   1674       1.73    tsarna 		aisort(&sentinel, res);
   1675       1.73    tsarna 
   1676       1.73    tsarna 	__res_put_state(res);
   1677       1.73    tsarna 
   1678      1.103  christos 	return sentinel.ai_next;
   1679      1.103  christos }
   1680      1.103  christos 
   1681      1.103  christos /*ARGSUSED*/
   1682      1.103  christos static int
   1683      1.103  christos _dns_getaddrinfo(void *rv, void *cb_data, va_list ap)
   1684      1.103  christos {
   1685      1.103  christos 	struct addrinfo *ai = NULL;
   1686      1.103  christos 	const char *name, *servname;
   1687      1.103  christos 	const struct addrinfo *pai;
   1688      1.103  christos 
   1689      1.103  christos 	name = va_arg(ap, char *);
   1690      1.103  christos 	pai = va_arg(ap, const struct addrinfo *);
   1691      1.103  christos 	servname = va_arg(ap, char *);
   1692      1.103  christos 
   1693      1.103  christos 	/*
   1694      1.103  christos 	 * Try doing SRV lookup on service first.
   1695      1.103  christos 	 */
   1696      1.103  christos 	if (servname
   1697      1.103  christos #ifdef AI_SRV
   1698      1.103  christos 	    && (pai->ai_flags & AI_SRV)
   1699      1.103  christos #endif
   1700      1.103  christos 	    && !(pai->ai_flags & AI_NUMERICSERV)
   1701      1.103  christos 	    && str2number(servname) == -1) {
   1702      1.103  christos 
   1703      1.103  christos #ifdef DNS_DEBUG
   1704      1.103  christos 		printf("%s: try SRV lookup\n", __func__);
   1705      1.103  christos #endif
   1706      1.103  christos 		ai = _dns_srv_lookup(name, servname, pai);
   1707      1.103  christos 	}
   1708      1.103  christos 
   1709      1.103  christos 	/*
   1710      1.103  christos 	 * Do lookup on name.
   1711      1.103  christos 	 */
   1712      1.103  christos 	if (ai == NULL) {
   1713      1.103  christos 
   1714      1.103  christos #ifdef DNS_DEBUG
   1715      1.103  christos 		printf("%s: try HOST lookup\n", __func__);
   1716      1.103  christos #endif
   1717      1.103  christos 		ai = _dns_host_lookup(name, pai);
   1718      1.103  christos 
   1719      1.103  christos 		if (ai == NULL) {
   1720      1.103  christos 			switch (h_errno) {
   1721      1.103  christos 			case HOST_NOT_FOUND:
   1722      1.103  christos 				return NS_NOTFOUND;
   1723      1.103  christos 			case TRY_AGAIN:
   1724      1.103  christos 				return NS_TRYAGAIN;
   1725      1.103  christos 			default:
   1726      1.103  christos 				return NS_UNAVAIL;
   1727      1.103  christos 			}
   1728      1.103  christos 		}
   1729      1.103  christos 	}
   1730      1.103  christos 
   1731      1.103  christos 	*((struct addrinfo **)rv) = ai;
   1732       1.32    itojun 	return NS_SUCCESS;
   1733       1.32    itojun }
   1734       1.32    itojun 
   1735       1.32    itojun static void
   1736       1.70  christos _sethtent(FILE **hostf)
   1737       1.32    itojun {
   1738       1.51     lukem 
   1739       1.70  christos 	if (!*hostf)
   1740       1.96  christos 		*hostf = fopen(_PATH_HOSTS, "re");
   1741       1.32    itojun 	else
   1742       1.70  christos 		rewind(*hostf);
   1743       1.32    itojun }
   1744       1.32    itojun 
   1745       1.32    itojun static void
   1746       1.70  christos _endhtent(FILE **hostf)
   1747       1.32    itojun {
   1748       1.51     lukem 
   1749       1.70  christos 	if (*hostf) {
   1750       1.70  christos 		(void) fclose(*hostf);
   1751       1.70  christos 		*hostf = NULL;
   1752       1.32    itojun 	}
   1753       1.32    itojun }
   1754       1.32    itojun 
   1755       1.32    itojun static struct addrinfo *
   1756       1.70  christos _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
   1757       1.32    itojun {
   1758       1.32    itojun 	char *p;
   1759       1.48    itojun 	char *cp, *tname, *cname;
   1760       1.32    itojun 	struct addrinfo hints, *res0, *res;
   1761       1.32    itojun 	int error;
   1762       1.32    itojun 	const char *addr;
   1763       1.32    itojun 	char hostbuf[8*1024];
   1764       1.32    itojun 
   1765       1.51     lukem 	_DIAGASSERT(name != NULL);
   1766       1.51     lukem 	_DIAGASSERT(pai != NULL);
   1767       1.51     lukem 
   1768       1.96  christos 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
   1769       1.32    itojun 		return (NULL);
   1770       1.32    itojun  again:
   1771       1.97  christos 	if (!(p = fgets(hostbuf, (int)sizeof hostbuf, *hostf)))
   1772       1.32    itojun 		return (NULL);
   1773       1.32    itojun 	if (*p == '#')
   1774       1.32    itojun 		goto again;
   1775       1.32    itojun 	if (!(cp = strpbrk(p, "#\n")))
   1776       1.32    itojun 		goto again;
   1777       1.32    itojun 	*cp = '\0';
   1778       1.32    itojun 	if (!(cp = strpbrk(p, " \t")))
   1779       1.32    itojun 		goto again;
   1780       1.32    itojun 	*cp++ = '\0';
   1781       1.32    itojun 	addr = p;
   1782       1.32    itojun 	/* if this is not something we're looking for, skip it. */
   1783       1.48    itojun 	cname = NULL;
   1784       1.32    itojun 	while (cp && *cp) {
   1785       1.32    itojun 		if (*cp == ' ' || *cp == '\t') {
   1786       1.32    itojun 			cp++;
   1787       1.32    itojun 			continue;
   1788       1.32    itojun 		}
   1789       1.48    itojun 		if (!cname)
   1790       1.48    itojun 			cname = cp;
   1791       1.32    itojun 		tname = cp;
   1792       1.32    itojun 		if ((cp = strpbrk(cp, " \t")) != NULL)
   1793       1.32    itojun 			*cp++ = '\0';
   1794       1.32    itojun 		if (strcasecmp(name, tname) == 0)
   1795       1.32    itojun 			goto found;
   1796       1.32    itojun 	}
   1797       1.32    itojun 	goto again;
   1798       1.32    itojun 
   1799       1.32    itojun found:
   1800       1.32    itojun 	hints = *pai;
   1801       1.32    itojun 	hints.ai_flags = AI_NUMERICHOST;
   1802       1.32    itojun 	error = getaddrinfo(addr, NULL, &hints, &res0);
   1803       1.32    itojun 	if (error)
   1804       1.32    itojun 		goto again;
   1805       1.32    itojun 	for (res = res0; res; res = res->ai_next) {
   1806       1.32    itojun 		/* cover it up */
   1807       1.32    itojun 		res->ai_flags = pai->ai_flags;
   1808       1.32    itojun 
   1809       1.32    itojun 		if (pai->ai_flags & AI_CANONNAME) {
   1810       1.48    itojun 			if (get_canonname(pai, res, cname) != 0) {
   1811       1.32    itojun 				freeaddrinfo(res0);
   1812       1.32    itojun 				goto again;
   1813       1.32    itojun 			}
   1814       1.32    itojun 		}
   1815       1.32    itojun 	}
   1816       1.32    itojun 	return res0;
   1817       1.32    itojun }
   1818       1.32    itojun 
   1819       1.32    itojun /*ARGSUSED*/
   1820       1.32    itojun static int
   1821       1.70  christos _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
   1822       1.32    itojun {
   1823       1.32    itojun 	const char *name;
   1824       1.32    itojun 	const struct addrinfo *pai;
   1825       1.32    itojun 	struct addrinfo sentinel, *cur;
   1826       1.32    itojun 	struct addrinfo *p;
   1827       1.70  christos #ifndef _REENTRANT
   1828       1.70  christos 	static
   1829       1.70  christos #endif
   1830       1.70  christos 	FILE *hostf = NULL;
   1831       1.32    itojun 
   1832       1.32    itojun 	name = va_arg(ap, char *);
   1833       1.91     lukem 	pai = va_arg(ap, const struct addrinfo *);
   1834       1.32    itojun 
   1835       1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1836       1.32    itojun 	cur = &sentinel;
   1837       1.32    itojun 
   1838       1.70  christos 	_sethtent(&hostf);
   1839       1.70  christos 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
   1840       1.32    itojun 		cur->ai_next = p;
   1841       1.32    itojun 		while (cur && cur->ai_next)
   1842       1.32    itojun 			cur = cur->ai_next;
   1843       1.32    itojun 	}
   1844       1.70  christos 	_endhtent(&hostf);
   1845       1.32    itojun 
   1846       1.32    itojun 	*((struct addrinfo **)rv) = sentinel.ai_next;
   1847       1.32    itojun 	if (sentinel.ai_next == NULL)
   1848       1.32    itojun 		return NS_NOTFOUND;
   1849       1.32    itojun 	return NS_SUCCESS;
   1850       1.32    itojun }
   1851       1.32    itojun 
   1852       1.32    itojun #ifdef YP
   1853       1.32    itojun /*ARGSUSED*/
   1854       1.32    itojun static struct addrinfo *
   1855       1.70  christos _yphostent(char *line, const struct addrinfo *pai)
   1856       1.32    itojun {
   1857       1.32    itojun 	struct addrinfo sentinel, *cur;
   1858       1.32    itojun 	struct addrinfo hints, *res, *res0;
   1859       1.32    itojun 	int error;
   1860       1.51     lukem 	char *p;
   1861       1.32    itojun 	const char *addr, *canonname;
   1862       1.32    itojun 	char *nextline;
   1863       1.32    itojun 	char *cp;
   1864       1.32    itojun 
   1865       1.51     lukem 	_DIAGASSERT(line != NULL);
   1866       1.51     lukem 	_DIAGASSERT(pai != NULL);
   1867       1.51     lukem 
   1868       1.51     lukem 	p = line;
   1869       1.32    itojun 	addr = canonname = NULL;
   1870       1.32    itojun 
   1871       1.36  christos 	memset(&sentinel, 0, sizeof(sentinel));
   1872       1.36  christos 	cur = &sentinel;
   1873       1.36  christos 
   1874       1.32    itojun nextline:
   1875       1.32    itojun 	/* terminate line */
   1876       1.32    itojun 	cp = strchr(p, '\n');
   1877       1.32    itojun 	if (cp) {
   1878       1.32    itojun 		*cp++ = '\0';
   1879       1.32    itojun 		nextline = cp;
   1880       1.32    itojun 	} else
   1881       1.32    itojun 		nextline = NULL;
   1882       1.32    itojun 
   1883       1.32    itojun 	cp = strpbrk(p, " \t");
   1884       1.32    itojun 	if (cp == NULL) {
   1885       1.32    itojun 		if (canonname == NULL)
   1886       1.32    itojun 			return (NULL);
   1887       1.32    itojun 		else
   1888       1.32    itojun 			goto done;
   1889       1.32    itojun 	}
   1890       1.32    itojun 	*cp++ = '\0';
   1891       1.32    itojun 
   1892       1.32    itojun 	addr = p;
   1893       1.32    itojun 
   1894       1.32    itojun 	while (cp && *cp) {
   1895       1.32    itojun 		if (*cp == ' ' || *cp == '\t') {
   1896       1.32    itojun 			cp++;
   1897       1.32    itojun 			continue;
   1898       1.32    itojun 		}
   1899       1.32    itojun 		if (!canonname)
   1900       1.32    itojun 			canonname = cp;
   1901       1.32    itojun 		if ((cp = strpbrk(cp, " \t")) != NULL)
   1902       1.32    itojun 			*cp++ = '\0';
   1903       1.32    itojun 	}
   1904       1.32    itojun 
   1905       1.32    itojun 	hints = *pai;
   1906       1.32    itojun 	hints.ai_flags = AI_NUMERICHOST;
   1907       1.32    itojun 	error = getaddrinfo(addr, NULL, &hints, &res0);
   1908       1.32    itojun 	if (error == 0) {
   1909       1.32    itojun 		for (res = res0; res; res = res->ai_next) {
   1910       1.32    itojun 			/* cover it up */
   1911       1.32    itojun 			res->ai_flags = pai->ai_flags;
   1912       1.32    itojun 
   1913       1.32    itojun 			if (pai->ai_flags & AI_CANONNAME)
   1914       1.32    itojun 				(void)get_canonname(pai, res, canonname);
   1915       1.32    itojun 		}
   1916       1.37    itojun 	} else
   1917       1.37    itojun 		res0 = NULL;
   1918       1.32    itojun 	if (res0) {
   1919       1.32    itojun 		cur->ai_next = res0;
   1920       1.80  christos 		while (cur->ai_next)
   1921       1.32    itojun 			cur = cur->ai_next;
   1922       1.32    itojun 	}
   1923       1.32    itojun 
   1924       1.32    itojun 	if (nextline) {
   1925       1.32    itojun 		p = nextline;
   1926       1.32    itojun 		goto nextline;
   1927       1.32    itojun 	}
   1928       1.32    itojun 
   1929       1.32    itojun done:
   1930       1.32    itojun 	return sentinel.ai_next;
   1931       1.32    itojun }
   1932       1.32    itojun 
   1933       1.32    itojun /*ARGSUSED*/
   1934       1.32    itojun static int
   1935       1.70  christos _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
   1936       1.32    itojun {
   1937       1.32    itojun 	struct addrinfo sentinel, *cur;
   1938       1.32    itojun 	struct addrinfo *ai = NULL;
   1939       1.72  christos 	char *ypbuf;
   1940       1.72  christos 	int ypbuflen, r;
   1941       1.32    itojun 	const char *name;
   1942       1.32    itojun 	const struct addrinfo *pai;
   1943       1.72  christos 	char *ypdomain;
   1944       1.72  christos 
   1945       1.72  christos 	if (_yp_check(&ypdomain) == 0)
   1946       1.72  christos 		return NS_UNAVAIL;
   1947       1.32    itojun 
   1948       1.32    itojun 	name = va_arg(ap, char *);
   1949       1.32    itojun 	pai = va_arg(ap, const struct addrinfo *);
   1950       1.32    itojun 
   1951       1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1952       1.32    itojun 	cur = &sentinel;
   1953       1.32    itojun 
   1954       1.32    itojun 	/* hosts.byname is only for IPv4 (Solaris8) */
   1955       1.33    itojun 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
   1956       1.72  christos 		r = yp_match(ypdomain, "hosts.byname", name,
   1957       1.72  christos 			(int)strlen(name), &ypbuf, &ypbuflen);
   1958       1.33    itojun 		if (r == 0) {
   1959       1.33    itojun 			struct addrinfo ai4;
   1960       1.33    itojun 
   1961       1.33    itojun 			ai4 = *pai;
   1962       1.33    itojun 			ai4.ai_family = AF_INET;
   1963       1.72  christos 			ai = _yphostent(ypbuf, &ai4);
   1964       1.33    itojun 			if (ai) {
   1965       1.33    itojun 				cur->ai_next = ai;
   1966       1.33    itojun 				while (cur && cur->ai_next)
   1967       1.33    itojun 					cur = cur->ai_next;
   1968       1.33    itojun 			}
   1969       1.32    itojun 		}
   1970       1.72  christos 		free(ypbuf);
   1971       1.32    itojun 	}
   1972       1.32    itojun 
   1973       1.32    itojun 	/* ipnodes.byname can hold both IPv4/v6 */
   1974       1.72  christos 	r = yp_match(ypdomain, "ipnodes.byname", name,
   1975       1.72  christos 		(int)strlen(name), &ypbuf, &ypbuflen);
   1976       1.32    itojun 	if (r == 0) {
   1977       1.72  christos 		ai = _yphostent(ypbuf, pai);
   1978       1.72  christos 		if (ai)
   1979       1.32    itojun 			cur->ai_next = ai;
   1980       1.72  christos 		free(ypbuf);
   1981       1.32    itojun 	}
   1982       1.32    itojun 
   1983       1.32    itojun 	if (sentinel.ai_next == NULL) {
   1984       1.32    itojun 		h_errno = HOST_NOT_FOUND;
   1985       1.32    itojun 		return NS_NOTFOUND;
   1986       1.32    itojun 	}
   1987       1.32    itojun 	*((struct addrinfo **)rv) = sentinel.ai_next;
   1988       1.32    itojun 	return NS_SUCCESS;
   1989       1.32    itojun }
   1990       1.32    itojun #endif
   1991       1.32    itojun 
   1992       1.32    itojun /* resolver logic */
   1993       1.32    itojun 
   1994       1.32    itojun /*
   1995       1.32    itojun  * Formulate a normal query, send, and await answer.
   1996       1.32    itojun  * Returned answer is placed in supplied buffer "answer".
   1997       1.32    itojun  * Perform preliminary check of answer, returning success only
   1998       1.32    itojun  * if no error is indicated and the answer count is nonzero.
   1999       1.32    itojun  * Return the size of the response on success, -1 on error.
   2000       1.32    itojun  * Error number is left in h_errno.
   2001       1.32    itojun  *
   2002       1.32    itojun  * Caller must parse answer and determine whether it answers the question.
   2003       1.32    itojun  */
   2004       1.32    itojun static int
   2005       1.70  christos res_queryN(const char *name, /* domain name */ struct res_target *target,
   2006       1.70  christos     res_state res)
   2007       1.32    itojun {
   2008       1.32    itojun 	u_char buf[MAXPACKET];
   2009       1.32    itojun 	HEADER *hp;
   2010       1.32    itojun 	int n;
   2011       1.32    itojun 	struct res_target *t;
   2012       1.32    itojun 	int rcode;
   2013       1.32    itojun 	int ancount;
   2014       1.32    itojun 
   2015       1.51     lukem 	_DIAGASSERT(name != NULL);
   2016       1.51     lukem 	/* XXX: target may be NULL??? */
   2017       1.51     lukem 
   2018       1.32    itojun 	rcode = NOERROR;
   2019       1.32    itojun 	ancount = 0;
   2020       1.32    itojun 
   2021       1.32    itojun 	for (t = target; t; t = t->next) {
   2022       1.32    itojun 		int class, type;
   2023       1.32    itojun 		u_char *answer;
   2024       1.32    itojun 		int anslen;
   2025       1.32    itojun 
   2026       1.32    itojun 		hp = (HEADER *)(void *)t->answer;
   2027       1.32    itojun 		hp->rcode = NOERROR;	/* default */
   2028       1.32    itojun 
   2029       1.32    itojun 		/* make it easier... */
   2030       1.43    itojun 		class = t->qclass;
   2031       1.43    itojun 		type = t->qtype;
   2032       1.32    itojun 		answer = t->answer;
   2033       1.32    itojun 		anslen = t->anslen;
   2034       1.32    itojun #ifdef DEBUG
   2035       1.70  christos 		if (res->options & RES_DEBUG)
   2036       1.70  christos 			printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
   2037       1.32    itojun #endif
   2038       1.32    itojun 
   2039       1.70  christos 		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
   2040       1.97  christos 		    buf, (int)sizeof(buf));
   2041       1.47    itojun #ifdef RES_USE_EDNS0
   2042       1.70  christos 		if (n > 0 && (res->options & RES_USE_EDNS0) != 0)
   2043       1.97  christos 			n = res_nopt(res, n, buf, (int)sizeof(buf), anslen);
   2044       1.47    itojun #endif
   2045       1.32    itojun 		if (n <= 0) {
   2046       1.32    itojun #ifdef DEBUG
   2047       1.70  christos 			if (res->options & RES_DEBUG)
   2048       1.70  christos 				printf(";; res_nquery: mkquery failed\n");
   2049       1.32    itojun #endif
   2050       1.32    itojun 			h_errno = NO_RECOVERY;
   2051       1.70  christos 			return n;
   2052       1.32    itojun 		}
   2053       1.70  christos 		n = res_nsend(res, buf, n, answer, anslen);
   2054       1.35    itojun #if 0
   2055       1.32    itojun 		if (n < 0) {
   2056       1.32    itojun #ifdef DEBUG
   2057       1.70  christos 			if (res->options & RES_DEBUG)
   2058       1.32    itojun 				printf(";; res_query: send error\n");
   2059       1.32    itojun #endif
   2060       1.32    itojun 			h_errno = TRY_AGAIN;
   2061       1.70  christos 			return n;
   2062       1.32    itojun 		}
   2063       1.35    itojun #endif
   2064       1.32    itojun 
   2065       1.35    itojun 		if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
   2066       1.32    itojun 			rcode = hp->rcode;	/* record most recent error */
   2067       1.32    itojun #ifdef DEBUG
   2068       1.70  christos 			if (res->options & RES_DEBUG)
   2069       1.57    itojun 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
   2070       1.32    itojun 				    ntohs(hp->ancount));
   2071       1.32    itojun #endif
   2072       1.32    itojun 			continue;
   2073       1.32    itojun 		}
   2074       1.32    itojun 
   2075       1.32    itojun 		ancount += ntohs(hp->ancount);
   2076       1.32    itojun 
   2077       1.32    itojun 		t->n = n;
   2078       1.32    itojun 	}
   2079       1.32    itojun 
   2080       1.32    itojun 	if (ancount == 0) {
   2081       1.32    itojun 		switch (rcode) {
   2082       1.32    itojun 		case NXDOMAIN:
   2083       1.32    itojun 			h_errno = HOST_NOT_FOUND;
   2084       1.32    itojun 			break;
   2085       1.32    itojun 		case SERVFAIL:
   2086       1.32    itojun 			h_errno = TRY_AGAIN;
   2087       1.32    itojun 			break;
   2088       1.32    itojun 		case NOERROR:
   2089       1.32    itojun 			h_errno = NO_DATA;
   2090       1.32    itojun 			break;
   2091       1.32    itojun 		case FORMERR:
   2092       1.32    itojun 		case NOTIMP:
   2093       1.32    itojun 		case REFUSED:
   2094       1.32    itojun 		default:
   2095       1.32    itojun 			h_errno = NO_RECOVERY;
   2096       1.32    itojun 			break;
   2097       1.32    itojun 		}
   2098       1.70  christos 		return -1;
   2099       1.32    itojun 	}
   2100       1.70  christos 	return ancount;
   2101       1.32    itojun }
   2102       1.32    itojun 
   2103       1.32    itojun /*
   2104       1.32    itojun  * Formulate a normal query, send, and retrieve answer in supplied buffer.
   2105       1.32    itojun  * Return the size of the response on success, -1 on error.
   2106       1.32    itojun  * If enabled, implement search rules until answer or unrecoverable failure
   2107      1.102  christos  * is detected.	 Error code, if any, is left in h_errno.
   2108       1.32    itojun  */
   2109       1.32    itojun static int
   2110       1.73    tsarna res_searchN(const char *name, struct res_target *target, res_state res)
   2111       1.32    itojun {
   2112       1.32    itojun 	const char *cp, * const *domain;
   2113       1.51     lukem 	HEADER *hp;
   2114       1.32    itojun 	u_int dots;
   2115      1.106  christos 	char buf[MAXHOSTNAMELEN];
   2116       1.32    itojun 	int trailing_dot, ret, saved_herrno;
   2117       1.32    itojun 	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
   2118       1.71  christos 
   2119       1.51     lukem 	_DIAGASSERT(name != NULL);
   2120       1.51     lukem 	_DIAGASSERT(target != NULL);
   2121       1.51     lukem 
   2122       1.51     lukem 	hp = (HEADER *)(void *)target->answer;	/*XXX*/
   2123       1.51     lukem 
   2124       1.32    itojun 	errno = 0;
   2125       1.32    itojun 	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
   2126       1.32    itojun 	dots = 0;
   2127       1.32    itojun 	for (cp = name; *cp; cp++)
   2128       1.32    itojun 		dots += (*cp == '.');
   2129       1.32    itojun 	trailing_dot = 0;
   2130       1.32    itojun 	if (cp > name && *--cp == '.')
   2131       1.32    itojun 		trailing_dot++;
   2132       1.32    itojun 
   2133       1.32    itojun 	/*
   2134       1.95       wiz 	 * if there aren't any dots, it could be a user-level alias
   2135       1.32    itojun 	 */
   2136      1.106  christos 	if (!dots && (cp = res_hostalias(res, name, buf, sizeof(buf))) != NULL) {
   2137       1.70  christos 		ret = res_queryN(cp, target, res);
   2138       1.70  christos 		return ret;
   2139       1.70  christos 	}
   2140       1.32    itojun 
   2141       1.32    itojun 	/*
   2142       1.32    itojun 	 * If there are dots in the name already, let's just give it a try
   2143       1.32    itojun 	 * 'as is'.  The threshold can be set with the "ndots" option.
   2144       1.32    itojun 	 */
   2145       1.32    itojun 	saved_herrno = -1;
   2146       1.70  christos 	if (dots >= res->ndots) {
   2147       1.70  christos 		ret = res_querydomainN(name, NULL, target, res);
   2148       1.73    tsarna 		if (ret > 0)
   2149       1.32    itojun 			return (ret);
   2150       1.32    itojun 		saved_herrno = h_errno;
   2151       1.32    itojun 		tried_as_is++;
   2152       1.32    itojun 	}
   2153       1.32    itojun 
   2154       1.32    itojun 	/*
   2155       1.32    itojun 	 * We do at least one level of search if
   2156       1.32    itojun 	 *	- there is no dot and RES_DEFNAME is set, or
   2157       1.32    itojun 	 *	- there is at least one dot, there is no trailing dot,
   2158       1.32    itojun 	 *	  and RES_DNSRCH is set.
   2159       1.32    itojun 	 */
   2160       1.70  christos 	if ((!dots && (res->options & RES_DEFNAMES)) ||
   2161       1.70  christos 	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
   2162       1.32    itojun 		int done = 0;
   2163       1.32    itojun 
   2164       1.70  christos 		for (domain = (const char * const *)res->dnsrch;
   2165       1.32    itojun 		   *domain && !done;
   2166       1.32    itojun 		   domain++) {
   2167       1.32    itojun 
   2168       1.70  christos 			ret = res_querydomainN(name, *domain, target, res);
   2169       1.73    tsarna 			if (ret > 0)
   2170       1.70  christos 				return ret;
   2171       1.32    itojun 
   2172       1.32    itojun 			/*
   2173       1.32    itojun 			 * If no server present, give up.
   2174       1.32    itojun 			 * If name isn't found in this domain,
   2175       1.32    itojun 			 * keep trying higher domains in the search list
   2176       1.32    itojun 			 * (if that's enabled).
   2177       1.32    itojun 			 * On a NO_DATA error, keep trying, otherwise
   2178       1.32    itojun 			 * a wildcard entry of another type could keep us
   2179       1.32    itojun 			 * from finding this entry higher in the domain.
   2180       1.32    itojun 			 * If we get some other error (negative answer or
   2181       1.32    itojun 			 * server failure), then stop searching up,
   2182       1.32    itojun 			 * but try the input name below in case it's
   2183       1.32    itojun 			 * fully-qualified.
   2184       1.32    itojun 			 */
   2185       1.32    itojun 			if (errno == ECONNREFUSED) {
   2186       1.32    itojun 				h_errno = TRY_AGAIN;
   2187       1.70  christos 				return -1;
   2188       1.32    itojun 			}
   2189       1.32    itojun 
   2190       1.32    itojun 			switch (h_errno) {
   2191       1.32    itojun 			case NO_DATA:
   2192       1.32    itojun 				got_nodata++;
   2193       1.32    itojun 				/* FALLTHROUGH */
   2194       1.32    itojun 			case HOST_NOT_FOUND:
   2195       1.32    itojun 				/* keep trying */
   2196       1.32    itojun 				break;
   2197       1.32    itojun 			case TRY_AGAIN:
   2198       1.32    itojun 				if (hp->rcode == SERVFAIL) {
   2199       1.32    itojun 					/* try next search element, if any */
   2200       1.32    itojun 					got_servfail++;
   2201       1.32    itojun 					break;
   2202       1.32    itojun 				}
   2203       1.32    itojun 				/* FALLTHROUGH */
   2204       1.32    itojun 			default:
   2205       1.32    itojun 				/* anything else implies that we're done */
   2206       1.32    itojun 				done++;
   2207       1.32    itojun 			}
   2208       1.32    itojun 			/*
   2209       1.32    itojun 			 * if we got here for some reason other than DNSRCH,
   2210       1.32    itojun 			 * we only wanted one iteration of the loop, so stop.
   2211       1.32    itojun 			 */
   2212       1.70  christos 			if (!(res->options & RES_DNSRCH))
   2213      1.102  christos 				done++;
   2214       1.32    itojun 		}
   2215       1.32    itojun 	}
   2216       1.32    itojun 
   2217       1.32    itojun 	/*
   2218       1.32    itojun 	 * if we have not already tried the name "as is", do that now.
   2219       1.32    itojun 	 * note that we do this regardless of how many dots were in the
   2220       1.32    itojun 	 * name or whether it ends with a dot.
   2221       1.32    itojun 	 */
   2222       1.32    itojun 	if (!tried_as_is) {
   2223       1.70  christos 		ret = res_querydomainN(name, NULL, target, res);
   2224       1.73    tsarna 		if (ret > 0)
   2225       1.70  christos 			return ret;
   2226       1.32    itojun 	}
   2227       1.32    itojun 
   2228       1.32    itojun 	/*
   2229       1.32    itojun 	 * if we got here, we didn't satisfy the search.
   2230       1.32    itojun 	 * if we did an initial full query, return that query's h_errno
   2231       1.32    itojun 	 * (note that we wouldn't be here if that query had succeeded).
   2232       1.32    itojun 	 * else if we ever got a nodata, send that back as the reason.
   2233       1.32    itojun 	 * else send back meaningless h_errno, that being the one from
   2234       1.32    itojun 	 * the last DNSRCH we did.
   2235       1.32    itojun 	 */
   2236       1.32    itojun 	if (saved_herrno != -1)
   2237       1.32    itojun 		h_errno = saved_herrno;
   2238       1.32    itojun 	else if (got_nodata)
   2239       1.32    itojun 		h_errno = NO_DATA;
   2240       1.32    itojun 	else if (got_servfail)
   2241       1.32    itojun 		h_errno = TRY_AGAIN;
   2242       1.70  christos 	return -1;
   2243       1.32    itojun }
   2244       1.32    itojun 
   2245       1.32    itojun /*
   2246       1.32    itojun  * Perform a call on res_query on the concatenation of name and domain,
   2247       1.32    itojun  * removing a trailing dot from name if domain is NULL.
   2248       1.32    itojun  */
   2249       1.32    itojun static int
   2250       1.73    tsarna res_querydomainN(const char *name, const char *domain,
   2251       1.70  christos     struct res_target *target, res_state res)
   2252       1.32    itojun {
   2253       1.32    itojun 	char nbuf[MAXDNAME];
   2254       1.32    itojun 	const char *longname = nbuf;
   2255       1.32    itojun 	size_t n, d;
   2256       1.51     lukem 
   2257       1.51     lukem 	_DIAGASSERT(name != NULL);
   2258       1.51     lukem 	/* XXX: target may be NULL??? */
   2259       1.32    itojun 
   2260       1.32    itojun #ifdef DEBUG
   2261       1.70  christos 	if (res->options & RES_DEBUG)
   2262       1.32    itojun 		printf(";; res_querydomain(%s, %s)\n",
   2263       1.32    itojun 			name, domain?domain:"<Nil>");
   2264       1.32    itojun #endif
   2265       1.32    itojun 	if (domain == NULL) {
   2266       1.32    itojun 		/*
   2267       1.32    itojun 		 * Check for trailing '.';
   2268       1.32    itojun 		 * copy without '.' if present.
   2269       1.32    itojun 		 */
   2270       1.32    itojun 		n = strlen(name);
   2271       1.63    itojun 		if (n + 1 > sizeof(nbuf)) {
   2272       1.32    itojun 			h_errno = NO_RECOVERY;
   2273       1.70  christos 			return -1;
   2274       1.32    itojun 		}
   2275       1.40    itojun 		if (n > 0 && name[--n] == '.') {
   2276       1.32    itojun 			strncpy(nbuf, name, n);
   2277       1.32    itojun 			nbuf[n] = '\0';
   2278       1.32    itojun 		} else
   2279       1.32    itojun 			longname = name;
   2280       1.32    itojun 	} else {
   2281       1.32    itojun 		n = strlen(name);
   2282       1.32    itojun 		d = strlen(domain);
   2283       1.63    itojun 		if (n + 1 + d + 1 > sizeof(nbuf)) {
   2284       1.32    itojun 			h_errno = NO_RECOVERY;
   2285       1.70  christos 			return -1;
   2286       1.32    itojun 		}
   2287       1.54    itojun 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
   2288       1.32    itojun 	}
   2289       1.70  christos 	return res_queryN(longname, target, res);
   2290       1.32    itojun }
   2291      1.103  christos 
   2292      1.103  christos #ifdef TEST
   2293      1.103  christos int
   2294      1.103  christos main(int argc, char *argv[]) {
   2295      1.103  christos 	struct addrinfo *ai, *sai;
   2296      1.103  christos 	int i, e;
   2297      1.103  christos 	char buf[1024];
   2298      1.103  christos 
   2299      1.103  christos 	for (i = 1; i < argc; i++) {
   2300      1.103  christos 		if ((e = getaddrinfo(argv[i], NULL, NULL, &sai)) != 0)
   2301      1.103  christos 			warnx("%s: %s", argv[i], gai_strerror(e));
   2302      1.103  christos 		for (ai = sai; ai; ai = ai->ai_next) {
   2303      1.103  christos 			sockaddr_snprintf(buf, sizeof(buf), "%a", ai->ai_addr);
   2304      1.103  christos              		printf("flags=0x%x family=%d socktype=%d protocol=%d "
   2305      1.103  christos 			    "addrlen=%zu addr=%s canonname=%s next=%p\n",
   2306      1.103  christos 			    ai->ai_flags,
   2307      1.103  christos              		    ai->ai_family,
   2308      1.103  christos              		    ai->ai_socktype,
   2309      1.103  christos              		    ai->ai_protocol,
   2310      1.103  christos              		    (size_t)ai->ai_addrlen,
   2311      1.103  christos 			    buf,
   2312      1.103  christos 			    ai->ai_canonname,
   2313      1.103  christos 			    ai->ai_next);
   2314      1.103  christos 		}
   2315      1.103  christos 		if (sai)
   2316      1.103  christos 			freeaddrinfo(sai);
   2317      1.103  christos 	}
   2318      1.103  christos 	return 0;
   2319      1.103  christos }
   2320      1.103  christos #endif
   2321