getnameinfo.c revision 1.27 1 1.27 kleink /* $NetBSD: getnameinfo.c,v 1.27 2001/04/26 19:43:53 kleink Exp $ */
2 1.22 itojun /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 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 * - RFC2553 says that we should raise error on short buffer. X/Open says
37 1.5 itojun * we need to truncate the result. We obey RFC2553 (and X/Open should be
38 1.21 itojun * modified). ipngwg rough consensus seems to follow RFC2553.
39 1.16 itojun * - What is "local" in NI_FQDN?
40 1.16 itojun * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
41 1.16 itojun * - (KAME extension) NI_WITHSCOPEID when called with global address,
42 1.16 itojun * and sin6_scope_id filled
43 1.1 itojun */
44 1.15 itojun
45 1.15 itojun #include <sys/cdefs.h>
46 1.15 itojun #if defined(LIBC_SCCS) && !defined(lint)
47 1.27 kleink __RCSID("$NetBSD: getnameinfo.c,v 1.27 2001/04/26 19:43:53 kleink Exp $");
48 1.15 itojun #endif /* LIBC_SCCS and not lint */
49 1.1 itojun
50 1.14 itojun #include "namespace.h"
51 1.1 itojun #include <sys/types.h>
52 1.1 itojun #include <sys/socket.h>
53 1.5 itojun #include <net/if.h>
54 1.1 itojun #include <netinet/in.h>
55 1.1 itojun #include <arpa/inet.h>
56 1.1 itojun #include <arpa/nameser.h>
57 1.24 lukem #include <assert.h>
58 1.1 itojun #include <netdb.h>
59 1.1 itojun #include <resolv.h>
60 1.24 lukem #include <stddef.h>
61 1.1 itojun #include <string.h>
62 1.14 itojun
63 1.14 itojun #ifdef __weak_alias
64 1.14 itojun __weak_alias(getnameinfo,_getnameinfo)
65 1.14 itojun #endif
66 1.1 itojun
67 1.1 itojun #define SUCCESS 0
68 1.1 itojun #define ANY 0
69 1.1 itojun #define YES 1
70 1.1 itojun #define NO 0
71 1.1 itojun
72 1.25 jdolecek static const struct afd {
73 1.27 kleink int a_af;
74 1.27 kleink socklen_t a_addrlen;
75 1.27 kleink socklen_t a_socklen;
76 1.27 kleink int a_off;
77 1.1 itojun } afdl [] = {
78 1.1 itojun #ifdef INET6
79 1.1 itojun {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
80 1.1 itojun offsetof(struct sockaddr_in6, sin6_addr)},
81 1.1 itojun #endif
82 1.1 itojun {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
83 1.1 itojun offsetof(struct sockaddr_in, sin_addr)},
84 1.1 itojun {0, 0, 0},
85 1.1 itojun };
86 1.1 itojun
87 1.1 itojun struct sockinet {
88 1.1 itojun u_char si_len;
89 1.1 itojun u_char si_family;
90 1.1 itojun u_short si_port;
91 1.1 itojun };
92 1.1 itojun
93 1.10 itojun #ifdef INET6
94 1.16 itojun static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
95 1.16 itojun size_t, int));
96 1.16 itojun static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
97 1.18 itojun #endif
98 1.10 itojun
99 1.21 itojun /* 2553bis: use EAI_xx for getnameinfo */
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.25 jdolecek const 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.24 lukem /* sa is checked below */
129 1.24 lukem /* host may be NULL */
130 1.24 lukem /* serv may be NULL */
131 1.24 lukem
132 1.1 itojun if (sa == NULL)
133 1.1 itojun return ENI_NOSOCKET;
134 1.1 itojun
135 1.13 christos #ifdef BSD4_4
136 1.5 itojun if (sa->sa_len != salen)
137 1.5 itojun return ENI_SALEN;
138 1.13 christos #endif
139 1.1 itojun
140 1.1 itojun family = sa->sa_family;
141 1.1 itojun for (i = 0; afdl[i].a_af; i++)
142 1.1 itojun if (afdl[i].a_af == family) {
143 1.1 itojun afd = &afdl[i];
144 1.1 itojun goto found;
145 1.1 itojun }
146 1.1 itojun return ENI_FAMILY;
147 1.1 itojun
148 1.1 itojun found:
149 1.5 itojun if (salen != afd->a_socklen)
150 1.5 itojun return ENI_SALEN;
151 1.1 itojun
152 1.16 itojun /* network byte order */
153 1.20 christos port = ((const struct sockinet *)(const void *)sa)->si_port;
154 1.20 christos addr = (const char *)(const void *)sa + afd->a_off;
155 1.1 itojun
156 1.1 itojun if (serv == NULL || servlen == 0) {
157 1.5 itojun /*
158 1.5 itojun * do nothing in this case.
159 1.5 itojun * in case you are wondering if "&&" is more correct than
160 1.5 itojun * "||" here: RFC2553 says that serv == NULL OR servlen == 0
161 1.5 itojun * means that the caller does not want the result.
162 1.5 itojun */
163 1.1 itojun } else {
164 1.5 itojun if (flags & NI_NUMERICSERV)
165 1.5 itojun sp = NULL;
166 1.5 itojun else {
167 1.5 itojun sp = getservbyport(port,
168 1.5 itojun (flags & NI_DGRAM) ? "udp" : "tcp");
169 1.5 itojun }
170 1.1 itojun if (sp) {
171 1.22 itojun if (strlen(sp->s_name) + 1 > servlen)
172 1.1 itojun return ENI_MEMORY;
173 1.1 itojun strcpy(serv, sp->s_name);
174 1.5 itojun } else {
175 1.5 itojun snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
176 1.22 itojun if (strlen(numserv) + 1 > servlen)
177 1.5 itojun return ENI_MEMORY;
178 1.5 itojun strcpy(serv, numserv);
179 1.5 itojun }
180 1.1 itojun }
181 1.1 itojun
182 1.1 itojun switch (sa->sa_family) {
183 1.1 itojun case AF_INET:
184 1.5 itojun v4a = (u_int32_t)
185 1.20 christos ntohl(((const struct sockaddr_in *)
186 1.20 christos (const void *)sa)->sin_addr.s_addr);
187 1.1 itojun if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
188 1.1 itojun flags |= NI_NUMERICHOST;
189 1.1 itojun v4a >>= IN_CLASSA_NSHIFT;
190 1.7 itojun if (v4a == 0)
191 1.1 itojun flags |= NI_NUMERICHOST;
192 1.1 itojun break;
193 1.1 itojun #ifdef INET6
194 1.1 itojun case AF_INET6:
195 1.3 itojun {
196 1.16 itojun const struct sockaddr_in6 *sin6;
197 1.20 christos sin6 = (const struct sockaddr_in6 *)(const void *)sa;
198 1.5 itojun switch (sin6->sin6_addr.s6_addr[0]) {
199 1.3 itojun case 0x00:
200 1.3 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
201 1.3 itojun ;
202 1.3 itojun else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
203 1.3 itojun ;
204 1.3 itojun else
205 1.3 itojun flags |= NI_NUMERICHOST;
206 1.3 itojun break;
207 1.3 itojun default:
208 1.5 itojun if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
209 1.3 itojun flags |= NI_NUMERICHOST;
210 1.5 itojun }
211 1.3 itojun else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
212 1.3 itojun flags |= NI_NUMERICHOST;
213 1.3 itojun break;
214 1.3 itojun }
215 1.3 itojun }
216 1.1 itojun break;
217 1.1 itojun #endif
218 1.1 itojun }
219 1.1 itojun if (host == NULL || hostlen == 0) {
220 1.5 itojun /*
221 1.5 itojun * do nothing in this case.
222 1.5 itojun * in case you are wondering if "&&" is more correct than
223 1.5 itojun * "||" here: RFC2553 says that host == NULL OR hostlen == 0
224 1.5 itojun * means that the caller does not want the result.
225 1.5 itojun */
226 1.1 itojun } else if (flags & NI_NUMERICHOST) {
227 1.10 itojun int numaddrlen;
228 1.10 itojun
229 1.3 itojun /* NUMERICHOST and NAMEREQD conflicts with each other */
230 1.3 itojun if (flags & NI_NAMEREQD)
231 1.3 itojun return ENI_NOHOSTNAME;
232 1.5 itojun
233 1.16 itojun switch(afd->a_af) {
234 1.16 itojun #ifdef INET6
235 1.16 itojun case AF_INET6:
236 1.16 itojun {
237 1.16 itojun int error;
238 1.16 itojun
239 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
240 1.16 itojun hostlen, flags)) != 0)
241 1.16 itojun return(error);
242 1.16 itojun break;
243 1.16 itojun }
244 1.11 itojun #endif
245 1.16 itojun default:
246 1.16 itojun if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
247 1.16 itojun == NULL)
248 1.16 itojun return ENI_SYSTEM;
249 1.16 itojun numaddrlen = strlen(numaddr);
250 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
251 1.16 itojun return ENI_MEMORY;
252 1.16 itojun strcpy(host, numaddr);
253 1.16 itojun break;
254 1.5 itojun }
255 1.1 itojun } else {
256 1.1 itojun hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
257 1.1 itojun
258 1.1 itojun if (hp) {
259 1.16 itojun #if 0
260 1.16 itojun /*
261 1.16 itojun * commented out, since "for local host" is not
262 1.16 itojun * implemented here - see RFC2553 p30
263 1.16 itojun */
264 1.1 itojun if (flags & NI_NOFQDN) {
265 1.16 itojun char *p;
266 1.1 itojun p = strchr(hp->h_name, '.');
267 1.16 itojun if (p)
268 1.16 itojun *p = '\0';
269 1.1 itojun }
270 1.16 itojun #endif
271 1.22 itojun if (strlen(hp->h_name) + 1 > hostlen) {
272 1.1 itojun return ENI_MEMORY;
273 1.1 itojun }
274 1.1 itojun strcpy(host, hp->h_name);
275 1.1 itojun } else {
276 1.1 itojun if (flags & NI_NAMEREQD)
277 1.1 itojun return ENI_NOHOSTNAME;
278 1.16 itojun switch(afd->a_af) {
279 1.16 itojun #ifdef INET6
280 1.16 itojun case AF_INET6:
281 1.16 itojun {
282 1.16 itojun int error;
283 1.16 itojun
284 1.16 itojun if ((error = ip6_parsenumeric(sa, addr, host,
285 1.16 itojun hostlen,
286 1.16 itojun flags)) != 0)
287 1.16 itojun return(error);
288 1.16 itojun break;
289 1.16 itojun }
290 1.16 itojun #endif
291 1.16 itojun default:
292 1.16 itojun if (inet_ntop(afd->a_af, addr, host,
293 1.16 itojun hostlen) == NULL)
294 1.16 itojun return ENI_SYSTEM;
295 1.16 itojun break;
296 1.16 itojun }
297 1.1 itojun }
298 1.1 itojun }
299 1.1 itojun return SUCCESS;
300 1.1 itojun }
301 1.10 itojun
302 1.10 itojun #ifdef INET6
303 1.16 itojun static int
304 1.16 itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
305 1.16 itojun const struct sockaddr *sa;
306 1.16 itojun const char *addr;
307 1.16 itojun char *host;
308 1.16 itojun size_t hostlen;
309 1.16 itojun int flags;
310 1.16 itojun {
311 1.16 itojun int numaddrlen;
312 1.16 itojun char numaddr[512];
313 1.16 itojun
314 1.24 lukem _DIAGASSERT(sa != NULL);
315 1.24 lukem _DIAGASSERT(addr != NULL);
316 1.24 lukem _DIAGASSERT(host != NULL);
317 1.24 lukem
318 1.16 itojun if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr))
319 1.16 itojun == NULL)
320 1.16 itojun return ENI_SYSTEM;
321 1.16 itojun
322 1.16 itojun numaddrlen = strlen(numaddr);
323 1.16 itojun if (numaddrlen + 1 > hostlen) /* don't forget terminator */
324 1.16 itojun return ENI_MEMORY;
325 1.16 itojun strcpy(host, numaddr);
326 1.16 itojun
327 1.16 itojun #ifdef NI_WITHSCOPEID
328 1.20 christos if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
329 1.16 itojun if (flags & NI_WITHSCOPEID)
330 1.16 itojun {
331 1.16 itojun char scopebuf[MAXHOSTNAMELEN];
332 1.16 itojun int scopelen;
333 1.16 itojun
334 1.16 itojun /* ip6_sa2str never fails */
335 1.20 christos scopelen = ip6_sa2str(
336 1.20 christos (const struct sockaddr_in6 *)(const void *)sa,
337 1.20 christos scopebuf, sizeof(scopebuf), 0);
338 1.16 itojun if (scopelen + 1 + numaddrlen + 1 > hostlen)
339 1.16 itojun return ENI_MEMORY;
340 1.16 itojun /*
341 1.16 itojun * construct <numeric-addr><delim><scopeid>
342 1.16 itojun */
343 1.16 itojun memcpy(host + numaddrlen + 1, scopebuf,
344 1.20 christos (size_t)scopelen);
345 1.16 itojun host[numaddrlen] = SCOPE_DELIMITER;
346 1.16 itojun host[numaddrlen + 1 + scopelen] = '\0';
347 1.16 itojun }
348 1.16 itojun }
349 1.16 itojun #endif /* NI_WITHSCOPEID */
350 1.16 itojun
351 1.16 itojun return 0;
352 1.16 itojun }
353 1.16 itojun
354 1.10 itojun /* ARGSUSED */
355 1.16 itojun static int
356 1.16 itojun ip6_sa2str(sa6, buf, bufsiz, flags)
357 1.16 itojun const struct sockaddr_in6 *sa6;
358 1.10 itojun char *buf;
359 1.16 itojun size_t bufsiz;
360 1.10 itojun int flags;
361 1.10 itojun {
362 1.24 lukem unsigned int ifindex;
363 1.24 lukem const struct in6_addr *a6;
364 1.24 lukem
365 1.24 lukem _DIAGASSERT(sa6 != NULL);
366 1.24 lukem _DIAGASSERT(buf != NULL);
367 1.24 lukem
368 1.24 lukem ifindex = (unsigned int)sa6->sin6_scope_id;
369 1.24 lukem a6 = &sa6->sin6_addr;
370 1.10 itojun
371 1.10 itojun #ifdef notyet
372 1.10 itojun if (flags & NI_NUMERICSCOPE) {
373 1.16 itojun return(snprintf(buf, bufsiz, "%d", sa6->sin6_scope_id));
374 1.10 itojun }
375 1.10 itojun #endif
376 1.18 itojun
377 1.16 itojun /* if_indextoname() does not take buffer size. not a good api... */
378 1.16 itojun if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
379 1.16 itojun bufsiz >= IF_NAMESIZE) {
380 1.16 itojun char *p = if_indextoname(ifindex, buf);
381 1.16 itojun if (p) {
382 1.16 itojun return(strlen(p));
383 1.16 itojun }
384 1.16 itojun }
385 1.10 itojun
386 1.16 itojun /* last resort */
387 1.16 itojun return(snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id));
388 1.10 itojun }
389 1.16 itojun #endif /* INET6 */
390