Merge pull request #4778

71eb32a9 dns_utils: do not exit if DNS records are corrupt (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2018-11-16 11:02:28 +02:00
commit 140defd89f
No known key found for this signature in database
GPG key ID: 55432DF31CCD4FCD
2 changed files with 37 additions and 9 deletions

View file

@ -37,6 +37,7 @@
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp> #include <boost/thread/thread.hpp>
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
#include <boost/optional.hpp>
using namespace epee; using namespace epee;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
@ -119,10 +120,25 @@ get_builtin_ds(void)
namespace tools namespace tools
{ {
// fuck it, I'm tired of dealing with getnameinfo()/inet_ntop/etc static const char *get_record_name(int record_type)
std::string ipv4_to_string(const char* src, size_t len)
{ {
assert(len >= 4); switch (record_type)
{
case DNS_TYPE_A: return "A";
case DNS_TYPE_TXT: return "TXT";
case DNS_TYPE_AAAA: return "AAAA";
default: return "unknown";
}
}
// fuck it, I'm tired of dealing with getnameinfo()/inet_ntop/etc
boost::optional<std::string> ipv4_to_string(const char* src, size_t len)
{
if (len < 4)
{
MERROR("Invalid IPv4 address: " << std::string(src, len));
return boost::none;
}
std::stringstream ss; std::stringstream ss;
unsigned int bytes[4]; unsigned int bytes[4];
@ -140,9 +156,13 @@ std::string ipv4_to_string(const char* src, size_t len)
// this obviously will need to change, but is here to reflect the above // this obviously will need to change, but is here to reflect the above
// stop-gap measure and to make the tests pass at least... // stop-gap measure and to make the tests pass at least...
std::string ipv6_to_string(const char* src, size_t len) boost::optional<std::string> ipv6_to_string(const char* src, size_t len)
{ {
assert(len >= 8); if (len < 8)
{
MERROR("Invalid IPv4 address: " << std::string(src, len));
return boost::none;
}
std::stringstream ss; std::stringstream ss;
unsigned int bytes[8]; unsigned int bytes[8];
@ -162,8 +182,10 @@ std::string ipv6_to_string(const char* src, size_t len)
return ss.str(); return ss.str();
} }
std::string txt_to_string(const char* src, size_t len) boost::optional<std::string> txt_to_string(const char* src, size_t len)
{ {
if (len == 0)
return boost::none;
return std::string(src+1, len-1); return std::string(src+1, len-1);
} }
@ -266,7 +288,7 @@ DNSResolver::~DNSResolver()
} }
} }
std::vector<std::string> DNSResolver::get_record(const std::string& url, int record_type, std::string (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid) std::vector<std::string> DNSResolver::get_record(const std::string& url, int record_type, boost::optional<std::string> (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid)
{ {
std::vector<std::string> addresses; std::vector<std::string> addresses;
dnssec_available = false; dnssec_available = false;
@ -289,7 +311,12 @@ std::vector<std::string> DNSResolver::get_record(const std::string& url, int rec
{ {
for (size_t i=0; result->data[i] != NULL; i++) for (size_t i=0; result->data[i] != NULL; i++)
{ {
addresses.push_back((*reader)(result->data[i], result->len[i])); boost::optional<std::string> res = (*reader)(result->data[i], result->len[i]);
if (res)
{
MINFO("Found \"" << *res << "\" in " << get_record_name(record_type) << " record for " << url);
addresses.push_back(*res);
}
} }
} }
} }

View file

@ -30,6 +30,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <functional> #include <functional>
#include <boost/optional/optional_fwd.hpp>
namespace tools namespace tools
{ {
@ -143,7 +144,7 @@ private:
* @return A vector of strings containing the requested record; or an empty vector * @return A vector of strings containing the requested record; or an empty vector
*/ */
// TODO: modify this to accommodate DNSSEC // TODO: modify this to accommodate DNSSEC
std::vector<std::string> get_record(const std::string& url, int record_type, std::string (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid); std::vector<std::string> get_record(const std::string& url, int record_type, boost::optional<std::string> (*reader)(const char *,size_t), bool& dnssec_available, bool& dnssec_valid);
/** /**
* @brief Checks a string to see if it looks like a URL * @brief Checks a string to see if it looks like a URL