hostthre.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <errno.h>
  26. #ifdef HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  28. #endif
  29. #ifdef HAVE_NETINET_IN_H
  30. #include <netinet/in.h>
  31. #endif
  32. #ifdef HAVE_NETDB_H
  33. #include <netdb.h>
  34. #endif
  35. #ifdef HAVE_ARPA_INET_H
  36. #include <arpa/inet.h>
  37. #endif
  38. #ifdef HAVE_STDLIB_H
  39. #include <stdlib.h> /* required for free() prototypes */
  40. #endif
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h> /* for the close() proto */
  43. #endif
  44. #ifdef __VMS
  45. #include <in.h>
  46. #include <inet.h>
  47. #include <stdlib.h>
  48. #endif
  49. #if defined(USE_THREADS_POSIX)
  50. # ifdef HAVE_PTHREAD_H
  51. # include <pthread.h>
  52. # endif
  53. #elif defined(USE_THREADS_WIN32)
  54. # ifdef HAVE_PROCESS_H
  55. # include <process.h>
  56. # endif
  57. #endif
  58. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  59. #undef in_addr_t
  60. #define in_addr_t unsigned long
  61. #endif
  62. #include "urldata.h"
  63. #include "sendf.h"
  64. #include "hostip.h"
  65. #include "hash.h"
  66. #include "share.h"
  67. #include "strerror.h"
  68. #include "url.h"
  69. #include "multiif.h"
  70. #include "inet_pton.h"
  71. #include "inet_ntop.h"
  72. #include "curl_threads.h"
  73. #define _MPRINTF_REPLACE /* use our functions only */
  74. #include <curl/mprintf.h>
  75. #include "curl_memory.h"
  76. /* The last #include file should be: */
  77. #include "memdebug.h"
  78. /***********************************************************************
  79. * Only for threaded name resolves builds
  80. **********************************************************************/
  81. #ifdef CURLRES_THREADED
  82. /* This function is used to init a threaded resolve */
  83. static bool init_resolve_thread(struct connectdata *conn,
  84. const char *hostname, int port,
  85. const struct addrinfo *hints);
  86. /* Data for synchronization between resolver thread and its parent */
  87. struct thread_sync_data {
  88. curl_mutex_t * mtx;
  89. int done;
  90. char * hostname; /* hostname to resolve, Curl_async.hostname
  91. duplicate */
  92. int port;
  93. int sock_error;
  94. Curl_addrinfo *res;
  95. #ifdef HAVE_GETADDRINFO
  96. struct addrinfo hints;
  97. #endif
  98. };
  99. struct thread_data {
  100. curl_thread_t thread_hnd;
  101. curl_socket_t dummy_sock;
  102. unsigned int poll_interval;
  103. int interval_end;
  104. struct thread_sync_data tsd;
  105. };
  106. static struct thread_sync_data * conn_thread_sync_data(struct connectdata *conn)
  107. {
  108. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  109. }
  110. #define CONN_THREAD_SYNC_DATA(conn) &(((conn)->async.os_specific)->tsd);
  111. /* Destroy resolver thread synchronization data */
  112. static
  113. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  114. {
  115. if (tsd->mtx) {
  116. Curl_mutex_destroy(tsd->mtx);
  117. free(tsd->mtx);
  118. }
  119. if(tsd->hostname)
  120. free(tsd->hostname);
  121. if (tsd->res)
  122. Curl_freeaddrinfo(tsd->res);
  123. memset(tsd,0,sizeof(*tsd));
  124. }
  125. /* Initialize resolver thread synchronization data */
  126. static
  127. int init_thread_sync_data(struct thread_sync_data * tsd,
  128. const char * hostname,
  129. int port,
  130. const struct addrinfo *hints)
  131. {
  132. memset(tsd, 0, sizeof(*tsd));
  133. tsd->port = port;
  134. #ifdef CURLRES_IPV6
  135. DEBUGASSERT(hints);
  136. tsd->hints = *hints;
  137. #else
  138. (void) hints;
  139. #endif
  140. tsd->mtx = malloc(sizeof(curl_mutex_t));
  141. if (tsd->mtx == NULL) goto err_exit;
  142. Curl_mutex_init(tsd->mtx);
  143. tsd->sock_error = CURL_ASYNC_SUCCESS;
  144. /* Copying hostname string because original can be destroyed by parent
  145. * thread during gethostbyname execution.
  146. */
  147. tsd->hostname = strdup(hostname);
  148. if (!tsd->hostname) goto err_exit;
  149. return 1;
  150. err_exit:
  151. /* Memory allocation failed */
  152. destroy_thread_sync_data(tsd);
  153. return 0;
  154. }
  155. /*
  156. * gethostbyname_thread() resolves a name and then exits.
  157. */
  158. static unsigned int CURL_STDCALL gethostbyname_thread (void *arg)
  159. {
  160. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  161. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  162. if (!tsd->res) {
  163. tsd->sock_error = SOCKERRNO;
  164. if (tsd->sock_error == 0)
  165. tsd->sock_error = ENOMEM;
  166. }
  167. Curl_mutex_acquire(tsd->mtx);
  168. tsd->done = 1;
  169. Curl_mutex_release(tsd->mtx);
  170. return 0;
  171. }
  172. static int getaddrinfo_complete(struct connectdata *conn)
  173. {
  174. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  175. int rc;
  176. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  177. /* The tsd->res structure has been copied to async.dns and perhaps the DNS cache.
  178. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  179. */
  180. tsd->res = NULL;
  181. return rc;
  182. }
  183. #if defined(HAVE_GETADDRINFO)
  184. /*
  185. * getaddrinfo_thread() resolves a name and then exits.
  186. *
  187. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  188. * and wait on it.
  189. */
  190. static unsigned int CURL_STDCALL getaddrinfo_thread (void *arg)
  191. {
  192. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  193. char service [NI_MAXSERV];
  194. int rc;
  195. snprintf(service, sizeof(service), "%d", tsd->port);
  196. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  197. if (rc != 0) {
  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;
  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. if (td->poll_interval != 0)
  344. Curl_expire(conn->data, 0);
  345. Curl_destroy_thread_data(&conn->async);
  346. if(!conn->async.dns) {
  347. failf(data, "Could not resolve host: %s; %s",
  348. conn->host.name, Curl_strerror(conn, conn->async.status));
  349. return CURLE_COULDNT_RESOLVE_HOST;
  350. }
  351. *entry = conn->async.dns;
  352. } else {
  353. /* poll for name lookup done with exponential backoff up to 250ms */
  354. int elapsed;
  355. elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle);
  356. if (elapsed < 0) {
  357. elapsed = 0;
  358. }
  359. if (td->poll_interval == 0) {
  360. /* Start at 1ms poll interval */
  361. td->poll_interval = 1;
  362. } else if (elapsed >= td->interval_end) {
  363. /* Back-off exponentially if last interval expired */
  364. td->poll_interval *= 2;
  365. }
  366. if (td->poll_interval > 250)
  367. td->poll_interval = 250;
  368. td->interval_end = elapsed + td->poll_interval;
  369. Curl_expire(conn->data, td->poll_interval);
  370. }
  371. return CURLE_OK;
  372. }
  373. int Curl_resolv_getsock(struct connectdata *conn,
  374. curl_socket_t *socks,
  375. int numsocks)
  376. {
  377. const struct thread_data *td =
  378. (const struct thread_data *) conn->async.os_specific;
  379. if(td && td->dummy_sock != CURL_SOCKET_BAD) {
  380. if(numsocks) {
  381. /* return one socket waiting for readable, even though this is just
  382. a dummy */
  383. socks[0] = td->dummy_sock;
  384. return GETSOCK_READSOCK(0);
  385. }
  386. }
  387. return 0;
  388. }
  389. #if !defined(HAVE_GETADDRINFO)
  390. /*
  391. * Curl_getaddrinfo() - for platforms without getaddrinfo
  392. */
  393. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  394. const char *hostname,
  395. int port,
  396. int *waitp)
  397. {
  398. struct hostent *h = NULL;
  399. struct SessionHandle *data = conn->data;
  400. struct in_addr in;
  401. *waitp = 0; /* default to synchronous response */
  402. if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
  403. /* This is a dotted IP address 123.123.123.123-style */
  404. return Curl_ip2addr(AF_INET, &in, hostname, port);
  405. /* fire up a new resolver thread! */
  406. if(init_resolve_thread(conn, hostname, port, NULL)) {
  407. *waitp = 1; /* expect asynchronous response */
  408. return NULL;
  409. }
  410. /* fall-back to blocking version */
  411. return Curl_ipv4_resolve_r(hostname, port);
  412. }
  413. #else /* HAVE_GETADDRINFO */
  414. /*
  415. * Curl_getaddrinfo() - for getaddrinfo
  416. */
  417. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  418. const char *hostname,
  419. int port,
  420. int *waitp)
  421. {
  422. struct addrinfo hints;
  423. Curl_addrinfo *res;
  424. int error;
  425. char sbuf[NI_MAXSERV];
  426. int pf = PF_INET;
  427. struct SessionHandle *data = conn->data;
  428. *waitp = 0; /* default to synchronous response */
  429. #if !defined(CURLRES_IPV4)
  430. /*
  431. * Check if a limited name resolve has been requested.
  432. */
  433. switch(data->set.ip_version) {
  434. case CURL_IPRESOLVE_V4:
  435. pf = PF_INET;
  436. break;
  437. case CURL_IPRESOLVE_V6:
  438. pf = PF_INET6;
  439. break;
  440. default:
  441. pf = PF_UNSPEC;
  442. break;
  443. }
  444. if (pf != PF_INET) {
  445. /* see if we have an IPv6 stack */
  446. curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
  447. if(s == CURL_SOCKET_BAD) {
  448. /* Some non-IPv6 stacks have been found to make very slow name resolves
  449. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  450. * the stack seems to be a non-ipv6 one. */
  451. pf = PF_INET;
  452. }
  453. else {
  454. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  455. * possible checks. And close the socket again.
  456. */
  457. sclose(s);
  458. }
  459. }
  460. #endif /* !CURLRES_IPV4 */
  461. memset(&hints, 0, sizeof(hints));
  462. hints.ai_family = pf;
  463. hints.ai_socktype = conn->socktype;
  464. #if 0 /* removed nov 8 2005 before 7.15.1 */
  465. hints.ai_flags = AI_CANONNAME;
  466. #endif
  467. snprintf(sbuf, sizeof(sbuf), "%d", port);
  468. /* fire up a new resolver thread! */
  469. if(init_resolve_thread(conn, hostname, port, &hints)) {
  470. *waitp = 1; /* expect asynchronous response */
  471. return NULL;
  472. }
  473. /* fall-back to blocking version */
  474. infof(data, "init_resolve_thread() failed for %s; %s\n",
  475. hostname, Curl_strerror(conn, ERRNO));
  476. error = Curl_getaddrinfo_ex(hostname, sbuf, &hints, &res);
  477. if(error) {
  478. infof(data, "getaddrinfo() failed for %s:%d; %s\n",
  479. hostname, port, Curl_strerror(conn, SOCKERRNO));
  480. return NULL;
  481. }
  482. return res;
  483. }
  484. #endif /* HAVE_GETADDRINFO */
  485. #endif /* CURLRES_THREADED */