xref: /freebsd/lib/libc/net/getnameinfo.c (revision abd87254)
1 /*	$KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * Copyright (c) 2000 Ben Harris.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Issues to be discussed:
37  * - Thread safe-ness must be checked
38  * - RFC2553 says that we should raise error on short buffer.  X/Open says
39  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
40  *   modified).  ipngwg rough consensus seems to follow RFC2553.
41  * - What is "local" in NI_FQDN?
42  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
43  * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
44  *   sin6_scope_id is filled - standardization status?
45  *   XXX breaks backward compat for code that expects no scopeid.
46  *   beware on merge.
47  */
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/un.h>
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 #include <net/firewire.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <arpa/nameser.h>
59 #include <netdb.h>
60 #include <resolv.h>
61 #include <string.h>
62 #include <stddef.h>
63 #include <errno.h>
64 
65 static const struct afd *find_afd(int);
66 static int getnameinfo_inet(const struct afd *,
67     const struct sockaddr *, socklen_t, char *,
68     size_t, char *, size_t, int);
69 #ifdef INET6
70 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
71     size_t, int);
72 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
73 #endif
74 static int getnameinfo_link(const struct afd *,
75     const struct sockaddr *, socklen_t, char *,
76     size_t, char *, size_t, int);
77 static int hexname(const u_int8_t *, size_t, char *, size_t);
78 static int getnameinfo_un(const struct afd *,
79     const struct sockaddr *, socklen_t, char *,
80     size_t, char *, size_t, int);
81 
82 static const struct afd {
83 	int a_af;
84 	size_t a_addrlen;
85 	socklen_t a_socklen;
86 	int a_off;
87 	int (*a_func)(const struct afd *,
88 	    const struct sockaddr *, socklen_t, char *,
89 	    size_t, char *, size_t, int);
90 } afdl [] = {
91 #ifdef INET6
92 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
93 	    offsetof(struct sockaddr_in6, sin6_addr),
94 	    getnameinfo_inet},
95 #endif
96 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
97 	    offsetof(struct sockaddr_in, sin_addr),
98 	    getnameinfo_inet},
99 #define	sizeofmember(type, member)	(sizeof(((type *)0)->member))
100 	{PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
101 	    sizeof(struct sockaddr_un),
102 	    offsetof(struct sockaddr_un, sun_path),
103 	    getnameinfo_un},
104 	{PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data),
105 	    sizeof(struct sockaddr_dl),
106 	    offsetof(struct sockaddr_dl, sdl_data),
107 	    getnameinfo_link},
108 	{0, 0, 0},
109 };
110 
111 int
112 getnameinfo(const struct sockaddr *sa, socklen_t salen,
113     char *host, size_t hostlen, char *serv, size_t servlen,
114     int flags)
115 {
116 	const struct afd *afd;
117 
118 	if (sa == NULL)
119 		return (EAI_FAIL);
120 
121 	afd = find_afd(sa->sa_family);
122 	if (afd == NULL)
123 		return (EAI_FAMILY);
124 	/*
125 	 * getnameinfo() accepts an salen of sizeof(struct sockaddr_storage)
126 	 * at maximum as shown in RFC 4038 Sec.6.2.3.
127 	 */
128 	if (salen > sizeof(struct sockaddr_storage))
129 		return (EAI_FAMILY);
130 
131 	switch (sa->sa_family) {
132 	case PF_LOCAL:
133 		/*
134 		 * PF_LOCAL uses variable salen depending on the
135 		 * content length of sun_path.  Require 1 byte in
136 		 * sun_path at least.
137 		 */
138 		if (salen <= afd->a_socklen -
139 			sizeofmember(struct sockaddr_un, sun_path))
140 			return (EAI_FAMILY);
141 		else if (salen > afd->a_socklen)
142 			salen = afd->a_socklen;
143 		break;
144 	case PF_LINK:
145 		if (salen <= afd->a_socklen -
146 			sizeofmember(struct sockaddr_dl, sdl_data))
147 			return (EAI_FAMILY);
148 		break;
149 	default:
150 		if (salen < afd->a_socklen)
151 			return (EAI_FAMILY);
152 		else
153 			salen = afd->a_socklen;
154 		break;
155 	}
156 
157 	return ((*afd->a_func)(afd, sa, salen, host, hostlen,
158 	    serv, servlen, flags));
159 }
160 
161 static const struct afd *
162 find_afd(int af)
163 {
164 	const struct afd *afd;
165 
166 	if (af == PF_UNSPEC)
167 		return (NULL);
168 	for (afd = &afdl[0]; afd->a_af > 0; afd++) {
169 		if (afd->a_af == af)
170 			return (afd);
171 	}
172 	return (NULL);
173 }
174 
175 static int
176 getnameinfo_inet(const struct afd *afd,
177     const struct sockaddr *sa, socklen_t salen,
178     char *host, size_t hostlen, char *serv, size_t servlen,
179     int flags)
180 {
181 	struct servent *sp;
182 	struct hostent *hp;
183 	u_short port;
184 	const char *addr;
185 	u_int32_t v4a;
186 	int h_error;
187 	char numserv[512];
188 	char numaddr[512];
189 
190 	/* network byte order */
191 	port = ((const struct sockaddr_in *)sa)->sin_port;
192 	addr = (const char *)sa + afd->a_off;
193 
194 	if (serv == NULL || servlen == 0) {
195 		/*
196 		 * do nothing in this case.
197 		 * in case you are wondering if "&&" is more correct than
198 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
199 		 * servlen == 0 means that the caller does not want the result.
200 		 */
201 	} else {
202 		if (flags & NI_NUMERICSERV)
203 			sp = NULL;
204 		else {
205 			sp = getservbyport(port,
206 				(flags & NI_DGRAM) ? "udp" : "tcp");
207 		}
208 		if (sp) {
209 			if (strlen(sp->s_name) + 1 > servlen)
210 				return EAI_MEMORY;
211 			strlcpy(serv, sp->s_name, servlen);
212 		} else {
213 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
214 			if (strlen(numserv) + 1 > servlen)
215 				return EAI_MEMORY;
216 			strlcpy(serv, numserv, servlen);
217 		}
218 	}
219 
220 	switch (sa->sa_family) {
221 	case AF_INET:
222 		v4a = (u_int32_t)
223 		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
224 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a) ||
225 		    IN_ZERONET(v4a))
226 			flags |= NI_NUMERICHOST;
227 		break;
228 #ifdef INET6
229 	case AF_INET6:
230 	    {
231 		const struct sockaddr_in6 *sin6;
232 		sin6 = (const struct sockaddr_in6 *)sa;
233 		switch (sin6->sin6_addr.s6_addr[0]) {
234 		case 0x00:
235 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
236 				;
237 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
238 				;
239 			else
240 				flags |= NI_NUMERICHOST;
241 			break;
242 		default:
243 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
244 				flags |= NI_NUMERICHOST;
245 			}
246 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
247 				flags |= NI_NUMERICHOST;
248 			break;
249 		}
250 	    }
251 		break;
252 #endif
253 	}
254 	if (host == NULL || hostlen == 0) {
255 		/*
256 		 * do nothing in this case.
257 		 * in case you are wondering if "&&" is more correct than
258 		 * "||" here: rfc2553bis-03 says that host == NULL or
259 		 * hostlen == 0 means that the caller does not want the result.
260 		 */
261 	} else if (flags & NI_NUMERICHOST) {
262 		size_t numaddrlen;
263 
264 		/* NUMERICHOST and NAMEREQD conflicts with each other */
265 		if (flags & NI_NAMEREQD)
266 			return EAI_NONAME;
267 
268 		switch(afd->a_af) {
269 #ifdef INET6
270 		case AF_INET6:
271 		{
272 			int error;
273 
274 			if ((error = ip6_parsenumeric(sa, addr, host,
275 						      hostlen, flags)) != 0)
276 				return(error);
277 			break;
278 		}
279 #endif
280 		default:
281 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
282 			    == NULL)
283 				return EAI_SYSTEM;
284 			numaddrlen = strlen(numaddr);
285 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
286 				return EAI_MEMORY;
287 			strlcpy(host, numaddr, hostlen);
288 			break;
289 		}
290 	} else {
291 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
292 
293 		if (hp) {
294 #if 0
295 			/*
296 			 * commented out, since "for local host" is not
297 			 * implemented here - see RFC2553 p30
298 			 */
299 			if (flags & NI_NOFQDN) {
300 				char *p;
301 				p = strchr(hp->h_name, '.');
302 				if (p)
303 					*p = '\0';
304 			}
305 #endif
306 			if (strlen(hp->h_name) + 1 > hostlen) {
307 				freehostent(hp);
308 				return EAI_MEMORY;
309 			}
310 			strlcpy(host, hp->h_name, hostlen);
311 			freehostent(hp);
312 		} else {
313 			if (flags & NI_NAMEREQD)
314 				return EAI_NONAME;
315 			switch(afd->a_af) {
316 #ifdef INET6
317 			case AF_INET6:
318 			{
319 				int error;
320 
321 				if ((error = ip6_parsenumeric(sa, addr, host,
322 							      hostlen,
323 							      flags)) != 0)
324 					return(error);
325 				break;
326 			}
327 #endif
328 			default:
329 				if (inet_ntop(afd->a_af, addr, host,
330 				    hostlen) == NULL)
331 					return EAI_SYSTEM;
332 				break;
333 			}
334 		}
335 	}
336 	return(0);
337 }
338 
339 #ifdef INET6
340 static int
341 ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
342     char *host, size_t hostlen, int flags)
343 {
344 	size_t numaddrlen;
345 	char numaddr[512];
346 
347 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
348 		return EAI_SYSTEM;
349 
350 	numaddrlen = strlen(numaddr);
351 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
352 		return EAI_OVERFLOW;
353 	strlcpy(host, numaddr, hostlen);
354 
355 	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
356 		char zonebuf[MAXHOSTNAMELEN];
357 		int zonelen;
358 
359 		zonelen = ip6_sa2str(
360 		    (const struct sockaddr_in6 *)(const void *)sa,
361 		    zonebuf, sizeof(zonebuf), flags);
362 		if (zonelen < 0)
363 			return EAI_OVERFLOW;
364 		if (zonelen + 1 + numaddrlen + 1 > hostlen)
365 			return EAI_OVERFLOW;
366 
367 		/* construct <numeric-addr><delim><zoneid> */
368 		memcpy(host + numaddrlen + 1, zonebuf,
369 		    (size_t)zonelen);
370 		host[numaddrlen] = SCOPE_DELIMITER;
371 		host[numaddrlen + 1 + zonelen] = '\0';
372 	}
373 
374 	return 0;
375 }
376 
377 /* ARGSUSED */
378 static int
379 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
380 {
381 	unsigned int ifindex;
382 	const struct in6_addr *a6;
383 	int n;
384 
385 	ifindex = (unsigned int)sa6->sin6_scope_id;
386 	a6 = &sa6->sin6_addr;
387 
388 	if ((flags & NI_NUMERICSCOPE) != 0) {
389 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
390 		if (n < 0 || n >= bufsiz)
391 			return -1;
392 		else
393 			return n;
394 	}
395 
396 	/* if_indextoname() does not take buffer size.  not a good api... */
397 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
398 	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
399 		char *p = if_indextoname(ifindex, buf);
400 		if (p) {
401 			return(strlen(p));
402 		}
403 	}
404 
405 	/* last resort */
406 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
407 	if (n < 0 || (size_t)n >= bufsiz)
408 		return -1;
409 	else
410 		return n;
411 }
412 #endif /* INET6 */
413 
414 /*
415  * getnameinfo_link():
416  * Format a link-layer address into a printable format, paying attention to
417  * the interface type.
418  */
419 /* ARGSUSED */
420 static int
421 getnameinfo_link(const struct afd *afd,
422     const struct sockaddr *sa, socklen_t salen,
423     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
424 {
425 	const struct sockaddr_dl *sdl =
426 	    (const struct sockaddr_dl *)(const void *)sa;
427 	const struct fw_hwaddr *iha;
428 	int n;
429 
430 	if (serv != NULL && servlen > 0)
431 		*serv = '\0';
432 
433 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
434 		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
435 		if (n >= hostlen) {
436 			*host = '\0';
437 			return (EAI_MEMORY);
438 		}
439 		return (0);
440 	}
441 
442 	if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
443 		n = sdl->sdl_nlen;
444 		if (n >= hostlen) {
445 			*host = '\0';
446 			return (EAI_MEMORY);
447 		}
448 		memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
449 		host[n] = '\0';
450 		return (0);
451 	}
452 
453 	switch (sdl->sdl_type) {
454 	case IFT_IEEE1394:
455 		if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) +
456 		    sizeof(iha->sender_unique_ID_lo))
457 			return EAI_FAMILY;
458 		iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl);
459 		return hexname((const u_int8_t *)&iha->sender_unique_ID_hi,
460 		    sizeof(iha->sender_unique_ID_hi) +
461 		    sizeof(iha->sender_unique_ID_lo),
462 		    host, hostlen);
463 	/*
464 	 * The following have zero-length addresses.
465 	 * IFT_GIF	(net/if_gif.c)
466 	 * IFT_LOOP	(net/if_loop.c)
467 	 * IFT_PPP	(net/if_tuntap.c)
468 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
469 	 * IFT_STF	(net/if_stf.c)
470 	 * IFT_L2VLAN	(net/if_vlan.c)
471 	 * IFT_BRIDGE (net/if_bridge.h>
472 	 */
473 	/*
474 	 * The following use IPv4 addresses as link-layer addresses:
475 	 * IFT_OTHER	(net/if_gre.c)
476 	 * IFT_OTHER	(netinet/ip_ipip.c)
477 	 */
478 	/* default below is believed correct for all these. */
479 	case IFT_ETHER:
480 	case IFT_FDDI:
481 	case IFT_HIPPI:
482 	case IFT_ISO88025:
483 	default:
484 		return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
485 		    host, hostlen);
486 	}
487 }
488 
489 static int
490 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
491 {
492 	int i, n;
493 	char *outp = host;
494 
495 	*outp = '\0';
496 	for (i = 0; i < len; i++) {
497 		n = snprintf(outp, hostlen, "%s%02x",
498 		    i ? ":" : "", cp[i]);
499 		if (n < 0 || n >= hostlen) {
500 			*host = '\0';
501 			return EAI_MEMORY;
502 		}
503 		outp += n;
504 		hostlen -= n;
505 	}
506 	return 0;
507 }
508 
509 /*
510  * getnameinfo_un():
511  * Format a UNIX IPC domain address (pathname).
512  */
513 /* ARGSUSED */
514 static int
515 getnameinfo_un(const struct afd *afd,
516     const struct sockaddr *sa, socklen_t salen,
517     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
518 {
519 	size_t pathlen;
520 
521 	if (serv != NULL && servlen > 0)
522 		*serv = '\0';
523 	if (host != NULL && hostlen > 0) {
524 		pathlen = salen - afd->a_off;
525 
526 		if (pathlen + 1 > hostlen) {
527 			*host = '\0';
528 			return (EAI_MEMORY);
529 		}
530 		strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1);
531 	}
532 
533 	return (0);
534 }
535