Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
ares: Store dns parameters for duphandle
Browse files Browse the repository at this point in the history
With ares the dns parameters lives in ares_channel. Store them in the
curl handle so we can set them again if we do easy_duphandle.

Closes curl#4893

Signed-off-by: Ernst Sjöstrand <ernst.sjostrand@verisure.com>
  • Loading branch information
ernstp committed Mar 2, 2020
1 parent 967bf46 commit b8459bc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/asyn-ares.c
Expand Up @@ -707,6 +707,7 @@ CURLcode Curl_set_dns_servers(struct Curl_easy *data,
CURLcode result = CURLE_NOT_BUILT_IN;
int ares_result;

Curl_safefree(data->dns_servers);
/* If server is NULL or empty, this would purge all DNS servers
* from ares library, which will cause any and all queries to fail.
* So, just return OK if none are configured and don't actually make
Expand All @@ -717,6 +718,11 @@ CURLcode Curl_set_dns_servers(struct Curl_easy *data,
return CURLE_OK;

#if (ARES_VERSION >= 0x010704)
/* Make a copy of servers for any later duphandle */
data->dns_servers = strdup(servers);
if(!data->dns_servers)
return CURLE_OUT_OF_MEMORY;

#if (ARES_VERSION >= 0x010b00)
ares_result = ares_set_servers_ports_csv(data->state.resolver, servers);
#else
Expand Down Expand Up @@ -747,6 +753,14 @@ CURLcode Curl_set_dns_interface(struct Curl_easy *data,
const char *interf)
{
#if (ARES_VERSION >= 0x010704)
Curl_safefree(data->dns_interface);
if(interf) {
/* Make a copy of interface for any later duphandle */
data->dns_interface = strdup(interf);
if(!data->dns_interface)
return CURLE_OUT_OF_MEMORY;
}

if(!interf)
interf = "";

Expand All @@ -766,6 +780,14 @@ CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
#if (ARES_VERSION >= 0x010704)
struct in_addr a4;

Curl_safefree(data->dns_local_ip4);
if(local_ip4) {
/* Make a copy of interface for any later duphandle */
data->dns_local_ip4 = strdup(local_ip4);
if(!data->dns_local_ip4)
return CURLE_OUT_OF_MEMORY;
}

if((!local_ip4) || (local_ip4[0] == 0)) {
a4.s_addr = 0; /* disabled: do not bind to a specific address */
}
Expand All @@ -791,6 +813,14 @@ CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
#if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
unsigned char a6[INET6_ADDRSTRLEN];

Curl_safefree(data->dns_local_ip6);
if(local_ip6) {
/* Make a copy of interface for any later duphandle */
data->dns_local_ip6 = strdup(local_ip6);
if(!data->dns_local_ip6)
return CURLE_OUT_OF_MEMORY;
}

if((!local_ip6) || (local_ip6[0] == 0)) {
/* disabled: do not bind to a specific address */
memset(a6, 0, sizeof(a6));
Expand Down
11 changes: 11 additions & 0 deletions lib/easy.c
Expand Up @@ -887,6 +887,17 @@ struct Curl_easy *curl_easy_duphandle(struct Curl_easy *data)
data->state.resolver))
goto fail;

#ifdef USE_ARES
if(Curl_set_dns_servers(outcurl, data->dns_servers))
goto fail;
if(Curl_set_dns_interface(outcurl, data->dns_interface))
goto fail;
if(Curl_set_dns_local_ip4(outcurl, data->dns_local_ip4))
goto fail;
if(Curl_set_dns_local_ip6(outcurl, data->dns_local_ip6))
goto fail;
#endif /* USE_ARES */

Curl_convert_setup(outcurl);

Curl_initinfo(outcurl);
Expand Down
7 changes: 7 additions & 0 deletions lib/url.c
Expand Up @@ -408,6 +408,13 @@ CURLcode Curl_close(struct Curl_easy **datap)
curl_slist_free_all(data->req.doh.headers);
#endif

#ifdef USE_ARES
Curl_safefree(data->dns_servers);
Curl_safefree(data->dns_interface);
Curl_safefree(data->dns_local_ip4);
Curl_safefree(data->dns_local_ip6);
#endif /* CURL_ARES */

/* destruct wildcard structures if it is needed */
Curl_wildcard_dtor(&data->wildcard);
Curl_freeset(data);
Expand Down
6 changes: 6 additions & 0 deletions lib/urldata.h
Expand Up @@ -1916,6 +1916,12 @@ struct Curl_easy {
iconv_t utf8_cd; /* for translating to UTF8 */
#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
unsigned int magic; /* set to a CURLEASY_MAGIC_NUMBER */
#ifdef USE_ARES
char *dns_servers;
char *dns_interface;
char *dns_local_ip4;
char *dns_local_ip6;
#endif /* USE_ARES */
};

#define LIBCURL_NAME "libcurl"
Expand Down

0 comments on commit b8459bc

Please sign in to comment.