1 /* $NetBSD: getnameinfo.c,v 1.50 2010/06/29 14:44:19 seanb 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.50 2010/06/29 14:44:19 seanb 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, sizeof(numaddr)) 347 == 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, sizeof(numaddr)) == NULL) 419 return EAI_SYSTEM; 420 421 numaddrlen = strlen(numaddr); 422 if (numaddrlen + 1 > hostlen) /* don't forget terminator */ 423 return EAI_OVERFLOW; 424 strlcpy(host, numaddr, hostlen); 425 426 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { 427 char zonebuf[MAXHOSTNAMELEN]; 428 int zonelen; 429 430 zonelen = ip6_sa2str( 431 (const struct sockaddr_in6 *)(const void *)sa, 432 zonebuf, sizeof(zonebuf), flags); 433 if (zonelen < 0) 434 return EAI_OVERFLOW; 435 if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen) 436 return EAI_OVERFLOW; 437 /* construct <numeric-addr><delim><zoneid> */ 438 memcpy(host + numaddrlen + 1, zonebuf, 439 (size_t)zonelen); 440 host[numaddrlen] = SCOPE_DELIMITER; 441 host[numaddrlen + 1 + zonelen] = '\0'; 442 } 443 444 return 0; 445 } 446 447 /* ARGSUSED */ 448 static int 449 ip6_sa2str(sa6, buf, bufsiz, flags) 450 const struct sockaddr_in6 *sa6; 451 char *buf; 452 size_t bufsiz; 453 int flags; 454 { 455 unsigned int ifindex; 456 const struct in6_addr *a6; 457 int n; 458 459 _DIAGASSERT(sa6 != NULL); 460 _DIAGASSERT(buf != NULL); 461 462 ifindex = (unsigned int)sa6->sin6_scope_id; 463 a6 = &sa6->sin6_addr; 464 465 #ifdef NI_NUMERICSCOPE 466 if ((flags & NI_NUMERICSCOPE) != 0) { 467 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 468 if (n < 0 || (size_t)n >= bufsiz) 469 return -1; 470 else 471 return n; 472 } 473 #endif 474 475 /* if_indextoname() does not take buffer size. not a good api... */ 476 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && 477 bufsiz >= IF_NAMESIZE) { 478 char *p = if_indextoname(ifindex, buf); 479 if (p) { 480 return(strlen(p)); 481 } 482 } 483 484 /* last resort */ 485 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 486 if (n < 0 || (size_t) n >= bufsiz) 487 return -1; 488 else 489 return n; 490 } 491 #endif /* INET6 */ 492 493 494 /* 495 * getnameinfo_link(): 496 * Format a link-layer address into a printable format, paying attention to 497 * the interface type. 498 */ 499 /* ARGSUSED */ 500 static int 501 getnameinfo_link(const struct sockaddr *sa, socklen_t salen, 502 char *host, socklen_t hostlen, char *serv, socklen_t servlen, 503 int flags) 504 { 505 const struct sockaddr_dl *sdl = 506 (const struct sockaddr_dl *)(const void *)sa; 507 const struct ieee1394_hwaddr *iha; 508 int n; 509 510 if (serv != NULL && servlen > 0) 511 *serv = '\0'; 512 513 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { 514 n = snprintf(host, hostlen, "link#%u", sdl->sdl_index); 515 if (n < 0 || (socklen_t) n > hostlen) { 516 *host = '\0'; 517 return EAI_MEMORY; 518 } 519 return 0; 520 } 521 522 switch (sdl->sdl_type) { 523 #ifdef IFT_ECONET 524 case IFT_ECONET: 525 if (sdl->sdl_alen < 2) 526 return EAI_FAMILY; 527 if (CLLADDR(sdl)[1] == 0) 528 n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]); 529 else 530 n = snprintf(host, hostlen, "%u.%u", 531 CLLADDR(sdl)[1], CLLADDR(sdl)[0]); 532 if (n < 0 || (socklen_t) n >= hostlen) { 533 *host = '\0'; 534 return EAI_MEMORY; 535 } else 536 return 0; 537 #endif 538 case IFT_IEEE1394: 539 if (sdl->sdl_alen < sizeof(iha->iha_uid)) 540 return EAI_FAMILY; 541 iha = 542 (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl); 543 return hexname(iha->iha_uid, sizeof(iha->iha_uid), 544 host, hostlen); 545 /* 546 * The following have zero-length addresses. 547 * IFT_ATM (net/if_atmsubr.c) 548 * IFT_FAITH (net/if_faith.c) 549 * IFT_GIF (net/if_gif.c) 550 * IFT_LOOP (net/if_loop.c) 551 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) 552 * IFT_SLIP (net/if_sl.c, net/if_strip.c) 553 * IFT_STF (net/if_stf.c) 554 * IFT_L2VLAN (net/if_vlan.c) 555 * IFT_PROPVIRTUAL (net/if_bridge.h> 556 */ 557 /* 558 * The following use IPv4 addresses as link-layer addresses: 559 * IFT_OTHER (net/if_gre.c) 560 */ 561 case IFT_ARCNET: /* default below is believed correct for all these. */ 562 case IFT_ETHER: 563 case IFT_FDDI: 564 case IFT_HIPPI: 565 case IFT_ISO88025: 566 default: 567 return hexname((const u_int8_t *)CLLADDR(sdl), 568 (size_t)sdl->sdl_alen, host, hostlen); 569 } 570 } 571 572 static int 573 hexname(cp, len, host, hostlen) 574 const u_int8_t *cp; 575 char *host; 576 size_t len; 577 socklen_t hostlen; 578 { 579 int n; 580 size_t i; 581 char *outp = host; 582 583 *outp = '\0'; 584 for (i = 0; i < len; i++) { 585 n = snprintf(outp, hostlen, "%s%02x", 586 i ? ":" : "", cp[i]); 587 if (n < 0 || (socklen_t) n >= hostlen) { 588 *host = '\0'; 589 return EAI_MEMORY; 590 } 591 outp += n; 592 hostlen -= n; 593 } 594 return 0; 595 } 596