asyn-thread.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. td->complete_ev = NULL;
  493. }
  494. #endif
  495. if(td->thread_hnd != curl_thread_t_null) {
  496. Curl_thread_destroy(td->thread_hnd);
  497. td->thread_hnd = curl_thread_t_null;
  498. }
  499. }
  500. else {
  501. #ifdef _WIN32
  502. if(td->complete_ev) {
  503. Curl_GetAddrInfoExCancel(&td->tsd.w8.cancel_ev);
  504. WaitForSingleObject(td->complete_ev, INFINITE);
  505. CloseHandle(td->complete_ev);
  506. td->complete_ev = NULL;
  507. }
  508. #endif
  509. if(td->thread_hnd != curl_thread_t_null)
  510. Curl_thread_join(&td->thread_hnd);
  511. destroy_thread_sync_data(&td->tsd);
  512. free(async->tdata);
  513. }
  514. #ifndef CURL_DISABLE_SOCKETPAIR
  515. /*
  516. * ensure CURLMOPT_SOCKETFUNCTION fires CURL_POLL_REMOVE
  517. * before the FD is invalidated to avoid EBADF on EPOLL_CTL_DEL
  518. */
  519. Curl_multi_closed(data, sock_rd);
  520. wakeup_close(sock_rd);
  521. #endif
  522. }
  523. async->tdata = NULL;
  524. free(async->hostname);
  525. async->hostname = NULL;
  526. }
  527. /*
  528. * init_resolve_thread() starts a new thread that performs the actual
  529. * resolve. This function returns before the resolve is done.
  530. *
  531. * Returns FALSE in case of failure, otherwise TRUE.
  532. */
  533. static bool init_resolve_thread(struct Curl_easy *data,
  534. const char *hostname, int port,
  535. const struct addrinfo *hints)
  536. {
  537. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  538. int err = ENOMEM;
  539. struct Curl_async *asp = &data->state.async;
  540. data->state.async.tdata = td;
  541. if(!td)
  542. goto errno_exit;
  543. asp->port = port;
  544. asp->done = FALSE;
  545. asp->status = 0;
  546. asp->dns = NULL;
  547. td->thread_hnd = curl_thread_t_null;
  548. #ifdef _WIN32
  549. td->complete_ev = NULL;
  550. #endif
  551. if(!init_thread_sync_data(td, hostname, port, hints)) {
  552. asp->tdata = NULL;
  553. free(td);
  554. goto errno_exit;
  555. }
  556. free(asp->hostname);
  557. asp->hostname = strdup(hostname);
  558. if(!asp->hostname)
  559. goto err_exit;
  560. /* The thread will set this to 1 when complete. */
  561. td->tsd.done = 0;
  562. #ifdef _WIN32
  563. if(Curl_isWindows8OrGreater && Curl_FreeAddrInfoExW &&
  564. Curl_GetAddrInfoExCancel && Curl_GetAddrInfoExW) {
  565. #define MAX_NAME_LEN 256 /* max domain name is 253 chars */
  566. #define MAX_PORT_LEN 8
  567. WCHAR namebuf[MAX_NAME_LEN];
  568. WCHAR portbuf[MAX_PORT_LEN];
  569. /* calculate required length */
  570. int w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname,
  571. -1, NULL, 0);
  572. if((w_len > 0) && (w_len < MAX_NAME_LEN)) {
  573. /* do utf8 conversion */
  574. w_len = MultiByteToWideChar(CP_UTF8, 0, hostname, -1, namebuf, w_len);
  575. if((w_len > 0) && (w_len < MAX_NAME_LEN)) {
  576. swprintf(portbuf, MAX_PORT_LEN, L"%d", port);
  577. td->tsd.w8.hints.ai_family = hints->ai_family;
  578. td->tsd.w8.hints.ai_socktype = hints->ai_socktype;
  579. td->complete_ev = CreateEvent(NULL, TRUE, FALSE, NULL);
  580. if(!td->complete_ev) {
  581. /* failed to start, mark it as done here for proper cleanup. */
  582. td->tsd.done = 1;
  583. goto err_exit;
  584. }
  585. err = Curl_GetAddrInfoExW(namebuf, portbuf, NS_DNS,
  586. NULL, &td->tsd.w8.hints, &td->tsd.w8.res,
  587. NULL, &td->tsd.w8.overlapped,
  588. &query_complete, &td->tsd.w8.cancel_ev);
  589. if(err != WSA_IO_PENDING)
  590. query_complete(err, 0, &td->tsd.w8.overlapped);
  591. return TRUE;
  592. }
  593. }
  594. }
  595. #endif
  596. #ifdef HAVE_GETADDRINFO
  597. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  598. #else
  599. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  600. #endif
  601. if(td->thread_hnd == curl_thread_t_null) {
  602. /* The thread never started, so mark it as done here for proper cleanup. */
  603. td->tsd.done = 1;
  604. err = errno;
  605. goto err_exit;
  606. }
  607. return TRUE;
  608. err_exit:
  609. destroy_async_data(asp);
  610. errno_exit:
  611. errno = err;
  612. return FALSE;
  613. }
  614. /*
  615. * 'entry' may be NULL and then no data is returned
  616. */
  617. static CURLcode thread_wait_resolv(struct Curl_easy *data,
  618. struct Curl_dns_entry **entry,
  619. bool report)
  620. {
  621. struct thread_data *td;
  622. CURLcode result = CURLE_OK;
  623. DEBUGASSERT(data);
  624. td = data->state.async.tdata;
  625. DEBUGASSERT(td);
  626. #ifdef _WIN32
  627. DEBUGASSERT(td->complete_ev || td->thread_hnd != curl_thread_t_null);
  628. #else
  629. DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
  630. #endif
  631. /* wait for the thread to resolve the name */
  632. #ifdef _WIN32
  633. if(td->complete_ev) {
  634. WaitForSingleObject(td->complete_ev, INFINITE);
  635. CloseHandle(td->complete_ev);
  636. td->complete_ev = NULL;
  637. if(entry)
  638. result = getaddrinfo_complete(data);
  639. }
  640. else
  641. #endif
  642. if(Curl_thread_join(&td->thread_hnd)) {
  643. if(entry)
  644. result = getaddrinfo_complete(data);
  645. }
  646. else
  647. DEBUGASSERT(0);
  648. data->state.async.done = TRUE;
  649. if(entry)
  650. *entry = data->state.async.dns;
  651. if(!data->state.async.dns && report)
  652. /* a name was not resolved, report error */
  653. result = Curl_resolver_error(data);
  654. destroy_async_data(&data->state.async);
  655. if(!data->state.async.dns && report)
  656. connclose(data->conn, "asynch resolve failed");
  657. return result;
  658. }
  659. /*
  660. * Until we gain a way to signal the resolver threads to stop early, we must
  661. * simply wait for them and ignore their results.
  662. */
  663. void Curl_resolver_kill(struct Curl_easy *data)
  664. {
  665. struct thread_data *td = data->state.async.tdata;
  666. /* If we're still resolving, we must wait for the threads to fully clean up,
  667. unfortunately. Otherwise, we can simply cancel to clean up any resolver
  668. data. */
  669. #ifdef _WIN32
  670. if(td && td->complete_ev) {
  671. Curl_GetAddrInfoExCancel(&td->tsd.w8.cancel_ev);
  672. (void)thread_wait_resolv(data, NULL, FALSE);
  673. }
  674. else
  675. #endif
  676. if(td && td->thread_hnd != curl_thread_t_null
  677. && (data->set.quick_exit != 1L))
  678. (void)thread_wait_resolv(data, NULL, FALSE);
  679. else
  680. Curl_resolver_cancel(data);
  681. }
  682. /*
  683. * Curl_resolver_wait_resolv()
  684. *
  685. * Waits for a resolve to finish. This function should be avoided since using
  686. * this risk getting the multi interface to "hang".
  687. *
  688. * If 'entry' is non-NULL, make it point to the resolved dns entry
  689. *
  690. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  691. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  692. *
  693. * This is the version for resolves-in-a-thread.
  694. */
  695. CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
  696. struct Curl_dns_entry **entry)
  697. {
  698. return thread_wait_resolv(data, entry, TRUE);
  699. }
  700. /*
  701. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  702. * name resolve request has completed. It should also make sure to time-out if
  703. * the operation seems to take too long.
  704. */
  705. CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
  706. struct Curl_dns_entry **entry)
  707. {
  708. struct thread_data *td = data->state.async.tdata;
  709. int done = 0;
  710. DEBUGASSERT(entry);
  711. *entry = NULL;
  712. if(!td) {
  713. DEBUGASSERT(td);
  714. return CURLE_COULDNT_RESOLVE_HOST;
  715. }
  716. Curl_mutex_acquire(td->tsd.mtx);
  717. done = td->tsd.done;
  718. Curl_mutex_release(td->tsd.mtx);
  719. if(done) {
  720. getaddrinfo_complete(data);
  721. if(!data->state.async.dns) {
  722. CURLcode result = Curl_resolver_error(data);
  723. destroy_async_data(&data->state.async);
  724. return result;
  725. }
  726. destroy_async_data(&data->state.async);
  727. *entry = data->state.async.dns;
  728. }
  729. else {
  730. /* poll for name lookup done with exponential backoff up to 250ms */
  731. /* should be fine even if this converts to 32 bit */
  732. timediff_t elapsed = Curl_timediff(Curl_now(),
  733. data->progress.t_startsingle);
  734. if(elapsed < 0)
  735. elapsed = 0;
  736. if(td->poll_interval == 0)
  737. /* Start at 1ms poll interval */
  738. td->poll_interval = 1;
  739. else if(elapsed >= td->interval_end)
  740. /* Back-off exponentially if last interval expired */
  741. td->poll_interval *= 2;
  742. if(td->poll_interval > 250)
  743. td->poll_interval = 250;
  744. td->interval_end = elapsed + td->poll_interval;
  745. Curl_expire(data, td->poll_interval, EXPIRE_ASYNC_NAME);
  746. }
  747. return CURLE_OK;
  748. }
  749. int Curl_resolver_getsock(struct Curl_easy *data, curl_socket_t *socks)
  750. {
  751. int ret_val = 0;
  752. timediff_t milli;
  753. timediff_t ms;
  754. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  755. #ifndef CURL_DISABLE_SOCKETPAIR
  756. struct thread_data *td = data->state.async.tdata;
  757. #else
  758. (void)socks;
  759. #endif
  760. #ifndef CURL_DISABLE_SOCKETPAIR
  761. if(td) {
  762. /* return read fd to client for polling the DNS resolution status */
  763. socks[0] = td->tsd.sock_pair[0];
  764. td->tsd.data = data;
  765. ret_val = GETSOCK_READSOCK(0);
  766. }
  767. else {
  768. #endif
  769. ms = Curl_timediff(Curl_now(), reslv->start);
  770. if(ms < 3)
  771. milli = 0;
  772. else if(ms <= 50)
  773. milli = ms/3;
  774. else if(ms <= 250)
  775. milli = 50;
  776. else
  777. milli = 200;
  778. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  779. #ifndef CURL_DISABLE_SOCKETPAIR
  780. }
  781. #endif
  782. return ret_val;
  783. }
  784. #ifndef HAVE_GETADDRINFO
  785. /*
  786. * Curl_getaddrinfo() - for platforms without getaddrinfo
  787. */
  788. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  789. const char *hostname,
  790. int port,
  791. int *waitp)
  792. {
  793. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  794. *waitp = 0; /* default to synchronous response */
  795. reslv->start = Curl_now();
  796. /* fire up a new resolver thread! */
  797. if(init_resolve_thread(data, hostname, port, NULL)) {
  798. *waitp = 1; /* expect asynchronous response */
  799. return NULL;
  800. }
  801. failf(data, "getaddrinfo() thread failed");
  802. return NULL;
  803. }
  804. #else /* !HAVE_GETADDRINFO */
  805. /*
  806. * Curl_resolver_getaddrinfo() - for getaddrinfo
  807. */
  808. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  809. const char *hostname,
  810. int port,
  811. int *waitp)
  812. {
  813. struct addrinfo hints;
  814. int pf = PF_INET;
  815. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  816. *waitp = 0; /* default to synchronous response */
  817. #ifdef CURLRES_IPV6
  818. if((data->conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  819. /* The stack seems to be IPv6-enabled */
  820. if(data->conn->ip_version == CURL_IPRESOLVE_V6)
  821. pf = PF_INET6;
  822. else
  823. pf = PF_UNSPEC;
  824. }
  825. #endif /* CURLRES_IPV6 */
  826. memset(&hints, 0, sizeof(hints));
  827. hints.ai_family = pf;
  828. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP)?
  829. SOCK_STREAM : SOCK_DGRAM;
  830. reslv->start = Curl_now();
  831. /* fire up a new resolver thread! */
  832. if(init_resolve_thread(data, hostname, port, &hints)) {
  833. *waitp = 1; /* expect asynchronous response */
  834. return NULL;
  835. }
  836. failf(data, "getaddrinfo() thread failed to start");
  837. return NULL;
  838. }
  839. #endif /* !HAVE_GETADDRINFO */
  840. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  841. char *servers)
  842. {
  843. (void)data;
  844. (void)servers;
  845. return CURLE_NOT_BUILT_IN;
  846. }
  847. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  848. const char *interf)
  849. {
  850. (void)data;
  851. (void)interf;
  852. return CURLE_NOT_BUILT_IN;
  853. }
  854. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  855. const char *local_ip4)
  856. {
  857. (void)data;
  858. (void)local_ip4;
  859. return CURLE_NOT_BUILT_IN;
  860. }
  861. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  862. const char *local_ip6)
  863. {
  864. (void)data;
  865. (void)local_ip6;
  866. return CURLE_NOT_BUILT_IN;
  867. }
  868. #endif /* CURLRES_THREADED */