asyn-thread.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. /***********************************************************************
  24. * Only for threaded name resolves builds
  25. **********************************************************************/
  26. #ifdef CURLRES_THREADED
  27. #ifdef HAVE_NETINET_IN_H
  28. #include <netinet/in.h>
  29. #endif
  30. #ifdef HAVE_NETDB_H
  31. #include <netdb.h>
  32. #endif
  33. #ifdef HAVE_ARPA_INET_H
  34. #include <arpa/inet.h>
  35. #endif
  36. #ifdef __VMS
  37. #include <in.h>
  38. #include <inet.h>
  39. #endif
  40. #if defined(USE_THREADS_POSIX)
  41. # ifdef HAVE_PTHREAD_H
  42. # include <pthread.h>
  43. # endif
  44. #elif defined(USE_THREADS_WIN32)
  45. # ifdef HAVE_PROCESS_H
  46. # include <process.h>
  47. # endif
  48. #endif
  49. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  50. #undef in_addr_t
  51. #define in_addr_t unsigned long
  52. #endif
  53. #ifdef HAVE_GETADDRINFO
  54. # define RESOLVER_ENOMEM EAI_MEMORY
  55. #else
  56. # define RESOLVER_ENOMEM ENOMEM
  57. #endif
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "hostip.h"
  61. #include "hash.h"
  62. #include "share.h"
  63. #include "strerror.h"
  64. #include "url.h"
  65. #include "multiif.h"
  66. #include "inet_pton.h"
  67. #include "inet_ntop.h"
  68. #include "curl_threads.h"
  69. #include "connect.h"
  70. /* The last 3 #include files should be in this order */
  71. #include "curl_printf.h"
  72. #include "curl_memory.h"
  73. #include "memdebug.h"
  74. /*
  75. * Curl_resolver_global_init()
  76. * Called from curl_global_init() to initialize global resolver environment.
  77. * Does nothing here.
  78. */
  79. int Curl_resolver_global_init(void)
  80. {
  81. return CURLE_OK;
  82. }
  83. /*
  84. * Curl_resolver_global_cleanup()
  85. * Called from curl_global_cleanup() to destroy global resolver environment.
  86. * Does nothing here.
  87. */
  88. void Curl_resolver_global_cleanup(void)
  89. {
  90. }
  91. /*
  92. * Curl_resolver_init()
  93. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  94. * URL-state specific environment ('resolver' member of the UrlState
  95. * structure). Does nothing here.
  96. */
  97. CURLcode Curl_resolver_init(void **resolver)
  98. {
  99. (void)resolver;
  100. return CURLE_OK;
  101. }
  102. /*
  103. * Curl_resolver_cleanup()
  104. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  105. * URL-state specific environment ('resolver' member of the UrlState
  106. * structure). Does nothing here.
  107. */
  108. void Curl_resolver_cleanup(void *resolver)
  109. {
  110. (void)resolver;
  111. }
  112. /*
  113. * Curl_resolver_duphandle()
  114. * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
  115. * environment ('resolver' member of the UrlState structure). Does nothing
  116. * here.
  117. */
  118. int Curl_resolver_duphandle(void **to, void *from)
  119. {
  120. (void)to;
  121. (void)from;
  122. return CURLE_OK;
  123. }
  124. static void destroy_async_data(struct Curl_async *);
  125. /*
  126. * Cancel all possibly still on-going resolves for this connection.
  127. */
  128. void Curl_resolver_cancel(struct connectdata *conn)
  129. {
  130. destroy_async_data(&conn->async);
  131. }
  132. /* This function is used to init a threaded resolve */
  133. static bool init_resolve_thread(struct connectdata *conn,
  134. const char *hostname, int port,
  135. const struct addrinfo *hints);
  136. /* Data for synchronization between resolver thread and its parent */
  137. struct thread_sync_data {
  138. curl_mutex_t * mtx;
  139. int done;
  140. char *hostname; /* hostname to resolve, Curl_async.hostname
  141. duplicate */
  142. int port;
  143. int sock_error;
  144. Curl_addrinfo *res;
  145. #ifdef HAVE_GETADDRINFO
  146. struct addrinfo hints;
  147. #endif
  148. struct thread_data *td; /* for thread-self cleanup */
  149. };
  150. struct thread_data {
  151. curl_thread_t thread_hnd;
  152. unsigned int poll_interval;
  153. time_t interval_end;
  154. struct thread_sync_data tsd;
  155. };
  156. static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
  157. {
  158. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  159. }
  160. #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
  161. /* Destroy resolver thread synchronization data */
  162. static
  163. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  164. {
  165. if(tsd->mtx) {
  166. Curl_mutex_destroy(tsd->mtx);
  167. free(tsd->mtx);
  168. }
  169. free(tsd->hostname);
  170. if(tsd->res)
  171. Curl_freeaddrinfo(tsd->res);
  172. memset(tsd, 0, sizeof(*tsd));
  173. }
  174. /* Initialize resolver thread synchronization data */
  175. static
  176. int init_thread_sync_data(struct thread_data * td,
  177. const char *hostname,
  178. int port,
  179. const struct addrinfo *hints)
  180. {
  181. struct thread_sync_data *tsd = &td->tsd;
  182. memset(tsd, 0, sizeof(*tsd));
  183. tsd->td = td;
  184. tsd->port = port;
  185. /* Treat the request as done until the thread actually starts so any early
  186. * cleanup gets done properly.
  187. */
  188. tsd->done = 1;
  189. #ifdef HAVE_GETADDRINFO
  190. DEBUGASSERT(hints);
  191. tsd->hints = *hints;
  192. #else
  193. (void) hints;
  194. #endif
  195. tsd->mtx = malloc(sizeof(curl_mutex_t));
  196. if(tsd->mtx == NULL)
  197. goto err_exit;
  198. Curl_mutex_init(tsd->mtx);
  199. tsd->sock_error = CURL_ASYNC_SUCCESS;
  200. /* Copying hostname string because original can be destroyed by parent
  201. * thread during gethostbyname execution.
  202. */
  203. tsd->hostname = strdup(hostname);
  204. if(!tsd->hostname)
  205. goto err_exit;
  206. return 1;
  207. err_exit:
  208. /* Memory allocation failed */
  209. destroy_thread_sync_data(tsd);
  210. return 0;
  211. }
  212. static int getaddrinfo_complete(struct connectdata *conn)
  213. {
  214. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  215. int rc;
  216. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  217. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  218. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  219. */
  220. tsd->res = NULL;
  221. return rc;
  222. }
  223. #ifdef HAVE_GETADDRINFO
  224. /*
  225. * getaddrinfo_thread() resolves a name and then exits.
  226. *
  227. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  228. * and wait on it.
  229. */
  230. static unsigned int CURL_STDCALL getaddrinfo_thread(void *arg)
  231. {
  232. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  233. struct thread_data *td = tsd->td;
  234. char service[12];
  235. int rc;
  236. snprintf(service, sizeof(service), "%d", tsd->port);
  237. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  238. if(rc != 0) {
  239. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  240. if(tsd->sock_error == 0)
  241. tsd->sock_error = RESOLVER_ENOMEM;
  242. }
  243. else {
  244. Curl_addrinfo_set_port(tsd->res, tsd->port);
  245. }
  246. Curl_mutex_acquire(tsd->mtx);
  247. if(tsd->done) {
  248. /* too late, gotta clean up the mess */
  249. Curl_mutex_release(tsd->mtx);
  250. destroy_thread_sync_data(tsd);
  251. free(td);
  252. }
  253. else {
  254. tsd->done = 1;
  255. Curl_mutex_release(tsd->mtx);
  256. }
  257. return 0;
  258. }
  259. #else /* HAVE_GETADDRINFO */
  260. /*
  261. * gethostbyname_thread() resolves a name and then exits.
  262. */
  263. static unsigned int CURL_STDCALL gethostbyname_thread(void *arg)
  264. {
  265. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  266. struct thread_data *td = tsd->td;
  267. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  268. if(!tsd->res) {
  269. tsd->sock_error = SOCKERRNO;
  270. if(tsd->sock_error == 0)
  271. tsd->sock_error = RESOLVER_ENOMEM;
  272. }
  273. Curl_mutex_acquire(tsd->mtx);
  274. if(tsd->done) {
  275. /* too late, gotta clean up the mess */
  276. Curl_mutex_release(tsd->mtx);
  277. destroy_thread_sync_data(tsd);
  278. free(td);
  279. }
  280. else {
  281. tsd->done = 1;
  282. Curl_mutex_release(tsd->mtx);
  283. }
  284. return 0;
  285. }
  286. #endif /* HAVE_GETADDRINFO */
  287. /*
  288. * destroy_async_data() cleans up async resolver data and thread handle.
  289. */
  290. static void destroy_async_data(struct Curl_async *async)
  291. {
  292. if(async->os_specific) {
  293. struct thread_data *td = (struct thread_data*) async->os_specific;
  294. int done;
  295. /*
  296. * if the thread is still blocking in the resolve syscall, detach it and
  297. * let the thread do the cleanup...
  298. */
  299. Curl_mutex_acquire(td->tsd.mtx);
  300. done = td->tsd.done;
  301. td->tsd.done = 1;
  302. Curl_mutex_release(td->tsd.mtx);
  303. if(!done) {
  304. Curl_thread_destroy(td->thread_hnd);
  305. }
  306. else {
  307. if(td->thread_hnd != curl_thread_t_null)
  308. Curl_thread_join(&td->thread_hnd);
  309. destroy_thread_sync_data(&td->tsd);
  310. free(async->os_specific);
  311. }
  312. }
  313. async->os_specific = NULL;
  314. free(async->hostname);
  315. async->hostname = NULL;
  316. }
  317. /*
  318. * init_resolve_thread() starts a new thread that performs the actual
  319. * resolve. This function returns before the resolve is done.
  320. *
  321. * Returns FALSE in case of failure, otherwise TRUE.
  322. */
  323. static bool init_resolve_thread(struct connectdata *conn,
  324. const char *hostname, int port,
  325. const struct addrinfo *hints)
  326. {
  327. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  328. int err = ENOMEM;
  329. conn->async.os_specific = (void *)td;
  330. if(!td)
  331. goto errno_exit;
  332. conn->async.port = port;
  333. conn->async.done = FALSE;
  334. conn->async.status = 0;
  335. conn->async.dns = NULL;
  336. td->thread_hnd = curl_thread_t_null;
  337. if(!init_thread_sync_data(td, hostname, port, hints)) {
  338. conn->async.os_specific = NULL;
  339. free(td);
  340. goto errno_exit;
  341. }
  342. free(conn->async.hostname);
  343. conn->async.hostname = strdup(hostname);
  344. if(!conn->async.hostname)
  345. goto err_exit;
  346. /* The thread will set this to 1 when complete. */
  347. td->tsd.done = 0;
  348. #ifdef HAVE_GETADDRINFO
  349. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  350. #else
  351. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  352. #endif
  353. if(!td->thread_hnd) {
  354. /* The thread never started, so mark it as done here for proper cleanup. */
  355. td->tsd.done = 1;
  356. err = errno;
  357. goto err_exit;
  358. }
  359. return TRUE;
  360. err_exit:
  361. destroy_async_data(&conn->async);
  362. errno_exit:
  363. errno = err;
  364. return FALSE;
  365. }
  366. /*
  367. * resolver_error() calls failf() with the appropriate message after a resolve
  368. * error
  369. */
  370. static CURLcode resolver_error(struct connectdata *conn)
  371. {
  372. const char *host_or_proxy;
  373. CURLcode result;
  374. if(conn->bits.httpproxy) {
  375. host_or_proxy = "proxy";
  376. result = CURLE_COULDNT_RESOLVE_PROXY;
  377. }
  378. else {
  379. host_or_proxy = "host";
  380. result = CURLE_COULDNT_RESOLVE_HOST;
  381. }
  382. failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
  383. conn->async.hostname);
  384. return result;
  385. }
  386. /*
  387. * Curl_resolver_wait_resolv()
  388. *
  389. * waits for a resolve to finish. This function should be avoided since using
  390. * this risk getting the multi interface to "hang".
  391. *
  392. * If 'entry' is non-NULL, make it point to the resolved dns entry
  393. *
  394. * This is the version for resolves-in-a-thread.
  395. */
  396. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  397. struct Curl_dns_entry **entry)
  398. {
  399. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  400. CURLcode result = CURLE_OK;
  401. DEBUGASSERT(conn && td);
  402. /* wait for the thread to resolve the name */
  403. if(Curl_thread_join(&td->thread_hnd))
  404. result = getaddrinfo_complete(conn);
  405. else
  406. DEBUGASSERT(0);
  407. conn->async.done = TRUE;
  408. if(entry)
  409. *entry = conn->async.dns;
  410. if(!conn->async.dns)
  411. /* a name was not resolved, report error */
  412. result = resolver_error(conn);
  413. destroy_async_data(&conn->async);
  414. if(!conn->async.dns)
  415. connclose(conn, "asynch resolve failed");
  416. return result;
  417. }
  418. /*
  419. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  420. * name resolve request has completed. It should also make sure to time-out if
  421. * the operation seems to take too long.
  422. */
  423. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  424. struct Curl_dns_entry **entry)
  425. {
  426. struct Curl_easy *data = conn->data;
  427. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  428. int done = 0;
  429. *entry = NULL;
  430. if(!td) {
  431. DEBUGASSERT(td);
  432. return CURLE_COULDNT_RESOLVE_HOST;
  433. }
  434. Curl_mutex_acquire(td->tsd.mtx);
  435. done = td->tsd.done;
  436. Curl_mutex_release(td->tsd.mtx);
  437. if(done) {
  438. getaddrinfo_complete(conn);
  439. if(!conn->async.dns) {
  440. CURLcode result = resolver_error(conn);
  441. destroy_async_data(&conn->async);
  442. return result;
  443. }
  444. destroy_async_data(&conn->async);
  445. *entry = conn->async.dns;
  446. }
  447. else {
  448. /* poll for name lookup done with exponential backoff up to 250ms */
  449. timediff_t elapsed = Curl_timediff(Curl_now(),
  450. data->progress.t_startsingle);
  451. if(elapsed < 0)
  452. elapsed = 0;
  453. if(td->poll_interval == 0)
  454. /* Start at 1ms poll interval */
  455. td->poll_interval = 1;
  456. else if(elapsed >= td->interval_end)
  457. /* Back-off exponentially if last interval expired */
  458. td->poll_interval *= 2;
  459. if(td->poll_interval > 250)
  460. td->poll_interval = 250;
  461. td->interval_end = elapsed + td->poll_interval;
  462. Curl_expire(conn->data, td->poll_interval, EXPIRE_ASYNC_NAME);
  463. }
  464. return CURLE_OK;
  465. }
  466. int Curl_resolver_getsock(struct connectdata *conn,
  467. curl_socket_t *socks,
  468. int numsocks)
  469. {
  470. (void)conn;
  471. (void)socks;
  472. (void)numsocks;
  473. return 0;
  474. }
  475. #ifndef HAVE_GETADDRINFO
  476. /*
  477. * Curl_getaddrinfo() - for platforms without getaddrinfo
  478. */
  479. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  480. const char *hostname,
  481. int port,
  482. int *waitp)
  483. {
  484. struct in_addr in;
  485. *waitp = 0; /* default to synchronous response */
  486. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  487. /* This is a dotted IP address 123.123.123.123-style */
  488. return Curl_ip2addr(AF_INET, &in, hostname, port);
  489. /* fire up a new resolver thread! */
  490. if(init_resolve_thread(conn, hostname, port, NULL)) {
  491. *waitp = 1; /* expect asynchronous response */
  492. return NULL;
  493. }
  494. /* fall-back to blocking version */
  495. return Curl_ipv4_resolve_r(hostname, port);
  496. }
  497. #else /* !HAVE_GETADDRINFO */
  498. /*
  499. * Curl_resolver_getaddrinfo() - for getaddrinfo
  500. */
  501. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  502. const char *hostname,
  503. int port,
  504. int *waitp)
  505. {
  506. struct addrinfo hints;
  507. Curl_addrinfo *res;
  508. int error;
  509. char sbuf[12];
  510. int pf = PF_INET;
  511. *waitp = 0; /* default to synchronous response */
  512. #ifndef USE_RESOLVE_ON_IPS
  513. {
  514. struct in_addr in;
  515. /* First check if this is an IPv4 address string */
  516. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  517. /* This is a dotted IP address 123.123.123.123-style */
  518. return Curl_ip2addr(AF_INET, &in, hostname, port);
  519. }
  520. #ifdef CURLRES_IPV6
  521. {
  522. struct in6_addr in6;
  523. /* check if this is an IPv6 address string */
  524. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0)
  525. /* This is an IPv6 address literal */
  526. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  527. }
  528. #endif /* CURLRES_IPV6 */
  529. #endif /* !USE_RESOLVE_ON_IPS */
  530. #ifdef CURLRES_IPV6
  531. /*
  532. * Check if a limited name resolve has been requested.
  533. */
  534. switch(conn->ip_version) {
  535. case CURL_IPRESOLVE_V4:
  536. pf = PF_INET;
  537. break;
  538. case CURL_IPRESOLVE_V6:
  539. pf = PF_INET6;
  540. break;
  541. default:
  542. pf = PF_UNSPEC;
  543. break;
  544. }
  545. if((pf != PF_INET) && !Curl_ipv6works())
  546. /* The stack seems to be a non-IPv6 one */
  547. pf = PF_INET;
  548. #endif /* CURLRES_IPV6 */
  549. memset(&hints, 0, sizeof(hints));
  550. hints.ai_family = pf;
  551. hints.ai_socktype = conn->socktype;
  552. snprintf(sbuf, sizeof(sbuf), "%d", port);
  553. /* fire up a new resolver thread! */
  554. if(init_resolve_thread(conn, hostname, port, &hints)) {
  555. *waitp = 1; /* expect asynchronous response */
  556. return NULL;
  557. }
  558. /* fall-back to blocking version */
  559. infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
  560. hostname, Curl_strerror(conn, errno));
  561. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  562. if(error) {
  563. infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
  564. hostname, port, Curl_strerror(conn, SOCKERRNO));
  565. return NULL;
  566. }
  567. else {
  568. Curl_addrinfo_set_port(res, port);
  569. }
  570. return res;
  571. }
  572. #endif /* !HAVE_GETADDRINFO */
  573. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  574. char *servers)
  575. {
  576. (void)data;
  577. (void)servers;
  578. return CURLE_NOT_BUILT_IN;
  579. }
  580. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  581. const char *interf)
  582. {
  583. (void)data;
  584. (void)interf;
  585. return CURLE_NOT_BUILT_IN;
  586. }
  587. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  588. const char *local_ip4)
  589. {
  590. (void)data;
  591. (void)local_ip4;
  592. return CURLE_NOT_BUILT_IN;
  593. }
  594. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  595. const char *local_ip6)
  596. {
  597. (void)data;
  598. (void)local_ip6;
  599. return CURLE_NOT_BUILT_IN;
  600. }
  601. #endif /* CURLRES_THREADED */