From 98a35823d3f7741c5c92c6811c6611b104884a1b Mon Sep 17 00:00:00 2001 From: anonimal Date: Sat, 18 Nov 2017 00:49:35 +0000 Subject: [PATCH] Core: rewrite RI address add'er Note: template function was not needed afterall. --- src/core/router/context.cc | 26 +++++++------ src/core/router/info.cc | 78 +++++++++++++++++++++++--------------- src/core/router/info.h | 23 +++++------ src/util/routerinfo.cc | 7 +++- src/util/routerinfo.h | 4 +- 5 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/core/router/context.cc b/src/core/router/context.cc index b6dcdf15..7b1481df 100644 --- a/src/core/router/context.cc +++ b/src/core/router/context.cc @@ -36,6 +36,7 @@ #include #include +#include #include "core/router/i2np.h" #include "core/router/info.h" @@ -125,13 +126,14 @@ void RouterContext::Initialize(const boost::program_options::variables_map& map) // NTCP if (has_ntcp && !router.GetNTCPAddress()) - router.AddNTCPAddress(host, port); + router.AddAddress(std::make_tuple(Transport::NTCP, host, port)); if (!has_ntcp) RemoveTransport(core::RouterInfo::Transport::NTCP); // SSU if (has_ssu && !router.GetSSUAddress()) - router.AddSSUAddress(host, port, router.GetIdentHash()); + router.AddAddress( + std::make_tuple(Transport::SSU, host, port), router.GetIdentHash()); if (!has_ssu) { RemoveTransport(core::RouterInfo::Transport::SSU); @@ -276,9 +278,11 @@ void RouterContext::SetReachable() { for (std::size_t i = 0; i < addresses.size(); i++) { if (addresses[i].transport == core::RouterInfo::Transport::SSU) { // insert NTCP address with host/port form SSU - m_RouterInfo.AddNTCPAddress( // TODO(anonimal): but if NTCP is disabled?... - addresses[i].host.to_string().c_str(), - addresses[i].port); + m_RouterInfo.AddAddress( // TODO(anonimal): but if NTCP is disabled?... + std::make_tuple( + Transport::NTCP, + addresses[i].host.to_string(), + addresses[i].port)); break; } } @@ -311,14 +315,12 @@ void RouterContext::UpdateNTCPV6Address( } if (!found) { // create new address - m_RouterInfo.AddNTCPAddress( - host.to_string().c_str(), - port); - m_RouterInfo.AddSSUAddress( - host.to_string().c_str(), - port, + m_RouterInfo.AddAddress( + std::make_tuple(Transport::NTCP, host.to_string(), port)); + m_RouterInfo.AddAddress( + std::make_tuple(Transport::SSU, host.to_string(), port), GetIdentHash(), - kovri::core::GetMTU(host)); + core::GetMTU(host)); updated = true; } if (updated) diff --git a/src/core/router/info.cc b/src/core/router/info.cc index 1e9f00da..c50cf95a 100644 --- a/src/core/router/info.cc +++ b/src/core/router/info.cc @@ -36,6 +36,7 @@ #include #include +#include #include "core/router/context.h" @@ -76,11 +77,12 @@ RouterInfo::RouterInfo( // Set default transports if (has_transport.first) - AddNTCPAddress(point.first, point.second); + AddAddress(std::make_tuple(Transport::NTCP, point.first, point.second)); if (has_transport.second) { - AddSSUAddress(point.first, point.second, hash); + AddAddress( + std::make_tuple(Transport::SSU, point.first, point.second), hash); SetCaps( m_Caps | core::RouterInfo::Cap::SSUTesting | core::RouterInfo::Cap::SSUIntroducer); @@ -524,39 +526,53 @@ void RouterInfo::SetRouterIdentity(const IdentityEx& identity) m_Timestamp = kovri::core::GetMillisecondsSinceEpoch(); } -void RouterInfo::AddNTCPAddress(const std::string& host, std::uint16_t port) -{ - Address addr; - addr.host = boost::asio::ip::address::from_string(host); - addr.port = port; - addr.transport = Transport::NTCP; - addr.cost = Size::NTCPCost; - addr.date = 0; - addr.mtu = 0; - m_Addresses.push_back(addr); - m_SupportedTransports |= addr.host.is_v6() ? SupportedTransport::NTCPv6 - : SupportedTransport::NTCPv4; -} - -void RouterInfo::AddSSUAddress( - const std::string& host, - std::uint16_t port, +void RouterInfo::AddAddress( + const std::tuple& point, const std::uint8_t* key, - std::uint16_t mtu) + const std::uint16_t mtu) { Address addr; - addr.host = boost::asio::ip::address::from_string(host); - addr.port = port; - addr.transport = Transport::SSU; - addr.cost = Size::SSUCost; - addr.date = 0; - addr.mtu = mtu; - std::memcpy(addr.key, key, 32); + addr.transport = std::get<0>(point); + addr.host = boost::asio::ip::address::from_string(std::get<1>(point)); + addr.port = std::get<2>(point); + addr.date = 0; // TODO(anonimal): ?... + + // Set transport-specific + switch (addr.transport) + { + case Transport::NTCP: + { + addr.cost = Size::NTCPCost; + addr.mtu = 0; // TODO(anonimal): ?... + m_SupportedTransports |= addr.host.is_v6() + ? SupportedTransport::NTCPv6 + : SupportedTransport::NTCPv4; + } + break; + + case Transport::SSU: + { + addr.cost = Size::SSUCost; + addr.mtu = mtu; + if (!key) + throw std::runtime_error("RouterInfo: null SSU intro key"); + addr.key = key; + m_SupportedTransports |= addr.host.is_v6() + ? SupportedTransport::SSUv6 + : SupportedTransport::SSUv4; + // Set our caps + m_Caps |= Cap::SSUTesting | Cap::SSUIntroducer; + } + break; + + default: + throw std::runtime_error( + "RouterInfo: " + std::string(__func__) + ": unsupported transport"); + break; + } + + // Save address m_Addresses.push_back(addr); - m_SupportedTransports |= - addr.host.is_v6() ? SupportedTransport::SSUv6 : SupportedTransport::SSUv4; - m_Caps |= Cap::SSUTesting; - m_Caps |= Cap::SSUIntroducer; } bool RouterInfo::AddIntroducer(const Address* address, std::uint32_t tag) diff --git a/src/core/router/info.h b/src/core/router/info.h index 194e0632..b6058847 100644 --- a/src/core/router/info.h +++ b/src/core/router/info.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include "core/crypto/signature.h" @@ -487,20 +488,14 @@ class RouterInfo : public RouterInfoTraits, public RoutingDestination /// @brief Set RI identity and current timestamp void SetRouterIdentity(const IdentityEx& identity); - - // TODO(anonimal): template address adder - - /// @brief Adds SSU address to RI - /// @details Sets RI members appropriately, saves address object - void AddNTCPAddress(const std::string& host, std::uint16_t port); - - /// @brief Adds SSU address to RI - /// @details Sets RI members appropriately, saves address object - void AddSSUAddress( - const std::string& host, - std::uint16_t port, - const std::uint8_t* key, - std::uint16_t mtu = 0); + /// @brief Adds/saves address + sets appropriate RI members + /// @param point Supported transport / Host string / Port integral + /// @param key Our intoducer key + /// @param mtu Address MTU + void AddAddress( + const std::tuple& point, + const std::uint8_t* key = nullptr, + const std::uint16_t mtu = 0); /// @brief Adds introducer to RI using SSU capable address object /// @param address SSU capable address diff --git a/src/util/routerinfo.cc b/src/util/routerinfo.cc index d028e6d6..214bbbac 100644 --- a/src/util/routerinfo.cc +++ b/src/util/routerinfo.cc @@ -31,6 +31,7 @@ #include "util/routerinfo.h" #include #include +#include #include "core/crypto/rand.h" #include "core/router/info.h" @@ -124,8 +125,10 @@ bool RouterInfoCommand::Impl( // Set router info attributes core::RouterInfo routerInfo; routerInfo.SetRouterIdentity(keys.GetPublic()); - routerInfo.AddSSUAddress(host, port, routerInfo.GetIdentHash()); - routerInfo.AddNTCPAddress(host, port); + routerInfo.AddAddress(std::make_tuple(Transport::NTCP, host, port)); + routerInfo.AddAddress( + std::make_tuple(Transport::SSU, host, port), + routerInfo.GetIdentHash()); // Set capabilities routerInfo.SetCaps(core::RouterInfo::Cap::Reachable); if (vm["ssuintroducer"].as()) diff --git a/src/util/routerinfo.h b/src/util/routerinfo.h index 5527c679..60396d8f 100644 --- a/src/util/routerinfo.h +++ b/src/util/routerinfo.h @@ -37,12 +37,14 @@ #include #include "util/command.h" +#include "core/router/info.h" + /** * @class RouterInfoCommand * @brief base class for routerinfo */ -class RouterInfoCommand : public Command +class RouterInfoCommand : public Command, public kovri::core::RouterInfoTraits { public: RouterInfoCommand();