asyn-ares.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. #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(conn->data, milli, EXPIRE_ASYNC_NAME);
  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 curltime now = Curl_tvnow();
  319. struct Curl_dns_entry *temp_entry;
  320. if(entry)
  321. *entry = NULL; /* clear on entry */
  322. timeout = Curl_timeleft(data, &now, TRUE);
  323. if(timeout < 0) {
  324. /* already expired! */
  325. connclose(conn, "Timed out before name resolve started");
  326. return CURLE_OPERATION_TIMEDOUT;
  327. }
  328. if(!timeout)
  329. timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
  330. /* Wait for the name resolve query to complete. */
  331. while(!result) {
  332. struct timeval *tvp, tv, store;
  333. int itimeout;
  334. int timeout_ms;
  335. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  336. store.tv_sec = itimeout/1000;
  337. store.tv_usec = (itimeout%1000)*1000;
  338. tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
  339. /* use the timeout period ares returned to us above if less than one
  340. second is left, otherwise just use 1000ms to make sure the progress
  341. callback gets called frequent enough */
  342. if(!tvp->tv_sec)
  343. timeout_ms = (int)(tvp->tv_usec/1000);
  344. else
  345. timeout_ms = 1000;
  346. waitperform(conn, timeout_ms);
  347. result = Curl_resolver_is_resolved(conn, &temp_entry);
  348. if(result || conn->async.done)
  349. break;
  350. if(Curl_pgrsUpdate(conn))
  351. result = CURLE_ABORTED_BY_CALLBACK;
  352. else {
  353. struct curltime now2 = Curl_tvnow();
  354. time_t timediff = Curl_tvdiff(now2, now); /* spent time */
  355. if(timediff <= 0)
  356. timeout -= 1; /* always deduct at least 1 */
  357. else if(timediff > timeout)
  358. timeout = -1;
  359. else
  360. timeout -= (long)timediff;
  361. now = now2; /* for next loop */
  362. }
  363. if(timeout < 0)
  364. result = CURLE_OPERATION_TIMEDOUT;
  365. }
  366. if(result)
  367. /* failure, so we cancel the ares operation */
  368. ares_cancel((ares_channel)data->state.resolver);
  369. /* Operation complete, if the lookup was successful we now have the entry
  370. in the cache. */
  371. if(entry)
  372. *entry = conn->async.dns;
  373. if(result)
  374. /* close the connection, since we can't return failure here without
  375. cleaning up this connection properly.
  376. TODO: remove this action from here, it is not a name resolver decision.
  377. */
  378. connclose(conn, "c-ares resolve failed");
  379. return result;
  380. }
  381. /* Connects results to the list */
  382. static void compound_results(struct ResolverResults *res,
  383. Curl_addrinfo *ai)
  384. {
  385. Curl_addrinfo *ai_tail;
  386. if(!ai)
  387. return;
  388. ai_tail = ai;
  389. while(ai_tail->ai_next)
  390. ai_tail = ai_tail->ai_next;
  391. /* Add the new results to the list of old results. */
  392. ai_tail->ai_next = res->temp_ai;
  393. res->temp_ai = ai;
  394. }
  395. /*
  396. * ares_query_completed_cb() is the callback that ares will call when
  397. * the host query initiated by ares_gethostbyname() from Curl_getaddrinfo(),
  398. * when using ares, is completed either successfully or with failure.
  399. */
  400. static void query_completed_cb(void *arg, /* (struct connectdata *) */
  401. int status,
  402. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  403. int timeouts,
  404. #endif
  405. struct hostent *hostent)
  406. {
  407. struct connectdata *conn = (struct connectdata *)arg;
  408. struct ResolverResults *res;
  409. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  410. (void)timeouts; /* ignored */
  411. #endif
  412. if(ARES_EDESTRUCTION == status)
  413. /* when this ares handle is getting destroyed, the 'arg' pointer may not
  414. be valid so only defer it when we know the 'status' says its fine! */
  415. return;
  416. res = (struct ResolverResults *)conn->async.os_specific;
  417. res->num_pending--;
  418. if(CURL_ASYNC_SUCCESS == status) {
  419. Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
  420. if(ai) {
  421. compound_results(res, ai);
  422. }
  423. }
  424. /* A successful result overwrites any previous error */
  425. if(res->last_status != ARES_SUCCESS)
  426. res->last_status = status;
  427. }
  428. /*
  429. * Curl_resolver_getaddrinfo() - when using ares
  430. *
  431. * Returns name information about the given hostname and port number. If
  432. * successful, the 'hostent' is returned and the forth argument will point to
  433. * memory we need to free after use. That memory *MUST* be freed with
  434. * Curl_freeaddrinfo(), nothing else.
  435. */
  436. Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
  437. const char *hostname,
  438. int port,
  439. int *waitp)
  440. {
  441. char *bufp;
  442. struct Curl_easy *data = conn->data;
  443. struct in_addr in;
  444. int family = PF_INET;
  445. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  446. struct in6_addr in6;
  447. #endif /* CURLRES_IPV6 */
  448. *waitp = 0; /* default to synchronous response */
  449. /* First check if this is an IPv4 address string */
  450. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  451. /* This is a dotted IP address 123.123.123.123-style */
  452. return Curl_ip2addr(AF_INET, &in, hostname, port);
  453. }
  454. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  455. /* Otherwise, check if this is an IPv6 address string */
  456. if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
  457. /* This must be an IPv6 address literal. */
  458. return Curl_ip2addr(AF_INET6, &in6, hostname, port);
  459. switch(conn->ip_version) {
  460. default:
  461. #if ARES_VERSION >= 0x010601
  462. family = PF_UNSPEC; /* supported by c-ares since 1.6.1, so for older
  463. c-ares versions this just falls through and defaults
  464. to PF_INET */
  465. break;
  466. #endif
  467. case CURL_IPRESOLVE_V4:
  468. family = PF_INET;
  469. break;
  470. case CURL_IPRESOLVE_V6:
  471. family = PF_INET6;
  472. break;
  473. }
  474. #endif /* CURLRES_IPV6 */
  475. bufp = strdup(hostname);
  476. if(bufp) {
  477. struct ResolverResults *res = NULL;
  478. free(conn->async.hostname);
  479. conn->async.hostname = bufp;
  480. conn->async.port = port;
  481. conn->async.done = FALSE; /* not done */
  482. conn->async.status = 0; /* clear */
  483. conn->async.dns = NULL; /* clear */
  484. res = calloc(sizeof(struct ResolverResults), 1);
  485. if(!res) {
  486. free(conn->async.hostname);
  487. conn->async.hostname = NULL;
  488. return NULL;
  489. }
  490. conn->async.os_specific = res;
  491. /* initial status - failed */
  492. res->last_status = ARES_ENOTFOUND;
  493. #ifdef ENABLE_IPV6 /* CURLRES_IPV6 */
  494. if(family == PF_UNSPEC) {
  495. if(Curl_ipv6works()) {
  496. res->num_pending = 2;
  497. /* areschannel is already setup in the Curl_open() function */
  498. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  499. PF_INET, query_completed_cb, conn);
  500. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  501. PF_INET6, query_completed_cb, conn);
  502. }
  503. else {
  504. res->num_pending = 1;
  505. /* areschannel is already setup in the Curl_open() function */
  506. ares_gethostbyname((ares_channel)data->state.resolver, hostname,
  507. PF_INET, query_completed_cb, conn);
  508. }
  509. }
  510. else
  511. #endif /* CURLRES_IPV6 */
  512. {
  513. res->num_pending = 1;
  514. /* areschannel is already setup in the Curl_open() function */
  515. ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
  516. query_completed_cb, conn);
  517. }
  518. *waitp = 1; /* expect asynchronous response */
  519. }
  520. return NULL; /* no struct yet */
  521. }
  522. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  523. char *servers)
  524. {
  525. CURLcode result = CURLE_NOT_BUILT_IN;
  526. int ares_result;
  527. /* If server is NULL or empty, this would purge all DNS servers
  528. * from ares library, which will cause any and all queries to fail.
  529. * So, just return OK if none are configured and don't actually make
  530. * any changes to c-ares. This lets c-ares use it's defaults, which
  531. * it gets from the OS (for instance from /etc/resolv.conf on Linux).
  532. */
  533. if(!(servers && servers[0]))
  534. return CURLE_OK;
  535. #if (ARES_VERSION >= 0x010704)
  536. ares_result = ares_set_servers_csv(data->state.resolver, servers);
  537. switch(ares_result) {
  538. case ARES_SUCCESS:
  539. result = CURLE_OK;
  540. break;
  541. case ARES_ENOMEM:
  542. result = CURLE_OUT_OF_MEMORY;
  543. break;
  544. case ARES_ENOTINITIALIZED:
  545. case ARES_ENODATA:
  546. case ARES_EBADSTR:
  547. default:
  548. result = CURLE_BAD_FUNCTION_ARGUMENT;
  549. break;
  550. }
  551. #else /* too old c-ares version! */
  552. (void)data;
  553. (void)(ares_result);
  554. #endif
  555. return result;
  556. }
  557. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  558. const char *interf)
  559. {
  560. #if (ARES_VERSION >= 0x010704)
  561. if(!interf)
  562. interf = "";
  563. ares_set_local_dev((ares_channel)data->state.resolver, interf);
  564. return CURLE_OK;
  565. #else /* c-ares version too old! */
  566. (void)data;
  567. (void)interf;
  568. return CURLE_NOT_BUILT_IN;
  569. #endif
  570. }
  571. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  572. const char *local_ip4)
  573. {
  574. #if (ARES_VERSION >= 0x010704)
  575. struct in_addr a4;
  576. if((!local_ip4) || (local_ip4[0] == 0)) {
  577. a4.s_addr = 0; /* disabled: do not bind to a specific address */
  578. }
  579. else {
  580. if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
  581. return CURLE_BAD_FUNCTION_ARGUMENT;
  582. }
  583. }
  584. ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
  585. return CURLE_OK;
  586. #else /* c-ares version too old! */
  587. (void)data;
  588. (void)local_ip4;
  589. return CURLE_NOT_BUILT_IN;
  590. #endif
  591. }
  592. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  593. const char *local_ip6)
  594. {
  595. #if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
  596. unsigned char a6[INET6_ADDRSTRLEN];
  597. if((!local_ip6) || (local_ip6[0] == 0)) {
  598. /* disabled: do not bind to a specific address */
  599. memset(a6, 0, sizeof(a6));
  600. }
  601. else {
  602. if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
  603. return CURLE_BAD_FUNCTION_ARGUMENT;
  604. }
  605. }
  606. ares_set_local_ip6((ares_channel)data->state.resolver, a6);
  607. return CURLE_OK;
  608. #else /* c-ares version too old! */
  609. (void)data;
  610. (void)local_ip6;
  611. return CURLE_NOT_BUILT_IN;
  612. #endif
  613. }
  614. #endif /* CURLRES_ARES */