xref: /netbsd-src/lib/libc/net/getnameinfo.c (revision 511e898dc22170bd9c8df38b43681e646939c9fd)
1 /*	$NetBSD: getnameinfo.c,v 1.28 2001/08/19 01:37:39 itojun Exp $	*/
2 /*	$KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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) NI_WITHSCOPEID when called with global address,
42  *   and sin6_scope_id filled
43  */
44 
45 #include <sys/cdefs.h>
46 #if defined(LIBC_SCCS) && !defined(lint)
47 __RCSID("$NetBSD: getnameinfo.c,v 1.28 2001/08/19 01:37:39 itojun Exp $");
48 #endif /* LIBC_SCCS and not lint */
49 
50 #include "namespace.h"
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <arpa/nameser.h>
57 #include <assert.h>
58 #include <netdb.h>
59 #include <resolv.h>
60 #include <stddef.h>
61 #include <string.h>
62 
63 #ifdef __weak_alias
64 __weak_alias(getnameinfo,_getnameinfo)
65 #endif
66 
67 #define SUCCESS 0
68 #define ANY 0
69 #define YES 1
70 #define NO  0
71 
72 static const struct afd {
73 	int		a_af;
74 	socklen_t	a_addrlen;
75 	socklen_t	a_socklen;
76 	int		a_off;
77 } afdl [] = {
78 #ifdef INET6
79 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
80 		offsetof(struct sockaddr_in6, sin6_addr)},
81 #endif
82 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 		offsetof(struct sockaddr_in, sin_addr)},
84 	{0, 0, 0},
85 };
86 
87 struct sockinet {
88 	u_char	si_len;
89 	u_char	si_family;
90 	u_short	si_port;
91 };
92 
93 #ifdef INET6
94 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
95 				 size_t, int));
96 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
97 #endif
98 
99 /* 2553bis: use EAI_xx for getnameinfo */
100 #define ENI_NOSOCKET 	EAI_FAIL		/*XXX*/
101 #define ENI_NOSERVNAME	EAI_NONAME
102 #define ENI_NOHOSTNAME	EAI_NONAME
103 #define ENI_MEMORY	EAI_MEMORY
104 #define ENI_SYSTEM	EAI_SYSTEM
105 #define ENI_FAMILY	EAI_FAMILY
106 #define ENI_SALEN	EAI_FAMILY
107 
108 int
109 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
110 	const struct sockaddr *sa;
111 	socklen_t salen;
112 	char *host;
113 	size_t hostlen;
114 	char *serv;
115 	size_t servlen;
116 	int flags;
117 {
118 	const struct afd *afd;
119 	struct servent *sp;
120 	struct hostent *hp;
121 	u_short port;
122 	int family, i;
123 	const char *addr;
124 	u_int32_t v4a;
125 	char numserv[512];
126 	char numaddr[512];
127 
128 	/* sa is checked below */
129 	/* host may be NULL */
130 	/* serv may be NULL */
131 
132 	if (sa == NULL)
133 		return ENI_NOSOCKET;
134 
135 #ifdef BSD4_4
136 	if (sa->sa_len != salen)
137 		return ENI_SALEN;
138 #endif
139 
140 	family = sa->sa_family;
141 	for (i = 0; afdl[i].a_af; i++)
142 		if (afdl[i].a_af == family) {
143 			afd = &afdl[i];
144 			goto found;
145 		}
146 	return ENI_FAMILY;
147 
148  found:
149 	if (salen != afd->a_socklen)
150 		return ENI_SALEN;
151 
152 	/* network byte order */
153 	port = ((const struct sockinet *)(const void *)sa)->si_port;
154 	addr = (const char *)(const void *)sa + afd->a_off;
155 
156 	if (serv == NULL || servlen == 0) {
157 		/*
158 		 * do nothing in this case.
159 		 * in case you are wondering if "&&" is more correct than
160 		 * "||" here: RFC2553 says that serv == NULL OR servlen == 0
161 		 * means that the caller does not want the result.
162 		 */
163 	} else {
164 		if (flags & NI_NUMERICSERV)
165 			sp = NULL;
166 		else {
167 			sp = getservbyport(port,
168 				(flags & NI_DGRAM) ? "udp" : "tcp");
169 		}
170 		if (sp) {
171 			if (strlen(sp->s_name) + 1 > servlen)
172 				return ENI_MEMORY;
173 			strcpy(serv, sp->s_name);
174 		} else {
175 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
176 			if (strlen(numserv) + 1 > servlen)
177 				return ENI_MEMORY;
178 			strcpy(serv, numserv);
179 		}
180 	}
181 
182 	switch (sa->sa_family) {
183 	case AF_INET:
184 		v4a = (u_int32_t)
185 		    ntohl(((const struct sockaddr_in *)
186 		    (const void *)sa)->sin_addr.s_addr);
187 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
188 			flags |= NI_NUMERICHOST;
189 		v4a >>= IN_CLASSA_NSHIFT;
190 		if (v4a == 0)
191 			flags |= NI_NUMERICHOST;
192 		break;
193 #ifdef INET6
194 	case AF_INET6:
195 	    {
196 		const struct sockaddr_in6 *sin6;
197 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
198 		switch (sin6->sin6_addr.s6_addr[0]) {
199 		case 0x00:
200 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
201 				;
202 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
203 				;
204 			else
205 				flags |= NI_NUMERICHOST;
206 			break;
207 		default:
208 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
209 				flags |= NI_NUMERICHOST;
210 			}
211 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
212 				flags |= NI_NUMERICHOST;
213 			break;
214 		}
215 	    }
216 		break;
217 #endif
218 	}
219 	if (host == NULL || hostlen == 0) {
220 		/*
221 		 * do nothing in this case.
222 		 * in case you are wondering if "&&" is more correct than
223 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
224 		 * means that the caller does not want the result.
225 		 */
226 	} else if (flags & NI_NUMERICHOST) {
227 		int numaddrlen;
228 
229 		/* NUMERICHOST and NAMEREQD conflicts with each other */
230 		if (flags & NI_NAMEREQD)
231 			return ENI_NOHOSTNAME;
232 
233 		switch(afd->a_af) {
234 #ifdef INET6
235 		case AF_INET6:
236 		{
237 			int error;
238 
239 			if ((error = ip6_parsenumeric(sa, addr, host,
240 						      hostlen, flags)) != 0)
241 				return(error);
242 			break;
243 		}
244 #endif
245 		default:
246 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
247 			    == NULL)
248 				return ENI_SYSTEM;
249 			numaddrlen = strlen(numaddr);
250 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
251 				return ENI_MEMORY;
252 			strcpy(host, numaddr);
253 			break;
254 		}
255 	} else {
256 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
257 
258 		if (hp) {
259 #if 0
260 			/*
261 			 * commented out, since "for local host" is not
262 			 * implemented here - see RFC2553 p30
263 			 */
264 			if (flags & NI_NOFQDN) {
265 				char *p;
266 				p = strchr(hp->h_name, '.');
267 				if (p)
268 					*p = '\0';
269 			}
270 #endif
271 			if (strlen(hp->h_name) + 1 > hostlen) {
272 				return ENI_MEMORY;
273 			}
274 			strcpy(host, hp->h_name);
275 		} else {
276 			if (flags & NI_NAMEREQD)
277 				return ENI_NOHOSTNAME;
278 			switch(afd->a_af) {
279 #ifdef INET6
280 			case AF_INET6:
281 			{
282 				int error;
283 
284 				if ((error = ip6_parsenumeric(sa, addr, host,
285 							      hostlen,
286 							      flags)) != 0)
287 					return(error);
288 				break;
289 			}
290 #endif
291 			default:
292 				if (inet_ntop(afd->a_af, addr, host,
293 				    hostlen) == NULL)
294 					return ENI_SYSTEM;
295 				break;
296 			}
297 		}
298 	}
299 	return SUCCESS;
300 }
301 
302 #ifdef INET6
303 static int
304 ip6_parsenumeric(sa, addr, host, hostlen, flags)
305 	const struct sockaddr *sa;
306 	const char *addr;
307 	char *host;
308 	size_t hostlen;
309 	int flags;
310 {
311 	int numaddrlen;
312 	char numaddr[512];
313 
314 	_DIAGASSERT(sa != NULL);
315 	_DIAGASSERT(addr != NULL);
316 	_DIAGASSERT(host != NULL);
317 
318 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
319 	    == NULL)
320 		return ENI_SYSTEM;
321 
322 	numaddrlen = strlen(numaddr);
323 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
324 		return ENI_MEMORY;
325 	strcpy(host, numaddr);
326 
327 #ifdef NI_WITHSCOPEID
328 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
329 		if (flags & NI_WITHSCOPEID)
330 		{
331 			char scopebuf[MAXHOSTNAMELEN];
332 			int scopelen;
333 
334 			scopelen = ip6_sa2str(
335 			    (const struct sockaddr_in6 *)(const void *)sa,
336 			    scopebuf, sizeof(scopebuf), 0);
337 			if (scopelen < 0)
338 				return ENI_MEMORY;
339 			if (scopelen + 1 + numaddrlen + 1 > hostlen)
340 				return ENI_MEMORY;
341 			/*
342 			 * construct <numeric-addr><delim><scopeid>
343 			 */
344 			memcpy(host + numaddrlen + 1, scopebuf,
345 			    (size_t)scopelen);
346 			host[numaddrlen] = SCOPE_DELIMITER;
347 			host[numaddrlen + 1 + scopelen] = '\0';
348 		}
349 	}
350 #endif /* NI_WITHSCOPEID */
351 
352 	return 0;
353 }
354 
355 /* ARGSUSED */
356 static int
357 ip6_sa2str(sa6, buf, bufsiz, flags)
358 	const struct sockaddr_in6 *sa6;
359 	char *buf;
360 	size_t bufsiz;
361 	int flags;
362 {
363 	unsigned int ifindex;
364 	const struct in6_addr *a6;
365 	int n;
366 
367 	_DIAGASSERT(sa6 != NULL);
368 	_DIAGASSERT(buf != NULL);
369 
370 	ifindex = (unsigned int)sa6->sin6_scope_id;
371 	a6 = &sa6->sin6_addr;
372 
373 #ifdef NI_NUMERICSCOPE
374 	if ((flags & NI_NUMERICSCOPE) != 0) {
375 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
376 		if (n < 0 || n >= bufsiz)
377 			return -1;
378 		else
379 			return n;
380 	}
381 #endif
382 
383 	/* if_indextoname() does not take buffer size.  not a good api... */
384 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
385 	    bufsiz >= IF_NAMESIZE) {
386 		char *p = if_indextoname(ifindex, buf);
387 		if (p) {
388 			return(strlen(p));
389 		}
390 	}
391 
392 	/* last resort */
393 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
394 	if (n < 0 || n >= bufsiz)
395 		return -1;
396 	else
397 		return n;
398 }
399 #endif /* INET6 */
400