Skip to content

Commit

Permalink
follow dns_server_params.address_family when parse nameserver (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
kedixa authored Dec 21, 2023
1 parent d371bd5 commit ba98ed0
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/manager/WFGlobal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "WFTaskError.h"
#include "WFDnsClient.h"
#include "WFGlobal.h"
#include "URIParser.h"

class __WFGlobal
{
Expand Down Expand Up @@ -478,7 +479,38 @@ inline ExecQueue *__ExecManager::get_exec_queue(const std::string& queue_name)
return queue;
}

static std::string __dns_server_url(const std::string& url,
const struct addrinfo *hints)
{
std::string host;
ParsedURI uri;
struct addrinfo *res;
struct in6_addr buf;

if (strncasecmp(url.c_str(), "dns://", 6) == 0 ||
strncasecmp(url.c_str(), "dnss://", 7) == 0)
{
host = url;
}
else if (inet_pton(AF_INET6, url.c_str(), &buf) > 0)
host = "dns://[" + url + "]";
else
host = "dns://" + url;

if (URIParser::parse(host, uri) == 0 && uri.host && uri.host[0])
{
if (getaddrinfo(uri.host, "53", hints, &res) == 0)
{
freeaddrinfo(res);
return host;
}
}

return "";
}

static void __split_merge_str(const char *p, bool is_nameserver,
const struct addrinfo *hints,
std::string& result)
{
const char *start;
Expand All @@ -498,18 +530,17 @@ static void __split_merge_str(const char *p, bool is_nameserver,
if (start == p)
break;

if (!result.empty())
result.push_back(',');

std::string str(start, p);
if (is_nameserver)
str = __dns_server_url(str, hints);

if (!str.empty())
{
struct in6_addr buf;
if (inet_pton(AF_INET6, str.c_str(), &buf) > 0)
str = "[" + str + "]";
}
if (!result.empty())
result.push_back(',');

result.append(str);
result.append(str);
}
}
}

Expand Down Expand Up @@ -565,12 +596,19 @@ static int __parse_resolv_conf(const char *path,
if (!fp)
return -1;

const struct WFGlobalSettings *settings = WFGlobal::get_global_settings();
struct addrinfo hints = {
.ai_flags = AI_ADDRCONFIG | AI_NUMERICHOST | AI_NUMERICSERV,
.ai_family = settings->dns_server_params.address_family,
.ai_socktype = SOCK_STREAM,
};

while ((ret = getline(&line, &bufsize, fp)) > 0)
{
if (strncmp(line, "nameserver", 10) == 0)
__split_merge_str(line + 10, true, url);
__split_merge_str(line + 10, true, &hints, url);
else if (strncmp(line, "search", 6) == 0)
__split_merge_str(line + 6, false, search_list);
__split_merge_str(line + 6, false, &hints, search_list);
else if (strncmp(line, "options", 7) == 0)
__set_options(line + 7, ndots, attempts, rotate);
}
Expand Down

0 comments on commit ba98ed0

Please sign in to comment.