aboutsummaryrefslogtreecommitdiff
path: root/src/common/dns_utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/dns_utils.cpp')
-rw-r--r--src/common/dns_utils.cpp66
1 files changed, 54 insertions, 12 deletions
diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp
index 9c306505e..f549218cb 100644
--- a/src/common/dns_utils.cpp
+++ b/src/common/dns_utils.cpp
@@ -40,6 +40,8 @@ namespace bf = boost::filesystem;
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "net.dns"
+#define DEFAULT_DNS_PUBLIC_ADDR "8.8.4.4"
+
static boost::mutex instance_lock;
namespace
@@ -197,16 +199,19 @@ public:
DNSResolver::DNSResolver() : m_data(new DNSResolverData())
{
int use_dns_public = 0;
- const char* dns_public_addr = "8.8.4.4";
+ std::string dns_public_addr = DEFAULT_DNS_PUBLIC_ADDR;
if (auto res = getenv("DNS_PUBLIC"))
{
- std::string dns_public(res);
- // TODO: could allow parsing of IP and protocol: e.g. DNS_PUBLIC=tcp:8.8.8.8
- if (dns_public == "tcp")
+ dns_public_addr = tools::dns_utils::parse_dns_public(res);
+ if (!dns_public_addr.empty())
{
- LOG_PRINT_L0("Using public DNS server: " << dns_public_addr << " (TCP)");
+ MGINFO("Using public DNS server: " << dns_public_addr << " (TCP)");
use_dns_public = 1;
}
+ else
+ {
+ MERROR("Failed to parse DNS_PUBLIC");
+ }
}
// init libunbound context
@@ -214,7 +219,7 @@ DNSResolver::DNSResolver() : m_data(new DNSResolverData())
if (use_dns_public)
{
- ub_ctx_set_fwd(m_data->m_ub_context, string_copy(dns_public_addr));
+ ub_ctx_set_fwd(m_data->m_ub_context, dns_public_addr.c_str());
ub_ctx_set_option(m_data->m_ub_context, string_copy("do-udp:"), string_copy("no"));
ub_ctx_set_option(m_data->m_ub_context, string_copy("do-tcp:"), string_copy("yes"));
}
@@ -443,19 +448,28 @@ bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std
std::uniform_int_distribution<int> dis(0, dns_urls.size() - 1);
size_t first_index = dis(gen);
- bool avail, valid;
+ // send all requests in parallel
+ std::vector<boost::thread> threads(dns_urls.size());
+ std::deque<bool> avail(dns_urls.size(), false), valid(dns_urls.size(), false);
+ for (size_t n = 0; n < dns_urls.size(); ++n)
+ {
+ threads[n] = boost::thread([n, dns_urls, &records, &avail, &valid](){
+ records[n] = tools::DNSResolver::instance().get_txt_record(dns_urls[n], avail[n], valid[n]);
+ });
+ }
+ for (size_t n = 0; n < dns_urls.size(); ++n)
+ threads[n].join();
+
size_t cur_index = first_index;
do
{
- std::string url = dns_urls[cur_index];
-
- records[cur_index] = tools::DNSResolver::instance().get_txt_record(url, avail, valid);
- if (!avail)
+ const std::string &url = dns_urls[cur_index];
+ if (!avail[cur_index])
{
records[cur_index].clear();
LOG_PRINT_L2("DNSSEC not available for checkpoint update at URL: " << url << ", skipping.");
}
- if (!valid)
+ if (!valid[cur_index])
{
records[cur_index].clear();
LOG_PRINT_L2("DNSSEC validation failed for checkpoint update at URL: " << url << ", skipping.");
@@ -510,6 +524,34 @@ bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std
return true;
}
+std::string parse_dns_public(const char *s)
+{
+ unsigned ip0, ip1, ip2, ip3;
+ char c;
+ std::string dns_public_addr;
+ if (!strcmp(s, "tcp"))
+ {
+ dns_public_addr = DEFAULT_DNS_PUBLIC_ADDR;
+ LOG_PRINT_L0("Using default public DNS server: " << dns_public_addr << " (TCP)");
+ }
+ else if (sscanf(s, "tcp://%u.%u.%u.%u%c", &ip0, &ip1, &ip2, &ip3, &c) == 4)
+ {
+ if (ip0 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255)
+ {
+ MERROR("Invalid IP: " << s << ", using default");
+ }
+ else
+ {
+ dns_public_addr = std::string(s + strlen("tcp://"));
+ }
+ }
+ else
+ {
+ MERROR("Invalid DNS_PUBLIC contents, ignored");
+ }
+ return dns_public_addr;
+}
+
} // namespace tools::dns_utils
} // namespace tools