1 /* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 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 /* 33 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 34 * 35 * Issues to be discussed: 36 * - Thread safe-ness must be checked. 37 * - Return values. There are nonstandard return values defined and used 38 * in the source code. This is because RFC2553 is silent about which error 39 * code must be returned for which situation. 40 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 41 * invalid. 42 * current code - SEGV on freeaddrinfo(NULL) 43 * Note: 44 * - We use getipnodebyname() just for thread-safeness. There's no intent 45 * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to 46 * getipnodebyname(). 47 * - The code filters out AFs that are not supported by the kernel, 48 * when globbing NULL hostname (to loopback, or wildcard). Is it the right 49 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 50 * in ai_flags? 51 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 52 * (1) what should we do against numeric hostname (2) what should we do 53 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 54 * non-loopback address configured? global address configured? 55 * - To avoid search order issue, we have a big amount of code duplicate 56 * from gethnamaddr.c and some other places. The issues that there's no 57 * lower layer function to lookup "IPv4 or IPv6" record. Calling 58 * gethostbyname2 from getaddrinfo will end up in wrong search order, as 59 * follows: 60 * - The code makes use of following calls when asked to resolver with 61 * ai_family = PF_UNSPEC: 62 * getipnodebyname(host, AF_INET6); 63 * getipnodebyname(host, AF_INET); 64 * This will result in the following queries if the node is configure to 65 * prefer /etc/hosts than DNS: 66 * lookup /etc/hosts for IPv6 address 67 * lookup DNS for IPv6 address 68 * lookup /etc/hosts for IPv4 address 69 * lookup DNS for IPv4 address 70 * which may not meet people's requirement. 71 * The right thing to happen is to have underlying layer which does 72 * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. 73 * This would result in a bit of code duplicate with _dns_ghbyname() and 74 * friends. 75 */ 76 /* 77 * diffs with other KAME platforms: 78 * - other KAME platforms already nuked FAITH ($GAI), but as FreeBSD 79 * 4.0-RELEASE supplies it, we still have the code here. 80 * - AI_ADDRCONFIG support is supplied 81 * - some of FreeBSD style (#define tabify and others) 82 * - classful IPv4 numeric (127.1) is allowed. 83 */ 84 85 #include <sys/cdefs.h> 86 __FBSDID("$FreeBSD$"); 87 88 #include "namespace.h" 89 #include <sys/types.h> 90 #include <sys/param.h> 91 #include <sys/socket.h> 92 #include <net/if.h> 93 #include <netinet/in.h> 94 #include <arpa/inet.h> 95 #include <arpa/nameser.h> 96 #include <rpc/rpc.h> 97 #include <rpcsvc/yp_prot.h> 98 #include <rpcsvc/ypclnt.h> 99 #include <netdb.h> 100 #include <resolv.h> 101 #include <string.h> 102 #include <stdlib.h> 103 #include <stddef.h> 104 #include <ctype.h> 105 #include <unistd.h> 106 #include <stdio.h> 107 #include <errno.h> 108 #ifdef DEBUG 109 #include <syslog.h> 110 #endif 111 112 #include <stdarg.h> 113 #include <nsswitch.h> 114 #include "un-namespace.h" 115 116 #if defined(__KAME__) && defined(INET6) 117 # define FAITH 118 #endif 119 120 #define SUCCESS 0 121 #define ANY 0 122 #define YES 1 123 #define NO 0 124 125 static const char in_addrany[] = { 0, 0, 0, 0 }; 126 static const char in6_addrany[] = { 127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 128 }; 129 static const char in_loopback[] = { 127, 0, 0, 1 }; 130 static const char in6_loopback[] = { 131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 132 }; 133 134 static const struct afd { 135 int a_af; 136 int a_addrlen; 137 int a_socklen; 138 int a_off; 139 const char *a_addrany; 140 const char *a_loopback; 141 int a_scoped; 142 } afdl [] = { 143 #ifdef INET6 144 #define N_INET6 0 145 {PF_INET6, sizeof(struct in6_addr), 146 sizeof(struct sockaddr_in6), 147 offsetof(struct sockaddr_in6, sin6_addr), 148 in6_addrany, in6_loopback, 1}, 149 #define N_INET 1 150 #else 151 #define N_INET 0 152 #endif 153 {PF_INET, sizeof(struct in_addr), 154 sizeof(struct sockaddr_in), 155 offsetof(struct sockaddr_in, sin_addr), 156 in_addrany, in_loopback, 0}, 157 {0, 0, 0, 0, NULL, NULL, 0}, 158 }; 159 160 struct explore { 161 int e_af; 162 int e_socktype; 163 int e_protocol; 164 const char *e_protostr; 165 int e_wild; 166 #define WILD_AF(ex) ((ex)->e_wild & 0x01) 167 #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 168 #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 169 }; 170 171 static const struct explore explore[] = { 172 #if 0 173 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 174 #endif 175 #ifdef INET6 176 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 177 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 178 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 179 #endif 180 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 181 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 182 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 183 { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 184 { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 185 { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 186 { -1, 0, 0, NULL, 0 }, 187 }; 188 189 #ifdef INET6 190 #define PTON_MAX 16 191 #else 192 #define PTON_MAX 4 193 #endif 194 195 static const ns_src default_dns_files[] = { 196 { NSSRC_FILES, NS_SUCCESS }, 197 { NSSRC_DNS, NS_SUCCESS }, 198 { 0 } 199 }; 200 201 #if PACKETSZ > 1024 202 #define MAXPACKET PACKETSZ 203 #else 204 #define MAXPACKET 1024 205 #endif 206 207 typedef union { 208 HEADER hdr; 209 u_char buf[MAXPACKET]; 210 } querybuf; 211 212 struct res_target { 213 struct res_target *next; 214 const char *name; /* domain name */ 215 int qclass, qtype; /* class and type of query */ 216 u_char *answer; /* buffer to put answer */ 217 int anslen; /* size of answer buffer */ 218 int n; /* result length */ 219 }; 220 221 static int str_isnumber(const char *); 222 static int explore_fqdn(const struct addrinfo *, const char *, 223 const char *, struct addrinfo **); 224 static int explore_null(const struct addrinfo *, 225 const char *, struct addrinfo **); 226 static int explore_numeric(const struct addrinfo *, const char *, 227 const char *, struct addrinfo **); 228 static int explore_numeric_scope(const struct addrinfo *, const char *, 229 const char *, struct addrinfo **); 230 static int get_canonname(const struct addrinfo *, 231 struct addrinfo *, const char *); 232 static struct addrinfo *get_ai(const struct addrinfo *, 233 const struct afd *, const char *); 234 static int get_portmatch(const struct addrinfo *, const char *); 235 static int get_port(struct addrinfo *, const char *, int); 236 static const struct afd *find_afd(int); 237 static int addrconfig(struct addrinfo *); 238 #ifdef INET6 239 static int ip6_str2scopeid(char *, struct sockaddr_in6 *); 240 #endif 241 242 static struct addrinfo *getanswer(const querybuf *, int, const char *, int, 243 const struct addrinfo *); 244 static int _dns_getaddrinfo(void *, void *, va_list); 245 static void _sethtent(void); 246 static void _endhtent(void); 247 static struct addrinfo *_gethtent(const char *, const struct addrinfo *); 248 static int _files_getaddrinfo(void *, void *, va_list); 249 #ifdef YP 250 static struct addrinfo *_yphostent(char *, const struct addrinfo *); 251 static int _yp_getaddrinfo(void *, void *, va_list); 252 extern int _yp_check(char **); 253 #endif 254 255 static int res_queryN(const char *, struct res_target *); 256 static int res_searchN(const char *, struct res_target *); 257 static int res_querydomainN(const char *, const char *, 258 struct res_target *); 259 260 static char *ai_errlist[] = { 261 "Success", 262 "Address family for hostname not supported", /* EAI_ADDRFAMILY */ 263 "Temporary failure in name resolution", /* EAI_AGAIN */ 264 "Invalid value for ai_flags", /* EAI_BADFLAGS */ 265 "Non-recoverable failure in name resolution", /* EAI_FAIL */ 266 "ai_family not supported", /* EAI_FAMILY */ 267 "Memory allocation failure", /* EAI_MEMORY */ 268 "No address associated with hostname", /* EAI_NODATA */ 269 "hostname nor servname provided, or not known", /* EAI_NONAME */ 270 "servname not supported for ai_socktype", /* EAI_SERVICE */ 271 "ai_socktype not supported", /* EAI_SOCKTYPE */ 272 "System error returned in errno", /* EAI_SYSTEM */ 273 "Invalid value for hints", /* EAI_BADHINTS */ 274 "Resolved protocol is unknown", /* EAI_PROTOCOL */ 275 "Unknown error", /* EAI_MAX */ 276 }; 277 278 /* XXX macros that make external reference is BAD. */ 279 280 #define GET_AI(ai, afd, addr) \ 281 do { \ 282 /* external reference: pai, error, and label free */ \ 283 (ai) = get_ai(pai, (afd), (addr)); \ 284 if ((ai) == NULL) { \ 285 error = EAI_MEMORY; \ 286 goto free; \ 287 } \ 288 } while (/*CONSTCOND*/0) 289 290 #define GET_PORT(ai, serv) \ 291 do { \ 292 /* external reference: error and label free */ \ 293 error = get_port((ai), (serv), 0); \ 294 if (error != 0) \ 295 goto free; \ 296 } while (/*CONSTCOND*/0) 297 298 #define GET_CANONNAME(ai, str) \ 299 do { \ 300 /* external reference: pai, error and label free */ \ 301 error = get_canonname(pai, (ai), (str)); \ 302 if (error != 0) \ 303 goto free; \ 304 } while (/*CONSTCOND*/0) 305 306 #define ERR(err) \ 307 do { \ 308 /* external reference: error, and label bad */ \ 309 error = (err); \ 310 goto bad; \ 311 /*NOTREACHED*/ \ 312 } while (/*CONSTCOND*/0) 313 314 #define MATCH_FAMILY(x, y, w) \ 315 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 316 #define MATCH(x, y, w) \ 317 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 318 319 char * 320 gai_strerror(ecode) 321 int ecode; 322 { 323 if (ecode < 0 || ecode > EAI_MAX) 324 ecode = EAI_MAX; 325 return ai_errlist[ecode]; 326 } 327 328 void 329 freeaddrinfo(ai) 330 struct addrinfo *ai; 331 { 332 struct addrinfo *next; 333 334 do { 335 next = ai->ai_next; 336 if (ai->ai_canonname) 337 free(ai->ai_canonname); 338 /* no need to free(ai->ai_addr) */ 339 free(ai); 340 ai = next; 341 } while (ai); 342 } 343 344 static int 345 str_isnumber(p) 346 const char *p; 347 { 348 char *ep; 349 350 if (*p == '\0') 351 return NO; 352 ep = NULL; 353 (void)strtoul(p, &ep, 10); 354 if (ep && *ep == '\0') 355 return YES; 356 else 357 return NO; 358 } 359 360 int 361 getaddrinfo(hostname, servname, hints, res) 362 const char *hostname, *servname; 363 const struct addrinfo *hints; 364 struct addrinfo **res; 365 { 366 struct addrinfo sentinel; 367 struct addrinfo *cur; 368 int error = 0; 369 struct addrinfo ai; 370 struct addrinfo ai0; 371 struct addrinfo *pai; 372 const struct explore *ex; 373 374 memset(&sentinel, 0, sizeof(sentinel)); 375 cur = &sentinel; 376 pai = &ai; 377 pai->ai_flags = 0; 378 pai->ai_family = PF_UNSPEC; 379 pai->ai_socktype = ANY; 380 pai->ai_protocol = ANY; 381 pai->ai_addrlen = 0; 382 pai->ai_canonname = NULL; 383 pai->ai_addr = NULL; 384 pai->ai_next = NULL; 385 386 if (hostname == NULL && servname == NULL) 387 return EAI_NONAME; 388 if (hints) { 389 /* error check for hints */ 390 if (hints->ai_addrlen || hints->ai_canonname || 391 hints->ai_addr || hints->ai_next) 392 ERR(EAI_BADHINTS); /* xxx */ 393 if (hints->ai_flags & ~AI_MASK) 394 ERR(EAI_BADFLAGS); 395 switch (hints->ai_family) { 396 case PF_UNSPEC: 397 case PF_INET: 398 #ifdef INET6 399 case PF_INET6: 400 #endif 401 break; 402 default: 403 ERR(EAI_FAMILY); 404 } 405 memcpy(pai, hints, sizeof(*pai)); 406 407 /* 408 * if both socktype/protocol are specified, check if they 409 * are meaningful combination. 410 */ 411 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 412 for (ex = explore; ex->e_af >= 0; ex++) { 413 if (pai->ai_family != ex->e_af) 414 continue; 415 if (ex->e_socktype == ANY) 416 continue; 417 if (ex->e_protocol == ANY) 418 continue; 419 if (pai->ai_socktype == ex->e_socktype 420 && pai->ai_protocol != ex->e_protocol) { 421 ERR(EAI_BADHINTS); 422 } 423 } 424 } 425 } 426 427 /* 428 * post-2553: AI_ALL and AI_V4MAPPED are effective only against 429 * AF_INET6 query. They needs to be ignored if specified in other 430 * occassions. 431 */ 432 switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 433 case AI_V4MAPPED: 434 case AI_ALL | AI_V4MAPPED: 435 if (pai->ai_family != AF_INET6) 436 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 437 break; 438 case AI_ALL: 439 #if 1 440 /* illegal */ 441 ERR(EAI_BADFLAGS); 442 #else 443 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 444 #endif 445 break; 446 } 447 448 /* 449 * check for special cases. (1) numeric servname is disallowed if 450 * socktype/protocol are left unspecified. (2) servname is disallowed 451 * for raw and other inet{,6} sockets. 452 */ 453 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 454 #ifdef PF_INET6 455 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 456 #endif 457 ) { 458 ai0 = *pai; /* backup *pai */ 459 460 if (pai->ai_family == PF_UNSPEC) { 461 #ifdef PF_INET6 462 pai->ai_family = PF_INET6; 463 #else 464 pai->ai_family = PF_INET; 465 #endif 466 } 467 error = get_portmatch(pai, servname); 468 if (error) 469 ERR(error); 470 471 *pai = ai0; 472 } 473 474 ai0 = *pai; 475 476 /* NULL hostname, or numeric hostname */ 477 for (ex = explore; ex->e_af >= 0; ex++) { 478 *pai = ai0; 479 480 /* PF_UNSPEC entries are prepared for DNS queries only */ 481 if (ex->e_af == PF_UNSPEC) 482 continue; 483 484 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 485 continue; 486 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 487 continue; 488 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 489 continue; 490 491 if (pai->ai_family == PF_UNSPEC) 492 pai->ai_family = ex->e_af; 493 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 494 pai->ai_socktype = ex->e_socktype; 495 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 496 pai->ai_protocol = ex->e_protocol; 497 498 if (hostname == NULL) 499 error = explore_null(pai, servname, &cur->ai_next); 500 else 501 error = explore_numeric_scope(pai, hostname, servname, &cur->ai_next); 502 503 if (error) 504 goto free; 505 506 while (cur && cur->ai_next) 507 cur = cur->ai_next; 508 } 509 510 /* 511 * XXX 512 * If numreic representation of AF1 can be interpreted as FQDN 513 * representation of AF2, we need to think again about the code below. 514 */ 515 if (sentinel.ai_next) 516 goto good; 517 518 if (pai->ai_flags & AI_NUMERICHOST) 519 ERR(EAI_NONAME); 520 if (hostname == NULL) 521 ERR(EAI_NODATA); 522 523 if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 524 ERR(EAI_FAIL); 525 526 /* 527 * hostname as alphabetical name. 528 * we would like to prefer AF_INET6 than AF_INET, so we'll make a 529 * outer loop by AFs. 530 */ 531 for (ex = explore; ex->e_af >= 0; ex++) { 532 *pai = ai0; 533 534 /* require exact match for family field */ 535 if (pai->ai_family != ex->e_af) 536 continue; 537 538 if (!MATCH(pai->ai_socktype, ex->e_socktype, 539 WILD_SOCKTYPE(ex))) { 540 continue; 541 } 542 if (!MATCH(pai->ai_protocol, ex->e_protocol, 543 WILD_PROTOCOL(ex))) { 544 continue; 545 } 546 547 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 548 pai->ai_socktype = ex->e_socktype; 549 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 550 pai->ai_protocol = ex->e_protocol; 551 552 error = explore_fqdn(pai, hostname, servname, 553 &cur->ai_next); 554 555 while (cur && cur->ai_next) 556 cur = cur->ai_next; 557 } 558 559 /* XXX */ 560 if (sentinel.ai_next) 561 error = 0; 562 563 if (error) 564 goto free; 565 if (error == 0) { 566 if (sentinel.ai_next) { 567 good: 568 *res = sentinel.ai_next; 569 return SUCCESS; 570 } else 571 error = EAI_FAIL; 572 } 573 free: 574 bad: 575 if (sentinel.ai_next) 576 freeaddrinfo(sentinel.ai_next); 577 *res = NULL; 578 return error; 579 } 580 581 /* 582 * FQDN hostname, DNS lookup 583 */ 584 static int 585 explore_fqdn(pai, hostname, servname, res) 586 const struct addrinfo *pai; 587 const char *hostname; 588 const char *servname; 589 struct addrinfo **res; 590 { 591 struct addrinfo *result; 592 struct addrinfo *cur; 593 int error = 0; 594 static const ns_dtab dtab[] = { 595 NS_FILES_CB(_files_getaddrinfo, NULL) 596 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 597 NS_NIS_CB(_yp_getaddrinfo, NULL) 598 { 0 } 599 }; 600 601 result = NULL; 602 603 /* 604 * if the servname does not match socktype/protocol, ignore it. 605 */ 606 if (get_portmatch(pai, servname) != 0) 607 return 0; 608 609 switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 610 default_dns_files, hostname, pai)) { 611 case NS_TRYAGAIN: 612 error = EAI_AGAIN; 613 goto free; 614 case NS_UNAVAIL: 615 error = EAI_FAIL; 616 goto free; 617 case NS_NOTFOUND: 618 error = EAI_NODATA; 619 goto free; 620 case NS_SUCCESS: 621 error = 0; 622 for (cur = result; cur; cur = cur->ai_next) { 623 GET_PORT(cur, servname); 624 /* canonname should be filled already */ 625 } 626 break; 627 } 628 629 *res = result; 630 631 return 0; 632 633 free: 634 if (result) 635 freeaddrinfo(result); 636 return error; 637 } 638 639 /* 640 * hostname == NULL. 641 * passive socket -> anyaddr (0.0.0.0 or ::) 642 * non-passive socket -> localhost (127.0.0.1 or ::1) 643 */ 644 static int 645 explore_null(pai, servname, res) 646 const struct addrinfo *pai; 647 const char *servname; 648 struct addrinfo **res; 649 { 650 int s; 651 const struct afd *afd; 652 struct addrinfo *cur; 653 struct addrinfo sentinel; 654 int error; 655 656 *res = NULL; 657 sentinel.ai_next = NULL; 658 cur = &sentinel; 659 660 /* 661 * filter out AFs that are not supported by the kernel 662 * XXX errno? 663 */ 664 s = _socket(pai->ai_family, SOCK_DGRAM, 0); 665 if (s < 0) { 666 if (errno != EMFILE) 667 return 0; 668 } else 669 _close(s); 670 671 /* 672 * if the servname does not match socktype/protocol, ignore it. 673 */ 674 if (get_portmatch(pai, servname) != 0) 675 return 0; 676 677 afd = find_afd(pai->ai_family); 678 if (afd == NULL) 679 return 0; 680 681 if (pai->ai_flags & AI_PASSIVE) { 682 GET_AI(cur->ai_next, afd, afd->a_addrany); 683 /* xxx meaningless? 684 * GET_CANONNAME(cur->ai_next, "anyaddr"); 685 */ 686 GET_PORT(cur->ai_next, servname); 687 } else { 688 GET_AI(cur->ai_next, afd, afd->a_loopback); 689 /* xxx meaningless? 690 * GET_CANONNAME(cur->ai_next, "localhost"); 691 */ 692 GET_PORT(cur->ai_next, servname); 693 } 694 cur = cur->ai_next; 695 696 *res = sentinel.ai_next; 697 return 0; 698 699 free: 700 if (sentinel.ai_next) 701 freeaddrinfo(sentinel.ai_next); 702 return error; 703 } 704 705 /* 706 * numeric hostname 707 */ 708 static int 709 explore_numeric(pai, hostname, servname, res) 710 const struct addrinfo *pai; 711 const char *hostname; 712 const char *servname; 713 struct addrinfo **res; 714 { 715 const struct afd *afd; 716 struct addrinfo *cur; 717 struct addrinfo sentinel; 718 int error; 719 char pton[PTON_MAX]; 720 721 *res = NULL; 722 sentinel.ai_next = NULL; 723 cur = &sentinel; 724 725 /* 726 * if the servname does not match socktype/protocol, ignore it. 727 */ 728 if (get_portmatch(pai, servname) != 0) 729 return 0; 730 731 afd = find_afd(pai->ai_family); 732 if (afd == NULL) 733 return 0; 734 735 switch (afd->a_af) { 736 #if 1 /*X/Open spec*/ 737 case AF_INET: 738 if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 739 if (pai->ai_family == afd->a_af || 740 pai->ai_family == PF_UNSPEC /*?*/) { 741 GET_AI(cur->ai_next, afd, pton); 742 GET_PORT(cur->ai_next, servname); 743 while (cur && cur->ai_next) 744 cur = cur->ai_next; 745 } else 746 ERR(EAI_FAMILY); /*xxx*/ 747 } 748 break; 749 #endif 750 default: 751 if (inet_pton(afd->a_af, hostname, pton) == 1) { 752 if (pai->ai_family == afd->a_af || 753 pai->ai_family == PF_UNSPEC /*?*/) { 754 GET_AI(cur->ai_next, afd, pton); 755 GET_PORT(cur->ai_next, servname); 756 while (cur && cur->ai_next) 757 cur = cur->ai_next; 758 } else 759 ERR(EAI_FAMILY); /*xxx*/ 760 } 761 break; 762 } 763 764 *res = sentinel.ai_next; 765 return 0; 766 767 free: 768 bad: 769 if (sentinel.ai_next) 770 freeaddrinfo(sentinel.ai_next); 771 return error; 772 } 773 774 /* 775 * numeric hostname with scope 776 */ 777 static int 778 explore_numeric_scope(pai, hostname, servname, res) 779 const struct addrinfo *pai; 780 const char *hostname; 781 const char *servname; 782 struct addrinfo **res; 783 { 784 #if !defined(SCOPE_DELIMITER) || !defined(INET6) 785 return explore_numeric(pai, hostname, servname, res); 786 #else 787 const struct afd *afd; 788 struct addrinfo *cur; 789 int error; 790 char *cp, *hostname2 = NULL, *scope, *addr; 791 struct sockaddr_in6 *sin6; 792 793 /* 794 * if the servname does not match socktype/protocol, ignore it. 795 */ 796 if (get_portmatch(pai, servname) != 0) 797 return 0; 798 799 afd = find_afd(pai->ai_family); 800 if (afd == NULL) 801 return 0; 802 803 if (!afd->a_scoped) 804 return explore_numeric(pai, hostname, servname, res); 805 806 cp = strchr(hostname, SCOPE_DELIMITER); 807 if (cp == NULL) 808 return explore_numeric(pai, hostname, servname, res); 809 810 /* 811 * Handle special case of <scoped_address><delimiter><scope id> 812 */ 813 hostname2 = strdup(hostname); 814 if (hostname2 == NULL) 815 return EAI_MEMORY; 816 /* terminate at the delimiter */ 817 hostname2[cp - hostname] = '\0'; 818 addr = hostname2; 819 scope = cp + 1; 820 821 error = explore_numeric(pai, addr, servname, res); 822 if (error == 0) { 823 int scopeid; 824 825 for (cur = *res; cur; cur = cur->ai_next) { 826 if (cur->ai_family != AF_INET6) 827 continue; 828 sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 829 if ((scopeid = ip6_str2scopeid(scope, sin6)) == -1) { 830 free(hostname2); 831 return(EAI_NODATA); /* XXX: is return OK? */ 832 } 833 sin6->sin6_scope_id = scopeid; 834 } 835 } 836 837 free(hostname2); 838 839 return error; 840 #endif 841 } 842 843 static int 844 get_canonname(pai, ai, str) 845 const struct addrinfo *pai; 846 struct addrinfo *ai; 847 const char *str; 848 { 849 if ((pai->ai_flags & AI_CANONNAME) != 0) { 850 ai->ai_canonname = (char *)malloc(strlen(str) + 1); 851 if (ai->ai_canonname == NULL) 852 return EAI_MEMORY; 853 strcpy(ai->ai_canonname, str); 854 } 855 return 0; 856 } 857 858 static struct addrinfo * 859 get_ai(pai, afd, addr) 860 const struct addrinfo *pai; 861 const struct afd *afd; 862 const char *addr; 863 { 864 char *p; 865 struct addrinfo *ai; 866 #ifdef FAITH 867 struct in6_addr faith_prefix; 868 char *fp_str; 869 int translate = 0; 870 #endif 871 872 #ifdef FAITH 873 /* 874 * Transfrom an IPv4 addr into a special IPv6 addr format for 875 * IPv6->IPv4 translation gateway. (only TCP is supported now) 876 * 877 * +-----------------------------------+------------+ 878 * | faith prefix part (12 bytes) | embedded | 879 * | | IPv4 addr part (4 bytes) 880 * +-----------------------------------+------------+ 881 * 882 * faith prefix part is specified as ascii IPv6 addr format 883 * in environmental variable GAI. 884 * For FAITH to work correctly, routing to faith prefix must be 885 * setup toward a machine where a FAITH daemon operates. 886 * Also, the machine must enable some mechanizm 887 * (e.g. faith interface hack) to divert those packet with 888 * faith prefixed destination addr to user-land FAITH daemon. 889 */ 890 fp_str = getenv("GAI"); 891 if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 892 afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 893 u_int32_t v4a; 894 u_int8_t v4a_top; 895 896 memcpy(&v4a, addr, sizeof v4a); 897 v4a_top = v4a >> IN_CLASSA_NSHIFT; 898 if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 899 v4a_top != 0 && v4a != IN_LOOPBACKNET) { 900 afd = &afdl[N_INET6]; 901 memcpy(&faith_prefix.s6_addr[12], addr, 902 sizeof(struct in_addr)); 903 translate = 1; 904 } 905 } 906 #endif 907 908 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 909 + (afd->a_socklen)); 910 if (ai == NULL) 911 return NULL; 912 913 memcpy(ai, pai, sizeof(struct addrinfo)); 914 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 915 memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 916 ai->ai_addr->sa_len = afd->a_socklen; 917 ai->ai_addrlen = afd->a_socklen; 918 ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 919 p = (char *)(void *)(ai->ai_addr); 920 #ifdef FAITH 921 if (translate == 1) 922 memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 923 else 924 #endif 925 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 926 return ai; 927 } 928 929 static int 930 get_portmatch(ai, servname) 931 const struct addrinfo *ai; 932 const char *servname; 933 { 934 935 /* get_port does not touch first argument. when matchonly == 1. */ 936 /* LINTED const cast */ 937 return get_port((struct addrinfo *)ai, servname, 1); 938 } 939 940 static int 941 get_port(ai, servname, matchonly) 942 struct addrinfo *ai; 943 const char *servname; 944 int matchonly; 945 { 946 const char *proto; 947 struct servent *sp; 948 int port; 949 int allownumeric; 950 951 if (servname == NULL) 952 return 0; 953 switch (ai->ai_family) { 954 case AF_INET: 955 #ifdef AF_INET6 956 case AF_INET6: 957 #endif 958 break; 959 default: 960 return 0; 961 } 962 963 switch (ai->ai_socktype) { 964 case SOCK_RAW: 965 return EAI_SERVICE; 966 case SOCK_DGRAM: 967 case SOCK_STREAM: 968 allownumeric = 1; 969 break; 970 case ANY: 971 allownumeric = 0; 972 break; 973 default: 974 return EAI_SOCKTYPE; 975 } 976 977 if (str_isnumber(servname)) { 978 if (!allownumeric) 979 return EAI_SERVICE; 980 port = htons(atoi(servname)); 981 if (port < 0 || port > 65535) 982 return EAI_SERVICE; 983 } else { 984 switch (ai->ai_socktype) { 985 case SOCK_DGRAM: 986 proto = "udp"; 987 break; 988 case SOCK_STREAM: 989 proto = "tcp"; 990 break; 991 default: 992 proto = NULL; 993 break; 994 } 995 996 if ((sp = getservbyname(servname, proto)) == NULL) 997 return EAI_SERVICE; 998 port = sp->s_port; 999 } 1000 1001 if (!matchonly) { 1002 switch (ai->ai_family) { 1003 case AF_INET: 1004 ((struct sockaddr_in *)(void *) 1005 ai->ai_addr)->sin_port = port; 1006 break; 1007 #ifdef INET6 1008 case AF_INET6: 1009 ((struct sockaddr_in6 *)(void *) 1010 ai->ai_addr)->sin6_port = port; 1011 break; 1012 #endif 1013 } 1014 } 1015 1016 return 0; 1017 } 1018 1019 static const struct afd * 1020 find_afd(af) 1021 int af; 1022 { 1023 const struct afd *afd; 1024 1025 if (af == PF_UNSPEC) 1026 return NULL; 1027 for (afd = afdl; afd->a_af; afd++) { 1028 if (afd->a_af == af) 1029 return afd; 1030 } 1031 return NULL; 1032 } 1033 1034 /* 1035 * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1036 * will take care of it. 1037 * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1038 * if the code is right or not. 1039 * 1040 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1041 * _dns_getaddrinfo. 1042 */ 1043 static int 1044 addrconfig(pai) 1045 struct addrinfo *pai; 1046 { 1047 int s, af; 1048 1049 /* 1050 * TODO: 1051 * Note that implementation dependent test for address 1052 * configuration should be done everytime called 1053 * (or apropriate interval), 1054 * because addresses will be dynamically assigned or deleted. 1055 */ 1056 af = pai->ai_family; 1057 if (af == AF_UNSPEC) { 1058 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1059 af = AF_INET; 1060 else { 1061 _close(s); 1062 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1063 af = AF_INET6; 1064 else 1065 _close(s); 1066 } 1067 } 1068 if (af != AF_UNSPEC) { 1069 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 1070 return 0; 1071 _close(s); 1072 } 1073 pai->ai_family = af; 1074 return 1; 1075 } 1076 1077 #ifdef INET6 1078 /* convert a string to a scope identifier. XXX: IPv6 specific */ 1079 static int 1080 ip6_str2scopeid(scope, sin6) 1081 char *scope; 1082 struct sockaddr_in6 *sin6; 1083 { 1084 int scopeid; 1085 struct in6_addr *a6 = &sin6->sin6_addr; 1086 char *ep; 1087 1088 /* empty scopeid portion is invalid */ 1089 if (*scope == '\0') 1090 return -1; 1091 1092 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1093 /* 1094 * We currently assume a one-to-one mapping between links 1095 * and interfaces, so we simply use interface indices for 1096 * like-local scopes. 1097 */ 1098 scopeid = if_nametoindex(scope); 1099 if (scopeid == 0) 1100 goto trynumeric; 1101 return(scopeid); 1102 } 1103 1104 /* still unclear about literal, allow numeric only - placeholder */ 1105 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1106 goto trynumeric; 1107 if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1108 goto trynumeric; 1109 else 1110 goto trynumeric; /* global */ 1111 1112 /* try to convert to a numeric id as a last resort */ 1113 trynumeric: 1114 scopeid = (int)strtoul(scope, &ep, 10); 1115 if (*ep == '\0') 1116 return scopeid; 1117 else 1118 return -1; 1119 } 1120 #endif 1121 1122 #ifdef DEBUG 1123 static const char AskedForGot[] = 1124 "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1125 #endif 1126 static FILE *hostf = NULL; 1127 1128 static struct addrinfo * 1129 getanswer(answer, anslen, qname, qtype, pai) 1130 const querybuf *answer; 1131 int anslen; 1132 const char *qname; 1133 int qtype; 1134 const struct addrinfo *pai; 1135 { 1136 struct addrinfo sentinel, *cur; 1137 struct addrinfo ai; 1138 const struct afd *afd; 1139 char *canonname; 1140 const HEADER *hp; 1141 const u_char *cp; 1142 int n; 1143 const u_char *eom; 1144 char *bp; 1145 int type, class, buflen, ancount, qdcount; 1146 int haveanswer, had_error; 1147 char tbuf[MAXDNAME]; 1148 int (*name_ok)(const char *); 1149 char hostbuf[8*1024]; 1150 1151 memset(&sentinel, 0, sizeof(sentinel)); 1152 cur = &sentinel; 1153 1154 canonname = NULL; 1155 eom = answer->buf + anslen; 1156 switch (qtype) { 1157 case T_A: 1158 case T_AAAA: 1159 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1160 name_ok = res_hnok; 1161 break; 1162 default: 1163 return (NULL); /* XXX should be abort(); */ 1164 } 1165 /* 1166 * find first satisfactory answer 1167 */ 1168 hp = &answer->hdr; 1169 ancount = ntohs(hp->ancount); 1170 qdcount = ntohs(hp->qdcount); 1171 bp = hostbuf; 1172 buflen = sizeof hostbuf; 1173 cp = answer->buf + HFIXEDSZ; 1174 if (qdcount != 1) { 1175 h_errno = NO_RECOVERY; 1176 return (NULL); 1177 } 1178 n = dn_expand(answer->buf, eom, cp, bp, buflen); 1179 if ((n < 0) || !(*name_ok)(bp)) { 1180 h_errno = NO_RECOVERY; 1181 return (NULL); 1182 } 1183 cp += n + QFIXEDSZ; 1184 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1185 /* res_send() has already verified that the query name is the 1186 * same as the one we sent; this just gets the expanded name 1187 * (i.e., with the succeeding search-domain tacked on). 1188 */ 1189 n = strlen(bp) + 1; /* for the \0 */ 1190 if (n >= MAXHOSTNAMELEN) { 1191 h_errno = NO_RECOVERY; 1192 return (NULL); 1193 } 1194 canonname = bp; 1195 bp += n; 1196 buflen -= n; 1197 /* The qname can be abbreviated, but h_name is now absolute. */ 1198 qname = canonname; 1199 } 1200 haveanswer = 0; 1201 had_error = 0; 1202 while (ancount-- > 0 && cp < eom && !had_error) { 1203 n = dn_expand(answer->buf, eom, cp, bp, buflen); 1204 if ((n < 0) || !(*name_ok)(bp)) { 1205 had_error++; 1206 continue; 1207 } 1208 cp += n; /* name */ 1209 type = _getshort(cp); 1210 cp += INT16SZ; /* type */ 1211 class = _getshort(cp); 1212 cp += INT16SZ + INT32SZ; /* class, TTL */ 1213 n = _getshort(cp); 1214 cp += INT16SZ; /* len */ 1215 if (class != C_IN) { 1216 /* XXX - debug? syslog? */ 1217 cp += n; 1218 continue; /* XXX - had_error++ ? */ 1219 } 1220 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1221 type == T_CNAME) { 1222 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1223 if ((n < 0) || !(*name_ok)(tbuf)) { 1224 had_error++; 1225 continue; 1226 } 1227 cp += n; 1228 /* Get canonical name. */ 1229 n = strlen(tbuf) + 1; /* for the \0 */ 1230 if (n > buflen || n >= MAXHOSTNAMELEN) { 1231 had_error++; 1232 continue; 1233 } 1234 strcpy(bp, tbuf); 1235 canonname = bp; 1236 bp += n; 1237 buflen -= n; 1238 continue; 1239 } 1240 if (qtype == T_ANY) { 1241 if (!(type == T_A || type == T_AAAA)) { 1242 cp += n; 1243 continue; 1244 } 1245 } else if (type != qtype) { 1246 #ifdef DEBUG 1247 if (type != T_KEY && type != T_SIG) 1248 syslog(LOG_NOTICE|LOG_AUTH, 1249 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1250 qname, p_class(C_IN), p_type(qtype), 1251 p_type(type)); 1252 #endif 1253 cp += n; 1254 continue; /* XXX - had_error++ ? */ 1255 } 1256 switch (type) { 1257 case T_A: 1258 case T_AAAA: 1259 if (strcasecmp(canonname, bp) != 0) { 1260 #ifdef DEBUG 1261 syslog(LOG_NOTICE|LOG_AUTH, 1262 AskedForGot, canonname, bp); 1263 #endif 1264 cp += n; 1265 continue; /* XXX - had_error++ ? */ 1266 } 1267 if (type == T_A && n != INADDRSZ) { 1268 cp += n; 1269 continue; 1270 } 1271 if (type == T_AAAA && n != IN6ADDRSZ) { 1272 cp += n; 1273 continue; 1274 } 1275 #ifdef FILTER_V4MAPPED 1276 if (type == T_AAAA) { 1277 struct in6_addr in6; 1278 memcpy(&in6, cp, sizeof(in6)); 1279 if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1280 cp += n; 1281 continue; 1282 } 1283 } 1284 #endif 1285 if (!haveanswer) { 1286 int nn; 1287 1288 canonname = bp; 1289 nn = strlen(bp) + 1; /* for the \0 */ 1290 bp += nn; 1291 buflen -= nn; 1292 } 1293 1294 /* don't overwrite pai */ 1295 ai = *pai; 1296 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1297 afd = find_afd(ai.ai_family); 1298 if (afd == NULL) { 1299 cp += n; 1300 continue; 1301 } 1302 cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1303 if (cur->ai_next == NULL) 1304 had_error++; 1305 while (cur && cur->ai_next) 1306 cur = cur->ai_next; 1307 cp += n; 1308 break; 1309 default: 1310 abort(); 1311 } 1312 if (!had_error) 1313 haveanswer++; 1314 } 1315 if (haveanswer) { 1316 if (!canonname) 1317 (void)get_canonname(pai, sentinel.ai_next, qname); 1318 else 1319 (void)get_canonname(pai, sentinel.ai_next, canonname); 1320 h_errno = NETDB_SUCCESS; 1321 return sentinel.ai_next; 1322 } 1323 1324 h_errno = NO_RECOVERY; 1325 return NULL; 1326 } 1327 1328 /*ARGSUSED*/ 1329 static int 1330 _dns_getaddrinfo(rv, cb_data, ap) 1331 void *rv; 1332 void *cb_data; 1333 va_list ap; 1334 { 1335 struct addrinfo *ai; 1336 querybuf buf, buf2; 1337 const char *name; 1338 const struct addrinfo *pai; 1339 struct addrinfo sentinel, *cur; 1340 struct res_target q, q2; 1341 1342 name = va_arg(ap, char *); 1343 pai = va_arg(ap, const struct addrinfo *); 1344 1345 memset(&q, 0, sizeof(q2)); 1346 memset(&q2, 0, sizeof(q2)); 1347 memset(&sentinel, 0, sizeof(sentinel)); 1348 cur = &sentinel; 1349 1350 switch (pai->ai_family) { 1351 case AF_UNSPEC: 1352 /* prefer IPv6 */ 1353 q.qclass = C_IN; 1354 q.qtype = T_AAAA; 1355 q.answer = buf.buf; 1356 q.anslen = sizeof(buf); 1357 q.next = &q2; 1358 q2.qclass = C_IN; 1359 q2.qtype = T_A; 1360 q2.answer = buf2.buf; 1361 q2.anslen = sizeof(buf2); 1362 break; 1363 case AF_INET: 1364 q.qclass = C_IN; 1365 q.qtype = T_A; 1366 q.answer = buf.buf; 1367 q.anslen = sizeof(buf); 1368 break; 1369 case AF_INET6: 1370 q.qclass = C_IN; 1371 q.qtype = T_AAAA; 1372 q.answer = buf.buf; 1373 q.anslen = sizeof(buf); 1374 break; 1375 default: 1376 return NS_UNAVAIL; 1377 } 1378 if (res_searchN(name, &q) < 0) 1379 return NS_NOTFOUND; 1380 ai = getanswer(&buf, q.n, q.name, q.qtype, pai); 1381 if (ai) { 1382 cur->ai_next = ai; 1383 while (cur && cur->ai_next) 1384 cur = cur->ai_next; 1385 } 1386 if (q.next) { 1387 ai = getanswer(&buf2, q2.n, q2.name, q2.qtype, pai); 1388 if (ai) 1389 cur->ai_next = ai; 1390 } 1391 if (sentinel.ai_next == NULL) 1392 switch (h_errno) { 1393 case HOST_NOT_FOUND: 1394 return NS_NOTFOUND; 1395 case TRY_AGAIN: 1396 return NS_TRYAGAIN; 1397 default: 1398 return NS_UNAVAIL; 1399 } 1400 *((struct addrinfo **)rv) = sentinel.ai_next; 1401 return NS_SUCCESS; 1402 } 1403 1404 static void 1405 _sethtent() 1406 { 1407 if (!hostf) 1408 hostf = fopen(_PATH_HOSTS, "r" ); 1409 else 1410 rewind(hostf); 1411 } 1412 1413 static void 1414 _endhtent() 1415 { 1416 if (hostf) { 1417 (void) fclose(hostf); 1418 hostf = NULL; 1419 } 1420 } 1421 1422 static struct addrinfo * 1423 _gethtent(name, pai) 1424 const char *name; 1425 const struct addrinfo *pai; 1426 { 1427 char *p; 1428 char *cp, *tname, *cname; 1429 struct addrinfo hints, *res0, *res; 1430 int error; 1431 const char *addr; 1432 char hostbuf[8*1024]; 1433 1434 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) 1435 return (NULL); 1436 again: 1437 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) 1438 return (NULL); 1439 if (*p == '#') 1440 goto again; 1441 if (!(cp = strpbrk(p, "#\n"))) 1442 goto again; 1443 *cp = '\0'; 1444 if (!(cp = strpbrk(p, " \t"))) 1445 goto again; 1446 *cp++ = '\0'; 1447 addr = p; 1448 cname = NULL; 1449 /* if this is not something we're looking for, skip it. */ 1450 while (cp && *cp) { 1451 if (*cp == ' ' || *cp == '\t') { 1452 cp++; 1453 continue; 1454 } 1455 tname = cp; 1456 if (cname == NULL) 1457 cname = cp; 1458 if ((cp = strpbrk(cp, " \t")) != NULL) 1459 *cp++ = '\0'; 1460 if (strcasecmp(name, tname) == 0) 1461 goto found; 1462 } 1463 goto again; 1464 1465 found: 1466 hints = *pai; 1467 hints.ai_flags = AI_NUMERICHOST; 1468 error = getaddrinfo(addr, NULL, &hints, &res0); 1469 if (error) 1470 goto again; 1471 #ifdef FILTER_V4MAPPED 1472 /* XXX should check all items in the chain */ 1473 if (res0->ai_family == AF_INET6 && 1474 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 1475 freeaddrinfo(res0); 1476 goto again; 1477 } 1478 #endif 1479 for (res = res0; res; res = res->ai_next) { 1480 /* cover it up */ 1481 res->ai_flags = pai->ai_flags; 1482 1483 if (pai->ai_flags & AI_CANONNAME) { 1484 if (get_canonname(pai, res, cname) != 0) { 1485 freeaddrinfo(res0); 1486 goto again; 1487 } 1488 } 1489 } 1490 return res0; 1491 } 1492 1493 /*ARGSUSED*/ 1494 static int 1495 _files_getaddrinfo(rv, cb_data, ap) 1496 void *rv; 1497 void *cb_data; 1498 va_list ap; 1499 { 1500 const char *name; 1501 const struct addrinfo *pai; 1502 struct addrinfo sentinel, *cur; 1503 struct addrinfo *p; 1504 1505 name = va_arg(ap, char *); 1506 pai = va_arg(ap, struct addrinfo *); 1507 1508 memset(&sentinel, 0, sizeof(sentinel)); 1509 cur = &sentinel; 1510 1511 _sethtent(); 1512 while ((p = _gethtent(name, pai)) != NULL) { 1513 cur->ai_next = p; 1514 while (cur && cur->ai_next) 1515 cur = cur->ai_next; 1516 } 1517 _endhtent(); 1518 1519 *((struct addrinfo **)rv) = sentinel.ai_next; 1520 if (sentinel.ai_next == NULL) 1521 return NS_NOTFOUND; 1522 return NS_SUCCESS; 1523 } 1524 1525 #ifdef YP 1526 static char *__ypdomain; 1527 1528 /*ARGSUSED*/ 1529 static struct addrinfo * 1530 _yphostent(line, pai) 1531 char *line; 1532 const struct addrinfo *pai; 1533 { 1534 struct addrinfo sentinel, *cur; 1535 struct addrinfo hints, *res, *res0; 1536 int error; 1537 char *p = line; 1538 const char *addr, *canonname; 1539 char *nextline; 1540 char *cp; 1541 1542 addr = canonname = NULL; 1543 1544 memset(&sentinel, 0, sizeof(sentinel)); 1545 cur = &sentinel; 1546 1547 nextline: 1548 /* terminate line */ 1549 cp = strchr(p, '\n'); 1550 if (cp) { 1551 *cp++ = '\0'; 1552 nextline = cp; 1553 } else 1554 nextline = NULL; 1555 1556 cp = strpbrk(p, " \t"); 1557 if (cp == NULL) { 1558 if (canonname == NULL) 1559 return (NULL); 1560 else 1561 goto done; 1562 } 1563 *cp++ = '\0'; 1564 1565 addr = p; 1566 1567 while (cp && *cp) { 1568 if (*cp == ' ' || *cp == '\t') { 1569 cp++; 1570 continue; 1571 } 1572 if (!canonname) 1573 canonname = cp; 1574 if ((cp = strpbrk(cp, " \t")) != NULL) 1575 *cp++ = '\0'; 1576 } 1577 1578 hints = *pai; 1579 hints.ai_flags = AI_NUMERICHOST; 1580 error = getaddrinfo(addr, NULL, &hints, &res0); 1581 if (error == 0) { 1582 for (res = res0; res; res = res->ai_next) { 1583 /* cover it up */ 1584 res->ai_flags = pai->ai_flags; 1585 1586 if (pai->ai_flags & AI_CANONNAME) 1587 (void)get_canonname(pai, res, canonname); 1588 } 1589 } else 1590 res0 = NULL; 1591 if (res0) { 1592 cur->ai_next = res0; 1593 while (cur && cur->ai_next) 1594 cur = cur->ai_next; 1595 } 1596 1597 if (nextline) { 1598 p = nextline; 1599 goto nextline; 1600 } 1601 1602 done: 1603 return sentinel.ai_next; 1604 } 1605 1606 /*ARGSUSED*/ 1607 static int 1608 _yp_getaddrinfo(rv, cb_data, ap) 1609 void *rv; 1610 void *cb_data; 1611 va_list ap; 1612 { 1613 struct addrinfo sentinel, *cur; 1614 struct addrinfo *ai = NULL; 1615 static char *__ypcurrent; 1616 int __ypcurrentlen, r; 1617 const char *name; 1618 const struct addrinfo *pai; 1619 1620 name = va_arg(ap, char *); 1621 pai = va_arg(ap, const struct addrinfo *); 1622 1623 memset(&sentinel, 0, sizeof(sentinel)); 1624 cur = &sentinel; 1625 1626 if (!__ypdomain) { 1627 if (_yp_check(&__ypdomain) == 0) 1628 return NS_UNAVAIL; 1629 } 1630 if (__ypcurrent) 1631 free(__ypcurrent); 1632 __ypcurrent = NULL; 1633 1634 /* hosts.byname is only for IPv4 (Solaris8) */ 1635 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 1636 r = yp_match(__ypdomain, "hosts.byname", name, 1637 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1638 if (r == 0) { 1639 struct addrinfo ai4; 1640 1641 ai4 = *pai; 1642 ai4.ai_family = AF_INET; 1643 ai = _yphostent(__ypcurrent, &ai4); 1644 if (ai) { 1645 cur->ai_next = ai; 1646 while (cur && cur->ai_next) 1647 cur = cur->ai_next; 1648 } 1649 } 1650 } 1651 1652 /* ipnodes.byname can hold both IPv4/v6 */ 1653 r = yp_match(__ypdomain, "ipnodes.byname", name, 1654 (int)strlen(name), &__ypcurrent, &__ypcurrentlen); 1655 if (r == 0) { 1656 ai = _yphostent(__ypcurrent, pai); 1657 if (ai) { 1658 cur->ai_next = ai; 1659 while (cur && cur->ai_next) 1660 cur = cur->ai_next; 1661 } 1662 } 1663 1664 if (sentinel.ai_next == NULL) { 1665 h_errno = HOST_NOT_FOUND; 1666 return NS_NOTFOUND; 1667 } 1668 *((struct addrinfo **)rv) = sentinel.ai_next; 1669 return NS_SUCCESS; 1670 } 1671 #endif 1672 1673 /* resolver logic */ 1674 1675 extern const char *__hostalias(const char *); 1676 extern int h_errno; 1677 1678 /* 1679 * Formulate a normal query, send, and await answer. 1680 * Returned answer is placed in supplied buffer "answer". 1681 * Perform preliminary check of answer, returning success only 1682 * if no error is indicated and the answer count is nonzero. 1683 * Return the size of the response on success, -1 on error. 1684 * Error number is left in h_errno. 1685 * 1686 * Caller must parse answer and determine whether it answers the question. 1687 */ 1688 static int 1689 res_queryN(name, target) 1690 const char *name; /* domain name */ 1691 struct res_target *target; 1692 { 1693 u_char buf[MAXPACKET]; 1694 HEADER *hp; 1695 int n; 1696 struct res_target *t; 1697 int rcode; 1698 int ancount; 1699 1700 rcode = NOERROR; 1701 ancount = 0; 1702 1703 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1704 h_errno = NETDB_INTERNAL; 1705 return (-1); 1706 } 1707 1708 for (t = target; t; t = t->next) { 1709 int class, type; 1710 u_char *answer; 1711 int anslen; 1712 1713 hp = (HEADER *)(void *)t->answer; 1714 hp->rcode = NOERROR; /* default */ 1715 1716 /* make it easier... */ 1717 class = t->qclass; 1718 type = t->qtype; 1719 answer = t->answer; 1720 anslen = t->anslen; 1721 #ifdef DEBUG 1722 if (_res.options & RES_DEBUG) 1723 printf(";; res_query(%s, %d, %d)\n", name, class, type); 1724 #endif 1725 1726 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, 1727 buf, sizeof(buf)); 1728 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0) 1729 n = res_opt(n, buf, sizeof(buf), anslen); 1730 if (n <= 0) { 1731 #ifdef DEBUG 1732 if (_res.options & RES_DEBUG) 1733 printf(";; res_query: mkquery failed\n"); 1734 #endif 1735 h_errno = NO_RECOVERY; 1736 return (n); 1737 } 1738 n = res_send(buf, n, answer, anslen); 1739 #if 0 1740 if (n < 0) { 1741 #ifdef DEBUG 1742 if (_res.options & RES_DEBUG) 1743 printf(";; res_query: send error\n"); 1744 #endif 1745 h_errno = TRY_AGAIN; 1746 return (n); 1747 } 1748 #endif 1749 1750 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 1751 rcode = hp->rcode; /* record most recent error */ 1752 #ifdef DEBUG 1753 if (_res.options & RES_DEBUG) 1754 printf(";; rcode = %d, ancount=%d\n", hp->rcode, 1755 ntohs(hp->ancount)); 1756 #endif 1757 continue; 1758 } 1759 1760 ancount += ntohs(hp->ancount); 1761 1762 t->n = n; 1763 } 1764 1765 if (ancount == 0) { 1766 switch (rcode) { 1767 case NXDOMAIN: 1768 h_errno = HOST_NOT_FOUND; 1769 break; 1770 case SERVFAIL: 1771 h_errno = TRY_AGAIN; 1772 break; 1773 case NOERROR: 1774 h_errno = NO_DATA; 1775 break; 1776 case FORMERR: 1777 case NOTIMP: 1778 case REFUSED: 1779 default: 1780 h_errno = NO_RECOVERY; 1781 break; 1782 } 1783 return (-1); 1784 } 1785 return (ancount); 1786 } 1787 1788 /* 1789 * Formulate a normal query, send, and retrieve answer in supplied buffer. 1790 * Return the size of the response on success, -1 on error. 1791 * If enabled, implement search rules until answer or unrecoverable failure 1792 * is detected. Error code, if any, is left in h_errno. 1793 */ 1794 static int 1795 res_searchN(name, target) 1796 const char *name; /* domain name */ 1797 struct res_target *target; 1798 { 1799 const char *cp, * const *domain; 1800 HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 1801 u_int dots; 1802 int trailing_dot, ret, saved_herrno; 1803 int got_nodata = 0, got_servfail = 0, tried_as_is = 0; 1804 1805 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1806 h_errno = NETDB_INTERNAL; 1807 return (-1); 1808 } 1809 1810 errno = 0; 1811 h_errno = HOST_NOT_FOUND; /* default, if we never query */ 1812 dots = 0; 1813 for (cp = name; *cp; cp++) 1814 dots += (*cp == '.'); 1815 trailing_dot = 0; 1816 if (cp > name && *--cp == '.') 1817 trailing_dot++; 1818 1819 /* 1820 * if there aren't any dots, it could be a user-level alias 1821 */ 1822 if (!dots && (cp = __hostalias(name)) != NULL) 1823 return (res_queryN(cp, target)); 1824 1825 /* 1826 * If there are dots in the name already, let's just give it a try 1827 * 'as is'. The threshold can be set with the "ndots" option. 1828 */ 1829 saved_herrno = -1; 1830 if (dots >= _res.ndots) { 1831 ret = res_querydomainN(name, NULL, target); 1832 if (ret > 0) 1833 return (ret); 1834 saved_herrno = h_errno; 1835 tried_as_is++; 1836 } 1837 1838 /* 1839 * We do at least one level of search if 1840 * - there is no dot and RES_DEFNAME is set, or 1841 * - there is at least one dot, there is no trailing dot, 1842 * and RES_DNSRCH is set. 1843 */ 1844 if ((!dots && (_res.options & RES_DEFNAMES)) || 1845 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 1846 int done = 0; 1847 1848 for (domain = (const char * const *)_res.dnsrch; 1849 *domain && !done; 1850 domain++) { 1851 1852 ret = res_querydomainN(name, *domain, target); 1853 if (ret > 0) 1854 return (ret); 1855 1856 /* 1857 * If no server present, give up. 1858 * If name isn't found in this domain, 1859 * keep trying higher domains in the search list 1860 * (if that's enabled). 1861 * On a NO_DATA error, keep trying, otherwise 1862 * a wildcard entry of another type could keep us 1863 * from finding this entry higher in the domain. 1864 * If we get some other error (negative answer or 1865 * server failure), then stop searching up, 1866 * but try the input name below in case it's 1867 * fully-qualified. 1868 */ 1869 if (errno == ECONNREFUSED) { 1870 h_errno = TRY_AGAIN; 1871 return (-1); 1872 } 1873 1874 switch (h_errno) { 1875 case NO_DATA: 1876 got_nodata++; 1877 /* FALLTHROUGH */ 1878 case HOST_NOT_FOUND: 1879 /* keep trying */ 1880 break; 1881 case TRY_AGAIN: 1882 if (hp->rcode == SERVFAIL) { 1883 /* try next search element, if any */ 1884 got_servfail++; 1885 break; 1886 } 1887 /* FALLTHROUGH */ 1888 default: 1889 /* anything else implies that we're done */ 1890 done++; 1891 } 1892 /* 1893 * if we got here for some reason other than DNSRCH, 1894 * we only wanted one iteration of the loop, so stop. 1895 */ 1896 if (!(_res.options & RES_DNSRCH)) 1897 done++; 1898 } 1899 } 1900 1901 /* 1902 * if we have not already tried the name "as is", do that now. 1903 * note that we do this regardless of how many dots were in the 1904 * name or whether it ends with a dot. 1905 */ 1906 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) { 1907 ret = res_querydomainN(name, NULL, target); 1908 if (ret > 0) 1909 return (ret); 1910 } 1911 1912 /* 1913 * if we got here, we didn't satisfy the search. 1914 * if we did an initial full query, return that query's h_errno 1915 * (note that we wouldn't be here if that query had succeeded). 1916 * else if we ever got a nodata, send that back as the reason. 1917 * else send back meaningless h_errno, that being the one from 1918 * the last DNSRCH we did. 1919 */ 1920 if (saved_herrno != -1) 1921 h_errno = saved_herrno; 1922 else if (got_nodata) 1923 h_errno = NO_DATA; 1924 else if (got_servfail) 1925 h_errno = TRY_AGAIN; 1926 return (-1); 1927 } 1928 1929 /* 1930 * Perform a call on res_query on the concatenation of name and domain, 1931 * removing a trailing dot from name if domain is NULL. 1932 */ 1933 static int 1934 res_querydomainN(name, domain, target) 1935 const char *name, *domain; 1936 struct res_target *target; 1937 { 1938 char nbuf[MAXDNAME]; 1939 const char *longname = nbuf; 1940 size_t n, d; 1941 1942 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 1943 h_errno = NETDB_INTERNAL; 1944 return (-1); 1945 } 1946 #ifdef DEBUG 1947 if (_res.options & RES_DEBUG) 1948 printf(";; res_querydomain(%s, %s)\n", 1949 name, domain?domain:"<Nil>"); 1950 #endif 1951 if (domain == NULL) { 1952 /* 1953 * Check for trailing '.'; 1954 * copy without '.' if present. 1955 */ 1956 n = strlen(name); 1957 if (n >= MAXDNAME) { 1958 h_errno = NO_RECOVERY; 1959 return (-1); 1960 } 1961 if (n > 0 && name[--n] == '.') { 1962 strncpy(nbuf, name, n); 1963 nbuf[n] = '\0'; 1964 } else 1965 longname = name; 1966 } else { 1967 n = strlen(name); 1968 d = strlen(domain); 1969 if (n + d + 1 >= MAXDNAME) { 1970 h_errno = NO_RECOVERY; 1971 return (-1); 1972 } 1973 sprintf(nbuf, "%s.%s", name, domain); 1974 } 1975 return (res_queryN(longname, target)); 1976 } 1977