name6.c revision 144527
1/* $KAME: name6.c,v 1.25 2000/06/26 16:44:40 itojun Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31/* 32 * ++Copyright++ 1985, 1988, 1993 33 * - 34 * Copyright (c) 1985, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * - 65 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 66 * 67 * Permission to use, copy, modify, and distribute this software for any 68 * purpose with or without fee is hereby granted, provided that the above 69 * copyright notice and this permission notice appear in all copies, and that 70 * the name of Digital Equipment Corporation not be used in advertising or 71 * publicity pertaining to distribution of the document or software without 72 * specific, written prior permission. 73 * 74 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 75 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 76 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 77 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 78 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 79 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 80 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 81 * SOFTWARE. 82 * - 83 * --Copyright-- 84 */ 85 86/* 87 * Atsushi Onoe <onoe@sm.sony.co.jp> 88 */ 89 90/* 91 * TODO for thread safe 92 * use mutex for _hostconf, _hostconf_init. 93 * rewrite resolvers to be thread safe 94 */ 95 96#include <sys/cdefs.h> 97__FBSDID("$FreeBSD: head/lib/libc/net/name6.c 144527 2005-04-02 08:18:33Z ume $"); 98 99#include "namespace.h" 100#include <sys/param.h> 101#include <sys/socket.h> 102#include <sys/time.h> 103#include <sys/queue.h> 104#include <netinet/in.h> 105#ifdef INET6 106#include <net/if.h> 107#include <net/if_var.h> 108#include <sys/sysctl.h> 109#include <sys/ioctl.h> 110#include <netinet6/in6_var.h> /* XXX */ 111#endif 112 113#include <arpa/inet.h> 114#include <arpa/nameser.h> 115 116#include <errno.h> 117#include <netdb.h> 118#include <resolv.h> 119#include <stdio.h> 120#include <stdlib.h> 121#include <string.h> 122#include <stdarg.h> 123#include <nsswitch.h> 124#include <pthread.h> 125#include <unistd.h> 126#include "un-namespace.h" 127 128#ifndef _PATH_HOSTS 129#define _PATH_HOSTS "/etc/hosts" 130#endif 131 132#ifndef MAXALIASES 133#define MAXALIASES 10 134#endif 135#ifndef MAXADDRS 136#define MAXADDRS 20 137#endif 138#ifndef MAXDNAME 139#define MAXDNAME 1025 140#endif 141 142#ifdef INET6 143#define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \ 144 sizeof(struct in_addr)) 145#else 146#define ADDRLEN(af) sizeof(struct in_addr) 147#endif 148 149#define MAPADDR(ab, ina) \ 150do { \ 151 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \ 152 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \ 153 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \ 154} while (0) 155#define MAPADDRENABLED(flags) \ 156 (((flags) & AI_V4MAPPED) || \ 157 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled())) 158 159union inx_addr { 160 struct in_addr in_addr; 161#ifdef INET6 162 struct in6_addr in6_addr; 163#endif 164 struct { 165 u_char mau_zero[10]; 166 u_char mau_one[2]; 167 struct in_addr mau_inaddr; 168 } map_addr_un; 169#define map_zero map_addr_un.mau_zero 170#define map_one map_addr_un.mau_one 171#define map_inaddr map_addr_un.mau_inaddr 172}; 173 174struct policyqueue { 175 TAILQ_ENTRY(policyqueue) pc_entry; 176#ifdef INET6 177 struct in6_addrpolicy pc_policy; 178#endif 179}; 180TAILQ_HEAD(policyhead, policyqueue); 181 182#define AIO_SRCFLAG_DEPRECATED 0x1 183 184struct hp_order { 185 union { 186 struct sockaddr_storage aiou_ss; 187 struct sockaddr aiou_sa; 188 } aio_src_un; 189#define aio_srcsa aio_src_un.aiou_sa 190 u_int32_t aio_srcflag; 191 int aio_srcscope; 192 int aio_dstscope; 193 struct policyqueue *aio_srcpolicy; 194 struct policyqueue *aio_dstpolicy; 195 union { 196 struct sockaddr_storage aiou_ss; 197 struct sockaddr aiou_sa; 198 } aio_un; 199#define aio_sa aio_un.aiou_sa 200 int aio_matchlen; 201 u_char *aio_h_addr; 202}; 203 204static struct hostent *_hpcopy(struct hostent *hp, int *errp); 205static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp); 206static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp); 207#ifdef INET6 208static struct hostent *_hpmapv6(struct hostent *hp, int *errp); 209#endif 210static struct hostent *_hpsort(struct hostent *hp); 211static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp); 212static char *_hgetword(char **pp); 213static int _mapped_addr_enabled(void); 214 215static struct hostent *_hpreorder(struct hostent *hp); 216static int get_addrselectpolicy(struct policyhead *); 217static void free_addrselectpolicy(struct policyhead *); 218static struct policyqueue *match_addrselectpolicy(struct sockaddr *, 219 struct policyhead *); 220static void set_source(struct hp_order *, struct policyhead *); 221static int matchlen(struct sockaddr *, struct sockaddr *); 222static int comp_dst(const void *, const void *); 223static int gai_addr2scopetype(struct sockaddr *); 224 225static FILE *_files_open(int *errp); 226static int _files_ghbyname(void *, void *, va_list); 227static int _files_ghbyaddr(void *, void *, va_list); 228#ifdef YP 229static int _nis_ghbyname(void *, void *, va_list); 230static int _nis_ghbyaddr(void *, void *, va_list); 231#endif 232static int _dns_ghbyname(void *, void *, va_list); 233static int _dns_ghbyaddr(void *, void *, va_list); 234static void _dns_shent(int stayopen) __unused; 235static void _dns_ehent(void) __unused; 236#ifdef ICMPNL 237static int _icmp_ghbyaddr(void *, void *, va_list); 238#endif /* ICMPNL */ 239 240/* 241 * XXX: Many dependencies are not thread-safe. So, we share lock between 242 * getaddrinfo() and getipnodeby*(). Still, we cannot use 243 * getaddrinfo() and getipnodeby*() in conjunction with other 244 * functions which call them. 245 */ 246#include "libc_private.h" 247extern pthread_mutex_t __getaddrinfo_thread_lock; 248#define THREAD_LOCK() \ 249 if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock); 250#define THREAD_UNLOCK() \ 251 if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock); 252 253/* Host lookup order if nsswitch.conf is broken or nonexistant */ 254static const ns_src default_src[] = { 255 { NSSRC_FILES, NS_SUCCESS }, 256 { NSSRC_DNS, NS_SUCCESS }, 257#ifdef ICMPNL 258#define NSSRC_ICMP "icmp" 259 { NSSRC_ICMP, NS_SUCCESS }, 260#endif 261 { 0 } 262}; 263 264/* 265 * Check if kernel supports mapped address. 266 * implementation dependent 267 */ 268#ifdef __KAME__ 269#include <sys/sysctl.h> 270#endif /* __KAME__ */ 271 272static int 273_mapped_addr_enabled(void) 274{ 275 /* implementation dependent check */ 276#if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR) 277 int mib[4]; 278 size_t len; 279 int val; 280 281 mib[0] = CTL_NET; 282 mib[1] = PF_INET6; 283 mib[2] = IPPROTO_IPV6; 284 mib[3] = IPV6CTL_MAPPED_ADDR; 285 len = sizeof(val); 286 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0) 287 return 1; 288#endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */ 289 return 0; 290} 291 292/* 293 * Functions defined in RFC2553 294 * getipnodebyname, getipnodebyaddr, freehostent 295 */ 296 297static struct hostent * 298_ghbyname(const char *name, int af, int flags, int *errp) 299{ 300 struct hostent *hp; 301 int rval; 302 303 static const ns_dtab dtab[] = { 304 NS_FILES_CB(_files_ghbyname, NULL) 305 { NSSRC_DNS, _dns_ghbyname, NULL }, 306 NS_NIS_CB(_nis_ghbyname, NULL) 307 { 0 } 308 }; 309 310 if (flags & AI_ADDRCONFIG) { 311 int s; 312 313 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 314 return NULL; 315 /* 316 * TODO: 317 * Note that implementation dependent test for address 318 * configuration should be done everytime called 319 * (or apropriate interval), 320 * because addresses will be dynamically assigned or deleted. 321 */ 322 _close(s); 323 } 324 325 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src, 326 name, af, errp); 327 return (rval == NS_SUCCESS) ? hp : NULL; 328} 329 330struct hostent * 331getipnodebyname(const char *name, int af, int flags, int *errp) 332{ 333 struct hostent *hp; 334 union inx_addr addrbuf; 335 336 if (af != AF_INET 337#ifdef INET6 338 && af != AF_INET6 339#endif 340 ) 341 { 342 *errp = NO_RECOVERY; 343 return NULL; 344 } 345 346#ifdef INET6 347 /* special case for literal address */ 348 if (inet_pton(AF_INET6, name, &addrbuf) == 1) { 349 if (af != AF_INET6) { 350 *errp = HOST_NOT_FOUND; 351 return NULL; 352 } 353 return _hpaddr(af, name, &addrbuf, errp); 354 } 355#endif 356 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) { 357 if (af != AF_INET) { 358 if (MAPADDRENABLED(flags)) { 359 MAPADDR(&addrbuf, &addrbuf.in_addr); 360 } else { 361 *errp = HOST_NOT_FOUND; 362 return NULL; 363 } 364 } 365 return _hpaddr(af, name, &addrbuf, errp); 366 } 367 368 *errp = HOST_NOT_FOUND; 369 hp = _ghbyname(name, af, flags, errp); 370 371#ifdef INET6 372 if (af == AF_INET6 373 && ((flags & AI_ALL) || hp == NULL) 374 && (MAPADDRENABLED(flags))) { 375 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp); 376 if (hp == NULL) 377 hp = _hpmapv6(hp2, errp); 378 else { 379 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) { 380 freehostent(hp2); 381 hp2 = NULL; 382 } 383 hp = _hpmerge(hp, hp2, errp); 384 } 385 } 386#endif 387 return _hpreorder(_hpsort(hp)); 388} 389 390struct hostent * 391getipnodebyaddr(const void *src, size_t len, int af, int *errp) 392{ 393 struct hostent *hp; 394 int rval; 395#ifdef INET6 396 struct in6_addr addrbuf; 397#else 398 struct in_addr addrbuf; 399#endif 400 401 static const ns_dtab dtab[] = { 402 NS_FILES_CB(_files_ghbyaddr, NULL) 403 { NSSRC_DNS, _dns_ghbyaddr, NULL }, 404 NS_NIS_CB(_nis_ghbyaddr, NULL) 405#ifdef ICMPNL 406 { NSSRC_ICMP, _icmp_ghbyaddr, NULL }, 407#endif 408 { 0 } 409 }; 410 411 *errp = HOST_NOT_FOUND; 412 413 switch (af) { 414 case AF_INET: 415 if (len != sizeof(struct in_addr)) { 416 *errp = NO_RECOVERY; 417 return NULL; 418 } 419 if ((long)src & ~(sizeof(struct in_addr) - 1)) { 420 memcpy(&addrbuf, src, len); 421 src = &addrbuf; 422 } 423 if (((struct in_addr *)src)->s_addr == 0) 424 return NULL; 425 break; 426#ifdef INET6 427 case AF_INET6: 428 if (len != sizeof(struct in6_addr)) { 429 *errp = NO_RECOVERY; 430 return NULL; 431 } 432 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/ 433 memcpy(&addrbuf, src, len); 434 src = &addrbuf; 435 } 436 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src)) 437 return NULL; 438 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src) 439 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) { 440 src = (char *)src + 441 (sizeof(struct in6_addr) - sizeof(struct in_addr)); 442 af = AF_INET; 443 len = sizeof(struct in_addr); 444 } 445 break; 446#endif 447 default: 448 *errp = NO_RECOVERY; 449 return NULL; 450 } 451 452 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src, 453 src, len, af, errp); 454 return (rval == NS_SUCCESS) ? hp : NULL; 455} 456 457void 458freehostent(struct hostent *ptr) 459{ 460 free(ptr); 461} 462 463#if 0 464 465/* XXX: should be deprecated */ 466struct hostent * 467getnodebyname(const char *name, int af, int flags) 468{ 469 return getipnodebyname(name, af, flags, &h_errno); 470} 471 472#ifdef __warn_references 473__warn_references(getnodebyname, 474 "warning: getnodebyname() deprecated, " 475 "should use getaddrinfo() or getipnodebyname()"); 476#endif 477 478struct hostent * 479getnodebyaddr(const void *src, size_t len, int af) 480{ 481 return getipnodebyaddr(src, len, af, &h_errno); 482} 483 484#ifdef __warn_references 485__warn_references(getnodebyaddr, 486 "warning: getnodebyaddr() deprecated, " 487 "should use getnameinfo() or getipnodebyaddr()"); 488#endif 489 490#endif 491 492/* 493 * Private utility functions 494 */ 495 496/* 497 * _hpcopy: allocate and copy hostent structure 498 */ 499static struct hostent * 500_hpcopy(struct hostent *hp, int *errp) 501{ 502 struct hostent *nhp; 503 char *cp, **pp; 504 int size, addrsize; 505 int nalias = 0, naddr = 0; 506 int al_off; 507 int i; 508 509 if (hp == NULL) 510 return hp; 511 512 /* count size to be allocated */ 513 size = sizeof(struct hostent); 514 if (hp->h_name != NULL) 515 size += strlen(hp->h_name) + 1; 516 if ((pp = hp->h_aliases) != NULL) { 517 for (i = 0; *pp != NULL; i++, pp++) { 518 if (**pp != '\0') { 519 size += strlen(*pp) + 1; 520 nalias++; 521 } 522 } 523 } 524 /* adjust alignment */ 525 size = ALIGN(size); 526 al_off = size; 527 size += sizeof(char *) * (nalias + 1); 528 addrsize = ALIGN(hp->h_length); 529 if ((pp = hp->h_addr_list) != NULL) { 530 while (*pp++ != NULL) 531 naddr++; 532 } 533 size += addrsize * naddr; 534 size += sizeof(char *) * (naddr + 1); 535 536 /* copy */ 537 if ((nhp = (struct hostent *)malloc(size)) == NULL) { 538 *errp = TRY_AGAIN; 539 return NULL; 540 } 541 cp = (char *)&nhp[1]; 542 if (hp->h_name != NULL) { 543 nhp->h_name = cp; 544 strcpy(cp, hp->h_name); 545 cp += strlen(cp) + 1; 546 } else 547 nhp->h_name = NULL; 548 nhp->h_aliases = (char **)((char *)nhp + al_off); 549 if ((pp = hp->h_aliases) != NULL) { 550 for (i = 0; *pp != NULL; pp++) { 551 if (**pp != '\0') { 552 nhp->h_aliases[i++] = cp; 553 strcpy(cp, *pp); 554 cp += strlen(cp) + 1; 555 } 556 } 557 } 558 nhp->h_aliases[nalias] = NULL; 559 cp = (char *)&nhp->h_aliases[nalias + 1]; 560 nhp->h_addrtype = hp->h_addrtype; 561 nhp->h_length = hp->h_length; 562 nhp->h_addr_list = (char **)cp; 563 if ((pp = hp->h_addr_list) != NULL) { 564 cp = (char *)&nhp->h_addr_list[naddr + 1]; 565 for (i = 0; *pp != NULL; pp++) { 566 nhp->h_addr_list[i++] = cp; 567 memcpy(cp, *pp, hp->h_length); 568 cp += addrsize; 569 } 570 } 571 nhp->h_addr_list[naddr] = NULL; 572 return nhp; 573} 574 575/* 576 * _hpaddr: construct hostent structure with one address 577 */ 578static struct hostent * 579_hpaddr(int af, const char *name, void *addr, int *errp) 580{ 581 struct hostent *hp, hpbuf; 582 char *addrs[2]; 583 584 hp = &hpbuf; 585 hp->h_name = (char *)name; 586 hp->h_aliases = NULL; 587 hp->h_addrtype = af; 588 hp->h_length = ADDRLEN(af); 589 hp->h_addr_list = addrs; 590 addrs[0] = (char *)addr; 591 addrs[1] = NULL; 592 return _hpcopy(hp, errp); 593} 594 595/* 596 * _hpmerge: merge 2 hostent structure, arguments will be freed 597 */ 598static struct hostent * 599_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp) 600{ 601 int i, j; 602 int naddr, nalias; 603 char **pp; 604 struct hostent *hp, hpbuf; 605 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1]; 606 union inx_addr addrbuf[MAXADDRS]; 607 608 if (hp1 == NULL) 609 return hp2; 610 if (hp2 == NULL) 611 return hp1; 612 613#define HP(i) (i == 1 ? hp1 : hp2) 614 hp = &hpbuf; 615 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name); 616 hp->h_aliases = aliases; 617 nalias = 0; 618 for (i = 1; i <= 2; i++) { 619 if ((pp = HP(i)->h_aliases) == NULL) 620 continue; 621 for (; nalias < MAXALIASES && *pp != NULL; pp++) { 622 /* check duplicates */ 623 for (j = 0; j < nalias; j++) 624 if (strcasecmp(*pp, aliases[j]) == 0) 625 break; 626 if (j == nalias) 627 aliases[nalias++] = *pp; 628 } 629 } 630 aliases[nalias] = NULL; 631#ifdef INET6 632 if (hp1->h_length != hp2->h_length) { 633 hp->h_addrtype = AF_INET6; 634 hp->h_length = sizeof(struct in6_addr); 635 } else { 636#endif 637 hp->h_addrtype = hp1->h_addrtype; 638 hp->h_length = hp1->h_length; 639#ifdef INET6 640 } 641#endif 642 hp->h_addr_list = addrs; 643 naddr = 0; 644 for (i = 1; i <= 2; i++) { 645 if ((pp = HP(i)->h_addr_list) == NULL) 646 continue; 647 if (HP(i)->h_length == hp->h_length) { 648 while (naddr < MAXADDRS && *pp != NULL) 649 addrs[naddr++] = *pp++; 650 } else { 651 /* copy IPv4 addr as mapped IPv6 addr */ 652 while (naddr < MAXADDRS && *pp != NULL) { 653 MAPADDR(&addrbuf[naddr], *pp++); 654 addrs[naddr] = (char *)&addrbuf[naddr]; 655 naddr++; 656 } 657 } 658 } 659 addrs[naddr] = NULL; 660 hp = _hpcopy(hp, errp); 661 freehostent(hp1); 662 freehostent(hp2); 663 return hp; 664} 665 666/* 667 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses 668 */ 669#ifdef INET6 670static struct hostent * 671_hpmapv6(struct hostent *hp, int *errp) 672{ 673 struct hostent *hp6; 674 675 if (hp == NULL) 676 return NULL; 677 if (hp->h_addrtype == AF_INET6) 678 return hp; 679 680 /* make dummy hostent to convert IPv6 address */ 681 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) { 682 *errp = TRY_AGAIN; 683 return NULL; 684 } 685 hp6->h_name = NULL; 686 hp6->h_aliases = NULL; 687 hp6->h_addrtype = AF_INET6; 688 hp6->h_length = sizeof(struct in6_addr); 689 hp6->h_addr_list = NULL; 690 return _hpmerge(hp6, hp, errp); 691} 692#endif 693 694/* 695 * _hpsort: sort address by sortlist 696 */ 697static struct hostent * 698_hpsort(struct hostent *hp) 699{ 700 int i, j, n; 701 u_char *ap, *sp, *mp, **pp; 702 char t; 703 char order[MAXADDRS]; 704 int nsort = _res.nsort; 705 706 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0) 707 return hp; 708 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) { 709 for (j = 0; j < nsort; j++) { 710#ifdef INET6 711 if (_res_ext.sort_list[j].af != hp->h_addrtype) 712 continue; 713 sp = (u_char *)&_res_ext.sort_list[j].addr; 714 mp = (u_char *)&_res_ext.sort_list[j].mask; 715#else 716 sp = (u_char *)&_res.sort_list[j].addr; 717 mp = (u_char *)&_res.sort_list[j].mask; 718#endif 719 for (n = 0; n < hp->h_length; n++) { 720 if ((ap[n] & mp[n]) != sp[n]) 721 break; 722 } 723 if (n == hp->h_length) 724 break; 725 } 726 order[i] = j; 727 } 728 n = i; 729 pp = (u_char **)hp->h_addr_list; 730 for (i = 0; i < n - 1; i++) { 731 for (j = i + 1; j < n; j++) { 732 if (order[i] > order[j]) { 733 ap = pp[i]; 734 pp[i] = pp[j]; 735 pp[j] = ap; 736 t = order[i]; 737 order[i] = order[j]; 738 order[j] = t; 739 } 740 } 741 } 742 return hp; 743} 744 745static char * 746_hgetword(char **pp) 747{ 748 char c, *p, *ret; 749 const char *sp; 750 static const char sep[] = "# \t\n"; 751 752 ret = NULL; 753 for (p = *pp; (c = *p) != '\0'; p++) { 754 for (sp = sep; *sp != '\0'; sp++) { 755 if (c == *sp) 756 break; 757 } 758 if (c == '#') 759 p[1] = '\0'; /* ignore rest of line */ 760 if (ret == NULL) { 761 if (*sp == '\0') 762 ret = p; 763 } else { 764 if (*sp != '\0') { 765 *p++ = '\0'; 766 break; 767 } 768 } 769 } 770 *pp = p; 771 if (ret == NULL || *ret == '\0') 772 return NULL; 773 return ret; 774} 775 776/* 777 * _hpreorder: sort address by default address selection 778 */ 779static struct hostent * 780_hpreorder(struct hostent *hp) 781{ 782 struct hp_order *aio; 783 int i, n; 784 u_char *ap; 785 struct sockaddr *sa; 786 struct policyhead policyhead; 787 788 if (hp == NULL) 789 return hp; 790 791 switch (hp->h_addrtype) { 792 case AF_INET: 793#ifdef INET6 794 case AF_INET6: 795#endif 796 break; 797 default: 798 free_addrselectpolicy(&policyhead); 799 return hp; 800 } 801 802 /* count the number of addrinfo elements for sorting. */ 803 for (n = 0; hp->h_addr_list[n] != NULL; n++) 804 ; 805 806 /* 807 * If the number is small enough, we can skip the reordering process. 808 */ 809 if (n <= 1) 810 return hp; 811 812 /* allocate a temporary array for sort and initialization of it. */ 813 if ((aio = malloc(sizeof(*aio) * n)) == NULL) 814 return hp; /* give up reordering */ 815 memset(aio, 0, sizeof(*aio) * n); 816 817 /* retrieve address selection policy from the kernel */ 818 TAILQ_INIT(&policyhead); 819 if (!get_addrselectpolicy(&policyhead)) { 820 /* no policy is installed into kernel, we don't sort. */ 821 free(aio); 822 return hp; 823 } 824 825 for (i = 0; i < n; i++) { 826 ap = (u_char *)hp->h_addr_list[i]; 827 aio[i].aio_h_addr = ap; 828 sa = &aio[i].aio_sa; 829 switch (hp->h_addrtype) { 830 case AF_INET: 831 sa->sa_family = AF_INET; 832 sa->sa_len = sizeof(struct sockaddr_in); 833 memcpy(&((struct sockaddr_in *)sa)->sin_addr, ap, 834 sizeof(struct in_addr)); 835 break; 836#ifdef INET6 837 case AF_INET6: 838 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { 839 sa->sa_family = AF_INET; 840 sa->sa_len = sizeof(struct sockaddr_in); 841 memcpy(&((struct sockaddr_in *)sa)->sin_addr, 842 &ap[12], sizeof(struct in_addr)); 843 } else { 844 sa->sa_family = AF_INET6; 845 sa->sa_len = sizeof(struct sockaddr_in6); 846 memcpy(&((struct sockaddr_in6 *)sa)->sin6_addr, 847 ap, sizeof(struct in6_addr)); 848 } 849 break; 850#endif 851 } 852 aio[i].aio_dstscope = gai_addr2scopetype(sa); 853 aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead); 854 set_source(&aio[i], &policyhead); 855 } 856 857 /* perform sorting. */ 858 qsort(aio, n, sizeof(*aio), comp_dst); 859 860 /* reorder the h_addr_list. */ 861 for (i = 0; i < n; i++) 862 hp->h_addr_list[i] = aio[i].aio_h_addr; 863 864 /* cleanup and return */ 865 free(aio); 866 free_addrselectpolicy(&policyhead); 867 return hp; 868} 869 870static int 871get_addrselectpolicy(head) 872 struct policyhead *head; 873{ 874#ifdef INET6 875 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 876 size_t l; 877 char *buf; 878 struct in6_addrpolicy *pol, *ep; 879 880 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) 881 return (0); 882 if ((buf = malloc(l)) == NULL) 883 return (0); 884 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 885 free(buf); 886 return (0); 887 } 888 889 ep = (struct in6_addrpolicy *)(buf + l); 890 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 891 struct policyqueue *new; 892 893 if ((new = malloc(sizeof(*new))) == NULL) { 894 free_addrselectpolicy(head); /* make the list empty */ 895 break; 896 } 897 new->pc_policy = *pol; 898 TAILQ_INSERT_TAIL(head, new, pc_entry); 899 } 900 901 free(buf); 902 return (1); 903#else 904 return (0); 905#endif 906} 907 908static void 909free_addrselectpolicy(head) 910 struct policyhead *head; 911{ 912 struct policyqueue *ent, *nent; 913 914 for (ent = TAILQ_FIRST(head); ent; ent = nent) { 915 nent = TAILQ_NEXT(ent, pc_entry); 916 TAILQ_REMOVE(head, ent, pc_entry); 917 free(ent); 918 } 919} 920 921static struct policyqueue * 922match_addrselectpolicy(addr, head) 923 struct sockaddr *addr; 924 struct policyhead *head; 925{ 926#ifdef INET6 927 struct policyqueue *ent, *bestent = NULL; 928 struct in6_addrpolicy *pol; 929 int matchlen, bestmatchlen = -1; 930 u_char *mp, *ep, *k, *p, m; 931 struct sockaddr_in6 key; 932 933 switch(addr->sa_family) { 934 case AF_INET6: 935 key = *(struct sockaddr_in6 *)addr; 936 break; 937 case AF_INET: 938 /* convert the address into IPv4-mapped IPv6 address. */ 939 memset(&key, 0, sizeof(key)); 940 key.sin6_family = AF_INET6; 941 key.sin6_len = sizeof(key); 942 key.sin6_addr.s6_addr[10] = 0xff; 943 key.sin6_addr.s6_addr[11] = 0xff; 944 memcpy(&key.sin6_addr.s6_addr[12], 945 &((struct sockaddr_in *)addr)->sin_addr, 4); 946 break; 947 default: 948 return(NULL); 949 } 950 951 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 952 pol = &ent->pc_policy; 953 matchlen = 0; 954 955 mp = (u_char *)&pol->addrmask.sin6_addr; 956 ep = mp + 16; /* XXX: scope field? */ 957 k = (u_char *)&key.sin6_addr; 958 p = (u_char *)&pol->addr.sin6_addr; 959 for (; mp < ep && *mp; mp++, k++, p++) { 960 m = *mp; 961 if ((*k & m) != *p) 962 goto next; /* not match */ 963 if (m == 0xff) /* short cut for a typical case */ 964 matchlen += 8; 965 else { 966 while (m >= 0x80) { 967 matchlen++; 968 m <<= 1; 969 } 970 } 971 } 972 973 /* matched. check if this is better than the current best. */ 974 if (matchlen > bestmatchlen) { 975 bestent = ent; 976 bestmatchlen = matchlen; 977 } 978 979 next: 980 continue; 981 } 982 983 return(bestent); 984#else 985 return(NULL); 986#endif 987 988} 989 990static void 991set_source(aio, ph) 992 struct hp_order *aio; 993 struct policyhead *ph; 994{ 995 struct sockaddr_storage ss = aio->aio_un.aiou_ss; 996 int s, srclen; 997 998 /* set unspec ("no source is available"), just in case */ 999 aio->aio_srcsa.sa_family = AF_UNSPEC; 1000 aio->aio_srcscope = -1; 1001 1002 switch(ss.ss_family) { 1003 case AF_INET: 1004 ((struct sockaddr_in *)&ss)->sin_port = htons(1); 1005 break; 1006#ifdef INET6 1007 case AF_INET6: 1008 ((struct sockaddr_in6 *)&ss)->sin6_port = htons(1); 1009 break; 1010#endif 1011 default: /* ignore unsupported AFs explicitly */ 1012 return; 1013 } 1014 1015 /* open a socket to get the source address for the given dst */ 1016 if ((s = _socket(ss.ss_family, SOCK_DGRAM, IPPROTO_UDP)) < 0) 1017 return; /* give up */ 1018 if (_connect(s, (struct sockaddr *)&ss, ss.ss_len) < 0) 1019 goto cleanup; 1020 srclen = ss.ss_len; 1021 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 1022 aio->aio_srcsa.sa_family = AF_UNSPEC; 1023 goto cleanup; 1024 } 1025 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 1026 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 1027 aio->aio_matchlen = matchlen(&aio->aio_srcsa, (struct sockaddr *)&ss); 1028#ifdef INET6 1029 if (ss.ss_family == AF_INET6) { 1030 struct in6_ifreq ifr6; 1031 u_int32_t flags6; 1032 1033 /* XXX: interface name should not be hardcoded */ 1034 strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name)); 1035 memset(&ifr6, 0, sizeof(ifr6)); 1036 memcpy(&ifr6.ifr_addr, &ss, ss.ss_len); 1037 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 1038 flags6 = ifr6.ifr_ifru.ifru_flags6; 1039 if ((flags6 & IN6_IFF_DEPRECATED)) 1040 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 1041 } 1042 } 1043#endif 1044 1045 cleanup: 1046 _close(s); 1047 return; 1048} 1049 1050static int 1051matchlen(src, dst) 1052 struct sockaddr *src, *dst; 1053{ 1054 int match = 0; 1055 u_char *s, *d; 1056 u_char *lim, r; 1057 int addrlen; 1058 1059 switch (src->sa_family) { 1060#ifdef INET6 1061 case AF_INET6: 1062 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 1063 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 1064 addrlen = sizeof(struct in6_addr); 1065 lim = s + addrlen; 1066 break; 1067#endif 1068 case AF_INET: 1069 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 1070 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 1071 addrlen = sizeof(struct in_addr); 1072 lim = s + addrlen; 1073 break; 1074 default: 1075 return(0); 1076 } 1077 1078 while (s < lim) 1079 if ((r = (*d++ ^ *s++)) != 0) { 1080 while (r < addrlen * 8) { 1081 match++; 1082 r <<= 1; 1083 } 1084 break; 1085 } else 1086 match += 8; 1087 return(match); 1088} 1089 1090static int 1091comp_dst(arg1, arg2) 1092 const void *arg1, *arg2; 1093{ 1094 const struct hp_order *dst1 = arg1, *dst2 = arg2; 1095 1096 /* 1097 * Rule 1: Avoid unusable destinations. 1098 * XXX: we currently do not consider if an appropriate route exists. 1099 */ 1100 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 1101 dst2->aio_srcsa.sa_family == AF_UNSPEC) { 1102 return(-1); 1103 } 1104 if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 1105 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 1106 return(1); 1107 } 1108 1109 /* Rule 2: Prefer matching scope. */ 1110 if (dst1->aio_dstscope == dst1->aio_srcscope && 1111 dst2->aio_dstscope != dst2->aio_srcscope) { 1112 return(-1); 1113 } 1114 if (dst1->aio_dstscope != dst1->aio_srcscope && 1115 dst2->aio_dstscope == dst2->aio_srcscope) { 1116 return(1); 1117 } 1118 1119 /* Rule 3: Avoid deprecated addresses. */ 1120 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 1121 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 1122 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 1123 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 1124 return(-1); 1125 } 1126 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 1127 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 1128 return(1); 1129 } 1130 } 1131 1132 /* Rule 4: Prefer home addresses. */ 1133 /* XXX: not implemented yet */ 1134 1135 /* Rule 5: Prefer matching label. */ 1136#ifdef INET6 1137 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 1138 dst1->aio_srcpolicy->pc_policy.label == 1139 dst1->aio_dstpolicy->pc_policy.label && 1140 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 1141 dst2->aio_srcpolicy->pc_policy.label != 1142 dst2->aio_dstpolicy->pc_policy.label)) { 1143 return(-1); 1144 } 1145 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 1146 dst2->aio_srcpolicy->pc_policy.label == 1147 dst2->aio_dstpolicy->pc_policy.label && 1148 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 1149 dst1->aio_srcpolicy->pc_policy.label != 1150 dst1->aio_dstpolicy->pc_policy.label)) { 1151 return(1); 1152 } 1153#endif 1154 1155 /* Rule 6: Prefer higher precedence. */ 1156#ifdef INET6 1157 if (dst1->aio_dstpolicy && 1158 (dst2->aio_dstpolicy == NULL || 1159 dst1->aio_dstpolicy->pc_policy.preced > 1160 dst2->aio_dstpolicy->pc_policy.preced)) { 1161 return(-1); 1162 } 1163 if (dst2->aio_dstpolicy && 1164 (dst1->aio_dstpolicy == NULL || 1165 dst2->aio_dstpolicy->pc_policy.preced > 1166 dst1->aio_dstpolicy->pc_policy.preced)) { 1167 return(1); 1168 } 1169#endif 1170 1171 /* Rule 7: Prefer native transport. */ 1172 /* XXX: not implemented yet */ 1173 1174 /* Rule 8: Prefer smaller scope. */ 1175 if (dst1->aio_dstscope >= 0 && 1176 dst1->aio_dstscope < dst2->aio_dstscope) { 1177 return(-1); 1178 } 1179 if (dst2->aio_dstscope >= 0 && 1180 dst2->aio_dstscope < dst1->aio_dstscope) { 1181 return(1); 1182 } 1183 1184 /* 1185 * Rule 9: Use longest matching prefix. 1186 * We compare the match length in a same AF only. 1187 */ 1188 if (dst1->aio_sa.sa_family == dst2->aio_sa.sa_family) { 1189 if (dst1->aio_matchlen > dst2->aio_matchlen) { 1190 return(-1); 1191 } 1192 if (dst1->aio_matchlen < dst2->aio_matchlen) { 1193 return(1); 1194 } 1195 } 1196 1197 /* Rule 10: Otherwise, leave the order unchanged. */ 1198 return(-1); 1199} 1200 1201/* 1202 * Copy from scope.c. 1203 * XXX: we should standardize the functions and link them as standard 1204 * library. 1205 */ 1206static int 1207gai_addr2scopetype(sa) 1208 struct sockaddr *sa; 1209{ 1210#ifdef INET6 1211 struct sockaddr_in6 *sa6; 1212#endif 1213 struct sockaddr_in *sa4; 1214 1215 switch(sa->sa_family) { 1216#ifdef INET6 1217 case AF_INET6: 1218 sa6 = (struct sockaddr_in6 *)sa; 1219 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 1220 /* just use the scope field of the multicast address */ 1221 return(sa6->sin6_addr.s6_addr[2] & 0x0f); 1222 } 1223 /* 1224 * Unicast addresses: map scope type to corresponding scope 1225 * value defined for multcast addresses. 1226 * XXX: hardcoded scope type values are bad... 1227 */ 1228 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 1229 return(1); /* node local scope */ 1230 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 1231 return(2); /* link-local scope */ 1232 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 1233 return(5); /* site-local scope */ 1234 return(14); /* global scope */ 1235 break; 1236#endif 1237 case AF_INET: 1238 /* 1239 * IPv4 pseudo scoping according to RFC 3484. 1240 */ 1241 sa4 = (struct sockaddr_in *)sa; 1242 /* IPv4 autoconfiguration addresses have link-local scope. */ 1243 if (((u_char *)&sa4->sin_addr)[0] == 169 && 1244 ((u_char *)&sa4->sin_addr)[1] == 254) 1245 return(2); 1246 /* Private addresses have site-local scope. */ 1247 if (((u_char *)&sa4->sin_addr)[0] == 10 || 1248 (((u_char *)&sa4->sin_addr)[0] == 172 && 1249 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 1250 (((u_char *)&sa4->sin_addr)[0] == 192 && 1251 ((u_char *)&sa4->sin_addr)[1] == 168)) 1252 return(14); /* XXX: It should be 5 unless NAT */ 1253 /* Loopback addresses have link-local scope. */ 1254 if (((u_char *)&sa4->sin_addr)[0] == 127) 1255 return(2); 1256 return(14); 1257 break; 1258 default: 1259 errno = EAFNOSUPPORT; /* is this a good error? */ 1260 return(-1); 1261 } 1262} 1263 1264/* 1265 * FILES (/etc/hosts) 1266 */ 1267 1268static FILE * 1269_files_open(int *errp) 1270{ 1271 FILE *fp; 1272 fp = fopen(_PATH_HOSTS, "r"); 1273 if (fp == NULL) 1274 *errp = NO_RECOVERY; 1275 return fp; 1276} 1277 1278static int 1279_files_ghbyname(void *rval, void *cb_data, va_list ap) 1280{ 1281 const char *name; 1282 int af; 1283 int *errp; 1284 int match, nalias; 1285 char *p, *line, *addrstr, *cname; 1286 FILE *fp; 1287 struct hostent *rethp, *hp, hpbuf; 1288 char *aliases[MAXALIASES + 1], *addrs[2]; 1289 union inx_addr addrbuf; 1290 char buf[BUFSIZ]; 1291 1292 name = va_arg(ap, const char *); 1293 af = va_arg(ap, int); 1294 errp = va_arg(ap, int *); 1295 1296 *(struct hostent **)rval = NULL; 1297 1298 if ((fp = _files_open(errp)) == NULL) 1299 return NS_UNAVAIL; 1300 rethp = hp = NULL; 1301 1302 while (fgets(buf, sizeof(buf), fp)) { 1303 line = buf; 1304 if ((addrstr = _hgetword(&line)) == NULL 1305 || (cname = _hgetword(&line)) == NULL) 1306 continue; 1307 match = (strcasecmp(cname, name) == 0); 1308 nalias = 0; 1309 while ((p = _hgetword(&line)) != NULL) { 1310 if (!match) 1311 match = (strcasecmp(p, name) == 0); 1312 if (nalias < MAXALIASES) 1313 aliases[nalias++] = p; 1314 } 1315 if (!match) 1316 continue; 1317 switch (af) { 1318 case AF_INET: 1319 if (inet_aton(addrstr, (struct in_addr *)&addrbuf) 1320 != 1) { 1321 *errp = NO_DATA; /* name found */ 1322 continue; 1323 } 1324 break; 1325#ifdef INET6 1326 case AF_INET6: 1327 if (inet_pton(af, addrstr, &addrbuf) != 1) { 1328 *errp = NO_DATA; /* name found */ 1329 continue; 1330 } 1331 break; 1332#endif 1333 } 1334 hp = &hpbuf; 1335 hp->h_name = cname; 1336 hp->h_aliases = aliases; 1337 aliases[nalias] = NULL; 1338 hp->h_addrtype = af; 1339 hp->h_length = ADDRLEN(af); 1340 hp->h_addr_list = addrs; 1341 addrs[0] = (char *)&addrbuf; 1342 addrs[1] = NULL; 1343 hp = _hpcopy(hp, errp); 1344 rethp = _hpmerge(rethp, hp, errp); 1345 } 1346 fclose(fp); 1347 *(struct hostent **)rval = rethp; 1348 return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1349} 1350 1351static int 1352_files_ghbyaddr(void *rval, void *cb_data, va_list ap) 1353{ 1354 const void *addr; 1355 int addrlen; 1356 int af; 1357 int *errp; 1358 int nalias; 1359 char *p, *line; 1360 FILE *fp; 1361 struct hostent *hp, hpbuf; 1362 char *aliases[MAXALIASES + 1], *addrs[2]; 1363 union inx_addr addrbuf; 1364 char buf[BUFSIZ]; 1365 1366 addr = va_arg(ap, const void *); 1367 addrlen = va_arg(ap, int); 1368 af = va_arg(ap, int); 1369 errp = va_arg(ap, int *); 1370 1371 *(struct hostent**)rval = NULL; 1372 1373 if ((fp = _files_open(errp)) == NULL) 1374 return NS_UNAVAIL; 1375 hp = NULL; 1376 while (fgets(buf, sizeof(buf), fp)) { 1377 line = buf; 1378 if ((p = _hgetword(&line)) == NULL 1379 || (af == AF_INET 1380 ? inet_aton(p, (struct in_addr *)&addrbuf) 1381 : inet_pton(af, p, &addrbuf)) != 1 1382 || memcmp(addr, &addrbuf, addrlen) != 0 1383 || (p = _hgetword(&line)) == NULL) 1384 continue; 1385 hp = &hpbuf; 1386 hp->h_name = p; 1387 hp->h_aliases = aliases; 1388 nalias = 0; 1389 while ((p = _hgetword(&line)) != NULL) { 1390 if (nalias < MAXALIASES) 1391 aliases[nalias++] = p; 1392 } 1393 aliases[nalias] = NULL; 1394 hp->h_addrtype = af; 1395 hp->h_length = addrlen; 1396 hp->h_addr_list = addrs; 1397 addrs[0] = (char *)&addrbuf; 1398 addrs[1] = NULL; 1399 hp = _hpcopy(hp, errp); 1400 break; 1401 } 1402 fclose(fp); 1403 *(struct hostent **)rval = hp; 1404 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1405} 1406 1407#ifdef YP 1408/* 1409 * NIS 1410 * 1411 * XXX actually a hack, these are INET4 specific. 1412 */ 1413static int 1414_nis_ghbyname(void *rval, void *cb_data, va_list ap) 1415{ 1416 const char *name; 1417 int af; 1418 int *errp; 1419 struct hostent *hp = NULL; 1420 1421 name = va_arg(ap, const char *); 1422 af = va_arg(ap, int); 1423 errp = va_arg(ap, int *); 1424 1425 if (af == AF_INET) { 1426 THREAD_LOCK(); 1427 hp = _gethostbynisname(name, af); 1428 if (hp != NULL) 1429 hp = _hpcopy(hp, errp); 1430 THREAD_UNLOCK(); 1431 } 1432 1433 *(struct hostent **)rval = hp; 1434 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1435 1436} 1437 1438static int 1439_nis_ghbyaddr(void *rval, void *cb_data, va_list ap) 1440{ 1441 const void *addr; 1442 int addrlen; 1443 int af; 1444 int *errp; 1445 struct hostent *hp = NULL; 1446 1447 addr = va_arg(ap, const void *); 1448 addrlen = va_arg(ap, int); 1449 af = va_arg(ap, int); 1450 1451 if (af == AF_INET) { 1452 THREAD_LOCK(); 1453 hp = _gethostbynisaddr(addr, addrlen, af); 1454 if (hp != NULL) 1455 hp = _hpcopy(hp, errp); 1456 THREAD_UNLOCK(); 1457 } 1458 *(struct hostent **)rval = hp; 1459 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1460} 1461#endif 1462 1463#define MAXPACKET (64*1024) 1464 1465typedef union { 1466 HEADER hdr; 1467 u_char buf[MAXPACKET]; 1468} querybuf; 1469 1470static struct hostent *getanswer(const querybuf *, int, const char *, int, 1471 struct hostent *, int *); 1472 1473/* 1474 * we don't need to take care about sorting, nor IPv4 mapped address here. 1475 */ 1476static struct hostent * 1477getanswer(answer, anslen, qname, qtype, template, errp) 1478 const querybuf *answer; 1479 int anslen; 1480 const char *qname; 1481 int qtype; 1482 struct hostent *template; 1483 int *errp; 1484{ 1485 const HEADER *hp; 1486 const u_char *cp; 1487 int n; 1488 const u_char *eom, *erdata; 1489 char *bp, *ep, **ap, **hap; 1490 int type, class, ancount, qdcount; 1491 int haveanswer, had_error; 1492 char tbuf[MAXDNAME]; 1493 const char *tname; 1494 int (*name_ok)(const char *); 1495 static char *h_addr_ptrs[MAXADDRS + 1]; 1496 static char *host_aliases[MAXALIASES]; 1497 static char hostbuf[8*1024]; 1498 1499#define BOUNDED_INCR(x) \ 1500 do { \ 1501 cp += x; \ 1502 if (cp > eom) { \ 1503 *errp = NO_RECOVERY; \ 1504 return (NULL); \ 1505 } \ 1506 } while (0) 1507 1508#define BOUNDS_CHECK(ptr, count) \ 1509 do { \ 1510 if ((ptr) + (count) > eom) { \ 1511 *errp = NO_RECOVERY; \ 1512 return (NULL); \ 1513 } \ 1514 } while (0) 1515 1516/* XXX do {} while (0) cannot be put here */ 1517#define DNS_ASSERT(x) \ 1518 { \ 1519 if (!(x)) { \ 1520 cp += n; \ 1521 continue; \ 1522 } \ 1523 } 1524 1525/* XXX do {} while (0) cannot be put here */ 1526#define DNS_FATAL(x) \ 1527 { \ 1528 if (!(x)) { \ 1529 had_error++; \ 1530 continue; \ 1531 } \ 1532 } 1533 1534 tname = qname; 1535 template->h_name = NULL; 1536 eom = answer->buf + anslen; 1537 switch (qtype) { 1538 case T_A: 1539 case T_AAAA: 1540 name_ok = res_hnok; 1541 break; 1542 case T_PTR: 1543 name_ok = res_dnok; 1544 break; 1545 default: 1546 return (NULL); /* XXX should be abort(); */ 1547 } 1548 /* 1549 * find first satisfactory answer 1550 */ 1551 hp = &answer->hdr; 1552 ancount = ntohs(hp->ancount); 1553 qdcount = ntohs(hp->qdcount); 1554 bp = hostbuf; 1555 ep = hostbuf + sizeof hostbuf; 1556 cp = answer->buf; 1557 BOUNDED_INCR(HFIXEDSZ); 1558 if (qdcount != 1) { 1559 *errp = NO_RECOVERY; 1560 return (NULL); 1561 } 1562 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1563 if ((n < 0) || !(*name_ok)(bp)) { 1564 *errp = NO_RECOVERY; 1565 return (NULL); 1566 } 1567 BOUNDED_INCR(n + QFIXEDSZ); 1568 if (qtype == T_A || qtype == T_AAAA) { 1569 /* res_send() has already verified that the query name is the 1570 * same as the one we sent; this just gets the expanded name 1571 * (i.e., with the succeeding search-domain tacked on). 1572 */ 1573 n = strlen(bp) + 1; /* for the \0 */ 1574 if (n >= MAXHOSTNAMELEN) { 1575 *errp = NO_RECOVERY; 1576 return (NULL); 1577 } 1578 template->h_name = bp; 1579 bp += n; 1580 /* The qname can be abbreviated, but h_name is now absolute. */ 1581 qname = template->h_name; 1582 } 1583 ap = host_aliases; 1584 *ap = NULL; 1585 template->h_aliases = host_aliases; 1586 hap = h_addr_ptrs; 1587 *hap = NULL; 1588 template->h_addr_list = h_addr_ptrs; 1589 haveanswer = 0; 1590 had_error = 0; 1591 while (ancount-- > 0 && cp < eom && !had_error) { 1592 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1593 DNS_FATAL(n >= 0); 1594 DNS_FATAL((*name_ok)(bp)); 1595 cp += n; /* name */ 1596 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 1597 type = _getshort(cp); 1598 cp += INT16SZ; /* type */ 1599 class = _getshort(cp); 1600 cp += INT16SZ + INT32SZ; /* class, TTL */ 1601 n = _getshort(cp); 1602 cp += INT16SZ; /* len */ 1603 BOUNDS_CHECK(cp, n); 1604 erdata = cp + n; 1605 DNS_ASSERT(class == C_IN); 1606 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { 1607 if (ap >= &host_aliases[MAXALIASES-1]) 1608 continue; 1609 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1610 DNS_FATAL(n >= 0); 1611 DNS_FATAL((*name_ok)(tbuf)); 1612 cp += n; 1613 if (cp != erdata) { 1614 *errp = NO_RECOVERY; 1615 return (NULL); 1616 } 1617 /* Store alias. */ 1618 *ap++ = bp; 1619 n = strlen(bp) + 1; /* for the \0 */ 1620 DNS_FATAL(n < MAXHOSTNAMELEN); 1621 bp += n; 1622 /* Get canonical name. */ 1623 n = strlen(tbuf) + 1; /* for the \0 */ 1624 DNS_FATAL(n <= ep - bp); 1625 DNS_FATAL(n < MAXHOSTNAMELEN); 1626 strcpy(bp, tbuf); 1627 template->h_name = bp; 1628 bp += n; 1629 continue; 1630 } 1631 if (qtype == T_PTR && type == T_CNAME) { 1632 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1633 if (n < 0 || !res_dnok(tbuf)) { 1634 had_error++; 1635 continue; 1636 } 1637 cp += n; 1638 if (cp != erdata) { 1639 *errp = NO_RECOVERY; 1640 return (NULL); 1641 } 1642 /* Get canonical name. */ 1643 n = strlen(tbuf) + 1; /* for the \0 */ 1644 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1645 had_error++; 1646 continue; 1647 } 1648 strcpy(bp, tbuf); 1649 tname = bp; 1650 bp += n; 1651 continue; 1652 } 1653 DNS_ASSERT(type == qtype); 1654 switch (type) { 1655 case T_PTR: 1656 DNS_ASSERT(strcasecmp(tname, bp) == 0); 1657 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1658 DNS_FATAL(n >= 0); 1659 DNS_FATAL(res_hnok(bp)); 1660#if MULTI_PTRS_ARE_ALIASES 1661 cp += n; 1662 if (cp != erdata) { 1663 *errp = NO_RECOVERY; 1664 return (NULL); 1665 } 1666 if (!haveanswer) 1667 template->h_name = bp; 1668 else if (ap < &host_aliases[MAXALIASES-1]) 1669 *ap++ = bp; 1670 else 1671 n = -1; 1672 if (n != -1) { 1673 n = strlen(bp) + 1; /* for the \0 */ 1674 if (n >= MAXHOSTNAMELEN) { 1675 had_error++; 1676 break; 1677 } 1678 bp += n; 1679 } 1680 break; 1681#else 1682 template->h_name = bp; 1683 *errp = NETDB_SUCCESS; 1684 return (template); 1685#endif 1686 case T_A: 1687 case T_AAAA: 1688 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0); 1689 DNS_ASSERT(n == template->h_length); 1690 if (!haveanswer) { 1691 int nn; 1692 1693 template->h_name = bp; 1694 nn = strlen(bp) + 1; /* for the \0 */ 1695 bp += nn; 1696 } 1697 bp = (char *)ALIGN(bp); 1698 1699 DNS_FATAL(bp + n < ep); 1700 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]); 1701#ifdef FILTER_V4MAPPED 1702 if (type == T_AAAA) { 1703 struct in6_addr in6; 1704 memcpy(&in6, cp, sizeof(in6)); 1705 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0); 1706 } 1707#endif 1708 bcopy(cp, *hap++ = bp, n); 1709 bp += n; 1710 cp += n; 1711 if (cp != erdata) { 1712 *errp = NO_RECOVERY; 1713 return (NULL); 1714 } 1715 break; 1716 default: 1717 abort(); 1718 } 1719 if (!had_error) 1720 haveanswer++; 1721 } 1722 if (haveanswer) { 1723 *ap = NULL; 1724 *hap = NULL; 1725 if (!template->h_name) { 1726 n = strlen(qname) + 1; /* for the \0 */ 1727 if (n > ep - bp || n >= MAXHOSTNAMELEN) 1728 goto no_recovery; 1729 strcpy(bp, qname); 1730 template->h_name = bp; 1731 bp += n; 1732 } 1733 *errp = NETDB_SUCCESS; 1734 return (template); 1735 } 1736 no_recovery: 1737 *errp = NO_RECOVERY; 1738 return (NULL); 1739 1740#undef BOUNDED_INCR 1741#undef BOUNDS_CHECK 1742#undef DNS_ASSERT 1743#undef DNS_FATAL 1744} 1745 1746static int 1747_dns_ghbyname(void *rval, void *cb_data, va_list ap) 1748{ 1749 const char *name; 1750 int af; 1751 int *errp; 1752 int n; 1753 struct hostent *hp; 1754 int qtype; 1755 struct hostent hbuf; 1756 querybuf *buf; 1757 1758 name = va_arg(ap, const char *); 1759 af = va_arg(ap, int); 1760 errp = va_arg(ap, int *); 1761 1762 if ((_res.options & RES_INIT) == 0) { 1763 if (res_init() < 0) { 1764 *errp = h_errno; 1765 return NS_UNAVAIL; 1766 } 1767 } 1768 memset(&hbuf, 0, sizeof(hbuf)); 1769 hbuf.h_addrtype = af; 1770 hbuf.h_length = ADDRLEN(af); 1771 1772 switch (af) { 1773#ifdef INET6 1774 case AF_INET6: 1775 qtype = T_AAAA; 1776 break; 1777#endif 1778 case AF_INET: 1779 qtype = T_A; 1780 break; 1781 default: 1782 *errp = NO_RECOVERY; 1783 return NS_NOTFOUND; 1784 } 1785 buf = malloc(sizeof(*buf)); 1786 if (buf == NULL) { 1787 *errp = NETDB_INTERNAL; 1788 return NS_UNAVAIL; 1789 } 1790 n = res_search(name, C_IN, qtype, buf->buf, sizeof(buf->buf)); 1791 if (n < 0) { 1792 free(buf); 1793 *errp = h_errno; 1794 return NS_UNAVAIL; 1795 } 1796 hp = getanswer(buf, n, name, qtype, &hbuf, errp); 1797 free(buf); 1798 if (!hp) { 1799 *errp = NO_RECOVERY; 1800 return NS_NOTFOUND; 1801 } 1802 *(struct hostent **)rval = _hpcopy(&hbuf, errp); 1803 if (*(struct hostent **)rval != NULL) 1804 return NS_SUCCESS; 1805 else if (*errp == TRY_AGAIN) 1806 return NS_TRYAGAIN; 1807 else 1808 return NS_NOTFOUND; 1809} 1810 1811static int 1812_dns_ghbyaddr(void *rval, void *cb_data, va_list ap) 1813{ 1814 const void *addr; 1815 int addrlen; 1816 int af; 1817 int *errp; 1818 int n; 1819 int err; 1820 struct hostent *hp; 1821 u_char c, *cp; 1822 char *bp; 1823 struct hostent hbuf; 1824 int na; 1825#ifdef INET6 1826 static const char hex[] = "0123456789abcdef"; 1827#endif 1828 querybuf *buf; 1829 char qbuf[MAXDNAME+1]; 1830 char *hlist[2]; 1831 char *tld6[] = { "ip6.arpa", NULL }; 1832 char *tld4[] = { "in-addr.arpa", NULL }; 1833 char **tld; 1834 1835 addr = va_arg(ap, const void *); 1836 addrlen = va_arg(ap, int); 1837 af = va_arg(ap, int); 1838 errp = va_arg(ap, int *); 1839 1840 *(struct hostent **)rval = NULL; 1841 1842#ifdef INET6 1843 /* XXX */ 1844 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr)) 1845 return NS_NOTFOUND; 1846#endif 1847 1848 switch (af) { 1849#ifdef INET6 1850 case AF_INET6: 1851 tld = tld6; 1852 break; 1853#endif 1854 case AF_INET: 1855 tld = tld4; 1856 break; 1857 default: 1858 return NS_NOTFOUND; 1859 } 1860 1861 if ((_res.options & RES_INIT) == 0) { 1862 if (res_init() < 0) { 1863 *errp = h_errno; 1864 return NS_UNAVAIL; 1865 } 1866 } 1867 memset(&hbuf, 0, sizeof(hbuf)); 1868 hbuf.h_name = NULL; 1869 hbuf.h_addrtype = af; 1870 hbuf.h_length = addrlen; 1871 na = 0; 1872 1873 buf = malloc(sizeof(*buf)); 1874 if (buf == NULL) { 1875 *errp = NETDB_INTERNAL; 1876 return NS_UNAVAIL; 1877 } 1878 err = NS_SUCCESS; 1879 for (/* nothing */; *tld; tld++) { 1880 /* 1881 * XXX assumes that MAXDNAME is big enough - error checks 1882 * has been made by callers 1883 */ 1884 n = 0; 1885 bp = qbuf; 1886 cp = (u_char *)addr+addrlen-1; 1887 switch (af) { 1888#ifdef INET6 1889 case AF_INET6: 1890 for (; n < addrlen; n++, cp--) { 1891 c = *cp; 1892 *bp++ = hex[c & 0xf]; 1893 *bp++ = '.'; 1894 *bp++ = hex[c >> 4]; 1895 *bp++ = '.'; 1896 } 1897 strcpy(bp, *tld); 1898 break; 1899#endif 1900 case AF_INET: 1901 for (; n < addrlen; n++, cp--) { 1902 c = *cp; 1903 if (c >= 100) 1904 *bp++ = '0' + c / 100; 1905 if (c >= 10) 1906 *bp++ = '0' + (c % 100) / 10; 1907 *bp++ = '0' + c % 10; 1908 *bp++ = '.'; 1909 } 1910 strcpy(bp, *tld); 1911 break; 1912 } 1913 1914 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf); 1915 if (n < 0) { 1916 *errp = h_errno; 1917 err = NS_UNAVAIL; 1918 continue; 1919 } else if (n > sizeof(buf->buf)) { 1920#if 0 1921 errno = ERANGE; /* XXX is it OK to set errno here? */ 1922#endif 1923 *errp = NETDB_INTERNAL; 1924 err = NS_UNAVAIL; 1925 continue; 1926 } 1927 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp); 1928 if (!hp) { 1929 err = NS_NOTFOUND; 1930 continue; 1931 } 1932 free(buf); 1933 hbuf.h_addrtype = af; 1934 hbuf.h_length = addrlen; 1935 hbuf.h_addr_list = hlist; 1936 hlist[0] = (char *)addr; 1937 hlist[1] = NULL; 1938 *(struct hostent **)rval = _hpcopy(&hbuf, errp); 1939 return NS_SUCCESS; 1940 } 1941 free(buf); 1942 return err; 1943} 1944 1945static void 1946_dns_shent(int stayopen) 1947{ 1948 if ((_res.options & RES_INIT) == 0) { 1949 if (res_init() < 0) 1950 return; 1951 } 1952 if (stayopen) 1953 _res.options |= RES_STAYOPEN | RES_USEVC; 1954} 1955 1956static void 1957_dns_ehent(void) 1958{ 1959 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 1960 res_close(); 1961} 1962 1963#ifdef ICMPNL 1964 1965/* 1966 * experimental: 1967 * draft-ietf-ipngwg-icmp-namelookups-02.txt 1968 * ifindex is assumed to be encoded in addr. 1969 */ 1970#include <sys/uio.h> 1971#include <netinet/ip6.h> 1972#include <netinet/icmp6.h> 1973 1974struct _icmp_host_cache { 1975 struct _icmp_host_cache *hc_next; 1976 int hc_ifindex; 1977 struct in6_addr hc_addr; 1978 char *hc_name; 1979}; 1980 1981static char * 1982_icmp_fqdn_query(const struct in6_addr *addr, int ifindex) 1983{ 1984 int s; 1985 struct icmp6_filter filter; 1986 struct msghdr msg; 1987 struct cmsghdr *cmsg; 1988 struct in6_pktinfo *pkt; 1989 char cbuf[256]; 1990 char buf[1024]; 1991 int cc; 1992 struct icmp6_fqdn_query *fq; 1993 struct icmp6_fqdn_reply *fr; 1994 struct _icmp_host_cache *hc; 1995 struct sockaddr_in6 sin6; 1996 struct iovec iov; 1997 fd_set s_fds, fds; 1998 struct timeval tout; 1999 int len; 2000 char *name; 2001 static struct _icmp_host_cache *hc_head; 2002 2003 THREAD_LOCK(); 2004 for (hc = hc_head; hc; hc = hc->hc_next) { 2005 if (hc->hc_ifindex == ifindex 2006 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr)) { 2007 THREAD_UNLOCK(); 2008 return hc->hc_name; /* XXX: never freed */ 2009 } 2010 } 2011 2012 ICMP6_FILTER_SETBLOCKALL(&filter); 2013 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter); 2014 2015 FD_ZERO(&s_fds); 2016 tout.tv_sec = 0; 2017 tout.tv_usec = 200000; /*XXX: 200ms*/ 2018 2019 fq = (struct icmp6_fqdn_query *)buf; 2020 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY; 2021 fq->icmp6_fqdn_code = 0; 2022 fq->icmp6_fqdn_cksum = 0; 2023 fq->icmp6_fqdn_id = (u_short)getpid(); 2024 fq->icmp6_fqdn_unused = 0; 2025 fq->icmp6_fqdn_cookie[0] = 0; 2026 fq->icmp6_fqdn_cookie[1] = 0; 2027 2028 memset(&sin6, 0, sizeof(sin6)); 2029 sin6.sin6_family = AF_INET6; 2030 sin6.sin6_addr = *addr; 2031 2032 memset(&msg, 0, sizeof(msg)); 2033 msg.msg_name = (caddr_t)&sin6; 2034 msg.msg_namelen = sizeof(sin6); 2035 msg.msg_iov = &iov; 2036 msg.msg_iovlen = 1; 2037 msg.msg_control = NULL; 2038 msg.msg_controllen = 0; 2039 iov.iov_base = (caddr_t)buf; 2040 iov.iov_len = sizeof(struct icmp6_fqdn_query); 2041 2042 if (ifindex) { 2043 msg.msg_control = cbuf; 2044 msg.msg_controllen = sizeof(cbuf); 2045 cmsg = CMSG_FIRSTHDR(&msg); 2046 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 2047 cmsg->cmsg_level = IPPROTO_IPV6; 2048 cmsg->cmsg_type = IPV6_PKTINFO; 2049 pkt = (struct in6_pktinfo *)&cmsg[1]; 2050 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr)); 2051 pkt->ipi6_ifindex = ifindex; 2052 cmsg = CMSG_NXTHDR(&msg, cmsg); 2053 msg.msg_controllen = (char *)cmsg - cbuf; 2054 } 2055 2056 if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) 2057 return NULL; 2058 (void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, 2059 (char *)&filter, sizeof(filter)); 2060 cc = _sendmsg(s, &msg, 0); 2061 if (cc < 0) { 2062 _close(s); 2063 return NULL; 2064 } 2065 FD_SET(s, &s_fds); 2066 for (;;) { 2067 fds = s_fds; 2068 if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) { 2069 _close(s); 2070 return NULL; 2071 } 2072 len = sizeof(sin6); 2073 cc = _recvfrom(s, buf, sizeof(buf), 0, 2074 (struct sockaddr *)&sin6, &len); 2075 if (cc <= 0) { 2076 _close(s); 2077 return NULL; 2078 } 2079 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) 2080 continue; 2081 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr)) 2082 continue; 2083 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr)); 2084 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY) 2085 break; 2086 } 2087 _close(s); 2088 if (fr->icmp6_fqdn_cookie[1] != 0) { 2089 /* rfc1788 type */ 2090 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4; 2091 len = (buf + cc) - name; 2092 } else { 2093 len = fr->icmp6_fqdn_namelen; 2094 name = fr->icmp6_fqdn_name; 2095 } 2096 if (len <= 0) 2097 return NULL; 2098 name[len] = 0; 2099 2100 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL) 2101 return NULL; 2102 /* XXX: limit number of cached entries */ 2103 hc->hc_ifindex = ifindex; 2104 hc->hc_addr = *addr; 2105 hc->hc_name = strdup(name); 2106 THREAD_LOCK(); 2107 hc->hc_next = hc_head; 2108 hc_head = hc; 2109 THREAD_UNLOCK(); 2110 return hc->hc_name; 2111} 2112 2113static struct hostent * 2114_icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp) 2115{ 2116 char *hname; 2117 int ifindex; 2118 struct in6_addr addr6; 2119 2120 if (af != AF_INET6) { 2121 /* 2122 * Note: rfc1788 defines Who Are You for IPv4, 2123 * but no one implements it. 2124 */ 2125 return NULL; 2126 } 2127 2128 memcpy(&addr6, addr, addrlen); 2129 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3]; 2130 addr6.s6_addr[2] = addr6.s6_addr[3] = 0; 2131 2132 if (!IN6_IS_ADDR_LINKLOCAL(&addr6)) 2133 return NULL; /*XXX*/ 2134 2135 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL) 2136 return NULL; 2137 return _hpaddr(af, hname, &addr6, errp); 2138} 2139#endif /* ICMPNL */ 2140