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 __FBSDID("$FreeBSD$"); 51 52 #include <sys/types.h> 53 #include <sys/socket.h> 54 #include <sys/un.h> 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/firewire.h> 59 #include <netinet/in.h> 60 #include <arpa/inet.h> 61 #include <arpa/nameser.h> 62 #include <netdb.h> 63 #include <resolv.h> 64 #include <string.h> 65 #include <stddef.h> 66 #include <errno.h> 67 68 static const struct afd *find_afd(int); 69 static int getnameinfo_inet(const struct afd *, 70 const struct sockaddr *, socklen_t, char *, 71 size_t, char *, size_t, int); 72 #ifdef INET6 73 static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, 74 size_t, int); 75 static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); 76 #endif 77 static int getnameinfo_link(const struct afd *, 78 const struct sockaddr *, socklen_t, char *, 79 size_t, char *, size_t, int); 80 static int hexname(const u_int8_t *, size_t, char *, size_t); 81 static int getnameinfo_un(const struct afd *, 82 const struct sockaddr *, socklen_t, char *, 83 size_t, char *, size_t, int); 84 85 static const struct afd { 86 int a_af; 87 size_t a_addrlen; 88 socklen_t a_socklen; 89 int a_off; 90 int (*a_func)(const struct afd *, 91 const struct sockaddr *, socklen_t, char *, 92 size_t, char *, size_t, int); 93 } afdl [] = { 94 #ifdef INET6 95 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), 96 offsetof(struct sockaddr_in6, sin6_addr), 97 getnameinfo_inet}, 98 #endif 99 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), 100 offsetof(struct sockaddr_in, sin_addr), 101 getnameinfo_inet}, 102 #define sizeofmember(type, member) (sizeof(((type *)0)->member)) 103 {PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path), 104 sizeof(struct sockaddr_un), 105 offsetof(struct sockaddr_un, sun_path), 106 getnameinfo_un}, 107 {PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data), 108 sizeof(struct sockaddr_dl), 109 offsetof(struct sockaddr_dl, sdl_data), 110 getnameinfo_link}, 111 {0, 0, 0}, 112 }; 113 114 int 115 getnameinfo(const struct sockaddr *sa, socklen_t salen, 116 char *host, size_t hostlen, char *serv, size_t servlen, 117 int flags) 118 { 119 const struct afd *afd; 120 121 if (sa == NULL) 122 return (EAI_FAIL); 123 124 afd = find_afd(sa->sa_family); 125 if (afd == NULL) 126 return (EAI_FAMILY); 127 switch (sa->sa_family) { 128 case PF_LOCAL: 129 /* 130 * PF_LOCAL uses variable sa->sa_len depending on the 131 * content length of sun_path. Require 1 byte in 132 * sun_path at least. 133 */ 134 if (salen > afd->a_socklen || 135 salen <= afd->a_socklen - 136 sizeofmember(struct sockaddr_un, sun_path)) 137 return (EAI_FAIL); 138 break; 139 case PF_LINK: 140 if (salen <= afd->a_socklen - 141 sizeofmember(struct sockaddr_dl, sdl_data)) 142 return (EAI_FAIL); 143 break; 144 default: 145 if (salen != afd->a_socklen) 146 return (EAI_FAIL); 147 break; 148 } 149 150 return ((*afd->a_func)(afd, sa, salen, host, hostlen, 151 serv, servlen, flags)); 152 } 153 154 static const struct afd * 155 find_afd(int af) 156 { 157 const struct afd *afd; 158 159 if (af == PF_UNSPEC) 160 return (NULL); 161 for (afd = &afdl[0]; afd->a_af > 0; afd++) { 162 if (afd->a_af == af) 163 return (afd); 164 } 165 return (NULL); 166 } 167 168 static int 169 getnameinfo_inet(const struct afd *afd, 170 const struct sockaddr *sa, socklen_t salen, 171 char *host, size_t hostlen, char *serv, size_t servlen, 172 int flags) 173 { 174 struct servent *sp; 175 struct hostent *hp; 176 u_short port; 177 const char *addr; 178 u_int32_t v4a; 179 int h_error; 180 char numserv[512]; 181 char numaddr[512]; 182 183 /* network byte order */ 184 port = ((const struct sockaddr_in *)sa)->sin_port; 185 addr = (const char *)sa + afd->a_off; 186 187 if (serv == NULL || servlen == 0) { 188 /* 189 * do nothing in this case. 190 * in case you are wondering if "&&" is more correct than 191 * "||" here: rfc2553bis-03 says that serv == NULL OR 192 * servlen == 0 means that the caller does not want the result. 193 */ 194 } else { 195 if (flags & NI_NUMERICSERV) 196 sp = NULL; 197 else { 198 sp = getservbyport(port, 199 (flags & NI_DGRAM) ? "udp" : "tcp"); 200 } 201 if (sp) { 202 if (strlen(sp->s_name) + 1 > servlen) 203 return EAI_MEMORY; 204 strlcpy(serv, sp->s_name, servlen); 205 } else { 206 snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); 207 if (strlen(numserv) + 1 > servlen) 208 return EAI_MEMORY; 209 strlcpy(serv, numserv, servlen); 210 } 211 } 212 213 switch (sa->sa_family) { 214 case AF_INET: 215 v4a = (u_int32_t) 216 ntohl(((const struct sockaddr_in *)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 *)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 = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); 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 freehostent(hp); 303 return EAI_MEMORY; 304 } 305 strlcpy(host, hp->h_name, hostlen); 306 freehostent(hp); 307 } else { 308 if (flags & NI_NAMEREQD) 309 return EAI_NONAME; 310 switch(afd->a_af) { 311 #ifdef INET6 312 case AF_INET6: 313 { 314 int error; 315 316 if ((error = ip6_parsenumeric(sa, addr, host, 317 hostlen, 318 flags)) != 0) 319 return(error); 320 break; 321 } 322 #endif 323 default: 324 if (inet_ntop(afd->a_af, addr, host, 325 hostlen) == NULL) 326 return EAI_SYSTEM; 327 break; 328 } 329 } 330 } 331 return(0); 332 } 333 334 #ifdef INET6 335 static int 336 ip6_parsenumeric(const struct sockaddr *sa, const char *addr, 337 char *host, size_t hostlen, int flags) 338 { 339 size_t numaddrlen; 340 char numaddr[512]; 341 342 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) 343 return EAI_SYSTEM; 344 345 numaddrlen = strlen(numaddr); 346 if (numaddrlen + 1 > hostlen) /* don't forget terminator */ 347 return EAI_OVERFLOW; 348 strlcpy(host, numaddr, hostlen); 349 350 if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { 351 char zonebuf[MAXHOSTNAMELEN]; 352 int zonelen; 353 354 zonelen = ip6_sa2str( 355 (const struct sockaddr_in6 *)(const void *)sa, 356 zonebuf, sizeof(zonebuf), flags); 357 if (zonelen < 0) 358 return EAI_OVERFLOW; 359 if (zonelen + 1 + numaddrlen + 1 > hostlen) 360 return EAI_OVERFLOW; 361 362 /* construct <numeric-addr><delim><zoneid> */ 363 memcpy(host + numaddrlen + 1, zonebuf, 364 (size_t)zonelen); 365 host[numaddrlen] = SCOPE_DELIMITER; 366 host[numaddrlen + 1 + zonelen] = '\0'; 367 } 368 369 return 0; 370 } 371 372 /* ARGSUSED */ 373 static int 374 ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) 375 { 376 unsigned int ifindex; 377 const struct in6_addr *a6; 378 int n; 379 380 ifindex = (unsigned int)sa6->sin6_scope_id; 381 a6 = &sa6->sin6_addr; 382 383 if ((flags & NI_NUMERICSCOPE) != 0) { 384 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 385 if (n < 0 || n >= bufsiz) 386 return -1; 387 else 388 return n; 389 } 390 391 /* if_indextoname() does not take buffer size. not a good api... */ 392 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || 393 IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) { 394 char *p = if_indextoname(ifindex, buf); 395 if (p) { 396 return(strlen(p)); 397 } 398 } 399 400 /* last resort */ 401 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); 402 if (n < 0 || (size_t)n >= bufsiz) 403 return -1; 404 else 405 return n; 406 } 407 #endif /* INET6 */ 408 409 /* 410 * getnameinfo_link(): 411 * Format a link-layer address into a printable format, paying attention to 412 * the interface type. 413 */ 414 /* ARGSUSED */ 415 static int 416 getnameinfo_link(const struct afd *afd, 417 const struct sockaddr *sa, socklen_t salen, 418 char *host, size_t hostlen, char *serv, size_t servlen, int flags) 419 { 420 const struct sockaddr_dl *sdl = 421 (const struct sockaddr_dl *)(const void *)sa; 422 const struct fw_hwaddr *iha; 423 int n; 424 425 if (serv != NULL && servlen > 0) 426 *serv = '\0'; 427 428 if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { 429 n = snprintf(host, hostlen, "link#%d", sdl->sdl_index); 430 if (n >= hostlen) { 431 *host = '\0'; 432 return (EAI_MEMORY); 433 } 434 return (0); 435 } 436 437 if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) { 438 n = sdl->sdl_nlen; 439 if (n >= hostlen) { 440 *host = '\0'; 441 return (EAI_MEMORY); 442 } 443 memcpy(host, sdl->sdl_data, sdl->sdl_nlen); 444 host[n] = '\0'; 445 return (0); 446 } 447 448 switch (sdl->sdl_type) { 449 case IFT_IEEE1394: 450 if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) + 451 sizeof(iha->sender_unique_ID_lo)) 452 return EAI_FAMILY; 453 iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl); 454 return hexname((const u_int8_t *)&iha->sender_unique_ID_hi, 455 sizeof(iha->sender_unique_ID_hi) + 456 sizeof(iha->sender_unique_ID_lo), 457 host, hostlen); 458 /* 459 * The following have zero-length addresses. 460 * IFT_GIF (net/if_gif.c) 461 * IFT_LOOP (net/if_loop.c) 462 * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) 463 * IFT_SLIP (net/if_sl.c, net/if_strip.c) 464 * IFT_STF (net/if_stf.c) 465 * IFT_L2VLAN (net/if_vlan.c) 466 * IFT_BRIDGE (net/if_bridge.h> 467 */ 468 /* 469 * The following use IPv4 addresses as link-layer addresses: 470 * IFT_OTHER (net/if_gre.c) 471 * IFT_OTHER (netinet/ip_ipip.c) 472 */ 473 /* default below is believed correct for all these. */ 474 case IFT_ARCNET: 475 case IFT_ETHER: 476 case IFT_FDDI: 477 case IFT_HIPPI: 478 case IFT_ISO88025: 479 default: 480 return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen, 481 host, hostlen); 482 } 483 } 484 485 static int 486 hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen) 487 { 488 int i, n; 489 char *outp = host; 490 491 *outp = '\0'; 492 for (i = 0; i < len; i++) { 493 n = snprintf(outp, hostlen, "%s%02x", 494 i ? ":" : "", cp[i]); 495 if (n < 0 || n >= hostlen) { 496 *host = '\0'; 497 return EAI_MEMORY; 498 } 499 outp += n; 500 hostlen -= n; 501 } 502 return 0; 503 } 504 505 /* 506 * getnameinfo_un(): 507 * Format a UNIX IPC domain address (pathname). 508 */ 509 /* ARGSUSED */ 510 static int 511 getnameinfo_un(const struct afd *afd, 512 const struct sockaddr *sa, socklen_t salen, 513 char *host, size_t hostlen, char *serv, size_t servlen, int flags) 514 { 515 size_t pathlen; 516 517 if (serv != NULL && servlen > 0) 518 *serv = '\0'; 519 if (host != NULL && hostlen > 0) { 520 pathlen = sa->sa_len - afd->a_off; 521 522 if (pathlen + 1 > hostlen) { 523 *host = '\0'; 524 return (EAI_MEMORY); 525 } 526 strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1); 527 } 528 529 return (0); 530 } 531