asyn-thread.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include "socketpair.h"
  26. /***********************************************************************
  27. * Only for threaded name resolves builds
  28. **********************************************************************/
  29. #ifdef CURLRES_THREADED
  30. #ifdef HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_NETDB_H
  34. #include <netdb.h>
  35. #endif
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <arpa/inet.h>
  38. #endif
  39. #ifdef __VMS
  40. #include <in.h>
  41. #include <inet.h>
  42. #endif
  43. #if defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
  44. # include <pthread.h>
  45. #endif
  46. #ifdef HAVE_GETADDRINFO
  47. # define RESOLVER_ENOMEM EAI_MEMORY
  48. #else
  49. # define RESOLVER_ENOMEM ENOMEM
  50. #endif
  51. #include "system_win32.h"
  52. #include "urldata.h"
  53. #include "sendf.h"
  54. #include "hostip.h"
  55. #include "hash.h"
  56. #include "share.h"
  57. #include "url.h"
  58. #include "multiif.h"
  59. #include "inet_ntop.h"
  60. #include "curl_threads.h"
  61. #include "connect.h"
  62. /* The last 3 #include files should be in this order */
  63. #include "curl_printf.h"
  64. #include "curl_memory.h"
  65. #include "memdebug.h"
  66. struct resdata {
  67. struct curltime start;
  68. };
  69. /*
  70. * Curl_resolver_global_init()
  71. * Called from curl_global_init() to initialize global resolver environment.
  72. * Does nothing here.
  73. */
  74. int Curl_resolver_global_init(void)
  75. {
  76. return CURLE_OK;
  77. }
  78. /*
  79. * Curl_resolver_global_cleanup()
  80. * Called from curl_global_cleanup() to destroy global resolver environment.
  81. * Does nothing here.
  82. */
  83. void Curl_resolver_global_cleanup(void)
  84. {
  85. }
  86. /*
  87. * Curl_resolver_init()
  88. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  89. * URL-state specific environment ('resolver' member of the UrlState
  90. * structure).
  91. */
  92. CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
  93. {
  94. (void)easy;
  95. *resolver = calloc(1, sizeof(struct resdata));
  96. if(!*resolver)
  97. return CURLE_OUT_OF_MEMORY;
  98. return CURLE_OK;
  99. }
  100. /*
  101. * Curl_resolver_cleanup()
  102. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  103. * URL-state specific environment ('resolver' member of the UrlState
  104. * structure).
  105. */
  106. void Curl_resolver_cleanup(void *resolver)
  107. {
  108. free(resolver);
  109. }
  110. /*
  111. * Curl_resolver_duphandle()
  112. * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
  113. * environment ('resolver' member of the UrlState structure).
  114. */
  115. CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
  116. {
  117. (void)from;
  118. return Curl_resolver_init(easy, to);
  119. }
  120. static void destroy_async_data(struct Curl_async *);
  121. /*
  122. * Cancel all possibly still on-going resolves for this connection.
  123. */
  124. void Curl_resolver_cancel(struct Curl_easy *data)
  125. {
  126. destroy_async_data(&data->state.async);
  127. }
  128. /* This function is used to init a threaded resolve */
  129. static bool init_resolve_thread(struct Curl_easy *data,
  130. const char *hostname, int port,
  131. const struct addrinfo *hints);
  132. #ifdef _WIN32
  133. /* Thread sync data used by GetAddrInfoExW for win8+ */
  134. struct thread_sync_data_w8
  135. {
  136. OVERLAPPED overlapped;
  137. ADDRINFOEXW_ *res;
  138. HANDLE cancel_ev;
  139. ADDRINFOEXW_ hints;
  140. };
  141. #endif
  142. /* Data for synchronization between resolver thread and its parent */
  143. struct thread_sync_data {
  144. #ifdef _WIN32
  145. struct thread_sync_data_w8 w8;
  146. #endif
  147. curl_mutex_t *mtx;
  148. int done;
  149. int port;
  150. char *hostname; /* hostname to resolve, Curl_async.hostname
  151. duplicate */
  152. #ifndef CURL_DISABLE_SOCKETPAIR
  153. struct Curl_easy *data;
  154. curl_socket_t sock_pair[2]; /* socket pair */
  155. #endif
  156. int sock_error;
  157. struct Curl_addrinfo *res;
  158. #ifdef HAVE_GETADDRINFO
  159. struct addrinfo hints;
  160. #endif
  161. struct thread_data *td; /* for thread-self cleanup */
  162. };
  163. struct thread_data {
  164. #ifdef _WIN32
  165. HANDLE complete_ev;
  166. #endif
  167. curl_thread_t thread_hnd;
  168. unsigned int poll_interval;
  169. timediff_t interval_end;
  170. struct thread_sync_data tsd;
  171. };
  172. static struct thread_sync_data *conn_thread_sync_data(struct Curl_easy *data)
  173. {
  174. return &(data->state.async.tdata->tsd);
  175. }
  176. /* Destroy resolver thread synchronization data */
  177. static
  178. void destroy_thread_sync_data(struct thread_sync_data *tsd)
  179. {
  180. if(tsd->mtx) {
  181. Curl_mutex_destroy(tsd->mtx);
  182. free(tsd->mtx);
  183. }
  184. free(tsd->hostname);
  185. if(tsd->res)
  186. Curl_freeaddrinfo(tsd->res);
  187. #ifndef CURL_DISABLE_SOCKETPAIR
  188. /*
  189. * close one end of the socket pair (may be done in resolver thread);
  190. * the other end (for reading) is always closed in the parent thread.
  191. */
  192. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  193. wakeup_close(tsd->sock_pair[1]);
  194. }
  195. #endif
  196. memset(tsd, 0, sizeof(*tsd));
  197. }
  198. /* Initialize resolver thread synchronization data */
  199. static
  200. int init_thread_sync_data(struct thread_data *td,
  201. const char *hostname,
  202. int port,
  203. const struct addrinfo *hints)
  204. {
  205. struct thread_sync_data *tsd = &td->tsd;
  206. memset(tsd, 0, sizeof(*tsd));
  207. tsd->td = td;
  208. tsd->port = port;
  209. /* Treat the request as done until the thread actually starts so any early
  210. * cleanup gets done properly.
  211. */
  212. tsd->done = 1;
  213. #ifdef HAVE_GETADDRINFO
  214. DEBUGASSERT(hints);
  215. tsd->hints = *hints;
  216. #else
  217. (void) hints;
  218. #endif
  219. tsd->mtx = malloc(sizeof(curl_mutex_t));
  220. if(!tsd->mtx)
  221. goto err_exit;
  222. Curl_mutex_init(tsd->mtx);
  223. #ifndef CURL_DISABLE_SOCKETPAIR
  224. /* create socket pair or pipe */
  225. if(wakeup_create(&tsd->sock_pair[0]) < 0) {
  226. tsd->sock_pair[0] = CURL_SOCKET_BAD;
  227. tsd->sock_pair[1] = CURL_SOCKET_BAD;
  228. goto err_exit;
  229. }
  230. #endif
  231. tsd->sock_error = CURL_ASYNC_SUCCESS;
  232. /* Copying hostname string because original can be destroyed by parent
  233. * thread during gethostbyname execution.
  234. */
  235. tsd->hostname = strdup(hostname);
  236. if(!tsd->hostname)
  237. goto err_exit;
  238. return 1;
  239. err_exit:
  240. #ifndef CURL_DISABLE_SOCKETPAIR
  241. if(tsd->sock_pair[0] != CURL_SOCKET_BAD) {
  242. wakeup_close(tsd->sock_pair[0]);
  243. tsd->sock_pair[0] = CURL_SOCKET_BAD;
  244. }
  245. #endif
  246. destroy_thread_sync_data(tsd);
  247. return 0;
  248. }
  249. static CURLcode getaddrinfo_complete(struct Curl_easy *data)
  250. {
  251. struct thread_sync_data *tsd = conn_thread_sync_data(data);
  252. CURLcode result;
  253. result = Curl_addrinfo_callback(data, tsd->sock_error, tsd->res);
  254. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  255. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  256. */
  257. tsd->res = NULL;
  258. return result;
  259. }
  260. #ifdef _WIN32
  261. static VOID WINAPI
  262. query_complete(DWORD err, DWORD bytes, LPWSAOVERLAPPED overlapped)
  263. {
  264. size_t ss_size;
  265. const ADDRINFOEXW_ *ai;
  266. struct Curl_addrinfo *ca;
  267. struct Curl_addrinfo *cafirst = NULL;
  268. struct Curl_addrinfo *calast = NULL;
  269. #ifdef __clang__
  270. #pragma clang diagnostic push
  271. #pragma clang diagnostic ignored "-Wcast-align"
  272. #endif
  273. struct thread_sync_data *tsd =
  274. CONTAINING_RECORD(overlapped, struct thread_sync_data, w8.overlapped);
  275. #ifdef __clang__
  276. #pragma clang diagnostic pop
  277. #endif
  278. struct thread_data *td = tsd->td;
  279. const ADDRINFOEXW_ *res = tsd->w8.res;
  280. int error = (int)err;
  281. (void)bytes;
  282. if(error == ERROR_SUCCESS) {
  283. /* traverse the addrinfo list */
  284. for(ai = res; ai != NULL; ai = ai->ai_next) {
  285. size_t namelen = ai->ai_canonname ? wcslen(ai->ai_canonname) + 1 : 0;
  286. /* ignore elements with unsupported address family, */
  287. /* settle family-specific sockaddr structure size. */
  288. if(ai->ai_family == AF_INET)
  289. ss_size = sizeof(struct sockaddr_in);
  290. #ifdef USE_IPV6
  291. else if(ai->ai_family == AF_INET6)
  292. ss_size = sizeof(struct sockaddr_in6);
  293. #endif
  294. else
  295. continue;
  296. /* ignore elements without required address info */
  297. if(!ai->ai_addr || !(ai->ai_addrlen > 0))
  298. continue;
  299. /* ignore elements with bogus address size */
  300. if((size_t)ai->ai_addrlen < ss_size)
  301. continue;
  302. ca = malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen);
  303. if(!ca) {
  304. error = EAI_MEMORY;
  305. break;
  306. }
  307. /* copy each structure member individually, member ordering, */
  308. /* size, or padding might be different for each platform. */
  309. ca->ai_flags = ai->ai_flags;
  310. ca->ai_family = ai->ai_family;
  311. ca->ai_socktype = ai->ai_socktype;
  312. ca->ai_protocol = ai->ai_protocol;
  313. ca->ai_addrlen = (curl_socklen_t)ss_size;
  314. ca->ai_addr = NULL;
  315. ca->ai_canonname = NULL;
  316. ca->ai_next = NULL;
  317. ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
  318. memcpy(ca->ai_addr, ai->ai_addr, ss_size);
  319. if(namelen) {
  320. size_t i;
  321. ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size);
  322. for(i = 0; i < namelen; ++i) /* convert wide string to ascii */
  323. ca->ai_canonname[i] = (char)ai->ai_canonname[i];
  324. ca->ai_canonname[namelen] = '\0';
  325. }
  326. /* if the return list is empty, this becomes the first element */
  327. if(!cafirst)
  328. cafirst = ca;
  329. /* add this element last in the return list */
  330. if(calast)
  331. calast->ai_next = ca;
  332. calast = ca;
  333. }
  334. /* if we failed, also destroy the Curl_addrinfo list */
  335. if(error) {
  336. Curl_freeaddrinfo(cafirst);
  337. cafirst = NULL;
  338. }
  339. else if(!cafirst) {
  340. #ifdef EAI_NONAME
  341. /* rfc3493 conformant */
  342. error = EAI_NONAME;
  343. #else
  344. /* rfc3493 obsoleted */
  345. error = EAI_NODATA;
  346. #endif
  347. #ifdef USE_WINSOCK
  348. SET_SOCKERRNO(error);
  349. #endif
  350. }
  351. tsd->res = cafirst;
  352. }
  353. if(tsd->w8.res) {
  354. Curl_FreeAddrInfoExW(tsd->w8.res);
  355. tsd->w8.res = NULL;
  356. }
  357. if(error) {
  358. tsd->sock_error = SOCKERRNO?SOCKERRNO:error;
  359. if(tsd->sock_error == 0)
  360. tsd->sock_error = RESOLVER_ENOMEM;
  361. }
  362. else {
  363. Curl_addrinfo_set_port(tsd->res, tsd->port);
  364. }
  365. Curl_mutex_acquire(tsd->mtx);
  366. if(tsd->done) {
  367. /* too late, gotta clean up the mess */
  368. Curl_mutex_release(tsd->mtx);
  369. destroy_thread_sync_data(tsd);
  370. free(td);
  371. }
  372. else {
  373. #ifndef CURL_DISABLE_SOCKETPAIR
  374. char buf[1];
  375. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  376. /* DNS has been resolved, signal client task */
  377. buf[0] = 1;
  378. if(swrite(tsd->sock_pair[1], buf, sizeof(buf)) < 0) {
  379. /* update sock_erro to errno */
  380. tsd->sock_error = SOCKERRNO;
  381. }
  382. }
  383. #endif
  384. tsd->done = 1;
  385. Curl_mutex_release(tsd->mtx);
  386. if(td->complete_ev)
  387. SetEvent(td->complete_ev); /* Notify caller that the query completed */
  388. }
  389. }
  390. #endif
  391. #ifdef HAVE_GETADDRINFO
  392. /*
  393. * getaddrinfo_thread() resolves a name and then exits.
  394. *
  395. * For builds without ARES, but with USE_IPV6, create a resolver thread
  396. * and wait on it.
  397. */
  398. static unsigned int CURL_STDCALL getaddrinfo_thread(void *arg)
  399. {
  400. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  401. struct thread_data *td = tsd->td;
  402. char service[12];
  403. int rc;
  404. #ifndef CURL_DISABLE_SOCKETPAIR
  405. char buf[1];
  406. #endif
  407. msnprintf(service, sizeof(service), "%d", tsd->port);
  408. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  409. if(rc) {
  410. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  411. if(tsd->sock_error == 0)
  412. tsd->sock_error = RESOLVER_ENOMEM;
  413. }
  414. else {
  415. Curl_addrinfo_set_port(tsd->res, tsd->port);
  416. }
  417. Curl_mutex_acquire(tsd->mtx);
  418. if(tsd->done) {
  419. /* too late, gotta clean up the mess */
  420. Curl_mutex_release(tsd->mtx);
  421. destroy_thread_sync_data(tsd);
  422. free(td);
  423. }
  424. else {
  425. #ifndef CURL_DISABLE_SOCKETPAIR
  426. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  427. /* DNS has been resolved, signal client task */
  428. buf[0] = 1;
  429. if(wakeup_write(tsd->sock_pair[1], buf, sizeof(buf)) < 0) {
  430. /* update sock_erro to errno */
  431. tsd->sock_error = SOCKERRNO;
  432. }
  433. }
  434. #endif
  435. tsd->done = 1;
  436. Curl_mutex_release(tsd->mtx);
  437. }
  438. return 0;
  439. }
  440. #else /* HAVE_GETADDRINFO */
  441. /*
  442. * gethostbyname_thread() resolves a name and then exits.
  443. */
  444. static unsigned int CURL_STDCALL gethostbyname_thread(void *arg)
  445. {
  446. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  447. struct thread_data *td = tsd->td;
  448. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  449. if(!tsd->res) {
  450. tsd->sock_error = SOCKERRNO;
  451. if(tsd->sock_error == 0)
  452. tsd->sock_error = RESOLVER_ENOMEM;
  453. }
  454. Curl_mutex_acquire(tsd->mtx);
  455. if(tsd->done) {
  456. /* too late, gotta clean up the mess */
  457. Curl_mutex_release(tsd->mtx);
  458. destroy_thread_sync_data(tsd);
  459. free(td);
  460. }
  461. else {
  462. tsd->done = 1;
  463. Curl_mutex_release(tsd->mtx);
  464. }
  465. return 0;
  466. }
  467. #endif /* HAVE_GETADDRINFO */
  468. /*
  469. * destroy_async_data() cleans up async resolver data and thread handle.
  470. */
  471. static void destroy_async_data(struct Curl_async *async)
  472. {
  473. if(async->tdata) {
  474. struct thread_data *td = async->tdata;
  475. int done;
  476. #ifndef CURL_DISABLE_SOCKETPAIR
  477. curl_socket_t sock_rd = td->tsd.sock_pair[0];
  478. struct Curl_easy *data = td->tsd.data;
  479. #endif
  480. /*
  481. * if the thread is still blocking in the resolve syscall, detach it and
  482. * let the thread do the cleanup...
  483. */
  484. Curl_mutex_acquire(td->tsd.mtx);
  485. done = td->tsd.done;
  486. td->tsd.done = 1;
  487. Curl_mutex_release(td->tsd.mtx);
  488. if(!done) {
  489. #ifdef _WIN32
  490. if(td->complete_ev)
  491. CloseHandle(td->complete_ev);
  492. else
  493. #endif
  494. Curl_thread_destroy(td->thread_hnd);
  495. }
  496. else {
  497. #ifdef _WIN32
  498. if(td->complete_ev) {
  499. Curl_GetAddrInfoExCancel(&td->tsd.w8.cancel_ev);
  500. WaitForSingleObject(td->complete_ev, INFINITE);
  501. CloseHandle(td->complete_ev);
  502. }
  503. #endif
  504. if(td->thread_hnd != curl_thread_t_null)
  505. Curl_thread_join(&td->thread_hnd);
  506. destroy_thread_sync_data(&td->tsd);
  507. free(async->tdata);
  508. }
  509. #ifndef CURL_DISABLE_SOCKETPAIR
  510. /*
  511. * ensure CURLMOPT_SOCKETFUNCTION fires CURL_POLL_REMOVE
  512. * before the FD is invalidated to avoid EBADF on EPOLL_CTL_DEL
  513. */
  514. Curl_multi_closed(data, sock_rd);
  515. wakeup_close(sock_rd);
  516. #endif
  517. }
  518. async->tdata = NULL;
  519. free(async->hostname);
  520. async->hostname = NULL;
  521. }
  522. /*
  523. * init_resolve_thread() starts a new thread that performs the actual
  524. * resolve. This function returns before the resolve is done.
  525. *
  526. * Returns FALSE in case of failure, otherwise TRUE.
  527. */
  528. static bool init_resolve_thread(struct Curl_easy *data,
  529. const char *hostname, int port,
  530. const struct addrinfo *hints)
  531. {
  532. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  533. int err = ENOMEM;
  534. struct Curl_async *asp = &data->state.async;
  535. data->state.async.tdata = td;
  536. if(!td)
  537. goto errno_exit;
  538. asp->port = port;
  539. asp->done = FALSE;
  540. asp->status = 0;
  541. asp->dns = NULL;
  542. td->thread_hnd = curl_thread_t_null;
  543. #ifdef _WIN32
  544. td->complete_ev = NULL;
  545. #endif
  546. if(!init_thread_sync_data(td, hostname, port, hints)) {
  547. asp->tdata = NULL;
  548. free(td);
  549. goto errno_exit;
  550. }
  551. free(asp->hostname);
  552. asp->hostname = strdup(hostname);
  553. if(!asp->hostname)
  554. goto err_exit;
  555. /* The thread will set this to 1 when complete. */
  556. td->tsd.done = 0;
  557. #ifdef _WIN32
  558. if(Curl_isWindows8OrGreater && Curl_FreeAddrInfoExW &&
  559. Curl_GetAddrInfoExCancel && Curl_GetAddrInfoExW) {
  560. #define MAX_NAME_LEN 256 /* max domain name is 253 chars */
  561. #define MAX_PORT_LEN 8
  562. WCHAR namebuf[MAX_NAME_LEN];
  563. WCHAR portbuf[MAX_PORT_LEN];
  564. /* calculate required length */
  565. int w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname,
  566. -1, NULL, 0);
  567. if((w_len > 0) && (w_len < MAX_NAME_LEN)) {
  568. /* do utf8 conversion */
  569. w_len = MultiByteToWideChar(CP_UTF8, 0, hostname, -1, namebuf, w_len);
  570. if((w_len > 0) && (w_len < MAX_NAME_LEN)) {
  571. swprintf(portbuf, MAX_PORT_LEN, L"%d", port);
  572. td->tsd.w8.hints.ai_family = hints->ai_family;
  573. td->tsd.w8.hints.ai_socktype = hints->ai_socktype;
  574. td->complete_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
  575. if(!td->complete_ev) {
  576. /* failed to start, mark it as done here for proper cleanup. */
  577. td->tsd.done = 1;
  578. goto err_exit;
  579. }
  580. err = Curl_GetAddrInfoExW(namebuf, portbuf, NS_DNS,
  581. NULL, &td->tsd.w8.hints, &td->tsd.w8.res,
  582. NULL, &td->tsd.w8.overlapped,
  583. &query_complete, &td->tsd.w8.cancel_ev);
  584. if(err != WSA_IO_PENDING)
  585. query_complete(err, 0, &td->tsd.w8.overlapped);
  586. return TRUE;
  587. }
  588. }
  589. }
  590. #endif
  591. #ifdef HAVE_GETADDRINFO
  592. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  593. #else
  594. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  595. #endif
  596. if(!td->thread_hnd) {
  597. /* The thread never started, so mark it as done here for proper cleanup. */
  598. td->tsd.done = 1;
  599. err = errno;
  600. goto err_exit;
  601. }
  602. return TRUE;
  603. err_exit:
  604. destroy_async_data(asp);
  605. errno_exit:
  606. errno = err;
  607. return FALSE;
  608. }
  609. /*
  610. * 'entry' may be NULL and then no data is returned
  611. */
  612. static CURLcode thread_wait_resolv(struct Curl_easy *data,
  613. struct Curl_dns_entry **entry,
  614. bool report)
  615. {
  616. struct thread_data *td;
  617. CURLcode result = CURLE_OK;
  618. DEBUGASSERT(data);
  619. td = data->state.async.tdata;
  620. DEBUGASSERT(td);
  621. #ifdef _WIN32
  622. DEBUGASSERT(td->complete_ev || td->thread_hnd != curl_thread_t_null);
  623. #else
  624. DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
  625. #endif
  626. /* wait for the thread to resolve the name */
  627. #ifdef _WIN32
  628. if(td->complete_ev) {
  629. WaitForSingleObject(td->complete_ev, INFINITE);
  630. CloseHandle(td->complete_ev);
  631. if(entry)
  632. result = getaddrinfo_complete(data);
  633. }
  634. else
  635. #endif
  636. if(Curl_thread_join(&td->thread_hnd)) {
  637. if(entry)
  638. result = getaddrinfo_complete(data);
  639. }
  640. else
  641. DEBUGASSERT(0);
  642. data->state.async.done = TRUE;
  643. if(entry)
  644. *entry = data->state.async.dns;
  645. if(!data->state.async.dns && report)
  646. /* a name was not resolved, report error */
  647. result = Curl_resolver_error(data);
  648. destroy_async_data(&data->state.async);
  649. if(!data->state.async.dns && report)
  650. connclose(data->conn, "asynch resolve failed");
  651. return result;
  652. }
  653. /*
  654. * Until we gain a way to signal the resolver threads to stop early, we must
  655. * simply wait for them and ignore their results.
  656. */
  657. void Curl_resolver_kill(struct Curl_easy *data)
  658. {
  659. struct thread_data *td = data->state.async.tdata;
  660. /* If we're still resolving, we must wait for the threads to fully clean up,
  661. unfortunately. Otherwise, we can simply cancel to clean up any resolver
  662. data. */
  663. if(td && td->thread_hnd != curl_thread_t_null
  664. && (data->set.quick_exit != 1L))
  665. (void)thread_wait_resolv(data, NULL, FALSE);
  666. else
  667. Curl_resolver_cancel(data);
  668. }
  669. /*
  670. * Curl_resolver_wait_resolv()
  671. *
  672. * Waits for a resolve to finish. This function should be avoided since using
  673. * this risk getting the multi interface to "hang".
  674. *
  675. * If 'entry' is non-NULL, make it point to the resolved dns entry
  676. *
  677. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  678. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  679. *
  680. * This is the version for resolves-in-a-thread.
  681. */
  682. CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
  683. struct Curl_dns_entry **entry)
  684. {
  685. return thread_wait_resolv(data, entry, TRUE);
  686. }
  687. /*
  688. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  689. * name resolve request has completed. It should also make sure to time-out if
  690. * the operation seems to take too long.
  691. */
  692. CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
  693. struct Curl_dns_entry **entry)
  694. {
  695. struct thread_data *td = data->state.async.tdata;
  696. int done = 0;
  697. DEBUGASSERT(entry);
  698. *entry = NULL;
  699. if(!td) {
  700. DEBUGASSERT(td);
  701. return CURLE_COULDNT_RESOLVE_HOST;
  702. }
  703. Curl_mutex_acquire(td->tsd.mtx);
  704. done = td->tsd.done;
  705. Curl_mutex_release(td->tsd.mtx);
  706. if(done) {
  707. getaddrinfo_complete(data);
  708. if(!data->state.async.dns) {
  709. CURLcode result = Curl_resolver_error(data);
  710. destroy_async_data(&data->state.async);
  711. return result;
  712. }
  713. destroy_async_data(&data->state.async);
  714. *entry = data->state.async.dns;
  715. }
  716. else {
  717. /* poll for name lookup done with exponential backoff up to 250ms */
  718. /* should be fine even if this converts to 32 bit */
  719. timediff_t elapsed = Curl_timediff(Curl_now(),
  720. data->progress.t_startsingle);
  721. if(elapsed < 0)
  722. elapsed = 0;
  723. if(td->poll_interval == 0)
  724. /* Start at 1ms poll interval */
  725. td->poll_interval = 1;
  726. else if(elapsed >= td->interval_end)
  727. /* Back-off exponentially if last interval expired */
  728. td->poll_interval *= 2;
  729. if(td->poll_interval > 250)
  730. td->poll_interval = 250;
  731. td->interval_end = elapsed + td->poll_interval;
  732. Curl_expire(data, td->poll_interval, EXPIRE_ASYNC_NAME);
  733. }
  734. return CURLE_OK;
  735. }
  736. int Curl_resolver_getsock(struct Curl_easy *data, curl_socket_t *socks)
  737. {
  738. int ret_val = 0;
  739. timediff_t milli;
  740. timediff_t ms;
  741. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  742. #ifndef CURL_DISABLE_SOCKETPAIR
  743. struct thread_data *td = data->state.async.tdata;
  744. #else
  745. (void)socks;
  746. #endif
  747. #ifndef CURL_DISABLE_SOCKETPAIR
  748. if(td) {
  749. /* return read fd to client for polling the DNS resolution status */
  750. socks[0] = td->tsd.sock_pair[0];
  751. td->tsd.data = data;
  752. ret_val = GETSOCK_READSOCK(0);
  753. }
  754. else {
  755. #endif
  756. ms = Curl_timediff(Curl_now(), reslv->start);
  757. if(ms < 3)
  758. milli = 0;
  759. else if(ms <= 50)
  760. milli = ms/3;
  761. else if(ms <= 250)
  762. milli = 50;
  763. else
  764. milli = 200;
  765. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  766. #ifndef CURL_DISABLE_SOCKETPAIR
  767. }
  768. #endif
  769. return ret_val;
  770. }
  771. #ifndef HAVE_GETADDRINFO
  772. /*
  773. * Curl_getaddrinfo() - for platforms without getaddrinfo
  774. */
  775. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  776. const char *hostname,
  777. int port,
  778. int *waitp)
  779. {
  780. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  781. *waitp = 0; /* default to synchronous response */
  782. reslv->start = Curl_now();
  783. /* fire up a new resolver thread! */
  784. if(init_resolve_thread(data, hostname, port, NULL)) {
  785. *waitp = 1; /* expect asynchronous response */
  786. return NULL;
  787. }
  788. failf(data, "getaddrinfo() thread failed");
  789. return NULL;
  790. }
  791. #else /* !HAVE_GETADDRINFO */
  792. /*
  793. * Curl_resolver_getaddrinfo() - for getaddrinfo
  794. */
  795. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  796. const char *hostname,
  797. int port,
  798. int *waitp)
  799. {
  800. struct addrinfo hints;
  801. int pf = PF_INET;
  802. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  803. *waitp = 0; /* default to synchronous response */
  804. #ifdef CURLRES_IPV6
  805. if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  806. /* The stack seems to be IPv6-enabled */
  807. if(data->conn->ip_version == CURL_IPRESOLVE_V6)
  808. pf = PF_INET6;
  809. else
  810. pf = PF_UNSPEC;
  811. }
  812. #endif /* CURLRES_IPV6 */
  813. memset(&hints, 0, sizeof(hints));
  814. hints.ai_family = pf;
  815. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP)?
  816. SOCK_STREAM : SOCK_DGRAM;
  817. reslv->start = Curl_now();
  818. /* fire up a new resolver thread! */
  819. if(init_resolve_thread(data, hostname, port, &hints)) {
  820. *waitp = 1; /* expect asynchronous response */
  821. return NULL;
  822. }
  823. failf(data, "getaddrinfo() thread failed to start");
  824. return NULL;
  825. }
  826. #endif /* !HAVE_GETADDRINFO */
  827. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  828. char *servers)
  829. {
  830. (void)data;
  831. (void)servers;
  832. return CURLE_NOT_BUILT_IN;
  833. }
  834. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  835. const char *interf)
  836. {
  837. (void)data;
  838. (void)interf;
  839. return CURLE_NOT_BUILT_IN;
  840. }
  841. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  842. const char *local_ip4)
  843. {
  844. (void)data;
  845. (void)local_ip4;
  846. return CURLE_NOT_BUILT_IN;
  847. }
  848. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  849. const char *local_ip6)
  850. {
  851. (void)data;
  852. (void)local_ip6;
  853. return CURLE_NOT_BUILT_IN;
  854. }
  855. #endif /* CURLRES_THREADED */