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