From 6caec6b551a704e31d49e40f5add84a739c9e00b Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 13 Jun 2024 18:10:45 -0400 Subject: [PATCH] check if updated router is still introducer. Remove non-introducer sessions from introducers list --- libi2pd/SSU2.cpp | 29 ++++++++++++++--------------- libi2pd/SSU2Session.cpp | 35 ++++++++++++++++++++++++++++------- libi2pd/SSU2Session.h | 1 + 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 107c5ef7..ab1a7d8b 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -1104,22 +1104,21 @@ namespace transport session = it1->second; excluded.insert (it); } - if (session && session->IsEstablished () && session->GetRelayTag () && session->IsOutgoing ()) // still session with introducer? - { - if (ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION) + if (session && session->IsEstablished () && session->GetRelayTag () && session->IsOutgoing () && // still session with introducer? + ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_EXPIRATION) + { + session->SendKeepAlive (); + if (ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION) + newList.push_back (it); + else { - session->SendKeepAlive (); - if (ts < session->GetCreationTime () + SSU2_TO_INTRODUCER_SESSION_DURATION) - newList.push_back (it); - else - { - impliedList.push_back (it); // keep in introducers list, but not publish - session = nullptr; - } - } - else - session = nullptr; - } + impliedList.push_back (it); // keep in introducers list, but not publish + session = nullptr; + } + } + else + session = nullptr; + if (!session) i2p::context.RemoveSSU2Introducer (it, v4); } diff --git a/libi2pd/SSU2Session.cpp b/libi2pd/SSU2Session.cpp index a0adb5f3..51e328b6 100644 --- a/libi2pd/SSU2Session.cpp +++ b/libi2pd/SSU2Session.cpp @@ -1575,14 +1575,9 @@ namespace transport LogPrint (eLogDebug, "SSU2: Options"); break; case eSSU2BlkRouterInfo: - { - // not from SessionConfirmed, we must add it instantly to use in next block LogPrint (eLogDebug, "SSU2: RouterInfo"); - auto ri = ExtractRouterInfo (buf + offset, size); - if (ri) - i2p::data::netdb.AddRouterInfo (ri->GetBuffer (), ri->GetBufferLen ()); // TODO: add ri - break; - } + HandleRouterInfo (buf + offset, size); + break; case eSSU2BlkI2NPMessage: { LogPrint (eLogDebug, "SSU2: I2NP message"); @@ -1742,6 +1737,32 @@ namespace transport }; } + void SSU2Session::HandleRouterInfo (const uint8_t * buf, size_t len) + { + auto ri = ExtractRouterInfo (buf, len); + if (ri) + { + // not from SessionConfirmed, we must add it instantly to use in next block + auto newRi = i2p::data::netdb.AddRouterInfo (ri->GetBuffer (), ri->GetBufferLen ()); // TODO: add ri + if (newRi) + { + auto remoteIdentity = GetRemoteIdentity (); + if (remoteIdentity && remoteIdentity->GetIdentHash () == newRi->GetIdentHash ()) + { + // peer's RouterInfo update + SetRemoteIdentity (newRi->GetIdentity ()); + auto address = m_RemoteEndpoint.address ().is_v6 () ? newRi->GetSSU2V6Address () : newRi->GetSSU2V4Address (); + if (address) + { + m_Address = address; + if (IsOutgoing () && m_RelayTag && !address->IsIntroducer ()) + m_RelayTag = 0; // not longer introducer + } + } + } + } + } + void SSU2Session::HandleAck (const uint8_t * buf, size_t len) { if (m_State == eSSU2SessionStateSessionConfirmedSent) diff --git a/libi2pd/SSU2Session.h b/libi2pd/SSU2Session.h index 76dc55df..587f74b6 100644 --- a/libi2pd/SSU2Session.h +++ b/libi2pd/SSU2Session.h @@ -303,6 +303,7 @@ namespace transport void HandlePayload (const uint8_t * buf, size_t len); void HandleDateTime (const uint8_t * buf, size_t len); + void HandleRouterInfo (const uint8_t * buf, size_t len); void HandleAck (const uint8_t * buf, size_t len); void HandleAckRange (uint32_t firstPacketNum, uint32_t lastPacketNum, uint64_t ts); void HandleAddress (const uint8_t * buf, size_t len);