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  */
getnameinfo(const struct sockaddr * sa,socklen_t salen,char * host,size_t hostlen,char * serv,size_t servlen,int flags)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 
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)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
getnameinfo_local(const struct sockaddr * sa,socklen_t salen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)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
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)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 		hp = android_gethostbyaddrfornet_proxy(addr, afd->a_addrlen, afd->a_af, netid, mark);
304 		if (hp) {
305 #if 0
306 			/*
307 			 * commented out, since "for local host" is not
308 			 * implemented here - see RFC2553 p30
309 			 */
310 			if (flags & NI_NOFQDN) {
311 				char *p;
312 				p = strchr(hp->h_name, '.');
313 				if (p)
314 					TODO: Before uncommenting rewrite to avoid modifying hp.
315 					*p = '\0';
316 			}
317 #endif
318 			if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
319 				return EAI_MEMORY;
320 			}
321 			strlcpy(host, hp->h_name, hostlen);
322 		} else {
323 			if (flags & NI_NAMEREQD)
324 				return EAI_NONAME;
325 			switch(afd->a_af) {
326 #ifdef INET6
327 			case AF_INET6:
328 			{
329 				int error;
330 
331 				if ((error = ip6_parsenumeric(sa, addr, host,
332 							      hostlen,
333 							      flags)) != 0)
334 					return(error);
335 				break;
336 			}
337 #endif
338 			default:
339 				if (inet_ntop(afd->a_af, addr, host,
340 				    hostlen) == NULL)
341 					return EAI_SYSTEM;
342 				break;
343 			}
344 		}
345 	}
346 	return(0);
347 }
348 
349 #ifdef INET6
350 static int
ip6_parsenumeric(const struct sockaddr * sa,const char * addr,char * host,socklen_t hostlen,int flags)351 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
352        socklen_t hostlen, int flags)
353 {
354 	size_t numaddrlen;
355 	char numaddr[512];
356 
357 	assert(sa != NULL);
358 	assert(addr != NULL);
359 	assert(host != NULL);
360 
361 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
362 		return EAI_SYSTEM;
363 
364 	numaddrlen = strlen(numaddr);
365 	if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
366 		return EAI_OVERFLOW;
367 	strlcpy(host, numaddr, hostlen);
368 
369 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
370 		char zonebuf[MAXHOSTNAMELEN];
371 		int zonelen;
372 
373 		zonelen = ip6_sa2str(
374 		    (const struct sockaddr_in6 *)(const void *)sa,
375 		    zonebuf, sizeof(zonebuf), flags);
376 		if (zonelen < 0)
377 			return EAI_OVERFLOW;
378 		if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
379 			return EAI_OVERFLOW;
380 		/* construct <numeric-addr><delim><zoneid> */
381 		memcpy(host + numaddrlen + 1, zonebuf,
382 		    (size_t)zonelen);
383 		host[numaddrlen] = SCOPE_DELIMITER;
384 		host[numaddrlen + 1 + zonelen] = '\0';
385 	}
386 
387 	return 0;
388 }
389 
390 /* ARGSUSED */
391 static int
ip6_sa2str(const struct sockaddr_in6 * sa6,char * buf,size_t bufsiz,int flags)392 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
393 {
394 	unsigned int ifindex;
395 	const struct in6_addr *a6;
396 	int n;
397 
398 	assert(sa6 != NULL);
399 	assert(buf != NULL);
400 
401 	ifindex = (unsigned int)sa6->sin6_scope_id;
402 	a6 = &sa6->sin6_addr;
403 
404 #ifdef NI_NUMERICSCOPE
405 	if ((flags & NI_NUMERICSCOPE) != 0) {
406 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
407 		if (n < 0 || n >= bufsiz)
408 			return -1;
409 		else
410 			return n;
411 	}
412 #endif
413 
414 	/* if_indextoname() does not take buffer size.  not a good api... */
415 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
416 	    bufsiz >= IF_NAMESIZE) {
417 		char *p = if_indextoname(ifindex, buf);
418 		if (p) {
419 			return(strlen(p));
420 		}
421 	}
422 
423 	/* last resort */
424 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
425 	if (n < 0 || (size_t) n >= bufsiz)
426 		return -1;
427 	else
428 		return n;
429 }
430 #endif /* INET6 */
431