Skip to content

Commit

Permalink
CXXCBC-336: Do not fallback to 8.8.8.8, if we cannot obtain system DN…
Browse files Browse the repository at this point in the history
…S server (#533)

Avoid assuming Google DNS and rather report DNS-SRV lookup failure. The
details still logged and user still can specify their DNS server in
options.
  • Loading branch information
avsej committed Mar 5, 2024
1 parent 4043da8 commit 978d233
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@
/cmake-build-report.tar.gz
/.idea/vcs.xml
/.idea/workspace.xml
/.idea/editor.xml
/.idea/sonarlint
/.cache
/.vs
Expand Down
4 changes: 4 additions & 0 deletions core/io/dns_client.cxx
Expand Up @@ -285,6 +285,10 @@ dns_client::query_srv(const std::string& name,
const dns_config& config,
utils::movable_function<void(dns_srv_response&&)>&& handler)
{
if (config.nameserver().empty()) {
return handler({ {} });
}

std::error_code ec;
auto address = asio::ip::make_address(config.nameserver(), ec);
if (ec) {
Expand Down
19 changes: 15 additions & 4 deletions core/io/dns_config.cxx
Expand Up @@ -53,6 +53,7 @@ load_resolv_conf()
fixed_info = (FIXED_INFO*)malloc(sizeof(FIXED_INFO));
if (fixed_info == NULL) {
CB_LOG_WARNING("Error allocating memory needed to call GetNetworkParams");
return {};
}
buf = sizeof(FIXED_INFO);

Expand All @@ -63,6 +64,7 @@ load_resolv_conf()
fixed_info = (FIXED_INFO*)malloc(buf);
if (fixed_info == NULL) {
CB_LOG_WARNING("Error allocating memory needed to call GetNetworkParams");
return {};
}
}

Expand All @@ -86,10 +88,12 @@ load_resolv_conf()
}
} else {
CB_LOG_WARNING("GetNetworkParams failed with error: {}", ret);
return {};
}

if (fixed_info)
if (fixed_info) {
free(fixed_info);
}

if (dns_servers.size() > 0) {
CB_LOG_DEBUG(
Expand Down Expand Up @@ -153,10 +157,17 @@ dns_config::system_config()
std::error_code ec;
asio::ip::make_address(nameserver, ec);
if (ec) {
CB_LOG_DEBUG("Unable to parse \"{}\" as a network address, fall back to \"{}\"", nameserver, default_nameserver);
nameserver = default_nameserver;
std::string extra_info{};
#ifndef _WIN32
extra_info = fmt::format(" in \"{}\"", default_resolv_conf_path);
#endif
CB_LOG_WARNING("System DNS detection failed: unable to parse \"{}\" as a network address{}. DNS-SRV will not work "
"unless nameserver is specified explicitly in the options.",
nameserver,
extra_info);
} else {
instance.nameserver_ = nameserver;
}
instance.nameserver_ = nameserver;
});

return instance;
Expand Down
2 changes: 1 addition & 1 deletion core/io/dns_config.hxx
Expand Up @@ -40,7 +40,7 @@ class dns_config
[[nodiscard]] std::chrono::milliseconds timeout() const;

private:
std::string nameserver_{ default_nameserver };
std::string nameserver_{};
std::uint16_t port_{ default_port };
std::chrono::milliseconds timeout_{ timeout_defaults::dns_srv_timeout };
};
Expand Down

0 comments on commit 978d233

Please sign in to comment.