asyn-thread.c 17 KB

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