i2pd/TunnelGateway.cpp

213 lines
6.6 KiB
C++
Raw Normal View History

2013-11-10 23:19:49 +00:00
#include <string.h>
2015-11-03 14:15:49 +00:00
#include <openssl/rand.h>
#include <openssl/sha.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
{
2015-06-26 20:06:59 +00:00
TunnelGatewayBuffer::TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID),
m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0)
{
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 ()
{
}
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
std::shared_ptr<I2NPMessage> msg = block.data;
2014-10-21 01:39:32 +00:00
auto fullMsgLen = diLen + msg->GetLength () + 2; // delivery instructions + payload + 2 bytes length
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
{
auto numFollowOnFragments = fullMsgLen / TUNNEL_DATA_MAX_PAYLOAD_SIZE;
// length of bytes don't fit full tunnel message
// every follow-on fragment adds 7 bytes
auto nonFit = (fullMsgLen + numFollowOnFragments*7) % TUNNEL_DATA_MAX_PAYLOAD_SIZE;
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)
{
m_RemainingSize -= s+7;
if (!m_RemainingSize)
CompleteCurrentTunnelDataMessage ();
}
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 ();
}
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
{
m_CurrentTunnelDataMsg = ToSharedI2NPMessage (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 ();
htobe32buf (buf, m_TunnelID);
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 ();
2013-11-29 12:52:09 +00:00
auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs ();
for (auto tunnelMsg : tunnelMsgs)
{
m_Tunnel->EncryptTunnelMsg (tunnelMsg, tunnelMsg);
tunnelMsg->FillI2NPMessageHeader (eI2NPTunnelData);
2013-11-29 12:52:09 +00:00
m_NumSentBytes += TUNNEL_DATA_MSG_SIZE;
}
2015-01-21 02:05:57 +00:00
i2p::transport::transports.SendMessages (m_Tunnel->GetNextIdentHash (), tunnelMsgs);
2014-06-26 18:45:34 +00:00
m_Buffer.ClearTunnelDataMsgs ();
}
2013-11-10 23:19:49 +00:00
}
}