xref: /netbsd-src/lib/libc/net/getnameinfo.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: getnameinfo.c,v 1.7 2000/01/05 04:54:54 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  * - Return values.  There seems to be no standard for return value (RFC2553)
36  *   but INRIA implementation returns EAI_xxx defined for getaddrinfo().
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).
40  */
41 
42 #if 0
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #else
47 #define HAVE_SA_LEN
48 #endif
49 
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <net/if.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 
61 #if 0
62 #ifndef HAVE_PORTABLE_PROTOTYPE
63 #include "cdecl_ext.h"
64 #endif
65 
66 #ifndef HAVE_ADDRINFO
67 #include "addrinfo.h"
68 #endif
69 #endif
70 
71 #define SUCCESS 0
72 #define ANY 0
73 #define YES 1
74 #define NO  0
75 
76 static struct afd {
77 	int a_af;
78 	int a_addrlen;
79 	int a_socklen;
80 	int a_off;
81 } afdl [] = {
82 #ifdef INET6
83 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
84 		offsetof(struct sockaddr_in6, sin6_addr)},
85 #endif
86 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
87 		offsetof(struct sockaddr_in, sin_addr)},
88 	{0, 0, 0},
89 };
90 
91 struct sockinet {
92 	u_char	si_len;
93 	u_char	si_family;
94 	u_short	si_port;
95 };
96 
97 #define ENI_NOSOCKET 	0
98 #define ENI_NOSERVNAME	1
99 #define ENI_NOHOSTNAME	2
100 #define ENI_MEMORY	3
101 #define ENI_SYSTEM	4
102 #define ENI_FAMILY	5
103 #define ENI_SALEN	6
104 
105 int
106 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
107 	const struct sockaddr *sa;
108 	size_t salen;
109 	char *host;
110 	size_t hostlen;
111 	char *serv;
112 	size_t servlen;
113 	int flags;
114 {
115 	struct afd *afd;
116 	struct servent *sp;
117 	struct hostent *hp;
118 	u_short port;
119 	int family, i;
120 	char *addr, *p;
121 	u_int32_t v4a;
122 	int h_error;
123 	char numserv[512];
124 	char numaddr[512];
125 
126 	if (sa == NULL)
127 		return ENI_NOSOCKET;
128 
129 #ifdef HAVE_SA_LEN
130 	if (sa->sa_len != salen)
131 		return ENI_SALEN;
132 #endif
133 
134 	family = sa->sa_family;
135 	for (i = 0; afdl[i].a_af; i++)
136 		if (afdl[i].a_af == family) {
137 			afd = &afdl[i];
138 			goto found;
139 		}
140 	return ENI_FAMILY;
141 
142  found:
143 	if (salen != afd->a_socklen)
144 		return ENI_SALEN;
145 
146 	port = ((struct sockinet *)sa)->si_port; /* network byte order */
147 	addr = (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: RFC2553 says that serv == NULL OR servlen == 0
154 		 * 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) > servlen)
165 				return ENI_MEMORY;
166 			strcpy(serv, sp->s_name);
167 		} else {
168 			snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
169 			if (strlen(numserv) > servlen)
170 				return ENI_MEMORY;
171 			strcpy(serv, numserv);
172 		}
173 	}
174 
175 	switch (sa->sa_family) {
176 	case AF_INET:
177 		v4a = (u_int32_t)
178 			ntohl(((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 0
183 		if (v4a == 0 || v4a == IN_LOOPBACKNET)
184 #else
185 		if (v4a == 0)
186 #endif
187 			flags |= NI_NUMERICHOST;
188 		break;
189 #ifdef INET6
190 	case AF_INET6:
191 	    {
192 		struct sockaddr_in6 *sin6;
193 		sin6 = (struct sockaddr_in6 *)sa;
194 		switch (sin6->sin6_addr.s6_addr[0]) {
195 		case 0x00:
196 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
197 				;
198 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
199 				;
200 			else
201 				flags |= NI_NUMERICHOST;
202 			break;
203 		default:
204 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
205 				flags |= NI_NUMERICHOST;
206 			}
207 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
208 				flags |= NI_NUMERICHOST;
209 			break;
210 		}
211 	    }
212 		break;
213 #endif
214 	}
215 	if (host == NULL || hostlen == 0) {
216 		/*
217 		 * do nothing in this case.
218 		 * in case you are wondering if "&&" is more correct than
219 		 * "||" here: RFC2553 says that host == NULL OR hostlen == 0
220 		 * means that the caller does not want the result.
221 		 */
222 	} else if (flags & NI_NUMERICHOST) {
223 		/* NUMERICHOST and NAMEREQD conflicts with each other */
224 		if (flags & NI_NAMEREQD)
225 			return ENI_NOHOSTNAME;
226 		if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
227 		    == NULL)
228 			return ENI_SYSTEM;
229 		if (strlen(numaddr) > hostlen)
230 			return ENI_MEMORY;
231 		strcpy(host, numaddr);
232 #if defined(INET6) && defined(NI_WITHSCOPEID)
233 		if (afd->a_af == AF_INET6 &&
234 		    (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr) ||
235 		     IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) &&
236 		    ((struct sockaddr_in6 *)sa)->sin6_scope_id) {
237 #ifndef ALWAYS_WITHSCOPE
238 			if (flags & NI_WITHSCOPEID)
239 #endif /* !ALWAYS_WITHSCOPE */
240 			{
241 				char *ep = strchr(host, '\0');
242 				unsigned int ifindex =
243 					((struct sockaddr_in6 *)sa)->sin6_scope_id;
244 
245 				*ep = SCOPE_DELIMITER;
246 				if ((if_indextoname(ifindex, ep + 1)) == NULL)
247 					/* XXX what should we do? */
248 					strncpy(ep + 1, "???", 3);
249 			}
250 		}
251 #endif /* INET6 */
252 	} else {
253 #ifdef USE_GETIPNODEBY
254 		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
255 #else
256 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
257 		h_error = h_errno;
258 #endif
259 
260 		if (hp) {
261 			if (flags & NI_NOFQDN) {
262 				p = strchr(hp->h_name, '.');
263 				if (p) *p = '\0';
264 			}
265 			if (strlen(hp->h_name) > hostlen) {
266 #ifdef USE_GETIPNODEBY
267 				freehostent(hp);
268 #endif
269 				return ENI_MEMORY;
270 			}
271 			strcpy(host, hp->h_name);
272 #ifdef USE_GETIPNODEBY
273 			freehostent(hp);
274 #endif
275 		} else {
276 			if (flags & NI_NAMEREQD)
277 				return ENI_NOHOSTNAME;
278 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
279 			    == NULL)
280 				return ENI_NOHOSTNAME;
281 			if (strlen(numaddr) > hostlen)
282 				return ENI_MEMORY;
283 			strcpy(host, numaddr);
284 		}
285 	}
286 	return SUCCESS;
287 }
288