asyn-ares.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 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(_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. Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares parts */
  80. int last_status;
  81. };
  82. /*
  83. * Curl_resolver_global_init() - the generic low-level asynchronous name
  84. * resolve API. Called from curl_global_init() to initialize global resolver
  85. * environment. Initializes ares library.
  86. */
  87. int Curl_resolver_global_init(void)
  88. {
  89. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  90. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  91. return CURLE_FAILED_INIT;
  92. }
  93. #endif
  94. return CURLE_OK;
  95. }
  96. /*
  97. * Curl_resolver_global_cleanup()
  98. *
  99. * Called from curl_global_cleanup() to destroy global resolver environment.
  100. * Deinitializes ares library.
  101. */
  102. void Curl_resolver_global_cleanup(void)
  103. {
  104. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  105. ares_library_cleanup();
  106. #endif
  107. }
  108. static void Curl_ares_sock_state_cb(void *data, ares_socket_t socket_fd,
  109. int readable, int writable)
  110. {
  111. struct Curl_easy *easy = data;
  112. if(!readable && !writable) {
  113. DEBUGASSERT(easy);
  114. Curl_multi_closed(easy, socket_fd);
  115. }
  116. }
  117. /*
  118. * Curl_resolver_init()
  119. *
  120. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  121. * URL-state specific environment ('resolver' member of the UrlState
  122. * structure). Fills the passed pointer by the initialized ares_channel.
  123. */
  124. CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
  125. {
  126. int status;
  127. struct ares_options options;
  128. int optmask = ARES_OPT_SOCK_STATE_CB;
  129. options.sock_state_cb = Curl_ares_sock_state_cb;
  130. options.sock_state_cb_data = easy;
  131. status = ares_init_options((ares_channel*)resolver, &options, optmask);
  132. if(status != ARES_SUCCESS) {
  133. if(status == ARES_ENOMEM)
  134. return CURLE_OUT_OF_MEMORY;
  135. else
  136. return CURLE_FAILED_INIT;
  137. }
  138. return CURLE_OK;
  139. /* make sure that all other returns from this function should destroy the
  140. ares channel before returning error! */
  141. }
  142. /*
  143. * Curl_resolver_cleanup()
  144. *
  145. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  146. * URL-state specific environment ('resolver' member of the UrlState
  147. * structure). Destroys the ares channel.
  148. */
  149. void Curl_resolver_cleanup(void *resolver)
  150. {
  151. ares_destroy((ares_channel)resolver);
  152. }
  153. /*
  154. * Curl_resolver_duphandle()
  155. *
  156. * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
  157. * environment ('resolver' member of the UrlState structure). Duplicates the
  158. * 'from' ares channel and passes the resulting channel to the 'to' pointer.
  159. */
  160. CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
  161. {
  162. (void)from;
  163. /*
  164. * it would be better to call ares_dup instead, but right now
  165. * it is not possible to set 'sock_state_cb_data' outside of
  166. * ares_init_options
  167. */
  168. return Curl_resolver_init(easy, to);
  169. }
  170. static void destroy_async_data(struct Curl_async *async);
  171. /*
  172. * Cancel all possibly still on-going resolves for this connection.
  173. */
  174. void Curl_resolver_cancel(struct connectdata *conn)
  175. {
  176. if(conn->data && conn->data->state.resolver)
  177. ares_cancel((ares_channel)conn->data->state.resolver);
  178. destroy_async_data(&conn->async);
  179. }
  180. /*
  181. * We're equivalent to Curl_resolver_cancel() for the c-ares resolver. We
  182. * never block.
  183. */
  184. void Curl_resolver_kill(struct connectdata *conn)
  185. {
  186. /* We don't need to check the resolver state because we can be called safely
  187. at any time and we always do the same thing. */
  188. Curl_resolver_cancel(conn);
  189. }
  190. /*
  191. * destroy_async_data() cleans up async resolver data.
  192. */
  193. static void destroy_async_data(struct Curl_async *async)
  194. {
  195. free(async->hostname);
  196. if(async->os_specific) {
  197. struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
  198. if(res) {
  199. if(res->temp_ai) {
  200. Curl_freeaddrinfo(res->temp_ai);
  201. res->temp_ai = NULL;
  202. }
  203. free(res);
  204. }
  205. async->os_specific = NULL;
  206. }
  207. async->hostname = NULL;
  208. }
  209. /*
  210. * Curl_resolver_getsock() is called when someone from the outside world
  211. * (using curl_multi_fdset()) wants to get our fd_set setup and we're talking
  212. * with ares. The caller must make sure that this function is only called when
  213. * we have a working ares channel.
  214. *
  215. * Returns: sockets-in-use-bitmap
  216. */
  217. int Curl_resolver_getsock(struct connectdata *conn,
  218. curl_socket_t *socks,
  219. int numsocks)
  220. {
  221. struct timeval maxtime;
  222. struct timeval timebuf;
  223. struct timeval *timeout;
  224. long milli;
  225. int max = ares_getsock((ares_channel)conn->data->state.resolver,
  226. (ares_socket_t *)socks, numsocks);
  227. maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
  228. maxtime.tv_usec = 0;
  229. timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
  230. &timebuf);
  231. milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
  232. if(milli == 0)
  233. milli += 10;
  234. Curl_expire(conn->data, milli, EXPIRE_ASYNC_NAME);
  235. return max;
  236. }
  237. /*
  238. * waitperform()
  239. *
  240. * 1) Ask ares what sockets it currently plays with, then
  241. * 2) wait for the timeout period to check for action on ares' sockets.
  242. * 3) tell ares to act on all the sockets marked as "with action"
  243. *
  244. * return number of sockets it worked on
  245. */
  246. static int waitperform(struct connectdata *conn, int timeout_ms)
  247. {
  248. struct Curl_easy *data = conn->data;
  249. int nfds;
  250. int bitmask;
  251. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  252. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  253. int i;
  254. int num = 0;
  255. bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
  256. ARES_GETSOCK_MAXNUM);
  257. for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
  258. pfd[i].events = 0;
  259. pfd[i].revents = 0;
  260. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  261. pfd[i].fd = socks[i];
  262. pfd[i].events |= POLLRDNORM|POLLIN;
  263. }
  264. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  265. pfd[i].fd = socks[i];
  266. pfd[i].events |= POLLWRNORM|POLLOUT;
  267. }
  268. if(pfd[i].events != 0)
  269. num++;
  270. else
  271. break;
  272. }
  273. if(num)
  274. nfds = Curl_poll(pfd, num, timeout_ms);
  275. else
  276. nfds = 0;
  277. if(!nfds)
  278. /* Call ares_process() unconditonally here, even if we simply timed out
  279. above, as otherwise the ares name resolve won't timeout! */
  280. ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
  281. ARES_SOCKET_BAD);
  282. else {
  283. /* move through the descriptors and ask for processing on them */
  284. for(i = 0; i < num; i++)
  285. ares_process_fd((ares_channel)data->state.resolver,
  286. pfd[i].revents & (POLLRDNORM|POLLIN)?
  287. pfd[i].fd:ARES_SOCKET_BAD,
  288. pfd[i].revents & (POLLWRNORM|POLLOUT)?
  289. pfd[i].fd:ARES_SOCKET_BAD);
  290. }
  291. return nfds;
  292. }
  293. /*
  294. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  295. * name resolve request has completed. It should also make sure to time-out if
  296. * the operation seems to take too long.
  297. *
  298. * Returns normal CURLcode errors.
  299. */
  300. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  301. struct Curl_dns_entry **dns)
  302. {
  303. struct Curl_easy *data = conn->data;
  304. struct ResolverResults *res = (struct ResolverResults *)
  305. conn->async.os_specific;
  306. CURLcode result = CURLE_OK;
  307. if(dns)
  308. *dns = NULL;
  309. waitperform(conn, 0);
  310. if(res && !res->num_pending) {
  311. if(dns) {
  312. (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
  313. /* temp_ai ownership is moved to the connection, so we need not free-up
  314. them */
  315. res->temp_ai = NULL;
  316. }
  317. if(!conn->async.dns) {
  318. failf(data, "Could not resolve: %s (%s)",
  319. conn->async.hostname, ares_strerror(conn->async.status));
  320. result = conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
  321. CURLE_COULDNT_RESOLVE_HOST;
  322. }
  323. else if(dns)
  324. *dns = conn->async.dns;
  325. destroy_async_data(&conn->async);
  326. }
  327. return result;
  328. }
  329. /*
  330. * Curl_resolver_wait_resolv()
  331. *
  332. * Waits for a resolve to finish. This function should be avoided since using
  333. * this risk getting the multi interface to "hang".
  334. *
  335. * If 'entry' is non-NULL, make it point to the resolved dns entry
  336. *
  337. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  338. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  339. */
  340. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  341. struct Curl_dns_entry **entry)
  342. {
  343. CURLcode result = CURLE_OK;
  344. struct Curl_easy *data = conn->data;
  345. timediff_t timeout;
  346. struct curltime now = Curl_now();
  347. struct Curl_dns_entry *temp_entry;
  348. if(entry)
  349. *entry = NULL; /* clear on entry */
  350. timeout = Curl_timeleft(data, &now, TRUE);
  351. if(timeout < 0) {
  352. /* already expired! */
  353. connclose(conn, "Timed out before name resolve started");
  354. return CURLE_OPERATION_TIMEDOUT;
  355. }
  356. if(!timeout)
  357. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  358. /* Wait for the name resolve query to complete. */
  359. while(!result) {
  360. struct timeval *tvp, tv, store;
  361. int itimeout;
  362. int timeout_ms;
  363. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  364. store.tv_sec = itimeout/1000;
  365. store.tv_usec = (itimeout%1000)*1000;
  366. tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
  367. /* use the timeout period ares returned to us above if less than one
  368. second is left, otherwise just use 1000ms to make sure the progress
  369. callback gets called frequent enough */
  370. if(!tvp->tv_sec)
  371. timeout_ms = (int)(tvp->tv_usec/1000);
  372. else
  373. timeout_ms = 1000;
  374. waitperform(conn, timeout_ms);
  375. result = Curl_resolver_is_resolved(conn, entry?&temp_entry:NULL);
  376. if(result || conn->async.done)
  377. break;
  378. if(Curl_pgrsUpdate(conn))
  379. result = CURLE_ABORTED_BY_CALLBACK;
  380. else {
  381. struct curltime now2 = Curl_now();
  382. timediff_t timediff = Curl_timediff(now2, now); /* spent time */
  383. if(timediff <= 0)
  384. timeout -= 1; /* always deduct at least 1 */
  385. else if(timediff > timeout)
  386. timeout = -1;
  387. else
  388. timeout -= (long)timediff;
  389. now = now2; /* for next loop */
  390. }
  391. if(timeout < 0)
  392. result = CURLE_OPERATION_TIMEDOUT;
  393. }
  394. if(result)
  395. /* failure, so we cancel the ares operation */
  396. ares_cancel((ares_channel)data->state.resolver);
  397. /* Operation complete, if the lookup was successful we now have the entry
  398. in the cache. */
  399. if(entry)
  400. *entry = conn->async.dns;
  401. if(result)
  402. /* close the connection, since we can't return failure here without
  403. cleaning up this connection properly.
  404. TODO: remove this action from here, it is not a name resolver decision.
  405. */
  406. connclose(conn, "c-ares resolve failed");
  407. return result;
  408. }
  409. /* Connects results to the list */
  410. static void compound_results(struct ResolverResults *res,
  411. Curl_addrinfo *ai)
  412. {
  413. Curl_addrinfo *ai_tail;
  414. if(!ai)
  415. return;
  416. ai_tail = ai;
  417. while(ai_tail->ai_next)
  418. ai_tail = ai_tail->ai_next;
  419. /* Add the new results to the list of old results. */
  420. ai_tail->ai_next = res->temp_ai;
  421. res->temp_ai = ai;
  422. }
  423. /*
  424. * ares_query_completed_cb() is the callback that ares will call when
  425. * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
  426. * when using ares, is completed either successfully or with failure.
  427. */
  428. static void query_completed_cb(void *arg, /* (struct connectdata *) */
  429. int status,
  430. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  431. int timeouts,
  432. #endif
  433. struct hostent *hostent)
  434. {
  435. struct connectdata *conn = (struct connectdata *)arg;
  436. struct ResolverResults *res;
  437. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  438. (void)timeouts; /* ignored */
  439. #endif
  440. if(ARES_EDESTRUCTION == status)
  441. /* when this ares handle is getting destroyed, the 'arg' pointer may not
  442. be valid so only defer it when we know the 'status' says its fine! */
  443. return;
  444. res = (struct ResolverResults *)conn->async.os_specific;
  445. if(res) {
  446. res->num_pending--;
  447. if(CURL_ASYNC_SUCCESS == status) {
  448. Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
  449. if(ai) {
  450. compound_results(res, ai);
  451. }
  452. }
  453. /* A successful result overwrites any previous error */
  454. if(res->last_status != ARES_SUCCESS)
  455. res->last_status = status;
  456. }
  457. }
  458. /*
  459. * Curl_resolver_getaddrinfo() - when using ares
  460. *
  461. * Returns name information about the given hostname and port number. If
  462. * successful, the 'hostent' is returned and the forth argument will point to
  463. * memory we need to free after use. That memory *MUST* be freed with
  464. * Curl_freeaddrinfo(), nothing else.
  465. */
  466. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  467. const char *hostname,
  468. int port,
  469. int *waitp)
  470. {
  471. char *bufp;
  472. struct Curl_easy *data = conn->data;
  473. struct in_addr in;
  474. int family = PF_INET;
  475. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  476. struct in6_addr in6;
  477. #endif /* CURLRES_IPV6 */
  478. *waitp = 0; /* default to synchronous response */
  479. /* First check if this is an IPv4 address string */
  480. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  481. /* This is a dotted IP address 123.123.123.123-style */
  482. return Curl_ip2addr(AF_INET, &in, hostname, port);
  483. }
  484. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  485. /* Otherwise, check if this is an IPv6 address string */
  486. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  487. /* This must be an IPv6 address literal. */
  488. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  489. switch(conn->ip_version) {
  490. default:
  491. #if ARES_VERSION >= 0x010601
  492. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  493. c-ares versions this just falls through and defaults
  494. to PF_INET */
  495. break;
  496. #endif
  497. case CURL_IPRESOLVE_V4:
  498. family = PF_INET;
  499. break;
  500. case CURL_IPRESOLVE_V6:
  501. family = PF_INET6;
  502. break;
  503. }
  504. #endif /* CURLRES_IPV6 */
  505. bufp = strdup(hostname);
  506. if(bufp) {
  507. struct ResolverResults *res = NULL;
  508. free(conn->async.hostname);
  509. conn->async.hostname = bufp;
  510. conn->async.port = port;
  511. conn->async.done = FALSE; /* not done */
  512. conn->async.status = 0; /* clear */
  513. conn->async.dns = NULL; /* clear */
  514. res = calloc(sizeof(struct ResolverResults), 1);
  515. if(!res) {
  516. free(conn->async.hostname);
  517. conn->async.hostname = NULL;
  518. return NULL;
  519. }
  520. conn->async.os_specific = res;
  521. /* initial status - failed */
  522. res->last_status = ARES_ENOTFOUND;
  523. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  524. if(family == PF_UNSPEC) {
  525. if(Curl_ipv6works()) {
  526. res->num_pending = 2;
  527. /* areschannel is already setup in the Curl_open() function */
  528. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  529. PF_INET, query_completed_cb, conn);
  530. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  531. PF_INET6, query_completed_cb, conn);
  532. }
  533. else {
  534. res->num_pending = 1;
  535. /* areschannel is already setup in the Curl_open() function */
  536. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  537. PF_INET, query_completed_cb, conn);
  538. }
  539. }
  540. else
  541. #endif /* CURLRES_IPV6 */
  542. {
  543. res->num_pending = 1;
  544. /* areschannel is already setup in the Curl_open() function */
  545. ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
  546. query_completed_cb, conn);
  547. }
  548. *waitp = 1; /* expect asynchronous response */
  549. }
  550. return NULL; /* no struct yet */
  551. }
  552. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  553. char *servers)
  554. {
  555. CURLcode result = CURLE_NOT_BUILT_IN;
  556. int ares_result;
  557. /* If server is NULL or empty, this would purge all DNS servers
  558. * from ares library, which will cause any and all queries to fail.
  559. * So, just return OK if none are configured and don't actually make
  560. * any changes to c-ares. This lets c-ares use it's defaults, which
  561. * it gets from the OS (for instance from /etc/resolv.conf on Linux).
  562. */
  563. if(!(servers && servers[0]))
  564. return CURLE_OK;
  565. #if (ARES_VERSION >= 0x010704)
  566. ares_result = ares_set_servers_csv(data->state.resolver, servers);
  567. switch(ares_result) {
  568. case ARES_SUCCESS:
  569. result = CURLE_OK;
  570. break;
  571. case ARES_ENOMEM:
  572. result = CURLE_OUT_OF_MEMORY;
  573. break;
  574. case ARES_ENOTINITIALIZED:
  575. case ARES_ENODATA:
  576. case ARES_EBADSTR:
  577. default:
  578. result = CURLE_BAD_FUNCTION_ARGUMENT;
  579. break;
  580. }
  581. #else /* too old c-ares version! */
  582. (void)data;
  583. (void)(ares_result);
  584. #endif
  585. return result;
  586. }
  587. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  588. const char *interf)
  589. {
  590. #if (ARES_VERSION >= 0x010704)
  591. if(!interf)
  592. interf = "";
  593. ares_set_local_dev((ares_channel)data->state.resolver, interf);
  594. return CURLE_OK;
  595. #else /* c-ares version too old! */
  596. (void)data;
  597. (void)interf;
  598. return CURLE_NOT_BUILT_IN;
  599. #endif
  600. }
  601. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  602. const char *local_ip4)
  603. {
  604. #if (ARES_VERSION >= 0x010704)
  605. struct in_addr a4;
  606. if((!local_ip4) || (local_ip4[0] == 0)) {
  607. a4.s_addr = 0; /* disabled: do not bind to a specific address */
  608. }
  609. else {
  610. if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
  611. return CURLE_BAD_FUNCTION_ARGUMENT;
  612. }
  613. }
  614. ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
  615. return CURLE_OK;
  616. #else /* c-ares version too old! */
  617. (void)data;
  618. (void)local_ip4;
  619. return CURLE_NOT_BUILT_IN;
  620. #endif
  621. }
  622. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  623. const char *local_ip6)
  624. {
  625. #if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
  626. unsigned char a6[INET6_ADDRSTRLEN];
  627. if((!local_ip6) || (local_ip6[0] == 0)) {
  628. /* disabled: do not bind to a specific address */
  629. memset(a6, 0, sizeof(a6));
  630. }
  631. else {
  632. if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
  633. return CURLE_BAD_FUNCTION_ARGUMENT;
  634. }
  635. }
  636. ares_set_local_ip6((ares_channel)data->state.resolver, a6);
  637. return CURLE_OK;
  638. #else /* c-ares version too old! */
  639. (void)data;
  640. (void)local_ip6;
  641. return CURLE_NOT_BUILT_IN;
  642. #endif
  643. }
  644. #endif /* CURLRES_ARES */