From 41b9f19b019fb8223975d4b6afe0b54ac7b19c25 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 4 Aug 2018 08:47:58 -0400 Subject: [PATCH] get unpublished NTCP2 address --- libi2pd/NTCP2.cpp | 4 ++-- libi2pd/RouterInfo.cpp | 50 ++++++++++++++++++++---------------------- libi2pd/RouterInfo.h | 5 +++-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 35100ef6..0d66f08f 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -149,7 +149,7 @@ namespace transport m_Establisher.reset (new NTCP2Establisher); if (in_RemoteRouter) // Alice { - auto addr = in_RemoteRouter->GetNTCP2Address (); + auto addr = in_RemoteRouter->GetNTCP2Address (true); // we need a published address if (addr) { memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32); @@ -580,7 +580,7 @@ namespace transport SendTerminationAndTerminate (eNTCP2RouterInfoSignatureVerificationFail); return; } - auto addr = ri.GetNTCP2Address (); + auto addr = ri.GetNTCP2Address (false); // any NTCP2 address if (!addr) { LogPrint (eLogError, "NTCP2: No NTCP2 address found in SessionConfirmed"); diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index 2151ef3d..21f7132f 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -860,20 +860,33 @@ namespace data std::shared_ptr RouterInfo::GetNTCPAddress (bool v4only) const { - return GetAddress (eTransportNTCP, v4only); + return GetAddress ( + [v4only](std::shared_ptr address)->bool + { + return (address->transportStyle == eTransportNTCP) && (!v4only || address->host.is_v4 ()); + }); } std::shared_ptr RouterInfo::GetSSUAddress (bool v4only) const { - return GetAddress (eTransportSSU, v4only); + return GetAddress ( + [v4only](std::shared_ptr address)->bool + { + return (address->transportStyle == eTransportSSU) && (!v4only || address->host.is_v4 ()); + }); } std::shared_ptr RouterInfo::GetSSUV6Address () const { - return GetAddress (eTransportSSU, false, true); + return GetAddress ( + [](std::shared_ptr address)->bool + { + return (address->transportStyle == eTransportSSU) && address->host.is_v6 (); + }); } - std::shared_ptr RouterInfo::GetAddress (TransportStyle s, bool v4only, bool v6only) const + template + std::shared_ptr RouterInfo::GetAddress (Filter filter) const { // TODO: make it more gereric using comparator #if (BOOST_VERSION >= 105300) @@ -882,33 +895,18 @@ namespace data auto addresses = m_Addresses; #endif for (const auto& address : *addresses) - { - if (address->transportStyle == s) - { - if ((!v4only || address->host.is_v4 ()) && (!v6only || address->host.is_v6 ())) - return address; - } - } + if (filter (address)) return address; + return nullptr; } - std::shared_ptr RouterInfo::GetNTCP2Address (bool v4only) const + std::shared_ptr RouterInfo::GetNTCP2Address (bool publishedOnly, bool v4only) const { - // TODO: implement through GetAddress -#if (BOOST_VERSION >= 105300) - auto addresses = boost::atomic_load (&m_Addresses); -#else - auto addresses = m_Addresses; -#endif - for (const auto& address : *addresses) - { - if (address->IsPublishedNTCP2 ()) + return GetAddress ( + [publishedOnly, v4only](std::shared_ptr address)->bool { - if (!v4only || address->host.is_v4 ()) - return address; - } - } - return nullptr; + return address->IsNTCP2 () && (!publishedOnly || address->IsPublishedNTCP2 ()) && (!v4only || address->host.is_v4 ()); + }); } std::shared_ptr RouterInfo::GetProfile () const diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index 525c6e65..f95659d7 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -142,7 +142,7 @@ namespace data uint64_t GetTimestamp () const { return m_Timestamp; }; Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr std::shared_ptr GetNTCPAddress (bool v4only = true) const; - std::shared_ptr GetNTCP2Address (bool v4only = true) const; + std::shared_ptr GetNTCP2Address (bool publishedOnly, bool v4only = true) const; std::shared_ptr GetSSUAddress (bool v4only = true) const; std::shared_ptr GetSSUV6Address () const; @@ -216,7 +216,8 @@ namespace data size_t ReadString (char* str, size_t len, std::istream& s) const; void WriteString (const std::string& str, std::ostream& s) const; void ExtractCaps (const char * value); - std::shared_ptr GetAddress (TransportStyle s, bool v4only, bool v6only = false) const; + template + std::shared_ptr GetAddress (Filter filter) const; void UpdateCapsProperty (); private: