getnameinfo.c revision 1.51 1 /* $NetBSD: getnameinfo.c,v 1.51 2012/03/13 21:13:41 christos 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.51 2012/03/13 21:13:41 christos 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 <netatalk/at.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <arpa/nameser.h>
64 #include <assert.h>
65 #include <limits.h>
66 #include <netdb.h>
67 #include <resolv.h>
68 #include <stddef.h>
69 #include <string.h>
70
71 #include "servent.h"
72
73 #ifdef __weak_alias
74 __weak_alias(getnameinfo,_getnameinfo)
75 #endif
76
77 static const struct afd {
78 int a_af;
79 socklen_t a_addrlen;
80 socklen_t a_socklen;
81 int a_off;
82 } afdl [] = {
83 #ifdef INET6
84 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
85 offsetof(struct sockaddr_in6, sin6_addr)},
86 #endif
87 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
88 offsetof(struct sockaddr_in, sin_addr)},
89 {0, 0, 0, 0},
90 };
91
92 struct sockinet {
93 u_char si_len;
94 u_char si_family;
95 u_short si_port;
96 };
97
98 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
99 socklen_t, char *, socklen_t, int));
100 #ifdef INET6
101 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
102 socklen_t, int));
103 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
104 int));
105 #endif
106 static int getnameinfo_atalk __P((const struct sockaddr *, socklen_t, char *,
107 socklen_t, char *, socklen_t, int));
108
109 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
110 socklen_t, char *, socklen_t, int));
111 static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
112
113 /*
114 * Top-level getnameinfo() code. Look at the address family, and pick an
115 * appropriate function to call.
116 */
117 int
118 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
119 const struct sockaddr *sa;
120 socklen_t salen;
121 char *host, *serv;
122 socklen_t hostlen, servlen;
123 int flags;
124 {
125
126 switch (sa->sa_family) {
127 case AF_APPLETALK:
128 return getnameinfo_atalk(sa, salen, host, hostlen,
129 serv, servlen, flags);
130 case AF_INET:
131 case AF_INET6:
132 return getnameinfo_inet(sa, salen, host, hostlen,
133 serv, servlen, flags);
134 case AF_LINK:
135 return getnameinfo_link(sa, salen, host, hostlen,
136 serv, servlen, flags);
137 default:
138 return EAI_FAMILY;
139 }
140 }
141
142 /*
143 * getnameinfo_atalk():
144 * Format an AppleTalk address into a printable format.
145 */
146 /* ARGSUSED */
147 static int
148 getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
149 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
150 int flags)
151 {
152 char numserv[8];
153 int n, m=0;
154
155 const struct sockaddr_at *sat =
156 (const struct sockaddr_at *)(const void *)sa;
157
158 if (serv != NULL && servlen > 0) {
159 snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
160 if (strlen(numserv) + 1 > servlen)
161 return EAI_MEMORY;
162 strlcpy(serv, numserv, servlen);
163 }
164
165 n = snprintf(host, hostlen, "%u.%u",
166 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
167
168 if (n < 0 || (socklen_t)(m+n) >= hostlen)
169 goto errout;
170
171 m += n;
172
173 if (sat->sat_range.r_netrange.nr_phase) {
174 n = snprintf(host+m, hostlen-m, " phase %u",
175 sat->sat_range.r_netrange.nr_phase);
176
177 if (n < 0 || (socklen_t)(m+n) >= hostlen)
178 goto errout;
179
180 m += n;
181 }
182 if (sat->sat_range.r_netrange.nr_firstnet) {
183 n = snprintf(host+m, hostlen-m, " range %u - %u",
184 ntohs(sat->sat_range.r_netrange.nr_firstnet),
185 ntohs(sat->sat_range.r_netrange.nr_lastnet ));
186
187 if (n < 0 || (socklen_t)(m+n) >= hostlen)
188 goto errout;
189
190 m += n;
191 }
192
193 return 0;
194
195 errout:
196 if (host && hostlen>0)
197 host[m] = '\0'; /* XXX ??? */
198
199 return EAI_MEMORY;
200 }
201
202 /*
203 * getnameinfo_inet():
204 * Format an IPv4 or IPv6 sockaddr into a printable string.
205 */
206 static int
207 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
208 const struct sockaddr *sa;
209 socklen_t salen;
210 char *host;
211 socklen_t hostlen;
212 char *serv;
213 socklen_t servlen;
214 int flags;
215 {
216 const struct afd *afd;
217 struct servent *sp;
218 struct hostent *hp;
219 u_short port;
220 int family, i;
221 const char *addr;
222 u_int32_t v4a;
223 char numserv[512];
224 char numaddr[512];
225
226 /* sa is checked below */
227 /* host may be NULL */
228 /* serv may be NULL */
229
230 if (sa == NULL)
231 return EAI_FAIL;
232
233 family = sa->sa_family;
234 for (i = 0; afdl[i].a_af; i++)
235 if (afdl[i].a_af == family) {
236 afd = &afdl[i];
237 goto found;
238 }
239 return EAI_FAMILY;
240
241 found:
242 if (salen != afd->a_socklen)
243 return EAI_FAIL;
244
245 /* network byte order */
246 port = ((const struct sockinet *)(const void *)sa)->si_port;
247 addr = (const char *)(const void *)sa + afd->a_off;
248
249 if (serv == NULL || servlen == 0) {
250 /*
251 * do nothing in this case.
252 * in case you are wondering if "&&" is more correct than
253 * "||" here: rfc2553bis-03 says that serv == NULL OR
254 * servlen == 0 means that the caller does not want the result.
255 */
256 } else {
257 struct servent_data svd;
258 struct servent sv;
259
260 if (flags & NI_NUMERICSERV)
261 sp = NULL;
262 else {
263 (void)memset(&svd, 0, sizeof(svd));
264 sp = getservbyport_r(port,
265 (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
266 }
267 if (sp) {
268 if (strlen(sp->s_name) + 1 > servlen) {
269 endservent_r(&svd);
270 return EAI_MEMORY;
271 }
272 strlcpy(serv, sp->s_name, servlen);
273 endservent_r(&svd);
274 } else {
275 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
276 if (strlen(numserv) + 1 > servlen)
277 return EAI_MEMORY;
278 strlcpy(serv, numserv, servlen);
279 }
280 }
281
282 switch (sa->sa_family) {
283 case AF_INET:
284 v4a = (u_int32_t)
285 ntohl(((const struct sockaddr_in *)
286 (const void *)sa)->sin_addr.s_addr);
287 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
288 flags |= NI_NUMERICHOST;
289 v4a >>= IN_CLASSA_NSHIFT;
290 if (v4a == 0)
291 flags |= NI_NUMERICHOST;
292 break;
293 #ifdef INET6
294 case AF_INET6:
295 {
296 const struct sockaddr_in6 *sin6;
297 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
298 switch (sin6->sin6_addr.s6_addr[0]) {
299 case 0x00:
300 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
301 ;
302 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
303 ;
304 else
305 flags |= NI_NUMERICHOST;
306 break;
307 default:
308 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
309 flags |= NI_NUMERICHOST;
310 }
311 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
312 flags |= NI_NUMERICHOST;
313 break;
314 }
315 }
316 break;
317 #endif
318 }
319 if (host == NULL || hostlen == 0) {
320 /*
321 * do nothing in this case.
322 * in case you are wondering if "&&" is more correct than
323 * "||" here: rfc2553bis-03 says that host == NULL or
324 * hostlen == 0 means that the caller does not want the result.
325 */
326 } else if (flags & NI_NUMERICHOST) {
327 size_t numaddrlen;
328
329 /* NUMERICHOST and NAMEREQD conflicts with each other */
330 if (flags & NI_NAMEREQD)
331 return EAI_NONAME;
332
333 switch(afd->a_af) {
334 #ifdef INET6
335 case AF_INET6:
336 {
337 int error;
338
339 if ((error = ip6_parsenumeric(sa, addr, host,
340 hostlen, flags)) != 0)
341 return(error);
342 break;
343 }
344 #endif
345 default:
346 if (inet_ntop(afd->a_af, addr, numaddr,
347 (socklen_t)sizeof(numaddr)) == NULL)
348 return EAI_SYSTEM;
349 numaddrlen = strlen(numaddr);
350 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
351 return EAI_MEMORY;
352 strlcpy(host, numaddr, hostlen);
353 break;
354 }
355 } else {
356 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
357
358 if (hp) {
359 #if 0
360 /*
361 * commented out, since "for local host" is not
362 * implemented here - see RFC2553 p30
363 */
364 if (flags & NI_NOFQDN) {
365 char *p;
366 p = strchr(hp->h_name, '.');
367 if (p)
368 *p = '\0';
369 }
370 #endif
371 if (strlen(hp->h_name) + 1 > hostlen) {
372 return EAI_MEMORY;
373 }
374 strlcpy(host, hp->h_name, hostlen);
375 } else {
376 if (flags & NI_NAMEREQD)
377 return EAI_NONAME;
378 switch(afd->a_af) {
379 #ifdef INET6
380 case AF_INET6:
381 {
382 int error;
383
384 if ((error = ip6_parsenumeric(sa, addr, host,
385 hostlen,
386 flags)) != 0)
387 return(error);
388 break;
389 }
390 #endif
391 default:
392 if (inet_ntop(afd->a_af, addr, host,
393 hostlen) == NULL)
394 return EAI_SYSTEM;
395 break;
396 }
397 }
398 }
399 return(0);
400 }
401
402 #ifdef INET6
403 static int
404 ip6_parsenumeric(sa, addr, host, hostlen, flags)
405 const struct sockaddr *sa;
406 const char *addr;
407 char *host;
408 socklen_t hostlen;
409 int flags;
410 {
411 size_t numaddrlen;
412 char numaddr[512];
413
414 _DIAGASSERT(sa != NULL);
415 _DIAGASSERT(addr != NULL);
416 _DIAGASSERT(host != NULL);
417
418 if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
419 == NULL)
420 return EAI_SYSTEM;
421
422 numaddrlen = strlen(numaddr);
423 if (numaddrlen + 1 > hostlen) /* don't forget terminator */
424 return EAI_OVERFLOW;
425 strlcpy(host, numaddr, hostlen);
426
427 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
428 char zonebuf[MAXHOSTNAMELEN];
429 int zonelen;
430
431 zonelen = ip6_sa2str(
432 (const struct sockaddr_in6 *)(const void *)sa,
433 zonebuf, sizeof(zonebuf), flags);
434 if (zonelen < 0)
435 return EAI_OVERFLOW;
436 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
437 return EAI_OVERFLOW;
438 /* construct <numeric-addr><delim><zoneid> */
439 memcpy(host + numaddrlen + 1, zonebuf,
440 (size_t)zonelen);
441 host[numaddrlen] = SCOPE_DELIMITER;
442 host[numaddrlen + 1 + zonelen] = '\0';
443 }
444
445 return 0;
446 }
447
448 /* ARGSUSED */
449 static int
450 ip6_sa2str(sa6, buf, bufsiz, flags)
451 const struct sockaddr_in6 *sa6;
452 char *buf;
453 size_t bufsiz;
454 int flags;
455 {
456 unsigned int ifindex;
457 const struct in6_addr *a6;
458 int n;
459
460 _DIAGASSERT(sa6 != NULL);
461 _DIAGASSERT(buf != NULL);
462
463 ifindex = (unsigned int)sa6->sin6_scope_id;
464 a6 = &sa6->sin6_addr;
465
466 #ifdef NI_NUMERICSCOPE
467 if ((flags & NI_NUMERICSCOPE) != 0) {
468 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
469 if (n < 0 || (size_t)n >= bufsiz)
470 return -1;
471 else
472 return n;
473 }
474 #endif
475
476 /* if_indextoname() does not take buffer size. not a good api... */
477 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
478 bufsiz >= IF_NAMESIZE) {
479 char *p = if_indextoname(ifindex, buf);
480 if (p) {
481 return (int)strlen(p);
482 }
483 }
484
485 /* last resort */
486 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
487 if (n < 0 || (size_t) n >= bufsiz)
488 return -1;
489 else
490 return n;
491 }
492 #endif /* INET6 */
493
494
495 /*
496 * getnameinfo_link():
497 * Format a link-layer address into a printable format, paying attention to
498 * the interface type.
499 */
500 /* ARGSUSED */
501 static int
502 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
503 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
504 int flags)
505 {
506 const struct sockaddr_dl *sdl =
507 (const struct sockaddr_dl *)(const void *)sa;
508 const struct ieee1394_hwaddr *iha;
509 int n;
510
511 if (serv != NULL && servlen > 0)
512 *serv = '\0';
513
514 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
515 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
516 if (n < 0 || (socklen_t) n > hostlen) {
517 *host = '\0';
518 return EAI_MEMORY;
519 }
520 return 0;
521 }
522
523 switch (sdl->sdl_type) {
524 #ifdef IFT_ECONET
525 case IFT_ECONET:
526 if (sdl->sdl_alen < 2)
527 return EAI_FAMILY;
528 if (CLLADDR(sdl)[1] == 0)
529 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
530 else
531 n = snprintf(host, hostlen, "%u.%u",
532 CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
533 if (n < 0 || (socklen_t) n >= hostlen) {
534 *host = '\0';
535 return EAI_MEMORY;
536 } else
537 return 0;
538 #endif
539 case IFT_IEEE1394:
540 if (sdl->sdl_alen < sizeof(iha->iha_uid))
541 return EAI_FAMILY;
542 iha =
543 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
544 return hexname(iha->iha_uid, sizeof(iha->iha_uid),
545 host, hostlen);
546 /*
547 * The following have zero-length addresses.
548 * IFT_ATM (net/if_atmsubr.c)
549 * IFT_FAITH (net/if_faith.c)
550 * IFT_GIF (net/if_gif.c)
551 * IFT_LOOP (net/if_loop.c)
552 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c)
553 * IFT_SLIP (net/if_sl.c, net/if_strip.c)
554 * IFT_STF (net/if_stf.c)
555 * IFT_L2VLAN (net/if_vlan.c)
556 * IFT_PROPVIRTUAL (net/if_bridge.h>
557 */
558 /*
559 * The following use IPv4 addresses as link-layer addresses:
560 * IFT_OTHER (net/if_gre.c)
561 */
562 case IFT_ARCNET: /* default below is believed correct for all these. */
563 case IFT_ETHER:
564 case IFT_FDDI:
565 case IFT_HIPPI:
566 case IFT_ISO88025:
567 default:
568 return hexname((const u_int8_t *)CLLADDR(sdl),
569 (size_t)sdl->sdl_alen, host, hostlen);
570 }
571 }
572
573 static int
574 hexname(cp, len, host, hostlen)
575 const u_int8_t *cp;
576 char *host;
577 size_t len;
578 socklen_t hostlen;
579 {
580 int n;
581 size_t i;
582 char *outp = host;
583
584 *outp = '\0';
585 for (i = 0; i < len; i++) {
586 n = snprintf(outp, hostlen, "%s%02x",
587 i ? ":" : "", cp[i]);
588 if (n < 0 || (socklen_t) n >= hostlen) {
589 *host = '\0';
590 return EAI_MEMORY;
591 }
592 outp += n;
593 hostlen -= n;
594 }
595 return 0;
596 }
597