hostthre.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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 NEED_MALLOC_H
  27. #include <malloc.h>
  28. #endif
  29. #ifdef HAVE_SYS_TYPES_H
  30. #include <sys/types.h>
  31. #endif
  32. #ifdef HAVE_SYS_SOCKET_H
  33. #include <sys/socket.h>
  34. #endif
  35. #ifdef HAVE_NETINET_IN_H
  36. #include <netinet/in.h>
  37. #endif
  38. #ifdef HAVE_NETDB_H
  39. #include <netdb.h>
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #ifdef HAVE_STDLIB_H
  45. #include <stdlib.h> /* required for free() prototypes */
  46. #endif
  47. #ifdef HAVE_UNISTD_H
  48. #include <unistd.h> /* for the close() proto */
  49. #endif
  50. #ifdef VMS
  51. #include <in.h>
  52. #include <inet.h>
  53. #include <stdlib.h>
  54. #endif
  55. #ifdef HAVE_SETJMP_H
  56. #include <setjmp.h>
  57. #endif
  58. #ifdef HAVE_PROCESS_H
  59. #include <process.h>
  60. #endif
  61. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  62. #undef in_addr_t
  63. #define in_addr_t unsigned long
  64. #endif
  65. #include "urldata.h"
  66. #include "sendf.h"
  67. #include "hostip.h"
  68. #include "hash.h"
  69. #include "share.h"
  70. #include "strerror.h"
  71. #include "url.h"
  72. #include "multiif.h"
  73. #define _MPRINTF_REPLACE /* use our functions only */
  74. #include <curl/mprintf.h>
  75. #include "inet_ntop.h"
  76. #include "memory.h"
  77. /* The last #include file should be: */
  78. #include "memdebug.h"
  79. #if defined(_MSC_VER) && defined(CURL_NO__BEGINTHREADEX)
  80. #pragma message ("No _beginthreadex() available in this RTL")
  81. #endif
  82. /***********************************************************************
  83. * Only for Windows threaded name resolves builds
  84. **********************************************************************/
  85. #ifdef CURLRES_THREADED
  86. /* This function is used to init a threaded resolve */
  87. static bool init_resolve_thread(struct connectdata *conn,
  88. const char *hostname, int port,
  89. const Curl_addrinfo *hints);
  90. #ifdef CURLRES_IPV4
  91. #define THREAD_FUNC gethostbyname_thread
  92. #define THREAD_NAME "gethostbyname_thread"
  93. #else
  94. #define THREAD_FUNC getaddrinfo_thread
  95. #define THREAD_NAME "getaddrinfo_thread"
  96. #endif
  97. #if defined(DEBUG_THREADING_GETHOSTBYNAME) || \
  98. defined(DEBUG_THREADING_GETADDRINFO)
  99. /* If this is defined, provide tracing */
  100. #define TRACE(args) \
  101. do { trace_it("%u: ", __LINE__); trace_it args; } while (0)
  102. static void trace_it (const char *fmt, ...)
  103. {
  104. static int do_trace = -1;
  105. va_list args;
  106. if (do_trace == -1) {
  107. const char *env = getenv("CURL_TRACE");
  108. do_trace = (env && atoi(env) > 0);
  109. }
  110. if (!do_trace)
  111. return;
  112. va_start (args, fmt);
  113. vfprintf (stderr, fmt, args);
  114. fflush (stderr);
  115. va_end (args);
  116. }
  117. #else
  118. #define TRACE(x)
  119. #endif
  120. #ifdef DEBUG_THREADING_GETADDRINFO
  121. static void dump_addrinfo (struct connectdata *conn, const struct addrinfo *ai)
  122. {
  123. TRACE(("dump_addrinfo:\n"));
  124. for ( ; ai; ai = ai->ai_next) {
  125. char buf [INET6_ADDRSTRLEN];
  126. trace_it(" fam %2d, CNAME %s, ",
  127. ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
  128. if (Curl_printable_address(ai, buf, sizeof(buf)))
  129. trace_it("%s\n", buf);
  130. else
  131. trace_it("failed; %s\n", Curl_strerror(conn,WSAGetLastError()));
  132. }
  133. }
  134. #endif
  135. struct thread_data {
  136. HANDLE thread_hnd;
  137. unsigned thread_id;
  138. DWORD thread_status;
  139. curl_socket_t dummy_sock; /* dummy for Curl_resolv_fdset() */
  140. HANDLE mutex_waiting; /* marks that we are still waiting for a resolve */
  141. HANDLE event_resolved; /* marks that the thread obtained the information */
  142. HANDLE event_thread_started; /* marks that the thread has initialized and
  143. started */
  144. HANDLE mutex_terminate; /* serializes access to flag_terminate */
  145. HANDLE event_terminate; /* flag for thread to terminate instead of calling
  146. callbacks */
  147. #ifdef CURLRES_IPV6
  148. struct addrinfo hints;
  149. #endif
  150. };
  151. /* Data for synchronization between resolver thread and its parent */
  152. struct thread_sync_data {
  153. HANDLE mutex_waiting; /* thread_data.mutex_waiting duplicate */
  154. HANDLE mutex_terminate; /* thread_data.mutex_terminate duplicate */
  155. HANDLE event_terminate; /* thread_data.event_terminate duplicate */
  156. char * hostname; /* hostname to resolve, Curl_async.hostname
  157. duplicate */
  158. };
  159. /* Destroy resolver thread synchronization data */
  160. static
  161. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  162. {
  163. if (tsd->hostname) {
  164. free(tsd->hostname);
  165. tsd->hostname = NULL;
  166. }
  167. if (tsd->event_terminate) {
  168. CloseHandle(tsd->event_terminate);
  169. tsd->event_terminate = NULL;
  170. }
  171. if (tsd->mutex_terminate) {
  172. CloseHandle(tsd->mutex_terminate);
  173. tsd->mutex_terminate = NULL;
  174. }
  175. if (tsd->mutex_waiting) {
  176. CloseHandle(tsd->mutex_waiting);
  177. tsd->mutex_waiting = NULL;
  178. }
  179. }
  180. /* Initialize resolver thread synchronization data */
  181. static
  182. BOOL init_thread_sync_data(struct thread_data * td,
  183. char * hostname,
  184. struct thread_sync_data * tsd)
  185. {
  186. HANDLE curr_proc = GetCurrentProcess();
  187. memset(tsd, 0, sizeof(*tsd));
  188. if (!DuplicateHandle(curr_proc, td->mutex_waiting,
  189. curr_proc, &tsd->mutex_waiting, 0, FALSE,
  190. DUPLICATE_SAME_ACCESS)) {
  191. /* failed to duplicate the mutex, no point in continuing */
  192. destroy_thread_sync_data(tsd);
  193. return FALSE;
  194. }
  195. if (!DuplicateHandle(curr_proc, td->mutex_terminate,
  196. curr_proc, &tsd->mutex_terminate, 0, FALSE,
  197. DUPLICATE_SAME_ACCESS)) {
  198. /* failed to duplicate the mutex, no point in continuing */
  199. destroy_thread_sync_data(tsd);
  200. return FALSE;
  201. }
  202. if (!DuplicateHandle(curr_proc, td->event_terminate,
  203. curr_proc, &tsd->event_terminate, 0, FALSE,
  204. DUPLICATE_SAME_ACCESS)) {
  205. /* failed to duplicate the event, no point in continuing */
  206. destroy_thread_sync_data(tsd);
  207. return FALSE;
  208. }
  209. /* Copying hostname string because original can be destroyed by parent
  210. * thread during gethostbyname execution.
  211. */
  212. tsd->hostname = strdup(hostname);
  213. if (!tsd->hostname) {
  214. /* Memory allocation failed */
  215. destroy_thread_sync_data(tsd);
  216. return FALSE;
  217. }
  218. return TRUE;
  219. }
  220. /* acquire resolver thread synchronization */
  221. static
  222. BOOL acquire_thread_sync(struct thread_sync_data * tsd)
  223. {
  224. /* is the thread initiator still waiting for us ? */
  225. if (WaitForSingleObject(tsd->mutex_waiting, 0) == WAIT_TIMEOUT) {
  226. /* yes, it is */
  227. /* Waiting access to event_terminate */
  228. if (WaitForSingleObject(tsd->mutex_terminate, INFINITE) != WAIT_OBJECT_0) {
  229. /* Something went wrong - now just ignoring */
  230. }
  231. else {
  232. if (WaitForSingleObject(tsd->event_terminate, 0) != WAIT_TIMEOUT) {
  233. /* Parent thread signaled us to terminate.
  234. * This means that all data in conn->async is now destroyed
  235. * and we cannot use it.
  236. */
  237. }
  238. else {
  239. return TRUE;
  240. }
  241. }
  242. }
  243. return FALSE;
  244. }
  245. /* release resolver thread synchronization */
  246. static
  247. void release_thread_sync(struct thread_sync_data * tsd)
  248. {
  249. ReleaseMutex(tsd->mutex_terminate);
  250. }
  251. #if defined(CURLRES_IPV4)
  252. /*
  253. * gethostbyname_thread() resolves a name, calls the Curl_addrinfo4_callback
  254. * and then exits.
  255. *
  256. * For builds without ARES/ENABLE_IPV6, create a resolver thread and wait on
  257. * it.
  258. */
  259. static unsigned __stdcall gethostbyname_thread (void *arg)
  260. {
  261. struct connectdata *conn = (struct connectdata*) arg;
  262. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  263. struct hostent *he;
  264. int rc = 0;
  265. /* Duplicate the passed mutex and event handles.
  266. * This allows us to use it even after the container gets destroyed
  267. * due to a resolver timeout.
  268. */
  269. struct thread_sync_data tsd = { 0,0,0,NULL };
  270. if (!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
  271. /* thread synchronization data initialization failed */
  272. return (unsigned)-1;
  273. }
  274. WSASetLastError (conn->async.status = NO_DATA); /* pending status */
  275. /* Signaling that we have initialized all copies of data and handles we
  276. need */
  277. SetEvent(td->event_thread_started);
  278. he = gethostbyname (tsd.hostname);
  279. /* is parent thread waiting for us and are we able to access conn members? */
  280. if (acquire_thread_sync(&tsd)) {
  281. /* Mark that we have obtained the information, and that we are calling
  282. * back with it. */
  283. SetEvent(td->event_resolved);
  284. if (he) {
  285. rc = Curl_addrinfo4_callback(conn, CURL_ASYNC_SUCCESS, he);
  286. }
  287. else {
  288. rc = Curl_addrinfo4_callback(conn, (int)WSAGetLastError(), NULL);
  289. }
  290. TRACE(("Winsock-error %d, addr %s\n", conn->async.status,
  291. he ? inet_ntoa(*(struct in_addr*)he->h_addr) : "unknown"));
  292. release_thread_sync(&tsd);
  293. }
  294. /* clean up */
  295. destroy_thread_sync_data(&tsd);
  296. return (rc);
  297. /* An implicit _endthreadex() here */
  298. }
  299. #elif defined(CURLRES_IPV6)
  300. /*
  301. * getaddrinfo_thread() resolves a name, calls Curl_addrinfo6_callback and then
  302. * exits.
  303. *
  304. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  305. * and wait on it.
  306. */
  307. static unsigned __stdcall getaddrinfo_thread (void *arg)
  308. {
  309. struct connectdata *conn = (struct connectdata*) arg;
  310. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  311. struct addrinfo *res;
  312. char service [NI_MAXSERV];
  313. int rc;
  314. struct addrinfo hints = td->hints;
  315. /* Duplicate the passed mutex handle.
  316. * This allows us to use it even after the container gets destroyed
  317. * due to a resolver timeout.
  318. */
  319. struct thread_sync_data tsd = { 0,0,0,NULL };
  320. if (!init_thread_sync_data(td, conn->async.hostname, &tsd)) {
  321. /* thread synchronization data initialization failed */
  322. return -1;
  323. }
  324. itoa(conn->async.port, service, 10);
  325. WSASetLastError(conn->async.status = NO_DATA); /* pending status */
  326. /* Signaling that we have initialized all copies of data and handles we
  327. need */
  328. SetEvent(td->event_thread_started);
  329. rc = getaddrinfo(tsd.hostname, service, &hints, &res);
  330. /* is parent thread waiting for us and are we able to access conn members? */
  331. if (acquire_thread_sync(&tsd)) {
  332. /* Mark that we have obtained the information, and that we are calling
  333. back with it. */
  334. SetEvent(td->event_resolved);
  335. if (rc == 0) {
  336. #ifdef DEBUG_THREADING_GETADDRINFO
  337. dump_addrinfo (conn, res);
  338. #endif
  339. rc = Curl_addrinfo6_callback(conn, CURL_ASYNC_SUCCESS, res);
  340. }
  341. else {
  342. rc = Curl_addrinfo6_callback(conn, (int)WSAGetLastError(), NULL);
  343. TRACE(("Winsock-error %d, no address\n", conn->async.status));
  344. }
  345. release_thread_sync(&tsd);
  346. }
  347. /* clean up */
  348. destroy_thread_sync_data(&tsd);
  349. return (rc);
  350. /* An implicit _endthreadex() here */
  351. }
  352. #endif
  353. /*
  354. * Curl_destroy_thread_data() cleans up async resolver data and thread handle.
  355. * Complementary of ares_destroy.
  356. */
  357. void Curl_destroy_thread_data (struct Curl_async *async)
  358. {
  359. if (async->hostname)
  360. free(async->hostname);
  361. if (async->os_specific) {
  362. struct thread_data *td = (struct thread_data*) async->os_specific;
  363. curl_socket_t sock = td->dummy_sock;
  364. if (td->mutex_terminate && td->event_terminate) {
  365. /* Signaling resolver thread to terminate */
  366. if (WaitForSingleObject(td->mutex_terminate, INFINITE) == WAIT_OBJECT_0) {
  367. SetEvent(td->event_terminate);
  368. ReleaseMutex(td->mutex_terminate);
  369. }
  370. else {
  371. /* Something went wrong - just ignoring it */
  372. }
  373. }
  374. if (td->mutex_terminate)
  375. CloseHandle(td->mutex_terminate);
  376. if (td->event_terminate)
  377. CloseHandle(td->event_terminate);
  378. if (td->event_thread_started)
  379. CloseHandle(td->event_thread_started);
  380. if (sock != CURL_SOCKET_BAD)
  381. sclose(sock);
  382. /* destroy the synchronization objects */
  383. if (td->mutex_waiting)
  384. CloseHandle(td->mutex_waiting);
  385. td->mutex_waiting = NULL;
  386. if (td->event_resolved)
  387. CloseHandle(td->event_resolved);
  388. if (td->thread_hnd)
  389. CloseHandle(td->thread_hnd);
  390. free(async->os_specific);
  391. }
  392. async->hostname = NULL;
  393. async->os_specific = NULL;
  394. }
  395. /*
  396. * init_resolve_thread() starts a new thread that performs the actual
  397. * resolve. This function returns before the resolve is done.
  398. *
  399. * Returns FALSE in case of failure, otherwise TRUE.
  400. */
  401. static bool init_resolve_thread (struct connectdata *conn,
  402. const char *hostname, int port,
  403. const Curl_addrinfo *hints)
  404. {
  405. struct thread_data *td = calloc(sizeof(*td), 1);
  406. HANDLE thread_and_event[2] = {0};
  407. if (!td) {
  408. SetLastError(ENOMEM);
  409. return FALSE;
  410. }
  411. Curl_safefree(conn->async.hostname);
  412. conn->async.hostname = strdup(hostname);
  413. if (!conn->async.hostname) {
  414. free(td);
  415. SetLastError(ENOMEM);
  416. return FALSE;
  417. }
  418. conn->async.port = port;
  419. conn->async.done = FALSE;
  420. conn->async.status = 0;
  421. conn->async.dns = NULL;
  422. conn->async.os_specific = (void*) td;
  423. td->dummy_sock = CURL_SOCKET_BAD;
  424. /* Create the mutex used to inform the resolver thread that we're
  425. * still waiting, and take initial ownership.
  426. */
  427. td->mutex_waiting = CreateMutex(NULL, TRUE, NULL);
  428. if (td->mutex_waiting == NULL) {
  429. Curl_destroy_thread_data(&conn->async);
  430. SetLastError(EAGAIN);
  431. return FALSE;
  432. }
  433. /* Create the event that the thread uses to inform us that it's
  434. * done resolving. Do not signal it.
  435. */
  436. td->event_resolved = CreateEvent(NULL, TRUE, FALSE, NULL);
  437. if (td->event_resolved == NULL) {
  438. Curl_destroy_thread_data(&conn->async);
  439. SetLastError(EAGAIN);
  440. return FALSE;
  441. }
  442. /* Create the mutex used to serialize access to event_terminated
  443. * between us and resolver thread.
  444. */
  445. td->mutex_terminate = CreateMutex(NULL, FALSE, NULL);
  446. if (td->mutex_terminate == NULL) {
  447. Curl_destroy_thread_data(&conn->async);
  448. SetLastError(EAGAIN);
  449. return FALSE;
  450. }
  451. /* Create the event used to signal thread that it should terminate.
  452. */
  453. td->event_terminate = CreateEvent(NULL, TRUE, FALSE, NULL);
  454. if (td->event_terminate == NULL) {
  455. Curl_destroy_thread_data(&conn->async);
  456. SetLastError(EAGAIN);
  457. return FALSE;
  458. }
  459. /* Create the event used by thread to inform it has initialized its own data.
  460. */
  461. td->event_thread_started = CreateEvent(NULL, TRUE, FALSE, NULL);
  462. if (td->event_thread_started == NULL) {
  463. Curl_destroy_thread_data(&conn->async);
  464. SetLastError(EAGAIN);
  465. return FALSE;
  466. }
  467. #ifdef _WIN32_WCE
  468. td->thread_hnd = (HANDLE) CreateThread(NULL, 0,
  469. (LPTHREAD_START_ROUTINE) THREAD_FUNC,
  470. conn, 0, &td->thread_id);
  471. #else
  472. td->thread_hnd = (HANDLE) _beginthreadex(NULL, 0, THREAD_FUNC,
  473. conn, 0, &td->thread_id);
  474. #endif
  475. #ifdef CURLRES_IPV6
  476. curlassert(hints);
  477. td->hints = *hints;
  478. #else
  479. (void) hints;
  480. #endif
  481. if (!td->thread_hnd) {
  482. #ifdef _WIN32_WCE
  483. TRACE(("CreateThread() failed; %s\n", Curl_strerror(conn,GetLastError())));
  484. #else
  485. SetLastError(errno);
  486. TRACE(("_beginthreadex() failed; %s\n", Curl_strerror(conn,errno)));
  487. #endif
  488. Curl_destroy_thread_data(&conn->async);
  489. return FALSE;
  490. }
  491. /* Waiting until the thread will initialize its data or it will exit due errors.
  492. */
  493. thread_and_event[0] = td->thread_hnd;
  494. thread_and_event[1] = td->event_thread_started;
  495. if (WaitForMultipleObjects(sizeof(thread_and_event) /
  496. sizeof(thread_and_event[0]),
  497. (const HANDLE*)thread_and_event, FALSE,
  498. INFINITE) == WAIT_FAILED) {
  499. /* The resolver thread has been created,
  500. * most probably it works now - ignoring this "minor" error
  501. */
  502. }
  503. /* This socket is only to keep Curl_resolv_fdset() and select() happy;
  504. * should never become signalled for read/write since it's unbound but
  505. * Windows needs atleast 1 socket in select().
  506. */
  507. td->dummy_sock = socket(AF_INET, SOCK_DGRAM, 0);
  508. return TRUE;
  509. }
  510. /*
  511. * Curl_wait_for_resolv() waits for a resolve to finish. This function should
  512. * be avoided since using this risk getting the multi interface to "hang".
  513. *
  514. * If 'entry' is non-NULL, make it point to the resolved dns entry
  515. *
  516. * This is the version for resolves-in-a-thread.
  517. */
  518. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  519. struct Curl_dns_entry **entry)
  520. {
  521. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  522. struct SessionHandle *data = conn->data;
  523. long timeout;
  524. DWORD status, ticks;
  525. CURLcode rc;
  526. curlassert (conn && td);
  527. /* now, see if there's a connect timeout or a regular timeout to
  528. use instead of the default one */
  529. timeout =
  530. conn->data->set.connecttimeout ? conn->data->set.connecttimeout :
  531. conn->data->set.timeout ? conn->data->set.timeout :
  532. CURL_TIMEOUT_RESOLVE; /* default name resolve timeout */
  533. ticks = GetTickCount();
  534. /* wait for the thread to resolve the name */
  535. status = WaitForSingleObject(td->event_resolved, 1000UL*timeout);
  536. /* mark that we are now done waiting */
  537. ReleaseMutex(td->mutex_waiting);
  538. /* close our handle to the mutex, no point in hanging on to it */
  539. CloseHandle(td->mutex_waiting);
  540. td->mutex_waiting = NULL;
  541. /* close the event handle, it's useless now */
  542. CloseHandle(td->event_resolved);
  543. td->event_resolved = NULL;
  544. /* has the resolver thread succeeded in resolving our query ? */
  545. if (status == WAIT_OBJECT_0) {
  546. /* wait for the thread to exit, it's in the callback sequence */
  547. if (WaitForSingleObject(td->thread_hnd, 5000) == WAIT_TIMEOUT) {
  548. TerminateThread(td->thread_hnd, 0);
  549. conn->async.done = TRUE;
  550. td->thread_status = (DWORD)-1;
  551. TRACE(("%s() thread stuck?!, ", THREAD_NAME));
  552. }
  553. else {
  554. /* Thread finished before timeout; propagate Winsock error to this
  555. * thread. 'conn->async.done = TRUE' is set in
  556. * Curl_addrinfo4/6_callback().
  557. */
  558. WSASetLastError(conn->async.status);
  559. GetExitCodeThread(td->thread_hnd, &td->thread_status);
  560. TRACE(("%s() status %lu, thread retval %lu, ",
  561. THREAD_NAME, status, td->thread_status));
  562. }
  563. }
  564. else {
  565. conn->async.done = TRUE;
  566. td->thread_status = (DWORD)-1;
  567. TRACE(("%s() timeout, ", THREAD_NAME));
  568. }
  569. TRACE(("elapsed %lu ms\n", GetTickCount()-ticks));
  570. if(entry)
  571. *entry = conn->async.dns;
  572. rc = CURLE_OK;
  573. if (!conn->async.dns) {
  574. /* a name was not resolved */
  575. if (td->thread_status == CURLE_OUT_OF_MEMORY) {
  576. rc = CURLE_OUT_OF_MEMORY;
  577. failf(data, "Could not resolve host: %s", curl_easy_strerror(rc));
  578. }
  579. else if(conn->async.done) {
  580. if(conn->bits.httpproxy) {
  581. failf(data, "Could not resolve proxy: %s; %s",
  582. conn->proxy.dispname, Curl_strerror(conn, conn->async.status));
  583. rc = CURLE_COULDNT_RESOLVE_PROXY;
  584. }
  585. else {
  586. failf(data, "Could not resolve host: %s; %s",
  587. conn->host.name, Curl_strerror(conn, conn->async.status));
  588. rc = CURLE_COULDNT_RESOLVE_HOST;
  589. }
  590. }
  591. else if (td->thread_status == (DWORD)-1 || conn->async.status == NO_DATA) {
  592. failf(data, "Resolving host timed out: %s", conn->host.name);
  593. rc = CURLE_OPERATION_TIMEDOUT;
  594. }
  595. else
  596. rc = CURLE_OPERATION_TIMEDOUT;
  597. }
  598. Curl_destroy_thread_data(&conn->async);
  599. if(!conn->async.dns)
  600. conn->bits.close = TRUE;
  601. return (rc);
  602. }
  603. /*
  604. * Curl_is_resolved() is called repeatedly to check if a previous name resolve
  605. * request has completed. It should also make sure to time-out if the
  606. * operation seems to take too long.
  607. */
  608. CURLcode Curl_is_resolved(struct connectdata *conn,
  609. struct Curl_dns_entry **entry)
  610. {
  611. *entry = NULL;
  612. if (conn->async.done) {
  613. /* we're done */
  614. Curl_destroy_thread_data(&conn->async);
  615. if (!conn->async.dns) {
  616. TRACE(("Curl_is_resolved(): CURLE_COULDNT_RESOLVE_HOST\n"));
  617. return CURLE_COULDNT_RESOLVE_HOST;
  618. }
  619. *entry = conn->async.dns;
  620. TRACE(("resolved okay, dns %p\n", *entry));
  621. }
  622. return CURLE_OK;
  623. }
  624. int Curl_resolv_getsock(struct connectdata *conn,
  625. curl_socket_t *socks,
  626. int numsocks)
  627. {
  628. const struct thread_data *td =
  629. (const struct thread_data *) conn->async.os_specific;
  630. if (td && td->dummy_sock != CURL_SOCKET_BAD) {
  631. if(numsocks) {
  632. /* return one socket waiting for writable, even though this is just
  633. a dummy */
  634. socks[0] = td->dummy_sock;
  635. return GETSOCK_WRITESOCK(0);
  636. }
  637. }
  638. return 0;
  639. }
  640. #ifdef CURLRES_IPV4
  641. /*
  642. * Curl_getaddrinfo() - for Windows threading without ENABLE_IPV6.
  643. */
  644. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  645. const char *hostname,
  646. int port,
  647. int *waitp)
  648. {
  649. struct hostent *h = NULL;
  650. struct SessionHandle *data = conn->data;
  651. in_addr_t in;
  652. *waitp = 0; /* don't wait, we act synchronously */
  653. in = inet_addr(hostname);
  654. if (in != CURL_INADDR_NONE)
  655. /* This is a dotted IP address 123.123.123.123-style */
  656. return Curl_ip2addr(in, hostname, port);
  657. /* fire up a new resolver thread! */
  658. if (init_resolve_thread(conn, hostname, port, NULL)) {
  659. *waitp = TRUE; /* please wait for the response */
  660. return NULL;
  661. }
  662. /* fall-back to blocking version */
  663. infof(data, "init_resolve_thread() failed for %s; %s\n",
  664. hostname, Curl_strerror(conn,GetLastError()));
  665. h = gethostbyname(hostname);
  666. if (!h) {
  667. infof(data, "gethostbyname(2) failed for %s:%d; %s\n",
  668. hostname, port, Curl_strerror(conn,WSAGetLastError()));
  669. return NULL;
  670. }
  671. return Curl_he2ai(h, port);
  672. }
  673. #endif /* CURLRES_IPV4 */
  674. #ifdef CURLRES_IPV6
  675. /*
  676. * Curl_getaddrinfo() - for Windows threading IPv6 enabled
  677. */
  678. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  679. const char *hostname,
  680. int port,
  681. int *waitp)
  682. {
  683. struct addrinfo hints, *res;
  684. int error;
  685. char sbuf[NI_MAXSERV];
  686. curl_socket_t s;
  687. int pf;
  688. struct SessionHandle *data = conn->data;
  689. *waitp = FALSE; /* default to synch response */
  690. /* see if we have an IPv6 stack */
  691. s = socket(PF_INET6, SOCK_DGRAM, 0);
  692. if (s == CURL_SOCKET_BAD) {
  693. /* Some non-IPv6 stacks have been found to make very slow name resolves
  694. * when PF_UNSPEC is used, so thus we switch to a mere PF_INET lookup if
  695. * the stack seems to be a non-ipv6 one. */
  696. pf = PF_INET;
  697. }
  698. else {
  699. /* This seems to be an IPv6-capable stack, use PF_UNSPEC for the widest
  700. * possible checks. And close the socket again.
  701. */
  702. sclose(s);
  703. /*
  704. * Check if a more limited name resolve has been requested.
  705. */
  706. switch(data->set.ip_version) {
  707. case CURL_IPRESOLVE_V4:
  708. pf = PF_INET;
  709. break;
  710. case CURL_IPRESOLVE_V6:
  711. pf = PF_INET6;
  712. break;
  713. default:
  714. pf = PF_UNSPEC;
  715. break;
  716. }
  717. }
  718. memset(&hints, 0, sizeof(hints));
  719. hints.ai_family = pf;
  720. hints.ai_socktype = conn->socktype;
  721. #if 0 /* removed nov 8 2005 before 7.15.1 */
  722. hints.ai_flags = AI_CANONNAME;
  723. #endif
  724. itoa(port, sbuf, 10);
  725. /* fire up a new resolver thread! */
  726. if (init_resolve_thread(conn, hostname, port, &hints)) {
  727. *waitp = TRUE; /* please wait for the response */
  728. return NULL;
  729. }
  730. /* fall-back to blocking version */
  731. infof(data, "init_resolve_thread() failed for %s; %s\n",
  732. hostname, Curl_strerror(conn,GetLastError()));
  733. error = getaddrinfo(hostname, sbuf, &hints, &res);
  734. if (error) {
  735. infof(data, "getaddrinfo() failed for %s:%d; %s\n",
  736. hostname, port, Curl_strerror(conn,WSAGetLastError()));
  737. return NULL;
  738. }
  739. return res;
  740. }
  741. #endif /* CURLRES_IPV6 */
  742. #endif /* CURLRES_THREADED */