From 578050e91d8fad8834a7c180a15824f5a46940f9 Mon Sep 17 00:00:00 2001 From: Thomas Winget Date: Wed, 17 Sep 2014 15:35:52 -0400 Subject: 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. --- src/common/dns_utils.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 11 deletions(-) (limited to 'src/common/dns_utils.cpp') diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index 0bacc4d46..f95a8d72b 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -27,29 +27,110 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "common/dns_utils.h" +#include // for RR type and class defs +#include +#include // for inet_ntoa (bytes to text for IPs) namespace tools { -namespace dns + +struct DNSResolverData +{ + ub_ctx* m_ub_context; +}; + +DNSResolver::DNSResolver() : m_data(new DNSResolverData()) { + // init libunbound context + m_data->m_ub_context = ub_ctx_create(); - std::vector get_ipv4(const std::string& uri) + // 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::vectorm_ub_context != NULL) + { + ub_ctx_delete(m_data->m_ub_context); + } + delete m_data; } +} - std::vector get_ipv6(const std::string& uri) +std::vector DNSResolver::get_ipv4(const std::string& url) +{ + ub_result* result = NULL; + std::vector 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)) { - std::vectorhavedata) + { + 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::string get_payment_address(const std::string& uri) + // cleanup + ub_resolve_free(result); + return retval; +} + +std::vector DNSResolver::get_ipv6(const std::string& url) +{ + ub_result* result = NULL; + std::vector 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; +} + +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) { - std::string retval; - return retval; + staticInstance = new DNSResolver(); } + return *staticInstance; +} -} // namespace dns } // namespace tools -- cgit v1.2.3