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