2
0

asyn-ares.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. /***********************************************************************
  24. * Only for ares-enabled builds
  25. * And only for functions that fulfill the asynch resolver backend API
  26. * as defined in asyn.h, nothing else belongs in this file!
  27. **********************************************************************/
  28. #ifdef CURLRES_ARES
  29. #include <limits.h>
  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. #ifdef HAVE_PROCESS_H
  44. #include <process.h>
  45. #endif
  46. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  47. #undef in_addr_t
  48. #define in_addr_t unsigned long
  49. #endif
  50. #include "urldata.h"
  51. #include "sendf.h"
  52. #include "hostip.h"
  53. #include "hash.h"
  54. #include "share.h"
  55. #include "strerror.h"
  56. #include "url.h"
  57. #include "multiif.h"
  58. #include "inet_pton.h"
  59. #include "connect.h"
  60. #include "select.h"
  61. #include "progress.h"
  62. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  63. (defined(WIN32) || defined(__SYMBIAN32__))
  64. # define CARES_STATICLIB
  65. # endif
  66. # include <ares.h>
  67. # include <ares_version.h> /* really old c-ares didn't include this by
  68. itself */
  69. #if ARES_VERSION >= 0x010500
  70. /* c-ares 1.5.0 or later, the callback proto is modified */
  71. #define HAVE_CARES_CALLBACK_TIMEOUTS 1
  72. #endif
  73. /* The last 3 #include files should be in this order */
  74. #include "curl_printf.h"
  75. #include "curl_memory.h"
  76. #include "memdebug.h"
  77. struct ResolverResults {
  78. int num_pending; /* number of ares_gethostbyname() requests */
  79. struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares
  80. parts */
  81. int last_status;
  82. struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */
  83. };
  84. /* How long we are willing to wait for additional parallel responses after
  85. obtaining a "definitive" one.
  86. This is intended to equal the c-ares default timeout. cURL always uses that
  87. default value. Unfortunately, c-ares doesn't expose its default timeout in
  88. its API, but it is officially documented as 5 seconds.
  89. See query_completed_cb() for an explanation of how this is used.
  90. */
  91. #define HAPPY_EYEBALLS_DNS_TIMEOUT 5000
  92. /*
  93. * Curl_resolver_global_init() - the generic low-level asynchronous name
  94. * resolve API. Called from curl_global_init() to initialize global resolver
  95. * environment. Initializes ares library.
  96. */
  97. int Curl_resolver_global_init(void)
  98. {
  99. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  100. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  101. return CURLE_FAILED_INIT;
  102. }
  103. #endif
  104. return CURLE_OK;
  105. }
  106. /*
  107. * Curl_resolver_global_cleanup()
  108. *
  109. * Called from curl_global_cleanup() to destroy global resolver environment.
  110. * Deinitializes ares library.
  111. */
  112. void Curl_resolver_global_cleanup(void)
  113. {
  114. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  115. ares_library_cleanup();
  116. #endif
  117. }
  118. static void Curl_ares_sock_state_cb(void *data, ares_socket_t socket_fd,
  119. int readable, int writable)
  120. {
  121. struct Curl_easy *easy = data;
  122. if(!readable && !writable) {
  123. DEBUGASSERT(easy);
  124. Curl_multi_closed(easy, socket_fd);
  125. }
  126. }
  127. /*
  128. * Curl_resolver_init()
  129. *
  130. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  131. * URL-state specific environment ('resolver' member of the UrlState
  132. * structure). Fills the passed pointer by the initialized ares_channel.
  133. */
  134. CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
  135. {
  136. int status;
  137. struct ares_options options;
  138. int optmask = ARES_OPT_SOCK_STATE_CB;
  139. options.sock_state_cb = Curl_ares_sock_state_cb;
  140. options.sock_state_cb_data = easy;
  141. status = ares_init_options((ares_channel*)resolver, &options, optmask);
  142. if(status != ARES_SUCCESS) {
  143. if(status == ARES_ENOMEM)
  144. return CURLE_OUT_OF_MEMORY;
  145. else
  146. return CURLE_FAILED_INIT;
  147. }
  148. return CURLE_OK;
  149. /* make sure that all other returns from this function should destroy the
  150. ares channel before returning error! */
  151. }
  152. /*
  153. * Curl_resolver_cleanup()
  154. *
  155. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  156. * URL-state specific environment ('resolver' member of the UrlState
  157. * structure). Destroys the ares channel.
  158. */
  159. void Curl_resolver_cleanup(void *resolver)
  160. {
  161. ares_destroy((ares_channel)resolver);
  162. }
  163. /*
  164. * Curl_resolver_duphandle()
  165. *
  166. * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
  167. * environment ('resolver' member of the UrlState structure). Duplicates the
  168. * 'from' ares channel and passes the resulting channel to the 'to' pointer.
  169. */
  170. CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
  171. {
  172. (void)from;
  173. /*
  174. * it would be better to call ares_dup instead, but right now
  175. * it is not possible to set 'sock_state_cb_data' outside of
  176. * ares_init_options
  177. */
  178. return Curl_resolver_init(easy, to);
  179. }
  180. static void destroy_async_data(struct Curl_async *async);
  181. /*
  182. * Cancel all possibly still on-going resolves for this connection.
  183. */
  184. void Curl_resolver_cancel(struct connectdata *conn)
  185. {
  186. if(conn->data && conn->data->state.resolver)
  187. ares_cancel((ares_channel)conn->data->state.resolver);
  188. destroy_async_data(&conn->async);
  189. }
  190. /*
  191. * We're equivalent to Curl_resolver_cancel() for the c-ares resolver. We
  192. * never block.
  193. */
  194. void Curl_resolver_kill(struct connectdata *conn)
  195. {
  196. /* We don't need to check the resolver state because we can be called safely
  197. at any time and we always do the same thing. */
  198. Curl_resolver_cancel(conn);
  199. }
  200. /*
  201. * destroy_async_data() cleans up async resolver data.
  202. */
  203. static void destroy_async_data(struct Curl_async *async)
  204. {
  205. free(async->hostname);
  206. if(async->os_specific) {
  207. struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
  208. if(res) {
  209. if(res->temp_ai) {
  210. Curl_freeaddrinfo(res->temp_ai);
  211. res->temp_ai = NULL;
  212. }
  213. free(res);
  214. }
  215. async->os_specific = NULL;
  216. }
  217. async->hostname = NULL;
  218. }
  219. /*
  220. * Curl_resolver_getsock() is called when someone from the outside world
  221. * (using curl_multi_fdset()) wants to get our fd_set setup and we're talking
  222. * with ares. The caller must make sure that this function is only called when
  223. * we have a working ares channel.
  224. *
  225. * Returns: sockets-in-use-bitmap
  226. */
  227. int Curl_resolver_getsock(struct connectdata *conn,
  228. curl_socket_t *socks)
  229. {
  230. struct timeval maxtime;
  231. struct timeval timebuf;
  232. struct timeval *timeout;
  233. long milli;
  234. int max = ares_getsock((ares_channel)conn->data->state.resolver,
  235. (ares_socket_t *)socks, MAX_SOCKSPEREASYHANDLE);
  236. maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
  237. maxtime.tv_usec = 0;
  238. timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
  239. &timebuf);
  240. milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
  241. if(milli == 0)
  242. milli += 10;
  243. Curl_expire(conn->data, milli, EXPIRE_ASYNC_NAME);
  244. return max;
  245. }
  246. /*
  247. * waitperform()
  248. *
  249. * 1) Ask ares what sockets it currently plays with, then
  250. * 2) wait for the timeout period to check for action on ares' sockets.
  251. * 3) tell ares to act on all the sockets marked as "with action"
  252. *
  253. * return number of sockets it worked on
  254. */
  255. static int waitperform(struct connectdata *conn, timediff_t timeout_ms)
  256. {
  257. struct Curl_easy *data = conn->data;
  258. int nfds;
  259. int bitmask;
  260. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  261. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  262. int i;
  263. int num = 0;
  264. bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
  265. ARES_GETSOCK_MAXNUM);
  266. for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
  267. pfd[i].events = 0;
  268. pfd[i].revents = 0;
  269. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  270. pfd[i].fd = socks[i];
  271. pfd[i].events |= POLLRDNORM|POLLIN;
  272. }
  273. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  274. pfd[i].fd = socks[i];
  275. pfd[i].events |= POLLWRNORM|POLLOUT;
  276. }
  277. if(pfd[i].events != 0)
  278. num++;
  279. else
  280. break;
  281. }
  282. if(num)
  283. nfds = Curl_poll(pfd, num, timeout_ms);
  284. else
  285. nfds = 0;
  286. if(!nfds)
  287. /* Call ares_process() unconditonally here, even if we simply timed out
  288. above, as otherwise the ares name resolve won't timeout! */
  289. ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
  290. ARES_SOCKET_BAD);
  291. else {
  292. /* move through the descriptors and ask for processing on them */
  293. for(i = 0; i < num; i++)
  294. ares_process_fd((ares_channel)data->state.resolver,
  295. (pfd[i].revents & (POLLRDNORM|POLLIN))?
  296. pfd[i].fd:ARES_SOCKET_BAD,
  297. (pfd[i].revents & (POLLWRNORM|POLLOUT))?
  298. pfd[i].fd:ARES_SOCKET_BAD);
  299. }
  300. return nfds;
  301. }
  302. /*
  303. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  304. * name resolve request has completed. It should also make sure to time-out if
  305. * the operation seems to take too long.
  306. *
  307. * Returns normal CURLcode errors.
  308. */
  309. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  310. struct Curl_dns_entry **dns)
  311. {
  312. struct Curl_easy *data = conn->data;
  313. struct ResolverResults *res = (struct ResolverResults *)
  314. conn->async.os_specific;
  315. CURLcode result = CURLE_OK;
  316. DEBUGASSERT(dns);
  317. *dns = NULL;
  318. waitperform(conn, 0);
  319. /* Now that we've checked for any last minute results above, see if there are
  320. any responses still pending when the EXPIRE_HAPPY_EYEBALLS_DNS timer
  321. expires. */
  322. if(res
  323. && res->num_pending
  324. /* This is only set to non-zero if the timer was started. */
  325. && (res->happy_eyeballs_dns_time.tv_sec
  326. || res->happy_eyeballs_dns_time.tv_usec)
  327. && (Curl_timediff(Curl_now(), res->happy_eyeballs_dns_time)
  328. >= HAPPY_EYEBALLS_DNS_TIMEOUT)) {
  329. /* Remember that the EXPIRE_HAPPY_EYEBALLS_DNS timer is no longer
  330. running. */
  331. memset(
  332. &res->happy_eyeballs_dns_time, 0, sizeof(res->happy_eyeballs_dns_time));
  333. /* Cancel the raw c-ares request, which will fire query_completed_cb() with
  334. ARES_ECANCELLED synchronously for all pending responses. This will
  335. leave us with res->num_pending == 0, which is perfect for the next
  336. block. */
  337. ares_cancel((ares_channel)data->state.resolver);
  338. DEBUGASSERT(res->num_pending == 0);
  339. }
  340. if(res && !res->num_pending) {
  341. (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
  342. /* temp_ai ownership is moved to the connection, so we need not free-up
  343. them */
  344. res->temp_ai = NULL;
  345. if(!conn->async.dns) {
  346. failf(data, "Could not resolve: %s (%s)",
  347. conn->async.hostname, ares_strerror(conn->async.status));
  348. result = conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
  349. CURLE_COULDNT_RESOLVE_HOST;
  350. }
  351. else
  352. *dns = conn->async.dns;
  353. destroy_async_data(&conn->async);
  354. }
  355. return result;
  356. }
  357. /*
  358. * Curl_resolver_wait_resolv()
  359. *
  360. * Waits for a resolve to finish. This function should be avoided since using
  361. * this risk getting the multi interface to "hang".
  362. *
  363. * 'entry' MUST be non-NULL.
  364. *
  365. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  366. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  367. */
  368. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  369. struct Curl_dns_entry **entry)
  370. {
  371. CURLcode result = CURLE_OK;
  372. struct Curl_easy *data = conn->data;
  373. timediff_t timeout;
  374. struct curltime now = Curl_now();
  375. DEBUGASSERT(entry);
  376. *entry = NULL; /* clear on entry */
  377. timeout = Curl_timeleft(data, &now, TRUE);
  378. if(timeout < 0) {
  379. /* already expired! */
  380. connclose(conn, "Timed out before name resolve started");
  381. return CURLE_OPERATION_TIMEDOUT;
  382. }
  383. if(!timeout)
  384. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  385. /* Wait for the name resolve query to complete. */
  386. while(!result) {
  387. struct timeval *tvp, tv, store;
  388. int itimeout;
  389. timediff_t timeout_ms;
  390. #if TIMEDIFF_T_MAX > INT_MAX
  391. itimeout = (timeout > INT_MAX) ? INT_MAX : (int)timeout;
  392. #else
  393. itimeout = (int)timeout;
  394. #endif
  395. store.tv_sec = itimeout/1000;
  396. store.tv_usec = (itimeout%1000)*1000;
  397. tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
  398. /* use the timeout period ares returned to us above if less than one
  399. second is left, otherwise just use 1000ms to make sure the progress
  400. callback gets called frequent enough */
  401. if(!tvp->tv_sec)
  402. timeout_ms = (timediff_t)(tvp->tv_usec/1000);
  403. else
  404. timeout_ms = 1000;
  405. waitperform(conn, timeout_ms);
  406. result = Curl_resolver_is_resolved(conn, entry);
  407. if(result || conn->async.done)
  408. break;
  409. if(Curl_pgrsUpdate(conn))
  410. result = CURLE_ABORTED_BY_CALLBACK;
  411. else {
  412. struct curltime now2 = Curl_now();
  413. timediff_t timediff = Curl_timediff(now2, now); /* spent time */
  414. if(timediff <= 0)
  415. timeout -= 1; /* always deduct at least 1 */
  416. else if(timediff > timeout)
  417. timeout = -1;
  418. else
  419. timeout -= timediff;
  420. now = now2; /* for next loop */
  421. }
  422. if(timeout < 0)
  423. result = CURLE_OPERATION_TIMEDOUT;
  424. }
  425. if(result)
  426. /* failure, so we cancel the ares operation */
  427. ares_cancel((ares_channel)data->state.resolver);
  428. /* Operation complete, if the lookup was successful we now have the entry
  429. in the cache. */
  430. if(entry)
  431. *entry = conn->async.dns;
  432. if(result)
  433. /* close the connection, since we can't return failure here without
  434. cleaning up this connection properly. */
  435. connclose(conn, "c-ares resolve failed");
  436. return result;
  437. }
  438. /* Connects results to the list */
  439. static void compound_results(struct ResolverResults *res,
  440. struct Curl_addrinfo *ai)
  441. {
  442. struct Curl_addrinfo *ai_tail;
  443. if(!ai)
  444. return;
  445. ai_tail = ai;
  446. while(ai_tail->ai_next)
  447. ai_tail = ai_tail->ai_next;
  448. /* Add the new results to the list of old results. */
  449. ai_tail->ai_next = res->temp_ai;
  450. res->temp_ai = ai;
  451. }
  452. /*
  453. * ares_query_completed_cb() is the callback that ares will call when
  454. * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
  455. * when using ares, is completed either successfully or with failure.
  456. */
  457. static void query_completed_cb(void *arg, /* (struct connectdata *) */
  458. int status,
  459. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  460. int timeouts,
  461. #endif
  462. struct hostent *hostent)
  463. {
  464. struct connectdata *conn = (struct connectdata *)arg;
  465. struct ResolverResults *res;
  466. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  467. (void)timeouts; /* ignored */
  468. #endif
  469. if(ARES_EDESTRUCTION == status)
  470. /* when this ares handle is getting destroyed, the 'arg' pointer may not
  471. be valid so only defer it when we know the 'status' says its fine! */
  472. return;
  473. res = (struct ResolverResults *)conn->async.os_specific;
  474. if(res) {
  475. res->num_pending--;
  476. if(CURL_ASYNC_SUCCESS == status) {
  477. struct Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
  478. if(ai) {
  479. compound_results(res, ai);
  480. }
  481. }
  482. /* A successful result overwrites any previous error */
  483. if(res->last_status != ARES_SUCCESS)
  484. res->last_status = status;
  485. /* If there are responses still pending, we presume they must be the
  486. complementary IPv4 or IPv6 lookups that we started in parallel in
  487. Curl_resolver_getaddrinfo() (for Happy Eyeballs). If we've got a
  488. "definitive" response from one of a set of parallel queries, we need to
  489. think about how long we're willing to wait for more responses. */
  490. if(res->num_pending
  491. /* Only these c-ares status values count as "definitive" for these
  492. purposes. For example, ARES_ENODATA is what we expect when there is
  493. no IPv6 entry for a domain name, and that's not a reason to get more
  494. aggressive in our timeouts for the other response. Other errors are
  495. either a result of bad input (which should affect all parallel
  496. requests), local or network conditions, non-definitive server
  497. responses, or us cancelling the request. */
  498. && (status == ARES_SUCCESS || status == ARES_ENOTFOUND)) {
  499. /* Right now, there can only be up to two parallel queries, so don't
  500. bother handling any other cases. */
  501. DEBUGASSERT(res->num_pending == 1);
  502. /* It's possible that one of these parallel queries could succeed
  503. quickly, but the other could always fail or timeout (when we're
  504. talking to a pool of DNS servers that can only successfully resolve
  505. IPv4 address, for example).
  506. It's also possible that the other request could always just take
  507. longer because it needs more time or only the second DNS server can
  508. fulfill it successfully. But, to align with the philosophy of Happy
  509. Eyeballs, we don't want to wait _too_ long or users will think
  510. requests are slow when IPv6 lookups don't actually work (but IPv4 ones
  511. do).
  512. So, now that we have a usable answer (some IPv4 addresses, some IPv6
  513. addresses, or "no such domain"), we start a timeout for the remaining
  514. pending responses. Even though it is typical that this resolved
  515. request came back quickly, that needn't be the case. It might be that
  516. this completing request didn't get a result from the first DNS server
  517. or even the first round of the whole DNS server pool. So it could
  518. already be quite some time after we issued the DNS queries in the
  519. first place. Without modifying c-ares, we can't know exactly where in
  520. its retry cycle we are. We could guess based on how much time has
  521. gone by, but it doesn't really matter. Happy Eyeballs tells us that,
  522. given usable information in hand, we simply don't want to wait "too
  523. much longer" after we get a result.
  524. We simply wait an additional amount of time equal to the default
  525. c-ares query timeout. That is enough time for a typical parallel
  526. response to arrive without being "too long". Even on a network
  527. where one of the two types of queries is failing or timing out
  528. constantly, this will usually mean we wait a total of the default
  529. c-ares timeout (5 seconds) plus the round trip time for the successful
  530. request, which seems bearable. The downside is that c-ares might race
  531. with us to issue one more retry just before we give up, but it seems
  532. better to "waste" that request instead of trying to guess the perfect
  533. timeout to prevent it. After all, we don't even know where in the
  534. c-ares retry cycle each request is.
  535. */
  536. res->happy_eyeballs_dns_time = Curl_now();
  537. Curl_expire(
  538. conn->data, HAPPY_EYEBALLS_DNS_TIMEOUT, EXPIRE_HAPPY_EYEBALLS_DNS);
  539. }
  540. }
  541. }
  542. /*
  543. * Curl_resolver_getaddrinfo() - when using ares
  544. *
  545. * Returns name information about the given hostname and port number. If
  546. * successful, the 'hostent' is returned and the forth argument will point to
  547. * memory we need to free after use. That memory *MUST* be freed with
  548. * Curl_freeaddrinfo(), nothing else.
  549. */
  550. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  551. const char *hostname,
  552. int port,
  553. int *waitp)
  554. {
  555. char *bufp;
  556. struct Curl_easy *data = conn->data;
  557. int family = PF_INET;
  558. *waitp = 0; /* default to synchronous response */
  559. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  560. switch(conn->ip_version) {
  561. default:
  562. #if ARES_VERSION >= 0x010601
  563. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  564. c-ares versions this just falls through and defaults
  565. to PF_INET */
  566. break;
  567. #endif
  568. case CURL_IPRESOLVE_V4:
  569. family = PF_INET;
  570. break;
  571. case CURL_IPRESOLVE_V6:
  572. family = PF_INET6;
  573. break;
  574. }
  575. #endif /* CURLRES_IPV6 */
  576. bufp = strdup(hostname);
  577. if(bufp) {
  578. struct ResolverResults *res = NULL;
  579. free(conn->async.hostname);
  580. conn->async.hostname = bufp;
  581. conn->async.port = port;
  582. conn->async.done = FALSE; /* not done */
  583. conn->async.status = 0; /* clear */
  584. conn->async.dns = NULL; /* clear */
  585. res = calloc(sizeof(struct ResolverResults), 1);
  586. if(!res) {
  587. free(conn->async.hostname);
  588. conn->async.hostname = NULL;
  589. return NULL;
  590. }
  591. conn->async.os_specific = res;
  592. /* initial status - failed */
  593. res->last_status = ARES_ENOTFOUND;
  594. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  595. if(family == PF_UNSPEC) {
  596. if(Curl_ipv6works(conn)) {
  597. res->num_pending = 2;
  598. /* areschannel is already setup in the Curl_open() function */
  599. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  600. PF_INET, query_completed_cb, conn);
  601. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  602. PF_INET6, query_completed_cb, conn);
  603. }
  604. else {
  605. res->num_pending = 1;
  606. /* areschannel is already setup in the Curl_open() function */
  607. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  608. PF_INET, query_completed_cb, conn);
  609. }
  610. }
  611. else
  612. #endif /* CURLRES_IPV6 */
  613. {
  614. res->num_pending = 1;
  615. /* areschannel is already setup in the Curl_open() function */
  616. ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
  617. query_completed_cb, conn);
  618. }
  619. *waitp = 1; /* expect asynchronous response */
  620. }
  621. return NULL; /* no struct yet */
  622. }
  623. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  624. char *servers)
  625. {
  626. CURLcode result = CURLE_NOT_BUILT_IN;
  627. int ares_result;
  628. /* If server is NULL or empty, this would purge all DNS servers
  629. * from ares library, which will cause any and all queries to fail.
  630. * So, just return OK if none are configured and don't actually make
  631. * any changes to c-ares. This lets c-ares use it's defaults, which
  632. * it gets from the OS (for instance from /etc/resolv.conf on Linux).
  633. */
  634. if(!(servers && servers[0]))
  635. return CURLE_OK;
  636. #if (ARES_VERSION >= 0x010704)
  637. #if (ARES_VERSION >= 0x010b00)
  638. ares_result = ares_set_servers_ports_csv(data->state.resolver, servers);
  639. #else
  640. ares_result = ares_set_servers_csv(data->state.resolver, servers);
  641. #endif
  642. switch(ares_result) {
  643. case ARES_SUCCESS:
  644. result = CURLE_OK;
  645. break;
  646. case ARES_ENOMEM:
  647. result = CURLE_OUT_OF_MEMORY;
  648. break;
  649. case ARES_ENOTINITIALIZED:
  650. case ARES_ENODATA:
  651. case ARES_EBADSTR:
  652. default:
  653. result = CURLE_BAD_FUNCTION_ARGUMENT;
  654. break;
  655. }
  656. #else /* too old c-ares version! */
  657. (void)data;
  658. (void)(ares_result);
  659. #endif
  660. return result;
  661. }
  662. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  663. const char *interf)
  664. {
  665. #if (ARES_VERSION >= 0x010704)
  666. if(!interf)
  667. interf = "";
  668. ares_set_local_dev((ares_channel)data->state.resolver, interf);
  669. return CURLE_OK;
  670. #else /* c-ares version too old! */
  671. (void)data;
  672. (void)interf;
  673. return CURLE_NOT_BUILT_IN;
  674. #endif
  675. }
  676. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  677. const char *local_ip4)
  678. {
  679. #if (ARES_VERSION >= 0x010704)
  680. struct in_addr a4;
  681. if((!local_ip4) || (local_ip4[0] == 0)) {
  682. a4.s_addr = 0; /* disabled: do not bind to a specific address */
  683. }
  684. else {
  685. if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
  686. return CURLE_BAD_FUNCTION_ARGUMENT;
  687. }
  688. }
  689. ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
  690. return CURLE_OK;
  691. #else /* c-ares version too old! */
  692. (void)data;
  693. (void)local_ip4;
  694. return CURLE_NOT_BUILT_IN;
  695. #endif
  696. }
  697. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  698. const char *local_ip6)
  699. {
  700. #if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
  701. unsigned char a6[INET6_ADDRSTRLEN];
  702. if((!local_ip6) || (local_ip6[0] == 0)) {
  703. /* disabled: do not bind to a specific address */
  704. memset(a6, 0, sizeof(a6));
  705. }
  706. else {
  707. if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
  708. return CURLE_BAD_FUNCTION_ARGUMENT;
  709. }
  710. }
  711. ares_set_local_ip6((ares_channel)data->state.resolver, a6);
  712. return CURLE_OK;
  713. #else /* c-ares version too old! */
  714. (void)data;
  715. (void)local_ip6;
  716. return CURLE_NOT_BUILT_IN;
  717. #endif
  718. }
  719. #endif /* CURLRES_ARES */