/contrib/bind9/lib/irs/getaddrinfo.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1295 lines · 930 code · 131 blank · 234 comment · 333 complexity · 7a55969e5562264c1c13f2ee3c517d7a MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id: getaddrinfo.c,v 1.3 2009/09/02 23:48:02 tbox Exp $ */
  17. /*! \file */
  18. /**
  19. * getaddrinfo() is used to get a list of IP addresses and port
  20. * numbers for host hostname and service servname as defined in RFC3493.
  21. * hostname and servname are pointers to null-terminated strings
  22. * or NULL. hostname is either a host name or a numeric host address
  23. * string: a dotted decimal IPv4 address or an IPv6 address. servname is
  24. * either a decimal port number or a service name as listed in
  25. * /etc/services.
  26. *
  27. * If the operating system does not provide a struct addrinfo, the
  28. * following structure is used:
  29. *
  30. * \code
  31. * struct addrinfo {
  32. * int ai_flags; // AI_PASSIVE, AI_CANONNAME
  33. * int ai_family; // PF_xxx
  34. * int ai_socktype; // SOCK_xxx
  35. * int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
  36. * size_t ai_addrlen; // length of ai_addr
  37. * char *ai_canonname; // canonical name for hostname
  38. * struct sockaddr *ai_addr; // binary address
  39. * struct addrinfo *ai_next; // next structure in linked list
  40. * };
  41. * \endcode
  42. *
  43. *
  44. * hints is an optional pointer to a struct addrinfo. This structure can
  45. * be used to provide hints concerning the type of socket that the caller
  46. * supports or wishes to use. The caller can supply the following
  47. * structure elements in *hints:
  48. *
  49. * <ul>
  50. * <li>ai_family:
  51. * The protocol family that should be used. When ai_family is set
  52. * to PF_UNSPEC, it means the caller will accept any protocol
  53. * family supported by the operating system.</li>
  54. *
  55. * <li>ai_socktype:
  56. * denotes the type of socket -- SOCK_STREAM, SOCK_DGRAM or
  57. * SOCK_RAW -- that is wanted. When ai_socktype is zero the caller
  58. * will accept any socket type.</li>
  59. *
  60. * <li>ai_protocol:
  61. * indicates which transport protocol is wanted: IPPROTO_UDP or
  62. * IPPROTO_TCP. If ai_protocol is zero the caller will accept any
  63. * protocol.</li>
  64. *
  65. * <li>ai_flags:
  66. * Flag bits. If the AI_CANONNAME bit is set, a successful call to
  67. * getaddrinfo() will return a null-terminated string
  68. * containing the canonical name of the specified hostname in
  69. * ai_canonname of the first addrinfo structure returned. Setting
  70. * the AI_PASSIVE bit indicates that the returned socket address
  71. * structure is intended for used in a call to bind(2). In this
  72. * case, if the hostname argument is a NULL pointer, then the IP
  73. * address portion of the socket address structure will be set to
  74. * INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6
  75. * address.<br /><br />
  76. *
  77. * When ai_flags does not set the AI_PASSIVE bit, the returned
  78. * socket address structure will be ready for use in a call to
  79. * connect(2) for a connection-oriented protocol or connect(2),
  80. * sendto(2), or sendmsg(2) if a connectionless protocol was
  81. * chosen. The IP address portion of the socket address structure
  82. * will be set to the loopback address if hostname is a NULL
  83. * pointer and AI_PASSIVE is not set in ai_flags.<br /><br />
  84. *
  85. * If ai_flags is set to AI_NUMERICHOST it indicates that hostname
  86. * should be treated as a numeric string defining an IPv4 or IPv6
  87. * address and no name resolution should be attempted.
  88. * </li></ul>
  89. *
  90. * All other elements of the struct addrinfo passed via hints must be
  91. * zero.
  92. *
  93. * A hints of NULL is treated as if the caller provided a struct addrinfo
  94. * initialized to zero with ai_familyset to PF_UNSPEC.
  95. *
  96. * After a successful call to getaddrinfo(), *res is a pointer to a
  97. * linked list of one or more addrinfo structures. Each struct addrinfo
  98. * in this list cn be processed by following the ai_next pointer, until a
  99. * NULL pointer is encountered. The three members ai_family, ai_socktype,
  100. * and ai_protocol in each returned addrinfo structure contain the
  101. * corresponding arguments for a call to socket(2). For each addrinfo
  102. * structure in the list, the ai_addr member points to a filled-in socket
  103. * address structure of length ai_addrlen.
  104. *
  105. * All of the information returned by getaddrinfo() is dynamically
  106. * allocated: the addrinfo structures, and the socket address structures
  107. * and canonical host name strings pointed to by the addrinfostructures.
  108. * Memory allocated for the dynamically allocated structures created by a
  109. * successful call to getaddrinfo() is released by freeaddrinfo().
  110. * ai is a pointer to a struct addrinfo created by a call to getaddrinfo().
  111. *
  112. * \section irsreturn RETURN VALUES
  113. *
  114. * getaddrinfo() returns zero on success or one of the error codes
  115. * listed in gai_strerror() if an error occurs. If both hostname and
  116. * servname are NULL getaddrinfo() returns #EAI_NONAME.
  117. *
  118. * \section irssee SEE ALSO
  119. *
  120. * getaddrinfo(), freeaddrinfo(),
  121. * gai_strerror(), RFC3493, getservbyname(3), connect(2),
  122. * sendto(2), sendmsg(2), socket(2).
  123. */
  124. #include <config.h>
  125. #include <stdlib.h>
  126. #include <string.h>
  127. #include <errno.h>
  128. #include <isc/app.h>
  129. #include <isc/buffer.h>
  130. #include <isc/lib.h>
  131. #include <isc/mem.h>
  132. #include <isc/sockaddr.h>
  133. #include <isc/util.h>
  134. #include <dns/client.h>
  135. #include <dns/fixedname.h>
  136. #include <dns/name.h>
  137. #include <dns/rdata.h>
  138. #include <dns/rdataset.h>
  139. #include <dns/rdatastruct.h>
  140. #include <dns/rdatatype.h>
  141. #include <dns/result.h>
  142. #include <irs/context.h>
  143. #include <irs/netdb.h>
  144. #include <irs/resconf.h>
  145. #define SA(addr) ((struct sockaddr *)(addr))
  146. #define SIN(addr) ((struct sockaddr_in *)(addr))
  147. #define SIN6(addr) ((struct sockaddr_in6 *)(addr))
  148. #define SLOCAL(addr) ((struct sockaddr_un *)(addr))
  149. /*! \struct addrinfo
  150. */
  151. static struct addrinfo
  152. *ai_concat(struct addrinfo *ai1, struct addrinfo *ai2),
  153. *ai_reverse(struct addrinfo *oai),
  154. *ai_clone(struct addrinfo *oai, int family),
  155. *ai_alloc(int family, int addrlen);
  156. #ifdef AF_LOCAL
  157. static int get_local(const char *name, int socktype, struct addrinfo **res);
  158. #endif
  159. static int
  160. resolve_name(int family, const char *hostname, int flags,
  161. struct addrinfo **aip, int socktype, int port);
  162. static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
  163. int socktype, int port);
  164. static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
  165. int socktype, int port);
  166. static void set_order(int, int (**)(const char *, int, struct addrinfo **,
  167. int, int));
  168. #define FOUND_IPV4 0x1
  169. #define FOUND_IPV6 0x2
  170. #define FOUND_MAX 2
  171. #define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
  172. /*%
  173. * Get a list of IP addresses and port numbers for host hostname and
  174. * service servname.
  175. */
  176. int
  177. getaddrinfo(const char *hostname, const char *servname,
  178. const struct addrinfo *hints, struct addrinfo **res)
  179. {
  180. struct servent *sp;
  181. const char *proto;
  182. int family, socktype, flags, protocol;
  183. struct addrinfo *ai, *ai_list;
  184. int err = 0;
  185. int port, i;
  186. int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
  187. int, int);
  188. if (hostname == NULL && servname == NULL)
  189. return (EAI_NONAME);
  190. proto = NULL;
  191. if (hints != NULL) {
  192. if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
  193. return (EAI_BADFLAGS);
  194. if (hints->ai_addrlen || hints->ai_canonname ||
  195. hints->ai_addr || hints->ai_next) {
  196. errno = EINVAL;
  197. return (EAI_SYSTEM);
  198. }
  199. family = hints->ai_family;
  200. socktype = hints->ai_socktype;
  201. protocol = hints->ai_protocol;
  202. flags = hints->ai_flags;
  203. switch (family) {
  204. case AF_UNSPEC:
  205. switch (hints->ai_socktype) {
  206. case SOCK_STREAM:
  207. proto = "tcp";
  208. break;
  209. case SOCK_DGRAM:
  210. proto = "udp";
  211. break;
  212. }
  213. break;
  214. case AF_INET:
  215. case AF_INET6:
  216. switch (hints->ai_socktype) {
  217. case 0:
  218. break;
  219. case SOCK_STREAM:
  220. proto = "tcp";
  221. break;
  222. case SOCK_DGRAM:
  223. proto = "udp";
  224. break;
  225. case SOCK_RAW:
  226. break;
  227. default:
  228. return (EAI_SOCKTYPE);
  229. }
  230. break;
  231. #ifdef AF_LOCAL
  232. case AF_LOCAL:
  233. switch (hints->ai_socktype) {
  234. case 0:
  235. break;
  236. case SOCK_STREAM:
  237. break;
  238. case SOCK_DGRAM:
  239. break;
  240. default:
  241. return (EAI_SOCKTYPE);
  242. }
  243. break;
  244. #endif
  245. default:
  246. return (EAI_FAMILY);
  247. }
  248. } else {
  249. protocol = 0;
  250. family = 0;
  251. socktype = 0;
  252. flags = 0;
  253. }
  254. #ifdef AF_LOCAL
  255. /*!
  256. * First, deal with AF_LOCAL. If the family was not set,
  257. * then assume AF_LOCAL if the first character of the
  258. * hostname/servname is '/'.
  259. */
  260. if (hostname != NULL &&
  261. (family == AF_LOCAL || (family == 0 && *hostname == '/')))
  262. return (get_local(hostname, socktype, res));
  263. if (servname != NULL &&
  264. (family == AF_LOCAL || (family == 0 && *servname == '/')))
  265. return (get_local(servname, socktype, res));
  266. #endif
  267. /*
  268. * Ok, only AF_INET and AF_INET6 left.
  269. */
  270. ai_list = NULL;
  271. /*
  272. * First, look up the service name (port) if it was
  273. * requested. If the socket type wasn't specified, then
  274. * try and figure it out.
  275. */
  276. if (servname != NULL) {
  277. char *e;
  278. port = strtol(servname, &e, 10);
  279. if (*e == '\0') {
  280. if (socktype == 0)
  281. return (EAI_SOCKTYPE);
  282. if (port < 0 || port > 65535)
  283. return (EAI_SERVICE);
  284. port = htons((unsigned short) port);
  285. } else {
  286. sp = getservbyname(servname, proto);
  287. if (sp == NULL)
  288. return (EAI_SERVICE);
  289. port = sp->s_port;
  290. if (socktype == 0) {
  291. if (strcmp(sp->s_proto, "tcp") == 0)
  292. socktype = SOCK_STREAM;
  293. else if (strcmp(sp->s_proto, "udp") == 0)
  294. socktype = SOCK_DGRAM;
  295. }
  296. }
  297. } else
  298. port = 0;
  299. /*
  300. * Next, deal with just a service name, and no hostname.
  301. * (we verified that one of them was non-null up above).
  302. */
  303. if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
  304. if (family == AF_INET || family == 0) {
  305. ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
  306. if (ai == NULL)
  307. return (EAI_MEMORY);
  308. ai->ai_socktype = socktype;
  309. ai->ai_protocol = protocol;
  310. SIN(ai->ai_addr)->sin_port = port;
  311. ai->ai_next = ai_list;
  312. ai_list = ai;
  313. }
  314. if (family == AF_INET6 || family == 0) {
  315. ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
  316. if (ai == NULL) {
  317. freeaddrinfo(ai_list);
  318. return (EAI_MEMORY);
  319. }
  320. ai->ai_socktype = socktype;
  321. ai->ai_protocol = protocol;
  322. SIN6(ai->ai_addr)->sin6_port = port;
  323. ai->ai_next = ai_list;
  324. ai_list = ai;
  325. }
  326. *res = ai_list;
  327. return (0);
  328. }
  329. /*
  330. * If the family isn't specified or AI_NUMERICHOST specified, check
  331. * first to see if it is a numeric address.
  332. * Though the gethostbyname2() routine will recognize numeric addresses,
  333. * it will only recognize the format that it is being called for. Thus,
  334. * a numeric AF_INET address will be treated by the AF_INET6 call as
  335. * a domain name, and vice versa. Checking for both numerics here
  336. * avoids that.
  337. */
  338. if (hostname != NULL &&
  339. (family == 0 || (flags & AI_NUMERICHOST) != 0)) {
  340. char abuf[sizeof(struct in6_addr)];
  341. char nbuf[NI_MAXHOST];
  342. int addrsize, addroff;
  343. #ifdef IRS_HAVE_SIN6_SCOPE_ID
  344. char *p, *ep;
  345. char ntmp[NI_MAXHOST];
  346. isc_uint32_t scopeid;
  347. #endif
  348. #ifdef IRS_HAVE_SIN6_SCOPE_ID
  349. /*
  350. * Scope identifier portion.
  351. */
  352. ntmp[0] = '\0';
  353. if (strchr(hostname, '%') != NULL) {
  354. strncpy(ntmp, hostname, sizeof(ntmp) - 1);
  355. ntmp[sizeof(ntmp) - 1] = '\0';
  356. p = strchr(ntmp, '%');
  357. ep = NULL;
  358. /*
  359. * Vendors may want to support non-numeric
  360. * scopeid around here.
  361. */
  362. if (p != NULL)
  363. scopeid = (isc_uint32_t)strtoul(p + 1,
  364. &ep, 10);
  365. if (p != NULL && ep != NULL && ep[0] == '\0')
  366. *p = '\0';
  367. else {
  368. ntmp[0] = '\0';
  369. scopeid = 0;
  370. }
  371. } else
  372. scopeid = 0;
  373. #endif
  374. if (inet_pton(AF_INET, hostname, (struct in_addr *)abuf)
  375. == 1) {
  376. if (family == AF_INET6) {
  377. /*
  378. * Convert to a V4 mapped address.
  379. */
  380. struct in6_addr *a6 = (struct in6_addr *)abuf;
  381. memcpy(&a6->s6_addr[12], &a6->s6_addr[0], 4);
  382. memset(&a6->s6_addr[10], 0xff, 2);
  383. memset(&a6->s6_addr[0], 0, 10);
  384. goto inet6_addr;
  385. }
  386. addrsize = sizeof(struct in_addr);
  387. addroff = (char *)(&SIN(0)->sin_addr) - (char *)0;
  388. family = AF_INET;
  389. goto common;
  390. #ifdef IRS_HAVE_SIN6_SCOPE_ID
  391. } else if (ntmp[0] != '\0' &&
  392. inet_pton(AF_INET6, ntmp, abuf) == 1) {
  393. if (family && family != AF_INET6)
  394. return (EAI_NONAME);
  395. addrsize = sizeof(struct in6_addr);
  396. addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
  397. family = AF_INET6;
  398. goto common;
  399. #endif
  400. } else if (inet_pton(AF_INET6, hostname, abuf) == 1) {
  401. if (family != 0 && family != AF_INET6)
  402. return (EAI_NONAME);
  403. inet6_addr:
  404. addrsize = sizeof(struct in6_addr);
  405. addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
  406. family = AF_INET6;
  407. common:
  408. ai = ai_alloc(family,
  409. ((family == AF_INET6) ?
  410. sizeof(struct sockaddr_in6) :
  411. sizeof(struct sockaddr_in)));
  412. if (ai == NULL)
  413. return (EAI_MEMORY);
  414. ai_list = ai;
  415. ai->ai_socktype = socktype;
  416. SIN(ai->ai_addr)->sin_port = port;
  417. memcpy((char *)ai->ai_addr + addroff, abuf, addrsize);
  418. if ((flags & AI_CANONNAME) != 0) {
  419. #ifdef IRS_HAVE_SIN6_SCOPE_ID
  420. if (ai->ai_family == AF_INET6)
  421. SIN6(ai->ai_addr)->sin6_scope_id =
  422. scopeid;
  423. #endif
  424. if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
  425. nbuf, sizeof(nbuf), NULL, 0,
  426. NI_NUMERICHOST) == 0) {
  427. ai->ai_canonname = strdup(nbuf);
  428. if (ai->ai_canonname == NULL) {
  429. freeaddrinfo(ai);
  430. return (EAI_MEMORY);
  431. }
  432. } else {
  433. /* XXX raise error? */
  434. ai->ai_canonname = NULL;
  435. }
  436. }
  437. goto done;
  438. } else if ((flags & AI_NUMERICHOST) != 0) {
  439. return (EAI_NONAME);
  440. }
  441. }
  442. if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
  443. set_order(family, net_order);
  444. for (i = 0; i < FOUND_MAX; i++) {
  445. if (net_order[i] == NULL)
  446. break;
  447. err = (net_order[i])(hostname, flags, &ai_list,
  448. socktype, port);
  449. if (err != 0) {
  450. if (ai_list != NULL)
  451. freeaddrinfo(ai_list);
  452. break;
  453. }
  454. }
  455. } else
  456. err = resolve_name(family, hostname, flags, &ai_list,
  457. socktype, port);
  458. if (ai_list == NULL) {
  459. if (err == 0)
  460. err = EAI_NONAME;
  461. return (err);
  462. }
  463. done:
  464. ai_list = ai_reverse(ai_list);
  465. *res = ai_list;
  466. return (0);
  467. }
  468. typedef struct gai_restrans {
  469. dns_clientrestrans_t *xid;
  470. isc_boolean_t is_inprogress;
  471. int error;
  472. struct addrinfo ai_sentinel;
  473. struct gai_resstate *resstate;
  474. } gai_restrans_t;
  475. typedef struct gai_resstate {
  476. isc_mem_t *mctx;
  477. struct gai_statehead *head;
  478. dns_fixedname_t fixedname;
  479. dns_name_t *qname;
  480. gai_restrans_t *trans4;
  481. gai_restrans_t *trans6;
  482. ISC_LINK(struct gai_resstate) link;
  483. } gai_resstate_t;
  484. typedef struct gai_statehead {
  485. int ai_family;
  486. int ai_flags;
  487. int ai_socktype;
  488. int ai_port;
  489. isc_appctx_t *actx;
  490. dns_client_t *dnsclient;
  491. ISC_LIST(struct gai_resstate) resstates;
  492. unsigned int activestates;
  493. } gai_statehead_t;
  494. static isc_result_t
  495. make_resstate(isc_mem_t *mctx, gai_statehead_t *head, const char *hostname,
  496. const char *domain, gai_resstate_t **statep)
  497. {
  498. isc_result_t result;
  499. gai_resstate_t *state;
  500. dns_fixedname_t fixeddomain;
  501. dns_name_t *qdomain;
  502. size_t namelen;
  503. isc_buffer_t b;
  504. isc_boolean_t need_v4 = ISC_FALSE;
  505. isc_boolean_t need_v6 = ISC_FALSE;
  506. state = isc_mem_get(mctx, sizeof(*state));
  507. if (state == NULL)
  508. return (ISC_R_NOMEMORY);
  509. /* Construct base domain name */
  510. namelen = strlen(domain);
  511. isc_buffer_init(&b, domain, namelen);
  512. isc_buffer_add(&b, namelen);
  513. dns_fixedname_init(&fixeddomain);
  514. qdomain = dns_fixedname_name(&fixeddomain);
  515. result = dns_name_fromtext(qdomain, &b, dns_rootname, 0, NULL);
  516. if (result != ISC_R_SUCCESS) {
  517. isc_mem_put(mctx, state, sizeof(*state));
  518. return (result);
  519. }
  520. /* Construct query name */
  521. namelen = strlen(hostname);
  522. isc_buffer_init(&b, hostname, namelen);
  523. isc_buffer_add(&b, namelen);
  524. dns_fixedname_init(&state->fixedname);
  525. state->qname = dns_fixedname_name(&state->fixedname);
  526. result = dns_name_fromtext(state->qname, &b, qdomain, 0, NULL);
  527. if (result != ISC_R_SUCCESS) {
  528. isc_mem_put(mctx, state, sizeof(*state));
  529. return (result);
  530. }
  531. if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET)
  532. need_v4 = ISC_TRUE;
  533. if (head->ai_family == AF_UNSPEC || head->ai_family == AF_INET6)
  534. need_v6 = ISC_TRUE;
  535. state->trans6 = NULL;
  536. state->trans4 = NULL;
  537. if (need_v4) {
  538. state->trans4 = isc_mem_get(mctx, sizeof(gai_restrans_t));
  539. if (state->trans4 == NULL) {
  540. isc_mem_put(mctx, state, sizeof(*state));
  541. return (ISC_R_NOMEMORY);
  542. }
  543. state->trans4->error = 0;
  544. state->trans4->xid = NULL;
  545. state->trans4->resstate = state;
  546. state->trans4->is_inprogress = ISC_TRUE;
  547. state->trans4->ai_sentinel.ai_next = NULL;
  548. }
  549. if (need_v6) {
  550. state->trans6 = isc_mem_get(mctx, sizeof(gai_restrans_t));
  551. if (state->trans6 == NULL) {
  552. if (state->trans4 != NULL)
  553. isc_mem_put(mctx, state->trans4,
  554. sizeof(*state->trans4));
  555. isc_mem_put(mctx, state, sizeof(*state));
  556. return (ISC_R_NOMEMORY);
  557. }
  558. state->trans6->error = 0;
  559. state->trans6->xid = NULL;
  560. state->trans6->resstate = state;
  561. state->trans6->is_inprogress = ISC_TRUE;
  562. state->trans6->ai_sentinel.ai_next = NULL;
  563. }
  564. state->mctx = mctx;
  565. state->head = head;
  566. ISC_LINK_INIT(state, link);
  567. *statep = state;
  568. return (ISC_R_SUCCESS);
  569. }
  570. static isc_result_t
  571. make_resstates(isc_mem_t *mctx, const char *hostname, gai_statehead_t *head,
  572. irs_resconf_t *resconf)
  573. {
  574. isc_result_t result;
  575. irs_resconf_searchlist_t *searchlist;
  576. irs_resconf_search_t *searchent;
  577. gai_resstate_t *resstate, *resstate0;
  578. resstate0 = NULL;
  579. result = make_resstate(mctx, head, hostname, ".", &resstate0);
  580. if (result != ISC_R_SUCCESS)
  581. return (result);
  582. searchlist = irs_resconf_getsearchlist(resconf);
  583. for (searchent = ISC_LIST_HEAD(*searchlist); searchent != NULL;
  584. searchent = ISC_LIST_NEXT(searchent, link)) {
  585. resstate = NULL;
  586. result = make_resstate(mctx, head, hostname,
  587. (const char *)searchent->domain,
  588. &resstate);
  589. if (result != ISC_R_SUCCESS)
  590. break;
  591. ISC_LIST_APPEND(head->resstates, resstate, link);
  592. head->activestates++;
  593. }
  594. /*
  595. * Insert the original hostname either at the head or the tail of the
  596. * state list, depending on the number of labels contained in the
  597. * original name and the 'ndots' configuration parameter.
  598. */
  599. if (dns_name_countlabels(resstate0->qname) >
  600. irs_resconf_getndots(resconf) + 1) {
  601. ISC_LIST_PREPEND(head->resstates, resstate0, link);
  602. } else
  603. ISC_LIST_APPEND(head->resstates, resstate0, link);
  604. head->activestates++;
  605. if (result != ISC_R_SUCCESS) {
  606. while ((resstate = ISC_LIST_HEAD(head->resstates)) != NULL) {
  607. ISC_LIST_UNLINK(head->resstates, resstate, link);
  608. if (resstate->trans4 != NULL) {
  609. isc_mem_put(mctx, resstate->trans4,
  610. sizeof(*resstate->trans4));
  611. }
  612. if (resstate->trans6 != NULL) {
  613. isc_mem_put(mctx, resstate->trans6,
  614. sizeof(*resstate->trans6));
  615. }
  616. isc_mem_put(mctx, resstate, sizeof(*resstate));
  617. }
  618. }
  619. return (result);
  620. }
  621. static void
  622. process_answer(isc_task_t *task, isc_event_t *event) {
  623. int error = 0, family;
  624. gai_restrans_t *trans = event->ev_arg;
  625. gai_resstate_t *resstate;
  626. dns_clientresevent_t *rev = (dns_clientresevent_t *)event;
  627. dns_rdatatype_t qtype;
  628. dns_name_t *name;
  629. REQUIRE(trans != NULL);
  630. resstate = trans->resstate;
  631. REQUIRE(resstate != NULL);
  632. REQUIRE(task != NULL);
  633. if (trans == resstate->trans4) {
  634. family = AF_INET;
  635. qtype = dns_rdatatype_a;
  636. } else {
  637. INSIST(trans == resstate->trans6);
  638. family = AF_INET6;
  639. qtype = dns_rdatatype_aaaa;
  640. }
  641. INSIST(trans->is_inprogress);
  642. trans->is_inprogress = ISC_FALSE;
  643. switch (rev->result) {
  644. case ISC_R_SUCCESS:
  645. case DNS_R_NCACHENXDOMAIN: /* treat this as a fatal error? */
  646. case DNS_R_NCACHENXRRSET:
  647. break;
  648. default:
  649. switch (rev->vresult) {
  650. case DNS_R_SIGINVALID:
  651. case DNS_R_SIGEXPIRED:
  652. case DNS_R_SIGFUTURE:
  653. case DNS_R_KEYUNAUTHORIZED:
  654. case DNS_R_MUSTBESECURE:
  655. case DNS_R_COVERINGNSEC:
  656. case DNS_R_NOTAUTHORITATIVE:
  657. case DNS_R_NOVALIDKEY:
  658. case DNS_R_NOVALIDDS:
  659. case DNS_R_NOVALIDSIG:
  660. error = EAI_INSECUREDATA;
  661. break;
  662. default:
  663. error = EAI_FAIL;
  664. }
  665. goto done;
  666. }
  667. /* Parse the response and construct the addrinfo chain */
  668. for (name = ISC_LIST_HEAD(rev->answerlist); name != NULL;
  669. name = ISC_LIST_NEXT(name, link)) {
  670. isc_result_t result;
  671. dns_rdataset_t *rdataset;
  672. isc_buffer_t b;
  673. isc_region_t r;
  674. char t[1024];
  675. for (rdataset = ISC_LIST_HEAD(name->list);
  676. rdataset != NULL;
  677. rdataset = ISC_LIST_NEXT(rdataset, link)) {
  678. if (!dns_rdataset_isassociated(rdataset))
  679. continue;
  680. if (rdataset->type != qtype)
  681. continue;
  682. if ((resstate->head->ai_flags & AI_CANONNAME) != 0) {
  683. isc_buffer_init(&b, t, sizeof(t));
  684. result = dns_name_totext(name, ISC_TRUE, &b);
  685. if (result != ISC_R_SUCCESS) {
  686. error = EAI_FAIL;
  687. goto done;
  688. }
  689. isc_buffer_putuint8(&b, '\0');
  690. isc_buffer_usedregion(&b, &r);
  691. }
  692. for (result = dns_rdataset_first(rdataset);
  693. result == ISC_R_SUCCESS;
  694. result = dns_rdataset_next(rdataset)) {
  695. struct addrinfo *ai;
  696. dns_rdata_t rdata;
  697. dns_rdata_in_a_t rdata_a;
  698. dns_rdata_in_aaaa_t rdata_aaaa;
  699. ai = ai_alloc(family,
  700. ((family == AF_INET6) ?
  701. sizeof(struct sockaddr_in6) :
  702. sizeof(struct sockaddr_in)));
  703. if (ai == NULL) {
  704. error = EAI_MEMORY;
  705. goto done;
  706. }
  707. ai->ai_socktype = resstate->head->ai_socktype;
  708. ai->ai_next = trans->ai_sentinel.ai_next;
  709. trans->ai_sentinel.ai_next = ai;
  710. /*
  711. * Set AF-specific parameters
  712. * (IPv4/v6 address/port)
  713. */
  714. dns_rdata_init(&rdata);
  715. switch (family) {
  716. case AF_INET:
  717. dns_rdataset_current(rdataset, &rdata);
  718. dns_rdata_tostruct(&rdata, &rdata_a,
  719. NULL);
  720. SIN(ai->ai_addr)->sin_port =
  721. resstate->head->ai_port;
  722. memcpy(&SIN(ai->ai_addr)->sin_addr,
  723. &rdata_a.in_addr, 4);
  724. dns_rdata_freestruct(&rdata_a);
  725. break;
  726. case AF_INET6:
  727. dns_rdataset_current(rdataset, &rdata);
  728. dns_rdata_tostruct(&rdata, &rdata_aaaa,
  729. NULL);
  730. SIN6(ai->ai_addr)->sin6_port =
  731. resstate->head->ai_port;
  732. memcpy(&SIN6(ai->ai_addr)->sin6_addr,
  733. &rdata_aaaa.in6_addr, 16);
  734. dns_rdata_freestruct(&rdata_aaaa);
  735. break;
  736. }
  737. if ((resstate->head->ai_flags & AI_CANONNAME)
  738. != 0) {
  739. ai->ai_canonname =
  740. strdup((const char *)r.base);
  741. if (ai->ai_canonname == NULL) {
  742. error = EAI_MEMORY;
  743. goto done;
  744. }
  745. }
  746. }
  747. }
  748. }
  749. done:
  750. dns_client_freeresanswer(resstate->head->dnsclient, &rev->answerlist);
  751. dns_client_destroyrestrans(&trans->xid);
  752. isc_event_free(&event);
  753. /* Make sure that error == 0 iff we have a non-empty list */
  754. if (error == 0) {
  755. if (trans->ai_sentinel.ai_next == NULL)
  756. error = EAI_NONAME;
  757. } else {
  758. if (trans->ai_sentinel.ai_next != NULL) {
  759. freeaddrinfo(trans->ai_sentinel.ai_next);
  760. trans->ai_sentinel.ai_next = NULL;
  761. }
  762. }
  763. trans->error = error;
  764. /* Check whether we are done */
  765. if ((resstate->trans4 == NULL || !resstate->trans4->is_inprogress) &&
  766. (resstate->trans6 == NULL || !resstate->trans6->is_inprogress)) {
  767. /*
  768. * We're done for this state. If there is no other outstanding
  769. * state, we can exit.
  770. */
  771. resstate->head->activestates--;
  772. if (resstate->head->activestates == 0) {
  773. isc_app_ctxsuspend(resstate->head->actx);
  774. return;
  775. }
  776. /*
  777. * There are outstanding states, but if we are at the head
  778. * of the state list (i.e., at the highest search priority)
  779. * and have any answer, we can stop now by canceling the
  780. * others.
  781. */
  782. if (resstate == ISC_LIST_HEAD(resstate->head->resstates)) {
  783. if ((resstate->trans4 != NULL &&
  784. resstate->trans4->ai_sentinel.ai_next != NULL) ||
  785. (resstate->trans6 != NULL &&
  786. resstate->trans6->ai_sentinel.ai_next != NULL)) {
  787. gai_resstate_t *rest;
  788. for (rest = ISC_LIST_NEXT(resstate, link);
  789. rest != NULL;
  790. rest = ISC_LIST_NEXT(rest, link)) {
  791. if (rest->trans4 != NULL &&
  792. rest->trans4->xid != NULL)
  793. dns_client_cancelresolve(
  794. rest->trans4->xid);
  795. if (rest->trans6 != NULL &&
  796. rest->trans6->xid != NULL)
  797. dns_client_cancelresolve(
  798. rest->trans6->xid);
  799. }
  800. } else {
  801. /*
  802. * This search fails, so we move to the tail
  803. * of the list so that the next entry will
  804. * have the highest priority.
  805. */
  806. ISC_LIST_UNLINK(resstate->head->resstates,
  807. resstate, link);
  808. ISC_LIST_APPEND(resstate->head->resstates,
  809. resstate, link);
  810. }
  811. }
  812. }
  813. }
  814. static int
  815. resolve_name(int family, const char *hostname, int flags,
  816. struct addrinfo **aip, int socktype, int port)
  817. {
  818. isc_result_t result;
  819. irs_context_t *irsctx;
  820. irs_resconf_t *conf;
  821. isc_mem_t *mctx;
  822. isc_appctx_t *actx;
  823. isc_task_t *task;
  824. int terror = 0;
  825. int error = 0;
  826. dns_client_t *client;
  827. gai_resstate_t *resstate;
  828. gai_statehead_t head;
  829. isc_boolean_t all_fail = ISC_TRUE;
  830. /* get IRS context and the associated parameters */
  831. irsctx = NULL;
  832. result = irs_context_get(&irsctx);
  833. if (result != ISC_R_SUCCESS)
  834. return (EAI_FAIL);
  835. actx = irs_context_getappctx(irsctx);
  836. mctx = irs_context_getmctx(irsctx);
  837. task = irs_context_gettask(irsctx);
  838. conf = irs_context_getresconf(irsctx);
  839. client = irs_context_getdnsclient(irsctx);
  840. /* construct resolution states */
  841. head.activestates = 0;
  842. head.ai_family = family;
  843. head.ai_socktype = socktype;
  844. head.ai_flags = flags;
  845. head.ai_port = port;
  846. head.actx = actx;
  847. head.dnsclient = client;
  848. ISC_LIST_INIT(head.resstates);
  849. result = make_resstates(mctx, hostname, &head, conf);
  850. if (result != ISC_R_SUCCESS)
  851. return (EAI_FAIL);
  852. for (resstate = ISC_LIST_HEAD(head.resstates);
  853. resstate != NULL; resstate = ISC_LIST_NEXT(resstate, link)) {
  854. if (resstate->trans4 != NULL) {
  855. result = dns_client_startresolve(client,
  856. resstate->qname,
  857. dns_rdataclass_in,
  858. dns_rdatatype_a,
  859. 0, task,
  860. process_answer,
  861. resstate->trans4,
  862. &resstate->trans4->xid);
  863. if (result == ISC_R_SUCCESS) {
  864. resstate->trans4->is_inprogress = ISC_TRUE;
  865. all_fail = ISC_FALSE;
  866. } else
  867. resstate->trans4->is_inprogress = ISC_FALSE;
  868. }
  869. if (resstate->trans6 != NULL) {
  870. result = dns_client_startresolve(client,
  871. resstate->qname,
  872. dns_rdataclass_in,
  873. dns_rdatatype_aaaa,
  874. 0, task,
  875. process_answer,
  876. resstate->trans6,
  877. &resstate->trans6->xid);
  878. if (result == ISC_R_SUCCESS) {
  879. resstate->trans6->is_inprogress = ISC_TRUE;
  880. all_fail = ISC_FALSE;
  881. } else
  882. resstate->trans6->is_inprogress= ISC_FALSE;
  883. }
  884. }
  885. if (!all_fail) {
  886. /* Start all the events */
  887. isc_app_ctxrun(actx);
  888. } else
  889. error = EAI_FAIL;
  890. /* Cleanup */
  891. while ((resstate = ISC_LIST_HEAD(head.resstates)) != NULL) {
  892. int terror4 = 0, terror6 = 0;
  893. ISC_LIST_UNLINK(head.resstates, resstate, link);
  894. if (*aip == NULL) {
  895. struct addrinfo *sentinel4 = NULL;
  896. struct addrinfo *sentinel6 = NULL;
  897. if (resstate->trans4 != NULL) {
  898. sentinel4 =
  899. resstate->trans4->ai_sentinel.ai_next;
  900. resstate->trans4->ai_sentinel.ai_next = NULL;
  901. }
  902. if (resstate->trans6 != NULL) {
  903. sentinel6 =
  904. resstate->trans6->ai_sentinel.ai_next;
  905. resstate->trans6->ai_sentinel.ai_next = NULL;
  906. }
  907. *aip = ai_concat(sentinel4, sentinel6);
  908. }
  909. if (resstate->trans4 != NULL) {
  910. INSIST(resstate->trans4->xid == NULL);
  911. terror4 = resstate->trans4->error;
  912. isc_mem_put(mctx, resstate->trans4,
  913. sizeof(*resstate->trans4));
  914. }
  915. if (resstate->trans6 != NULL) {
  916. INSIST(resstate->trans6->xid == NULL);
  917. terror6 = resstate->trans6->error;
  918. isc_mem_put(mctx, resstate->trans6,
  919. sizeof(*resstate->trans6));
  920. }
  921. /*
  922. * If the entire lookup fails, we need to choose an appropriate
  923. * error code from individual codes. We'll try to provide as
  924. * specific a code as possible. In general, we are going to
  925. * find an error code other than EAI_NONAME (which is too
  926. * generic and may actually not be problematic in some cases).
  927. * EAI_NONAME will be set below if no better code is found.
  928. */
  929. if (terror == 0 || terror == EAI_NONAME) {
  930. if (terror4 != 0 && terror4 != EAI_NONAME)
  931. terror = terror4;
  932. else if (terror6 != 0 && terror6 != EAI_NONAME)
  933. terror = terror6;
  934. }
  935. isc_mem_put(mctx, resstate, sizeof(*resstate));
  936. }
  937. if (*aip == NULL) {
  938. error = terror;
  939. if (error == 0)
  940. error = EAI_NONAME;
  941. }
  942. #if 1 /* XXX: enabled for finding leaks. should be cleaned up later. */
  943. isc_app_ctxfinish(actx);
  944. irs_context_destroy(&irsctx);
  945. #endif
  946. return (error);
  947. }
  948. static char *
  949. irs_strsep(char **stringp, const char *delim) {
  950. char *string = *stringp;
  951. char *s;
  952. const char *d;
  953. char sc, dc;
  954. if (string == NULL)
  955. return (NULL);
  956. for (s = string; *s != '\0'; s++) {
  957. sc = *s;
  958. for (d = delim; (dc = *d) != '\0'; d++)
  959. if (sc == dc) {
  960. *s++ = '\0';
  961. *stringp = s;
  962. return (string);
  963. }
  964. }
  965. *stringp = NULL;
  966. return (string);
  967. }
  968. static void
  969. set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
  970. int, int))
  971. {
  972. char *order, *tok;
  973. int found;
  974. if (family) {
  975. switch (family) {
  976. case AF_INET:
  977. *net_order++ = add_ipv4;
  978. break;
  979. case AF_INET6:
  980. *net_order++ = add_ipv6;
  981. break;
  982. }
  983. } else {
  984. order = getenv("NET_ORDER");
  985. found = 0;
  986. while (order != NULL) {
  987. /*
  988. * We ignore any unknown names.
  989. */
  990. tok = irs_strsep(&order, ":");
  991. if (strcasecmp(tok, "inet6") == 0) {
  992. if ((found & FOUND_IPV6) == 0)
  993. *net_order++ = add_ipv6;
  994. found |= FOUND_IPV6;
  995. } else if (strcasecmp(tok, "inet") == 0 ||
  996. strcasecmp(tok, "inet4") == 0) {
  997. if ((found & FOUND_IPV4) == 0)
  998. *net_order++ = add_ipv4;
  999. found |= FOUND_IPV4;
  1000. }
  1001. }
  1002. /*
  1003. * Add in anything that we didn't find.
  1004. */
  1005. if ((found & FOUND_IPV4) == 0)
  1006. *net_order++ = add_ipv4;
  1007. if ((found & FOUND_IPV6) == 0)
  1008. *net_order++ = add_ipv6;
  1009. }
  1010. *net_order = NULL;
  1011. return;
  1012. }
  1013. static char v4_loop[4] = { 127, 0, 0, 1 };
  1014. static int
  1015. add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
  1016. int socktype, int port)
  1017. {
  1018. struct addrinfo *ai;
  1019. UNUSED(hostname);
  1020. UNUSED(flags);
  1021. ai = ai_clone(*aip, AF_INET); /* don't use ai_clone() */
  1022. if (ai == NULL) {
  1023. freeaddrinfo(*aip);
  1024. return (EAI_MEMORY);
  1025. }
  1026. *aip = ai;
  1027. ai->ai_socktype = socktype;
  1028. SIN(ai->ai_addr)->sin_port = port;
  1029. memcpy(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
  1030. return (0);
  1031. }
  1032. static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
  1033. static int
  1034. add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
  1035. int socktype, int port)
  1036. {
  1037. struct addrinfo *ai;
  1038. UNUSED(hostname);
  1039. UNUSED(flags);
  1040. ai = ai_clone(*aip, AF_INET6); /* don't use ai_clone() */
  1041. if (ai == NULL) {
  1042. freeaddrinfo(*aip);
  1043. return (EAI_MEMORY);
  1044. }
  1045. *aip = ai;
  1046. ai->ai_socktype = socktype;
  1047. SIN6(ai->ai_addr)->sin6_port = port;
  1048. memcpy(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
  1049. return (0);
  1050. }
  1051. /*% Free address info. */
  1052. void
  1053. freeaddrinfo(struct addrinfo *ai) {
  1054. struct addrinfo *ai_next;
  1055. while (ai != NULL) {
  1056. ai_next = ai->ai_next;
  1057. if (ai->ai_addr != NULL)
  1058. free(ai->ai_addr);
  1059. if (ai->ai_canonname)
  1060. free(ai->ai_canonname);
  1061. free(ai);
  1062. ai = ai_next;
  1063. }
  1064. }
  1065. #ifdef AF_LOCAL
  1066. static int
  1067. get_local(const char *name, int socktype, struct addrinfo **res) {
  1068. struct addrinfo *ai;
  1069. struct sockaddr_un *slocal;
  1070. if (socktype == 0)
  1071. return (EAI_SOCKTYPE);
  1072. ai = ai_alloc(AF_LOCAL, sizeof(*slocal));
  1073. if (ai == NULL)
  1074. return (EAI_MEMORY);
  1075. slocal = SLOCAL(ai->ai_addr);
  1076. strncpy(slocal->sun_path, name, sizeof(slocal->sun_path));
  1077. ai->ai_socktype = socktype;
  1078. /*
  1079. * ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
  1080. * and ai->ai_next were initialized to zero.
  1081. */
  1082. *res = ai;
  1083. return (0);
  1084. }
  1085. #endif
  1086. /*!
  1087. * Allocate an addrinfo structure, and a sockaddr structure
  1088. * of the specificed length. We initialize:
  1089. * ai_addrlen
  1090. * ai_family
  1091. * ai_addr
  1092. * ai_addr->sa_family
  1093. * ai_addr->sa_len (IRS_PLATFORM_HAVESALEN)
  1094. * and everything else is initialized to zero.
  1095. */
  1096. static struct addrinfo *
  1097. ai_alloc(int family, int addrlen) {
  1098. struct addrinfo *ai;
  1099. ai = (struct addrinfo *)calloc(1, sizeof(*ai));
  1100. if (ai == NULL)
  1101. return (NULL);
  1102. ai->ai_addr = SA(calloc(1, addrlen));
  1103. if (ai->ai_addr == NULL) {
  1104. free(ai);
  1105. return (NULL);
  1106. }
  1107. ai->ai_addrlen = addrlen;
  1108. ai->ai_family = family;
  1109. ai->ai_addr->sa_family = family;
  1110. #ifdef IRS_PLATFORM_HAVESALEN
  1111. ai->ai_addr->sa_len = addrlen;
  1112. #endif
  1113. return (ai);
  1114. }
  1115. static struct addrinfo *
  1116. ai_clone(struct addrinfo *oai, int family) {
  1117. struct addrinfo *ai;
  1118. ai = ai_alloc(family, ((family == AF_INET6) ?
  1119. sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
  1120. if (ai == NULL) {
  1121. if (oai != NULL)
  1122. freeaddrinfo(oai);
  1123. return (NULL);
  1124. }
  1125. if (oai == NULL)
  1126. return (ai);
  1127. ai->ai_flags = oai->ai_flags;
  1128. ai->ai_socktype = oai->ai_socktype;
  1129. ai->ai_protocol = oai->ai_protocol;
  1130. ai->ai_canonname = NULL;
  1131. ai->ai_next = oai;
  1132. return (ai);
  1133. }
  1134. static struct addrinfo *
  1135. ai_reverse(struct addrinfo *oai) {
  1136. struct addrinfo *nai, *tai;
  1137. nai = NULL;
  1138. while (oai != NULL) {
  1139. /*
  1140. * Grab one off the old list.
  1141. */
  1142. tai = oai;
  1143. oai = oai->ai_next;
  1144. /*
  1145. * Put it on the front of the new list.
  1146. */
  1147. tai->ai_next = nai;
  1148. nai = tai;
  1149. }
  1150. return (nai);
  1151. }
  1152. static struct addrinfo *
  1153. ai_concat(struct addrinfo *ai1, struct addrinfo *ai2) {
  1154. struct addrinfo *ai_tmp;
  1155. if (ai1 == NULL)
  1156. return (ai2);
  1157. else if (ai2 == NULL)
  1158. return (ai1);
  1159. for (ai_tmp = ai1; ai_tmp != NULL && ai_tmp->ai_next != NULL;
  1160. ai_tmp = ai_tmp->ai_next)
  1161. ;
  1162. ai_tmp->ai_next = ai2;
  1163. return (ai1);
  1164. }