Browse Source

lazy initialization or tunnel gateway's random buffer

pull/1717/head
orignal 3 years ago
parent
commit
6c57ba36f7
  1. 13
      libi2pd/TunnelGateway.cpp
  2. 4
      libi2pd/TunnelGateway.h

13
libi2pd/TunnelGateway.cpp

@ -19,16 +19,14 @@ namespace i2p @@ -19,16 +19,14 @@ namespace i2p
namespace tunnel
{
TunnelGatewayBuffer::TunnelGatewayBuffer ():
m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0)
m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0), m_NonZeroRandomBuffer (nullptr)
{
RAND_bytes (m_NonZeroRandomBuffer, TUNNEL_DATA_MAX_PAYLOAD_SIZE);
for (size_t i = 0; i < TUNNEL_DATA_MAX_PAYLOAD_SIZE; i++)
if (!m_NonZeroRandomBuffer[i]) m_NonZeroRandomBuffer[i] = 1;
}
TunnelGatewayBuffer::~TunnelGatewayBuffer ()
{
ClearTunnelDataMsgs ();
if (m_NonZeroRandomBuffer) delete[] m_NonZeroRandomBuffer;
}
void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block)
@ -183,6 +181,13 @@ namespace tunnel @@ -183,6 +181,13 @@ namespace tunnel
if (paddingSize > 0)
{
// non-zero padding
if (!m_NonZeroRandomBuffer) // first time?
{
m_NonZeroRandomBuffer = new uint8_t[TUNNEL_DATA_MAX_PAYLOAD_SIZE];
RAND_bytes (m_NonZeroRandomBuffer, TUNNEL_DATA_MAX_PAYLOAD_SIZE);
for (size_t i = 0; i < TUNNEL_DATA_MAX_PAYLOAD_SIZE; i++)
if (!m_NonZeroRandomBuffer[i]) m_NonZeroRandomBuffer[i] = 1;
}
auto randomOffset = rand () % (TUNNEL_DATA_MAX_PAYLOAD_SIZE - paddingSize + 1);
memcpy (buf + 24, m_NonZeroRandomBuffer + randomOffset, paddingSize);
}

4
libi2pd/TunnelGateway.h

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
* Copyright (c) 2013-2021, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
@ -38,7 +38,7 @@ namespace tunnel @@ -38,7 +38,7 @@ namespace tunnel
std::vector<std::shared_ptr<const I2NPMessage> > m_TunnelDataMsgs;
std::shared_ptr<I2NPMessage> m_CurrentTunnelDataMsg;
size_t m_RemainingSize;
uint8_t m_NonZeroRandomBuffer[TUNNEL_DATA_MAX_PAYLOAD_SIZE];
uint8_t * m_NonZeroRandomBuffer;
};
class TunnelGateway

Loading…
Cancel
Save