From 123fc2a25afaebe89b94c9806eb05033e021d807 Mon Sep 17 00:00:00 2001 From: Jethro Grassie Date: Mon, 21 Jan 2019 11:50:03 -0500 Subject: [PATCH] i2p: initial support --- ANONYMITY_NETWORKS.md | 57 +++--- contrib/epee/include/net/net_utils_base.h | 5 +- src/cryptonote_config.h | 2 +- src/net/CMakeLists.txt | 4 +- src/net/fwd.h | 1 + src/net/i2p_address.cpp | 200 +++++++++++++++++++++ src/net/i2p_address.h | 140 +++++++++++++++ src/net/parse.cpp | 3 +- src/net/parse.h | 4 +- src/net/socks.cpp | 8 + src/net/socks.h | 3 + src/p2p/net_node.cpp | 15 +- src/p2p/net_peerlist_boost_serialization.h | 47 ++++- src/p2p/p2p_protocol_defs.h | 1 + 14 files changed, 450 insertions(+), 40 deletions(-) create mode 100644 src/net/i2p_address.cpp create mode 100644 src/net/i2p_address.h diff --git a/ANONYMITY_NETWORKS.md b/ANONYMITY_NETWORKS.md index 6ac8cd9994..a5f18010e5 100644 --- a/ANONYMITY_NETWORKS.md +++ b/ANONYMITY_NETWORKS.md @@ -1,7 +1,6 @@ # Anonymity Networks with Monero -Currently only Tor has been integrated into Monero. Providing support for -Kovri/I2P should be minimal, but has not yet been attempted. The usage of +Currently only Tor and I2P have been integrated into Monero. The usage of these networks is still considered experimental - there are a few pessimistic cases where privacy is leaked. The design is intended to maximize privacy of the source of a transaction by broadcasting it over an anonymity network, while @@ -23,8 +22,8 @@ connections enabled. ## P2P Commands -Only handshakes, peer timed syncs, and transaction broadcast messages are -supported over anonymity networks. If one `--add-exclusive-node` onion address +Only handshakes, peer timed syncs and transaction broadcast messages are +supported over anonymity networks. If one `--add-exclusive-node` p2p address is specified, then no syncing will take place and only transaction broadcasting can occur. It is therefore recommended that `--add-exclusive-node` be combined with additional exclusive IPv4 address(es). @@ -47,9 +46,9 @@ separate process. On most systems the configuration will look like: which tells `monerod` that ".onion" p2p addresses can be forwarded to a socks proxy at IP 127.0.0.1 port 9050 with a max of 10 outgoing connections and -".i2p" p2p addresses can be forwarded to a socks proxy at IP 127.0.0.1 port 9000 -with the default max outgoing connections. Since there are no seed nodes for -anonymity connections, peers must be manually specified: +".b32.i2p" p2p addresses can be forwarded to a socks proxy at IP 127.0.0.1 port +9000 with the default max outgoing connections. Since there are no seed nodes +for anonymity connections, peers must be manually specified: > `--add-exclusive-node rveahdfho7wo4b2m.onion:28083` > `--add-peer rveahdfho7wo4b2m.onion:28083` @@ -64,27 +63,28 @@ Receiving anonymity connections is done through the option `--anonymous-inbound`. This option tells `monerod` the inbound address, network type, and max connections: -> `--anonymous-inbound rveahdfho7wo4b2m.onion:28083,127.0.0.28083,25` -> `--anonymous-inbound foobar.i2p:5000,127.0.0.1:30000` +> `--anonymous-inbound rveahdfho7wo4b2m.onion:28083,127.0.0.1:28083,25` +> `--anonymous-inbound cmeua5767mz2q5jsaelk2rxhf67agrwuetaso5dzbenyzwlbkg2q.b32.i2p:5000,127.0.0.1:30000` which tells `monerod` that a max of 25 inbound Tor connections are being received at address "rveahdfho7wo4b2m.onion:28083" and forwarded to `monerod` localhost port 28083, and a default max I2P connections are being received at -address "foobar.i2p:5000" and forwarded to `monerod` localhost port 30000. +address "cmeua5767mz2q5jsaelk2rxhf67agrwuetaso5dzbenyzwlbkg2q.b32.i2p:5000" and +forwarded to `monerod` localhost port 30000. These addresses will be shared with outgoing peers, over the same network type, otherwise the peer will not be notified of the peer address by the proxy. ### Network Types -#### Tor +#### Tor & I2P -Options `--add-exclusive-node` and `--add-peer` recognize ".onion" addresses, -and will properly forward those addresses to the proxy provided with -`--proxy tor,...`. +Options `--add-exclusive-node` and `--add-peer` recognize ".onion" and +".b32.i2p" addresses, and will properly forward those addresses to the proxy +provided with `--proxy tor,...` or `--proxy i2p,...`. -Option `--anonymous-inbound` also recognizes ".onion" addresses, and will -automatically be sent out to outgoing Tor connections so the peer can -distribute the address to its other peers. +Option `--anonymous-inbound` also recognizes ".onion" and ".b32.i2p" addresses, +and will automatically be sent out to outgoing Tor/I2P connections so the peer +can distribute the address to its other peers. ##### Configuration @@ -99,11 +99,8 @@ This will store key information in `/var/lib/tor/data/monero` and will forward `/usr/lib/tor/data/monero/hostname` will contain the ".onion" address for use with `--anonymous-inbound`. -#### Kovri/I2P - -Support for this network has not been implemented. Using ".i2p" addresses or -specifying "i2p" will currently generate an error. - +I2P must be configured with a standard server tunnel. Configuration differs by +I2P implementation. ## Privacy Limitations @@ -132,11 +129,11 @@ more difficult. ### Bandwidth Usage An ISP can passively monitor `monerod` connections from a node and observe when -a transaction is sent over a Tor/Kovri connection via timing analysis + size of -data sent during that timeframe. Kovri should provide better protection against +a transaction is sent over a Tor/I2P connection via timing analysis + size of +data sent during that timeframe. I2P should provide better protection against this attack - its connections are not circuit based. However, if a node is -only using Kovri for broadcasting Monero transactions, the total aggregate of -Kovri/I2P data would also leak information. +only using I2P for broadcasting Monero transactions, the total aggregate of +I2P data would also leak information. #### Mitigation @@ -165,15 +162,15 @@ simply a best effort attempt. ### Active Bandwidth Shaping An attacker could attempt to bandwidth shape traffic in an attempt to determine -the source of a Tor/Kovri/I2P connection. There isn't great mitigation against -this, but Kovri/I2P should provide better protection against this attack since +the source of a Tor/I2P connection. There isn't great mitigation against +this, but I2P should provide better protection against this attack since the connections are not circuit based. #### Mitigation -The best mitigiation is to use Kovri/I2P instead of Tor. However, Kovri/I2P +The best mitigiation is to use I2P instead of Tor. However, I2P has a smaller set of users (less cover traffic) and academic reviews, so there is a tradeoff in potential isses. Also, anyone attempting this strategy really wants to uncover a user, it seems unlikely that this would be performed against -every Tor/Kovri/I2P user. +every Tor/I2P user. diff --git a/contrib/epee/include/net/net_utils_base.h b/contrib/epee/include/net/net_utils_base.h index 82f8a7fe81..ffd106e102 100644 --- a/contrib/epee/include/net/net_utils_base.h +++ b/contrib/epee/include/net/net_utils_base.h @@ -47,6 +47,7 @@ namespace net { class tor_address; + class i2p_address; } namespace epee @@ -196,7 +197,7 @@ namespace net_utils template const Type &as() const { return as_mutable(); } BEGIN_KV_SERIALIZE_MAP() - // need to `#include "net/tor_address.h"` when serializing `network_address` + // need to `#include "net/[i2p|tor]_address.h"` when serializing `network_address` static constexpr std::integral_constant is_store_{}; std::uint8_t type = std::uint8_t(is_store ? this_ref.get_type_id() : address_type::invalid); @@ -209,6 +210,8 @@ namespace net_utils return this_ref.template serialize_addr(is_store_, stg, hparent_section); case address_type::tor: return this_ref.template serialize_addr(is_store_, stg, hparent_section); + case address_type::i2p: + return this_ref.template serialize_addr(is_store_, stg, hparent_section); case address_type::invalid: default: break; diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index 93db71705c..716825a2df 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -108,7 +108,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 diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt index a813721257..fdb988f392 100644 --- a/src/net/CMakeLists.txt +++ b/src/net/CMakeLists.txt @@ -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}) diff --git a/src/net/fwd.h b/src/net/fwd.h index ee7c539b06..7cae882513 100644 --- a/src/net/fwd.h +++ b/src/net/fwd.h @@ -34,6 +34,7 @@ namespace net { enum class error : int; class tor_address; + class i2p_address; namespace socks { diff --git a/src/net/i2p_address.cpp b/src/net/i2p_address.cpp new file mode 100644 index 0000000000..cba829d3fd --- /dev/null +++ b/src/net/i2p_address.cpp @@ -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 +#include +#include +#include +#include +#include + +#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[] = ""; + + constexpr const unsigned b32_length = 52; + + constexpr const char base32_alphabet[] = + u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz234567"; + + expect 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::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::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; + } +} diff --git a/src/net/i2p_address.h b/src/net/i2p_address.h new file mode 100644 index 0000000000..28a1118ba5 --- /dev/null +++ b/src/net/i2p_address.h @@ -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 +#include +#include + +#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 ``. + 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 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 diff --git a/src/net/parse.cpp b/src/net/parse.cpp index ebf91eeff5..eaaadb67ef 100644 --- a/src/net/parse.cpp +++ b/src/net/parse.cpp @@ -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()) diff --git a/src/net/parse.h b/src/net/parse.h index 9195ddc2b9..5804c41285 100644 --- a/src/net/parse.h +++ b/src/net/parse.h @@ -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. diff --git a/src/net/socks.cpp b/src/net/socks.cpp index f31efc8c1f..53154369bd 100644 --- a/src/net/socks.cpp +++ b/src/net/socks.cpp @@ -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) diff --git a/src/net/socks.h b/src/net/socks.h index d29a51ccb6..8259377924 100644 --- a/src/net/socks.h +++ b/src/net/socks.h @@ -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); diff --git a/src/p2p/net_node.cpp b/src/p2p/net_node.cpp index 8639fdb3b9..2f0678913d 100644 --- a/src/p2p/net_node.cpp +++ b/src/p2p/net_node.cpp @@ -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 value) noexcept { @@ -90,6 +91,9 @@ namespace case net::tor_address::get_type_id(): set = client->set_connect_command(remote.as()); break; + case net::i2p_address::get_type_id(): + set = client->set_connect_command(remote.as()); + 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; diff --git a/src/p2p/net_peerlist_boost_serialization.h b/src/p2p/net_peerlist_boost_serialization.h index d2e9efa3dd..6c891581fb 100644 --- a/src/p2p/net_peerlist_boost_serialization.h +++ b/src/p2p/net_peerlist_boost_serialization.h @@ -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(is_saving, a, na); break; + case net::i2p_address::get_type_id(): + do_serialize(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 + 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 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 + 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 inline void serialize(Archive &a, net::tor_address& na, const ver_type ver) { boost::serialization::split_free(a, na, ver); } + template + inline void serialize(Archive &a, net::i2p_address& na, const ver_type ver) + { + boost::serialization::split_free(a, na, ver); + } + template inline void serialize(Archive &a, nodetool::peerlist_entry& pl, const ver_type ver) { diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h index 6e5e450085..e9449b9509 100644 --- a/src/p2p/p2p_protocol_defs.h +++ b/src/p2p/p2p_protocol_defs.h @@ -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"