+++ /dev/null
-/* $NetBSD: getaddrinfo.c,v 1.91.6.1 2009/01/26 00:27:34 snj Exp $ */\r
-/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */\r
-\r
-/*\r
- * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * 3. Neither the name of the project nor the names of its contributors\r
- * may be used to endorse or promote products derived from this software\r
- * without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND\r
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE\r
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
- * SUCH DAMAGE.\r
- */\r
-\r
-/*\r
- * Issues to be discussed:\r
- * - Return values. There are nonstandard return values defined and used\r
- * in the source code. This is because RFC2553 is silent about which error\r
- * code must be returned for which situation.\r
- * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2\r
- * says to use inet_aton() to convert IPv4 numeric to binary (alows\r
- * classful form as a result).\r
- * current code - disallow classful form for IPv4 (due to use of inet_pton).\r
- * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is\r
- * invalid.\r
- * current code - SEGV on freeaddrinfo(NULL)\r
- * Note:\r
- * - The code filters out AFs that are not supported by the kernel,\r
- * when globbing NULL hostname (to loopback, or wildcard). Is it the right\r
- * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG\r
- * in ai_flags?\r
- * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague.\r
- * (1) what should we do against numeric hostname (2) what should we do\r
- * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready?\r
- * non-loopback address configured? global address configured?\r
- */\r
-\r
-#include <sys/cdefs.h>\r
-#if defined(LIBC_SCCS) && !defined(lint)\r
-__RCSID("$NetBSD: getaddrinfo.c,v 1.91.6.1 2009/01/26 00:27:34 snj Exp $");\r
-#endif /* LIBC_SCCS and not lint */\r
-\r
-#define INET6 1\r
-\r
-#include "namespace.h"\r
-#include <sys/types.h>\r
-#include <sys/param.h>\r
-#include <sys/socket.h>\r
-#include <net/if.h>\r
-#include <netinet/in.h>\r
-#include <arpa/inet.h>\r
-#include <arpa/nameser.h>\r
-#include <assert.h>\r
-#include <ctype.h>\r
-#include <errno.h>\r
-#include <netdb.h>\r
-#include <resolv.h>\r
-#include <stddef.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-#include <unistd.h>\r
-\r
-#include <stdarg.h>\r
-#include <nsswitch.h>\r
-#include <resolv.h>\r
-\r
-//#ifdef YP\r
-//#include <rpc/rpc.h>\r
-//#include <rpcsvc/yp_prot.h>\r
-//#include <rpcsvc/ypclnt.h>\r
-//#endif\r
-\r
-#include <net/servent.h>\r
-\r
-#define endservent_r(svd) endservent()\r
-#define res_nmkquery(state,op,dname,class,type,data,datalen,newrr_in,buf,buflen) res_mkquery( op, dname, class, type, data, datalen, newrr_in, buf, buflen )\r
-#define res_nsend(state,buf,buflen,ans,anssiz) res_send ( buf, buflen, ans, anssiz )\r
-\r
-/* Things involving an internal (static) resolver context. */\r
-__BEGIN_DECLS\r
-#define __res_get_state() (( 0 != _res.nscount ) ? &_res : NULL )\r
-#define __res_put_state(state)\r
-#define __res_state() _res\r
-__END_DECLS\r
-\r
-#ifdef __weak_alias\r
-__weak_alias(getaddrinfo,_getaddrinfo)\r
-__weak_alias(freeaddrinfo,_freeaddrinfo)\r
-__weak_alias(gai_strerror,_gai_strerror)\r
-#endif\r
-\r
-#define SUCCESS 0\r
-#define ANY 0\r
-#define YES 1\r
-#define NO 0\r
-\r
-static const char in_addrany[] = { 0, 0, 0, 0 };\r
-static const char in_loopback[] = { 127, 0, 0, 1 };\r
-#ifdef INET6\r
-static const char in6_addrany[] = {\r
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\r
-};\r
-static const char in6_loopback[] = {\r
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1\r
-};\r
-#endif\r
-\r
-static const struct afd {\r
- int a_af;\r
- int a_addrlen;\r
- int a_socklen;\r
- int a_off;\r
- const char *a_addrany;\r
- const char *a_loopback; \r
- int a_scoped;\r
-} afdl [] = {\r
-#ifdef INET6\r
- {PF_INET6, sizeof(struct in6_addr),\r
- sizeof(struct sockaddr_in6),\r
- offsetof(struct sockaddr_in6, sin6_addr),\r
- in6_addrany, in6_loopback, 1},\r
-#endif\r
- {PF_INET, sizeof(struct in_addr),\r
- sizeof(struct sockaddr_in),\r
- offsetof(struct sockaddr_in, sin_addr),\r
- in_addrany, in_loopback, 0},\r
- {0, 0, 0, 0, NULL, NULL, 0},\r
-};\r
-\r
-struct explore {\r
- int e_af;\r
- int e_socktype;\r
- int e_protocol;\r
- const char *e_protostr;\r
- int e_wild;\r
-#define WILD_AF(ex) ((ex)->e_wild & 0x01)\r
-#define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02)\r
-#define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04)\r
-};\r
-\r
-static const struct explore explore[] = {\r
-#if 0\r
- { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 },\r
-#endif\r
-#ifdef INET6\r
- { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },\r
- { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },\r
- { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 },\r
-#endif\r
- { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },\r
- { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },\r
- { PF_INET, SOCK_RAW, ANY, NULL, 0x05 },\r
- { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 },\r
- { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 },\r
- { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 },\r
- { -1, 0, 0, NULL, 0 },\r
-};\r
-\r
-#ifdef INET6\r
-#define PTON_MAX 16\r
-#else\r
-#define PTON_MAX 4\r
-#endif\r
-\r
-static const ns_src default_dns_files[] = {\r
- { NSSRC_FILES, NS_SUCCESS },\r
- { NSSRC_DNS, NS_SUCCESS },\r
- { 0, 0 }\r
-};\r
-\r
-#define MAXPACKET (64*1024)\r
-\r
-typedef union {\r
- HEADER hdr;\r
- u_char buf[MAXPACKET];\r
-} querybuf;\r
-\r
-struct res_target {\r
- struct res_target *next;\r
- const char *name; /* domain name */\r
- int qclass, qtype; /* class and type of query */\r
- u_char *answer; /* buffer to put answer */\r
- int anslen; /* size of answer buffer */\r
- int n; /* result length */\r
-};\r
-\r
-static int str2number(const char *);\r
-static int explore_fqdn(const struct addrinfo *, const char *,\r
- const char *, struct addrinfo **, struct servent_data *);\r
-static int explore_null(const struct addrinfo *,\r
- const char *, struct addrinfo **, struct servent_data *);\r
-static int explore_numeric(const struct addrinfo *, const char *,\r
- const char *, struct addrinfo **, const char *, struct servent_data *);\r
-static int explore_numeric_scope(const struct addrinfo *, const char *,\r
- const char *, struct addrinfo **, struct servent_data *);\r
-static int get_canonname(const struct addrinfo *,\r
- struct addrinfo *, const char *);\r
-static struct addrinfo *get_ai(const struct addrinfo *,\r
- const struct afd *, const char *);\r
-static int get_portmatch(const struct addrinfo *, const char *,\r
- struct servent_data *);\r
-static int get_port(const struct addrinfo *, const char *, int,\r
- struct servent_data *);\r
-static const struct afd *find_afd(int);\r
-#ifdef INET6\r
-static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);\r
-#endif\r
-\r
-static struct addrinfo *getanswer(const querybuf *, int, const char *, int,\r
- const struct addrinfo *);\r
-static void aisort(struct addrinfo *s, res_state res);\r
-static int _dns_getaddrinfo(void *, void *, va_list);\r
-static void _sethtent(FILE **);\r
-static void _endhtent(FILE **);\r
-static struct addrinfo *_gethtent(FILE **, const char *,\r
- const struct addrinfo *);\r
-static int _files_getaddrinfo(void *, void *, va_list);\r
-#ifdef YP\r
-static struct addrinfo *_yphostent(char *, const struct addrinfo *);\r
-static int _yp_getaddrinfo(void *, void *, va_list);\r
-#endif\r
-\r
-static int res_queryN(const char *, struct res_target *, res_state);\r
-static int res_searchN(const char *, struct res_target *, res_state);\r
-static int res_querydomainN(const char *, const char *,\r
- struct res_target *, res_state);\r
-\r
-static const char * const ai_errlist[] = {\r
- "Success",\r
- "Address family for hostname not supported", /* EAI_ADDRFAMILY */\r
- "Temporary failure in name resolution", /* EAI_AGAIN */\r
- "Invalid value for ai_flags", /* EAI_BADFLAGS */\r
- "Non-recoverable failure in name resolution", /* EAI_FAIL */\r
- "ai_family not supported", /* EAI_FAMILY */\r
- "Memory allocation failure", /* EAI_MEMORY */\r
- "No address associated with hostname", /* EAI_NODATA */\r
- "hostname nor servname provided, or not known", /* EAI_NONAME */\r
- "servname not supported for ai_socktype", /* EAI_SERVICE */\r
- "ai_socktype not supported", /* EAI_SOCKTYPE */\r
- "System error returned in errno", /* EAI_SYSTEM */\r
- "Invalid value for hints", /* EAI_BADHINTS */\r
- "Resolved protocol is unknown", /* EAI_PROTOCOL */\r
- "Argument buffer overflow", /* EAI_OVERFLOW */\r
- "Unknown error", /* EAI_MAX */\r
-};\r
-\r
-/* XXX macros that make external reference is BAD. */\r
-\r
-#define GET_AI(ai, afd, addr) \\r
-do { \\r
- /* external reference: pai, error, and label free */ \\r
- (ai) = get_ai(pai, (afd), (addr)); \\r
- if ((ai) == NULL) { \\r
- error = EAI_MEMORY; \\r
- goto free; \\r
- } \\r
-} while (/*CONSTCOND*/0)\r
-\r
-#define GET_PORT(ai, serv, svd) \\r
-do { \\r
- /* external reference: error and label free */ \\r
- error = get_port((ai), (serv), 0, (svd)); \\r
- if (error != 0) \\r
- goto free; \\r
-} while (/*CONSTCOND*/0)\r
-\r
-#define GET_CANONNAME(ai, str) \\r
-do { \\r
- /* external reference: pai, error and label free */ \\r
- error = get_canonname(pai, (ai), (str)); \\r
- if (error != 0) \\r
- goto free; \\r
-} while (/*CONSTCOND*/0)\r
-\r
-#define ERR(err) \\r
-do { \\r
- /* external reference: error, and label bad */ \\r
- error = (err); \\r
- goto bad; \\r
- /*NOTREACHED*/ \\r
-} while (/*CONSTCOND*/0)\r
-\r
-#define MATCH_FAMILY(x, y, w) \\r
- ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \\r
- (y) == PF_UNSPEC))) \r
-#define MATCH(x, y, w) \\r
- ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY)))\r
-\r
-int nsdispatch(void *result, const ns_dtab dist_tab[], const char* database,\r
- const char *method, const ns_src defaults[], ...)\r
-{\r
- return NS_NOTFOUND;\r
-}\r
-\r
-const char *\r
-gai_strerror(int ecode)\r
-{\r
- if (ecode < 0 || ecode > EAI_MAX)\r
- ecode = EAI_MAX;\r
- return ai_errlist[ecode];\r
-}\r
-\r
-void\r
-freeaddrinfo(struct addrinfo *ai)\r
-{\r
- struct addrinfo *next;\r
-\r
- _DIAGASSERT(ai != NULL);\r
-\r
- do {\r
- next = ai->ai_next;\r
- if (ai->ai_canonname)\r
- free(ai->ai_canonname);\r
- /* no need to free(ai->ai_addr) */\r
- free(ai);\r
- ai = next;\r
- } while (ai);\r
-}\r
-\r
-static int\r
-str2number(const char *p)\r
-{\r
- char *ep;\r
- unsigned long v;\r
-\r
- _DIAGASSERT(p != NULL);\r
-\r
- if (*p == '\0')\r
- return -1;\r
- ep = NULL;\r
- errno = 0;\r
- v = strtoul(p, &ep, 10);\r
- if (errno == 0 && ep && *ep == '\0' && v <= UINT_MAX)\r
- return v;\r
- else\r
- return -1;\r
-}\r
-\r
-int\r
-getaddrinfo(const char *hostname, const char *servname,\r
- const struct addrinfo *hints, struct addrinfo **res)\r
-{\r
- struct addrinfo sentinel;\r
- struct addrinfo *cur;\r
- int error = 0;\r
- struct addrinfo ai;\r
- struct addrinfo ai0;\r
- struct addrinfo *pai;\r
- const struct explore *ex;\r
- struct servent_data svd;\r
-\r
- /* hostname is allowed to be NULL */\r
- /* servname is allowed to be NULL */\r
- /* hints is allowed to be NULL */\r
- _DIAGASSERT(res != NULL);\r
-\r
- (void)memset(&svd, 0, sizeof(svd));\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
- memset(&ai, 0, sizeof(ai));\r
- pai = &ai;\r
- pai->ai_flags = 0;\r
- pai->ai_family = PF_UNSPEC;\r
- pai->ai_socktype = ANY;\r
- pai->ai_protocol = ANY;\r
- pai->ai_addrlen = 0;\r
- pai->ai_canonname = NULL;\r
- pai->ai_addr = NULL;\r
- pai->ai_next = NULL;\r
- \r
- if (hostname == NULL && servname == NULL)\r
- return EAI_NONAME;\r
- if (hints) {\r
- /* error check for hints */\r
- if (hints->ai_addrlen || hints->ai_canonname ||\r
- hints->ai_addr || hints->ai_next)\r
- ERR(EAI_BADHINTS); /* xxx */\r
- if (hints->ai_flags & ~AI_MASK)\r
- ERR(EAI_BADFLAGS);\r
- switch (hints->ai_family) {\r
- case PF_UNSPEC:\r
- case PF_INET:\r
-#ifdef INET6\r
- case PF_INET6:\r
-#endif\r
- break;\r
- default:\r
- ERR(EAI_FAMILY);\r
- }\r
- memcpy(pai, hints, sizeof(*pai));\r
-\r
- /*\r
- * if both socktype/protocol are specified, check if they\r
- * are meaningful combination.\r
- */\r
- if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) {\r
- for (ex = explore; ex->e_af >= 0; ex++) {\r
- if (pai->ai_family != ex->e_af)\r
- continue;\r
- if (ex->e_socktype == ANY)\r
- continue;\r
- if (ex->e_protocol == ANY)\r
- continue;\r
- if (pai->ai_socktype == ex->e_socktype\r
- && pai->ai_protocol != ex->e_protocol) {\r
- ERR(EAI_BADHINTS);\r
- }\r
- }\r
- }\r
- }\r
-\r
- /*\r
- * check for special cases. (1) numeric servname is disallowed if\r
- * socktype/protocol are left unspecified. (2) servname is disallowed\r
- * for raw and other inet{,6} sockets.\r
- */\r
- if (MATCH_FAMILY(pai->ai_family, PF_INET, 1)\r
-#ifdef PF_INET6\r
- || MATCH_FAMILY(pai->ai_family, PF_INET6, 1)\r
-#endif\r
- ) {\r
- ai0 = *pai; /* backup *pai */\r
-\r
- if (pai->ai_family == PF_UNSPEC) {\r
-#ifdef PF_INET6\r
- pai->ai_family = PF_INET6;\r
-#else\r
- pai->ai_family = PF_INET;\r
-#endif\r
- }\r
- error = get_portmatch(pai, servname, &svd);\r
- if (error)\r
- ERR(error);\r
-\r
- *pai = ai0;\r
- }\r
-\r
- ai0 = *pai;\r
-\r
- /* NULL hostname, or numeric hostname */\r
- for (ex = explore; ex->e_af >= 0; ex++) {\r
- *pai = ai0;\r
-\r
- /* PF_UNSPEC entries are prepared for DNS queries only */\r
- if (ex->e_af == PF_UNSPEC)\r
- continue;\r
-\r
- if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex)))\r
- continue;\r
- if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex)))\r
- continue;\r
- if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex)))\r
- continue;\r
-\r
- if (pai->ai_family == PF_UNSPEC)\r
- pai->ai_family = ex->e_af;\r
- if (pai->ai_socktype == ANY && ex->e_socktype != ANY)\r
- pai->ai_socktype = ex->e_socktype;\r
- if (pai->ai_protocol == ANY && ex->e_protocol != ANY)\r
- pai->ai_protocol = ex->e_protocol;\r
-\r
- if (hostname == NULL)\r
- error = explore_null(pai, servname, &cur->ai_next,\r
- &svd);\r
- else\r
- error = explore_numeric_scope(pai, hostname, servname,\r
- &cur->ai_next, &svd);\r
-\r
- if (error)\r
- goto free;\r
-\r
- while (cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
-\r
- /*\r
- * XXX\r
- * If numeric representation of AF1 can be interpreted as FQDN\r
- * representation of AF2, we need to think again about the code below.\r
- */\r
- if (sentinel.ai_next)\r
- goto good;\r
-\r
- if (hostname == NULL)\r
- ERR(EAI_NODATA);\r
- if (pai->ai_flags & AI_NUMERICHOST)\r
- ERR(EAI_NONAME);\r
-\r
- /*\r
- * hostname as alphabetical name.\r
- * we would like to prefer AF_INET6 than AF_INET, so we'll make a\r
- * outer loop by AFs.\r
- */\r
- for (ex = explore; ex->e_af >= 0; ex++) {\r
- *pai = ai0;\r
-\r
- /* require exact match for family field */\r
- if (pai->ai_family != ex->e_af)\r
- continue;\r
-\r
- if (!MATCH(pai->ai_socktype, ex->e_socktype,\r
- WILD_SOCKTYPE(ex))) {\r
- continue;\r
- }\r
- if (!MATCH(pai->ai_protocol, ex->e_protocol,\r
- WILD_PROTOCOL(ex))) {\r
- continue;\r
- }\r
-\r
- if (pai->ai_socktype == ANY && ex->e_socktype != ANY)\r
- pai->ai_socktype = ex->e_socktype;\r
- if (pai->ai_protocol == ANY && ex->e_protocol != ANY)\r
- pai->ai_protocol = ex->e_protocol;\r
-\r
- error = explore_fqdn(pai, hostname, servname, &cur->ai_next,\r
- &svd);\r
-\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
-\r
- /* XXX */\r
- if (sentinel.ai_next)\r
- error = 0;\r
-\r
- if (error)\r
- goto free;\r
-\r
- if (sentinel.ai_next) {\r
- good:\r
- endservent_r(&svd);\r
- *res = sentinel.ai_next;\r
- return SUCCESS;\r
- } else\r
- error = EAI_FAIL;\r
- free:\r
- bad:\r
- endservent_r(&svd);\r
- if (sentinel.ai_next)\r
- freeaddrinfo(sentinel.ai_next);\r
- *res = NULL;\r
- return error;\r
-}\r
-\r
-static const ns_dtab dtab[] = {\r
- NS_FILES_CB(((nss_method)_files_getaddrinfo), NULL)\r
- { NSSRC_DNS, ((nss_method)_dns_getaddrinfo), NULL }, /* force -DHESIOD */\r
- NS_NIS_CB(_yp_getaddrinfo, NULL)\r
- NS_NULL_CB\r
-};\r
-\r
-/*\r
- * FQDN hostname, DNS lookup\r
- */\r
-static int\r
-explore_fqdn(const struct addrinfo *pai, const char *hostname,\r
- const char *servname, struct addrinfo **res, struct servent_data *svd)\r
-{\r
- struct addrinfo *result;\r
- struct addrinfo *cur;\r
- int error = 0;\r
-\r
- _DIAGASSERT(pai != NULL);\r
- /* hostname may be NULL */\r
- /* servname may be NULL */\r
- _DIAGASSERT(res != NULL);\r
-\r
- result = NULL;\r
-\r
- /*\r
- * if the servname does not match socktype/protocol, ignore it.\r
- */\r
- if (get_portmatch(pai, servname, svd) != 0)\r
- return 0;\r
-\r
- switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo",\r
- default_dns_files, hostname, pai)) {\r
- case NS_TRYAGAIN:\r
- error = EAI_AGAIN;\r
- goto free;\r
- case NS_UNAVAIL:\r
- error = EAI_FAIL;\r
- goto free;\r
- case NS_NOTFOUND:\r
- error = EAI_NODATA;\r
- goto free;\r
- case NS_SUCCESS:\r
- error = 0;\r
- for (cur = result; cur; cur = cur->ai_next) {\r
- GET_PORT(cur, servname, svd);\r
- /* canonname should be filled already */\r
- }\r
- break;\r
- }\r
-\r
- *res = result;\r
-\r
- return 0;\r
-\r
-free:\r
- if (result)\r
- freeaddrinfo(result);\r
- return error;\r
-}\r
-\r
-/*\r
- * hostname == NULL.\r
- * passive socket -> anyaddr (0.0.0.0 or ::)\r
- * non-passive socket -> localhost (127.0.0.1 or ::1)\r
- */\r
-static int\r
-explore_null(const struct addrinfo *pai, const char *servname,\r
- struct addrinfo **res, struct servent_data *svd)\r
-{\r
- int s;\r
- const struct afd *afd;\r
- struct addrinfo *cur;\r
- struct addrinfo sentinel;\r
- int error;\r
-\r
- _DIAGASSERT(pai != NULL);\r
- /* servname may be NULL */\r
- _DIAGASSERT(res != NULL);\r
-\r
- *res = NULL;\r
- sentinel.ai_next = NULL;\r
- cur = &sentinel;\r
-\r
- /*\r
- * filter out AFs that are not supported by the kernel\r
- * XXX errno?\r
- */\r
- s = socket(pai->ai_family, SOCK_DGRAM, 0);\r
- if (s < 0) {\r
- if (errno != EMFILE)\r
- return 0;\r
- } else\r
- close(s);\r
-\r
- /*\r
- * if the servname does not match socktype/protocol, ignore it.\r
- */\r
- if (get_portmatch(pai, servname, svd) != 0)\r
- return 0;\r
-\r
- afd = find_afd(pai->ai_family);\r
- if (afd == NULL)\r
- return 0;\r
-\r
- if (pai->ai_flags & AI_PASSIVE) {\r
- GET_AI(cur->ai_next, afd, afd->a_addrany);\r
- /* xxx meaningless?\r
- * GET_CANONNAME(cur->ai_next, "anyaddr");\r
- */\r
- GET_PORT(cur->ai_next, servname, svd);\r
- } else {\r
- GET_AI(cur->ai_next, afd, afd->a_loopback);\r
- /* xxx meaningless?\r
- * GET_CANONNAME(cur->ai_next, "localhost");\r
- */\r
- GET_PORT(cur->ai_next, servname, svd);\r
- }\r
- cur = cur->ai_next;\r
-\r
- *res = sentinel.ai_next;\r
- return 0;\r
-\r
-free:\r
- if (sentinel.ai_next)\r
- freeaddrinfo(sentinel.ai_next);\r
- return error;\r
-}\r
-\r
-/*\r
- * numeric hostname\r
- */\r
-static int\r
-explore_numeric(const struct addrinfo *pai, const char *hostname,\r
- const char *servname, struct addrinfo **res, const char *canonname,\r
- struct servent_data *svd)\r
-{\r
- const struct afd *afd;\r
- struct addrinfo *cur;\r
- struct addrinfo sentinel;\r
- int error;\r
- char pton[PTON_MAX];\r
-\r
- _DIAGASSERT(pai != NULL);\r
- /* hostname may be NULL */\r
- /* servname may be NULL */\r
- _DIAGASSERT(res != NULL);\r
-\r
- *res = NULL;\r
- sentinel.ai_next = NULL;\r
- cur = &sentinel;\r
-\r
- /*\r
- * if the servname does not match socktype/protocol, ignore it.\r
- */\r
- if (get_portmatch(pai, servname, svd) != 0)\r
- return 0;\r
-\r
- afd = find_afd(pai->ai_family);\r
- if (afd == NULL)\r
- return 0;\r
-\r
- switch (afd->a_af) {\r
-#if 0 /*X/Open spec*/\r
- case AF_INET:\r
- if (inet_aton(hostname, (struct in_addr *)pton) == 1) {\r
- if (pai->ai_family == afd->a_af ||\r
- pai->ai_family == PF_UNSPEC /*?*/) {\r
- GET_AI(cur->ai_next, afd, pton);\r
- GET_PORT(cur->ai_next, servname, svd);\r
- if ((pai->ai_flags & AI_CANONNAME)) {\r
- /*\r
- * Set the numeric address itself as\r
- * the canonical name, based on a\r
- * clarification in rfc2553bis-03.\r
- */\r
- GET_CANONNAME(cur->ai_next, canonname);\r
- }\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- } else\r
- ERR(EAI_FAMILY); /*xxx*/\r
- }\r
- break;\r
-#endif\r
- default:\r
- if (inet_pton(afd->a_af, hostname, pton) == 1) {\r
- if (pai->ai_family == afd->a_af ||\r
- pai->ai_family == PF_UNSPEC /*?*/) {\r
- GET_AI(cur->ai_next, afd, pton);\r
- GET_PORT(cur->ai_next, servname, svd);\r
- if ((pai->ai_flags & AI_CANONNAME)) {\r
- /*\r
- * Set the numeric address itself as\r
- * the canonical name, based on a\r
- * clarification in rfc2553bis-03.\r
- */\r
- GET_CANONNAME(cur->ai_next, canonname);\r
- }\r
- while (cur->ai_next)\r
- cur = cur->ai_next;\r
- } else\r
- ERR(EAI_FAMILY); /*xxx*/\r
- }\r
- break;\r
- }\r
-\r
- *res = sentinel.ai_next;\r
- return 0;\r
-\r
-free:\r
-bad:\r
- if (sentinel.ai_next)\r
- freeaddrinfo(sentinel.ai_next);\r
- return error;\r
-}\r
-\r
-/*\r
- * numeric hostname with scope\r
- */\r
-static int\r
-explore_numeric_scope(const struct addrinfo *pai, const char *hostname,\r
- const char *servname, struct addrinfo **res, struct servent_data *svd)\r
-{\r
-#if !defined(SCOPE_DELIMITER) || !defined(INET6)\r
- return explore_numeric(pai, hostname, servname, res, hostname, svd);\r
-#else\r
- const struct afd *afd;\r
- struct addrinfo *cur;\r
- int error;\r
- char *cp, *hostname2 = NULL, *scope, *addr;\r
- struct sockaddr_in6 *sin6;\r
-\r
- _DIAGASSERT(pai != NULL);\r
- /* hostname may be NULL */\r
- /* servname may be NULL */\r
- _DIAGASSERT(res != NULL);\r
-\r
- /*\r
- * if the servname does not match socktype/protocol, ignore it.\r
- */\r
- if (get_portmatch(pai, servname, svd) != 0)\r
- return 0;\r
-\r
- afd = find_afd(pai->ai_family);\r
- if (afd == NULL)\r
- return 0;\r
-\r
- if (!afd->a_scoped)\r
- return explore_numeric(pai, hostname, servname, res, hostname,\r
- svd);\r
-\r
- cp = strchr(hostname, SCOPE_DELIMITER);\r
- if (cp == NULL)\r
- return explore_numeric(pai, hostname, servname, res, hostname,\r
- svd);\r
-\r
- /*\r
- * Handle special case of <scoped_address><delimiter><scope id>\r
- */\r
- hostname2 = strdup(hostname);\r
- if (hostname2 == NULL)\r
- return EAI_MEMORY;\r
- /* terminate at the delimiter */\r
- hostname2[cp - hostname] = '\0';\r
- addr = hostname2;\r
- scope = cp + 1;\r
-\r
- error = explore_numeric(pai, addr, servname, res, hostname, svd);\r
- if (error == 0) {\r
- u_int32_t scopeid;\r
-\r
- for (cur = *res; cur; cur = cur->ai_next) {\r
- if (cur->ai_family != AF_INET6)\r
- continue;\r
- sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr;\r
- if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {\r
- free(hostname2);\r
- return(EAI_NODATA); /* XXX: is return OK? */\r
- }\r
- sin6->sin6_scope_id = scopeid;\r
- }\r
- }\r
-\r
- free(hostname2);\r
-\r
- return error;\r
-#endif\r
-}\r
-\r
-static int\r
-get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str)\r
-{\r
-\r
- _DIAGASSERT(pai != NULL);\r
- _DIAGASSERT(ai != NULL);\r
- _DIAGASSERT(str != NULL);\r
-\r
- if ((pai->ai_flags & AI_CANONNAME) != 0) {\r
- ai->ai_canonname = strdup(str);\r
- if (ai->ai_canonname == NULL)\r
- return EAI_MEMORY;\r
- }\r
- return 0;\r
-}\r
-\r
-static struct addrinfo *\r
-get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr)\r
-{\r
- char *p;\r
- struct addrinfo *ai;\r
-\r
- _DIAGASSERT(pai != NULL);\r
- _DIAGASSERT(afd != NULL);\r
- _DIAGASSERT(addr != NULL);\r
-\r
- ai = (struct addrinfo *)malloc(sizeof(struct addrinfo)\r
- + (afd->a_socklen));\r
- if (ai == NULL)\r
- return NULL;\r
-\r
- memcpy(ai, pai, sizeof(struct addrinfo));\r
- ai->ai_addr = (struct sockaddr *)(void *)(ai + 1);\r
- memset(ai->ai_addr, 0, (size_t)afd->a_socklen);\r
- ai->ai_addr->sa_len = (uint8_t)afd->a_socklen;\r
- ai->ai_addrlen = afd->a_socklen;\r
- ai->ai_family = afd->a_af;\r
- ai->ai_addr->sa_family = (sa_family_t)ai->ai_family;\r
- p = (char *)(void *)(ai->ai_addr);\r
- memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen);\r
- return ai;\r
-}\r
-\r
-static int\r
-get_portmatch(const struct addrinfo *ai, const char *servname,\r
- struct servent_data *svd)\r
-{\r
-\r
- _DIAGASSERT(ai != NULL);\r
- /* servname may be NULL */\r
-\r
- return get_port(ai, servname, 1, svd);\r
-}\r
-\r
-static int\r
-get_port(const struct addrinfo *ai, const char *servname, int matchonly,\r
- struct servent_data *svd)\r
-{\r
- const char *proto;\r
- struct servent *sp;\r
- int port;\r
- int allownumeric;\r
-\r
- _DIAGASSERT(ai != NULL);\r
- /* servname may be NULL */\r
-\r
- if (servname == NULL)\r
- return 0;\r
- switch (ai->ai_family) {\r
- case AF_INET:\r
-#ifdef AF_INET6\r
- case AF_INET6:\r
-#endif\r
- break;\r
- default:\r
- return 0;\r
- }\r
-\r
- switch (ai->ai_socktype) {\r
- case SOCK_RAW:\r
- return EAI_SERVICE;\r
- case SOCK_DGRAM:\r
- case SOCK_STREAM:\r
- allownumeric = 1;\r
- break;\r
- case ANY:\r
- /*\r
- * This was 0. It is now 1 so that queries specifying\r
- * a NULL hint, or hint without socktype (but, hopefully,\r
- * with protocol) and numeric address actually work.\r
- */\r
- allownumeric = 1;\r
- break;\r
- default:\r
- return EAI_SOCKTYPE;\r
- }\r
-\r
- port = str2number(servname);\r
- if (port >= 0) {\r
- if (!allownumeric)\r
- return EAI_SERVICE;\r
- if (port < 0 || port > 65535)\r
- return EAI_SERVICE;\r
- port = htons(port);\r
- } else {\r
-// struct servent sv;\r
- if (ai->ai_flags & AI_NUMERICSERV)\r
- return EAI_NONAME;\r
-\r
- switch (ai->ai_socktype) {\r
- case SOCK_DGRAM:\r
- proto = "udp";\r
- break;\r
- case SOCK_STREAM:\r
- proto = "tcp";\r
- break;\r
- default:\r
- proto = NULL;\r
- break;\r
- }\r
-\r
-// sp = getservbyname_r(servname, proto, &sv, svd);\r
- sp = getservbyname ( servname, proto );\r
- if (sp == NULL)\r
- return EAI_SERVICE;\r
- port = sp->s_port;\r
- }\r
-\r
- if (!matchonly) {\r
- switch (ai->ai_family) {\r
- case AF_INET:\r
- ((struct sockaddr_in *)(void *)\r
- ai->ai_addr)->sin_port = (in_port_t)port;\r
- break;\r
-#ifdef INET6\r
- case AF_INET6:\r
- ((struct sockaddr_in6 *)(void *)\r
- ai->ai_addr)->sin6_port = (in_port_t)port;\r
- break;\r
-#endif\r
- }\r
- }\r
-\r
- return 0;\r
-}\r
-\r
-static const struct afd *\r
-find_afd(int af)\r
-{\r
- const struct afd *afd;\r
-\r
- if (af == PF_UNSPEC)\r
- return NULL;\r
- for (afd = afdl; afd->a_af; afd++) {\r
- if (afd->a_af == af)\r
- return afd;\r
- }\r
- return NULL;\r
-}\r
-\r
-#ifdef INET6\r
-/* convert a string to a scope identifier. XXX: IPv6 specific */\r
-static int\r
-ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid)\r
-{\r
- u_long lscopeid;\r
- struct in6_addr *a6;\r
- char *ep;\r
-\r
- _DIAGASSERT(scope != NULL);\r
- _DIAGASSERT(sin6 != NULL);\r
- _DIAGASSERT(scopeid != NULL);\r
-\r
- a6 = &sin6->sin6_addr;\r
-\r
- /* empty scopeid portion is invalid */\r
- if (*scope == '\0')\r
- return -1;\r
-\r
- if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {\r
- /*\r
- * We currently assume a one-to-one mapping between links\r
- * and interfaces, so we simply use interface indices for\r
- * like-local scopes.\r
- */\r
-/*\r
- *scopeid = if_nametoindex(scope);\r
- if (*scopeid == 0)\r
- goto trynumeric;\r
- return 0;\r
-*/\r
- return -1;\r
- }\r
-\r
- /* still unclear about literal, allow numeric only - placeholder */\r
- if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6))\r
- goto trynumeric;\r
- if (IN6_IS_ADDR_MC_ORGLOCAL(a6))\r
- goto trynumeric;\r
- else\r
- goto trynumeric; /* global */\r
-\r
- /* try to convert to a numeric id as a last resort */\r
- trynumeric:\r
- errno = 0;\r
- lscopeid = strtoul(scope, &ep, 10);\r
- *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL);\r
- if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid)\r
- return 0;\r
- else\r
- return -1;\r
-}\r
-#endif\r
-\r
-/* code duplicate with gethnamaddr.c */\r
-\r
-static struct addrinfo *\r
-getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,\r
- const struct addrinfo *pai)\r
-{\r
- struct addrinfo sentinel, *cur;\r
- struct addrinfo ai;\r
- const struct afd *afd;\r
- char *canonname;\r
- const HEADER *hp;\r
- const u_char *cp;\r
- int n;\r
- const u_char *eom;\r
- char *bp, *ep;\r
- int type, class, ancount, qdcount;\r
- int haveanswer, had_error;\r
- char tbuf[MAXDNAME];\r
- int (*name_ok) (const char *);\r
- static char hostbuf[8*1024];\r
-\r
- _DIAGASSERT(answer != NULL);\r
- _DIAGASSERT(qname != NULL);\r
- _DIAGASSERT(pai != NULL);\r
-\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
-\r
- canonname = NULL;\r
- eom = answer->buf + anslen;\r
- switch (qtype) {\r
- case T_A:\r
- case T_AAAA:\r
- case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/\r
- name_ok = res_hnok;\r
- break;\r
- default:\r
- return NULL; /* XXX should be abort(); */\r
- }\r
- /*\r
- * find first satisfactory answer\r
- */\r
- hp = &answer->hdr;\r
- ancount = ntohs(hp->ancount);\r
- qdcount = ntohs(hp->qdcount);\r
- bp = hostbuf;\r
- ep = hostbuf + sizeof hostbuf;\r
- cp = answer->buf + HFIXEDSZ;\r
- if (qdcount != 1) {\r
- h_errno = NO_RECOVERY;\r
- return (NULL);\r
- }\r
- n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));\r
- if ((n < 0) || !(*name_ok)(bp)) {\r
- h_errno = NO_RECOVERY;\r
- return (NULL);\r
- }\r
- cp += n + QFIXEDSZ;\r
- if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {\r
- /* res_send() has already verified that the query name is the\r
- * same as the one we sent; this just gets the expanded name\r
- * (i.e., with the succeeding search-domain tacked on).\r
- */\r
- n = (int)strlen(bp) + 1; /* for the \0 */\r
- if (n >= MAXHOSTNAMELEN) {\r
- h_errno = NO_RECOVERY;\r
- return (NULL);\r
- }\r
- canonname = bp;\r
- bp += n;\r
- /* The qname can be abbreviated, but h_name is now absolute. */\r
- qname = canonname;\r
- }\r
- haveanswer = 0;\r
- had_error = 0;\r
- while (ancount-- > 0 && cp < eom && !had_error) {\r
- n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));\r
- if ((n < 0) || !(*name_ok)(bp)) {\r
- had_error++;\r
- continue;\r
- }\r
- cp += n; /* name */\r
- type = _getshort(cp);\r
- cp += INT16SZ; /* type */\r
- class = _getshort(cp);\r
- cp += INT16SZ + INT32SZ; /* class, TTL */\r
- n = _getshort(cp);\r
- cp += INT16SZ; /* len */\r
- if (class != C_IN) {\r
- /* XXX - debug? syslog? */\r
- cp += n;\r
- continue; /* XXX - had_error++ ? */\r
- }\r
- if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&\r
- type == T_CNAME) {\r
- n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);\r
- if ((n < 0) || !(*name_ok)(tbuf)) {\r
- had_error++;\r
- continue;\r
- }\r
- cp += n;\r
- /* Get canonical name. */\r
- n = (int)strlen(tbuf) + 1; /* for the \0 */\r
- if (n > ep - bp || n >= MAXHOSTNAMELEN) {\r
- had_error++;\r
- continue;\r
- }\r
- strlcpy(bp, tbuf, (size_t)(ep - bp));\r
- canonname = bp;\r
- bp += n;\r
- continue;\r
- }\r
- if (qtype == T_ANY) {\r
- if (!(type == T_A || type == T_AAAA)) {\r
- cp += n;\r
- continue;\r
- }\r
- } else if (type != qtype) {\r
- if (type != T_KEY && type != T_SIG) {\r
-#ifdef _ORG_FREEBSD_\r
- struct syslog_data sd = SYSLOG_DATA_INIT;\r
- syslog_r(LOG_NOTICE|LOG_AUTH, &sd,\r
- "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",\r
- qname, p_class(C_IN), p_type(qtype),\r
- p_type(type));\r
-#endif\r
- }\r
- cp += n;\r
- continue; /* XXX - had_error++ ? */\r
- }\r
- switch (type) {\r
- case T_A:\r
- case T_AAAA:\r
- if (strcasecmp(canonname, bp) != 0) {\r
-#ifdef _ORG_FREEBSD_\r
- struct syslog_data sd = SYSLOG_DATA_INIT;\r
- syslog_r(LOG_NOTICE|LOG_AUTH, &sd,\r
- AskedForGot, canonname, bp);\r
-#endif\r
- cp += n;\r
- continue; /* XXX - had_error++ ? */\r
- }\r
- if (type == T_A && n != INADDRSZ) {\r
- cp += n;\r
- continue;\r
- }\r
- if (type == T_AAAA && n != IN6ADDRSZ) {\r
- cp += n;\r
- continue;\r
- }\r
- if (type == T_AAAA) {\r
- struct in6_addr in6;\r
- memcpy(&in6, cp, IN6ADDRSZ);\r
- if (IN6_IS_ADDR_V4MAPPED(&in6)) {\r
- cp += n;\r
- continue;\r
- }\r
- }\r
- if (!haveanswer) {\r
- int nn;\r
-\r
- canonname = bp;\r
- nn = (int)strlen(bp) + 1; /* for the \0 */\r
- bp += nn;\r
- }\r
-\r
- /* don't overwrite pai */\r
- ai = *pai;\r
- ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;\r
- afd = find_afd(ai.ai_family);\r
- if (afd == NULL) {\r
- cp += n;\r
- continue;\r
- }\r
- cur->ai_next = get_ai(&ai, afd, (const char *)cp);\r
- if (cur->ai_next == NULL)\r
- had_error++;\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- cp += n;\r
- break;\r
- default:\r
- abort();\r
- }\r
- if (!had_error)\r
- haveanswer++;\r
- }\r
- if (haveanswer) {\r
- if (!canonname)\r
- (void)get_canonname(pai, sentinel.ai_next, qname);\r
- else\r
- (void)get_canonname(pai, sentinel.ai_next, canonname);\r
- h_errno = NETDB_SUCCESS;\r
- return sentinel.ai_next;\r
- }\r
-\r
- h_errno = NO_RECOVERY;\r
- return NULL;\r
-}\r
-\r
-#define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr)\r
-#define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr)\r
-\r
-static void\r
-aisort(struct addrinfo *s, res_state res)\r
-{\r
- struct addrinfo head, *t, *p;\r
- int i;\r
-\r
- head.ai_next = NULL;\r
- t = &head;\r
-\r
- for (i = 0; i < (int)res->nsort; i++) {\r
- p = s;\r
- while (p->ai_next) {\r
- if ((p->ai_next->ai_family != AF_INET)\r
- || SORTMATCH(p, res->sort_list[i])) {\r
- t->ai_next = p->ai_next;\r
- t = t->ai_next;\r
- p->ai_next = p->ai_next->ai_next;\r
- } else {\r
- p = p->ai_next;\r
- }\r
- }\r
- }\r
-\r
- /* add rest of list and reset s to the new list*/\r
- t->ai_next = s->ai_next;\r
- s->ai_next = head.ai_next;\r
-}\r
-\r
-/*ARGSUSED*/\r
-static int\r
-_dns_getaddrinfo(void *rv, void *cb_data, va_list ap)\r
-{\r
- struct addrinfo *ai;\r
- querybuf *buf, *buf2;\r
- const char *name;\r
- const struct addrinfo *pai;\r
- struct addrinfo sentinel, *cur;\r
- struct res_target q, q2;\r
- res_state res;\r
-\r
- name = va_arg(ap, char *);\r
- pai = va_arg(ap, const struct addrinfo *);\r
-\r
- memset(&q, 0, sizeof(q));\r
- memset(&q2, 0, sizeof(q2));\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
-\r
- buf = malloc(sizeof(*buf));\r
- if (buf == NULL) {\r
- h_errno = NETDB_INTERNAL;\r
- return NS_NOTFOUND;\r
- }\r
- buf2 = malloc(sizeof(*buf2));\r
- if (buf2 == NULL) {\r
- free(buf);\r
- h_errno = NETDB_INTERNAL;\r
- return NS_NOTFOUND;\r
- }\r
-\r
- switch (pai->ai_family) {\r
- case AF_UNSPEC:\r
- /* prefer IPv6 */\r
- q.name = name;\r
- q.qclass = C_IN;\r
- q.qtype = T_AAAA;\r
- q.answer = buf->buf;\r
- q.anslen = sizeof(buf->buf);\r
- q.next = &q2;\r
- q2.name = name;\r
- q2.qclass = C_IN;\r
- q2.qtype = T_A;\r
- q2.answer = buf2->buf;\r
- q2.anslen = sizeof(buf2->buf);\r
- break;\r
- case AF_INET:\r
- q.name = name;\r
- q.qclass = C_IN;\r
- q.qtype = T_A;\r
- q.answer = buf->buf;\r
- q.anslen = sizeof(buf->buf);\r
- break;\r
- case AF_INET6:\r
- q.name = name;\r
- q.qclass = C_IN;\r
- q.qtype = T_AAAA;\r
- q.answer = buf->buf;\r
- q.anslen = sizeof(buf->buf);\r
- break;\r
- default:\r
- free(buf);\r
- free(buf2);\r
- return NS_UNAVAIL;\r
- }\r
-\r
- res = __res_get_state();\r
- if (res == NULL) {\r
- free(buf);\r
- free(buf2);\r
- return NS_NOTFOUND;\r
- }\r
-\r
- if (res_searchN(name, &q, res) < 0) {\r
- __res_put_state(res);\r
- free(buf);\r
- free(buf2);\r
- return NS_NOTFOUND;\r
- }\r
- ai = getanswer(buf, q.n, q.name, q.qtype, pai);\r
- if (ai) {\r
- cur->ai_next = ai;\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
- if (q.next) {\r
- ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai);\r
- if (ai)\r
- cur->ai_next = ai;\r
- }\r
- free(buf);\r
- free(buf2);\r
- if (sentinel.ai_next == NULL) {\r
- __res_put_state(res);\r
- switch (h_errno) {\r
- case HOST_NOT_FOUND:\r
- return NS_NOTFOUND;\r
- case TRY_AGAIN:\r
- return NS_TRYAGAIN;\r
- default:\r
- return NS_UNAVAIL;\r
- }\r
- }\r
-\r
- if (res->nsort)\r
- aisort(&sentinel, res);\r
-\r
- __res_put_state(res);\r
-\r
- *((struct addrinfo **)rv) = sentinel.ai_next;\r
- return NS_SUCCESS;\r
-}\r
-\r
-static void\r
-_sethtent(FILE **hostf)\r
-{\r
-\r
- if (!*hostf)\r
- *hostf = fopen(_PATH_HOSTS, "r" );\r
- else\r
- rewind(*hostf);\r
-}\r
-\r
-static void\r
-_endhtent(FILE **hostf)\r
-{\r
-\r
- if (*hostf) {\r
- (void) fclose(*hostf);\r
- *hostf = NULL;\r
- }\r
-}\r
-\r
-static struct addrinfo *\r
-_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai)\r
-{\r
- char *p;\r
- char *cp, *tname, *cname;\r
- struct addrinfo hints, *res0, *res;\r
- int error;\r
- const char *addr;\r
- static char hostbuf[8*1024];\r
-\r
- _DIAGASSERT(name != NULL);\r
- _DIAGASSERT(pai != NULL);\r
-\r
- if (!*hostf && ( NULL == (*hostf = fopen(_PATH_HOSTS, "r" ))))\r
- return (NULL);\r
- again:\r
- if ( NULL == (p = fgets(hostbuf, sizeof hostbuf, *hostf)))\r
- return (NULL);\r
- if (*p == '#')\r
- goto again;\r
- if ( NULL == (cp = strpbrk(p, "#\n")))\r
- goto again;\r
- *cp = '\0';\r
- if ( NULL == (cp = strpbrk(p, " \t")))\r
- goto again;\r
- *cp++ = '\0';\r
- addr = p;\r
- /* if this is not something we're looking for, skip it. */\r
- cname = NULL;\r
- while (cp && *cp) {\r
- if (*cp == ' ' || *cp == '\t') {\r
- cp++;\r
- continue;\r
- }\r
- if (!cname)\r
- cname = cp;\r
- tname = cp;\r
- if ((cp = strpbrk(cp, " \t")) != NULL)\r
- *cp++ = '\0';\r
- if (strcasecmp(name, tname) == 0)\r
- goto found;\r
- }\r
- goto again;\r
-\r
-found:\r
- hints = *pai;\r
- hints.ai_flags = AI_NUMERICHOST;\r
- error = getaddrinfo(addr, NULL, &hints, &res0);\r
- if (error)\r
- goto again;\r
- for (res = res0; res; res = res->ai_next) {\r
- /* cover it up */\r
- res->ai_flags = pai->ai_flags;\r
-\r
- if (pai->ai_flags & AI_CANONNAME) {\r
- if (get_canonname(pai, res, cname) != 0) {\r
- freeaddrinfo(res0);\r
- goto again;\r
- }\r
- }\r
- }\r
- return res0;\r
-}\r
-\r
-/*ARGSUSED*/\r
-static int\r
-_files_getaddrinfo(void *rv, void *cb_data, va_list ap)\r
-{\r
- const char *name;\r
- const struct addrinfo *pai;\r
- struct addrinfo sentinel, *cur;\r
- struct addrinfo *p;\r
-#ifndef _REENTRANT\r
- static\r
-#endif\r
- FILE *hostf = NULL;\r
-\r
- name = va_arg(ap, char *);\r
- pai = va_arg(ap, const struct addrinfo *);\r
-\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
-\r
- _sethtent(&hostf);\r
- while ((p = _gethtent(&hostf, name, pai)) != NULL) {\r
- cur->ai_next = p;\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
- _endhtent(&hostf);\r
-\r
- *((struct addrinfo **)rv) = sentinel.ai_next;\r
- if (sentinel.ai_next == NULL)\r
- return NS_NOTFOUND;\r
- return NS_SUCCESS;\r
-}\r
-\r
-#ifdef YP\r
-/*ARGSUSED*/\r
-static struct addrinfo *\r
-_yphostent(char *line, const struct addrinfo *pai)\r
-{\r
- struct addrinfo sentinel, *cur;\r
- struct addrinfo hints, *res, *res0;\r
- int error;\r
- char *p;\r
- const char *addr, *canonname;\r
- char *nextline;\r
- char *cp;\r
-\r
- _DIAGASSERT(line != NULL);\r
- _DIAGASSERT(pai != NULL);\r
-\r
- p = line;\r
- addr = canonname = NULL;\r
-\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
-\r
-nextline:\r
- /* terminate line */\r
- cp = strchr(p, '\n');\r
- if (cp) {\r
- *cp++ = '\0';\r
- nextline = cp;\r
- } else\r
- nextline = NULL;\r
-\r
- cp = strpbrk(p, " \t");\r
- if (cp == NULL) {\r
- if (canonname == NULL)\r
- return (NULL);\r
- else\r
- goto done;\r
- }\r
- *cp++ = '\0';\r
-\r
- addr = p;\r
-\r
- while (cp && *cp) {\r
- if (*cp == ' ' || *cp == '\t') {\r
- cp++;\r
- continue;\r
- }\r
- if (!canonname)\r
- canonname = cp;\r
- if ((cp = strpbrk(cp, " \t")) != NULL)\r
- *cp++ = '\0';\r
- }\r
-\r
- hints = *pai;\r
- hints.ai_flags = AI_NUMERICHOST;\r
- error = getaddrinfo(addr, NULL, &hints, &res0);\r
- if (error == 0) {\r
- for (res = res0; res; res = res->ai_next) {\r
- /* cover it up */\r
- res->ai_flags = pai->ai_flags;\r
-\r
- if (pai->ai_flags & AI_CANONNAME)\r
- (void)get_canonname(pai, res, canonname);\r
- }\r
- } else\r
- res0 = NULL;\r
- if (res0) {\r
- cur->ai_next = res0;\r
- while (cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
-\r
- if (nextline) {\r
- p = nextline;\r
- goto nextline;\r
- }\r
-\r
-done:\r
- return sentinel.ai_next;\r
-}\r
-\r
-/*ARGSUSED*/\r
-static int\r
-_yp_getaddrinfo(void *rv, void *cb_data, va_list ap)\r
-{\r
- struct addrinfo sentinel, *cur;\r
- struct addrinfo *ai = NULL;\r
- char *ypbuf;\r
- int ypbuflen, r;\r
- const char *name;\r
- const struct addrinfo *pai;\r
- char *ypdomain;\r
-\r
- if (_yp_check(&ypdomain) == 0)\r
- return NS_UNAVAIL;\r
-\r
- name = va_arg(ap, char *);\r
- pai = va_arg(ap, const struct addrinfo *);\r
-\r
- memset(&sentinel, 0, sizeof(sentinel));\r
- cur = &sentinel;\r
-\r
- /* hosts.byname is only for IPv4 (Solaris8) */\r
- if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) {\r
- r = yp_match(ypdomain, "hosts.byname", name,\r
- (int)strlen(name), &ypbuf, &ypbuflen);\r
- if (r == 0) {\r
- struct addrinfo ai4;\r
-\r
- ai4 = *pai;\r
- ai4.ai_family = AF_INET;\r
- ai = _yphostent(ypbuf, &ai4);\r
- if (ai) {\r
- cur->ai_next = ai;\r
- while (cur && cur->ai_next)\r
- cur = cur->ai_next;\r
- }\r
- }\r
- free(ypbuf);\r
- }\r
-\r
- /* ipnodes.byname can hold both IPv4/v6 */\r
- r = yp_match(ypdomain, "ipnodes.byname", name,\r
- (int)strlen(name), &ypbuf, &ypbuflen);\r
- if (r == 0) {\r
- ai = _yphostent(ypbuf, pai);\r
- if (ai)\r
- cur->ai_next = ai;\r
- free(ypbuf);\r
- }\r
-\r
- if (sentinel.ai_next == NULL) {\r
- h_errno = HOST_NOT_FOUND;\r
- return NS_NOTFOUND;\r
- }\r
- *((struct addrinfo **)rv) = sentinel.ai_next;\r
- return NS_SUCCESS;\r
-}\r
-#endif\r
-\r
-/* resolver logic */\r
-\r
-/*\r
- * Formulate a normal query, send, and await answer.\r
- * Returned answer is placed in supplied buffer "answer".\r
- * Perform preliminary check of answer, returning success only\r
- * if no error is indicated and the answer count is nonzero.\r
- * Return the size of the response on success, -1 on error.\r
- * Error number is left in h_errno.\r
- *\r
- * Caller must parse answer and determine whether it answers the question.\r
- */\r
-static int\r
-res_queryN(const char *name, /* domain name */ struct res_target *target,\r
- res_state res)\r
-{\r
- static u_char buf[MAXPACKET];\r
- HEADER *hp;\r
- int n;\r
- struct res_target *t;\r
- int rcode;\r
- int ancount;\r
-\r
- _DIAGASSERT(name != NULL);\r
- /* XXX: target may be NULL??? */\r
-\r
- rcode = NOERROR;\r
- ancount = 0;\r
-\r
- for (t = target; t; t = t->next) {\r
- int class, type;\r
- u_char *answer;\r
- int anslen;\r
-\r
- hp = (HEADER *)(void *)t->answer;\r
- hp->rcode = NOERROR; /* default */\r
-\r
- /* make it easier... */\r
- class = t->qclass;\r
- type = t->qtype;\r
- answer = t->answer;\r
- anslen = t->anslen;\r
-#ifdef DEBUG\r
- if (res->options & RES_DEBUG)\r
- printf(";; res_nquery(%s, %d, %d)\n", name, class, type);\r
-#endif\r
-\r
- n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL,\r
- buf, sizeof(buf));\r
-#ifdef RES_USE_EDNS0\r
- if (n > 0 && (res->options & RES_USE_EDNS0) != 0)\r
- n = res_nopt(res, n, buf, sizeof(buf), anslen);\r
-#endif\r
- if (n <= 0) {\r
-#ifdef DEBUG\r
- if (res->options & RES_DEBUG)\r
- printf(";; res_nquery: mkquery failed\n");\r
-#endif\r
- h_errno = NO_RECOVERY;\r
- return n;\r
- }\r
- n = res_nsend(res, buf, n, answer, anslen);\r
-#if 0\r
- if (n < 0) {\r
-#ifdef DEBUG\r
- if (res->options & RES_DEBUG)\r
- printf(";; res_query: send error\n");\r
-#endif\r
- h_errno = TRY_AGAIN;\r
- return n;\r
- }\r
-#endif\r
-\r
- if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {\r
- rcode = hp->rcode; /* record most recent error */\r
-#ifdef DEBUG\r
- if (res->options & RES_DEBUG)\r
- printf(";; rcode = %u, ancount=%u\n", hp->rcode,\r
- ntohs(hp->ancount));\r
-#endif\r
- continue;\r
- }\r
-\r
- ancount += ntohs(hp->ancount);\r
-\r
- t->n = n;\r
- }\r
-\r
- if (ancount == 0) {\r
- switch (rcode) {\r
- case NXDOMAIN:\r
- h_errno = HOST_NOT_FOUND;\r
- break;\r
- case SERVFAIL:\r
- h_errno = TRY_AGAIN;\r
- break;\r
- case NOERROR:\r
- h_errno = NO_DATA;\r
- break;\r
- case FORMERR:\r
- case NOTIMP:\r
- case REFUSED:\r
- default:\r
- h_errno = NO_RECOVERY;\r
- break;\r
- }\r
- return -1;\r
- }\r
- return ancount;\r
-}\r
-\r
-/*\r
- * Formulate a normal query, send, and retrieve answer in supplied buffer.\r
- * Return the size of the response on success, -1 on error.\r
- * If enabled, implement search rules until answer or unrecoverable failure\r
- * is detected. Error code, if any, is left in h_errno.\r
- */\r
-static int\r
-res_searchN(const char *name, struct res_target *target, res_state res)\r
-{\r
- const char *cp, * const *domain;\r
- HEADER *hp;\r
- u_int dots;\r
- int trailing_dot, ret, saved_herrno;\r
- int got_nodata = 0, got_servfail = 0, tried_as_is = 0;\r
-\r
- _DIAGASSERT(name != NULL);\r
- _DIAGASSERT(target != NULL);\r
-\r
- hp = (HEADER *)(void *)target->answer; /*XXX*/\r
-\r
- errno = 0;\r
- h_errno = HOST_NOT_FOUND; /* default, if we never query */\r
- dots = 0;\r
- for (cp = name; *cp; cp++)\r
- dots += (*cp == '.');\r
- trailing_dot = 0;\r
- if (cp > name && *--cp == '.')\r
- trailing_dot++;\r
-\r
- /*\r
- * if there aren't any dots, it could be a user-level alias\r
- */\r
- if (!dots && (cp = __hostalias(name)) != NULL) {\r
- ret = res_queryN(cp, target, res);\r
- return ret;\r
- }\r
-\r
- /*\r
- * If there are dots in the name already, let's just give it a try\r
- * 'as is'. The threshold can be set with the "ndots" option.\r
- */\r
- saved_herrno = -1;\r
- if (dots >= res->ndots) {\r
- ret = res_querydomainN(name, NULL, target, res);\r
- if (ret > 0)\r
- return (ret);\r
- saved_herrno = h_errno;\r
- tried_as_is++;\r
- }\r
-\r
- /*\r
- * We do at least one level of search if\r
- * - there is no dot and RES_DEFNAME is set, or\r
- * - there is at least one dot, there is no trailing dot,\r
- * and RES_DNSRCH is set.\r
- */\r
- if ((!dots && (res->options & RES_DEFNAMES)) ||\r
- (dots && !trailing_dot && (res->options & RES_DNSRCH))) {\r
- int done = 0;\r
-\r
- for (domain = (const char * const *)res->dnsrch;\r
- *domain && !done;\r
- domain++) {\r
-\r
- ret = res_querydomainN(name, *domain, target, res);\r
- if (ret > 0)\r
- return ret;\r
-\r
- /*\r
- * If no server present, give up.\r
- * If name isn't found in this domain,\r
- * keep trying higher domains in the search list\r
- * (if that's enabled).\r
- * On a NO_DATA error, keep trying, otherwise\r
- * a wildcard entry of another type could keep us\r
- * from finding this entry higher in the domain.\r
- * If we get some other error (negative answer or\r
- * server failure), then stop searching up,\r
- * but try the input name below in case it's\r
- * fully-qualified.\r
- */\r
- if (errno == ECONNREFUSED) {\r
- h_errno = TRY_AGAIN;\r
- return -1;\r
- }\r
-\r
- switch (h_errno) {\r
- case NO_DATA:\r
- got_nodata++;\r
- /* FALLTHROUGH */\r
- case HOST_NOT_FOUND:\r
- /* keep trying */\r
- break;\r
- case TRY_AGAIN:\r
- if (hp->rcode == SERVFAIL) {\r
- /* try next search element, if any */\r
- got_servfail++;\r
- break;\r
- }\r
- /* FALLTHROUGH */\r
- default:\r
- /* anything else implies that we're done */\r
- done++;\r
- }\r
- /*\r
- * if we got here for some reason other than DNSRCH,\r
- * we only wanted one iteration of the loop, so stop.\r
- */\r
- if (!(res->options & RES_DNSRCH))\r
- done++;\r
- }\r
- }\r
-\r
- /*\r
- * if we have not already tried the name "as is", do that now.\r
- * note that we do this regardless of how many dots were in the\r
- * name or whether it ends with a dot.\r
- */\r
- if (!tried_as_is) {\r
- ret = res_querydomainN(name, NULL, target, res);\r
- if (ret > 0)\r
- return ret;\r
- }\r
-\r
- /*\r
- * if we got here, we didn't satisfy the search.\r
- * if we did an initial full query, return that query's h_errno\r
- * (note that we wouldn't be here if that query had succeeded).\r
- * else if we ever got a nodata, send that back as the reason.\r
- * else send back meaningless h_errno, that being the one from\r
- * the last DNSRCH we did.\r
- */\r
- if (saved_herrno != -1)\r
- h_errno = saved_herrno;\r
- else if (got_nodata)\r
- h_errno = NO_DATA;\r
- else if (got_servfail)\r
- h_errno = TRY_AGAIN;\r
- return -1;\r
-}\r
-\r
-/*\r
- * Perform a call on res_query on the concatenation of name and domain,\r
- * removing a trailing dot from name if domain is NULL.\r
- */\r
-static int\r
-res_querydomainN(const char *name, const char *domain,\r
- struct res_target *target, res_state res)\r
-{\r
- char nbuf[MAXDNAME];\r
- const char *longname = nbuf;\r
- size_t n, d;\r
-\r
- _DIAGASSERT(name != NULL);\r
- /* XXX: target may be NULL??? */\r
-\r
-#ifdef DEBUG\r
- if (res->options & RES_DEBUG)\r
- printf(";; res_querydomain(%s, %s)\n",\r
- name, domain?domain:"<Nil>");\r
-#endif\r
- if (domain == NULL) {\r
- /*\r
- * Check for trailing '.';\r
- * copy without '.' if present.\r
- */\r
- n = strlen(name);\r
- if (n + 1 > sizeof(nbuf)) {\r
- h_errno = NO_RECOVERY;\r
- return -1;\r
- }\r
- if (n > 0 && name[--n] == '.') {\r
- strncpy(nbuf, name, n);\r
- nbuf[n] = '\0';\r
- } else\r
- longname = name;\r
- } else {\r
- n = strlen(name);\r
- d = strlen(domain);\r
- if (n + 1 + d + 1 > sizeof(nbuf)) {\r
- h_errno = NO_RECOVERY;\r
- return -1;\r
- }\r
- snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);\r
- }\r
- return res_queryN(longname, target, res);\r
-}\r