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