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