getnameinfo.c revision 1.20 1 1.20 christos /* $NetBSD: getnameinfo.c,v 1.20 2000/07/06 02:54:12 christos Exp $ */
2 1.19 itojun /* $KAME: getnameinfo.c,v 1.43 2000/06/12 04:27:03 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.20 christos __RCSID("$NetBSD: getnameinfo.c,v 1.20 2000/07/06 02:54:12 christos 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.19 itojun #define ENI_SALEN EAI_FAMILY
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 char numserv[512];
126 1.1 itojun char numaddr[512];
127 1.1 itojun
128 1.1 itojun if (sa == NULL)
129 1.1 itojun return ENI_NOSOCKET;
130 1.1 itojun
131 1.13 christos #ifdef BSD4_4
132 1.5 itojun if (sa->sa_len != salen)
133 1.5 itojun return ENI_SALEN;
134 1.13 christos #endif
135 1.1 itojun
136 1.1 itojun family = sa->sa_family;
137 1.1 itojun for (i = 0; afdl[i].a_af; i++)
138 1.1 itojun if (afdl[i].a_af == family) {
139 1.1 itojun afd = &afdl[i];
140 1.1 itojun goto found;
141 1.1 itojun }
142 1.1 itojun return ENI_FAMILY;
143 1.1 itojun
144 1.1 itojun found:
145 1.5 itojun if (salen != afd->a_socklen)
146 1.5 itojun return ENI_SALEN;
147 1.1 itojun
148 1.16 itojun /* network byte order */
149 1.20 christos port = ((const struct sockinet *)(const void *)sa)->si_port;
150 1.20 christos addr = (const char *)(const void *)sa + afd->a_off;
151 1.1 itojun
152 1.1 itojun if (serv == NULL || servlen == 0) {
153 1.5 itojun /*
154 1.5 itojun * do nothing in this case.
155 1.5 itojun * in case you are wondering if "&&" is more correct than
156 1.5 itojun * "||" here: RFC2553 says that serv == NULL OR servlen == 0
157 1.5 itojun * means that the caller does not want the result.
158 1.5 itojun */
159 1.1 itojun } else {
160 1.5 itojun if (flags & NI_NUMERICSERV)
161 1.5 itojun sp = NULL;
162 1.5 itojun else {
163 1.5 itojun sp = getservbyport(port,
164 1.5 itojun (flags & NI_DGRAM) ? "udp" : "tcp");
165 1.5 itojun }
166 1.1 itojun if (sp) {
167 1.1 itojun if (strlen(sp->s_name) > servlen)
168 1.1 itojun return ENI_MEMORY;
169 1.1 itojun strcpy(serv, sp->s_name);
170 1.5 itojun } else {
171 1.5 itojun snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
172 1.5 itojun if (strlen(numserv) > servlen)
173 1.5 itojun return ENI_MEMORY;
174 1.5 itojun strcpy(serv, numserv);
175 1.5 itojun }
176 1.1 itojun }
177 1.1 itojun
178 1.1 itojun switch (sa->sa_family) {
179 1.1 itojun case AF_INET:
180 1.5 itojun v4a = (u_int32_t)
181 1.20 christos ntohl(((const struct sockaddr_in *)
182 1.20 christos (const void *)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.20 christos sin6 = (const struct sockaddr_in6 *)(const void *)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.1 itojun
254 1.1 itojun if (hp) {
255 1.16 itojun #if 0
256 1.16 itojun /*
257 1.16 itojun * commented out, since "for local host" is not
258 1.16 itojun * implemented here - see RFC2553 p30
259 1.16 itojun */
260 1.1 itojun if (flags & NI_NOFQDN) {
261 1.16 itojun char *p;
262 1.1 itojun p = strchr(hp->h_name, '.');
263 1.16 itojun if (p)
264 1.16 itojun *p = '\0';
265 1.1 itojun }
266 1.16 itojun #endif
267 1.1 itojun if (strlen(hp->h_name) > hostlen) {
268 1.1 itojun return ENI_MEMORY;
269 1.1 itojun }
270 1.1 itojun strcpy(host, hp->h_name);
271 1.1 itojun } else {
272 1.1 itojun if (flags & NI_NAMEREQD)
273 1.1 itojun return ENI_NOHOSTNAME;
274 1.16 itojun switch(afd->a_af) {
275 1.16 itojun #ifdef INET6
276 1.16 itojun case AF_INET6:
277 1.16 itojun {
278 1.16 itojun int error;
279 1.16 itojun
280 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
281 1.16 itojun hostlen,
282 1.16 itojun flags)) != 0)
283 1.16 itojun return(error);
284 1.16 itojun break;
285 1.16 itojun }
286 1.16 itojun #endif
287 1.16 itojun default:
288 1.16 itojun if (inet_ntop(afd->a_af, addr, host,
289 1.16 itojun hostlen) == NULL)
290 1.16 itojun return ENI_SYSTEM;
291 1.16 itojun break;
292 1.16 itojun }
293 1.1 itojun }
294 1.1 itojun }
295 1.1 itojun return SUCCESS;
296 1.1 itojun }
297 1.10 itojun
298 1.10 itojun #ifdef INET6
299 1.16 itojun static int
300 1.16 itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
301 1.16 itojun const struct sockaddr *sa;
302 1.16 itojun const char *addr;
303 1.16 itojun char *host;
304 1.16 itojun size_t hostlen;
305 1.16 itojun int flags;
306 1.16 itojun {
307 1.16 itojun int numaddrlen;
308 1.16 itojun char numaddr[512];
309 1.16 itojun
310 1.16 itojun if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
311 1.16 itojun == NULL)
312 1.16 itojun return ENI_SYSTEM;
313 1.16 itojun
314 1.16 itojun numaddrlen = strlen(numaddr);
315 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
316 1.16 itojun return ENI_MEMORY;
317 1.16 itojun strcpy(host, numaddr);
318 1.16 itojun
319 1.16 itojun #ifdef NI_WITHSCOPEID
320 1.20 christos if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
321 1.16 itojun if (flags & NI_WITHSCOPEID)
322 1.16 itojun {
323 1.16 itojun char scopebuf[MAXHOSTNAMELEN];
324 1.16 itojun int scopelen;
325 1.16 itojun
326 1.16 itojun /* ip6_sa2str never fails */
327 1.20 christos scopelen = ip6_sa2str(
328 1.20 christos (const struct sockaddr_in6 *)(const void *)sa,
329 1.20 christos scopebuf, sizeof(scopebuf), 0);
330 1.16 itojun if (scopelen + 1 + numaddrlen + 1 > hostlen)
331 1.16 itojun return ENI_MEMORY;
332 1.16 itojun /*
333 1.16 itojun * construct <numeric-addr><delim><scopeid>
334 1.16 itojun */
335 1.16 itojun memcpy(host + numaddrlen + 1, scopebuf,
336 1.20 christos (size_t)scopelen);
337 1.16 itojun host[numaddrlen] = SCOPE_DELIMITER;
338 1.16 itojun host[numaddrlen + 1 + scopelen] = '\0';
339 1.16 itojun }
340 1.16 itojun }
341 1.16 itojun #endif /* NI_WITHSCOPEID */
342 1.16 itojun
343 1.16 itojun return 0;
344 1.16 itojun }
345 1.16 itojun
346 1.10 itojun /* ARGSUSED */
347 1.16 itojun static int
348 1.16 itojun ip6_sa2str(sa6, buf, bufsiz, flags)
349 1.16 itojun const struct sockaddr_in6 *sa6;
350 1.10 itojun char *buf;
351 1.16 itojun size_t bufsiz;
352 1.10 itojun int flags;
353 1.10 itojun {
354 1.10 itojun unsigned int ifindex = (unsigned int)sa6->sin6_scope_id;
355 1.16 itojun const struct in6_addr *a6 = &sa6->sin6_addr;
356 1.10 itojun
357 1.10 itojun #ifdef notyet
358 1.10 itojun if (flags & NI_NUMERICSCOPE) {
359 1.16 itojun return(snprintf(buf, bufsiz, "%d", sa6->sin6_scope_id));
360 1.10 itojun }
361 1.10 itojun #endif
362 1.18 itojun
363 1.16 itojun /* if_indextoname() does not take buffer size. not a good api... */
364 1.16 itojun if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
365 1.16 itojun bufsiz >= IF_NAMESIZE) {
366 1.16 itojun char *p = if_indextoname(ifindex, buf);
367 1.16 itojun if (p) {
368 1.16 itojun return(strlen(p));
369 1.16 itojun }
370 1.16 itojun }
371 1.10 itojun
372 1.16 itojun /* last resort */
373 1.16 itojun return(snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id));
374 1.10 itojun }
375 1.16 itojun #endif /* INET6 */
376