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