xref: /freebsd/lib/libc/net/getnameinfo.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
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 	if ((flags & NI_NUMERICSCOPE) != 0) {
382 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
383 		if (n < 0 || n >= bufsiz)
384 			return -1;
385 		else
386 			return n;
387 	}
388 
389 	/* if_indextoname() does not take buffer size.  not a good api... */
390 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
391 	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
392 		char *p = if_indextoname(ifindex, buf);
393 		if (p) {
394 			return(strlen(p));
395 		}
396 	}
397 
398 	/* last resort */
399 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
400 	if (n < 0 || (size_t)n >= bufsiz)
401 		return -1;
402 	else
403 		return n;
404 }
405 #endif /* INET6 */
406 
407 /*
408  * getnameinfo_link():
409  * Format a link-layer address into a printable format, paying attention to
410  * the interface type.
411  */
412 /* ARGSUSED */
413 static int
414 getnameinfo_link(const struct afd *afd,
415     const struct sockaddr *sa, socklen_t salen,
416     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
417 {
418 	const struct sockaddr_dl *sdl =
419 	    (const struct sockaddr_dl *)(const void *)sa;
420 	const struct fw_hwaddr *iha;
421 	int n;
422 
423 	if (serv != NULL && servlen > 0)
424 		*serv = '\0';
425 
426 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
427 		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
428 		if (n >= hostlen) {
429 			*host = '\0';
430 			return (EAI_MEMORY);
431 		}
432 		return (0);
433 	}
434 
435 	if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
436 		n = sdl->sdl_nlen;
437 		if (n >= hostlen) {
438 			*host = '\0';
439 			return (EAI_MEMORY);
440 		}
441 		memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
442 		host[n] = '\0';
443 		return (0);
444 	}
445 
446 	switch (sdl->sdl_type) {
447 	case IFT_IEEE1394:
448 		if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) +
449 		    sizeof(iha->sender_unique_ID_lo))
450 			return EAI_FAMILY;
451 		iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl);
452 		return hexname((const u_int8_t *)&iha->sender_unique_ID_hi,
453 		    sizeof(iha->sender_unique_ID_hi) +
454 		    sizeof(iha->sender_unique_ID_lo),
455 		    host, hostlen);
456 	/*
457 	 * The following have zero-length addresses.
458 	 * IFT_ATM	(net/if_atmsubr.c)
459 	 * IFT_GIF	(net/if_gif.c)
460 	 * IFT_LOOP	(net/if_loop.c)
461 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
462 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
463 	 * IFT_STF	(net/if_stf.c)
464 	 * IFT_L2VLAN	(net/if_vlan.c)
465 	 * IFT_BRIDGE (net/if_bridge.h>
466 	 */
467 	/*
468 	 * The following use IPv4 addresses as link-layer addresses:
469 	 * IFT_OTHER	(net/if_gre.c)
470 	 * IFT_OTHER	(netinet/ip_ipip.c)
471 	 */
472 	/* default below is believed correct for all these. */
473 	case IFT_ARCNET:
474 	case IFT_ETHER:
475 	case IFT_FDDI:
476 	case IFT_HIPPI:
477 	case IFT_ISO88025:
478 	default:
479 		return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
480 		    host, hostlen);
481 	}
482 }
483 
484 static int
485 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
486 {
487 	int i, n;
488 	char *outp = host;
489 
490 	*outp = '\0';
491 	for (i = 0; i < len; i++) {
492 		n = snprintf(outp, hostlen, "%s%02x",
493 		    i ? ":" : "", cp[i]);
494 		if (n < 0 || n >= hostlen) {
495 			*host = '\0';
496 			return EAI_MEMORY;
497 		}
498 		outp += n;
499 		hostlen -= n;
500 	}
501 	return 0;
502 }
503 
504 /*
505  * getnameinfo_un():
506  * Format a UNIX IPC domain address (pathname).
507  */
508 /* ARGSUSED */
509 static int
510 getnameinfo_un(const struct afd *afd,
511     const struct sockaddr *sa, socklen_t salen,
512     char *host, size_t hostlen, char *serv, size_t servlen, int flags)
513 {
514 	size_t pathlen;
515 
516 	if (serv != NULL && servlen > 0)
517 		*serv = '\0';
518 	if (host != NULL && hostlen > 0) {
519 		pathlen = sa->sa_len - afd->a_off;
520 
521 		if (pathlen + 1 > hostlen) {
522 			*host = '\0';
523 			return (EAI_MEMORY);
524 		}
525 		strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1);
526 	}
527 
528 	return (0);
529 }
530