asyn-ares.c 19 KB

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