mirror of
https://github.com/monero-project/monero.git
synced 2025-08-19 07:57:55 -04: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
5 changed files with 226 additions and 34 deletions
|
@ -27,29 +27,110 @@
|
|||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#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 dns
|
||||
|
||||
struct DNSResolverData
|
||||
{
|
||||
ub_ctx* m_ub_context;
|
||||
};
|
||||
|
||||
std::vector<std::string> get_ipv4(const std::string& uri)
|
||||
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)
|
||||
{
|
||||
std::vector<std::string retval;
|
||||
return retval;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> get_ipv6(const std::string& uri)
|
||||
// cleanup
|
||||
ub_resolve_free(result);
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::vector<std::string> DNSResolver::get_ipv6(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_AAAA, LDNS_RR_CLASS_IN, &result))
|
||||
{
|
||||
std::vector<std::string retval;
|
||||
return retval;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_payment_address(const std::string& uri)
|
||||
{
|
||||
std::string retval;
|
||||
return retval;
|
||||
}
|
||||
// cleanup
|
||||
ub_resolve_free(result);
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::string DNSResolver::get_payment_address(const std::string& url)
|
||||
{
|
||||
std::string retval;
|
||||
return retval;
|
||||
}
|
||||
|
||||
DNSResolver& DNSResolver::instance()
|
||||
{
|
||||
static DNSResolver* staticInstance = NULL;
|
||||
if (staticInstance == NULL)
|
||||
{
|
||||
staticInstance = new DNSResolver();
|
||||
}
|
||||
return *staticInstance;
|
||||
}
|
||||
|
||||
} // namespace dns
|
||||
} // namespace tools
|
||||
|
|
|
@ -32,6 +32,8 @@
|
|||
namespace tools
|
||||
{
|
||||
|
||||
struct DNSResolverData;
|
||||
|
||||
/**
|
||||
* @brief Provides high-level access to DNS resolution
|
||||
*
|
||||
|
@ -44,43 +46,64 @@ class DNSResolver
|
|||
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.
|
||||
*
|
||||
* @param url A string containing a URL to query for
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param url A string containing a URL to query for
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* 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,
|
||||
* returns an empty string.
|
||||
*
|
||||
* @param url A string containing a URL to query for
|
||||
*
|
||||
* @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
|
||||
|
||||
} // namespace tools
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue