asyn-thrdd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. #include "socketpair.h"
  26. /***********************************************************************
  27. * Only for threaded name resolves builds
  28. **********************************************************************/
  29. #ifdef CURLRES_THREADED
  30. #ifdef HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_NETDB_H
  34. #include <netdb.h>
  35. #endif
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <arpa/inet.h>
  38. #endif
  39. #ifdef __VMS
  40. #include <in.h>
  41. #include <inet.h>
  42. #endif
  43. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  44. # include <pthread.h>
  45. #endif
  46. #ifdef HAVE_GETADDRINFO
  47. # define RESOLVER_ENOMEM EAI_MEMORY /* = WSA_NOT_ENOUGH_MEMORY on Windows */
  48. #else
  49. # define RESOLVER_ENOMEM SOCKENOMEM
  50. #endif
  51. #include "urldata.h"
  52. #include "cfilters.h"
  53. #include "sendf.h"
  54. #include "hostip.h"
  55. #include "hash.h"
  56. #include "share.h"
  57. #include "url.h"
  58. #include "multiif.h"
  59. #include "curl_threads.h"
  60. #include "select.h"
  61. #include "strdup.h"
  62. #ifdef USE_ARES
  63. #include <ares.h>
  64. #ifdef USE_HTTPSRR
  65. #define USE_HTTPSRR_ARES /* the combo */
  66. #endif
  67. #endif
  68. /* The last 3 #include files should be in this order */
  69. #include "curl_printf.h"
  70. #include "curl_memory.h"
  71. #include "memdebug.h"
  72. /*
  73. * Curl_async_global_init()
  74. * Called from curl_global_init() to initialize global resolver environment.
  75. * Does nothing here.
  76. */
  77. int Curl_async_global_init(void)
  78. {
  79. #if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
  80. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  81. return CURLE_FAILED_INIT;
  82. }
  83. #endif
  84. return CURLE_OK;
  85. }
  86. /*
  87. * Curl_async_global_cleanup()
  88. * Called from curl_global_cleanup() to destroy global resolver environment.
  89. * Does nothing here.
  90. */
  91. void Curl_async_global_cleanup(void)
  92. {
  93. #if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
  94. ares_library_cleanup();
  95. #endif
  96. }
  97. static void async_thrdd_destroy(struct Curl_easy *);
  98. static void async_thrdd_shutdown(struct Curl_easy *);
  99. CURLcode Curl_async_get_impl(struct Curl_easy *data, void **impl)
  100. {
  101. (void)data;
  102. *impl = NULL;
  103. return CURLE_OK;
  104. }
  105. /* Give up reference to add_ctx */
  106. static void addr_ctx_unlink(struct async_thrdd_addr_ctx **paddr_ctx,
  107. struct Curl_easy *data)
  108. {
  109. struct async_thrdd_addr_ctx *addr_ctx = *paddr_ctx;
  110. bool destroy;
  111. (void)data;
  112. if(!addr_ctx)
  113. return;
  114. Curl_mutex_acquire(&addr_ctx->mutx);
  115. if(!data) /* called by resolving thread */
  116. addr_ctx->thrd_done = TRUE;
  117. DEBUGASSERT(addr_ctx->ref_count);
  118. --addr_ctx->ref_count;
  119. destroy = !addr_ctx->ref_count;
  120. Curl_mutex_release(&addr_ctx->mutx);
  121. if(destroy) {
  122. Curl_mutex_destroy(&addr_ctx->mutx);
  123. free(addr_ctx->hostname);
  124. if(addr_ctx->res)
  125. Curl_freeaddrinfo(addr_ctx->res);
  126. #ifndef CURL_DISABLE_SOCKETPAIR
  127. #ifndef USE_EVENTFD
  128. wakeup_close(addr_ctx->sock_pair[1]);
  129. #endif
  130. wakeup_close(addr_ctx->sock_pair[0]);
  131. #endif
  132. free(addr_ctx);
  133. }
  134. *paddr_ctx = NULL;
  135. }
  136. /* Initialize context for threaded resolver */
  137. static struct async_thrdd_addr_ctx *
  138. addr_ctx_create(struct Curl_easy *data,
  139. const char *hostname, int port,
  140. const struct addrinfo *hints)
  141. {
  142. struct async_thrdd_addr_ctx *addr_ctx = calloc(1, sizeof(*addr_ctx));
  143. if(!addr_ctx)
  144. return NULL;
  145. addr_ctx->thread_hnd = curl_thread_t_null;
  146. addr_ctx->port = port;
  147. addr_ctx->ref_count = 1;
  148. #ifdef HAVE_GETADDRINFO
  149. DEBUGASSERT(hints);
  150. addr_ctx->hints = *hints;
  151. #else
  152. (void)hints;
  153. #endif
  154. Curl_mutex_init(&addr_ctx->mutx);
  155. #ifndef CURL_DISABLE_SOCKETPAIR
  156. /* create socket pair or pipe */
  157. if(wakeup_create(addr_ctx->sock_pair, FALSE) < 0) {
  158. addr_ctx->sock_pair[0] = CURL_SOCKET_BAD;
  159. addr_ctx->sock_pair[1] = CURL_SOCKET_BAD;
  160. goto err_exit;
  161. }
  162. #endif
  163. addr_ctx->sock_error = 0;
  164. /* Copying hostname string because original can be destroyed by parent
  165. * thread during gethostbyname execution.
  166. */
  167. addr_ctx->hostname = strdup(hostname);
  168. if(!addr_ctx->hostname)
  169. goto err_exit;
  170. return addr_ctx;
  171. err_exit:
  172. addr_ctx_unlink(&addr_ctx, data);
  173. return NULL;
  174. }
  175. static void async_thrd_cleanup(void *arg)
  176. {
  177. struct async_thrdd_addr_ctx *addr_ctx = arg;
  178. Curl_thread_disable_cancel();
  179. addr_ctx_unlink(&addr_ctx, NULL);
  180. }
  181. #ifdef HAVE_GETADDRINFO
  182. /*
  183. * getaddrinfo_thread() resolves a name and then exits.
  184. *
  185. * For builds without ARES, but with USE_IPV6, create a resolver thread
  186. * and wait on it.
  187. */
  188. static CURL_THREAD_RETURN_T CURL_STDCALL getaddrinfo_thread(void *arg)
  189. {
  190. struct async_thrdd_addr_ctx *addr_ctx = arg;
  191. bool do_abort;
  192. /* clang complains about empty statements and the pthread_cleanup* macros
  193. * are pretty ill defined. */
  194. #if defined(__clang__)
  195. #pragma clang diagnostic push
  196. #pragma clang diagnostic ignored "-Wextra-semi-stmt"
  197. #endif
  198. Curl_thread_push_cleanup(async_thrd_cleanup, addr_ctx);
  199. Curl_mutex_acquire(&addr_ctx->mutx);
  200. do_abort = addr_ctx->do_abort;
  201. Curl_mutex_release(&addr_ctx->mutx);
  202. if(!do_abort) {
  203. char service[12];
  204. int rc;
  205. #ifdef DEBUGBUILD
  206. Curl_resolve_test_delay();
  207. #endif
  208. msnprintf(service, sizeof(service), "%d", addr_ctx->port);
  209. rc = Curl_getaddrinfo_ex(addr_ctx->hostname, service,
  210. &addr_ctx->hints, &addr_ctx->res);
  211. if(rc) {
  212. addr_ctx->sock_error = SOCKERRNO ? SOCKERRNO : rc;
  213. if(addr_ctx->sock_error == 0)
  214. addr_ctx->sock_error = RESOLVER_ENOMEM;
  215. }
  216. else {
  217. Curl_addrinfo_set_port(addr_ctx->res, addr_ctx->port);
  218. }
  219. Curl_mutex_acquire(&addr_ctx->mutx);
  220. do_abort = addr_ctx->do_abort;
  221. Curl_mutex_release(&addr_ctx->mutx);
  222. #ifndef CURL_DISABLE_SOCKETPAIR
  223. if(!do_abort) {
  224. #ifdef USE_EVENTFD
  225. const uint64_t buf[1] = { 1 };
  226. #else
  227. const char buf[1] = { 1 };
  228. #endif
  229. /* Thread is done, notify transfer */
  230. if(wakeup_write(addr_ctx->sock_pair[1], buf, sizeof(buf)) < 0) {
  231. /* update sock_error to errno */
  232. addr_ctx->sock_error = SOCKERRNO;
  233. }
  234. }
  235. #endif
  236. }
  237. Curl_thread_pop_cleanup();
  238. #if defined(__clang__)
  239. #pragma clang diagnostic pop
  240. #endif
  241. addr_ctx_unlink(&addr_ctx, NULL);
  242. return 0;
  243. }
  244. #else /* HAVE_GETADDRINFO */
  245. /*
  246. * gethostbyname_thread() resolves a name and then exits.
  247. */
  248. static CURL_THREAD_RETURN_T CURL_STDCALL gethostbyname_thread(void *arg)
  249. {
  250. struct async_thrdd_addr_ctx *addr_ctx = arg;
  251. bool do_abort;
  252. /* clang complains about empty statements and the pthread_cleanup* macros
  253. * are pretty ill defined. */
  254. #if defined(__clang__)
  255. #pragma clang diagnostic push
  256. #pragma clang diagnostic ignored "-Wextra-semi-stmt"
  257. #endif
  258. Curl_thread_push_cleanup(async_thrd_cleanup, addr_ctx);
  259. Curl_mutex_acquire(&addr_ctx->mutx);
  260. do_abort = addr_ctx->do_abort;
  261. Curl_mutex_release(&addr_ctx->mutx);
  262. if(!do_abort) {
  263. #ifdef DEBUGBUILD
  264. Curl_resolve_test_delay();
  265. #endif
  266. addr_ctx->res = Curl_ipv4_resolve_r(addr_ctx->hostname, addr_ctx->port);
  267. if(!addr_ctx->res) {
  268. addr_ctx->sock_error = SOCKERRNO;
  269. if(addr_ctx->sock_error == 0)
  270. addr_ctx->sock_error = RESOLVER_ENOMEM;
  271. }
  272. Curl_mutex_acquire(&addr_ctx->mutx);
  273. do_abort = addr_ctx->do_abort;
  274. Curl_mutex_release(&addr_ctx->mutx);
  275. #ifndef CURL_DISABLE_SOCKETPAIR
  276. if(!do_abort) {
  277. #ifdef USE_EVENTFD
  278. const uint64_t buf[1] = { 1 };
  279. #else
  280. const char buf[1] = { 1 };
  281. #endif
  282. /* Thread is done, notify transfer */
  283. if(wakeup_write(addr_ctx->sock_pair[1], buf, sizeof(buf)) < 0) {
  284. /* update sock_error to errno */
  285. addr_ctx->sock_error = SOCKERRNO;
  286. }
  287. }
  288. #endif
  289. }
  290. Curl_thread_pop_cleanup();
  291. #if defined(__clang__)
  292. #pragma clang diagnostic pop
  293. #endif
  294. async_thrd_cleanup(addr_ctx);
  295. return 0;
  296. }
  297. #endif /* HAVE_GETADDRINFO */
  298. /*
  299. * async_thrdd_destroy() cleans up async resolver data and thread handle.
  300. */
  301. static void async_thrdd_destroy(struct Curl_easy *data)
  302. {
  303. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  304. struct async_thrdd_addr_ctx *addr = thrdd->addr;
  305. #ifdef USE_HTTPSRR_ARES
  306. if(thrdd->rr.channel) {
  307. ares_destroy(thrdd->rr.channel);
  308. thrdd->rr.channel = NULL;
  309. }
  310. Curl_httpsrr_cleanup(&thrdd->rr.hinfo);
  311. #endif
  312. if(thrdd->addr && (thrdd->addr->thread_hnd != curl_thread_t_null)) {
  313. bool done;
  314. Curl_mutex_acquire(&addr->mutx);
  315. #ifndef CURL_DISABLE_SOCKETPAIR
  316. if(!addr->do_abort)
  317. Curl_multi_will_close(data, addr->sock_pair[0]);
  318. #endif
  319. addr->do_abort = TRUE;
  320. done = addr->thrd_done;
  321. Curl_mutex_release(&addr->mutx);
  322. if(done) {
  323. Curl_thread_join(&addr->thread_hnd);
  324. CURL_TRC_DNS(data, "async_thrdd_destroy, thread joined");
  325. }
  326. else {
  327. /* thread is still running. Detach the thread while mutexed, it will
  328. * trigger the cleanup when it releases its reference. */
  329. Curl_thread_destroy(&addr->thread_hnd);
  330. CURL_TRC_DNS(data, "async_thrdd_destroy, thread detached");
  331. }
  332. }
  333. addr_ctx_unlink(&thrdd->addr, data);
  334. }
  335. #ifdef USE_HTTPSRR_ARES
  336. static void async_thrdd_rr_done(void *user_data, ares_status_t status,
  337. size_t timeouts,
  338. const ares_dns_record_t *dnsrec)
  339. {
  340. struct Curl_easy *data = user_data;
  341. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  342. (void)timeouts;
  343. thrdd->rr.done = TRUE;
  344. if((ARES_SUCCESS != status) || !dnsrec)
  345. return;
  346. thrdd->rr.result = Curl_httpsrr_from_ares(data, dnsrec, &thrdd->rr.hinfo);
  347. }
  348. static CURLcode async_rr_start(struct Curl_easy *data)
  349. {
  350. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  351. int status;
  352. DEBUGASSERT(!thrdd->rr.channel);
  353. status = ares_init_options(&thrdd->rr.channel, NULL, 0);
  354. if(status != ARES_SUCCESS) {
  355. thrdd->rr.channel = NULL;
  356. return CURLE_FAILED_INIT;
  357. }
  358. #ifdef CURLDEBUG
  359. if(getenv("CURL_DNS_SERVER")) {
  360. const char *servers = getenv("CURL_DNS_SERVER");
  361. status = ares_set_servers_ports_csv(thrdd->rr.channel, servers);
  362. if(status)
  363. return CURLE_FAILED_INIT;
  364. }
  365. #endif
  366. memset(&thrdd->rr.hinfo, 0, sizeof(thrdd->rr.hinfo));
  367. thrdd->rr.hinfo.port = -1;
  368. ares_query_dnsrec(thrdd->rr.channel,
  369. data->conn->host.name, ARES_CLASS_IN,
  370. ARES_REC_TYPE_HTTPS,
  371. async_thrdd_rr_done, data, NULL);
  372. CURL_TRC_DNS(data, "Issued HTTPS-RR request for %s", data->conn->host.name);
  373. return CURLE_OK;
  374. }
  375. #endif
  376. /*
  377. * async_thrdd_init() starts a new thread that performs the actual
  378. * resolve. This function returns before the resolve is done.
  379. *
  380. * Returns FALSE in case of failure, otherwise TRUE.
  381. */
  382. static bool async_thrdd_init(struct Curl_easy *data,
  383. const char *hostname, int port, int ip_version,
  384. const struct addrinfo *hints)
  385. {
  386. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  387. struct async_thrdd_addr_ctx *addr_ctx;
  388. /* !checksrc! disable ERRNOVAR 1 */
  389. int err = ENOMEM;
  390. if(thrdd->addr
  391. #ifdef USE_HTTPSRR_ARES
  392. || thrdd->rr.channel
  393. #endif
  394. ) {
  395. CURL_TRC_DNS(data, "starting new resolve, with previous not cleaned up");
  396. async_thrdd_destroy(data);
  397. DEBUGASSERT(!thrdd->addr);
  398. #ifdef USE_HTTPSRR_ARES
  399. DEBUGASSERT(!thrdd->rr.channel);
  400. #endif
  401. }
  402. data->state.async.dns = NULL;
  403. data->state.async.done = FALSE;
  404. data->state.async.port = port;
  405. data->state.async.ip_version = ip_version;
  406. free(data->state.async.hostname);
  407. data->state.async.hostname = strdup(hostname);
  408. if(!data->state.async.hostname)
  409. goto err_exit;
  410. addr_ctx = addr_ctx_create(data, hostname, port, hints);
  411. if(!addr_ctx)
  412. goto err_exit;
  413. thrdd->addr = addr_ctx;
  414. /* passing addr_ctx to the thread adds a reference */
  415. addr_ctx->ref_count = 2;
  416. addr_ctx->start = curlx_now();
  417. #ifdef HAVE_GETADDRINFO
  418. addr_ctx->thread_hnd = Curl_thread_create(getaddrinfo_thread, addr_ctx);
  419. #else
  420. addr_ctx->thread_hnd = Curl_thread_create(gethostbyname_thread, addr_ctx);
  421. #endif
  422. if(addr_ctx->thread_hnd == curl_thread_t_null) {
  423. /* The thread never started */
  424. addr_ctx->ref_count = 1;
  425. addr_ctx->thrd_done = TRUE;
  426. err = errno;
  427. goto err_exit;
  428. }
  429. #ifdef USE_HTTPSRR_ARES
  430. if(async_rr_start(data))
  431. infof(data, "Failed HTTPS RR operation");
  432. #endif
  433. CURL_TRC_DNS(data, "resolve thread started for of %s:%d", hostname, port);
  434. return TRUE;
  435. err_exit:
  436. CURL_TRC_DNS(data, "resolve thread failed init: %d", err);
  437. async_thrdd_destroy(data);
  438. CURL_SETERRNO(err);
  439. return FALSE;
  440. }
  441. static void async_thrdd_shutdown(struct Curl_easy *data)
  442. {
  443. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  444. struct async_thrdd_addr_ctx *addr_ctx = thrdd->addr;
  445. bool done;
  446. if(!addr_ctx)
  447. return;
  448. if(addr_ctx->thread_hnd == curl_thread_t_null)
  449. return;
  450. Curl_mutex_acquire(&addr_ctx->mutx);
  451. #ifndef CURL_DISABLE_SOCKETPAIR
  452. if(!addr_ctx->do_abort)
  453. Curl_multi_will_close(data, addr_ctx->sock_pair[0]);
  454. #endif
  455. addr_ctx->do_abort = TRUE;
  456. done = addr_ctx->thrd_done;
  457. Curl_mutex_release(&addr_ctx->mutx);
  458. DEBUGASSERT(addr_ctx->thread_hnd != curl_thread_t_null);
  459. if(!done && (addr_ctx->thread_hnd != curl_thread_t_null)) {
  460. CURL_TRC_DNS(data, "cancelling resolve thread");
  461. (void)Curl_thread_cancel(&addr_ctx->thread_hnd);
  462. }
  463. }
  464. /*
  465. * 'entry' may be NULL and then no data is returned
  466. */
  467. static CURLcode asyn_thrdd_await(struct Curl_easy *data,
  468. struct async_thrdd_addr_ctx *addr_ctx,
  469. struct Curl_dns_entry **entry)
  470. {
  471. CURLcode result = CURLE_OK;
  472. if(addr_ctx->thread_hnd != curl_thread_t_null) {
  473. /* not interested in result? cancel, if still running... */
  474. if(!entry)
  475. async_thrdd_shutdown(data);
  476. CURL_TRC_DNS(data, "resolve, wait for thread to finish");
  477. if(!Curl_thread_join(&addr_ctx->thread_hnd)) {
  478. DEBUGASSERT(0);
  479. }
  480. if(entry)
  481. result = Curl_async_is_resolved(data, entry);
  482. }
  483. data->state.async.done = TRUE;
  484. if(entry)
  485. *entry = data->state.async.dns;
  486. return result;
  487. }
  488. /*
  489. * Until we gain a way to signal the resolver threads to stop early, we must
  490. * simply wait for them and ignore their results.
  491. */
  492. void Curl_async_thrdd_shutdown(struct Curl_easy *data)
  493. {
  494. async_thrdd_shutdown(data);
  495. }
  496. void Curl_async_thrdd_destroy(struct Curl_easy *data)
  497. {
  498. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  499. if(thrdd->addr && !data->set.quick_exit) {
  500. (void)asyn_thrdd_await(data, thrdd->addr, NULL);
  501. }
  502. async_thrdd_destroy(data);
  503. }
  504. /*
  505. * Curl_async_await()
  506. *
  507. * Waits for a resolve to finish. This function should be avoided since using
  508. * this risk getting the multi interface to "hang".
  509. *
  510. * If 'entry' is non-NULL, make it point to the resolved dns entry
  511. *
  512. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  513. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  514. *
  515. * This is the version for resolves-in-a-thread.
  516. */
  517. CURLcode Curl_async_await(struct Curl_easy *data,
  518. struct Curl_dns_entry **entry)
  519. {
  520. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  521. if(thrdd->addr)
  522. return asyn_thrdd_await(data, thrdd->addr, entry);
  523. return CURLE_FAILED_INIT;
  524. }
  525. /*
  526. * Curl_async_is_resolved() is called repeatedly to check if a previous
  527. * name resolve request has completed. It should also make sure to time-out if
  528. * the operation seems to take too long.
  529. */
  530. CURLcode Curl_async_is_resolved(struct Curl_easy *data,
  531. struct Curl_dns_entry **dns)
  532. {
  533. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  534. bool done = FALSE;
  535. DEBUGASSERT(dns);
  536. *dns = NULL;
  537. if(data->state.async.done) {
  538. *dns = data->state.async.dns;
  539. CURL_TRC_DNS(data, "threaded: is_resolved(), already done, dns=%sfound",
  540. *dns ? "" : "not ");
  541. return CURLE_OK;
  542. }
  543. #ifdef USE_HTTPSRR_ARES
  544. /* best effort, ignore errors */
  545. if(thrdd->rr.channel)
  546. (void)Curl_ares_perform(thrdd->rr.channel, 0);
  547. #endif
  548. DEBUGASSERT(thrdd->addr);
  549. if(!thrdd->addr)
  550. return CURLE_FAILED_INIT;
  551. Curl_mutex_acquire(&thrdd->addr->mutx);
  552. done = thrdd->addr->thrd_done;
  553. Curl_mutex_release(&thrdd->addr->mutx);
  554. if(done) {
  555. CURLcode result = CURLE_OK;
  556. data->state.async.done = TRUE;
  557. Curl_resolv_unlink(data, &data->state.async.dns);
  558. if(thrdd->addr->res) {
  559. data->state.async.dns =
  560. Curl_dnscache_mk_entry(data, thrdd->addr->res,
  561. data->state.async.hostname, 0,
  562. data->state.async.port, FALSE);
  563. thrdd->addr->res = NULL;
  564. if(!data->state.async.dns)
  565. result = CURLE_OUT_OF_MEMORY;
  566. #ifdef USE_HTTPSRR_ARES
  567. if(thrdd->rr.channel) {
  568. result = thrdd->rr.result;
  569. if(!result) {
  570. struct Curl_https_rrinfo *lhrr;
  571. lhrr = Curl_httpsrr_dup_move(&thrdd->rr.hinfo);
  572. if(!lhrr)
  573. result = CURLE_OUT_OF_MEMORY;
  574. else
  575. data->state.async.dns->hinfo = lhrr;
  576. }
  577. }
  578. #endif
  579. if(!result && data->state.async.dns)
  580. result = Curl_dnscache_add(data, data->state.async.dns);
  581. }
  582. if(!result && !data->state.async.dns)
  583. result = Curl_resolver_error(data, NULL);
  584. if(result)
  585. Curl_resolv_unlink(data, &data->state.async.dns);
  586. *dns = data->state.async.dns;
  587. CURL_TRC_DNS(data, "is_resolved() result=%d, dns=%sfound",
  588. result, *dns ? "" : "not ");
  589. async_thrdd_shutdown(data);
  590. return result;
  591. }
  592. else {
  593. /* poll for name lookup done with exponential backoff up to 250ms */
  594. /* should be fine even if this converts to 32-bit */
  595. timediff_t elapsed = curlx_timediff(curlx_now(),
  596. data->progress.t_startsingle);
  597. if(elapsed < 0)
  598. elapsed = 0;
  599. if(thrdd->addr->poll_interval == 0)
  600. /* Start at 1ms poll interval */
  601. thrdd->addr->poll_interval = 1;
  602. else if(elapsed >= thrdd->addr->interval_end)
  603. /* Back-off exponentially if last interval expired */
  604. thrdd->addr->poll_interval *= 2;
  605. if(thrdd->addr->poll_interval > 250)
  606. thrdd->addr->poll_interval = 250;
  607. thrdd->addr->interval_end = elapsed + thrdd->addr->poll_interval;
  608. Curl_expire(data, thrdd->addr->poll_interval, EXPIRE_ASYNC_NAME);
  609. return CURLE_OK;
  610. }
  611. }
  612. CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps)
  613. {
  614. struct async_thrdd_ctx *thrdd = &data->state.async.thrdd;
  615. CURLcode result = CURLE_OK;
  616. bool thrd_done;
  617. #if !defined(USE_HTTPSRR_ARES) && defined(CURL_DISABLE_SOCKETPAIR)
  618. (void)ps;
  619. #endif
  620. #ifdef USE_HTTPSRR_ARES
  621. if(thrdd->rr.channel) {
  622. result = Curl_ares_pollset(data, thrdd->rr.channel, ps);
  623. if(result)
  624. return result;
  625. }
  626. #endif
  627. if(!thrdd->addr)
  628. return result;
  629. Curl_mutex_acquire(&thrdd->addr->mutx);
  630. thrd_done = thrdd->addr->thrd_done;
  631. Curl_mutex_release(&thrdd->addr->mutx);
  632. if(!thrd_done) {
  633. #ifndef CURL_DISABLE_SOCKETPAIR
  634. /* return read fd to client for polling the DNS resolution status */
  635. result = Curl_pollset_add_in(data, ps, thrdd->addr->sock_pair[0]);
  636. #else
  637. timediff_t milli;
  638. timediff_t ms = curlx_timediff(curlx_now(), thrdd->addr->start);
  639. if(ms < 3)
  640. milli = 0;
  641. else if(ms <= 50)
  642. milli = ms/3;
  643. else if(ms <= 250)
  644. milli = 50;
  645. else
  646. milli = 200;
  647. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  648. #endif
  649. }
  650. return result;
  651. }
  652. #ifndef HAVE_GETADDRINFO
  653. /*
  654. * Curl_async_getaddrinfo() - for platforms without getaddrinfo
  655. */
  656. struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,
  657. const char *hostname,
  658. int port,
  659. int ip_version,
  660. int *waitp)
  661. {
  662. (void)ip_version;
  663. *waitp = 0; /* default to synchronous response */
  664. /* fire up a new resolver thread! */
  665. if(async_thrdd_init(data, hostname, port, ip_version, NULL)) {
  666. *waitp = 1; /* expect asynchronous response */
  667. return NULL;
  668. }
  669. failf(data, "getaddrinfo() thread failed");
  670. return NULL;
  671. }
  672. #else /* !HAVE_GETADDRINFO */
  673. /*
  674. * Curl_async_getaddrinfo() - for getaddrinfo
  675. */
  676. struct Curl_addrinfo *Curl_async_getaddrinfo(struct Curl_easy *data,
  677. const char *hostname,
  678. int port,
  679. int ip_version,
  680. int *waitp)
  681. {
  682. struct addrinfo hints;
  683. int pf = PF_INET;
  684. *waitp = 0; /* default to synchronous response */
  685. CURL_TRC_DNS(data, "init threaded resolve of %s:%d", hostname, port);
  686. #ifdef CURLRES_IPV6
  687. if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  688. /* The stack seems to be IPv6-enabled */
  689. if(ip_version == CURL_IPRESOLVE_V6)
  690. pf = PF_INET6;
  691. else
  692. pf = PF_UNSPEC;
  693. }
  694. #else
  695. (void)ip_version;
  696. #endif /* CURLRES_IPV6 */
  697. memset(&hints, 0, sizeof(hints));
  698. hints.ai_family = pf;
  699. hints.ai_socktype =
  700. (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ?
  701. SOCK_STREAM : SOCK_DGRAM;
  702. /* fire up a new resolver thread! */
  703. if(async_thrdd_init(data, hostname, port, ip_version, &hints)) {
  704. *waitp = 1; /* expect asynchronous response */
  705. return NULL;
  706. }
  707. failf(data, "getaddrinfo() thread failed to start");
  708. return NULL;
  709. }
  710. #endif /* !HAVE_GETADDRINFO */
  711. #endif /* CURLRES_THREADED */