getaddrinfo.c revision 145113
1155192Srwatson/* $KAME: getaddrinfo.c,v 1.15 2000/07/09 04:37:24 itojun Exp $ */ 2188313Srwatson 3155192Srwatson/* 4155192Srwatson * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5155192Srwatson * All rights reserved. 6155192Srwatson * 7155192Srwatson * Redistribution and use in source and binary forms, with or without 8155192Srwatson * modification, are permitted provided that the following conditions 9155192Srwatson * are met: 10155192Srwatson * 1. Redistributions of source code must retain the above copyright 11155192Srwatson * notice, this list of conditions and the following disclaimer. 12155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright 13155192Srwatson * notice, this list of conditions and the following disclaimer in the 14180701Srwatson * documentation and/or other materials provided with the distribution. 15155192Srwatson * 3. Neither the name of the project nor the names of its contributors 16155192Srwatson * may be used to endorse or promote products derived from this software 17155192Srwatson * without specific prior written permission. 18155192Srwatson * 19155192Srwatson * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22155192Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23155192Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27155192Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28155192Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29155192Srwatson * SUCH DAMAGE. 30155192Srwatson */ 31178186Srwatson 32178186Srwatson/* 33178186Srwatson * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. 34155192Srwatson * 35155192Srwatson * Issues to be discussed: 36155192Srwatson * - Thread safe-ness must be checked. 37155192Srwatson * - Return values. There are nonstandard return values defined and used 38155192Srwatson * in the source code. This is because RFC2553 is silent about which error 39155192Srwatson * code must be returned for which situation. 40155192Srwatson * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is 41184482Srwatson * invalid. current code - SEGV on freeaddrinfo(NULL) 42155192Srwatson * 43181060Scsjp * Note: 44155192Srwatson * - The code filters out AFs that are not supported by the kernel, 45155192Srwatson * when globbing NULL hostname (to loopback, or wildcard). Is it the right 46155192Srwatson * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG 47155192Srwatson * in ai_flags? 48155192Srwatson * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. 49155192Srwatson * (1) what should we do against numeric hostname (2) what should we do 50155192Srwatson * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? 51155192Srwatson * non-loopback address configured? global address configured? 52155192Srwatson * 53155192Srwatson * OS specific notes for netbsd/openbsd/freebsd4/bsdi4: 54155192Srwatson * - To avoid search order issue, we have a big amount of code duplicate 55155192Srwatson * from gethnamaddr.c and some other places. The issues that there's no 56155192Srwatson * lower layer function to lookup "IPv4 or IPv6" record. Calling 57155192Srwatson * gethostbyname2 from getaddrinfo will end up in wrong search order, as 58180735Srwatson * presented above. 59155192Srwatson * 60155192Srwatson * OS specific notes for freebsd4: 61155192Srwatson * - FreeBSD supported $GAI. The code does not. 62155192Srwatson * - FreeBSD allowed classful IPv4 numeric (127.1), the code does not. 63155192Srwatson */ 64155192Srwatson 65155192Srwatson#include <sys/cdefs.h> 66155192Srwatson__FBSDID("$FreeBSD: head/lib/libc/net/getaddrinfo.c 145113 2005-04-15 14:42:29Z ume $"); 67155192Srwatson 68155192Srwatson#include "namespace.h" 69184482Srwatson#include "reentrant.h" 70156889Srwatson#include <sys/types.h> 71155192Srwatson#include <sys/param.h> 72184482Srwatson#include <sys/socket.h> 73184482Srwatson#include <net/if.h> 74184482Srwatson#include <netinet/in.h> 75184482Srwatson#include <sys/queue.h> 76184482Srwatson#ifdef INET6 77184482Srwatson#include <net/if_var.h> 78195925Srwatson#include <sys/sysctl.h> 79195925Srwatson#include <sys/ioctl.h> 80195925Srwatson#include <netinet6/in6_var.h> /* XXX */ 81195925Srwatson#endif 82195925Srwatson#include <arpa/inet.h> 83195925Srwatson#include <arpa/nameser.h> 84195925Srwatson#include <rpc/rpc.h> 85195925Srwatson#include <rpcsvc/yp_prot.h> 86195925Srwatson#include <rpcsvc/ypclnt.h> 87195925Srwatson#include <netdb.h> 88195925Srwatson#include <resolv.h> 89195925Srwatson#include <string.h> 90195925Srwatson#include <stdlib.h> 91195925Srwatson#include <stddef.h> 92195925Srwatson#include <ctype.h> 93195925Srwatson#include <unistd.h> 94195925Srwatson#include <stdio.h> 95195925Srwatson#include <errno.h> 96195925Srwatson 97195925Srwatson#include "res_config.h" 98195925Srwatson 99195925Srwatson#ifdef DEBUG 100195925Srwatson#include <syslog.h> 101195925Srwatson#endif 102195925Srwatson 103195925Srwatson#include <stdarg.h> 104195925Srwatson#include <nsswitch.h> 105195925Srwatson#include "un-namespace.h" 106195925Srwatson#include "libc_private.h" 107195925Srwatson 108195925Srwatson#if defined(__KAME__) && defined(INET6) 109195925Srwatson# define FAITH 110195925Srwatson#endif 111195925Srwatson 112195925Srwatson#define SUCCESS 0 113195925Srwatson#define ANY 0 114195925Srwatson#define YES 1 115155192Srwatson#define NO 0 116155192Srwatson 117155192Srwatsonstatic const char in_addrany[] = { 0, 0, 0, 0 }; 118155192Srwatsonstatic const char in_loopback[] = { 127, 0, 0, 1 }; 119155192Srwatson#ifdef INET6 120155192Srwatsonstatic const char in6_addrany[] = { 121155192Srwatson 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 122155192Srwatson}; 123155192Srwatsonstatic const char in6_loopback[] = { 124155192Srwatson 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 125184482Srwatson}; 126155192Srwatson#endif 127173142Srwatson 128155192Srwatsonstruct policyqueue { 129155192Srwatson TAILQ_ENTRY(policyqueue) pc_entry; 130155192Srwatson#ifdef INET6 131155192Srwatson struct in6_addrpolicy pc_policy; 132155192Srwatson#endif 133155192Srwatson}; 134155192SrwatsonTAILQ_HEAD(policyhead, policyqueue); 135184482Srwatson 136155192Srwatsonstatic const struct afd { 137155192Srwatson int a_af; 138155192Srwatson int a_addrlen; 139156889Srwatson int a_socklen; 140155192Srwatson int a_off; 141155192Srwatson const char *a_addrany; 142156889Srwatson const char *a_loopback; 143155192Srwatson int a_scoped; 144156889Srwatson} afdl [] = { 145155192Srwatson#ifdef INET6 146155192Srwatson#define N_INET6 0 147156889Srwatson {PF_INET6, sizeof(struct in6_addr), 148155192Srwatson sizeof(struct sockaddr_in6), 149155192Srwatson offsetof(struct sockaddr_in6, sin6_addr), 150155192Srwatson in6_addrany, in6_loopback, 1}, 151155192Srwatson#define N_INET 1 152155192Srwatson#else 153155192Srwatson#define N_INET 0 154155192Srwatson#endif 155155192Srwatson {PF_INET, sizeof(struct in_addr), 156155192Srwatson sizeof(struct sockaddr_in), 157155192Srwatson offsetof(struct sockaddr_in, sin_addr), 158184482Srwatson in_addrany, in_loopback, 0}, 159155192Srwatson {0, 0, 0, 0, NULL, NULL, 0}, 160155192Srwatson}; 161155192Srwatson 162155192Srwatsonstruct explore { 163184482Srwatson int e_af; 164155192Srwatson int e_socktype; 165155192Srwatson int e_protocol; 166155192Srwatson const char *e_protostr; 167155192Srwatson int e_wild; 168155192Srwatson#define WILD_AF(ex) ((ex)->e_wild & 0x01) 169155192Srwatson#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) 170155192Srwatson#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) 171155192Srwatson}; 172184482Srwatson 173155192Srwatsonstatic const struct explore explore[] = { 174155192Srwatson#if 0 175155192Srwatson { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, 176156889Srwatson#endif 177155192Srwatson#ifdef INET6 178155192Srwatson { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 179155192Srwatson { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 180184482Srwatson { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, 181155192Srwatson#endif 182155192Srwatson { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 183155192Srwatson { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 184155192Srwatson { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, 185155192Srwatson { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, 186155192Srwatson { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, 187155192Srwatson { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, 188155192Srwatson { -1, 0, 0, NULL, 0 }, 189155192Srwatson}; 190155192Srwatson 191156889Srwatson#ifdef INET6 192155192Srwatson#define PTON_MAX 16 193155192Srwatson#else 194173142Srwatson#define PTON_MAX 4 195155192Srwatson#endif 196155192Srwatson 197155192Srwatson#define AIO_SRCFLAG_DEPRECATED 0x1 198155192Srwatson 199155192Srwatsonstruct ai_order { 200155192Srwatson union { 201155192Srwatson struct sockaddr_storage aiou_ss; 202155192Srwatson struct sockaddr aiou_sa; 203159269Srwatson } aio_src_un; 204155192Srwatson#define aio_srcsa aio_src_un.aiou_sa 205155192Srwatson u_int32_t aio_srcflag; 206155192Srwatson int aio_srcscope; 207155192Srwatson int aio_dstscope; 208155192Srwatson struct policyqueue *aio_srcpolicy; 209155192Srwatson struct policyqueue *aio_dstpolicy; 210156889Srwatson struct addrinfo *aio_ai; 211155192Srwatson int aio_matchlen; 212155192Srwatson}; 213155192Srwatson 214159269Srwatsonstatic const ns_src default_dns_files[] = { 215156889Srwatson { NSSRC_FILES, NS_SUCCESS }, 216155192Srwatson { NSSRC_DNS, NS_SUCCESS }, 217159269Srwatson { 0 } 218156889Srwatson}; 219155192Srwatson 220155192Srwatsonstruct res_target { 221156889Srwatson struct res_target *next; 222155192Srwatson const char *name; /* domain name */ 223155192Srwatson int qclass, qtype; /* class and type of query */ 224155192Srwatson u_char *answer; /* buffer to put answer */ 225155192Srwatson int anslen; /* size of answer buffer */ 226156889Srwatson int n; /* result length */ 227155192Srwatson}; 228155192Srwatson 229176690Srwatson#define MAXPACKET (64*1024) 230155192Srwatson 231155192Srwatsontypedef union { 232155192Srwatson HEADER hdr; 233156889Srwatson u_char buf[MAXPACKET]; 234155192Srwatson} querybuf; 235155192Srwatson 236155192Srwatsonstatic int str2number(const char *); 237155192Srwatsonstatic int explore_null(const struct addrinfo *, 238155192Srwatson const char *, struct addrinfo **); 239155192Srwatsonstatic int explore_numeric(const struct addrinfo *, const char *, 240155192Srwatson const char *, struct addrinfo **, const char *); 241155192Srwatsonstatic int explore_numeric_scope(const struct addrinfo *, const char *, 242155192Srwatson const char *, struct addrinfo **); 243155192Srwatsonstatic int get_canonname(const struct addrinfo *, 244155192Srwatson struct addrinfo *, const char *); 245155192Srwatsonstatic struct addrinfo *get_ai(const struct addrinfo *, 246155192Srwatson const struct afd *, const char *); 247155192Srwatsonstatic int get_portmatch(const struct addrinfo *, const char *); 248155192Srwatsonstatic int get_port(struct addrinfo *, const char *, int); 249155192Srwatsonstatic const struct afd *find_afd(int); 250155192Srwatsonstatic int addrconfig(struct addrinfo *); 251155192Srwatsonstatic void set_source(struct ai_order *, struct policyhead *); 252155192Srwatsonstatic int comp_dst(const void *, const void *); 253155192Srwatson#ifdef INET6 254155192Srwatsonstatic int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); 255155192Srwatson#endif 256155192Srwatsonstatic int gai_addr2scopetype(struct sockaddr *); 257155192Srwatson 258155192Srwatsonstatic int explore_fqdn(const struct addrinfo *, const char *, 259155192Srwatson const char *, struct addrinfo **); 260155192Srwatson 261155192Srwatsonstatic int reorder(struct addrinfo *); 262155192Srwatsonstatic int get_addrselectpolicy(struct policyhead *); 263155192Srwatsonstatic void free_addrselectpolicy(struct policyhead *); 264155192Srwatsonstatic struct policyqueue *match_addrselectpolicy(struct sockaddr *, 265155192Srwatson struct policyhead *); 266155192Srwatsonstatic int matchlen(struct sockaddr *, struct sockaddr *); 267155192Srwatson 268155192Srwatsonstatic struct addrinfo *getanswer(const querybuf *, int, const char *, int, 269155192Srwatson const struct addrinfo *); 270155192Srwatson#if defined(RESOLVSORT) 271155192Srwatsonstatic int addr4sort(struct addrinfo *); 272155192Srwatson#endif 273155192Srwatsonstatic int _dns_getaddrinfo(void *, void *, va_list); 274155192Srwatsonstatic void _sethtent(FILE **); 275155192Srwatsonstatic void _endhtent(FILE **); 276155192Srwatsonstatic struct addrinfo *_gethtent(FILE **, const char *, 277155192Srwatson const struct addrinfo *); 278181053Srwatsonstatic int _files_getaddrinfo(void *, void *, va_list); 279155192Srwatson#ifdef YP 280155192Srwatsonstatic struct addrinfo *_yphostent(char *, const struct addrinfo *); 281155192Srwatsonstatic int _yp_getaddrinfo(void *, void *, va_list); 282155192Srwatson#endif 283155192Srwatson 284155192Srwatsonstatic int res_queryN(const char *, struct res_target *); 285155192Srwatsonstatic int res_searchN(const char *, struct res_target *); 286155192Srwatsonstatic int res_querydomainN(const char *, const char *, 287156889Srwatson struct res_target *); 288155192Srwatson 289155192Srwatson/* 290155192Srwatson * XXX: Many dependencies are not thread-safe. Still, we cannot use 291176690Srwatson * getaddrinfo() in conjunction with other functions which call them. 292156889Srwatson */ 293195925Srwatsonstatic mutex_t _getaddrinfo_thread_lock = MUTEX_INITIALIZER; 294155192Srwatson#define THREAD_LOCK() mutex_lock(&_getaddrinfo_thread_lock); 295156889Srwatson#define THREAD_UNLOCK() mutex_unlock(&_getaddrinfo_thread_lock); 296156889Srwatson 297156889Srwatson/* XXX macros that make external reference is BAD. */ 298155192Srwatson 299195925Srwatson#define GET_AI(ai, afd, addr) \ 300195925Srwatsondo { \ 301195925Srwatson /* external reference: pai, error, and label free */ \ 302195925Srwatson (ai) = get_ai(pai, (afd), (addr)); \ 303195925Srwatson if ((ai) == NULL) { \ 304195925Srwatson error = EAI_MEMORY; \ 305155192Srwatson goto free; \ 306195925Srwatson } \ 307195925Srwatson} while (/*CONSTCOND*/0) 308195925Srwatson 309195925Srwatson#define GET_PORT(ai, serv) \ 310155192Srwatsondo { \ 311156889Srwatson /* external reference: error and label free */ \ 312195925Srwatson error = get_port((ai), (serv), 0); \ 313155192Srwatson if (error != 0) \ 314195925Srwatson goto free; \ 315195925Srwatson} while (/*CONSTCOND*/0) 316195925Srwatson 317195925Srwatson#define GET_CANONNAME(ai, str) \ 318155192Srwatsondo { \ 319195925Srwatson /* external reference: pai, error and label free */ \ 320155192Srwatson error = get_canonname(pai, (ai), (str)); \ 321155192Srwatson if (error != 0) \ 322155192Srwatson goto free; \ 323155192Srwatson} while (/*CONSTCOND*/0) 324155192Srwatson 325188313Srwatson#define ERR(err) \ 326176565Srwatsondo { \ 327155192Srwatson /* external reference: error, and label bad */ \ 328155192Srwatson error = (err); \ 329155192Srwatson goto bad; \ 330155192Srwatson /*NOTREACHED*/ \ 331155192Srwatson} while (/*CONSTCOND*/0) 332155192Srwatson 333155192Srwatson#define MATCH_FAMILY(x, y, w) \ 334155192Srwatson ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) 335155192Srwatson#define MATCH(x, y, w) \ 336155192Srwatson ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) 337155192Srwatson 338155192Srwatsonvoid 339155192Srwatsonfreeaddrinfo(ai) 340170196Srwatson struct addrinfo *ai; 341155192Srwatson{ 342155192Srwatson struct addrinfo *next; 343155192Srwatson 344155192Srwatson do { 345155192Srwatson next = ai->ai_next; 346155192Srwatson if (ai->ai_canonname) 347155192Srwatson free(ai->ai_canonname); 348188313Srwatson /* no need to free(ai->ai_addr) */ 349176565Srwatson free(ai); 350155192Srwatson ai = next; 351155192Srwatson } while (ai); 352155192Srwatson} 353155192Srwatson 354155192Srwatsonstatic int 355155192Srwatsonstr2number(p) 356155192Srwatson const char *p; 357155192Srwatson{ 358155192Srwatson char *ep; 359155192Srwatson unsigned long v; 360155192Srwatson 361155192Srwatson if (*p == '\0') 362155192Srwatson return -1; 363155192Srwatson ep = NULL; 364155192Srwatson errno = 0; 365155192Srwatson v = strtoul(p, &ep, 10); 366155192Srwatson if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX) 367155192Srwatson return v; 368155192Srwatson else 369155192Srwatson return -1; 370155192Srwatson} 371155192Srwatson 372155192Srwatsonint 373155192Srwatsongetaddrinfo(hostname, servname, hints, res) 374155192Srwatson const char *hostname, *servname; 375155192Srwatson const struct addrinfo *hints; 376155192Srwatson struct addrinfo **res; 377155192Srwatson{ 378155192Srwatson struct addrinfo sentinel; 379155192Srwatson struct addrinfo *cur; 380155192Srwatson int error = 0; 381155192Srwatson struct addrinfo ai; 382155192Srwatson struct addrinfo ai0; 383155192Srwatson struct addrinfo *pai; 384181053Srwatson const struct explore *ex; 385155192Srwatson int numeric = 0; 386155192Srwatson 387155192Srwatson memset(&sentinel, 0, sizeof(sentinel)); 388155192Srwatson cur = &sentinel; 389155192Srwatson pai = &ai; 390155192Srwatson pai->ai_flags = 0; 391155192Srwatson pai->ai_family = PF_UNSPEC; 392188313Srwatson pai->ai_socktype = ANY; 393155192Srwatson pai->ai_protocol = ANY; 394155192Srwatson pai->ai_addrlen = 0; 395155192Srwatson pai->ai_canonname = NULL; 396155192Srwatson pai->ai_addr = NULL; 397155192Srwatson pai->ai_next = NULL; 398155192Srwatson 399155192Srwatson if (hostname == NULL && servname == NULL) 400155192Srwatson return EAI_NONAME; 401155192Srwatson if (hints) { 402155192Srwatson /* error check for hints */ 403155192Srwatson if (hints->ai_addrlen || hints->ai_canonname || 404155192Srwatson hints->ai_addr || hints->ai_next) 405155192Srwatson ERR(EAI_BADHINTS); /* xxx */ 406155192Srwatson if (hints->ai_flags & ~AI_MASK) 407155192Srwatson ERR(EAI_BADFLAGS); 408155192Srwatson switch (hints->ai_family) { 409155192Srwatson case PF_UNSPEC: 410155192Srwatson case PF_INET: 411155192Srwatson#ifdef INET6 412155192Srwatson case PF_INET6: 413155192Srwatson#endif 414155192Srwatson break; 415155192Srwatson default: 416155192Srwatson ERR(EAI_FAMILY); 417155192Srwatson } 418155192Srwatson memcpy(pai, hints, sizeof(*pai)); 419155192Srwatson 420155192Srwatson /* 421155192Srwatson * if both socktype/protocol are specified, check if they 422155192Srwatson * are meaningful combination. 423155192Srwatson */ 424155192Srwatson if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { 425155192Srwatson for (ex = explore; ex->e_af >= 0; ex++) { 426155192Srwatson if (pai->ai_family != ex->e_af) 427155192Srwatson continue; 428155192Srwatson if (ex->e_socktype == ANY) 429155192Srwatson continue; 430155192Srwatson if (ex->e_protocol == ANY) 431155192Srwatson continue; 432155192Srwatson if (pai->ai_socktype == ex->e_socktype && 433155192Srwatson pai->ai_protocol != ex->e_protocol) { 434155192Srwatson ERR(EAI_BADHINTS); 435155192Srwatson } 436155192Srwatson } 437155192Srwatson } 438155192Srwatson } 439155192Srwatson 440155192Srwatson /* 441155192Srwatson * post-2553: AI_ALL and AI_V4MAPPED are effective only against 442155192Srwatson * AF_INET6 query. They need to be ignored if specified in other 443155192Srwatson * occassions. 444155192Srwatson */ 445155192Srwatson switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { 446155192Srwatson case AI_V4MAPPED: 447155192Srwatson case AI_ALL | AI_V4MAPPED: 448155192Srwatson if (pai->ai_family != AF_INET6) 449155192Srwatson pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 450155192Srwatson break; 451155192Srwatson case AI_ALL: 452155192Srwatson#if 1 453155192Srwatson /* illegal */ 454155192Srwatson ERR(EAI_BADFLAGS); 455155192Srwatson#else 456155192Srwatson pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); 457156889Srwatson#endif 458156889Srwatson break; 459156889Srwatson } 460170196Srwatson 461156889Srwatson /* 462156889Srwatson * check for special cases. (1) numeric servname is disallowed if 463155192Srwatson * socktype/protocol are left unspecified. (2) servname is disallowed 464155192Srwatson * for raw and other inet{,6} sockets. 465176565Srwatson */ 466155192Srwatson if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) 467181060Scsjp#ifdef PF_INET6 468181060Scsjp || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) 469155192Srwatson#endif 470181060Scsjp ) { 471184660Sjhb ai0 = *pai; /* backup *pai */ 472155192Srwatson 473181060Scsjp if (pai->ai_family == PF_UNSPEC) { 474181060Scsjp#ifdef PF_INET6 475165621Srwatson pai->ai_family = PF_INET6; 476181060Scsjp#else 477181060Scsjp pai->ai_family = PF_INET; 478155192Srwatson#endif 479168355Srwatson } 480181060Scsjp error = get_portmatch(pai, servname); 481181060Scsjp if (error) 482181060Scsjp ERR(error); 483181060Scsjp 484181060Scsjp *pai = ai0; 485181060Scsjp } 486181060Scsjp 487155192Srwatson ai0 = *pai; 488181060Scsjp 489181060Scsjp /* NULL hostname, or numeric hostname */ 490181060Scsjp for (ex = explore; ex->e_af >= 0; ex++) { 491181060Scsjp *pai = ai0; 492181060Scsjp 493181060Scsjp /* PF_UNSPEC entries are prepared for DNS queries only */ 494181060Scsjp if (ex->e_af == PF_UNSPEC) 495181060Scsjp continue; 496181060Scsjp 497181060Scsjp if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) 498168355Srwatson continue; 499181060Scsjp if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) 500181060Scsjp continue; 501181060Scsjp if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) 502181060Scsjp continue; 503181060Scsjp 504181060Scsjp if (pai->ai_family == PF_UNSPEC) 505181060Scsjp pai->ai_family = ex->e_af; 506181060Scsjp if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 507181060Scsjp pai->ai_socktype = ex->e_socktype; 508181060Scsjp if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 509181060Scsjp pai->ai_protocol = ex->e_protocol; 510181060Scsjp 511181060Scsjp if (hostname == NULL) 512181060Scsjp error = explore_null(pai, servname, &cur->ai_next); 513181060Scsjp else 514181060Scsjp error = explore_numeric_scope(pai, hostname, servname, 515181060Scsjp &cur->ai_next); 516181060Scsjp 517181060Scsjp if (error) 518184660Sjhb goto free; 519181060Scsjp 520181060Scsjp while (cur && cur->ai_next) 521155192Srwatson cur = cur->ai_next; 522181060Scsjp } 523181060Scsjp 524181060Scsjp /* 525181060Scsjp * XXX 526181060Scsjp * If numreic representation of AF1 can be interpreted as FQDN 527181060Scsjp * representation of AF2, we need to think again about the code below. 528181060Scsjp */ 529181060Scsjp if (sentinel.ai_next) { 530184660Sjhb numeric = 1; 531181060Scsjp goto good; 532181060Scsjp } 533181060Scsjp 534181060Scsjp if (hostname == NULL) 535181060Scsjp ERR(EAI_NONAME); /* used to be EAI_NODATA */ 536181060Scsjp if (pai->ai_flags & AI_NUMERICHOST) 537181060Scsjp ERR(EAI_NONAME); 538181060Scsjp 539181060Scsjp if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(&ai0)) 540182090Scsjp ERR(EAI_FAIL); 541181060Scsjp 542181060Scsjp /* 543181060Scsjp * hostname as alphabetical name. 544181060Scsjp * we would like to prefer AF_INET6 than AF_INET, so we'll make a 545181060Scsjp * outer loop by AFs. 546181060Scsjp */ 547181060Scsjp for (ex = explore; ex->e_af >= 0; ex++) { 548181060Scsjp *pai = ai0; 549181060Scsjp 550181060Scsjp /* require exact match for family field */ 551212425Smdf if (pai->ai_family != ex->e_af) 552181060Scsjp continue; 553181060Scsjp 554181060Scsjp if (!MATCH(pai->ai_socktype, ex->e_socktype, 555181060Scsjp WILD_SOCKTYPE(ex))) { 556155192Srwatson continue; 557 } 558 if (!MATCH(pai->ai_protocol, ex->e_protocol, 559 WILD_PROTOCOL(ex))) { 560 continue; 561 } 562 563 if (pai->ai_socktype == ANY && ex->e_socktype != ANY) 564 pai->ai_socktype = ex->e_socktype; 565 if (pai->ai_protocol == ANY && ex->e_protocol != ANY) 566 pai->ai_protocol = ex->e_protocol; 567 568 error = explore_fqdn(pai, hostname, servname, 569 &cur->ai_next); 570 571 while (cur && cur->ai_next) 572 cur = cur->ai_next; 573 } 574 575 /* XXX inhibit errors if we have the result */ 576 if (sentinel.ai_next) 577 error = 0; 578 579good: 580 /* 581 * ensure we return either: 582 * - error == 0, non-NULL *res 583 * - error != 0, NULL *res 584 */ 585 if (error == 0) { 586 if (sentinel.ai_next) { 587 /* 588 * If the returned entry is for an active connection, 589 * and the given name is not numeric, reorder the 590 * list, so that the application would try the list 591 * in the most efficient order. 592 */ 593 if (hints == NULL || !(hints->ai_flags & AI_PASSIVE)) { 594 if (!numeric) 595 (void)reorder(&sentinel); 596 } 597 *res = sentinel.ai_next; 598 return SUCCESS; 599 } else 600 error = EAI_FAIL; 601 } 602free: 603bad: 604 if (sentinel.ai_next) 605 freeaddrinfo(sentinel.ai_next); 606 *res = NULL; 607 return error; 608} 609 610static int 611reorder(sentinel) 612 struct addrinfo *sentinel; 613{ 614 struct addrinfo *ai, **aip; 615 struct ai_order *aio; 616 int i, n; 617 struct policyhead policyhead; 618 619 /* count the number of addrinfo elements for sorting. */ 620 for (n = 0, ai = sentinel->ai_next; ai != NULL; ai = ai->ai_next, n++) 621 ; 622 623 /* 624 * If the number is small enough, we can skip the reordering process. 625 */ 626 if (n <= 1) 627 return(n); 628 629 /* allocate a temporary array for sort and initialization of it. */ 630 if ((aio = malloc(sizeof(*aio) * n)) == NULL) 631 return(n); /* give up reordering */ 632 memset(aio, 0, sizeof(*aio) * n); 633 634 /* retrieve address selection policy from the kernel */ 635 TAILQ_INIT(&policyhead); 636 if (!get_addrselectpolicy(&policyhead)) { 637 /* no policy is installed into kernel, we don't sort. */ 638 free(aio); 639 return (n); 640 } 641 642 for (i = 0, ai = sentinel->ai_next; i < n; ai = ai->ai_next, i++) { 643 aio[i].aio_ai = ai; 644 aio[i].aio_dstscope = gai_addr2scopetype(ai->ai_addr); 645 aio[i].aio_dstpolicy = match_addrselectpolicy(ai->ai_addr, 646 &policyhead); 647 set_source(&aio[i], &policyhead); 648 } 649 650 /* perform sorting. */ 651 qsort(aio, n, sizeof(*aio), comp_dst); 652 653 /* reorder the addrinfo chain. */ 654 for (i = 0, aip = &sentinel->ai_next; i < n; i++) { 655 *aip = aio[i].aio_ai; 656 aip = &aio[i].aio_ai->ai_next; 657 } 658 *aip = NULL; 659 660 /* cleanup and return */ 661 free(aio); 662 free_addrselectpolicy(&policyhead); 663 return(n); 664} 665 666static int 667get_addrselectpolicy(head) 668 struct policyhead *head; 669{ 670#ifdef INET6 671 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; 672 size_t l; 673 char *buf; 674 struct in6_addrpolicy *pol, *ep; 675 676 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) 677 return (0); 678 if ((buf = malloc(l)) == NULL) 679 return (0); 680 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 681 free(buf); 682 return (0); 683 } 684 685 ep = (struct in6_addrpolicy *)(buf + l); 686 for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { 687 struct policyqueue *new; 688 689 if ((new = malloc(sizeof(*new))) == NULL) { 690 free_addrselectpolicy(head); /* make the list empty */ 691 break; 692 } 693 new->pc_policy = *pol; 694 TAILQ_INSERT_TAIL(head, new, pc_entry); 695 } 696 697 free(buf); 698 return (1); 699#else 700 return (0); 701#endif 702} 703 704static void 705free_addrselectpolicy(head) 706 struct policyhead *head; 707{ 708 struct policyqueue *ent, *nent; 709 710 for (ent = TAILQ_FIRST(head); ent; ent = nent) { 711 nent = TAILQ_NEXT(ent, pc_entry); 712 TAILQ_REMOVE(head, ent, pc_entry); 713 free(ent); 714 } 715} 716 717static struct policyqueue * 718match_addrselectpolicy(addr, head) 719 struct sockaddr *addr; 720 struct policyhead *head; 721{ 722#ifdef INET6 723 struct policyqueue *ent, *bestent = NULL; 724 struct in6_addrpolicy *pol; 725 int matchlen, bestmatchlen = -1; 726 u_char *mp, *ep, *k, *p, m; 727 struct sockaddr_in6 key; 728 729 switch(addr->sa_family) { 730 case AF_INET6: 731 key = *(struct sockaddr_in6 *)addr; 732 break; 733 case AF_INET: 734 /* convert the address into IPv4-mapped IPv6 address. */ 735 memset(&key, 0, sizeof(key)); 736 key.sin6_family = AF_INET6; 737 key.sin6_len = sizeof(key); 738 key.sin6_addr.s6_addr[10] = 0xff; 739 key.sin6_addr.s6_addr[11] = 0xff; 740 memcpy(&key.sin6_addr.s6_addr[12], 741 &((struct sockaddr_in *)addr)->sin_addr, 4); 742 break; 743 default: 744 return(NULL); 745 } 746 747 for (ent = TAILQ_FIRST(head); ent; ent = TAILQ_NEXT(ent, pc_entry)) { 748 pol = &ent->pc_policy; 749 matchlen = 0; 750 751 mp = (u_char *)&pol->addrmask.sin6_addr; 752 ep = mp + 16; /* XXX: scope field? */ 753 k = (u_char *)&key.sin6_addr; 754 p = (u_char *)&pol->addr.sin6_addr; 755 for (; mp < ep && *mp; mp++, k++, p++) { 756 m = *mp; 757 if ((*k & m) != *p) 758 goto next; /* not match */ 759 if (m == 0xff) /* short cut for a typical case */ 760 matchlen += 8; 761 else { 762 while (m >= 0x80) { 763 matchlen++; 764 m <<= 1; 765 } 766 } 767 } 768 769 /* matched. check if this is better than the current best. */ 770 if (matchlen > bestmatchlen) { 771 bestent = ent; 772 bestmatchlen = matchlen; 773 } 774 775 next: 776 continue; 777 } 778 779 return(bestent); 780#else 781 return(NULL); 782#endif 783 784} 785 786static void 787set_source(aio, ph) 788 struct ai_order *aio; 789 struct policyhead *ph; 790{ 791 struct addrinfo ai = *aio->aio_ai; 792 struct sockaddr_storage ss; 793 int s, srclen; 794 795 /* set unspec ("no source is available"), just in case */ 796 aio->aio_srcsa.sa_family = AF_UNSPEC; 797 aio->aio_srcscope = -1; 798 799 switch(ai.ai_family) { 800 case AF_INET: 801#ifdef INET6 802 case AF_INET6: 803#endif 804 break; 805 default: /* ignore unsupported AFs explicitly */ 806 return; 807 } 808 809 /* XXX: make a dummy addrinfo to call connect() */ 810 ai.ai_socktype = SOCK_DGRAM; 811 ai.ai_protocol = IPPROTO_UDP; /* is UDP too specific? */ 812 ai.ai_next = NULL; 813 memset(&ss, 0, sizeof(ss)); 814 memcpy(&ss, ai.ai_addr, ai.ai_addrlen); 815 ai.ai_addr = (struct sockaddr *)&ss; 816 get_port(&ai, "1", 0); 817 818 /* open a socket to get the source address for the given dst */ 819 if ((s = _socket(ai.ai_family, ai.ai_socktype, ai.ai_protocol)) < 0) 820 return; /* give up */ 821 if (_connect(s, ai.ai_addr, ai.ai_addrlen) < 0) 822 goto cleanup; 823 srclen = ai.ai_addrlen; 824 if (_getsockname(s, &aio->aio_srcsa, &srclen) < 0) { 825 aio->aio_srcsa.sa_family = AF_UNSPEC; 826 goto cleanup; 827 } 828 aio->aio_srcscope = gai_addr2scopetype(&aio->aio_srcsa); 829 aio->aio_srcpolicy = match_addrselectpolicy(&aio->aio_srcsa, ph); 830 aio->aio_matchlen = matchlen(&aio->aio_srcsa, aio->aio_ai->ai_addr); 831#ifdef INET6 832 if (ai.ai_family == AF_INET6) { 833 struct in6_ifreq ifr6; 834 u_int32_t flags6; 835 836 /* XXX: interface name should not be hardcoded */ 837 strncpy(ifr6.ifr_name, "lo0", sizeof(ifr6.ifr_name)); 838 memset(&ifr6, 0, sizeof(ifr6)); 839 memcpy(&ifr6.ifr_addr, ai.ai_addr, ai.ai_addrlen); 840 if (_ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == 0) { 841 flags6 = ifr6.ifr_ifru.ifru_flags6; 842 if ((flags6 & IN6_IFF_DEPRECATED)) 843 aio->aio_srcflag |= AIO_SRCFLAG_DEPRECATED; 844 } 845 } 846#endif 847 848 cleanup: 849 _close(s); 850 return; 851} 852 853static int 854matchlen(src, dst) 855 struct sockaddr *src, *dst; 856{ 857 int match = 0; 858 u_char *s, *d; 859 u_char *lim, r; 860 int addrlen; 861 862 switch (src->sa_family) { 863#ifdef INET6 864 case AF_INET6: 865 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 866 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 867 addrlen = sizeof(struct in6_addr); 868 lim = s + addrlen; 869 break; 870#endif 871 case AF_INET: 872 s = (u_char *)&((struct sockaddr_in6 *)src)->sin6_addr; 873 d = (u_char *)&((struct sockaddr_in6 *)dst)->sin6_addr; 874 addrlen = sizeof(struct in_addr); 875 lim = s + addrlen; 876 break; 877 default: 878 return(0); 879 } 880 881 while (s < lim) 882 if ((r = (*d++ ^ *s++)) != 0) { 883 while (r < addrlen * 8) { 884 match++; 885 r <<= 1; 886 } 887 break; 888 } else 889 match += 8; 890 return(match); 891} 892 893static int 894comp_dst(arg1, arg2) 895 const void *arg1, *arg2; 896{ 897 const struct ai_order *dst1 = arg1, *dst2 = arg2; 898 899 /* 900 * Rule 1: Avoid unusable destinations. 901 * XXX: we currently do not consider if an appropriate route exists. 902 */ 903 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 904 dst2->aio_srcsa.sa_family == AF_UNSPEC) { 905 return(-1); 906 } 907 if (dst1->aio_srcsa.sa_family == AF_UNSPEC && 908 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 909 return(1); 910 } 911 912 /* Rule 2: Prefer matching scope. */ 913 if (dst1->aio_dstscope == dst1->aio_srcscope && 914 dst2->aio_dstscope != dst2->aio_srcscope) { 915 return(-1); 916 } 917 if (dst1->aio_dstscope != dst1->aio_srcscope && 918 dst2->aio_dstscope == dst2->aio_srcscope) { 919 return(1); 920 } 921 922 /* Rule 3: Avoid deprecated addresses. */ 923 if (dst1->aio_srcsa.sa_family != AF_UNSPEC && 924 dst2->aio_srcsa.sa_family != AF_UNSPEC) { 925 if (!(dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 926 (dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 927 return(-1); 928 } 929 if ((dst1->aio_srcflag & AIO_SRCFLAG_DEPRECATED) && 930 !(dst2->aio_srcflag & AIO_SRCFLAG_DEPRECATED)) { 931 return(1); 932 } 933 } 934 935 /* Rule 4: Prefer home addresses. */ 936 /* XXX: not implemented yet */ 937 938 /* Rule 5: Prefer matching label. */ 939#ifdef INET6 940 if (dst1->aio_srcpolicy && dst1->aio_dstpolicy && 941 dst1->aio_srcpolicy->pc_policy.label == 942 dst1->aio_dstpolicy->pc_policy.label && 943 (dst2->aio_srcpolicy == NULL || dst2->aio_dstpolicy == NULL || 944 dst2->aio_srcpolicy->pc_policy.label != 945 dst2->aio_dstpolicy->pc_policy.label)) { 946 return(-1); 947 } 948 if (dst2->aio_srcpolicy && dst2->aio_dstpolicy && 949 dst2->aio_srcpolicy->pc_policy.label == 950 dst2->aio_dstpolicy->pc_policy.label && 951 (dst1->aio_srcpolicy == NULL || dst1->aio_dstpolicy == NULL || 952 dst1->aio_srcpolicy->pc_policy.label != 953 dst1->aio_dstpolicy->pc_policy.label)) { 954 return(1); 955 } 956#endif 957 958 /* Rule 6: Prefer higher precedence. */ 959#ifdef INET6 960 if (dst1->aio_dstpolicy && 961 (dst2->aio_dstpolicy == NULL || 962 dst1->aio_dstpolicy->pc_policy.preced > 963 dst2->aio_dstpolicy->pc_policy.preced)) { 964 return(-1); 965 } 966 if (dst2->aio_dstpolicy && 967 (dst1->aio_dstpolicy == NULL || 968 dst2->aio_dstpolicy->pc_policy.preced > 969 dst1->aio_dstpolicy->pc_policy.preced)) { 970 return(1); 971 } 972#endif 973 974 /* Rule 7: Prefer native transport. */ 975 /* XXX: not implemented yet */ 976 977 /* Rule 8: Prefer smaller scope. */ 978 if (dst1->aio_dstscope >= 0 && 979 dst1->aio_dstscope < dst2->aio_dstscope) { 980 return(-1); 981 } 982 if (dst2->aio_dstscope >= 0 && 983 dst2->aio_dstscope < dst1->aio_dstscope) { 984 return(1); 985 } 986 987 /* 988 * Rule 9: Use longest matching prefix. 989 * We compare the match length in a same AF only. 990 */ 991 if (dst1->aio_ai->ai_addr->sa_family == 992 dst2->aio_ai->ai_addr->sa_family) { 993 if (dst1->aio_matchlen > dst2->aio_matchlen) { 994 return(-1); 995 } 996 if (dst1->aio_matchlen < dst2->aio_matchlen) { 997 return(1); 998 } 999 } 1000 1001 /* Rule 10: Otherwise, leave the order unchanged. */ 1002 return(-1); 1003} 1004 1005/* 1006 * Copy from scope.c. 1007 * XXX: we should standardize the functions and link them as standard 1008 * library. 1009 */ 1010static int 1011gai_addr2scopetype(sa) 1012 struct sockaddr *sa; 1013{ 1014#ifdef INET6 1015 struct sockaddr_in6 *sa6; 1016#endif 1017 struct sockaddr_in *sa4; 1018 1019 switch(sa->sa_family) { 1020#ifdef INET6 1021 case AF_INET6: 1022 sa6 = (struct sockaddr_in6 *)sa; 1023 if (IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) { 1024 /* just use the scope field of the multicast address */ 1025 return(sa6->sin6_addr.s6_addr[2] & 0x0f); 1026 } 1027 /* 1028 * Unicast addresses: map scope type to corresponding scope 1029 * value defined for multcast addresses. 1030 * XXX: hardcoded scope type values are bad... 1031 */ 1032 if (IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) 1033 return(1); /* node local scope */ 1034 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) 1035 return(2); /* link-local scope */ 1036 if (IN6_IS_ADDR_SITELOCAL(&sa6->sin6_addr)) 1037 return(5); /* site-local scope */ 1038 return(14); /* global scope */ 1039 break; 1040#endif 1041 case AF_INET: 1042 /* 1043 * IPv4 pseudo scoping according to RFC 3484. 1044 */ 1045 sa4 = (struct sockaddr_in *)sa; 1046 /* IPv4 autoconfiguration addresses have link-local scope. */ 1047 if (((u_char *)&sa4->sin_addr)[0] == 169 && 1048 ((u_char *)&sa4->sin_addr)[1] == 254) 1049 return(2); 1050 /* Private addresses have site-local scope. */ 1051 if (((u_char *)&sa4->sin_addr)[0] == 10 || 1052 (((u_char *)&sa4->sin_addr)[0] == 172 && 1053 (((u_char *)&sa4->sin_addr)[1] & 0xf0) == 16) || 1054 (((u_char *)&sa4->sin_addr)[0] == 192 && 1055 ((u_char *)&sa4->sin_addr)[1] == 168)) 1056 return(14); /* XXX: It should be 5 unless NAT */ 1057 /* Loopback addresses have link-local scope. */ 1058 if (((u_char *)&sa4->sin_addr)[0] == 127) 1059 return(2); 1060 return(14); 1061 break; 1062 default: 1063 errno = EAFNOSUPPORT; /* is this a good error? */ 1064 return(-1); 1065 } 1066} 1067 1068/* 1069 * hostname == NULL. 1070 * passive socket -> anyaddr (0.0.0.0 or ::) 1071 * non-passive socket -> localhost (127.0.0.1 or ::1) 1072 */ 1073static int 1074explore_null(pai, servname, res) 1075 const struct addrinfo *pai; 1076 const char *servname; 1077 struct addrinfo **res; 1078{ 1079 int s; 1080 const struct afd *afd; 1081 struct addrinfo *cur; 1082 struct addrinfo sentinel; 1083 int error; 1084 1085 *res = NULL; 1086 sentinel.ai_next = NULL; 1087 cur = &sentinel; 1088 1089 /* 1090 * filter out AFs that are not supported by the kernel 1091 * XXX errno? 1092 */ 1093 s = _socket(pai->ai_family, SOCK_DGRAM, 0); 1094 if (s < 0) { 1095 if (errno != EMFILE) 1096 return 0; 1097 } else 1098 _close(s); 1099 1100 /* 1101 * if the servname does not match socktype/protocol, ignore it. 1102 */ 1103 if (get_portmatch(pai, servname) != 0) 1104 return 0; 1105 1106 afd = find_afd(pai->ai_family); 1107 if (afd == NULL) 1108 return 0; 1109 1110 if (pai->ai_flags & AI_PASSIVE) { 1111 GET_AI(cur->ai_next, afd, afd->a_addrany); 1112 /* xxx meaningless? 1113 * GET_CANONNAME(cur->ai_next, "anyaddr"); 1114 */ 1115 GET_PORT(cur->ai_next, servname); 1116 } else { 1117 GET_AI(cur->ai_next, afd, afd->a_loopback); 1118 /* xxx meaningless? 1119 * GET_CANONNAME(cur->ai_next, "localhost"); 1120 */ 1121 GET_PORT(cur->ai_next, servname); 1122 } 1123 cur = cur->ai_next; 1124 1125 *res = sentinel.ai_next; 1126 return 0; 1127 1128free: 1129 if (sentinel.ai_next) 1130 freeaddrinfo(sentinel.ai_next); 1131 return error; 1132} 1133 1134/* 1135 * numeric hostname 1136 */ 1137static int 1138explore_numeric(pai, hostname, servname, res, canonname) 1139 const struct addrinfo *pai; 1140 const char *hostname; 1141 const char *servname; 1142 struct addrinfo **res; 1143 const char *canonname; 1144{ 1145 const struct afd *afd; 1146 struct addrinfo *cur; 1147 struct addrinfo sentinel; 1148 int error; 1149 char pton[PTON_MAX]; 1150 1151 *res = NULL; 1152 sentinel.ai_next = NULL; 1153 cur = &sentinel; 1154 1155 /* 1156 * if the servname does not match socktype/protocol, ignore it. 1157 */ 1158 if (get_portmatch(pai, servname) != 0) 1159 return 0; 1160 1161 afd = find_afd(pai->ai_family); 1162 if (afd == NULL) 1163 return 0; 1164 1165 switch (afd->a_af) { 1166#if 1 /*X/Open spec*/ 1167 case AF_INET: 1168 if (inet_aton(hostname, (struct in_addr *)pton) == 1) { 1169 if (pai->ai_family == afd->a_af || 1170 pai->ai_family == PF_UNSPEC /*?*/) { 1171 GET_AI(cur->ai_next, afd, pton); 1172 GET_PORT(cur->ai_next, servname); 1173 if ((pai->ai_flags & AI_CANONNAME)) { 1174 /* 1175 * Set the numeric address itself as 1176 * the canonical name, based on a 1177 * clarification in rfc3493. 1178 */ 1179 GET_CANONNAME(cur->ai_next, canonname); 1180 } 1181 while (cur && cur->ai_next) 1182 cur = cur->ai_next; 1183 } else 1184 ERR(EAI_FAMILY); /*xxx*/ 1185 } 1186 break; 1187#endif 1188 default: 1189 if (inet_pton(afd->a_af, hostname, pton) == 1) { 1190 if (pai->ai_family == afd->a_af || 1191 pai->ai_family == PF_UNSPEC /*?*/) { 1192 GET_AI(cur->ai_next, afd, pton); 1193 GET_PORT(cur->ai_next, servname); 1194 if ((pai->ai_flags & AI_CANONNAME)) { 1195 /* 1196 * Set the numeric address itself as 1197 * the canonical name, based on a 1198 * clarification in rfc3493. 1199 */ 1200 GET_CANONNAME(cur->ai_next, canonname); 1201 } 1202 while (cur && cur->ai_next) 1203 cur = cur->ai_next; 1204 } else 1205 ERR(EAI_FAMILY); /* XXX */ 1206 } 1207 break; 1208 } 1209 1210 *res = sentinel.ai_next; 1211 return 0; 1212 1213free: 1214bad: 1215 if (sentinel.ai_next) 1216 freeaddrinfo(sentinel.ai_next); 1217 return error; 1218} 1219 1220/* 1221 * numeric hostname with scope 1222 */ 1223static int 1224explore_numeric_scope(pai, hostname, servname, res) 1225 const struct addrinfo *pai; 1226 const char *hostname; 1227 const char *servname; 1228 struct addrinfo **res; 1229{ 1230#if !defined(SCOPE_DELIMITER) || !defined(INET6) 1231 return explore_numeric(pai, hostname, servname, res, hostname); 1232#else 1233 const struct afd *afd; 1234 struct addrinfo *cur; 1235 int error; 1236 char *cp, *hostname2 = NULL, *scope, *addr; 1237 struct sockaddr_in6 *sin6; 1238 1239 /* 1240 * if the servname does not match socktype/protocol, ignore it. 1241 */ 1242 if (get_portmatch(pai, servname) != 0) 1243 return 0; 1244 1245 afd = find_afd(pai->ai_family); 1246 if (afd == NULL) 1247 return 0; 1248 1249 if (!afd->a_scoped) 1250 return explore_numeric(pai, hostname, servname, res, hostname); 1251 1252 cp = strchr(hostname, SCOPE_DELIMITER); 1253 if (cp == NULL) 1254 return explore_numeric(pai, hostname, servname, res, hostname); 1255 1256 /* 1257 * Handle special case of <scoped_address><delimiter><scope id> 1258 */ 1259 hostname2 = strdup(hostname); 1260 if (hostname2 == NULL) 1261 return EAI_MEMORY; 1262 /* terminate at the delimiter */ 1263 hostname2[cp - hostname] = '\0'; 1264 addr = hostname2; 1265 scope = cp + 1; 1266 1267 error = explore_numeric(pai, addr, servname, res, hostname); 1268 if (error == 0) { 1269 u_int32_t scopeid; 1270 1271 for (cur = *res; cur; cur = cur->ai_next) { 1272 if (cur->ai_family != AF_INET6) 1273 continue; 1274 sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; 1275 if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { 1276 free(hostname2); 1277 return(EAI_NONAME); /* XXX: is return OK? */ 1278 } 1279 sin6->sin6_scope_id = scopeid; 1280 } 1281 } 1282 1283 free(hostname2); 1284 1285 return error; 1286#endif 1287} 1288 1289static int 1290get_canonname(pai, ai, str) 1291 const struct addrinfo *pai; 1292 struct addrinfo *ai; 1293 const char *str; 1294{ 1295 if ((pai->ai_flags & AI_CANONNAME) != 0) { 1296 ai->ai_canonname = strdup(str); 1297 if (ai->ai_canonname == NULL) 1298 return EAI_MEMORY; 1299 } 1300 return 0; 1301} 1302 1303static struct addrinfo * 1304get_ai(pai, afd, addr) 1305 const struct addrinfo *pai; 1306 const struct afd *afd; 1307 const char *addr; 1308{ 1309 char *p; 1310 struct addrinfo *ai; 1311#ifdef FAITH 1312 struct in6_addr faith_prefix; 1313 char *fp_str; 1314 int translate = 0; 1315#endif 1316 1317#ifdef FAITH 1318 /* 1319 * Transfrom an IPv4 addr into a special IPv6 addr format for 1320 * IPv6->IPv4 translation gateway. (only TCP is supported now) 1321 * 1322 * +-----------------------------------+------------+ 1323 * | faith prefix part (12 bytes) | embedded | 1324 * | | IPv4 addr part (4 bytes) 1325 * +-----------------------------------+------------+ 1326 * 1327 * faith prefix part is specified as ascii IPv6 addr format 1328 * in environmental variable GAI. 1329 * For FAITH to work correctly, routing to faith prefix must be 1330 * setup toward a machine where a FAITH daemon operates. 1331 * Also, the machine must enable some mechanizm 1332 * (e.g. faith interface hack) to divert those packet with 1333 * faith prefixed destination addr to user-land FAITH daemon. 1334 */ 1335 fp_str = getenv("GAI"); 1336 if (fp_str && inet_pton(AF_INET6, fp_str, &faith_prefix) == 1 && 1337 afd->a_af == AF_INET && pai->ai_socktype == SOCK_STREAM) { 1338 u_int32_t v4a; 1339 u_int8_t v4a_top; 1340 1341 memcpy(&v4a, addr, sizeof v4a); 1342 v4a_top = v4a >> IN_CLASSA_NSHIFT; 1343 if (!IN_MULTICAST(v4a) && !IN_EXPERIMENTAL(v4a) && 1344 v4a_top != 0 && v4a != IN_LOOPBACKNET) { 1345 afd = &afdl[N_INET6]; 1346 memcpy(&faith_prefix.s6_addr[12], addr, 1347 sizeof(struct in_addr)); 1348 translate = 1; 1349 } 1350 } 1351#endif 1352 1353 ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) 1354 + (afd->a_socklen)); 1355 if (ai == NULL) 1356 return NULL; 1357 1358 memcpy(ai, pai, sizeof(struct addrinfo)); 1359 ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); 1360 memset(ai->ai_addr, 0, (size_t)afd->a_socklen); 1361 ai->ai_addr->sa_len = afd->a_socklen; 1362 ai->ai_addrlen = afd->a_socklen; 1363 ai->ai_addr->sa_family = ai->ai_family = afd->a_af; 1364 p = (char *)(void *)(ai->ai_addr); 1365#ifdef FAITH 1366 if (translate == 1) 1367 memcpy(p + afd->a_off, &faith_prefix, (size_t)afd->a_addrlen); 1368 else 1369#endif 1370 memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); 1371 return ai; 1372} 1373 1374static int 1375get_portmatch(ai, servname) 1376 const struct addrinfo *ai; 1377 const char *servname; 1378{ 1379 1380 /* get_port does not touch first argument when matchonly == 1. */ 1381 /* LINTED const cast */ 1382 return get_port((struct addrinfo *)ai, servname, 1); 1383} 1384 1385static int 1386get_port(ai, servname, matchonly) 1387 struct addrinfo *ai; 1388 const char *servname; 1389 int matchonly; 1390{ 1391 const char *proto; 1392 struct servent *sp; 1393 int port; 1394 int allownumeric; 1395 1396 if (servname == NULL) 1397 return 0; 1398 switch (ai->ai_family) { 1399 case AF_INET: 1400#ifdef AF_INET6 1401 case AF_INET6: 1402#endif 1403 break; 1404 default: 1405 return 0; 1406 } 1407 1408 switch (ai->ai_socktype) { 1409 case SOCK_RAW: 1410 return EAI_SERVICE; 1411 case SOCK_DGRAM: 1412 case SOCK_STREAM: 1413 allownumeric = 1; 1414 break; 1415 case ANY: 1416 allownumeric = 0; 1417 break; 1418 default: 1419 return EAI_SOCKTYPE; 1420 } 1421 1422 port = str2number(servname); 1423 if (port >= 0) { 1424 if (!allownumeric) 1425 return EAI_SERVICE; 1426 if (port < 0 || port > 65535) 1427 return EAI_SERVICE; 1428 port = htons(port); 1429 } else { 1430 if (ai->ai_flags & AI_NUMERICSERV) 1431 return EAI_NONAME; 1432 switch (ai->ai_socktype) { 1433 case SOCK_DGRAM: 1434 proto = "udp"; 1435 break; 1436 case SOCK_STREAM: 1437 proto = "tcp"; 1438 break; 1439 default: 1440 proto = NULL; 1441 break; 1442 } 1443 1444 THREAD_LOCK(); 1445 if ((sp = getservbyname(servname, proto)) == NULL) { 1446 THREAD_UNLOCK(); 1447 return EAI_SERVICE; 1448 } 1449 port = sp->s_port; 1450 THREAD_UNLOCK(); 1451 } 1452 1453 if (!matchonly) { 1454 switch (ai->ai_family) { 1455 case AF_INET: 1456 ((struct sockaddr_in *)(void *) 1457 ai->ai_addr)->sin_port = port; 1458 break; 1459#ifdef INET6 1460 case AF_INET6: 1461 ((struct sockaddr_in6 *)(void *) 1462 ai->ai_addr)->sin6_port = port; 1463 break; 1464#endif 1465 } 1466 } 1467 1468 return 0; 1469} 1470 1471static const struct afd * 1472find_afd(af) 1473 int af; 1474{ 1475 const struct afd *afd; 1476 1477 if (af == PF_UNSPEC) 1478 return NULL; 1479 for (afd = afdl; afd->a_af; afd++) { 1480 if (afd->a_af == af) 1481 return afd; 1482 } 1483 return NULL; 1484} 1485 1486/* 1487 * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend 1488 * will take care of it. 1489 * the semantics of AI_ADDRCONFIG is not defined well. we are not sure 1490 * if the code is right or not. 1491 * 1492 * XXX PF_UNSPEC -> PF_INET6 + PF_INET mapping needs to be in sync with 1493 * _dns_getaddrinfo. 1494 */ 1495static int 1496addrconfig(pai) 1497 struct addrinfo *pai; 1498{ 1499 int s, af; 1500 1501 /* 1502 * TODO: 1503 * Note that implementation dependent test for address 1504 * configuration should be done everytime called 1505 * (or apropriate interval), 1506 * because addresses will be dynamically assigned or deleted. 1507 */ 1508 af = pai->ai_family; 1509 if (af == AF_UNSPEC) { 1510 if ((s = _socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1511 af = AF_INET; 1512 else { 1513 _close(s); 1514 if ((s = _socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1515 af = AF_INET6; 1516 else 1517 _close(s); 1518 } 1519 } 1520 if (af != AF_UNSPEC) { 1521 if ((s = _socket(af, SOCK_DGRAM, 0)) < 0) 1522 return 0; 1523 _close(s); 1524 } 1525 pai->ai_family = af; 1526 return 1; 1527} 1528 1529#ifdef INET6 1530/* convert a string to a scope identifier. XXX: IPv6 specific */ 1531static int 1532ip6_str2scopeid(scope, sin6, scopeid) 1533 char *scope; 1534 struct sockaddr_in6 *sin6; 1535 u_int32_t *scopeid; 1536{ 1537 u_long lscopeid; 1538 struct in6_addr *a6; 1539 char *ep; 1540 1541 a6 = &sin6->sin6_addr; 1542 1543 /* empty scopeid portion is invalid */ 1544 if (*scope == '\0') 1545 return -1; 1546 1547 if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { 1548 /* 1549 * We currently assume a one-to-one mapping between links 1550 * and interfaces, so we simply use interface indices for 1551 * like-local scopes. 1552 */ 1553 *scopeid = if_nametoindex(scope); 1554 if (*scopeid == 0) 1555 goto trynumeric; 1556 return 0; 1557 } 1558 1559 /* still unclear about literal, allow numeric only - placeholder */ 1560 if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) 1561 goto trynumeric; 1562 if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) 1563 goto trynumeric; 1564 else 1565 goto trynumeric; /* global */ 1566 1567 /* try to convert to a numeric id as a last resort */ 1568 trynumeric: 1569 errno = 0; 1570 lscopeid = strtoul(scope, &ep, 10); 1571 *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); 1572 if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) 1573 return 0; 1574 else 1575 return -1; 1576} 1577#endif 1578 1579/* 1580 * FQDN hostname, DNS lookup 1581 */ 1582static int 1583explore_fqdn(pai, hostname, servname, res) 1584 const struct addrinfo *pai; 1585 const char *hostname; 1586 const char *servname; 1587 struct addrinfo **res; 1588{ 1589 struct addrinfo *result; 1590 struct addrinfo *cur; 1591 int error = 0; 1592 static const ns_dtab dtab[] = { 1593 NS_FILES_CB(_files_getaddrinfo, NULL) 1594 { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ 1595 NS_NIS_CB(_yp_getaddrinfo, NULL) 1596 { 0 } 1597 }; 1598 1599 result = NULL; 1600 1601 /* 1602 * if the servname does not match socktype/protocol, ignore it. 1603 */ 1604 if (get_portmatch(pai, servname) != 0) 1605 return 0; 1606 1607 switch (_nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", 1608 default_dns_files, hostname, pai)) { 1609 case NS_TRYAGAIN: 1610 error = EAI_AGAIN; 1611 goto free; 1612 case NS_UNAVAIL: 1613 error = EAI_FAIL; 1614 goto free; 1615 case NS_NOTFOUND: 1616 error = EAI_NONAME; 1617 goto free; 1618 case NS_SUCCESS: 1619 error = 0; 1620 for (cur = result; cur; cur = cur->ai_next) { 1621 GET_PORT(cur, servname); 1622 /* canonname should be filled already */ 1623 } 1624 break; 1625 } 1626 1627 *res = result; 1628 1629 return 0; 1630 1631free: 1632 if (result) 1633 freeaddrinfo(result); 1634 return error; 1635} 1636 1637#ifdef DEBUG 1638static const char AskedForGot[] = 1639 "gethostby*.getanswer: asked for \"%s\", got \"%s\""; 1640#endif 1641 1642static struct addrinfo * 1643getanswer(answer, anslen, qname, qtype, pai) 1644 const querybuf *answer; 1645 int anslen; 1646 const char *qname; 1647 int qtype; 1648 const struct addrinfo *pai; 1649{ 1650 struct addrinfo sentinel, *cur; 1651 struct addrinfo ai; 1652 const struct afd *afd; 1653 char *canonname; 1654 const HEADER *hp; 1655 const u_char *cp; 1656 int n; 1657 const u_char *eom; 1658 char *bp, *ep; 1659 int type, class, ancount, qdcount; 1660 int haveanswer, had_error; 1661 char tbuf[MAXDNAME]; 1662 int (*name_ok)(const char *); 1663 char hostbuf[8*1024]; 1664 1665 memset(&sentinel, 0, sizeof(sentinel)); 1666 cur = &sentinel; 1667 1668 canonname = NULL; 1669 eom = answer->buf + anslen; 1670 switch (qtype) { 1671 case T_A: 1672 case T_AAAA: 1673 case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ 1674 name_ok = res_hnok; 1675 break; 1676 default: 1677 return (NULL); /* XXX should be abort(); */ 1678 } 1679 /* 1680 * find first satisfactory answer 1681 */ 1682 hp = &answer->hdr; 1683 ancount = ntohs(hp->ancount); 1684 qdcount = ntohs(hp->qdcount); 1685 bp = hostbuf; 1686 ep = hostbuf + sizeof hostbuf; 1687 cp = answer->buf + HFIXEDSZ; 1688 if (qdcount != 1) { 1689 h_errno = NO_RECOVERY; 1690 return (NULL); 1691 } 1692 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1693 if ((n < 0) || !(*name_ok)(bp)) { 1694 h_errno = NO_RECOVERY; 1695 return (NULL); 1696 } 1697 cp += n + QFIXEDSZ; 1698 if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 1699 /* res_send() has already verified that the query name is the 1700 * same as the one we sent; this just gets the expanded name 1701 * (i.e., with the succeeding search-domain tacked on). 1702 */ 1703 n = strlen(bp) + 1; /* for the \0 */ 1704 if (n >= MAXHOSTNAMELEN) { 1705 h_errno = NO_RECOVERY; 1706 return (NULL); 1707 } 1708 canonname = bp; 1709 bp += n; 1710 /* The qname can be abbreviated, but h_name is now absolute. */ 1711 qname = canonname; 1712 } 1713 haveanswer = 0; 1714 had_error = 0; 1715 while (ancount-- > 0 && cp < eom && !had_error) { 1716 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 1717 if ((n < 0) || !(*name_ok)(bp)) { 1718 had_error++; 1719 continue; 1720 } 1721 cp += n; /* name */ 1722 type = _getshort(cp); 1723 cp += INT16SZ; /* type */ 1724 class = _getshort(cp); 1725 cp += INT16SZ + INT32SZ; /* class, TTL */ 1726 n = _getshort(cp); 1727 cp += INT16SZ; /* len */ 1728 if (class != C_IN) { 1729 /* XXX - debug? syslog? */ 1730 cp += n; 1731 continue; /* XXX - had_error++ ? */ 1732 } 1733 if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 1734 type == T_CNAME) { 1735 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); 1736 if ((n < 0) || !(*name_ok)(tbuf)) { 1737 had_error++; 1738 continue; 1739 } 1740 cp += n; 1741 /* Get canonical name. */ 1742 n = strlen(tbuf) + 1; /* for the \0 */ 1743 if (n > ep - bp || n >= MAXHOSTNAMELEN) { 1744 had_error++; 1745 continue; 1746 } 1747 strlcpy(bp, tbuf, ep - bp); 1748 canonname = bp; 1749 bp += n; 1750 continue; 1751 } 1752 if (qtype == T_ANY) { 1753 if (!(type == T_A || type == T_AAAA)) { 1754 cp += n; 1755 continue; 1756 } 1757 } else if (type != qtype) { 1758#ifdef DEBUG 1759 if (type != T_KEY && type != T_SIG) 1760 syslog(LOG_NOTICE|LOG_AUTH, 1761 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", 1762 qname, p_class(C_IN), p_type(qtype), 1763 p_type(type)); 1764#endif 1765 cp += n; 1766 continue; /* XXX - had_error++ ? */ 1767 } 1768 switch (type) { 1769 case T_A: 1770 case T_AAAA: 1771 if (strcasecmp(canonname, bp) != 0) { 1772#ifdef DEBUG 1773 syslog(LOG_NOTICE|LOG_AUTH, 1774 AskedForGot, canonname, bp); 1775#endif 1776 cp += n; 1777 continue; /* XXX - had_error++ ? */ 1778 } 1779 if (type == T_A && n != INADDRSZ) { 1780 cp += n; 1781 continue; 1782 } 1783 if (type == T_AAAA && n != IN6ADDRSZ) { 1784 cp += n; 1785 continue; 1786 } 1787#ifdef FILTER_V4MAPPED 1788 if (type == T_AAAA) { 1789 struct in6_addr in6; 1790 memcpy(&in6, cp, sizeof(in6)); 1791 if (IN6_IS_ADDR_V4MAPPED(&in6)) { 1792 cp += n; 1793 continue; 1794 } 1795 } 1796#endif 1797 if (!haveanswer) { 1798 int nn; 1799 1800 canonname = bp; 1801 nn = strlen(bp) + 1; /* for the \0 */ 1802 bp += nn; 1803 } 1804 1805 /* don't overwrite pai */ 1806 ai = *pai; 1807 ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; 1808 afd = find_afd(ai.ai_family); 1809 if (afd == NULL) { 1810 cp += n; 1811 continue; 1812 } 1813 cur->ai_next = get_ai(&ai, afd, (const char *)cp); 1814 if (cur->ai_next == NULL) 1815 had_error++; 1816 while (cur && cur->ai_next) 1817 cur = cur->ai_next; 1818 cp += n; 1819 break; 1820 default: 1821 abort(); 1822 } 1823 if (!had_error) 1824 haveanswer++; 1825 } 1826 if (haveanswer) { 1827#if defined(RESOLVSORT) 1828 /* 1829 * We support only IPv4 address for backward 1830 * compatibility against gethostbyname(3). 1831 */ 1832 if (_res.nsort && qtype == T_A) { 1833 if (addr4sort(&sentinel) < 0) { 1834 freeaddrinfo(sentinel.ai_next); 1835 h_errno = NO_RECOVERY; 1836 return NULL; 1837 } 1838 } 1839#endif /*RESOLVSORT*/ 1840 if (!canonname) 1841 (void)get_canonname(pai, sentinel.ai_next, qname); 1842 else 1843 (void)get_canonname(pai, sentinel.ai_next, canonname); 1844 h_errno = NETDB_SUCCESS; 1845 return sentinel.ai_next; 1846 } 1847 1848 h_errno = NO_RECOVERY; 1849 return NULL; 1850} 1851 1852#ifdef RESOLVSORT 1853struct addr_ptr { 1854 struct addrinfo *ai; 1855 int aval; 1856}; 1857 1858static int 1859addr4sort(struct addrinfo *sentinel) 1860{ 1861 struct addrinfo *ai; 1862 struct addr_ptr *addrs, addr; 1863 struct sockaddr_in *sin; 1864 int naddrs, i, j; 1865 int needsort = 0; 1866 1867 if (!sentinel) 1868 return -1; 1869 naddrs = 0; 1870 for (ai = sentinel->ai_next; ai; ai = ai->ai_next) 1871 naddrs++; 1872 if (naddrs < 2) 1873 return 0; /* We don't need sorting. */ 1874 if ((addrs = malloc(sizeof(struct addr_ptr) * naddrs)) == NULL) 1875 return -1; 1876 i = 0; 1877 for (ai = sentinel->ai_next; ai; ai = ai->ai_next) { 1878 sin = (struct sockaddr_in *)ai->ai_addr; 1879 for (j = 0; (unsigned)j < _res.nsort; j++) { 1880 if (_res.sort_list[j].addr.s_addr == 1881 (sin->sin_addr.s_addr & _res.sort_list[j].mask)) 1882 break; 1883 } 1884 addrs[i].ai = ai; 1885 addrs[i].aval = j; 1886 if (needsort == 0 && i > 0 && j < addrs[i - 1].aval) 1887 needsort = i; 1888 i++; 1889 } 1890 if (!needsort) { 1891 free(addrs); 1892 return 0; 1893 } 1894 1895 while (needsort < naddrs) { 1896 for (j = needsort - 1; j >= 0; j--) { 1897 if (addrs[j].aval > addrs[j+1].aval) { 1898 addr = addrs[j]; 1899 addrs[j] = addrs[j + 1]; 1900 addrs[j + 1] = addr; 1901 } else 1902 break; 1903 } 1904 needsort++; 1905 } 1906 1907 ai = sentinel; 1908 for (i = 0; i < naddrs; ++i) { 1909 ai->ai_next = addrs[i].ai; 1910 ai = ai->ai_next; 1911 } 1912 ai->ai_next = NULL; 1913 free(addrs); 1914 return 0; 1915} 1916#endif /*RESOLVSORT*/ 1917 1918/*ARGSUSED*/ 1919static int 1920_dns_getaddrinfo(rv, cb_data, ap) 1921 void *rv; 1922 void *cb_data; 1923 va_list ap; 1924{ 1925 struct addrinfo *ai; 1926 querybuf *buf, *buf2; 1927 const char *hostname; 1928 const struct addrinfo *pai; 1929 struct addrinfo sentinel, *cur; 1930 struct res_target q, q2; 1931 1932 hostname = va_arg(ap, char *); 1933 pai = va_arg(ap, const struct addrinfo *); 1934 1935 memset(&q, 0, sizeof(q2)); 1936 memset(&q2, 0, sizeof(q2)); 1937 memset(&sentinel, 0, sizeof(sentinel)); 1938 cur = &sentinel; 1939 1940 buf = malloc(sizeof(*buf)); 1941 if (!buf) { 1942 h_errno = NETDB_INTERNAL; 1943 return NS_NOTFOUND; 1944 } 1945 buf2 = malloc(sizeof(*buf2)); 1946 if (!buf2) { 1947 free(buf); 1948 h_errno = NETDB_INTERNAL; 1949 return NS_NOTFOUND; 1950 } 1951 1952 switch (pai->ai_family) { 1953 case AF_UNSPEC: 1954 q.name = hostname; 1955 q.qclass = C_IN; 1956 q.qtype = T_A; 1957 q.answer = buf->buf; 1958 q.anslen = sizeof(buf->buf); 1959 q.next = &q2; 1960 q2.name = hostname; 1961 q2.qclass = C_IN; 1962 q2.qtype = T_AAAA; 1963 q2.answer = buf2->buf; 1964 q2.anslen = sizeof(buf2->buf); 1965 break; 1966 case AF_INET: 1967 q.name = hostname; 1968 q.qclass = C_IN; 1969 q.qtype = T_A; 1970 q.answer = buf->buf; 1971 q.anslen = sizeof(buf->buf); 1972 break; 1973 case AF_INET6: 1974 q.name = hostname; 1975 q.qclass = C_IN; 1976 q.qtype = T_AAAA; 1977 q.answer = buf->buf; 1978 q.anslen = sizeof(buf->buf); 1979 break; 1980 default: 1981 free(buf); 1982 free(buf2); 1983 return NS_UNAVAIL; 1984 } 1985 if (res_searchN(hostname, &q) < 0) { 1986 free(buf); 1987 free(buf2); 1988 return NS_NOTFOUND; 1989 } 1990 /* prefer IPv6 */ 1991 if (q.next) { 1992 ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); 1993 if (ai) { 1994 cur->ai_next = ai; 1995 while (cur && cur->ai_next) 1996 cur = cur->ai_next; 1997 } 1998 } 1999 ai = getanswer(buf, q.n, q.name, q.qtype, pai); 2000 if (ai) 2001 cur->ai_next = ai; 2002 free(buf); 2003 free(buf2); 2004 if (sentinel.ai_next == NULL) 2005 switch (h_errno) { 2006 case HOST_NOT_FOUND: 2007 return NS_NOTFOUND; 2008 case TRY_AGAIN: 2009 return NS_TRYAGAIN; 2010 default: 2011 return NS_UNAVAIL; 2012 } 2013 *((struct addrinfo **)rv) = sentinel.ai_next; 2014 return NS_SUCCESS; 2015} 2016 2017static void 2018_sethtent(FILE **hostf) 2019{ 2020 if (!*hostf) 2021 *hostf = fopen(_PATH_HOSTS, "r"); 2022 else 2023 rewind(*hostf); 2024} 2025 2026static void 2027_endhtent(FILE **hostf) 2028{ 2029 if (*hostf) { 2030 (void) fclose(*hostf); 2031 *hostf = NULL; 2032 } 2033} 2034 2035static struct addrinfo * 2036_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) 2037{ 2038 char *p; 2039 char *cp, *tname, *cname; 2040 struct addrinfo hints, *res0, *res; 2041 int error; 2042 const char *addr; 2043 char hostbuf[8*1024]; 2044 2045 if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "r"))) 2046 return (NULL); 2047again: 2048 if (!(p = fgets(hostbuf, sizeof hostbuf, *hostf))) 2049 return (NULL); 2050 if (*p == '#') 2051 goto again; 2052 cp = strpbrk(p, "#\n"); 2053 if (cp != NULL) 2054 *cp = '\0'; 2055 if (!(cp = strpbrk(p, " \t"))) 2056 goto again; 2057 *cp++ = '\0'; 2058 addr = p; 2059 cname = NULL; 2060 /* if this is not something we're looking for, skip it. */ 2061 while (cp && *cp) { 2062 if (*cp == ' ' || *cp == '\t') { 2063 cp++; 2064 continue; 2065 } 2066 tname = cp; 2067 if (cname == NULL) 2068 cname = cp; 2069 if ((cp = strpbrk(cp, " \t")) != NULL) 2070 *cp++ = '\0'; 2071 if (strcasecmp(name, tname) == 0) 2072 goto found; 2073 } 2074 goto again; 2075 2076found: 2077 /* we should not glob socktype/protocol here */ 2078 memset(&hints, 0, sizeof(hints)); 2079 hints.ai_family = pai->ai_family; 2080 hints.ai_socktype = SOCK_DGRAM; 2081 hints.ai_protocol = 0; 2082 hints.ai_flags = AI_NUMERICHOST; 2083 error = getaddrinfo(addr, "0", &hints, &res0); 2084 if (error) 2085 goto again; 2086#ifdef FILTER_V4MAPPED 2087 /* XXX should check all items in the chain */ 2088 if (res0->ai_family == AF_INET6 && 2089 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res0->ai_addr)->sin6_addr)) { 2090 freeaddrinfo(res0); 2091 goto again; 2092 } 2093#endif 2094 for (res = res0; res; res = res->ai_next) { 2095 /* cover it up */ 2096 res->ai_flags = pai->ai_flags; 2097 res->ai_socktype = pai->ai_socktype; 2098 res->ai_protocol = pai->ai_protocol; 2099 2100 if (pai->ai_flags & AI_CANONNAME) { 2101 if (get_canonname(pai, res, cname) != 0) { 2102 freeaddrinfo(res0); 2103 goto again; 2104 } 2105 } 2106 } 2107 return res0; 2108} 2109 2110/*ARGSUSED*/ 2111static int 2112_files_getaddrinfo(rv, cb_data, ap) 2113 void *rv; 2114 void *cb_data; 2115 va_list ap; 2116{ 2117 const char *name; 2118 const struct addrinfo *pai; 2119 struct addrinfo sentinel, *cur; 2120 struct addrinfo *p; 2121 FILE *hostf = NULL; 2122 2123 name = va_arg(ap, char *); 2124 pai = va_arg(ap, struct addrinfo *); 2125 2126 memset(&sentinel, 0, sizeof(sentinel)); 2127 cur = &sentinel; 2128 2129 _sethtent(&hostf); 2130 while ((p = _gethtent(&hostf, name, pai)) != NULL) { 2131 cur->ai_next = p; 2132 while (cur && cur->ai_next) 2133 cur = cur->ai_next; 2134 } 2135 _endhtent(&hostf); 2136 2137 *((struct addrinfo **)rv) = sentinel.ai_next; 2138 if (sentinel.ai_next == NULL) 2139 return NS_NOTFOUND; 2140 return NS_SUCCESS; 2141} 2142 2143#ifdef YP 2144/*ARGSUSED*/ 2145static struct addrinfo * 2146_yphostent(line, pai) 2147 char *line; 2148 const struct addrinfo *pai; 2149{ 2150 struct addrinfo sentinel, *cur; 2151 struct addrinfo hints, *res, *res0; 2152 int error; 2153 char *p = line; 2154 const char *addr, *canonname; 2155 char *nextline; 2156 char *cp; 2157 2158 addr = canonname = NULL; 2159 2160 memset(&sentinel, 0, sizeof(sentinel)); 2161 cur = &sentinel; 2162 2163nextline: 2164 /* terminate line */ 2165 cp = strchr(p, '\n'); 2166 if (cp) { 2167 *cp++ = '\0'; 2168 nextline = cp; 2169 } else 2170 nextline = NULL; 2171 2172 cp = strpbrk(p, " \t"); 2173 if (cp == NULL) { 2174 if (canonname == NULL) 2175 return (NULL); 2176 else 2177 goto done; 2178 } 2179 *cp++ = '\0'; 2180 2181 addr = p; 2182 2183 while (cp && *cp) { 2184 if (*cp == ' ' || *cp == '\t') { 2185 cp++; 2186 continue; 2187 } 2188 if (!canonname) 2189 canonname = cp; 2190 if ((cp = strpbrk(cp, " \t")) != NULL) 2191 *cp++ = '\0'; 2192 } 2193 2194 hints = *pai; 2195 hints.ai_flags = AI_NUMERICHOST; 2196 error = getaddrinfo(addr, NULL, &hints, &res0); 2197 if (error == 0) { 2198 for (res = res0; res; res = res->ai_next) { 2199 /* cover it up */ 2200 res->ai_flags = pai->ai_flags; 2201 2202 if (pai->ai_flags & AI_CANONNAME) 2203 (void)get_canonname(pai, res, canonname); 2204 } 2205 } else 2206 res0 = NULL; 2207 if (res0) { 2208 cur->ai_next = res0; 2209 while (cur && cur->ai_next) 2210 cur = cur->ai_next; 2211 } 2212 2213 if (nextline) { 2214 p = nextline; 2215 goto nextline; 2216 } 2217 2218done: 2219 return sentinel.ai_next; 2220} 2221 2222/*ARGSUSED*/ 2223static int 2224_yp_getaddrinfo(rv, cb_data, ap) 2225 void *rv; 2226 void *cb_data; 2227 va_list ap; 2228{ 2229 struct addrinfo sentinel, *cur; 2230 struct addrinfo *ai = NULL; 2231 char *ypbuf; 2232 int ypbuflen, r; 2233 const char *name; 2234 const struct addrinfo *pai; 2235 char *ypdomain; 2236 2237 if (_yp_check(&ypdomain) == 0) 2238 return NS_UNAVAIL; 2239 2240 name = va_arg(ap, char *); 2241 pai = va_arg(ap, const struct addrinfo *); 2242 2243 memset(&sentinel, 0, sizeof(sentinel)); 2244 cur = &sentinel; 2245 2246 /* hosts.byname is only for IPv4 (Solaris8) */ 2247 if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { 2248 r = yp_match(ypdomain, "hosts.byname", name, 2249 (int)strlen(name), &ypbuf, &ypbuflen); 2250 if (r == 0) { 2251 struct addrinfo ai4; 2252 2253 ai4 = *pai; 2254 ai4.ai_family = AF_INET; 2255 ai = _yphostent(ypbuf, &ai4); 2256 if (ai) { 2257 cur->ai_next = ai; 2258 while (cur && cur->ai_next) 2259 cur = cur->ai_next; 2260 } 2261 } 2262 free(ypbuf); 2263 } 2264 2265 /* ipnodes.byname can hold both IPv4/v6 */ 2266 r = yp_match(ypdomain, "ipnodes.byname", name, 2267 (int)strlen(name), &ypbuf, &ypbuflen); 2268 if (r == 0) { 2269 ai = _yphostent(ypbuf, pai); 2270 if (ai) 2271 cur->ai_next = ai; 2272 free(ypbuf); 2273 } 2274 2275 if (sentinel.ai_next == NULL) { 2276 h_errno = HOST_NOT_FOUND; 2277 return NS_NOTFOUND; 2278 } 2279 *((struct addrinfo **)rv) = sentinel.ai_next; 2280 return NS_SUCCESS; 2281} 2282#endif 2283 2284/* resolver logic */ 2285 2286extern const char *_res_hostalias(const char *, char *, size_t); 2287 2288/* 2289 * Formulate a normal query, send, and await answer. 2290 * Returned answer is placed in supplied buffer "answer". 2291 * Perform preliminary check of answer, returning success only 2292 * if no error is indicated and the answer count is nonzero. 2293 * Return the size of the response on success, -1 on error. 2294 * Error number is left in h_errno. 2295 * 2296 * Caller must parse answer and determine whether it answers the question. 2297 */ 2298static int 2299res_queryN(name, target) 2300 const char *name; /* domain name */ 2301 struct res_target *target; 2302{ 2303 u_char *buf; 2304 HEADER *hp; 2305 int n; 2306 struct res_target *t; 2307 int rcode; 2308 int ancount; 2309 2310 rcode = NOERROR; 2311 ancount = 0; 2312 2313 buf = malloc(MAXPACKET); 2314 if (!buf) { 2315 h_errno = NETDB_INTERNAL; 2316 return -1; 2317 } 2318 2319 for (t = target; t; t = t->next) { 2320 int class, type; 2321 u_char *answer; 2322 int anslen; 2323 2324 hp = (HEADER *)(void *)t->answer; 2325 hp->rcode = NOERROR; /* default */ 2326 2327 /* make it easier... */ 2328 class = t->qclass; 2329 type = t->qtype; 2330 answer = t->answer; 2331 anslen = t->anslen; 2332#ifdef DEBUG 2333 if (_res.options & RES_DEBUG) 2334 printf(";; res_query(%s, %d, %d)\n", name, class, type); 2335#endif 2336 2337 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, 2338 buf, MAXPACKET); 2339 if (n > 0 && (_res.options & RES_USE_EDNS0) != 0) 2340 n = res_opt(n, buf, MAXPACKET, anslen); 2341 if (n <= 0) { 2342#ifdef DEBUG 2343 if (_res.options & RES_DEBUG) 2344 printf(";; res_query: mkquery failed\n"); 2345#endif 2346 free(buf); 2347 h_errno = NO_RECOVERY; 2348 return (n); 2349 } 2350 n = res_send(buf, n, answer, anslen); 2351#if 0 2352 if (n < 0) { 2353#ifdef DEBUG 2354 if (_res.options & RES_DEBUG) 2355 printf(";; res_query: send error\n"); 2356#endif 2357 free(buf); 2358 h_errno = TRY_AGAIN; 2359 return (n); 2360 } 2361#endif 2362 2363 if (n < 0 || n > anslen) 2364 hp->rcode = FORMERR; /* XXX not very informative */ 2365 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 2366 rcode = hp->rcode; /* record most recent error */ 2367#ifdef DEBUG 2368 if (_res.options & RES_DEBUG) 2369 printf(";; rcode = %u, ancount=%u\n", hp->rcode, 2370 ntohs(hp->ancount)); 2371#endif 2372 continue; 2373 } 2374 2375 ancount += ntohs(hp->ancount); 2376 2377 t->n = n; 2378 } 2379 2380 free(buf); 2381 2382 if (ancount == 0) { 2383 switch (rcode) { 2384 case NXDOMAIN: 2385 h_errno = HOST_NOT_FOUND; 2386 break; 2387 case SERVFAIL: 2388 h_errno = TRY_AGAIN; 2389 break; 2390 case NOERROR: 2391 h_errno = NO_DATA; 2392 break; 2393 case FORMERR: 2394 case NOTIMP: 2395 case REFUSED: 2396 default: 2397 h_errno = NO_RECOVERY; 2398 break; 2399 } 2400 return (-1); 2401 } 2402 return (ancount); 2403} 2404 2405/* 2406 * Formulate a normal query, send, and retrieve answer in supplied buffer. 2407 * Return the size of the response on success, -1 on error. 2408 * If enabled, implement search rules until answer or unrecoverable failure 2409 * is detected. Error code, if any, is left in h_errno. 2410 */ 2411static int 2412res_searchN(name, target) 2413 const char *name; /* domain name */ 2414 struct res_target *target; 2415{ 2416 const char *cp, * const *domain; 2417 HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ 2418 u_int dots; 2419 int trailing_dot, ret, saved_herrno; 2420 int got_nodata = 0, got_servfail = 0, tried_as_is = 0; 2421 char abuf[MAXDNAME]; 2422 2423 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 2424 h_errno = NETDB_INTERNAL; 2425 return (-1); 2426 } 2427 2428 errno = 0; 2429 h_errno = HOST_NOT_FOUND; /* default, if we never query */ 2430 dots = 0; 2431 for (cp = name; *cp; cp++) 2432 dots += (*cp == '.'); 2433 trailing_dot = 0; 2434 if (cp > name && *--cp == '.') 2435 trailing_dot++; 2436 2437 /* 2438 * if there aren't any dots, it could be a user-level alias 2439 */ 2440 if (!dots && (cp = _res_hostalias(name, abuf, sizeof(abuf))) != NULL) 2441 return (res_queryN(cp, target)); 2442 2443 /* 2444 * If there are dots in the name already, let's just give it a try 2445 * 'as is'. The threshold can be set with the "ndots" option. 2446 */ 2447 saved_herrno = -1; 2448 if (dots >= _res.ndots) { 2449 ret = res_querydomainN(name, NULL, target); 2450 if (ret > 0) 2451 return (ret); 2452 saved_herrno = h_errno; 2453 tried_as_is++; 2454 } 2455 2456 /* 2457 * We do at least one level of search if 2458 * - there is no dot and RES_DEFNAME is set, or 2459 * - there is at least one dot, there is no trailing dot, 2460 * and RES_DNSRCH is set. 2461 */ 2462 if ((!dots && (_res.options & RES_DEFNAMES)) || 2463 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { 2464 int done = 0; 2465 2466 for (domain = (const char * const *)_res.dnsrch; 2467 *domain && !done; 2468 domain++) { 2469 2470 ret = res_querydomainN(name, *domain, target); 2471 if (ret > 0) 2472 return (ret); 2473 2474 /* 2475 * If no server present, give up. 2476 * If name isn't found in this domain, 2477 * keep trying higher domains in the search list 2478 * (if that's enabled). 2479 * On a NO_DATA error, keep trying, otherwise 2480 * a wildcard entry of another type could keep us 2481 * from finding this entry higher in the domain. 2482 * If we get some other error (negative answer or 2483 * server failure), then stop searching up, 2484 * but try the input name below in case it's 2485 * fully-qualified. 2486 */ 2487 if (errno == ECONNREFUSED) { 2488 h_errno = TRY_AGAIN; 2489 return (-1); 2490 } 2491 2492 switch (h_errno) { 2493 case NO_DATA: 2494 got_nodata++; 2495 /* FALLTHROUGH */ 2496 case HOST_NOT_FOUND: 2497 /* keep trying */ 2498 break; 2499 case TRY_AGAIN: 2500 if (hp->rcode == SERVFAIL) { 2501 /* try next search element, if any */ 2502 got_servfail++; 2503 break; 2504 } 2505 /* FALLTHROUGH */ 2506 default: 2507 /* anything else implies that we're done */ 2508 done++; 2509 } 2510 /* 2511 * if we got here for some reason other than DNSRCH, 2512 * we only wanted one iteration of the loop, so stop. 2513 */ 2514 if (!(_res.options & RES_DNSRCH)) 2515 done++; 2516 } 2517 } 2518 2519 /* 2520 * if we have not already tried the name "as is", do that now. 2521 * note that we do this regardless of how many dots were in the 2522 * name or whether it ends with a dot. 2523 */ 2524 if (!tried_as_is && (dots || !(_res.options & RES_NOTLDQUERY))) { 2525 ret = res_querydomainN(name, NULL, target); 2526 if (ret > 0) 2527 return (ret); 2528 } 2529 2530 /* 2531 * if we got here, we didn't satisfy the search. 2532 * if we did an initial full query, return that query's h_errno 2533 * (note that we wouldn't be here if that query had succeeded). 2534 * else if we ever got a nodata, send that back as the reason. 2535 * else send back meaningless h_errno, that being the one from 2536 * the last DNSRCH we did. 2537 */ 2538 if (saved_herrno != -1) 2539 h_errno = saved_herrno; 2540 else if (got_nodata) 2541 h_errno = NO_DATA; 2542 else if (got_servfail) 2543 h_errno = TRY_AGAIN; 2544 return (-1); 2545} 2546 2547/* 2548 * Perform a call on res_query on the concatenation of name and domain, 2549 * removing a trailing dot from name if domain is NULL. 2550 */ 2551static int 2552res_querydomainN(name, domain, target) 2553 const char *name, *domain; 2554 struct res_target *target; 2555{ 2556 char nbuf[MAXDNAME]; 2557 const char *longname = nbuf; 2558 size_t n, d; 2559 2560#ifdef DEBUG 2561 if (_res.options & RES_DEBUG) 2562 printf(";; res_querydomain(%s, %s)\n", 2563 name, domain?domain:"<Nil>"); 2564#endif 2565 if (domain == NULL) { 2566 /* 2567 * Check for trailing '.'; 2568 * copy without '.' if present. 2569 */ 2570 n = strlen(name); 2571 if (n >= MAXDNAME) { 2572 h_errno = NO_RECOVERY; 2573 return (-1); 2574 } 2575 if (n > 0 && name[--n] == '.') { 2576 strncpy(nbuf, name, n); 2577 nbuf[n] = '\0'; 2578 } else 2579 longname = name; 2580 } else { 2581 n = strlen(name); 2582 d = strlen(domain); 2583 if (n + d + 1 >= MAXDNAME) { 2584 h_errno = NO_RECOVERY; 2585 return (-1); 2586 } 2587 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); 2588 } 2589 return (res_queryN(longname, target)); 2590} 2591