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