hostthre.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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. #include <string.h>
  24. #include <errno.h>
  25. #ifdef HAVE_SYS_SOCKET_H
  26. #include <sys/socket.h>
  27. #endif
  28. #ifdef HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. #include <netdb.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #ifdef HAVE_STDLIB_H
  38. #include <stdlib.h> /* required for free() prototypes */
  39. #endif
  40. #ifdef HAVE_UNISTD_H
  41. #include <unistd.h> /* for the close() proto */
  42. #endif
  43. #ifdef __VMS
  44. #include <in.h>
  45. #include <inet.h>
  46. #include <stdlib.h>
  47. #endif
  48. #if defined(USE_THREADS_POSIX)
  49. # ifdef HAVE_PTHREAD_H
  50. # include <pthread.h>
  51. # endif
  52. #elif defined(USE_THREADS_WIN32)
  53. # ifdef HAVE_PROCESS_H
  54. # include <process.h>
  55. # endif
  56. #endif
  57. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  58. #undef in_addr_t
  59. #define in_addr_t unsigned long
  60. #endif
  61. #include "urldata.h"
  62. #include "sendf.h"
  63. #include "hostip.h"
  64. #include "hash.h"
  65. #include "share.h"
  66. #include "strerror.h"
  67. #include "url.h"
  68. #include "multiif.h"
  69. #include "inet_pton.h"
  70. #include "inet_ntop.h"
  71. #include "curl_threads.h"
  72. #define _MPRINTF_REPLACE /* use our functions only */
  73. #include <curl/mprintf.h>
  74. #include "curl_memory.h"
  75. /* The last #include file should be: */
  76. #include "memdebug.h"
  77. /***********************************************************************
  78. * Only for threaded name resolves builds
  79. **********************************************************************/
  80. #ifdef CURLRES_THREADED
  81. /* This function is used to init a threaded resolve */
  82. static bool init_resolve_thread(struct connectdata *conn,
  83. const char *hostname, int port,
  84. const struct addrinfo *hints);
  85. /* Data for synchronization between resolver thread and its parent */
  86. struct thread_sync_data {
  87. curl_mutex_t * mtx;
  88. int done;
  89. char * hostname; /* hostname to resolve, Curl_async.hostname
  90. duplicate */
  91. int port;
  92. int sock_error;
  93. Curl_addrinfo *res;
  94. #ifdef HAVE_GETADDRINFO
  95. struct addrinfo hints;
  96. #endif
  97. };
  98. struct thread_data {
  99. curl_thread_t thread_hnd;
  100. curl_socket_t dummy_sock;
  101. unsigned int poll_interval;
  102. int interval_end;
  103. struct thread_sync_data tsd;
  104. };
  105. static struct thread_sync_data * conn_thread_sync_data(struct connectdata *conn)
  106. {
  107. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  108. }
  109. #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
  110. /* Destroy resolver thread synchronization data */
  111. static
  112. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  113. {
  114. if (tsd->mtx) {
  115. Curl_mutex_destroy(tsd->mtx);
  116. free(tsd->mtx);
  117. }
  118. if(tsd->hostname)
  119. free(tsd->hostname);
  120. if (tsd->res)
  121. Curl_freeaddrinfo(tsd->res);
  122. memset(tsd,0,sizeof(*tsd));
  123. }
  124. /* Initialize resolver thread synchronization data */
  125. static
  126. int init_thread_sync_data(struct thread_sync_data * tsd,
  127. const char * hostname,
  128. int port,
  129. const struct addrinfo *hints)
  130. {
  131. memset(tsd, 0, sizeof(*tsd));
  132. tsd->port = port;
  133. #ifdef CURLRES_IPV6
  134. DEBUGASSERT(hints);
  135. tsd->hints = *hints;
  136. #else
  137. (void) hints;
  138. #endif
  139. tsd->mtx = malloc(sizeof(curl_mutex_t));
  140. if (tsd->mtx == NULL) goto err_exit;
  141. Curl_mutex_init(tsd->mtx);
  142. tsd->sock_error = CURL_ASYNC_SUCCESS;
  143. /* Copying hostname string because original can be destroyed by parent
  144. * thread during gethostbyname execution.
  145. */
  146. tsd->hostname = strdup(hostname);
  147. if (!tsd->hostname) goto err_exit;
  148. return 1;
  149. err_exit:
  150. /* Memory allocation failed */
  151. destroy_thread_sync_data(tsd);
  152. return 0;
  153. }
  154. static int getaddrinfo_complete(struct connectdata *conn)
  155. {
  156. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  157. int rc;
  158. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  159. /* The tsd->res structure has been copied to async.dns and perhaps the DNS cache.
  160. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  161. */
  162. tsd->res = NULL;
  163. return rc;
  164. }
  165. #ifdef HAVE_GETADDRINFO
  166. /*
  167. * getaddrinfo_thread() resolves a name and then exits.
  168. *
  169. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  170. * and wait on it.
  171. */
  172. static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
  173. {
  174. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  175. char service [NI_MAXSERV];
  176. int rc;
  177. snprintf(service, sizeof(service), "%d", tsd->port);
  178. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  179. if (rc != 0) {
  180. tsd->sock_error = SOCKERRNO;
  181. if (tsd->sock_error == 0)
  182. tsd->sock_error = ENOMEM;
  183. }
  184. Curl_mutex_acquire(tsd->mtx);
  185. tsd->done = 1;
  186. Curl_mutex_release(tsd->mtx);
  187. return 0;
  188. }
  189. #else /* HAVE_GETADDRINFO */
  190. /*
  191. * gethostbyname_thread() resolves a name and then exits.
  192. */
  193. static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
  194. {
  195. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  196. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  197. if (!tsd->res) {
  198. tsd->sock_error = SOCKERRNO;
  199. if (tsd->sock_error == 0)
  200. tsd->sock_error = ENOMEM;
  201. }
  202. Curl_mutex_acquire(tsd->mtx);
  203. tsd->done = 1;
  204. Curl_mutex_release(tsd->mtx);
  205. return 0;
  206. }
  207. #endif /* HAVE_GETADDRINFO */
  208. /*
  209. * Curl_destroy_thread_data() cleans up async resolver data and thread handle.
  210. * Complementary of ares_destroy.
  211. */
  212. void Curl_destroy_thread_data (struct Curl_async *async)
  213. {
  214. if(async->hostname)
  215. free(async->hostname);
  216. if(async->os_specific) {
  217. struct thread_data *td = (struct thread_data*) async->os_specific;
  218. if (td->dummy_sock != CURL_SOCKET_BAD)
  219. sclose(td->dummy_sock);
  220. if (td->thread_hnd != curl_thread_t_null)
  221. Curl_thread_join(&td->thread_hnd);
  222. destroy_thread_sync_data(&td->tsd);
  223. free(async->os_specific);
  224. }
  225. async->hostname = NULL;
  226. async->os_specific = NULL;
  227. }
  228. /*
  229. * init_resolve_thread() starts a new thread that performs the actual
  230. * resolve. This function returns before the resolve is done.
  231. *
  232. * Returns FALSE in case of failure, otherwise TRUE.
  233. */
  234. static bool init_resolve_thread (struct connectdata *conn,
  235. const char *hostname, int port,
  236. const struct addrinfo *hints)
  237. {
  238. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  239. int err = ENOMEM;
  240. conn->async.os_specific = (void*) td;
  241. if(!td)
  242. goto err_exit;
  243. conn->async.port = port;
  244. conn->async.done = FALSE;
  245. conn->async.status = 0;
  246. conn->async.dns = NULL;
  247. td->dummy_sock = CURL_SOCKET_BAD;
  248. td->thread_hnd = curl_thread_t_null;
  249. if (!init_thread_sync_data(&td->tsd, hostname, port, hints))
  250. goto err_exit;
  251. Curl_safefree(conn->async.hostname);
  252. conn->async.hostname = strdup(hostname);
  253. if(!conn->async.hostname)
  254. goto err_exit;
  255. #ifdef WIN32
  256. /* This socket is only to keep Curl_resolv_fdset() and select() happy;
  257. * should never become signalled for read since it's unbound but
  258. * Windows needs at least 1 socket in select().
  259. */
  260. td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
  261. if (td->dummy_sock == CURL_SOCKET_BAD)
  262. goto err_exit;
  263. #endif
  264. #ifdef HAVE_GETADDRINFO
  265. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  266. #else
  267. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  268. #endif
  269. if(!td->thread_hnd) {
  270. #ifndef _WIN32_WCE
  271. err = errno;
  272. #endif
  273. goto err_exit;
  274. }
  275. return TRUE;
  276. err_exit:
  277. Curl_destroy_thread_data(&conn->async);
  278. SET_ERRNO(err);
  279. return FALSE;
  280. }
  281. /*
  282. * Curl_wait_for_resolv() waits for a resolve to finish. This function should
  283. * be avoided since using this risk getting the multi interface to "hang".
  284. *
  285. * If 'entry' is non-NULL, make it point to the resolved dns entry
  286. *
  287. * This is the version for resolves-in-a-thread.
  288. */
  289. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  290. struct Curl_dns_entry **entry)
  291. {
  292. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  293. struct SessionHandle *data = conn->data;
  294. CURLcode rc = CURLE_OK;
  295. DEBUGASSERT(conn && td);
  296. /* wait for the thread to resolve the name */
  297. if (Curl_thread_join(&td->thread_hnd)) {
  298. rc = getaddrinfo_complete(conn);
  299. } else {
  300. DEBUGASSERT(0);
  301. }
  302. conn->async.done = TRUE;
  303. if(entry)
  304. *entry = conn->async.dns;
  305. if(!conn->async.dns) {
  306. /* a name was not resolved */
  307. if (conn->bits.httpproxy) {
  308. failf(data, "Could not resolve proxy: %s; %s",
  309. conn->async.hostname, Curl_strerror(conn, conn->async.status));
  310. rc = CURLE_COULDNT_RESOLVE_PROXY;
  311. } else {
  312. failf(data, "Could not resolve host: %s; %s",
  313. conn->async.hostname, Curl_strerror(conn, conn->async.status));
  314. rc = CURLE_COULDNT_RESOLVE_HOST;
  315. }
  316. }
  317. Curl_destroy_thread_data(&conn->async);
  318. if(!conn->async.dns)
  319. conn->bits.close = TRUE;
  320. return (rc);
  321. }
  322. /*
  323. * Curl_is_resolved() is called repeatedly to check if a previous name resolve
  324. * request has completed. It should also make sure to time-out if the
  325. * operation seems to take too long.
  326. */
  327. CURLcode Curl_is_resolved(struct connectdata *conn,
  328. struct Curl_dns_entry **entry)
  329. {
  330. struct SessionHandle *data = conn->data;
  331. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  332. int done = 0;
  333. *entry = NULL;
  334. if (!td) {
  335. DEBUGASSERT(td);
  336. return CURLE_COULDNT_RESOLVE_HOST;
  337. }
  338. Curl_mutex_acquire(td->tsd.mtx);
  339. done = td->tsd.done;
  340. Curl_mutex_release(td->tsd.mtx);
  341. if (done) {
  342. getaddrinfo_complete(conn);
  343. Curl_destroy_thread_data(&conn->async);
  344. if(!conn->async.dns) {
  345. failf(data, "Could not resolve host: %s; %s",
  346. conn->host.name, Curl_strerror(conn, conn->async.status));
  347. return CURLE_COULDNT_RESOLVE_HOST;
  348. }
  349. *entry = conn->async.dns;
  350. } else {
  351. /* poll for name lookup done with exponential backoff up to 250ms */
  352. int elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
  353. if (elapsed < 0)
  354. elapsed = 0;
  355. if (td->poll_interval == 0)
  356. /* Start at 1ms poll interval */
  357. td->poll_interval = 1;
  358. else if (elapsed >= td->interval_end)
  359. /* Back-off exponentially if last interval expired */
  360. td->poll_interval *= 2;
  361. if (td->poll_interval > 250)
  362. td->poll_interval = 250;
  363. td->interval_end = elapsed + td->poll_interval;
  364. Curl_expire(conn->data, td->poll_interval);
  365. }
  366. return CURLE_OK;
  367. }
  368. int Curl_resolv_getsock(struct connectdata *conn,
  369. curl_socket_t *socks,
  370. int numsocks)
  371. {
  372. const struct thread_data *td =
  373. (const struct thread_data *) conn->async.os_specific;
  374. if(td && td->dummy_sock != CURL_SOCKET_BAD) {
  375. if(numsocks) {
  376. /* return one socket waiting for readable, even though this is just
  377. a dummy */
  378. socks[0] = td->dummy_sock;
  379. return GETSOCK_READSOCK(0);
  380. }
  381. }
  382. return 0;
  383. }
  384. #ifndef HAVE_GETADDRINFO
  385. /*
  386. * Curl_getaddrinfo() - for platforms without getaddrinfo
  387. */
  388. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  389. const char *hostname,
  390. int port,
  391. int *waitp)
  392. {
  393. struct in_addr in;
  394. *waitp = 0; /* default to synchronous response */
  395. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  396. /* This is a dotted IP address 123.123.123.123-style */
  397. return Curl_ip2addr(AF_INET, &in, hostname, port);
  398. /* fire up a new resolver thread! */
  399. if(init_resolve_thread(conn, hostname, port, NULL)) {
  400. *waitp = 1; /* expect asynchronous response */
  401. return NULL;
  402. }
  403. /* fall-back to blocking version */
  404. return Curl_ipv4_resolve_r(hostname, port);
  405. }
  406. #else /* !HAVE_GETADDRINFO */
  407. /*
  408. * Curl_getaddrinfo() - for getaddrinfo
  409. */
  410. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  411. const char *hostname,
  412. int port,
  413. int *waitp)
  414. {
  415. struct addrinfo hints;
  416. Curl_addrinfo *res;
  417. int error;
  418. char sbuf[NI_MAXSERV];
  419. int pf = PF_INET;
  420. struct SessionHandle *data = conn->data;
  421. *waitp = 0; /* default to synchronous response */
  422. #ifndef CURLRES_IPV4
  423. /*
  424. * Check if a limited name resolve has been requested.
  425. */
  426. switch(conn->ip_version) {
  427. case CURL_IPRESOLVE_V4:
  428. pf = PF_INET;
  429. break;
  430. case CURL_IPRESOLVE_V6:
  431. pf = PF_INET6;
  432. break;
  433. default:
  434. pf = PF_UNSPEC;
  435. break;
  436. }
  437. if (pf != PF_INET) {
  438. /* see if we have an IPv6 stack */
  439. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  440. if(s == CURL_SOCKET_BAD) {
  441. /* Some non-IPv6 stacks have been found to make very slow name resolves
  442. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  443. * the stack seems to be a non-ipv6 one. */
  444. pf = PF_INET;
  445. }
  446. else {
  447. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  448. * possible checks. And close the socket again.
  449. */
  450. sclose(s);
  451. }
  452. }
  453. #endif /* !CURLRES_IPV4 */
  454. memset(&hints, 0, sizeof(hints));
  455. hints.ai_family = pf;
  456. hints.ai_socktype = conn->socktype;
  457. snprintf(sbuf, sizeof(sbuf), "%d", port);
  458. /* fire up a new resolver thread! */
  459. if(init_resolve_thread(conn, hostname, port, &hints)) {
  460. *waitp = 1; /* expect asynchronous response */
  461. return NULL;
  462. }
  463. /* fall-back to blocking version */
  464. infof(data, "init_resolve_thread() failed for %s; %s\n",
  465. hostname, Curl_strerror(conn, ERRNO));
  466. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  467. if(error) {
  468. infof(data, "getaddrinfo() failed for %s:%d; %s\n",
  469. hostname, port, Curl_strerror(conn, SOCKERRNO));
  470. return NULL;
  471. }
  472. return res;
  473. }
  474. #endif /* !HAVE_GETADDRINFO */
  475. #endif /* CURLRES_THREADED */