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$"); 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 106 #include <arpa/inet.h> 107 #include <arpa/nameser.h> 108 109 #include <errno.h> 110 #include <netdb.h> 111 #include <resolv.h> 112 #include <stdio.h> 113 #include <stdlib.h> 114 #include <string.h> 115 #include <stdarg.h> 116 #include <nsswitch.h> 117 #include <pthread.h> 118 #include <unistd.h> 119 #include "un-namespace.h" 120 121 #ifndef _PATH_HOSTS 122 #define _PATH_HOSTS "/etc/hosts" 123 #endif 124 125 #ifndef MAXALIASES 126 #define MAXALIASES 10 127 #endif 128 #ifndef MAXADDRS 129 #define MAXADDRS 20 130 #endif 131 #ifndef MAXDNAME 132 #define MAXDNAME 1025 133 #endif 134 135 #ifdef INET6 136 #define ADDRLEN(af) ((af) == AF_INET6 ? sizeof(struct in6_addr) : \ 137 sizeof(struct in_addr)) 138 #else 139 #define ADDRLEN(af) sizeof(struct in_addr) 140 #endif 141 142 #define MAPADDR(ab, ina) \ 143 do { \ 144 memcpy(&(ab)->map_inaddr, ina, sizeof(struct in_addr)); \ 145 memset((ab)->map_zero, 0, sizeof((ab)->map_zero)); \ 146 memset((ab)->map_one, 0xff, sizeof((ab)->map_one)); \ 147 } while (0) 148 #define MAPADDRENABLED(flags) \ 149 (((flags) & AI_V4MAPPED) || \ 150 (((flags) & AI_V4MAPPED_CFG) && _mapped_addr_enabled())) 151 152 union inx_addr { 153 struct in_addr in_addr; 154 #ifdef INET6 155 struct in6_addr in6_addr; 156 #endif 157 struct { 158 u_char mau_zero[10]; 159 u_char mau_one[2]; 160 struct in_addr mau_inaddr; 161 } map_addr_un; 162 #define map_zero map_addr_un.mau_zero 163 #define map_one map_addr_un.mau_one 164 #define map_inaddr map_addr_un.mau_inaddr 165 }; 166 167 static struct hostent *_hpcopy(struct hostent *hp, int *errp); 168 static struct hostent *_hpaddr(int af, const char *name, void *addr, int *errp); 169 static struct hostent *_hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp); 170 #ifdef INET6 171 static struct hostent *_hpmapv6(struct hostent *hp, int *errp); 172 #endif 173 static struct hostent *_hpsort(struct hostent *hp); 174 static struct hostent *_ghbyname(const char *name, int af, int flags, int *errp); 175 static char *_hgetword(char **pp); 176 static int _mapped_addr_enabled(void); 177 178 static FILE *_files_open(int *errp); 179 static int _files_ghbyname(void *, void *, va_list); 180 static int _files_ghbyaddr(void *, void *, va_list); 181 #ifdef YP 182 static int _nis_ghbyname(void *, void *, va_list); 183 static int _nis_ghbyaddr(void *, void *, va_list); 184 #endif 185 static int _dns_ghbyname(void *, void *, va_list); 186 static int _dns_ghbyaddr(void *, void *, va_list); 187 static void _dns_shent(int stayopen) __unused; 188 static void _dns_ehent(void) __unused; 189 #ifdef ICMPNL 190 static int _icmp_ghbyaddr(void *, void *, va_list); 191 #endif /* ICMPNL */ 192 193 /* 194 * XXX: Our res_*() is not thread-safe. So, we share lock between 195 * getaddrinfo() and getipnodeby*(). Still, we cannot use 196 * getaddrinfo() and getipnodeby*() in conjunction with other 197 * functions which call res_*(). 198 */ 199 #include "libc_private.h" 200 extern pthread_mutex_t __getaddrinfo_thread_lock; 201 #define THREAD_LOCK() \ 202 if (__isthreaded) _pthread_mutex_lock(&__getaddrinfo_thread_lock); 203 #define THREAD_UNLOCK() \ 204 if (__isthreaded) _pthread_mutex_unlock(&__getaddrinfo_thread_lock); 205 206 /* Host lookup order if nsswitch.conf is broken or nonexistant */ 207 static const ns_src default_src[] = { 208 { NSSRC_FILES, NS_SUCCESS }, 209 { NSSRC_DNS, NS_SUCCESS }, 210 #ifdef ICMPNL 211 #define NSSRC_ICMP "icmp" 212 { NSSRC_ICMP, NS_SUCCESS }, 213 #endif 214 { 0 } 215 }; 216 217 /* 218 * Check if kernel supports mapped address. 219 * implementation dependent 220 */ 221 #ifdef __KAME__ 222 #include <sys/sysctl.h> 223 #endif /* __KAME__ */ 224 225 static int 226 _mapped_addr_enabled(void) 227 { 228 /* implementation dependent check */ 229 #if defined(__KAME__) && defined(IPV6CTL_MAPPED_ADDR) 230 int mib[4]; 231 size_t len; 232 int val; 233 234 mib[0] = CTL_NET; 235 mib[1] = PF_INET6; 236 mib[2] = IPPROTO_IPV6; 237 mib[3] = IPV6CTL_MAPPED_ADDR; 238 len = sizeof(val); 239 if (sysctl(mib, 4, &val, &len, 0, 0) == 0 && val != 0) 240 return 1; 241 #endif /* __KAME__ && IPV6CTL_MAPPED_ADDR */ 242 return 0; 243 } 244 245 /* 246 * Functions defined in RFC2553 247 * getipnodebyname, getipnodebyaddr, freehostent 248 */ 249 250 static struct hostent * 251 _ghbyname(const char *name, int af, int flags, int *errp) 252 { 253 struct hostent *hp; 254 int rval; 255 256 static const ns_dtab dtab[] = { 257 NS_FILES_CB(_files_ghbyname, NULL) 258 { NSSRC_DNS, _dns_ghbyname, NULL }, 259 NS_NIS_CB(_nis_ghbyname, NULL) 260 { 0 } 261 }; 262 263 if (flags & AI_ADDRCONFIG) { 264 int s; 265 266 /* 267 * TODO: 268 * Note that implementation dependent test for address 269 * configuration should be done everytime called 270 * (or apropriate interval), 271 * because addresses will be dynamically assigned or deleted. 272 */ 273 if (af == AF_UNSPEC) { 274 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 275 af = AF_INET; 276 else { 277 _close(s); 278 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 279 af = AF_INET6; 280 else 281 _close(s); 282 } 283 284 } 285 if (af != AF_UNSPEC) { 286 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 287 return NULL; 288 _close(s); 289 } 290 } 291 292 THREAD_LOCK(); 293 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyname", default_src, 294 name, af, errp); 295 THREAD_UNLOCK(); 296 return (rval == NS_SUCCESS) ? hp : NULL; 297 } 298 299 /* getipnodebyname() internal routine for multiple query(PF_UNSPEC) support. */ 300 struct hostent * 301 _getipnodebyname_multi(const char *name, int af, int flags, int *errp) 302 { 303 struct hostent *hp; 304 union inx_addr addrbuf; 305 306 /* XXX: PF_UNSPEC is only supposed to be passed from getaddrinfo() */ 307 if (af != AF_INET 308 #ifdef INET6 309 && af != AF_INET6 310 #endif 311 && af != PF_UNSPEC 312 ) 313 { 314 *errp = NO_RECOVERY; 315 return NULL; 316 } 317 318 #ifdef INET6 319 /* special case for literal address */ 320 if (inet_pton(AF_INET6, name, &addrbuf) == 1) { 321 if (af != AF_INET6) { 322 *errp = HOST_NOT_FOUND; 323 return NULL; 324 } 325 return _hpaddr(af, name, &addrbuf, errp); 326 } 327 #endif 328 if (inet_aton(name, (struct in_addr *)&addrbuf) == 1) { 329 if (af != AF_INET) { 330 if (MAPADDRENABLED(flags)) { 331 MAPADDR(&addrbuf, &addrbuf.in_addr); 332 } else { 333 *errp = HOST_NOT_FOUND; 334 return NULL; 335 } 336 } 337 return _hpaddr(af, name, &addrbuf, errp); 338 } 339 340 *errp = HOST_NOT_FOUND; 341 hp = _ghbyname(name, af, flags, errp); 342 343 #ifdef INET6 344 if (af == AF_INET6 345 && ((flags & AI_ALL) || hp == NULL) 346 && (MAPADDRENABLED(flags))) { 347 struct hostent *hp2 = _ghbyname(name, AF_INET, flags, errp); 348 if (hp == NULL) 349 hp = _hpmapv6(hp2, errp); 350 else { 351 if (hp2 && strcmp(hp->h_name, hp2->h_name) != 0) { 352 freehostent(hp2); 353 hp2 = NULL; 354 } 355 hp = _hpmerge(hp, hp2, errp); 356 } 357 } 358 #endif 359 return _hpsort(hp); 360 } 361 362 struct hostent * 363 getipnodebyname(const char *name, int af, int flags, int *errp) 364 { 365 if (af != AF_INET 366 #ifdef INET6 367 && af != AF_INET6 368 #endif 369 ) 370 { 371 *errp = NO_RECOVERY; 372 return NULL; 373 } 374 return(_getipnodebyname_multi(name, af ,flags, errp)); 375 } 376 377 struct hostent * 378 getipnodebyaddr(const void *src, size_t len, int af, int *errp) 379 { 380 struct hostent *hp; 381 int rval; 382 #ifdef INET6 383 struct in6_addr addrbuf; 384 #else 385 struct in_addr addrbuf; 386 #endif 387 388 static const ns_dtab dtab[] = { 389 NS_FILES_CB(_files_ghbyaddr, NULL) 390 { NSSRC_DNS, _dns_ghbyaddr, NULL }, 391 NS_NIS_CB(_nis_ghbyaddr, NULL) 392 #ifdef ICMPNL 393 { NSSRC_ICMP, _icmp_ghbyaddr, NULL }, 394 #endif 395 { 0 } 396 }; 397 398 *errp = HOST_NOT_FOUND; 399 400 switch (af) { 401 case AF_INET: 402 if (len != sizeof(struct in_addr)) { 403 *errp = NO_RECOVERY; 404 return NULL; 405 } 406 if ((long)src & ~(sizeof(struct in_addr) - 1)) { 407 memcpy(&addrbuf, src, len); 408 src = &addrbuf; 409 } 410 if (((struct in_addr *)src)->s_addr == 0) 411 return NULL; 412 break; 413 #ifdef INET6 414 case AF_INET6: 415 if (len != sizeof(struct in6_addr)) { 416 *errp = NO_RECOVERY; 417 return NULL; 418 } 419 if ((long)src & ~(sizeof(struct in6_addr) / 2 - 1)) { /*XXX*/ 420 memcpy(&addrbuf, src, len); 421 src = &addrbuf; 422 } 423 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)src)) 424 return NULL; 425 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src) 426 || IN6_IS_ADDR_V4COMPAT((struct in6_addr *)src)) { 427 src = (char *)src + 428 (sizeof(struct in6_addr) - sizeof(struct in_addr)); 429 af = AF_INET; 430 len = sizeof(struct in_addr); 431 } 432 break; 433 #endif 434 default: 435 *errp = NO_RECOVERY; 436 return NULL; 437 } 438 439 THREAD_LOCK(); 440 rval = _nsdispatch(&hp, dtab, NSDB_HOSTS, "ghbyaddr", default_src, 441 src, len, af, errp); 442 THREAD_UNLOCK(); 443 return (rval == NS_SUCCESS) ? hp : NULL; 444 } 445 446 void 447 freehostent(struct hostent *ptr) 448 { 449 free(ptr); 450 } 451 452 #if 0 453 454 /* XXX: should be deprecated */ 455 struct hostent * 456 getnodebyname(const char *name, int af, int flags) 457 { 458 return getipnodebyname(name, af, flags, &h_errno); 459 } 460 461 #ifdef __warn_references 462 __warn_references(getnodebyname, 463 "warning: getnodebyname() deprecated, " 464 "should use getaddrinfo() or getipnodebyname()"); 465 #endif 466 467 struct hostent * 468 getnodebyaddr(const void *src, size_t len, int af) 469 { 470 return getipnodebyaddr(src, len, af, &h_errno); 471 } 472 473 #ifdef __warn_references 474 __warn_references(getnodebyaddr, 475 "warning: getnodebyaddr() deprecated, " 476 "should use getnameinfo() or getipnodebyaddr()"); 477 #endif 478 479 #endif 480 481 /* 482 * Private utility functions 483 */ 484 485 /* 486 * _hpcopy: allocate and copy hostent structure 487 */ 488 static struct hostent * 489 _hpcopy(struct hostent *hp, int *errp) 490 { 491 struct hostent *nhp; 492 char *cp, **pp; 493 int size, addrsize; 494 int nalias = 0, naddr = 0; 495 int al_off; 496 int i; 497 498 if (hp == NULL) 499 return hp; 500 501 /* count size to be allocated */ 502 size = sizeof(struct hostent); 503 if (hp->h_name != NULL) 504 size += strlen(hp->h_name) + 1; 505 if ((pp = hp->h_aliases) != NULL) { 506 for (i = 0; *pp != NULL; i++, pp++) { 507 if (**pp != '\0') { 508 size += strlen(*pp) + 1; 509 nalias++; 510 } 511 } 512 } 513 /* adjust alignment */ 514 size = ALIGN(size); 515 al_off = size; 516 size += sizeof(char *) * (nalias + 1); 517 addrsize = ALIGN(hp->h_length); 518 if ((pp = hp->h_addr_list) != NULL) { 519 while (*pp++ != NULL) 520 naddr++; 521 } 522 size += addrsize * naddr; 523 size += sizeof(char *) * (naddr + 1); 524 525 /* copy */ 526 if ((nhp = (struct hostent *)malloc(size)) == NULL) { 527 *errp = TRY_AGAIN; 528 return NULL; 529 } 530 cp = (char *)&nhp[1]; 531 if (hp->h_name != NULL) { 532 nhp->h_name = cp; 533 strcpy(cp, hp->h_name); 534 cp += strlen(cp) + 1; 535 } else 536 nhp->h_name = NULL; 537 nhp->h_aliases = (char **)((char *)nhp + al_off); 538 if ((pp = hp->h_aliases) != NULL) { 539 for (i = 0; *pp != NULL; pp++) { 540 if (**pp != '\0') { 541 nhp->h_aliases[i++] = cp; 542 strcpy(cp, *pp); 543 cp += strlen(cp) + 1; 544 } 545 } 546 } 547 nhp->h_aliases[nalias] = NULL; 548 cp = (char *)&nhp->h_aliases[nalias + 1]; 549 nhp->h_addrtype = hp->h_addrtype; 550 nhp->h_length = hp->h_length; 551 nhp->h_addr_list = (char **)cp; 552 if ((pp = hp->h_addr_list) != NULL) { 553 cp = (char *)&nhp->h_addr_list[naddr + 1]; 554 for (i = 0; *pp != NULL; pp++) { 555 nhp->h_addr_list[i++] = cp; 556 memcpy(cp, *pp, hp->h_length); 557 cp += addrsize; 558 } 559 } 560 nhp->h_addr_list[naddr] = NULL; 561 return nhp; 562 } 563 564 /* 565 * _hpaddr: construct hostent structure with one address 566 */ 567 static struct hostent * 568 _hpaddr(int af, const char *name, void *addr, int *errp) 569 { 570 struct hostent *hp, hpbuf; 571 char *addrs[2]; 572 573 hp = &hpbuf; 574 hp->h_name = (char *)name; 575 hp->h_aliases = NULL; 576 hp->h_addrtype = af; 577 hp->h_length = ADDRLEN(af); 578 hp->h_addr_list = addrs; 579 addrs[0] = (char *)addr; 580 addrs[1] = NULL; 581 return _hpcopy(hp, errp); 582 } 583 584 /* 585 * _hpmerge: merge 2 hostent structure, arguments will be freed 586 */ 587 static struct hostent * 588 _hpmerge(struct hostent *hp1, struct hostent *hp2, int *errp) 589 { 590 int i, j; 591 int naddr, nalias; 592 char **pp; 593 struct hostent *hp, hpbuf; 594 char *aliases[MAXALIASES + 1], *addrs[MAXADDRS + 1]; 595 union inx_addr addrbuf[MAXADDRS]; 596 597 if (hp1 == NULL) 598 return hp2; 599 if (hp2 == NULL) 600 return hp1; 601 602 #define HP(i) (i == 1 ? hp1 : hp2) 603 hp = &hpbuf; 604 hp->h_name = (hp1->h_name != NULL ? hp1->h_name : hp2->h_name); 605 hp->h_aliases = aliases; 606 nalias = 0; 607 for (i = 1; i <= 2; i++) { 608 if ((pp = HP(i)->h_aliases) == NULL) 609 continue; 610 for (; nalias < MAXALIASES && *pp != NULL; pp++) { 611 /* check duplicates */ 612 for (j = 0; j < nalias; j++) 613 if (strcasecmp(*pp, aliases[j]) == 0) 614 break; 615 if (j == nalias) 616 aliases[nalias++] = *pp; 617 } 618 } 619 aliases[nalias] = NULL; 620 #ifdef INET6 621 if (hp1->h_length != hp2->h_length) { 622 hp->h_addrtype = AF_INET6; 623 hp->h_length = sizeof(struct in6_addr); 624 } else { 625 #endif 626 hp->h_addrtype = hp1->h_addrtype; 627 hp->h_length = hp1->h_length; 628 #ifdef INET6 629 } 630 #endif 631 hp->h_addr_list = addrs; 632 naddr = 0; 633 for (i = 1; i <= 2; i++) { 634 if ((pp = HP(i)->h_addr_list) == NULL) 635 continue; 636 if (HP(i)->h_length == hp->h_length) { 637 while (naddr < MAXADDRS && *pp != NULL) 638 addrs[naddr++] = *pp++; 639 } else { 640 /* copy IPv4 addr as mapped IPv6 addr */ 641 while (naddr < MAXADDRS && *pp != NULL) { 642 MAPADDR(&addrbuf[naddr], *pp++); 643 addrs[naddr] = (char *)&addrbuf[naddr]; 644 naddr++; 645 } 646 } 647 } 648 addrs[naddr] = NULL; 649 hp = _hpcopy(hp, errp); 650 freehostent(hp1); 651 freehostent(hp2); 652 return hp; 653 } 654 655 /* 656 * _hpmapv6: convert IPv4 hostent into IPv4-mapped IPv6 addresses 657 */ 658 #ifdef INET6 659 static struct hostent * 660 _hpmapv6(struct hostent *hp, int *errp) 661 { 662 struct hostent *hp6; 663 664 if (hp == NULL) 665 return NULL; 666 if (hp->h_addrtype == AF_INET6) 667 return hp; 668 669 /* make dummy hostent to convert IPv6 address */ 670 if ((hp6 = (struct hostent *)malloc(sizeof(struct hostent))) == NULL) { 671 *errp = TRY_AGAIN; 672 return NULL; 673 } 674 hp6->h_name = NULL; 675 hp6->h_aliases = NULL; 676 hp6->h_addrtype = AF_INET6; 677 hp6->h_length = sizeof(struct in6_addr); 678 hp6->h_addr_list = NULL; 679 return _hpmerge(hp6, hp, errp); 680 } 681 #endif 682 683 /* 684 * _hpsort: sort address by sortlist 685 */ 686 static struct hostent * 687 _hpsort(struct hostent *hp) 688 { 689 int i, j, n; 690 u_char *ap, *sp, *mp, **pp; 691 char t; 692 char order[MAXADDRS]; 693 int nsort = _res.nsort; 694 695 if (hp == NULL || hp->h_addr_list[1] == NULL || nsort == 0) 696 return hp; 697 for (i = 0; (ap = (u_char *)hp->h_addr_list[i]); i++) { 698 for (j = 0; j < nsort; j++) { 699 #ifdef INET6 700 if (_res_ext.sort_list[j].af != hp->h_addrtype) 701 continue; 702 sp = (u_char *)&_res_ext.sort_list[j].addr; 703 mp = (u_char *)&_res_ext.sort_list[j].mask; 704 #else 705 sp = (u_char *)&_res.sort_list[j].addr; 706 mp = (u_char *)&_res.sort_list[j].mask; 707 #endif 708 for (n = 0; n < hp->h_length; n++) { 709 if ((ap[n] & mp[n]) != sp[n]) 710 break; 711 } 712 if (n == hp->h_length) 713 break; 714 } 715 order[i] = j; 716 } 717 n = i; 718 pp = (u_char **)hp->h_addr_list; 719 for (i = 0; i < n - 1; i++) { 720 for (j = i + 1; j < n; j++) { 721 if (order[i] > order[j]) { 722 ap = pp[i]; 723 pp[i] = pp[j]; 724 pp[j] = ap; 725 t = order[i]; 726 order[i] = order[j]; 727 order[j] = t; 728 } 729 } 730 } 731 return hp; 732 } 733 734 static char * 735 _hgetword(char **pp) 736 { 737 char c, *p, *ret; 738 const char *sp; 739 static const char sep[] = "# \t\n"; 740 741 ret = NULL; 742 for (p = *pp; (c = *p) != '\0'; p++) { 743 for (sp = sep; *sp != '\0'; sp++) { 744 if (c == *sp) 745 break; 746 } 747 if (c == '#') 748 p[1] = '\0'; /* ignore rest of line */ 749 if (ret == NULL) { 750 if (*sp == '\0') 751 ret = p; 752 } else { 753 if (*sp != '\0') { 754 *p++ = '\0'; 755 break; 756 } 757 } 758 } 759 *pp = p; 760 if (ret == NULL || *ret == '\0') 761 return NULL; 762 return ret; 763 } 764 765 /* 766 * FILES (/etc/hosts) 767 */ 768 769 static FILE * 770 _files_open(int *errp) 771 { 772 FILE *fp; 773 fp = fopen(_PATH_HOSTS, "r"); 774 if (fp == NULL) 775 *errp = NO_RECOVERY; 776 return fp; 777 } 778 779 static int 780 _files_ghbyname(void *rval, void *cb_data, va_list ap) 781 { 782 const char *name; 783 int af; 784 int *errp; 785 int match, nalias; 786 char *p, *line, *addrstr, *cname; 787 FILE *fp; 788 struct hostent *rethp, *hp, hpbuf; 789 char *aliases[MAXALIASES + 1], *addrs[2]; 790 union inx_addr addrbuf; 791 char buf[BUFSIZ]; 792 int af0; 793 794 name = va_arg(ap, const char *); 795 af = va_arg(ap, int); 796 errp = va_arg(ap, int *); 797 798 *(struct hostent **)rval = NULL; 799 800 if ((fp = _files_open(errp)) == NULL) 801 return NS_UNAVAIL; 802 rethp = hp = NULL; 803 804 af0 = af; 805 while (fgets(buf, sizeof(buf), fp)) { 806 line = buf; 807 if ((addrstr = _hgetword(&line)) == NULL 808 || (cname = _hgetword(&line)) == NULL) 809 continue; 810 match = (strcasecmp(cname, name) == 0); 811 nalias = 0; 812 while ((p = _hgetword(&line)) != NULL) { 813 if (!match) 814 match = (strcasecmp(p, name) == 0); 815 if (nalias < MAXALIASES) 816 aliases[nalias++] = p; 817 } 818 if (!match) 819 continue; 820 switch (af0) { 821 case AF_INET: 822 if (inet_aton(addrstr, (struct in_addr *)&addrbuf) 823 != 1) { 824 *errp = NO_DATA; /* name found */ 825 continue; 826 } 827 af = af0; 828 break; 829 #ifdef INET6 830 case AF_INET6: 831 if (inet_pton(af, addrstr, &addrbuf) != 1) { 832 *errp = NO_DATA; /* name found */ 833 continue; 834 } 835 af = af0; 836 break; 837 #endif 838 case AF_UNSPEC: 839 if (inet_aton(addrstr, (struct in_addr *)&addrbuf) 840 == 1) { 841 af = AF_INET; 842 break; 843 } 844 #ifdef INET6 845 if (inet_pton(AF_INET6, addrstr, &addrbuf) == 1) { 846 af = AF_INET6; 847 break; 848 } 849 #endif 850 *errp = NO_DATA; /* name found */ 851 continue; 852 /* NOTREACHED */ 853 } 854 hp = &hpbuf; 855 hp->h_name = cname; 856 hp->h_aliases = aliases; 857 aliases[nalias] = NULL; 858 hp->h_addrtype = af; 859 hp->h_length = ADDRLEN(af); 860 hp->h_addr_list = addrs; 861 addrs[0] = (char *)&addrbuf; 862 addrs[1] = NULL; 863 hp = _hpcopy(hp, errp); 864 rethp = _hpmerge(rethp, hp, errp); 865 } 866 fclose(fp); 867 *(struct hostent **)rval = rethp; 868 return (rethp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 869 } 870 871 static int 872 _files_ghbyaddr(void *rval, void *cb_data, va_list ap) 873 { 874 const void *addr; 875 int addrlen; 876 int af; 877 int *errp; 878 int nalias; 879 char *p, *line; 880 FILE *fp; 881 struct hostent *hp, hpbuf; 882 char *aliases[MAXALIASES + 1], *addrs[2]; 883 union inx_addr addrbuf; 884 char buf[BUFSIZ]; 885 886 addr = va_arg(ap, const void *); 887 addrlen = va_arg(ap, int); 888 af = va_arg(ap, int); 889 errp = va_arg(ap, int *); 890 891 *(struct hostent**)rval = NULL; 892 893 if ((fp = _files_open(errp)) == NULL) 894 return NS_UNAVAIL; 895 hp = NULL; 896 while (fgets(buf, sizeof(buf), fp)) { 897 line = buf; 898 if ((p = _hgetword(&line)) == NULL 899 || (af == AF_INET 900 ? inet_aton(p, (struct in_addr *)&addrbuf) 901 : inet_pton(af, p, &addrbuf)) != 1 902 || memcmp(addr, &addrbuf, addrlen) != 0 903 || (p = _hgetword(&line)) == NULL) 904 continue; 905 hp = &hpbuf; 906 hp->h_name = p; 907 hp->h_aliases = aliases; 908 nalias = 0; 909 while ((p = _hgetword(&line)) != NULL) { 910 if (nalias < MAXALIASES) 911 aliases[nalias++] = p; 912 } 913 aliases[nalias] = NULL; 914 hp->h_addrtype = af; 915 hp->h_length = addrlen; 916 hp->h_addr_list = addrs; 917 addrs[0] = (char *)&addrbuf; 918 addrs[1] = NULL; 919 hp = _hpcopy(hp, errp); 920 break; 921 } 922 fclose(fp); 923 *(struct hostent **)rval = hp; 924 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 925 } 926 927 #ifdef YP 928 /* 929 * NIS 930 * 931 * XXX actually a hack, these are INET4 specific. 932 */ 933 static int 934 _nis_ghbyname(void *rval, void *cb_data, va_list ap) 935 { 936 const char *name; 937 int af; 938 int *errp; 939 struct hostent *hp = NULL; 940 941 name = va_arg(ap, const char *); 942 af = va_arg(ap, int); 943 errp = va_arg(ap, int *); 944 945 if (af == AF_UNSPEC) 946 af = AF_INET; 947 if (af == AF_INET) { 948 hp = _gethostbynisname(name, af); 949 if (hp != NULL) 950 hp = _hpcopy(hp, errp); 951 } 952 953 *(struct hostent **)rval = hp; 954 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 955 956 } 957 958 static int 959 _nis_ghbyaddr(void *rval, void *cb_data, va_list ap) 960 { 961 const void *addr; 962 int addrlen; 963 int af; 964 int *errp; 965 struct hostent *hp = NULL; 966 967 addr = va_arg(ap, const void *); 968 addrlen = va_arg(ap, int); 969 af = va_arg(ap, int); 970 971 if (af == AF_INET) { 972 hp = _gethostbynisaddr(addr, addrlen, af); 973 if (hp != NULL) 974 hp = _hpcopy(hp, errp); 975 } 976 *(struct hostent **)rval = hp; 977 return (hp != NULL) ? NS_SUCCESS : NS_NOTFOUND; 978 } 979 #endif 980 981 struct __res_type_list { 982 SLIST_ENTRY(__res_type_list) rtl_entry; 983 int rtl_type; 984 }; 985 986 #define MAXPACKET (64*1024) 987 988 typedef union { 989 HEADER hdr; 990 u_char buf[MAXPACKET]; 991 } querybuf; 992 993 static struct hostent *getanswer(const querybuf *, int, const char *, int, 994 struct hostent *, int *); 995 996 /* 997 * we don't need to take care about sorting, nor IPv4 mapped address here. 998 */ 999 static struct hostent * 1000 getanswer(answer, anslen, qname, qtype, template, errp) 1001 const querybuf *answer; 1002 int anslen; 1003 const char *qname; 1004 int qtype; 1005 struct hostent *template; 1006 int *errp; 1007 { 1008 const HEADER *hp; 1009 const u_char *cp; 1010 int n; 1011 const u_char *eom, *erdata; 1012 char *bp, *ep, **ap, **hap; 1013 int type, class, ancount, qdcount; 1014 int haveanswer, had_error; 1015 char tbuf[MAXDNAME]; 1016 const char *tname; 1017 int (*name_ok)(const char *); 1018 static char *h_addr_ptrs[MAXADDRS + 1]; 1019 static char *host_aliases[MAXALIASES]; 1020 static char hostbuf[8*1024]; 1021 1022 #define BOUNDED_INCR(x) \ 1023 do { \ 1024 cp += x; \ 1025 if (cp > eom) { \ 1026 *errp = NO_RECOVERY; \ 1027 return (NULL); \ 1028 } \ 1029 } while (0) 1030 1031 #define BOUNDS_CHECK(ptr, count) \ 1032 do { \ 1033 if ((ptr) + (count) > eom) { \ 1034 *errp = NO_RECOVERY; \ 1035 return (NULL); \ 1036 } \ 1037 } while (0) 1038 1039 /* XXX do {} while (0) cannot be put here */ 1040 #define DNS_ASSERT(x) \ 1041 { \ 1042 if (!(x)) { \ 1043 cp += n; \ 1044 continue; \ 1045 } \ 1046 } 1047 1048 /* XXX do {} while (0) cannot be put here */ 1049 #define DNS_FATAL(x) \ 1050 { \ 1051 if (!(x)) { \ 1052 had_error++; \ 1053 continue; \ 1054 } \ 1055 } 1056 1057 tname = qname; 1058 template->h_name = NULL; 1059 eom = answer->buf + anslen; 1060 switch (qtype) { 1061 case T_A: 1062 case T_AAAA: 1063 name_ok = res_hnok; 1064 break; 1065 case T_PTR: 1066 name_ok = res_dnok; 1067 break; 1068 default: 1069 return (NULL); /* XXX should be abort(); */ 1070 } 1071 /* 1072 * find first satisfactory answer 1073 */ 1074 hp = &answer->hdr; 1075 ancount = ntohs(hp->ancount); 1076 qdcount = ntohs(hp->qdcount); 1077 bp = hostbuf; 1078 ep = hostbuf + sizeof hostbuf; 1079 cp = answer->buf; 1080 BOUNDED_INCR(HFIXEDSZ); 1081 if (qdcount != 1) { 1082 *errp = NO_RECOVERY; 1083 return (NULL); 1084 } 1085 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1086 if ((n < 0) || !(*name_ok)(bp)) { 1087 *errp = NO_RECOVERY; 1088 return (NULL); 1089 } 1090 BOUNDED_INCR(n + QFIXEDSZ); 1091 if (qtype == T_A || qtype == T_AAAA) { 1092 /* res_send() has already verified that the query name is the 1093 * same as the one we sent; this just gets the expanded name 1094 * (i.e., with the succeeding search-domain tacked on). 1095 */ 1096 n = strlen(bp) + 1; /* for the \0 */ 1097 if (n >= MAXHOSTNAMELEN) { 1098 *errp = NO_RECOVERY; 1099 return (NULL); 1100 } 1101 template->h_name = bp; 1102 bp += n; 1103 /* The qname can be abbreviated, but h_name is now absolute. */ 1104 qname = template->h_name; 1105 } 1106 ap = host_aliases; 1107 *ap = NULL; 1108 template->h_aliases = host_aliases; 1109 hap = h_addr_ptrs; 1110 *hap = NULL; 1111 template->h_addr_list = h_addr_ptrs; 1112 haveanswer = 0; 1113 had_error = 0; 1114 while (ancount-- > 0 && cp < eom && !had_error) { 1115 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1116 DNS_FATAL(n >= 0); 1117 DNS_FATAL((*name_ok)(bp)); 1118 cp += n; /* name */ 1119 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 1120 type = _getshort(cp); 1121 cp += INT16SZ; /* type */ 1122 class = _getshort(cp); 1123 cp += INT16SZ + INT32SZ; /* class, TTL */ 1124 n = _getshort(cp); 1125 cp += INT16SZ; /* len */ 1126 BOUNDS_CHECK(cp, n); 1127 erdata = cp + n; 1128 DNS_ASSERT(class == C_IN); 1129 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { 1130 if (ap >= &host_aliases[MAXALIASES-1]) 1131 continue; 1132 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1133 DNS_FATAL(n >= 0); 1134 DNS_FATAL((*name_ok)(tbuf)); 1135 cp += n; 1136 if (cp != erdata) { 1137 *errp = NO_RECOVERY; 1138 return (NULL); 1139 } 1140 /* Store alias. */ 1141 *ap++ = bp; 1142 n = strlen(bp) + 1; /* for the \0 */ 1143 DNS_FATAL(n < MAXHOSTNAMELEN); 1144 bp += n; 1145 /* Get canonical name. */ 1146 n = strlen(tbuf) + 1; /* for the \0 */ 1147 DNS_FATAL(n <= ep - bp); 1148 DNS_FATAL(n < MAXHOSTNAMELEN); 1149 strcpy(bp, tbuf); 1150 template->h_name = bp; 1151 bp += n; 1152 continue; 1153 } 1154 if (qtype == T_PTR && type == T_CNAME) { 1155 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1156 if (n < 0 || !res_dnok(tbuf)) { 1157 had_error++; 1158 continue; 1159 } 1160 cp += n; 1161 if (cp != erdata) { 1162 *errp = NO_RECOVERY; 1163 return (NULL); 1164 } 1165 /* Get canonical name. */ 1166 n = strlen(tbuf) + 1; /* for the \0 */ 1167 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1168 had_error++; 1169 continue; 1170 } 1171 strcpy(bp, tbuf); 1172 tname = bp; 1173 bp += n; 1174 continue; 1175 } 1176 DNS_ASSERT(type == qtype); 1177 switch (type) { 1178 case T_PTR: 1179 DNS_ASSERT(strcasecmp(tname, bp) == 0); 1180 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1181 DNS_FATAL(n >= 0); 1182 DNS_FATAL(res_hnok(bp)); 1183 #if MULTI_PTRS_ARE_ALIASES 1184 cp += n; 1185 if (cp != erdata) { 1186 *errp = NO_RECOVERY; 1187 return (NULL); 1188 } 1189 if (!haveanswer) 1190 template->h_name = bp; 1191 else if (ap < &host_aliases[MAXALIASES-1]) 1192 *ap++ = bp; 1193 else 1194 n = -1; 1195 if (n != -1) { 1196 n = strlen(bp) + 1; /* for the \0 */ 1197 if (n >= MAXHOSTNAMELEN) { 1198 had_error++; 1199 break; 1200 } 1201 bp += n; 1202 } 1203 break; 1204 #else 1205 template->h_name = bp; 1206 *errp = NETDB_SUCCESS; 1207 return (template); 1208 #endif 1209 case T_A: 1210 case T_AAAA: 1211 DNS_ASSERT(strcasecmp(template->h_name, bp) == 0); 1212 DNS_ASSERT(n == template->h_length); 1213 if (!haveanswer) { 1214 int nn; 1215 1216 template->h_name = bp; 1217 nn = strlen(bp) + 1; /* for the \0 */ 1218 bp += nn; 1219 } 1220 bp = (char *)ALIGN(bp); 1221 1222 DNS_FATAL(bp + n < ep); 1223 DNS_ASSERT(hap < &h_addr_ptrs[MAXADDRS-1]); 1224 #ifdef FILTER_V4MAPPED 1225 if (type == T_AAAA) { 1226 struct in6_addr in6; 1227 memcpy(&in6, cp, sizeof(in6)); 1228 DNS_ASSERT(IN6_IS_ADDR_V4MAPPED(&in6) == 0); 1229 } 1230 #endif 1231 bcopy(cp, *hap++ = bp, n); 1232 bp += n; 1233 cp += n; 1234 if (cp != erdata) { 1235 *errp = NO_RECOVERY; 1236 return (NULL); 1237 } 1238 break; 1239 default: 1240 abort(); 1241 } 1242 if (!had_error) 1243 haveanswer++; 1244 } 1245 if (haveanswer) { 1246 *ap = NULL; 1247 *hap = NULL; 1248 if (!template->h_name) { 1249 n = strlen(qname) + 1; /* for the \0 */ 1250 if (n > ep - bp || n >= MAXHOSTNAMELEN) 1251 goto no_recovery; 1252 strcpy(bp, qname); 1253 template->h_name = bp; 1254 bp += n; 1255 } 1256 *errp = NETDB_SUCCESS; 1257 return (template); 1258 } 1259 no_recovery: 1260 *errp = NO_RECOVERY; 1261 return (NULL); 1262 1263 #undef BOUNDED_INCR 1264 #undef BOUNDS_CHECK 1265 #undef DNS_ASSERT 1266 #undef DNS_FATAL 1267 } 1268 1269 /* res_search() variant with multiple query support. */ 1270 static struct hostent * 1271 _res_search_multi(name, rtl, errp) 1272 const char *name; /* domain name */ 1273 struct __res_type_list *rtl; /* list of query types */ 1274 int *errp; 1275 { 1276 const char *cp, * const *domain; 1277 struct hostent *hp0 = NULL, *hp; 1278 struct hostent hpbuf; 1279 u_int dots; 1280 int trailing_dot, ret, saved_herrno; 1281 int got_nodata = 0, got_servfail = 0, tried_as_is = 0; 1282 struct __res_type_list *rtl0 = rtl; 1283 querybuf *buf; 1284 1285 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1286 *errp = NETDB_INTERNAL; 1287 return (NULL); 1288 } 1289 dots = 0; 1290 for (cp = name; *cp; cp++) 1291 dots += (*cp == '.'); 1292 trailing_dot = 0; 1293 if (cp > name && *--cp == '.') 1294 trailing_dot++; 1295 1296 buf = malloc(sizeof(*buf)); 1297 if (buf == NULL) { 1298 *errp = NETDB_INTERNAL; 1299 return NULL; 1300 } 1301 1302 /* If there aren't any dots, it could be a user-level alias */ 1303 if (!dots && (cp = hostalias(name)) != NULL) { 1304 for(rtl = rtl0; rtl != NULL; 1305 rtl = SLIST_NEXT(rtl, rtl_entry)) { 1306 ret = res_query(cp, C_IN, rtl->rtl_type, buf->buf, 1307 sizeof(buf->buf)); 1308 if (ret > 0 && ret < sizeof(buf->buf)) { 1309 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA) 1310 ? AF_INET6 : AF_INET; 1311 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype); 1312 hp = getanswer(buf, ret, name, rtl->rtl_type, 1313 &hpbuf, errp); 1314 if (!hp) 1315 continue; 1316 hp = _hpcopy(&hpbuf, errp); 1317 hp0 = _hpmerge(hp0, hp, errp); 1318 } 1319 } 1320 free(buf); 1321 return (hp0); 1322 } 1323 1324 /* 1325 * If there are dots in the name already, let's just give it a try 1326 * 'as is'. The threshold can be set with the "ndots" option. 1327 */ 1328 saved_herrno = -1; 1329 if (dots >= _res.ndots) { 1330 for(rtl = rtl0; rtl != NULL; 1331 rtl = SLIST_NEXT(rtl, rtl_entry)) { 1332 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type, 1333 buf->buf, sizeof(buf->buf)); 1334 if (ret > 0 && ret < sizeof(buf->buf)) { 1335 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA) 1336 ? AF_INET6 : AF_INET; 1337 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype); 1338 hp = getanswer(buf, ret, name, rtl->rtl_type, 1339 &hpbuf, errp); 1340 if (!hp) 1341 continue; 1342 hp = _hpcopy(&hpbuf, errp); 1343 hp0 = _hpmerge(hp0, hp, errp); 1344 } 1345 } 1346 if (hp0 != NULL) { 1347 free(buf); 1348 return (hp0); 1349 } 1350 saved_herrno = *errp; 1351 tried_as_is++; 1352 } 1353 1354 /* 1355 * We do at least one level of search if 1356 * - there is no dot and RES_DEFNAME is set, or 1357 * - there is at least one dot, there is no trailing dot, 1358 * and RES_DNSRCH is set. 1359 */ 1360 if ((!dots && (_res.options & RES_DEFNAMES)) || 1361 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 1362 int done = 0; 1363 1364 for (domain = (const char * const *)_res.dnsrch; 1365 *domain && !done; 1366 domain++) { 1367 1368 for(rtl = rtl0; rtl != NULL; 1369 rtl = SLIST_NEXT(rtl, rtl_entry)) { 1370 ret = res_querydomain(name, *domain, C_IN, 1371 rtl->rtl_type, 1372 buf->buf, sizeof(buf->buf)); 1373 if (ret > 0 && ret < sizeof(buf->buf)) { 1374 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA) 1375 ? AF_INET6 : AF_INET; 1376 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype); 1377 hp = getanswer(buf, ret, name, 1378 rtl->rtl_type, &hpbuf, errp); 1379 if (!hp) 1380 continue; 1381 hp = _hpcopy(&hpbuf, errp); 1382 hp0 = _hpmerge(hp0, hp, errp); 1383 } 1384 } 1385 if (hp0 != NULL) { 1386 free(buf); 1387 return (hp0); 1388 } 1389 1390 /* 1391 * If no server present, give up. 1392 * If name isn't found in this domain, 1393 * keep trying higher domains in the search list 1394 * (if that's enabled). 1395 * On a NO_DATA error, keep trying, otherwise 1396 * a wildcard entry of another type could keep us 1397 * from finding this entry higher in the domain. 1398 * If we get some other error (negative answer or 1399 * server failure), then stop searching up, 1400 * but try the input name below in case it's 1401 * fully-qualified. 1402 */ 1403 if (errno == ECONNREFUSED) { 1404 free(buf); 1405 *errp = TRY_AGAIN; 1406 return (NULL); 1407 } 1408 1409 switch (*errp) { 1410 case NO_DATA: 1411 got_nodata++; 1412 /* FALLTHROUGH */ 1413 case HOST_NOT_FOUND: 1414 /* keep trying */ 1415 break; 1416 case TRY_AGAIN: 1417 if (buf->hdr.rcode == SERVFAIL) { 1418 /* try next search element, if any */ 1419 got_servfail++; 1420 break; 1421 } 1422 /* FALLTHROUGH */ 1423 default: 1424 /* anything else implies that we're done */ 1425 done++; 1426 } 1427 1428 /* if we got here for some reason other than DNSRCH, 1429 * we only wanted one iteration of the loop, so stop. 1430 */ 1431 if (!(_res.options & RES_DNSRCH)) 1432 done++; 1433 } 1434 } 1435 1436 /* 1437 * If we have not already tried the name "as is", do that now. 1438 * note that we do this regardless of how many dots were in the 1439 * name or whether it ends with a dot unless NOTLDQUERY is set. 1440 */ 1441 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) { 1442 for(rtl = rtl0; rtl != NULL; 1443 rtl = SLIST_NEXT(rtl, rtl_entry)) { 1444 ret = res_querydomain(name, NULL, C_IN, rtl->rtl_type, 1445 buf->buf, sizeof(buf->buf)); 1446 if (ret > 0 && ret < sizeof(buf->buf)) { 1447 hpbuf.h_addrtype = (rtl->rtl_type == T_AAAA) 1448 ? AF_INET6 : AF_INET; 1449 hpbuf.h_length = ADDRLEN(hpbuf.h_addrtype); 1450 hp = getanswer(buf, ret, name, rtl->rtl_type, 1451 &hpbuf, errp); 1452 if (!hp) 1453 continue; 1454 hp = _hpcopy(&hpbuf, errp); 1455 hp0 = _hpmerge(hp0, hp, errp); 1456 } 1457 } 1458 if (hp0 != NULL) { 1459 free(buf); 1460 return (hp0); 1461 } 1462 } 1463 1464 free(buf); 1465 1466 /* if we got here, we didn't satisfy the search. 1467 * if we did an initial full query, return that query's h_errno 1468 * (note that we wouldn't be here if that query had succeeded). 1469 * else if we ever got a nodata, send that back as the reason. 1470 * else send back meaningless h_errno, that being the one from 1471 * the last DNSRCH we did. 1472 */ 1473 if (saved_herrno != -1) 1474 *errp = saved_herrno; 1475 else if (got_nodata) 1476 *errp = NO_DATA; 1477 else if (got_servfail) 1478 *errp = TRY_AGAIN; 1479 return (NULL); 1480 } 1481 1482 static int 1483 _dns_ghbyname(void *rval, void *cb_data, va_list ap) 1484 { 1485 const char *name; 1486 int af; 1487 int *errp; 1488 struct __res_type_list *rtl, rtl4; 1489 #ifdef INET6 1490 struct __res_type_list rtl6; 1491 #endif 1492 1493 name = va_arg(ap, const char *); 1494 af = va_arg(ap, int); 1495 errp = va_arg(ap, int *); 1496 1497 #ifdef INET6 1498 switch (af) { 1499 case AF_UNSPEC: 1500 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A; 1501 SLIST_NEXT(&rtl6, rtl_entry) = &rtl4; rtl6.rtl_type = T_AAAA; 1502 rtl = &rtl6; 1503 break; 1504 case AF_INET6: 1505 SLIST_NEXT(&rtl6, rtl_entry) = NULL; rtl6.rtl_type = T_AAAA; 1506 rtl = &rtl6; 1507 break; 1508 case AF_INET: 1509 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A; 1510 rtl = &rtl4; 1511 break; 1512 } 1513 #else 1514 SLIST_NEXT(&rtl4, rtl_entry) = NULL; rtl4.rtl_type = T_A; 1515 rtl = &rtl4; 1516 #endif 1517 *(struct hostent **)rval = _res_search_multi(name, rtl, errp); 1518 return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 1519 } 1520 1521 static int 1522 _dns_ghbyaddr(void *rval, void *cb_data, va_list ap) 1523 { 1524 const void *addr; 1525 int addrlen; 1526 int af; 1527 int *errp; 1528 int n; 1529 int err; 1530 struct hostent *hp; 1531 u_char c, *cp; 1532 char *bp; 1533 struct hostent hbuf; 1534 int na; 1535 #ifdef INET6 1536 static const char hex[] = "0123456789abcdef"; 1537 #endif 1538 querybuf *buf; 1539 char qbuf[MAXDNAME+1]; 1540 char *hlist[2]; 1541 char *tld6[] = { "ip6.arpa", "ip6.int", NULL }; 1542 char *tld4[] = { "in-addr.arpa", NULL }; 1543 char **tld; 1544 1545 addr = va_arg(ap, const void *); 1546 addrlen = va_arg(ap, int); 1547 af = va_arg(ap, int); 1548 errp = va_arg(ap, int *); 1549 1550 *(struct hostent **)rval = NULL; 1551 1552 #ifdef INET6 1553 /* XXX */ 1554 if (af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)addr)) 1555 return NS_NOTFOUND; 1556 #endif 1557 1558 switch (af) { 1559 #ifdef INET6 1560 case AF_INET6: 1561 tld = tld6; 1562 break; 1563 #endif 1564 case AF_INET: 1565 tld = tld4; 1566 break; 1567 default: 1568 return NS_NOTFOUND; 1569 } 1570 1571 if ((_res.options & RES_INIT) == 0) { 1572 if (res_init() < 0) { 1573 *errp = h_errno; 1574 return NS_UNAVAIL; 1575 } 1576 } 1577 memset(&hbuf, 0, sizeof(hbuf)); 1578 hbuf.h_name = NULL; 1579 hbuf.h_addrtype = af; 1580 hbuf.h_length = addrlen; 1581 na = 0; 1582 1583 buf = malloc(sizeof(*buf)); 1584 if (buf == NULL) { 1585 *errp = NETDB_INTERNAL; 1586 return NS_UNAVAIL; 1587 } 1588 err = NS_SUCCESS; 1589 for (/* nothing */; *tld; tld++) { 1590 /* 1591 * XXX assumes that MAXDNAME is big enough - error checks 1592 * has been made by callers 1593 */ 1594 n = 0; 1595 bp = qbuf; 1596 cp = (u_char *)addr+addrlen-1; 1597 switch (af) { 1598 #ifdef INET6 1599 case AF_INET6: 1600 for (; n < addrlen; n++, cp--) { 1601 c = *cp; 1602 *bp++ = hex[c & 0xf]; 1603 *bp++ = '.'; 1604 *bp++ = hex[c >> 4]; 1605 *bp++ = '.'; 1606 } 1607 strcpy(bp, *tld); 1608 break; 1609 #endif 1610 case AF_INET: 1611 for (; n < addrlen; n++, cp--) { 1612 c = *cp; 1613 if (c >= 100) 1614 *bp++ = '0' + c / 100; 1615 if (c >= 10) 1616 *bp++ = '0' + (c % 100) / 10; 1617 *bp++ = '0' + c % 10; 1618 *bp++ = '.'; 1619 } 1620 strcpy(bp, *tld); 1621 break; 1622 } 1623 1624 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof buf->buf); 1625 if (n < 0) { 1626 *errp = h_errno; 1627 err = NS_UNAVAIL; 1628 continue; 1629 } else if (n > sizeof(buf->buf)) { 1630 #if 0 1631 errno = ERANGE; /* XXX is it OK to set errno here? */ 1632 #endif 1633 *errp = NETDB_INTERNAL; 1634 err = NS_UNAVAIL; 1635 continue; 1636 } 1637 hp = getanswer(buf, n, qbuf, T_PTR, &hbuf, errp); 1638 if (!hp) { 1639 err = NS_NOTFOUND; 1640 continue; 1641 } 1642 free(buf); 1643 hbuf.h_addrtype = af; 1644 hbuf.h_length = addrlen; 1645 hbuf.h_addr_list = hlist; 1646 hlist[0] = (char *)addr; 1647 hlist[1] = NULL; 1648 *(struct hostent **)rval = _hpcopy(&hbuf, errp); 1649 return NS_SUCCESS; 1650 } 1651 free(buf); 1652 return err; 1653 } 1654 1655 static void 1656 _dns_shent(int stayopen) 1657 { 1658 if ((_res.options & RES_INIT) == 0) { 1659 if (res_init() < 0) 1660 return; 1661 } 1662 if (stayopen) 1663 _res.options |= RES_STAYOPEN | RES_USEVC; 1664 } 1665 1666 static void 1667 _dns_ehent(void) 1668 { 1669 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 1670 res_close(); 1671 } 1672 1673 #ifdef ICMPNL 1674 1675 /* 1676 * experimental: 1677 * draft-ietf-ipngwg-icmp-namelookups-02.txt 1678 * ifindex is assumed to be encoded in addr. 1679 */ 1680 #include <sys/uio.h> 1681 #include <netinet/ip6.h> 1682 #include <netinet/icmp6.h> 1683 1684 struct _icmp_host_cache { 1685 struct _icmp_host_cache *hc_next; 1686 int hc_ifindex; 1687 struct in6_addr hc_addr; 1688 char *hc_name; 1689 }; 1690 1691 static char * 1692 _icmp_fqdn_query(const struct in6_addr *addr, int ifindex) 1693 { 1694 int s; 1695 struct icmp6_filter filter; 1696 struct msghdr msg; 1697 struct cmsghdr *cmsg; 1698 struct in6_pktinfo *pkt; 1699 char cbuf[256]; 1700 char buf[1024]; 1701 int cc; 1702 struct icmp6_fqdn_query *fq; 1703 struct icmp6_fqdn_reply *fr; 1704 struct _icmp_host_cache *hc; 1705 struct sockaddr_in6 sin6; 1706 struct iovec iov; 1707 fd_set s_fds, fds; 1708 struct timeval tout; 1709 int len; 1710 char *name; 1711 static int pid; 1712 static struct _icmp_host_cache *hc_head; 1713 1714 for (hc = hc_head; hc; hc = hc->hc_next) { 1715 if (hc->hc_ifindex == ifindex 1716 && IN6_ARE_ADDR_EQUAL(&hc->hc_addr, addr)) 1717 return hc->hc_name; 1718 } 1719 1720 if (pid == 0) 1721 pid = getpid(); 1722 1723 ICMP6_FILTER_SETBLOCKALL(&filter); 1724 ICMP6_FILTER_SETPASS(ICMP6_FQDN_REPLY, &filter); 1725 1726 FD_ZERO(&s_fds); 1727 tout.tv_sec = 0; 1728 tout.tv_usec = 200000; /*XXX: 200ms*/ 1729 1730 fq = (struct icmp6_fqdn_query *)buf; 1731 fq->icmp6_fqdn_type = ICMP6_FQDN_QUERY; 1732 fq->icmp6_fqdn_code = 0; 1733 fq->icmp6_fqdn_cksum = 0; 1734 fq->icmp6_fqdn_id = (u_short)pid; 1735 fq->icmp6_fqdn_unused = 0; 1736 fq->icmp6_fqdn_cookie[0] = 0; 1737 fq->icmp6_fqdn_cookie[1] = 0; 1738 1739 memset(&sin6, 0, sizeof(sin6)); 1740 sin6.sin6_family = AF_INET6; 1741 sin6.sin6_addr = *addr; 1742 1743 memset(&msg, 0, sizeof(msg)); 1744 msg.msg_name = (caddr_t)&sin6; 1745 msg.msg_namelen = sizeof(sin6); 1746 msg.msg_iov = &iov; 1747 msg.msg_iovlen = 1; 1748 msg.msg_control = NULL; 1749 msg.msg_controllen = 0; 1750 iov.iov_base = (caddr_t)buf; 1751 iov.iov_len = sizeof(struct icmp6_fqdn_query); 1752 1753 if (ifindex) { 1754 msg.msg_control = cbuf; 1755 msg.msg_controllen = sizeof(cbuf); 1756 cmsg = CMSG_FIRSTHDR(&msg); 1757 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 1758 cmsg->cmsg_level = IPPROTO_IPV6; 1759 cmsg->cmsg_type = IPV6_PKTINFO; 1760 pkt = (struct in6_pktinfo *)&cmsg[1]; 1761 memset(&pkt->ipi6_addr, 0, sizeof(struct in6_addr)); 1762 pkt->ipi6_ifindex = ifindex; 1763 cmsg = CMSG_NXTHDR(&msg, cmsg); 1764 msg.msg_controllen = (char *)cmsg - cbuf; 1765 } 1766 1767 if ((s = _socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) 1768 return NULL; 1769 (void)_setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, 1770 (char *)&filter, sizeof(filter)); 1771 cc = _sendmsg(s, &msg, 0); 1772 if (cc < 0) { 1773 _close(s); 1774 return NULL; 1775 } 1776 FD_SET(s, &s_fds); 1777 for (;;) { 1778 fds = s_fds; 1779 if (_select(s + 1, &fds, NULL, NULL, &tout) <= 0) { 1780 _close(s); 1781 return NULL; 1782 } 1783 len = sizeof(sin6); 1784 cc = _recvfrom(s, buf, sizeof(buf), 0, 1785 (struct sockaddr *)&sin6, &len); 1786 if (cc <= 0) { 1787 _close(s); 1788 return NULL; 1789 } 1790 if (cc < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) 1791 continue; 1792 if (!IN6_ARE_ADDR_EQUAL(addr, &sin6.sin6_addr)) 1793 continue; 1794 fr = (struct icmp6_fqdn_reply *)(buf + sizeof(struct ip6_hdr)); 1795 if (fr->icmp6_fqdn_type == ICMP6_FQDN_REPLY) 1796 break; 1797 } 1798 _close(s); 1799 if (fr->icmp6_fqdn_cookie[1] != 0) { 1800 /* rfc1788 type */ 1801 name = buf + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + 4; 1802 len = (buf + cc) - name; 1803 } else { 1804 len = fr->icmp6_fqdn_namelen; 1805 name = fr->icmp6_fqdn_name; 1806 } 1807 if (len <= 0) 1808 return NULL; 1809 name[len] = 0; 1810 1811 if ((hc = (struct _icmp_host_cache *)malloc(sizeof(*hc))) == NULL) 1812 return NULL; 1813 /* XXX: limit number of cached entries */ 1814 hc->hc_ifindex = ifindex; 1815 hc->hc_addr = *addr; 1816 hc->hc_name = strdup(name); 1817 hc->hc_next = hc_head; 1818 hc_head = hc; 1819 return hc->hc_name; 1820 } 1821 1822 static struct hostent * 1823 _icmp_ghbyaddr(const void *addr, int addrlen, int af, int *errp) 1824 { 1825 char *hname; 1826 int ifindex; 1827 struct in6_addr addr6; 1828 1829 if (af != AF_INET6) { 1830 /* 1831 * Note: rfc1788 defines Who Are You for IPv4, 1832 * but no one implements it. 1833 */ 1834 return NULL; 1835 } 1836 1837 memcpy(&addr6, addr, addrlen); 1838 ifindex = (addr6.s6_addr[2] << 8) | addr6.s6_addr[3]; 1839 addr6.s6_addr[2] = addr6.s6_addr[3] = 0; 1840 1841 if (!IN6_IS_ADDR_LINKLOCAL(&addr6)) 1842 return NULL; /*XXX*/ 1843 1844 if ((hname = _icmp_fqdn_query(&addr6, ifindex)) == NULL) 1845 return NULL; 1846 return _hpaddr(af, hname, &addr6, errp); 1847 } 1848 #endif /* ICMPNL */ 1849