From fcd36805472518c30e8e281d288f1c2e18838e8e Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 17 Oct 2014 22:15:42 -0400 Subject: [PATCH] delete expired incoming tags --- Garlic.cpp | 26 ++++++++++++++++++++++++-- Garlic.h | 7 ++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Garlic.cpp b/Garlic.cpp index 0fac63bd..359f8fec 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -148,7 +148,7 @@ namespace garlic size_t GarlicRoutingSession::CreateAESBlock (uint8_t * buf, const I2NPMessage * msg) { size_t blockSize = 0; - bool createNewTags = m_Owner && ((int)m_SessionTags.size () <= m_NumTags/4); + bool createNewTags = m_Owner && ((int)m_SessionTags.size () <= m_NumTags/2); UnconfirmedTags * newTags = createNewTags ? GenerateSessionTags () : nullptr; *(uint16_t *)buf = newTags ? htobe16 (newTags->numTags) : 0; // tag count blockSize += 2; @@ -343,7 +343,29 @@ namespace garlic else LogPrint ("Failed to decrypt garlic"); } - DeleteI2NPMessage (msg); + DeleteI2NPMessage (msg); + + // cleanup expired tags + uint32_t ts = i2p::util::GetSecondsSinceEpoch (); + if (ts > m_LastTagsCleanupTime + INCOMING_TAGS_EXPIRATION_TIMEOUT) + { + if (m_LastTagsCleanupTime) + { + int numExpiredTags = 0; + for (auto it = m_Tags.begin (); it != m_Tags.end ();) + { + if (ts > it->first.creationTime + INCOMING_TAGS_EXPIRATION_TIMEOUT) + { + numExpiredTags++; + it = m_Tags.erase (it); + } + else + it++; + } + LogPrint (numExpiredTags, " tags expired for ", GetIdentHash().ToBase64 ()); + } + m_LastTagsCleanupTime = ts; + } } void GarlicDestination::HandleAESBlock (uint8_t * buf, size_t len, std::shared_ptr decryption, diff --git a/Garlic.h b/Garlic.h index c514c52e..92a8a081 100644 --- a/Garlic.h +++ b/Garlic.h @@ -37,7 +37,7 @@ namespace garlic }; #pragma pack() - const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes + const int INCOMING_TAGS_EXPIRATION_TIMEOUT = 960; // 16 minutes const int OUTGOING_TAGS_EXPIRATION_TIMEOUT = 720; // 12 minutes struct SessionTag: public i2p::data::Tag<32> @@ -102,7 +102,7 @@ namespace garlic { public: - GarlicDestination () {}; + GarlicDestination (): m_LastTagsCleanupTime (0) {}; ~GarlicDestination (); GarlicRoutingSession * GetRoutingSession (const i2p::data::RoutingDestination& destination, int numTags); @@ -136,7 +136,8 @@ namespace garlic std::mutex m_SessionsMutex; std::map m_Sessions; // incoming - std::map> m_Tags; + std::map> m_Tags; + uint32_t m_LastTagsCleanupTime; // DeliveryStatus std::map m_CreatedSessions; // msgID -> session };