getnameinfo.c revision 157119
1100628Sume/*	$KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $	*/
262615Sitojun
355163Sshin/*
455163Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
555163Sshin * All rights reserved.
655163Sshin *
755163Sshin * Redistribution and use in source and binary forms, with or without
855163Sshin * modification, are permitted provided that the following conditions
955163Sshin * are met:
1055163Sshin * 1. Redistributions of source code must retain the above copyright
1155163Sshin *    notice, this list of conditions and the following disclaimer.
1255163Sshin * 2. Redistributions in binary form must reproduce the above copyright
1355163Sshin *    notice, this list of conditions and the following disclaimer in the
1455163Sshin *    documentation and/or other materials provided with the distribution.
1555163Sshin * 3. Neither the name of the project nor the names of its contributors
1655163Sshin *    may be used to endorse or promote products derived from this software
1755163Sshin *    without specific prior written permission.
1855163Sshin *
1955163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2055163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2155163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2255163Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2355163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2455163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2555163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2655163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2755163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2855163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2955163Sshin * SUCH DAMAGE.
3055163Sshin */
3155163Sshin
3255163Sshin/*
3355163Sshin * Issues to be discussed:
3455163Sshin * - Thread safe-ness must be checked
3562615Sitojun * - RFC2553 says that we should raise error on short buffer.  X/Open says
3662615Sitojun *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
3766374Sitojun *   modified).  ipngwg rough consensus seems to follow RFC2553.
3862615Sitojun * - What is "local" in NI_FQDN?
3962615Sitojun * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
4099252Sume * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
4199252Sume *   sin6_scope_id is filled - standardization status?
4299252Sume *   XXX breaks backward compat for code that expects no scopeid.
4399252Sume *   beware on merge.
4455163Sshin */
4555163Sshin
4692986Sobrien#include <sys/cdefs.h>
4792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/net/getnameinfo.c 157119 2006-03-25 11:46:37Z ume $");
4892986Sobrien
4955163Sshin#include <sys/types.h>
5055163Sshin#include <sys/socket.h>
5155163Sshin#include <net/if.h>
5255163Sshin#include <netinet/in.h>
5355163Sshin#include <arpa/inet.h>
5455163Sshin#include <arpa/nameser.h>
5555163Sshin#include <netdb.h>
5655163Sshin#include <resolv.h>
5755163Sshin#include <string.h>
5855163Sshin#include <stddef.h>
5962615Sitojun#include <errno.h>
6055163Sshin
61100628Sumestatic const struct afd {
6255163Sshin	int a_af;
63145831Sume	size_t a_addrlen;
64145831Sume	socklen_t a_socklen;
6555163Sshin	int a_off;
6655163Sshin} afdl [] = {
6755163Sshin#ifdef INET6
6855163Sshin	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
6955163Sshin		offsetof(struct sockaddr_in6, sin6_addr)},
7055163Sshin#endif
7155163Sshin	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
7255163Sshin		offsetof(struct sockaddr_in, sin_addr)},
7355163Sshin	{0, 0, 0},
7455163Sshin};
7555163Sshin
7655163Sshinstruct sockinet {
7755163Sshin	u_char	si_len;
7855163Sshin	u_char	si_family;
7955163Sshin	u_short	si_port;
8055163Sshin};
8155163Sshin
8262615Sitojun#ifdef INET6
8392941Sobrienstatic int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
84100628Sume    size_t, int);
8592905Sobrienstatic int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
8662615Sitojun#endif
8755163Sshin
8855163Sshinint
89157119Sumegetnameinfo(const struct sockaddr *sa, socklen_t salen,
90157119Sume    char *host, size_t hostlen, char *serv, size_t servlen,
91157119Sume    int flags)
9255163Sshin{
93100628Sume	const struct afd *afd;
9455163Sshin	struct servent *sp;
9555163Sshin	struct hostent *hp;
9655163Sshin	u_short port;
9755163Sshin	int family, i;
9862615Sitojun	const char *addr;
9962615Sitojun	u_int32_t v4a;
10055163Sshin	int h_error;
10155163Sshin	char numserv[512];
10255163Sshin	char numaddr[512];
10355163Sshin
10455163Sshin	if (sa == NULL)
105100628Sume		return EAI_FAIL;
10655163Sshin
10755163Sshin	family = sa->sa_family;
10855163Sshin	for (i = 0; afdl[i].a_af; i++)
10955163Sshin		if (afdl[i].a_af == family) {
11055163Sshin			afd = &afdl[i];
11155163Sshin			goto found;
11255163Sshin		}
113100628Sume	return EAI_FAMILY;
114100628Sume
11555163Sshin found:
11655163Sshin	if (salen != afd->a_socklen)
117100628Sume		return EAI_FAIL;
118100628Sume
11962615Sitojun	/* network byte order */
12062615Sitojun	port = ((const struct sockinet *)sa)->si_port;
12162615Sitojun	addr = (const char *)sa + afd->a_off;
12255163Sshin
12355163Sshin	if (serv == NULL || servlen == 0) {
12462615Sitojun		/*
12562615Sitojun		 * do nothing in this case.
12662615Sitojun		 * in case you are wondering if "&&" is more correct than
127100628Sume		 * "||" here: rfc2553bis-03 says that serv == NULL OR
128100628Sume		 * servlen == 0 means that the caller does not want the result.
12962615Sitojun		 */
13055163Sshin	} else {
13155163Sshin		if (flags & NI_NUMERICSERV)
13255163Sshin			sp = NULL;
13355163Sshin		else {
13455163Sshin			sp = getservbyport(port,
13555163Sshin				(flags & NI_DGRAM) ? "udp" : "tcp");
13655163Sshin		}
13755163Sshin		if (sp) {
13866374Sitojun			if (strlen(sp->s_name) + 1 > servlen)
139100628Sume				return EAI_MEMORY;
140114443Snectar			strlcpy(serv, sp->s_name, servlen);
14155163Sshin		} else {
142100628Sume			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
14366374Sitojun			if (strlen(numserv) + 1 > servlen)
144100628Sume				return EAI_MEMORY;
145114443Snectar			strlcpy(serv, numserv, servlen);
14655163Sshin		}
14755163Sshin	}
14855163Sshin
14955163Sshin	switch (sa->sa_family) {
15055163Sshin	case AF_INET:
15162615Sitojun		v4a = (u_int32_t)
15262615Sitojun		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
15355163Sshin		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
15455163Sshin			flags |= NI_NUMERICHOST;
15555163Sshin		v4a >>= IN_CLASSA_NSHIFT;
15656671Sshin		if (v4a == 0)
157157119Sume			flags |= NI_NUMERICHOST;
15855163Sshin		break;
15955163Sshin#ifdef INET6
16055163Sshin	case AF_INET6:
16155163Sshin	    {
16262615Sitojun		const struct sockaddr_in6 *sin6;
16362615Sitojun		sin6 = (const struct sockaddr_in6 *)sa;
16462615Sitojun		switch (sin6->sin6_addr.s6_addr[0]) {
16562615Sitojun		case 0x00:
16662615Sitojun			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
16762615Sitojun				;
16862615Sitojun			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
16962615Sitojun				;
17062615Sitojun			else
17162615Sitojun				flags |= NI_NUMERICHOST;
17262615Sitojun			break;
17362615Sitojun		default:
17462615Sitojun			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
17562615Sitojun				flags |= NI_NUMERICHOST;
17662615Sitojun			}
17762615Sitojun			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
17862615Sitojun				flags |= NI_NUMERICHOST;
17962615Sitojun			break;
18062615Sitojun		}
18155163Sshin	    }
18255163Sshin		break;
18355163Sshin#endif
18455163Sshin	}
18555163Sshin	if (host == NULL || hostlen == 0) {
18662615Sitojun		/*
18762615Sitojun		 * do nothing in this case.
18862615Sitojun		 * in case you are wondering if "&&" is more correct than
189100628Sume		 * "||" here: rfc2553bis-03 says that host == NULL or
190100628Sume		 * hostlen == 0 means that the caller does not want the result.
19162615Sitojun		 */
19255163Sshin	} else if (flags & NI_NUMERICHOST) {
193145831Sume		size_t numaddrlen;
19462615Sitojun
19555163Sshin		/* NUMERICHOST and NAMEREQD conflicts with each other */
19655163Sshin		if (flags & NI_NAMEREQD)
197100628Sume			return EAI_NONAME;
19862615Sitojun
19962615Sitojun		switch(afd->a_af) {
20055163Sshin#ifdef INET6
20162615Sitojun		case AF_INET6:
20262615Sitojun		{
20362615Sitojun			int error;
20455163Sshin
20562615Sitojun			if ((error = ip6_parsenumeric(sa, addr, host,
20662615Sitojun						      hostlen, flags)) != 0)
20762615Sitojun				return(error);
20862615Sitojun			break;
20955163Sshin		}
21062615Sitojun#endif
21162615Sitojun		default:
21262615Sitojun			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
21362615Sitojun			    == NULL)
214100628Sume				return EAI_SYSTEM;
21562615Sitojun			numaddrlen = strlen(numaddr);
21662615Sitojun			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
217100628Sume				return EAI_MEMORY;
218114443Snectar			strlcpy(host, numaddr, hostlen);
21962615Sitojun			break;
22062615Sitojun		}
22155163Sshin	} else {
22255163Sshin		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
22362615Sitojun
22455163Sshin		if (hp) {
22562615Sitojun#if 0
22662615Sitojun			/*
22762615Sitojun			 * commented out, since "for local host" is not
22862615Sitojun			 * implemented here - see RFC2553 p30
22962615Sitojun			 */
23055163Sshin			if (flags & NI_NOFQDN) {
23162615Sitojun				char *p;
23255163Sshin				p = strchr(hp->h_name, '.');
23362615Sitojun				if (p)
23462615Sitojun					*p = '\0';
23555163Sshin			}
23662615Sitojun#endif
23766374Sitojun			if (strlen(hp->h_name) + 1 > hostlen) {
23855163Sshin				freehostent(hp);
239100628Sume				return EAI_MEMORY;
24055163Sshin			}
241114443Snectar			strlcpy(host, hp->h_name, hostlen);
24255163Sshin			freehostent(hp);
24355163Sshin		} else {
24455163Sshin			if (flags & NI_NAMEREQD)
245100628Sume				return EAI_NONAME;
24662615Sitojun			switch(afd->a_af) {
24762615Sitojun#ifdef INET6
24862615Sitojun			case AF_INET6:
24962615Sitojun			{
25062615Sitojun				int error;
25162615Sitojun
25262615Sitojun				if ((error = ip6_parsenumeric(sa, addr, host,
25362615Sitojun							      hostlen,
25462615Sitojun							      flags)) != 0)
25562615Sitojun					return(error);
25662615Sitojun				break;
25762615Sitojun			}
25862615Sitojun#endif
25962615Sitojun			default:
26062615Sitojun				if (inet_ntop(afd->a_af, addr, host,
26162615Sitojun				    hostlen) == NULL)
262100628Sume					return EAI_SYSTEM;
26362615Sitojun				break;
26462615Sitojun			}
26555163Sshin		}
26655163Sshin	}
267100628Sume	return(0);
26855163Sshin}
26962615Sitojun
27062615Sitojun#ifdef INET6
27162615Sitojunstatic int
272157119Sumeip6_parsenumeric(const struct sockaddr *sa, const char *addr,
273157119Sume    char *host, size_t hostlen, int flags)
27462615Sitojun{
275145831Sume	size_t numaddrlen;
27662615Sitojun	char numaddr[512];
27762615Sitojun
278100628Sume	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
279100628Sume		return EAI_SYSTEM;
28062615Sitojun
28162615Sitojun	numaddrlen = strlen(numaddr);
28262615Sitojun	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
283100628Sume		return EAI_MEMORY;
284114443Snectar	strlcpy(host, numaddr, hostlen);
28562615Sitojun
28699252Sume	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
28799252Sume		char zonebuf[MAXHOSTNAMELEN];
28899252Sume		int zonelen;
28962615Sitojun
29099252Sume		zonelen = ip6_sa2str(
29199252Sume		    (const struct sockaddr_in6 *)(const void *)sa,
29299252Sume		    zonebuf, sizeof(zonebuf), flags);
29399252Sume		if (zonelen < 0)
29499252Sume			return EAI_MEMORY;
29599252Sume		if (zonelen + 1 + numaddrlen + 1 > hostlen)
296100628Sume			return EAI_MEMORY;
297100628Sume
298100628Sume		/* construct <numeric-addr><delim><zoneid> */
29999252Sume		memcpy(host + numaddrlen + 1, zonebuf,
300100628Sume		    (size_t)zonelen);
30199252Sume		host[numaddrlen] = SCOPE_DELIMITER;
30299252Sume		host[numaddrlen + 1 + zonelen] = '\0';
30362615Sitojun	}
30462615Sitojun
30562615Sitojun	return 0;
30662615Sitojun}
30762615Sitojun
30862615Sitojun/* ARGSUSED */
30962615Sitojunstatic int
310157119Sumeip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
31162615Sitojun{
312100628Sume	unsigned int ifindex;
313100628Sume	const struct in6_addr *a6;
314100628Sume	int n;
31562615Sitojun
316100628Sume	ifindex = (unsigned int)sa6->sin6_scope_id;
317100628Sume	a6 = &sa6->sin6_addr;
318100628Sume
31962615Sitojun#ifdef NI_NUMERICSCOPE
320100628Sume	if ((flags & NI_NUMERICSCOPE) != 0) {
321100628Sume		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
322100628Sume		if (n < 0 || n >= bufsiz)
323100628Sume			return -1;
324100628Sume		else
325100628Sume			return n;
32662615Sitojun	}
32762615Sitojun#endif
32862615Sitojun
32962615Sitojun	/* if_indextoname() does not take buffer size.  not a good api... */
330100628Sume	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
331100628Sume	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
33262615Sitojun		char *p = if_indextoname(ifindex, buf);
33362615Sitojun		if (p) {
33462615Sitojun			return(strlen(p));
33562615Sitojun		}
33662615Sitojun	}
33762615Sitojun
33862615Sitojun	/* last resort */
339100628Sume	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
340145831Sume	if (n < 0 || (size_t)n >= bufsiz)
341100628Sume		return -1;
342100628Sume	else
343100628Sume		return n;
34462615Sitojun}
34562615Sitojun#endif /* INET6 */
346