asyn-thread.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. #define _MPRINTF_REPLACE /* use our functions only */
  66. #include <curl/mprintf.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. };
  149. struct thread_data {
  150. curl_thread_t thread_hnd;
  151. unsigned int poll_interval;
  152. long interval_end;
  153. struct thread_sync_data tsd;
  154. };
  155. static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
  156. {
  157. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  158. }
  159. #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
  160. /* Destroy resolver thread synchronization data */
  161. static
  162. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  163. {
  164. if(tsd->mtx) {
  165. Curl_mutex_destroy(tsd->mtx);
  166. free(tsd->mtx);
  167. }
  168. if(tsd->hostname)
  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_sync_data * tsd,
  177. const char * hostname,
  178. int port,
  179. const struct addrinfo *hints)
  180. {
  181. memset(tsd, 0, sizeof(*tsd));
  182. tsd->port = port;
  183. #ifdef CURLRES_IPV6
  184. DEBUGASSERT(hints);
  185. tsd->hints = *hints;
  186. #else
  187. (void) hints;
  188. #endif
  189. tsd->mtx = malloc(sizeof(curl_mutex_t));
  190. if(tsd->mtx == NULL)
  191. goto err_exit;
  192. Curl_mutex_init(tsd->mtx);
  193. tsd->sock_error = CURL_ASYNC_SUCCESS;
  194. /* Copying hostname string because original can be destroyed by parent
  195. * thread during gethostbyname execution.
  196. */
  197. tsd->hostname = strdup(hostname);
  198. if(!tsd->hostname)
  199. goto err_exit;
  200. return 1;
  201. err_exit:
  202. /* Memory allocation failed */
  203. destroy_thread_sync_data(tsd);
  204. return 0;
  205. }
  206. static int getaddrinfo_complete(struct connectdata *conn)
  207. {
  208. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  209. int rc;
  210. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  211. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  212. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  213. */
  214. tsd->res = NULL;
  215. return rc;
  216. }
  217. #ifdef HAVE_GETADDRINFO
  218. /*
  219. * getaddrinfo_thread() resolves a name and then exits.
  220. *
  221. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  222. * and wait on it.
  223. */
  224. static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
  225. {
  226. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  227. char service [NI_MAXSERV];
  228. int rc;
  229. snprintf(service, sizeof(service), "%d", tsd->port);
  230. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  231. if(rc != 0) {
  232. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  233. if(tsd->sock_error == 0)
  234. tsd->sock_error = RESOLVER_ENOMEM;
  235. }
  236. Curl_mutex_acquire(tsd->mtx);
  237. tsd->done = 1;
  238. Curl_mutex_release(tsd->mtx);
  239. return 0;
  240. }
  241. #else /* HAVE_GETADDRINFO */
  242. /*
  243. * gethostbyname_thread() resolves a name and then exits.
  244. */
  245. static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
  246. {
  247. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  248. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  249. if(!tsd->res) {
  250. tsd->sock_error = SOCKERRNO;
  251. if(tsd->sock_error == 0)
  252. tsd->sock_error = RESOLVER_ENOMEM;
  253. }
  254. Curl_mutex_acquire(tsd->mtx);
  255. tsd->done = 1;
  256. Curl_mutex_release(tsd->mtx);
  257. return 0;
  258. }
  259. #endif /* HAVE_GETADDRINFO */
  260. /*
  261. * destroy_async_data() cleans up async resolver data and thread handle.
  262. */
  263. static void destroy_async_data (struct Curl_async *async)
  264. {
  265. if(async->hostname)
  266. free(async->hostname);
  267. if(async->os_specific) {
  268. struct thread_data *td = (struct thread_data*) async->os_specific;
  269. if(td->thread_hnd != curl_thread_t_null)
  270. Curl_thread_join(&td->thread_hnd);
  271. destroy_thread_sync_data(&td->tsd);
  272. free(async->os_specific);
  273. }
  274. async->hostname = NULL;
  275. async->os_specific = NULL;
  276. }
  277. /*
  278. * init_resolve_thread() starts a new thread that performs the actual
  279. * resolve. This function returns before the resolve is done.
  280. *
  281. * Returns FALSE in case of failure, otherwise TRUE.
  282. */
  283. static bool init_resolve_thread (struct connectdata *conn,
  284. const char *hostname, int port,
  285. const struct addrinfo *hints)
  286. {
  287. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  288. int err = RESOLVER_ENOMEM;
  289. conn->async.os_specific = (void*) td;
  290. if(!td)
  291. goto err_exit;
  292. conn->async.port = port;
  293. conn->async.done = FALSE;
  294. conn->async.status = 0;
  295. conn->async.dns = NULL;
  296. td->thread_hnd = curl_thread_t_null;
  297. if(!init_thread_sync_data(&td->tsd, hostname, port, hints))
  298. goto err_exit;
  299. Curl_safefree(conn->async.hostname);
  300. conn->async.hostname = strdup(hostname);
  301. if(!conn->async.hostname)
  302. goto err_exit;
  303. #ifdef HAVE_GETADDRINFO
  304. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  305. #else
  306. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  307. #endif
  308. if(!td->thread_hnd) {
  309. #ifndef _WIN32_WCE
  310. err = errno;
  311. #endif
  312. goto err_exit;
  313. }
  314. return TRUE;
  315. err_exit:
  316. destroy_async_data(&conn->async);
  317. SET_ERRNO(err);
  318. return FALSE;
  319. }
  320. /*
  321. * resolver_error() calls failf() with the appropriate message after a resolve
  322. * error
  323. */
  324. static CURLcode resolver_error(struct connectdata *conn)
  325. {
  326. const char *host_or_proxy;
  327. CURLcode rc;
  328. if(conn->bits.httpproxy) {
  329. host_or_proxy = "proxy";
  330. rc = CURLE_COULDNT_RESOLVE_PROXY;
  331. }
  332. else {
  333. host_or_proxy = "host";
  334. rc = CURLE_COULDNT_RESOLVE_HOST;
  335. }
  336. failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
  337. conn->async.hostname);
  338. return rc;
  339. }
  340. /*
  341. * Curl_resolver_wait_resolv()
  342. *
  343. * waits for a resolve to finish. This function should be avoided since using
  344. * this risk getting the multi interface to "hang".
  345. *
  346. * If 'entry' is non-NULL, make it point to the resolved dns entry
  347. *
  348. * This is the version for resolves-in-a-thread.
  349. */
  350. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  351. struct Curl_dns_entry **entry)
  352. {
  353. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  354. CURLcode rc = CURLE_OK;
  355. DEBUGASSERT(conn && td);
  356. /* wait for the thread to resolve the name */
  357. if(Curl_thread_join(&td->thread_hnd))
  358. rc = getaddrinfo_complete(conn);
  359. else
  360. DEBUGASSERT(0);
  361. conn->async.done = TRUE;
  362. if(entry)
  363. *entry = conn->async.dns;
  364. if(!conn->async.dns)
  365. /* a name was not resolved, report error */
  366. rc = resolver_error(conn);
  367. destroy_async_data(&conn->async);
  368. if(!conn->async.dns)
  369. conn->bits.close = TRUE;
  370. return (rc);
  371. }
  372. /*
  373. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  374. * name resolve request has completed. It should also make sure to time-out if
  375. * the operation seems to take too long.
  376. */
  377. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  378. struct Curl_dns_entry **entry)
  379. {
  380. struct SessionHandle *data = conn->data;
  381. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  382. int done = 0;
  383. *entry = NULL;
  384. if(!td) {
  385. DEBUGASSERT(td);
  386. return CURLE_COULDNT_RESOLVE_HOST;
  387. }
  388. Curl_mutex_acquire(td->tsd.mtx);
  389. done = td->tsd.done;
  390. Curl_mutex_release(td->tsd.mtx);
  391. if(done) {
  392. getaddrinfo_complete(conn);
  393. if(!conn->async.dns) {
  394. CURLcode rc = resolver_error(conn);
  395. destroy_async_data(&conn->async);
  396. return rc;
  397. }
  398. destroy_async_data(&conn->async);
  399. *entry = conn->async.dns;
  400. }
  401. else {
  402. /* poll for name lookup done with exponential backoff up to 250ms */
  403. long elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
  404. if(elapsed < 0)
  405. elapsed = 0;
  406. if(td->poll_interval == 0)
  407. /* Start at 1ms poll interval */
  408. td->poll_interval = 1;
  409. else if(elapsed >= td->interval_end)
  410. /* Back-off exponentially if last interval expired */
  411. td->poll_interval *= 2;
  412. if(td->poll_interval > 250)
  413. td->poll_interval = 250;
  414. td->interval_end = elapsed + td->poll_interval;
  415. Curl_expire(conn->data, td->poll_interval);
  416. }
  417. return CURLE_OK;
  418. }
  419. int Curl_resolver_getsock(struct connectdata *conn,
  420. curl_socket_t *socks,
  421. int numsocks)
  422. {
  423. (void)conn;
  424. (void)socks;
  425. (void)numsocks;
  426. return 0;
  427. }
  428. #ifndef HAVE_GETADDRINFO
  429. /*
  430. * Curl_getaddrinfo() - for platforms without getaddrinfo
  431. */
  432. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  433. const char *hostname,
  434. int port,
  435. int *waitp)
  436. {
  437. struct in_addr in;
  438. *waitp = 0; /* default to synchronous response */
  439. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  440. /* This is a dotted IP address 123.123.123.123-style */
  441. return Curl_ip2addr(AF_INET, &in, hostname, port);
  442. /* fire up a new resolver thread! */
  443. if(init_resolve_thread(conn, hostname, port, NULL)) {
  444. *waitp = 1; /* expect asynchronous response */
  445. return NULL;
  446. }
  447. /* fall-back to blocking version */
  448. return Curl_ipv4_resolve_r(hostname, port);
  449. }
  450. #else /* !HAVE_GETADDRINFO */
  451. /*
  452. * Curl_resolver_getaddrinfo() - for getaddrinfo
  453. */
  454. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  455. const char *hostname,
  456. int port,
  457. int *waitp)
  458. {
  459. struct addrinfo hints;
  460. struct in_addr in;
  461. Curl_addrinfo *res;
  462. int error;
  463. char sbuf[NI_MAXSERV];
  464. int pf = PF_INET;
  465. #ifdef CURLRES_IPV6
  466. struct in6_addr in6;
  467. #endif /* CURLRES_IPV6 */
  468. *waitp = 0; /* default to synchronous response */
  469. /* First check if this is an IPv4 address string */
  470. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  471. /* This is a dotted IP address 123.123.123.123-style */
  472. return Curl_ip2addr(AF_INET, &in, hostname, port);
  473. #ifdef CURLRES_IPV6
  474. /* check if this is an IPv6 address string */
  475. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  476. /* This is an IPv6 address literal */
  477. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  478. /*
  479. * Check if a limited name resolve has been requested.
  480. */
  481. switch(conn->ip_version) {
  482. case CURL_IPRESOLVE_V4:
  483. pf = PF_INET;
  484. break;
  485. case CURL_IPRESOLVE_V6:
  486. pf = PF_INET6;
  487. break;
  488. default:
  489. pf = PF_UNSPEC;
  490. break;
  491. }
  492. if((pf != PF_INET) && !Curl_ipv6works())
  493. /* the stack seems to be a non-ipv6 one */
  494. pf = PF_INET;
  495. #endif /* CURLRES_IPV6 */
  496. memset(&hints, 0, sizeof(hints));
  497. hints.ai_family = pf;
  498. hints.ai_socktype = conn->socktype;
  499. snprintf(sbuf, sizeof(sbuf), "%d", port);
  500. /* fire up a new resolver thread! */
  501. if(init_resolve_thread(conn, hostname, port, &hints)) {
  502. *waitp = 1; /* expect asynchronous response */
  503. return NULL;
  504. }
  505. /* fall-back to blocking version */
  506. infof(conn->data, "init_resolve_thread() failed for %s; %s\n",
  507. hostname, Curl_strerror(conn, ERRNO));
  508. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  509. if(error) {
  510. infof(conn->data, "getaddrinfo() failed for %s:%d; %s\n",
  511. hostname, port, Curl_strerror(conn, SOCKERRNO));
  512. return NULL;
  513. }
  514. return res;
  515. }
  516. #endif /* !HAVE_GETADDRINFO */
  517. CURLcode Curl_set_dns_servers(struct SessionHandle *data,
  518. char *servers)
  519. {
  520. (void)data;
  521. (void)servers;
  522. return CURLE_NOT_BUILT_IN;
  523. }
  524. #endif /* CURLRES_THREADED */