1 /* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * Copyright (c) 2000 Ben Harris. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the project nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Issues to be discussed: 37 * - Thread safe-ness must be checked 38 * - RFC2553 says that we should raise error on short buffer. X/Open says 39 * we need to truncate the result. We obey RFC2553 (and X/Open should be 40 * modified). ipngwg rough consensus seems to follow RFC2553. 41 * - What is "local" in NI_FQDN? 42 * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. 43 * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if 44 * sin6_scope_id is filled - standardization status? 45 * XXX breaks backward compat for code that expects no scopeid. 46 * beware on merge. 47 */ 48 49 #include <sys/cdefs.h> 50 #include <sys/types.h> 51 #include <sys/socket.h> 52 #include <sys/un.h> 53 #include <net/if.h> 54 #include <net/if_dl.h> 55 #include <net/if_types.h> 56 #include <net/firewire.h> 57 #include <netinet/in.h> 58 #include <arpa/inet.h> 59 #include <arpa/nameser.h> 60 #include <netdb.h> 61 #include <resolv.h> 62 #include <string.h> 63 #include <stddef.h> 64 #include <errno.h> 65 66 static const struct afd *find_afd(int); 67 static int getnameinfo_inet(const struct afd *, 68 const struct sockaddr *, socklen_t, char *, 69 size_t, char *, size_t, int); 70 #ifdef INET6 71 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, 72 size_t, int); 73 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); 74 #endif 75 static int getnameinfo_link(const struct afd *, 76 const struct sockaddr *, socklen_t, char *, 77 size_t, char *, size_t, int); 78 static int hexname(const u_int8_t *, size_t, char *, size_t); 79 static int getnameinfo_un(const struct afd *, 80 const struct sockaddr *, socklen_t, char *, 81 size_t, char *, size_t, int); 82 83 static const struct afd { 84 int a_af; 85 size_t a_addrlen; 86 socklen_t a_socklen; 87 int a_off; 88 int (*a_func)(const struct afd *, 89 const struct sockaddr *, socklen_t, char *, 90 size_t, char *, size_t, int); 91 } afdl [] = { 92 #ifdef INET6 93 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), 94 offsetof(struct sockaddr_in6, sin6_addr), 95 getnameinfo_inet}, 96 #endif 97 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), 98 offsetof(struct sockaddr_in, sin_addr), 99 getnameinfo_inet}, 100 #define sizeofmember(type, member) (sizeof(((type *)0)->member)) 101 {PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path), 102 sizeof(struct sockaddr_un), 103 offsetof(struct sockaddr_un, sun_path), 104 getnameinfo_un}, 105 {PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data), 106 sizeof(struct sockaddr_dl), 107 offsetof(struct sockaddr_dl, sdl_data), 108 getnameinfo_link}, 109 {0, 0, 0}, 110 }; 111 112 int 113 getnameinfo(const struct sockaddr *sa, socklen_t salen, 114 char *host, size_t hostlen, char *serv, size_t servlen, 115 int flags) 116 { 117 const struct afd *afd; 118 119 if (sa == NULL) 120 return (EAI_FAIL); 121 122 afd = find_afd(sa->sa_family); 123 if (afd == NULL) 124 return (EAI_FAMILY); 125 /* 126 * getnameinfo() accepts an salen of sizeof(struct sockaddr_storage) 127 * at maximum as shown in RFC 4038 Sec.6.2.3. 128 */ 129 if (salen > sizeof(struct sockaddr_storage)) 130 return (EAI_FAMILY); 131 132 switch (sa->sa_family) { 133 case PF_LOCAL: 134 /* 135 * PF_LOCAL uses variable salen depending on the 136 * content length of sun_path. Require 1 byte in 137 * sun_path at least. 138 */ 139 if (salen <= afd->a_socklen - 140 sizeofmember(struct sockaddr_un, sun_path)) 141 return (EAI_FAMILY); 142 else if (salen > afd->a_socklen) 143 salen = afd->a_socklen; 144 break; 145 case PF_LINK: 146 if (salen <= afd->a_socklen - 147 sizeofmember(struct sockaddr_dl, sdl_data)) 148 return (EAI_FAMILY); 149 break; 150 default: 151 if (salen < afd->a_socklen) 152 return (EAI_FAMILY); 153 else 154 salen = afd->a_socklen; 155 break; 156 } 157 158 return ((*afd->a_func)(afd, sa, salen, host, hostlen, 159 serv, servlen, flags)); 160 } 161 162 static const struct afd * 163 find_afd(int af) 164 { 165 const struct afd *afd; 166 167 if (af == PF_UNSPEC) 168 return (NULL); 169 for (afd = &afdl[0]; afd->a_af > 0; afd++) { 170 if (afd->a_af == af) 171 return (afd); 172 } 173 return (NULL); 174 } 175 176 static int 177 getnameinfo_inet(const struct afd *afd, 178 const struct sockaddr *sa, socklen_t salen, 179 char *host, size_t hostlen, char *serv, size_t servlen, 180 int flags) 181 { 182 struct servent *sp; 183 struct hostent *hp; 184 u_short port; 185 const char *addr; 186 u_int32_t v4a; 187 int h_error; 188 char numserv[512]; 189 char numaddr[512]; 190 191 /* network byte order */ 192 port = ((const struct sockaddr_in *)sa)->sin_port; 193 addr = (const char *)sa + afd->a_off; 194 195 if (serv == NULL || servlen == 0) { 196 /* 197 * do nothing in this case. 198 * in case you are wondering if "&&" is more correct than 199 * "||" here: rfc2553bis-03 says that serv == NULL OR 200 * servlen == 0 means that the caller does not want the result. 201 */ 202 } else { 203 if (flags & NI_NUMERICSERV) 204 sp = NULL; 205 else { 206 sp = getservbyport(port, 207 (flags & NI_DGRAM) ? "udp" : "tcp"); 208 } 209 if (sp) { 210 if (strlen(sp->s_name) + 1 > servlen) 211 return EAI_MEMORY; 212 strlcpy(serv, sp->s_name, servlen); 213 } else { 214 snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); 215 if (strlen(numserv) + 1 > servlen) 216 return EAI_MEMORY; 217 strlcpy(serv, numserv, servlen); 218 } 219 } 220 221 switch (sa->sa_family) { 222 case AF_INET: 223 v4a = (u_int32_t) 224 ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr); 225 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a) || 226 IN_ZERONET(v4a)) 227 flags |= NI_NUMERICHOST; 228 break; 229 #ifdef INET6 230 case AF_INET6: 231 { 232 const struct sockaddr_in6 *sin6; 233 sin6 = (const struct sockaddr_in6 *)sa; 234 switch (sin6->sin6_addr.s6_addr[0]) { 235 case 0x00: 236 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) 237 ; 238 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) 239 ; 240 else 241 flags |= NI_NUMERICHOST; 242 break; 243 default: 244 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 245 flags |= NI_NUMERICHOST; 246 } 247 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 248 flags |= NI_NUMERICHOST; 249 break; 250 } 251 } 252 break; 253 #endif 254 } 255 if (host == NULL || hostlen == 0) { 256 /* 257 * do nothing in this case. 258 * in case you are wondering if "&&" is more correct than 259 * "||" here: rfc2553bis-03 says that host == NULL or 260 * hostlen == 0 means that the caller does not want the result. 261 */ 262 } else if (flags & NI_NUMERICHOST) { 263 size_t numaddrlen; 264 265 /* NUMERICHOST and NAMEREQD conflicts with each other */ 266 if (flags & NI_NAMEREQD) 267 return EAI_NONAME; 268 269 switch(afd->a_af) { 270 #ifdef INET6 271 case AF_INET6: 272 { 273 int error; 274 275 if ((error = ip6_parsenumeric(sa, addr, host, 276 hostlen, flags)) != 0) 277 return(error); 278 break; 279 } 280 #endif 281 default: 282 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) 283 == NULL) 284 return EAI_SYSTEM; 285 numaddrlen = strlen(numaddr); 286 if (numaddrlen + 1 > hostlen) /* don't forget terminator */ 287 return EAI_MEMORY; 288 strlcpy(host, numaddr, hostlen); 289 break; 290 } 291 } else { 292 hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); 293 294 if (hp) { 295 #if 0 296 /* 297 * commented out, since "for local host" is not 298 * implemented here - see RFC2553 p30 299 */ 300 if (flags & NI_NOFQDN) { 301 char *p; 302 p = strchr(hp->h_name, '.'); 303 if (p) 304 *p = '\0'; 305 } 306 #endif 307 if (strlen(hp->h_name) + 1 > hostlen) { 308 freehostent(hp); 309 return EAI_MEMORY; 310 } 311 strlcpy(host, hp->h_name, hostlen); 312 freehostent(hp); 313 } else { 314 if (flags & NI_NAMEREQD) 315 return EAI_NONAME; 316 switch(afd->a_af) { 317 #ifdef INET6 318 case AF_INET6: 319 { 320 int error; 321 322 if ((error = ip6_parsenumeric(sa, addr, host, 323 hostlen, 324 flags)) != 0) 325 return(error); 326 break; 327 } 328 #endif 329 default: 330 if (inet_ntop(afd->a_af, addr, host, 331 hostlen) == NULL) 332 return EAI_SYSTEM; 333 break; 334 } 335 } 336 } 337 return(0); 338 } 339 340 #ifdef INET6 341 static int 342 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, 343 char *host, size_t hostlen, int flags) 344 { 345 size_t numaddrlen; 346 char numaddr[512]; 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_OVERFLOW; 354 strlcpy(host, numaddr, hostlen); 355 356 if (((const struct sockaddr_in6 *)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_OVERFLOW; 365 if (zonelen + 1 + numaddrlen + 1 > hostlen) 366 return EAI_OVERFLOW; 367 368 /* construct <numeric-addr><delim><zoneid> */ 369 memcpy(host + numaddrlen + 1, zonebuf, 370 (size_t)zonelen); 371 host[numaddrlen] = SCOPE_DELIMITER; 372 host[numaddrlen + 1 + zonelen] = '\0'; 373 } 374 375 return 0; 376 } 377 378 /* ARGSUSED */ 379 static int 380 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) 381 { 382 unsigned int ifindex; 383 const struct in6_addr *a6; 384 int n; 385 386 ifindex = (unsigned int)sa6->sin6_scope_id; 387 a6 = &sa6->sin6_addr; 388 389 if ((flags & NI_NUMERICSCOPE) != 0) { 390 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 391 if (n < 0 || n >= bufsiz) 392 return -1; 393 else 394 return n; 395 } 396 397 /* if_indextoname() does not take buffer size. not a good api... */ 398 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || 399 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) { 400 char *p = if_indextoname(ifindex, buf); 401 if (p) { 402 return(strlen(p)); 403 } 404 } 405 406 /* last resort */ 407 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 408 if (n < 0 || (size_t)n >= bufsiz) 409 return -1; 410 else 411 return n; 412 } 413 #endif /* INET6 */ 414 415 /* 416 * getnameinfo_link(): 417 * Format a link-layer address into a printable format, paying attention to 418 * the interface type. 419 */ 420 /* ARGSUSED */ 421 static int 422 getnameinfo_link(const struct afd *afd, 423 const struct sockaddr *sa, socklen_t salen, 424 char *host, size_t hostlen, char *serv, size_t servlen, int flags) 425 { 426 const struct sockaddr_dl *sdl = 427 (const struct sockaddr_dl *)(const void *)sa; 428 const struct fw_hwaddr *iha; 429 int n; 430 431 if (serv != NULL && servlen > 0) 432 *serv = '\0'; 433 434 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { 435 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index); 436 if (n >= hostlen) { 437 *host = '\0'; 438 return (EAI_MEMORY); 439 } 440 return (0); 441 } 442 443 if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) { 444 n = sdl->sdl_nlen; 445 if (n >= hostlen) { 446 *host = '\0'; 447 return (EAI_MEMORY); 448 } 449 memcpy(host, sdl->sdl_data, sdl->sdl_nlen); 450 host[n] = '\0'; 451 return (0); 452 } 453 454 switch (sdl->sdl_type) { 455 case IFT_IEEE1394: 456 if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) + 457 sizeof(iha->sender_unique_ID_lo)) 458 return EAI_FAMILY; 459 iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl); 460 return hexname((const u_int8_t *)&iha->sender_unique_ID_hi, 461 sizeof(iha->sender_unique_ID_hi) + 462 sizeof(iha->sender_unique_ID_lo), 463 host, hostlen); 464 /* 465 * The following have zero-length addresses. 466 * IFT_GIF (net/if_gif.c) 467 * IFT_LOOP (net/if_loop.c) 468 * IFT_PPP (net/if_tuntap.c) 469 * IFT_SLIP (net/if_sl.c, net/if_strip.c) 470 * IFT_STF (net/if_stf.c) 471 * IFT_L2VLAN (net/if_vlan.c) 472 * IFT_BRIDGE (net/if_bridge.h> 473 */ 474 /* 475 * The following use IPv4 addresses as link-layer addresses: 476 * IFT_OTHER (net/if_gre.c) 477 * IFT_OTHER (netinet/ip_ipip.c) 478 */ 479 /* default below is believed correct for all these. */ 480 case IFT_ETHER: 481 case IFT_FDDI: 482 case IFT_HIPPI: 483 case IFT_ISO88025: 484 default: 485 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen, 486 host, hostlen); 487 } 488 } 489 490 static int 491 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen) 492 { 493 int i, n; 494 char *outp = host; 495 496 *outp = '\0'; 497 for (i = 0; i < len; i++) { 498 n = snprintf(outp, hostlen, "%s%02x", 499 i ? ":" : "", cp[i]); 500 if (n < 0 || n >= hostlen) { 501 *host = '\0'; 502 return EAI_MEMORY; 503 } 504 outp += n; 505 hostlen -= n; 506 } 507 return 0; 508 } 509 510 /* 511 * getnameinfo_un(): 512 * Format a UNIX IPC domain address (pathname). 513 */ 514 /* ARGSUSED */ 515 static int 516 getnameinfo_un(const struct afd *afd, 517 const struct sockaddr *sa, socklen_t salen, 518 char *host, size_t hostlen, char *serv, size_t servlen, int flags) 519 { 520 size_t pathlen; 521 522 if (serv != NULL && servlen > 0) 523 *serv = '\0'; 524 if (host != NULL && hostlen > 0) { 525 pathlen = salen - afd->a_off; 526 527 if (pathlen + 1 > hostlen) { 528 *host = '\0'; 529 return (EAI_MEMORY); 530 } 531 strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1); 532 } 533 534 return (0); 535 } 536