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