Browse Source

split long message to multiple fragments

pull/6/head
orignal 11 years ago
parent
commit
89aa99c198
  1. 10
      TunnelBase.h
  2. 79
      TunnelGateway.cpp
  3. 15
      TunnelGateway.h

10
TunnelBase.h

@ -2,6 +2,7 @@
#define TUNNEL_BASE_H__ #define TUNNEL_BASE_H__
#include <inttypes.h> #include <inttypes.h>
#include "I2NPProtocol.h"
namespace i2p namespace i2p
{ {
@ -20,6 +21,15 @@ namespace tunnel
uint8_t hash[32]; uint8_t hash[32];
I2NPMessage * data; I2NPMessage * data;
}; };
class TunnelBase
{
public:
virtual void EncryptTunnelMsg (I2NPMessage * tunnelMsg) = 0;
virtual uint32_t GetNextTunnelID () const = 0;
virtual const uint8_t * GetNextIdentHash () const = 0;
};
} }
} }

79
TunnelGateway.cpp

@ -1,7 +1,9 @@
#include <string.h> #include <string.h>
#include <endian.h> #include <endian.h>
#include <cryptopp/sha.h> #include <cryptopp/sha.h>
#include "Log.h"
#include "RouterContext.h" #include "RouterContext.h"
#include "Transports.h"
#include "TunnelGateway.h" #include "TunnelGateway.h"
namespace i2p namespace i2p
@ -27,6 +29,7 @@ namespace tunnel
} }
else else
block->deliveryType = eDeliveryTypeLocal; block->deliveryType = eDeliveryTypeLocal;
block->deliveryInstructionsLen += 2; // size
// we don't reserve 4 bytes for msgID because we don't if it fits // we don't reserve 4 bytes for msgID because we don't if it fits
block->totalLen = block->deliveryInstructionsLen + msg->GetLength (); block->totalLen = block->deliveryInstructionsLen + msg->GetLength ();
block->data = msg; block->data = msg;
@ -36,34 +39,23 @@ namespace tunnel
std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs (uint32_t tunnelID) std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs (uint32_t tunnelID)
{ {
std::vector<I2NPMessage *> res; std::vector<I2NPMessage *> res;
int cnt = m_I2NPMsgs.size (), pos = 0, prev = 0; int cnt = m_I2NPMsgs.size ();
m_NextOffset = 0; m_NextOffset = 0;
if (cnt > 0) if (cnt > 0)
{ {
size_t size = 0; for (auto m: m_I2NPMsgs)
while (pos < cnt)
{
TunnelMessageBlockExt * block = m_I2NPMsgs[pos];
if (size + block->totalLen >= 1003) // 1003 = 1008 - checksum - zero
{
// we have to make sure if we can put delivery instructions + msgID of last message
if (size + block->deliveryInstructionsLen + 4 > 1003)
{ {
// we have to exclude last message if (m->totalLen <= 1003)
pos--; res.push_back (CreateNextTunnelMessage (tunnelID, m, m->totalLen));
}
else
size = 1003;
res.push_back (CreateNextTunnelMessage (tunnelID, prev, pos, size));
prev = pos;
}
else else
size += block->totalLen; {
pos++; res.push_back (CreateNextTunnelMessage (tunnelID, m, 1003));
size_t remaining = m->data->GetLength () - m_NextOffset; // remaining payload
remaining += 7; // follow-on header
res.push_back (CreateNextTunnelMessage (tunnelID, m, remaining));
} }
res.push_back (CreateNextTunnelMessage (tunnelID, prev, pos, size)); // last message
for (auto m: m_I2NPMsgs)
delete m; delete m;
}
m_I2NPMsgs.clear (); m_I2NPMsgs.clear ();
} }
@ -94,7 +86,7 @@ namespace tunnel
*(uint32_t *)(buf + ret) = m_NextMsgID; *(uint32_t *)(buf + ret) = m_NextMsgID;
ret += 4; // msgID ret += 4; // msgID
m_NextSeqn = 1; m_NextSeqn = 1;
size -= (block->totalLen - len); size = len - ret - 2; // 2 bytes for size field
m_NextOffset = size; m_NextOffset = size;
} }
*(uint16_t *)(buf + ret) = htobe16 (size); // size *(uint16_t *)(buf + ret) = htobe16 (size); // size
@ -109,10 +101,10 @@ namespace tunnel
int ret = 0; int ret = 0;
buf[0] = 0x80 | (m_NextSeqn << 1);// follow-on flag and seqn buf[0] = 0x80 | (m_NextSeqn << 1);// follow-on flag and seqn
size_t fragmentLen = len - 7; // 7 bytes of header size_t fragmentLen = len - 7; // 7 bytes of header
if (fragmentLen >= block->totalLen - m_NextOffset) if (fragmentLen >= block->data->GetLength () - m_NextOffset)
{ {
// fragment fits // fragment fits
fragmentLen = block->totalLen - m_NextOffset; fragmentLen = block->data->GetLength () - m_NextOffset;
buf[0] |= 0x01; // last fragment buf[0] |= 0x01; // last fragment
} }
else else
@ -129,7 +121,7 @@ namespace tunnel
} }
I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (uint32_t tunnelID, I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (uint32_t tunnelID,
int from, int to, size_t size) TunnelMessageBlockExt * block, size_t size)
{ {
I2NPMessage * tunnelMsg = NewI2NPMessage (); I2NPMessage * tunnelMsg = NewI2NPMessage ();
uint8_t * buf = tunnelMsg->GetPayload (); uint8_t * buf = tunnelMsg->GetPayload ();
@ -137,30 +129,41 @@ namespace tunnel
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
rnd.GenerateBlock (buf + 4, 16); // original IV rnd.GenerateBlock (buf + 4, 16); // original IV
memcpy (buf + 1028, buf + 4, 16); // copy IV for checksum memcpy (buf + 1028, buf + 4, 16); // copy IV for checksum
size_t zero = 1028 - size; size_t zero = 1028 - size -1;
buf[zero] = 0; // zero buf[zero] = 0; // zero
buf += zero;
for (int i = from; i <= to; i++) if (m_NextOffset)
{
TunnelMessageBlockExt * block = m_I2NPMsgs[i];
size_t s = CreateFirstFragment (block, buf, size);
if (s < size)
{ {
size -= s; size_t s = CreateFollowOnFragment (block, buf + zero + 1, 1003);
buf += s; if (s != size)
LogPrint ("Follow-on fragment size mismatch ", s, "!=", size);
} }
else else
break; CreateFirstFragment (block, buf + zero + 1, 1003);
}
uint8_t hash[32]; uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, buf+zero+1, size+16); CryptoPP::SHA256().CalculateDigest(hash, buf+zero+1, size+16);
memcpy (buf+20, hash, 4); // checksum memcpy (buf+20, hash, 4); // checksum
if (zero > 25) if (zero > 24)
memset (buf+24, 1, zero-25); // padding memset (buf+24, 1, zero-24); // padding
tunnelMsg->len += 1028;
// we can't fill message header yet because encryption is required // we can't fill message header yet because encryption is required
return tunnelMsg; return tunnelMsg;
} }
void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)
{
m_Buffer.PutI2NPMsg (gwHash, gwTunnel, msg);
auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs (m_Tunnel->GetNextTunnelID ());
for (auto tunnelMsg : tunnelMsgs)
{
m_Tunnel->EncryptTunnelMsg (tunnelMsg);
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
i2p::transports.SendMessage (m_Tunnel->GetNextIdentHash (), tunnelMsg);
}
}
} }
} }

15
TunnelGateway.h

@ -26,7 +26,7 @@ namespace tunnel
size_t CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len); size_t CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len);
size_t CreateFollowOnFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len); size_t CreateFollowOnFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len);
I2NPMessage * CreateNextTunnelMessage (uint32_t tunnelID, int from, int to, size_t size); I2NPMessage * CreateNextTunnelMessage (uint32_t tunnelID, TunnelMessageBlockExt * block, size_t size);
private: private:
@ -35,6 +35,19 @@ namespace tunnel
size_t m_NextOffset, m_NextSeqn; size_t m_NextOffset, m_NextSeqn;
uint32_t m_NextMsgID; uint32_t m_NextMsgID;
}; };
class TunnelGateway
{
public:
TunnelGateway (TunnelBase * tunnel): m_Tunnel (tunnel) {};
void SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg);
private:
TunnelBase * m_Tunnel;
TunnelGatewayBuffer m_Buffer;
};
} }
} }

Loading…
Cancel
Save