From 6a590bf9708b9e6c9d015e01040c7b70e774c843 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 7 Jun 2024 22:10:52 -0400 Subject: [PATCH] use std::mt19937 instead rand() --- libi2pd/SSU2.cpp | 20 ++++++++++---------- libi2pd/SSU2.h | 5 ++++- libi2pd/SSU2Session.cpp | 10 +++++----- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index baba7026..0e6b1910 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -25,7 +25,7 @@ namespace transport m_TerminationTimer (GetService ()), m_CleanupTimer (GetService ()), m_ResendTimer (GetService ()), m_IntroducersUpdateTimer (GetService ()), m_IntroducersUpdateTimerV6 (GetService ()), m_IsPublished (true), m_IsSyncClockFromPeers (true), m_PendingTimeOffset (0), - m_IsThroughProxy (false) + m_Rng(i2p::util::GetMonotonicMicroseconds ()%1000000LL), m_IsThroughProxy (false) { } @@ -800,7 +800,7 @@ namespace transport if (!indices.empty ()) { if (indices.size () > 1) - std::shuffle (indices.begin(), indices.end(), std::mt19937(ts)); + std::shuffle (indices.begin(), indices.end(), m_Rng); for (auto ind: indices) { @@ -984,8 +984,8 @@ namespace transport void SSU2Server::ScheduleResend (bool more) { m_ResendTimer.expires_from_now (boost::posix_time::milliseconds (more ? - (SSU2_RESEND_CHECK_MORE_TIMEOUT + rand () % SSU2_RESEND_CHECK_MORE_TIMEOUT_VARIANCE): - (SSU2_RESEND_CHECK_TIMEOUT + rand () % SSU2_RESEND_CHECK_TIMEOUT_VARIANCE))); + (SSU2_RESEND_CHECK_MORE_TIMEOUT + m_Rng () % SSU2_RESEND_CHECK_MORE_TIMEOUT_VARIANCE): + (SSU2_RESEND_CHECK_TIMEOUT + m_Rng () % SSU2_RESEND_CHECK_TIMEOUT_VARIANCE))); m_ResendTimer.async_wait (std::bind (&SSU2Server::HandleResendTimer, this, std::placeholders::_1)); } @@ -1057,7 +1057,7 @@ namespace transport } std::list > SSU2Server::FindIntroducers (int maxNumIntroducers, - bool v4, const std::unordered_set& excluded) const + bool v4, const std::unordered_set& excluded) { std::list > ret; for (const auto& s : m_Sessions) @@ -1074,7 +1074,7 @@ namespace transport int sz = ret.size () - maxNumIntroducers; for (int i = 0; i < sz; i++) { - auto ind = rand () % ret.size (); + auto ind = m_Rng () % ret.size (); auto it = ret.begin (); std::advance (it, ind); ret.erase (it); @@ -1192,7 +1192,7 @@ namespace transport if (m_IsPublished) { m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds( - SSU2_KEEP_ALIVE_INTERVAL + rand () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)); + SSU2_KEEP_ALIVE_INTERVAL + m_Rng () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)); m_IntroducersUpdateTimer.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer, this, std::placeholders::_1, true)); } @@ -1206,7 +1206,7 @@ namespace transport i2p::context.ClearSSU2Introducers (true); m_Introducers.clear (); m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds( - (SSU2_KEEP_ALIVE_INTERVAL + rand () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)/2)); + (SSU2_KEEP_ALIVE_INTERVAL + m_Rng () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)/2)); m_IntroducersUpdateTimer.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer, this, std::placeholders::_1, true)); } @@ -1217,7 +1217,7 @@ namespace transport if (m_IsPublished) { m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds( - SSU2_KEEP_ALIVE_INTERVAL + rand () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)); + SSU2_KEEP_ALIVE_INTERVAL + m_Rng () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)); m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer, this, std::placeholders::_1, false)); } @@ -1231,7 +1231,7 @@ namespace transport i2p::context.ClearSSU2Introducers (false); m_IntroducersV6.clear (); m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds( - (SSU2_KEEP_ALIVE_INTERVAL + rand () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)/2)); + (SSU2_KEEP_ALIVE_INTERVAL + m_Rng () % SSU2_KEEP_ALIVE_INTERVAL_VARIANCE)/2)); m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer, this, std::placeholders::_1, false)); } diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index d81acf6f..aace654a 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "util.h" #include "SSU2Session.h" #include "Socks5.h" @@ -69,6 +70,7 @@ namespace transport bool UsesProxy () const { return m_IsThroughProxy; }; bool IsSupported (const boost::asio::ip::address& addr) const; uint16_t GetPort (bool v4) const; + std::mt19937& GetRng () { return m_Rng; } bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; }; void AdjustTimeOffset (int64_t offset, std::shared_ptr from); @@ -128,7 +130,7 @@ namespace transport void ConnectThroughIntroducer (std::shared_ptr session); std::list > FindIntroducers (int maxNumIntroducers, - bool v4, const std::unordered_set& excluded) const; + bool v4, const std::unordered_set& excluded); void UpdateIntroducers (bool v4); void ScheduleIntroducersUpdateTimer (); void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4); @@ -168,6 +170,7 @@ namespace transport bool m_IsSyncClockFromPeers; int64_t m_PendingTimeOffset; // during peer test std::shared_ptr m_PendingTimeOffsetFrom; + std::mt19937 m_Rng; // proxy bool m_IsThroughProxy; diff --git a/libi2pd/SSU2Session.cpp b/libi2pd/SSU2Session.cpp index f5b36fcf..2c490e9b 100644 --- a/libi2pd/SSU2Session.cpp +++ b/libi2pd/SSU2Session.cpp @@ -516,7 +516,7 @@ namespace transport else extraSize -= packet->payloadSize; } - size_t offset = extraSize > 0 ? (rand () % extraSize) : 0; + size_t offset = extraSize > 0 ? (m_Server.GetRng ()() % extraSize) : 0; if (offset + packet->payloadSize >= m_MaxPayloadSize) offset = 0; auto size = CreateFirstFragmentBlock (packet->payload + packet->payloadSize, m_MaxPayloadSize - offset - packet->payloadSize, msg); if (!size) return false; @@ -528,7 +528,7 @@ namespace transport uint8_t fragmentNum = 0; while (msg->offset < msg->len) { - offset = extraSize > 0 ? (rand () % extraSize) : 0; + offset = extraSize > 0 ? (m_Server.GetRng ()() % extraSize) : 0; packet = m_Server.GetSentPacketsPool ().AcquireShared (); packet->payloadSize = CreateFollowOnFragmentBlock (packet->payload, m_MaxPayloadSize - offset, msg, fragmentNum, msgID); extraSize -= offset; @@ -934,7 +934,7 @@ namespace transport { if (payloadSize > m_MaxPayloadSize - 48) { - payloadSize = m_MaxPayloadSize - 48 - (rand () % 16); + payloadSize = m_MaxPayloadSize - 48 - (m_Server.GetRng ()() % 16); if (m_SentHandshakePacket->payloadSize - payloadSize < 24) payloadSize -= 24; } @@ -2695,7 +2695,7 @@ namespace transport size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize) { if (len < 3 || len < minSize) return 0; - size_t paddingSize = rand () & 0x0F; // 0 - 15 + size_t paddingSize = m_Server.GetRng ()() & 0x0F; // 0 - 15 if (paddingSize + 3 > len) paddingSize = len - 3; else if (paddingSize + 3 < minSize) paddingSize = minSize - 3; buf[0] = eSSU2BlkPadding; @@ -2981,7 +2981,7 @@ namespace transport { uint8_t payload[SSU2_MAX_PACKET_SIZE]; payload[0] = eSSU2BlkPathChallenge; - size_t len = rand () % (m_MaxPayloadSize - 3); + size_t len = m_Server.GetRng ()() % (m_MaxPayloadSize - 3); htobe16buf (payload + 1, len); if (len > 0) {