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