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