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