asyn-ares.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef CURLRES_ARES
  26. /***********************************************************************
  27. * Only for ares-enabled builds
  28. * And only for functions that fulfill the asynch resolver backend API
  29. * as defined in asyn.h, nothing else belongs in this file!
  30. **********************************************************************/
  31. #include <limits.h>
  32. #ifdef HAVE_NETINET_IN_H
  33. #include <netinet/in.h>
  34. #endif
  35. #ifdef HAVE_NETDB_H
  36. #include <netdb.h>
  37. #endif
  38. #ifdef HAVE_ARPA_INET_H
  39. #include <arpa/inet.h>
  40. #endif
  41. #ifdef __VMS
  42. #include <in.h>
  43. #include <inet.h>
  44. #endif
  45. #include "urldata.h"
  46. #include "cfilters.h"
  47. #include "sendf.h"
  48. #include "hostip.h"
  49. #include "hash.h"
  50. #include "share.h"
  51. #include "url.h"
  52. #include "multiif.h"
  53. #include "curlx/inet_pton.h"
  54. #include "connect.h"
  55. #include "select.h"
  56. #include "progress.h"
  57. #include "curlx/timediff.h"
  58. #include "httpsrr.h"
  59. #include <ares.h>
  60. #include <ares_version.h> /* really old c-ares did not include this by
  61. itself */
  62. #if ARES_VERSION >= 0x010601
  63. /* IPv6 supported since 1.6.1 */
  64. #define HAVE_CARES_IPV6 1
  65. #endif
  66. #if ARES_VERSION >= 0x010704
  67. #define HAVE_CARES_SERVERS_CSV 1
  68. #define HAVE_CARES_LOCAL_DEV 1
  69. #define HAVE_CARES_SET_LOCAL 1
  70. #endif
  71. #if ARES_VERSION >= 0x010b00
  72. #define HAVE_CARES_PORTS_CSV 1
  73. #endif
  74. #if ARES_VERSION >= 0x011000
  75. /* 1.16.0 or later has ares_getaddrinfo */
  76. #define HAVE_CARES_GETADDRINFO 1
  77. #else
  78. /* How long we are willing to wait for additional parallel responses after
  79. obtaining a "definitive" one. For old c-ares without getaddrinfo.
  80. This is intended to equal the c-ares default timeout. cURL always uses that
  81. default value. Unfortunately, c-ares does not expose its default timeout in
  82. its API, but it is officially documented as 5 seconds.
  83. See query_completed_cb() for an explanation of how this is used.
  84. */
  85. #define HAPPY_EYEBALLS_DNS_TIMEOUT 5000
  86. #endif
  87. #ifdef USE_HTTPSRR
  88. #if ARES_VERSION < 0x011c00
  89. #error "requires c-ares 1.28.0 or newer for HTTPSRR"
  90. #endif
  91. #define HTTPSRR_WORKS
  92. #endif
  93. /* The last 2 #include files should be in this order */
  94. #include "curl_memory.h"
  95. #include "memdebug.h"
  96. #define CARES_TIMEOUT_PER_ATTEMPT 2000
  97. static int ares_ver = 0;
  98. static CURLcode async_ares_set_dns_servers(struct Curl_easy *data,
  99. bool reset_on_null);
  100. /*
  101. * Curl_async_global_init() - the generic low-level asynchronous name
  102. * resolve API. Called from curl_global_init() to initialize global resolver
  103. * environment. Initializes ares library.
  104. */
  105. int Curl_async_global_init(void)
  106. {
  107. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  108. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  109. return CURLE_FAILED_INIT;
  110. }
  111. #endif
  112. ares_version(&ares_ver);
  113. return CURLE_OK;
  114. }
  115. /*
  116. * Curl_async_global_cleanup()
  117. *
  118. * Called from curl_global_cleanup() to destroy global resolver environment.
  119. * Deinitializes ares library.
  120. */
  121. void Curl_async_global_cleanup(void)
  122. {
  123. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  124. ares_library_cleanup();
  125. #endif
  126. }
  127. static void sock_state_cb(void *data, ares_socket_t socket_fd,
  128. int readable, int writable)
  129. {
  130. struct Curl_easy *easy = data;
  131. if(!readable && !writable) {
  132. DEBUGASSERT(easy);
  133. Curl_multi_will_close(easy, socket_fd);
  134. }
  135. }
  136. static CURLcode async_ares_init(struct Curl_easy *data)
  137. {
  138. struct async_ares_ctx *ares = &data->state.async.ares;
  139. int status;
  140. struct ares_options options;
  141. int optmask = ARES_OPT_SOCK_STATE_CB;
  142. CURLcode rc = CURLE_OK;
  143. options.sock_state_cb = sock_state_cb;
  144. options.sock_state_cb_data = data;
  145. DEBUGASSERT(!ares->channel);
  146. /*
  147. if c ares < 1.20.0: curl set timeout to CARES_TIMEOUT_PER_ATTEMPT (2s)
  148. if c-ares >= 1.20.0 it already has the timeout to 2s, curl does not need
  149. to set the timeout value;
  150. if c-ares >= 1.24.0, user can set the timeout via /etc/resolv.conf to
  151. overwrite c-ares' timeout.
  152. */
  153. DEBUGASSERT(ares_ver);
  154. if(ares_ver < 0x011400) {
  155. options.timeout = CARES_TIMEOUT_PER_ATTEMPT;
  156. optmask |= ARES_OPT_TIMEOUTMS;
  157. }
  158. status = ares_init_options(&ares->channel, &options, optmask);
  159. if(status != ARES_SUCCESS) {
  160. ares->channel = NULL;
  161. rc = (status == ARES_ENOMEM) ?
  162. CURLE_OUT_OF_MEMORY : CURLE_FAILED_INIT;
  163. goto out;
  164. }
  165. rc = async_ares_set_dns_servers(data, FALSE);
  166. if(rc && rc != CURLE_NOT_BUILT_IN)
  167. goto out;
  168. rc = Curl_async_ares_set_dns_interface(data);
  169. if(rc && rc != CURLE_NOT_BUILT_IN)
  170. goto out;
  171. rc = Curl_async_ares_set_dns_local_ip4(data);
  172. if(rc && rc != CURLE_NOT_BUILT_IN)
  173. goto out;
  174. rc = Curl_async_ares_set_dns_local_ip6(data);
  175. if(rc && rc != CURLE_NOT_BUILT_IN)
  176. goto out;
  177. rc = CURLE_OK;
  178. out:
  179. if(rc && ares->channel) {
  180. ares_destroy(ares->channel);
  181. ares->channel = NULL;
  182. }
  183. return rc;
  184. }
  185. static CURLcode async_ares_init_lazy(struct Curl_easy *data)
  186. {
  187. struct async_ares_ctx *ares = &data->state.async.ares;
  188. if(!ares->channel)
  189. return async_ares_init(data);
  190. return CURLE_OK;
  191. }
  192. CURLcode Curl_async_get_impl(struct Curl_easy *data, void **impl)
  193. {
  194. struct async_ares_ctx *ares = &data->state.async.ares;
  195. CURLcode result = CURLE_OK;
  196. if(!ares->channel) {
  197. result = async_ares_init(data);
  198. }
  199. *impl = ares->channel;
  200. return result;
  201. }
  202. /*
  203. * async_ares_cleanup() cleans up async resolver data.
  204. */
  205. static void async_ares_cleanup(struct Curl_easy *data)
  206. {
  207. struct async_ares_ctx *ares = &data->state.async.ares;
  208. if(ares->temp_ai) {
  209. Curl_freeaddrinfo(ares->temp_ai);
  210. ares->temp_ai = NULL;
  211. }
  212. #ifdef USE_HTTPSRR
  213. Curl_httpsrr_cleanup(&ares->hinfo);
  214. #endif
  215. }
  216. void Curl_async_ares_shutdown(struct Curl_easy *data)
  217. {
  218. /* c-ares has a method to "cancel" operations on a channel, but
  219. * as reported in #18216, this does not totally reset the channel
  220. * and ares may get stuck.
  221. * We need to destroy the channel and on demand create a new
  222. * one to avoid that. */
  223. Curl_async_ares_destroy(data);
  224. }
  225. void Curl_async_ares_destroy(struct Curl_easy *data)
  226. {
  227. struct async_ares_ctx *ares = &data->state.async.ares;
  228. if(ares->channel) {
  229. ares_destroy(ares->channel);
  230. ares->channel = NULL;
  231. }
  232. async_ares_cleanup(data);
  233. }
  234. /*
  235. * Curl_async_pollset() is called when someone from the outside world
  236. * (using curl_multi_fdset()) wants to get our fd_set setup.
  237. */
  238. CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps)
  239. {
  240. struct async_ares_ctx *ares = &data->state.async.ares;
  241. if(ares->channel)
  242. return Curl_ares_pollset(data, ares->channel, ps);
  243. return CURLE_OK;
  244. }
  245. /*
  246. * Curl_async_is_resolved() is called repeatedly to check if a previous
  247. * name resolve request has completed. It should also make sure to time-out if
  248. * the operation seems to take too long.
  249. *
  250. * Returns normal CURLcode errors.
  251. */
  252. CURLcode Curl_async_is_resolved(struct Curl_easy *data,
  253. struct Curl_dns_entry **dns)
  254. {
  255. struct async_ares_ctx *ares = &data->state.async.ares;
  256. CURLcode result = CURLE_OK;
  257. DEBUGASSERT(dns);
  258. *dns = NULL;
  259. if(data->state.async.done) {
  260. *dns = data->state.async.dns;
  261. return ares->result;
  262. }
  263. if(Curl_ares_perform(ares->channel, 0) < 0) {
  264. result = CURLE_UNRECOVERABLE_POLL;
  265. goto out;
  266. }
  267. #ifndef HAVE_CARES_GETADDRINFO
  268. /* Now that we have checked for any last minute results above, see if there
  269. are any responses still pending when the EXPIRE_HAPPY_EYEBALLS_DNS timer
  270. expires. */
  271. if(ares->num_pending
  272. /* This is only set to non-zero if the timer was started. */
  273. && (ares->happy_eyeballs_dns_time.tv_sec
  274. || ares->happy_eyeballs_dns_time.tv_usec)
  275. && (curlx_timediff(curlx_now(), ares->happy_eyeballs_dns_time)
  276. >= HAPPY_EYEBALLS_DNS_TIMEOUT)) {
  277. /* Remember that the EXPIRE_HAPPY_EYEBALLS_DNS timer is no longer
  278. running. */
  279. memset(&ares->happy_eyeballs_dns_time, 0,
  280. sizeof(ares->happy_eyeballs_dns_time));
  281. /* Cancel the raw c-ares request, which will fire query_completed_cb() with
  282. ARES_ECANCELLED synchronously for all pending responses. This will
  283. leave us with res->num_pending == 0, which is perfect for the next
  284. block. */
  285. ares_cancel(ares->channel);
  286. DEBUGASSERT(ares->num_pending == 0);
  287. }
  288. #endif
  289. if(!ares->num_pending) {
  290. /* all c-ares operations done, what is the result to report? */
  291. Curl_resolv_unlink(data, &data->state.async.dns);
  292. data->state.async.done = TRUE;
  293. result = ares->result;
  294. if(ares->ares_status == ARES_SUCCESS && !result) {
  295. data->state.async.dns =
  296. Curl_dnscache_mk_entry(data, ares->temp_ai,
  297. data->state.async.hostname, 0,
  298. data->state.async.port, FALSE);
  299. ares->temp_ai = NULL; /* temp_ai now owned by entry */
  300. #ifdef HTTPSRR_WORKS
  301. if(data->state.async.dns) {
  302. struct Curl_https_rrinfo *lhrr = Curl_httpsrr_dup_move(&ares->hinfo);
  303. if(!lhrr)
  304. result = CURLE_OUT_OF_MEMORY;
  305. else
  306. data->state.async.dns->hinfo = lhrr;
  307. }
  308. #endif
  309. if(!result && data->state.async.dns)
  310. result = Curl_dnscache_add(data, data->state.async.dns);
  311. }
  312. /* if we have not found anything, report the proper
  313. * CURLE_COULDNT_RESOLVE_* code */
  314. if(!result && !data->state.async.dns) {
  315. const char *msg = NULL;
  316. if(ares->ares_status != ARES_SUCCESS)
  317. msg = ares_strerror(ares->ares_status);
  318. result = Curl_resolver_error(data, msg);
  319. }
  320. if(result)
  321. Curl_resolv_unlink(data, &data->state.async.dns);
  322. *dns = data->state.async.dns;
  323. CURL_TRC_DNS(data, "is_resolved() result=%d, dns=%sfound",
  324. result, *dns ? "" : "not ");
  325. async_ares_cleanup(data);
  326. }
  327. out:
  328. ares->result = result;
  329. return result;
  330. }
  331. /*
  332. * Curl_async_await()
  333. *
  334. * Waits for a resolve to finish. This function should be avoided since using
  335. * this risk getting the multi interface to "hang".
  336. *
  337. * 'entry' MUST be non-NULL.
  338. *
  339. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  340. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  341. */
  342. CURLcode Curl_async_await(struct Curl_easy *data,
  343. struct Curl_dns_entry **entry)
  344. {
  345. struct async_ares_ctx *ares = &data->state.async.ares;
  346. CURLcode result = CURLE_OK;
  347. timediff_t timeout;
  348. struct curltime now = curlx_now();
  349. DEBUGASSERT(entry);
  350. *entry = NULL; /* clear on entry */
  351. timeout = Curl_timeleft(data, &now, TRUE);
  352. if(timeout < 0) {
  353. /* already expired! */
  354. connclose(data->conn, "Timed out before name resolve started");
  355. return CURLE_OPERATION_TIMEDOUT;
  356. }
  357. if(!timeout)
  358. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  359. /* Wait for the name resolve query to complete. */
  360. while(!result) {
  361. struct timeval *tvp, tv, store;
  362. int itimeout;
  363. timediff_t timeout_ms;
  364. #if TIMEDIFF_T_MAX > INT_MAX
  365. itimeout = (timeout > INT_MAX) ? INT_MAX : (int)timeout;
  366. #else
  367. itimeout = (int)timeout;
  368. #endif
  369. store.tv_sec = itimeout/1000;
  370. store.tv_usec = (itimeout%1000)*1000;
  371. tvp = ares_timeout(ares->channel, &store, &tv);
  372. /* use the timeout period ares returned to us above if less than one
  373. second is left, otherwise just use 1000ms to make sure the progress
  374. callback gets called frequent enough */
  375. if(!tvp->tv_sec)
  376. timeout_ms = (timediff_t)(tvp->tv_usec/1000);
  377. else
  378. timeout_ms = 1000;
  379. if(Curl_ares_perform(ares->channel, timeout_ms) < 0)
  380. return CURLE_UNRECOVERABLE_POLL;
  381. result = Curl_async_is_resolved(data, entry);
  382. if(result || data->state.async.done)
  383. break;
  384. if(Curl_pgrsUpdate(data))
  385. result = CURLE_ABORTED_BY_CALLBACK;
  386. else {
  387. struct curltime now2 = curlx_now();
  388. timediff_t timediff = curlx_timediff(now2, now); /* spent time */
  389. if(timediff <= 0)
  390. timeout -= 1; /* always deduct at least 1 */
  391. else if(timediff > timeout)
  392. timeout = -1;
  393. else
  394. timeout -= timediff;
  395. now = now2; /* for next loop */
  396. }
  397. if(timeout < 0)
  398. result = CURLE_OPERATION_TIMEDOUT;
  399. }
  400. /* Operation complete, if the lookup was successful we now have the entry
  401. in the cache. */
  402. data->state.async.done = TRUE;
  403. *entry = data->state.async.dns;
  404. if(result)
  405. ares_cancel(ares->channel);
  406. return result;
  407. }
  408. #ifndef HAVE_CARES_GETADDRINFO
  409. /* Connects results to the list */
  410. static void async_addr_concat(struct Curl_addrinfo **pbase,
  411. struct Curl_addrinfo *ai)
  412. {
  413. if(!ai)
  414. return;
  415. /* When adding `ai` to an existing address list, we prefer ipv6
  416. * to be in front. */
  417. #ifdef USE_IPV6 /* CURLRES_IPV6 */
  418. if(*pbase && (*pbase)->ai_family == PF_INET6) {
  419. /* ipv6 already in front, append `ai` */
  420. struct Curl_addrinfo *tail = *pbase;
  421. while(tail->ai_next)
  422. tail = tail->ai_next;
  423. tail->ai_next = ai;
  424. }
  425. else
  426. #endif /* CURLRES_IPV6 */
  427. {
  428. /* prepend to the (possibly) existing list. */
  429. struct Curl_addrinfo *tail = ai;
  430. while(tail->ai_next)
  431. tail = tail->ai_next;
  432. tail->ai_next = *pbase;
  433. *pbase = ai;
  434. }
  435. }
  436. /*
  437. * ares_query_completed_cb() is the callback that ares will call when
  438. * the host query initiated by ares_gethostbyname() from
  439. * Curl_async_getaddrinfo(), when using ares, is completed either
  440. * successfully or with failure.
  441. */
  442. static void async_ares_hostbyname_cb(void *user_data,
  443. int status,
  444. int timeouts,
  445. struct hostent *hostent)
  446. {
  447. struct Curl_easy *data = (struct Curl_easy *)user_data;
  448. struct async_ares_ctx *ares = &data->state.async.ares;
  449. (void)timeouts; /* ignored */
  450. if(ARES_EDESTRUCTION == status)
  451. return;
  452. if(ARES_SUCCESS == status) {
  453. ares->ares_status = status; /* one success overrules any error */
  454. async_addr_concat(&ares->temp_ai,
  455. Curl_he2ai(hostent, data->state.async.port));
  456. }
  457. else if(ares->ares_status != ARES_SUCCESS) {
  458. /* no success so far, remember last error */
  459. ares->ares_status = status;
  460. }
  461. ares->num_pending--;
  462. CURL_TRC_DNS(data, "ares: hostbyname done, status=%d, pending=%d, "
  463. "addr=%sfound",
  464. status, ares->num_pending, ares->temp_ai ? "" : "not ");
  465. /* If there are responses still pending, we presume they must be the
  466. complementary IPv4 or IPv6 lookups that we started in parallel in
  467. Curl_async_getaddrinfo() (for Happy Eyeballs). If we have got a
  468. "definitive" response from one of a set of parallel queries, we need to
  469. think about how long we are willing to wait for more responses. */
  470. if(ares->num_pending
  471. /* Only these c-ares status values count as "definitive" for these
  472. purposes. For example, ARES_ENODATA is what we expect when there is
  473. no IPv6 entry for a domain name, and that is not a reason to get more
  474. aggressive in our timeouts for the other response. Other errors are
  475. either a result of bad input (which should affect all parallel
  476. requests), local or network conditions, non-definitive server
  477. responses, or us cancelling the request. */
  478. && (status == ARES_SUCCESS || status == ARES_ENOTFOUND)) {
  479. /* Right now, there can only be up to two parallel queries, so do not
  480. bother handling any other cases. */
  481. DEBUGASSERT(ares->num_pending == 1);
  482. /* it is possible that one of these parallel queries could succeed
  483. quickly, but the other could always fail or timeout (when we are
  484. talking to a pool of DNS servers that can only successfully resolve
  485. IPv4 address, for example).
  486. it is also possible that the other request could always just take
  487. longer because it needs more time or only the second DNS server can
  488. fulfill it successfully. But, to align with the philosophy of Happy
  489. Eyeballs, we do not want to wait _too_ long or users will think
  490. requests are slow when IPv6 lookups do not actually work (but IPv4
  491. ones do).
  492. So, now that we have a usable answer (some IPv4 addresses, some IPv6
  493. addresses, or "no such domain"), we start a timeout for the remaining
  494. pending responses. Even though it is typical that this resolved
  495. request came back quickly, that needn't be the case. It might be that
  496. this completing request did not get a result from the first DNS
  497. server or even the first round of the whole DNS server pool. So it
  498. could already be quite some time after we issued the DNS queries in
  499. the first place. Without modifying c-ares, we cannot know exactly
  500. where in its retry cycle we are. We could guess based on how much
  501. time has gone by, but it does not really matter. Happy Eyeballs tells
  502. us that, given usable information in hand, we simply do not want to
  503. wait "too much longer" after we get a result.
  504. We simply wait an additional amount of time equal to the default
  505. c-ares query timeout. That is enough time for a typical parallel
  506. response to arrive without being "too long". Even on a network
  507. where one of the two types of queries is failing or timing out
  508. constantly, this will usually mean we wait a total of the default
  509. c-ares timeout (5 seconds) plus the round trip time for the successful
  510. request, which seems bearable. The downside is that c-ares might race
  511. with us to issue one more retry just before we give up, but it seems
  512. better to "waste" that request instead of trying to guess the perfect
  513. timeout to prevent it. After all, we do not even know where in the
  514. c-ares retry cycle each request is.
  515. */
  516. ares->happy_eyeballs_dns_time = curlx_now();
  517. Curl_expire(data, HAPPY_EYEBALLS_DNS_TIMEOUT,
  518. EXPIRE_HAPPY_EYEBALLS_DNS);
  519. }
  520. }
  521. #else
  522. /* c-ares 1.16.0 or later */
  523. /*
  524. * async_ares_node2addr() converts an address list provided by c-ares
  525. * to an internal libcurl compatible list.
  526. */
  527. static struct Curl_addrinfo *
  528. async_ares_node2addr(struct ares_addrinfo_node *node)
  529. {
  530. /* traverse the ares_addrinfo_node list */
  531. struct ares_addrinfo_node *ai;
  532. struct Curl_addrinfo *cafirst = NULL;
  533. struct Curl_addrinfo *calast = NULL;
  534. int error = 0;
  535. for(ai = node; ai != NULL; ai = ai->ai_next) {
  536. size_t ss_size;
  537. struct Curl_addrinfo *ca;
  538. /* ignore elements with unsupported address family, */
  539. /* settle family-specific sockaddr structure size. */
  540. if(ai->ai_family == AF_INET)
  541. ss_size = sizeof(struct sockaddr_in);
  542. #ifdef USE_IPV6
  543. else if(ai->ai_family == AF_INET6)
  544. ss_size = sizeof(struct sockaddr_in6);
  545. #endif
  546. else
  547. continue;
  548. /* ignore elements without required address info */
  549. if(!ai->ai_addr || !(ai->ai_addrlen > 0))
  550. continue;
  551. /* ignore elements with bogus address size */
  552. if((size_t)ai->ai_addrlen < ss_size)
  553. continue;
  554. ca = malloc(sizeof(struct Curl_addrinfo) + ss_size);
  555. if(!ca) {
  556. error = EAI_MEMORY;
  557. break;
  558. }
  559. /* copy each structure member individually, member ordering, */
  560. /* size, or padding might be different for each platform. */
  561. ca->ai_flags = ai->ai_flags;
  562. ca->ai_family = ai->ai_family;
  563. ca->ai_socktype = ai->ai_socktype;
  564. ca->ai_protocol = ai->ai_protocol;
  565. ca->ai_addrlen = (curl_socklen_t)ss_size;
  566. ca->ai_addr = NULL;
  567. ca->ai_canonname = NULL;
  568. ca->ai_next = NULL;
  569. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  570. memcpy(ca->ai_addr, ai->ai_addr, ss_size);
  571. /* if the return list is empty, this becomes the first element */
  572. if(!cafirst)
  573. cafirst = ca;
  574. /* add this element last in the return list */
  575. if(calast)
  576. calast->ai_next = ca;
  577. calast = ca;
  578. }
  579. /* if we failed, destroy the Curl_addrinfo list */
  580. if(error) {
  581. Curl_freeaddrinfo(cafirst);
  582. cafirst = NULL;
  583. }
  584. return cafirst;
  585. }
  586. static void async_ares_addrinfo_cb(void *user_data, int status, int timeouts,
  587. struct ares_addrinfo *ares_ai)
  588. {
  589. struct Curl_easy *data = (struct Curl_easy *)user_data;
  590. struct async_ares_ctx *ares = &data->state.async.ares;
  591. (void)timeouts;
  592. if(ares->ares_status != ARES_SUCCESS) /* do not overwrite success */
  593. ares->ares_status = status;
  594. if(status == ARES_SUCCESS) {
  595. ares->temp_ai = async_ares_node2addr(ares_ai->nodes);
  596. ares_freeaddrinfo(ares_ai);
  597. }
  598. ares->num_pending--;
  599. CURL_TRC_DNS(data, "ares: addrinfo done, query status=%d, "
  600. "overall status=%d, pending=%d, addr=%sfound",
  601. status, ares->ares_status, ares->num_pending,
  602. ares->temp_ai ? "" : "not ");
  603. }
  604. #endif
  605. #ifdef USE_HTTPSRR
  606. static void async_ares_rr_done(void *user_data, ares_status_t status,
  607. size_t timeouts,
  608. const ares_dns_record_t *dnsrec)
  609. {
  610. struct Curl_easy *data = user_data;
  611. struct async_ares_ctx *ares = &data->state.async.ares;
  612. (void)timeouts;
  613. --ares->num_pending;
  614. CURL_TRC_DNS(data, "ares: httpsrr done, status=%d, pending=%d, "
  615. "dnsres=%sfound",
  616. status, ares->num_pending,
  617. (dnsrec &&
  618. ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER)) ?
  619. "" : "not ");
  620. if((ARES_SUCCESS != status) || !dnsrec)
  621. return;
  622. ares->result = Curl_httpsrr_from_ares(data, dnsrec, &ares->hinfo);
  623. }
  624. #endif /* USE_HTTPSRR */
  625. /*
  626. * Curl_async_getaddrinfo() - when using ares
  627. *
  628. * Returns name information about the given hostname and port number. If
  629. * successful, the 'hostent' is returned and the fourth argument will point to
  630. * memory we need to free after use. That memory *MUST* be freed with
  631. * Curl_freeaddrinfo(), nothing else.
  632. */
  633. struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,
  634. const char *hostname,
  635. int port,
  636. int ip_version,
  637. int *waitp)
  638. {
  639. struct async_ares_ctx *ares = &data->state.async.ares;
  640. #ifdef USE_HTTPSRR
  641. char *rrname = NULL;
  642. #endif
  643. *waitp = 0; /* default to synchronous response */
  644. if(async_ares_init_lazy(data))
  645. return NULL;
  646. data->state.async.done = FALSE; /* not done */
  647. data->state.async.dns = NULL; /* clear */
  648. data->state.async.port = port;
  649. data->state.async.ip_version = ip_version;
  650. data->state.async.hostname = strdup(hostname);
  651. if(!data->state.async.hostname)
  652. return NULL;
  653. #ifdef USE_HTTPSRR
  654. if(port != 443) {
  655. rrname = curl_maprintf("_%d_.https.%s", port, hostname);
  656. if(!rrname) {
  657. free(data->state.async.hostname);
  658. return NULL;
  659. }
  660. }
  661. #endif
  662. /* initial status - failed */
  663. ares->ares_status = ARES_ENOTFOUND;
  664. ares->result = CURLE_OK;
  665. #if !defined(CURL_DISABLE_VERBOSE_STRINGS) && \
  666. ARES_VERSION >= 0x011800 /* >= v1.24.0 */
  667. if(CURL_TRC_DNS_is_verbose(data)) {
  668. char *csv = ares_get_servers_csv(ares->channel);
  669. CURL_TRC_DNS(data, "asyn-ares: servers=%s", csv);
  670. ares_free_string(csv);
  671. }
  672. #endif
  673. #ifdef HAVE_CARES_GETADDRINFO
  674. {
  675. struct ares_addrinfo_hints hints;
  676. char service[12];
  677. int pf = PF_INET;
  678. memset(&hints, 0, sizeof(hints));
  679. #ifdef CURLRES_IPV6
  680. if((ip_version != CURL_IPRESOLVE_V4) &&
  681. Curl_ipv6works(data)) {
  682. /* The stack seems to be IPv6-enabled */
  683. if(ip_version == CURL_IPRESOLVE_V6)
  684. pf = PF_INET6;
  685. else
  686. pf = PF_UNSPEC;
  687. }
  688. #endif /* CURLRES_IPV6 */
  689. CURL_TRC_DNS(data, "asyn-ares: fire off getaddrinfo for %s",
  690. (pf == PF_UNSPEC) ? "A+AAAA" :
  691. ((pf == PF_INET) ? "A" : "AAAA"));
  692. hints.ai_family = pf;
  693. hints.ai_socktype =
  694. (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ?
  695. SOCK_STREAM : SOCK_DGRAM;
  696. /* Since the service is a numerical one, set the hint flags
  697. * accordingly to save a call to getservbyname in inside C-Ares
  698. */
  699. hints.ai_flags = ARES_AI_NUMERICSERV;
  700. curl_msnprintf(service, sizeof(service), "%d", port);
  701. ares->num_pending = 1;
  702. ares_getaddrinfo(ares->channel, data->state.async.hostname,
  703. service, &hints, async_ares_addrinfo_cb, data);
  704. }
  705. #else
  706. #ifdef HAVE_CARES_IPV6
  707. if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  708. /* The stack seems to be IPv6-enabled */
  709. /* areschannel is already setup in the Curl_open() function */
  710. CURL_TRC_DNS(data, "asyn-ares: fire off query for A");
  711. ares_gethostbyname(ares->channel, data->state.async.hostname, PF_INET,
  712. async_ares_hostbyname_cb, data);
  713. CURL_TRC_DNS(data, "asyn-ares: fire off query for AAAA");
  714. ares->num_pending = 2;
  715. ares_gethostbyname(ares->channel, data->state.async.hostname, PF_INET6,
  716. async_ares_hostbyname_cb, data);
  717. }
  718. else
  719. #endif
  720. {
  721. /* areschannel is already setup in the Curl_open() function */
  722. CURL_TRC_DNS(data, "asyn-ares: fire off query for A");
  723. ares->num_pending = 1;
  724. ares_gethostbyname(ares->channel, data->state.async.hostname, PF_INET,
  725. async_ares_hostbyname_cb, data);
  726. }
  727. #endif
  728. #ifdef USE_HTTPSRR
  729. {
  730. CURL_TRC_DNS(data, "asyn-ares: fire off query for HTTPSRR: %s",
  731. rrname ? rrname : data->state.async.hostname);
  732. memset(&ares->hinfo, 0, sizeof(ares->hinfo));
  733. ares->hinfo.port = -1;
  734. ares->hinfo.rrname = rrname;
  735. ares->num_pending++; /* one more */
  736. ares_query_dnsrec(ares->channel,
  737. rrname ? rrname : data->state.async.hostname,
  738. ARES_CLASS_IN, ARES_REC_TYPE_HTTPS,
  739. async_ares_rr_done, data, NULL);
  740. }
  741. #endif
  742. *waitp = 1; /* expect asynchronous response */
  743. return NULL; /* no struct yet */
  744. }
  745. /* Set what DNS server are is to use. This is called in 2 situations:
  746. * 1. when the application does 'CURLOPT_DNS_SERVERS' and passing NULL
  747. * means any previous set value should be unset. Which means
  748. * we need to destroy and create the are channel anew, if there is one.
  749. * 2. When we lazy init the ares channel and NULL means that there
  750. * are no preferences and we do not reset any existing channel. */
  751. static CURLcode async_ares_set_dns_servers(struct Curl_easy *data,
  752. bool reset_on_null)
  753. {
  754. struct async_ares_ctx *ares = &data->state.async.ares;
  755. CURLcode result = CURLE_NOT_BUILT_IN;
  756. const char *servers = data->set.str[STRING_DNS_SERVERS];
  757. int ares_result = ARES_SUCCESS;
  758. #if defined(CURLDEBUG) && defined(HAVE_CARES_SERVERS_CSV)
  759. if(getenv("CURL_DNS_SERVER"))
  760. servers = getenv("CURL_DNS_SERVER");
  761. #endif
  762. if(!servers) {
  763. if(reset_on_null) {
  764. Curl_async_destroy(data);
  765. }
  766. return CURLE_OK;
  767. }
  768. #ifdef HAVE_CARES_SERVERS_CSV
  769. /* if channel is not there, this is just a parameter check */
  770. if(ares->channel)
  771. #ifdef HAVE_CARES_PORTS_CSV
  772. ares_result = ares_set_servers_ports_csv(ares->channel, servers);
  773. #else
  774. ares_result = ares_set_servers_csv(ares->channel, servers);
  775. #endif
  776. switch(ares_result) {
  777. case ARES_SUCCESS:
  778. result = CURLE_OK;
  779. break;
  780. case ARES_ENOMEM:
  781. result = CURLE_OUT_OF_MEMORY;
  782. break;
  783. case ARES_ENOTINITIALIZED:
  784. case ARES_ENODATA:
  785. case ARES_EBADSTR:
  786. default:
  787. DEBUGF(infof(data, "bad servers set"));
  788. result = CURLE_BAD_FUNCTION_ARGUMENT;
  789. break;
  790. }
  791. #else /* too old c-ares version! */
  792. (void)data;
  793. (void)(ares_result);
  794. #endif
  795. return result;
  796. }
  797. CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data)
  798. {
  799. return async_ares_set_dns_servers(data, TRUE);
  800. }
  801. CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data)
  802. {
  803. #ifdef HAVE_CARES_LOCAL_DEV
  804. struct async_ares_ctx *ares = &data->state.async.ares;
  805. const char *interf = data->set.str[STRING_DNS_INTERFACE];
  806. if(!interf)
  807. interf = "";
  808. /* if channel is not there, this is just a parameter check */
  809. if(ares->channel)
  810. ares_set_local_dev(ares->channel, interf);
  811. return CURLE_OK;
  812. #else /* c-ares version too old! */
  813. (void)data;
  814. (void)interf;
  815. return CURLE_NOT_BUILT_IN;
  816. #endif
  817. }
  818. CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data)
  819. {
  820. #ifdef HAVE_CARES_SET_LOCAL
  821. struct async_ares_ctx *ares = &data->state.async.ares;
  822. struct in_addr a4;
  823. const char *local_ip4 = data->set.str[STRING_DNS_LOCAL_IP4];
  824. if((!local_ip4) || (local_ip4[0] == 0)) {
  825. a4.s_addr = 0; /* disabled: do not bind to a specific address */
  826. }
  827. else {
  828. if(curlx_inet_pton(AF_INET, local_ip4, &a4) != 1) {
  829. DEBUGF(infof(data, "bad DNS IPv4 address"));
  830. return CURLE_BAD_FUNCTION_ARGUMENT;
  831. }
  832. }
  833. /* if channel is not there yet, this is just a parameter check */
  834. if(ares->channel)
  835. ares_set_local_ip4(ares->channel, ntohl(a4.s_addr));
  836. return CURLE_OK;
  837. #else /* c-ares version too old! */
  838. (void)data;
  839. (void)local_ip4;
  840. return CURLE_NOT_BUILT_IN;
  841. #endif
  842. }
  843. CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data)
  844. {
  845. #if defined(HAVE_CARES_SET_LOCAL) && defined(USE_IPV6)
  846. struct async_ares_ctx *ares = &data->state.async.ares;
  847. unsigned char a6[INET6_ADDRSTRLEN];
  848. const char *local_ip6 = data->set.str[STRING_DNS_LOCAL_IP6];
  849. if((!local_ip6) || (local_ip6[0] == 0)) {
  850. /* disabled: do not bind to a specific address */
  851. memset(a6, 0, sizeof(a6));
  852. }
  853. else {
  854. if(curlx_inet_pton(AF_INET6, local_ip6, a6) != 1) {
  855. DEBUGF(infof(data, "bad DNS IPv6 address"));
  856. return CURLE_BAD_FUNCTION_ARGUMENT;
  857. }
  858. }
  859. /* if channel is not there, this is just a parameter check */
  860. if(ares->channel)
  861. ares_set_local_ip6(ares->channel, a6);
  862. return CURLE_OK;
  863. #else /* c-ares version too old! */
  864. (void)data;
  865. return CURLE_NOT_BUILT_IN;
  866. #endif
  867. }
  868. #endif /* CURLRES_ARES */