From 89aa99c198e7746cc0e4e57eb4d7f1b2cb6e113e Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 13 Nov 2013 07:56:16 -0500 Subject: [PATCH] split long message to multiple fragments --- TunnelBase.h | 10 ++++++ TunnelGateway.cpp | 85 ++++++++++++++++++++++++----------------------- TunnelGateway.h | 15 ++++++++- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/TunnelBase.h b/TunnelBase.h index e9d45acd..83ed4b72 100644 --- a/TunnelBase.h +++ b/TunnelBase.h @@ -2,6 +2,7 @@ #define TUNNEL_BASE_H__ #include +#include "I2NPProtocol.h" namespace i2p { @@ -20,6 +21,15 @@ namespace tunnel uint8_t hash[32]; I2NPMessage * data; }; + + class TunnelBase + { + public: + + virtual void EncryptTunnelMsg (I2NPMessage * tunnelMsg) = 0; + virtual uint32_t GetNextTunnelID () const = 0; + virtual const uint8_t * GetNextIdentHash () const = 0; + }; } } diff --git a/TunnelGateway.cpp b/TunnelGateway.cpp index 8333fb98..f6787605 100644 --- a/TunnelGateway.cpp +++ b/TunnelGateway.cpp @@ -1,7 +1,9 @@ #include #include #include +#include "Log.h" #include "RouterContext.h" +#include "Transports.h" #include "TunnelGateway.h" namespace i2p @@ -27,6 +29,7 @@ namespace tunnel } else block->deliveryType = eDeliveryTypeLocal; + block->deliveryInstructionsLen += 2; // size // we don't reserve 4 bytes for msgID because we don't if it fits block->totalLen = block->deliveryInstructionsLen + msg->GetLength (); block->data = msg; @@ -36,34 +39,23 @@ namespace tunnel std::vector TunnelGatewayBuffer::GetTunnelDataMsgs (uint32_t tunnelID) { std::vector res; - int cnt = m_I2NPMsgs.size (), pos = 0, prev = 0; + int cnt = m_I2NPMsgs.size (); m_NextOffset = 0; if (cnt > 0) { - size_t size = 0; - while (pos < cnt) + for (auto m: m_I2NPMsgs) { - 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 - pos--; - } - else - size = 1003; - res.push_back (CreateNextTunnelMessage (tunnelID, prev, pos, size)); - prev = pos; - } + if (m->totalLen <= 1003) + res.push_back (CreateNextTunnelMessage (tunnelID, m, m->totalLen)); else - size += block->totalLen; - pos++; - } - res.push_back (CreateNextTunnelMessage (tunnelID, prev, pos, size)); // last message - for (auto m: m_I2NPMsgs) + { + 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)); + } delete m; + } m_I2NPMsgs.clear (); } @@ -94,7 +86,7 @@ namespace tunnel *(uint32_t *)(buf + ret) = m_NextMsgID; ret += 4; // msgID m_NextSeqn = 1; - size -= (block->totalLen - len); + size = len - ret - 2; // 2 bytes for size field m_NextOffset = size; } *(uint16_t *)(buf + ret) = htobe16 (size); // size @@ -109,10 +101,10 @@ namespace tunnel int ret = 0; buf[0] = 0x80 | (m_NextSeqn << 1);// follow-on flag and seqn size_t fragmentLen = len - 7; // 7 bytes of header - if (fragmentLen >= block->totalLen - m_NextOffset) + if (fragmentLen >= block->data->GetLength () - m_NextOffset) { // fragment fits - fragmentLen = block->totalLen - m_NextOffset; + fragmentLen = block->data->GetLength () - m_NextOffset; buf[0] |= 0x01; // last fragment } else @@ -129,7 +121,7 @@ namespace tunnel } I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (uint32_t tunnelID, - int from, int to, size_t size) + TunnelMessageBlockExt * block, size_t size) { I2NPMessage * tunnelMsg = NewI2NPMessage (); uint8_t * buf = tunnelMsg->GetPayload (); @@ -137,30 +129,41 @@ namespace tunnel CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); rnd.GenerateBlock (buf + 4, 16); // original IV 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; - for (int i = from; i <= to; i++) - { - TunnelMessageBlockExt * block = m_I2NPMsgs[i]; - size_t s = CreateFirstFragment (block, buf, size); - if (s < size) - { - size -= s; - buf += s; - } - else - break; + + if (m_NextOffset) + { + size_t s = CreateFollowOnFragment (block, buf + zero + 1, 1003); + if (s != size) + LogPrint ("Follow-on fragment size mismatch ", s, "!=", size); } + else + CreateFirstFragment (block, buf + zero + 1, 1003); + uint8_t hash[32]; CryptoPP::SHA256().CalculateDigest(hash, buf+zero+1, size+16); memcpy (buf+20, hash, 4); // checksum - if (zero > 25) - memset (buf+24, 1, zero-25); // padding + if (zero > 24) + memset (buf+24, 1, zero-24); // padding + tunnelMsg->len += 1028; // we can't fill message header yet because encryption is required 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); + } + } } } diff --git a/TunnelGateway.h b/TunnelGateway.h index 71a24336..fd1bdd30 100644 --- a/TunnelGateway.h +++ b/TunnelGateway.h @@ -26,7 +26,7 @@ namespace tunnel size_t CreateFirstFragment (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: @@ -35,6 +35,19 @@ namespace tunnel size_t m_NextOffset, m_NextSeqn; 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; + }; } }