Home | History | Annotate | Line # | Download | only in net
getaddrinfo.c revision 1.9
      1  1.9  itojun /*	$NetBSD: getaddrinfo.c,v 1.9 1999/07/14 22:10:03 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  * "#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.1  itojun  *   in the source code.  This is because RFC2133 is silent about which error
     39  1.1  itojun  *   code must be returned for which situation.
     40  1.1  itojun  * - PF_UNSPEC case would be handled in getipnodebyname() with the AI_ALL flag.
     41  1.1  itojun  */
     42  1.1  itojun 
     43  1.1  itojun #include <sys/types.h>
     44  1.1  itojun #include <sys/param.h>
     45  1.1  itojun #include <sys/sysctl.h>
     46  1.1  itojun #include <sys/socket.h>
     47  1.1  itojun #include <netinet/in.h>
     48  1.1  itojun #include <arpa/inet.h>
     49  1.1  itojun #include <arpa/nameser.h>
     50  1.1  itojun #include <netdb.h>
     51  1.1  itojun #include <resolv.h>
     52  1.1  itojun #include <string.h>
     53  1.1  itojun #include <stdlib.h>
     54  1.1  itojun #include <stddef.h>
     55  1.1  itojun #include <ctype.h>
     56  1.1  itojun #include <unistd.h>
     57  1.1  itojun 
     58  1.1  itojun #if 0	/*quickhack*/
     59  1.1  itojun #if defined(__KAME__) && defined(INET6)
     60  1.1  itojun # define FAITH
     61  1.1  itojun #endif
     62  1.1  itojun #endif
     63  1.1  itojun 
     64  1.1  itojun #define SUCCESS 0
     65  1.1  itojun #define ANY 0
     66  1.1  itojun #define YES 1
     67  1.1  itojun #define NO  0
     68  1.1  itojun 
     69  1.1  itojun #ifdef FAITH
     70  1.1  itojun static int translate = NO;
     71  1.1  itojun static struct in6_addr faith_prefix = IN6ADDR_ANY_INIT;
     72  1.1  itojun #endif
     73  1.1  itojun 
     74  1.1  itojun static const char in_addrany[] = { 0, 0, 0, 0 };
     75  1.1  itojun static const char in6_addrany[] = {
     76  1.1  itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     77  1.1  itojun };
     78  1.1  itojun static const char in_loopback[] = { 127, 0, 0, 1 };
     79  1.1  itojun static const char in6_loopback[] = {
     80  1.1  itojun 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
     81  1.1  itojun };
     82  1.1  itojun 
     83  1.1  itojun struct sockinet {
     84  1.1  itojun 	u_char	si_len;
     85  1.1  itojun 	u_char	si_family;
     86  1.1  itojun 	u_short	si_port;
     87  1.1  itojun };
     88  1.1  itojun 
     89  1.1  itojun static struct afd {
     90  1.1  itojun 	int a_af;
     91  1.1  itojun 	int a_addrlen;
     92  1.1  itojun 	int a_socklen;
     93  1.1  itojun 	int a_off;
     94  1.1  itojun 	const char *a_addrany;
     95  1.1  itojun 	const char *a_loopback;
     96  1.1  itojun } afdl [] = {
     97  1.1  itojun #ifdef INET6
     98  1.1  itojun #define N_INET6 0
     99  1.1  itojun 	{PF_INET6, sizeof(struct in6_addr),
    100  1.1  itojun 	 sizeof(struct sockaddr_in6),
    101  1.1  itojun 	 offsetof(struct sockaddr_in6, sin6_addr),
    102  1.1  itojun 	 in6_addrany, in6_loopback},
    103  1.1  itojun #define N_INET  1
    104  1.1  itojun #else
    105  1.1  itojun #define N_INET  0
    106  1.1  itojun #endif
    107  1.1  itojun 	{PF_INET, sizeof(struct in_addr),
    108  1.1  itojun 	 sizeof(struct sockaddr_in),
    109  1.1  itojun 	 offsetof(struct sockaddr_in, sin_addr),
    110  1.1  itojun 	 in_addrany, in_loopback},
    111  1.1  itojun 	{0, 0, 0, 0, NULL, NULL},
    112  1.1  itojun };
    113  1.1  itojun 
    114  1.1  itojun #ifdef INET6
    115  1.1  itojun #define PTON_MAX	16
    116  1.1  itojun #else
    117  1.1  itojun #define PTON_MAX	4
    118  1.1  itojun #endif
    119  1.1  itojun 
    120  1.1  itojun 
    121  1.1  itojun static int get_name __P((const char *, struct afd *,
    122  1.1  itojun 			  struct addrinfo **, char *, struct addrinfo *,
    123  1.1  itojun 			  int));
    124  1.1  itojun static int get_addr __P((const char *, int, struct addrinfo **,
    125  1.1  itojun 			struct addrinfo *, int));
    126  1.1  itojun static int get_addr0 __P((const char *, int, struct addrinfo **,
    127  1.1  itojun 			struct addrinfo *, int));
    128  1.1  itojun static int str_isnumber __P((const char *));
    129  1.1  itojun 
    130  1.1  itojun static char *ai_errlist[] = {
    131  1.7   lukem 	"Success",
    132  1.7   lukem 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
    133  1.7   lukem 	"Temporary failure in name resolution",		/* EAI_AGAIN      */
    134  1.7   lukem 	"Invalid value for ai_flags",		       	/* EAI_BADFLAGS   */
    135  1.7   lukem 	"Non-recoverable failure in name resolution", 	/* EAI_FAIL       */
    136  1.2   lukem 	"ai_family not supported",			/* EAI_FAMILY     */
    137  1.7   lukem 	"Memory allocation failure", 			/* EAI_MEMORY     */
    138  1.7   lukem 	"No address associated with hostname", 		/* EAI_NODATA     */
    139  1.2   lukem 	"hostname nor servname provided, or not known",/* EAI_NONAME     */
    140  1.2   lukem 	"servname not supported for ai_socktype",	/* EAI_SERVICE    */
    141  1.2   lukem 	"ai_socktype not supported", 			/* EAI_SOCKTYPE   */
    142  1.7   lukem 	"System error returned in errno", 		/* EAI_SYSTEM     */
    143  1.7   lukem 	"Invalid value for hints",			/* EAI_BADHINTS	  */
    144  1.7   lukem 	"Resolved protocol is unknown",			/* EAI_PROTOCOL   */
    145  1.7   lukem 	"Unknown error", 				/* EAI_MAX        */
    146  1.1  itojun };
    147  1.1  itojun 
    148  1.1  itojun #define GET_CANONNAME(ai, str) \
    149  1.1  itojun if (pai->ai_flags & AI_CANONNAME) {\
    150  1.1  itojun 	if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\
    151  1.1  itojun 		strcpy((ai)->ai_canonname, (str));\
    152  1.1  itojun 	} else {\
    153  1.1  itojun 		error = EAI_MEMORY;\
    154  1.1  itojun 		goto free;\
    155  1.1  itojun 	}\
    156  1.1  itojun }
    157  1.1  itojun 
    158  1.1  itojun #define GET_AI(ai, afd, addr, port) {\
    159  1.1  itojun 	char *p;\
    160  1.1  itojun 	if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
    161  1.1  itojun 					      ((afd)->a_socklen)))\
    162  1.1  itojun 	    == NULL) {\
    163  1.1  itojun 		error = EAI_MEMORY;\
    164  1.1  itojun 		goto free;\
    165  1.1  itojun 	}\
    166  1.1  itojun 	memcpy(ai, pai, sizeof(struct addrinfo));\
    167  1.1  itojun 	(ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
    168  1.1  itojun 	memset((ai)->ai_addr, 0, (afd)->a_socklen);\
    169  1.1  itojun 	(ai)->ai_addr->sa_len = (ai)->ai_addrlen = (afd)->a_socklen;\
    170  1.1  itojun 	(ai)->ai_addr->sa_family = (ai)->ai_family = (afd)->a_af;\
    171  1.1  itojun 	((struct sockinet *)(ai)->ai_addr)->si_port = port;\
    172  1.1  itojun 	p = (char *)((ai)->ai_addr);\
    173  1.1  itojun 	memcpy(p + (afd)->a_off, (addr), (afd)->a_addrlen);\
    174  1.1  itojun }
    175  1.1  itojun 
    176  1.1  itojun #define ERR(err) { error = (err); goto bad; }
    177  1.1  itojun 
    178  1.1  itojun char *
    179  1.1  itojun gai_strerror(ecode)
    180  1.1  itojun 	int ecode;
    181  1.1  itojun {
    182  1.1  itojun 	if (ecode < 0 || ecode > EAI_MAX)
    183  1.1  itojun 		ecode = EAI_MAX;
    184  1.1  itojun 	return ai_errlist[ecode];
    185  1.1  itojun }
    186  1.1  itojun 
    187  1.1  itojun void
    188  1.1  itojun freeaddrinfo(ai)
    189  1.1  itojun 	struct addrinfo *ai;
    190  1.1  itojun {
    191  1.1  itojun 	struct addrinfo *next;
    192  1.1  itojun 
    193  1.1  itojun 	do {
    194  1.1  itojun 		next = ai->ai_next;
    195  1.1  itojun 		if (ai->ai_canonname)
    196  1.1  itojun 			free(ai->ai_canonname);
    197  1.1  itojun 		/* no need to free(ai->ai_addr) */
    198  1.1  itojun 		free(ai);
    199  1.1  itojun 	} while ((ai = next) != NULL);
    200  1.1  itojun }
    201  1.1  itojun 
    202  1.1  itojun static int
    203  1.1  itojun str_isnumber(p)
    204  1.1  itojun 	const char *p;
    205  1.1  itojun {
    206  1.1  itojun 	char *q = (char *)p;
    207  1.1  itojun 	while (*q) {
    208  1.1  itojun 		if (! isdigit(*q))
    209  1.1  itojun 			return NO;
    210  1.1  itojun 		q++;
    211  1.1  itojun 	}
    212  1.1  itojun 	return YES;
    213  1.1  itojun }
    214  1.1  itojun 
    215  1.1  itojun int
    216  1.1  itojun getaddrinfo(hostname, servname, hints, res)
    217  1.1  itojun 	const char *hostname, *servname;
    218  1.1  itojun 	const struct addrinfo *hints;
    219  1.1  itojun 	struct addrinfo **res;
    220  1.1  itojun {
    221  1.1  itojun 	struct addrinfo sentinel;
    222  1.1  itojun 	struct addrinfo *top = NULL;
    223  1.1  itojun 	struct addrinfo *cur;
    224  1.1  itojun 	int i, error = 0;
    225  1.1  itojun 	char pton[PTON_MAX];
    226  1.1  itojun 	struct addrinfo ai;
    227  1.1  itojun 	struct addrinfo *pai;
    228  1.1  itojun 	u_short port;
    229  1.1  itojun 
    230  1.1  itojun #ifdef FAITH
    231  1.1  itojun 	static int firsttime = 1;
    232  1.1  itojun 
    233  1.1  itojun 	if (firsttime) {
    234  1.1  itojun 		/* translator hack */
    235  1.1  itojun 		{
    236  1.1  itojun 			char *q = getenv("GAI");
    237  1.1  itojun 			if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1)
    238  1.1  itojun 				translate = YES;
    239  1.1  itojun 		}
    240  1.1  itojun 		firsttime = 0;
    241  1.1  itojun 	}
    242  1.1  itojun #endif
    243  1.1  itojun 
    244  1.1  itojun 	/* initialize file static vars */
    245  1.1  itojun 	sentinel.ai_next = NULL;
    246  1.1  itojun 	cur = &sentinel;
    247  1.1  itojun 	pai = &ai;
    248  1.1  itojun 	pai->ai_flags = 0;
    249  1.1  itojun 	pai->ai_family = PF_UNSPEC;
    250  1.1  itojun 	pai->ai_socktype = ANY;
    251  1.1  itojun 	pai->ai_protocol = ANY;
    252  1.1  itojun 	pai->ai_addrlen = 0;
    253  1.1  itojun 	pai->ai_canonname = NULL;
    254  1.1  itojun 	pai->ai_addr = NULL;
    255  1.1  itojun 	pai->ai_next = NULL;
    256  1.1  itojun 	port = ANY;
    257  1.1  itojun 
    258  1.1  itojun 	if (hostname == NULL && servname == NULL)
    259  1.1  itojun 		return EAI_NONAME;
    260  1.1  itojun 	if (hints) {
    261  1.1  itojun 		/* error check for hints */
    262  1.1  itojun 		if (hints->ai_addrlen || hints->ai_canonname ||
    263  1.1  itojun 		    hints->ai_addr || hints->ai_next)
    264  1.1  itojun 			ERR(EAI_BADHINTS); /* xxx */
    265  1.1  itojun 		if (hints->ai_flags & ~AI_MASK)
    266  1.1  itojun 			ERR(EAI_BADFLAGS);
    267  1.1  itojun 		switch (hints->ai_family) {
    268  1.1  itojun 		case PF_UNSPEC:
    269  1.1  itojun 		case PF_INET:
    270  1.1  itojun #ifdef INET6
    271  1.1  itojun 		case PF_INET6:
    272  1.1  itojun #endif
    273  1.1  itojun 			break;
    274  1.1  itojun 		default:
    275  1.1  itojun 			ERR(EAI_FAMILY);
    276  1.1  itojun 		}
    277  1.1  itojun 		memcpy(pai, hints, sizeof(*pai));
    278  1.1  itojun 		switch (pai->ai_socktype) {
    279  1.1  itojun 		case ANY:
    280  1.1  itojun 			switch (pai->ai_protocol) {
    281  1.1  itojun 			case ANY:
    282  1.1  itojun 				break;
    283  1.1  itojun 			case IPPROTO_UDP:
    284  1.1  itojun 				pai->ai_socktype = SOCK_DGRAM;
    285  1.1  itojun 				break;
    286  1.1  itojun 			case IPPROTO_TCP:
    287  1.1  itojun 				pai->ai_socktype = SOCK_STREAM;
    288  1.1  itojun 				break;
    289  1.1  itojun 			default:
    290  1.1  itojun 				pai->ai_socktype = SOCK_RAW;
    291  1.1  itojun 				break;
    292  1.1  itojun 			}
    293  1.1  itojun 			break;
    294  1.1  itojun 		case SOCK_RAW:
    295  1.1  itojun 			break;
    296  1.1  itojun 		case SOCK_DGRAM:
    297  1.1  itojun 			if (pai->ai_protocol != IPPROTO_UDP &&
    298  1.1  itojun 			    pai->ai_protocol != ANY)
    299  1.1  itojun 				ERR(EAI_BADHINTS);	/*xxx*/
    300  1.1  itojun 			pai->ai_protocol = IPPROTO_UDP;
    301  1.1  itojun 			break;
    302  1.1  itojun 		case SOCK_STREAM:
    303  1.1  itojun 			if (pai->ai_protocol != IPPROTO_TCP &&
    304  1.1  itojun 			    pai->ai_protocol != ANY)
    305  1.1  itojun 				ERR(EAI_BADHINTS);	/*xxx*/
    306  1.1  itojun 			pai->ai_protocol = IPPROTO_TCP;
    307  1.1  itojun 			break;
    308  1.1  itojun 		default:
    309  1.1  itojun 			ERR(EAI_SOCKTYPE);
    310  1.1  itojun 			break;
    311  1.1  itojun 		}
    312  1.1  itojun 	}
    313  1.1  itojun 
    314  1.1  itojun 	/*
    315  1.1  itojun 	 * service port
    316  1.1  itojun 	 */
    317  1.1  itojun 	if (servname) {
    318  1.1  itojun 		if (str_isnumber(servname)) {
    319  1.1  itojun 			if (pai->ai_socktype == ANY) {
    320  1.1  itojun 				/* caller accept *ANY* socktype */
    321  1.1  itojun 				pai->ai_socktype = SOCK_DGRAM;
    322  1.1  itojun 				pai->ai_protocol = IPPROTO_UDP;
    323  1.1  itojun 			}
    324  1.1  itojun 			port = htons(atoi(servname));
    325  1.1  itojun 		} else {
    326  1.1  itojun 			struct servent *sp;
    327  1.1  itojun 			char *proto;
    328  1.1  itojun 
    329  1.1  itojun 			proto = NULL;
    330  1.1  itojun 			switch (pai->ai_socktype) {
    331  1.1  itojun 			case ANY:
    332  1.1  itojun 				proto = NULL;
    333  1.1  itojun 				break;
    334  1.1  itojun 			case SOCK_DGRAM:
    335  1.1  itojun 				proto = "udp";
    336  1.1  itojun 				break;
    337  1.1  itojun 			case SOCK_STREAM:
    338  1.1  itojun 				proto = "tcp";
    339  1.1  itojun 				break;
    340  1.1  itojun 			default:
    341  1.1  itojun 				fprintf(stderr, "panic!\n");
    342  1.1  itojun 				break;
    343  1.1  itojun 			}
    344  1.1  itojun 			if ((sp = getservbyname(servname, proto)) == NULL)
    345  1.1  itojun 				ERR(EAI_SERVICE);
    346  1.1  itojun 			port = sp->s_port;
    347  1.1  itojun 			if (pai->ai_socktype == ANY) {
    348  1.1  itojun 				if (strcmp(sp->s_proto, "udp") == 0) {
    349  1.1  itojun 					pai->ai_socktype = SOCK_DGRAM;
    350  1.1  itojun 					pai->ai_protocol = IPPROTO_UDP;
    351  1.1  itojun 				} else if (strcmp(sp->s_proto, "tcp") == 0) {
    352  1.1  itojun 					pai->ai_socktype = SOCK_STREAM;
    353  1.1  itojun 					pai->ai_protocol = IPPROTO_TCP;
    354  1.1  itojun 				} else
    355  1.1  itojun 					ERR(EAI_PROTOCOL);	/*xxx*/
    356  1.1  itojun 			}
    357  1.1  itojun 		}
    358  1.1  itojun 	}
    359  1.1  itojun 
    360  1.1  itojun 	/*
    361  1.1  itojun 	 * hostname == NULL.
    362  1.1  itojun 	 * passive socket -> anyaddr (0.0.0.0 or ::)
    363  1.1  itojun 	 * non-passive socket -> localhost (127.0.0.1 or ::1)
    364  1.1  itojun 	 */
    365  1.1  itojun 	if (hostname == NULL) {
    366  1.1  itojun 		struct afd *afd;
    367  1.1  itojun 		int s;
    368  1.1  itojun 
    369  1.1  itojun 		for (afd = &afdl[0]; afd->a_af; afd++) {
    370  1.1  itojun 			if (!(pai->ai_family == PF_UNSPEC
    371  1.1  itojun 			   || pai->ai_family == afd->a_af)) {
    372  1.1  itojun 				continue;
    373  1.1  itojun 			}
    374  1.1  itojun 
    375  1.1  itojun 			/*
    376  1.1  itojun 			 * filter out AFs that are not supported by the kernel
    377  1.1  itojun 			 * XXX errno?
    378  1.1  itojun 			 */
    379  1.1  itojun 			s = socket(afd->a_af, SOCK_DGRAM, 0);
    380  1.1  itojun 			if (s < 0)
    381  1.1  itojun 				continue;
    382  1.1  itojun 			close(s);
    383  1.1  itojun 
    384  1.1  itojun 			if (pai->ai_flags & AI_PASSIVE) {
    385  1.1  itojun 				GET_AI(cur->ai_next, afd, afd->a_addrany, port);
    386  1.1  itojun 				/* xxx meaningless?
    387  1.1  itojun 				 * GET_CANONNAME(cur->ai_next, "anyaddr");
    388  1.1  itojun 				 */
    389  1.1  itojun 			} else {
    390  1.1  itojun 				GET_AI(cur->ai_next, afd, afd->a_loopback,
    391  1.1  itojun 					port);
    392  1.1  itojun 				/* xxx meaningless?
    393  1.1  itojun 				 * GET_CANONNAME(cur->ai_next, "localhost");
    394  1.1  itojun 				 */
    395  1.1  itojun 			}
    396  1.1  itojun 			cur = cur->ai_next;
    397  1.1  itojun 		}
    398  1.1  itojun 		top = sentinel.ai_next;
    399  1.1  itojun 		if (top)
    400  1.1  itojun 			goto good;
    401  1.1  itojun 		else
    402  1.1  itojun 			ERR(EAI_FAMILY);
    403  1.1  itojun 	}
    404  1.1  itojun 
    405  1.1  itojun 	/* hostname as numeric name */
    406  1.1  itojun 	for (i = 0; afdl[i].a_af; i++) {
    407  1.1  itojun 		if (inet_pton(afdl[i].a_af, hostname, pton)) {
    408  1.1  itojun 			u_long v4a;
    409  1.1  itojun 			u_char pfx;
    410  1.1  itojun 
    411  1.1  itojun 			switch (afdl[i].a_af) {
    412  1.1  itojun 			case AF_INET:
    413  1.1  itojun 				v4a = ntohl(((struct in_addr *)pton)->s_addr);
    414  1.1  itojun 				if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    415  1.1  itojun 					pai->ai_flags &= ~AI_CANONNAME;
    416  1.1  itojun 				v4a >>= IN_CLASSA_NSHIFT;
    417  1.1  itojun 				if (v4a == 0 || v4a == IN_LOOPBACKNET)
    418  1.1  itojun 					pai->ai_flags &= ~AI_CANONNAME;
    419  1.1  itojun 				break;
    420  1.1  itojun #ifdef INET6
    421  1.1  itojun 			case AF_INET6:
    422  1.1  itojun 				pfx = ((struct in6_addr *)pton)->s6_addr8[0];
    423  1.1  itojun 				if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
    424  1.1  itojun 					pai->ai_flags &= ~AI_CANONNAME;
    425  1.1  itojun 				break;
    426  1.1  itojun #endif
    427  1.1  itojun 			}
    428  1.1  itojun 
    429  1.1  itojun 			if (pai->ai_family == afdl[i].a_af ||
    430  1.1  itojun 			    pai->ai_family == PF_UNSPEC) {
    431  1.1  itojun 				if (! (pai->ai_flags & AI_CANONNAME)) {
    432  1.1  itojun 					GET_AI(top, &afdl[i], pton, port);
    433  1.1  itojun 					goto good;
    434  1.1  itojun 				}
    435  1.1  itojun 				/*
    436  1.1  itojun 				 * if AI_CANONNAME and if reverse lookup
    437  1.1  itojun 				 * fail, return ai anyway to pacify
    438  1.1  itojun 				 * calling application.
    439  1.1  itojun 				 *
    440  1.1  itojun 				 * XXX getaddrinfo() is a name->address
    441  1.1  itojun 				 * translation function, and it looks strange
    442  1.1  itojun 				 * that we do addr->name translation here.
    443  1.1  itojun 				 */
    444  1.1  itojun 				get_name(pton, &afdl[i], &top, pton, pai, port);
    445  1.1  itojun 				goto good;
    446  1.1  itojun 			} else
    447  1.1  itojun 				ERR(EAI_FAMILY);	/*xxx*/
    448  1.1  itojun 		}
    449  1.1  itojun 	}
    450  1.1  itojun 
    451  1.1  itojun 	if (pai->ai_flags & AI_NUMERICHOST)
    452  1.1  itojun 		ERR(EAI_NONAME);
    453  1.1  itojun 
    454  1.1  itojun 	/* hostname as alphabetical name */
    455  1.1  itojun 	error = get_addr(hostname, pai->ai_family, &top, pai, port);
    456  1.1  itojun 	if (error == 0) {
    457  1.1  itojun 		if (top) {
    458  1.1  itojun  good:
    459  1.1  itojun 			*res = top;
    460  1.1  itojun 			return SUCCESS;
    461  1.1  itojun 		} else
    462  1.1  itojun 			error = EAI_FAIL;
    463  1.1  itojun 	}
    464  1.1  itojun  free:
    465  1.1  itojun 	if (top)
    466  1.1  itojun 		freeaddrinfo(top);
    467  1.1  itojun  bad:
    468  1.1  itojun 	*res = NULL;
    469  1.1  itojun 	return error;
    470  1.1  itojun }
    471  1.1  itojun 
    472  1.1  itojun static int
    473  1.1  itojun get_name(addr, afd, res, numaddr, pai, port0)
    474  1.1  itojun 	const char *addr;
    475  1.1  itojun 	struct afd *afd;
    476  1.1  itojun 	struct addrinfo **res;
    477  1.1  itojun 	char *numaddr;
    478  1.1  itojun 	struct addrinfo *pai;
    479  1.1  itojun 	int port0;
    480  1.1  itojun {
    481  1.1  itojun 	u_short port = port0 & 0xffff;
    482  1.1  itojun 	struct hostent *hp;
    483  1.1  itojun 	struct addrinfo *cur;
    484  1.1  itojun 	int error = 0;
    485  1.1  itojun #ifdef USE_GETIPNODEBY
    486  1.1  itojun 	int h_error;
    487  1.1  itojun 
    488  1.1  itojun 	hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
    489  1.1  itojun #else
    490  1.1  itojun 	hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    491  1.1  itojun #endif
    492  1.1  itojun 	if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
    493  1.1  itojun 		GET_AI(cur, afd, hp->h_addr_list[0], port);
    494  1.1  itojun 		GET_CANONNAME(cur, hp->h_name);
    495  1.1  itojun 	} else
    496  1.1  itojun 		GET_AI(cur, afd, numaddr, port);
    497  1.1  itojun 
    498  1.1  itojun #ifdef USE_GETIPNODEBY
    499  1.1  itojun 	if (hp)
    500  1.1  itojun 		freehostent(hp);
    501  1.1  itojun #endif
    502  1.1  itojun 	*res = cur;
    503  1.1  itojun 	return SUCCESS;
    504  1.1  itojun  free:
    505  1.1  itojun 	if (cur)
    506  1.1  itojun 		freeaddrinfo(cur);
    507  1.1  itojun #ifdef USE_GETIPNODEBY
    508  1.1  itojun 	if (hp)
    509  1.1  itojun 		freehostent(hp);
    510  1.1  itojun #endif
    511  1.1  itojun  /* bad: */
    512  1.1  itojun 	*res = NULL;
    513  1.1  itojun 	return error;
    514  1.1  itojun }
    515  1.1  itojun 
    516  1.1  itojun static int
    517  1.4  itojun get_addr(hostname, af, res0, pai, port0)
    518  1.1  itojun 	const char *hostname;
    519  1.1  itojun 	int af;
    520  1.4  itojun 	struct addrinfo **res0;
    521  1.1  itojun 	struct addrinfo *pai;
    522  1.1  itojun 	int port0;
    523  1.1  itojun {
    524  1.3  itojun #ifdef USE_GETIPNODEBY
    525  1.5  itojun 	return get_addr0(hostname, af, res0, pai, port0);
    526  1.3  itojun #else
    527  1.4  itojun 	int i, error, ekeep;
    528  1.1  itojun 	struct addrinfo *cur;
    529  1.4  itojun 	struct addrinfo **res;
    530  1.9  itojun 	int retry;
    531  1.9  itojun 	int s;
    532  1.1  itojun 
    533  1.4  itojun 	res = res0;
    534  1.4  itojun 	ekeep = 0;
    535  1.1  itojun 	for (i = 0; afdl[i].a_af; i++) {
    536  1.9  itojun 		retry = 0;
    537  1.9  itojun 		if (af == AF_UNSPEC) {
    538  1.9  itojun 			/*
    539  1.9  itojun 			 * filter out AFs that are not supported by the kernel
    540  1.9  itojun 			 * XXX errno?
    541  1.9  itojun 			 */
    542  1.9  itojun 			s = socket(afdl[i].a_af, SOCK_DGRAM, 0);
    543  1.9  itojun 			if (s < 0)
    544  1.9  itojun 				continue;
    545  1.9  itojun 			close(s);
    546  1.9  itojun 		} else {
    547  1.9  itojun 			if (af != afdl[i].a_af)
    548  1.9  itojun 				continue;
    549  1.9  itojun 		}
    550  1.4  itojun 		/* It is WRONG, we need getipnodebyname(). */
    551  1.4  itojun again:
    552  1.1  itojun 		error = get_addr0(hostname, afdl[i].a_af, res, pai, port0);
    553  1.4  itojun 		switch (error) {
    554  1.4  itojun 		case EAI_AGAIN:
    555  1.9  itojun 			if (++retry < 3)
    556  1.9  itojun 				goto again;
    557  1.9  itojun 			/* FALL THROUGH*/
    558  1.4  itojun 		default:
    559  1.4  itojun 			if (ekeep == 0)
    560  1.4  itojun 				ekeep = error;
    561  1.4  itojun 			break;
    562  1.4  itojun 		}
    563  1.1  itojun 		if (*res) {
    564  1.1  itojun 			/* make chain of addrs */
    565  1.1  itojun 			for (cur = *res;
    566  1.1  itojun 			     cur && cur->ai_next;
    567  1.1  itojun 			     cur = cur->ai_next)
    568  1.1  itojun 				;
    569  1.1  itojun 			if (!cur)
    570  1.1  itojun 				return EAI_FAIL;
    571  1.1  itojun 			res = &cur->ai_next;
    572  1.1  itojun 		}
    573  1.1  itojun 	}
    574  1.1  itojun 
    575  1.4  itojun 	/* if we got something, it's okay */
    576  1.4  itojun 	if (*res0)
    577  1.4  itojun 		return 0;
    578  1.4  itojun 
    579  1.4  itojun 	return error ? error : ekeep;
    580  1.1  itojun #endif
    581  1.1  itojun }
    582  1.1  itojun 
    583  1.1  itojun static int
    584  1.1  itojun get_addr0(hostname, af, res, pai, port0)
    585  1.1  itojun 	const char *hostname;
    586  1.1  itojun 	int af;
    587  1.1  itojun 	struct addrinfo **res;
    588  1.1  itojun 	struct addrinfo *pai;
    589  1.1  itojun 	int port0;
    590  1.1  itojun {
    591  1.1  itojun 	u_short port = port0 & 0xffff;
    592  1.1  itojun 	struct addrinfo sentinel;
    593  1.1  itojun 	struct hostent *hp;
    594  1.1  itojun 	struct addrinfo *top, *cur;
    595  1.1  itojun 	struct afd *afd;
    596  1.1  itojun 	int i, error = 0, h_error;
    597  1.1  itojun 	char *ap;
    598  1.1  itojun #ifndef USE_GETIPNODEBY
    599  1.1  itojun 	extern int h_errno;
    600  1.1  itojun #endif
    601  1.1  itojun 
    602  1.1  itojun 	top = NULL;
    603  1.1  itojun 	sentinel.ai_next = NULL;
    604  1.1  itojun 	cur = &sentinel;
    605  1.1  itojun #ifdef USE_GETIPNODEBY
    606  1.1  itojun 	if (af == AF_UNSPEC) {
    607  1.1  itojun 		hp = getipnodebyname(hostname, AF_INET6,
    608  1.1  itojun 				AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error);
    609  1.1  itojun 	} else
    610  1.1  itojun 		hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error);
    611  1.1  itojun #else
    612  1.1  itojun 	if (af == AF_UNSPEC) {
    613  1.1  itojun 		error = EAI_FAIL;
    614  1.1  itojun 		goto bad;
    615  1.1  itojun 	}
    616  1.1  itojun 	hp = gethostbyname2(hostname, af);
    617  1.1  itojun 	h_error = h_errno;
    618  1.1  itojun #endif
    619  1.1  itojun 	if (hp == NULL) {
    620  1.1  itojun 		switch (h_error) {
    621  1.1  itojun 		case HOST_NOT_FOUND:
    622  1.1  itojun 		case NO_DATA:
    623  1.1  itojun 			error = EAI_NODATA;
    624  1.1  itojun 			break;
    625  1.1  itojun 		case TRY_AGAIN:
    626  1.1  itojun 			error = EAI_AGAIN;
    627  1.1  itojun 			break;
    628  1.1  itojun 		case NO_RECOVERY:
    629  1.9  itojun 		case NETDB_INTERNAL:
    630  1.1  itojun 		default:
    631  1.1  itojun 			error = EAI_FAIL;
    632  1.1  itojun 			break;
    633  1.1  itojun 		}
    634  1.1  itojun 		goto bad;
    635  1.1  itojun 	}
    636  1.1  itojun 
    637  1.1  itojun 	if ((hp->h_name == NULL) || (hp->h_name[0] == 0) ||
    638  1.1  itojun 	    (hp->h_addr_list[0] == NULL))
    639  1.1  itojun 		ERR(EAI_FAIL);
    640  1.1  itojun 
    641  1.1  itojun 	for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) {
    642  1.1  itojun 		switch (af) {
    643  1.1  itojun #ifdef INET6
    644  1.1  itojun 		case AF_INET6:
    645  1.1  itojun 			afd = &afdl[N_INET6];
    646  1.1  itojun 			break;
    647  1.1  itojun #endif
    648  1.1  itojun #ifndef INET6
    649  1.1  itojun 		default:	/* AF_UNSPEC */
    650  1.1  itojun #endif
    651  1.1  itojun 		case AF_INET:
    652  1.1  itojun 			afd = &afdl[N_INET];
    653  1.1  itojun 			break;
    654  1.1  itojun #ifdef INET6
    655  1.1  itojun 		default:	/* AF_UNSPEC */
    656  1.1  itojun 			if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
    657  1.1  itojun 				ap += sizeof(struct in6_addr) -
    658  1.1  itojun 					sizeof(struct in_addr);
    659  1.1  itojun 				afd = &afdl[N_INET];
    660  1.1  itojun 			} else
    661  1.1  itojun 				afd = &afdl[N_INET6];
    662  1.1  itojun 			break;
    663  1.1  itojun #endif
    664  1.1  itojun 		}
    665  1.1  itojun #ifdef FAITH
    666  1.1  itojun 		if (translate && afd->a_af == AF_INET) {
    667  1.1  itojun 			struct in6_addr *in6;
    668  1.1  itojun 
    669  1.1  itojun 			GET_AI(cur->ai_next, &afdl[N_INET6], ap, port);
    670  1.1  itojun 			in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr;
    671  1.1  itojun 			memcpy(&in6->s6_addr32[0], &faith_prefix,
    672  1.1  itojun 			    sizeof(struct in6_addr) - sizeof(struct in_addr));
    673  1.1  itojun 			memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr));
    674  1.1  itojun 		} else
    675  1.1  itojun #endif /* FAITH */
    676  1.1  itojun 		GET_AI(cur->ai_next, afd, ap, port);
    677  1.1  itojun 		if (cur == &sentinel) {
    678  1.1  itojun 			top = cur->ai_next;
    679  1.1  itojun 			GET_CANONNAME(top, hp->h_name);
    680  1.1  itojun 		}
    681  1.1  itojun 		cur = cur->ai_next;
    682  1.1  itojun 	}
    683  1.1  itojun #ifdef USE_GETIPNODEBY
    684  1.1  itojun 	freehostent(hp);
    685  1.1  itojun #endif
    686  1.1  itojun 	*res = top;
    687  1.1  itojun 	return SUCCESS;
    688  1.1  itojun  free:
    689  1.1  itojun 	if (top)
    690  1.1  itojun 		freeaddrinfo(top);
    691  1.1  itojun #ifdef USE_GETIPNODEBY
    692  1.1  itojun 	if (hp)
    693  1.1  itojun 		freehostent(hp);
    694  1.1  itojun #endif
    695  1.1  itojun  bad:
    696  1.1  itojun 	*res = NULL;
    697  1.1  itojun 	return error;
    698  1.1  itojun }
    699