asyn-ares.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_LIMITS_H
  24. #include <limits.h>
  25. #endif
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_NETDB_H
  30. #include <netdb.h>
  31. #endif
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef __VMS
  36. #include <in.h>
  37. #include <inet.h>
  38. #endif
  39. #ifdef HAVE_PROCESS_H
  40. #include <process.h>
  41. #endif
  42. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  43. #undef in_addr_t
  44. #define in_addr_t unsigned long
  45. #endif
  46. /***********************************************************************
  47. * Only for ares-enabled builds
  48. * And only for functions that fulfill the asynch resolver backend API
  49. * as defined in asyn.h, nothing else belongs in this file!
  50. **********************************************************************/
  51. #ifdef CURLRES_ARES
  52. #include "urldata.h"
  53. #include "sendf.h"
  54. #include "hostip.h"
  55. #include "hash.h"
  56. #include "share.h"
  57. #include "strerror.h"
  58. #include "url.h"
  59. #include "multiif.h"
  60. #include "inet_pton.h"
  61. #include "connect.h"
  62. #include "select.h"
  63. #include "progress.h"
  64. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  65. (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__))
  66. # define CARES_STATICLIB
  67. # endif
  68. # include <ares.h>
  69. # include <ares_version.h> /* really old c-ares didn't include this by
  70. itself */
  71. #if ARES_VERSION >= 0x010500
  72. /* c-ares 1.5.0 or later, the callback proto is modified */
  73. #define HAVE_CARES_CALLBACK_TIMEOUTS 1
  74. #endif
  75. /* The last 3 #include files should be in this order */
  76. #include "curl_printf.h"
  77. #include "curl_memory.h"
  78. #include "memdebug.h"
  79. struct ResolverResults {
  80. int num_pending; /* number of ares_gethostbyname() requests */
  81. Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares parts */
  82. int last_status;
  83. };
  84. /*
  85. * Curl_resolver_global_init() - the generic low-level asynchronous name
  86. * resolve API. Called from curl_global_init() to initialize global resolver
  87. * environment. Initializes ares library.
  88. */
  89. int Curl_resolver_global_init(void)
  90. {
  91. #ifdef CARES_HAVE_ARES_LIBRARY_INIT
  92. if(ares_library_init(ARES_LIB_INIT_ALL)) {
  93. return CURLE_FAILED_INIT;
  94. }
  95. #endif
  96. return CURLE_OK;
  97. }
  98. /*
  99. * Curl_resolver_global_cleanup()
  100. *
  101. * Called from curl_global_cleanup() to destroy global resolver environment.
  102. * Deinitializes ares library.
  103. */
  104. void Curl_resolver_global_cleanup(void)
  105. {
  106. #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
  107. ares_library_cleanup();
  108. #endif
  109. }
  110. /*
  111. * Curl_resolver_init()
  112. *
  113. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  114. * URL-state specific environment ('resolver' member of the UrlState
  115. * structure). Fills the passed pointer by the initialized ares_channel.
  116. */
  117. CURLcode Curl_resolver_init(void **resolver)
  118. {
  119. int status = ares_init((ares_channel*)resolver);
  120. if(status != ARES_SUCCESS) {
  121. if(status == ARES_ENOMEM)
  122. return CURLE_OUT_OF_MEMORY;
  123. else
  124. return CURLE_FAILED_INIT;
  125. }
  126. return CURLE_OK;
  127. /* make sure that all other returns from this function should destroy the
  128. ares channel before returning error! */
  129. }
  130. /*
  131. * Curl_resolver_cleanup()
  132. *
  133. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  134. * URL-state specific environment ('resolver' member of the UrlState
  135. * structure). Destroys the ares channel.
  136. */
  137. void Curl_resolver_cleanup(void *resolver)
  138. {
  139. ares_destroy((ares_channel)resolver);
  140. }
  141. /*
  142. * Curl_resolver_duphandle()
  143. *
  144. * Called from curl_easy_duphandle() to duplicate resolver URL-state specific
  145. * environment ('resolver' member of the UrlState structure). Duplicates the
  146. * 'from' ares channel and passes the resulting channel to the 'to' pointer.
  147. */
  148. int Curl_resolver_duphandle(void **to, void *from)
  149. {
  150. /* Clone the ares channel for the new handle */
  151. if(ARES_SUCCESS != ares_dup((ares_channel*)to, (ares_channel)from))
  152. return CURLE_FAILED_INIT;
  153. return CURLE_OK;
  154. }
  155. static void destroy_async_data(struct Curl_async *async);
  156. /*
  157. * Cancel all possibly still on-going resolves for this connection.
  158. */
  159. void Curl_resolver_cancel(struct connectdata *conn)
  160. {
  161. if(conn->data && conn->data->state.resolver)
  162. ares_cancel((ares_channel)conn->data->state.resolver);
  163. destroy_async_data(&conn->async);
  164. }
  165. /*
  166. * destroy_async_data() cleans up async resolver data.
  167. */
  168. static void destroy_async_data(struct Curl_async *async)
  169. {
  170. free(async->hostname);
  171. if(async->os_specific) {
  172. struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
  173. if(res) {
  174. if(res->temp_ai) {
  175. Curl_freeaddrinfo(res->temp_ai);
  176. res->temp_ai = NULL;
  177. }
  178. free(res);
  179. }
  180. async->os_specific = NULL;
  181. }
  182. async->hostname = NULL;
  183. }
  184. /*
  185. * Curl_resolver_getsock() is called when someone from the outside world
  186. * (using curl_multi_fdset()) wants to get our fd_set setup and we're talking
  187. * with ares. The caller must make sure that this function is only called when
  188. * we have a working ares channel.
  189. *
  190. * Returns: sockets-in-use-bitmap
  191. */
  192. int Curl_resolver_getsock(struct connectdata *conn,
  193. curl_socket_t *socks,
  194. int numsocks)
  195. {
  196. struct timeval maxtime;
  197. struct timeval timebuf;
  198. struct timeval *timeout;
  199. long milli;
  200. int max = ares_getsock((ares_channel)conn->data->state.resolver,
  201. (ares_socket_t *)socks, numsocks);
  202. maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
  203. maxtime.tv_usec = 0;
  204. timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
  205. &timebuf);
  206. milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
  207. if(milli == 0)
  208. milli += 10;
  209. Curl_expire_latest(conn->data, milli);
  210. return max;
  211. }
  212. /*
  213. * waitperform()
  214. *
  215. * 1) Ask ares what sockets it currently plays with, then
  216. * 2) wait for the timeout period to check for action on ares' sockets.
  217. * 3) tell ares to act on all the sockets marked as "with action"
  218. *
  219. * return number of sockets it worked on
  220. */
  221. static int waitperform(struct connectdata *conn, int timeout_ms)
  222. {
  223. struct Curl_easy *data = conn->data;
  224. int nfds;
  225. int bitmask;
  226. ares_socket_t socks[ARES_GETSOCK_MAXNUM];
  227. struct pollfd pfd[ARES_GETSOCK_MAXNUM];
  228. int i;
  229. int num = 0;
  230. bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
  231. ARES_GETSOCK_MAXNUM);
  232. for(i=0; i < ARES_GETSOCK_MAXNUM; i++) {
  233. pfd[i].events = 0;
  234. pfd[i].revents = 0;
  235. if(ARES_GETSOCK_READABLE(bitmask, i)) {
  236. pfd[i].fd = socks[i];
  237. pfd[i].events |= POLLRDNORM|POLLIN;
  238. }
  239. if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
  240. pfd[i].fd = socks[i];
  241. pfd[i].events |= POLLWRNORM|POLLOUT;
  242. }
  243. if(pfd[i].events != 0)
  244. num++;
  245. else
  246. break;
  247. }
  248. if(num)
  249. nfds = Curl_poll(pfd, num, timeout_ms);
  250. else
  251. nfds = 0;
  252. if(!nfds)
  253. /* Call ares_process() unconditonally here, even if we simply timed out
  254. above, as otherwise the ares name resolve won't timeout! */
  255. ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
  256. ARES_SOCKET_BAD);
  257. else {
  258. /* move through the descriptors and ask for processing on them */
  259. for(i=0; i < num; i++)
  260. ares_process_fd((ares_channel)data->state.resolver,
  261. pfd[i].revents & (POLLRDNORM|POLLIN)?
  262. pfd[i].fd:ARES_SOCKET_BAD,
  263. pfd[i].revents & (POLLWRNORM|POLLOUT)?
  264. pfd[i].fd:ARES_SOCKET_BAD);
  265. }
  266. return nfds;
  267. }
  268. /*
  269. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  270. * name resolve request has completed. It should also make sure to time-out if
  271. * the operation seems to take too long.
  272. *
  273. * Returns normal CURLcode errors.
  274. */
  275. CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
  276. struct Curl_dns_entry **dns)
  277. {
  278. struct Curl_easy *data = conn->data;
  279. struct ResolverResults *res = (struct ResolverResults *)
  280. conn->async.os_specific;
  281. CURLcode result = CURLE_OK;
  282. *dns = NULL;
  283. waitperform(conn, 0);
  284. if(res && !res->num_pending) {
  285. (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
  286. /* temp_ai ownership is moved to the connection, so we need not free-up
  287. them */
  288. res->temp_ai = NULL;
  289. if(!conn->async.dns) {
  290. failf(data, "Could not resolve: %s (%s)",
  291. conn->async.hostname, ares_strerror(conn->async.status));
  292. result = conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
  293. CURLE_COULDNT_RESOLVE_HOST;
  294. }
  295. else
  296. *dns = conn->async.dns;
  297. destroy_async_data(&conn->async);
  298. }
  299. return result;
  300. }
  301. /*
  302. * Curl_resolver_wait_resolv()
  303. *
  304. * waits for a resolve to finish. This function should be avoided since using
  305. * this risk getting the multi interface to "hang".
  306. *
  307. * If 'entry' is non-NULL, make it point to the resolved dns entry
  308. *
  309. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved, and
  310. * CURLE_OPERATION_TIMEDOUT if a time-out occurred.
  311. */
  312. CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
  313. struct Curl_dns_entry **entry)
  314. {
  315. CURLcode result = CURLE_OK;
  316. struct Curl_easy *data = conn->data;
  317. long timeout;
  318. struct timeval now = Curl_tvnow();
  319. struct Curl_dns_entry *temp_entry;
  320. timeout = Curl_timeleft(data, &now, TRUE);
  321. if(!timeout)
  322. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  323. /* Wait for the name resolve query to complete. */
  324. for(;;) {
  325. struct timeval *tvp, tv, store;
  326. long timediff;
  327. int itimeout;
  328. int timeout_ms;
  329. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  330. store.tv_sec = itimeout/1000;
  331. store.tv_usec = (itimeout%1000)*1000;
  332. tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
  333. /* use the timeout period ares returned to us above if less than one
  334. second is left, otherwise just use 1000ms to make sure the progress
  335. callback gets called frequent enough */
  336. if(!tvp->tv_sec)
  337. timeout_ms = (int)(tvp->tv_usec/1000);
  338. else
  339. timeout_ms = 1000;
  340. waitperform(conn, timeout_ms);
  341. Curl_resolver_is_resolved(conn, &temp_entry);
  342. if(conn->async.done)
  343. break;
  344. if(Curl_pgrsUpdate(conn)) {
  345. result = CURLE_ABORTED_BY_CALLBACK;
  346. timeout = -1; /* trigger the cancel below */
  347. }
  348. else {
  349. struct timeval now2 = Curl_tvnow();
  350. timediff = Curl_tvdiff(now2, now); /* spent time */
  351. timeout -= timediff?timediff:1; /* always deduct at least 1 */
  352. now = now2; /* for next loop */
  353. }
  354. if(timeout < 0) {
  355. /* our timeout, so we cancel the ares operation */
  356. ares_cancel((ares_channel)data->state.resolver);
  357. break;
  358. }
  359. }
  360. /* Operation complete, if the lookup was successful we now have the entry
  361. in the cache. */
  362. if(entry)
  363. *entry = conn->async.dns;
  364. if(result)
  365. /* close the connection, since we can't return failure here without
  366. cleaning up this connection properly.
  367. TODO: remove this action from here, it is not a name resolver decision.
  368. */
  369. connclose(conn, "c-ares resolve failed");
  370. return result;
  371. }
  372. /* Connects results to the list */
  373. static void compound_results(struct ResolverResults *res,
  374. Curl_addrinfo *ai)
  375. {
  376. Curl_addrinfo *ai_tail;
  377. if(!ai)
  378. return;
  379. ai_tail = ai;
  380. while(ai_tail->ai_next)
  381. ai_tail = ai_tail->ai_next;
  382. /* Add the new results to the list of old results. */
  383. ai_tail->ai_next = res->temp_ai;
  384. res->temp_ai = ai;
  385. }
  386. /*
  387. * ares_query_completed_cb() is the callback that ares will call when
  388. * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
  389. * when using ares, is completed either successfully or with failure.
  390. */
  391. static void query_completed_cb(void *arg, /* (struct connectdata *) */
  392. int status,
  393. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  394. int timeouts,
  395. #endif
  396. struct hostent *hostent)
  397. {
  398. struct connectdata *conn = (struct connectdata *)arg;
  399. struct ResolverResults *res;
  400. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  401. (void)timeouts; /* ignored */
  402. #endif
  403. if(ARES_EDESTRUCTION == status)
  404. /* when this ares handle is getting destroyed, the 'arg' pointer may not
  405. be valid so only defer it when we know the 'status' says its fine! */
  406. return;
  407. res = (struct ResolverResults *)conn->async.os_specific;
  408. res->num_pending--;
  409. if(CURL_ASYNC_SUCCESS == status) {
  410. Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
  411. if(ai) {
  412. compound_results(res, ai);
  413. }
  414. }
  415. /* A successful result overwrites any previous error */
  416. if(res->last_status != ARES_SUCCESS)
  417. res->last_status = status;
  418. }
  419. /*
  420. * Curl_resolver_getaddrinfo() - when using ares
  421. *
  422. * Returns name information about the given hostname and port number. If
  423. * successful, the 'hostent' is returned and the forth argument will point to
  424. * memory we need to free after use. That memory *MUST* be freed with
  425. * Curl_freeaddrinfo(), nothing else.
  426. */
  427. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  428. const char *hostname,
  429. int port,
  430. int *waitp)
  431. {
  432. char *bufp;
  433. struct Curl_easy *data = conn->data;
  434. struct in_addr in;
  435. int family = PF_INET;
  436. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  437. struct in6_addr in6;
  438. #endif /* CURLRES_IPV6 */
  439. *waitp = 0; /* default to synchronous response */
  440. /* First check if this is an IPv4 address string */
  441. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  442. /* This is a dotted IP address 123.123.123.123-style */
  443. return Curl_ip2addr(AF_INET, &in, hostname, port);
  444. }
  445. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  446. /* Otherwise, check if this is an IPv6 address string */
  447. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  448. /* This must be an IPv6 address literal. */
  449. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  450. switch(conn->ip_version) {
  451. default:
  452. #if ARES_VERSION >= 0x010601
  453. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  454. c-ares versions this just falls through and defaults
  455. to PF_INET */
  456. break;
  457. #endif
  458. case CURL_IPRESOLVE_V4:
  459. family = PF_INET;
  460. break;
  461. case CURL_IPRESOLVE_V6:
  462. family = PF_INET6;
  463. break;
  464. }
  465. #endif /* CURLRES_IPV6 */
  466. bufp = strdup(hostname);
  467. if(bufp) {
  468. struct ResolverResults *res = NULL;
  469. free(conn->async.hostname);
  470. conn->async.hostname = bufp;
  471. conn->async.port = port;
  472. conn->async.done = FALSE; /* not done */
  473. conn->async.status = 0; /* clear */
  474. conn->async.dns = NULL; /* clear */
  475. res = calloc(sizeof(struct ResolverResults), 1);
  476. if(!res) {
  477. free(conn->async.hostname);
  478. conn->async.hostname = NULL;
  479. return NULL;
  480. }
  481. conn->async.os_specific = res;
  482. /* initial status - failed */
  483. res->last_status = ARES_ENOTFOUND;
  484. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  485. if(family == PF_UNSPEC) {
  486. if(Curl_ipv6works()) {
  487. res->num_pending = 2;
  488. /* areschannel is already setup in the Curl_open() function */
  489. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  490. PF_INET, query_completed_cb, conn);
  491. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  492. PF_INET6, query_completed_cb, conn);
  493. }
  494. else {
  495. res->num_pending = 1;
  496. /* areschannel is already setup in the Curl_open() function */
  497. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  498. PF_INET, query_completed_cb, conn);
  499. }
  500. }
  501. else
  502. #endif /* CURLRES_IPV6 */
  503. {
  504. res->num_pending = 1;
  505. /* areschannel is already setup in the Curl_open() function */
  506. ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
  507. query_completed_cb, conn);
  508. }
  509. *waitp = 1; /* expect asynchronous response */
  510. }
  511. return NULL; /* no struct yet */
  512. }
  513. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  514. char *servers)
  515. {
  516. CURLcode result = CURLE_NOT_BUILT_IN;
  517. int ares_result;
  518. /* If server is NULL or empty, this would purge all DNS servers
  519. * from ares library, which will cause any and all queries to fail.
  520. * So, just return OK if none are configured and don't actually make
  521. * any changes to c-ares. This lets c-ares use it's defaults, which
  522. * it gets from the OS (for instance from /etc/resolv.conf on Linux).
  523. */
  524. if(!(servers && servers[0]))
  525. return CURLE_OK;
  526. #if (ARES_VERSION >= 0x010704)
  527. ares_result = ares_set_servers_csv(data->state.resolver, servers);
  528. switch(ares_result) {
  529. case ARES_SUCCESS:
  530. result = CURLE_OK;
  531. break;
  532. case ARES_ENOMEM:
  533. result = CURLE_OUT_OF_MEMORY;
  534. break;
  535. case ARES_ENOTINITIALIZED:
  536. case ARES_ENODATA:
  537. case ARES_EBADSTR:
  538. default:
  539. result = CURLE_BAD_FUNCTION_ARGUMENT;
  540. break;
  541. }
  542. #else /* too old c-ares version! */
  543. (void)data;
  544. (void)(ares_result);
  545. #endif
  546. return result;
  547. }
  548. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  549. const char *interf)
  550. {
  551. #if (ARES_VERSION >= 0x010704)
  552. if(!interf)
  553. interf = "";
  554. ares_set_local_dev((ares_channel)data->state.resolver, interf);
  555. return CURLE_OK;
  556. #else /* c-ares version too old! */
  557. (void)data;
  558. (void)interf;
  559. return CURLE_NOT_BUILT_IN;
  560. #endif
  561. }
  562. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  563. const char *local_ip4)
  564. {
  565. #if (ARES_VERSION >= 0x010704)
  566. struct in_addr a4;
  567. if((!local_ip4) || (local_ip4[0] == 0)) {
  568. a4.s_addr = 0; /* disabled: do not bind to a specific address */
  569. }
  570. else {
  571. if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
  572. return CURLE_BAD_FUNCTION_ARGUMENT;
  573. }
  574. }
  575. ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
  576. return CURLE_OK;
  577. #else /* c-ares version too old! */
  578. (void)data;
  579. (void)local_ip4;
  580. return CURLE_NOT_BUILT_IN;
  581. #endif
  582. }
  583. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  584. const char *local_ip6)
  585. {
  586. #if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
  587. unsigned char a6[INET6_ADDRSTRLEN];
  588. if((!local_ip6) || (local_ip6[0] == 0)) {
  589. /* disabled: do not bind to a specific address */
  590. memset(a6, 0, sizeof(a6));
  591. }
  592. else {
  593. if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
  594. return CURLE_BAD_FUNCTION_ARGUMENT;
  595. }
  596. }
  597. ares_set_local_ip6((ares_channel)data->state.resolver, a6);
  598. return CURLE_OK;
  599. #else /* c-ares version too old! */
  600. (void)data;
  601. (void)local_ip6;
  602. return CURLE_NOT_BUILT_IN;
  603. #endif
  604. }
  605. #endif /* CURLRES_ARES */