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