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