getaddrinfo.c revision 157093
1/* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32/* 33 * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 34 * 35 * Issues to be discussed: 36 * - Thread safe-ness must be checked. 37 * - Return values. There are nonstandard return values defined and used 38 * in the source code. This is because RFC2553 is silent about which error 39 * code must be returned for which situation. 40 * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 41 * invalid. current code - SEGV on freeaddrinfo(NULL) 42 * 43 * Note: 44 * - The code filters out AFs that are not supported by the kernel, 45 * when globbing NULL hostname (to loopback, or wildcard). Is it the right 46 * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 47 * in ai_flags? 48 * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 49 * (1) what should we do against numeric hostname (2) what should we do 50 * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 51 * non-loopback address configured? global address configured? 52 * 53 * OS specific notes for netbsd/openbsd/freebsd4/bsdi4: 54 * - To avoid search order issue, we have a big amount of code duplicate 55 * from gethnamaddr.c and some other places. The issues that there's no 56 * lower layer function to lookup "IPv4 or IPv6" record. Calling 57 * gethostbyname2 from getaddrinfo will end up in wrong search order, as 58 * presented above. 59 * 60 * OS specific notes for freebsd4: 61 * - FreeBSD supported $GAI. The code does not. 62 * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not. 63 */ 64 65#include <sys/cdefs.h> 66__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 157093 2006-03-24 13:30:10Z ume $"); 67 68#include "namespace.h" 69#include <sys/types.h> 70#include <sys/param.h> 71#include <sys/socket.h> 72#include <net/if.h> 73#include <netinet/in.h> 74#include <sys/queue.h> 75#ifdef INET6 76#include <net/if_var.h> 77#include <sys/sysctl.h> 78#include <sys/ioctl.h> 79#include <netinet6/in6_var.h> /* XXX */ 80#endif 81#include <arpa/inet.h> 82#include <arpa/nameser.h> 83#include <rpc/rpc.h> 84#include <rpcsvc/yp_prot.h> 85#include <rpcsvc/ypclnt.h> 86#include <netdb.h> 87#include <resolv.h> 88#include <string.h> 89#include <stdlib.h> 90#include <stddef.h> 91#include <ctype.h> 92#include <unistd.h> 93#include <stdio.h> 94#include <errno.h> 95 96#include "res_config.h" 97 98#ifdef DEBUG 99#include <syslog.h> 100#endif 101 102#include <stdarg.h> 103#include <nsswitch.h> 104#include "un-namespace.h" 105#include "libc_private.h" 106 107#if defined(__KAME__) && defined(INET6) 108# define FAITH 109#endif 110 111#define SUCCESS 0 112#define ANY 0 113#define YES 1 114#define NO 0 115 116static const char in_addrany[] = { 0, 0, 0, 0 }; 117static const char in_loopback[] = { 127, 0, 0, 1 }; 118#ifdef INET6 119static const char in6_addrany[] = { 120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 121}; 122static const char in6_loopback[] = { 123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 124}; 125#endif 126 127struct policyqueue { 128 TAILQ_ENTRY(policyqueue) pc_entry; 129#ifdef INET6 130 struct in6_addrpolicy pc_policy; 131#endif 132}; 133TAILQ_HEAD(policyhead, policyqueue); 134 135static const struct afd { 136 int a_af; 137 int a_addrlen; 138 socklen_t a_socklen; 139 int a_off; 140 const char *a_addrany; 141 const char *a_loopback; 142 int a_scoped; 143} afdl [] = { 144#ifdef INET6 145#define N_INET6 0 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#define N_INET 1 151#else 152#define N_INET 0 153#endif 154 {PF_INET, sizeof(struct in_addr), 155 sizeof(struct sockaddr_in), 156 offsetof(struct sockaddr_in, sin_addr), 157 in_addrany, in_loopback, 0}, 158 {0, 0, 0, 0, NULL, NULL, 0}, 159}; 160 161struct explore { 162 int e_af; 163 int e_socktype; 164 int e_protocol; 165 const char *e_protostr; 166 int e_wild; 167#define WILD_AF(ex) ((ex)->e_wild & 0x01) 168#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 169#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 170}; 171 172static const struct explore explore[] = { 173#if 0 174 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 175#endif 176#ifdef INET6 177 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 178 { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 179 { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 180#endif 181 { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 182 { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 183 { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 184 { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 185 { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 186 { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 187 { -1, 0, 0, NULL, 0 }, 188}; 189 190#ifdef INET6 191#define PTON_MAX 16 192#else 193#define PTON_MAX 4 194#endif 195 196#define AIO_SRCFLAG_DEPRECATED 0x1 197 198struct ai_order { 199 union { 200 struct sockaddr_storage aiou_ss; 201 struct sockaddr aiou_sa; 202 } aio_src_un; 203#define aio_srcsa aio_src_un.aiou_sa 204 u_int32_t aio_srcflag; 205 int aio_srcscope; 206 int aio_dstscope; 207 struct policyqueue *aio_srcpolicy; 208 struct policyqueue *aio_dstpolicy; 209 struct addrinfo *aio_ai; 210 int aio_matchlen; 211}; 212 213static const ns_src default_dns_files[] = { 214 { NSSRC_FILES, NS_SUCCESS }, 215 { NSSRC_DNS, NS_SUCCESS }, 216 { 0 } 217}; 218 219struct res_target { 220 struct res_target *next; 221 const char *name; /* domain name */ 222 int qclass, qtype; /* class and type of query */ 223 u_char *answer; /* buffer to put answer */ 224 int anslen; /* size of answer buffer */ 225 int n; /* result length */ 226}; 227 228#define MAXPACKET (64*1024) 229 230typedef union { 231 HEADER hdr; 232 u_char buf[MAXPACKET]; 233} querybuf; 234 235static int str2number(const char *); 236static int explore_null(const struct addrinfo *, 237 const char *, struct addrinfo **); 238static int explore_numeric(const struct addrinfo *, const char *, 239 const char *, struct addrinfo **, const char *); 240static int explore_numeric_scope(const struct addrinfo *, const char *, 241 const char *, struct addrinfo **); 242static int get_canonname(const struct addrinfo *, 243 struct addrinfo *, const char *); 244static struct addrinfo *get_ai(const struct addrinfo *, 245 const struct afd *, const char *); 246static int get_portmatch(const struct addrinfo *, const char *); 247static int get_port(struct addrinfo *, const char *, int); 248static const struct afd *find_afd(int); 249static int addrconfig(struct addrinfo *); 250static void set_source(struct ai_order *, struct policyhead *); 251static int comp_dst(const void *, const void *); 252#ifdef INET6 253static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 254#endif 255static int gai_addr2scopetype(struct sockaddr *); 256 257static int explore_fqdn(const struct addrinfo *, const char *, 258 const char *, struct addrinfo **); 259 260static int reorder(struct addrinfo *); 261static int get_addrselectpolicy(struct policyhead *); 262static void free_addrselectpolicy(struct policyhead *); 263static struct policyqueue *match_addrselectpolicy(struct sockaddr *, 264 struct policyhead *); 265static int matchlen(struct sockaddr *, struct sockaddr *); 266 267static struct addrinfo *getanswer(const querybuf *, int, const char *, int, 268 const struct addrinfo *, res_state); 269#if defined(RESOLVSORT) 270static int addr4sort(struct addrinfo *, res_state); 271#endif 272static int _dns_getaddrinfo(void *, void *, va_list); 273static void _sethtent(FILE **); 274static void _endhtent(FILE **); 275static struct addrinfo *_gethtent(FILE **, const char *, 276 const struct addrinfo *); 277static int _files_getaddrinfo(void *, void *, va_list); 278#ifdef YP 279static struct addrinfo *_yphostent(char *, const struct addrinfo *); 280static int _yp_getaddrinfo(void *, void *, va_list); 281#endif 282 283static int res_queryN(const char *, struct res_target *, res_state); 284static int res_searchN(const char *, struct res_target *, res_state); 285static int res_querydomainN(const char *, const char *, 286 struct res_target *, res_state); 287 288/* XXX macros that make external reference is BAD. */ 289 290#define GET_AI(ai, afd, addr) \ 291do { \ 292 /* external reference: pai, error, and label free */ \ 293 (ai) = get_ai(pai, (afd), (addr)); \ 294 if ((ai) == NULL) { \ 295 error = EAI_MEMORY; \ 296 goto free; \ 297 } \ 298} while (/*CONSTCOND*/0) 299 300#define GET_PORT(ai, serv) \ 301do { \ 302 /* external reference: error and label free */ \ 303 error = get_port((ai), (serv), 0); \ 304 if (error != 0) \ 305 goto free; \ 306} while (/*CONSTCOND*/0) 307 308#define GET_CANONNAME(ai, str) \ 309do { \ 310 /* external reference: pai, error and label free */ \ 311 error = get_canonname(pai, (ai), (str)); \ 312 if (error != 0) \ 313 goto free; \ 314} while (/*CONSTCOND*/0) 315 316#define ERR(err) \ 317do { \ 318 /* external reference: error, and label bad */ \ 319 error = (err); \ 320 goto bad; \ 321 /*NOTREACHED*/ \ 322} while (/*CONSTCOND*/0) 323 324#define MATCH_FAMILY(x, y, w) \ 325 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 326#define MATCH(x, y, w) \ 327 ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 328 329void 330freeaddrinfo(ai) 331 struct addrinfo *ai; 332{ 333 struct addrinfo *next; 334 335 do { 336 next = ai->ai_next; 337 if (ai->ai_canonname) 338 free(ai->ai_canonname); 339 /* no need to free(ai->ai_addr) */ 340 free(ai); 341 ai = next; 342 } while (ai); 343} 344 345static int 346str2number(p) 347 const char *p; 348{ 349 char *ep; 350 unsigned long v; 351 352 if (*p == '\0') 353 return -1; 354 ep = NULL; 355 errno = 0; 356 v = strtoul(p, &ep, 10); 357 if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) 358 return v; 359 else 360 return -1; 361} 362 363int 364getaddrinfo(hostname, servname, hints, res) 365 const char *hostname, *servname; 366 const struct addrinfo *hints; 367 struct addrinfo **res; 368{ 369 struct addrinfo sentinel; 370 struct addrinfo *cur; 371 int error = 0; 372 struct addrinfo ai; 373 struct addrinfo ai0; 374 struct addrinfo *pai; 375 const struct explore *ex; 376 int numeric = 0; 377 378 memset(&sentinel, 0, sizeof(sentinel)); 379 cur = &sentinel; 380 pai = &ai; 381 pai->ai_flags = 0; 382 pai->ai_family = PF_UNSPEC; 383 pai->ai_socktype = ANY; 384 pai->ai_protocol = ANY; 385 pai->ai_addrlen = 0; 386 pai->ai_canonname = NULL; 387 pai->ai_addr = NULL; 388 pai->ai_next = NULL; 389 390 if (hostname == NULL && servname == NULL) 391 return EAI_NONAME; 392 if (hints) { 393 /* error check for hints */ 394 if (hints->ai_addrlen || hints->ai_canonname || 395 hints->ai_addr || hints->ai_next) 396 ERR(EAI_BADHINTS); /* xxx */ 397 if (hints->ai_flags & ~AI_MASK) 398 ERR(EAI_BADFLAGS); 399 switch (hints->ai_family) { 400 case PF_UNSPEC: 401 case PF_INET: 402#ifdef INET6 403 case PF_INET6: 404#endif 405 break; 406 default: 407 ERR(EAI_FAMILY); 408 } 409 memcpy(pai, hints, sizeof(*pai)); 410 411 /* 412 * if both socktype/protocol are specified, check if they 413 * are meaningful combination. 414 */ 415 if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 416 for (ex = explore; ex->e_af >= 0; ex++) { 417 if (pai->ai_family != ex->e_af) 418 continue; 419 if (ex->e_socktype == ANY) 420 continue; 421 if (ex->e_protocol == ANY) 422 continue; 423 if (pai->ai_socktype == ex->e_socktype && 424 pai->ai_protocol != ex->e_protocol) { 425 ERR(EAI_BADHINTS); 426 } 427 } 428 } 429 } 430 431 /* 432 * post-2553: AI_ALL and AI_V4MAPPED are effective only against 433 * AF_INET6 query. They need to be ignored if specified in other 434 * occassions. 435 */ 436 switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 437 case AI_V4MAPPED: 438 case AI_ALL | AI_V4MAPPED: 439 if (pai->ai_family != AF_INET6) 440 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 441 break; 442 case AI_ALL: 443#if 1 444 /* illegal */ 445 ERR(EAI_BADFLAGS); 446#else 447 pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 448#endif 449 break; 450 } 451 452 /* 453 * check for special cases. (1) numeric servname is disallowed if 454 * socktype/protocol are left unspecified. (2) servname is disallowed 455 * for raw and other inet{,6} sockets. 456 */ 457 if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 458#ifdef PF_INET6 459 || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 460#endif 461 ) { 462 ai0 = *pai; /* backup *pai */ 463 464 if (pai->ai_family == PF_UNSPEC) { 465#ifdef PF_INET6 466 pai->ai_family = PF_INET6; 467#else 468 pai->ai_family = PF_INET; 469#endif 470 } 471 error = get_portmatch(pai, servname); 472 if (error) 473 ERR(error); 474 475 *pai = ai0; 476 } 477 478 ai0 = *pai; 479 480 /* NULL hostname, or numeric hostname */ 481 for (ex = explore; ex->e_af >= 0; ex++) { 482 *pai = ai0; 483 484 /* PF_UNSPEC entries are prepared for DNS queries only */ 485 if (ex->e_af == PF_UNSPEC) 486 continue; 487 488 if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 489 continue; 490 if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 491 continue; 492 if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 493 continue; 494 495 if (pai->ai_family == PF_UNSPEC) 496 pai->ai_family = ex->e_af; 497 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 498 pai->ai_socktype = ex->e_socktype; 499 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 500 pai->ai_protocol = ex->e_protocol; 501 502 if (hostname == NULL) 503 error = explore_null(pai, servname, &cur->ai_next); 504 else 505 error = explore_numeric_scope(pai, hostname, servname, 506 &cur->ai_next); 507 508 if (error) 509 goto free; 510 511 while (cur && cur->ai_next) 512 cur = cur->ai_next; 513 } 514 515 /* 516 * XXX 517 * If numreic representation of AF1 can be interpreted as FQDN 518 * representation of AF2, we need to think again about the code below. 519 */ 520 if (sentinel.ai_next) { 521 numeric = 1; 522 goto good; 523 } 524 525 if (hostname == NULL) 526 ERR(EAI_NONAME); /* used to be EAI_NODATA */ 527 if (pai->ai_flags & AI_NUMERICHOST) 528 ERR(EAI_NONAME); 529 530 if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 531 ERR(EAI_FAIL); 532 533 /* 534 * hostname as alphabetical name. 535 * we would like to prefer AF_INET6 than AF_INET, so we'll make a 536 * outer loop by AFs. 537 */ 538 for (ex = explore; ex->e_af >= 0; ex++) { 539 *pai = ai0; 540 541 /* require exact match for family field */ 542 if (pai->ai_family != ex->e_af) 543 continue; 544 545 if (!MATCH(pai->ai_socktype, ex->e_socktype, 546 WILD_SOCKTYPE(ex))) { 547 continue; 548 } 549 if (!MATCH(pai->ai_protocol, ex->e_protocol, 550 WILD_PROTOCOL(ex))) { 551 continue; 552 } 553 554 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 555 pai->ai_socktype = ex->e_socktype; 556 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 557 pai->ai_protocol = ex->e_protocol; 558 559 error = explore_fqdn(pai, hostname, servname, 560 &cur->ai_next); 561 562 while (cur && cur->ai_next) 563 cur = cur->ai_next; 564 } 565 566 /* XXX inhibit errors if we have the result */ 567 if (sentinel.ai_next) 568 error = 0; 569 570good: 571 /* 572 * ensure we return either: 573 * - error == 0, non-NULL *res 574 * - error != 0, NULL *res 575 */ 576 if (error == 0) { 577 if (sentinel.ai_next) { 578 /* 579 * If the returned entry is for an active connection, 580 * and the given name is not numeric, reorder the 581 * list, so that the application would try the list 582 * in the most efficient order. 583 */ 584 if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) { 585 if (!numeric) 586 (void)reorder(&sentinel); 587 } 588 *res = sentinel.ai_next; 589 return SUCCESS; 590 } else 591 error = EAI_FAIL; 592 } 593free: 594bad: 595 if (sentinel.ai_next) 596 freeaddrinfo(sentinel.ai_next); 597 *res = NULL; 598 return error; 599} 600 601static int 602reorder(sentinel) 603 struct addrinfo *sentinel; 604{ 605 struct addrinfo *ai, **aip; 606 struct ai_order *aio; 607 int i, n; 608 struct policyhead policyhead; 609 610 /* count the number of addrinfo elements for sorting. */ 611 for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++) 612 ; 613 614 /* 615 * If the number is small enough, we can skip the reordering process. 616 */ 617 if (n <= 1) 618 return(n); 619 620 /* allocate a temporary array for sort and initialization of it. */ 621 if ((aio = malloc(sizeof(*aio) * n)) == NULL) 622 return(n); /* give up reordering */ 623 memset(aio, 0, sizeof(*aio) * n); 624 625 /* retrieve address selection policy from the kernel */ 626 TAILQ_INIT(&policyhead); 627 if (!get_addrselectpolicy(&policyhead)) { 628 /* no policy is installed into kernel, we don't sort. */ 629 free(aio); 630 return (n); 631 } 632 633 for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) { 634 aio[i].aio_ai = ai; 635 aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr); 636 aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr, 637 &policyhead); 638 set_source(&aio[i], &policyhead); 639 } 640 641 /* perform sorting. */ 642 qsort(aio, n, sizeof(*aio), comp_dst); 643 644 /* reorder the addrinfo chain. */ 645 for (i = 0, aip = &sentinel->ai_next; i < n; i++) { 646 *aip = aio[i].aio_ai; 647 aip = &aio[i].aio_ai->ai_next; 648 } 649 *aip = NULL; 650 651 /* cleanup and return */ 652 free(aio); 653 free_addrselectpolicy(&policyhead); 654 return(n); 655} 656 657static int 658get_addrselectpolicy(head) 659 struct policyhead *head; 660{ 661#ifdef INET6 662 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 663 size_t l; 664 char *buf; 665 struct in6_addrpolicy *pol, *ep; 666 667 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) 668 return (0); 669 if ((buf = malloc(l)) == NULL) 670 return (0); 671 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 672 free(buf); 673 return (0); 674 } 675 676 ep = (struct in6_addrpolicy *)(buf + l); 677 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 678 struct policyqueue *new; 679 680 if ((new = malloc(sizeof(*new))) == NULL) { 681 free_addrselectpolicy(head); /* make the list empty */ 682 break; 683 } 684 new->pc_policy = *pol; 685 TAILQ_INSERT_TAIL(head, new, pc_entry); 686 } 687 688 free(buf); 689 return (1); 690#else 691 return (0); 692#endif 693} 694 695static void 696free_addrselectpolicy(head) 697 struct policyhead *head; 698{ 699 struct policyqueue *ent, *nent; 700 701 for (ent = TAILQ_FIRST(head); ent; ent = nent) { 702 nent = TAILQ_NEXT(ent, pc_entry); 703 TAILQ_REMOVE(head, ent, pc_entry); 704 free(ent); 705 } 706} 707 708static struct policyqueue * 709match_addrselectpolicy(addr, head) 710 struct sockaddr *addr; 711 struct policyhead *head; 712{ 713#ifdef INET6 714 struct policyqueue *ent, *bestent = NULL; 715 struct in6_addrpolicy *pol; 716 int matchlen, bestmatchlen = -1; 717 u_char *mp, *ep, *k, *p, m; 718 struct sockaddr_in6 key; 719 720 switch(addr->sa_family) { 721 case AF_INET6: 722 key = *(struct sockaddr_in6 *)addr; 723 break; 724 case AF_INET: 725 /* convert the address into IPv4-mapped IPv6 address. */ 726 memset(&key, 0, sizeof(key)); 727 key.sin6_family = AF_INET6; 728 key.sin6_len = sizeof(key); 729 key.sin6_addr.s6_addr[10] = 0xff; 730 key.sin6_addr.s6_addr[11] = 0xff; 731 memcpy(&key.sin6_addr.s6_addr[12], 732 &((struct sockaddr_in *)addr)->sin_addr, 4); 733 break; 734 default: 735 return(NULL); 736 } 737 738 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 739 pol = &ent->pc_policy; 740 matchlen = 0; 741 742 mp = (u_char *)&pol->addrmask.sin6_addr; 743 ep = mp + 16; /* XXX: scope field? */ 744 k = (u_char *)&key.sin6_addr; 745 p = (u_char *)&pol->addr.sin6_addr; 746 for (; mp < ep && *mp; mp++, k++, p++) { 747 m = *mp; 748 if ((*k & m) != *p) 749 goto next; /* not match */ 750 if (m == 0xff) /* short cut for a typical case */ 751 matchlen += 8; 752 else { 753 while (m >= 0x80) { 754 matchlen++; 755 m <<= 1; 756 } 757 } 758 } 759 760 /* matched. check if this is better than the current best. */ 761 if (matchlen > bestmatchlen) { 762 bestent = ent; 763 bestmatchlen = matchlen; 764 } 765 766 next: 767 continue; 768 } 769 770 return(bestent); 771#else 772 return(NULL); 773#endif 774 775} 776 777static void 778set_source(aio, ph) 779 struct ai_order *aio; 780 struct policyhead *ph; 781{ 782 struct addrinfo ai = *aio->aio_ai; 783 struct sockaddr_storage ss; 784 socklen_t srclen; 785 int s; 786 787 /* set unspec ("no source is available"), just in case */ 788 aio->aio_srcsa.sa_family = AF_UNSPEC; 789 aio->aio_srcscope = -1; 790 791 switch(ai.ai_family) { 792 case AF_INET: 793#ifdef INET6 794 case AF_INET6: 795#endif 796 break; 797 default: /* ignore unsupported AFs explicitly */ 798 return; 799 } 800 801 /* XXX: make a dummy addrinfo to call connect() */ 802 ai.ai_socktype = SOCK_DGRAM; 803 ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */ 804 ai.ai_next = NULL; 805 memset(&ss, 0, sizeof(ss)); 806 memcpy(&ss, ai.ai_addr, ai.ai_addrlen); 807 ai.ai_addr = (struct sockaddr *)&ss; 808 get_port(&ai, "1", 0); 809 810 /* open a socket to get the source address for the given dst */ 811 if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0) 812 return; /* give up */ 813 if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0) 814 goto cleanup; 815 srclen = ai.ai_addrlen; 816 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 817 aio->aio_srcsa.sa_family = AF_UNSPEC; 818 goto cleanup; 819 } 820 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 821 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 822 aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr); 823#ifdef INET6 824 if (ai.ai_family == AF_INET6) { 825 struct in6_ifreq ifr6; 826 u_int32_t flags6; 827 828 /* XXX: interface name should not be hardcoded */ 829 strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name)); 830 memset(&ifr6, 0, sizeof(ifr6)); 831 memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen); 832 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 833 flags6 = ifr6.ifr_ifru.ifru_flags6; 834 if ((flags6 & IN6_IFF_DEPRECATED)) 835 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 836 } 837 } 838#endif 839 840 cleanup: 841 _close(s); 842 return; 843} 844 845static int 846matchlen(src, dst) 847 struct sockaddr *src, *dst; 848{ 849 int match = 0; 850 u_char *s, *d; 851 u_char *lim, r; 852 int addrlen; 853 854 switch (src->sa_family) { 855#ifdef INET6 856 case AF_INET6: 857 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 858 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 859 addrlen = sizeof(struct in6_addr); 860 lim = s + addrlen; 861 break; 862#endif 863 case AF_INET: 864 s = (u_char *)&((struct sockaddr_in *)src)->sin_addr; 865 d = (u_char *)&((struct sockaddr_in *)dst)->sin_addr; 866 addrlen = sizeof(struct in_addr); 867 lim = s + addrlen; 868 break; 869 default: 870 return(0); 871 } 872 873 while (s < lim) 874 if ((r = (*d++ ^ *s++)) != 0) { 875 while (r < addrlen * 8) { 876 match++; 877 r <<= 1; 878 } 879 break; 880 } else 881 match += 8; 882 return(match); 883} 884 885static int 886comp_dst(arg1, arg2) 887 const void *arg1, *arg2; 888{ 889 const struct ai_order *dst1 = arg1, *dst2 = arg2; 890 891 /* 892 * Rule 1: Avoid unusable destinations. 893 * XXX: we currently do not consider if an appropriate route exists. 894 */ 895 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 896 dst2->aio_srcsa.sa_family == AF_UNSPEC) { 897 return(-1); 898 } 899 if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 900 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 901 return(1); 902 } 903 904 /* Rule 2: Prefer matching scope. */ 905 if (dst1->aio_dstscope == dst1->aio_srcscope && 906 dst2->aio_dstscope != dst2->aio_srcscope) { 907 return(-1); 908 } 909 if (dst1->aio_dstscope != dst1->aio_srcscope && 910 dst2->aio_dstscope == dst2->aio_srcscope) { 911 return(1); 912 } 913 914 /* Rule 3: Avoid deprecated addresses. */ 915 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 916 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 917 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 918 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 919 return(-1); 920 } 921 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 922 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 923 return(1); 924 } 925 } 926 927 /* Rule 4: Prefer home addresses. */ 928 /* XXX: not implemented yet */ 929 930 /* Rule 5: Prefer matching label. */ 931#ifdef INET6 932 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 933 dst1->aio_srcpolicy->pc_policy.label == 934 dst1->aio_dstpolicy->pc_policy.label && 935 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 936 dst2->aio_srcpolicy->pc_policy.label != 937 dst2->aio_dstpolicy->pc_policy.label)) { 938 return(-1); 939 } 940 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 941 dst2->aio_srcpolicy->pc_policy.label == 942 dst2->aio_dstpolicy->pc_policy.label && 943 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 944 dst1->aio_srcpolicy->pc_policy.label != 945 dst1->aio_dstpolicy->pc_policy.label)) { 946 return(1); 947 } 948#endif 949 950 /* Rule 6: Prefer higher precedence. */ 951#ifdef INET6 952 if (dst1->aio_dstpolicy && 953 (dst2->aio_dstpolicy == NULL || 954 dst1->aio_dstpolicy->pc_policy.preced > 955 dst2->aio_dstpolicy->pc_policy.preced)) { 956 return(-1); 957 } 958 if (dst2->aio_dstpolicy && 959 (dst1->aio_dstpolicy == NULL || 960 dst2->aio_dstpolicy->pc_policy.preced > 961 dst1->aio_dstpolicy->pc_policy.preced)) { 962 return(1); 963 } 964#endif 965 966 /* Rule 7: Prefer native transport. */ 967 /* XXX: not implemented yet */ 968 969 /* Rule 8: Prefer smaller scope. */ 970 if (dst1->aio_dstscope >= 0 && 971 dst1->aio_dstscope < dst2->aio_dstscope) { 972 return(-1); 973 } 974 if (dst2->aio_dstscope >= 0 && 975 dst2->aio_dstscope < dst1->aio_dstscope) { 976 return(1); 977 } 978 979 /* 980 * Rule 9: Use longest matching prefix. 981 * We compare the match length in a same AF only. 982 */ 983 if (dst1->aio_ai->ai_addr->sa_family == 984 dst2->aio_ai->ai_addr->sa_family) { 985 if (dst1->aio_matchlen > dst2->aio_matchlen) { 986 return(-1); 987 } 988 if (dst1->aio_matchlen < dst2->aio_matchlen) { 989 return(1); 990 } 991 } 992 993 /* Rule 10: Otherwise, leave the order unchanged. */ 994 return(-1); 995} 996 997/* 998 * Copy from scope.c. 999 * XXX: we should standardize the functions and link them as standard 1000 * library. 1001 */ 1002static int 1003gai_addr2scopetype(sa) 1004 struct sockaddr *sa; 1005{ 1006#ifdef INET6 1007 struct sockaddr_in6 *sa6; 1008#endif 1009 struct sockaddr_in *sa4; 1010 1011 switch(sa->sa_family) { 1012#ifdef INET6 1013 case AF_INET6: 1014 sa6 = (struct sockaddr_in6 *)sa; 1015 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 1016 /* just use the scope field of the multicast address */ 1017 return(sa6->sin6_addr.s6_addr[2] & 0x0f); 1018 } 1019 /* 1020 * Unicast addresses: map scope type to corresponding scope 1021 * value defined for multcast addresses. 1022 * XXX: hardcoded scope type values are bad... 1023 */ 1024 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 1025 return(1); /* node local scope */ 1026 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 1027 return(2); /* link-local scope */ 1028 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 1029 return(5); /* site-local scope */ 1030 return(14); /* global scope */ 1031 break; 1032#endif 1033 case AF_INET: 1034 /* 1035 * IPv4 pseudo scoping according to RFC 3484. 1036 */ 1037 sa4 = (struct sockaddr_in *)sa; 1038 /* IPv4 autoconfiguration addresses have link-local scope. */ 1039 if (((u_char *)&sa4->sin_addr)[0] == 169 && 1040 ((u_char *)&sa4->sin_addr)[1] == 254) 1041 return(2); 1042 /* Private addresses have site-local scope. */ 1043 if (((u_char *)&sa4->sin_addr)[0] == 10 || 1044 (((u_char *)&sa4->sin_addr)[0] == 172 && 1045 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 1046 (((u_char *)&sa4->sin_addr)[0] == 192 && 1047 ((u_char *)&sa4->sin_addr)[1] == 168)) 1048 return(14); /* XXX: It should be 5 unless NAT */ 1049 /* Loopback addresses have link-local scope. */ 1050 if (((u_char *)&sa4->sin_addr)[0] == 127) 1051 return(2); 1052 return(14); 1053 break; 1054 default: 1055 errno = EAFNOSUPPORT; /* is this a good error? */ 1056 return(-1); 1057 } 1058} 1059 1060/* 1061 * hostname == NULL. 1062 * passive socket -> anyaddr (0.0.0.0 or ::) 1063 * non-passive socket -> localhost (127.0.0.1 or ::1) 1064 */ 1065static int 1066explore_null(pai, servname, res) 1067 const struct addrinfo *pai; 1068 const char *servname; 1069 struct addrinfo **res; 1070{ 1071 int s; 1072 const struct afd *afd; 1073 struct addrinfo *cur; 1074 struct addrinfo sentinel; 1075 int error; 1076 1077 *res = NULL; 1078 sentinel.ai_next = NULL; 1079 cur = &sentinel; 1080 1081 /* 1082 * filter out AFs that are not supported by the kernel 1083 * XXX errno? 1084 */ 1085 s = _socket(pai->ai_family, SOCK_DGRAM, 0); 1086 if (s < 0) { 1087 if (errno != EMFILE) 1088 return 0; 1089 } else 1090 _close(s); 1091 1092 /* 1093 * if the servname does not match socktype/protocol, ignore it. 1094 */ 1095 if (get_portmatch(pai, servname) != 0) 1096 return 0; 1097 1098 afd = find_afd(pai->ai_family); 1099 if (afd == NULL) 1100 return 0; 1101 1102 if (pai->ai_flags & AI_PASSIVE) { 1103 GET_AI(cur->ai_next, afd, afd->a_addrany); 1104 /* xxx meaningless? 1105 * GET_CANONNAME(cur->ai_next, "anyaddr"); 1106 */ 1107 GET_PORT(cur->ai_next, servname); 1108 } else { 1109 GET_AI(cur->ai_next, afd, afd->a_loopback); 1110 /* xxx meaningless? 1111 * GET_CANONNAME(cur->ai_next, "localhost"); 1112 */ 1113 GET_PORT(cur->ai_next, servname); 1114 } 1115 cur = cur->ai_next; 1116 1117 *res = sentinel.ai_next; 1118 return 0; 1119 1120free: 1121 if (sentinel.ai_next) 1122 freeaddrinfo(sentinel.ai_next); 1123 return error; 1124} 1125 1126/* 1127 * numeric hostname 1128 */ 1129static int 1130explore_numeric(pai, hostname, servname, res, canonname) 1131 const struct addrinfo *pai; 1132 const char *hostname; 1133 const char *servname; 1134 struct addrinfo **res; 1135 const char *canonname; 1136{ 1137 const struct afd *afd; 1138 struct addrinfo *cur; 1139 struct addrinfo sentinel; 1140 int error; 1141 char pton[PTON_MAX]; 1142 1143 *res = NULL; 1144 sentinel.ai_next = NULL; 1145 cur = &sentinel; 1146 1147 /* 1148 * if the servname does not match socktype/protocol, ignore it. 1149 */ 1150 if (get_portmatch(pai, servname) != 0) 1151 return 0; 1152 1153 afd = find_afd(pai->ai_family); 1154 if (afd == NULL) 1155 return 0; 1156 1157 switch (afd->a_af) { 1158#if 1 /*X/Open spec*/ 1159 case AF_INET: 1160 if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 1161 if (pai->ai_family == afd->a_af || 1162 pai->ai_family == PF_UNSPEC /*?*/) { 1163 GET_AI(cur->ai_next, afd, pton); 1164 GET_PORT(cur->ai_next, servname); 1165 if ((pai->ai_flags & AI_CANONNAME)) { 1166 /* 1167 * Set the numeric address itself as 1168 * the canonical name, based on a 1169 * clarification in rfc3493. 1170 */ 1171 GET_CANONNAME(cur->ai_next, canonname); 1172 } 1173 while (cur && cur->ai_next) 1174 cur = cur->ai_next; 1175 } else 1176 ERR(EAI_FAMILY); /*xxx*/ 1177 } 1178 break; 1179#endif 1180 default: 1181 if (inet_pton(afd->a_af, hostname, pton) == 1) { 1182 if (pai->ai_family == afd->a_af || 1183 pai->ai_family == PF_UNSPEC /*?*/) { 1184 GET_AI(cur->ai_next, afd, pton); 1185 GET_PORT(cur->ai_next, servname); 1186 if ((pai->ai_flags & AI_CANONNAME)) { 1187 /* 1188 * Set the numeric address itself as 1189 * the canonical name, based on a 1190 * clarification in rfc3493. 1191 */ 1192 GET_CANONNAME(cur->ai_next, canonname); 1193 } 1194 while (cur && cur->ai_next) 1195 cur = cur->ai_next; 1196 } else 1197 ERR(EAI_FAMILY); /* XXX */ 1198 } 1199 break; 1200 } 1201 1202 *res = sentinel.ai_next; 1203 return 0; 1204 1205free: 1206bad: 1207 if (sentinel.ai_next) 1208 freeaddrinfo(sentinel.ai_next); 1209 return error; 1210} 1211 1212/* 1213 * numeric hostname with scope 1214 */ 1215static int 1216explore_numeric_scope(pai, hostname, servname, res) 1217 const struct addrinfo *pai; 1218 const char *hostname; 1219 const char *servname; 1220 struct addrinfo **res; 1221{ 1222#if !defined(SCOPE_DELIMITER) || !defined(INET6) 1223 return explore_numeric(pai, hostname, servname, res, hostname); 1224#else 1225 const struct afd *afd; 1226 struct addrinfo *cur; 1227 int error; 1228 char *cp, *hostname2 = NULL, *scope, *addr; 1229 struct sockaddr_in6 *sin6; 1230 1231 /* 1232 * if the servname does not match socktype/protocol, ignore it. 1233 */ 1234 if (get_portmatch(pai, servname) != 0) 1235 return 0; 1236 1237 afd = find_afd(pai->ai_family); 1238 if (afd == NULL) 1239 return 0; 1240 1241 if (!afd->a_scoped) 1242 return explore_numeric(pai, hostname, servname, res, hostname); 1243 1244 cp = strchr(hostname, SCOPE_DELIMITER); 1245 if (cp == NULL) 1246 return explore_numeric(pai, hostname, servname, res, hostname); 1247 1248 /* 1249 * Handle special case of <scoped_address><delimiter><scope id> 1250 */ 1251 hostname2 = strdup(hostname); 1252 if (hostname2 == NULL) 1253 return EAI_MEMORY; 1254 /* terminate at the delimiter */ 1255 hostname2[cp - hostname] = '\0'; 1256 addr = hostname2; 1257 scope = cp + 1; 1258 1259 error = explore_numeric(pai, addr, servname, res, hostname); 1260 if (error == 0) { 1261 u_int32_t scopeid; 1262 1263 for (cur = *res; cur; cur = cur->ai_next) { 1264 if (cur->ai_family != AF_INET6) 1265 continue; 1266 sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 1267 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { 1268 free(hostname2); 1269 return(EAI_NONAME); /* XXX: is return OK? */ 1270 } 1271 sin6->sin6_scope_id = scopeid; 1272 } 1273 } 1274 1275 free(hostname2); 1276 1277 return error; 1278#endif 1279} 1280 1281static int 1282get_canonname(pai, ai, str) 1283 const struct addrinfo *pai; 1284 struct addrinfo *ai; 1285 const char *str; 1286{ 1287 if ((pai->ai_flags & AI_CANONNAME) != 0) { 1288 ai->ai_canonname = strdup(str); 1289 if (ai->ai_canonname == NULL) 1290 return EAI_MEMORY; 1291 } 1292 return 0; 1293} 1294 1295static struct addrinfo * 1296get_ai(pai, afd, addr) 1297 const struct addrinfo *pai; 1298 const struct afd *afd; 1299 const char *addr; 1300{ 1301 char *p; 1302 struct addrinfo *ai; 1303#ifdef FAITH 1304 struct in6_addr faith_prefix; 1305 char *fp_str; 1306 int translate = 0; 1307#endif 1308 1309#ifdef FAITH 1310 /* 1311 * Transfrom an IPv4 addr into a special IPv6 addr format for 1312 * IPv6->IPv4 translation gateway. (only TCP is supported now) 1313 * 1314 * +-----------------------------------+------------+ 1315 * | faith prefix part (12 bytes) | embedded | 1316 * | | IPv4 addr part (4 bytes) 1317 * +-----------------------------------+------------+ 1318 * 1319 * faith prefix part is specified as ascii IPv6 addr format 1320 * in environmental variable GAI. 1321 * For FAITH to work correctly, routing to faith prefix must be 1322 * setup toward a machine where a FAITH daemon operates. 1323 * Also, the machine must enable some mechanizm 1324 * (e.g. faith interface hack) to divert those packet with 1325 * faith prefixed destination addr to user-land FAITH daemon. 1326 */ 1327 fp_str = getenv("GAI"); 1328 if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 1329 afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 1330 u_int32_t v4a; 1331 u_int8_t v4a_top; 1332 1333 memcpy(&v4a, addr, sizeof v4a); 1334 v4a_top = v4a >> IN_CLASSA_NSHIFT; 1335 if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 1336 v4a_top != 0 && v4a != IN_LOOPBACKNET) { 1337 afd = &afdl[N_INET6]; 1338 memcpy(&faith_prefix.s6_addr[12], addr, 1339 sizeof(struct in_addr)); 1340 translate = 1; 1341 } 1342 } 1343#endif 1344 1345 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 1346 + (afd->a_socklen)); 1347 if (ai == NULL) 1348 return NULL; 1349 1350 memcpy(ai, pai, sizeof(struct addrinfo)); 1351 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1352 memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 1353 ai->ai_addr->sa_len = afd->a_socklen; 1354 ai->ai_addrlen = afd->a_socklen; 1355 ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 1356 p = (char *)(void *)(ai->ai_addr); 1357#ifdef FAITH 1358 if (translate == 1) 1359 memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 1360 else 1361#endif 1362 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 1363 return ai; 1364} 1365 1366static int 1367get_portmatch(ai, servname) 1368 const struct addrinfo *ai; 1369 const char *servname; 1370{ 1371 1372 /* get_port does not touch first argument when matchonly == 1. */ 1373 /* LINTED const cast */ 1374 return get_port((struct addrinfo *)ai, servname, 1); 1375} 1376 1377static int 1378get_port(ai, servname, matchonly) 1379 struct addrinfo *ai; 1380 const char *servname; 1381 int matchonly; 1382{ 1383 const char *proto; 1384 struct servent *sp; 1385 int port; 1386 int allownumeric; 1387 1388 if (servname == NULL) 1389 return 0; 1390 switch (ai->ai_family) { 1391 case AF_INET: 1392#ifdef AF_INET6 1393 case AF_INET6: 1394#endif 1395 break; 1396 default: 1397 return 0; 1398 } 1399 1400 switch (ai->ai_socktype) { 1401 case SOCK_RAW: 1402 return EAI_SERVICE; 1403 case SOCK_DGRAM: 1404 case SOCK_STREAM: 1405 allownumeric = 1; 1406 break; 1407 case ANY: 1408 allownumeric = 0; 1409 break; 1410 default: 1411 return EAI_SOCKTYPE; 1412 } 1413 1414 port = str2number(servname); 1415 if (port >= 0) { 1416 if (!allownumeric) 1417 return EAI_SERVICE; 1418 if (port < 0 || port > 65535) 1419 return EAI_SERVICE; 1420 port = htons(port); 1421 } else { 1422 if (ai->ai_flags & AI_NUMERICSERV) 1423 return EAI_NONAME; 1424 switch (ai->ai_socktype) { 1425 case SOCK_DGRAM: 1426 proto = "udp"; 1427 break; 1428 case SOCK_STREAM: 1429 proto = "tcp"; 1430 break; 1431 default: 1432 proto = NULL; 1433 break; 1434 } 1435 1436 if ((sp = getservbyname(servname, proto)) == NULL) 1437 return EAI_SERVICE; 1438 port = sp->s_port; 1439 } 1440 1441 if (!matchonly) { 1442 switch (ai->ai_family) { 1443 case AF_INET: 1444 ((struct sockaddr_in *)(void *) 1445 ai->ai_addr)->sin_port = port; 1446 break; 1447#ifdef INET6 1448 case AF_INET6: 1449 ((struct sockaddr_in6 *)(void *) 1450 ai->ai_addr)->sin6_port = port; 1451 break; 1452#endif 1453 } 1454 } 1455 1456 return 0; 1457} 1458 1459static const struct afd * 1460find_afd(af) 1461 int af; 1462{ 1463 const struct afd *afd; 1464 1465 if (af == PF_UNSPEC) 1466 return NULL; 1467 for (afd = afdl; afd->a_af; afd++) { 1468 if (afd->a_af == af) 1469 return afd; 1470 } 1471 return NULL; 1472} 1473 1474/* 1475 * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1476 * will take care of it. 1477 * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1478 * if the code is right or not. 1479 * 1480 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1481 * _dns_getaddrinfo. 1482 */ 1483static int 1484addrconfig(pai) 1485 struct addrinfo *pai; 1486{ 1487 int s, af; 1488 1489 /* 1490 * TODO: 1491 * Note that implementation dependent test for address 1492 * configuration should be done everytime called 1493 * (or apropriate interval), 1494 * because addresses will be dynamically assigned or deleted. 1495 */ 1496 af = pai->ai_family; 1497 if (af == AF_UNSPEC) { 1498 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1499 af = AF_INET; 1500 else { 1501 _close(s); 1502 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1503 af = AF_INET6; 1504 else 1505 _close(s); 1506 } 1507 } 1508 if (af != AF_UNSPEC) { 1509 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 1510 return 0; 1511 _close(s); 1512 } 1513 pai->ai_family = af; 1514 return 1; 1515} 1516 1517#ifdef INET6 1518/* convert a string to a scope identifier. XXX: IPv6 specific */ 1519static int 1520ip6_str2scopeid(scope, sin6, scopeid) 1521 char *scope; 1522 struct sockaddr_in6 *sin6; 1523 u_int32_t *scopeid; 1524{ 1525 u_long lscopeid; 1526 struct in6_addr *a6; 1527 char *ep; 1528 1529 a6 = &sin6->sin6_addr; 1530 1531 /* empty scopeid portion is invalid */ 1532 if (*scope == '\0') 1533 return -1; 1534 1535 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1536 /* 1537 * We currently assume a one-to-one mapping between links 1538 * and interfaces, so we simply use interface indices for 1539 * like-local scopes. 1540 */ 1541 *scopeid = if_nametoindex(scope); 1542 if (*scopeid == 0) 1543 goto trynumeric; 1544 return 0; 1545 } 1546 1547 /* still unclear about literal, allow numeric only - placeholder */ 1548 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1549 goto trynumeric; 1550 if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1551 goto trynumeric; 1552 else 1553 goto trynumeric; /* global */ 1554 1555 /* try to convert to a numeric id as a last resort */ 1556 trynumeric: 1557 errno = 0; 1558 lscopeid = strtoul(scope, &ep, 10); 1559 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 1560 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 1561 return 0; 1562 else 1563 return -1; 1564} 1565#endif 1566 1567/* 1568 * FQDN hostname, DNS lookup 1569 */ 1570static int 1571explore_fqdn(pai, hostname, servname, res) 1572 const struct addrinfo *pai; 1573 const char *hostname; 1574 const char *servname; 1575 struct addrinfo **res; 1576{ 1577 struct addrinfo *result; 1578 struct addrinfo *cur; 1579 int error = 0; 1580 static const ns_dtab dtab[] = { 1581 NS_FILES_CB(_files_getaddrinfo, NULL) 1582 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 1583 NS_NIS_CB(_yp_getaddrinfo, NULL) 1584 { 0 } 1585 }; 1586 1587 result = NULL; 1588 1589 /* 1590 * if the servname does not match socktype/protocol, ignore it. 1591 */ 1592 if (get_portmatch(pai, servname) != 0) 1593 return 0; 1594 1595 switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 1596 default_dns_files, hostname, pai)) { 1597 case NS_TRYAGAIN: 1598 error = EAI_AGAIN; 1599 goto free; 1600 case NS_UNAVAIL: 1601 error = EAI_FAIL; 1602 goto free; 1603 case NS_NOTFOUND: 1604 error = EAI_NONAME; 1605 goto free; 1606 case NS_SUCCESS: 1607 error = 0; 1608 for (cur = result; cur; cur = cur->ai_next) { 1609 GET_PORT(cur, servname); 1610 /* canonname should be filled already */ 1611 } 1612 break; 1613 } 1614 1615 *res = result; 1616 1617 return 0; 1618 1619free: 1620 if (result) 1621 freeaddrinfo(result); 1622 return error; 1623} 1624 1625#ifdef DEBUG 1626static const char AskedForGot[] = 1627 "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1628#endif 1629 1630static struct addrinfo * 1631getanswer(answer, anslen, qname, qtype, pai, res) 1632 const querybuf *answer; 1633 int anslen; 1634 const char *qname; 1635 int qtype; 1636 const struct addrinfo *pai; 1637 res_state res; 1638{ 1639 struct addrinfo sentinel, *cur; 1640 struct addrinfo ai; 1641 const struct afd *afd; 1642 char *canonname; 1643 const HEADER *hp; 1644 const u_char *cp; 1645 int n; 1646 const u_char *eom; 1647 char *bp, *ep; 1648 int type, class, ancount, qdcount; 1649 int haveanswer, had_error; 1650 char tbuf[MAXDNAME]; 1651 int (*name_ok)(const char *); 1652 char hostbuf[8*1024]; 1653 1654 memset(&sentinel, 0, sizeof(sentinel)); 1655 cur = &sentinel; 1656 1657 canonname = NULL; 1658 eom = answer->buf + anslen; 1659 switch (qtype) { 1660 case T_A: 1661 case T_AAAA: 1662 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1663 name_ok = res_hnok; 1664 break; 1665 default: 1666 return (NULL); /* XXX should be abort(); */ 1667 } 1668 /* 1669 * find first satisfactory answer 1670 */ 1671 hp = &answer->hdr; 1672 ancount = ntohs(hp->ancount); 1673 qdcount = ntohs(hp->qdcount); 1674 bp = hostbuf; 1675 ep = hostbuf + sizeof hostbuf; 1676 cp = answer->buf + HFIXEDSZ; 1677 if (qdcount != 1) { 1678 RES_SET_H_ERRNO(res, NO_RECOVERY); 1679 return (NULL); 1680 } 1681 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1682 if ((n < 0) || !(*name_ok)(bp)) { 1683 RES_SET_H_ERRNO(res, NO_RECOVERY); 1684 return (NULL); 1685 } 1686 cp += n + QFIXEDSZ; 1687 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1688 /* res_send() has already verified that the query name is the 1689 * same as the one we sent; this just gets the expanded name 1690 * (i.e., with the succeeding search-domain tacked on). 1691 */ 1692 n = strlen(bp) + 1; /* for the \0 */ 1693 if (n >= MAXHOSTNAMELEN) { 1694 RES_SET_H_ERRNO(res, NO_RECOVERY); 1695 return (NULL); 1696 } 1697 canonname = bp; 1698 bp += n; 1699 /* The qname can be abbreviated, but h_name is now absolute. */ 1700 qname = canonname; 1701 } 1702 haveanswer = 0; 1703 had_error = 0; 1704 while (ancount-- > 0 && cp < eom && !had_error) { 1705 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1706 if ((n < 0) || !(*name_ok)(bp)) { 1707 had_error++; 1708 continue; 1709 } 1710 cp += n; /* name */ 1711 type = _getshort(cp); 1712 cp += INT16SZ; /* type */ 1713 class = _getshort(cp); 1714 cp += INT16SZ + INT32SZ; /* class, TTL */ 1715 n = _getshort(cp); 1716 cp += INT16SZ; /* len */ 1717 if (class != C_IN) { 1718 /* XXX - debug? syslog? */ 1719 cp += n; 1720 continue; /* XXX - had_error++ ? */ 1721 } 1722 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1723 type == T_CNAME) { 1724 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1725 if ((n < 0) || !(*name_ok)(tbuf)) { 1726 had_error++; 1727 continue; 1728 } 1729 cp += n; 1730 /* Get canonical name. */ 1731 n = strlen(tbuf) + 1; /* for the \0 */ 1732 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1733 had_error++; 1734 continue; 1735 } 1736 strlcpy(bp, tbuf, ep - bp); 1737 canonname = bp; 1738 bp += n; 1739 continue; 1740 } 1741 if (qtype == T_ANY) { 1742 if (!(type == T_A || type == T_AAAA)) { 1743 cp += n; 1744 continue; 1745 } 1746 } else if (type != qtype) { 1747#ifdef DEBUG 1748 if (type != T_KEY && type != T_SIG) 1749 syslog(LOG_NOTICE|LOG_AUTH, 1750 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1751 qname, p_class(C_IN), p_type(qtype), 1752 p_type(type)); 1753#endif 1754 cp += n; 1755 continue; /* XXX - had_error++ ? */ 1756 } 1757 switch (type) { 1758 case T_A: 1759 case T_AAAA: 1760 if (strcasecmp(canonname, bp) != 0) { 1761#ifdef DEBUG 1762 syslog(LOG_NOTICE|LOG_AUTH, 1763 AskedForGot, canonname, bp); 1764#endif 1765 cp += n; 1766 continue; /* XXX - had_error++ ? */ 1767 } 1768 if (type == T_A && n != INADDRSZ) { 1769 cp += n; 1770 continue; 1771 } 1772 if (type == T_AAAA && n != IN6ADDRSZ) { 1773 cp += n; 1774 continue; 1775 } 1776#ifdef FILTER_V4MAPPED 1777 if (type == T_AAAA) { 1778 struct in6_addr in6; 1779 memcpy(&in6, cp, sizeof(in6)); 1780 if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1781 cp += n; 1782 continue; 1783 } 1784 } 1785#endif 1786 if (!haveanswer) { 1787 int nn; 1788 1789 canonname = bp; 1790 nn = strlen(bp) + 1; /* for the \0 */ 1791 bp += nn; 1792 } 1793 1794 /* don't overwrite pai */ 1795 ai = *pai; 1796 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1797 afd = find_afd(ai.ai_family); 1798 if (afd == NULL) { 1799 cp += n; 1800 continue; 1801 } 1802 cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1803 if (cur->ai_next == NULL) 1804 had_error++; 1805 while (cur && cur->ai_next) 1806 cur = cur->ai_next; 1807 cp += n; 1808 break; 1809 default: 1810 abort(); 1811 } 1812 if (!had_error) 1813 haveanswer++; 1814 } 1815 if (haveanswer) { 1816#if defined(RESOLVSORT) 1817 /* 1818 * We support only IPv4 address for backward 1819 * compatibility against gethostbyname(3). 1820 */ 1821 if (res->nsort && qtype == T_A) { 1822 if (addr4sort(&sentinel, res) < 0) { 1823 freeaddrinfo(sentinel.ai_next); 1824 RES_SET_H_ERRNO(res, NO_RECOVERY); 1825 return NULL; 1826 } 1827 } 1828#endif /*RESOLVSORT*/ 1829 if (!canonname) 1830 (void)get_canonname(pai, sentinel.ai_next, qname); 1831 else 1832 (void)get_canonname(pai, sentinel.ai_next, canonname); 1833 RES_SET_H_ERRNO(res, NETDB_SUCCESS); 1834 return sentinel.ai_next; 1835 } 1836 1837 RES_SET_H_ERRNO(res, NO_RECOVERY); 1838 return NULL; 1839} 1840 1841#ifdef RESOLVSORT 1842struct addr_ptr { 1843 struct addrinfo *ai; 1844 int aval; 1845}; 1846 1847static int 1848addr4sort(struct addrinfo *sentinel, res_state res) 1849{ 1850 struct addrinfo *ai; 1851 struct addr_ptr *addrs, addr; 1852 struct sockaddr_in *sin; 1853 int naddrs, i, j; 1854 int needsort = 0; 1855 1856 if (!sentinel) 1857 return -1; 1858 naddrs = 0; 1859 for (ai = sentinel->ai_next; ai; ai = ai->ai_next) 1860 naddrs++; 1861 if (naddrs < 2) 1862 return 0; /* We don't need sorting. */ 1863 if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL) 1864 return -1; 1865 i = 0; 1866 for (ai = sentinel->ai_next; ai; ai = ai->ai_next) { 1867 sin = (struct sockaddr_in *)ai->ai_addr; 1868 for (j = 0; (unsigned)j < res->nsort; j++) { 1869 if (res->sort_list[j].addr.s_addr == 1870 (sin->sin_addr.s_addr & res->sort_list[j].mask)) 1871 break; 1872 } 1873 addrs[i].ai = ai; 1874 addrs[i].aval = j; 1875 if (needsort == 0 && i > 0 && j < addrs[i - 1].aval) 1876 needsort = i; 1877 i++; 1878 } 1879 if (!needsort) { 1880 free(addrs); 1881 return 0; 1882 } 1883 1884 while (needsort < naddrs) { 1885 for (j = needsort - 1; j >= 0; j--) { 1886 if (addrs[j].aval > addrs[j+1].aval) { 1887 addr = addrs[j]; 1888 addrs[j] = addrs[j + 1]; 1889 addrs[j + 1] = addr; 1890 } else 1891 break; 1892 } 1893 needsort++; 1894 } 1895 1896 ai = sentinel; 1897 for (i = 0; i < naddrs; ++i) { 1898 ai->ai_next = addrs[i].ai; 1899 ai = ai->ai_next; 1900 } 1901 ai->ai_next = NULL; 1902 free(addrs); 1903 return 0; 1904} 1905#endif /*RESOLVSORT*/ 1906 1907/*ARGSUSED*/ 1908static int 1909_dns_getaddrinfo(rv, cb_data, ap) 1910 void *rv; 1911 void *cb_data; 1912 va_list ap; 1913{ 1914 struct addrinfo *ai; 1915 querybuf *buf, *buf2; 1916 const char *hostname; 1917 const struct addrinfo *pai; 1918 struct addrinfo sentinel, *cur; 1919 struct res_target q, q2; 1920 res_state res; 1921 1922 hostname = va_arg(ap, char *); 1923 pai = va_arg(ap, const struct addrinfo *); 1924 1925 memset(&q, 0, sizeof(q)); 1926 memset(&q2, 0, sizeof(q2)); 1927 memset(&sentinel, 0, sizeof(sentinel)); 1928 cur = &sentinel; 1929 1930 buf = malloc(sizeof(*buf)); 1931 if (!buf) { 1932 RES_SET_H_ERRNO(res, NETDB_INTERNAL); 1933 return NS_NOTFOUND; 1934 } 1935 buf2 = malloc(sizeof(*buf2)); 1936 if (!buf2) { 1937 free(buf); 1938 RES_SET_H_ERRNO(res, NETDB_INTERNAL); 1939 return NS_NOTFOUND; 1940 } 1941 1942 switch (pai->ai_family) { 1943 case AF_UNSPEC: 1944 q.name = hostname; 1945 q.qclass = C_IN; 1946 q.qtype = T_A; 1947 q.answer = buf->buf; 1948 q.anslen = sizeof(buf->buf); 1949 q.next = &q2; 1950 q2.name = hostname; 1951 q2.qclass = C_IN; 1952 q2.qtype = T_AAAA; 1953 q2.answer = buf2->buf; 1954 q2.anslen = sizeof(buf2->buf); 1955 break; 1956 case AF_INET: 1957 q.name = hostname; 1958 q.qclass = C_IN; 1959 q.qtype = T_A; 1960 q.answer = buf->buf; 1961 q.anslen = sizeof(buf->buf); 1962 break; 1963 case AF_INET6: 1964 q.name = hostname; 1965 q.qclass = C_IN; 1966 q.qtype = T_AAAA; 1967 q.answer = buf->buf; 1968 q.anslen = sizeof(buf->buf); 1969 break; 1970 default: 1971 free(buf); 1972 free(buf2); 1973 return NS_UNAVAIL; 1974 } 1975 1976 res = __res_state(); 1977 if ((res->options & RES_INIT) == 0 && res_ninit(res) == -1) { 1978 RES_SET_H_ERRNO(res, NETDB_INTERNAL); 1979 free(buf); 1980 free(buf2); 1981 return NS_NOTFOUND; 1982 } 1983 1984 if (res_searchN(hostname, &q, res) < 0) { 1985 free(buf); 1986 free(buf2); 1987 return NS_NOTFOUND; 1988 } 1989 /* prefer IPv6 */ 1990 if (q.next) { 1991 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai, res); 1992 if (ai) { 1993 cur->ai_next = ai; 1994 while (cur && cur->ai_next) 1995 cur = cur->ai_next; 1996 } 1997 } 1998 ai = getanswer(buf, q.n, q.name, q.qtype, pai, res); 1999 if (ai) 2000 cur->ai_next = ai; 2001 free(buf); 2002 free(buf2); 2003 if (sentinel.ai_next == NULL) 2004 switch (res->res_h_errno) { 2005 case HOST_NOT_FOUND: 2006 return NS_NOTFOUND; 2007 case TRY_AGAIN: 2008 return NS_TRYAGAIN; 2009 default: 2010 return NS_UNAVAIL; 2011 } 2012 *((struct addrinfo **)rv) = sentinel.ai_next; 2013 return NS_SUCCESS; 2014} 2015 2016static void 2017_sethtent(FILE **hostf) 2018{ 2019 if (!*hostf) 2020 *hostf = fopen(_PATH_HOSTS, "r"); 2021 else 2022 rewind(*hostf); 2023} 2024 2025static void 2026_endhtent(FILE **hostf) 2027{ 2028 if (*hostf) { 2029 (void) fclose(*hostf); 2030 *hostf = NULL; 2031 } 2032} 2033 2034static struct addrinfo * 2035_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) 2036{ 2037 char *p; 2038 char *cp, *tname, *cname; 2039 struct addrinfo hints, *res0, *res; 2040 int error; 2041 const char *addr; 2042 char hostbuf[8*1024]; 2043 2044 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r"))) 2045 return (NULL); 2046again: 2047 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) 2048 return (NULL); 2049 if (*p == '#') 2050 goto again; 2051 cp = strpbrk(p, "#\n"); 2052 if (cp != NULL) 2053 *cp = '\0'; 2054 if (!(cp = strpbrk(p, " \t"))) 2055 goto again; 2056 *cp++ = '\0'; 2057 addr = p; 2058 cname = NULL; 2059 /* if this is not something we're looking for, skip it. */ 2060 while (cp && *cp) { 2061 if (*cp == ' ' || *cp == '\t') { 2062 cp++; 2063 continue; 2064 } 2065 tname = cp; 2066 if (cname == NULL) 2067 cname = cp; 2068 if ((cp = strpbrk(cp, " \t")) != NULL) 2069 *cp++ = '\0'; 2070 if (strcasecmp(name, tname) == 0) 2071 goto found; 2072 } 2073 goto again; 2074 2075found: 2076 /* we should not glob socktype/protocol here */ 2077 memset(&hints, 0, sizeof(hints)); 2078 hints.ai_family = pai->ai_family; 2079 hints.ai_socktype = SOCK_DGRAM; 2080 hints.ai_protocol = 0; 2081 hints.ai_flags = AI_NUMERICHOST; 2082 error = getaddrinfo(addr, "0", &hints, &res0); 2083 if (error) 2084 goto again; 2085#ifdef FILTER_V4MAPPED 2086 /* XXX should check all items in the chain */ 2087 if (res0->ai_family == AF_INET6 && 2088 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 2089 freeaddrinfo(res0); 2090 goto again; 2091 } 2092#endif 2093 for (res = res0; res; res = res->ai_next) { 2094 /* cover it up */ 2095 res->ai_flags = pai->ai_flags; 2096 res->ai_socktype = pai->ai_socktype; 2097 res->ai_protocol = pai->ai_protocol; 2098 2099 if (pai->ai_flags & AI_CANONNAME) { 2100 if (get_canonname(pai, res, cname) != 0) { 2101 freeaddrinfo(res0); 2102 goto again; 2103 } 2104 } 2105 } 2106 return res0; 2107} 2108 2109/*ARGSUSED*/ 2110static int 2111_files_getaddrinfo(rv, cb_data, ap) 2112 void *rv; 2113 void *cb_data; 2114 va_list ap; 2115{ 2116 const char *name; 2117 const struct addrinfo *pai; 2118 struct addrinfo sentinel, *cur; 2119 struct addrinfo *p; 2120 FILE *hostf = NULL; 2121 2122 name = va_arg(ap, char *); 2123 pai = va_arg(ap, struct addrinfo *); 2124 2125 memset(&sentinel, 0, sizeof(sentinel)); 2126 cur = &sentinel; 2127 2128 _sethtent(&hostf); 2129 while ((p = _gethtent(&hostf, name, pai)) != NULL) { 2130 cur->ai_next = p; 2131 while (cur && cur->ai_next) 2132 cur = cur->ai_next; 2133 } 2134 _endhtent(&hostf); 2135 2136 *((struct addrinfo **)rv) = sentinel.ai_next; 2137 if (sentinel.ai_next == NULL) 2138 return NS_NOTFOUND; 2139 return NS_SUCCESS; 2140} 2141 2142#ifdef YP 2143/*ARGSUSED*/ 2144static struct addrinfo * 2145_yphostent(line, pai) 2146 char *line; 2147 const struct addrinfo *pai; 2148{ 2149 struct addrinfo sentinel, *cur; 2150 struct addrinfo hints, *res, *res0; 2151 int error; 2152 char *p = line; 2153 const char *addr, *canonname; 2154 char *nextline; 2155 char *cp; 2156 2157 addr = canonname = NULL; 2158 2159 memset(&sentinel, 0, sizeof(sentinel)); 2160 cur = &sentinel; 2161 2162nextline: 2163 /* terminate line */ 2164 cp = strchr(p, '\n'); 2165 if (cp) { 2166 *cp++ = '\0'; 2167 nextline = cp; 2168 } else 2169 nextline = NULL; 2170 2171 cp = strpbrk(p, " \t"); 2172 if (cp == NULL) { 2173 if (canonname == NULL) 2174 return (NULL); 2175 else 2176 goto done; 2177 } 2178 *cp++ = '\0'; 2179 2180 addr = p; 2181 2182 while (cp && *cp) { 2183 if (*cp == ' ' || *cp == '\t') { 2184 cp++; 2185 continue; 2186 } 2187 if (!canonname) 2188 canonname = cp; 2189 if ((cp = strpbrk(cp, " \t")) != NULL) 2190 *cp++ = '\0'; 2191 } 2192 2193 hints = *pai; 2194 hints.ai_flags = AI_NUMERICHOST; 2195 error = getaddrinfo(addr, NULL, &hints, &res0); 2196 if (error == 0) { 2197 for (res = res0; res; res = res->ai_next) { 2198 /* cover it up */ 2199 res->ai_flags = pai->ai_flags; 2200 2201 if (pai->ai_flags & AI_CANONNAME) 2202 (void)get_canonname(pai, res, canonname); 2203 } 2204 } else 2205 res0 = NULL; 2206 if (res0) { 2207 cur->ai_next = res0; 2208 while (cur && cur->ai_next) 2209 cur = cur->ai_next; 2210 } 2211 2212 if (nextline) { 2213 p = nextline; 2214 goto nextline; 2215 } 2216 2217done: 2218 return sentinel.ai_next; 2219} 2220 2221/*ARGSUSED*/ 2222static int 2223_yp_getaddrinfo(rv, cb_data, ap) 2224 void *rv; 2225 void *cb_data; 2226 va_list ap; 2227{ 2228 struct addrinfo sentinel, *cur; 2229 struct addrinfo *ai = NULL; 2230 char *ypbuf; 2231 int ypbuflen, r; 2232 const char *name; 2233 const struct addrinfo *pai; 2234 char *ypdomain; 2235 2236 if (_yp_check(&ypdomain) == 0) 2237 return NS_UNAVAIL; 2238 2239 name = va_arg(ap, char *); 2240 pai = va_arg(ap, const struct addrinfo *); 2241 2242 memset(&sentinel, 0, sizeof(sentinel)); 2243 cur = &sentinel; 2244 2245 /* hosts.byname is only for IPv4 (Solaris8) */ 2246 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 2247 r = yp_match(ypdomain, "hosts.byname", name, 2248 (int)strlen(name), &ypbuf, &ypbuflen); 2249 if (r == 0) { 2250 struct addrinfo ai4; 2251 2252 ai4 = *pai; 2253 ai4.ai_family = AF_INET; 2254 ai = _yphostent(ypbuf, &ai4); 2255 if (ai) { 2256 cur->ai_next = ai; 2257 while (cur && cur->ai_next) 2258 cur = cur->ai_next; 2259 } 2260 free(ypbuf); 2261 } 2262 } 2263 2264 /* ipnodes.byname can hold both IPv4/v6 */ 2265 r = yp_match(ypdomain, "ipnodes.byname", name, 2266 (int)strlen(name), &ypbuf, &ypbuflen); 2267 if (r == 0) { 2268 ai = _yphostent(ypbuf, pai); 2269 if (ai) 2270 cur->ai_next = ai; 2271 free(ypbuf); 2272 } 2273 2274 if (sentinel.ai_next == NULL) { 2275 RES_SET_H_ERRNO(__res_state(), HOST_NOT_FOUND); 2276 return NS_NOTFOUND; 2277 } 2278 *((struct addrinfo **)rv) = sentinel.ai_next; 2279 return NS_SUCCESS; 2280} 2281#endif 2282 2283/* resolver logic */ 2284 2285/* 2286 * Formulate a normal query, send, and await answer. 2287 * Returned answer is placed in supplied buffer "answer". 2288 * Perform preliminary check of answer, returning success only 2289 * if no error is indicated and the answer count is nonzero. 2290 * Return the size of the response on success, -1 on error. 2291 * Error number is left in h_errno. 2292 * 2293 * Caller must parse answer and determine whether it answers the question. 2294 */ 2295static int 2296res_queryN(name, target, res) 2297 const char *name; /* domain name */ 2298 struct res_target *target; 2299 res_state res; 2300{ 2301 u_char *buf; 2302 HEADER *hp; 2303 int n; 2304 struct res_target *t; 2305 int rcode; 2306 int ancount; 2307 2308 rcode = NOERROR; 2309 ancount = 0; 2310 2311 buf = malloc(MAXPACKET); 2312 if (!buf) { 2313 RES_SET_H_ERRNO(res, NETDB_INTERNAL); 2314 return -1; 2315 } 2316 2317 for (t = target; t; t = t->next) { 2318 int class, type; 2319 u_char *answer; 2320 int anslen; 2321 2322 hp = (HEADER *)(void *)t->answer; 2323 hp->rcode = NOERROR; /* default */ 2324 2325 /* make it easier... */ 2326 class = t->qclass; 2327 type = t->qtype; 2328 answer = t->answer; 2329 anslen = t->anslen; 2330#ifdef DEBUG 2331 if (res->options & RES_DEBUG) 2332 printf(";; res_query(%s, %d, %d)\n", name, class, type); 2333#endif 2334 2335 n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, 2336 buf, MAXPACKET); 2337 if (n > 0 && (res->options & RES_USE_EDNS0) != 0) 2338 n = res_nopt(res, n, buf, MAXPACKET, anslen); 2339 if (n <= 0) { 2340#ifdef DEBUG 2341 if (res->options & RES_DEBUG) 2342 printf(";; res_query: mkquery failed\n"); 2343#endif 2344 free(buf); 2345 RES_SET_H_ERRNO(res, NO_RECOVERY); 2346 return (n); 2347 } 2348 n = res_nsend(res, buf, n, answer, anslen); 2349#if 0 2350 if (n < 0) { 2351#ifdef DEBUG 2352 if (res->options & RES_DEBUG) 2353 printf(";; res_query: send error\n"); 2354#endif 2355 free(buf); 2356 RES_SET_H_ERRNO(res, TRY_AGAIN); 2357 return (n); 2358 } 2359#endif 2360 2361 if (n > anslen) 2362 hp->rcode = FORMERR; /* XXX not very informative */ 2363 if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 2364 rcode = hp->rcode; /* record most recent error */ 2365#ifdef DEBUG 2366 if (res->options & RES_DEBUG) 2367 printf(";; rcode = %u, ancount=%u\n", hp->rcode, 2368 ntohs(hp->ancount)); 2369#endif 2370 continue; 2371 } 2372 2373 ancount += ntohs(hp->ancount); 2374 2375 t->n = n; 2376 } 2377 2378 free(buf); 2379 2380 if (ancount == 0) { 2381 switch (rcode) { 2382 case NXDOMAIN: 2383 RES_SET_H_ERRNO(res, HOST_NOT_FOUND); 2384 break; 2385 case SERVFAIL: 2386 RES_SET_H_ERRNO(res, TRY_AGAIN); 2387 break; 2388 case NOERROR: 2389 RES_SET_H_ERRNO(res, NO_DATA); 2390 break; 2391 case FORMERR: 2392 case NOTIMP: 2393 case REFUSED: 2394 default: 2395 RES_SET_H_ERRNO(res, NO_RECOVERY); 2396 break; 2397 } 2398 return (-1); 2399 } 2400 return (ancount); 2401} 2402 2403/* 2404 * Formulate a normal query, send, and retrieve answer in supplied buffer. 2405 * Return the size of the response on success, -1 on error. 2406 * If enabled, implement search rules until answer or unrecoverable failure 2407 * is detected. Error code, if any, is left in h_errno. 2408 */ 2409static int 2410res_searchN(name, target, res) 2411 const char *name; /* domain name */ 2412 struct res_target *target; 2413 res_state res; 2414{ 2415 const char *cp, * const *domain; 2416 HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 2417 u_int dots; 2418 int trailing_dot, ret, saved_herrno; 2419 int got_nodata = 0, got_servfail = 0, root_on_list = 0; 2420 int tried_as_is = 0; 2421 int searched = 0; 2422 char abuf[MAXDNAME]; 2423 2424 errno = 0; 2425 RES_SET_H_ERRNO(res, HOST_NOT_FOUND); /* default, if we never query */ 2426 dots = 0; 2427 for (cp = name; *cp; cp++) 2428 dots += (*cp == '.'); 2429 trailing_dot = 0; 2430 if (cp > name && *--cp == '.') 2431 trailing_dot++; 2432 2433 /* 2434 * if there aren't any dots, it could be a user-level alias 2435 */ 2436 if (!dots && 2437 (cp = res_hostalias(res, name, abuf, sizeof(abuf))) != NULL) 2438 return (res_queryN(cp, target, res)); 2439 2440 /* 2441 * If there are enough dots in the name, let's just give it a 2442 * try 'as is'. The threshold can be set with the "ndots" option. 2443 * Also, query 'as is', if there is a trailing dot in the name. 2444 */ 2445 saved_herrno = -1; 2446 if (dots >= res->ndots || trailing_dot) { 2447 ret = res_querydomainN(name, NULL, target, res); 2448 if (ret > 0 || trailing_dot) 2449 return (ret); 2450 if (errno == ECONNREFUSED) { 2451 RES_SET_H_ERRNO(res, TRY_AGAIN); 2452 return (-1); 2453 } 2454 switch (res->res_h_errno) { 2455 case NO_DATA: 2456 case HOST_NOT_FOUND: 2457 break; 2458 case TRY_AGAIN: 2459 if (hp->rcode == SERVFAIL) 2460 break; 2461 /* FALLTHROUGH */ 2462 default: 2463 return (-1); 2464 } 2465 saved_herrno = res->res_h_errno; 2466 tried_as_is++; 2467 } 2468 2469 /* 2470 * We do at least one level of search if 2471 * - there is no dot and RES_DEFNAME is set, or 2472 * - there is at least one dot, there is no trailing dot, 2473 * and RES_DNSRCH is set. 2474 */ 2475 if ((!dots && (res->options & RES_DEFNAMES)) || 2476 (dots && !trailing_dot && (res->options & RES_DNSRCH))) { 2477 int done = 0; 2478 2479 for (domain = (const char * const *)res->dnsrch; 2480 *domain && !done; 2481 domain++) { 2482 searched = 1; 2483 2484 if (domain[0][0] == '\0' || 2485 (domain[0][0] == '.' && domain[0][1] == '\0')) 2486 root_on_list++; 2487 2488 if (root_on_list && tried_as_is) 2489 continue; 2490 2491 ret = res_querydomainN(name, *domain, target, res); 2492 if (ret > 0) 2493 return (ret); 2494 2495 /* 2496 * If no server present, give up. 2497 * If name isn't found in this domain, 2498 * keep trying higher domains in the search list 2499 * (if that's enabled). 2500 * On a NO_DATA error, keep trying, otherwise 2501 * a wildcard entry of another type could keep us 2502 * from finding this entry higher in the domain. 2503 * If we get some other error (negative answer or 2504 * server failure), then stop searching up, 2505 * but try the input name below in case it's 2506 * fully-qualified. 2507 */ 2508 if (errno == ECONNREFUSED) { 2509 RES_SET_H_ERRNO(res, TRY_AGAIN); 2510 return (-1); 2511 } 2512 2513 switch (res->res_h_errno) { 2514 case NO_DATA: 2515 got_nodata++; 2516 /* FALLTHROUGH */ 2517 case HOST_NOT_FOUND: 2518 /* keep trying */ 2519 break; 2520 case TRY_AGAIN: 2521 got_servfail++; 2522 if (hp->rcode == SERVFAIL) { 2523 /* try next search element, if any */ 2524 break; 2525 } 2526 /* FALLTHROUGH */ 2527 default: 2528 /* anything else implies that we're done */ 2529 done++; 2530 } 2531 /* 2532 * if we got here for some reason other than DNSRCH, 2533 * we only wanted one iteration of the loop, so stop. 2534 */ 2535 if (!(res->options & RES_DNSRCH)) 2536 done++; 2537 } 2538 } 2539 2540 switch (res->res_h_errno) { 2541 case NO_DATA: 2542 case HOST_NOT_FOUND: 2543 break; 2544 case TRY_AGAIN: 2545 if (hp->rcode == SERVFAIL) 2546 break; 2547 /* FALLTHROUGH */ 2548 default: 2549 goto giveup; 2550 } 2551 2552 /* 2553 * If the query has not already been tried as is then try it 2554 * unless RES_NOTLDQUERY is set and there were no dots. 2555 */ 2556 if ((dots || !searched || !(res->options & RES_NOTLDQUERY)) && 2557 !(tried_as_is || root_on_list)) { 2558 ret = res_querydomainN(name, NULL, target, res); 2559 if (ret > 0) 2560 return (ret); 2561 } 2562 2563 /* 2564 * if we got here, we didn't satisfy the search. 2565 * if we did an initial full query, return that query's h_errno 2566 * (note that we wouldn't be here if that query had succeeded). 2567 * else if we ever got a nodata, send that back as the reason. 2568 * else send back meaningless h_errno, that being the one from 2569 * the last DNSRCH we did. 2570 */ 2571giveup: 2572 if (saved_herrno != -1) 2573 RES_SET_H_ERRNO(res, saved_herrno); 2574 else if (got_nodata) 2575 RES_SET_H_ERRNO(res, NO_DATA); 2576 else if (got_servfail) 2577 RES_SET_H_ERRNO(res, TRY_AGAIN); 2578 return (-1); 2579} 2580 2581/* 2582 * Perform a call on res_query on the concatenation of name and domain, 2583 * removing a trailing dot from name if domain is NULL. 2584 */ 2585static int 2586res_querydomainN(name, domain, target, res) 2587 const char *name, *domain; 2588 struct res_target *target; 2589 res_state res; 2590{ 2591 char nbuf[MAXDNAME]; 2592 const char *longname = nbuf; 2593 size_t n, d; 2594 2595#ifdef DEBUG 2596 if (res->options & RES_DEBUG) 2597 printf(";; res_querydomain(%s, %s)\n", 2598 name, domain?domain:"<Nil>"); 2599#endif 2600 if (domain == NULL) { 2601 /* 2602 * Check for trailing '.'; 2603 * copy without '.' if present. 2604 */ 2605 n = strlen(name); 2606 if (n >= MAXDNAME) { 2607 RES_SET_H_ERRNO(res, NO_RECOVERY); 2608 return (-1); 2609 } 2610 if (n > 0 && name[--n] == '.') { 2611 strncpy(nbuf, name, n); 2612 nbuf[n] = '\0'; 2613 } else 2614 longname = name; 2615 } else { 2616 n = strlen(name); 2617 d = strlen(domain); 2618 if (n + d + 1 >= MAXDNAME) { 2619 RES_SET_H_ERRNO(res, NO_RECOVERY); 2620 return (-1); 2621 } 2622 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); 2623 } 2624 return (res_queryN(longname, target, res)); 2625} 2626