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