asyn-thread.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 https://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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "socketpair.h"
  24. /***********************************************************************
  25. * Only for threaded name resolves builds
  26. **********************************************************************/
  27. #ifdef CURLRES_THREADED
  28. #ifdef HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. #include <netdb.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #ifdef __VMS
  38. #include <in.h>
  39. #include <inet.h>
  40. #endif
  41. #if defined(USE_THREADS_POSIX)
  42. # ifdef HAVE_PTHREAD_H
  43. # include <pthread.h>
  44. # endif
  45. #elif defined(USE_THREADS_WIN32)
  46. # ifdef HAVE_PROCESS_H
  47. # include <process.h>
  48. # endif
  49. #endif
  50. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  51. #undef in_addr_t
  52. #define in_addr_t unsigned long
  53. #endif
  54. #ifdef HAVE_GETADDRINFO
  55. # define RESOLVER_ENOMEM EAI_MEMORY
  56. #else
  57. # define RESOLVER_ENOMEM ENOMEM
  58. #endif
  59. #include "urldata.h"
  60. #include "sendf.h"
  61. #include "hostip.h"
  62. #include "hash.h"
  63. #include "share.h"
  64. #include "strerror.h"
  65. #include "url.h"
  66. #include "multiif.h"
  67. #include "inet_ntop.h"
  68. #include "curl_threads.h"
  69. #include "connect.h"
  70. #include "socketpair.h"
  71. /* The last 3 #include files should be in this order */
  72. #include "curl_printf.h"
  73. #include "curl_memory.h"
  74. #include "memdebug.h"
  75. struct resdata {
  76. struct curltime start;
  77. };
  78. /*
  79. * Curl_resolver_global_init()
  80. * Called from curl_global_init() to initialize global resolver environment.
  81. * Does nothing here.
  82. */
  83. int Curl_resolver_global_init(void)
  84. {
  85. return CURLE_OK;
  86. }
  87. /*
  88. * Curl_resolver_global_cleanup()
  89. * Called from curl_global_cleanup() to destroy global resolver environment.
  90. * Does nothing here.
  91. */
  92. void Curl_resolver_global_cleanup(void)
  93. {
  94. }
  95. /*
  96. * Curl_resolver_init()
  97. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  98. * URL-state specific environment ('resolver' member of the UrlState
  99. * structure).
  100. */
  101. CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
  102. {
  103. (void)easy;
  104. *resolver = calloc(1, sizeof(struct resdata));
  105. if(!*resolver)
  106. return CURLE_OUT_OF_MEMORY;
  107. return CURLE_OK;
  108. }
  109. /*
  110. * Curl_resolver_cleanup()
  111. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  112. * URL-state specific environment ('resolver' member of the UrlState
  113. * structure).
  114. */
  115. void Curl_resolver_cleanup(void *resolver)
  116. {
  117. free(resolver);
  118. }
  119. /*
  120. * Curl_resolver_duphandle()
  121. * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
  122. * environment ('resolver' member of the UrlState structure).
  123. */
  124. CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
  125. {
  126. (void)from;
  127. return Curl_resolver_init(easy, to);
  128. }
  129. static void destroy_async_data(struct Curl_async *);
  130. /*
  131. * Cancel all possibly still on-going resolves for this connection.
  132. */
  133. void Curl_resolver_cancel(struct connectdata *conn)
  134. {
  135. destroy_async_data(&conn->async);
  136. }
  137. /* This function is used to init a threaded resolve */
  138. static bool init_resolve_thread(struct connectdata *conn,
  139. const char *hostname, int port,
  140. const struct addrinfo *hints);
  141. /* Data for synchronization between resolver thread and its parent */
  142. struct thread_sync_data {
  143. curl_mutex_t * mtx;
  144. int done;
  145. char *hostname; /* hostname to resolve, Curl_async.hostname
  146. duplicate */
  147. int port;
  148. #ifdef USE_SOCKETPAIR
  149. struct connectdata *conn;
  150. curl_socket_t sock_pair[2]; /* socket pair */
  151. #endif
  152. int sock_error;
  153. Curl_addrinfo *res;
  154. #ifdef HAVE_GETADDRINFO
  155. struct addrinfo hints;
  156. #endif
  157. struct thread_data *td; /* for thread-self cleanup */
  158. };
  159. struct thread_data {
  160. curl_thread_t thread_hnd;
  161. unsigned int poll_interval;
  162. time_t interval_end;
  163. struct thread_sync_data tsd;
  164. };
  165. static struct thread_sync_data *conn_thread_sync_data(struct connectdata *conn)
  166. {
  167. return &(((struct thread_data *)conn->async.os_specific)->tsd);
  168. }
  169. /* Destroy resolver thread synchronization data */
  170. static
  171. void destroy_thread_sync_data(struct thread_sync_data * tsd)
  172. {
  173. if(tsd->mtx) {
  174. Curl_mutex_destroy(tsd->mtx);
  175. free(tsd->mtx);
  176. }
  177. free(tsd->hostname);
  178. if(tsd->res)
  179. Curl_freeaddrinfo(tsd->res);
  180. #ifdef USE_SOCKETPAIR
  181. /*
  182. * close one end of the socket pair (may be done in resolver thread);
  183. * the other end (for reading) is always closed in the parent thread.
  184. */
  185. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  186. sclose(tsd->sock_pair[1]);
  187. }
  188. #endif
  189. memset(tsd, 0, sizeof(*tsd));
  190. }
  191. /* Initialize resolver thread synchronization data */
  192. static
  193. int init_thread_sync_data(struct thread_data * td,
  194. const char *hostname,
  195. int port,
  196. const struct addrinfo *hints)
  197. {
  198. struct thread_sync_data *tsd = &td->tsd;
  199. memset(tsd, 0, sizeof(*tsd));
  200. tsd->td = td;
  201. tsd->port = port;
  202. /* Treat the request as done until the thread actually starts so any early
  203. * cleanup gets done properly.
  204. */
  205. tsd->done = 1;
  206. #ifdef HAVE_GETADDRINFO
  207. DEBUGASSERT(hints);
  208. tsd->hints = *hints;
  209. #else
  210. (void) hints;
  211. #endif
  212. tsd->mtx = malloc(sizeof(curl_mutex_t));
  213. if(tsd->mtx == NULL)
  214. goto err_exit;
  215. Curl_mutex_init(tsd->mtx);
  216. #ifdef USE_SOCKETPAIR
  217. /* create socket pair, avoid AF_LOCAL since it doesn't build on Solaris */
  218. if(Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, &tsd->sock_pair[0]) < 0) {
  219. tsd->sock_pair[0] = CURL_SOCKET_BAD;
  220. tsd->sock_pair[1] = CURL_SOCKET_BAD;
  221. goto err_exit;
  222. }
  223. #endif
  224. tsd->sock_error = CURL_ASYNC_SUCCESS;
  225. /* Copying hostname string because original can be destroyed by parent
  226. * thread during gethostbyname execution.
  227. */
  228. tsd->hostname = strdup(hostname);
  229. if(!tsd->hostname)
  230. goto err_exit;
  231. return 1;
  232. err_exit:
  233. /* Memory allocation failed */
  234. destroy_thread_sync_data(tsd);
  235. return 0;
  236. }
  237. static int getaddrinfo_complete(struct connectdata *conn)
  238. {
  239. struct thread_sync_data *tsd = conn_thread_sync_data(conn);
  240. int rc;
  241. rc = Curl_addrinfo_callback(conn, tsd->sock_error, tsd->res);
  242. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  243. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  244. */
  245. tsd->res = NULL;
  246. return rc;
  247. }
  248. #ifdef HAVE_GETADDRINFO
  249. /*
  250. * getaddrinfo_thread() resolves a name and then exits.
  251. *
  252. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  253. * and wait on it.
  254. */
  255. static unsigned int CURL_STDCALL getaddrinfo_thread(void *arg)
  256. {
  257. struct thread_sync_data *tsd = (struct thread_sync_data*)arg;
  258. struct thread_data *td = tsd->td;
  259. char service[12];
  260. int rc;
  261. #ifdef USE_SOCKETPAIR
  262. char buf[1];
  263. #endif
  264. msnprintf(service, sizeof(service), "%d", tsd->port);
  265. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  266. if(rc != 0) {
  267. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  268. if(tsd->sock_error == 0)
  269. tsd->sock_error = RESOLVER_ENOMEM;
  270. }
  271. else {
  272. Curl_addrinfo_set_port(tsd->res, tsd->port);
  273. }
  274. Curl_mutex_acquire(tsd->mtx);
  275. if(tsd->done) {
  276. /* too late, gotta clean up the mess */
  277. Curl_mutex_release(tsd->mtx);
  278. destroy_thread_sync_data(tsd);
  279. free(td);
  280. }
  281. else {
  282. #ifdef USE_SOCKETPAIR
  283. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  284. /* DNS has been resolved, signal client task */
  285. buf[0] = 1;
  286. if(swrite(tsd->sock_pair[1], buf, sizeof(buf)) < 0) {
  287. /* update sock_erro to errno */
  288. tsd->sock_error = SOCKERRNO;
  289. }
  290. }
  291. #endif
  292. tsd->done = 1;
  293. Curl_mutex_release(tsd->mtx);
  294. }
  295. return 0;
  296. }
  297. #else /* HAVE_GETADDRINFO */
  298. /*
  299. * gethostbyname_thread() resolves a name and then exits.
  300. */
  301. static unsigned int CURL_STDCALL gethostbyname_thread(void *arg)
  302. {
  303. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  304. struct thread_data *td = tsd->td;
  305. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  306. if(!tsd->res) {
  307. tsd->sock_error = SOCKERRNO;
  308. if(tsd->sock_error == 0)
  309. tsd->sock_error = RESOLVER_ENOMEM;
  310. }
  311. Curl_mutex_acquire(tsd->mtx);
  312. if(tsd->done) {
  313. /* too late, gotta clean up the mess */
  314. Curl_mutex_release(tsd->mtx);
  315. destroy_thread_sync_data(tsd);
  316. free(td);
  317. }
  318. else {
  319. tsd->done = 1;
  320. Curl_mutex_release(tsd->mtx);
  321. }
  322. return 0;
  323. }
  324. #endif /* HAVE_GETADDRINFO */
  325. /*
  326. * destroy_async_data() cleans up async resolver data and thread handle.
  327. */
  328. static void destroy_async_data(struct Curl_async *async)
  329. {
  330. if(async->os_specific) {
  331. struct thread_data *td = (struct thread_data*) async->os_specific;
  332. int done;
  333. #ifdef USE_SOCKETPAIR
  334. curl_socket_t sock_rd = td->tsd.sock_pair[0];
  335. struct connectdata *conn = td->tsd.conn;
  336. #endif
  337. /*
  338. * if the thread is still blocking in the resolve syscall, detach it and
  339. * let the thread do the cleanup...
  340. */
  341. Curl_mutex_acquire(td->tsd.mtx);
  342. done = td->tsd.done;
  343. td->tsd.done = 1;
  344. Curl_mutex_release(td->tsd.mtx);
  345. if(!done) {
  346. Curl_thread_destroy(td->thread_hnd);
  347. }
  348. else {
  349. if(td->thread_hnd != curl_thread_t_null)
  350. Curl_thread_join(&td->thread_hnd);
  351. destroy_thread_sync_data(&td->tsd);
  352. free(async->os_specific);
  353. }
  354. #ifdef USE_SOCKETPAIR
  355. /*
  356. * ensure CURLMOPT_SOCKETFUNCTION fires CURL_POLL_REMOVE
  357. * before the FD is invalidated to avoid EBADF on EPOLL_CTL_DEL
  358. */
  359. if(conn)
  360. Curl_multi_closed(conn->data, sock_rd);
  361. sclose(sock_rd);
  362. #endif
  363. }
  364. async->os_specific = NULL;
  365. free(async->hostname);
  366. async->hostname = NULL;
  367. }
  368. /*
  369. * init_resolve_thread() starts a new thread that performs the actual
  370. * resolve. This function returns before the resolve is done.
  371. *
  372. * Returns FALSE in case of failure, otherwise TRUE.
  373. */
  374. static bool init_resolve_thread(struct connectdata *conn,
  375. const char *hostname, int port,
  376. const struct addrinfo *hints)
  377. {
  378. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  379. int err = ENOMEM;
  380. conn->async.os_specific = (void *)td;
  381. if(!td)
  382. goto errno_exit;
  383. conn->async.port = port;
  384. conn->async.done = FALSE;
  385. conn->async.status = 0;
  386. conn->async.dns = NULL;
  387. td->thread_hnd = curl_thread_t_null;
  388. if(!init_thread_sync_data(td, hostname, port, hints)) {
  389. conn->async.os_specific = NULL;
  390. free(td);
  391. goto errno_exit;
  392. }
  393. free(conn->async.hostname);
  394. conn->async.hostname = strdup(hostname);
  395. if(!conn->async.hostname)
  396. goto err_exit;
  397. /* The thread will set this to 1 when complete. */
  398. td->tsd.done = 0;
  399. #ifdef HAVE_GETADDRINFO
  400. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  401. #else
  402. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  403. #endif
  404. if(!td->thread_hnd) {
  405. /* The thread never started, so mark it as done here for proper cleanup. */
  406. td->tsd.done = 1;
  407. err = errno;
  408. goto err_exit;
  409. }
  410. return TRUE;
  411. err_exit:
  412. destroy_async_data(&conn->async);
  413. errno_exit:
  414. errno = err;
  415. return FALSE;
  416. }
  417. /*
  418. * resolver_error() calls failf() with the appropriate message after a resolve
  419. * error
  420. */
  421. static CURLcode resolver_error(struct connectdata *conn)
  422. {
  423. const char *host_or_proxy;
  424. CURLcode result;
  425. if(conn->bits.httpproxy) {
  426. host_or_proxy = "proxy";
  427. result = CURLE_COULDNT_RESOLVE_PROXY;
  428. }
  429. else {
  430. host_or_proxy = "host";
  431. result = CURLE_COULDNT_RESOLVE_HOST;
  432. }
  433. failf(conn->data, "Could not resolve %s: %s", host_or_proxy,
  434. conn->async.hostname);
  435. return result;
  436. }
  437. static CURLcode thread_wait_resolv(struct connectdata *conn,
  438. struct Curl_dns_entry **entry,
  439. bool report)
  440. {
  441. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  442. CURLcode result = CURLE_OK;
  443. DEBUGASSERT(conn && td);
  444. DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
  445. /* wait for the thread to resolve the name */
  446. if(Curl_thread_join(&td->thread_hnd)) {
  447. if(entry)
  448. result = getaddrinfo_complete(conn);
  449. }
  450. else
  451. DEBUGASSERT(0);
  452. conn->async.done = TRUE;
  453. if(entry)
  454. *entry = conn->async.dns;
  455. if(!conn->async.dns && report)
  456. /* a name was not resolved, report error */
  457. result = resolver_error(conn);
  458. destroy_async_data(&conn->async);
  459. if(!conn->async.dns && report)
  460. connclose(conn, "asynch resolve failed");
  461. return result;
  462. }
  463. /*
  464. * Until we gain a way to signal the resolver threads to stop early, we must
  465. * simply wait for them and ignore their results.
  466. */
  467. void Curl_resolver_kill(struct connectdata *conn)
  468. {
  469. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  470. /* If we're still resolving, we must wait for the threads to fully clean up,
  471. unfortunately. Otherwise, we can simply cancel to clean up any resolver
  472. data. */
  473. if(td && td->thread_hnd != curl_thread_t_null)
  474. (void)thread_wait_resolv(conn, NULL, FALSE);
  475. else
  476. Curl_resolver_cancel(conn);
  477. }
  478. /*
  479. * Curl_resolver_wait_resolv()
  480. *
  481. * Waits for a resolve to finish. This function should be avoided since using
  482. * this risk getting the multi interface to "hang".
  483. *
  484. * If 'entry' is non-NULL, make it point to the resolved dns entry
  485. *
  486. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  487. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  488. *
  489. * This is the version for resolves-in-a-thread.
  490. */
  491. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  492. struct Curl_dns_entry **entry)
  493. {
  494. return thread_wait_resolv(conn, entry, TRUE);
  495. }
  496. /*
  497. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  498. * name resolve request has completed. It should also make sure to time-out if
  499. * the operation seems to take too long.
  500. */
  501. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  502. struct Curl_dns_entry **entry)
  503. {
  504. struct Curl_easy *data = conn->data;
  505. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  506. int done = 0;
  507. *entry = NULL;
  508. if(!td) {
  509. DEBUGASSERT(td);
  510. return CURLE_COULDNT_RESOLVE_HOST;
  511. }
  512. Curl_mutex_acquire(td->tsd.mtx);
  513. done = td->tsd.done;
  514. Curl_mutex_release(td->tsd.mtx);
  515. if(done) {
  516. getaddrinfo_complete(conn);
  517. if(!conn->async.dns) {
  518. CURLcode result = resolver_error(conn);
  519. destroy_async_data(&conn->async);
  520. return result;
  521. }
  522. destroy_async_data(&conn->async);
  523. *entry = conn->async.dns;
  524. }
  525. else {
  526. /* poll for name lookup done with exponential backoff up to 250ms */
  527. /* should be fine even if this converts to 32 bit */
  528. time_t elapsed = (time_t)Curl_timediff(Curl_now(),
  529. data->progress.t_startsingle);
  530. if(elapsed < 0)
  531. elapsed = 0;
  532. if(td->poll_interval == 0)
  533. /* Start at 1ms poll interval */
  534. td->poll_interval = 1;
  535. else if(elapsed >= td->interval_end)
  536. /* Back-off exponentially if last interval expired */
  537. td->poll_interval *= 2;
  538. if(td->poll_interval > 250)
  539. td->poll_interval = 250;
  540. td->interval_end = elapsed + td->poll_interval;
  541. Curl_expire(conn->data, td->poll_interval, EXPIRE_ASYNC_NAME);
  542. }
  543. return CURLE_OK;
  544. }
  545. int Curl_resolver_getsock(struct connectdata *conn,
  546. curl_socket_t *socks)
  547. {
  548. int ret_val = 0;
  549. time_t milli;
  550. timediff_t ms;
  551. struct Curl_easy *data = conn->data;
  552. struct resdata *reslv = (struct resdata *)data->state.resolver;
  553. #ifdef USE_SOCKETPAIR
  554. struct thread_data *td = (struct thread_data*)conn->async.os_specific;
  555. #else
  556. (void)socks;
  557. #endif
  558. #ifdef USE_SOCKETPAIR
  559. if(td) {
  560. /* return read fd to client for polling the DNS resolution status */
  561. socks[0] = td->tsd.sock_pair[0];
  562. DEBUGASSERT(td->tsd.conn == conn || !td->tsd.conn);
  563. td->tsd.conn = conn;
  564. ret_val = GETSOCK_READSOCK(0);
  565. }
  566. else {
  567. #endif
  568. ms = Curl_timediff(Curl_now(), reslv->start);
  569. if(ms < 3)
  570. milli = 0;
  571. else if(ms <= 50)
  572. milli = (time_t)ms/3;
  573. else if(ms <= 250)
  574. milli = 50;
  575. else
  576. milli = 200;
  577. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  578. #ifdef USE_SOCKETPAIR
  579. }
  580. #endif
  581. return ret_val;
  582. }
  583. #ifndef HAVE_GETADDRINFO
  584. /*
  585. * Curl_getaddrinfo() - for platforms without getaddrinfo
  586. */
  587. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  588. const char *hostname,
  589. int port,
  590. int *waitp)
  591. {
  592. struct Curl_easy *data = conn->data;
  593. struct resdata *reslv = (struct resdata *)data->state.resolver;
  594. *waitp = 0; /* default to synchronous response */
  595. reslv->start = Curl_now();
  596. /* fire up a new resolver thread! */
  597. if(init_resolve_thread(conn, hostname, port, NULL)) {
  598. *waitp = 1; /* expect asynchronous response */
  599. return NULL;
  600. }
  601. failf(conn->data, "getaddrinfo() thread failed\n");
  602. return NULL;
  603. }
  604. #else /* !HAVE_GETADDRINFO */
  605. /*
  606. * Curl_resolver_getaddrinfo() - for getaddrinfo
  607. */
  608. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  609. const char *hostname,
  610. int port,
  611. int *waitp)
  612. {
  613. struct addrinfo hints;
  614. int pf = PF_INET;
  615. struct Curl_easy *data = conn->data;
  616. struct resdata *reslv = (struct resdata *)data->state.resolver;
  617. *waitp = 0; /* default to synchronous response */
  618. #ifdef CURLRES_IPV6
  619. /*
  620. * Check if a limited name resolve has been requested.
  621. */
  622. switch(conn->ip_version) {
  623. case CURL_IPRESOLVE_V4:
  624. pf = PF_INET;
  625. break;
  626. case CURL_IPRESOLVE_V6:
  627. pf = PF_INET6;
  628. break;
  629. default:
  630. pf = PF_UNSPEC;
  631. break;
  632. }
  633. if((pf != PF_INET) && !Curl_ipv6works(conn))
  634. /* The stack seems to be a non-IPv6 one */
  635. pf = PF_INET;
  636. #endif /* CURLRES_IPV6 */
  637. memset(&hints, 0, sizeof(hints));
  638. hints.ai_family = pf;
  639. hints.ai_socktype = (conn->transport == TRNSPRT_TCP)?
  640. SOCK_STREAM : SOCK_DGRAM;
  641. reslv->start = Curl_now();
  642. /* fire up a new resolver thread! */
  643. if(init_resolve_thread(conn, hostname, port, &hints)) {
  644. *waitp = 1; /* expect asynchronous response */
  645. return NULL;
  646. }
  647. failf(data, "getaddrinfo() thread failed to start\n");
  648. return NULL;
  649. }
  650. #endif /* !HAVE_GETADDRINFO */
  651. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  652. char *servers)
  653. {
  654. (void)data;
  655. (void)servers;
  656. return CURLE_NOT_BUILT_IN;
  657. }
  658. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  659. const char *interf)
  660. {
  661. (void)data;
  662. (void)interf;
  663. return CURLE_NOT_BUILT_IN;
  664. }
  665. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  666. const char *local_ip4)
  667. {
  668. (void)data;
  669. (void)local_ip4;
  670. return CURLE_NOT_BUILT_IN;
  671. }
  672. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  673. const char *local_ip6)
  674. {
  675. (void)data;
  676. (void)local_ip6;
  677. return CURLE_NOT_BUILT_IN;
  678. }
  679. #endif /* CURLRES_THREADED */