Merge pull request #5091

123fc2a2 i2p: initial support (Jethro Grassie)
This commit is contained in:
Riccardo Spagni
2019-03-04 21:20:34 +02:00
14 changed files with 450 additions and 40 deletions
+1 -1
View File
@@ -110,7 +110,7 @@
#define P2P_DEFAULT_PACKET_MAX_SIZE 50000000 //50000000 bytes maximum packet size
#define P2P_DEFAULT_PEERS_IN_HANDSHAKE 250
#define P2P_DEFAULT_CONNECTION_TIMEOUT 5000 //5 seconds
#define P2P_DEFAULT_TOR_CONNECT_TIMEOUT 20 // seconds
#define P2P_DEFAULT_SOCKS_CONNECT_TIMEOUT 45 // seconds
#define P2P_DEFAULT_PING_CONNECTION_TIMEOUT 2000 //2 seconds
#define P2P_DEFAULT_INVOKE_TIMEOUT 60*2*1000 //2 minutes
#define P2P_DEFAULT_HANDSHAKE_INVOKE_TIMEOUT 5000 //5 seconds
+2 -2
View File
@@ -26,8 +26,8 @@
# 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.
set(net_sources error.cpp parse.cpp socks.cpp tor_address.cpp)
set(net_headers error.h parse.h socks.h tor_address.h)
set(net_sources error.cpp parse.cpp socks.cpp tor_address.cpp i2p_address.cpp)
set(net_headers error.h parse.h socks.h tor_address.h i2p_address.h)
monero_add_library(net ${net_sources} ${net_headers})
target_link_libraries(net epee ${Boost_ASIO_LIBRARY})
+1
View File
@@ -34,6 +34,7 @@ namespace net
{
enum class error : int;
class tor_address;
class i2p_address;
namespace socks
{
+200
View File
@@ -0,0 +1,200 @@
// Copyright (c) 2019, 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 "i2p_address.h"
#include <algorithm>
#include <boost/spirit/include/karma_generate.hpp>
#include <boost/spirit/include/karma_uint.hpp>
#include <cassert>
#include <cstring>
#include <limits>
#include "net/error.h"
#include "serialization/keyvalue_serialization.h"
#include "storages/portable_storage.h"
#include "string_tools.h"
namespace net
{
namespace
{
// !TODO only b32 addresses right now
constexpr const char tld[] = u8".b32.i2p";
constexpr const char unknown_host[] = "<unknown i2p host>";
constexpr const unsigned b32_length = 52;
constexpr const char base32_alphabet[] =
u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz234567";
expect<void> host_check(boost::string_ref host) noexcept
{
if (!host.ends_with(tld))
return {net::error::expected_tld};
host.remove_suffix(sizeof(tld) - 1);
if (host.size() != b32_length)
return {net::error::invalid_i2p_address};
if (host.find_first_not_of(base32_alphabet) != boost::string_ref::npos)
return {net::error::invalid_i2p_address};
return success();
}
struct i2p_serialized
{
std::string host;
std::uint16_t port;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(host)
KV_SERIALIZE(port)
END_KV_SERIALIZE_MAP()
};
}
i2p_address::i2p_address(const boost::string_ref host, const std::uint16_t port) noexcept
: port_(port)
{
// this is a private constructor, throw if moved to public
assert(host.size() < sizeof(host_));
const std::size_t length = std::min(sizeof(host_) - 1, host.size());
std::memcpy(host_, host.data(), length);
std::memset(host_ + length, 0, sizeof(host_) - length);
}
const char* i2p_address::unknown_str() noexcept
{
return unknown_host;
}
i2p_address::i2p_address() noexcept
: port_(0)
{
static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
std::memcpy(host_, unknown_host, sizeof(unknown_host));
std::memset(host_ + sizeof(unknown_host), 0, sizeof(host_) - sizeof(unknown_host));
}
expect<i2p_address> i2p_address::make(const boost::string_ref address, const std::uint16_t default_port)
{
boost::string_ref host = address.substr(0, address.rfind(':'));
const boost::string_ref port =
address.substr(host.size() + (host.size() == address.size() ? 0 : 1));
MONERO_CHECK(host_check(host));
std::uint16_t porti = default_port;
if (!port.empty() && !epee::string_tools::get_xtype_from_string(porti, std::string{port}))
return {net::error::invalid_port};
static_assert(b32_length + sizeof(tld) == sizeof(i2p_address::host_), "bad internal host size");
return i2p_address{host, porti};
}
bool i2p_address::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent)
{
i2p_serialized in{};
if (in._load(src, hparent) && in.host.size() < sizeof(host_) && (in.host == unknown_host || !host_check(in.host).has_error()))
{
std::memcpy(host_, in.host.data(), in.host.size());
std::memset(host_ + in.host.size(), 0, sizeof(host_) - in.host.size());
port_ = in.port;
return true;
}
static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
std::memcpy(host_, unknown_host, sizeof(unknown_host)); // include null terminator
port_ = 0;
return false;
}
bool i2p_address::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const
{
const i2p_serialized out{std::string{host_}, port_};
return out.store(dest, hparent);
}
i2p_address::i2p_address(const i2p_address& rhs) noexcept
: port_(rhs.port_)
{
std::memcpy(host_, rhs.host_, sizeof(host_));
}
i2p_address& i2p_address::operator=(const i2p_address& rhs) noexcept
{
if (this != std::addressof(rhs))
{
port_ = rhs.port_;
std::memcpy(host_, rhs.host_, sizeof(host_));
}
return *this;
}
bool i2p_address::is_unknown() const noexcept
{
static_assert(1 <= sizeof(host_), "host size too small");
return host_[0] == '<'; // character is not allowed otherwise
}
bool i2p_address::equal(const i2p_address& rhs) const noexcept
{
return port_ == rhs.port_ && is_same_host(rhs);
}
bool i2p_address::less(const i2p_address& rhs) const noexcept
{
return std::strcmp(host_str(), rhs.host_str()) < 0 || port() < rhs.port();
}
bool i2p_address::is_same_host(const i2p_address& rhs) const noexcept
{
return std::strcmp(host_str(), rhs.host_str()) == 0;
}
std::string i2p_address::str() const
{
const std::size_t host_length = std::strlen(host_str());
const std::size_t port_length =
port_ == 0 ? 0 : std::numeric_limits<std::uint16_t>::digits10 + 2;
std::string out{};
out.reserve(host_length + port_length);
out.assign(host_str(), host_length);
if (port_ != 0)
{
out.push_back(':');
namespace karma = boost::spirit::karma;
karma::generate(std::back_inserter(out), karma::ushort_, port());
}
return out;
}
}
+140
View File
@@ -0,0 +1,140 @@
// Copyright (c) 2019, 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.
#pragma once
#include <boost/utility/string_ref.hpp>
#include <cstdint>
#include <string>
#include "common/expect.h"
#include "net/enums.h"
#include "net/error.h"
namespace epee
{
namespace serialization
{
class portable_storage;
struct section;
}
}
namespace net
{
//! b32 i2p address; internal format not condensed/decoded.
class i2p_address
{
std::uint16_t port_;
char host_[61]; // null-terminated
//! Keep in private, `host.size()` has no runtime check
i2p_address(boost::string_ref host, std::uint16_t port) noexcept;
public:
//! \return Size of internal buffer for host.
static constexpr std::size_t buffer_size() noexcept { return sizeof(host_); }
//! \return `<unknown tor host>`.
static const char* unknown_str() noexcept;
//! An object with `port() == 0` and `host_str() == unknown_str()`.
i2p_address() noexcept;
//! \return A default constructed `i2p_address` object.
static i2p_address unknown() noexcept { return i2p_address{}; }
/*!
Parse `address` in b32 i2p format (i.e. x.b32.i2p:80)
with `default_port` being used if port is not specified in
`address`.
*/
static expect<i2p_address> make(boost::string_ref address, std::uint16_t default_port = 0);
//! Load from epee p2p format, and \return false if not valid tor address
bool _load(epee::serialization::portable_storage& src, epee::serialization::section* hparent);
//! Store in epee p2p format
bool store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const;
// Moves and copies are currently identical
i2p_address(const i2p_address& rhs) noexcept;
~i2p_address() = default;
i2p_address& operator=(const i2p_address& rhs) noexcept;
//! \return True if default constructed or via `unknown()`.
bool is_unknown() const noexcept;
bool equal(const i2p_address& rhs) const noexcept;
bool less(const i2p_address& rhs) const noexcept;
//! \return True if i2p addresses are identical.
bool is_same_host(const i2p_address& rhs) const noexcept;
//! \return `x.b32.i2p` or `x.b32.i2p:z` if `port() != 0`.
std::string str() const;
//! \return Null-terminated `x.b32.i2p` value or `unknown_str()`.
const char* host_str() const noexcept { return host_; }
//! \return Port value or `0` if unspecified.
std::uint16_t port() const noexcept { return port_; }
static constexpr bool is_loopback() noexcept { return false; }
static constexpr bool is_local() noexcept { return false; }
static constexpr epee::net_utils::address_type get_type_id() noexcept
{
return epee::net_utils::address_type::i2p;
}
static constexpr epee::net_utils::zone get_zone() noexcept
{
return epee::net_utils::zone::i2p;
}
//! \return `!is_unknown()`.
bool is_blockable() const noexcept { return !is_unknown(); }
};
inline bool operator==(const i2p_address& lhs, const i2p_address& rhs) noexcept
{
return lhs.equal(rhs);
}
inline bool operator!=(const i2p_address& lhs, const i2p_address& rhs) noexcept
{
return !lhs.equal(rhs);
}
inline bool operator<(const i2p_address& lhs, const i2p_address& rhs) noexcept
{
return lhs.less(rhs);
}
} // net
+2 -1
View File
@@ -29,6 +29,7 @@
#include "parse.h"
#include "net/tor_address.h"
#include "net/i2p_address.h"
#include "string_tools.h"
namespace net
@@ -43,7 +44,7 @@ namespace net
if (host.ends_with(".onion"))
return tor_address::make(address, default_port);
if (host.ends_with(".i2p"))
return make_error_code(net::error::invalid_i2p_address); // not yet implemented (prevent public DNS lookup)
return i2p_address::make(address, default_port);
std::uint16_t port = default_port;
if (host.size() < address.size())
+2 -2
View File
@@ -37,11 +37,11 @@
namespace net
{
/*!
Identifies onion and IPv4 addresses and returns them as a generic
Identifies onion, i2p and IPv4 addresses and returns them as a generic
`network_address`. If the type is unsupported, it might be a hostname,
and `error() == net::error::kUnsupportedAddress` is returned.
\param address An onion address, ipv4 address or hostname. Hostname
\param address An onion address, i2p address, ipv4 address or hostname. Hostname
will return an error.
\param default_port If `address` does not specify a port, this value
will be used.
+8
View File
@@ -40,6 +40,7 @@
#include "net/net_utils_base.h"
#include "net/tor_address.h"
#include "net/i2p_address.h"
namespace net
{
@@ -273,6 +274,13 @@ namespace socks
return false;
}
bool client::set_connect_command(const net::i2p_address& address)
{
if (!address.is_unknown())
return set_connect_command(address.host_str(), address.port());
return false;
}
bool client::set_resolve_command(boost::string_ref domain)
{
if (socks_version() != version::v4a_tor)
+3
View File
@@ -155,6 +155,9 @@ namespace socks
//! Try to set `address` as remote Tor hidden service connection request.
bool set_connect_command(const net::tor_address& address);
//! Try to set `address` as remote i2p hidden service connection request.
bool set_connect_command(const net::i2p_address& address);
//! Try to set `domain` as remote DNS A record lookup request.
bool set_resolve_command(boost::string_ref domain);
+13 -2
View File
@@ -46,13 +46,14 @@
#include "net/socks.h"
#include "net/parse.h"
#include "net/tor_address.h"
#include "net/i2p_address.h"
#include "p2p/p2p_protocol_defs.h"
#include "string_tools.h"
namespace
{
constexpr const boost::chrono::milliseconds future_poll_interval{500};
constexpr const std::chrono::seconds tor_connect_timeout{P2P_DEFAULT_TOR_CONNECT_TIMEOUT};
constexpr const std::chrono::seconds socks_connect_timeout{P2P_DEFAULT_SOCKS_CONNECT_TIMEOUT};
std::int64_t get_max_connections(const boost::iterator_range<boost::string_ref::const_iterator> value) noexcept
{
@@ -90,6 +91,9 @@ namespace
case net::tor_address::get_type_id():
set = client->set_connect_command(remote.as<net::tor_address>());
break;
case net::i2p_address::get_type_id():
set = client->set_connect_command(remote.as<net::i2p_address>());
break;
default:
MERROR("Unsupported network address in socks_connect");
return false;
@@ -177,6 +181,9 @@ namespace nodetool
case epee::net_utils::zone::tor:
proxies.back().zone = epee::net_utils::zone::tor;
break;
case epee::net_utils::zone::i2p:
proxies.back().zone = epee::net_utils::zone::i2p;
break;
default:
MERROR("Invalid network for --" << arg_proxy.name);
return boost::none;
@@ -235,6 +242,10 @@ namespace nodetool
inbounds.back().our_address = std::move(*our_address);
inbounds.back().default_remote = net::tor_address::unknown();
break;
case net::i2p_address::get_type_id():
inbounds.back().our_address = std::move(*our_address);
inbounds.back().default_remote = net::i2p_address::unknown();
break;
default:
MERROR("Invalid inbound address (" << address << ") for --" << arg_anonymous_inbound.name << ": " << (our_address ? "invalid type" : our_address.error().message()));
return boost::none;
@@ -308,7 +319,7 @@ namespace nodetool
const auto start = std::chrono::steady_clock::now();
while (socks_result.wait_for(future_poll_interval) == boost::future_status::timeout)
{
if (tor_connect_timeout < std::chrono::steady_clock::now() - start)
if (socks_connect_timeout < std::chrono::steady_clock::now() - start)
{
MERROR("Timeout on socks connect (" << proxy << " to " << remote.str() << ")");
return boost::none;
+46 -1
View File
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2018, The Monero Project
// Copyright (c) 2014-2018, The Monero Project
//
// All rights reserved.
//
@@ -35,6 +35,7 @@
#include "common/expect.h"
#include "net/net_utils_base.h"
#include "net/tor_address.h"
#include "net/i2p_address.h"
#include "p2p/p2p_protocol_defs.h"
#ifdef CRYPTONOTE_PRUNING_DEBUG_SPOOF_SEED
@@ -76,6 +77,9 @@ namespace boost
case net::tor_address::get_type_id():
do_serialize<net::tor_address>(is_saving, a, na);
break;
case net::i2p_address::get_type_id():
do_serialize<net::i2p_address>(is_saving, a, na);
break;
case epee::net_utils::address_type::invalid:
default:
throw std::runtime_error("Unsupported network address type");
@@ -106,6 +110,20 @@ namespace boost
a.save_binary(na.host_str(), length);
}
template <class Archive, class ver_type>
inline void save(Archive& a, const net::i2p_address& na, const ver_type)
{
const size_t length = std::strlen(na.host_str());
if (length > 255)
MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
const uint16_t port{na.port()};
const uint8_t len = length;
a & port;
a & len;
a.save_binary(na.host_str(), length);
}
template <class Archive, class ver_type>
inline void load(Archive& a, net::tor_address& na, const ver_type)
{
@@ -127,12 +145,39 @@ namespace boost
na = MONERO_UNWRAP(net::tor_address::make(host, port));
}
template <class Archive, class ver_type>
inline void load(Archive& a, net::i2p_address& na, const ver_type)
{
uint16_t port = 0;
uint8_t length = 0;
a & port;
a & length;
if (length > net::i2p_address::buffer_size())
MONERO_THROW(net::error::invalid_i2p_address, "i2p address too long");
char host[net::i2p_address::buffer_size()] = {0};
a.load_binary(host, length);
host[sizeof(host) - 1] = 0;
if (std::strcmp(host, net::i2p_address::unknown_str()) == 0)
na = net::i2p_address::unknown();
else
na = MONERO_UNWRAP(net::i2p_address::make(host, port));
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, net::tor_address& na, const ver_type ver)
{
boost::serialization::split_free(a, na, ver);
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, net::i2p_address& na, const ver_type ver)
{
boost::serialization::split_free(a, na, ver);
}
template <class Archive, class ver_type>
inline void serialize(Archive &a, nodetool::peerlist_entry& pl, const ver_type ver)
{
+1
View File
@@ -35,6 +35,7 @@
#include "serialization/keyvalue_serialization.h"
#include "net/net_utils_base.h"
#include "net/tor_address.h" // needed for serialization
#include "net/i2p_address.h" // needed for serialization
#include "misc_language.h"
#include "string_tools.h"
#include "time_helper.h"