asyn-thrdd.c 21 KB

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