hostthre.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <[email protected]>, 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. #define _REENTRANT
  27. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  28. #include <malloc.h>
  29. #else
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_SYS_SOCKET_H
  34. #include <sys/socket.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_NETDB_H
  40. #include <netdb.h>
  41. #endif
  42. #ifdef HAVE_ARPA_INET_H
  43. #include <arpa/inet.h>
  44. #endif
  45. #ifdef HAVE_STDLIB_H
  46. #include <stdlib.h> /* required for free() prototypes */
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h> /* for the close() proto */
  50. #endif
  51. #ifdef VMS
  52. #include <in.h>
  53. #include <inet.h>
  54. #include <stdlib.h>
  55. #endif
  56. #endif
  57. #ifdef HAVE_SETJMP_H
  58. #include <setjmp.h>
  59. #endif
  60. #ifdef WIN32
  61. #include <process.h>
  62. #endif
  63. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  64. #undef in_addr_t
  65. #define in_addr_t unsigned long
  66. #endif
  67. #include "urldata.h"
  68. #include "sendf.h"
  69. #include "hostip.h"
  70. #include "hash.h"
  71. #include "share.h"
  72. #include "strerror.h"
  73. #include "url.h"
  74. #define _MPRINTF_REPLACE /* use our functions only */
  75. #include <curl/mprintf.h>
  76. #include "inet_ntop.h"
  77. #include "curl_memory.h"
  78. /* The last #include file should be: */
  79. #include "memdebug.h"
  80. /***********************************************************************
  81. * Only for Windows threaded name resolves builds
  82. **********************************************************************/
  83. #ifdef CURLRES_THREADED
  84. /* This function is used to init a threaded resolve */
  85. static bool init_resolve_thread(struct connectdata *conn,
  86. const char *hostname, int port,
  87. const Curl_addrinfo *hints);
  88. #ifdef CURLRES_IPV4
  89. #define THREAD_FUNC gethostbyname_thread
  90. #define THREAD_NAME "gethostbyname_thread"
  91. #else
  92. #define THREAD_FUNC getaddrinfo_thread
  93. #define THREAD_NAME "getaddrinfo_thread"
  94. #endif
  95. #if defined(DEBUG_THREADING_GETHOSTBYNAME) || \
  96. defined(DEBUG_THREADING_GETADDRINFO)
  97. /* If this is defined, provide tracing */
  98. #define TRACE(args) \
  99. do { trace_it("%u: ", __LINE__); trace_it args; } while (0)
  100. static void trace_it (const char *fmt, ...)
  101. {
  102. static int do_trace = -1;
  103. va_list args;
  104. if (do_trace == -1) {
  105. const char *env = getenv("CURL_TRACE");
  106. do_trace = (env && atoi(env) > 0);
  107. }
  108. if (!do_trace)
  109. return;
  110. va_start (args, fmt);
  111. vfprintf (stderr, fmt, args);
  112. fflush (stderr);
  113. va_end (args);
  114. }
  115. #else
  116. #define TRACE(x)
  117. #endif
  118. #ifdef DEBUG_THREADING_GETADDRINFO
  119. static void dump_addrinfo (struct connectdata *conn, const struct addrinfo *ai)
  120. {
  121. TRACE(("dump_addrinfo:\n"));
  122. for ( ; ai; ai = ai->ai_next) {
  123. char buf [INET6_ADDRSTRLEN];
  124. trace_it(" fam %2d, CNAME %s, ",
  125. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  126. if (Curl_printable_address(ai, buf, sizeof(buf)))
  127. trace_it("%s\n", buf);
  128. else
  129. trace_it("failed; %s\n", Curl_strerror(conn,WSAGetLastError()));
  130. }
  131. }
  132. #endif
  133. struct thread_data {
  134. HANDLE thread_hnd;
  135. unsigned thread_id;
  136. DWORD thread_status;
  137. curl_socket_t dummy_sock; /* dummy for Curl_fdset() */
  138. FILE *stderr_file;
  139. #ifdef CURLRES_IPV6
  140. struct addrinfo hints;
  141. #endif
  142. };
  143. #if defined(CURLRES_IPV4)
  144. /*
  145. * gethostbyname_thread() resolves a name, calls the Curl_addrinfo4_callback
  146. * and then exits.
  147. *
  148. * For builds without ARES/ENABLE_IPV6, create a resolver thread and wait on
  149. * it.
  150. */
  151. static unsigned __stdcall gethostbyname_thread (void *arg)
  152. {
  153. struct connectdata *conn = (struct connectdata*) arg;
  154. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  155. struct hostent *he;
  156. int rc;
  157. /* Sharing the same _iob[] element with our parent thread should
  158. * hopefully make printouts synchronised. I'm not sure it works
  159. * with a static runtime lib (MSVC's libc.lib).
  160. */
  161. *stderr = *td->stderr_file;
  162. WSASetLastError (conn->async.status = NO_DATA); /* pending status */
  163. he = gethostbyname (conn->async.hostname);
  164. if (he) {
  165. Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
  166. rc = 1;
  167. }
  168. else {
  169. Curl_addrinfo4_callback(conn, (int)WSAGetLastError(), NULL);
  170. rc = 0;
  171. }
  172. TRACE(("Winsock-error %d, addr %s\n", conn->async.status,
  173. he ? inet_ntoa(*(struct in_addr*)he->h_addr) : "unknown"));
  174. return (rc);
  175. /* An implicit _endthreadex() here */
  176. }
  177. #elif defined(CURLRES_IPV6)
  178. /*
  179. * getaddrinfo_thread() resolves a name, calls Curl_addrinfo6_callback and then
  180. * exits.
  181. *
  182. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  183. * and wait on it.
  184. */
  185. static unsigned __stdcall getaddrinfo_thread (void *arg)
  186. {
  187. struct connectdata *conn = (struct connectdata*) arg;
  188. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  189. struct addrinfo *res;
  190. char service [NI_MAXSERV];
  191. int rc;
  192. *stderr = *td->stderr_file;
  193. itoa(conn->async.port, service, 10);
  194. WSASetLastError(conn->async.status = NO_DATA); /* pending status */
  195. rc = getaddrinfo(conn->async.hostname, service, &td->hints, &res);
  196. if (rc == 0) {
  197. #ifdef DEBUG_THREADING_GETADDRINFO
  198. dump_addrinfo (conn, res);
  199. #endif
  200. Curl_addrinfo6_callback(conn, CURL_ASYNC_SUCCESS, res);
  201. }
  202. else {
  203. Curl_addrinfo6_callback(conn, (int)WSAGetLastError(), NULL);
  204. TRACE(("Winsock-error %d, no address\n", conn->async.status));
  205. }
  206. return (rc);
  207. /* An implicit _endthreadex() here */
  208. }
  209. #endif
  210. /*
  211. * destroy_thread_data() cleans up async resolver data.
  212. * Complementary of ares_destroy.
  213. */
  214. static void destroy_thread_data (struct Curl_async *async)
  215. {
  216. if (async->hostname)
  217. free(async->hostname);
  218. if (async->os_specific) {
  219. curl_socket_t sock = ((const struct thread_data*)async->os_specific)->dummy_sock;
  220. if (sock != CURL_SOCKET_BAD)
  221. sclose(sock);
  222. free(async->os_specific);
  223. }
  224. async->hostname = NULL;
  225. async->os_specific = NULL;
  226. }
  227. /*
  228. * init_resolve_thread() starts a new thread that performs the actual
  229. * resolve. This function returns before the resolve is done.
  230. *
  231. * Returns FALSE in case of failure, otherwise TRUE.
  232. */
  233. static bool init_resolve_thread (struct connectdata *conn,
  234. const char *hostname, int port,
  235. const Curl_addrinfo *hints)
  236. {
  237. struct thread_data *td = calloc(sizeof(*td), 1);
  238. if (!td) {
  239. SetLastError(ENOMEM);
  240. return FALSE;
  241. }
  242. Curl_safefree(conn->async.hostname);
  243. conn->async.hostname = strdup(hostname);
  244. if (!conn->async.hostname) {
  245. free(td);
  246. SetLastError(ENOMEM);
  247. return FALSE;
  248. }
  249. conn->async.port = port;
  250. conn->async.done = FALSE;
  251. conn->async.status = 0;
  252. conn->async.dns = NULL;
  253. conn->async.os_specific = (void*) td;
  254. td->dummy_sock = CURL_SOCKET_BAD;
  255. td->stderr_file = stderr;
  256. td->thread_hnd = (HANDLE) _beginthreadex(NULL, 0, THREAD_FUNC,
  257. conn, 0, &td->thread_id);
  258. #ifdef CURLRES_IPV6
  259. curlassert(hints);
  260. td->hints = *hints;
  261. #else
  262. (void) hints;
  263. #endif
  264. if (!td->thread_hnd) {
  265. SetLastError(errno);
  266. TRACE(("_beginthreadex() failed; %s\n", Curl_strerror(conn,errno)));
  267. destroy_thread_data(&conn->async);
  268. return FALSE;
  269. }
  270. /* This socket is only to keep Curl_fdset() and select() happy; should never
  271. * become signalled for read/write since it's unbound but Windows needs
  272. * atleast 1 socket in select().
  273. */
  274. td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
  275. return TRUE;
  276. }
  277. /*
  278. * Curl_wait_for_resolv() waits for a resolve to finish. This function should
  279. * be avoided since using this risk getting the multi interface to "hang".
  280. *
  281. * If 'entry' is non-NULL, make it point to the resolved dns entry
  282. *
  283. * This is the version for resolves-in-a-thread.
  284. */
  285. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  286. struct Curl_dns_entry **entry)
  287. {
  288. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  289. struct SessionHandle *data = conn->data;
  290. long timeout;
  291. DWORD status, ticks;
  292. CURLcode rc;
  293. curlassert (conn && td);
  294. /* now, see if there's a connect timeout or a regular timeout to
  295. use instead of the default one */
  296. timeout =
  297. conn->data->set.connecttimeout ? conn->data->set.connecttimeout :
  298. conn->data->set.timeout ? conn->data->set.timeout :
  299. CURL_TIMEOUT_RESOLVE; /* default name resolve timeout */
  300. ticks = GetTickCount();
  301. (void)ticks;
  302. status = WaitForSingleObject(td->thread_hnd, 1000UL*timeout);
  303. if (status == WAIT_OBJECT_0 || status == WAIT_ABANDONED) {
  304. /* Thread finished before timeout; propagate Winsock error to this thread.
  305. * 'conn->async.done = TRUE' is set in Curl_addrinfo4/6_callback().
  306. */
  307. WSASetLastError(conn->async.status);
  308. GetExitCodeThread(td->thread_hnd, &td->thread_status);
  309. TRACE(("%s() status %lu, thread retval %lu, ",
  310. THREAD_NAME, status, td->thread_status));
  311. }
  312. else {
  313. conn->async.done = TRUE;
  314. td->thread_status = (DWORD)-1;
  315. TRACE(("%s() timeout, ", THREAD_NAME));
  316. }
  317. TRACE(("elapsed %lu ms\n", GetTickCount()-ticks));
  318. CloseHandle(td->thread_hnd);
  319. if(entry)
  320. *entry = conn->async.dns;
  321. rc = CURLE_OK;
  322. if (!conn->async.dns) {
  323. /* a name was not resolved */
  324. if (td->thread_status == (DWORD)-1 || conn->async.status == NO_DATA) {
  325. failf(data, "Resolving host timed out: %s", conn->host.name);
  326. rc = CURLE_OPERATION_TIMEDOUT;
  327. }
  328. else if(conn->async.done) {
  329. failf(data, "Could not resolve host: %s; %s",
  330. conn->host.name, Curl_strerror(conn,conn->async.status));
  331. rc = CURLE_COULDNT_RESOLVE_HOST;
  332. }
  333. else
  334. rc = CURLE_OPERATION_TIMEDOUT;
  335. }
  336. destroy_thread_data(&conn->async);
  337. if(CURLE_OK != rc)
  338. /* close the connection, since we must not return failure from here
  339. without cleaning up this connection properly */
  340. Curl_disconnect(conn);
  341. return (rc);
  342. }
  343. /*
  344. * Curl_is_resolved() is called repeatedly to check if a previous name resolve
  345. * request has completed. It should also make sure to time-out if the
  346. * operation seems to take too long.
  347. */
  348. CURLcode Curl_is_resolved(struct connectdata *conn,
  349. struct Curl_dns_entry **entry)
  350. {
  351. *entry = NULL;
  352. if (conn->async.done) {
  353. /* we're done */
  354. destroy_thread_data(&conn->async);
  355. if (!conn->async.dns) {
  356. TRACE(("Curl_is_resolved(): CURLE_COULDNT_RESOLVE_HOST\n"));
  357. return CURLE_COULDNT_RESOLVE_HOST;
  358. }
  359. *entry = conn->async.dns;
  360. TRACE(("resolved okay, dns %p\n", *entry));
  361. }
  362. else
  363. TRACE(("not yet\n"));
  364. return CURLE_OK;
  365. }
  366. CURLcode Curl_fdset(struct connectdata *conn,
  367. fd_set *read_fd_set,
  368. fd_set *write_fd_set,
  369. int *max_fdp)
  370. {
  371. const struct thread_data *td =
  372. (const struct thread_data *) conn->async.os_specific;
  373. if (td && td->dummy_sock != CURL_SOCKET_BAD) {
  374. FD_SET(td->dummy_sock,write_fd_set);
  375. *max_fdp = td->dummy_sock;
  376. }
  377. (void) read_fd_set;
  378. return CURLE_OK;
  379. }
  380. #ifdef CURLRES_IPV4
  381. /*
  382. * Curl_getaddrinfo() - for Windows threading without ENABLE_IPV6.
  383. */
  384. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  385. char *hostname,
  386. int port,
  387. int *waitp)
  388. {
  389. struct hostent *h;
  390. struct SessionHandle *data = conn->data;
  391. in_addr_t in;
  392. *waitp = 0; /* don't wait, we act synchronously */
  393. in = inet_addr(hostname);
  394. if (in != CURL_INADDR_NONE)
  395. /* This is a dotted IP address 123.123.123.123-style */
  396. return Curl_ip2addr(in, hostname, port);
  397. /* fire up a new resolver thread! */
  398. if (init_resolve_thread(conn, hostname, port, NULL)) {
  399. *waitp = TRUE; /* please wait for the response */
  400. return NULL;
  401. }
  402. /* fall-back to blocking version */
  403. infof(data, "init_resolve_thread() failed for %s; code %lu\n",
  404. hostname, GetLastError());
  405. h = gethostbyname(hostname);
  406. if (!h) {
  407. infof(data, "gethostbyname(2) failed for %s:%d; %s\n",
  408. hostname, port, Curl_strerror(conn,WSAGetLastError()));
  409. return NULL;
  410. }
  411. return Curl_he2ai(h, port);
  412. }
  413. #endif /* CURLRES_IPV4 */
  414. #ifdef CURLRES_IPV6
  415. /*
  416. * Curl_getaddrinfo() - for Windows threading IPv6 enabled
  417. */
  418. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  419. char *hostname,
  420. int port,
  421. int *waitp)
  422. {
  423. struct addrinfo hints, *res;
  424. int error;
  425. char sbuf[NI_MAXSERV];
  426. curl_socket_t s;
  427. int pf;
  428. struct SessionHandle *data = conn->data;
  429. *waitp = FALSE; /* default to synch response */
  430. /* see if we have an IPv6 stack */
  431. s = socket(PF_INET6, SOCK_DGRAM, 0);
  432. if (s == CURL_SOCKET_BAD) {
  433. /* Some non-IPv6 stacks have been found to make very slow name resolves
  434. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  435. * the stack seems to be a non-ipv6 one. */
  436. pf = PF_INET;
  437. }
  438. else {
  439. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  440. * possible checks. And close the socket again.
  441. */
  442. sclose(s);
  443. /*
  444. * Check if a more limited name resolve has been requested.
  445. */
  446. switch(data->set.ip_version) {
  447. case CURL_IPRESOLVE_V4:
  448. pf = PF_INET;
  449. break;
  450. case CURL_IPRESOLVE_V6:
  451. pf = PF_INET6;
  452. break;
  453. default:
  454. pf = PF_UNSPEC;
  455. break;
  456. }
  457. }
  458. memset(&hints, 0, sizeof(hints));
  459. hints.ai_family = pf;
  460. hints.ai_socktype = SOCK_STREAM;
  461. hints.ai_flags = AI_CANONNAME;
  462. itoa(port, sbuf, 10);
  463. /* fire up a new resolver thread! */
  464. if (init_resolve_thread(conn, hostname, port, &hints)) {
  465. *waitp = TRUE; /* please wait for the response */
  466. return NULL;
  467. }
  468. /* fall-back to blocking version */
  469. infof(data, "init_resolve_thread() failed for %s; code %lu\n",
  470. hostname, GetLastError());
  471. error = getaddrinfo(hostname, sbuf, &hints, &res);
  472. if (error) {
  473. infof(data, "getaddrinfo() failed for %s:%d; %s\n",
  474. hostname, port, Curl_strerror(conn,WSAGetLastError()));
  475. return NULL;
  476. }
  477. return res;
  478. }
  479. #endif /* CURLRES_IPV6 */
  480. #endif /* CURLRES_THREADED */