Browse Source

long tunnel messages

pull/8/head
orignal 11 years ago
parent
commit
4b9d2d60e2
  1. 7
      Tunnel.cpp
  2. 276
      TunnelGateway.cpp
  3. 23
      TunnelGateway.h

7
Tunnel.cpp

@ -65,14 +65,9 @@ namespace tunnel
FillI2NPMessageHeader (msg, eI2NPVariableTunnelBuild); FillI2NPMessageHeader (msg, eI2NPVariableTunnelBuild);
if (outboundTunnel) if (outboundTunnel)
{ outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg);
outboundTunnel->SendTunnelDataMsg (GetNextIdentHash (), 0, msg);
DeleteI2NPMessage (msg);
}
else else
{
i2p::transports.SendMessage (GetNextIdentHash (), msg); i2p::transports.SendMessage (GetNextIdentHash (), msg);
}
} }
bool Tunnel::HandleTunnelBuildResponse (uint8_t * msg, size_t len) bool Tunnel::HandleTunnelBuildResponse (uint8_t * msg, size_t len)

276
TunnelGateway.cpp

@ -12,198 +12,144 @@ namespace tunnel
{ {
void TunnelGatewayBuffer::PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg) void TunnelGatewayBuffer::PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg)
{ {
TunnelMessageBlockExt * block = new TunnelMessageBlockExt; if (!m_CurrentTunnelDataMsg)
block->deliveryInstructionsLen = 1; // flag CreateCurrentTunnelDataMessage ();
// create delivery instructions
uint8_t di[40];
size_t diLen = 1;// flag
TunnelDeliveryType dt = eDeliveryTypeLocal;
if (gwHash) if (gwHash)
{ {
block->deliveryInstructionsLen += 32; // hash
memcpy (block->hash, gwHash, 32);
if (gwTunnel) if (gwTunnel)
{ {
block->deliveryType = eDeliveryTypeTunnel; *(uint32_t *)(di + diLen) = htobe32 (gwTunnel);
block->deliveryInstructionsLen += 4; // tunnelID diLen += 4; // tunnelID
block->tunnelID = gwTunnel; dt = eDeliveryTypeTunnel;
} }
else else
block->deliveryType = eDeliveryTypeRouter; dt = eDeliveryTypeRouter;
memcpy (di + diLen, gwHash, 32);
diLen += 32; //len
} }
else di[0] = dt << 5; // set delivery type
block->deliveryType = eDeliveryTypeLocal;
block->deliveryInstructionsLen += 2; // size
// we don't reserve 4 bytes for msgID yet
block->totalLen = block->deliveryInstructionsLen + msg->GetLength ();
block->data = msg;
m_I2NPMsgs.push_back (block);
if (!m_Remaining) m_Remaining = TUNNEL_DATA_MAX_PAYLOAD_SIZE; // create fragments
if (block->totalLen <= m_Remaining) // message fits if (diLen + msg->GetLength () + 2<= m_RemainingSize)
{ {
block->isFragmented = false; // message fits. First and last fragment
m_Remaining -= block->totalLen; *(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);
} }
else // message doesn't fit else
{ {
if (block->deliveryInstructionsLen + 4 <= m_Remaining) if (diLen + 6 <= m_RemainingSize)
{ {
// delivery instructions of first fragment fits // delivery instructions fit
block->isFragmented = true; uint32_t msgID = msg->GetHeader ()->msgID;
block->deliveryInstructionsLen += 4; size_t size = m_RemainingSize - diLen - 6; // 6 = 4 (msgID) + 2 (size)
block->totalLen += 4;
m_Remaining = m_Remaining + TUNNEL_DATA_MAX_PAYLOAD_SIZE - block->totalLen - 7; // TODO: handle case if more than two fragments // 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 else
{ {
// delivery instructions of first fragment don't fit // delivery instructions don't fit. Create new message
block->isFragmented = false; CompleteCurrentTunnelDataMessage ();
m_Remaining = 0; PutI2NPMsg (gwHash, gwTunnel, msg);
// don't delete msg because it's taken care inside
} }
} }
}
std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs ()
{
m_Remaining = 0;
m_NextOffset = 0;
std::vector<I2NPMessage *> res;
int cnt = m_I2NPMsgs.size ();
if (cnt > 0)
{
int ind = 0;
while (ind < cnt)
{
auto tunnelMsg = CreateNextTunnelMessage (ind);
if (!tunnelMsg) break;
res.push_back (tunnelMsg);
}
for (auto msg: m_I2NPMsgs)
delete msg;
m_I2NPMsgs.clear ();
}
return res;
} }
size_t TunnelGatewayBuffer::CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len) std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs ()
{ {
size_t ret = 1; CompleteCurrentTunnelDataMessage ();
buf[0] = block->deliveryType << 5; // flag std::vector<I2NPMessage *> ret = m_TunnelDataMsgs; // TODO: implement it better
if (block->deliveryType == eDeliveryTypeTunnel) m_TunnelDataMsgs.clear ();
{ return ret;
*(uint32_t *)(buf + ret) = htobe32 (block->tunnelID);
ret += 4;
}
if (block->deliveryType == eDeliveryTypeTunnel || block->deliveryType == eDeliveryTypeRouter)
{
memcpy (buf + ret, block->hash, 32);
ret += 32;
}
size_t size = block->data->GetLength ();
if (block->totalLen > len) // entire message doesn't fit
{
buf[0] |= 0x08; // set fragmented bit
m_NextMsgID = block->data->GetHeader ()->msgID;
*(uint32_t *)(buf + ret) = m_NextMsgID;
ret += 4; // msgID
m_NextSeqn = 1;
size = len - ret - 2; // 2 bytes for size field
m_NextOffset = size;
}
*(uint16_t *)(buf + ret) = htobe16 (size); // size
ret += 2;
memcpy (buf + ret, block->data->GetBuffer (), size);
ret += size;
return ret;
} }
size_t TunnelGatewayBuffer::CreateFollowOnFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len) void TunnelGatewayBuffer::CreateCurrentTunnelDataMessage ()
{ {
int ret = 0; m_CurrentTunnelDataMsg = NewI2NPMessage ();
buf[0] = 0x80 | (m_NextSeqn << 1);// follow-on flag and seqn // we reserve space for padding
size_t fragmentLen = len - 7; // 7 bytes of header m_CurrentTunnelDataMsg->offset += TUNNEL_DATA_MSG_SIZE + sizeof (I2NPHeader);
if (fragmentLen >= block->data->GetLength () - m_NextOffset) m_CurrentTunnelDataMsg->len = m_CurrentTunnelDataMsg->offset;
{ m_RemainingSize = TUNNEL_DATA_MAX_PAYLOAD_SIZE;
// fragment fits
fragmentLen = block->data->GetLength () - m_NextOffset;
buf[0] |= 0x01; // last fragment
}
else
m_NextSeqn++;
*(uint32_t *)(buf + 1) = m_NextMsgID; // msgID
*(uint16_t *)(buf + 5) = htobe16 (fragmentLen); // size
memcpy (buf + 7, block->data->GetBuffer () + m_NextOffset, fragmentLen);
m_NextOffset += fragmentLen;
ret += fragmentLen + 7;
return ret;
} }
I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (int& ind) void TunnelGatewayBuffer::CompleteCurrentTunnelDataMessage ()
{ {
int cnt = m_I2NPMsgs.size (); if (!m_CurrentTunnelDataMsg) return;
if (ind > cnt - 1) return nullptr; // no more messages uint8_t * payload = m_CurrentTunnelDataMsg->GetBuffer ();
// calculate payload size size_t size = m_CurrentTunnelDataMsg->len - m_CurrentTunnelDataMsg->offset;
size_t size = 0;
int i = ind;
if (m_NextOffset)
{
size = m_I2NPMsgs[i]->data->GetLength () - m_NextOffset + 7; // including follow-on header
i++;
}
while (i < cnt)
{
auto msg = m_I2NPMsgs[i];
size += msg->totalLen;
if (size >= TUNNEL_DATA_MAX_PAYLOAD_SIZE)
{
size = TUNNEL_DATA_MAX_PAYLOAD_SIZE;
break;
}
if (msg->isFragmented) break;
i++;
}
I2NPMessage * tunnelMsg = NewI2NPMessage (); m_CurrentTunnelDataMsg->offset = m_CurrentTunnelDataMsg->len - TUNNEL_DATA_MSG_SIZE - sizeof (I2NPHeader);
uint8_t * buf = tunnelMsg->GetPayload (); uint8_t * buf = m_CurrentTunnelDataMsg->GetPayload ();
*(uint32_t *)(buf) = htobe32 (m_TunnelID); *(uint32_t *)(buf) = htobe32 (m_TunnelID);
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 + TUNNEL_DATA_MSG_SIZE, buf + 4, 16); // copy IV for checksum memcpy (payload + size, buf + 4, 16); // copy IV for checksum
size_t zero = TUNNEL_DATA_MSG_SIZE - size -1;
buf[zero] = 0; // zero
size_t s = 0;
while (ind < cnt)
{
auto msg = m_I2NPMsgs[ind];
if (m_NextOffset)
{
s += CreateFollowOnFragment (msg, buf + zero + 1 + s, size - s);
m_NextOffset = 0; // TODO:
}
else
{
s += CreateFirstFragment (msg, buf + zero + 1 + s, size - s);
if (msg->isFragmented) break; // payload is full, but we stay at the same message
}
ind++;
if (s >= size) break; // payload is full but we moved to next message
}
if (s != size)
{
LogPrint ("TunnelData payload size mismatch ", s, "!=", size);
return nullptr;
}
uint8_t hash[32]; uint8_t hash[32];
CryptoPP::SHA256().CalculateDigest(hash, buf+zero+1, size+16); CryptoPP::SHA256().CalculateDigest (hash, payload, size+16);
memcpy (buf+20, hash, 4); // checksum memcpy (buf+20, hash, 4); // checksum
if (zero > 24) payload[-1] = 0; // zero
memset (buf+24, 1, zero-24); // padding TODO: fill with random data ssize_t paddingSize = payload - buf - 25; // 25 = 24 + 1
tunnelMsg->len += TUNNEL_DATA_MSG_SIZE; if (paddingSize > 0)
memset (buf + 24, 1, paddingSize); // padding TODO: fill with random data
// we can't fill message header yet because encryption is required // we can't fill message header yet because encryption is required
return tunnelMsg; m_TunnelDataMsgs.push_back (m_CurrentTunnelDataMsg);
m_CurrentTunnelDataMsg = nullptr;
} }
void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg) void TunnelGateway::SendTunnelDataMsg (const uint8_t * gwHash, uint32_t gwTunnel, i2p::I2NPMessage * msg)

23
TunnelGateway.h

@ -12,32 +12,23 @@ namespace tunnel
{ {
class TunnelGatewayBuffer class TunnelGatewayBuffer
{ {
struct TunnelMessageBlockExt: public TunnelMessageBlock
{
size_t deliveryInstructionsLen, totalLen;
bool isFragmented;
};
public: public:
TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID),
TunnelGatewayBuffer (uint32_t tunnelID): m_TunnelID (tunnelID), m_Remaining (0) {}; m_CurrentTunnelDataMsg (nullptr), m_RemainingSize (0) {};
void PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg); void PutI2NPMsg (const uint8_t * gwHash, uint32_t gwTunnel, I2NPMessage * msg);
std::vector<I2NPMessage *> GetTunnelDataMsgs (); std::vector<I2NPMessage *> GetTunnelDataMsgs ();
private: private:
size_t CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len); void CreateCurrentTunnelDataMessage ();
size_t CreateFollowOnFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len); void CompleteCurrentTunnelDataMessage ();
I2NPMessage * CreateNextTunnelMessage (int& ind);
private: private:
uint32_t m_TunnelID; uint32_t m_TunnelID;
std::vector<TunnelMessageBlockExt *> m_I2NPMsgs; std::vector<I2NPMessage *> m_TunnelDataMsgs;
// for fragmented messages I2NPMessage * m_CurrentTunnelDataMsg;
size_t m_NextOffset, m_NextSeqn, m_Remaining; size_t m_RemainingSize;
uint32_t m_NextMsgID;
}; };
class TunnelGateway class TunnelGateway

Loading…
Cancel
Save