getnameinfo.c revision 1.34 1 /* $NetBSD: getnameinfo.c,v 1.34 2001/11/15 04:49:01 itojun Exp $ */
2 /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */
3
4 /*
5 * Copyright (c) 2000 Ben Harris.
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * Issues to be discussed:
36 * - Thread safe-ness must be checked
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). ipngwg rough consensus seems to follow RFC2553.
40 * - What is "local" in NI_FQDN?
41 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
42 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
43 * sin6_scope_id is filled - standardization status?
44 * XXX breaks backward compat for code that expects no scopeid.
45 * beware on merge.
46 */
47
48 #include <sys/cdefs.h>
49 #if defined(LIBC_SCCS) && !defined(lint)
50 __RCSID("$NetBSD: getnameinfo.c,v 1.34 2001/11/15 04:49:01 itojun Exp $");
51 #endif /* LIBC_SCCS and not lint */
52
53 #include "namespace.h"
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_ieee1394.h>
59 #include <net/if_types.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <arpa/nameser.h>
63 #include <assert.h>
64 #include <limits.h>
65 #include <netdb.h>
66 #include <resolv.h>
67 #include <stddef.h>
68 #include <string.h>
69
70 #ifdef __weak_alias
71 __weak_alias(getnameinfo,_getnameinfo)
72 #endif
73
74 static const struct afd {
75 int a_af;
76 socklen_t a_addrlen;
77 socklen_t a_socklen;
78 int a_off;
79 } afdl [] = {
80 #ifdef INET6
81 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
82 offsetof(struct sockaddr_in6, sin6_addr)},
83 #endif
84 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
85 offsetof(struct sockaddr_in, sin_addr)},
86 {0, 0, 0},
87 };
88
89 struct sockinet {
90 u_char si_len;
91 u_char si_family;
92 u_short si_port;
93 };
94
95 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
96 size_t, char *, size_t, int));
97 #ifdef INET6
98 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
99 size_t, int));
100 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
101 #endif
102 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
103 size_t, char *, size_t, int));
104 static int hexname __P((const u_int8_t *, size_t, char *, size_t));
105
106 /*
107 * Top-level getnameinfo() code. Look at the address family, and pick an
108 * appropriate function to call.
109 */
110 int
111 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
112 const struct sockaddr *sa;
113 socklen_t salen;
114 char *host, *serv;
115 size_t hostlen, servlen;
116 int flags;
117 {
118
119 switch (sa->sa_family) {
120 case AF_INET:
121 case AF_INET6:
122 return getnameinfo_inet(sa, salen, host, hostlen,
123 serv, servlen, flags);
124 case AF_LINK:
125 return getnameinfo_link(sa, salen, host, hostlen,
126 serv, servlen, flags);
127 default:
128 return EAI_FAMILY;
129 }
130 }
131
132
133 /*
134 * getnameinfo_inet():
135 * Format an IPv4 or IPv6 sockaddr into a printable string.
136 */
137 static int
138 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
139 const struct sockaddr *sa;
140 socklen_t salen;
141 char *host;
142 size_t hostlen;
143 char *serv;
144 size_t servlen;
145 int flags;
146 {
147 const struct afd *afd;
148 struct servent *sp;
149 struct hostent *hp;
150 u_short port;
151 int family, i;
152 const char *addr;
153 u_int32_t v4a;
154 char numserv[512];
155 char numaddr[512];
156
157 /* sa is checked below */
158 /* host may be NULL */
159 /* serv may be NULL */
160
161 if (sa == NULL)
162 return EAI_FAIL;
163
164 #ifdef BSD4_4
165 if (sa->sa_len != salen)
166 return EAI_FAIL;
167 #endif
168
169 family = sa->sa_family;
170 for (i = 0; afdl[i].a_af; i++)
171 if (afdl[i].a_af == family) {
172 afd = &afdl[i];
173 goto found;
174 }
175 return EAI_FAMILY;
176
177 found:
178 if (salen != afd->a_socklen)
179 return EAI_FAIL;
180
181 /* network byte order */
182 port = ((const struct sockinet *)(const void *)sa)->si_port;
183 addr = (const char *)(const void *)sa + afd->a_off;
184
185 if (serv == NULL || servlen == 0) {
186 /*
187 * do nothing in this case.
188 * in case you are wondering if "&&" is more correct than
189 * "||" here: rfc2553bis-03 says that serv == NULL OR
190 * servlen == 0 means that the caller does not want the result.
191 */
192 } else {
193 if (flags & NI_NUMERICSERV)
194 sp = NULL;
195 else {
196 sp = getservbyport(port,
197 (flags & NI_DGRAM) ? "udp" : "tcp");
198 }
199 if (sp) {
200 if (strlen(sp->s_name) + 1 > servlen)
201 return EAI_MEMORY;
202 strcpy(serv, sp->s_name);
203 } else {
204 snprintf(numserv, sizeof(numserv), "%d", ntohs(port));
205 if (strlen(numserv) + 1 > servlen)
206 return EAI_MEMORY;
207 strcpy(serv, numserv);
208 }
209 }
210
211 switch (sa->sa_family) {
212 case AF_INET:
213 v4a = (u_int32_t)
214 ntohl(((const struct sockaddr_in *)
215 (const void *)sa)->sin_addr.s_addr);
216 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
217 flags |= NI_NUMERICHOST;
218 v4a >>= IN_CLASSA_NSHIFT;
219 if (v4a == 0)
220 flags |= NI_NUMERICHOST;
221 break;
222 #ifdef INET6
223 case AF_INET6:
224 {
225 const struct sockaddr_in6 *sin6;
226 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
227 switch (sin6->sin6_addr.s6_addr[0]) {
228 case 0x00:
229 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
230 ;
231 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
232 ;
233 else
234 flags |= NI_NUMERICHOST;
235 break;
236 default:
237 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
238 flags |= NI_NUMERICHOST;
239 }
240 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
241 flags |= NI_NUMERICHOST;
242 break;
243 }
244 }
245 break;
246 #endif
247 }
248 if (host == NULL || hostlen == 0) {
249 /*
250 * do nothing in this case.
251 * in case you are wondering if "&&" is more correct than
252 * "||" here: rfc2553bis-03 says that host == NULL or
253 * hostlen == 0 means that the caller does not want the result.
254 */
255 } else if (flags & NI_NUMERICHOST) {
256 int numaddrlen;
257
258 /* NUMERICHOST and NAMEREQD conflicts with each other */
259 if (flags & NI_NAMEREQD)
260 return EAI_NONAME;
261
262 switch(afd->a_af) {
263 #ifdef INET6
264 case AF_INET6:
265 {
266 int error;
267
268 if ((error = ip6_parsenumeric(sa, addr, host,
269 hostlen, flags)) != 0)
270 return(error);
271 break;
272 }
273 #endif
274 default:
275 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
276 == NULL)
277 return EAI_SYSTEM;
278 numaddrlen = strlen(numaddr);
279 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
280 return EAI_MEMORY;
281 strcpy(host, numaddr);
282 break;
283 }
284 } else {
285 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
286
287 if (hp) {
288 #if 0
289 /*
290 * commented out, since "for local host" is not
291 * implemented here - see RFC2553 p30
292 */
293 if (flags & NI_NOFQDN) {
294 char *p;
295 p = strchr(hp->h_name, '.');
296 if (p)
297 *p = '\0';
298 }
299 #endif
300 if (strlen(hp->h_name) + 1 > hostlen) {
301 return EAI_MEMORY;
302 }
303 strcpy(host, hp->h_name);
304 } else {
305 if (flags & NI_NAMEREQD)
306 return EAI_NONAME;
307 switch(afd->a_af) {
308 #ifdef INET6
309 case AF_INET6:
310 {
311 int error;
312
313 if ((error = ip6_parsenumeric(sa, addr, host,
314 hostlen,
315 flags)) != 0)
316 return(error);
317 break;
318 }
319 #endif
320 default:
321 if (inet_ntop(afd->a_af, addr, host,
322 hostlen) == NULL)
323 return EAI_SYSTEM;
324 break;
325 }
326 }
327 }
328 return(0);
329 }
330
331 #ifdef INET6
332 static int
333 ip6_parsenumeric(sa, addr, host, hostlen, flags)
334 const struct sockaddr *sa;
335 const char *addr;
336 char *host;
337 size_t hostlen;
338 int flags;
339 {
340 int numaddrlen;
341 char numaddr[512];
342
343 _DIAGASSERT(sa != NULL);
344 _DIAGASSERT(addr != NULL);
345 _DIAGASSERT(host != NULL);
346
347 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
348 return EAI_SYSTEM;
349
350 numaddrlen = strlen(numaddr);
351 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
352 return EAI_MEMORY;
353 strcpy(host, numaddr);
354
355 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
356 char zonebuf[MAXHOSTNAMELEN];
357 int zonelen;
358
359 zonelen = ip6_sa2str(
360 (const struct sockaddr_in6 *)(const void *)sa,
361 zonebuf, sizeof(zonebuf), flags);
362 if (zonelen < 0)
363 return EAI_MEMORY;
364 if (zonelen + 1 + numaddrlen + 1 > hostlen)
365 return EAI_MEMORY;
366 /* construct <numeric-addr><delim><zoneid> */
367 memcpy(host + numaddrlen + 1, zonebuf,
368 (size_t)zonelen);
369 host[numaddrlen] = SCOPE_DELIMITER;
370 host[numaddrlen + 1 + zonelen] = '\0';
371 }
372
373 return 0;
374 }
375
376 /* ARGSUSED */
377 static int
378 ip6_sa2str(sa6, buf, bufsiz, flags)
379 const struct sockaddr_in6 *sa6;
380 char *buf;
381 size_t bufsiz;
382 int flags;
383 {
384 unsigned int ifindex;
385 const struct in6_addr *a6;
386 int n;
387
388 _DIAGASSERT(sa6 != NULL);
389 _DIAGASSERT(buf != NULL);
390
391 ifindex = (unsigned int)sa6->sin6_scope_id;
392 a6 = &sa6->sin6_addr;
393
394 #ifdef NI_NUMERICSCOPE
395 if ((flags & NI_NUMERICSCOPE) != 0) {
396 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
397 if (n < 0 || n >= bufsiz)
398 return -1;
399 else
400 return n;
401 }
402 #endif
403
404 /* if_indextoname() does not take buffer size. not a good api... */
405 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
406 bufsiz >= IF_NAMESIZE) {
407 char *p = if_indextoname(ifindex, buf);
408 if (p) {
409 return(strlen(p));
410 }
411 }
412
413 /* last resort */
414 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
415 if (n < 0 || n >= bufsiz)
416 return -1;
417 else
418 return n;
419 }
420 #endif /* INET6 */
421
422
423 /*
424 * getnameinfo_link():
425 * Format a link-layer address into a printable format, paying attention to
426 * the interface type.
427 */
428 /* ARGSUSED */
429 static int
430 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
431 char *host, size_t hostlen, char *serv, size_t servlen, int flags)
432 {
433 const struct sockaddr_dl *sdl =
434 (const struct sockaddr_dl *)(const void *)sa;
435 const struct ieee1394_hwaddr *iha;
436 int n;
437
438 if (serv != NULL && servlen > 0)
439 *serv = '\0';
440
441 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
442 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
443 if (n > hostlen) {
444 *host = '\0';
445 return EAI_MEMORY;
446 }
447 return 0;
448 }
449
450 switch (sdl->sdl_type) {
451 #ifdef IFT_ECONET
452 case IFT_ECONET:
453 if (sdl->sdl_alen < 2)
454 return EAI_FAMILY;
455 if (LLADDR(sdl)[1] == 0)
456 n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
457 else
458 n = snprintf(host, hostlen, "%d.%d",
459 LLADDR(sdl)[1], LLADDR(sdl)[0]);
460 if (n >= hostlen) {
461 *host = '\0';
462 return EAI_MEMORY;
463 } else
464 return 0;
465 #endif
466 case IFT_IEEE1394:
467 if (sdl->sdl_alen < sizeof(iha->iha_uid))
468 return EAI_FAMILY;
469 iha =
470 (const struct ieee1394_hwaddr *)(const void *)LLADDR(sdl);
471 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
472 host, hostlen);
473 /*
474 * The following have zero-length addresses.
475 * IFT_ATM (net/if_atmsubr.c)
476 * IFT_FAITH (net/if_faith.c)
477 * IFT_GIF (net/if_gif.c)
478 * IFT_LOOP (net/if_loop.c)
479 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
480 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
481 * IFT_STF (net/if_stf.c)
482 * IFT_L2VLAN (net/if_vlan.c)
483 * IFT_PROPVIRTUAL (net/if_bridge.h>
484 */
485 /*
486 * The following use IPv4 addresses as link-layer addresses:
487 * IFT_OTHER (net/if_gre.c)
488 * IFT_OTHER (netinet/ip_ipip.c)
489 */
490 case IFT_ARCNET: /* default below is believed correct for all these. */
491 case IFT_ETHER:
492 case IFT_FDDI:
493 case IFT_HIPPI:
494 case IFT_ISO88025:
495 default:
496 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
497 host, hostlen);
498 }
499 }
500
501 static int
502 hexname(cp, len, host, hostlen)
503 const u_int8_t *cp;
504 char *host;
505 size_t len, hostlen;
506 {
507 int i, n;
508 char *outp = host;
509
510 *outp = '\0';
511 for (i = 0; i < len; i++) {
512 n = snprintf(outp, hostlen, "%s%02x",
513 i ? ":" : "", cp[i]);
514 if (n < 0 || n >= hostlen) {
515 *host = '\0';
516 return EAI_MEMORY;
517 }
518 outp += n;
519 hostlen -= n;
520 }
521 return 0;
522 }
523