2
0

asyn-thread.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 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. #define _MPRINTF_REPLACE /* use our functions only */
  67. #include <curl/mprintf.h>
  68. #include "curl_memory.h"
  69. /* The last #include file should be: */
  70. #include "memdebug.h"
  71. /***********************************************************************
  72. * Only for threaded name resolves builds
  73. **********************************************************************/
  74. #ifdef CURLRES_THREADED
  75. /*
  76. * Curl_resolver_global_init()
  77. * Called from curl_global_init() to initialize global resolver environment.
  78. * Does nothing here.
  79. */
  80. int Curl_resolver_global_init(void)
  81. {
  82. return CURLE_OK;
  83. }
  84. /*
  85. * Curl_resolver_global_cleanup()
  86. * Called from curl_global_cleanup() to destroy global resolver environment.
  87. * Does nothing here.
  88. */
  89. void Curl_resolver_global_cleanup(void)
  90. {
  91. }
  92. /*
  93. * Curl_resolver_init()
  94. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  95. * URL-state specific environment ('resolver' member of the UrlState
  96. * structure). Does nothing here.
  97. */
  98. CURLcode Curl_resolver_init(void **resolver)
  99. {
  100. (void)resolver;
  101. return CURLE_OK;
  102. }
  103. /*
  104. * Curl_resolver_cleanup()
  105. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  106. * URL-state specific environment ('resolver' member of the UrlState
  107. * structure). Does nothing here.
  108. */
  109. void Curl_resolver_cleanup(void *resolver)
  110. {
  111. (void)resolver;
  112. }
  113. /*
  114. * Curl_resolver_duphandle()
  115. * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
  116. * environment ('resolver' member of the UrlState structure). Does nothing
  117. * here.
  118. */
  119. int Curl_resolver_duphandle(void **to, void *from)
  120. {
  121. (void)to;
  122. (void)from;
  123. return CURLE_OK;
  124. }
  125. static void destroy_async_data(struct Curl_async *);
  126. /*
  127. * Cancel all possibly still on-going resolves for this connection.
  128. */
  129. void Curl_resolver_cancel(struct connectdata *conn)
  130. {
  131. destroy_async_data(&conn->async);
  132. }
  133. /* This function is used to init a threaded resolve */
  134. static bool init_resolve_thread(struct connectdata *conn,
  135. const char *hostname, int port,
  136. const struct addrinfo *hints);
  137. /* Data for synchronization between resolver thread and its parent */
  138. struct thread_sync_data {
  139. curl_mutex_t * mtx;
  140. int done;
  141. char * hostname; /* hostname to resolve, Curl_async.hostname
  142. duplicate */
  143. int port;
  144. int sock_error;
  145. Curl_addrinfo *res;
  146. #ifdef HAVE_GETADDRINFO
  147. struct addrinfo hints;
  148. #endif
  149. struct thread_data *td; /* for thread-self cleanup */
  150. };
  151. struct thread_data {
  152. curl_thread_t thread_hnd;
  153. unsigned int poll_interval;
  154. long interval_end;
  155. struct thread_sync_data tsd;
  156. };
  157. static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
  158. {
  159. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  160. }
  161. #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
  162. /* Destroy resolver thread synchronization data */
  163. static
  164. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  165. {
  166. if(tsd->mtx) {
  167. Curl_mutex_destroy(tsd->mtx);
  168. free(tsd->mtx);
  169. }
  170. if(tsd->hostname)
  171. free(tsd->hostname);
  172. if(tsd->res)
  173. Curl_freeaddrinfo(tsd->res);
  174. memset(tsd,0,sizeof(*tsd));
  175. }
  176. /* Initialize resolver thread synchronization data */
  177. static
  178. int init_thread_sync_data(struct thread_data * td,
  179. const char * hostname,
  180. int port,
  181. const struct addrinfo *hints)
  182. {
  183. struct thread_sync_data *tsd = &td->tsd;
  184. memset(tsd, 0, sizeof(*tsd));
  185. tsd->td = td;
  186. tsd->port = port;
  187. #ifdef HAVE_GETADDRINFO
  188. DEBUGASSERT(hints);
  189. tsd->hints = *hints;
  190. #else
  191. (void) hints;
  192. #endif
  193. tsd->mtx = malloc(sizeof(curl_mutex_t));
  194. if(tsd->mtx == NULL)
  195. goto err_exit;
  196. Curl_mutex_init(tsd->mtx);
  197. tsd->sock_error = CURL_ASYNC_SUCCESS;
  198. /* Copying hostname string because original can be destroyed by parent
  199. * thread during gethostbyname execution.
  200. */
  201. tsd->hostname = strdup(hostname);
  202. if(!tsd->hostname)
  203. goto err_exit;
  204. return 1;
  205. err_exit:
  206. /* Memory allocation failed */
  207. destroy_thread_sync_data(tsd);
  208. return 0;
  209. }
  210. static int getaddrinfo_complete(struct connectdata *conn)
  211. {
  212. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  213. int rc;
  214. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  215. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  216. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  217. */
  218. tsd->res = NULL;
  219. return rc;
  220. }
  221. #ifdef HAVE_GETADDRINFO
  222. /*
  223. * getaddrinfo_thread() resolves a name and then exits.
  224. *
  225. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  226. * and wait on it.
  227. */
  228. static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
  229. {
  230. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  231. struct thread_data *td = tsd->td;
  232. char service[12];
  233. int rc;
  234. snprintf(service, sizeof(service), "%d", tsd->port);
  235. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  236. if(rc != 0) {
  237. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  238. if(tsd->sock_error == 0)
  239. tsd->sock_error = RESOLVER_ENOMEM;
  240. }
  241. Curl_mutex_acquire(tsd->mtx);
  242. if(tsd->done) {
  243. /* too late, gotta clean up the mess */
  244. Curl_mutex_release(tsd->mtx);
  245. destroy_thread_sync_data(tsd);
  246. free(td);
  247. }
  248. else {
  249. tsd->done = 1;
  250. Curl_mutex_release(tsd->mtx);
  251. }
  252. return 0;
  253. }
  254. #else /* HAVE_GETADDRINFO */
  255. /*
  256. * gethostbyname_thread() resolves a name and then exits.
  257. */
  258. static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
  259. {
  260. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  261. struct thread_data *td = tsd->td;
  262. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  263. if(!tsd->res) {
  264. tsd->sock_error = SOCKERRNO;
  265. if(tsd->sock_error == 0)
  266. tsd->sock_error = RESOLVER_ENOMEM;
  267. }
  268. Curl_mutex_acquire(tsd->mtx);
  269. if(tsd->done) {
  270. /* too late, gotta clean up the mess */
  271. Curl_mutex_release(tsd->mtx);
  272. destroy_thread_sync_data(tsd);
  273. free(td);
  274. }
  275. else {
  276. tsd->done = 1;
  277. Curl_mutex_release(tsd->mtx);
  278. }
  279. return 0;
  280. }
  281. #endif /* HAVE_GETADDRINFO */
  282. /*
  283. * destroy_async_data() cleans up async resolver data and thread handle.
  284. */
  285. static void destroy_async_data (struct Curl_async *async)
  286. {
  287. if(async->os_specific) {
  288. struct thread_data *td = (struct thread_data*) async->os_specific;
  289. int done;
  290. /*
  291. * if the thread is still blocking in the resolve syscall, detach it and
  292. * let the thread do the cleanup...
  293. */
  294. Curl_mutex_acquire(td->tsd.mtx);
  295. done = td->tsd.done;
  296. td->tsd.done = 1;
  297. Curl_mutex_release(td->tsd.mtx);
  298. if(!done) {
  299. Curl_thread_destroy(td->thread_hnd);
  300. }
  301. else {
  302. if(td->thread_hnd != curl_thread_t_null)
  303. Curl_thread_join(&td->thread_hnd);
  304. destroy_thread_sync_data(&td->tsd);
  305. free(async->os_specific);
  306. }
  307. }
  308. async->os_specific = NULL;
  309. if(async->hostname)
  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. Curl_safefree(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 rc;
  364. if(conn->bits.httpproxy) {
  365. host_or_proxy = "proxy";
  366. rc = CURLE_COULDNT_RESOLVE_PROXY;
  367. }
  368. else {
  369. host_or_proxy = "host";
  370. rc = CURLE_COULDNT_RESOLVE_HOST;
  371. }
  372. failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
  373. conn->async.hostname);
  374. return rc;
  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 rc = CURLE_OK;
  391. DEBUGASSERT(conn && td);
  392. /* wait for the thread to resolve the name */
  393. if(Curl_thread_join(&td->thread_hnd))
  394. rc = 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. rc = resolver_error(conn);
  403. destroy_async_data(&conn->async);
  404. if(!conn->async.dns)
  405. connclose(conn, "asynch resolve failed");
  406. return (rc);
  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 SessionHandle *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 rc = resolver_error(conn);
  431. destroy_async_data(&conn->async);
  432. return rc;
  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. long 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. /* First check if this is an IPv4 address string */
  506. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  507. /* This is a dotted IP address 123.123.123.123-style */
  508. return Curl_ip2addr(AF_INET, &in, hostname, port);
  509. #ifdef CURLRES_IPV6
  510. /* check if this is an IPv6 address string */
  511. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  512. /* This is an IPv6 address literal */
  513. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  514. /*
  515. * Check if a limited name resolve has been requested.
  516. */
  517. switch(conn->ip_version) {
  518. case CURL_IPRESOLVE_V4:
  519. pf = PF_INET;
  520. break;
  521. case CURL_IPRESOLVE_V6:
  522. pf = PF_INET6;
  523. break;
  524. default:
  525. pf = PF_UNSPEC;
  526. break;
  527. }
  528. if((pf != PF_INET) && !Curl_ipv6works())
  529. /* the stack seems to be a non-ipv6 one */
  530. pf = PF_INET;
  531. #endif /* CURLRES_IPV6 */
  532. memset(&hints, 0, sizeof(hints));
  533. hints.ai_family = pf;
  534. hints.ai_socktype = conn->socktype;
  535. snprintf(sbuf, sizeof(sbuf), "%d", port);
  536. /* fire up a new resolver thread! */
  537. if(init_resolve_thread(conn, hostname, port, &hints)) {
  538. *waitp = 1; /* expect asynchronous response */
  539. return NULL;
  540. }
  541. /* fall-back to blocking version */
  542. infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
  543. hostname, Curl_strerror(conn, ERRNO));
  544. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  545. if(error) {
  546. infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
  547. hostname, port, Curl_strerror(conn, SOCKERRNO));
  548. return NULL;
  549. }
  550. return res;
  551. }
  552. #endif /* !HAVE_GETADDRINFO */
  553. CURLcode Curl_set_dns_servers(struct SessionHandle *data,
  554. char *servers)
  555. {
  556. (void)data;
  557. (void)servers;
  558. return CURLE_NOT_BUILT_IN;
  559. }
  560. CURLcode Curl_set_dns_interface(struct SessionHandle *data,
  561. const char *interf)
  562. {
  563. (void)data;
  564. (void)interf;
  565. return CURLE_NOT_BUILT_IN;
  566. }
  567. CURLcode Curl_set_dns_local_ip4(struct SessionHandle *data,
  568. const char *local_ip4)
  569. {
  570. (void)data;
  571. (void)local_ip4;
  572. return CURLE_NOT_BUILT_IN;
  573. }
  574. CURLcode Curl_set_dns_local_ip6(struct SessionHandle *data,
  575. const char *local_ip6)
  576. {
  577. (void)data;
  578. (void)local_ip6;
  579. return CURLE_NOT_BUILT_IN;
  580. }
  581. #endif /* CURLRES_THREADED */