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