mirror of
https://git.wownero.com/wownero/wownero.git
synced 2024-12-23 02:58:51 +00:00
ipv4 and ipv6 resolution working
IPv4 and IPv6 name resolution working. Unit tests written (and passing). net_node.{h,inl} code modified to use DNS seeds.
This commit is contained in:
parent
dea98df6b1
commit
578050e91d
@ -27,29 +27,110 @@
|
|||||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#include "common/dns_utils.h"
|
#include "common/dns_utils.h"
|
||||||
|
#include <ldns/rr.h> // for RR type and class defs
|
||||||
|
#include <unbound.h>
|
||||||
|
#include <arpa/inet.h> // for inet_ntoa (bytes to text for IPs)
|
||||||
|
|
||||||
namespace tools
|
namespace tools
|
||||||
{
|
|
||||||
namespace dns
|
|
||||||
{
|
{
|
||||||
|
|
||||||
std::vector<std::string> get_ipv4(const std::string& uri)
|
struct DNSResolverData
|
||||||
{
|
{
|
||||||
std::vector<std::string retval;
|
ub_ctx* m_ub_context;
|
||||||
|
};
|
||||||
|
|
||||||
|
DNSResolver::DNSResolver() : m_data(new DNSResolverData())
|
||||||
|
{
|
||||||
|
// init libunbound context
|
||||||
|
m_data->m_ub_context = ub_ctx_create();
|
||||||
|
|
||||||
|
// look for "/etc/resolv.conf" and "/etc/hosts" or platform equivalent
|
||||||
|
ub_ctx_resolvconf(m_data->m_ub_context, "");
|
||||||
|
ub_ctx_hosts(m_data->m_ub_context, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
DNSResolver::~DNSResolver()
|
||||||
|
{
|
||||||
|
if (m_data)
|
||||||
|
{
|
||||||
|
if (m_data->m_ub_context != NULL)
|
||||||
|
{
|
||||||
|
ub_ctx_delete(m_data->m_ub_context);
|
||||||
|
}
|
||||||
|
delete m_data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> DNSResolver::get_ipv4(const std::string& url)
|
||||||
|
{
|
||||||
|
ub_result* result = NULL;
|
||||||
|
std::vector<std::string> retval;
|
||||||
|
|
||||||
|
// call DNS resolver, blocking. if return value not zero, something went wrong
|
||||||
|
if (!ub_resolve(m_data->m_ub_context, url.c_str(), LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN, &result))
|
||||||
|
{
|
||||||
|
if (result->havedata)
|
||||||
|
{
|
||||||
|
for (int i=0; result->data[i] != NULL; i++)
|
||||||
|
{
|
||||||
|
char as_str[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
|
// convert bytes to string, append if no error
|
||||||
|
if (inet_ntop(AF_INET, result->data[0], as_str, sizeof(as_str)))
|
||||||
|
{
|
||||||
|
retval.push_back(as_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
ub_resolve_free(result);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> get_ipv6(const std::string& uri)
|
std::vector<std::string> DNSResolver::get_ipv6(const std::string& url)
|
||||||
{
|
{
|
||||||
std::vector<std::string retval;
|
ub_result* result = NULL;
|
||||||
|
std::vector<std::string> retval;
|
||||||
|
|
||||||
|
// call DNS resolver, blocking. if return value not zero, something went wrong
|
||||||
|
if (!ub_resolve(m_data->m_ub_context, url.c_str(), LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN, &result))
|
||||||
|
{
|
||||||
|
if (result->havedata)
|
||||||
|
{
|
||||||
|
for (int i=0; result->data[i] != NULL; i++)
|
||||||
|
{
|
||||||
|
char as_str[INET6_ADDRSTRLEN];
|
||||||
|
|
||||||
|
// convert bytes to string, append if no error
|
||||||
|
if (inet_ntop(AF_INET6, result->data[0], as_str, sizeof(as_str)))
|
||||||
|
{
|
||||||
|
retval.push_back(as_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
ub_resolve_free(result);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_payment_address(const std::string& uri)
|
std::string DNSResolver::get_payment_address(const std::string& url)
|
||||||
{
|
{
|
||||||
std::string retval;
|
std::string retval;
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dns
|
DNSResolver& DNSResolver::instance()
|
||||||
|
{
|
||||||
|
static DNSResolver* staticInstance = NULL;
|
||||||
|
if (staticInstance == NULL)
|
||||||
|
{
|
||||||
|
staticInstance = new DNSResolver();
|
||||||
|
}
|
||||||
|
return *staticInstance;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace tools
|
} // namespace tools
|
||||||
|
@ -32,6 +32,8 @@
|
|||||||
namespace tools
|
namespace tools
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct DNSResolverData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Provides high-level access to DNS resolution
|
* @brief Provides high-level access to DNS resolution
|
||||||
*
|
*
|
||||||
@ -44,43 +46,64 @@ class DNSResolver
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Constructs and sets the URI to be resolved
|
* @brief Constructs an instance of DNSResolver
|
||||||
*
|
*
|
||||||
* @param uri the URI to be resolved
|
* Constructs a class instance and does setup stuff for the backend resolver.
|
||||||
*/
|
*/
|
||||||
DNSResolver(const std::string& uri);
|
DNSResolver();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief gets ipv4 addresses from DNS query
|
* @brief takes care of freeing C pointers and such
|
||||||
|
*/
|
||||||
|
~DNSResolver();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief gets ipv4 addresses from DNS query of a URL
|
||||||
*
|
*
|
||||||
* returns a vector of all IPv4 "A" records for given URI.
|
* returns a vector of all IPv4 "A" records for given URL.
|
||||||
* If no "A" records found, returns an empty vector.
|
* If no "A" records found, returns an empty vector.
|
||||||
*
|
*
|
||||||
|
* @param url A string containing a URL to query for
|
||||||
|
*
|
||||||
* @return vector of strings containing ipv4 addresses
|
* @return vector of strings containing ipv4 addresses
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> get_ipv4();
|
std::vector<std::string> get_ipv4(const std::string& url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief gets ipv6 addresses from DNS query
|
* @brief gets ipv6 addresses from DNS query
|
||||||
*
|
*
|
||||||
* returns a vector of all IPv6 "A" records for given URI.
|
* returns a vector of all IPv6 "A" records for given URL.
|
||||||
* If no "A" records found, returns an empty vector.
|
* If no "A" records found, returns an empty vector.
|
||||||
*
|
*
|
||||||
|
* @param url A string containing a URL to query for
|
||||||
|
*
|
||||||
* @return vector of strings containing ipv6 addresses
|
* @return vector of strings containing ipv6 addresses
|
||||||
*/
|
*/
|
||||||
std::vector<std::string> get_ipv6();
|
std::vector<std::string> get_ipv6(const std::string& url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief gets a monero address from the TXT record of the DNS query response
|
* @brief gets a monero address from the TXT record of the DNS query response
|
||||||
*
|
*
|
||||||
* returns a monero address string from the TXT record associated with URI
|
* returns a monero address string from the TXT record associated with URL
|
||||||
* if no TXT record present, or no valid monero address in TXT,
|
* if no TXT record present, or no valid monero address in TXT,
|
||||||
* returns an empty string.
|
* returns an empty string.
|
||||||
*
|
*
|
||||||
|
* @param url A string containing a URL to query for
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
std::string get_payment_address();
|
std::string get_payment_address(const std::string& url);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the singleton instance of DNSResolver
|
||||||
|
*
|
||||||
|
* @return returns a pointer to the singleton
|
||||||
|
*/
|
||||||
|
static DNSResolver& instance();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
DNSResolverData *m_data;
|
||||||
}; // class DNSResolver
|
}; // class DNSResolver
|
||||||
|
|
||||||
} // namespace tools
|
} // namespace tools
|
||||||
|
@ -112,6 +112,13 @@ namespace nodetool
|
|||||||
size_t get_outgoing_connections_count();
|
size_t get_outgoing_connections_count();
|
||||||
peerlist_manager& get_peerlist_manager(){return m_peerlist;}
|
peerlist_manager& get_peerlist_manager(){return m_peerlist;}
|
||||||
private:
|
private:
|
||||||
|
const std::vector<std::string> m_seed_nodes_list =
|
||||||
|
{ "seeds.moneroseeds.se"
|
||||||
|
, "seeds.moneroseeds.ae.org"
|
||||||
|
, "seeds.moneroseeds.ch"
|
||||||
|
, "seeds.moneroseeds.li"
|
||||||
|
};
|
||||||
|
|
||||||
typedef COMMAND_REQUEST_STAT_INFO_T<typename t_payload_net_handler::stat_info> COMMAND_REQUEST_STAT_INFO;
|
typedef COMMAND_REQUEST_STAT_INFO_T<typename t_payload_net_handler::stat_info> COMMAND_REQUEST_STAT_INFO;
|
||||||
|
|
||||||
CHAIN_LEVIN_INVOKE_MAP2(p2p_connection_context); //move levin_commands_handler interface invoke(...) callbacks into invoke map
|
CHAIN_LEVIN_INVOKE_MAP2(p2p_connection_context); //move levin_commands_handler interface invoke(...) callbacks into invoke map
|
||||||
|
@ -244,6 +244,21 @@ namespace nodetool
|
|||||||
add_hardcoded_seed_node(m_seed_nodes, "107.152.130.98:28080");
|
add_hardcoded_seed_node(m_seed_nodes, "107.152.130.98:28080");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
// for each hostname in the seed nodes list, attempt to DNS resolve and
|
||||||
|
// add the result addresses as seed nodes
|
||||||
|
// TODO: at some point add IPv6 support, but that won't be relevant
|
||||||
|
// for some time yet.
|
||||||
|
for (const std::string& addr_str : m_seed_nodes_list)
|
||||||
|
{
|
||||||
|
std::vector<std::string> addr_list = tools::DNSResolver::instance().get_ipv4(addr_str);
|
||||||
|
for (const std::string& a : addr_list)
|
||||||
|
{
|
||||||
|
append_net_address(m_seed_nodes, a + ":18080");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_seed_nodes.size())
|
||||||
{
|
{
|
||||||
add_hardcoded_seed_node(m_seed_nodes, "62.210.78.186:18080");
|
add_hardcoded_seed_node(m_seed_nodes, "62.210.78.186:18080");
|
||||||
add_hardcoded_seed_node(m_seed_nodes, "195.12.60.154:18080");
|
add_hardcoded_seed_node(m_seed_nodes, "195.12.60.154:18080");
|
||||||
@ -257,6 +272,7 @@ namespace nodetool
|
|||||||
add_hardcoded_seed_node(m_seed_nodes, "107.158.233.98:18080");
|
add_hardcoded_seed_node(m_seed_nodes, "107.158.233.98:18080");
|
||||||
add_hardcoded_seed_node(m_seed_nodes, "64.22.111.2:18080");
|
add_hardcoded_seed_node(m_seed_nodes, "64.22.111.2:18080");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool res = handle_command_line(vm, testnet);
|
bool res = handle_command_line(vm, testnet);
|
||||||
CHECK_AND_ASSERT_MES(res, false, "Failed to handle command line");
|
CHECK_AND_ASSERT_MES(res, false, "Failed to handle command line");
|
||||||
|
65
tests/unit_tests/dns_resolver.cpp
Normal file
65
tests/unit_tests/dns_resolver.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "common/dns_utils.h"
|
||||||
|
|
||||||
|
TEST(DNSResolver, IPv4Success)
|
||||||
|
{
|
||||||
|
tools::DNSResolver resolver;
|
||||||
|
|
||||||
|
auto ips = resolver.get_ipv4("example.com");
|
||||||
|
|
||||||
|
ASSERT_EQ(1, ips.size());
|
||||||
|
|
||||||
|
ASSERT_STREQ("93.184.216.119", ips[0].c_str());
|
||||||
|
|
||||||
|
ips = tools::DNSResolver::instance().get_ipv4("example.com");
|
||||||
|
|
||||||
|
ASSERT_EQ(1, ips.size());
|
||||||
|
|
||||||
|
ASSERT_STREQ("93.184.216.119", ips[0].c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DNSResolver, IPv4Failure)
|
||||||
|
{
|
||||||
|
// guaranteed by IANA/ICANN/RFC to be invalid
|
||||||
|
tools::DNSResolver resolver;
|
||||||
|
|
||||||
|
auto ips = resolver.get_ipv4("example.invalid");
|
||||||
|
|
||||||
|
ASSERT_EQ(0, ips.size());
|
||||||
|
|
||||||
|
ips = tools::DNSResolver::instance().get_ipv4("example.invalid");
|
||||||
|
|
||||||
|
ASSERT_EQ(0, ips.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DNSResolver, IPv6Success)
|
||||||
|
{
|
||||||
|
tools::DNSResolver resolver;
|
||||||
|
|
||||||
|
auto ips = resolver.get_ipv6("example.com");
|
||||||
|
|
||||||
|
ASSERT_EQ(1, ips.size());
|
||||||
|
|
||||||
|
ASSERT_STREQ("2606:2800:220:6d:26bf:1447:1097:aa7", ips[0].c_str());
|
||||||
|
|
||||||
|
ips = tools::DNSResolver::instance().get_ipv6("example.com");
|
||||||
|
|
||||||
|
ASSERT_EQ(1, ips.size());
|
||||||
|
|
||||||
|
ASSERT_STREQ("2606:2800:220:6d:26bf:1447:1097:aa7", ips[0].c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DNSResolver, IPv6Failure)
|
||||||
|
{
|
||||||
|
// guaranteed by IANA/ICANN/RFC to be invalid
|
||||||
|
tools::DNSResolver resolver;
|
||||||
|
|
||||||
|
auto ips = resolver.get_ipv6("example.invalid");
|
||||||
|
|
||||||
|
ASSERT_EQ(0, ips.size());
|
||||||
|
|
||||||
|
ips = tools::DNSResolver::instance().get_ipv6("example.invalid");
|
||||||
|
|
||||||
|
ASSERT_EQ(0, ips.size());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user