xref: /freebsd/lib/libc/net/getnameinfo.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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 	size_t a_addrlen;
64 	socklen_t 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(const struct sockaddr *sa, socklen_t salen,
90     char *host, size_t hostlen, char *serv, size_t servlen,
91     int flags)
92 {
93 	const struct afd *afd;
94 	struct servent *sp;
95 	struct hostent *hp;
96 	u_short port;
97 	int family, i;
98 	const char *addr;
99 	u_int32_t v4a;
100 	int h_error;
101 	char numserv[512];
102 	char numaddr[512];
103 
104 	if (sa == NULL)
105 		return EAI_FAIL;
106 
107 	family = sa->sa_family;
108 	for (i = 0; afdl[i].a_af; i++)
109 		if (afdl[i].a_af == family) {
110 			afd = &afdl[i];
111 			goto found;
112 		}
113 	return EAI_FAMILY;
114 
115  found:
116 	if (salen != afd->a_socklen)
117 		return EAI_FAIL;
118 
119 	/* network byte order */
120 	port = ((const struct sockinet *)sa)->si_port;
121 	addr = (const char *)sa + afd->a_off;
122 
123 	if (serv == NULL || servlen == 0) {
124 		/*
125 		 * do nothing in this case.
126 		 * in case you are wondering if "&&" is more correct than
127 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
128 		 * servlen == 0 means that the caller does not want the result.
129 		 */
130 	} else {
131 		if (flags & NI_NUMERICSERV)
132 			sp = NULL;
133 		else {
134 			sp = getservbyport(port,
135 				(flags & NI_DGRAM) ? "udp" : "tcp");
136 		}
137 		if (sp) {
138 			if (strlen(sp->s_name) + 1 > servlen)
139 				return EAI_MEMORY;
140 			strlcpy(serv, sp->s_name, servlen);
141 		} else {
142 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
143 			if (strlen(numserv) + 1 > servlen)
144 				return EAI_MEMORY;
145 			strlcpy(serv, numserv, servlen);
146 		}
147 	}
148 
149 	switch (sa->sa_family) {
150 	case AF_INET:
151 		v4a = (u_int32_t)
152 		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
153 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
154 			flags |= NI_NUMERICHOST;
155 		v4a >>= IN_CLASSA_NSHIFT;
156 		if (v4a == 0)
157 			flags |= NI_NUMERICHOST;
158 		break;
159 #ifdef INET6
160 	case AF_INET6:
161 	    {
162 		const struct sockaddr_in6 *sin6;
163 		sin6 = (const struct sockaddr_in6 *)sa;
164 		switch (sin6->sin6_addr.s6_addr[0]) {
165 		case 0x00:
166 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
167 				;
168 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
169 				;
170 			else
171 				flags |= NI_NUMERICHOST;
172 			break;
173 		default:
174 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
175 				flags |= NI_NUMERICHOST;
176 			}
177 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
178 				flags |= NI_NUMERICHOST;
179 			break;
180 		}
181 	    }
182 		break;
183 #endif
184 	}
185 	if (host == NULL || hostlen == 0) {
186 		/*
187 		 * do nothing in this case.
188 		 * in case you are wondering if "&&" is more correct than
189 		 * "||" here: rfc2553bis-03 says that host == NULL or
190 		 * hostlen == 0 means that the caller does not want the result.
191 		 */
192 	} else if (flags & NI_NUMERICHOST) {
193 		size_t numaddrlen;
194 
195 		/* NUMERICHOST and NAMEREQD conflicts with each other */
196 		if (flags & NI_NAMEREQD)
197 			return EAI_NONAME;
198 
199 		switch(afd->a_af) {
200 #ifdef INET6
201 		case AF_INET6:
202 		{
203 			int error;
204 
205 			if ((error = ip6_parsenumeric(sa, addr, host,
206 						      hostlen, flags)) != 0)
207 				return(error);
208 			break;
209 		}
210 #endif
211 		default:
212 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
213 			    == NULL)
214 				return EAI_SYSTEM;
215 			numaddrlen = strlen(numaddr);
216 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
217 				return EAI_MEMORY;
218 			strlcpy(host, numaddr, hostlen);
219 			break;
220 		}
221 	} else {
222 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
223 
224 		if (hp) {
225 #if 0
226 			/*
227 			 * commented out, since "for local host" is not
228 			 * implemented here - see RFC2553 p30
229 			 */
230 			if (flags & NI_NOFQDN) {
231 				char *p;
232 				p = strchr(hp->h_name, '.');
233 				if (p)
234 					*p = '\0';
235 			}
236 #endif
237 			if (strlen(hp->h_name) + 1 > hostlen) {
238 				freehostent(hp);
239 				return EAI_MEMORY;
240 			}
241 			strlcpy(host, hp->h_name, hostlen);
242 			freehostent(hp);
243 		} else {
244 			if (flags & NI_NAMEREQD)
245 				return EAI_NONAME;
246 			switch(afd->a_af) {
247 #ifdef INET6
248 			case AF_INET6:
249 			{
250 				int error;
251 
252 				if ((error = ip6_parsenumeric(sa, addr, host,
253 							      hostlen,
254 							      flags)) != 0)
255 					return(error);
256 				break;
257 			}
258 #endif
259 			default:
260 				if (inet_ntop(afd->a_af, addr, host,
261 				    hostlen) == NULL)
262 					return EAI_SYSTEM;
263 				break;
264 			}
265 		}
266 	}
267 	return(0);
268 }
269 
270 #ifdef INET6
271 static int
272 ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
273     char *host, size_t hostlen, int flags)
274 {
275 	size_t numaddrlen;
276 	char numaddr[512];
277 
278 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
279 		return EAI_SYSTEM;
280 
281 	numaddrlen = strlen(numaddr);
282 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
283 		return EAI_MEMORY;
284 	strlcpy(host, numaddr, hostlen);
285 
286 	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
287 		char zonebuf[MAXHOSTNAMELEN];
288 		int zonelen;
289 
290 		zonelen = ip6_sa2str(
291 		    (const struct sockaddr_in6 *)(const void *)sa,
292 		    zonebuf, sizeof(zonebuf), flags);
293 		if (zonelen < 0)
294 			return EAI_MEMORY;
295 		if (zonelen + 1 + numaddrlen + 1 > hostlen)
296 			return EAI_MEMORY;
297 
298 		/* construct <numeric-addr><delim><zoneid> */
299 		memcpy(host + numaddrlen + 1, zonebuf,
300 		    (size_t)zonelen);
301 		host[numaddrlen] = SCOPE_DELIMITER;
302 		host[numaddrlen + 1 + zonelen] = '\0';
303 	}
304 
305 	return 0;
306 }
307 
308 /* ARGSUSED */
309 static int
310 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
311 {
312 	unsigned int ifindex;
313 	const struct in6_addr *a6;
314 	int n;
315 
316 	ifindex = (unsigned int)sa6->sin6_scope_id;
317 	a6 = &sa6->sin6_addr;
318 
319 #ifdef NI_NUMERICSCOPE
320 	if ((flags & NI_NUMERICSCOPE) != 0) {
321 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
322 		if (n < 0 || n >= bufsiz)
323 			return -1;
324 		else
325 			return n;
326 	}
327 #endif
328 
329 	/* if_indextoname() does not take buffer size.  not a good api... */
330 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
331 	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
332 		char *p = if_indextoname(ifindex, buf);
333 		if (p) {
334 			return(strlen(p));
335 		}
336 	}
337 
338 	/* last resort */
339 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
340 	if (n < 0 || (size_t)n >= bufsiz)
341 		return -1;
342 	else
343 		return n;
344 }
345 #endif /* INET6 */
346