i2pd/TunnelGateway.cpp

180 lines
5.7 KiB
C++
Raw Normal View History

2013-11-10 23:19:49 +00:00
#include <string.h>
#include "I2PEndian.h"
2013-11-10 23:19:49 +00:00
#include <cryptopp/sha.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
{
void TunnelGatewayBuffer::PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg)
{
2014-01-06 02:25:48 +00:00
if (!m_CurrentTunnelDataMsg)
CreateCurrentTunnelDataMessage ();
// 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
TunnelDeliveryType dt = eDeliveryTypeLocal;
2013-11-10 23:19:49 +00:00
if (gwHash)
{
if (gwTunnel)
2014-01-06 02:25:48 +00:00
{
*(uint32_t *)(di + diLen) = htobe32 (gwTunnel);
diLen += 4; // tunnelID
dt = eDeliveryTypeTunnel;
}
2013-11-10 23:19:49 +00:00
else
2014-01-06 02:25:48 +00:00
dt = eDeliveryTypeRouter;
memcpy (di + diLen, gwHash, 32);
diLen += 32; //len
2013-11-10 23:19:49 +00:00
}
2014-01-06 02:25:48 +00:00
di[0] = dt << 5; // set delivery type
2014-01-06 02:25:48 +00:00
// create fragments
if (diLen + msg->GetLength () + 2<= m_RemainingSize)
{
2014-01-06 02:25:48 +00: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 02:25:48 +00:00
else
{
2014-01-06 02:25:48 +00:00
if (diLen + 6 <= m_RemainingSize)
{
2014-01-06 02:25:48 +00:00
// delivery instructions fit
uint32_t msgID = msg->GetHeader ()->msgID;
size_t size = m_RemainingSize - diLen - 6; // 6 = 4 (msgID) + 2 (size)
// first fragment
di[0] |= 0x08; // fragmented
*(uint32_t *)(di + diLen) = htobe32 (msgID);
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;
}
*(uint32_t *)(buf + 1) = htobe32 (msgID); //Message ID
*(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 02:25:48 +00:00
// delivery instructions don't fit. Create new message
CompleteCurrentTunnelDataMessage ();
PutI2NPMsg (gwHash, gwTunnel, msg);
// 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
std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs ()
2013-11-10 23:19:49 +00:00
{
2014-01-06 02:25:48 +00:00
CompleteCurrentTunnelDataMessage ();
std::vector<I2NPMessage *> ret = m_TunnelDataMsgs; // TODO: implement it better
m_TunnelDataMsgs.clear ();
return ret;
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
{
2014-01-06 02:25:48 +00: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-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;
2014-01-06 02:25:48 +00:00
m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - sizeof (I2NPHeader);
uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload ();
2013-11-29 12:52:09 +00:00
*(uint32_t *)(buf) = htobe32 (m_TunnelID);
2013-11-10 23:19:49 +00:00
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (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];
2014-01-06 02:25:48 +00: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 02:25:48 +00:00
if (paddingSize > 0)
memset (buf + 24, 1, paddingSize); // padding TODO: fill with random data
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
}
void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
{
PutTunnelDataMsg (gwHash, gwTunnel, msg);
SendBuffer ();
}
void TunnelGateway::PutTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
{
m_Buffer.PutI2NPMsg (gwHash, gwTunnel, msg);
}
void TunnelGateway::SendBuffer ()
{
2013-11-29 12:52:09 +00: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 12:52:09 +00:00
m_NumSentBytes += TUNNEL_DATA_MSG_SIZE;
}
}
2013-11-10 23:19:49 +00:00
}
}