diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index dd777794..61e766ea 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -333,8 +333,6 @@ namespace transport if (in_RemoteRouter) // Alice { m_Establisher->m_RemoteIdentHash = GetRemoteIdentity ()->GetIdentHash (); - if (!addr) - addr = in_RemoteRouter->GetNTCP2Address (true); // we need a published address if (addr) { memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32); @@ -342,7 +340,7 @@ namespace transport m_RemoteEndpoint = boost::asio::ip::tcp::endpoint (addr->host, addr->port); } else - LogPrint (eLogWarning, "NTCP2: Missing NTCP2 parameters"); + LogPrint (eLogWarning, "NTCP2: Missing NTCP2 address"); } m_NextRouterInfoResendTime = i2p::util::GetSecondsSinceEpoch () + NTCP2_ROUTERINFO_RESEND_INTERVAL + rand ()%NTCP2_ROUTERINFO_RESEND_INTERVAL_THRESHOLD; @@ -658,19 +656,13 @@ namespace transport SendTerminationAndTerminate (eNTCP2Message3Error); return; } - auto addr = ri.GetNTCP2Address (false); // any NTCP2 address + auto addr = ri.GetNTCP2AddressWithStaticKey (m_Establisher->m_RemoteStaticKey); if (!addr) { - LogPrint (eLogError, "NTCP2: No NTCP2 address found in SessionConfirmed"); + LogPrint (eLogError, "NTCP2: No NTCP2 address wth static key found in SessionConfirmed"); Terminate (); return; } - if (memcmp (addr->ntcp2->staticKey, m_Establisher->m_RemoteStaticKey, 32)) - { - LogPrint (eLogError, "NTCP2: Static key mismatch in SessionConfirmed"); - SendTerminationAndTerminate (eNTCP2IncorrectSParameter); - return; - } i2p::data::netdb.PostI2NPMsg (CreateI2NPMessage (eI2NPDummyMsg, buf.data () + 3, size)); // TODO: should insert ri and not parse it twice // TODO: process options @@ -1296,8 +1288,6 @@ namespace transport if (ecode != boost::asio::error::operation_aborted) { LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds"); - if (conn->GetRemoteIdentity ()) - i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true); conn->Terminate (); } }); @@ -1426,6 +1416,11 @@ namespace transport void NTCP2Server::ConnectWithProxy (std::shared_ptr conn) { if(!m_ProxyEndpoint) return; + if (!conn || conn->GetRemoteEndpoint ().address ().is_unspecified ()) + { + LogPrint (eLogError, "NTCP2: Can't connect to unspecified address"); + return; + } GetService().post([this, conn]() { if (this->AddNTCP2Session (conn)) @@ -1439,7 +1434,6 @@ namespace transport if (ecode != boost::asio::error::operation_aborted) { LogPrint (eLogInfo, "NTCP2: Not connected in ", timeout, " seconds"); - i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true); conn->Terminate (); } }); @@ -1633,8 +1627,6 @@ namespace transport return; } } - if(!e) - i2p::data::netdb.SetUnreachable (conn->GetRemoteIdentity ()->GetIdentHash (), true); timer->cancel(); conn->Terminate(); }); diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index 8da49fc6..424f23c5 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -962,12 +962,13 @@ namespace data return nullptr; } - std::shared_ptr RouterInfo::GetNTCP2Address (bool publishedOnly) const + std::shared_ptr RouterInfo::GetNTCP2AddressWithStaticKey (const uint8_t * key) const { + if (!key) return nullptr; return GetAddress ( - [publishedOnly](std::shared_ptr address)->bool + [key](std::shared_ptr address)->bool { - return address->IsNTCP2 () && (!publishedOnly || address->IsPublishedNTCP2 ()); + return address->IsNTCP2 () && !memcmp (address->ntcp2->staticKey, key, 32); }); } diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index 45670ee5..c712545e 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -150,7 +150,7 @@ namespace data uint64_t GetTimestamp () const { return m_Timestamp; }; int GetVersion () const { return m_Version; }; Addresses& GetAddresses () { return *m_Addresses; }; // should be called for local RI only, otherwise must return shared_ptr - std::shared_ptr GetNTCP2Address (bool publishedOnly) const; + std::shared_ptr GetNTCP2AddressWithStaticKey (const uint8_t * key) const; std::shared_ptr GetPublishedNTCP2V4Address () const; std::shared_ptr GetPublishedNTCP2V6Address () const; std::shared_ptr GetSSUAddress (bool v4only = true) const;