Home | History | Annotate | Download | only in net
      1 /*	$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos 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) always attach textual scopeid (fe80::1%lo0), if
     43  *   sin6_scope_id is filled - standardization status?
     44  *   XXX breaks backward compat for code that expects no scopeid.
     45  *   beware on merge.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 #if defined(LIBC_SCCS) && !defined(lint)
     50 __RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $");
     51 #endif /* LIBC_SCCS and not lint */
     52 
     53 #include <sys/types.h>
     54 #include <sys/socket.h>
     55 #include <sys/un.h>
     56 #include <net/if.h>
     57 #include <netinet/in.h>
     58 #include <arpa/inet.h>
     59 #include <assert.h>
     60 #include <limits.h>
     61 #include <netdb.h>
     62 #include <arpa/nameser.h>
     63 #include "resolv_netid.h"
     64 #include "resolv_private.h"
     65 #include <stdlib.h>
     66 #include <unistd.h>
     67 #include <errno.h>
     68 #include <stddef.h>
     69 #include <string.h>
     70 
     71 /* This macro is modelled after the ones in <netinet/in6.h>. */
     72 /* RFC 6052, section 2.1 */
     73 #define IN6_IS_ADDR_WKP(a) \
     74   ((((a)->s6_addr32[0]) == ntohl(0x0064ff9b)) && \
     75    (((a)->s6_addr32[1]) == 0) && \
     76    (((a)->s6_addr32[2]) == 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, 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(const struct sockaddr *, socklen_t, char *,
    100     socklen_t, char *, socklen_t, int, unsigned, unsigned);
    101 #ifdef INET6
    102 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
    103 				 socklen_t, int);
    104 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
    105 #endif
    106 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
    107     socklen_t, char *, socklen_t, int);
    108 
    109 /*
    110  * Top-level getnameinfo() code.  Look at the address family, and pick an
    111  * appropriate function to call.
    112  */
    113 int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen,
    114 		char* serv, size_t servlen, int flags)
    115 {
    116 	return android_getnameinfofornet(sa, salen, host, hostlen, serv, servlen, flags,
    117 			NETID_UNSET, MARK_UNSET);
    118 }
    119 
    120 int android_getnameinfofornet(const struct sockaddr* sa, socklen_t salen, char* host,
    121 		size_t hostlen, char* serv, size_t servlen, int flags, unsigned netid,
    122 		unsigned mark)
    123 {
    124 	switch (sa->sa_family) {
    125 	case AF_INET:
    126 	case AF_INET6:
    127 		return getnameinfo_inet(sa, salen, host, hostlen,
    128 				serv, servlen, flags, netid, mark);
    129 	case AF_LOCAL:
    130 		return getnameinfo_local(sa, salen, host, hostlen,
    131 		    serv, servlen, flags);
    132 	default:
    133 		return EAI_FAMILY;
    134 	}
    135 }
    136 
    137 /*
    138  * getnameinfo_local():
    139  * Format an local address into a printable format.
    140  */
    141 /* ARGSUSED */
    142 static int
    143 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
    144     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    145     int flags __attribute__((unused)))
    146 {
    147        const struct sockaddr_un *sun =
    148            (const struct sockaddr_un *)(const void *)sa;
    149 
    150        if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) {
    151            return EAI_FAMILY;
    152        }
    153 
    154        if (serv != NULL && servlen > 0)
    155                serv[0] = '\0';
    156 
    157        if (host && hostlen > 0)
    158                strlcpy(host, sun->sun_path,
    159                    MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen));
    160 
    161        return 0;
    162 }
    163 
    164 /*
    165  * getnameinfo_inet():
    166  * Format an IPv4 or IPv6 sockaddr into a printable string.
    167  */
    168 static int
    169 getnameinfo_inet(const struct sockaddr* sa, socklen_t salen,
    170        char *host, socklen_t hostlen,
    171        char *serv, socklen_t servlen,
    172        int flags, unsigned netid, unsigned mark)
    173 {
    174 	const struct afd *afd;
    175 	struct servent *sp;
    176 	struct hostent *hp;
    177 	u_short port;
    178 	int family, i;
    179 	const char *addr;
    180 	uint32_t v4a;
    181 	char numserv[512];
    182 	char numaddr[512];
    183 
    184 	/* sa is checked below */
    185 	/* host may be NULL */
    186 	/* serv may be NULL */
    187 
    188 	if (sa == NULL)
    189 		return EAI_FAIL;
    190 
    191 	family = sa->sa_family;
    192 	for (i = 0; afdl[i].a_af; i++)
    193 		if (afdl[i].a_af == family) {
    194 			afd = &afdl[i];
    195 			goto found;
    196 		}
    197 	return EAI_FAMILY;
    198 
    199  found:
    200 	// http://b/1889275: callers should be allowed to provide too much
    201 	// space, but not too little.
    202 	if (salen < afd->a_socklen) {
    203 		return EAI_FAMILY;
    204 	}
    205 
    206 	/* network byte order */
    207 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    208 	addr = (const char *)(const void *)sa + afd->a_off;
    209 
    210 	if (serv == NULL || servlen == 0) {
    211 		/*
    212 		 * do nothing in this case.
    213 		 * in case you are wondering if "&&" is more correct than
    214 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
    215 		 * servlen == 0 means that the caller does not want the result.
    216 		 */
    217 	} else {
    218 		if (flags & NI_NUMERICSERV)
    219 			sp = NULL;
    220 		else {
    221 			sp = getservbyport(port,
    222 				(flags & NI_DGRAM) ? "udp" : "tcp");
    223 		}
    224 		if (sp) {
    225 			if (strlen(sp->s_name) + 1 > (size_t)servlen)
    226 				return EAI_MEMORY;
    227 			strlcpy(serv, sp->s_name, servlen);
    228 		} else {
    229 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
    230 			if (strlen(numserv) + 1 > (size_t)servlen)
    231 				return EAI_MEMORY;
    232 			strlcpy(serv, numserv, servlen);
    233 		}
    234 	}
    235 
    236 	switch (sa->sa_family) {
    237 	case AF_INET:
    238 		v4a = (uint32_t)
    239 		    ntohl(((const struct sockaddr_in *)
    240 		    (const void *)sa)->sin_addr.s_addr);
    241 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    242 			flags |= NI_NUMERICHOST;
    243 		v4a >>= IN_CLASSA_NSHIFT;
    244 		if (v4a == 0)
    245 			flags |= NI_NUMERICHOST;
    246 		break;
    247 #ifdef INET6
    248 	case AF_INET6:
    249 	    {
    250 		const struct sockaddr_in6 *sin6;
    251 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    252 		switch (sin6->sin6_addr.s6_addr[0]) {
    253 		case 0x00:
    254 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    255 				;
    256 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    257 				;
    258 			else if (IN6_IS_ADDR_WKP(&sin6->sin6_addr))
    259 				;
    260 			else
    261 				flags |= NI_NUMERICHOST;
    262 			break;
    263 		default:
    264 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    265 				flags |= NI_NUMERICHOST;
    266 			}
    267 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    268 				flags |= NI_NUMERICHOST;
    269 			break;
    270 		}
    271 	    }
    272 		break;
    273 #endif
    274 	}
    275 	if (host == NULL || hostlen == 0) {
    276 		/*
    277 		 * do nothing in this case.
    278 		 * in case you are wondering if "&&" is more correct than
    279 		 * "||" here: rfc2553bis-03 says that host == NULL or
    280 		 * hostlen == 0 means that the caller does not want the result.
    281 		 */
    282 	} else if (flags & NI_NUMERICHOST) {
    283 		size_t numaddrlen;
    284 
    285 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    286 		if (flags & NI_NAMEREQD)
    287 			return EAI_NONAME;
    288 
    289 		switch(afd->a_af) {
    290 #ifdef INET6
    291 		case AF_INET6:
    292 		{
    293 			int error;
    294 
    295 			if ((error = ip6_parsenumeric(sa, addr, host,
    296 						      hostlen, flags)) != 0)
    297 				return(error);
    298 			break;
    299 		}
    300 #endif
    301 		default:
    302 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    303 			    == NULL)
    304 				return EAI_SYSTEM;
    305 			numaddrlen = strlen(numaddr);
    306 			if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
    307 				return EAI_MEMORY;
    308 			strlcpy(host, numaddr, hostlen);
    309 			break;
    310 		}
    311 	} else {
    312 		// This code should only run in the app context, not inside netd, so netid is
    313 		// the app's netid.  netd doesn't use getnameinfo for network requests.
    314 		const struct android_net_context netcontext = { .app_netid = netid, .app_mark = mark };
    315 		hp = android_gethostbyaddrfornetcontext_proxy(addr, afd->a_addrlen, afd->a_af, &netcontext);
    316 		if (hp) {
    317 #if 0
    318 			/*
    319 			 * commented out, since "for local host" is not
    320 			 * implemented here - see RFC2553 p30
    321 			 */
    322 			if (flags & NI_NOFQDN) {
    323 				char *p;
    324 				p = strchr(hp->h_name, '.');
    325 				if (p)
    326 					TODO: Before uncommenting rewrite to avoid modifying hp.
    327 					*p = '\0';
    328 			}
    329 #endif
    330 			if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
    331 				return EAI_MEMORY;
    332 			}
    333 			strlcpy(host, hp->h_name, hostlen);
    334 		} else {
    335 			if (flags & NI_NAMEREQD)
    336 				return EAI_NONAME;
    337 			switch(afd->a_af) {
    338 #ifdef INET6
    339 			case AF_INET6:
    340 			{
    341 				int error;
    342 
    343 				if ((error = ip6_parsenumeric(sa, addr, host,
    344 							      hostlen,
    345 							      flags)) != 0)
    346 					return(error);
    347 				break;
    348 			}
    349 #endif
    350 			default:
    351 				if (inet_ntop(afd->a_af, addr, host,
    352 				    hostlen) == NULL)
    353 					return EAI_SYSTEM;
    354 				break;
    355 			}
    356 		}
    357 	}
    358 	return(0);
    359 }
    360 
    361 #ifdef INET6
    362 static int
    363 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
    364        socklen_t hostlen, int flags)
    365 {
    366 	size_t numaddrlen;
    367 	char numaddr[512];
    368 
    369 	assert(sa != NULL);
    370 	assert(addr != NULL);
    371 	assert(host != NULL);
    372 
    373 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
    374 		return EAI_SYSTEM;
    375 
    376 	numaddrlen = strlen(numaddr);
    377 	if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
    378 		return EAI_OVERFLOW;
    379 	strlcpy(host, numaddr, hostlen);
    380 
    381 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    382 		char zonebuf[MAXHOSTNAMELEN];
    383 		int zonelen;
    384 
    385 		zonelen = ip6_sa2str(
    386 		    (const struct sockaddr_in6 *)(const void *)sa,
    387 		    zonebuf, sizeof(zonebuf), flags);
    388 		if (zonelen < 0)
    389 			return EAI_OVERFLOW;
    390 		if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
    391 			return EAI_OVERFLOW;
    392 		/* construct <numeric-addr><delim><zoneid> */
    393 		memcpy(host + numaddrlen + 1, zonebuf,
    394 		    (size_t)zonelen);
    395 		host[numaddrlen] = SCOPE_DELIMITER;
    396 		host[numaddrlen + 1 + zonelen] = '\0';
    397 	}
    398 
    399 	return 0;
    400 }
    401 
    402 /* ARGSUSED */
    403 static int
    404 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
    405 {
    406 	unsigned int ifindex;
    407 	const struct in6_addr *a6;
    408 	int n;
    409 
    410 	assert(sa6 != NULL);
    411 	assert(buf != NULL);
    412 
    413 	ifindex = (unsigned int)sa6->sin6_scope_id;
    414 	a6 = &sa6->sin6_addr;
    415 
    416 #ifdef NI_NUMERICSCOPE
    417 	if ((flags & NI_NUMERICSCOPE) != 0) {
    418 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    419 		if (n < 0 || n >= bufsiz)
    420 			return -1;
    421 		else
    422 			return n;
    423 	}
    424 #endif
    425 
    426 	/* if_indextoname() does not take buffer size.  not a good api... */
    427 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    428 	    bufsiz >= IF_NAMESIZE) {
    429 		char *p = if_indextoname(ifindex, buf);
    430 		if (p) {
    431 			return(strlen(p));
    432 		}
    433 	}
    434 
    435 	/* last resort */
    436 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    437 	if (n < 0 || (size_t) n >= bufsiz)
    438 		return -1;
    439 	else
    440 		return n;
    441 }
    442 #endif /* INET6 */
    443