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