Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS monero address aliasing and seed node lookup #146

Closed
wants to merge 11 commits into from
55 changes: 55 additions & 0 deletions CMake/FindUnbound.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2014, The Monero Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific
# prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

include (CheckIncludeFiles)
include (CheckLibraryExists)
include (CheckSymbolExists)

set (Unbound_FOUND FALSE)
MESSAGE("Attempting to find libunbound")

#FIND_PATH("unbound.h" CMAKE_HAVE_UNBOUND_H)
MESSAGE("CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")
MESSAGE("CMAKE_SYSTEM_INCLUDE_PATH: ${CMAKE_SYSTEM_INCLUDE_PATH}")
CHECK_INCLUDE_FILES("unbound.h" CMAKE_HAVE_UNBOUND_H)
MESSAGE("CMAKE_HAVE_UNBOUND_H: ${CMAKE_HAVE_UNBOUND_H}")

if(CMAKE_HAVE_UNBOUND_H)

MESSAGE("unbound.h found")

CHECK_LIBRARY_EXISTS(unbound ub_ctx_create "" CMAKE_HAVE_UNBOUND)

if(CMAKE_HAVE_UNBOUND)
MESSAGE("-lunbound works?")
set(CMAKE_UNBOUND_LIB "-lunbound")
set(Unbound_FOUND TRUE)
endif()
endif()

include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Unbound DEFAULT_MSG Unbound_FOUND)
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

cmake_minimum_required(VERSION 2.8.6)

set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMake")

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
enable_testing()
Expand Down Expand Up @@ -70,6 +72,8 @@ if (UNIX AND NOT APPLE)
find_package(Threads)
endif()

find_package(Unbound REQUIRED)

if(MSVC)
add_definitions("/bigobj /MP /W3 /GS- /D_CRT_SECURE_NO_WARNINGS /wd4996 /wd4345 /D_WIN32_WINNT=0x0600 /DWIN32_LEAN_AND_MEAN /DGTEST_HAS_TR1_TUPLE=0 /FIinline_c.h /D__SSE4_1__")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline")
Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ add_library(cryptonote_core ${CRYPTONOTE_CORE})
add_executable(daemon ${DAEMON} ${P2P} ${CRYPTONOTE_PROTOCOL})
add_executable(connectivity_tool ${CONN_TOOL})
add_executable(simpleminer ${MINER})
target_link_libraries(daemon rpc cryptonote_core crypto common ${UPNP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
target_link_libraries(connectivity_tool cryptonote_core crypto common ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
target_link_libraries(simpleminer cryptonote_core crypto common ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
target_link_libraries(daemon rpc cryptonote_core crypto common ${UPNP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CMAKE_UNBOUND_LIB})
target_link_libraries(connectivity_tool cryptonote_core crypto common ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CMAKE_UNBOUND_LIB})
target_link_libraries(simpleminer cryptonote_core crypto common ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CMAKE_UNBOUND_LIB})
add_library(rpc ${RPC})
add_library(wallet ${WALLET})
add_executable(simplewallet ${SIMPLEWALLET} )
target_link_libraries(simplewallet wallet rpc cryptonote_core crypto common ${UPNP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES})
target_link_libraries(simplewallet wallet rpc cryptonote_core crypto common ${UPNP_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${CMAKE_UNBOUND_LIB})
add_dependencies(daemon version)
add_dependencies(rpc version)
add_dependencies(simplewallet version)
Expand Down
191 changes: 191 additions & 0 deletions src/common/dns_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright (c) 2014, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// 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
{

// custom smart pointer.
// TODO: see if std::auto_ptr and the like support custom destructors
class ub_result_ptr
{
public:
ub_result_ptr()
{
ptr = nullptr;
}
~ub_result_ptr()
{
ub_resolve_free(ptr);
}
ub_result* ptr;
};

struct DNSResolverData
{
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)
{
std::vector<std::string> addresses;

if (!check_address_syntax(url))
{
return addresses;
}

// destructor takes care of cleanup
ub_result_ptr result;

// 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.ptr)))
{
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
{
char as_str[INET_ADDRSTRLEN];

// convert bytes to string, append if no error
if (inet_ntop(AF_INET, result.ptr->data[i], as_str, sizeof(as_str)))
{
addresses.push_back(as_str);
}
}
}
}

return addresses;
}

std::vector<std::string> DNSResolver::get_ipv6(const std::string& url)
{
std::vector<std::string> addresses;

if (!check_address_syntax(url))
{
return addresses;
}

ub_result_ptr result;

// 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.ptr)))
{
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
{
char as_str[INET6_ADDRSTRLEN];

// convert bytes to string, append if no error
if (inet_ntop(AF_INET6, result.ptr->data[i], as_str, sizeof(as_str)))
{
addresses.push_back(as_str);
}
}
}
}

return addresses;
}

std::vector<std::string> DNSResolver::get_txt_record(const std::string& url)
{
std::vector<std::string> records;

if (!check_address_syntax(url))
{
return records;
}

ub_result_ptr result;

// 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_TXT, LDNS_RR_CLASS_IN, &(result.ptr)))
{
if (result.ptr->havedata)
{
for (size_t i=0; result.ptr->data[i] != NULL; i++)
{
records.push_back(result.ptr->data[i]);
}
}
}

return records;
}

DNSResolver& DNSResolver::instance()
{
static DNSResolver* staticInstance = NULL;
if (staticInstance == NULL)
{
staticInstance = new DNSResolver();
}
return *staticInstance;
}

bool DNSResolver::check_address_syntax(const std::string& addr)
{
// if string doesn't contain a dot, we won't consider it a url for now.
if (addr.find(".") == std::string::npos)
{
return false;
}
return true;
}

} // namespace tools
Loading