xref: /minix/lib/libc/net/getnameinfo.c (revision 0a6a1f1d)
1 /*	$NetBSD: getnameinfo.c,v 1.59 2015/09/22 16:15:08 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.59 2015/09/22 16:15:08 christos Exp $");
51 #endif /* LIBC_SCCS and not lint */
52 
53 #ifndef RUMP_ACTION
54 #include "namespace.h"
55 #endif
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <net/if.h>
60 #if !defined(__minix)
61 #include <net/if_dl.h>
62 #include <net/if_ieee1394.h>
63 #include <net/if_types.h>
64 #include <netatalk/at.h>
65 #endif /* !defined(__minix) */
66 #include <netinet/in.h>
67 #include <arpa/inet.h>
68 #include <arpa/nameser.h>
69 #include <assert.h>
70 #include <limits.h>
71 #include <netdb.h>
72 #include <resolv.h>
73 #include <stddef.h>
74 #include <string.h>
75 
76 #include "servent.h"
77 #include "hostent.h"
78 
79 #ifndef RUMP_ACTION
80 #ifdef __weak_alias
81 __weak_alias(getnameinfo,_getnameinfo)
82 #endif
83 #endif
84 
85 static const struct afd {
86 	int		a_af;
87 	socklen_t	a_addrlen;
88 	socklen_t	a_socklen;
89 	int		a_off;
90 } afdl [] = {
91 #ifdef INET6
92 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
93 		offsetof(struct sockaddr_in6, sin6_addr)},
94 #endif
95 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
96 		offsetof(struct sockaddr_in, sin_addr)},
97 	{0, 0, 0, 0},
98 };
99 
100 struct sockinet {
101 	u_char	si_len;
102 	u_char	si_family;
103 	u_short	si_port;
104 };
105 
106 static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
107     socklen_t, char *, socklen_t, int);
108 #ifdef INET6
109 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
110 				 socklen_t, int);
111 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
112 #endif
113 #if !defined(__minix)
114 static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
115     socklen_t, char *, socklen_t, int);
116 static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
117     socklen_t, char *, socklen_t, int);
118 
119 static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
120     socklen_t, char *, socklen_t, int);
121 static int hexname(const uint8_t *, size_t, char *, socklen_t);
122 #endif /* !defined(__minix) */
123 
124 /*
125  * Top-level getnameinfo() code.  Look at the address family, and pick an
126  * appropriate function to call.
127  */
128 int
129 getnameinfo(const struct sockaddr *sa, socklen_t salen,
130 	char *host, socklen_t hostlen,
131 	char *serv, socklen_t servlen,
132 	int flags)
133 {
134 
135 	switch (sa->sa_family) {
136 #if !defined(__minix)
137 	case AF_APPLETALK:
138 		return getnameinfo_atalk(sa, salen, host, hostlen,
139 		    serv, servlen, flags);
140 #endif /* !defined(__minix) */
141 	case AF_INET:
142 	case AF_INET6:
143 		return getnameinfo_inet(sa, salen, host, hostlen,
144 		    serv, servlen, flags);
145 #if !defined(__minix)
146 	case AF_LINK:
147 		return getnameinfo_link(sa, salen, host, hostlen,
148 		    serv, servlen, flags);
149 	case AF_LOCAL:
150 		return getnameinfo_local(sa, salen, host, hostlen,
151 		    serv, servlen, flags);
152 #endif /* !defined(__minix) */
153 	default:
154 		return EAI_FAMILY;
155 	}
156 }
157 
158 #if !defined(__minix)
159 /*
160  * getnameinfo_atalk():
161  * Format an AppleTalk address into a printable format.
162  */
163 /* ARGSUSED */
164 static int
165 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
166     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
167     int flags)
168 {
169 	char numserv[8];
170 	int n, m=0;
171 
172 	const struct sockaddr_at *sat =
173 	    (const struct sockaddr_at *)(const void *)sa;
174 
175 	if (serv != NULL && servlen > 0) {
176 		snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
177 		if (strlen(numserv) + 1 > servlen)
178 			return EAI_MEMORY;
179 		strlcpy(serv, numserv, servlen);
180 	}
181 
182         n = snprintf(host, hostlen, "%u.%u",
183 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
184 
185 	if (n < 0 || (socklen_t)(m+n) >= hostlen)
186 		goto errout;
187 
188 	m += n;
189 
190 	if (sat->sat_range.r_netrange.nr_phase) {
191         	n = snprintf(host+m, hostlen-m, " phase %u",
192 			sat->sat_range.r_netrange.nr_phase);
193 
194 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
195 			goto errout;
196 
197 		m += n;
198 	}
199 	if (sat->sat_range.r_netrange.nr_firstnet) {
200         	n = snprintf(host+m, hostlen-m, " range %u - %u",
201 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
202 			ntohs(sat->sat_range.r_netrange.nr_lastnet ));
203 
204 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
205 			goto errout;
206 
207 		m += n;
208 	}
209 
210 	return 0;
211 
212 errout:
213 	if (host && hostlen>0)
214 		host[m] = '\0';	/* XXX ??? */
215 
216 	return EAI_MEMORY;
217 }
218 
219 /*
220  * getnameinfo_local():
221  * Format an local address into a printable format.
222  */
223 /* ARGSUSED */
224 static int
225 getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
226     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
227     int flags)
228 {
229 	const struct sockaddr_un *sun =
230 	    (const struct sockaddr_un *)(const void *)sa;
231 
232 	if (serv != NULL && servlen > 0)
233 		serv[0] = '\0';
234 
235 	if (host && hostlen > 0)
236 		strlcpy(host, sun->sun_path,
237 		    MIN(sizeof(sun->sun_path) + 1, hostlen));
238 
239 	return 0;
240 }
241 #endif /* !defined(__minix) */
242 
243 /*
244  * getnameinfo_inet():
245  * Format an IPv4 or IPv6 sockaddr into a printable string.
246  */
247 static int
248 getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
249 	char *host, socklen_t hostlen,
250 	char *serv, socklen_t servlen,
251 	int flags)
252 {
253 	const struct afd *afd;
254 	struct servent *sp;
255 	struct hostent *hp;
256 	u_short port;
257 	int family, i;
258 	const char *addr;
259 	uint32_t v4a;
260 	char numserv[512];
261 	char numaddr[512];
262 
263 	/* sa is checked below */
264 	/* host may be NULL */
265 	/* serv may be NULL */
266 
267 	if (sa == NULL)
268 		return EAI_FAIL;
269 
270 	family = sa->sa_family;
271 	for (i = 0; afdl[i].a_af; i++)
272 		if (afdl[i].a_af == family) {
273 			afd = &afdl[i];
274 			goto found;
275 		}
276 	return EAI_FAMILY;
277 
278  found:
279 	if (salen != afd->a_socklen)
280 		return EAI_FAIL;
281 
282 	/* network byte order */
283 	port = ((const struct sockinet *)(const void *)sa)->si_port;
284 	addr = (const char *)(const void *)sa + afd->a_off;
285 
286 	if (serv == NULL || servlen == 0) {
287 		/*
288 		 * do nothing in this case.
289 		 * in case you are wondering if "&&" is more correct than
290 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
291 		 * servlen == 0 means that the caller does not want the result.
292 		 */
293 	} else {
294 		struct servent_data svd;
295 		struct servent sv;
296 
297 		if (flags & NI_NUMERICSERV)
298 			sp = NULL;
299 		else {
300 			(void)memset(&svd, 0, sizeof(svd));
301 			sp = getservbyport_r(port,
302 				(flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
303 		}
304 		if (sp) {
305 			if (strlen(sp->s_name) + 1 > servlen) {
306 				endservent_r(&svd);
307 				return EAI_MEMORY;
308 			}
309 			strlcpy(serv, sp->s_name, servlen);
310 			endservent_r(&svd);
311 		} else {
312 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
313 			if (strlen(numserv) + 1 > servlen)
314 				return EAI_MEMORY;
315 			strlcpy(serv, numserv, servlen);
316 		}
317 	}
318 
319 	switch (sa->sa_family) {
320 	case AF_INET:
321 		v4a = (uint32_t)
322 		    ntohl(((const struct sockaddr_in *)
323 		    (const void *)sa)->sin_addr.s_addr);
324 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
325 			flags |= NI_NUMERICHOST;
326 		v4a >>= IN_CLASSA_NSHIFT;
327 		if (v4a == 0)
328 			flags |= NI_NUMERICHOST;
329 		break;
330 #ifdef INET6
331 	case AF_INET6:
332 	    {
333 		const struct sockaddr_in6 *sin6;
334 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
335 		switch (sin6->sin6_addr.s6_addr[0]) {
336 		case 0x00:
337 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
338 				;
339 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
340 				;
341 			else
342 				flags |= NI_NUMERICHOST;
343 			break;
344 		default:
345 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
346 				flags |= NI_NUMERICHOST;
347 			}
348 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
349 				flags |= NI_NUMERICHOST;
350 			break;
351 		}
352 	    }
353 		break;
354 #endif
355 	}
356 	if (host == NULL || hostlen == 0) {
357 		/*
358 		 * do nothing in this case.
359 		 * in case you are wondering if "&&" is more correct than
360 		 * "||" here: rfc2553bis-03 says that host == NULL or
361 		 * hostlen == 0 means that the caller does not want the result.
362 		 */
363 	} else if (flags & NI_NUMERICHOST) {
364 		size_t numaddrlen;
365 
366 		/* NUMERICHOST and NAMEREQD conflicts with each other */
367 		if (flags & NI_NAMEREQD)
368 			return EAI_NONAME;
369 
370 		switch(afd->a_af) {
371 #ifdef INET6
372 		case AF_INET6:
373 		{
374 			int error;
375 
376 			if ((error = ip6_parsenumeric(sa, addr, host,
377 						      hostlen, flags)) != 0)
378 				return(error);
379 			break;
380 		}
381 #endif
382 		default:
383 			if (inet_ntop(afd->a_af, addr, numaddr,
384 			    (socklen_t)sizeof(numaddr)) == NULL)
385 				return EAI_SYSTEM;
386 			numaddrlen = strlen(numaddr);
387 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
388 				return EAI_MEMORY;
389 			strlcpy(host, numaddr, hostlen);
390 			break;
391 		}
392 	} else {
393 		struct hostent hent;
394 		char hbuf[4096];
395 		int he;
396 		hp = gethostbyaddr_r(addr, afd->a_addrlen, afd->a_af, &hent,
397 		    hbuf, sizeof(hbuf), &he);
398 
399 		if (hp) {
400 #if 0
401 			/*
402 			 * commented out, since "for local host" is not
403 			 * implemented here - see RFC2553 p30
404 			 */
405 			if (flags & NI_NOFQDN) {
406 				char *p;
407 				p = strchr(hp->h_name, '.');
408 				if (p)
409 					*p = '\0';
410 			}
411 #endif
412 			if (strlen(hp->h_name) + 1 > hostlen) {
413 				return EAI_MEMORY;
414 			}
415 			strlcpy(host, hp->h_name, hostlen);
416 		} else {
417 			switch (he) {
418 			case NO_DATA:
419 			case HOST_NOT_FOUND:
420 				if (flags & NI_NAMEREQD)
421 					return EAI_NONAME;
422 				break;
423 			case TRY_AGAIN:
424 				return EAI_AGAIN;
425 			case NETDB_SUCCESS:
426 			case NETDB_INTERNAL:
427 			case NO_RECOVERY:
428 				/*FALLTHROUGH*/
429 			default:
430 				return EAI_SYSTEM;
431 			}
432 			switch(afd->a_af) {
433 #ifdef INET6
434 			case AF_INET6:
435 			{
436 				int error;
437 
438 				if ((error = ip6_parsenumeric(sa, addr, host,
439 							      hostlen,
440 							      flags)) != 0)
441 					return(error);
442 				break;
443 			}
444 #endif
445 			default:
446 				if (inet_ntop(afd->a_af, addr, host,
447 				    hostlen) == NULL)
448 					return EAI_SYSTEM;
449 				break;
450 			}
451 		}
452 	}
453 	return(0);
454 }
455 
456 #ifdef INET6
457 static int
458 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
459 	socklen_t hostlen, int flags)
460 {
461 	size_t numaddrlen;
462 	char numaddr[512];
463 
464 	_DIAGASSERT(sa != NULL);
465 	_DIAGASSERT(addr != NULL);
466 	_DIAGASSERT(host != NULL);
467 
468 	if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
469 	    == NULL)
470 		return EAI_SYSTEM;
471 
472 	numaddrlen = strlen(numaddr);
473 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
474 		return EAI_OVERFLOW;
475 	strlcpy(host, numaddr, hostlen);
476 
477 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
478 		char zonebuf[MAXHOSTNAMELEN];
479 		int zonelen;
480 
481 		zonelen = ip6_sa2str(
482 		    (const struct sockaddr_in6 *)(const void *)sa,
483 		    zonebuf, sizeof(zonebuf), flags);
484 		if (zonelen < 0)
485 			return EAI_OVERFLOW;
486 		if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
487 			return EAI_OVERFLOW;
488 		/* construct <numeric-addr><delim><zoneid> */
489 		memcpy(host + numaddrlen + 1, zonebuf,
490 		    (size_t)zonelen);
491 		host[numaddrlen] = SCOPE_DELIMITER;
492 		host[numaddrlen + 1 + zonelen] = '\0';
493 	}
494 
495 	return 0;
496 }
497 
498 /* ARGSUSED */
499 static int
500 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
501 {
502 	unsigned int ifindex;
503 	const struct in6_addr *a6;
504 	int n;
505 
506 	_DIAGASSERT(sa6 != NULL);
507 	_DIAGASSERT(buf != NULL);
508 
509 	ifindex = (unsigned int)sa6->sin6_scope_id;
510 	a6 = &sa6->sin6_addr;
511 
512 #ifdef NI_NUMERICSCOPE
513 	if ((flags & NI_NUMERICSCOPE) != 0) {
514 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
515 		if (n < 0 || (size_t)n >= bufsiz)
516 			return -1;
517 		else
518 			return n;
519 	}
520 #endif
521 
522 	/* if_indextoname() does not take buffer size.  not a good api... */
523 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
524 	    bufsiz >= IF_NAMESIZE) {
525 		char *p = if_indextoname(ifindex, buf);
526 		if (p) {
527 			return (int)strlen(p);
528 		}
529 	}
530 
531 	/* last resort */
532 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
533 	if (n < 0 || (size_t) n >= bufsiz)
534 		return -1;
535 	else
536 		return n;
537 }
538 #endif /* INET6 */
539 
540 
541 #if !defined(__minix)
542 /*
543  * getnameinfo_link():
544  * Format a link-layer address into a printable format, paying attention to
545  * the interface type.
546  */
547 /* ARGSUSED */
548 static int
549 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
550     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
551     int flags)
552 {
553 	const struct sockaddr_dl *sdl =
554 	    (const struct sockaddr_dl *)(const void *)sa;
555 	const struct ieee1394_hwaddr *iha;
556 	int n;
557 
558 	if (serv != NULL && servlen > 0)
559 		*serv = '\0';
560 
561 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
562 		n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
563 		goto out;
564 	}
565 
566 	switch (sdl->sdl_type) {
567 #ifdef IFT_ECONET
568 	case IFT_ECONET:
569 		if (sdl->sdl_alen < 2)
570 			return EAI_FAMILY;
571 		if (CLLADDR(sdl)[1] == 0)
572 			n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
573 		else
574 			n = snprintf(host, hostlen, "%u.%u",
575 			    CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
576 		goto out;
577 #endif
578 	case IFT_IEEE1394:
579 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
580 			return EAI_FAMILY;
581 		iha =
582 		    (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
583 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
584 		    host, hostlen);
585 	/*
586 	 * The following have zero-length addresses.
587 	 * IFT_ATM	(net/if_atmsubr.c)
588 	 * IFT_FAITH	(net/if_faith.c)
589 	 * IFT_GIF	(net/if_gif.c)
590 	 * IFT_LOOP	(net/if_loop.c)
591 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
592 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
593 	 * IFT_STF	(net/if_stf.c)
594 	 * IFT_L2VLAN	(net/if_vlan.c)
595 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
596 	 */
597 	/*
598 	 * The following use IPv4 addresses as link-layer addresses:
599 	 * IFT_OTHER	(net/if_gre.c)
600 	 */
601 	case IFT_ARCNET: /* default below is believed correct for all these. */
602 	case IFT_ETHER:
603 	case IFT_FDDI:
604 	case IFT_HIPPI:
605 	case IFT_ISO88025:
606 	default:
607 		return hexname((const uint8_t *)CLLADDR(sdl),
608 		    (size_t)sdl->sdl_alen, host, hostlen);
609 	}
610 out:
611 	if (n < 0 || (socklen_t) n >= hostlen) {
612 		*host = '\0';
613 		return EAI_MEMORY;
614 	}
615 	return 0;
616 }
617 
618 static int
619 hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
620 {
621 	int n;
622 	size_t i;
623 	char *outp = host;
624 
625 	*outp = '\0';
626 	for (i = 0; i < len; i++) {
627 		n = snprintf(outp, hostlen, "%s%02x",
628 		    i ? ":" : "", cp[i]);
629 		if (n < 0 || (socklen_t) n >= hostlen) {
630 			*host = '\0';
631 			return EAI_MEMORY;
632 		}
633 		outp += n;
634 		hostlen -= n;
635 	}
636 	return 0;
637 }
638 #endif /* !defined(__minix) */
639