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