asyn-thread.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.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. /*
  438. * 'entry' may be NULL and then no data is returned
  439. */
  440. static CURLcode thread_wait_resolv(struct connectdata *conn,
  441. struct Curl_dns_entry **entry,
  442. bool report)
  443. {
  444. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  445. CURLcode result = CURLE_OK;
  446. DEBUGASSERT(conn && td);
  447. DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
  448. /* wait for the thread to resolve the name */
  449. if(Curl_thread_join(&td->thread_hnd)) {
  450. if(entry)
  451. result = getaddrinfo_complete(conn);
  452. }
  453. else
  454. DEBUGASSERT(0);
  455. conn->async.done = TRUE;
  456. if(entry)
  457. *entry = conn->async.dns;
  458. if(!conn->async.dns && report)
  459. /* a name was not resolved, report error */
  460. result = resolver_error(conn);
  461. destroy_async_data(&conn->async);
  462. if(!conn->async.dns && report)
  463. connclose(conn, "asynch resolve failed");
  464. return result;
  465. }
  466. /*
  467. * Until we gain a way to signal the resolver threads to stop early, we must
  468. * simply wait for them and ignore their results.
  469. */
  470. void Curl_resolver_kill(struct connectdata *conn)
  471. {
  472. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  473. /* If we're still resolving, we must wait for the threads to fully clean up,
  474. unfortunately. Otherwise, we can simply cancel to clean up any resolver
  475. data. */
  476. if(td && td->thread_hnd != curl_thread_t_null)
  477. (void)thread_wait_resolv(conn, NULL, FALSE);
  478. else
  479. Curl_resolver_cancel(conn);
  480. }
  481. /*
  482. * Curl_resolver_wait_resolv()
  483. *
  484. * Waits for a resolve to finish. This function should be avoided since using
  485. * this risk getting the multi interface to "hang".
  486. *
  487. * If 'entry' is non-NULL, make it point to the resolved dns entry
  488. *
  489. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  490. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  491. *
  492. * This is the version for resolves-in-a-thread.
  493. */
  494. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  495. struct Curl_dns_entry **entry)
  496. {
  497. return thread_wait_resolv(conn, entry, TRUE);
  498. }
  499. /*
  500. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  501. * name resolve request has completed. It should also make sure to time-out if
  502. * the operation seems to take too long.
  503. */
  504. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  505. struct Curl_dns_entry **entry)
  506. {
  507. struct Curl_easy *data = conn->data;
  508. struct thread_data *td = (struct thread_data*) conn->async.os_specific;
  509. int done = 0;
  510. DEBUGASSERT(entry);
  511. *entry = NULL;
  512. if(!td) {
  513. DEBUGASSERT(td);
  514. return CURLE_COULDNT_RESOLVE_HOST;
  515. }
  516. Curl_mutex_acquire(td->tsd.mtx);
  517. done = td->tsd.done;
  518. Curl_mutex_release(td->tsd.mtx);
  519. if(done) {
  520. getaddrinfo_complete(conn);
  521. if(!conn->async.dns) {
  522. CURLcode result = resolver_error(conn);
  523. destroy_async_data(&conn->async);
  524. return result;
  525. }
  526. destroy_async_data(&conn->async);
  527. *entry = conn->async.dns;
  528. }
  529. else {
  530. /* poll for name lookup done with exponential backoff up to 250ms */
  531. /* should be fine even if this converts to 32 bit */
  532. time_t elapsed = (time_t)Curl_timediff(Curl_now(),
  533. data->progress.t_startsingle);
  534. if(elapsed < 0)
  535. elapsed = 0;
  536. if(td->poll_interval == 0)
  537. /* Start at 1ms poll interval */
  538. td->poll_interval = 1;
  539. else if(elapsed >= td->interval_end)
  540. /* Back-off exponentially if last interval expired */
  541. td->poll_interval *= 2;
  542. if(td->poll_interval > 250)
  543. td->poll_interval = 250;
  544. td->interval_end = elapsed + td->poll_interval;
  545. Curl_expire(conn->data, td->poll_interval, EXPIRE_ASYNC_NAME);
  546. }
  547. return CURLE_OK;
  548. }
  549. int Curl_resolver_getsock(struct connectdata *conn,
  550. curl_socket_t *socks)
  551. {
  552. int ret_val = 0;
  553. time_t milli;
  554. timediff_t ms;
  555. struct Curl_easy *data = conn->data;
  556. struct resdata *reslv = (struct resdata *)data->state.resolver;
  557. #ifdef USE_SOCKETPAIR
  558. struct thread_data *td = (struct thread_data*)conn->async.os_specific;
  559. #else
  560. (void)socks;
  561. #endif
  562. #ifdef USE_SOCKETPAIR
  563. if(td) {
  564. /* return read fd to client for polling the DNS resolution status */
  565. socks[0] = td->tsd.sock_pair[0];
  566. DEBUGASSERT(td->tsd.conn == conn || !td->tsd.conn);
  567. td->tsd.conn = conn;
  568. ret_val = GETSOCK_READSOCK(0);
  569. }
  570. else {
  571. #endif
  572. ms = Curl_timediff(Curl_now(), reslv->start);
  573. if(ms < 3)
  574. milli = 0;
  575. else if(ms <= 50)
  576. milli = (time_t)ms/3;
  577. else if(ms <= 250)
  578. milli = 50;
  579. else
  580. milli = 200;
  581. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  582. #ifdef USE_SOCKETPAIR
  583. }
  584. #endif
  585. return ret_val;
  586. }
  587. #ifndef HAVE_GETADDRINFO
  588. /*
  589. * Curl_getaddrinfo() - for platforms without getaddrinfo
  590. */
  591. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  592. const char *hostname,
  593. int port,
  594. int *waitp)
  595. {
  596. struct Curl_easy *data = conn->data;
  597. struct resdata *reslv = (struct resdata *)data->state.resolver;
  598. *waitp = 0; /* default to synchronous response */
  599. reslv->start = Curl_now();
  600. /* fire up a new resolver thread! */
  601. if(init_resolve_thread(conn, hostname, port, NULL)) {
  602. *waitp = 1; /* expect asynchronous response */
  603. return NULL;
  604. }
  605. failf(conn->data, "getaddrinfo() thread failed\n");
  606. return NULL;
  607. }
  608. #else /* !HAVE_GETADDRINFO */
  609. /*
  610. * Curl_resolver_getaddrinfo() - for getaddrinfo
  611. */
  612. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  613. const char *hostname,
  614. int port,
  615. int *waitp)
  616. {
  617. struct addrinfo hints;
  618. int pf = PF_INET;
  619. struct Curl_easy *data = conn->data;
  620. struct resdata *reslv = (struct resdata *)data->state.resolver;
  621. *waitp = 0; /* default to synchronous response */
  622. #ifdef CURLRES_IPV6
  623. /*
  624. * Check if a limited name resolve has been requested.
  625. */
  626. switch(conn->ip_version) {
  627. case CURL_IPRESOLVE_V4:
  628. pf = PF_INET;
  629. break;
  630. case CURL_IPRESOLVE_V6:
  631. pf = PF_INET6;
  632. break;
  633. default:
  634. pf = PF_UNSPEC;
  635. break;
  636. }
  637. if((pf != PF_INET) && !Curl_ipv6works(conn))
  638. /* The stack seems to be a non-IPv6 one */
  639. pf = PF_INET;
  640. #endif /* CURLRES_IPV6 */
  641. memset(&hints, 0, sizeof(hints));
  642. hints.ai_family = pf;
  643. hints.ai_socktype = (conn->transport == TRNSPRT_TCP)?
  644. SOCK_STREAM : SOCK_DGRAM;
  645. reslv->start = Curl_now();
  646. /* fire up a new resolver thread! */
  647. if(init_resolve_thread(conn, hostname, port, &hints)) {
  648. *waitp = 1; /* expect asynchronous response */
  649. return NULL;
  650. }
  651. failf(data, "getaddrinfo() thread failed to start\n");
  652. return NULL;
  653. }
  654. #endif /* !HAVE_GETADDRINFO */
  655. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  656. char *servers)
  657. {
  658. (void)data;
  659. (void)servers;
  660. return CURLE_NOT_BUILT_IN;
  661. }
  662. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  663. const char *interf)
  664. {
  665. (void)data;
  666. (void)interf;
  667. return CURLE_NOT_BUILT_IN;
  668. }
  669. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  670. const char *local_ip4)
  671. {
  672. (void)data;
  673. (void)local_ip4;
  674. return CURLE_NOT_BUILT_IN;
  675. }
  676. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  677. const char *local_ip6)
  678. {
  679. (void)data;
  680. (void)local_ip6;
  681. return CURLE_NOT_BUILT_IN;
  682. }
  683. #endif /* CURLRES_THREADED */