RNG per destination. Use RNG from tunnels in tunnel pool

This commit is contained in:
orignal
2025-12-11 17:43:14 -05:00
parent d4ec15361f
commit 4f73e9678e
13 changed files with 33 additions and 56 deletions
+2 -10
View File
@@ -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];
}
+2 -2
View File
@@ -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));
}
+1 -8
View File
@@ -7,7 +7,6 @@
*/
#include <string.h>
#include <openssl/sha.h>
#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;
-1
View File
@@ -248,7 +248,6 @@ namespace garlic
std::list<std::pair<uint16_t, int> > m_AckRequests; // incoming (tagsetid, index)
bool m_SendReverseKey = false, m_SendForwardKey = false, m_IsTerminated = false;
std::unique_ptr<DHRatchet> m_NextReceiveRatchet, m_NextSendRatchet;
uint8_t m_PaddingSizes[32], m_NextPaddingSize;
uint64_t m_LastAckRequestSendTime = 0; // milliseconds
uint32_t m_AckRequestMsgID = 0;
+3 -1
View File
@@ -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
{
+3
View File
@@ -16,6 +16,7 @@
#include <thread>
#include <mutex>
#include <memory>
#include <random>
#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;
+4 -5
View File
@@ -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));
}
-2
View File
@@ -12,7 +12,6 @@
#include <inttypes.h>
#include <string>
#include <memory>
#include <random>
#include <unordered_set>
#include <boost/asio.hpp>
#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<i2p::data::RouterInfo::Buffer> m_SaveBuffer;
std::mutex m_SaveBufferMutex; // TODO: make m_SaveBuffer atomic
std::atomic<bool> m_IsSaving;
+1 -7
View File
@@ -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 ();
}
}
}
+1 -1
View File
@@ -58,7 +58,7 @@ namespace tunnel
// shuffle records
std::vector<int> 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 ();
+9 -10
View File
@@ -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<std::shared_ptr<InboundTunnel> > inboundTunnels;
{
std::unique_lock<std::mutex> 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;
}
-5
View File
@@ -15,7 +15,6 @@
#include <utility>
#include <mutex>
#include <memory>
#include <random>
#include "Identity.h"
#include "LeaseSet.h"
#include "RouterInfo.h"
@@ -118,8 +117,6 @@ namespace tunnel
// for overriding tunnel peer selection
std::shared_ptr<const i2p::data::RouterInfo> SelectNextHop (std::shared_ptr<const i2p::data::RouterInfo> 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:
+7 -4
View File
@@ -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<i2p::garlic::GarlicRoutingPath> (