Home | History | Annotate | Line # | Download | only in net
getaddrinfo.c revision 1.15
      1 /*	$NetBSD: getaddrinfo.c,v 1.15 1999/12/13 16:22:56 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator.
     34  *
     35  * Issues to be discussed:
     36  * - Thread safe-ness must be checked.
     37  * - Return values.  There are nonstandard return values defined and used
     38  *   in the source code.  This is because RFC2553 is silent about which error
     39  *   code must be returned for which situation.
     40  * Note:
     41  * - We use getipnodebyname() just for thread-safeness.  There's no intent
     42  *   to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
     43  *   getipnodebyname().
     44  * - The code filters out AFs that are not supported by the kernel,
     45  *   when resolving FQDNs and globbing NULL hostname.  Is it the right
     46  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
     47  *   in ai_flags?
     48  */
     49 
     50 #if 0
     51 #ifdef HAVE_CONFIG_H
     52 #include "config.h"
     53 #endif
     54 #else
     55 #define HAVE_SOCKADDR_SA_LEN
     56 #endif
     57 
     58 #include <sys/types.h>
     59 #include <sys/param.h>
     60 #if 0
     61 #include <sys/sysctl.h>
     62 #endif
     63 #include <sys/socket.h>
     64 #include <net/if.h>
     65 #include <netinet/in.h>
     66 #include <arpa/inet.h>
     67 #include <arpa/nameser.h>
     68 #include <netdb.h>
     69 #include <resolv.h>
     70 #include <string.h>
     71 #include <stdlib.h>
     72 #include <stddef.h>
     73 #include <ctype.h>
     74 #include <unistd.h>
     75 #include <stdio.h>
     76 #include <errno.h>
     77 
     78 #if 0
     79 #ifndef HAVE_PORTABLE_PROTOTYPE
     80 #include "cdecl_ext.h"
     81 #endif
     82 
     83 #ifndef HAVE_U_INT32_T
     84 #include "bittypes.h"
     85 #endif
     86 
     87 #ifndef HAVE_SOCKADDR_STORAGE
     88 #include "sockstorage.h"
     89 #endif
     90 
     91 #ifndef HAVE_ADDRINFO
     92 #include "addrinfo.h"
     93 #endif
     94 
     95 #if defined(__KAME__) && defined(INET6)
     96 # define FAITH
     97 #endif
     98 #endif
     99 
    100 #define SUCCESS 0
    101 #define ANY 0
    102 #define YES 1
    103 #define NO  0
    104 
    105 #ifdef FAITH
    106 static int translate = NO;
    107 static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
    108 #endif
    109 
    110 static const char in_addrany[] = { 0, 0, 0, 0 };
    111 static const char in6_addrany[] = {
    112 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    113 };
    114 static const char in_loopback[] = { 127, 0, 0, 1 };
    115 static const char in6_loopback[] = {
    116 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
    117 };
    118 
    119 struct sockinet {
    120 	u_char	si_len;
    121 	u_char	si_family;
    122 	u_short	si_port;
    123 	u_int32_t si_scope_id;
    124 };
    125 
    126 static const struct afd {
    127 	int a_af;
    128 	int a_addrlen;
    129 	int a_socklen;
    130 	int a_off;
    131 	const char *a_addrany;
    132 	const char *a_loopback;
    133 	int a_scoped;
    134 } afdl [] = {
    135 #ifdef INET6
    136 	{PF_INET6, sizeof(struct in6_addr),
    137 	 sizeof(struct sockaddr_in6),
    138 	 offsetof(struct sockaddr_in6, sin6_addr),
    139 	 in6_addrany, in6_loopback, 1},
    140 #endif
    141 	{PF_INET, sizeof(struct in_addr),
    142 	 sizeof(struct sockaddr_in),
    143 	 offsetof(struct sockaddr_in, sin_addr),
    144 	 in_addrany, in_loopback, 0},
    145 	{0, 0, 0, 0, NULL, NULL, 0},
    146 };
    147 
    148 struct explore {
    149 	int e_af;
    150 	int e_socktype;
    151 	int e_protocol;
    152 	const char *e_protostr;
    153 	int e_wild;
    154 #define WILD_AF(ex)		((ex)->e_wild & 0x01)
    155 #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
    156 #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
    157 };
    158 
    159 static const struct explore explore[] = {
    160 #if 0
    161 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
    162 #endif
    163 #ifdef INET6
    164 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    165 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    166 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
    167 #endif
    168 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    169 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    170 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
    171 	{ -1, 0, 0, NULL, 0 },
    172 };
    173 
    174 #ifdef INET6
    175 #define PTON_MAX	16
    176 #else
    177 #define PTON_MAX	4
    178 #endif
    179 
    180 
    181 static int str_isnumber __P((const char *));
    182 static int explore_fqdn __P((const struct addrinfo *, const char *,
    183 	const char *, struct addrinfo **));
    184 static int explore_null __P((const struct addrinfo *, const char *,
    185 	const char *, struct addrinfo **));
    186 static int explore_numeric __P((const struct addrinfo *, const char *,
    187 	const char *, struct addrinfo **));
    188 static int explore_numeric_scope __P((const struct addrinfo *, const char *,
    189 	const char *, struct addrinfo **));
    190 static int get_name __P((const char *, const struct afd *, struct addrinfo **,
    191 	char *, const struct addrinfo *, const char *));
    192 static int get_canonname __P((const struct addrinfo *,
    193 	struct addrinfo *, const char *));
    194 static struct addrinfo *get_ai __P((const struct addrinfo *,
    195 	const struct afd *, const char *));
    196 static int get_portmatch __P((const struct addrinfo *, const char *));
    197 static int get_port __P((struct addrinfo *, const char *, int));
    198 static const struct afd *find_afd __P((int));
    199 
    200 static char *ai_errlist[] = {
    201 	"Success",
    202 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
    203 	"Temporary failure in name resolution",		/* EAI_AGAIN      */
    204 	"Invalid value for ai_flags",		       	/* EAI_BADFLAGS   */
    205 	"Non-recoverable failure in name resolution", 	/* EAI_FAIL       */
    206 	"ai_family not supported",			/* EAI_FAMILY     */
    207 	"Memory allocation failure", 			/* EAI_MEMORY     */
    208 	"No address associated with hostname", 		/* EAI_NODATA     */
    209 	"hostname nor servname provided, or not known",	/* EAI_NONAME     */
    210 	"servname not supported for ai_socktype",	/* EAI_SERVICE    */
    211 	"ai_socktype not supported", 			/* EAI_SOCKTYPE   */
    212 	"System error returned in errno", 		/* EAI_SYSTEM     */
    213 	"Invalid value for hints",			/* EAI_BADHINTS	  */
    214 	"Resolved protocol is unknown",			/* EAI_PROTOCOL   */
    215 	"Unknown error", 				/* EAI_MAX        */
    216 };
    217 
    218 /* XXX macros that make external reference is BAD. */
    219 
    220 #define GET_AI(ai, afd, addr) \
    221 do { \
    222 	/* external reference: pai, error, and label free */ \
    223 	(ai) = get_ai(pai, (afd), (addr)); \
    224 	if ((ai) == NULL) { \
    225 		error = EAI_MEMORY; \
    226 		goto free; \
    227 	} \
    228 } while (0)
    229 
    230 #define GET_PORT(ai, serv) \
    231 do { \
    232 	/* external reference: error and label free */ \
    233 	error = get_port((ai), (serv), 0); \
    234 	if (error != 0) \
    235 		goto free; \
    236 } while (0)
    237 
    238 #define GET_CANONNAME(ai, str) \
    239 do { \
    240 	/* external reference: pai, error and label free */ \
    241 	error = get_canonname(pai, (ai), (str)); \
    242 	if (error != 0) \
    243 		goto free; \
    244 } while (0)
    245 
    246 #define ERR(err) \
    247 do { \
    248 	/* external reference: error, and label bad */ \
    249 	error = (err); \
    250 	goto bad; \
    251 } while (0)
    252 
    253 #define MATCH_FAMILY(x, y, w) \
    254 	((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
    255 #define MATCH(x, y, w) \
    256 	((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
    257 
    258 char *
    259 gai_strerror(ecode)
    260 	int ecode;
    261 {
    262 	if (ecode < 0 || ecode > EAI_MAX)
    263 		ecode = EAI_MAX;
    264 	return ai_errlist[ecode];
    265 }
    266 
    267 void
    268 freeaddrinfo(ai)
    269 	struct addrinfo *ai;
    270 {
    271 	struct addrinfo *next;
    272 
    273 	do {
    274 		next = ai->ai_next;
    275 		if (ai->ai_canonname)
    276 			free(ai->ai_canonname);
    277 		/* no need to free(ai->ai_addr) */
    278 		free(ai);
    279 	} while ((ai = next) != NULL);
    280 }
    281 
    282 static int
    283 str_isnumber(p)
    284 	const char *p;
    285 {
    286 	char *q = (char *)p;
    287 	while (*q) {
    288 		if (! isdigit(*q))
    289 			return NO;
    290 		q++;
    291 	}
    292 	return YES;
    293 }
    294 
    295 int
    296 getaddrinfo(hostname, servname, hints, res)
    297 	const char *hostname, *servname;
    298 	const struct addrinfo *hints;
    299 	struct addrinfo **res;
    300 {
    301 	struct addrinfo sentinel;
    302 	struct addrinfo *cur;
    303 	int error = 0;
    304 	struct addrinfo ai;
    305 	struct addrinfo ai0;
    306 	struct addrinfo *pai;
    307 	const struct afd *afd;
    308 	const struct explore *ex;
    309 
    310 #ifdef FAITH
    311 	static int firsttime = 1;
    312 
    313 	if (firsttime) {
    314 		/* translator hack */
    315 		char *q = getenv("GAI");
    316 		if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
    317 			translate = YES;
    318 		firsttime = 0;
    319 	}
    320 #endif
    321 
    322 	sentinel.ai_next = NULL;
    323 	cur = &sentinel;
    324 	pai = &ai;
    325 	pai->ai_flags = 0;
    326 	pai->ai_family = PF_UNSPEC;
    327 	pai->ai_socktype = ANY;
    328 	pai->ai_protocol = ANY;
    329 	pai->ai_addrlen = 0;
    330 	pai->ai_canonname = NULL;
    331 	pai->ai_addr = NULL;
    332 	pai->ai_next = NULL;
    333 
    334 	if (hostname == NULL && servname == NULL)
    335 		return EAI_NONAME;
    336 	if (hints) {
    337 		/* error check for hints */
    338 		if (hints->ai_addrlen || hints->ai_canonname ||
    339 		    hints->ai_addr || hints->ai_next)
    340 			ERR(EAI_BADHINTS); /* xxx */
    341 		if (hints->ai_flags & ~AI_MASK)
    342 			ERR(EAI_BADFLAGS);
    343 		switch (hints->ai_family) {
    344 		case PF_UNSPEC:
    345 		case PF_INET:
    346 #ifdef INET6
    347 		case PF_INET6:
    348 #endif
    349 			break;
    350 		default:
    351 			ERR(EAI_FAMILY);
    352 		}
    353 		memcpy(pai, hints, sizeof(*pai));
    354 
    355 		/*
    356 		 * if both socktype/protocol are specified, check if they
    357 		 * are meaningful combination.
    358 		 */
    359 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
    360 			for (ex = explore; ex->e_af >= 0; ex++) {
    361 				if (pai->ai_family != ex->e_af)
    362 					continue;
    363 				if (ex->e_socktype == ANY)
    364 					continue;
    365 				if (ex->e_protocol == ANY)
    366 					continue;
    367 				if (pai->ai_socktype == ex->e_socktype
    368 				 && pai->ai_protocol != ex->e_protocol) {
    369 					ERR(EAI_BADHINTS);
    370 				}
    371 			}
    372 		}
    373 	}
    374 
    375 	/*
    376 	 * check for special cases.  (1) numeric servname is disallowed if
    377 	 * socktype/protocol are left unspecified. (2) servname is disallowed
    378 	 * for raw and other inet{,6} sockets.
    379 	 */
    380 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
    381 	 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)) {
    382 		ai0 = *pai;
    383 
    384 		if (pai->ai_family == PF_UNSPEC)
    385 			pai->ai_family = PF_INET6;
    386 		error = get_portmatch(pai, servname);
    387 		if (error)
    388 			ERR(error);
    389 
    390 		*pai = ai0;
    391 	}
    392 
    393 	ai0 = *pai;
    394 
    395 	/* NULL hostname, or numeric hostname */
    396 	for (ex = explore; ex->e_af >= 0; ex++) {
    397 		*pai = ai0;
    398 
    399 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    400 			continue;
    401 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
    402 			continue;
    403 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
    404 			continue;
    405 
    406 		if (pai->ai_family == PF_UNSPEC)
    407 			pai->ai_family = ex->e_af;
    408 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    409 			pai->ai_socktype = ex->e_socktype;
    410 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    411 			pai->ai_protocol = ex->e_protocol;
    412 
    413 		if (hostname == NULL)
    414 			error = explore_null(pai, hostname, servname, &cur->ai_next);
    415 		else
    416 			error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
    417 
    418 		if (error)
    419 			goto free;
    420 
    421 		while (cur && cur->ai_next)
    422 			cur = cur->ai_next;
    423 	}
    424 
    425 	/*
    426 	 * XXX
    427 	 * If numreic representation of AF1 can be interpreted as FQDN
    428 	 * representation of AF2, we need to think again about the code below.
    429 	 */
    430 	if (sentinel.ai_next)
    431 		goto good;
    432 
    433 	if (pai->ai_flags & AI_NUMERICHOST)
    434 		ERR(EAI_NONAME);
    435 	if (hostname == NULL)
    436 		ERR(EAI_NONAME);
    437 
    438 	/*
    439 	 * hostname as alphabetical name.
    440 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
    441 	 * outer loop by AFs.
    442 	 */
    443 	for (afd = afdl; afd->a_af; afd++) {
    444 		*pai = ai0;
    445 
    446 		if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
    447 			continue;
    448 
    449 		for (ex = explore; ex->e_af >= 0; ex++) {
    450 			*pai = ai0;
    451 
    452 			if (pai->ai_family == PF_UNSPEC)
    453 				pai->ai_family = afd->a_af;
    454 
    455 			if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    456 				continue;
    457 			if (!MATCH(pai->ai_socktype, ex->e_socktype,
    458 					WILD_SOCKTYPE(ex))) {
    459 				continue;
    460 			}
    461 			if (!MATCH(pai->ai_protocol, ex->e_protocol,
    462 					WILD_PROTOCOL(ex))) {
    463 				continue;
    464 			}
    465 
    466 			if (pai->ai_family == PF_UNSPEC)
    467 				pai->ai_family = ex->e_af;
    468 			if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    469 				pai->ai_socktype = ex->e_socktype;
    470 			if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    471 				pai->ai_protocol = ex->e_protocol;
    472 
    473 			error = explore_fqdn(pai, hostname, servname,
    474 				&cur->ai_next);
    475 
    476 			while (cur && cur->ai_next)
    477 				cur = cur->ai_next;
    478 		}
    479 	}
    480 
    481 	/* XXX */
    482 	if (sentinel.ai_next)
    483 		error = 0;
    484 
    485 	if (error)
    486 		goto free;
    487 	if (error == 0) {
    488 		if (sentinel.ai_next) {
    489  good:
    490 			*res = sentinel.ai_next;
    491 			return SUCCESS;
    492 		} else
    493 			error = EAI_FAIL;
    494 	}
    495  free:
    496  bad:
    497 	if (sentinel.ai_next)
    498 		freeaddrinfo(sentinel.ai_next);
    499 	*res = NULL;
    500 	return error;
    501 }
    502 
    503 /*
    504  * FQDN hostname, DNS lookup
    505  */
    506 static int
    507 explore_fqdn(pai, hostname, servname, res)
    508 	const struct addrinfo *pai;
    509 	const char *hostname;
    510 	const char *servname;
    511 	struct addrinfo **res;
    512 {
    513 	int s;
    514 	struct hostent *hp;
    515 	int h_error;
    516 	int af;
    517 	char *ap = NULL;
    518 	struct addrinfo sentinel, *cur;
    519 	int i;
    520 	const struct afd *afd;
    521 	int error;
    522 
    523 	*res = NULL;
    524 	sentinel.ai_next = NULL;
    525 	cur = &sentinel;
    526 
    527 	/*
    528 	 * filter out AFs that are not supported by the kernel
    529 	 * XXX errno?
    530 	 */
    531 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    532 	if (s < 0) {
    533 		if (errno != EMFILE)
    534 			return 0;
    535 	} else
    536 		close(s);
    537 
    538 	/*
    539 	 * if the servname does not match socktype/protocol, ignore it.
    540 	 */
    541 	if (get_portmatch(pai, servname) != 0)
    542 		return 0;
    543 
    544 	afd = find_afd(pai->ai_family);
    545 
    546 	/*
    547 	 * post-RFC2553: should look at (pai->ai_flags & AI_ADDRCONFIG)
    548 	 * rather than hardcoding it.  we may need to add AI_ADDRCONFIG
    549 	 * handling code by ourselves in case we don't have getipnodebyname().
    550 	 */
    551 #ifdef USE_GETIPNODEBY
    552 	hp = getipnodebyname(hostname, pai->ai_family, AI_ADDRCONFIG, &h_error);
    553 #else
    554 	hp = gethostbyname2(hostname, pai->ai_family);
    555 	h_error = h_errno;
    556 #endif
    557 
    558 	if (hp == NULL) {
    559 		switch (h_error) {
    560 		case HOST_NOT_FOUND:
    561 		case NO_DATA:
    562 			error = EAI_NODATA;
    563 			break;
    564 		case TRY_AGAIN:
    565 			error = EAI_AGAIN;
    566 			break;
    567 		case NO_RECOVERY:
    568 		case NETDB_INTERNAL:
    569 		default:
    570 			error = EAI_FAIL;
    571 			break;
    572 		}
    573 	} else if ((hp->h_name == NULL) || (hp->h_name[0] == 0)
    574 			|| (hp->h_addr_list[0] == NULL)) {
    575 #ifdef USE_GETIPNODEBY
    576 		freehostent(hp);
    577 #endif
    578 		hp = NULL;
    579 		error = EAI_FAIL;
    580 	}
    581 
    582 	if (hp == NULL)
    583 		goto free;
    584 
    585 	ap = malloc(hp->h_length);
    586 	if (ap == NULL) {
    587 		error = EAI_MEMORY;
    588 		goto free;
    589 	}
    590 
    591 	for (i = 0; hp->h_addr_list[i] != NULL; i++) {
    592 		/*
    593 		 * hp will be overwritten if we use gethostbyname2().
    594 		 * always deep copy for simplification.
    595 		 */
    596 		af = hp->h_addrtype;
    597 		memcpy(ap, hp->h_addr_list[i], hp->h_length);
    598 		if (af == AF_INET6
    599 		 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
    600 			af = AF_INET;
    601 			ap = ap + sizeof(struct in6_addr)
    602 				- sizeof(struct in_addr);
    603 		}
    604 
    605 		if (af != pai->ai_family)
    606 			continue;
    607 
    608 		if ((pai->ai_flags & AI_CANONNAME) == 0) {
    609 			GET_AI(cur->ai_next, afd, ap);
    610 			GET_PORT(cur->ai_next, servname);
    611 		} else {
    612 			/*
    613 			 * if AI_CANONNAME and if reverse lookup
    614 			 * fail, return ai anyway to pacify
    615 			 * calling application.
    616 			 *
    617 			 * XXX getaddrinfo() is a name->address
    618 			 * translation function, and it looks
    619 			 * strange that we do addr->name
    620 			 * translation here.
    621 			 */
    622 			get_name(ap, afd, &cur->ai_next,
    623 				ap, pai, servname);
    624 		}
    625 
    626 		while (cur && cur->ai_next)
    627 			cur = cur->ai_next;
    628 	}
    629 
    630 	*res = sentinel.ai_next;
    631 	return 0;
    632 
    633 free:
    634 #ifdef USE_GETIPNODEBY
    635 	if (hp)
    636 		freehostent(hp);
    637 #endif
    638 	if (ap)
    639 		free(ap);
    640 	if (sentinel.ai_next)
    641 		freeaddrinfo(sentinel.ai_next);
    642 	return error;
    643 }
    644 
    645 /*
    646  * hostname == NULL.
    647  * passive socket -> anyaddr (0.0.0.0 or ::)
    648  * non-passive socket -> localhost (127.0.0.1 or ::1)
    649  */
    650 static int
    651 explore_null(pai, hostname, servname, res)
    652 	const struct addrinfo *pai;
    653 	const char *hostname;
    654 	const char *servname;
    655 	struct addrinfo **res;
    656 {
    657 	int s;
    658 	const struct afd *afd;
    659 	struct addrinfo *cur;
    660 	struct addrinfo sentinel;
    661 	int error;
    662 
    663 	*res = NULL;
    664 	sentinel.ai_next = NULL;
    665 	cur = &sentinel;
    666 
    667 	/*
    668 	 * filter out AFs that are not supported by the kernel
    669 	 * XXX errno?
    670 	 */
    671 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    672 	if (s < 0) {
    673 		if (errno != EMFILE)
    674 			return 0;
    675 	} else
    676 		close(s);
    677 
    678 	/*
    679 	 * if the servname does not match socktype/protocol, ignore it.
    680 	 */
    681 	if (get_portmatch(pai, servname) != 0)
    682 		return 0;
    683 
    684 	afd = find_afd(pai->ai_family);
    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 	int flags;
    726 
    727 	*res = NULL;
    728 	sentinel.ai_next = NULL;
    729 	cur = &sentinel;
    730 
    731 	/*
    732 	 * if the servname does not match socktype/protocol, ignore it.
    733 	 */
    734 	if (get_portmatch(pai, servname) != 0)
    735 		return 0;
    736 
    737 	afd = find_afd(pai->ai_family);
    738 	flags = pai->ai_flags;
    739 
    740 	if (inet_pton(afd->a_af, hostname, pton) == 1) {
    741 		u_int32_t v4a;
    742 #ifdef INET6
    743 		u_char pfx;
    744 #endif
    745 
    746 		switch (afd->a_af) {
    747 		case AF_INET:
    748 			v4a = (u_int32_t)ntohl(((struct in_addr *)pton)->s_addr);
    749 			if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    750 				flags &= ~AI_CANONNAME;
    751 			v4a >>= IN_CLASSA_NSHIFT;
    752 			if (v4a == 0 || v4a == IN_LOOPBACKNET)
    753 				flags &= ~AI_CANONNAME;
    754 			break;
    755 #ifdef INET6
    756 		case AF_INET6:
    757 			pfx = ((struct in6_addr *)pton)->s6_addr[0];
    758 			if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
    759 				flags &= ~AI_CANONNAME;
    760 			break;
    761 #endif
    762 		}
    763 
    764 		if (pai->ai_family == afd->a_af ||
    765 		    pai->ai_family == PF_UNSPEC /*?*/) {
    766 			if ((flags & AI_CANONNAME) == 0) {
    767 				GET_AI(cur->ai_next, afd, pton);
    768 				GET_PORT(cur->ai_next, servname);
    769 			} else {
    770 				/*
    771 				 * if AI_CANONNAME and if reverse lookup
    772 				 * fail, return ai anyway to pacify
    773 				 * calling application.
    774 				 *
    775 				 * XXX getaddrinfo() is a name->address
    776 				 * translation function, and it looks
    777 				 * strange that we do addr->name
    778 				 * translation here.
    779 				 */
    780 				get_name(pton, afd, &cur->ai_next,
    781 					pton, pai, servname);
    782 			}
    783 			while (cur && cur->ai_next)
    784 				cur = cur->ai_next;
    785 		} else
    786 			ERR(EAI_FAMILY);	/*xxx*/
    787 	}
    788 
    789 	*res = sentinel.ai_next;
    790 	return 0;
    791 
    792 free:
    793 bad:
    794 	if (sentinel.ai_next)
    795 		freeaddrinfo(sentinel.ai_next);
    796 	return error;
    797 }
    798 
    799 /*
    800  * numeric hostname with scope
    801  */
    802 static int
    803 explore_numeric_scope(pai, hostname, servname, res)
    804 	const struct addrinfo *pai;
    805 	const char *hostname;
    806 	const char *servname;
    807 	struct addrinfo **res;
    808 {
    809 #ifndef SCOPE_DELIMITER
    810 	return explore_numeric(pai, hostname, servname, res);
    811 #else
    812 	const struct afd *afd;
    813 	struct addrinfo *cur;
    814 	int error;
    815 	char *cp, *hostname2 = NULL;
    816 	int scope;
    817 	struct sockaddr_in6 *sin6;
    818 
    819 	/*
    820 	 * if the servname does not match socktype/protocol, ignore it.
    821 	 */
    822 	if (get_portmatch(pai, servname) != 0)
    823 		return 0;
    824 
    825 	afd = find_afd(pai->ai_family);
    826 	if (!afd->a_scoped)
    827 		return explore_numeric(pai, hostname, servname, res);
    828 
    829 	cp = strchr(hostname, SCOPE_DELIMITER);
    830 	if (cp == NULL)
    831 		return explore_numeric(pai, hostname, servname, res);
    832 
    833 	/*
    834 	 * Handle special case of <scoped_address><delimiter><scope id>
    835 	 */
    836 	hostname2 = strdup(hostname);
    837 	if (hostname2 == NULL)
    838 		return EAI_MEMORY;
    839 	/* terminate at the delimiter */
    840 	hostname2[cp - hostname] = '\0';
    841 
    842 	cp++;
    843 	switch (pai->ai_family) {
    844 #ifdef INET6
    845 	case AF_INET6:
    846 		scope = if_nametoindex(cp);
    847 		break;
    848 #endif
    849 	}
    850 
    851 	error = explore_numeric(pai, hostname2, servname, res);
    852 	if (error == 0) {
    853 		for (cur = *res; cur; cur = cur->ai_next) {
    854 			if (cur->ai_family != AF_INET6)
    855 				continue;
    856 			sin6 = (struct sockaddr_in6 *)cur->ai_addr;
    857 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
    858 			    IN6_IS_ADDR_MC_LINKLOCAL(&sin6->sin6_addr))
    859 				sin6->sin6_scope_id = scope;
    860 		}
    861 	}
    862 
    863 	free(hostname2);
    864 
    865 	return error;
    866 #endif
    867 }
    868 
    869 static int
    870 get_name(addr, afd, res, numaddr, pai, servname)
    871 	const char *addr;
    872 	const struct afd *afd;
    873 	struct addrinfo **res;
    874 	char *numaddr;
    875 	const struct addrinfo *pai;
    876 	const char *servname;
    877 {
    878 	struct hostent *hp = NULL;
    879 	struct addrinfo *cur = NULL;
    880 	int error = 0;
    881 	char *ap = NULL, *cn = NULL;
    882 #ifdef USE_GETIPNODEBY
    883 	int h_error;
    884 
    885 	hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
    886 #else
    887 	hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    888 #endif
    889 	if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
    890 #ifdef USE_GETIPNODEBY
    891 		GET_AI(cur, afd, hp->h_addr_list[0]);
    892 		GET_PORT(cur, servname);
    893 		GET_CANONNAME(cur, hp->h_name);
    894 #else
    895 		/* hp will be damaged if we use gethostbyaddr() */
    896 		if ((ap = (char *)malloc(hp->h_length)) == NULL) {
    897 			error = EAI_MEMORY;
    898 			goto free;
    899 		}
    900 		memcpy(ap, hp->h_addr_list[0], hp->h_length);
    901 		if ((cn = strdup(hp->h_name)) == NULL) {
    902 			error = EAI_MEMORY;
    903 			goto free;
    904 		}
    905 
    906 		GET_AI(cur, afd, ap);
    907 		GET_PORT(cur, servname);
    908 		GET_CANONNAME(cur, cn);
    909 		free(ap); ap = NULL;
    910 		free(cn); cn = NULL;
    911 #endif
    912 	} else {
    913 		GET_AI(cur, afd, numaddr);
    914 		GET_PORT(cur, servname);
    915 	}
    916 
    917 #ifdef USE_GETIPNODEBY
    918 	if (hp)
    919 		freehostent(hp);
    920 #endif
    921 	*res = cur;
    922 	return SUCCESS;
    923  free:
    924 	if (cur)
    925 		freeaddrinfo(cur);
    926 	if (ap)
    927 		free(ap);
    928 	if (cn)
    929 		free(cn);
    930 #ifdef USE_GETIPNODEBY
    931 	if (hp)
    932 		freehostent(hp);
    933 #endif
    934 	*res = NULL;
    935 	return error;
    936 }
    937 
    938 static int
    939 get_canonname(pai, ai, str)
    940 	const struct addrinfo *pai;
    941 	struct addrinfo *ai;
    942 	const char *str;
    943 {
    944 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
    945 		ai->ai_canonname = (char *)malloc(strlen(str) + 1);
    946 		if (ai->ai_canonname == NULL)
    947 			return EAI_MEMORY;
    948 		strcpy(ai->ai_canonname, str);
    949 	}
    950 	return 0;
    951 }
    952 
    953 static struct addrinfo *
    954 get_ai(pai, afd, addr)
    955 	const struct addrinfo *pai;
    956 	const struct afd *afd;
    957 	const char *addr;
    958 {
    959 	char *p;
    960 	struct addrinfo *ai;
    961 
    962 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
    963 		+ (afd->a_socklen));
    964 	if (ai == NULL)
    965 		return NULL;
    966 
    967 	memcpy(ai, pai, sizeof(struct addrinfo));
    968 	ai->ai_addr = (struct sockaddr *)(ai + 1);
    969 	memset(ai->ai_addr, 0, afd->a_socklen);
    970 #ifdef HAVE_SOCKADDR_SA_LEN
    971 	ai->ai_addr->sa_len = afd->a_socklen;
    972 #endif
    973 	ai->ai_addrlen = afd->a_socklen;
    974 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
    975 	p = (char *)(ai->ai_addr);
    976 	memcpy(p + afd->a_off, addr, afd->a_addrlen);
    977 	return ai;
    978 }
    979 
    980 static int
    981 get_portmatch(ai, servname)
    982 	const struct addrinfo *ai;
    983 	const char *servname;
    984 {
    985 
    986 	/* get_port does not touch first argument. when matchonly == 1. */
    987 	return get_port((struct addrinfo *)ai, servname, 1);
    988 }
    989 
    990 static int
    991 get_port(ai, servname, matchonly)
    992 	struct addrinfo *ai;
    993 	const char *servname;
    994 	int matchonly;
    995 {
    996 	const char *proto;
    997 	struct servent *sp;
    998 	int port;
    999 	int allownumeric;
   1000 
   1001 	if (servname == NULL)
   1002 		return 0;
   1003 	if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
   1004 		return 0;
   1005 
   1006 	switch (ai->ai_socktype) {
   1007 	case SOCK_RAW:
   1008 		return EAI_SERVICE;
   1009 	case SOCK_DGRAM:
   1010 	case SOCK_STREAM:
   1011 		allownumeric = 1;
   1012 		break;
   1013 	case ANY:
   1014 		allownumeric = 0;
   1015 		break;
   1016 	default:
   1017 		return EAI_SOCKTYPE;
   1018 	}
   1019 
   1020 	if (str_isnumber(servname)) {
   1021 		if (!allownumeric)
   1022 			return EAI_SERVICE;
   1023 		port = htons(atoi(servname));
   1024 		if (port < 0 || port > 65535)
   1025 			return EAI_SERVICE;
   1026 	} else {
   1027 		switch (ai->ai_socktype) {
   1028 		case SOCK_DGRAM:
   1029 			proto = "udp";
   1030 			break;
   1031 		case SOCK_STREAM:
   1032 			proto = "tcp";
   1033 			break;
   1034 		default:
   1035 			proto = NULL;
   1036 			break;
   1037 		}
   1038 
   1039 		if ((sp = getservbyname(servname, proto)) == NULL)
   1040 			return EAI_SERVICE;
   1041 		port = sp->s_port;
   1042 	}
   1043 
   1044 	if (!matchonly) {
   1045 		switch (ai->ai_family) {
   1046 		case AF_INET:
   1047 			((struct sockaddr_in *)ai->ai_addr)->sin_port = port;
   1048 			break;
   1049 #ifdef INET6
   1050 		case AF_INET6:
   1051 			((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port;
   1052 			break;
   1053 #endif
   1054 		}
   1055 	}
   1056 
   1057 	return 0;
   1058 }
   1059 
   1060 static const struct afd *
   1061 find_afd(af)
   1062 	int af;
   1063 {
   1064 	const struct afd *afd;
   1065 
   1066 	if (af == PF_UNSPEC)
   1067 		return NULL;
   1068 	for (afd = afdl; afd->a_af; afd++) {
   1069 		if (afd->a_af == af)
   1070 			return afd;
   1071 	}
   1072 	return NULL;
   1073 }
   1074