i2pd/libi2pd/TunnelGateway.cpp

222 lines
7.0 KiB
C++
Raw Normal View History

2013-11-10 23:19:49 +00:00
#include <string.h>
2016-05-11 19:12:38 +00:00
#include "Crypto.h"
#include "I2PEndian.h"
#include "Log.h"
2013-11-10 23:19:49 +00:00
#include "RouterContext.h"
#include "Transports.h"
2013-11-10 23:19:49 +00:00
#include "TunnelGateway.h"
namespace i2p
{
namespace tunnel
{
TunnelGatewayBuffer::TunnelGatewayBuffer ():
m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0)
2015-06-26 20:06:59 +00:00
{
2015-11-03 14:15:49 +00:00
RAND_bytes (m_NonZeroRandomBuffer, TUNNEL_DATA_MAX_PAYLOAD_SIZE);
2015-06-26 20:06:59 +00:00
for (size_t i = 0; i < TUNNEL_DATA_MAX_PAYLOAD_SIZE; i++)
if (!m_NonZeroRandomBuffer[i]) m_NonZeroRandomBuffer[i] = 1;
}
2015-02-05 02:24:48 +00:00
TunnelGatewayBuffer::~TunnelGatewayBuffer ()
{
2016-12-24 21:05:44 +00:00
ClearTunnelDataMsgs ();
2015-02-05 02:24:48 +00:00
}
void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block)
2013-11-10 23:19:49 +00:00
{
2014-10-21 01:39:32 +00:00
bool messageCreated = false;
2014-01-06 02:25:48 +00:00
if (!m_CurrentTunnelDataMsg)
2014-10-21 01:39:32 +00:00
{
2014-01-06 02:25:48 +00:00
CreateCurrentTunnelDataMessage ();
2014-10-21 01:39:32 +00:00
messageCreated = true;
}
2014-01-06 02:25:48 +00:00
// create delivery instructions
2014-01-07 01:56:44 +00:00
uint8_t di[43]; // max delivery instruction length is 43 for tunnel
2014-01-06 02:25:48 +00:00
size_t diLen = 1;// flag
if (block.deliveryType != eDeliveryTypeLocal) // tunnel or router
2013-11-10 23:19:49 +00:00
{
if (block.deliveryType == eDeliveryTypeTunnel)
2014-01-06 02:25:48 +00:00
{
htobe32buf (di + diLen, block.tunnelID);
2014-01-06 02:25:48 +00:00
diLen += 4; // tunnelID
}
memcpy (di + diLen, block.hash, 32);
2014-01-06 02:25:48 +00:00
diLen += 32; //len
2013-11-10 23:19:49 +00:00
}
di[0] = block.deliveryType << 5; // set delivery type
2014-01-06 02:25:48 +00:00
// create fragments
2016-12-24 21:05:44 +00:00
const std::shared_ptr<I2NPMessage> & msg = block.data;
2016-06-28 16:20:18 +00:00
size_t fullMsgLen = diLen + msg->GetLength () + 2; // delivery instructions + payload + 2 bytes length
2014-10-21 01:39:32 +00:00
if (fullMsgLen <= m_RemainingSize)
{
2014-01-06 02:25:48 +00:00
// message fits. First and last fragment
htobe16buf (di + diLen, msg->GetLength ());
2014-01-06 02:25:48 +00:00
diLen += 2; // size
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen);
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), msg->GetLength ());
m_CurrentTunnelDataMsg->len += diLen + msg->GetLength ();
m_RemainingSize -= diLen + msg->GetLength ();
if (!m_RemainingSize)
CompleteCurrentTunnelDataMessage ();
}
2014-01-06 02:25:48 +00:00
else
{
2014-10-21 01:39:32 +00:00
if (!messageCreated) // check if we should complete previous message
{
2016-06-28 16:20:18 +00:00
size_t numFollowOnFragments = fullMsgLen / TUNNEL_DATA_MAX_PAYLOAD_SIZE;
2014-10-21 01:39:32 +00:00
// length of bytes don't fit full tunnel message
// every follow-on fragment adds 7 bytes
2016-06-28 16:20:18 +00:00
size_t nonFit = (fullMsgLen + numFollowOnFragments*7) % TUNNEL_DATA_MAX_PAYLOAD_SIZE;
2014-10-21 01:39:32 +00:00
if (!nonFit || nonFit > m_RemainingSize)
{
CompleteCurrentTunnelDataMessage ();
CreateCurrentTunnelDataMessage ();
}
}
2014-01-06 02:25:48 +00:00
if (diLen + 6 <= m_RemainingSize)
{
2014-01-06 02:25:48 +00:00
// delivery instructions fit
2015-01-02 04:00:33 +00:00
uint32_t msgID;
memcpy (&msgID, msg->GetHeader () + I2NP_HEADER_MSGID_OFFSET, 4); // in network bytes order
2014-01-06 02:25:48 +00:00
size_t size = m_RemainingSize - diLen - 6; // 6 = 4 (msgID) + 2 (size)
// first fragment
di[0] |= 0x08; // fragmented
htobuf32 (di + diLen, msgID);
2014-01-06 02:25:48 +00:00
diLen += 4; // Message ID
htobe16buf (di + diLen, size);
2014-01-06 02:25:48 +00:00
diLen += 2; // size
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len, di, diLen);
memcpy (m_CurrentTunnelDataMsg->buf + m_CurrentTunnelDataMsg->len + diLen, msg->GetBuffer (), size);
m_CurrentTunnelDataMsg->len += diLen + size;
CompleteCurrentTunnelDataMessage ();
// follow on fragments
int fragmentNumber = 1;
while (size < msg->GetLength ())
{
CreateCurrentTunnelDataMessage ();
uint8_t * buf = m_CurrentTunnelDataMsg->GetBuffer ();
buf[0] = 0x80 | (fragmentNumber << 1); // frag
bool isLastFragment = false;
size_t s = msg->GetLength () - size;
if (s > TUNNEL_DATA_MAX_PAYLOAD_SIZE - 7) // 7 follow on instructions
s = TUNNEL_DATA_MAX_PAYLOAD_SIZE - 7;
else // last fragment
{
buf[0] |= 0x01;
isLastFragment = true;
}
htobuf32 (buf + 1, msgID); //Message ID
htobe16buf (buf + 5, s); // size
2014-01-06 02:25:48 +00:00
memcpy (buf + 7, msg->GetBuffer () + size, s);
m_CurrentTunnelDataMsg->len += s+7;
if (isLastFragment)
{
2016-12-24 21:05:44 +00:00
if(m_RemainingSize < (s+7)) {
LogPrint (eLogError, "TunnelGateway: remaining size overflow: ", m_RemainingSize, " < ", s+7);
} else {
m_RemainingSize -= s+7;
if (m_RemainingSize == 0)
CompleteCurrentTunnelDataMessage ();
}
2014-01-06 02:25:48 +00:00
}
else
CompleteCurrentTunnelDataMessage ();
size += s;
fragmentNumber++;
}
}
else
{
2014-01-06 02:25:48 +00:00
// delivery instructions don't fit. Create new message
CompleteCurrentTunnelDataMessage ();
PutI2NPMsg (block);
2014-01-06 02:25:48 +00:00
// don't delete msg because it's taken care inside
}
2014-01-06 02:25:48 +00:00
}
2013-11-10 23:19:49 +00:00
}
2014-01-06 02:25:48 +00:00
2014-06-26 18:45:34 +00:00
void TunnelGatewayBuffer::ClearTunnelDataMsgs ()
2013-11-10 23:19:49 +00:00
{
2014-06-26 18:45:34 +00:00
m_TunnelDataMsgs.clear ();
2016-12-24 21:05:44 +00:00
m_CurrentTunnelDataMsg = nullptr;
2014-06-26 18:45:34 +00:00
}
2013-11-10 23:19:49 +00:00
2014-01-06 02:25:48 +00:00
void TunnelGatewayBuffer::CreateCurrentTunnelDataMessage ()
2013-11-10 23:19:49 +00:00
{
2016-12-24 21:05:44 +00:00
m_CurrentTunnelDataMsg = nullptr;
2015-11-24 18:09:12 +00:00
m_CurrentTunnelDataMsg = NewI2NPShortMessage ();
2014-11-27 02:42:14 +00:00
m_CurrentTunnelDataMsg->Align (12);
2014-01-06 02:25:48 +00:00
// we reserve space for padding
2015-01-02 04:00:33 +00:00
m_CurrentTunnelDataMsg->offset += TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE;
2014-01-06 02:25:48 +00:00
m_CurrentTunnelDataMsg->len = m_CurrentTunnelDataMsg->offset;
m_RemainingSize = TUNNEL_DATA_MAX_PAYLOAD_SIZE;
2013-11-10 23:19:49 +00:00
}
2014-01-06 02:25:48 +00:00
void TunnelGatewayBuffer::CompleteCurrentTunnelDataMessage ()
2013-11-10 23:19:49 +00:00
{
2014-01-06 02:25:48 +00:00
if (!m_CurrentTunnelDataMsg) return;
uint8_t * payload = m_CurrentTunnelDataMsg->GetBuffer ();
size_t size = m_CurrentTunnelDataMsg->len - m_CurrentTunnelDataMsg->offset;
2015-01-02 04:00:33 +00:00
m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - I2NP_HEADER_SIZE;
2014-01-06 02:25:48 +00:00
uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload ();
2015-11-03 14:15:49 +00:00
RAND_bytes (buf + 4, 16); // original IV
2014-01-06 02:25:48 +00:00
memcpy (payload + size, buf + 4, 16); // copy IV for checksum
2013-11-10 23:19:49 +00:00
uint8_t hash[32];
2015-11-03 14:15:49 +00:00
SHA256(payload, size+16, hash);
2014-01-06 02:25:48 +00:00
memcpy (buf+20, hash, 4); // checksum
payload[-1] = 0; // zero
ptrdiff_t paddingSize = payload - buf - 25; // 25 = 24 + 1
2014-01-06 02:25:48 +00:00
if (paddingSize > 0)
2015-06-26 20:06:59 +00:00
{
// non-zero padding
2015-11-03 14:15:49 +00:00
auto randomOffset = rand () % (TUNNEL_DATA_MAX_PAYLOAD_SIZE - paddingSize + 1);
2015-06-26 20:06:59 +00:00
memcpy (buf + 24, m_NonZeroRandomBuffer + randomOffset, paddingSize);
}
2014-01-06 02:25:48 +00:00
2013-11-10 23:19:49 +00:00
// we can't fill message header yet because encryption is required
2014-01-06 02:25:48 +00:00
m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg);
m_CurrentTunnelDataMsg = nullptr;
2013-11-10 23:19:49 +00:00
}
2015-06-26 20:06:59 +00:00
void TunnelGateway::SendTunnelDataMsg (const TunnelMessageBlock& block)
{
2014-08-25 02:32:28 +00:00
if (block.data)
{
PutTunnelDataMsg (block);
SendBuffer ();
}
}
void TunnelGateway::PutTunnelDataMsg (const TunnelMessageBlock& block)
{
2014-08-25 02:32:28 +00:00
if (block.data)
m_Buffer.PutI2NPMsg (block);
}
void TunnelGateway::SendBuffer ()
{
2014-06-26 18:45:34 +00:00
m_Buffer.CompleteCurrentTunnelDataMessage ();
std::vector<std::shared_ptr<I2NPMessage> > newTunnelMsgs;
2017-02-06 17:50:54 +00:00
const auto& tunnelDataMsgs = m_Buffer.GetTunnelDataMsgs ();
for (auto& tunnelMsg : tunnelDataMsgs)
{
auto newMsg = CreateEmptyTunnelDataMsg ();
m_Tunnel->EncryptTunnelMsg (tunnelMsg, newMsg);
htobe32buf (newMsg->GetPayload (), m_Tunnel->GetNextTunnelID ());
newMsg->FillI2NPMessageHeader (eI2NPTunnelData);
newTunnelMsgs.push_back (newMsg);
2013-11-29 12:52:09 +00:00
m_NumSentBytes += TUNNEL_DATA_MSG_SIZE;
}
2014-06-26 18:45:34 +00:00
m_Buffer.ClearTunnelDataMsgs ();
i2p::transport::transports.SendMessages (m_Tunnel->GetNextIdentHash (), newTunnelMsgs);
}
2013-11-10 23:19:49 +00:00
}
}