Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.33
      1 /*	$NetBSD: getnameinfo.c,v 1.33 2001/11/15 04:38:32 itojun Exp $	*/
      2 /*	$KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2000 Ben Harris.
      6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the project nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Issues to be discussed:
     36  * - Thread safe-ness must be checked
     37  * - RFC2553 says that we should raise error on short buffer.  X/Open says
     38  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
     39  *   modified).  ipngwg rough consensus seems to follow RFC2553.
     40  * - What is "local" in NI_FQDN?
     41  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
     42  * - (KAME extension) NI_WITHSCOPEID when called with global address,
     43  *   and sin6_scope_id filled
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 #if defined(LIBC_SCCS) && !defined(lint)
     48 __RCSID("$NetBSD: getnameinfo.c,v 1.33 2001/11/15 04:38:32 itojun Exp $");
     49 #endif /* LIBC_SCCS and not lint */
     50 
     51 #include "namespace.h"
     52 #include <sys/types.h>
     53 #include <sys/socket.h>
     54 #include <net/if.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_ieee1394.h>
     57 #include <net/if_types.h>
     58 #include <netinet/in.h>
     59 #include <arpa/inet.h>
     60 #include <arpa/nameser.h>
     61 #include <assert.h>
     62 #include <limits.h>
     63 #include <netdb.h>
     64 #include <resolv.h>
     65 #include <stddef.h>
     66 #include <string.h>
     67 
     68 #ifdef __weak_alias
     69 __weak_alias(getnameinfo,_getnameinfo)
     70 #endif
     71 
     72 static const struct afd {
     73 	int		a_af;
     74 	socklen_t	a_addrlen;
     75 	socklen_t	a_socklen;
     76 	int		a_off;
     77 } afdl [] = {
     78 #ifdef INET6
     79 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     80 		offsetof(struct sockaddr_in6, sin6_addr)},
     81 #endif
     82 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     83 		offsetof(struct sockaddr_in, sin_addr)},
     84 	{0, 0, 0},
     85 };
     86 
     87 struct sockinet {
     88 	u_char	si_len;
     89 	u_char	si_family;
     90 	u_short	si_port;
     91 };
     92 
     93 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
     94     size_t, char *, size_t, int));
     95 #ifdef INET6
     96 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
     97 				 size_t, int));
     98 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
     99 #endif
    100 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
    101     size_t, char *, size_t, int));
    102 static int hexname __P((const u_int8_t *, size_t, char *, size_t));
    103 
    104 /*
    105  * Top-level getnameinfo() code.  Look at the address family, and pick an
    106  * appropriate function to call.
    107  */
    108 int
    109 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
    110 	const struct sockaddr *sa;
    111 	socklen_t salen;
    112 	char *host, *serv;
    113 	size_t hostlen, servlen;
    114 	int flags;
    115 {
    116 
    117 	switch (sa->sa_family) {
    118 	case AF_INET:
    119 	case AF_INET6:
    120 		return getnameinfo_inet(sa, salen, host, hostlen,
    121 		    serv, servlen, flags);
    122 	case AF_LINK:
    123 		return getnameinfo_link(sa, salen, host, hostlen,
    124 		    serv, servlen, flags);
    125 	default:
    126 		return EAI_FAMILY;
    127 	}
    128 }
    129 
    130 
    131 /*
    132  * getnameinfo_inet():
    133  * Format an IPv4 or IPv6 sockaddr into a printable string.
    134  */
    135 static int
    136 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
    137 	const struct sockaddr *sa;
    138 	socklen_t salen;
    139 	char *host;
    140 	size_t hostlen;
    141 	char *serv;
    142 	size_t servlen;
    143 	int flags;
    144 {
    145 	const struct afd *afd;
    146 	struct servent *sp;
    147 	struct hostent *hp;
    148 	u_short port;
    149 	int family, i;
    150 	const char *addr;
    151 	u_int32_t v4a;
    152 	char numserv[512];
    153 	char numaddr[512];
    154 
    155 	/* sa is checked below */
    156 	/* host may be NULL */
    157 	/* serv may be NULL */
    158 
    159 	if (sa == NULL)
    160 		return EAI_FAIL;
    161 
    162 #ifdef BSD4_4
    163 	if (sa->sa_len != salen)
    164 		return EAI_FAIL;
    165 #endif
    166 
    167 	family = sa->sa_family;
    168 	for (i = 0; afdl[i].a_af; i++)
    169 		if (afdl[i].a_af == family) {
    170 			afd = &afdl[i];
    171 			goto found;
    172 		}
    173 	return EAI_FAMILY;
    174 
    175  found:
    176 	if (salen != afd->a_socklen)
    177 		return EAI_FAIL;
    178 
    179 	/* network byte order */
    180 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    181 	addr = (const char *)(const void *)sa + afd->a_off;
    182 
    183 	if (serv == NULL || servlen == 0) {
    184 		/*
    185 		 * do nothing in this case.
    186 		 * in case you are wondering if "&&" is more correct than
    187 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
    188 		 * servlen == 0 means that the caller does not want the result.
    189 		 */
    190 	} else {
    191 		if (flags & NI_NUMERICSERV)
    192 			sp = NULL;
    193 		else {
    194 			sp = getservbyport(port,
    195 				(flags & NI_DGRAM) ? "udp" : "tcp");
    196 		}
    197 		if (sp) {
    198 			if (strlen(sp->s_name) + 1 > servlen)
    199 				return EAI_MEMORY;
    200 			strcpy(serv, sp->s_name);
    201 		} else {
    202 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    203 			if (strlen(numserv) + 1 > servlen)
    204 				return EAI_MEMORY;
    205 			strcpy(serv, numserv);
    206 		}
    207 	}
    208 
    209 	switch (sa->sa_family) {
    210 	case AF_INET:
    211 		v4a = (u_int32_t)
    212 		    ntohl(((const struct sockaddr_in *)
    213 		    (const void *)sa)->sin_addr.s_addr);
    214 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    215 			flags |= NI_NUMERICHOST;
    216 		v4a >>= IN_CLASSA_NSHIFT;
    217 		if (v4a == 0)
    218 			flags |= NI_NUMERICHOST;
    219 		break;
    220 #ifdef INET6
    221 	case AF_INET6:
    222 	    {
    223 		const struct sockaddr_in6 *sin6;
    224 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    225 		switch (sin6->sin6_addr.s6_addr[0]) {
    226 		case 0x00:
    227 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    228 				;
    229 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    230 				;
    231 			else
    232 				flags |= NI_NUMERICHOST;
    233 			break;
    234 		default:
    235 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    236 				flags |= NI_NUMERICHOST;
    237 			}
    238 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    239 				flags |= NI_NUMERICHOST;
    240 			break;
    241 		}
    242 	    }
    243 		break;
    244 #endif
    245 	}
    246 	if (host == NULL || hostlen == 0) {
    247 		/*
    248 		 * do nothing in this case.
    249 		 * in case you are wondering if "&&" is more correct than
    250 		 * "||" here: rfc2553bis-03 says that host == NULL or
    251 		 * hostlen == 0 means that the caller does not want the result.
    252 		 */
    253 	} else if (flags & NI_NUMERICHOST) {
    254 		int numaddrlen;
    255 
    256 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    257 		if (flags & NI_NAMEREQD)
    258 			return EAI_NONAME;
    259 
    260 		switch(afd->a_af) {
    261 #ifdef INET6
    262 		case AF_INET6:
    263 		{
    264 			int error;
    265 
    266 			if ((error = ip6_parsenumeric(sa, addr, host,
    267 						      hostlen, flags)) != 0)
    268 				return(error);
    269 			break;
    270 		}
    271 #endif
    272 		default:
    273 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    274 			    == NULL)
    275 				return EAI_SYSTEM;
    276 			numaddrlen = strlen(numaddr);
    277 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    278 				return EAI_MEMORY;
    279 			strcpy(host, numaddr);
    280 			break;
    281 		}
    282 	} else {
    283 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    284 
    285 		if (hp) {
    286 #if 0
    287 			/*
    288 			 * commented out, since "for local host" is not
    289 			 * implemented here - see RFC2553 p30
    290 			 */
    291 			if (flags & NI_NOFQDN) {
    292 				char *p;
    293 				p = strchr(hp->h_name, '.');
    294 				if (p)
    295 					*p = '\0';
    296 			}
    297 #endif
    298 			if (strlen(hp->h_name) + 1 > hostlen) {
    299 				return EAI_MEMORY;
    300 			}
    301 			strcpy(host, hp->h_name);
    302 		} else {
    303 			if (flags & NI_NAMEREQD)
    304 				return EAI_NONAME;
    305 			switch(afd->a_af) {
    306 #ifdef INET6
    307 			case AF_INET6:
    308 			{
    309 				int error;
    310 
    311 				if ((error = ip6_parsenumeric(sa, addr, host,
    312 							      hostlen,
    313 							      flags)) != 0)
    314 					return(error);
    315 				break;
    316 			}
    317 #endif
    318 			default:
    319 				if (inet_ntop(afd->a_af, addr, host,
    320 				    hostlen) == NULL)
    321 					return EAI_SYSTEM;
    322 				break;
    323 			}
    324 		}
    325 	}
    326 	return(0);
    327 }
    328 
    329 #ifdef INET6
    330 static int
    331 ip6_parsenumeric(sa, addr, host, hostlen, flags)
    332 	const struct sockaddr *sa;
    333 	const char *addr;
    334 	char *host;
    335 	size_t hostlen;
    336 	int flags;
    337 {
    338 	int numaddrlen;
    339 	char numaddr[512];
    340 
    341 	_DIAGASSERT(sa != NULL);
    342 	_DIAGASSERT(addr != NULL);
    343 	_DIAGASSERT(host != NULL);
    344 
    345 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
    346 		return EAI_SYSTEM;
    347 
    348 	numaddrlen = strlen(numaddr);
    349 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    350 		return EAI_MEMORY;
    351 	strcpy(host, numaddr);
    352 
    353 #ifdef NI_WITHSCOPEID
    354 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    355 		if (flags & NI_WITHSCOPEID)
    356 		{
    357 			char scopebuf[MAXHOSTNAMELEN];
    358 			int scopelen;
    359 
    360 			scopelen = ip6_sa2str(
    361 			    (const struct sockaddr_in6 *)(const void *)sa,
    362 			    scopebuf, sizeof(scopebuf), 0);
    363 			if (scopelen < 0)
    364 				return EAI_MEMORY;
    365 			if (scopelen + 1 + numaddrlen + 1 > hostlen)
    366 				return EAI_MEMORY;
    367 			/*
    368 			 * construct <numeric-addr><delim><scopeid>
    369 			 */
    370 			memcpy(host + numaddrlen + 1, scopebuf,
    371 			    (size_t)scopelen);
    372 			host[numaddrlen] = SCOPE_DELIMITER;
    373 			host[numaddrlen + 1 + scopelen] = '\0';
    374 		}
    375 	}
    376 #endif /* NI_WITHSCOPEID */
    377 
    378 	return 0;
    379 }
    380 
    381 /* ARGSUSED */
    382 static int
    383 ip6_sa2str(sa6, buf, bufsiz, flags)
    384 	const struct sockaddr_in6 *sa6;
    385 	char *buf;
    386 	size_t bufsiz;
    387 	int flags;
    388 {
    389 	unsigned int ifindex;
    390 	const struct in6_addr *a6;
    391 	int n;
    392 
    393 	_DIAGASSERT(sa6 != NULL);
    394 	_DIAGASSERT(buf != NULL);
    395 
    396 	ifindex = (unsigned int)sa6->sin6_scope_id;
    397 	a6 = &sa6->sin6_addr;
    398 
    399 #ifdef NI_NUMERICSCOPE
    400 	if ((flags & NI_NUMERICSCOPE) != 0) {
    401 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    402 		if (n < 0 || n >= bufsiz)
    403 			return -1;
    404 		else
    405 			return n;
    406 	}
    407 #endif
    408 
    409 	/* if_indextoname() does not take buffer size.  not a good api... */
    410 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    411 	    bufsiz >= IF_NAMESIZE) {
    412 		char *p = if_indextoname(ifindex, buf);
    413 		if (p) {
    414 			return(strlen(p));
    415 		}
    416 	}
    417 
    418 	/* last resort */
    419 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    420 	if (n < 0 || n >= bufsiz)
    421 		return -1;
    422 	else
    423 		return n;
    424 }
    425 #endif /* INET6 */
    426 
    427 
    428 /*
    429  * getnameinfo_link():
    430  * Format a link-layer address into a printable format, paying attention to
    431  * the interface type.
    432  */
    433 /* ARGSUSED */
    434 static int
    435 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
    436     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
    437 {
    438 	const struct sockaddr_dl *sdl =
    439 	    (const struct sockaddr_dl *)(const void *)sa;
    440 	const struct ieee1394_hwaddr *iha;
    441 	int n;
    442 
    443 	if (serv != NULL && servlen > 0)
    444 		*serv = '\0';
    445 
    446 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
    447 		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
    448 		if (n > hostlen) {
    449 			*host = '\0';
    450 			return EAI_MEMORY;
    451 		}
    452 		return 0;
    453 	}
    454 
    455 	switch (sdl->sdl_type) {
    456 #ifdef IFT_ECONET
    457 	case IFT_ECONET:
    458 		if (sdl->sdl_alen < 2)
    459 			return EAI_FAMILY;
    460 		if (LLADDR(sdl)[1] == 0)
    461 			n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
    462 		else
    463 			n = snprintf(host, hostlen, "%d.%d",
    464 			    LLADDR(sdl)[1], LLADDR(sdl)[0]);
    465 		if (n >= hostlen) {
    466 			*host = '\0';
    467 			return EAI_MEMORY;
    468 		} else
    469 			return 0;
    470 #endif
    471 	case IFT_IEEE1394:
    472 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
    473 			return EAI_FAMILY;
    474 		iha =
    475 		    (const struct ieee1394_hwaddr *)(const void *)LLADDR(sdl);
    476 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
    477 		    host, hostlen);
    478 	/*
    479 	 * The following have zero-length addresses.
    480 	 * IFT_ATM	(net/if_atmsubr.c)
    481 	 * IFT_FAITH	(net/if_faith.c)
    482 	 * IFT_GIF	(net/if_gif.c)
    483 	 * IFT_LOOP	(net/if_loop.c)
    484 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
    485 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
    486 	 * IFT_STF	(net/if_stf.c)
    487 	 * IFT_L2VLAN	(net/if_vlan.c)
    488 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
    489 	 */
    490 	/*
    491 	 * The following use IPv4 addresses as link-layer addresses:
    492 	 * IFT_OTHER	(net/if_gre.c)
    493 	 * IFT_OTHER	(netinet/ip_ipip.c)
    494 	 */
    495 	case IFT_ARCNET: /* default below is believed correct for all these. */
    496 	case IFT_ETHER:
    497 	case IFT_FDDI:
    498 	case IFT_HIPPI:
    499 	case IFT_ISO88025:
    500 	default:
    501 		return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
    502 		    host, hostlen);
    503 	}
    504 }
    505 
    506 static int
    507 hexname(cp, len, host, hostlen)
    508 	const u_int8_t *cp;
    509 	char *host;
    510 	size_t len, hostlen;
    511 {
    512 	int i, n;
    513 	char *outp = host;
    514 
    515 	*outp = '\0';
    516 	for (i = 0; i < len; i++) {
    517 		n = snprintf(outp, hostlen, "%s%02x",
    518 		    i ? ":" : "", cp[i]);
    519 		if (n < 0 || n >= hostlen) {
    520 			*host = '\0';
    521 			return EAI_MEMORY;
    522 		}
    523 		outp += n;
    524 		hostlen -= n;
    525 	}
    526 	return 0;
    527 }
    528