diff --git a/libi2pd/Datagram.cpp b/libi2pd/Datagram.cpp index 7d58082b..4609eb8d 100644 --- a/libi2pd/Datagram.cpp +++ b/libi2pd/Datagram.cpp @@ -667,11 +667,7 @@ namespace datagram { int idx = -1; if (m_LocalDestination) - { - auto pool = m_LocalDestination->GetTunnelPool (); - if (pool) - idx = pool->GetRng ()() % sz; - } + idx = m_LocalDestination->GetRng ()() % sz; if (idx < 0) idx = rand () % sz; path->remoteLease = ls[idx]; } @@ -700,11 +696,7 @@ namespace datagram { int idx = -1; if (m_LocalDestination) - { - auto pool = m_LocalDestination->GetTunnelPool (); - if (pool) - idx = pool->GetRng ()() % sz; - } + idx = m_LocalDestination->GetRng ()() % sz; if (idx < 0) idx = rand () % sz; path->remoteLease = ls[idx]; } diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index cec25067..00b9d734 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -585,7 +585,7 @@ namespace client m_PublishReplyToken = 0; // schedule verification m_PublishVerificationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_VERIFICATION_TIMEOUT + - (m_Pool ? m_Pool->GetRng ()() % PUBLISH_VERIFICATION_TIMEOUT_VARIANCE : 0))); + GetRng ()() % PUBLISH_VERIFICATION_TIMEOUT_VARIANCE)); m_PublishVerificationTimer.async_wait (std::bind (&LeaseSetDestination::HandlePublishVerificationTimer, shared_from_this (), std::placeholders::_1)); } @@ -946,7 +946,7 @@ namespace client CleanupRemoteLeaseSets (); CleanupDestination (); m_CleanupTimer.expires_from_now (boost::posix_time::seconds (DESTINATION_CLEANUP_TIMEOUT + - (m_Pool ? m_Pool->GetRng ()() % DESTINATION_CLEANUP_TIMEOUT_VARIANCE : 0))); + GetRng ()() % DESTINATION_CLEANUP_TIMEOUT_VARIANCE)); m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer, shared_from_this (), std::placeholders::_1)); } diff --git a/libi2pd/ECIESX25519AEADRatchetSession.cpp b/libi2pd/ECIESX25519AEADRatchetSession.cpp index 7cdff7a5..212f0a9a 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.cpp +++ b/libi2pd/ECIESX25519AEADRatchetSession.cpp @@ -7,7 +7,6 @@ */ #include -#include #include "Log.h" #include "util.h" #include "Crypto.h" @@ -182,7 +181,6 @@ namespace garlic GarlicRoutingSession (owner, true), m_RemoteStaticKeyType (0) { if (!attachLeaseSetNS) SetLeaseSetUpdateStatus (eLeaseSetUpToDate); - RAND_bytes (m_PaddingSizes, 32); m_NextPaddingSize = 0; } ECIESX25519AEADRatchetSession::~ECIESX25519AEADRatchetSession () @@ -1235,12 +1233,7 @@ namespace garlic int delta = (int)ECIESX25519_OPTIMAL_PAYLOAD_SIZE - (int)payloadLen; if (delta < 0 || delta > 3) // don't create padding if we are close to optimal size { - paddingSize = m_PaddingSizes[m_NextPaddingSize++] & 0x0F; // 0 - 15 - if (m_NextPaddingSize >= 32) - { - RAND_bytes (m_PaddingSizes, 32); - m_NextPaddingSize = 0; - } + paddingSize = GetOwner ()->GetRng ()() % 16; // 0 - 15 if (delta > 3) { delta -= 3; diff --git a/libi2pd/ECIESX25519AEADRatchetSession.h b/libi2pd/ECIESX25519AEADRatchetSession.h index 51063f84..0bf9d51b 100644 --- a/libi2pd/ECIESX25519AEADRatchetSession.h +++ b/libi2pd/ECIESX25519AEADRatchetSession.h @@ -248,7 +248,6 @@ namespace garlic std::list > m_AckRequests; // incoming (tagsetid, index) bool m_SendReverseKey = false, m_SendForwardKey = false, m_IsTerminated = false; std::unique_ptr m_NextReceiveRatchet, m_NextSendRatchet; - uint8_t m_PaddingSizes[32], m_NextPaddingSize; uint64_t m_LastAckRequestSendTime = 0; // milliseconds uint32_t m_AckRequestMsgID = 0; diff --git a/libi2pd/Garlic.cpp b/libi2pd/Garlic.cpp index 96605c20..8221134c 100644 --- a/libi2pd/Garlic.cpp +++ b/libi2pd/Garlic.cpp @@ -433,7 +433,9 @@ namespace garlic return ret; } - GarlicDestination::GarlicDestination (): m_NumTags (32), // 32 tags by default + GarlicDestination::GarlicDestination (): + m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL), + m_NumTags (32), // 32 tags by default m_PayloadBuffer (nullptr), m_LastIncomingSessionTimestamp (0), m_NumRatchetInboundTags (0) // 0 means standard { diff --git a/libi2pd/Garlic.h b/libi2pd/Garlic.h index 2f5919db..506d9316 100644 --- a/libi2pd/Garlic.h +++ b/libi2pd/Garlic.h @@ -16,6 +16,7 @@ #include #include #include +#include #include "Crypto.h" #include "I2NPProtocol.h" #include "LeaseSet.h" @@ -243,6 +244,7 @@ namespace garlic ~GarlicDestination (); void CleanUp (); + std::mt19937& GetRng () { return m_Rng; }; void SetNumTags (int numTags) { m_NumTags = numTags; }; int GetNumTags () const { return m_NumTags; }; void SetNumRatchetInboundTags (int numTags) { m_NumRatchetInboundTags = numTags; }; @@ -303,6 +305,7 @@ namespace garlic private: + std::mt19937 m_Rng; // outgoing sessions int m_NumTags; std::mutex m_SessionsMutex; diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index ea24e794..4842a652 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -34,14 +34,13 @@ namespace i2p m_ShareRatio (100), m_Status (eRouterStatusUnknown), m_StatusV6 (eRouterStatusUnknown), m_Error (eRouterErrorNone), m_ErrorV6 (eRouterErrorNone), m_Testing (false), m_TestingV6 (false), m_NetID (I2PD_NET_ID), - m_PublishReplyToken (0), m_IsHiddenMode (false), - m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL), m_IsSaving (false) + m_PublishReplyToken (0), m_IsHiddenMode (false), m_IsSaving (false) { } void RouterContext::Init () { - srand (m_Rng () % 1000); + srand (GetRng ()() % 1000); m_StartupTime = i2p::util::GetMonotonicSeconds (); if (!Load ()) @@ -1395,7 +1394,7 @@ namespace i2p { m_PublishTimer->cancel (); m_PublishTimer->expires_from_now (boost::posix_time::seconds(ROUTER_INFO_PUBLISH_INTERVAL + - m_Rng () % ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE)); + GetRng ()() % ROUTER_INFO_PUBLISH_INTERVAL_VARIANCE)); m_PublishTimer->async_wait (std::bind (&RouterContext::HandlePublishTimer, this, std::placeholders::_1)); } @@ -1508,7 +1507,7 @@ namespace i2p { m_CongestionUpdateTimer->cancel (); m_CongestionUpdateTimer->expires_from_now (boost::posix_time::seconds( - ROUTER_INFO_CONGESTION_UPDATE_INTERVAL + m_Rng () % ROUTER_INFO_CONGESTION_UPDATE_INTERVAL_VARIANCE)); + ROUTER_INFO_CONGESTION_UPDATE_INTERVAL + GetRng ()() % ROUTER_INFO_CONGESTION_UPDATE_INTERVAL_VARIANCE)); m_CongestionUpdateTimer->async_wait (std::bind (&RouterContext::HandleCongestionUpdateTimer, this, std::placeholders::_1)); } diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index 2e295bd9..5e453160 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include "Identity.h" @@ -268,7 +267,6 @@ namespace garlic uint32_t m_PublishReplyToken; bool m_IsHiddenMode; // not publish mutable std::mutex m_RouterInfoMutex; - std::mt19937 m_Rng; std::shared_ptr m_SaveBuffer; std::mutex m_SaveBufferMutex; // TODO: make m_SaveBuffer atomic std::atomic m_IsSaving; diff --git a/libi2pd/Streaming.cpp b/libi2pd/Streaming.cpp index 58015bc0..0a00f142 100644 --- a/libi2pd/Streaming.cpp +++ b/libi2pd/Streaming.cpp @@ -2401,13 +2401,7 @@ namespace stream uint32_t StreamingDestination::GetRandom () { - if (m_Owner) - { - auto pool = m_Owner->GetTunnelPool (); - if (pool) - return pool->GetRng ()(); - } - return rand (); + return m_Owner ? m_Owner->GetRng ()() : rand (); } } } diff --git a/libi2pd/Tunnel.cpp b/libi2pd/Tunnel.cpp index a3c53fc1..3ce2b006 100644 --- a/libi2pd/Tunnel.cpp +++ b/libi2pd/Tunnel.cpp @@ -58,7 +58,7 @@ namespace tunnel // shuffle records std::vector recordIndicies; for (int i = 0; i < numRecords; i++) recordIndicies.push_back(i); - std::shuffle (recordIndicies.begin(), recordIndicies.end(), m_Pool ? m_Pool->GetRng () : std::mt19937(std::random_device()())); + std::shuffle (recordIndicies.begin(), recordIndicies.end(), tunnels.GetRng ()); // create real records uint8_t * records = msg->GetPayload () + 1; TunnelHopConfig * hop = m_Config->GetFirstHop (); diff --git a/libi2pd/TunnelPool.cpp b/libi2pd/TunnelPool.cpp index 26367aa6..80e07b79 100644 --- a/libi2pd/TunnelPool.cpp +++ b/libi2pd/TunnelPool.cpp @@ -45,8 +45,7 @@ namespace tunnel m_NumInboundHops (numInboundHops), m_NumOutboundHops (numOutboundHops), m_NumInboundTunnels (numInboundTunnels), m_NumOutboundTunnels (numOutboundTunnels), m_InboundVariance (inboundVariance), m_OutboundVariance (outboundVariance), - m_IsActive (true), m_IsHighBandwidth (isHighBandwidth), m_CustomPeerSelector(nullptr), - m_Rng(i2p::util::GetMonotonicMicroseconds ()%1000000LL) + m_IsActive (true), m_IsHighBandwidth (isHighBandwidth), m_CustomPeerSelector(nullptr) { if (m_NumInboundTunnels > TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY) m_NumInboundTunnels = TUNNEL_POOL_MAX_INBOUND_TUNNELS_QUANTITY; @@ -60,7 +59,7 @@ namespace tunnel m_InboundVariance = (m_NumInboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumInboundHops : 0; if (m_OutboundVariance > 0 && m_NumOutboundHops + m_OutboundVariance > STANDARD_NUM_RECORDS) m_OutboundVariance = (m_NumOutboundHops < STANDARD_NUM_RECORDS) ? STANDARD_NUM_RECORDS - m_NumOutboundHops : 0; - m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + m_Rng () % TUNNEL_POOL_MANAGE_INTERVAL; + m_NextManageTime = i2p::util::GetSecondsSinceEpoch () + rand () % TUNNEL_POOL_MANAGE_INTERVAL; } TunnelPool::~TunnelPool () @@ -229,7 +228,7 @@ namespace tunnel typename TTunnels::value_type excluded, i2p::data::RouterInfo::CompatibleTransports compatible) { if (tunnels.empty ()) return nullptr; - uint32_t ind = m_Rng () % (tunnels.size ()/2 + 1), i = 0; + uint32_t ind = i2p::tunnel::tunnels.GetRng ()() % (tunnels.size ()/2 + 1), i = 0; bool skipped = false; typename TTunnels::value_type tunnel = nullptr; for (const auto& it: tunnels) @@ -249,7 +248,7 @@ namespace tunnel } if (!tunnel && skipped) { - ind = m_Rng () % (tunnels.size ()/2 + 1), i = 0; + ind = i2p::tunnel::tunnels.GetRng ()() % (tunnels.size ()/2 + 1), i = 0; for (const auto& it: tunnels) { if (it->IsEstablished () && it != excluded) @@ -403,7 +402,7 @@ namespace tunnel if (it->IsEstablished ()) outboundTunnels.push_back (it); } - std::shuffle (outboundTunnels.begin(), outboundTunnels.end(), m_Rng); + std::shuffle (outboundTunnels.begin(), outboundTunnels.end(), tunnels.GetRng ()); std::vector > inboundTunnels; { std::unique_lock l(m_InboundTunnelsMutex); @@ -411,7 +410,7 @@ namespace tunnel if (it->IsEstablished ()) inboundTunnels.push_back (it); } - std::shuffle (inboundTunnels.begin(), inboundTunnels.end(), m_Rng); + std::shuffle (inboundTunnels.begin(), inboundTunnels.end(), tunnels.GetRng ()); auto it1 = outboundTunnels.begin (); auto it2 = inboundTunnels.begin (); while (it1 != outboundTunnels.end () && it2 != inboundTunnels.end ()) @@ -470,7 +469,7 @@ namespace tunnel { CreateTunnels (); TestTunnels (); - m_NextManageTime = ts + TUNNEL_POOL_MANAGE_INTERVAL + (m_Rng () % TUNNEL_POOL_MANAGE_INTERVAL)/2; + m_NextManageTime = ts + TUNNEL_POOL_MANAGE_INTERVAL + (tunnels.GetRng ()() % TUNNEL_POOL_MANAGE_INTERVAL)/2; } } @@ -635,7 +634,7 @@ namespace tunnel numHops = m_NumInboundHops; if (m_InboundVariance) { - int offset = m_Rng () % (std::abs (m_InboundVariance) + 1); + int offset = tunnels.GetRng ()() % (std::abs (m_InboundVariance) + 1); if (m_InboundVariance < 0) offset = -offset; numHops += offset; } @@ -645,7 +644,7 @@ namespace tunnel numHops = m_NumOutboundHops; if (m_OutboundVariance) { - int offset = m_Rng () % (std::abs (m_OutboundVariance) + 1); + int offset = tunnels.GetRng ()() % (std::abs (m_OutboundVariance) + 1); if (m_OutboundVariance < 0) offset = -offset; numHops += offset; } diff --git a/libi2pd/TunnelPool.h b/libi2pd/TunnelPool.h index 0ebfd1ac..41400f8b 100644 --- a/libi2pd/TunnelPool.h +++ b/libi2pd/TunnelPool.h @@ -15,7 +15,6 @@ #include #include #include -#include #include "Identity.h" #include "LeaseSet.h" #include "RouterInfo.h" @@ -118,8 +117,6 @@ namespace tunnel // for overriding tunnel peer selection std::shared_ptr SelectNextHop (std::shared_ptr prevHop, bool reverse, bool endpoint) const; bool StandardSelectPeers(Path & path, int numHops, bool inbound, SelectHopFunc nextHop); - - std::mt19937& GetRng () { return m_Rng; } private: @@ -153,8 +150,6 @@ namespace tunnel int m_MinLatency = 0; // if > 0 this tunnel pool will try building tunnels with minimum latency by ms int m_MaxLatency = 0; // if > 0 this tunnel pool will try building tunnels with maximum latency by ms - - std::mt19937 m_Rng; public: diff --git a/libi2pd_client/I2CP.cpp b/libi2pd_client/I2CP.cpp index 59ec046e..883d2565 100644 --- a/libi2pd_client/I2CP.cpp +++ b/libi2pd_client/I2CP.cpp @@ -291,11 +291,14 @@ namespace client leases = remote->GetNonExpiredLeases (true); // with threshold if (!leases.empty ()) { - auto pool = GetTunnelPool (); - remoteLease = leases[(pool ? pool->GetRng ()() : rand ()) % leases.size ()]; + remoteLease = leases[GetRng ()() % leases.size ()]; auto leaseRouter = i2p::data::netdb.FindRouter (remoteLease->tunnelGateway); - outboundTunnel = GetTunnelPool ()->GetNextOutboundTunnel (nullptr, - leaseRouter ? leaseRouter->GetCompatibleTransports (false) : (i2p::data::RouterInfo::CompatibleTransports)i2p::data::RouterInfo::eAllTransports); + auto pool = GetTunnelPool (); + if (pool) + outboundTunnel = pool->GetNextOutboundTunnel (nullptr, + leaseRouter ? leaseRouter->GetCompatibleTransports (false) : (i2p::data::RouterInfo::CompatibleTransports)i2p::data::RouterInfo::eAllTransports); + else + outboundTunnel = nullptr; } if (remoteLease && outboundTunnel) remoteSession->SetSharedRoutingPath (std::make_shared (