Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.30
      1 /*	$NetBSD: getnameinfo.c,v 1.30 2001/10/05 01:39:38 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.30 2001/10/05 01:39:38 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 #if 0
     57 #include <net/if_ieee1394.h>
     58 #endif
     59 #include <net/if_types.h>
     60 #include <netinet/in.h>
     61 #include <arpa/inet.h>
     62 #include <arpa/nameser.h>
     63 #include <assert.h>
     64 #include <netdb.h>
     65 #include <resolv.h>
     66 #include <stddef.h>
     67 #include <string.h>
     68 
     69 #ifdef __weak_alias
     70 __weak_alias(getnameinfo,_getnameinfo)
     71 #endif
     72 
     73 #define SUCCESS 0
     74 #define ANY 0
     75 #define YES 1
     76 #define NO  0
     77 
     78 static const struct afd {
     79 	int		a_af;
     80 	socklen_t	a_addrlen;
     81 	socklen_t	a_socklen;
     82 	int		a_off;
     83 } afdl [] = {
     84 #ifdef INET6
     85 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     86 		offsetof(struct sockaddr_in6, sin6_addr)},
     87 #endif
     88 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     89 		offsetof(struct sockaddr_in, sin_addr)},
     90 	{0, 0, 0},
     91 };
     92 
     93 struct sockinet {
     94 	u_char	si_len;
     95 	u_char	si_family;
     96 	u_short	si_port;
     97 };
     98 
     99 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
    100     size_t, char *, size_t, int));
    101 #ifdef INET6
    102 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
    103 				 size_t, int));
    104 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
    105 #endif
    106 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
    107     size_t, char *, size_t, int));
    108 static int hexname __P((u_int8_t *, size_t, char *, size_t));
    109 
    110 /* 2553bis: use EAI_xx for getnameinfo */
    111 #define ENI_NOSOCKET 	EAI_FAIL		/*XXX*/
    112 #define ENI_NOSERVNAME	EAI_NONAME
    113 #define ENI_NOHOSTNAME	EAI_NONAME
    114 #define ENI_MEMORY	EAI_MEMORY
    115 #define ENI_SYSTEM	EAI_SYSTEM
    116 #define ENI_FAMILY	EAI_FAMILY
    117 #define ENI_SALEN	EAI_FAMILY
    118 
    119 /*
    120  * Top-level getnameinfo() code.  Look at the address family, and pick an
    121  * appropriate function to call.
    122  */
    123 int
    124 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
    125 	const struct sockaddr *sa;
    126 	socklen_t salen;
    127 	char *host, *serv;
    128 	size_t hostlen, servlen;
    129 	int flags;
    130 {
    131 
    132 	switch (sa->sa_family) {
    133 	case AF_INET:
    134 	case AF_INET6:
    135 		return getnameinfo_inet(sa, salen, host, hostlen,
    136 		    serv, servlen, flags);
    137 	case AF_LINK:
    138 		return getnameinfo_link(sa, salen, host, hostlen,
    139 		    serv, servlen, flags);
    140 	default:
    141 		return EAI_FAMILY;
    142 	}
    143 }
    144 
    145 
    146 /*
    147  * getnameinfo_inet():
    148  * Format an IPv4 or IPv6 sockaddr into a printable string.
    149  */
    150 static int
    151 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
    152 	const struct sockaddr *sa;
    153 	socklen_t salen;
    154 	char *host;
    155 	size_t hostlen;
    156 	char *serv;
    157 	size_t servlen;
    158 	int flags;
    159 {
    160 	const struct afd *afd;
    161 	struct servent *sp;
    162 	struct hostent *hp;
    163 	u_short port;
    164 	int family, i;
    165 	const char *addr;
    166 	u_int32_t v4a;
    167 	char numserv[512];
    168 	char numaddr[512];
    169 
    170 	/* sa is checked below */
    171 	/* host may be NULL */
    172 	/* serv may be NULL */
    173 
    174 	if (sa == NULL)
    175 		return ENI_NOSOCKET;
    176 
    177 #ifdef BSD4_4
    178 	if (sa->sa_len != salen)
    179 		return ENI_SALEN;
    180 #endif
    181 
    182 	family = sa->sa_family;
    183 	for (i = 0; afdl[i].a_af; i++)
    184 		if (afdl[i].a_af == family) {
    185 			afd = &afdl[i];
    186 			goto found;
    187 		}
    188 	return ENI_FAMILY;
    189 
    190  found:
    191 	if (salen != afd->a_socklen)
    192 		return ENI_SALEN;
    193 
    194 	/* network byte order */
    195 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    196 	addr = (const char *)(const void *)sa + afd->a_off;
    197 
    198 	if (serv == NULL || servlen == 0) {
    199 		/*
    200 		 * do nothing in this case.
    201 		 * in case you are wondering if "&&" is more correct than
    202 		 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
    203 		 * means that the caller does not want the result.
    204 		 */
    205 	} else {
    206 		if (flags & NI_NUMERICSERV)
    207 			sp = NULL;
    208 		else {
    209 			sp = getservbyport(port,
    210 				(flags & NI_DGRAM) ? "udp" : "tcp");
    211 		}
    212 		if (sp) {
    213 			if (strlen(sp->s_name) + 1 > servlen)
    214 				return ENI_MEMORY;
    215 			strcpy(serv, sp->s_name);
    216 		} else {
    217 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
    218 			if (strlen(numserv) + 1 > servlen)
    219 				return ENI_MEMORY;
    220 			strcpy(serv, numserv);
    221 		}
    222 	}
    223 
    224 	switch (sa->sa_family) {
    225 	case AF_INET:
    226 		v4a = (u_int32_t)
    227 		    ntohl(((const struct sockaddr_in *)
    228 		    (const void *)sa)->sin_addr.s_addr);
    229 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    230 			flags |= NI_NUMERICHOST;
    231 		v4a >>= IN_CLASSA_NSHIFT;
    232 		if (v4a == 0)
    233 			flags |= NI_NUMERICHOST;
    234 		break;
    235 #ifdef INET6
    236 	case AF_INET6:
    237 	    {
    238 		const struct sockaddr_in6 *sin6;
    239 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    240 		switch (sin6->sin6_addr.s6_addr[0]) {
    241 		case 0x00:
    242 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    243 				;
    244 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    245 				;
    246 			else
    247 				flags |= NI_NUMERICHOST;
    248 			break;
    249 		default:
    250 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    251 				flags |= NI_NUMERICHOST;
    252 			}
    253 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    254 				flags |= NI_NUMERICHOST;
    255 			break;
    256 		}
    257 	    }
    258 		break;
    259 #endif
    260 	}
    261 	if (host == NULL || hostlen == 0) {
    262 		/*
    263 		 * do nothing in this case.
    264 		 * in case you are wondering if "&&" is more correct than
    265 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
    266 		 * means that the caller does not want the result.
    267 		 */
    268 	} else if (flags & NI_NUMERICHOST) {
    269 		int numaddrlen;
    270 
    271 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    272 		if (flags & NI_NAMEREQD)
    273 			return ENI_NOHOSTNAME;
    274 
    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, flags)) != 0)
    283 				return(error);
    284 			break;
    285 		}
    286 #endif
    287 		default:
    288 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    289 			    == NULL)
    290 				return ENI_SYSTEM;
    291 			numaddrlen = strlen(numaddr);
    292 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    293 				return ENI_MEMORY;
    294 			strcpy(host, numaddr);
    295 			break;
    296 		}
    297 	} else {
    298 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    299 
    300 		if (hp) {
    301 #if 0
    302 			/*
    303 			 * commented out, since "for local host" is not
    304 			 * implemented here - see RFC2553 p30
    305 			 */
    306 			if (flags & NI_NOFQDN) {
    307 				char *p;
    308 				p = strchr(hp->h_name, '.');
    309 				if (p)
    310 					*p = '\0';
    311 			}
    312 #endif
    313 			if (strlen(hp->h_name) + 1 > hostlen) {
    314 				return ENI_MEMORY;
    315 			}
    316 			strcpy(host, hp->h_name);
    317 		} else {
    318 			if (flags & NI_NAMEREQD)
    319 				return ENI_NOHOSTNAME;
    320 			switch(afd->a_af) {
    321 #ifdef INET6
    322 			case AF_INET6:
    323 			{
    324 				int error;
    325 
    326 				if ((error = ip6_parsenumeric(sa, addr, host,
    327 							      hostlen,
    328 							      flags)) != 0)
    329 					return(error);
    330 				break;
    331 			}
    332 #endif
    333 			default:
    334 				if (inet_ntop(afd->a_af, addr, host,
    335 				    hostlen) == NULL)
    336 					return ENI_SYSTEM;
    337 				break;
    338 			}
    339 		}
    340 	}
    341 	return SUCCESS;
    342 }
    343 
    344 #ifdef INET6
    345 static int
    346 ip6_parsenumeric(sa, addr, host, hostlen, flags)
    347 	const struct sockaddr *sa;
    348 	const char *addr;
    349 	char *host;
    350 	size_t hostlen;
    351 	int flags;
    352 {
    353 	int numaddrlen;
    354 	char numaddr[512];
    355 
    356 	_DIAGASSERT(sa != NULL);
    357 	_DIAGASSERT(addr != NULL);
    358 	_DIAGASSERT(host != NULL);
    359 
    360 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
    361 	    == NULL)
    362 		return ENI_SYSTEM;
    363 
    364 	numaddrlen = strlen(numaddr);
    365 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    366 		return ENI_MEMORY;
    367 	strcpy(host, numaddr);
    368 
    369 #ifdef NI_WITHSCOPEID
    370 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    371 		if (flags & NI_WITHSCOPEID)
    372 		{
    373 			char scopebuf[MAXHOSTNAMELEN];
    374 			int scopelen;
    375 
    376 			scopelen = ip6_sa2str(
    377 			    (const struct sockaddr_in6 *)(const void *)sa,
    378 			    scopebuf, sizeof(scopebuf), 0);
    379 			if (scopelen < 0)
    380 				return ENI_MEMORY;
    381 			if (scopelen + 1 + numaddrlen + 1 > hostlen)
    382 				return ENI_MEMORY;
    383 			/*
    384 			 * construct <numeric-addr><delim><scopeid>
    385 			 */
    386 			memcpy(host + numaddrlen + 1, scopebuf,
    387 			    (size_t)scopelen);
    388 			host[numaddrlen] = SCOPE_DELIMITER;
    389 			host[numaddrlen + 1 + scopelen] = '\0';
    390 		}
    391 	}
    392 #endif /* NI_WITHSCOPEID */
    393 
    394 	return 0;
    395 }
    396 
    397 /* ARGSUSED */
    398 static int
    399 ip6_sa2str(sa6, buf, bufsiz, flags)
    400 	const struct sockaddr_in6 *sa6;
    401 	char *buf;
    402 	size_t bufsiz;
    403 	int flags;
    404 {
    405 	unsigned int ifindex;
    406 	const struct in6_addr *a6;
    407 	int n;
    408 
    409 	_DIAGASSERT(sa6 != NULL);
    410 	_DIAGASSERT(buf != NULL);
    411 
    412 	ifindex = (unsigned int)sa6->sin6_scope_id;
    413 	a6 = &sa6->sin6_addr;
    414 
    415 #ifdef NI_NUMERICSCOPE
    416 	if ((flags & NI_NUMERICSCOPE) != 0) {
    417 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    418 		if (n < 0 || n >= bufsiz)
    419 			return -1;
    420 		else
    421 			return n;
    422 	}
    423 #endif
    424 
    425 	/* if_indextoname() does not take buffer size.  not a good api... */
    426 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    427 	    bufsiz >= IF_NAMESIZE) {
    428 		char *p = if_indextoname(ifindex, buf);
    429 		if (p) {
    430 			return(strlen(p));
    431 		}
    432 	}
    433 
    434 	/* last resort */
    435 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    436 	if (n < 0 || n >= bufsiz)
    437 		return -1;
    438 	else
    439 		return n;
    440 }
    441 #endif /* INET6 */
    442 
    443 
    444 /*
    445  * getnameinfo_link():
    446  * Format a link-layer address into a printable format, paying attention to
    447  * the interface type.
    448  */
    449 static int
    450 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
    451     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
    452 {
    453 	const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)sa;
    454 #if 0
    455 	struct ieee1394_hwaddr *iha;
    456 #endif
    457 	int n;
    458 
    459 	if (serv != NULL && servlen > 0)
    460 		*serv = '\0';
    461 
    462 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
    463 		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
    464 		if (n > hostlen) {
    465 			*host = '\0';
    466 			return EAI_MEMORY;
    467 		}
    468 		return 0;
    469 	}
    470 
    471 	switch (sdl->sdl_type) {
    472 #ifdef IFT_ECONET
    473 	case IFT_ECONET:
    474 		if (sdl->sdl_alen < 2)
    475 			return EAI_FAMILY;
    476 		if (LLADDR(sdl)[1] == 0)
    477 			n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
    478 		else
    479 			n = snprintf(host, hostlen, "%d.%d",
    480 			    LLADDR(sdl)[1], LLADDR(sdl)[0]);
    481 		if (n >= hostlen) {
    482 			*host = '\0';
    483 			return EAI_MEMORY;
    484 		} else
    485 			return 0;
    486 #endif
    487 #if 0
    488 	case IFT_IEEE1394:
    489 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
    490 			return EAI_FAMILY;
    491 		iha = (struct ieee1394_hwaddr *)LLADDR(sdl);
    492 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
    493 		    host, hostlen);
    494 #endif
    495 	/*
    496 	 * The following have zero-length addresses.
    497 	 * IFT_ATM	(net/if_atmsubr.c)
    498 	 * IFT_FAITH	(net/if_faith.c)
    499 	 * IFT_GIF	(net/if_gif.c)
    500 	 * IFT_LOOP	(net/if_loop.c)
    501 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
    502 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
    503 	 * IFT_STF	(net/if_stf.c)
    504 	 * IFT_L2VLAN	(net/if_vlan.c)
    505 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
    506 	 */
    507 	/*
    508 	 * The following use IPv4 addresses as link-layer addresses:
    509 	 * IFT_OTHER	(net/if_gre.c)
    510 	 * IFT_OTHER	(netinet/ip_ipip.c)
    511 	 */
    512 	case IFT_ARCNET: /* default below is believed correct for all these. */
    513 	case IFT_ETHER:
    514 	case IFT_FDDI:
    515 	case IFT_HIPPI:
    516 	case IFT_ISO88025:
    517 	default:
    518 		return hexname(LLADDR(sdl), sdl->sdl_alen, host, hostlen);
    519 	}
    520 }
    521 
    522 static int
    523 hexname(cp, len, host, hostlen)
    524 	u_int8_t *cp;
    525 	char *host;
    526 	size_t len, hostlen;
    527 {
    528 	int i, n, space = hostlen;
    529 	char *outp = host;
    530 
    531 	*outp = '\0';
    532 	for (i = 0; i < len; i++) {
    533 		n = snprintf(outp, space, "%s%02x", i ? ":" : "", cp[i]);
    534 		if (n < 0 || n >= space) {
    535 			*host = '\0';
    536 			return EAI_MEMORY;
    537 		}
    538 		outp += n;
    539 		space -= n;
    540 		if (space <= 0) {
    541 			*host = '\0';
    542 			return EAI_MEMORY;
    543 		}
    544 	}
    545 	return 0;
    546 }
    547