Home | History | Annotate | Line # | Download | only in net
getaddrinfo.c revision 1.30
      1  1.30   itojun /*	$NetBSD: getaddrinfo.c,v 1.30 2000/02/16 04:50:23 itojun 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  * Issues to be discussed:
     34   1.1   itojun  * - Thread safe-ness must be checked.
     35   1.1   itojun  * - Return values.  There are nonstandard return values defined and used
     36  1.14   itojun  *   in the source code.  This is because RFC2553 is silent about which error
     37   1.1   itojun  *   code must be returned for which situation.
     38  1.27   itojun  * - IPv4 classful (shortened) form.  RFC2553 is silent about it.  XNET 5.2
     39  1.27   itojun  *   says to use inet_aton() to convert IPv4 numeric to binary (alows
     40  1.27   itojun  *   classful form as a result).
     41  1.27   itojun  *   current code - disallow classful form for IPv4 (due to use of inet_pton).
     42  1.27   itojun  * - freeaddrinfo(NULL).  RFC2553 is silent about it.  XNET 5.2 says it is
     43  1.27   itojun  *   invalid.
     44  1.27   itojun  *   current code - SEGV on freeaddrinfo(NULL)
     45  1.14   itojun  * Note:
     46  1.14   itojun  * - We use getipnodebyname() just for thread-safeness.  There's no intent
     47  1.14   itojun  *   to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to
     48  1.14   itojun  *   getipnodebyname().
     49  1.14   itojun  * - The code filters out AFs that are not supported by the kernel,
     50  1.22   itojun  *   when globbing NULL hostname (to loopback, or wildcard).  Is it the right
     51  1.14   itojun  *   thing to do?  What is the relationship with post-RFC2553 AI_ADDRCONFIG
     52  1.14   itojun  *   in ai_flags?
     53  1.30   itojun  * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.
     54  1.30   itojun  *   (1) what should we do against numeric hostname (2) what should we do
     55  1.30   itojun  *   against NULL hostname (3) what is AI_ADDRCONFIG itself.  AF not ready?
     56  1.30   itojun  *   non-loopback address configured?  global address configured?
     57  1.30   itojun  * - The code makes use of following calls when asked to resolver with
     58  1.30   itojun  *   ai_family  = PF_UNSPEC:
     59  1.30   itojun  *	getipnodebyname(host, AF_INET6);
     60  1.30   itojun  *	getipnodebyname(host, AF_INET);
     61  1.30   itojun  *   This will result in the following queries if the node is configure to
     62  1.30   itojun  *   prefer /etc/hosts than DNS:
     63  1.30   itojun  *	lookup /etc/hosts for IPv6 address
     64  1.30   itojun  *	lookup DNS for IPv6 address
     65  1.30   itojun  *	lookup /etc/hosts for IPv4 address
     66  1.30   itojun  *	lookup DNS for IPv4 address
     67  1.30   itojun  *   which may not meet people's requirement.
     68  1.30   itojun  *   The right thing to happen is to have underlying layer which does
     69  1.30   itojun  *   PF_UNSPEC lookup (lookup both) and return chain of addrinfos.
     70  1.30   itojun  *   This would result in a bit of code duplicate with _dns_ghbyname() and
     71  1.30   itojun  *   friends.
     72   1.1   itojun  */
     73   1.1   itojun 
     74  1.14   itojun #include <sys/types.h>
     75   1.1   itojun #include <sys/param.h>
     76   1.1   itojun #include <sys/socket.h>
     77  1.14   itojun #include <net/if.h>
     78   1.1   itojun #include <netinet/in.h>
     79   1.1   itojun #include <arpa/inet.h>
     80   1.1   itojun #include <arpa/nameser.h>
     81   1.1   itojun #include <netdb.h>
     82   1.1   itojun #include <resolv.h>
     83  1.14   itojun #include <string.h>
     84  1.14   itojun #include <stdlib.h>
     85  1.12    lukem #include <stddef.h>
     86  1.14   itojun #include <ctype.h>
     87   1.1   itojun #include <unistd.h>
     88  1.14   itojun #include <stdio.h>
     89  1.14   itojun #include <errno.h>
     90  1.14   itojun 
     91   1.1   itojun #define SUCCESS 0
     92   1.1   itojun #define ANY 0
     93   1.1   itojun #define YES 1
     94   1.1   itojun #define NO  0
     95   1.1   itojun 
     96   1.1   itojun static const char in_addrany[] = { 0, 0, 0, 0 };
     97   1.1   itojun static const char in6_addrany[] = {
     98   1.1   itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     99   1.1   itojun };
    100   1.1   itojun static const char in_loopback[] = { 127, 0, 0, 1 };
    101   1.1   itojun static const char in6_loopback[] = {
    102   1.1   itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
    103   1.1   itojun };
    104   1.1   itojun 
    105   1.1   itojun struct sockinet {
    106   1.1   itojun 	u_char	si_len;
    107   1.1   itojun 	u_char	si_family;
    108   1.1   itojun 	u_short	si_port;
    109  1.14   itojun 	u_int32_t si_scope_id;
    110   1.1   itojun };
    111   1.1   itojun 
    112  1.14   itojun static const struct afd {
    113   1.1   itojun 	int a_af;
    114   1.1   itojun 	int a_addrlen;
    115   1.1   itojun 	int a_socklen;
    116   1.1   itojun 	int a_off;
    117   1.1   itojun 	const char *a_addrany;
    118   1.1   itojun 	const char *a_loopback;
    119  1.14   itojun 	int a_scoped;
    120   1.1   itojun } afdl [] = {
    121   1.1   itojun #ifdef INET6
    122   1.1   itojun 	{PF_INET6, sizeof(struct in6_addr),
    123   1.1   itojun 	 sizeof(struct sockaddr_in6),
    124   1.1   itojun 	 offsetof(struct sockaddr_in6, sin6_addr),
    125  1.14   itojun 	 in6_addrany, in6_loopback, 1},
    126   1.1   itojun #endif
    127   1.1   itojun 	{PF_INET, sizeof(struct in_addr),
    128   1.1   itojun 	 sizeof(struct sockaddr_in),
    129   1.1   itojun 	 offsetof(struct sockaddr_in, sin_addr),
    130  1.14   itojun 	 in_addrany, in_loopback, 0},
    131  1.14   itojun 	{0, 0, 0, 0, NULL, NULL, 0},
    132  1.14   itojun };
    133  1.14   itojun 
    134  1.14   itojun struct explore {
    135  1.14   itojun 	int e_af;
    136  1.14   itojun 	int e_socktype;
    137  1.14   itojun 	int e_protocol;
    138  1.14   itojun 	const char *e_protostr;
    139  1.14   itojun 	int e_wild;
    140  1.14   itojun #define WILD_AF(ex)		((ex)->e_wild & 0x01)
    141  1.14   itojun #define WILD_SOCKTYPE(ex)	((ex)->e_wild & 0x02)
    142  1.14   itojun #define WILD_PROTOCOL(ex)	((ex)->e_wild & 0x04)
    143  1.14   itojun };
    144  1.14   itojun 
    145  1.14   itojun static const struct explore explore[] = {
    146  1.14   itojun #if 0
    147  1.14   itojun 	{ PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },
    148  1.14   itojun #endif
    149  1.14   itojun #ifdef INET6
    150  1.14   itojun 	{ PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    151  1.14   itojun 	{ PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    152  1.14   itojun 	{ PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },
    153  1.14   itojun #endif
    154  1.14   itojun 	{ PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },
    155  1.14   itojun 	{ PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },
    156  1.14   itojun 	{ PF_INET, SOCK_RAW, ANY, NULL, 0x05 },
    157  1.14   itojun 	{ -1, 0, 0, NULL, 0 },
    158   1.1   itojun };
    159   1.1   itojun 
    160   1.1   itojun #ifdef INET6
    161   1.1   itojun #define PTON_MAX	16
    162   1.1   itojun #else
    163   1.1   itojun #define PTON_MAX	4
    164   1.1   itojun #endif
    165   1.1   itojun 
    166   1.1   itojun 
    167   1.1   itojun static int str_isnumber __P((const char *));
    168  1.14   itojun static int explore_fqdn __P((const struct addrinfo *, const char *,
    169  1.14   itojun 	const char *, struct addrinfo **));
    170  1.20  mycroft static int explore_null __P((const struct addrinfo *,
    171  1.14   itojun 	const char *, struct addrinfo **));
    172  1.14   itojun static int explore_numeric __P((const struct addrinfo *, const char *,
    173  1.14   itojun 	const char *, struct addrinfo **));
    174  1.14   itojun static int explore_numeric_scope __P((const struct addrinfo *, const char *,
    175  1.14   itojun 	const char *, struct addrinfo **));
    176  1.14   itojun static int get_canonname __P((const struct addrinfo *,
    177  1.14   itojun 	struct addrinfo *, const char *));
    178  1.14   itojun static struct addrinfo *get_ai __P((const struct addrinfo *,
    179  1.14   itojun 	const struct afd *, const char *));
    180  1.14   itojun static int get_portmatch __P((const struct addrinfo *, const char *));
    181  1.14   itojun static int get_port __P((struct addrinfo *, const char *, int));
    182  1.14   itojun static const struct afd *find_afd __P((int));
    183  1.27   itojun static int addrconfig __P((const struct addrinfo *));
    184  1.27   itojun #ifdef INET6
    185  1.27   itojun static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *));
    186  1.27   itojun #endif
    187  1.14   itojun 
    188   1.1   itojun static char *ai_errlist[] = {
    189   1.7    lukem 	"Success",
    190   1.7    lukem 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
    191   1.7    lukem 	"Temporary failure in name resolution",		/* EAI_AGAIN      */
    192   1.7    lukem 	"Invalid value for ai_flags",		       	/* EAI_BADFLAGS   */
    193   1.7    lukem 	"Non-recoverable failure in name resolution", 	/* EAI_FAIL       */
    194   1.2    lukem 	"ai_family not supported",			/* EAI_FAMILY     */
    195   1.7    lukem 	"Memory allocation failure", 			/* EAI_MEMORY     */
    196   1.7    lukem 	"No address associated with hostname", 		/* EAI_NODATA     */
    197  1.14   itojun 	"hostname nor servname provided, or not known",	/* EAI_NONAME     */
    198   1.2    lukem 	"servname not supported for ai_socktype",	/* EAI_SERVICE    */
    199   1.2    lukem 	"ai_socktype not supported", 			/* EAI_SOCKTYPE   */
    200   1.7    lukem 	"System error returned in errno", 		/* EAI_SYSTEM     */
    201   1.7    lukem 	"Invalid value for hints",			/* EAI_BADHINTS	  */
    202   1.7    lukem 	"Resolved protocol is unknown",			/* EAI_PROTOCOL   */
    203   1.7    lukem 	"Unknown error", 				/* EAI_MAX        */
    204   1.1   itojun };
    205   1.1   itojun 
    206  1.14   itojun /* XXX macros that make external reference is BAD. */
    207  1.14   itojun 
    208  1.14   itojun #define GET_AI(ai, afd, addr) \
    209  1.14   itojun do { \
    210  1.14   itojun 	/* external reference: pai, error, and label free */ \
    211  1.14   itojun 	(ai) = get_ai(pai, (afd), (addr)); \
    212  1.14   itojun 	if ((ai) == NULL) { \
    213  1.14   itojun 		error = EAI_MEMORY; \
    214  1.14   itojun 		goto free; \
    215  1.14   itojun 	} \
    216  1.20  mycroft } while (/*CONSTCOND*/0)
    217  1.14   itojun 
    218  1.14   itojun #define GET_PORT(ai, serv) \
    219  1.14   itojun do { \
    220  1.14   itojun 	/* external reference: error and label free */ \
    221  1.14   itojun 	error = get_port((ai), (serv), 0); \
    222  1.14   itojun 	if (error != 0) \
    223  1.14   itojun 		goto free; \
    224  1.20  mycroft } while (/*CONSTCOND*/0)
    225  1.14   itojun 
    226   1.1   itojun #define GET_CANONNAME(ai, str) \
    227  1.14   itojun do { \
    228  1.14   itojun 	/* external reference: pai, error and label free */ \
    229  1.14   itojun 	error = get_canonname(pai, (ai), (str)); \
    230  1.14   itojun 	if (error != 0) \
    231  1.14   itojun 		goto free; \
    232  1.20  mycroft } while (/*CONSTCOND*/0)
    233  1.14   itojun 
    234  1.14   itojun #define ERR(err) \
    235  1.14   itojun do { \
    236  1.14   itojun 	/* external reference: error, and label bad */ \
    237  1.14   itojun 	error = (err); \
    238  1.14   itojun 	goto bad; \
    239  1.20  mycroft } while (/*CONSTCOND*/0)
    240  1.14   itojun 
    241  1.14   itojun #define MATCH_FAMILY(x, y, w) \
    242  1.20  mycroft 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
    243  1.14   itojun #define MATCH(x, y, w) \
    244  1.20  mycroft 	((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))
    245   1.1   itojun 
    246   1.1   itojun char *
    247   1.1   itojun gai_strerror(ecode)
    248   1.1   itojun 	int ecode;
    249   1.1   itojun {
    250   1.1   itojun 	if (ecode < 0 || ecode > EAI_MAX)
    251   1.1   itojun 		ecode = EAI_MAX;
    252   1.1   itojun 	return ai_errlist[ecode];
    253   1.1   itojun }
    254   1.1   itojun 
    255   1.1   itojun void
    256   1.1   itojun freeaddrinfo(ai)
    257   1.1   itojun 	struct addrinfo *ai;
    258   1.1   itojun {
    259   1.1   itojun 	struct addrinfo *next;
    260   1.1   itojun 
    261  1.26   itojun 	do {
    262   1.1   itojun 		next = ai->ai_next;
    263   1.1   itojun 		if (ai->ai_canonname)
    264   1.1   itojun 			free(ai->ai_canonname);
    265   1.1   itojun 		/* no need to free(ai->ai_addr) */
    266   1.1   itojun 		free(ai);
    267  1.27   itojun 		ai = next;
    268  1.27   itojun 	} while (ai);
    269   1.1   itojun }
    270   1.1   itojun 
    271   1.1   itojun static int
    272   1.1   itojun str_isnumber(p)
    273   1.1   itojun 	const char *p;
    274   1.1   itojun {
    275  1.20  mycroft 	const char *q = (const char *)p;
    276   1.1   itojun 	while (*q) {
    277  1.20  mycroft 		if (!isdigit(*q))
    278   1.1   itojun 			return NO;
    279   1.1   itojun 		q++;
    280   1.1   itojun 	}
    281   1.1   itojun 	return YES;
    282   1.1   itojun }
    283   1.1   itojun 
    284   1.1   itojun int
    285   1.1   itojun getaddrinfo(hostname, servname, hints, res)
    286   1.1   itojun 	const char *hostname, *servname;
    287   1.1   itojun 	const struct addrinfo *hints;
    288   1.1   itojun 	struct addrinfo **res;
    289   1.1   itojun {
    290   1.1   itojun 	struct addrinfo sentinel;
    291   1.1   itojun 	struct addrinfo *cur;
    292  1.14   itojun 	int error = 0;
    293   1.1   itojun 	struct addrinfo ai;
    294  1.14   itojun 	struct addrinfo ai0;
    295   1.1   itojun 	struct addrinfo *pai;
    296  1.14   itojun 	const struct afd *afd;
    297  1.14   itojun 	const struct explore *ex;
    298   1.1   itojun 
    299   1.1   itojun 	sentinel.ai_next = NULL;
    300   1.1   itojun 	cur = &sentinel;
    301   1.1   itojun 	pai = &ai;
    302   1.1   itojun 	pai->ai_flags = 0;
    303   1.1   itojun 	pai->ai_family = PF_UNSPEC;
    304   1.1   itojun 	pai->ai_socktype = ANY;
    305   1.1   itojun 	pai->ai_protocol = ANY;
    306   1.1   itojun 	pai->ai_addrlen = 0;
    307   1.1   itojun 	pai->ai_canonname = NULL;
    308   1.1   itojun 	pai->ai_addr = NULL;
    309   1.1   itojun 	pai->ai_next = NULL;
    310   1.1   itojun 
    311   1.1   itojun 	if (hostname == NULL && servname == NULL)
    312   1.1   itojun 		return EAI_NONAME;
    313   1.1   itojun 	if (hints) {
    314   1.1   itojun 		/* error check for hints */
    315   1.1   itojun 		if (hints->ai_addrlen || hints->ai_canonname ||
    316   1.1   itojun 		    hints->ai_addr || hints->ai_next)
    317   1.1   itojun 			ERR(EAI_BADHINTS); /* xxx */
    318   1.1   itojun 		if (hints->ai_flags & ~AI_MASK)
    319   1.1   itojun 			ERR(EAI_BADFLAGS);
    320   1.1   itojun 		switch (hints->ai_family) {
    321   1.1   itojun 		case PF_UNSPEC:
    322   1.1   itojun 		case PF_INET:
    323   1.1   itojun #ifdef INET6
    324   1.1   itojun 		case PF_INET6:
    325   1.1   itojun #endif
    326   1.1   itojun 			break;
    327   1.1   itojun 		default:
    328   1.1   itojun 			ERR(EAI_FAMILY);
    329   1.1   itojun 		}
    330   1.1   itojun 		memcpy(pai, hints, sizeof(*pai));
    331  1.14   itojun 
    332  1.14   itojun 		/*
    333  1.14   itojun 		 * if both socktype/protocol are specified, check if they
    334  1.14   itojun 		 * are meaningful combination.
    335  1.14   itojun 		 */
    336  1.14   itojun 		if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {
    337  1.14   itojun 			for (ex = explore; ex->e_af >= 0; ex++) {
    338  1.14   itojun 				if (pai->ai_family != ex->e_af)
    339  1.14   itojun 					continue;
    340  1.14   itojun 				if (ex->e_socktype == ANY)
    341  1.14   itojun 					continue;
    342  1.14   itojun 				if (ex->e_protocol == ANY)
    343  1.14   itojun 					continue;
    344  1.14   itojun 				if (pai->ai_socktype == ex->e_socktype
    345  1.14   itojun 				 && pai->ai_protocol != ex->e_protocol) {
    346  1.14   itojun 					ERR(EAI_BADHINTS);
    347  1.14   itojun 				}
    348   1.1   itojun 			}
    349   1.1   itojun 		}
    350   1.1   itojun 	}
    351   1.1   itojun 
    352   1.1   itojun 	/*
    353  1.14   itojun 	 * check for special cases.  (1) numeric servname is disallowed if
    354  1.14   itojun 	 * socktype/protocol are left unspecified. (2) servname is disallowed
    355  1.14   itojun 	 * for raw and other inet{,6} sockets.
    356   1.1   itojun 	 */
    357  1.14   itojun 	if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)
    358  1.27   itojun #ifdef PF_INET6
    359  1.27   itojun 	 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)
    360  1.27   itojun #endif
    361  1.27   itojun 	    ) {
    362  1.23   itojun 		ai0 = *pai;	/* backup *pai */
    363  1.21  mycroft 
    364  1.27   itojun 		if (pai->ai_family == PF_UNSPEC) {
    365  1.27   itojun #ifdef PF_INET6
    366  1.14   itojun 			pai->ai_family = PF_INET6;
    367  1.27   itojun #else
    368  1.27   itojun 			pai->ai_family = PF_INET;
    369  1.27   itojun #endif
    370  1.27   itojun 		}
    371  1.14   itojun 		error = get_portmatch(pai, servname);
    372  1.14   itojun 		if (error)
    373  1.14   itojun 			ERR(error);
    374  1.21  mycroft 
    375  1.21  mycroft 		*pai = ai0;
    376  1.14   itojun 	}
    377  1.14   itojun 
    378  1.14   itojun 	ai0 = *pai;
    379  1.14   itojun 
    380  1.14   itojun 	/* NULL hostname, or numeric hostname */
    381  1.14   itojun 	for (ex = explore; ex->e_af >= 0; ex++) {
    382  1.14   itojun 		*pai = ai0;
    383  1.14   itojun 
    384  1.14   itojun 		if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    385  1.14   itojun 			continue;
    386  1.14   itojun 		if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))
    387  1.14   itojun 			continue;
    388  1.14   itojun 		if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))
    389  1.14   itojun 			continue;
    390  1.14   itojun 
    391  1.14   itojun 		if (pai->ai_family == PF_UNSPEC)
    392  1.14   itojun 			pai->ai_family = ex->e_af;
    393  1.14   itojun 		if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    394  1.14   itojun 			pai->ai_socktype = ex->e_socktype;
    395  1.14   itojun 		if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    396  1.14   itojun 			pai->ai_protocol = ex->e_protocol;
    397  1.14   itojun 
    398  1.14   itojun 		if (hostname == NULL)
    399  1.20  mycroft 			error = explore_null(pai, servname, &cur->ai_next);
    400  1.14   itojun 		else
    401  1.14   itojun 			error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next);
    402  1.14   itojun 
    403  1.14   itojun 		if (error)
    404  1.14   itojun 			goto free;
    405  1.14   itojun 
    406  1.14   itojun 		while (cur && cur->ai_next)
    407  1.14   itojun 			cur = cur->ai_next;
    408  1.14   itojun 	}
    409  1.14   itojun 
    410  1.14   itojun 	/*
    411  1.14   itojun 	 * XXX
    412  1.14   itojun 	 * If numreic representation of AF1 can be interpreted as FQDN
    413  1.14   itojun 	 * representation of AF2, we need to think again about the code below.
    414  1.14   itojun 	 */
    415  1.14   itojun 	if (sentinel.ai_next)
    416  1.14   itojun 		goto good;
    417  1.14   itojun 
    418  1.14   itojun 	if (pai->ai_flags & AI_NUMERICHOST)
    419  1.14   itojun 		ERR(EAI_NONAME);
    420  1.14   itojun 	if (hostname == NULL)
    421  1.14   itojun 		ERR(EAI_NONAME);
    422  1.14   itojun 
    423  1.14   itojun 	/*
    424  1.14   itojun 	 * hostname as alphabetical name.
    425  1.14   itojun 	 * we would like to prefer AF_INET6 than AF_INET, so we'll make a
    426  1.14   itojun 	 * outer loop by AFs.
    427  1.14   itojun 	 */
    428  1.14   itojun 	for (afd = afdl; afd->a_af; afd++) {
    429  1.14   itojun 		*pai = ai0;
    430  1.14   itojun 
    431  1.14   itojun 		if (!MATCH_FAMILY(pai->ai_family, afd->a_af, 1))
    432  1.14   itojun 			continue;
    433  1.14   itojun 
    434  1.14   itojun 		for (ex = explore; ex->e_af >= 0; ex++) {
    435  1.14   itojun 			*pai = ai0;
    436  1.14   itojun 
    437  1.14   itojun 			if (pai->ai_family == PF_UNSPEC)
    438  1.14   itojun 				pai->ai_family = afd->a_af;
    439   1.1   itojun 
    440  1.14   itojun 			if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))
    441  1.14   itojun 				continue;
    442  1.14   itojun 			if (!MATCH(pai->ai_socktype, ex->e_socktype,
    443  1.14   itojun 					WILD_SOCKTYPE(ex))) {
    444  1.14   itojun 				continue;
    445   1.1   itojun 			}
    446  1.14   itojun 			if (!MATCH(pai->ai_protocol, ex->e_protocol,
    447  1.14   itojun 					WILD_PROTOCOL(ex))) {
    448  1.14   itojun 				continue;
    449   1.1   itojun 			}
    450  1.14   itojun 
    451  1.14   itojun 			if (pai->ai_family == PF_UNSPEC)
    452  1.14   itojun 				pai->ai_family = ex->e_af;
    453  1.14   itojun 			if (pai->ai_socktype == ANY && ex->e_socktype != ANY)
    454  1.14   itojun 				pai->ai_socktype = ex->e_socktype;
    455  1.14   itojun 			if (pai->ai_protocol == ANY && ex->e_protocol != ANY)
    456  1.14   itojun 				pai->ai_protocol = ex->e_protocol;
    457  1.14   itojun 
    458  1.14   itojun 			error = explore_fqdn(pai, hostname, servname,
    459  1.14   itojun 				&cur->ai_next);
    460  1.14   itojun 
    461  1.14   itojun 			while (cur && cur->ai_next)
    462  1.14   itojun 				cur = cur->ai_next;
    463   1.1   itojun 		}
    464   1.1   itojun 	}
    465  1.14   itojun 
    466  1.14   itojun 	/* XXX */
    467  1.14   itojun 	if (sentinel.ai_next)
    468  1.14   itojun 		error = 0;
    469  1.14   itojun 
    470  1.14   itojun 	if (error)
    471  1.14   itojun 		goto free;
    472  1.14   itojun 	if (error == 0) {
    473  1.14   itojun 		if (sentinel.ai_next) {
    474  1.14   itojun  good:
    475  1.14   itojun 			*res = sentinel.ai_next;
    476  1.14   itojun 			return SUCCESS;
    477  1.14   itojun 		} else
    478  1.14   itojun 			error = EAI_FAIL;
    479  1.14   itojun 	}
    480  1.14   itojun  free:
    481  1.14   itojun  bad:
    482  1.14   itojun 	if (sentinel.ai_next)
    483  1.14   itojun 		freeaddrinfo(sentinel.ai_next);
    484  1.14   itojun 	*res = NULL;
    485  1.14   itojun 	return error;
    486  1.14   itojun }
    487  1.14   itojun 
    488  1.14   itojun /*
    489  1.14   itojun  * FQDN hostname, DNS lookup
    490  1.14   itojun  */
    491  1.14   itojun static int
    492  1.14   itojun explore_fqdn(pai, hostname, servname, res)
    493  1.14   itojun 	const struct addrinfo *pai;
    494  1.14   itojun 	const char *hostname;
    495  1.14   itojun 	const char *servname;
    496  1.14   itojun 	struct addrinfo **res;
    497  1.14   itojun {
    498  1.14   itojun 	struct hostent *hp;
    499  1.14   itojun 	int h_error;
    500  1.14   itojun 	int af;
    501  1.16   itojun 	char **aplist = NULL, *apbuf = NULL;
    502  1.16   itojun 	char *ap;
    503  1.14   itojun 	struct addrinfo sentinel, *cur;
    504  1.14   itojun 	int i;
    505  1.16   itojun 	int naddrs;
    506  1.14   itojun 	const struct afd *afd;
    507  1.27   itojun 	int error = 0;
    508  1.14   itojun 
    509  1.14   itojun 	*res = NULL;
    510  1.14   itojun 	sentinel.ai_next = NULL;
    511  1.14   itojun 	cur = &sentinel;
    512  1.14   itojun 
    513  1.14   itojun 	/*
    514  1.27   itojun 	 * If AI_ADDRCONFIG is specified, check if we are expected to
    515  1.27   itojun 	 * return the address family or not.
    516  1.14   itojun 	 */
    517  1.27   itojun 	if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(pai))
    518  1.27   itojun 		return 0;
    519  1.14   itojun 
    520  1.14   itojun 	/*
    521  1.14   itojun 	 * if the servname does not match socktype/protocol, ignore it.
    522  1.14   itojun 	 */
    523  1.14   itojun 	if (get_portmatch(pai, servname) != 0)
    524  1.14   itojun 		return 0;
    525  1.14   itojun 
    526  1.14   itojun 	afd = find_afd(pai->ai_family);
    527  1.14   itojun 
    528  1.14   itojun 	hp = gethostbyname2(hostname, pai->ai_family);
    529  1.14   itojun 	h_error = h_errno;
    530  1.14   itojun 
    531  1.14   itojun 	if (hp == NULL) {
    532  1.14   itojun 		switch (h_error) {
    533  1.14   itojun 		case HOST_NOT_FOUND:
    534  1.14   itojun 		case NO_DATA:
    535  1.14   itojun 			error = EAI_NODATA;
    536  1.14   itojun 			break;
    537  1.14   itojun 		case TRY_AGAIN:
    538  1.14   itojun 			error = EAI_AGAIN;
    539  1.14   itojun 			break;
    540  1.14   itojun 		case NO_RECOVERY:
    541  1.14   itojun 		case NETDB_INTERNAL:
    542  1.14   itojun 		default:
    543  1.14   itojun 			error = EAI_FAIL;
    544  1.14   itojun 			break;
    545  1.14   itojun 		}
    546  1.14   itojun 	} else if ((hp->h_name == NULL) || (hp->h_name[0] == 0)
    547  1.14   itojun 			|| (hp->h_addr_list[0] == NULL)) {
    548  1.14   itojun 		hp = NULL;
    549  1.14   itojun 		error = EAI_FAIL;
    550  1.14   itojun 	}
    551  1.14   itojun 
    552  1.14   itojun 	if (hp == NULL)
    553  1.14   itojun 		goto free;
    554  1.14   itojun 
    555  1.16   itojun 	/*
    556  1.16   itojun 	 * hp will be overwritten if we use gethostbyname2().
    557  1.16   itojun 	 * always deep copy for simplification.
    558  1.16   itojun 	 */
    559  1.16   itojun 	for (naddrs = 0; hp->h_addr_list[naddrs] != NULL; naddrs++)
    560  1.16   itojun 		;
    561  1.16   itojun 	naddrs++;
    562  1.16   itojun 	aplist = (char **)malloc(sizeof(aplist[0]) * naddrs);
    563  1.20  mycroft 	apbuf = (char *)malloc((size_t)hp->h_length * naddrs);
    564  1.16   itojun 	if (aplist == NULL || apbuf == NULL) {
    565  1.15   itojun 		error = EAI_MEMORY;
    566  1.15   itojun 		goto free;
    567  1.15   itojun 	}
    568  1.16   itojun 	memset(aplist, 0, sizeof(aplist[0]) * naddrs);
    569  1.16   itojun 	for (i = 0; i < naddrs; i++) {
    570  1.16   itojun 		if (hp->h_addr_list[i] == NULL) {
    571  1.16   itojun 			aplist[i] = NULL;
    572  1.16   itojun 			continue;
    573  1.16   itojun 		}
    574  1.16   itojun 		memcpy(&apbuf[i * hp->h_length], hp->h_addr_list[i],
    575  1.20  mycroft 			(size_t)hp->h_length);
    576  1.16   itojun 		aplist[i] = &apbuf[i * hp->h_length];
    577  1.16   itojun 	}
    578  1.15   itojun 
    579  1.16   itojun 	for (i = 0; aplist[i] != NULL; i++) {
    580  1.14   itojun 		af = hp->h_addrtype;
    581  1.16   itojun 		ap = aplist[i];
    582  1.27   itojun #ifdef AF_INET6
    583  1.14   itojun 		if (af == AF_INET6
    584  1.14   itojun 		 && IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
    585  1.14   itojun 			af = AF_INET;
    586  1.14   itojun 			ap = ap + sizeof(struct in6_addr)
    587  1.14   itojun 				- sizeof(struct in_addr);
    588  1.14   itojun 		}
    589  1.27   itojun #endif
    590  1.14   itojun 
    591  1.14   itojun 		if (af != pai->ai_family)
    592  1.14   itojun 			continue;
    593   1.1   itojun 
    594  1.24   itojun 		GET_AI(cur->ai_next, afd, ap);
    595  1.24   itojun 		GET_PORT(cur->ai_next, servname);
    596  1.24   itojun 		if ((pai->ai_flags & AI_CANONNAME) != 0) {
    597   1.1   itojun 			/*
    598  1.24   itojun 			 * RFC2553 says that ai_canonname will be set only for
    599  1.24   itojun 			 * the first element.  we do it for all the elements,
    600  1.24   itojun 			 * just for convenience.
    601   1.1   itojun 			 */
    602  1.24   itojun 			GET_CANONNAME(cur->ai_next, hp->h_name);
    603  1.14   itojun 		}
    604   1.1   itojun 
    605  1.14   itojun 		while (cur && cur->ai_next)
    606   1.1   itojun 			cur = cur->ai_next;
    607   1.1   itojun 	}
    608  1.14   itojun 
    609  1.14   itojun 	*res = sentinel.ai_next;
    610  1.14   itojun 	return 0;
    611  1.14   itojun 
    612  1.14   itojun free:
    613  1.16   itojun 	if (aplist)
    614  1.16   itojun 		free(aplist);
    615  1.16   itojun 	if (apbuf)
    616  1.16   itojun 		free(apbuf);
    617  1.14   itojun 	if (sentinel.ai_next)
    618  1.14   itojun 		freeaddrinfo(sentinel.ai_next);
    619  1.14   itojun 	return error;
    620  1.14   itojun }
    621  1.14   itojun 
    622  1.14   itojun /*
    623  1.14   itojun  * hostname == NULL.
    624  1.14   itojun  * passive socket -> anyaddr (0.0.0.0 or ::)
    625  1.14   itojun  * non-passive socket -> localhost (127.0.0.1 or ::1)
    626  1.14   itojun  */
    627  1.14   itojun static int
    628  1.20  mycroft explore_null(pai, servname, res)
    629  1.14   itojun 	const struct addrinfo *pai;
    630  1.14   itojun 	const char *servname;
    631  1.14   itojun 	struct addrinfo **res;
    632  1.14   itojun {
    633  1.14   itojun 	int s;
    634  1.14   itojun 	const struct afd *afd;
    635  1.14   itojun 	struct addrinfo *cur;
    636  1.14   itojun 	struct addrinfo sentinel;
    637  1.14   itojun 	int error;
    638  1.14   itojun 
    639  1.14   itojun 	*res = NULL;
    640  1.14   itojun 	sentinel.ai_next = NULL;
    641  1.14   itojun 	cur = &sentinel;
    642  1.14   itojun 
    643  1.14   itojun 	/*
    644  1.14   itojun 	 * filter out AFs that are not supported by the kernel
    645  1.14   itojun 	 * XXX errno?
    646  1.14   itojun 	 */
    647  1.14   itojun 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    648  1.14   itojun 	if (s < 0) {
    649  1.14   itojun 		if (errno != EMFILE)
    650  1.14   itojun 			return 0;
    651  1.14   itojun 	} else
    652  1.14   itojun 		close(s);
    653  1.14   itojun 
    654  1.14   itojun 	/*
    655  1.14   itojun 	 * if the servname does not match socktype/protocol, ignore it.
    656  1.14   itojun 	 */
    657  1.14   itojun 	if (get_portmatch(pai, servname) != 0)
    658  1.14   itojun 		return 0;
    659  1.14   itojun 
    660  1.14   itojun 	afd = find_afd(pai->ai_family);
    661  1.14   itojun 
    662  1.14   itojun 	if (pai->ai_flags & AI_PASSIVE) {
    663  1.14   itojun 		GET_AI(cur->ai_next, afd, afd->a_addrany);
    664  1.14   itojun 		/* xxx meaningless?
    665  1.14   itojun 		 * GET_CANONNAME(cur->ai_next, "anyaddr");
    666  1.14   itojun 		 */
    667  1.14   itojun 		GET_PORT(cur->ai_next, servname);
    668  1.14   itojun 	} else {
    669  1.14   itojun 		GET_AI(cur->ai_next, afd, afd->a_loopback);
    670  1.14   itojun 		/* xxx meaningless?
    671  1.14   itojun 		 * GET_CANONNAME(cur->ai_next, "localhost");
    672  1.14   itojun 		 */
    673  1.14   itojun 		GET_PORT(cur->ai_next, servname);
    674  1.14   itojun 	}
    675  1.14   itojun 	cur = cur->ai_next;
    676  1.14   itojun 
    677  1.14   itojun 	*res = sentinel.ai_next;
    678  1.14   itojun 	return 0;
    679  1.14   itojun 
    680  1.14   itojun free:
    681  1.14   itojun 	if (sentinel.ai_next)
    682  1.14   itojun 		freeaddrinfo(sentinel.ai_next);
    683  1.14   itojun 	return error;
    684  1.14   itojun }
    685  1.14   itojun 
    686  1.14   itojun /*
    687  1.14   itojun  * numeric hostname
    688  1.14   itojun  */
    689  1.14   itojun static int
    690  1.14   itojun explore_numeric(pai, hostname, servname, res)
    691  1.14   itojun 	const struct addrinfo *pai;
    692  1.14   itojun 	const char *hostname;
    693  1.14   itojun 	const char *servname;
    694  1.14   itojun 	struct addrinfo **res;
    695  1.14   itojun {
    696  1.14   itojun 	const struct afd *afd;
    697  1.14   itojun 	struct addrinfo *cur;
    698  1.14   itojun 	struct addrinfo sentinel;
    699  1.14   itojun 	int error;
    700  1.14   itojun 	char pton[PTON_MAX];
    701  1.14   itojun 	int flags;
    702  1.14   itojun 
    703  1.14   itojun 	*res = NULL;
    704  1.14   itojun 	sentinel.ai_next = NULL;
    705  1.14   itojun 	cur = &sentinel;
    706  1.14   itojun 
    707  1.14   itojun 	/*
    708  1.14   itojun 	 * if the servname does not match socktype/protocol, ignore it.
    709  1.14   itojun 	 */
    710  1.14   itojun 	if (get_portmatch(pai, servname) != 0)
    711  1.14   itojun 		return 0;
    712  1.14   itojun 
    713  1.14   itojun 	afd = find_afd(pai->ai_family);
    714  1.14   itojun 	flags = pai->ai_flags;
    715  1.14   itojun 
    716  1.27   itojun 	switch (afd->a_af) {
    717  1.27   itojun #if 0 /*X/Open spec*/
    718  1.27   itojun 	case AF_INET:
    719  1.27   itojun 		if (inet_aton(hostname, (struct in_addr *)pton) == 1) {
    720  1.27   itojun 			if (pai->ai_family == afd->a_af ||
    721  1.27   itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    722  1.27   itojun 				GET_AI(cur->ai_next, afd, pton);
    723  1.27   itojun 				GET_PORT(cur->ai_next, servname);
    724  1.27   itojun 				while (cur && cur->ai_next)
    725  1.27   itojun 					cur = cur->ai_next;
    726  1.27   itojun 			} else
    727  1.27   itojun 				ERR(EAI_FAMILY);	/*xxx*/
    728  1.27   itojun 		}
    729  1.27   itojun 		break;
    730  1.27   itojun #endif
    731  1.27   itojun 	default:
    732  1.27   itojun 		if (inet_pton(afd->a_af, hostname, pton) == 1) {
    733  1.27   itojun 			if (pai->ai_family == afd->a_af ||
    734  1.27   itojun 			    pai->ai_family == PF_UNSPEC /*?*/) {
    735  1.27   itojun 				GET_AI(cur->ai_next, afd, pton);
    736  1.27   itojun 				GET_PORT(cur->ai_next, servname);
    737  1.27   itojun 				while (cur && cur->ai_next)
    738  1.27   itojun 					cur = cur->ai_next;
    739  1.27   itojun 			} else
    740  1.27   itojun 				ERR(EAI_FAMILY);	/*xxx*/
    741  1.27   itojun 		}
    742  1.27   itojun 		break;
    743   1.1   itojun 	}
    744   1.1   itojun 
    745  1.14   itojun 	*res = sentinel.ai_next;
    746  1.14   itojun 	return 0;
    747  1.14   itojun 
    748  1.14   itojun free:
    749  1.14   itojun bad:
    750  1.14   itojun 	if (sentinel.ai_next)
    751  1.14   itojun 		freeaddrinfo(sentinel.ai_next);
    752  1.14   itojun 	return error;
    753  1.14   itojun }
    754  1.14   itojun 
    755  1.14   itojun /*
    756  1.14   itojun  * numeric hostname with scope
    757  1.14   itojun  */
    758  1.14   itojun static int
    759  1.14   itojun explore_numeric_scope(pai, hostname, servname, res)
    760  1.14   itojun 	const struct addrinfo *pai;
    761  1.14   itojun 	const char *hostname;
    762  1.14   itojun 	const char *servname;
    763  1.14   itojun 	struct addrinfo **res;
    764  1.14   itojun {
    765  1.29   itojun #if !defined(SCOPE_DELIMITER) || !defined(INET6)
    766  1.14   itojun 	return explore_numeric(pai, hostname, servname, res);
    767  1.14   itojun #else
    768  1.14   itojun 	const struct afd *afd;
    769  1.14   itojun 	struct addrinfo *cur;
    770  1.14   itojun 	int error;
    771  1.30   itojun 	char *cp, *hostname2 = NULL, *scope, *addr;
    772  1.14   itojun 	struct sockaddr_in6 *sin6;
    773  1.14   itojun 
    774  1.14   itojun 	/*
    775  1.14   itojun 	 * if the servname does not match socktype/protocol, ignore it.
    776  1.14   itojun 	 */
    777  1.14   itojun 	if (get_portmatch(pai, servname) != 0)
    778  1.14   itojun 		return 0;
    779  1.14   itojun 
    780  1.14   itojun 	afd = find_afd(pai->ai_family);
    781  1.14   itojun 	if (!afd->a_scoped)
    782  1.14   itojun 		return explore_numeric(pai, hostname, servname, res);
    783  1.14   itojun 
    784  1.14   itojun 	cp = strchr(hostname, SCOPE_DELIMITER);
    785  1.14   itojun 	if (cp == NULL)
    786  1.14   itojun 		return explore_numeric(pai, hostname, servname, res);
    787  1.14   itojun 
    788  1.30   itojun #if 1
    789  1.14   itojun 	/*
    790  1.27   itojun 	 * Handle special case of <scope id><delimiter><scoped_address>
    791  1.14   itojun 	 */
    792  1.14   itojun 	hostname2 = strdup(hostname);
    793  1.14   itojun 	if (hostname2 == NULL)
    794  1.14   itojun 		return EAI_MEMORY;
    795  1.14   itojun 	/* terminate at the delimiter */
    796  1.14   itojun 	hostname2[cp - hostname] = '\0';
    797  1.27   itojun 	scope = hostname2;
    798  1.30   itojun 	addr = cp + 1;
    799  1.30   itojun #else
    800  1.30   itojun 	/*
    801  1.30   itojun 	 * Handle special case of <scoped_address><delimiter><scope id>
    802  1.30   itojun 	 */
    803  1.30   itojun 	hostname2 = strdup(hostname);
    804  1.30   itojun 	if (hostname2 == NULL)
    805  1.30   itojun 		return EAI_MEMORY;
    806  1.30   itojun 	/* terminate at the delimiter */
    807  1.30   itojun 	hostname2[cp - hostname] = '\0';
    808  1.30   itojun 	addr = hostname2;
    809  1.30   itojun 	scope = cp + 1;
    810  1.30   itojun #endif
    811   1.1   itojun 
    812  1.30   itojun 	error = explore_numeric(pai, addr, servname, res);
    813   1.1   itojun 	if (error == 0) {
    814  1.27   itojun 		int scopeid;
    815  1.27   itojun 
    816  1.14   itojun 		for (cur = *res; cur; cur = cur->ai_next) {
    817  1.14   itojun 			if (cur->ai_family != AF_INET6)
    818  1.14   itojun 				continue;
    819  1.14   itojun 			sin6 = (struct sockaddr_in6 *)cur->ai_addr;
    820  1.27   itojun 			if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) {
    821  1.27   itojun 				free(hostname2);
    822  1.27   itojun 				return(EAI_NONAME); /* XXX: is return OK? */
    823  1.27   itojun 			}
    824  1.27   itojun 			sin6->sin6_scope_id = scopeid;
    825  1.14   itojun 		}
    826   1.1   itojun 	}
    827  1.14   itojun 
    828  1.14   itojun 	free(hostname2);
    829  1.14   itojun 
    830   1.1   itojun 	return error;
    831  1.14   itojun #endif
    832   1.1   itojun }
    833   1.1   itojun 
    834   1.1   itojun static int
    835  1.14   itojun get_canonname(pai, ai, str)
    836  1.14   itojun 	const struct addrinfo *pai;
    837  1.14   itojun 	struct addrinfo *ai;
    838  1.14   itojun 	const char *str;
    839   1.1   itojun {
    840  1.14   itojun 	if ((pai->ai_flags & AI_CANONNAME) != 0) {
    841  1.14   itojun 		ai->ai_canonname = (char *)malloc(strlen(str) + 1);
    842  1.14   itojun 		if (ai->ai_canonname == NULL)
    843  1.14   itojun 			return EAI_MEMORY;
    844  1.14   itojun 		strcpy(ai->ai_canonname, str);
    845  1.14   itojun 	}
    846  1.14   itojun 	return 0;
    847  1.14   itojun }
    848   1.1   itojun 
    849  1.14   itojun static struct addrinfo *
    850  1.14   itojun get_ai(pai, afd, addr)
    851  1.14   itojun 	const struct addrinfo *pai;
    852  1.14   itojun 	const struct afd *afd;
    853  1.14   itojun 	const char *addr;
    854  1.14   itojun {
    855  1.14   itojun 	char *p;
    856  1.14   itojun 	struct addrinfo *ai;
    857  1.12    lukem 
    858  1.14   itojun 	ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)
    859  1.14   itojun 		+ (afd->a_socklen));
    860  1.14   itojun 	if (ai == NULL)
    861  1.14   itojun 		return NULL;
    862  1.14   itojun 
    863  1.14   itojun 	memcpy(ai, pai, sizeof(struct addrinfo));
    864  1.14   itojun 	ai->ai_addr = (struct sockaddr *)(ai + 1);
    865  1.20  mycroft 	memset(ai->ai_addr, 0, (size_t)afd->a_socklen);
    866  1.14   itojun 	ai->ai_addr->sa_len = afd->a_socklen;
    867  1.14   itojun 	ai->ai_addrlen = afd->a_socklen;
    868  1.14   itojun 	ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
    869  1.14   itojun 	p = (char *)(ai->ai_addr);
    870  1.20  mycroft 	memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);
    871  1.14   itojun 	return ai;
    872  1.14   itojun }
    873   1.1   itojun 
    874  1.14   itojun static int
    875  1.14   itojun get_portmatch(ai, servname)
    876  1.14   itojun 	const struct addrinfo *ai;
    877  1.14   itojun 	const char *servname;
    878  1.14   itojun {
    879   1.4   itojun 
    880  1.14   itojun 	/* get_port does not touch first argument. when matchonly == 1. */
    881  1.14   itojun 	return get_port((struct addrinfo *)ai, servname, 1);
    882   1.1   itojun }
    883   1.1   itojun 
    884   1.1   itojun static int
    885  1.14   itojun get_port(ai, servname, matchonly)
    886  1.14   itojun 	struct addrinfo *ai;
    887  1.14   itojun 	const char *servname;
    888  1.14   itojun 	int matchonly;
    889   1.1   itojun {
    890  1.14   itojun 	const char *proto;
    891  1.14   itojun 	struct servent *sp;
    892  1.14   itojun 	int port;
    893  1.14   itojun 	int allownumeric;
    894  1.12    lukem 
    895  1.14   itojun 	if (servname == NULL)
    896  1.14   itojun 		return 0;
    897  1.23   itojun 	switch (ai->ai_family) {
    898  1.23   itojun 	case AF_INET:
    899  1.27   itojun #ifdef AF_INET6
    900  1.23   itojun 	case AF_INET6:
    901  1.27   itojun #endif
    902  1.23   itojun 		break;
    903  1.23   itojun 	default:
    904  1.14   itojun 		return 0;
    905  1.23   itojun 	}
    906   1.1   itojun 
    907  1.14   itojun 	switch (ai->ai_socktype) {
    908  1.14   itojun 	case SOCK_RAW:
    909  1.14   itojun 		return EAI_SERVICE;
    910  1.14   itojun 	case SOCK_DGRAM:
    911  1.14   itojun 	case SOCK_STREAM:
    912  1.14   itojun 		allownumeric = 1;
    913  1.14   itojun 		break;
    914  1.14   itojun 	case ANY:
    915  1.14   itojun 		allownumeric = 0;
    916  1.14   itojun 		break;
    917  1.14   itojun 	default:
    918  1.14   itojun 		return EAI_SOCKTYPE;
    919   1.1   itojun 	}
    920  1.14   itojun 
    921  1.14   itojun 	if (str_isnumber(servname)) {
    922  1.14   itojun 		if (!allownumeric)
    923  1.14   itojun 			return EAI_SERVICE;
    924  1.14   itojun 		port = htons(atoi(servname));
    925  1.14   itojun 		if (port < 0 || port > 65535)
    926  1.14   itojun 			return EAI_SERVICE;
    927  1.14   itojun 	} else {
    928  1.14   itojun 		switch (ai->ai_socktype) {
    929  1.14   itojun 		case SOCK_DGRAM:
    930  1.14   itojun 			proto = "udp";
    931   1.1   itojun 			break;
    932  1.14   itojun 		case SOCK_STREAM:
    933  1.14   itojun 			proto = "tcp";
    934   1.1   itojun 			break;
    935   1.1   itojun 		default:
    936  1.14   itojun 			proto = NULL;
    937   1.1   itojun 			break;
    938   1.1   itojun 		}
    939  1.14   itojun 
    940  1.14   itojun 		if ((sp = getservbyname(servname, proto)) == NULL)
    941  1.14   itojun 			return EAI_SERVICE;
    942  1.14   itojun 		port = sp->s_port;
    943   1.1   itojun 	}
    944   1.1   itojun 
    945  1.14   itojun 	if (!matchonly) {
    946  1.14   itojun 		switch (ai->ai_family) {
    947   1.1   itojun 		case AF_INET:
    948  1.14   itojun 			((struct sockaddr_in *)ai->ai_addr)->sin_port = port;
    949   1.1   itojun 			break;
    950   1.1   itojun #ifdef INET6
    951  1.14   itojun 		case AF_INET6:
    952  1.14   itojun 			((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = port;
    953   1.1   itojun 			break;
    954   1.1   itojun #endif
    955   1.1   itojun 		}
    956  1.14   itojun 	}
    957  1.14   itojun 
    958  1.14   itojun 	return 0;
    959  1.14   itojun }
    960  1.14   itojun 
    961  1.14   itojun static const struct afd *
    962  1.14   itojun find_afd(af)
    963  1.14   itojun 	int af;
    964  1.14   itojun {
    965  1.14   itojun 	const struct afd *afd;
    966   1.1   itojun 
    967  1.14   itojun 	if (af == PF_UNSPEC)
    968  1.14   itojun 		return NULL;
    969  1.14   itojun 	for (afd = afdl; afd->a_af; afd++) {
    970  1.14   itojun 		if (afd->a_af == af)
    971  1.14   itojun 			return afd;
    972   1.1   itojun 	}
    973  1.14   itojun 	return NULL;
    974   1.1   itojun }
    975  1.27   itojun 
    976  1.27   itojun /*
    977  1.27   itojun  * post-2553: AI_ADDRCONFIG check.  if we use getipnodeby* as backend, backend
    978  1.27   itojun  * will take care of it.
    979  1.27   itojun  * the semantics of AI_ADDRCONFIG is not defined well.  we are not sure
    980  1.27   itojun  * if the code is right or not.
    981  1.27   itojun  */
    982  1.27   itojun static int
    983  1.27   itojun addrconfig(pai)
    984  1.27   itojun 	const struct addrinfo *pai;
    985  1.27   itojun {
    986  1.27   itojun 	int s;
    987  1.27   itojun 
    988  1.27   itojun 	/* XXX errno */
    989  1.27   itojun 	s = socket(pai->ai_family, SOCK_DGRAM, 0);
    990  1.27   itojun 	if (s < 0)
    991  1.27   itojun 		return 0;
    992  1.27   itojun 	close(s);
    993  1.27   itojun 	return 1;
    994  1.27   itojun }
    995  1.27   itojun 
    996  1.27   itojun #ifdef INET6
    997  1.27   itojun /* convert a string to a scope identifier. XXX: IPv6 specific */
    998  1.27   itojun static int
    999  1.27   itojun ip6_str2scopeid(scope, sin6)
   1000  1.27   itojun 	char *scope;
   1001  1.27   itojun 	struct sockaddr_in6 *sin6;
   1002  1.27   itojun {
   1003  1.27   itojun 	int scopeid;
   1004  1.27   itojun 	struct in6_addr *a6 = &sin6->sin6_addr;
   1005  1.27   itojun 	char *ep;
   1006  1.27   itojun 
   1007  1.27   itojun 	if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
   1008  1.27   itojun 		/*
   1009  1.27   itojun 		 * We currently assume a one-to-one mapping between links
   1010  1.27   itojun 		 * and interfaces, so we simply use interface indices for
   1011  1.27   itojun 		 * like-local scopes.
   1012  1.27   itojun 		 */
   1013  1.27   itojun 		scopeid = if_nametoindex(scope);
   1014  1.27   itojun 		if (scopeid == 0)
   1015  1.27   itojun 			goto trynumeric;
   1016  1.27   itojun 		return(scopeid);
   1017  1.27   itojun 	}
   1018  1.27   itojun 
   1019  1.27   itojun 	/* still unclear about literal, allow numeric only - placeholder */
   1020  1.27   itojun 	if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))
   1021  1.27   itojun 		goto trynumeric;
   1022  1.27   itojun 	if (IN6_IS_ADDR_MC_ORGLOCAL(a6))
   1023  1.27   itojun 		goto trynumeric;
   1024  1.27   itojun 	else
   1025  1.27   itojun 		goto trynumeric;	/* global */
   1026  1.27   itojun 
   1027  1.27   itojun 	/* try to convert to a numeric id as a last resort */
   1028  1.27   itojun   trynumeric:
   1029  1.27   itojun 	scopeid = (int)strtoul(scope, &ep, 10);
   1030  1.27   itojun 	if (*ep == '\0')
   1031  1.27   itojun 		return scopeid;
   1032  1.27   itojun 	else
   1033  1.27   itojun 		return -1;
   1034  1.27   itojun }
   1035  1.27   itojun #endif
   1036