aboutsummaryrefslogtreecommitdiff
path: root/src/common/dns_utils.cpp
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2017-02-21 11:16:43 +0200
committerRiccardo Spagni <ric@spagni.net>2017-02-21 11:16:43 +0200
commitd11dbe7dc24c8ab30afc2ce6b004885979ee8be6 (patch)
tree878f7b040b6fa675f6f117b0b7ca285570c4ceb1 /src/common/dns_utils.cpp
parentMerge pull request #1737 (diff)
parentcore: updates can now be downloaded (and SHA256 hash checked) (diff)
downloadmonero-d11dbe7dc24c8ab30afc2ce6b004885979ee8be6.tar.xz
Merge pull request #1744
a5a0a3c8 core: updates can now be downloaded (and SHA256 hash checked) (moneromooo-monero) 216f062e util: add a SHA256 function (moneromooo-monero) 4bf78492 mlog: only silence errors for net by default, not net.* (moneromooo-monero) d282cfcc core: test key images against validity domain (moneromooo-monero) efb72e74 http_client: add a couple consts (moneromooo-monero) f640512c Optionally query moneropulse DNS records to check for updates (moneromooo-monero) e3cae4ae core: display any fork warning at startup too (moneromooo-monero) 969ad710 dns_utils: fix first checked DNS entry being ignored (moneromooo-monero) 08c3f380 util: add a vercmp function to compare version numbers (moneromooo-monero) e8a7525c dns_utils: factor TXT record loading code from checkpoint code (moneromooo-monero)
Diffstat (limited to 'src/common/dns_utils.cpp')
-rw-r--r--src/common/dns_utils.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp
index 5ff39574c..f7655e3c7 100644
--- a/src/common/dns_utils.cpp
+++ b/src/common/dns_utils.cpp
@@ -37,6 +37,7 @@
#include <stdlib.h>
#include "include_base_utils.h"
+#include <random>
#include <boost/filesystem/fstream.hpp>
using namespace epee;
namespace bf = boost::filesystem;
@@ -451,6 +452,107 @@ std::string get_account_address_as_str_from_url(const std::string& url, bool& dn
return addresses[0];
}
+namespace
+{
+ bool dns_records_match(const std::vector<std::string>& a, const std::vector<std::string>& b)
+ {
+ if (a.size() != b.size()) return false;
+
+ for (const auto& record_in_a : a)
+ {
+ bool ok = false;
+ for (const auto& record_in_b : b)
+ {
+ if (record_in_a == record_in_b)
+ {
+ ok = true;
+ break;
+ }
+ }
+ if (!ok) return false;
+ }
+
+ return true;
+ }
+}
+
+bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std::vector<std::string> &dns_urls)
+{
+ std::vector<std::vector<std::string> > records;
+ records.resize(dns_urls.size());
+
+ std::random_device rd;
+ std::mt19937 gen(rd());
+ std::uniform_int_distribution<int> dis(0, dns_urls.size() - 1);
+ size_t first_index = dis(gen);
+
+ bool avail, valid;
+ 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)
+ {
+ records[cur_index].clear();
+ LOG_PRINT_L2("DNSSEC not available for checkpoint update at URL: " << url << ", skipping.");
+ }
+ if (!valid)
+ {
+ records[cur_index].clear();
+ LOG_PRINT_L2("DNSSEC validation failed for checkpoint update at URL: " << url << ", skipping.");
+ }
+
+ cur_index++;
+ if (cur_index == dns_urls.size())
+ {
+ cur_index = 0;
+ }
+ } while (cur_index != first_index);
+
+ size_t num_valid_records = 0;
+
+ for( const auto& record_set : records)
+ {
+ if (record_set.size() != 0)
+ {
+ num_valid_records++;
+ }
+ }
+
+ if (num_valid_records < 2)
+ {
+ LOG_PRINT_L0("WARNING: no two valid MoneroPulse DNS checkpoint records were received");
+ return false;
+ }
+
+ int good_records_index = -1;
+ for (size_t i = 0; i < records.size() - 1; ++i)
+ {
+ if (records[i].size() == 0) continue;
+
+ for (size_t j = i + 1; j < records.size(); ++j)
+ {
+ if (dns_records_match(records[i], records[j]))
+ {
+ good_records_index = i;
+ break;
+ }
+ }
+ if (good_records_index >= 0) break;
+ }
+
+ if (good_records_index < 0)
+ {
+ LOG_PRINT_L0("WARNING: no two MoneroPulse DNS checkpoint records matched");
+ return false;
+ }
+
+ good_records = records[good_records_index];
+ return true;
+}
+
} // namespace tools::dns_utils
} // namespace tools