asyn-thread.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://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. #include "curl_printf.h"
  67. #include "curl_memory.h"
  68. /* The last #include file should be: */
  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. long 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. Curl_mutex_acquire(tsd->mtx);
  240. if(tsd->done) {
  241. /* too late, gotta clean up the mess */
  242. Curl_mutex_release(tsd->mtx);
  243. destroy_thread_sync_data(tsd);
  244. free(td);
  245. }
  246. else {
  247. tsd->done = 1;
  248. Curl_mutex_release(tsd->mtx);
  249. }
  250. return 0;
  251. }
  252. #else /* HAVE_GETADDRINFO */
  253. /*
  254. * gethostbyname_thread() resolves a name and then exits.
  255. */
  256. static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
  257. {
  258. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  259. struct thread_data *td = tsd->td;
  260. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  261. if(!tsd->res) {
  262. tsd->sock_error = SOCKERRNO;
  263. if(tsd->sock_error == 0)
  264. tsd->sock_error = RESOLVER_ENOMEM;
  265. }
  266. Curl_mutex_acquire(tsd->mtx);
  267. if(tsd->done) {
  268. /* too late, gotta clean up the mess */
  269. Curl_mutex_release(tsd->mtx);
  270. destroy_thread_sync_data(tsd);
  271. free(td);
  272. }
  273. else {
  274. tsd->done = 1;
  275. Curl_mutex_release(tsd->mtx);
  276. }
  277. return 0;
  278. }
  279. #endif /* HAVE_GETADDRINFO */
  280. /*
  281. * destroy_async_data() cleans up async resolver data and thread handle.
  282. */
  283. static void destroy_async_data (struct Curl_async *async)
  284. {
  285. if(async->os_specific) {
  286. struct thread_data *td = (struct thread_data*) async->os_specific;
  287. int done;
  288. /*
  289. * if the thread is still blocking in the resolve syscall, detach it and
  290. * let the thread do the cleanup...
  291. */
  292. Curl_mutex_acquire(td->tsd.mtx);
  293. done = td->tsd.done;
  294. td->tsd.done = 1;
  295. Curl_mutex_release(td->tsd.mtx);
  296. if(!done) {
  297. Curl_thread_destroy(td->thread_hnd);
  298. }
  299. else {
  300. if(td->thread_hnd != curl_thread_t_null)
  301. Curl_thread_join(&td->thread_hnd);
  302. destroy_thread_sync_data(&td->tsd);
  303. free(async->os_specific);
  304. }
  305. }
  306. async->os_specific = NULL;
  307. free(async->hostname);
  308. async->hostname = NULL;
  309. }
  310. /*
  311. * init_resolve_thread() starts a new thread that performs the actual
  312. * resolve. This function returns before the resolve is done.
  313. *
  314. * Returns FALSE in case of failure, otherwise TRUE.
  315. */
  316. static bool init_resolve_thread (struct connectdata *conn,
  317. const char *hostname, int port,
  318. const struct addrinfo *hints)
  319. {
  320. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  321. int err = RESOLVER_ENOMEM;
  322. conn->async.os_specific = (void*) td;
  323. if(!td)
  324. goto err_exit;
  325. conn->async.port = port;
  326. conn->async.done = FALSE;
  327. conn->async.status = 0;
  328. conn->async.dns = NULL;
  329. td->thread_hnd = curl_thread_t_null;
  330. if(!init_thread_sync_data(td, hostname, port, hints))
  331. goto err_exit;
  332. free(conn->async.hostname);
  333. conn->async.hostname = strdup(hostname);
  334. if(!conn->async.hostname)
  335. goto err_exit;
  336. #ifdef HAVE_GETADDRINFO
  337. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  338. #else
  339. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  340. #endif
  341. if(!td->thread_hnd) {
  342. #ifndef _WIN32_WCE
  343. err = errno;
  344. #endif
  345. goto err_exit;
  346. }
  347. return TRUE;
  348. err_exit:
  349. destroy_async_data(&conn->async);
  350. SET_ERRNO(err);
  351. return FALSE;
  352. }
  353. /*
  354. * resolver_error() calls failf() with the appropriate message after a resolve
  355. * error
  356. */
  357. static CURLcode resolver_error(struct connectdata *conn)
  358. {
  359. const char *host_or_proxy;
  360. CURLcode result;
  361. if(conn->bits.httpproxy) {
  362. host_or_proxy = "proxy";
  363. result = CURLE_COULDNT_RESOLVE_PROXY;
  364. }
  365. else {
  366. host_or_proxy = "host";
  367. result = CURLE_COULDNT_RESOLVE_HOST;
  368. }
  369. failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
  370. conn->async.hostname);
  371. return result;
  372. }
  373. /*
  374. * Curl_resolver_wait_resolv()
  375. *
  376. * waits for a resolve to finish. This function should be avoided since using
  377. * this risk getting the multi interface to "hang".
  378. *
  379. * If 'entry' is non-NULL, make it point to the resolved dns entry
  380. *
  381. * This is the version for resolves-in-a-thread.
  382. */
  383. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  384. struct Curl_dns_entry **entry)
  385. {
  386. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  387. CURLcode result = CURLE_OK;
  388. DEBUGASSERT(conn && td);
  389. /* wait for the thread to resolve the name */
  390. if(Curl_thread_join(&td->thread_hnd))
  391. result = getaddrinfo_complete(conn);
  392. else
  393. DEBUGASSERT(0);
  394. conn->async.done = TRUE;
  395. if(entry)
  396. *entry = conn->async.dns;
  397. if(!conn->async.dns)
  398. /* a name was not resolved, report error */
  399. result = resolver_error(conn);
  400. destroy_async_data(&conn->async);
  401. if(!conn->async.dns)
  402. connclose(conn, "asynch resolve failed");
  403. return result;
  404. }
  405. /*
  406. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  407. * name resolve request has completed. It should also make sure to time-out if
  408. * the operation seems to take too long.
  409. */
  410. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  411. struct Curl_dns_entry **entry)
  412. {
  413. struct SessionHandle *data = conn->data;
  414. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  415. int done = 0;
  416. *entry = NULL;
  417. if(!td) {
  418. DEBUGASSERT(td);
  419. return CURLE_COULDNT_RESOLVE_HOST;
  420. }
  421. Curl_mutex_acquire(td->tsd.mtx);
  422. done = td->tsd.done;
  423. Curl_mutex_release(td->tsd.mtx);
  424. if(done) {
  425. getaddrinfo_complete(conn);
  426. if(!conn->async.dns) {
  427. CURLcode result = resolver_error(conn);
  428. destroy_async_data(&conn->async);
  429. return result;
  430. }
  431. destroy_async_data(&conn->async);
  432. *entry = conn->async.dns;
  433. }
  434. else {
  435. /* poll for name lookup done with exponential backoff up to 250ms */
  436. long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
  437. if(elapsed < 0)
  438. elapsed = 0;
  439. if(td->poll_interval == 0)
  440. /* Start at 1ms poll interval */
  441. td->poll_interval = 1;
  442. else if(elapsed >= td->interval_end)
  443. /* Back-off exponentially if last interval expired */
  444. td->poll_interval *= 2;
  445. if(td->poll_interval > 250)
  446. td->poll_interval = 250;
  447. td->interval_end = elapsed + td->poll_interval;
  448. Curl_expire(conn->data, td->poll_interval);
  449. }
  450. return CURLE_OK;
  451. }
  452. int Curl_resolver_getsock(struct connectdata *conn,
  453. curl_socket_t *socks,
  454. int numsocks)
  455. {
  456. (void)conn;
  457. (void)socks;
  458. (void)numsocks;
  459. return 0;
  460. }
  461. #ifndef HAVE_GETADDRINFO
  462. /*
  463. * Curl_getaddrinfo() - for platforms without getaddrinfo
  464. */
  465. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  466. const char *hostname,
  467. int port,
  468. int *waitp)
  469. {
  470. struct in_addr in;
  471. *waitp = 0; /* default to synchronous response */
  472. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  473. /* This is a dotted IP address 123.123.123.123-style */
  474. return Curl_ip2addr(AF_INET, &in, hostname, port);
  475. /* fire up a new resolver thread! */
  476. if(init_resolve_thread(conn, hostname, port, NULL)) {
  477. *waitp = 1; /* expect asynchronous response */
  478. return NULL;
  479. }
  480. /* fall-back to blocking version */
  481. return Curl_ipv4_resolve_r(hostname, port);
  482. }
  483. #else /* !HAVE_GETADDRINFO */
  484. /*
  485. * Curl_resolver_getaddrinfo() - for getaddrinfo
  486. */
  487. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  488. const char *hostname,
  489. int port,
  490. int *waitp)
  491. {
  492. struct addrinfo hints;
  493. struct in_addr in;
  494. Curl_addrinfo *res;
  495. int error;
  496. char sbuf[12];
  497. int pf = PF_INET;
  498. #ifdef CURLRES_IPV6
  499. struct in6_addr in6;
  500. #endif /* CURLRES_IPV6 */
  501. *waitp = 0; /* default to synchronous response */
  502. /* First check if this is an IPv4 address string */
  503. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  504. /* This is a dotted IP address 123.123.123.123-style */
  505. return Curl_ip2addr(AF_INET, &in, hostname, port);
  506. #ifdef CURLRES_IPV6
  507. /* check if this is an IPv6 address string */
  508. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  509. /* This is an IPv6 address literal */
  510. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  511. /*
  512. * Check if a limited name resolve has been requested.
  513. */
  514. switch(conn->ip_version) {
  515. case CURL_IPRESOLVE_V4:
  516. pf = PF_INET;
  517. break;
  518. case CURL_IPRESOLVE_V6:
  519. pf = PF_INET6;
  520. break;
  521. default:
  522. pf = PF_UNSPEC;
  523. break;
  524. }
  525. if((pf != PF_INET) && !Curl_ipv6works())
  526. /* The stack seems to be a non-IPv6 one */
  527. pf = PF_INET;
  528. #endif /* CURLRES_IPV6 */
  529. memset(&hints, 0, sizeof(hints));
  530. hints.ai_family = pf;
  531. hints.ai_socktype = conn->socktype;
  532. snprintf(sbuf, sizeof(sbuf), "%d", port);
  533. /* fire up a new resolver thread! */
  534. if(init_resolve_thread(conn, hostname, port, &hints)) {
  535. *waitp = 1; /* expect asynchronous response */
  536. return NULL;
  537. }
  538. /* fall-back to blocking version */
  539. infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
  540. hostname, Curl_strerror(conn, ERRNO));
  541. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  542. if(error) {
  543. infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
  544. hostname, port, Curl_strerror(conn, SOCKERRNO));
  545. return NULL;
  546. }
  547. return res;
  548. }
  549. #endif /* !HAVE_GETADDRINFO */
  550. CURLcode Curl_set_dns_servers(struct SessionHandle *data,
  551. char *servers)
  552. {
  553. (void)data;
  554. (void)servers;
  555. return CURLE_NOT_BUILT_IN;
  556. }
  557. CURLcode Curl_set_dns_interface(struct SessionHandle *data,
  558. const char *interf)
  559. {
  560. (void)data;
  561. (void)interf;
  562. return CURLE_NOT_BUILT_IN;
  563. }
  564. CURLcode Curl_set_dns_local_ip4(struct SessionHandle *data,
  565. const char *local_ip4)
  566. {
  567. (void)data;
  568. (void)local_ip4;
  569. return CURLE_NOT_BUILT_IN;
  570. }
  571. CURLcode Curl_set_dns_local_ip6(struct SessionHandle *data,
  572. const char *local_ip6)
  573. {
  574. (void)data;
  575. (void)local_ip6;
  576. return CURLE_NOT_BUILT_IN;
  577. }
  578. #endif /* CURLRES_THREADED */