asyn-thread.c 20 KB

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