asyn-thrdd.c 21 KB

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