hostthre.c 22 KB

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