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