xref: /freebsd/lib/libc/net/getnameinfo.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Issues to be discussed:
34  * - Thread safe-ness must be checked
35  * - RFC2553 says that we should raise error on short buffer.  X/Open says
36  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
37  *   modified).  ipngwg rough consensus seems to follow RFC2553.
38  * - What is "local" in NI_FQDN?
39  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
40  * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
41  *   sin6_scope_id is filled - standardization status?
42  *   XXX breaks backward compat for code that expects no scopeid.
43  *   beware on merge.
44  */
45 
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <arpa/nameser.h>
55 #include <netdb.h>
56 #include <resolv.h>
57 #include <string.h>
58 #include <stddef.h>
59 #include <errno.h>
60 
61 static const struct afd {
62 	int a_af;
63 	int a_addrlen;
64 	int a_socklen;
65 	int a_off;
66 } afdl [] = {
67 #ifdef INET6
68 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
69 		offsetof(struct sockaddr_in6, sin6_addr)},
70 #endif
71 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
72 		offsetof(struct sockaddr_in, sin_addr)},
73 	{0, 0, 0},
74 };
75 
76 struct sockinet {
77 	u_char	si_len;
78 	u_char	si_family;
79 	u_short	si_port;
80 };
81 
82 #ifdef INET6
83 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
84     size_t, int);
85 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
86 #endif
87 
88 int
89 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
90 	const struct sockaddr *sa;
91 	socklen_t salen;
92 	char *host;
93 	size_t hostlen;
94 	char *serv;
95 	size_t servlen;
96 	int flags;
97 {
98 	const struct afd *afd;
99 	struct servent *sp;
100 	struct hostent *hp;
101 	u_short port;
102 	int family, i;
103 	const char *addr;
104 	u_int32_t v4a;
105 	int h_error;
106 	char numserv[512];
107 	char numaddr[512];
108 
109 	if (sa == NULL)
110 		return EAI_FAIL;
111 
112 	if (sa->sa_len != salen)
113 		return EAI_FAIL;
114 
115 	family = sa->sa_family;
116 	for (i = 0; afdl[i].a_af; i++)
117 		if (afdl[i].a_af == family) {
118 			afd = &afdl[i];
119 			goto found;
120 		}
121 	return EAI_FAMILY;
122 
123  found:
124 	if (salen != afd->a_socklen)
125 		return EAI_FAIL;
126 
127 	/* network byte order */
128 	port = ((const struct sockinet *)sa)->si_port;
129 	addr = (const char *)sa + afd->a_off;
130 
131 	if (serv == NULL || servlen == 0) {
132 		/*
133 		 * do nothing in this case.
134 		 * in case you are wondering if "&&" is more correct than
135 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
136 		 * servlen == 0 means that the caller does not want the result.
137 		 */
138 	} else {
139 		if (flags & NI_NUMERICSERV)
140 			sp = NULL;
141 		else {
142 			sp = getservbyport(port,
143 				(flags & NI_DGRAM) ? "udp" : "tcp");
144 		}
145 		if (sp) {
146 			if (strlen(sp->s_name) + 1 > servlen)
147 				return EAI_MEMORY;
148 			strlcpy(serv, sp->s_name, servlen);
149 		} else {
150 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
151 			if (strlen(numserv) + 1 > servlen)
152 				return EAI_MEMORY;
153 			strlcpy(serv, numserv, servlen);
154 		}
155 	}
156 
157 	switch (sa->sa_family) {
158 	case AF_INET:
159 		v4a = (u_int32_t)
160 		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
161 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
162 			flags |= NI_NUMERICHOST;
163 		v4a >>= IN_CLASSA_NSHIFT;
164 		if (v4a == 0)
165 			flags |= NI_NUMERICHOST;
166 		break;
167 #ifdef INET6
168 	case AF_INET6:
169 	    {
170 		const struct sockaddr_in6 *sin6;
171 		sin6 = (const struct sockaddr_in6 *)sa;
172 		switch (sin6->sin6_addr.s6_addr[0]) {
173 		case 0x00:
174 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
175 				;
176 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
177 				;
178 			else
179 				flags |= NI_NUMERICHOST;
180 			break;
181 		default:
182 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
183 				flags |= NI_NUMERICHOST;
184 			}
185 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
186 				flags |= NI_NUMERICHOST;
187 			break;
188 		}
189 	    }
190 		break;
191 #endif
192 	}
193 	if (host == NULL || hostlen == 0) {
194 		/*
195 		 * do nothing in this case.
196 		 * in case you are wondering if "&&" is more correct than
197 		 * "||" here: rfc2553bis-03 says that host == NULL or
198 		 * hostlen == 0 means that the caller does not want the result.
199 		 */
200 	} else if (flags & NI_NUMERICHOST) {
201 		int numaddrlen;
202 
203 		/* NUMERICHOST and NAMEREQD conflicts with each other */
204 		if (flags & NI_NAMEREQD)
205 			return EAI_NONAME;
206 
207 		switch(afd->a_af) {
208 #ifdef INET6
209 		case AF_INET6:
210 		{
211 			int error;
212 
213 			if ((error = ip6_parsenumeric(sa, addr, host,
214 						      hostlen, flags)) != 0)
215 				return(error);
216 			break;
217 		}
218 #endif
219 		default:
220 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
221 			    == NULL)
222 				return EAI_SYSTEM;
223 			numaddrlen = strlen(numaddr);
224 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
225 				return EAI_MEMORY;
226 			strlcpy(host, numaddr, hostlen);
227 			break;
228 		}
229 	} else {
230 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
231 
232 		if (hp) {
233 #if 0
234 			/*
235 			 * commented out, since "for local host" is not
236 			 * implemented here - see RFC2553 p30
237 			 */
238 			if (flags & NI_NOFQDN) {
239 				char *p;
240 				p = strchr(hp->h_name, '.');
241 				if (p)
242 					*p = '\0';
243 			}
244 #endif
245 			if (strlen(hp->h_name) + 1 > hostlen) {
246 				freehostent(hp);
247 				return EAI_MEMORY;
248 			}
249 			strlcpy(host, hp->h_name, hostlen);
250 			freehostent(hp);
251 		} else {
252 			if (flags & NI_NAMEREQD)
253 				return EAI_NONAME;
254 			switch(afd->a_af) {
255 #ifdef INET6
256 			case AF_INET6:
257 			{
258 				int error;
259 
260 				if ((error = ip6_parsenumeric(sa, addr, host,
261 							      hostlen,
262 							      flags)) != 0)
263 					return(error);
264 				break;
265 			}
266 #endif
267 			default:
268 				if (inet_ntop(afd->a_af, addr, host,
269 				    hostlen) == NULL)
270 					return EAI_SYSTEM;
271 				break;
272 			}
273 		}
274 	}
275 	return(0);
276 }
277 
278 #ifdef INET6
279 static int
280 ip6_parsenumeric(sa, addr, host, hostlen, flags)
281 	const struct sockaddr *sa;
282 	const char *addr;
283 	char *host;
284 	size_t hostlen;
285 	int flags;
286 {
287 	int numaddrlen;
288 	char numaddr[512];
289 
290 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
291 		return EAI_SYSTEM;
292 
293 	numaddrlen = strlen(numaddr);
294 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
295 		return EAI_MEMORY;
296 	strlcpy(host, numaddr, hostlen);
297 
298 	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
299 		char zonebuf[MAXHOSTNAMELEN];
300 		int zonelen;
301 
302 		zonelen = ip6_sa2str(
303 		    (const struct sockaddr_in6 *)(const void *)sa,
304 		    zonebuf, sizeof(zonebuf), flags);
305 		if (zonelen < 0)
306 			return EAI_MEMORY;
307 		if (zonelen + 1 + numaddrlen + 1 > hostlen)
308 			return EAI_MEMORY;
309 
310 		/* construct <numeric-addr><delim><zoneid> */
311 		memcpy(host + numaddrlen + 1, zonebuf,
312 		    (size_t)zonelen);
313 		host[numaddrlen] = SCOPE_DELIMITER;
314 		host[numaddrlen + 1 + zonelen] = '\0';
315 	}
316 
317 	return 0;
318 }
319 
320 /* ARGSUSED */
321 static int
322 ip6_sa2str(sa6, buf, bufsiz, flags)
323 	const struct sockaddr_in6 *sa6;
324 	char *buf;
325 	size_t bufsiz;
326 	int flags;
327 {
328 	unsigned int ifindex;
329 	const struct in6_addr *a6;
330 	int n;
331 
332 	ifindex = (unsigned int)sa6->sin6_scope_id;
333 	a6 = &sa6->sin6_addr;
334 
335 #ifdef NI_NUMERICSCOPE
336 	if ((flags & NI_NUMERICSCOPE) != 0) {
337 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
338 		if (n < 0 || n >= bufsiz)
339 			return -1;
340 		else
341 			return n;
342 	}
343 #endif
344 
345 	/* if_indextoname() does not take buffer size.  not a good api... */
346 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
347 	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
348 		char *p = if_indextoname(ifindex, buf);
349 		if (p) {
350 			return(strlen(p));
351 		}
352 	}
353 
354 	/* last resort */
355 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
356 	if (n < 0 || n >= bufsiz)
357 		return -1;
358 	else
359 		return n;
360 }
361 #endif /* INET6 */
362