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