i2pd/TunnelGateway.cpp

180 lines
5.6 KiB
C++
Raw Normal View History

2013-11-11 03:19:49 +04:00
#include <string.h>
#include "I2PEndian.h"
2013-11-11 03:19:49 +04:00
#include <cryptopp/sha.h>
#include "Log.h"
2013-11-11 03:19:49 +04:00
#include "RouterContext.h"
#include "Transports.h"
2013-11-11 03:19:49 +04:00
#include "TunnelGateway.h"
namespace i2p
{
namespace tunnel
{
void TunnelGatewayBuffer::PutI2NPMsg (const TunnelMessageBlock& block)
2013-11-11 03:19:49 +04:00
{
2014-01-06 06:25:48 +04:00
if (!m_CurrentTunnelDataMsg)
CreateCurrentTunnelDataMessage ();
// create delivery instructions
2014-01-07 05:56:44 +04:00
uint8_t di[43]; // max delivery instruction length is 43 for tunnel
2014-01-06 06:25:48 +04:00
size_t diLen = 1;// flag
if (block.deliveryType != eDeliveryTypeLocal) // tunnel or router
2013-11-11 03:19:49 +04:00
{
if (block.deliveryType == eDeliveryTypeTunnel)
2014-01-06 06:25:48 +04:00
{
*(uint32_t *)(di + diLen) = htobe32 (block.tunnelID);
2014-01-06 06:25:48 +04:00
diLen += 4; // tunnelID
}
memcpy (di + diLen, block.hash, 32);
2014-01-06 06:25:48 +04:00
diLen += 32; //len
2013-11-11 03:19:49 +04:00
}
di[0] = block.deliveryType << 5; // set delivery type
2014-01-06 06:25:48 +04:00
// create fragments
I2NPMessage * msg = block.data;
2014-01-06 06:25:48 +04:00
if (diLen + msg->GetLength () + 2<= m_RemainingSize)
{
2014-01-06 06:25:48 +04:00
// message fits. First and last fragment
*(uint16_t *)(di + diLen) = htobe16 (msg->GetLength ());
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 ();
DeleteI2NPMessage (msg);
}
2014-01-06 06:25:48 +04:00
else
{
2014-01-06 06:25:48 +04:00
if (diLen + 6 <= m_RemainingSize)
{
2014-01-06 06:25:48 +04:00
// delivery instructions fit
2014-09-04 02:52:22 +04:00
uint32_t msgID = msg->GetHeader ()->msgID; // in network bytes order
2014-01-06 06:25:48 +04:00
size_t size = m_RemainingSize - diLen - 6; // 6 = 4 (msgID) + 2 (size)
// first fragment
di[0] |= 0x08; // fragmented
2014-09-04 02:52:22 +04:00
*(uint32_t *)(di + diLen) = msgID;
2014-01-06 06:25:48 +04:00
diLen += 4; // Message ID
*(uint16_t *)(di + diLen) = htobe16 (size);
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;
}
2014-09-04 02:52:22 +04:00
*(uint32_t *)(buf + 1) = msgID; //Message ID
2014-01-06 06:25:48 +04:00
*(uint16_t *)(buf + 5) = htobe16 (s); // size
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++;
}
DeleteI2NPMessage (msg);
}
else
{
2014-01-06 06:25:48 +04:00
// delivery instructions don't fit. Create new message
CompleteCurrentTunnelDataMessage ();
PutI2NPMsg (block);
2014-01-06 06:25:48 +04:00
// don't delete msg because it's taken care inside
}
2014-01-06 06:25:48 +04:00
}
2013-11-11 03:19:49 +04:00
}
2014-01-06 06:25:48 +04:00
2014-06-26 22:45:34 +04:00
void TunnelGatewayBuffer::ClearTunnelDataMsgs ()
2013-11-11 03:19:49 +04:00
{
2014-06-26 22:45:34 +04:00
m_TunnelDataMsgs.clear ();
}
2013-11-11 03:19:49 +04:00
2014-01-06 06:25:48 +04:00
void TunnelGatewayBuffer::CreateCurrentTunnelDataMessage ()
2013-11-11 03:19:49 +04:00
{
2014-01-06 06:25:48 +04:00
m_CurrentTunnelDataMsg = NewI2NPMessage ();
// we reserve space for padding
m_CurrentTunnelDataMsg->offset += TUNNEL_DATA_MSG_SIZE + sizeof (I2NPHeader);
m_CurrentTunnelDataMsg->len = m_CurrentTunnelDataMsg->offset;
m_RemainingSize = TUNNEL_DATA_MAX_PAYLOAD_SIZE;
2013-11-11 03:19:49 +04:00
}
2014-01-06 06:25:48 +04:00
void TunnelGatewayBuffer::CompleteCurrentTunnelDataMessage ()
2013-11-11 03:19:49 +04:00
{
2014-01-06 06:25:48 +04:00
if (!m_CurrentTunnelDataMsg) return;
uint8_t * payload = m_CurrentTunnelDataMsg->GetBuffer ();
size_t size = m_CurrentTunnelDataMsg->len - m_CurrentTunnelDataMsg->offset;
2014-01-06 06:25:48 +04:00
m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - sizeof (I2NPHeader);
uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload ();
2013-11-29 16:52:09 +04:00
*(uint32_t *)(buf) = htobe32 (m_TunnelID);
2013-11-11 03:19:49 +04:00
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (buf + 4, 16); // original IV
2014-01-06 06:25:48 +04:00
memcpy (payload + size, buf + 4, 16); // copy IV for checksum
2013-11-11 03:19:49 +04:00
uint8_t hash[32];
2014-01-06 06:25:48 +04:00
CryptoPP::SHA256().CalculateDigest (hash, payload, size+16);
memcpy (buf+20, hash, 4); // checksum
payload[-1] = 0; // zero
ptrdiff_t paddingSize = payload - buf - 25; // 25 = 24 + 1
2014-01-06 06:25:48 +04:00
if (paddingSize > 0)
memset (buf + 24, 1, paddingSize); // padding TODO: fill with random data
2013-11-11 03:19:49 +04:00
// we can't fill message header yet because encryption is required
2014-01-06 06:25:48 +04:00
m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg);
m_CurrentTunnelDataMsg = nullptr;
2013-11-11 03:19:49 +04:00
}
void TunnelGateway::SendTunnelDataMsg (const TunnelMessageBlock& block)
{
2014-08-25 06:32:28 +04:00
if (block.data)
{
PutTunnelDataMsg (block);
SendBuffer ();
}
}
void TunnelGateway::PutTunnelDataMsg (const TunnelMessageBlock& block)
{
2014-08-25 06:32:28 +04:00
if (block.data)
m_Buffer.PutI2NPMsg (block);
}
void TunnelGateway::SendBuffer ()
{
2014-06-26 22:45:34 +04:00
m_Buffer.CompleteCurrentTunnelDataMessage ();
2013-11-29 16:52:09 +04:00
auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs ();
for (auto tunnelMsg : tunnelMsgs)
{
m_Tunnel->EncryptTunnelMsg (tunnelMsg);
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
i2p::transports.SendMessage (m_Tunnel->GetNextIdentHash (), tunnelMsg);
2013-11-29 16:52:09 +04:00
m_NumSentBytes += TUNNEL_DATA_MSG_SIZE;
}
2014-06-26 22:45:34 +04:00
m_Buffer.ClearTunnelDataMsgs ();
}
2013-11-11 03:19:49 +04:00
}
}