mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-09 11:27:53 +00:00
use std::mt19937 instead rand()
This commit is contained in:
parent
2f847d62bb
commit
6a590bf970
@ -25,7 +25,7 @@ namespace transport
|
|||||||
m_TerminationTimer (GetService ()), m_CleanupTimer (GetService ()), m_ResendTimer (GetService ()),
|
m_TerminationTimer (GetService ()), m_CleanupTimer (GetService ()), m_ResendTimer (GetService ()),
|
||||||
m_IntroducersUpdateTimer (GetService ()), m_IntroducersUpdateTimerV6 (GetService ()),
|
m_IntroducersUpdateTimer (GetService ()), m_IntroducersUpdateTimerV6 (GetService ()),
|
||||||
m_IsPublished (true), m_IsSyncClockFromPeers (true), m_PendingTimeOffset (0),
|
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.empty ())
|
||||||
{
|
{
|
||||||
if (indices.size () > 1)
|
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)
|
for (auto ind: indices)
|
||||||
{
|
{
|
||||||
@ -984,8 +984,8 @@ namespace transport
|
|||||||
void SSU2Server::ScheduleResend (bool more)
|
void SSU2Server::ScheduleResend (bool more)
|
||||||
{
|
{
|
||||||
m_ResendTimer.expires_from_now (boost::posix_time::milliseconds (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_MORE_TIMEOUT + m_Rng () % SSU2_RESEND_CHECK_MORE_TIMEOUT_VARIANCE):
|
||||||
(SSU2_RESEND_CHECK_TIMEOUT + rand () % SSU2_RESEND_CHECK_TIMEOUT_VARIANCE)));
|
(SSU2_RESEND_CHECK_TIMEOUT + m_Rng () % SSU2_RESEND_CHECK_TIMEOUT_VARIANCE)));
|
||||||
m_ResendTimer.async_wait (std::bind (&SSU2Server::HandleResendTimer,
|
m_ResendTimer.async_wait (std::bind (&SSU2Server::HandleResendTimer,
|
||||||
this, std::placeholders::_1));
|
this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
@ -1057,7 +1057,7 @@ namespace transport
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::list<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers,
|
std::list<std::shared_ptr<SSU2Session> > SSU2Server::FindIntroducers (int maxNumIntroducers,
|
||||||
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded) const
|
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded)
|
||||||
{
|
{
|
||||||
std::list<std::shared_ptr<SSU2Session> > ret;
|
std::list<std::shared_ptr<SSU2Session> > ret;
|
||||||
for (const auto& s : m_Sessions)
|
for (const auto& s : m_Sessions)
|
||||||
@ -1074,7 +1074,7 @@ namespace transport
|
|||||||
int sz = ret.size () - maxNumIntroducers;
|
int sz = ret.size () - maxNumIntroducers;
|
||||||
for (int i = 0; i < sz; i++)
|
for (int i = 0; i < sz; i++)
|
||||||
{
|
{
|
||||||
auto ind = rand () % ret.size ();
|
auto ind = m_Rng () % ret.size ();
|
||||||
auto it = ret.begin ();
|
auto it = ret.begin ();
|
||||||
std::advance (it, ind);
|
std::advance (it, ind);
|
||||||
ret.erase (it);
|
ret.erase (it);
|
||||||
@ -1192,7 +1192,7 @@ namespace transport
|
|||||||
if (m_IsPublished)
|
if (m_IsPublished)
|
||||||
{
|
{
|
||||||
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(
|
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,
|
m_IntroducersUpdateTimer.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer,
|
||||||
this, std::placeholders::_1, true));
|
this, std::placeholders::_1, true));
|
||||||
}
|
}
|
||||||
@ -1206,7 +1206,7 @@ namespace transport
|
|||||||
i2p::context.ClearSSU2Introducers (true);
|
i2p::context.ClearSSU2Introducers (true);
|
||||||
m_Introducers.clear ();
|
m_Introducers.clear ();
|
||||||
m_IntroducersUpdateTimer.expires_from_now (boost::posix_time::seconds(
|
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,
|
m_IntroducersUpdateTimer.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer,
|
||||||
this, std::placeholders::_1, true));
|
this, std::placeholders::_1, true));
|
||||||
}
|
}
|
||||||
@ -1217,7 +1217,7 @@ namespace transport
|
|||||||
if (m_IsPublished)
|
if (m_IsPublished)
|
||||||
{
|
{
|
||||||
m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds(
|
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,
|
m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer,
|
||||||
this, std::placeholders::_1, false));
|
this, std::placeholders::_1, false));
|
||||||
}
|
}
|
||||||
@ -1231,7 +1231,7 @@ namespace transport
|
|||||||
i2p::context.ClearSSU2Introducers (false);
|
i2p::context.ClearSSU2Introducers (false);
|
||||||
m_IntroducersV6.clear ();
|
m_IntroducersV6.clear ();
|
||||||
m_IntroducersUpdateTimerV6.expires_from_now (boost::posix_time::seconds(
|
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,
|
m_IntroducersUpdateTimerV6.async_wait (std::bind (&SSU2Server::HandleIntroducersUpdateTimer,
|
||||||
this, std::placeholders::_1, false));
|
this, std::placeholders::_1, false));
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <random>
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "SSU2Session.h"
|
#include "SSU2Session.h"
|
||||||
#include "Socks5.h"
|
#include "Socks5.h"
|
||||||
@ -69,6 +70,7 @@ namespace transport
|
|||||||
bool UsesProxy () const { return m_IsThroughProxy; };
|
bool UsesProxy () const { return m_IsThroughProxy; };
|
||||||
bool IsSupported (const boost::asio::ip::address& addr) const;
|
bool IsSupported (const boost::asio::ip::address& addr) const;
|
||||||
uint16_t GetPort (bool v4) const;
|
uint16_t GetPort (bool v4) const;
|
||||||
|
std::mt19937& GetRng () { return m_Rng; }
|
||||||
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
|
bool IsSyncClockFromPeers () const { return m_IsSyncClockFromPeers; };
|
||||||
void AdjustTimeOffset (int64_t offset, std::shared_ptr<const i2p::data::IdentityEx> from);
|
void AdjustTimeOffset (int64_t offset, std::shared_ptr<const i2p::data::IdentityEx> from);
|
||||||
|
|
||||||
@ -128,7 +130,7 @@ namespace transport
|
|||||||
|
|
||||||
void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session);
|
void ConnectThroughIntroducer (std::shared_ptr<SSU2Session> session);
|
||||||
std::list<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers,
|
std::list<std::shared_ptr<SSU2Session> > FindIntroducers (int maxNumIntroducers,
|
||||||
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded) const;
|
bool v4, const std::unordered_set<i2p::data::IdentHash>& excluded);
|
||||||
void UpdateIntroducers (bool v4);
|
void UpdateIntroducers (bool v4);
|
||||||
void ScheduleIntroducersUpdateTimer ();
|
void ScheduleIntroducersUpdateTimer ();
|
||||||
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
|
void HandleIntroducersUpdateTimer (const boost::system::error_code& ecode, bool v4);
|
||||||
@ -168,6 +170,7 @@ namespace transport
|
|||||||
bool m_IsSyncClockFromPeers;
|
bool m_IsSyncClockFromPeers;
|
||||||
int64_t m_PendingTimeOffset; // during peer test
|
int64_t m_PendingTimeOffset; // during peer test
|
||||||
std::shared_ptr<const i2p::data::IdentityEx> m_PendingTimeOffsetFrom;
|
std::shared_ptr<const i2p::data::IdentityEx> m_PendingTimeOffsetFrom;
|
||||||
|
std::mt19937 m_Rng;
|
||||||
|
|
||||||
// proxy
|
// proxy
|
||||||
bool m_IsThroughProxy;
|
bool m_IsThroughProxy;
|
||||||
|
@ -516,7 +516,7 @@ namespace transport
|
|||||||
else
|
else
|
||||||
extraSize -= packet->payloadSize;
|
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;
|
if (offset + packet->payloadSize >= m_MaxPayloadSize) offset = 0;
|
||||||
auto size = CreateFirstFragmentBlock (packet->payload + packet->payloadSize, m_MaxPayloadSize - offset - packet->payloadSize, msg);
|
auto size = CreateFirstFragmentBlock (packet->payload + packet->payloadSize, m_MaxPayloadSize - offset - packet->payloadSize, msg);
|
||||||
if (!size) return false;
|
if (!size) return false;
|
||||||
@ -528,7 +528,7 @@ namespace transport
|
|||||||
uint8_t fragmentNum = 0;
|
uint8_t fragmentNum = 0;
|
||||||
while (msg->offset < msg->len)
|
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 = m_Server.GetSentPacketsPool ().AcquireShared ();
|
||||||
packet->payloadSize = CreateFollowOnFragmentBlock (packet->payload, m_MaxPayloadSize - offset, msg, fragmentNum, msgID);
|
packet->payloadSize = CreateFollowOnFragmentBlock (packet->payload, m_MaxPayloadSize - offset, msg, fragmentNum, msgID);
|
||||||
extraSize -= offset;
|
extraSize -= offset;
|
||||||
@ -934,7 +934,7 @@ namespace transport
|
|||||||
{
|
{
|
||||||
if (payloadSize > m_MaxPayloadSize - 48)
|
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)
|
if (m_SentHandshakePacket->payloadSize - payloadSize < 24)
|
||||||
payloadSize -= 24;
|
payloadSize -= 24;
|
||||||
}
|
}
|
||||||
@ -2695,7 +2695,7 @@ namespace transport
|
|||||||
size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize)
|
size_t SSU2Session::CreatePaddingBlock (uint8_t * buf, size_t len, size_t minSize)
|
||||||
{
|
{
|
||||||
if (len < 3 || len < minSize) return 0;
|
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;
|
if (paddingSize + 3 > len) paddingSize = len - 3;
|
||||||
else if (paddingSize + 3 < minSize) paddingSize = minSize - 3;
|
else if (paddingSize + 3 < minSize) paddingSize = minSize - 3;
|
||||||
buf[0] = eSSU2BlkPadding;
|
buf[0] = eSSU2BlkPadding;
|
||||||
@ -2981,7 +2981,7 @@ namespace transport
|
|||||||
{
|
{
|
||||||
uint8_t payload[SSU2_MAX_PACKET_SIZE];
|
uint8_t payload[SSU2_MAX_PACKET_SIZE];
|
||||||
payload[0] = eSSU2BlkPathChallenge;
|
payload[0] = eSSU2BlkPathChallenge;
|
||||||
size_t len = rand () % (m_MaxPayloadSize - 3);
|
size_t len = m_Server.GetRng ()() % (m_MaxPayloadSize - 3);
|
||||||
htobe16buf (payload + 1, len);
|
htobe16buf (payload + 1, len);
|
||||||
if (len > 0)
|
if (len > 0)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user