diff --git a/libi2pd/Datagram.cpp b/libi2pd/Datagram.cpp index 9d61f5e0..32db8ff1 100644 --- a/libi2pd/Datagram.cpp +++ b/libi2pd/Datagram.cpp @@ -295,7 +295,7 @@ namespace datagram } } - if (!m_RoutingSession || !m_RoutingSession->GetOwner () || !m_RoutingSession->IsReadyToSend ()) + if (!m_RoutingSession || m_RoutingSession->IsTerminated () || !m_RoutingSession->IsReadyToSend ()) { bool found = false; for (auto& it: m_PendingRoutingSessions) diff --git a/libi2pd/ECIESX25519AEADRatchetSession.cpp b/libi2pd/ECIESX25519AEADRatchetSession.cpp index a83c9285..870995e8 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.cpp +++ b/libi2pd/ECIESX25519AEADRatchetSession.cpp @@ -94,15 +94,20 @@ namespace garlic m_ItermediateSymmKeys.erase (index); } - void RatchetTagSet::Expire () + void ReceiveRatchetTagSet::Expire () { if (!m_ExpirationTimestamp) m_ExpirationTimestamp = i2p::util::GetSecondsSinceEpoch () + ECIESX25519_PREVIOUS_TAGSET_EXPIRATION_TIMEOUT; } + bool ReceiveRatchetTagSet::IsExpired (uint64_t ts) const + { + return m_ExpirationTimestamp && ts > m_ExpirationTimestamp; + } + bool ReceiveRatchetTagSet::IsIndexExpired (int index) const { - return index < m_TrimBehindIndex || !m_Session || !m_Session->GetOwner (); + return index < m_TrimBehindIndex || !m_Session || m_Session->IsTerminated (); } bool ReceiveRatchetTagSet::HandleNextMessage (uint8_t * buf, size_t len, int index) diff --git a/libi2pd/ECIESX25519AEADRatchetSession.h b/libi2pd/ECIESX25519AEADRatchetSession.h index 526f2ea4..974f1b15 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.h +++ b/libi2pd/ECIESX25519AEADRatchetSession.h @@ -56,10 +56,7 @@ namespace garlic int GetTagSetID () const { return m_TagSetID; }; void SetTagSetID (int tagsetID) { m_TagSetID = tagsetID; }; - - void Expire (); - bool IsExpired (uint64_t ts) const { return m_ExpirationTimestamp && ts > m_ExpirationTimestamp; }; - + private: union @@ -77,7 +74,6 @@ namespace garlic std::unordered_map > m_ItermediateSymmKeys; int m_TagSetID = 0; - uint64_t m_ExpirationTimestamp = 0; }; class ECIESX25519AEADRatchetSession; @@ -93,14 +89,18 @@ namespace garlic std::shared_ptr GetSession () { return m_Session; }; void SetTrimBehind (int index) { if (index > m_TrimBehindIndex) m_TrimBehindIndex = index; }; + void Expire (); + bool IsExpired (uint64_t ts) const; + virtual bool IsIndexExpired (int index) const; virtual bool HandleNextMessage (uint8_t * buf, size_t len, int index); private: - + int m_TrimBehindIndex = 0; std::shared_ptr m_Session; bool m_IsNS; + uint64_t m_ExpirationTimestamp = 0; }; class DatabaseLookupTagSet: public ReceiveRatchetTagSet @@ -170,7 +170,8 @@ namespace garlic const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; } void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); } - + + void Terminate () { m_IsTerminated = true; } void SetDestination (const i2p::data::IdentHash& dest) // TODO: { if (!m_Destination) m_Destination.reset (new i2p::data::IdentHash (dest)); @@ -182,6 +183,7 @@ namespace garlic bool IsRatchets () const { return true; }; bool IsReadyToSend () const { return m_State != eSessionStateNewSessionSent; }; + bool IsTerminated () const { return m_IsTerminated; } uint64_t GetLastActivityTimestamp () const { return m_LastActivityTimestamp; }; private: @@ -221,7 +223,7 @@ namespace garlic std::shared_ptr m_SendTagset, m_NSRSendTagset; std::unique_ptr m_Destination;// TODO: might not need it std::list > m_AckRequests; // (tagsetid, index) - bool m_SendReverseKey = false, m_SendForwardKey = false; + bool m_SendReverseKey = false, m_SendForwardKey = false, m_IsTerminated = false; std::unique_ptr m_NextReceiveRatchet, m_NextSendRatchet; uint8_t m_PaddingSizes[32], m_NextPaddingSize; diff --git a/libi2pd/Garlic.cpp b/libi2pd/Garlic.cpp index 17406b4f..a2b012db 100644 --- a/libi2pd/Garlic.cpp +++ b/libi2pd/Garlic.cpp @@ -445,9 +445,16 @@ namespace garlic void GarlicDestination::CleanUp () { + for (auto it: m_Sessions) + it.second->SetOwner (nullptr); m_Sessions.clear (); m_DeliveryStatusSessions.clear (); m_Tags.clear (); + for (auto it: m_ECIESx25519Sessions) + { + it.second->Terminate (); + it.second->SetOwner (nullptr); + } m_ECIESx25519Sessions.clear (); m_ECIESx25519Tags.clear (); } @@ -852,7 +859,7 @@ namespace garlic { if (it->second->CheckExpired (ts)) { - it->second->SetOwner (nullptr); + it->second->Terminate (); it = m_ECIESx25519Sessions.erase (it); } else @@ -1077,7 +1084,7 @@ namespace garlic { if (it->second->CanBeRestarted (i2p::util::GetSecondsSinceEpoch ())) { - it->second->SetOwner (nullptr); // detach + it->second->Terminate (); // detach m_ECIESx25519Sessions.erase (it); } else diff --git a/libi2pd/Garlic.h b/libi2pd/Garlic.h index a8806921..4288e74b 100644 --- a/libi2pd/Garlic.h +++ b/libi2pd/Garlic.h @@ -115,6 +115,7 @@ namespace garlic virtual bool MessageConfirmed (uint32_t msgID); virtual bool IsRatchets () const { return false; }; virtual bool IsReadyToSend () const { return true; }; + virtual bool IsTerminated () const { return !GetOwner (); }; virtual uint64_t GetLastActivityTimestamp () const { return 0; }; // non-zero for rathets only void SetLeaseSetUpdated () diff --git a/libi2pd/Streaming.cpp b/libi2pd/Streaming.cpp index f6bf7e8b..a873a660 100644 --- a/libi2pd/Streaming.cpp +++ b/libi2pd/Streaming.cpp @@ -764,7 +764,7 @@ namespace stream return; } } - if (!m_RoutingSession || !m_RoutingSession->GetOwner () || !m_RoutingSession->IsReadyToSend ()) // expired and detached or new session sent + if (!m_RoutingSession || m_RoutingSession->IsTerminated () || !m_RoutingSession->IsReadyToSend ()) // expired and detached or new session sent m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, true); if (!m_CurrentOutboundTunnel && m_RoutingSession) // first message to send {