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