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