From e5743548962b2446e2cd3650e3bcb3c583616ce6 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 10 Nov 2024 11:32:46 -0500 Subject: [PATCH] limit received SSU2 packets queue --- libi2pd/SSU2.cpp | 15 +++++++++++---- libi2pd/SSU2.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index c8f2909b..a53c877c 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -457,13 +457,20 @@ namespace transport void SSU2Server::InsertToReceivedPacketsQueue (std::list& packets) { if (packets.empty ()) return; - bool empty = false; + size_t queueSize = 0; { std::lock_guard l(m_ReceivedPacketsQueueMutex); - empty = m_ReceivedPacketsQueue.empty (); - m_ReceivedPacketsQueue.splice (m_ReceivedPacketsQueue.end (), packets); + queueSize = m_ReceivedPacketsQueue.size (); + if (queueSize < SSU2_MAX_RECEIVED_QUEUE_SIZE) + m_ReceivedPacketsQueue.splice (m_ReceivedPacketsQueue.end (), packets); + else + { + LogPrint (eLogError, "SSU2: Received queue size ", queueSize, " exceeds max size", SSU2_MAX_RECEIVED_QUEUE_SIZE); + m_PacketsPool.ReleaseMt (packets); + queueSize = 0; // invoke processing just in case + } } - if (empty) + if (!queueSize) GetService ().post([this]() { HandleReceivedPacketsQueue (); }); } diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index 319a4780..95f053a9 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -37,6 +37,7 @@ namespace transport const uint64_t SSU2_SOCKET_MAX_BUFFER_SIZE = 4 * 1024 * 1024; const size_t SSU2_MAX_NUM_INTRODUCERS = 3; const size_t SSU2_MIN_RECEIVED_PACKET_SIZE = 40; // 16 byte short header + 8 byte minimum payload + 16 byte MAC + const size_t SSU2_MAX_RECEIVED_QUEUE_SIZE = 2500; // in packets const int SSU2_TO_INTRODUCER_SESSION_DURATION = 3600; // 1 hour const int SSU2_TO_INTRODUCER_SESSION_EXPIRATION = 4800; // 80 minutes const int SSU2_KEEP_ALIVE_INTERVAL = 15; // in seconds