asyn-thread.c 17 KB

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