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