PageRenderTime 67ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/external/source/meterpreter/source/bionic/libc/netbsd/net/getaddrinfo.c

https://bitbucket.org/technopunk2099/metasploit-framework
C | 2160 lines | 1563 code | 233 blank | 364 comment | 525 complexity | 90d935b7ef68a5100b18c2c7434392f8 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file