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