Monero addres from DNS TXT record implemented, tests pass

Still need to deal with DNSSEC and optional fields in the TXT record.
This commit is contained in:
Thomas Winget 2014-09-17 17:26:51 -04:00 committed by Riccardo Spagni
parent 3fb0fc020f
commit a5757a628f
6 changed files with 243 additions and 24 deletions

View File

@ -34,6 +34,22 @@
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;
@ -63,20 +79,22 @@ DNSResolver::~DNSResolver()
std::vector<std::string> DNSResolver::get_ipv4(const std::string& url)
{
ub_result* result = NULL;
// destructor takes care of cleanup
ub_result_ptr result;
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 (!ub_resolve(m_data->m_ub_context, url.c_str(), LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN, &(result.ptr)))
{
if (result->havedata)
if (result.ptr->havedata)
{
for (int i=0; result->data[i] != NULL; i++)
for (int 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->data[i], as_str, sizeof(as_str)))
if (inet_ntop(AF_INET, result.ptr->data[i], as_str, sizeof(as_str)))
{
retval.push_back(as_str);
}
@ -84,27 +102,25 @@ std::vector<std::string> DNSResolver::get_ipv4(const std::string& url)
}
}
// cleanup
ub_resolve_free(result);
return retval;
}
std::vector<std::string> DNSResolver::get_ipv6(const std::string& url)
{
ub_result* result = NULL;
ub_result_ptr result;
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 (!ub_resolve(m_data->m_ub_context, url.c_str(), LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN, &(result.ptr)))
{
if (result->havedata)
if (result.ptr->havedata)
{
for (int i=0; result->data[i] != NULL; i++)
for (int 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->data[i], as_str, sizeof(as_str)))
if (inet_ntop(AF_INET6, result.ptr->data[i], as_str, sizeof(as_str)))
{
retval.push_back(as_str);
}
@ -112,15 +128,22 @@ std::vector<std::string> DNSResolver::get_ipv6(const std::string& url)
}
}
// cleanup
ub_resolve_free(result);
return retval;
}
std::string DNSResolver::get_payment_address(const std::string& url)
std::string DNSResolver::get_txt_record(const std::string& url)
{
std::string retval;
return retval;
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)
{
return std::string(result.ptr->data[0]);
}
}
return std::string();
}
DNSResolver& DNSResolver::instance()

View File

@ -82,17 +82,15 @@ public:
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 URL
* if no TXT record present, or no valid monero address in TXT,
* returns an empty string.
* @brief gets a TXT record from a DNS query for the supplied URL;
* if no TXT record present returns an empty string.
*
* @param url A string containing a URL to query for
*
* @return
* @return A string containing a TXT record; or an empty string
*/
std::string get_payment_address(const std::string& url);
// TODO: modify this to accomodate DNSSEC
std::string get_txt_record(const std::string& url);
/**
* @brief Gets the singleton instance of DNSResolver

View File

@ -47,6 +47,7 @@ using namespace epee;
#include "serialization/binary_utils.h"
#include "cryptonote_protocol/blobdatatype.h"
#include "crypto/electrum-words.h"
#include "common/dns_utils.h"
extern "C"
{
@ -751,6 +752,7 @@ void wallet2::add_unconfirmed_tx(const cryptonote::transaction& tx, uint64_t cha
utd.m_sent_time = time(NULL);
utd.m_tx = tx;
}
//----------------------------------------------------------------------------------------------------
void wallet2::transfer(const std::vector<cryptonote::tx_destination_entry>& dsts, size_t fake_outputs_count,
uint64_t unlock_time, uint64_t fee, const std::vector<uint8_t>& extra, cryptonote::transaction& tx, pending_tx& ptx)
@ -815,6 +817,58 @@ std::vector<std::vector<cryptonote::tx_destination_entry>> split_amounts(
}
} // anonymous namespace
/**
* @brief gets a monero address from the TXT record of a DNS entry
*
* gets the monero address from the TXT record of the DNS entry associated
* with <url>. If this lookup fails, or the TXT record does not contain an
* XMR address in the correct format, returns an empty string. <dnssec_valid>
* will be set true or false according to whether or not the DNS query passes
* DNSSEC validation.
*
* @param url the url to look up
* @param dnssec_valid return-by-reference for DNSSEC status of query
*
* @return a monero address (as a string) or an empty string
*/
std::string wallet2::address_from_url(const std::string& url, bool& dnssec_valid)
{
// TODO: update this correctly once DNSResolver::get_txt_record() supports it.
dnssec_valid = false;
// get txt record
std::string txt = tools::DNSResolver::instance().get_txt_record(url);
if (txt.size())
{
return address_from_txt_record(txt);
}
return std::string();
}
//----------------------------------------------------------------------------------------------------
std::string wallet2::address_from_txt_record(const std::string& s)
{
// make sure the txt record has "oa1:xmr" and find it
auto pos = s.find("oa1:xmr");
// search from there to find "recipient_address="
pos = s.find("recipient_address=", pos);
pos += 18; // move past "recipient_address="
// find the next semicolon
auto pos2 = s.find(";", pos);
if (pos2 != std::string::npos)
{
// length of address == 95, we can at least validate that much here
if (pos2 - pos == 95)
{
return s.substr(pos, 95);
}
}
return std::string();
}
//----------------------------------------------------------------------------------------------------
// take a pending tx and actually send it to the daemon
void wallet2::commit_tx(pending_tx& ptx)

View File

@ -196,6 +196,9 @@ namespace tools
static bool parse_payment_id(const std::string& payment_id_str, crypto::hash& payment_id);
static std::string address_from_url(const std::string& url, bool& dnssec_valid);
static std::string address_from_txt_record(const std::string& s);
private:
bool store_keys(const std::string& keys_file_name, const std::string& password);
void load_keys(const std::string& keys_file_name, const std::string& password);

View File

@ -0,0 +1,102 @@
// 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.
// FIXME: move this into a full wallet2 unit test suite, if possible
#include "gtest/gtest.h"
#include "wallet/wallet2.h"
#include <string>
TEST(AddressFromTXT, Success)
{
std::string addr = "46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em";
std::string txtr = "oa1:xmr";
txtr += " recipient_address=";
txtr += addr;
txtr += ";";
std::string res = tools::wallet2::address_from_txt_record(txtr);
EXPECT_STREQ(addr.c_str(), res.c_str());
std::string txtr2 = "foobar";
txtr2 += txtr;
txtr2 += "more foobar";
res = tools::wallet2::address_from_txt_record(txtr2);
EXPECT_STREQ(addr.c_str(), res.c_str());
std::string txtr3 = "foobar oa1:xmr tx_description=\"Donation for Monero Development Fund\"; ";
txtr3 += "recipient_address=";
txtr3 += addr;
txtr3 += "; foobar";
res = tools::wallet2::address_from_txt_record(txtr3);
EXPECT_STREQ(addr.c_str(), res.c_str());
}
TEST(AddressFromTXT, Failure)
{
std::string txtr = "oa1:xmr recipient_address=not a real address";
std::string res = tools::wallet2::address_from_txt_record(txtr);
ASSERT_STREQ("", res.c_str());
txtr += ";";
res = tools::wallet2::address_from_txt_record(txtr);
ASSERT_STREQ("", res.c_str());
}
TEST(AddressFromURL, Success)
{
std::string addr = "46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em";
bool dnssec_result = false;
std::string res = tools::wallet2::address_from_url("donate.monero.cc", dnssec_result);
EXPECT_STREQ(addr.c_str(), res.c_str());
}
TEST(AddressFromURL, Failure)
{
bool dnssec_result = false;
std::string res = tools::wallet2::address_from_url("example.invalid", dnssec_result);
ASSERT_FALSE(dnssec_result);
ASSERT_STREQ("", res.c_str());
}

View File

@ -1,3 +1,33 @@
// 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 <iostream>
#include "gtest/gtest.h"
#include "common/dns_utils.h"
@ -63,3 +93,12 @@ TEST(DNSResolver, IPv6Failure)
ASSERT_EQ(0, ips.size());
}
TEST(DNSResolver, GetTXTRecord)
{
std::string txt = tools::DNSResolver::instance().get_txt_record("donate.monero.cc");
std::cout << "TXT record for donate.monero.cc: " << txt << std::endl;
EXPECT_STRNE("", txt.c_str());
}