Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.21
      1  1.21    itojun /*	$NetBSD: getnameinfo.c,v 1.21 2000/08/07 16:18:45 itojun Exp $	*/
      2  1.21    itojun /*	$KAME: getnameinfo.c,v 1.44 2000/08/06 16:43:17 itojun Exp $	*/
      3   1.2    itojun 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1    itojun  * All rights reserved.
      7  1.18    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19  1.18    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  */
     32   1.1    itojun 
     33   1.1    itojun /*
     34   1.1    itojun  * Issues to be discussed:
     35   1.1    itojun  * - Thread safe-ness must be checked
     36   1.5    itojun  * - RFC2553 says that we should raise error on short buffer.  X/Open says
     37   1.5    itojun  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
     38  1.21    itojun  *   modified).  ipngwg rough consensus seems to follow RFC2553.
     39  1.16    itojun  * - What is "local" in NI_FQDN?
     40  1.16    itojun  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
     41  1.16    itojun  * - (KAME extension) NI_WITHSCOPEID when called with global address,
     42  1.16    itojun  *   and sin6_scope_id filled
     43   1.1    itojun  */
     44  1.15    itojun 
     45  1.15    itojun #include <sys/cdefs.h>
     46  1.15    itojun #if defined(LIBC_SCCS) && !defined(lint)
     47  1.21    itojun __RCSID("$NetBSD: getnameinfo.c,v 1.21 2000/08/07 16:18:45 itojun Exp $");
     48  1.15    itojun #endif /* LIBC_SCCS and not lint */
     49   1.1    itojun 
     50  1.14    itojun #include "namespace.h"
     51   1.1    itojun #include <sys/types.h>
     52   1.1    itojun #include <sys/socket.h>
     53   1.5    itojun #include <net/if.h>
     54   1.1    itojun #include <netinet/in.h>
     55   1.1    itojun #include <arpa/inet.h>
     56   1.1    itojun #include <arpa/nameser.h>
     57   1.1    itojun #include <netdb.h>
     58   1.1    itojun #include <resolv.h>
     59   1.1    itojun #include <string.h>
     60   1.1    itojun #include <stddef.h>
     61  1.14    itojun 
     62  1.14    itojun #ifdef __weak_alias
     63  1.14    itojun __weak_alias(getnameinfo,_getnameinfo)
     64  1.14    itojun #endif
     65   1.1    itojun 
     66   1.1    itojun #define SUCCESS 0
     67   1.1    itojun #define ANY 0
     68   1.1    itojun #define YES 1
     69   1.1    itojun #define NO  0
     70   1.1    itojun 
     71   1.1    itojun static struct afd {
     72   1.1    itojun 	int a_af;
     73   1.1    itojun 	int a_addrlen;
     74   1.1    itojun 	int a_socklen;
     75   1.1    itojun 	int a_off;
     76   1.1    itojun } afdl [] = {
     77   1.1    itojun #ifdef INET6
     78   1.1    itojun 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     79   1.1    itojun 		offsetof(struct sockaddr_in6, sin6_addr)},
     80   1.1    itojun #endif
     81   1.1    itojun 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     82   1.1    itojun 		offsetof(struct sockaddr_in, sin_addr)},
     83   1.1    itojun 	{0, 0, 0},
     84   1.1    itojun };
     85   1.1    itojun 
     86   1.1    itojun struct sockinet {
     87   1.1    itojun 	u_char	si_len;
     88   1.1    itojun 	u_char	si_family;
     89   1.1    itojun 	u_short	si_port;
     90   1.1    itojun };
     91   1.1    itojun 
     92  1.10    itojun #ifdef INET6
     93  1.16    itojun static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
     94  1.16    itojun 				 size_t, int));
     95  1.16    itojun static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
     96  1.18    itojun #endif
     97  1.10    itojun 
     98  1.21    itojun /* 2553bis: use EAI_xx for getnameinfo */
     99  1.18    itojun #define ENI_NOSOCKET 	EAI_FAIL		/*XXX*/
    100  1.18    itojun #define ENI_NOSERVNAME	EAI_NONAME
    101  1.18    itojun #define ENI_NOHOSTNAME	EAI_NONAME
    102  1.18    itojun #define ENI_MEMORY	EAI_MEMORY
    103  1.18    itojun #define ENI_SYSTEM	EAI_SYSTEM
    104  1.18    itojun #define ENI_FAMILY	EAI_FAMILY
    105  1.19    itojun #define ENI_SALEN	EAI_FAMILY
    106   1.1    itojun 
    107   1.1    itojun int
    108   1.1    itojun getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
    109   1.1    itojun 	const struct sockaddr *sa;
    110  1.17    itojun 	socklen_t salen;
    111   1.1    itojun 	char *host;
    112   1.1    itojun 	size_t hostlen;
    113   1.1    itojun 	char *serv;
    114   1.1    itojun 	size_t servlen;
    115   1.1    itojun 	int flags;
    116   1.1    itojun {
    117   1.1    itojun 	struct afd *afd;
    118   1.1    itojun 	struct servent *sp;
    119   1.1    itojun 	struct hostent *hp;
    120   1.1    itojun 	u_short port;
    121   1.5    itojun 	int family, i;
    122  1.16    itojun 	const char *addr;
    123   1.5    itojun 	u_int32_t v4a;
    124   1.1    itojun 	char numserv[512];
    125   1.1    itojun 	char numaddr[512];
    126   1.1    itojun 
    127   1.1    itojun 	if (sa == NULL)
    128   1.1    itojun 		return ENI_NOSOCKET;
    129   1.1    itojun 
    130  1.13  christos #ifdef BSD4_4
    131   1.5    itojun 	if (sa->sa_len != salen)
    132   1.5    itojun 		return ENI_SALEN;
    133  1.13  christos #endif
    134   1.1    itojun 
    135   1.1    itojun 	family = sa->sa_family;
    136   1.1    itojun 	for (i = 0; afdl[i].a_af; i++)
    137   1.1    itojun 		if (afdl[i].a_af == family) {
    138   1.1    itojun 			afd = &afdl[i];
    139   1.1    itojun 			goto found;
    140   1.1    itojun 		}
    141   1.1    itojun 	return ENI_FAMILY;
    142   1.1    itojun 
    143   1.1    itojun  found:
    144   1.5    itojun 	if (salen != afd->a_socklen)
    145   1.5    itojun 		return ENI_SALEN;
    146   1.1    itojun 
    147  1.16    itojun 	/* network byte order */
    148  1.20  christos 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    149  1.20  christos 	addr = (const char *)(const void *)sa + afd->a_off;
    150   1.1    itojun 
    151   1.1    itojun 	if (serv == NULL || servlen == 0) {
    152   1.5    itojun 		/*
    153   1.5    itojun 		 * do nothing in this case.
    154   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    155   1.5    itojun 		 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
    156   1.5    itojun 		 * means that the caller does not want the result.
    157   1.5    itojun 		 */
    158   1.1    itojun 	} else {
    159   1.5    itojun 		if (flags & NI_NUMERICSERV)
    160   1.5    itojun 			sp = NULL;
    161   1.5    itojun 		else {
    162   1.5    itojun 			sp = getservbyport(port,
    163   1.5    itojun 				(flags & NI_DGRAM) ? "udp" : "tcp");
    164   1.5    itojun 		}
    165   1.1    itojun 		if (sp) {
    166   1.1    itojun 			if (strlen(sp->s_name) > servlen)
    167   1.1    itojun 				return ENI_MEMORY;
    168   1.1    itojun 			strcpy(serv, sp->s_name);
    169   1.5    itojun 		} else {
    170   1.5    itojun 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    171   1.5    itojun 			if (strlen(numserv) > servlen)
    172   1.5    itojun 				return ENI_MEMORY;
    173   1.5    itojun 			strcpy(serv, numserv);
    174   1.5    itojun 		}
    175   1.1    itojun 	}
    176   1.1    itojun 
    177   1.1    itojun 	switch (sa->sa_family) {
    178   1.1    itojun 	case AF_INET:
    179   1.5    itojun 		v4a = (u_int32_t)
    180  1.20  christos 		    ntohl(((const struct sockaddr_in *)
    181  1.20  christos 		    (const void *)sa)->sin_addr.s_addr);
    182   1.1    itojun 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    183   1.1    itojun 			flags |= NI_NUMERICHOST;
    184   1.1    itojun 		v4a >>= IN_CLASSA_NSHIFT;
    185   1.7    itojun 		if (v4a == 0)
    186   1.1    itojun 			flags |= NI_NUMERICHOST;
    187   1.1    itojun 		break;
    188   1.1    itojun #ifdef INET6
    189   1.1    itojun 	case AF_INET6:
    190   1.3    itojun 	    {
    191  1.16    itojun 		const struct sockaddr_in6 *sin6;
    192  1.20  christos 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    193   1.5    itojun 		switch (sin6->sin6_addr.s6_addr[0]) {
    194   1.3    itojun 		case 0x00:
    195   1.3    itojun 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    196   1.3    itojun 				;
    197   1.3    itojun 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    198   1.3    itojun 				;
    199   1.3    itojun 			else
    200   1.3    itojun 				flags |= NI_NUMERICHOST;
    201   1.3    itojun 			break;
    202   1.3    itojun 		default:
    203   1.5    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    204   1.3    itojun 				flags |= NI_NUMERICHOST;
    205   1.5    itojun 			}
    206   1.3    itojun 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    207   1.3    itojun 				flags |= NI_NUMERICHOST;
    208   1.3    itojun 			break;
    209   1.3    itojun 		}
    210   1.3    itojun 	    }
    211   1.1    itojun 		break;
    212   1.1    itojun #endif
    213   1.1    itojun 	}
    214   1.1    itojun 	if (host == NULL || hostlen == 0) {
    215   1.5    itojun 		/*
    216   1.5    itojun 		 * do nothing in this case.
    217   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    218   1.5    itojun 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
    219   1.5    itojun 		 * means that the caller does not want the result.
    220   1.5    itojun 		 */
    221   1.1    itojun 	} else if (flags & NI_NUMERICHOST) {
    222  1.10    itojun 		int numaddrlen;
    223  1.10    itojun 
    224   1.3    itojun 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    225   1.3    itojun 		if (flags & NI_NAMEREQD)
    226   1.3    itojun 			return ENI_NOHOSTNAME;
    227   1.5    itojun 
    228  1.16    itojun 		switch(afd->a_af) {
    229  1.16    itojun #ifdef INET6
    230  1.16    itojun 		case AF_INET6:
    231  1.16    itojun 		{
    232  1.16    itojun 			int error;
    233  1.16    itojun 
    234  1.16    itojun 			if ((error = ip6_parsenumeric(sa, addr, host,
    235  1.16    itojun 						      hostlen, flags)) != 0)
    236  1.16    itojun 				return(error);
    237  1.16    itojun 			break;
    238  1.16    itojun 		}
    239  1.11    itojun #endif
    240  1.16    itojun 		default:
    241  1.16    itojun 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    242  1.16    itojun 			    == NULL)
    243  1.16    itojun 				return ENI_SYSTEM;
    244  1.16    itojun 			numaddrlen = strlen(numaddr);
    245  1.16    itojun 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    246  1.16    itojun 				return ENI_MEMORY;
    247  1.16    itojun 			strcpy(host, numaddr);
    248  1.16    itojun 			break;
    249   1.5    itojun 		}
    250   1.1    itojun 	} else {
    251   1.1    itojun 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    252   1.1    itojun 
    253   1.1    itojun 		if (hp) {
    254  1.16    itojun #if 0
    255  1.16    itojun 			/*
    256  1.16    itojun 			 * commented out, since "for local host" is not
    257  1.16    itojun 			 * implemented here - see RFC2553 p30
    258  1.16    itojun 			 */
    259   1.1    itojun 			if (flags & NI_NOFQDN) {
    260  1.16    itojun 				char *p;
    261   1.1    itojun 				p = strchr(hp->h_name, '.');
    262  1.16    itojun 				if (p)
    263  1.16    itojun 					*p = '\0';
    264   1.1    itojun 			}
    265  1.16    itojun #endif
    266   1.1    itojun 			if (strlen(hp->h_name) > hostlen) {
    267   1.1    itojun 				return ENI_MEMORY;
    268   1.1    itojun 			}
    269   1.1    itojun 			strcpy(host, hp->h_name);
    270   1.1    itojun 		} else {
    271   1.1    itojun 			if (flags & NI_NAMEREQD)
    272   1.1    itojun 				return ENI_NOHOSTNAME;
    273  1.16    itojun 			switch(afd->a_af) {
    274  1.16    itojun #ifdef INET6
    275  1.16    itojun 			case AF_INET6:
    276  1.16    itojun 			{
    277  1.16    itojun 				int error;
    278  1.16    itojun 
    279  1.16    itojun 				if ((error = ip6_parsenumeric(sa, addr, host,
    280  1.16    itojun 							      hostlen,
    281  1.16    itojun 							      flags)) != 0)
    282  1.16    itojun 					return(error);
    283  1.16    itojun 				break;
    284  1.16    itojun 			}
    285  1.16    itojun #endif
    286  1.16    itojun 			default:
    287  1.16    itojun 				if (inet_ntop(afd->a_af, addr, host,
    288  1.16    itojun 				    hostlen) == NULL)
    289  1.16    itojun 					return ENI_SYSTEM;
    290  1.16    itojun 				break;
    291  1.16    itojun 			}
    292   1.1    itojun 		}
    293   1.1    itojun 	}
    294   1.1    itojun 	return SUCCESS;
    295   1.1    itojun }
    296  1.10    itojun 
    297  1.10    itojun #ifdef INET6
    298  1.16    itojun static int
    299  1.16    itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
    300  1.16    itojun 	const struct sockaddr *sa;
    301  1.16    itojun 	const char *addr;
    302  1.16    itojun 	char *host;
    303  1.16    itojun 	size_t hostlen;
    304  1.16    itojun 	int flags;
    305  1.16    itojun {
    306  1.16    itojun 	int numaddrlen;
    307  1.16    itojun 	char numaddr[512];
    308  1.16    itojun 
    309  1.16    itojun 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
    310  1.16    itojun 	    == NULL)
    311  1.16    itojun 		return ENI_SYSTEM;
    312  1.16    itojun 
    313  1.16    itojun 	numaddrlen = strlen(numaddr);
    314  1.16    itojun 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    315  1.16    itojun 		return ENI_MEMORY;
    316  1.16    itojun 	strcpy(host, numaddr);
    317  1.16    itojun 
    318  1.16    itojun #ifdef NI_WITHSCOPEID
    319  1.20  christos 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    320  1.16    itojun 		if (flags & NI_WITHSCOPEID)
    321  1.16    itojun 		{
    322  1.16    itojun 			char scopebuf[MAXHOSTNAMELEN];
    323  1.16    itojun 			int scopelen;
    324  1.16    itojun 
    325  1.16    itojun 			/* ip6_sa2str never fails */
    326  1.20  christos 			scopelen = ip6_sa2str(
    327  1.20  christos 			    (const struct sockaddr_in6 *)(const void *)sa,
    328  1.20  christos 			    scopebuf, sizeof(scopebuf), 0);
    329  1.16    itojun 			if (scopelen + 1 + numaddrlen + 1 > hostlen)
    330  1.16    itojun 				return ENI_MEMORY;
    331  1.16    itojun 			/*
    332  1.16    itojun 			 * construct <numeric-addr><delim><scopeid>
    333  1.16    itojun 			 */
    334  1.16    itojun 			memcpy(host + numaddrlen + 1, scopebuf,
    335  1.20  christos 			    (size_t)scopelen);
    336  1.16    itojun 			host[numaddrlen] = SCOPE_DELIMITER;
    337  1.16    itojun 			host[numaddrlen + 1 + scopelen] = '\0';
    338  1.16    itojun 		}
    339  1.16    itojun 	}
    340  1.16    itojun #endif /* NI_WITHSCOPEID */
    341  1.16    itojun 
    342  1.16    itojun 	return 0;
    343  1.16    itojun }
    344  1.16    itojun 
    345  1.10    itojun /* ARGSUSED */
    346  1.16    itojun static int
    347  1.16    itojun ip6_sa2str(sa6, buf, bufsiz, flags)
    348  1.16    itojun 	const struct sockaddr_in6 *sa6;
    349  1.10    itojun 	char *buf;
    350  1.16    itojun 	size_t bufsiz;
    351  1.10    itojun 	int flags;
    352  1.10    itojun {
    353  1.10    itojun 	unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
    354  1.16    itojun 	const struct in6_addr *a6 = &sa6->sin6_addr;
    355  1.10    itojun 
    356  1.10    itojun #ifdef notyet
    357  1.10    itojun 	if (flags & NI_NUMERICSCOPE) {
    358  1.16    itojun 		return(snprintf(buf, bufsiz, "%d", sa6->sin6_scope_id));
    359  1.10    itojun 	}
    360  1.10    itojun #endif
    361  1.18    itojun 
    362  1.16    itojun 	/* if_indextoname() does not take buffer size.  not a good api... */
    363  1.16    itojun 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    364  1.16    itojun 	    bufsiz >= IF_NAMESIZE) {
    365  1.16    itojun 		char *p = if_indextoname(ifindex, buf);
    366  1.16    itojun 		if (p) {
    367  1.16    itojun 			return(strlen(p));
    368  1.16    itojun 		}
    369  1.16    itojun 	}
    370  1.10    itojun 
    371  1.16    itojun 	/* last resort */
    372  1.16    itojun 	return(snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id));
    373  1.10    itojun }
    374  1.16    itojun #endif /* INET6 */
    375