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/cdefs.h>
50__FBSDID("$FreeBSD$");
51
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <sys/un.h>
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_types.h>
58#include <net/firewire.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
61#include <arpa/nameser.h>
62#include <netdb.h>
63#include <resolv.h>
64#include <string.h>
65#include <stddef.h>
66#include <errno.h>
67
68static const struct afd *find_afd(int);
69static int getnameinfo_inet(const struct afd *,
70    const struct sockaddr *, socklen_t, char *,
71    size_t, char *, size_t, int);
72#ifdef INET6
73static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
74    size_t, int);
75static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
76#endif
77static int getnameinfo_link(const struct afd *,
78    const struct sockaddr *, socklen_t, char *,
79    size_t, char *, size_t, int);
80static int hexname(const u_int8_t *, size_t, char *, size_t);
81static int getnameinfo_un(const struct afd *,
82    const struct sockaddr *, socklen_t, char *,
83    size_t, char *, size_t, int);
84
85static const struct afd {
86	int a_af;
87	size_t a_addrlen;
88	socklen_t a_socklen;
89	int a_off;
90	int (*a_func)(const struct afd *,
91	    const struct sockaddr *, socklen_t, char *,
92	    size_t, char *, size_t, int);
93} afdl [] = {
94#ifdef INET6
95	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
96	    offsetof(struct sockaddr_in6, sin6_addr),
97	    getnameinfo_inet},
98#endif
99	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
100	    offsetof(struct sockaddr_in, sin_addr),
101	    getnameinfo_inet},
102#define	sizeofmember(type, member)	(sizeof(((type *)0)->member))
103	{PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
104	    sizeof(struct sockaddr_un),
105	    offsetof(struct sockaddr_un, sun_path),
106	    getnameinfo_un},
107	{PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data),
108	    sizeof(struct sockaddr_dl),
109	    offsetof(struct sockaddr_dl, sdl_data),
110	    getnameinfo_link},
111	{0, 0, 0},
112};
113
114int
115getnameinfo(const struct sockaddr *sa, socklen_t salen,
116    char *host, size_t hostlen, char *serv, size_t servlen,
117    int flags)
118{
119	const struct afd *afd;
120
121	if (sa == NULL)
122		return (EAI_FAIL);
123
124	afd = find_afd(sa->sa_family);
125	if (afd == NULL)
126		return (EAI_FAMILY);
127	/*
128	 * getnameinfo() accepts an salen of sizeof(struct sockaddr_storage)
129	 * at maximum as shown in RFC 4038 Sec.6.2.3.
130	 */
131	if (salen > sizeof(struct sockaddr_storage))
132		return (EAI_FAMILY);
133
134	switch (sa->sa_family) {
135	case PF_LOCAL:
136		/*
137		 * PF_LOCAL uses variable salen depending on the
138		 * content length of sun_path.  Require 1 byte in
139		 * sun_path at least.
140		 */
141		if (salen <= afd->a_socklen -
142			sizeofmember(struct sockaddr_un, sun_path))
143			return (EAI_FAMILY);
144		else if (salen > afd->a_socklen)
145			salen = afd->a_socklen;
146		break;
147	case PF_LINK:
148		if (salen <= afd->a_socklen -
149			sizeofmember(struct sockaddr_dl, sdl_data))
150			return (EAI_FAMILY);
151		break;
152	default:
153		if (salen < afd->a_socklen)
154			return (EAI_FAMILY);
155		else
156			salen = afd->a_socklen;
157		break;
158	}
159
160	return ((*afd->a_func)(afd, sa, salen, host, hostlen,
161	    serv, servlen, flags));
162}
163
164static const struct afd *
165find_afd(int af)
166{
167	const struct afd *afd;
168
169	if (af == PF_UNSPEC)
170		return (NULL);
171	for (afd = &afdl[0]; afd->a_af > 0; afd++) {
172		if (afd->a_af == af)
173			return (afd);
174	}
175	return (NULL);
176}
177
178static int
179getnameinfo_inet(const struct afd *afd,
180    const struct sockaddr *sa, socklen_t salen,
181    char *host, size_t hostlen, char *serv, size_t servlen,
182    int flags)
183{
184	struct servent *sp;
185	struct hostent *hp;
186	u_short port;
187	const char *addr;
188	u_int32_t v4a;
189	int h_error;
190	char numserv[512];
191	char numaddr[512];
192
193	/* network byte order */
194	port = ((const struct sockaddr_in *)sa)->sin_port;
195	addr = (const char *)sa + afd->a_off;
196
197	if (serv == NULL || servlen == 0) {
198		/*
199		 * do nothing in this case.
200		 * in case you are wondering if "&&" is more correct than
201		 * "||" here: rfc2553bis-03 says that serv == NULL OR
202		 * servlen == 0 means that the caller does not want the result.
203		 */
204	} else {
205		if (flags & NI_NUMERICSERV)
206			sp = NULL;
207		else {
208			sp = getservbyport(port,
209				(flags & NI_DGRAM) ? "udp" : "tcp");
210		}
211		if (sp) {
212			if (strlen(sp->s_name) + 1 > servlen)
213				return EAI_MEMORY;
214			strlcpy(serv, sp->s_name, servlen);
215		} else {
216			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
217			if (strlen(numserv) + 1 > servlen)
218				return EAI_MEMORY;
219			strlcpy(serv, numserv, servlen);
220		}
221	}
222
223	switch (sa->sa_family) {
224	case AF_INET:
225		v4a = (u_int32_t)
226		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
227		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a) ||
228		    IN_ZERONET(v4a))
229			flags |= NI_NUMERICHOST;
230		break;
231#ifdef INET6
232	case AF_INET6:
233	    {
234		const struct sockaddr_in6 *sin6;
235		sin6 = (const struct sockaddr_in6 *)sa;
236		switch (sin6->sin6_addr.s6_addr[0]) {
237		case 0x00:
238			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
239				;
240			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
241				;
242			else
243				flags |= NI_NUMERICHOST;
244			break;
245		default:
246			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
247				flags |= NI_NUMERICHOST;
248			}
249			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
250				flags |= NI_NUMERICHOST;
251			break;
252		}
253	    }
254		break;
255#endif
256	}
257	if (host == NULL || hostlen == 0) {
258		/*
259		 * do nothing in this case.
260		 * in case you are wondering if "&&" is more correct than
261		 * "||" here: rfc2553bis-03 says that host == NULL or
262		 * hostlen == 0 means that the caller does not want the result.
263		 */
264	} else if (flags & NI_NUMERICHOST) {
265		size_t numaddrlen;
266
267		/* NUMERICHOST and NAMEREQD conflicts with each other */
268		if (flags & NI_NAMEREQD)
269			return EAI_NONAME;
270
271		switch(afd->a_af) {
272#ifdef INET6
273		case AF_INET6:
274		{
275			int error;
276
277			if ((error = ip6_parsenumeric(sa, addr, host,
278						      hostlen, flags)) != 0)
279				return(error);
280			break;
281		}
282#endif
283		default:
284			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
285			    == NULL)
286				return EAI_SYSTEM;
287			numaddrlen = strlen(numaddr);
288			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
289				return EAI_MEMORY;
290			strlcpy(host, numaddr, hostlen);
291			break;
292		}
293	} else {
294		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
295
296		if (hp) {
297#if 0
298			/*
299			 * commented out, since "for local host" is not
300			 * implemented here - see RFC2553 p30
301			 */
302			if (flags & NI_NOFQDN) {
303				char *p;
304				p = strchr(hp->h_name, '.');
305				if (p)
306					*p = '\0';
307			}
308#endif
309			if (strlen(hp->h_name) + 1 > hostlen) {
310				freehostent(hp);
311				return EAI_MEMORY;
312			}
313			strlcpy(host, hp->h_name, hostlen);
314			freehostent(hp);
315		} else {
316			if (flags & NI_NAMEREQD)
317				return EAI_NONAME;
318			switch(afd->a_af) {
319#ifdef INET6
320			case AF_INET6:
321			{
322				int error;
323
324				if ((error = ip6_parsenumeric(sa, addr, host,
325							      hostlen,
326							      flags)) != 0)
327					return(error);
328				break;
329			}
330#endif
331			default:
332				if (inet_ntop(afd->a_af, addr, host,
333				    hostlen) == NULL)
334					return EAI_SYSTEM;
335				break;
336			}
337		}
338	}
339	return(0);
340}
341
342#ifdef INET6
343static int
344ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
345    char *host, size_t hostlen, int flags)
346{
347	size_t numaddrlen;
348	char numaddr[512];
349
350	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
351		return EAI_SYSTEM;
352
353	numaddrlen = strlen(numaddr);
354	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
355		return EAI_OVERFLOW;
356	strlcpy(host, numaddr, hostlen);
357
358	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
359		char zonebuf[MAXHOSTNAMELEN];
360		int zonelen;
361
362		zonelen = ip6_sa2str(
363		    (const struct sockaddr_in6 *)(const void *)sa,
364		    zonebuf, sizeof(zonebuf), flags);
365		if (zonelen < 0)
366			return EAI_OVERFLOW;
367		if (zonelen + 1 + numaddrlen + 1 > hostlen)
368			return EAI_OVERFLOW;
369
370		/* construct <numeric-addr><delim><zoneid> */
371		memcpy(host + numaddrlen + 1, zonebuf,
372		    (size_t)zonelen);
373		host[numaddrlen] = SCOPE_DELIMITER;
374		host[numaddrlen + 1 + zonelen] = '\0';
375	}
376
377	return 0;
378}
379
380/* ARGSUSED */
381static int
382ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
383{
384	unsigned int ifindex;
385	const struct in6_addr *a6;
386	int n;
387
388	ifindex = (unsigned int)sa6->sin6_scope_id;
389	a6 = &sa6->sin6_addr;
390
391	if ((flags & NI_NUMERICSCOPE) != 0) {
392		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
393		if (n < 0 || n >= bufsiz)
394			return -1;
395		else
396			return n;
397	}
398
399	/* if_indextoname() does not take buffer size.  not a good api... */
400	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
401	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
402		char *p = if_indextoname(ifindex, buf);
403		if (p) {
404			return(strlen(p));
405		}
406	}
407
408	/* last resort */
409	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
410	if (n < 0 || (size_t)n >= bufsiz)
411		return -1;
412	else
413		return n;
414}
415#endif /* INET6 */
416
417/*
418 * getnameinfo_link():
419 * Format a link-layer address into a printable format, paying attention to
420 * the interface type.
421 */
422/* ARGSUSED */
423static int
424getnameinfo_link(const struct afd *afd,
425    const struct sockaddr *sa, socklen_t salen,
426    char *host, size_t hostlen, char *serv, size_t servlen, int flags)
427{
428	const struct sockaddr_dl *sdl =
429	    (const struct sockaddr_dl *)(const void *)sa;
430	const struct fw_hwaddr *iha;
431	int n;
432
433	if (serv != NULL && servlen > 0)
434		*serv = '\0';
435
436	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
437		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
438		if (n >= hostlen) {
439			*host = '\0';
440			return (EAI_MEMORY);
441		}
442		return (0);
443	}
444
445	if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
446		n = sdl->sdl_nlen;
447		if (n >= hostlen) {
448			*host = '\0';
449			return (EAI_MEMORY);
450		}
451		memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
452		host[n] = '\0';
453		return (0);
454	}
455
456	switch (sdl->sdl_type) {
457	case IFT_IEEE1394:
458		if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) +
459		    sizeof(iha->sender_unique_ID_lo))
460			return EAI_FAMILY;
461		iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl);
462		return hexname((const u_int8_t *)&iha->sender_unique_ID_hi,
463		    sizeof(iha->sender_unique_ID_hi) +
464		    sizeof(iha->sender_unique_ID_lo),
465		    host, hostlen);
466	/*
467	 * The following have zero-length addresses.
468	 * IFT_GIF	(net/if_gif.c)
469	 * IFT_LOOP	(net/if_loop.c)
470	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
471	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
472	 * IFT_STF	(net/if_stf.c)
473	 * IFT_L2VLAN	(net/if_vlan.c)
474	 * IFT_BRIDGE (net/if_bridge.h>
475	 */
476	/*
477	 * The following use IPv4 addresses as link-layer addresses:
478	 * IFT_OTHER	(net/if_gre.c)
479	 * IFT_OTHER	(netinet/ip_ipip.c)
480	 */
481	/* default below is believed correct for all these. */
482	case IFT_ETHER:
483	case IFT_FDDI:
484	case IFT_HIPPI:
485	case IFT_ISO88025:
486	default:
487		return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
488		    host, hostlen);
489	}
490}
491
492static int
493hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
494{
495	int i, n;
496	char *outp = host;
497
498	*outp = '\0';
499	for (i = 0; i < len; i++) {
500		n = snprintf(outp, hostlen, "%s%02x",
501		    i ? ":" : "", cp[i]);
502		if (n < 0 || n >= hostlen) {
503			*host = '\0';
504			return EAI_MEMORY;
505		}
506		outp += n;
507		hostlen -= n;
508	}
509	return 0;
510}
511
512/*
513 * getnameinfo_un():
514 * Format a UNIX IPC domain address (pathname).
515 */
516/* ARGSUSED */
517static int
518getnameinfo_un(const struct afd *afd,
519    const struct sockaddr *sa, socklen_t salen,
520    char *host, size_t hostlen, char *serv, size_t servlen, int flags)
521{
522	size_t pathlen;
523
524	if (serv != NULL && servlen > 0)
525		*serv = '\0';
526	if (host != NULL && hostlen > 0) {
527		pathlen = salen - afd->a_off;
528
529		if (pathlen + 1 > hostlen) {
530			*host = '\0';
531			return (EAI_MEMORY);
532		}
533		strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1);
534	}
535
536	return (0);
537}
538