Home | History | Annotate | Line # | Download | only in net
getaddrinfo.c revision 1.98
      1  1.98  christos /*	$NetBSD: getaddrinfo.c,v 1.98 2012/03/17 21:56:40 christos 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.98  christos __RCSID("$NetBSD: getaddrinfo.c,v 1.98 2012/03/17 21:56:40 christos 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.1    itojun 	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.32    itojun 	{ NSSRC_FILES, 	NS_SUCCESS },
    174  1.32    itojun 	{ 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.70  christos static int str2number(const char *);
    195  1.70  christos static int explore_fqdn(const struct addrinfo *, const char *,
    196  1.89  christos 	const char *, struct addrinfo **, struct servent_data *);
    197  1.70  christos static int explore_null(const struct addrinfo *,
    198  1.89  christos 	const char *, struct addrinfo **, struct servent_data *);
    199  1.70  christos static int explore_numeric(const struct addrinfo *, const char *,
    200  1.89  christos 	const char *, struct addrinfo **, const char *, struct servent_data *);
    201  1.70  christos static int explore_numeric_scope(const struct addrinfo *, const char *,
    202  1.89  christos 	const char *, struct addrinfo **, struct servent_data *);
    203  1.70  christos static int get_canonname(const struct addrinfo *,
    204  1.70  christos 	struct addrinfo *, const char *);
    205  1.70  christos static struct addrinfo *get_ai(const struct addrinfo *,
    206  1.70  christos 	const struct afd *, const char *);
    207  1.89  christos static int get_portmatch(const struct addrinfo *, const char *,
    208  1.89  christos     struct servent_data *);
    209  1.89  christos static int get_port(const struct addrinfo *, const char *, int,
    210  1.89  christos     struct servent_data *);
    211  1.70  christos static const struct afd *find_afd(int);
    212  1.98  christos static int addrconfig(uint64_t *);
    213  1.27    itojun #ifdef INET6
    214  1.70  christos static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
    215  1.42    itojun #endif
    216  1.14    itojun 
    217  1.70  christos static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
    218  1.70  christos 	const struct addrinfo *);
    219  1.73    tsarna static void aisort(struct addrinfo *s, res_state res);
    220  1.70  christos static int _dns_getaddrinfo(void *, void *, va_list);
    221  1.70  christos static void _sethtent(FILE **);
    222  1.70  christos static void _endhtent(FILE **);
    223  1.70  christos static struct addrinfo *_gethtent(FILE **, const char *,
    224  1.70  christos     const struct addrinfo *);
    225  1.70  christos static int _files_getaddrinfo(void *, void *, va_list);
    226  1.32    itojun #ifdef YP
    227  1.70  christos static struct addrinfo *_yphostent(char *, const struct addrinfo *);
    228  1.70  christos static int _yp_getaddrinfo(void *, void *, va_list);
    229  1.32    itojun #endif
    230  1.32    itojun 
    231  1.70  christos static int res_queryN(const char *, struct res_target *, res_state);
    232  1.73    tsarna static int res_searchN(const char *, struct res_target *, res_state);
    233  1.70  christos static int res_querydomainN(const char *, const char *,
    234  1.70  christos 	struct res_target *, res_state);
    235  1.32    itojun 
    236  1.53  jdolecek static const char * const ai_errlist[] = {
    237   1.7     lukem 	"Success",
    238   1.7     lukem 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
    239   1.7     lukem 	"Temporary failure in name resolution",		/* EAI_AGAIN      */
    240   1.7     lukem 	"Invalid value for ai_flags",		       	/* EAI_BADFLAGS   */
    241   1.7     lukem 	"Non-recoverable failure in name resolution", 	/* EAI_FAIL       */
    242   1.2     lukem 	"ai_family not supported",			/* EAI_FAMILY     */
    243   1.7     lukem 	"Memory allocation failure", 			/* EAI_MEMORY     */
    244   1.7     lukem 	"No address associated with hostname", 		/* EAI_NODATA     */
    245  1.14    itojun 	"hostname nor servname provided, or not known",	/* EAI_NONAME     */
    246   1.2     lukem 	"servname not supported for ai_socktype",	/* EAI_SERVICE    */
    247   1.2     lukem 	"ai_socktype not supported", 			/* EAI_SOCKTYPE   */
    248   1.7     lukem 	"System error returned in errno", 		/* EAI_SYSTEM     */
    249   1.7     lukem 	"Invalid value for hints",			/* EAI_BADHINTS	  */
    250   1.7     lukem 	"Resolved protocol is unknown",			/* EAI_PROTOCOL   */
    251  1.77  ginsbach 	"Argument buffer overflow",			/* EAI_OVERFLOW   */
    252   1.7     lukem 	"Unknown error", 				/* EAI_MAX        */
    253   1.1    itojun };
    254   1.1    itojun 
    255  1.14    itojun /* XXX macros that make external reference is BAD. */
    256  1.14    itojun 
    257  1.82    rpaulo #define GET_AI(ai, afd, addr) 					\
    258  1.82    rpaulo do { 								\
    259  1.82    rpaulo 	/* external reference: pai, error, and label free */ 	\
    260  1.82    rpaulo 	(ai) = get_ai(pai, (afd), (addr)); 			\
    261  1.82    rpaulo 	if ((ai) == NULL) { 					\
    262  1.82    rpaulo 		error = EAI_MEMORY; 				\
    263  1.82    rpaulo 		goto free; 					\
    264  1.82    rpaulo 	} 							\
    265  1.20   mycroft } while (/*CONSTCOND*/0)
    266  1.14    itojun 
    267  1.89  christos #define GET_PORT(ai, serv, svd) 				\
    268  1.82    rpaulo do { 								\
    269  1.82    rpaulo 	/* external reference: error and label free */ 		\
    270  1.89  christos 	error = get_port((ai), (serv), 0, (svd)); 		\
    271  1.82    rpaulo 	if (error != 0) 					\
    272  1.82    rpaulo 		goto free; 					\
    273  1.20   mycroft } while (/*CONSTCOND*/0)
    274  1.14    itojun 
    275  1.82    rpaulo #define GET_CANONNAME(ai, str) 					\
    276  1.82    rpaulo do { 								\
    277  1.82    rpaulo 	/* external reference: pai, error and label free */ 	\
    278  1.82    rpaulo 	error = get_canonname(pai, (ai), (str)); 		\
    279  1.82    rpaulo 	if (error != 0) 					\
    280  1.82    rpaulo 		goto free; 					\
    281  1.20   mycroft } while (/*CONSTCOND*/0)
    282  1.14    itojun 
    283  1.82    rpaulo #define ERR(err) 						\
    284  1.82    rpaulo do { 								\
    285  1.82    rpaulo 	/* external reference: error, and label bad */ 		\
    286  1.82    rpaulo 	error = (err); 						\
    287  1.82    rpaulo 	goto bad; 						\
    288  1.82    rpaulo 	/*NOTREACHED*/ 						\
    289  1.20   mycroft } while (/*CONSTCOND*/0)
    290  1.14    itojun 
    291  1.82    rpaulo #define MATCH_FAMILY(x, y, w) 						\
    292  1.82    rpaulo 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || 	\
    293  1.82    rpaulo 	    (y) == PF_UNSPEC)))
    294  1.82    rpaulo #define MATCH(x, y, w) 							\
    295  1.20   mycroft 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
    296   1.1    itojun 
    297  1.70  christos const char *
    298  1.70  christos gai_strerror(int ecode)
    299   1.1    itojun {
    300   1.1    itojun 	if (ecode < 0 || ecode > EAI_MAX)
    301   1.1    itojun 		ecode = EAI_MAX;
    302  1.70  christos 	return ai_errlist[ecode];
    303   1.1    itojun }
    304   1.1    itojun 
    305   1.1    itojun void
    306  1.70  christos freeaddrinfo(struct addrinfo *ai)
    307   1.1    itojun {
    308   1.1    itojun 	struct addrinfo *next;
    309   1.1    itojun 
    310  1.51     lukem 	_DIAGASSERT(ai != NULL);
    311  1.51     lukem 
    312  1.26    itojun 	do {
    313   1.1    itojun 		next = ai->ai_next;
    314   1.1    itojun 		if (ai->ai_canonname)
    315   1.1    itojun 			free(ai->ai_canonname);
    316   1.1    itojun 		/* no need to free(ai->ai_addr) */
    317   1.1    itojun 		free(ai);
    318  1.27    itojun 		ai = next;
    319  1.27    itojun 	} while (ai);
    320   1.1    itojun }
    321   1.1    itojun 
    322   1.1    itojun static int
    323  1.70  christos str2number(const char *p)
    324   1.1    itojun {
    325  1.45    itojun 	char *ep;
    326  1.67    itojun 	unsigned long v;
    327  1.45    itojun 
    328  1.51     lukem 	_DIAGASSERT(p != NULL);
    329  1.51     lukem 
    330  1.46    itojun 	if (*p == '\0')
    331  1.67    itojun 		return -1;
    332  1.45    itojun 	ep = NULL;
    333  1.59    itojun 	errno = 0;
    334  1.67    itojun 	v = strtoul(p, &ep, 10);
    335  1.97  christos 	if (errno == 0 && ep && *ep == '\0' && v <= INT_MAX)
    336  1.97  christos 		return (int)v;
    337  1.45    itojun 	else
    338  1.67    itojun 		return -1;
    339   1.1    itojun }
    340   1.1    itojun 
    341   1.1    itojun int
    342  1.70  christos getaddrinfo(const char *hostname, const char *servname,
    343  1.70  christos     const struct addrinfo *hints, struct addrinfo **res)
    344   1.1    itojun {
    345   1.1    itojun 	struct addrinfo sentinel;
    346   1.1    itojun 	struct addrinfo *cur;
    347  1.14    itojun 	int error = 0;
    348   1.1    itojun 	struct addrinfo ai;
    349  1.14    itojun 	struct addrinfo ai0;
    350   1.1    itojun 	struct addrinfo *pai;
    351  1.14    itojun 	const struct explore *ex;
    352  1.89  christos 	struct servent_data svd;
    353  1.98  christos 	uint64_t mask = (uint64_t)~0ULL;
    354   1.1    itojun 
    355  1.51     lukem 	/* hostname is allowed to be NULL */
    356  1.51     lukem 	/* servname is allowed to be NULL */
    357  1.51     lukem 	/* hints is allowed to be NULL */
    358  1.51     lukem 	_DIAGASSERT(res != NULL);
    359  1.51     lukem 
    360  1.89  christos 	(void)memset(&svd, 0, sizeof(svd));
    361  1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
    362   1.1    itojun 	cur = &sentinel;
    363  1.86  christos 	memset(&ai, 0, sizeof(ai));
    364   1.1    itojun 	pai = &ai;
    365   1.1    itojun 	pai->ai_flags = 0;
    366   1.1    itojun 	pai->ai_family = PF_UNSPEC;
    367   1.1    itojun 	pai->ai_socktype = ANY;
    368   1.1    itojun 	pai->ai_protocol = ANY;
    369   1.1    itojun 	pai->ai_addrlen = 0;
    370   1.1    itojun 	pai->ai_canonname = NULL;
    371   1.1    itojun 	pai->ai_addr = NULL;
    372   1.1    itojun 	pai->ai_next = NULL;
    373   1.1    itojun 
    374   1.1    itojun 	if (hostname == NULL && servname == NULL)
    375   1.1    itojun 		return EAI_NONAME;
    376   1.1    itojun 	if (hints) {
    377   1.1    itojun 		/* error check for hints */
    378   1.1    itojun 		if (hints->ai_addrlen || hints->ai_canonname ||
    379   1.1    itojun 		    hints->ai_addr || hints->ai_next)
    380   1.1    itojun 			ERR(EAI_BADHINTS); /* xxx */
    381   1.1    itojun 		if (hints->ai_flags & ~AI_MASK)
    382   1.1    itojun 			ERR(EAI_BADFLAGS);
    383   1.1    itojun 		switch (hints->ai_family) {
    384   1.1    itojun 		case PF_UNSPEC:
    385   1.1    itojun 		case PF_INET:
    386   1.1    itojun #ifdef INET6
    387   1.1    itojun 		case PF_INET6:
    388   1.1    itojun #endif
    389   1.1    itojun 			break;
    390   1.1    itojun 		default:
    391   1.1    itojun 			ERR(EAI_FAMILY);
    392   1.1    itojun 		}
    393   1.1    itojun 		memcpy(pai, hints, sizeof(*pai));
    394  1.14    itojun 
    395  1.14    itojun 		/*
    396  1.14    itojun 		 * if both socktype/protocol are specified, check if they
    397  1.14    itojun 		 * are meaningful combination.
    398  1.14    itojun 		 */
    399  1.14    itojun 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
    400  1.14    itojun 			for (ex = explore; ex->e_af >= 0; ex++) {
    401  1.14    itojun 				if (pai->ai_family != ex->e_af)
    402  1.14    itojun 					continue;
    403  1.14    itojun 				if (ex->e_socktype == ANY)
    404  1.14    itojun 					continue;
    405  1.14    itojun 				if (ex->e_protocol == ANY)
    406  1.14    itojun 					continue;
    407  1.14    itojun 				if (pai->ai_socktype == ex->e_socktype
    408  1.14    itojun 				 && pai->ai_protocol != ex->e_protocol) {
    409  1.14    itojun 					ERR(EAI_BADHINTS);
    410  1.14    itojun 				}
    411   1.1    itojun 			}
    412   1.1    itojun 		}
    413   1.1    itojun 	}
    414   1.1    itojun 
    415  1.98  christos 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && addrconfig(&mask) == -1)
    416  1.98  christos 		ERR(EAI_FAIL);
    417  1.98  christos 
    418   1.1    itojun 	/*
    419  1.14    itojun 	 * check for special cases.  (1) numeric servname is disallowed if
    420  1.14    itojun 	 * socktype/protocol are left unspecified. (2) servname is disallowed
    421  1.14    itojun 	 * for raw and other inet{,6} sockets.
    422   1.1    itojun 	 */
    423  1.14    itojun 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
    424  1.27    itojun #ifdef PF_INET6
    425  1.27    itojun 	 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
    426  1.27    itojun #endif
    427  1.27    itojun 	    ) {
    428  1.23    itojun 		ai0 = *pai;	/* backup *pai */
    429  1.21   mycroft 
    430  1.27    itojun 		if (pai->ai_family == PF_UNSPEC) {
    431  1.27    itojun #ifdef PF_INET6
    432  1.14    itojun 			pai->ai_family = PF_INET6;
    433  1.27    itojun #else
    434  1.27    itojun 			pai->ai_family = PF_INET;
    435  1.27    itojun #endif
    436  1.27    itojun 		}
    437  1.89  christos 		error = get_portmatch(pai, servname, &svd);
    438  1.14    itojun 		if (error)
    439  1.14    itojun 			ERR(error);
    440  1.21   mycroft 
    441  1.21   mycroft 		*pai = ai0;
    442  1.14    itojun 	}
    443  1.14    itojun 
    444  1.14    itojun 	ai0 = *pai;
    445  1.14    itojun 
    446  1.14    itojun 	/* NULL hostname, or numeric hostname */
    447  1.14    itojun 	for (ex = explore; ex->e_af >= 0; ex++) {
    448  1.14    itojun 		*pai = ai0;
    449  1.14    itojun 
    450  1.98  christos 		/* ADDRCONFIG check */
    451  1.98  christos 		if ((((uint64_t)1 << ex->e_af) & mask) == 0)
    452  1.98  christos 			continue;
    453  1.98  christos 
    454  1.32    itojun 		/* PF_UNSPEC entries are prepared for DNS queries only */
    455  1.32    itojun 		if (ex->e_af == PF_UNSPEC)
    456  1.32    itojun 			continue;
    457  1.32    itojun 
    458  1.14    itojun 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    459  1.14    itojun 			continue;
    460  1.14    itojun 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
    461  1.14    itojun 			continue;
    462  1.14    itojun 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
    463  1.14    itojun 			continue;
    464  1.14    itojun 		if (pai->ai_family == PF_UNSPEC)
    465  1.14    itojun 			pai->ai_family = ex->e_af;
    466  1.14    itojun 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    467  1.14    itojun 			pai->ai_socktype = ex->e_socktype;
    468  1.14    itojun 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    469  1.14    itojun 			pai->ai_protocol = ex->e_protocol;
    470  1.14    itojun 
    471  1.14    itojun 		if (hostname == NULL)
    472  1.89  christos 			error = explore_null(pai, servname, &cur->ai_next,
    473  1.89  christos 			    &svd);
    474  1.14    itojun 		else
    475  1.65    itojun 			error = explore_numeric_scope(pai, hostname, servname,
    476  1.89  christos 			    &cur->ai_next, &svd);
    477  1.14    itojun 
    478  1.14    itojun 		if (error)
    479  1.14    itojun 			goto free;
    480  1.14    itojun 
    481  1.79  christos 		while (cur->ai_next)
    482  1.14    itojun 			cur = cur->ai_next;
    483  1.14    itojun 	}
    484  1.14    itojun 
    485  1.14    itojun 	/*
    486  1.14    itojun 	 * XXX
    487  1.76  ginsbach 	 * If numeric representation of AF1 can be interpreted as FQDN
    488  1.14    itojun 	 * representation of AF2, we need to think again about the code below.
    489  1.14    itojun 	 */
    490  1.14    itojun 	if (sentinel.ai_next)
    491  1.14    itojun 		goto good;
    492  1.14    itojun 
    493  1.14    itojun 	if (hostname == NULL)
    494  1.43    itojun 		ERR(EAI_NODATA);
    495  1.64    itojun 	if (pai->ai_flags & AI_NUMERICHOST)
    496  1.64    itojun 		ERR(EAI_NONAME);
    497  1.14    itojun 
    498  1.14    itojun 	/*
    499  1.14    itojun 	 * hostname as alphabetical name.
    500  1.14    itojun 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
    501  1.14    itojun 	 * outer loop by AFs.
    502  1.14    itojun 	 */
    503  1.32    itojun 	for (ex = explore; ex->e_af >= 0; ex++) {
    504  1.14    itojun 		*pai = ai0;
    505  1.14    itojun 
    506  1.98  christos 		/* ADDRCONFIG check */
    507  1.98  christos 		if ((((uint64_t)1 << ex->e_af) & mask) == 0)
    508  1.98  christos 			continue;
    509  1.98  christos 
    510  1.32    itojun 		/* require exact match for family field */
    511  1.32    itojun 		if (pai->ai_family != ex->e_af)
    512  1.14    itojun 			continue;
    513  1.14    itojun 
    514  1.32    itojun 		if (!MATCH(pai->ai_socktype, ex->e_socktype,
    515  1.32    itojun 				WILD_SOCKTYPE(ex))) {
    516  1.32    itojun 			continue;
    517  1.32    itojun 		}
    518  1.32    itojun 		if (!MATCH(pai->ai_protocol, ex->e_protocol,
    519  1.32    itojun 				WILD_PROTOCOL(ex))) {
    520  1.32    itojun 			continue;
    521  1.32    itojun 		}
    522  1.14    itojun 
    523  1.32    itojun 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    524  1.32    itojun 			pai->ai_socktype = ex->e_socktype;
    525  1.32    itojun 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    526  1.32    itojun 			pai->ai_protocol = ex->e_protocol;
    527   1.1    itojun 
    528  1.89  christos 		error = explore_fqdn(pai, hostname, servname, &cur->ai_next,
    529  1.89  christos 		    &svd);
    530  1.14    itojun 
    531  1.32    itojun 		while (cur && cur->ai_next)
    532  1.32    itojun 			cur = cur->ai_next;
    533   1.1    itojun 	}
    534  1.14    itojun 
    535  1.14    itojun 	/* XXX */
    536  1.14    itojun 	if (sentinel.ai_next)
    537  1.14    itojun 		error = 0;
    538  1.14    itojun 
    539  1.14    itojun 	if (error)
    540  1.14    itojun 		goto free;
    541  1.89  christos 
    542  1.89  christos 	if (sentinel.ai_next) {
    543  1.14    itojun  good:
    544  1.89  christos 		endservent_r(&svd);
    545  1.89  christos 		*res = sentinel.ai_next;
    546  1.89  christos 		return SUCCESS;
    547  1.89  christos 	} else
    548  1.89  christos 		error = EAI_FAIL;
    549  1.14    itojun  free:
    550  1.14    itojun  bad:
    551  1.89  christos 	endservent_r(&svd);
    552  1.14    itojun 	if (sentinel.ai_next)
    553  1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    554  1.14    itojun 	*res = NULL;
    555  1.14    itojun 	return error;
    556  1.14    itojun }
    557  1.14    itojun 
    558  1.14    itojun /*
    559  1.14    itojun  * FQDN hostname, DNS lookup
    560  1.14    itojun  */
    561  1.14    itojun static int
    562  1.70  christos explore_fqdn(const struct addrinfo *pai, const char *hostname,
    563  1.89  christos     const char *servname, struct addrinfo **res, struct servent_data *svd)
    564  1.14    itojun {
    565  1.32    itojun 	struct addrinfo *result;
    566  1.32    itojun 	struct addrinfo *cur;
    567  1.27    itojun 	int error = 0;
    568  1.32    itojun 	static const ns_dtab dtab[] = {
    569  1.32    itojun 		NS_FILES_CB(_files_getaddrinfo, NULL)
    570  1.32    itojun 		{ NSSRC_DNS, _dns_getaddrinfo, NULL },	/* force -DHESIOD */
    571  1.32    itojun 		NS_NIS_CB(_yp_getaddrinfo, NULL)
    572  1.87  christos 		NS_NULL_CB
    573  1.32    itojun 	};
    574  1.14    itojun 
    575  1.51     lukem 	_DIAGASSERT(pai != NULL);
    576  1.51     lukem 	/* hostname may be NULL */
    577  1.51     lukem 	/* servname may be NULL */
    578  1.51     lukem 	_DIAGASSERT(res != NULL);
    579  1.51     lukem 
    580  1.32    itojun 	result = NULL;
    581  1.14    itojun 
    582  1.14    itojun 	/*
    583  1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    584  1.14    itojun 	 */
    585  1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    586  1.14    itojun 		return 0;
    587  1.14    itojun 
    588  1.32    itojun 	switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",
    589  1.32    itojun 			default_dns_files, hostname, pai)) {
    590  1.32    itojun 	case NS_TRYAGAIN:
    591  1.32    itojun 		error = EAI_AGAIN;
    592  1.32    itojun 		goto free;
    593  1.32    itojun 	case NS_UNAVAIL:
    594  1.14    itojun 		error = EAI_FAIL;
    595  1.14    itojun 		goto free;
    596  1.32    itojun 	case NS_NOTFOUND:
    597  1.32    itojun 		error = EAI_NODATA;
    598  1.15    itojun 		goto free;
    599  1.32    itojun 	case NS_SUCCESS:
    600  1.32    itojun 		error = 0;
    601  1.32    itojun 		for (cur = result; cur; cur = cur->ai_next) {
    602  1.89  christos 			GET_PORT(cur, servname, svd);
    603  1.32    itojun 			/* canonname should be filled already */
    604  1.16    itojun 		}
    605  1.32    itojun 		break;
    606  1.16    itojun 	}
    607  1.15    itojun 
    608  1.32    itojun 	*res = result;
    609  1.14    itojun 
    610  1.14    itojun 	return 0;
    611  1.14    itojun 
    612  1.14    itojun free:
    613  1.34    itojun 	if (result)
    614  1.34    itojun 		freeaddrinfo(result);
    615  1.14    itojun 	return error;
    616  1.14    itojun }
    617  1.14    itojun 
    618  1.14    itojun /*
    619  1.14    itojun  * hostname == NULL.
    620  1.14    itojun  * passive socket -> anyaddr (0.0.0.0 or ::)
    621  1.14    itojun  * non-passive socket -> localhost (127.0.0.1 or ::1)
    622  1.14    itojun  */
    623  1.14    itojun static int
    624  1.70  christos explore_null(const struct addrinfo *pai, const char *servname,
    625  1.89  christos     struct addrinfo **res, struct servent_data *svd)
    626  1.14    itojun {
    627  1.14    itojun 	int s;
    628  1.14    itojun 	const struct afd *afd;
    629  1.14    itojun 	struct addrinfo *cur;
    630  1.14    itojun 	struct addrinfo sentinel;
    631  1.14    itojun 	int error;
    632  1.14    itojun 
    633  1.51     lukem 	_DIAGASSERT(pai != NULL);
    634  1.51     lukem 	/* servname may be NULL */
    635  1.51     lukem 	_DIAGASSERT(res != NULL);
    636  1.51     lukem 
    637  1.14    itojun 	*res = NULL;
    638  1.14    itojun 	sentinel.ai_next = NULL;
    639  1.14    itojun 	cur = &sentinel;
    640  1.14    itojun 
    641  1.14    itojun 	/*
    642  1.14    itojun 	 * filter out AFs that are not supported by the kernel
    643  1.14    itojun 	 * XXX errno?
    644  1.14    itojun 	 */
    645  1.14    itojun 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    646  1.14    itojun 	if (s < 0) {
    647  1.14    itojun 		if (errno != EMFILE)
    648  1.14    itojun 			return 0;
    649  1.14    itojun 	} else
    650  1.14    itojun 		close(s);
    651  1.14    itojun 
    652  1.14    itojun 	/*
    653  1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    654  1.14    itojun 	 */
    655  1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    656  1.14    itojun 		return 0;
    657  1.14    itojun 
    658  1.14    itojun 	afd = find_afd(pai->ai_family);
    659  1.42    itojun 	if (afd == NULL)
    660  1.42    itojun 		return 0;
    661  1.14    itojun 
    662  1.14    itojun 	if (pai->ai_flags & AI_PASSIVE) {
    663  1.14    itojun 		GET_AI(cur->ai_next, afd, afd->a_addrany);
    664  1.14    itojun 		/* xxx meaningless?
    665  1.14    itojun 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
    666  1.14    itojun 		 */
    667  1.89  christos 		GET_PORT(cur->ai_next, servname, svd);
    668  1.14    itojun 	} else {
    669  1.14    itojun 		GET_AI(cur->ai_next, afd, afd->a_loopback);
    670  1.14    itojun 		/* xxx meaningless?
    671  1.14    itojun 		 * GET_CANONNAME(cur->ai_next, "localhost");
    672  1.14    itojun 		 */
    673  1.89  christos 		GET_PORT(cur->ai_next, servname, svd);
    674  1.14    itojun 	}
    675  1.14    itojun 	cur = cur->ai_next;
    676  1.14    itojun 
    677  1.14    itojun 	*res = sentinel.ai_next;
    678  1.14    itojun 	return 0;
    679  1.14    itojun 
    680  1.14    itojun free:
    681  1.14    itojun 	if (sentinel.ai_next)
    682  1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    683  1.14    itojun 	return error;
    684  1.14    itojun }
    685  1.14    itojun 
    686  1.14    itojun /*
    687  1.14    itojun  * numeric hostname
    688  1.14    itojun  */
    689  1.14    itojun static int
    690  1.70  christos explore_numeric(const struct addrinfo *pai, const char *hostname,
    691  1.89  christos     const char *servname, struct addrinfo **res, const char *canonname,
    692  1.89  christos     struct servent_data *svd)
    693  1.14    itojun {
    694  1.14    itojun 	const struct afd *afd;
    695  1.14    itojun 	struct addrinfo *cur;
    696  1.14    itojun 	struct addrinfo sentinel;
    697  1.14    itojun 	int error;
    698  1.14    itojun 	char pton[PTON_MAX];
    699  1.14    itojun 
    700  1.51     lukem 	_DIAGASSERT(pai != NULL);
    701  1.51     lukem 	/* hostname may be NULL */
    702  1.51     lukem 	/* servname may be NULL */
    703  1.51     lukem 	_DIAGASSERT(res != NULL);
    704  1.51     lukem 
    705  1.14    itojun 	*res = NULL;
    706  1.14    itojun 	sentinel.ai_next = NULL;
    707  1.14    itojun 	cur = &sentinel;
    708  1.14    itojun 
    709  1.14    itojun 	/*
    710  1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    711  1.14    itojun 	 */
    712  1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    713  1.14    itojun 		return 0;
    714  1.14    itojun 
    715  1.14    itojun 	afd = find_afd(pai->ai_family);
    716  1.42    itojun 	if (afd == NULL)
    717  1.42    itojun 		return 0;
    718  1.14    itojun 
    719  1.27    itojun 	switch (afd->a_af) {
    720  1.27    itojun #if 0 /*X/Open spec*/
    721  1.27    itojun 	case AF_INET:
    722  1.27    itojun 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
    723  1.27    itojun 			if (pai->ai_family == afd->a_af ||
    724  1.27    itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    725  1.27    itojun 				GET_AI(cur->ai_next, afd, pton);
    726  1.89  christos 				GET_PORT(cur->ai_next, servname, svd);
    727  1.65    itojun 				if ((pai->ai_flags & AI_CANONNAME)) {
    728  1.65    itojun 					/*
    729  1.65    itojun 					 * Set the numeric address itself as
    730  1.65    itojun 					 * the canonical name, based on a
    731  1.65    itojun 					 * clarification in rfc2553bis-03.
    732  1.65    itojun 					 */
    733  1.65    itojun 					GET_CANONNAME(cur->ai_next, canonname);
    734  1.65    itojun 				}
    735  1.27    itojun 				while (cur && cur->ai_next)
    736  1.27    itojun 					cur = cur->ai_next;
    737  1.42    itojun 			} else
    738  1.27    itojun 				ERR(EAI_FAMILY);	/*xxx*/
    739  1.27    itojun 		}
    740  1.27    itojun 		break;
    741  1.27    itojun #endif
    742  1.27    itojun 	default:
    743  1.27    itojun 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
    744  1.27    itojun 			if (pai->ai_family == afd->a_af ||
    745  1.27    itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    746  1.27    itojun 				GET_AI(cur->ai_next, afd, pton);
    747  1.89  christos 				GET_PORT(cur->ai_next, servname, svd);
    748  1.65    itojun 				if ((pai->ai_flags & AI_CANONNAME)) {
    749  1.65    itojun 					/*
    750  1.65    itojun 					 * Set the numeric address itself as
    751  1.65    itojun 					 * the canonical name, based on a
    752  1.65    itojun 					 * clarification in rfc2553bis-03.
    753  1.65    itojun 					 */
    754  1.65    itojun 					GET_CANONNAME(cur->ai_next, canonname);
    755  1.65    itojun 				}
    756  1.81  christos 				while (cur->ai_next)
    757  1.27    itojun 					cur = cur->ai_next;
    758  1.42    itojun 			} else
    759  1.27    itojun 				ERR(EAI_FAMILY);	/*xxx*/
    760  1.27    itojun 		}
    761  1.27    itojun 		break;
    762   1.1    itojun 	}
    763   1.1    itojun 
    764  1.14    itojun 	*res = sentinel.ai_next;
    765  1.14    itojun 	return 0;
    766  1.14    itojun 
    767  1.14    itojun free:
    768  1.14    itojun bad:
    769  1.14    itojun 	if (sentinel.ai_next)
    770  1.14    itojun 		freeaddrinfo(sentinel.ai_next);
    771  1.14    itojun 	return error;
    772  1.14    itojun }
    773  1.14    itojun 
    774  1.14    itojun /*
    775  1.14    itojun  * numeric hostname with scope
    776  1.14    itojun  */
    777  1.14    itojun static int
    778  1.70  christos explore_numeric_scope(const struct addrinfo *pai, const char *hostname,
    779  1.89  christos     const char *servname, struct addrinfo **res, struct servent_data *svd)
    780  1.14    itojun {
    781  1.29    itojun #if !defined(SCOPE_DELIMITER) || !defined(INET6)
    782  1.90  christos 	return explore_numeric(pai, hostname, servname, res, hostname, svd);
    783  1.14    itojun #else
    784  1.14    itojun 	const struct afd *afd;
    785  1.14    itojun 	struct addrinfo *cur;
    786  1.14    itojun 	int error;
    787  1.30    itojun 	char *cp, *hostname2 = NULL, *scope, *addr;
    788  1.14    itojun 	struct sockaddr_in6 *sin6;
    789  1.14    itojun 
    790  1.51     lukem 	_DIAGASSERT(pai != NULL);
    791  1.51     lukem 	/* hostname may be NULL */
    792  1.51     lukem 	/* servname may be NULL */
    793  1.51     lukem 	_DIAGASSERT(res != NULL);
    794  1.51     lukem 
    795  1.14    itojun 	/*
    796  1.14    itojun 	 * if the servname does not match socktype/protocol, ignore it.
    797  1.14    itojun 	 */
    798  1.89  christos 	if (get_portmatch(pai, servname, svd) != 0)
    799  1.14    itojun 		return 0;
    800  1.14    itojun 
    801  1.14    itojun 	afd = find_afd(pai->ai_family);
    802  1.42    itojun 	if (afd == NULL)
    803  1.42    itojun 		return 0;
    804  1.42    itojun 
    805  1.14    itojun 	if (!afd->a_scoped)
    806  1.89  christos 		return explore_numeric(pai, hostname, servname, res, hostname,
    807  1.89  christos 		    svd);
    808  1.14    itojun 
    809  1.14    itojun 	cp = strchr(hostname, SCOPE_DELIMITER);
    810  1.14    itojun 	if (cp == NULL)
    811  1.89  christos 		return explore_numeric(pai, hostname, servname, res, hostname,
    812  1.89  christos 		    svd);
    813  1.14    itojun 
    814  1.30    itojun 	/*
    815  1.30    itojun 	 * Handle special case of <scoped_address><delimiter><scope id>
    816  1.30    itojun 	 */
    817  1.30    itojun 	hostname2 = strdup(hostname);
    818  1.30    itojun 	if (hostname2 == NULL)
    819  1.30    itojun 		return EAI_MEMORY;
    820  1.30    itojun 	/* terminate at the delimiter */
    821  1.30    itojun 	hostname2[cp - hostname] = '\0';
    822  1.30    itojun 	addr = hostname2;
    823  1.30    itojun 	scope = cp + 1;
    824   1.1    itojun 
    825  1.89  christos 	error = explore_numeric(pai, addr, servname, res, hostname, svd);
    826   1.1    itojun 	if (error == 0) {
    827  1.59    itojun 		u_int32_t scopeid;
    828  1.27    itojun 
    829  1.14    itojun 		for (cur = *res; cur; cur = cur->ai_next) {
    830  1.14    itojun 			if (cur->ai_family != AF_INET6)
    831  1.14    itojun 				continue;
    832  1.36  christos 			sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;
    833  1.60    itojun 			if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
    834  1.27    itojun 				free(hostname2);
    835  1.43    itojun 				return(EAI_NODATA); /* XXX: is return OK? */
    836  1.27    itojun 			}
    837  1.27    itojun 			sin6->sin6_scope_id = scopeid;
    838  1.14    itojun 		}
    839   1.1    itojun 	}
    840  1.14    itojun 
    841  1.14    itojun 	free(hostname2);
    842  1.14    itojun 
    843   1.1    itojun 	return error;
    844  1.14    itojun #endif
    845   1.1    itojun }
    846   1.1    itojun 
    847   1.1    itojun static int
    848  1.70  christos get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)
    849   1.1    itojun {
    850  1.51     lukem 
    851  1.51     lukem 	_DIAGASSERT(pai != NULL);
    852  1.51     lukem 	_DIAGASSERT(ai != NULL);
    853  1.51     lukem 	_DIAGASSERT(str != NULL);
    854  1.51     lukem 
    855  1.14    itojun 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
    856  1.66    itojun 		ai->ai_canonname = strdup(str);
    857  1.14    itojun 		if (ai->ai_canonname == NULL)
    858  1.14    itojun 			return EAI_MEMORY;
    859  1.14    itojun 	}
    860  1.14    itojun 	return 0;
    861  1.14    itojun }
    862   1.1    itojun 
    863  1.93    tsarna struct addrinfo *
    864  1.93    tsarna allocaddrinfo(socklen_t addrlen)
    865  1.93    tsarna {
    866  1.93    tsarna 	struct addrinfo *ai;
    867  1.93    tsarna 
    868  1.93    tsarna 	ai = calloc(sizeof(struct addrinfo) + addrlen, 1);
    869  1.93    tsarna 	if (ai) {
    870  1.93    tsarna 		ai->ai_addr = (void *)(ai+1);
    871  1.93    tsarna 		ai->ai_addrlen = ai->ai_addr->sa_len = addrlen;
    872  1.93    tsarna 	}
    873  1.93    tsarna 
    874  1.93    tsarna 	return ai;
    875  1.93    tsarna }
    876  1.93    tsarna 
    877  1.14    itojun static struct addrinfo *
    878  1.70  christos get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)
    879  1.14    itojun {
    880  1.14    itojun 	char *p;
    881  1.14    itojun 	struct addrinfo *ai;
    882  1.93    tsarna 	struct sockaddr *save;
    883  1.12     lukem 
    884  1.51     lukem 	_DIAGASSERT(pai != NULL);
    885  1.51     lukem 	_DIAGASSERT(afd != NULL);
    886  1.51     lukem 	_DIAGASSERT(addr != NULL);
    887  1.51     lukem 
    888  1.93    tsarna 	ai = allocaddrinfo((socklen_t)afd->a_socklen);
    889  1.14    itojun 	if (ai == NULL)
    890  1.14    itojun 		return NULL;
    891  1.14    itojun 
    892  1.93    tsarna         save = ai->ai_addr;
    893  1.14    itojun 	memcpy(ai, pai, sizeof(struct addrinfo));
    894  1.93    tsarna 
    895  1.93    tsarna         /* since we just overwrote all of ai, we have
    896  1.93    tsarna            to restore ai_addr and ai_addrlen */
    897  1.93    tsarna         ai->ai_addr = save;
    898  1.93    tsarna         ai->ai_addrlen = (socklen_t)afd->a_socklen;
    899  1.93    tsarna 
    900  1.14    itojun 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
    901  1.36  christos 	p = (char *)(void *)(ai->ai_addr);
    902  1.20   mycroft 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
    903  1.14    itojun 	return ai;
    904  1.14    itojun }
    905   1.1    itojun 
    906  1.14    itojun static int
    907  1.89  christos get_portmatch(const struct addrinfo *ai, const char *servname,
    908  1.89  christos     struct servent_data *svd)
    909  1.14    itojun {
    910   1.4    itojun 
    911  1.51     lukem 	_DIAGASSERT(ai != NULL);
    912  1.51     lukem 	/* servname may be NULL */
    913  1.51     lukem 
    914  1.89  christos 	return get_port(ai, servname, 1, svd);
    915   1.1    itojun }
    916   1.1    itojun 
    917   1.1    itojun static int
    918  1.89  christos get_port(const struct addrinfo *ai, const char *servname, int matchonly,
    919  1.89  christos     struct servent_data *svd)
    920   1.1    itojun {
    921  1.14    itojun 	const char *proto;
    922  1.14    itojun 	struct servent *sp;
    923  1.14    itojun 	int port;
    924  1.14    itojun 	int allownumeric;
    925  1.12     lukem 
    926  1.51     lukem 	_DIAGASSERT(ai != NULL);
    927  1.51     lukem 	/* servname may be NULL */
    928  1.51     lukem 
    929  1.14    itojun 	if (servname == NULL)
    930  1.14    itojun 		return 0;
    931  1.23    itojun 	switch (ai->ai_family) {
    932  1.23    itojun 	case AF_INET:
    933  1.27    itojun #ifdef AF_INET6
    934  1.23    itojun 	case AF_INET6:
    935  1.27    itojun #endif
    936  1.23    itojun 		break;
    937  1.23    itojun 	default:
    938  1.14    itojun 		return 0;
    939  1.23    itojun 	}
    940   1.1    itojun 
    941  1.14    itojun 	switch (ai->ai_socktype) {
    942  1.14    itojun 	case SOCK_RAW:
    943  1.14    itojun 		return EAI_SERVICE;
    944  1.14    itojun 	case SOCK_DGRAM:
    945  1.14    itojun 	case SOCK_STREAM:
    946  1.14    itojun 		allownumeric = 1;
    947  1.14    itojun 		break;
    948  1.14    itojun 	case ANY:
    949  1.92       tls 		/*
    950  1.92       tls 		 * This was 0.  It is now 1 so that queries specifying
    951  1.92       tls 		 * a NULL hint, or hint without socktype (but, hopefully,
    952  1.92       tls 		 * with protocol) and numeric address actually work.
    953  1.92       tls 		 */
    954  1.92       tls 		allownumeric = 1;
    955  1.14    itojun 		break;
    956  1.14    itojun 	default:
    957  1.14    itojun 		return EAI_SOCKTYPE;
    958   1.1    itojun 	}
    959  1.14    itojun 
    960  1.67    itojun 	port = str2number(servname);
    961  1.67    itojun 	if (port >= 0) {
    962  1.14    itojun 		if (!allownumeric)
    963  1.14    itojun 			return EAI_SERVICE;
    964  1.14    itojun 		if (port < 0 || port > 65535)
    965  1.14    itojun 			return EAI_SERVICE;
    966  1.58    itojun 		port = htons(port);
    967  1.14    itojun 	} else {
    968  1.84  christos 		struct servent sv;
    969  1.67    itojun 		if (ai->ai_flags & AI_NUMERICSERV)
    970  1.67    itojun 			return EAI_NONAME;
    971  1.67    itojun 
    972  1.14    itojun 		switch (ai->ai_socktype) {
    973  1.14    itojun 		case SOCK_DGRAM:
    974  1.14    itojun 			proto = "udp";
    975   1.1    itojun 			break;
    976  1.14    itojun 		case SOCK_STREAM:
    977  1.14    itojun 			proto = "tcp";
    978   1.1    itojun 			break;
    979   1.1    itojun 		default:
    980  1.14    itojun 			proto = NULL;
    981   1.1    itojun 			break;
    982   1.1    itojun 		}
    983  1.14    itojun 
    984  1.89  christos 		sp = getservbyname_r(servname, proto, &sv, svd);
    985  1.85  christos 		if (sp == NULL)
    986  1.14    itojun 			return EAI_SERVICE;
    987  1.14    itojun 		port = sp->s_port;
    988   1.1    itojun 	}
    989   1.1    itojun 
    990  1.14    itojun 	if (!matchonly) {
    991  1.14    itojun 		switch (ai->ai_family) {
    992   1.1    itojun 		case AF_INET:
    993  1.36  christos 			((struct sockaddr_in *)(void *)
    994  1.36  christos 			    ai->ai_addr)->sin_port = port;
    995   1.1    itojun 			break;
    996   1.1    itojun #ifdef INET6
    997  1.14    itojun 		case AF_INET6:
    998  1.36  christos 			((struct sockaddr_in6 *)(void *)
    999  1.36  christos 			    ai->ai_addr)->sin6_port = port;
   1000   1.1    itojun 			break;
   1001   1.1    itojun #endif
   1002   1.1    itojun 		}
   1003  1.14    itojun 	}
   1004  1.14    itojun 
   1005  1.14    itojun 	return 0;
   1006  1.14    itojun }
   1007  1.14    itojun 
   1008  1.14    itojun static const struct afd *
   1009  1.70  christos find_afd(int af)
   1010  1.14    itojun {
   1011  1.14    itojun 	const struct afd *afd;
   1012   1.1    itojun 
   1013  1.14    itojun 	if (af == PF_UNSPEC)
   1014  1.14    itojun 		return NULL;
   1015  1.14    itojun 	for (afd = afdl; afd->a_af; afd++) {
   1016  1.14    itojun 		if (afd->a_af == af)
   1017  1.14    itojun 			return afd;
   1018   1.1    itojun 	}
   1019  1.14    itojun 	return NULL;
   1020   1.1    itojun }
   1021  1.27    itojun 
   1022  1.98  christos /*
   1023  1.98  christos  * AI_ADDRCONFIG check: Build a mask containing a bit set for each address
   1024  1.98  christos  * family configured in the system.
   1025  1.98  christos  *
   1026  1.98  christos  */
   1027  1.98  christos static int
   1028  1.98  christos addrconfig(uint64_t *mask)
   1029  1.98  christos {
   1030  1.98  christos 	struct ifaddrs *ifaddrs, *ifa;
   1031  1.98  christos 
   1032  1.98  christos 	if (getifaddrs(&ifaddrs) == -1)
   1033  1.98  christos 		return -1;
   1034  1.98  christos 
   1035  1.98  christos 	mask = 0;
   1036  1.98  christos 	for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next)
   1037  1.98  christos 		if (ifa->ifa_addr && (ifa->ifa_flags & IFF_UP)) {
   1038  1.98  christos 			_DIAGASSERT(ifa->ifa_addr->sa_family < 64);
   1039  1.98  christos 			*mask |= (uint64_t)1 << ifa->ifa_addr->sa_family;
   1040  1.98  christos 		}
   1041  1.98  christos 
   1042  1.98  christos 	freeifaddrs(ifaddrs);
   1043  1.98  christos 	return 0;
   1044  1.98  christos }
   1045  1.98  christos 
   1046  1.27    itojun #ifdef INET6
   1047  1.27    itojun /* convert a string to a scope identifier. XXX: IPv6 specific */
   1048  1.60    itojun static int
   1049  1.70  christos ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)
   1050  1.27    itojun {
   1051  1.59    itojun 	u_long lscopeid;
   1052  1.51     lukem 	struct in6_addr *a6;
   1053  1.27    itojun 	char *ep;
   1054  1.46    itojun 
   1055  1.51     lukem 	_DIAGASSERT(scope != NULL);
   1056  1.51     lukem 	_DIAGASSERT(sin6 != NULL);
   1057  1.60    itojun 	_DIAGASSERT(scopeid != NULL);
   1058  1.51     lukem 
   1059  1.51     lukem 	a6 = &sin6->sin6_addr;
   1060  1.51     lukem 
   1061  1.46    itojun 	/* empty scopeid portion is invalid */
   1062  1.46    itojun 	if (*scope == '\0')
   1063  1.46    itojun 		return -1;
   1064  1.27    itojun 
   1065  1.27    itojun 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
   1066  1.27    itojun 		/*
   1067  1.95       wiz 		 * We currently assume a one-to-one mapping between links
   1068  1.27    itojun 		 * and interfaces, so we simply use interface indices for
   1069  1.27    itojun 		 * like-local scopes.
   1070  1.27    itojun 		 */
   1071  1.60    itojun 		*scopeid = if_nametoindex(scope);
   1072  1.60    itojun 		if (*scopeid == 0)
   1073  1.27    itojun 			goto trynumeric;
   1074  1.60    itojun 		return 0;
   1075  1.27    itojun 	}
   1076  1.27    itojun 
   1077  1.27    itojun 	/* still unclear about literal, allow numeric only - placeholder */
   1078  1.27    itojun 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
   1079  1.27    itojun 		goto trynumeric;
   1080  1.27    itojun 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
   1081  1.27    itojun 		goto trynumeric;
   1082  1.27    itojun 	else
   1083  1.27    itojun 		goto trynumeric;	/* global */
   1084  1.27    itojun 
   1085  1.27    itojun 	/* try to convert to a numeric id as a last resort */
   1086  1.27    itojun   trynumeric:
   1087  1.59    itojun 	errno = 0;
   1088  1.59    itojun 	lscopeid = strtoul(scope, &ep, 10);
   1089  1.61    itojun 	*scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);
   1090  1.60    itojun 	if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)
   1091  1.60    itojun 		return 0;
   1092  1.27    itojun 	else
   1093  1.27    itojun 		return -1;
   1094  1.27    itojun }
   1095  1.42    itojun #endif
   1096  1.32    itojun 
   1097  1.32    itojun /* code duplicate with gethnamaddr.c */
   1098  1.32    itojun 
   1099  1.32    itojun static const char AskedForGot[] =
   1100  1.32    itojun 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
   1101  1.32    itojun 
   1102  1.32    itojun static struct addrinfo *
   1103  1.70  christos getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
   1104  1.70  christos     const struct addrinfo *pai)
   1105  1.32    itojun {
   1106  1.32    itojun 	struct addrinfo sentinel, *cur;
   1107  1.32    itojun 	struct addrinfo ai;
   1108  1.32    itojun 	const struct afd *afd;
   1109  1.32    itojun 	char *canonname;
   1110  1.32    itojun 	const HEADER *hp;
   1111  1.32    itojun 	const u_char *cp;
   1112  1.32    itojun 	int n;
   1113  1.32    itojun 	const u_char *eom;
   1114  1.56    itojun 	char *bp, *ep;
   1115  1.56    itojun 	int type, class, ancount, qdcount;
   1116  1.32    itojun 	int haveanswer, had_error;
   1117  1.32    itojun 	char tbuf[MAXDNAME];
   1118  1.70  christos 	int (*name_ok) (const char *);
   1119  1.32    itojun 	char hostbuf[8*1024];
   1120  1.32    itojun 
   1121  1.51     lukem 	_DIAGASSERT(answer != NULL);
   1122  1.51     lukem 	_DIAGASSERT(qname != NULL);
   1123  1.51     lukem 	_DIAGASSERT(pai != NULL);
   1124  1.51     lukem 
   1125  1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1126  1.32    itojun 	cur = &sentinel;
   1127  1.32    itojun 
   1128  1.32    itojun 	canonname = NULL;
   1129  1.32    itojun 	eom = answer->buf + anslen;
   1130  1.32    itojun 	switch (qtype) {
   1131  1.32    itojun 	case T_A:
   1132  1.32    itojun 	case T_AAAA:
   1133  1.32    itojun 	case T_ANY:	/*use T_ANY only for T_A/T_AAAA lookup*/
   1134  1.32    itojun 		name_ok = res_hnok;
   1135  1.32    itojun 		break;
   1136  1.32    itojun 	default:
   1137  1.70  christos 		return NULL;	/* XXX should be abort(); */
   1138  1.32    itojun 	}
   1139  1.32    itojun 	/*
   1140  1.32    itojun 	 * find first satisfactory answer
   1141  1.32    itojun 	 */
   1142  1.32    itojun 	hp = &answer->hdr;
   1143  1.32    itojun 	ancount = ntohs(hp->ancount);
   1144  1.32    itojun 	qdcount = ntohs(hp->qdcount);
   1145  1.32    itojun 	bp = hostbuf;
   1146  1.56    itojun 	ep = hostbuf + sizeof hostbuf;
   1147  1.32    itojun 	cp = answer->buf + HFIXEDSZ;
   1148  1.32    itojun 	if (qdcount != 1) {
   1149  1.32    itojun 		h_errno = NO_RECOVERY;
   1150  1.32    itojun 		return (NULL);
   1151  1.32    itojun 	}
   1152  1.97  christos 	n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
   1153  1.32    itojun 	if ((n < 0) || !(*name_ok)(bp)) {
   1154  1.32    itojun 		h_errno = NO_RECOVERY;
   1155  1.32    itojun 		return (NULL);
   1156  1.32    itojun 	}
   1157  1.32    itojun 	cp += n + QFIXEDSZ;
   1158  1.32    itojun 	if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
   1159  1.32    itojun 		/* res_send() has already verified that the query name is the
   1160  1.32    itojun 		 * same as the one we sent; this just gets the expanded name
   1161  1.32    itojun 		 * (i.e., with the succeeding search-domain tacked on).
   1162  1.32    itojun 		 */
   1163  1.97  christos 		n = (int)strlen(bp) + 1;		/* for the \0 */
   1164  1.32    itojun 		if (n >= MAXHOSTNAMELEN) {
   1165  1.32    itojun 			h_errno = NO_RECOVERY;
   1166  1.32    itojun 			return (NULL);
   1167  1.32    itojun 		}
   1168  1.32    itojun 		canonname = bp;
   1169  1.32    itojun 		bp += n;
   1170  1.32    itojun 		/* The qname can be abbreviated, but h_name is now absolute. */
   1171  1.32    itojun 		qname = canonname;
   1172  1.32    itojun 	}
   1173  1.32    itojun 	haveanswer = 0;
   1174  1.32    itojun 	had_error = 0;
   1175  1.32    itojun 	while (ancount-- > 0 && cp < eom && !had_error) {
   1176  1.97  christos 		n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
   1177  1.32    itojun 		if ((n < 0) || !(*name_ok)(bp)) {
   1178  1.32    itojun 			had_error++;
   1179  1.32    itojun 			continue;
   1180  1.32    itojun 		}
   1181  1.32    itojun 		cp += n;			/* name */
   1182  1.32    itojun 		type = _getshort(cp);
   1183  1.32    itojun  		cp += INT16SZ;			/* type */
   1184  1.32    itojun 		class = _getshort(cp);
   1185  1.32    itojun  		cp += INT16SZ + INT32SZ;	/* class, TTL */
   1186  1.32    itojun 		n = _getshort(cp);
   1187  1.32    itojun 		cp += INT16SZ;			/* len */
   1188  1.32    itojun 		if (class != C_IN) {
   1189  1.32    itojun 			/* XXX - debug? syslog? */
   1190  1.32    itojun 			cp += n;
   1191  1.32    itojun 			continue;		/* XXX - had_error++ ? */
   1192  1.32    itojun 		}
   1193  1.32    itojun 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
   1194  1.32    itojun 		    type == T_CNAME) {
   1195  1.97  christos 			n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
   1196  1.32    itojun 			if ((n < 0) || !(*name_ok)(tbuf)) {
   1197  1.32    itojun 				had_error++;
   1198  1.32    itojun 				continue;
   1199  1.32    itojun 			}
   1200  1.32    itojun 			cp += n;
   1201  1.32    itojun 			/* Get canonical name. */
   1202  1.97  christos 			n = (int)strlen(tbuf) + 1;	/* for the \0 */
   1203  1.56    itojun 			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
   1204  1.32    itojun 				had_error++;
   1205  1.32    itojun 				continue;
   1206  1.32    itojun 			}
   1207  1.66    itojun 			strlcpy(bp, tbuf, (size_t)(ep - bp));
   1208  1.32    itojun 			canonname = bp;
   1209  1.32    itojun 			bp += n;
   1210  1.32    itojun 			continue;
   1211  1.32    itojun 		}
   1212  1.32    itojun 		if (qtype == T_ANY) {
   1213  1.32    itojun 			if (!(type == T_A || type == T_AAAA)) {
   1214  1.32    itojun 				cp += n;
   1215  1.32    itojun 				continue;
   1216  1.32    itojun 			}
   1217  1.32    itojun 		} else if (type != qtype) {
   1218  1.88  christos 			if (type != T_KEY && type != T_SIG) {
   1219  1.88  christos 				struct syslog_data sd = SYSLOG_DATA_INIT;
   1220  1.88  christos 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
   1221  1.32    itojun 	       "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
   1222  1.32    itojun 				       qname, p_class(C_IN), p_type(qtype),
   1223  1.32    itojun 				       p_type(type));
   1224  1.88  christos 			}
   1225  1.32    itojun 			cp += n;
   1226  1.32    itojun 			continue;		/* XXX - had_error++ ? */
   1227  1.32    itojun 		}
   1228  1.32    itojun 		switch (type) {
   1229  1.32    itojun 		case T_A:
   1230  1.32    itojun 		case T_AAAA:
   1231  1.32    itojun 			if (strcasecmp(canonname, bp) != 0) {
   1232  1.88  christos 				struct syslog_data sd = SYSLOG_DATA_INIT;
   1233  1.88  christos 				syslog_r(LOG_NOTICE|LOG_AUTH, &sd,
   1234  1.32    itojun 				       AskedForGot, canonname, bp);
   1235  1.32    itojun 				cp += n;
   1236  1.32    itojun 				continue;	/* XXX - had_error++ ? */
   1237  1.32    itojun 			}
   1238  1.32    itojun 			if (type == T_A && n != INADDRSZ) {
   1239  1.32    itojun 				cp += n;
   1240  1.32    itojun 				continue;
   1241  1.32    itojun 			}
   1242  1.32    itojun 			if (type == T_AAAA && n != IN6ADDRSZ) {
   1243  1.32    itojun 				cp += n;
   1244  1.32    itojun 				continue;
   1245  1.62    itojun 			}
   1246  1.62    itojun 			if (type == T_AAAA) {
   1247  1.62    itojun 				struct in6_addr in6;
   1248  1.62    itojun 				memcpy(&in6, cp, IN6ADDRSZ);
   1249  1.62    itojun 				if (IN6_IS_ADDR_V4MAPPED(&in6)) {
   1250  1.62    itojun 					cp += n;
   1251  1.62    itojun 					continue;
   1252  1.62    itojun 				}
   1253  1.32    itojun 			}
   1254  1.32    itojun 			if (!haveanswer) {
   1255  1.32    itojun 				int nn;
   1256  1.32    itojun 
   1257  1.32    itojun 				canonname = bp;
   1258  1.97  christos 				nn = (int)strlen(bp) + 1;	/* for the \0 */
   1259  1.32    itojun 				bp += nn;
   1260  1.32    itojun 			}
   1261  1.32    itojun 
   1262  1.32    itojun 			/* don't overwrite pai */
   1263  1.32    itojun 			ai = *pai;
   1264  1.32    itojun 			ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
   1265  1.32    itojun 			afd = find_afd(ai.ai_family);
   1266  1.32    itojun 			if (afd == NULL) {
   1267  1.32    itojun 				cp += n;
   1268  1.32    itojun 				continue;
   1269  1.32    itojun 			}
   1270  1.36  christos 			cur->ai_next = get_ai(&ai, afd, (const char *)cp);
   1271  1.32    itojun 			if (cur->ai_next == NULL)
   1272  1.32    itojun 				had_error++;
   1273  1.32    itojun 			while (cur && cur->ai_next)
   1274  1.32    itojun 				cur = cur->ai_next;
   1275  1.32    itojun 			cp += n;
   1276  1.32    itojun 			break;
   1277  1.32    itojun 		default:
   1278  1.32    itojun 			abort();
   1279  1.32    itojun 		}
   1280  1.32    itojun 		if (!had_error)
   1281  1.32    itojun 			haveanswer++;
   1282  1.32    itojun 	}
   1283  1.32    itojun 	if (haveanswer) {
   1284  1.32    itojun 		if (!canonname)
   1285  1.32    itojun 			(void)get_canonname(pai, sentinel.ai_next, qname);
   1286  1.32    itojun 		else
   1287  1.32    itojun 			(void)get_canonname(pai, sentinel.ai_next, canonname);
   1288  1.32    itojun 		h_errno = NETDB_SUCCESS;
   1289  1.32    itojun 		return sentinel.ai_next;
   1290  1.32    itojun 	}
   1291  1.32    itojun 
   1292  1.32    itojun 	h_errno = NO_RECOVERY;
   1293  1.32    itojun 	return NULL;
   1294  1.32    itojun }
   1295  1.32    itojun 
   1296  1.73    tsarna #define SORTEDADDR(p)	(((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)
   1297  1.73    tsarna #define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)
   1298  1.73    tsarna 
   1299  1.73    tsarna static void
   1300  1.73    tsarna aisort(struct addrinfo *s, res_state res)
   1301  1.73    tsarna {
   1302  1.73    tsarna 	struct addrinfo head, *t, *p;
   1303  1.73    tsarna 	int i;
   1304  1.73    tsarna 
   1305  1.73    tsarna 	head.ai_next = NULL;
   1306  1.73    tsarna 	t = &head;
   1307  1.73    tsarna 
   1308  1.73    tsarna 	for (i = 0; i < res->nsort; i++) {
   1309  1.73    tsarna 		p = s;
   1310  1.73    tsarna 		while (p->ai_next) {
   1311  1.73    tsarna 			if ((p->ai_next->ai_family != AF_INET)
   1312  1.73    tsarna 			|| SORTMATCH(p, res->sort_list[i])) {
   1313  1.73    tsarna 				t->ai_next = p->ai_next;
   1314  1.73    tsarna 				t = t->ai_next;
   1315  1.73    tsarna 				p->ai_next = p->ai_next->ai_next;
   1316  1.73    tsarna 			} else {
   1317  1.73    tsarna 				p = p->ai_next;
   1318  1.73    tsarna 			}
   1319  1.73    tsarna 		}
   1320  1.73    tsarna 	}
   1321  1.73    tsarna 
   1322  1.73    tsarna 	/* add rest of list and reset s to the new list*/
   1323  1.73    tsarna 	t->ai_next = s->ai_next;
   1324  1.73    tsarna 	s->ai_next = head.ai_next;
   1325  1.73    tsarna }
   1326  1.73    tsarna 
   1327  1.32    itojun /*ARGSUSED*/
   1328  1.32    itojun static int
   1329  1.70  christos _dns_getaddrinfo(void *rv, void	*cb_data, va_list ap)
   1330  1.32    itojun {
   1331  1.32    itojun 	struct addrinfo *ai;
   1332  1.63    itojun 	querybuf *buf, *buf2;
   1333  1.32    itojun 	const char *name;
   1334  1.32    itojun 	const struct addrinfo *pai;
   1335  1.32    itojun 	struct addrinfo sentinel, *cur;
   1336  1.32    itojun 	struct res_target q, q2;
   1337  1.73    tsarna 	res_state res;
   1338  1.32    itojun 
   1339  1.32    itojun 	name = va_arg(ap, char *);
   1340  1.32    itojun 	pai = va_arg(ap, const struct addrinfo *);
   1341  1.32    itojun 
   1342  1.78       mjl 	memset(&q, 0, sizeof(q));
   1343  1.32    itojun 	memset(&q2, 0, sizeof(q2));
   1344  1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1345  1.32    itojun 	cur = &sentinel;
   1346  1.32    itojun 
   1347  1.63    itojun 	buf = malloc(sizeof(*buf));
   1348  1.63    itojun 	if (buf == NULL) {
   1349  1.63    itojun 		h_errno = NETDB_INTERNAL;
   1350  1.63    itojun 		return NS_NOTFOUND;
   1351  1.63    itojun 	}
   1352  1.63    itojun 	buf2 = malloc(sizeof(*buf2));
   1353  1.63    itojun 	if (buf2 == NULL) {
   1354  1.63    itojun 		free(buf);
   1355  1.63    itojun 		h_errno = NETDB_INTERNAL;
   1356  1.63    itojun 		return NS_NOTFOUND;
   1357  1.63    itojun 	}
   1358  1.63    itojun 
   1359  1.32    itojun 	switch (pai->ai_family) {
   1360  1.32    itojun 	case AF_UNSPEC:
   1361  1.32    itojun 		/* prefer IPv6 */
   1362  1.52    itojun 		q.name = name;
   1363  1.43    itojun 		q.qclass = C_IN;
   1364  1.43    itojun 		q.qtype = T_AAAA;
   1365  1.63    itojun 		q.answer = buf->buf;
   1366  1.63    itojun 		q.anslen = sizeof(buf->buf);
   1367  1.32    itojun 		q.next = &q2;
   1368  1.52    itojun 		q2.name = name;
   1369  1.43    itojun 		q2.qclass = C_IN;
   1370  1.43    itojun 		q2.qtype = T_A;
   1371  1.63    itojun 		q2.answer = buf2->buf;
   1372  1.63    itojun 		q2.anslen = sizeof(buf2->buf);
   1373  1.32    itojun 		break;
   1374  1.32    itojun 	case AF_INET:
   1375  1.52    itojun 		q.name = name;
   1376  1.43    itojun 		q.qclass = C_IN;
   1377  1.43    itojun 		q.qtype = T_A;
   1378  1.63    itojun 		q.answer = buf->buf;
   1379  1.63    itojun 		q.anslen = sizeof(buf->buf);
   1380  1.32    itojun 		break;
   1381  1.32    itojun 	case AF_INET6:
   1382  1.52    itojun 		q.name = name;
   1383  1.43    itojun 		q.qclass = C_IN;
   1384  1.43    itojun 		q.qtype = T_AAAA;
   1385  1.63    itojun 		q.answer = buf->buf;
   1386  1.63    itojun 		q.anslen = sizeof(buf->buf);
   1387  1.32    itojun 		break;
   1388  1.32    itojun 	default:
   1389  1.63    itojun 		free(buf);
   1390  1.63    itojun 		free(buf2);
   1391  1.32    itojun 		return NS_UNAVAIL;
   1392  1.32    itojun 	}
   1393  1.73    tsarna 
   1394  1.73    tsarna 	res = __res_get_state();
   1395  1.73    tsarna 	if (res == NULL) {
   1396  1.73    tsarna 		free(buf);
   1397  1.73    tsarna 		free(buf2);
   1398  1.73    tsarna 		return NS_NOTFOUND;
   1399  1.73    tsarna 	}
   1400  1.73    tsarna 
   1401  1.73    tsarna 	if (res_searchN(name, &q, res) < 0) {
   1402  1.73    tsarna 		__res_put_state(res);
   1403  1.63    itojun 		free(buf);
   1404  1.63    itojun 		free(buf2);
   1405  1.32    itojun 		return NS_NOTFOUND;
   1406  1.63    itojun 	}
   1407  1.63    itojun 	ai = getanswer(buf, q.n, q.name, q.qtype, pai);
   1408  1.32    itojun 	if (ai) {
   1409  1.32    itojun 		cur->ai_next = ai;
   1410  1.32    itojun 		while (cur && cur->ai_next)
   1411  1.32    itojun 			cur = cur->ai_next;
   1412  1.32    itojun 	}
   1413  1.32    itojun 	if (q.next) {
   1414  1.63    itojun 		ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);
   1415  1.32    itojun 		if (ai)
   1416  1.32    itojun 			cur->ai_next = ai;
   1417  1.32    itojun 	}
   1418  1.63    itojun 	free(buf);
   1419  1.63    itojun 	free(buf2);
   1420  1.73    tsarna 	if (sentinel.ai_next == NULL) {
   1421  1.73    tsarna 		__res_put_state(res);
   1422  1.32    itojun 		switch (h_errno) {
   1423  1.32    itojun 		case HOST_NOT_FOUND:
   1424  1.32    itojun 			return NS_NOTFOUND;
   1425  1.32    itojun 		case TRY_AGAIN:
   1426  1.32    itojun 			return NS_TRYAGAIN;
   1427  1.32    itojun 		default:
   1428  1.32    itojun 			return NS_UNAVAIL;
   1429  1.32    itojun 		}
   1430  1.73    tsarna 	}
   1431  1.73    tsarna 
   1432  1.73    tsarna 	if (res->nsort)
   1433  1.73    tsarna 		aisort(&sentinel, res);
   1434  1.73    tsarna 
   1435  1.73    tsarna 	__res_put_state(res);
   1436  1.73    tsarna 
   1437  1.32    itojun 	*((struct addrinfo **)rv) = sentinel.ai_next;
   1438  1.32    itojun 	return NS_SUCCESS;
   1439  1.32    itojun }
   1440  1.32    itojun 
   1441  1.32    itojun static void
   1442  1.70  christos _sethtent(FILE **hostf)
   1443  1.32    itojun {
   1444  1.51     lukem 
   1445  1.70  christos 	if (!*hostf)
   1446  1.96  christos 		*hostf = fopen(_PATH_HOSTS, "re");
   1447  1.32    itojun 	else
   1448  1.70  christos 		rewind(*hostf);
   1449  1.32    itojun }
   1450  1.32    itojun 
   1451  1.32    itojun static void
   1452  1.70  christos _endhtent(FILE **hostf)
   1453  1.32    itojun {
   1454  1.51     lukem 
   1455  1.70  christos 	if (*hostf) {
   1456  1.70  christos 		(void) fclose(*hostf);
   1457  1.70  christos 		*hostf = NULL;
   1458  1.32    itojun 	}
   1459  1.32    itojun }
   1460  1.32    itojun 
   1461  1.32    itojun static struct addrinfo *
   1462  1.70  christos _gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)
   1463  1.32    itojun {
   1464  1.32    itojun 	char *p;
   1465  1.48    itojun 	char *cp, *tname, *cname;
   1466  1.32    itojun 	struct addrinfo hints, *res0, *res;
   1467  1.32    itojun 	int error;
   1468  1.32    itojun 	const char *addr;
   1469  1.32    itojun 	char hostbuf[8*1024];
   1470  1.32    itojun 
   1471  1.51     lukem 	_DIAGASSERT(name != NULL);
   1472  1.51     lukem 	_DIAGASSERT(pai != NULL);
   1473  1.51     lukem 
   1474  1.96  christos 	if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re")))
   1475  1.32    itojun 		return (NULL);
   1476  1.32    itojun  again:
   1477  1.97  christos 	if (!(p = fgets(hostbuf, (int)sizeof hostbuf, *hostf)))
   1478  1.32    itojun 		return (NULL);
   1479  1.32    itojun 	if (*p == '#')
   1480  1.32    itojun 		goto again;
   1481  1.32    itojun 	if (!(cp = strpbrk(p, "#\n")))
   1482  1.32    itojun 		goto again;
   1483  1.32    itojun 	*cp = '\0';
   1484  1.32    itojun 	if (!(cp = strpbrk(p, " \t")))
   1485  1.32    itojun 		goto again;
   1486  1.32    itojun 	*cp++ = '\0';
   1487  1.32    itojun 	addr = p;
   1488  1.32    itojun 	/* if this is not something we're looking for, skip it. */
   1489  1.48    itojun 	cname = NULL;
   1490  1.32    itojun 	while (cp && *cp) {
   1491  1.32    itojun 		if (*cp == ' ' || *cp == '\t') {
   1492  1.32    itojun 			cp++;
   1493  1.32    itojun 			continue;
   1494  1.32    itojun 		}
   1495  1.48    itojun 		if (!cname)
   1496  1.48    itojun 			cname = cp;
   1497  1.32    itojun 		tname = cp;
   1498  1.32    itojun 		if ((cp = strpbrk(cp, " \t")) != NULL)
   1499  1.32    itojun 			*cp++ = '\0';
   1500  1.32    itojun 		if (strcasecmp(name, tname) == 0)
   1501  1.32    itojun 			goto found;
   1502  1.32    itojun 	}
   1503  1.32    itojun 	goto again;
   1504  1.32    itojun 
   1505  1.32    itojun found:
   1506  1.32    itojun 	hints = *pai;
   1507  1.32    itojun 	hints.ai_flags = AI_NUMERICHOST;
   1508  1.32    itojun 	error = getaddrinfo(addr, NULL, &hints, &res0);
   1509  1.32    itojun 	if (error)
   1510  1.32    itojun 		goto again;
   1511  1.32    itojun 	for (res = res0; res; res = res->ai_next) {
   1512  1.32    itojun 		/* cover it up */
   1513  1.32    itojun 		res->ai_flags = pai->ai_flags;
   1514  1.32    itojun 
   1515  1.32    itojun 		if (pai->ai_flags & AI_CANONNAME) {
   1516  1.48    itojun 			if (get_canonname(pai, res, cname) != 0) {
   1517  1.32    itojun 				freeaddrinfo(res0);
   1518  1.32    itojun 				goto again;
   1519  1.32    itojun 			}
   1520  1.32    itojun 		}
   1521  1.32    itojun 	}
   1522  1.32    itojun 	return res0;
   1523  1.32    itojun }
   1524  1.32    itojun 
   1525  1.32    itojun /*ARGSUSED*/
   1526  1.32    itojun static int
   1527  1.70  christos _files_getaddrinfo(void *rv, void *cb_data, va_list ap)
   1528  1.32    itojun {
   1529  1.32    itojun 	const char *name;
   1530  1.32    itojun 	const struct addrinfo *pai;
   1531  1.32    itojun 	struct addrinfo sentinel, *cur;
   1532  1.32    itojun 	struct addrinfo *p;
   1533  1.70  christos #ifndef _REENTRANT
   1534  1.70  christos 	static
   1535  1.70  christos #endif
   1536  1.70  christos 	FILE *hostf = NULL;
   1537  1.32    itojun 
   1538  1.32    itojun 	name = va_arg(ap, char *);
   1539  1.91     lukem 	pai = va_arg(ap, const struct addrinfo *);
   1540  1.32    itojun 
   1541  1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1542  1.32    itojun 	cur = &sentinel;
   1543  1.32    itojun 
   1544  1.70  christos 	_sethtent(&hostf);
   1545  1.70  christos 	while ((p = _gethtent(&hostf, name, pai)) != NULL) {
   1546  1.32    itojun 		cur->ai_next = p;
   1547  1.32    itojun 		while (cur && cur->ai_next)
   1548  1.32    itojun 			cur = cur->ai_next;
   1549  1.32    itojun 	}
   1550  1.70  christos 	_endhtent(&hostf);
   1551  1.32    itojun 
   1552  1.32    itojun 	*((struct addrinfo **)rv) = sentinel.ai_next;
   1553  1.32    itojun 	if (sentinel.ai_next == NULL)
   1554  1.32    itojun 		return NS_NOTFOUND;
   1555  1.32    itojun 	return NS_SUCCESS;
   1556  1.32    itojun }
   1557  1.32    itojun 
   1558  1.32    itojun #ifdef YP
   1559  1.32    itojun /*ARGSUSED*/
   1560  1.32    itojun static struct addrinfo *
   1561  1.70  christos _yphostent(char *line, const struct addrinfo *pai)
   1562  1.32    itojun {
   1563  1.32    itojun 	struct addrinfo sentinel, *cur;
   1564  1.32    itojun 	struct addrinfo hints, *res, *res0;
   1565  1.32    itojun 	int error;
   1566  1.51     lukem 	char *p;
   1567  1.32    itojun 	const char *addr, *canonname;
   1568  1.32    itojun 	char *nextline;
   1569  1.32    itojun 	char *cp;
   1570  1.32    itojun 
   1571  1.51     lukem 	_DIAGASSERT(line != NULL);
   1572  1.51     lukem 	_DIAGASSERT(pai != NULL);
   1573  1.51     lukem 
   1574  1.51     lukem 	p = line;
   1575  1.32    itojun 	addr = canonname = NULL;
   1576  1.32    itojun 
   1577  1.36  christos 	memset(&sentinel, 0, sizeof(sentinel));
   1578  1.36  christos 	cur = &sentinel;
   1579  1.36  christos 
   1580  1.32    itojun nextline:
   1581  1.32    itojun 	/* terminate line */
   1582  1.32    itojun 	cp = strchr(p, '\n');
   1583  1.32    itojun 	if (cp) {
   1584  1.32    itojun 		*cp++ = '\0';
   1585  1.32    itojun 		nextline = cp;
   1586  1.32    itojun 	} else
   1587  1.32    itojun 		nextline = NULL;
   1588  1.32    itojun 
   1589  1.32    itojun 	cp = strpbrk(p, " \t");
   1590  1.32    itojun 	if (cp == NULL) {
   1591  1.32    itojun 		if (canonname == NULL)
   1592  1.32    itojun 			return (NULL);
   1593  1.32    itojun 		else
   1594  1.32    itojun 			goto done;
   1595  1.32    itojun 	}
   1596  1.32    itojun 	*cp++ = '\0';
   1597  1.32    itojun 
   1598  1.32    itojun 	addr = p;
   1599  1.32    itojun 
   1600  1.32    itojun 	while (cp && *cp) {
   1601  1.32    itojun 		if (*cp == ' ' || *cp == '\t') {
   1602  1.32    itojun 			cp++;
   1603  1.32    itojun 			continue;
   1604  1.32    itojun 		}
   1605  1.32    itojun 		if (!canonname)
   1606  1.32    itojun 			canonname = cp;
   1607  1.32    itojun 		if ((cp = strpbrk(cp, " \t")) != NULL)
   1608  1.32    itojun 			*cp++ = '\0';
   1609  1.32    itojun 	}
   1610  1.32    itojun 
   1611  1.32    itojun 	hints = *pai;
   1612  1.32    itojun 	hints.ai_flags = AI_NUMERICHOST;
   1613  1.32    itojun 	error = getaddrinfo(addr, NULL, &hints, &res0);
   1614  1.32    itojun 	if (error == 0) {
   1615  1.32    itojun 		for (res = res0; res; res = res->ai_next) {
   1616  1.32    itojun 			/* cover it up */
   1617  1.32    itojun 			res->ai_flags = pai->ai_flags;
   1618  1.32    itojun 
   1619  1.32    itojun 			if (pai->ai_flags & AI_CANONNAME)
   1620  1.32    itojun 				(void)get_canonname(pai, res, canonname);
   1621  1.32    itojun 		}
   1622  1.37    itojun 	} else
   1623  1.37    itojun 		res0 = NULL;
   1624  1.32    itojun 	if (res0) {
   1625  1.32    itojun 		cur->ai_next = res0;
   1626  1.80  christos 		while (cur->ai_next)
   1627  1.32    itojun 			cur = cur->ai_next;
   1628  1.32    itojun 	}
   1629  1.32    itojun 
   1630  1.32    itojun 	if (nextline) {
   1631  1.32    itojun 		p = nextline;
   1632  1.32    itojun 		goto nextline;
   1633  1.32    itojun 	}
   1634  1.32    itojun 
   1635  1.32    itojun done:
   1636  1.32    itojun 	return sentinel.ai_next;
   1637  1.32    itojun }
   1638  1.32    itojun 
   1639  1.32    itojun /*ARGSUSED*/
   1640  1.32    itojun static int
   1641  1.70  christos _yp_getaddrinfo(void *rv, void *cb_data, va_list ap)
   1642  1.32    itojun {
   1643  1.32    itojun 	struct addrinfo sentinel, *cur;
   1644  1.32    itojun 	struct addrinfo *ai = NULL;
   1645  1.72  christos 	char *ypbuf;
   1646  1.72  christos 	int ypbuflen, r;
   1647  1.32    itojun 	const char *name;
   1648  1.32    itojun 	const struct addrinfo *pai;
   1649  1.72  christos 	char *ypdomain;
   1650  1.72  christos 
   1651  1.72  christos 	if (_yp_check(&ypdomain) == 0)
   1652  1.72  christos 		return NS_UNAVAIL;
   1653  1.32    itojun 
   1654  1.32    itojun 	name = va_arg(ap, char *);
   1655  1.32    itojun 	pai = va_arg(ap, const struct addrinfo *);
   1656  1.32    itojun 
   1657  1.32    itojun 	memset(&sentinel, 0, sizeof(sentinel));
   1658  1.32    itojun 	cur = &sentinel;
   1659  1.32    itojun 
   1660  1.32    itojun 	/* hosts.byname is only for IPv4 (Solaris8) */
   1661  1.33    itojun 	if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {
   1662  1.72  christos 		r = yp_match(ypdomain, "hosts.byname", name,
   1663  1.72  christos 			(int)strlen(name), &ypbuf, &ypbuflen);
   1664  1.33    itojun 		if (r == 0) {
   1665  1.33    itojun 			struct addrinfo ai4;
   1666  1.33    itojun 
   1667  1.33    itojun 			ai4 = *pai;
   1668  1.33    itojun 			ai4.ai_family = AF_INET;
   1669  1.72  christos 			ai = _yphostent(ypbuf, &ai4);
   1670  1.33    itojun 			if (ai) {
   1671  1.33    itojun 				cur->ai_next = ai;
   1672  1.33    itojun 				while (cur && cur->ai_next)
   1673  1.33    itojun 					cur = cur->ai_next;
   1674  1.33    itojun 			}
   1675  1.32    itojun 		}
   1676  1.72  christos 		free(ypbuf);
   1677  1.32    itojun 	}
   1678  1.32    itojun 
   1679  1.32    itojun 	/* ipnodes.byname can hold both IPv4/v6 */
   1680  1.72  christos 	r = yp_match(ypdomain, "ipnodes.byname", name,
   1681  1.72  christos 		(int)strlen(name), &ypbuf, &ypbuflen);
   1682  1.32    itojun 	if (r == 0) {
   1683  1.72  christos 		ai = _yphostent(ypbuf, pai);
   1684  1.72  christos 		if (ai)
   1685  1.32    itojun 			cur->ai_next = ai;
   1686  1.72  christos 		free(ypbuf);
   1687  1.32    itojun 	}
   1688  1.32    itojun 
   1689  1.32    itojun 	if (sentinel.ai_next == NULL) {
   1690  1.32    itojun 		h_errno = HOST_NOT_FOUND;
   1691  1.32    itojun 		return NS_NOTFOUND;
   1692  1.32    itojun 	}
   1693  1.32    itojun 	*((struct addrinfo **)rv) = sentinel.ai_next;
   1694  1.32    itojun 	return NS_SUCCESS;
   1695  1.32    itojun }
   1696  1.32    itojun #endif
   1697  1.32    itojun 
   1698  1.32    itojun /* resolver logic */
   1699  1.32    itojun 
   1700  1.32    itojun /*
   1701  1.32    itojun  * Formulate a normal query, send, and await answer.
   1702  1.32    itojun  * Returned answer is placed in supplied buffer "answer".
   1703  1.32    itojun  * Perform preliminary check of answer, returning success only
   1704  1.32    itojun  * if no error is indicated and the answer count is nonzero.
   1705  1.32    itojun  * Return the size of the response on success, -1 on error.
   1706  1.32    itojun  * Error number is left in h_errno.
   1707  1.32    itojun  *
   1708  1.32    itojun  * Caller must parse answer and determine whether it answers the question.
   1709  1.32    itojun  */
   1710  1.32    itojun static int
   1711  1.70  christos res_queryN(const char *name, /* domain name */ struct res_target *target,
   1712  1.70  christos     res_state res)
   1713  1.32    itojun {
   1714  1.32    itojun 	u_char buf[MAXPACKET];
   1715  1.32    itojun 	HEADER *hp;
   1716  1.32    itojun 	int n;
   1717  1.32    itojun 	struct res_target *t;
   1718  1.32    itojun 	int rcode;
   1719  1.32    itojun 	int ancount;
   1720  1.32    itojun 
   1721  1.51     lukem 	_DIAGASSERT(name != NULL);
   1722  1.51     lukem 	/* XXX: target may be NULL??? */
   1723  1.51     lukem 
   1724  1.32    itojun 	rcode = NOERROR;
   1725  1.32    itojun 	ancount = 0;
   1726  1.32    itojun 
   1727  1.32    itojun 	for (t = target; t; t = t->next) {
   1728  1.32    itojun 		int class, type;
   1729  1.32    itojun 		u_char *answer;
   1730  1.32    itojun 		int anslen;
   1731  1.32    itojun 
   1732  1.32    itojun 		hp = (HEADER *)(void *)t->answer;
   1733  1.32    itojun 		hp->rcode = NOERROR;	/* default */
   1734  1.32    itojun 
   1735  1.32    itojun 		/* make it easier... */
   1736  1.43    itojun 		class = t->qclass;
   1737  1.43    itojun 		type = t->qtype;
   1738  1.32    itojun 		answer = t->answer;
   1739  1.32    itojun 		anslen = t->anslen;
   1740  1.32    itojun #ifdef DEBUG
   1741  1.70  christos 		if (res->options & RES_DEBUG)
   1742  1.70  christos 			printf(";; res_nquery(%s, %d, %d)\n", name, class, type);
   1743  1.32    itojun #endif
   1744  1.32    itojun 
   1745  1.70  christos 		n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,
   1746  1.97  christos 		    buf, (int)sizeof(buf));
   1747  1.47    itojun #ifdef RES_USE_EDNS0
   1748  1.70  christos 		if (n > 0 && (res->options & RES_USE_EDNS0) != 0)
   1749  1.97  christos 			n = res_nopt(res, n, buf, (int)sizeof(buf), anslen);
   1750  1.47    itojun #endif
   1751  1.32    itojun 		if (n <= 0) {
   1752  1.32    itojun #ifdef DEBUG
   1753  1.70  christos 			if (res->options & RES_DEBUG)
   1754  1.70  christos 				printf(";; res_nquery: mkquery failed\n");
   1755  1.32    itojun #endif
   1756  1.32    itojun 			h_errno = NO_RECOVERY;
   1757  1.70  christos 			return n;
   1758  1.32    itojun 		}
   1759  1.70  christos 		n = res_nsend(res, buf, n, answer, anslen);
   1760  1.35    itojun #if 0
   1761  1.32    itojun 		if (n < 0) {
   1762  1.32    itojun #ifdef DEBUG
   1763  1.70  christos 			if (res->options & RES_DEBUG)
   1764  1.32    itojun 				printf(";; res_query: send error\n");
   1765  1.32    itojun #endif
   1766  1.32    itojun 			h_errno = TRY_AGAIN;
   1767  1.70  christos 			return n;
   1768  1.32    itojun 		}
   1769  1.35    itojun #endif
   1770  1.32    itojun 
   1771  1.35    itojun 		if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
   1772  1.32    itojun 			rcode = hp->rcode;	/* record most recent error */
   1773  1.32    itojun #ifdef DEBUG
   1774  1.70  christos 			if (res->options & RES_DEBUG)
   1775  1.57    itojun 				printf(";; rcode = %u, ancount=%u\n", hp->rcode,
   1776  1.32    itojun 				    ntohs(hp->ancount));
   1777  1.32    itojun #endif
   1778  1.32    itojun 			continue;
   1779  1.32    itojun 		}
   1780  1.32    itojun 
   1781  1.32    itojun 		ancount += ntohs(hp->ancount);
   1782  1.32    itojun 
   1783  1.32    itojun 		t->n = n;
   1784  1.32    itojun 	}
   1785  1.32    itojun 
   1786  1.32    itojun 	if (ancount == 0) {
   1787  1.32    itojun 		switch (rcode) {
   1788  1.32    itojun 		case NXDOMAIN:
   1789  1.32    itojun 			h_errno = HOST_NOT_FOUND;
   1790  1.32    itojun 			break;
   1791  1.32    itojun 		case SERVFAIL:
   1792  1.32    itojun 			h_errno = TRY_AGAIN;
   1793  1.32    itojun 			break;
   1794  1.32    itojun 		case NOERROR:
   1795  1.32    itojun 			h_errno = NO_DATA;
   1796  1.32    itojun 			break;
   1797  1.32    itojun 		case FORMERR:
   1798  1.32    itojun 		case NOTIMP:
   1799  1.32    itojun 		case REFUSED:
   1800  1.32    itojun 		default:
   1801  1.32    itojun 			h_errno = NO_RECOVERY;
   1802  1.32    itojun 			break;
   1803  1.32    itojun 		}
   1804  1.70  christos 		return -1;
   1805  1.32    itojun 	}
   1806  1.70  christos 	return ancount;
   1807  1.32    itojun }
   1808  1.32    itojun 
   1809  1.32    itojun /*
   1810  1.32    itojun  * Formulate a normal query, send, and retrieve answer in supplied buffer.
   1811  1.32    itojun  * Return the size of the response on success, -1 on error.
   1812  1.32    itojun  * If enabled, implement search rules until answer or unrecoverable failure
   1813  1.32    itojun  * is detected.  Error code, if any, is left in h_errno.
   1814  1.32    itojun  */
   1815  1.32    itojun static int
   1816  1.73    tsarna res_searchN(const char *name, struct res_target *target, res_state res)
   1817  1.32    itojun {
   1818  1.32    itojun 	const char *cp, * const *domain;
   1819  1.51     lukem 	HEADER *hp;
   1820  1.32    itojun 	u_int dots;
   1821  1.32    itojun 	int trailing_dot, ret, saved_herrno;
   1822  1.32    itojun 	int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
   1823  1.71  christos 
   1824  1.51     lukem 	_DIAGASSERT(name != NULL);
   1825  1.51     lukem 	_DIAGASSERT(target != NULL);
   1826  1.51     lukem 
   1827  1.51     lukem 	hp = (HEADER *)(void *)target->answer;	/*XXX*/
   1828  1.51     lukem 
   1829  1.32    itojun 	errno = 0;
   1830  1.32    itojun 	h_errno = HOST_NOT_FOUND;	/* default, if we never query */
   1831  1.32    itojun 	dots = 0;
   1832  1.32    itojun 	for (cp = name; *cp; cp++)
   1833  1.32    itojun 		dots += (*cp == '.');
   1834  1.32    itojun 	trailing_dot = 0;
   1835  1.32    itojun 	if (cp > name && *--cp == '.')
   1836  1.32    itojun 		trailing_dot++;
   1837  1.32    itojun 
   1838  1.32    itojun 	/*
   1839  1.95       wiz 	 * if there aren't any dots, it could be a user-level alias
   1840  1.32    itojun 	 */
   1841  1.70  christos 	if (!dots && (cp = __hostalias(name)) != NULL) {
   1842  1.70  christos 		ret = res_queryN(cp, target, res);
   1843  1.70  christos 		return ret;
   1844  1.70  christos 	}
   1845  1.32    itojun 
   1846  1.32    itojun 	/*
   1847  1.32    itojun 	 * If there are dots in the name already, let's just give it a try
   1848  1.32    itojun 	 * 'as is'.  The threshold can be set with the "ndots" option.
   1849  1.32    itojun 	 */
   1850  1.32    itojun 	saved_herrno = -1;
   1851  1.70  christos 	if (dots >= res->ndots) {
   1852  1.70  christos 		ret = res_querydomainN(name, NULL, target, res);
   1853  1.73    tsarna 		if (ret > 0)
   1854  1.32    itojun 			return (ret);
   1855  1.32    itojun 		saved_herrno = h_errno;
   1856  1.32    itojun 		tried_as_is++;
   1857  1.32    itojun 	}
   1858  1.32    itojun 
   1859  1.32    itojun 	/*
   1860  1.32    itojun 	 * We do at least one level of search if
   1861  1.32    itojun 	 *	- there is no dot and RES_DEFNAME is set, or
   1862  1.32    itojun 	 *	- there is at least one dot, there is no trailing dot,
   1863  1.32    itojun 	 *	  and RES_DNSRCH is set.
   1864  1.32    itojun 	 */
   1865  1.70  christos 	if ((!dots && (res->options & RES_DEFNAMES)) ||
   1866  1.70  christos 	    (dots && !trailing_dot && (res->options & RES_DNSRCH))) {
   1867  1.32    itojun 		int done = 0;
   1868  1.32    itojun 
   1869  1.70  christos 		for (domain = (const char * const *)res->dnsrch;
   1870  1.32    itojun 		   *domain && !done;
   1871  1.32    itojun 		   domain++) {
   1872  1.32    itojun 
   1873  1.70  christos 			ret = res_querydomainN(name, *domain, target, res);
   1874  1.73    tsarna 			if (ret > 0)
   1875  1.70  christos 				return ret;
   1876  1.32    itojun 
   1877  1.32    itojun 			/*
   1878  1.32    itojun 			 * If no server present, give up.
   1879  1.32    itojun 			 * If name isn't found in this domain,
   1880  1.32    itojun 			 * keep trying higher domains in the search list
   1881  1.32    itojun 			 * (if that's enabled).
   1882  1.32    itojun 			 * On a NO_DATA error, keep trying, otherwise
   1883  1.32    itojun 			 * a wildcard entry of another type could keep us
   1884  1.32    itojun 			 * from finding this entry higher in the domain.
   1885  1.32    itojun 			 * If we get some other error (negative answer or
   1886  1.32    itojun 			 * server failure), then stop searching up,
   1887  1.32    itojun 			 * but try the input name below in case it's
   1888  1.32    itojun 			 * fully-qualified.
   1889  1.32    itojun 			 */
   1890  1.32    itojun 			if (errno == ECONNREFUSED) {
   1891  1.32    itojun 				h_errno = TRY_AGAIN;
   1892  1.70  christos 				return -1;
   1893  1.32    itojun 			}
   1894  1.32    itojun 
   1895  1.32    itojun 			switch (h_errno) {
   1896  1.32    itojun 			case NO_DATA:
   1897  1.32    itojun 				got_nodata++;
   1898  1.32    itojun 				/* FALLTHROUGH */
   1899  1.32    itojun 			case HOST_NOT_FOUND:
   1900  1.32    itojun 				/* keep trying */
   1901  1.32    itojun 				break;
   1902  1.32    itojun 			case TRY_AGAIN:
   1903  1.32    itojun 				if (hp->rcode == SERVFAIL) {
   1904  1.32    itojun 					/* try next search element, if any */
   1905  1.32    itojun 					got_servfail++;
   1906  1.32    itojun 					break;
   1907  1.32    itojun 				}
   1908  1.32    itojun 				/* FALLTHROUGH */
   1909  1.32    itojun 			default:
   1910  1.32    itojun 				/* anything else implies that we're done */
   1911  1.32    itojun 				done++;
   1912  1.32    itojun 			}
   1913  1.32    itojun 			/*
   1914  1.32    itojun 			 * if we got here for some reason other than DNSRCH,
   1915  1.32    itojun 			 * we only wanted one iteration of the loop, so stop.
   1916  1.32    itojun 			 */
   1917  1.70  christos 			if (!(res->options & RES_DNSRCH))
   1918  1.32    itojun 			        done++;
   1919  1.32    itojun 		}
   1920  1.32    itojun 	}
   1921  1.32    itojun 
   1922  1.32    itojun 	/*
   1923  1.32    itojun 	 * if we have not already tried the name "as is", do that now.
   1924  1.32    itojun 	 * note that we do this regardless of how many dots were in the
   1925  1.32    itojun 	 * name or whether it ends with a dot.
   1926  1.32    itojun 	 */
   1927  1.32    itojun 	if (!tried_as_is) {
   1928  1.70  christos 		ret = res_querydomainN(name, NULL, target, res);
   1929  1.73    tsarna 		if (ret > 0)
   1930  1.70  christos 			return ret;
   1931  1.32    itojun 	}
   1932  1.32    itojun 
   1933  1.32    itojun 	/*
   1934  1.32    itojun 	 * if we got here, we didn't satisfy the search.
   1935  1.32    itojun 	 * if we did an initial full query, return that query's h_errno
   1936  1.32    itojun 	 * (note that we wouldn't be here if that query had succeeded).
   1937  1.32    itojun 	 * else if we ever got a nodata, send that back as the reason.
   1938  1.32    itojun 	 * else send back meaningless h_errno, that being the one from
   1939  1.32    itojun 	 * the last DNSRCH we did.
   1940  1.32    itojun 	 */
   1941  1.32    itojun 	if (saved_herrno != -1)
   1942  1.32    itojun 		h_errno = saved_herrno;
   1943  1.32    itojun 	else if (got_nodata)
   1944  1.32    itojun 		h_errno = NO_DATA;
   1945  1.32    itojun 	else if (got_servfail)
   1946  1.32    itojun 		h_errno = TRY_AGAIN;
   1947  1.70  christos 	return -1;
   1948  1.32    itojun }
   1949  1.32    itojun 
   1950  1.32    itojun /*
   1951  1.32    itojun  * Perform a call on res_query on the concatenation of name and domain,
   1952  1.32    itojun  * removing a trailing dot from name if domain is NULL.
   1953  1.32    itojun  */
   1954  1.32    itojun static int
   1955  1.73    tsarna res_querydomainN(const char *name, const char *domain,
   1956  1.70  christos     struct res_target *target, res_state res)
   1957  1.32    itojun {
   1958  1.32    itojun 	char nbuf[MAXDNAME];
   1959  1.32    itojun 	const char *longname = nbuf;
   1960  1.32    itojun 	size_t n, d;
   1961  1.51     lukem 
   1962  1.51     lukem 	_DIAGASSERT(name != NULL);
   1963  1.51     lukem 	/* XXX: target may be NULL??? */
   1964  1.32    itojun 
   1965  1.32    itojun #ifdef DEBUG
   1966  1.70  christos 	if (res->options & RES_DEBUG)
   1967  1.32    itojun 		printf(";; res_querydomain(%s, %s)\n",
   1968  1.32    itojun 			name, domain?domain:"<Nil>");
   1969  1.32    itojun #endif
   1970  1.32    itojun 	if (domain == NULL) {
   1971  1.32    itojun 		/*
   1972  1.32    itojun 		 * Check for trailing '.';
   1973  1.32    itojun 		 * copy without '.' if present.
   1974  1.32    itojun 		 */
   1975  1.32    itojun 		n = strlen(name);
   1976  1.63    itojun 		if (n + 1 > sizeof(nbuf)) {
   1977  1.32    itojun 			h_errno = NO_RECOVERY;
   1978  1.70  christos 			return -1;
   1979  1.32    itojun 		}
   1980  1.40    itojun 		if (n > 0 && name[--n] == '.') {
   1981  1.32    itojun 			strncpy(nbuf, name, n);
   1982  1.32    itojun 			nbuf[n] = '\0';
   1983  1.32    itojun 		} else
   1984  1.32    itojun 			longname = name;
   1985  1.32    itojun 	} else {
   1986  1.32    itojun 		n = strlen(name);
   1987  1.32    itojun 		d = strlen(domain);
   1988  1.63    itojun 		if (n + 1 + d + 1 > sizeof(nbuf)) {
   1989  1.32    itojun 			h_errno = NO_RECOVERY;
   1990  1.70  christos 			return -1;
   1991  1.32    itojun 		}
   1992  1.54    itojun 		snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
   1993  1.32    itojun 	}
   1994  1.70  christos 	return res_queryN(longname, target, res);
   1995  1.32    itojun }
   1996