hostthre.c 22 KB

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