|
|
@ -16,7 +16,7 @@ namespace tunnel |
|
|
|
block->deliveryInstructionsLen = 1; // flag
|
|
|
|
block->deliveryInstructionsLen = 1; // flag
|
|
|
|
if (gwHash) |
|
|
|
if (gwHash) |
|
|
|
{ |
|
|
|
{ |
|
|
|
block->deliveryInstructionsLen = 32; // hash
|
|
|
|
block->deliveryInstructionsLen += 32; // hash
|
|
|
|
memcpy (block->hash, gwHash, 32); |
|
|
|
memcpy (block->hash, gwHash, 32); |
|
|
|
if (gwTunnel) |
|
|
|
if (gwTunnel) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -30,32 +30,53 @@ namespace tunnel |
|
|
|
else |
|
|
|
else |
|
|
|
block->deliveryType = eDeliveryTypeLocal; |
|
|
|
block->deliveryType = eDeliveryTypeLocal; |
|
|
|
block->deliveryInstructionsLen += 2; // size
|
|
|
|
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 yet
|
|
|
|
block->totalLen = block->deliveryInstructionsLen + msg->GetLength (); |
|
|
|
block->totalLen = block->deliveryInstructionsLen + msg->GetLength (); |
|
|
|
block->data = msg; |
|
|
|
block->data = msg; |
|
|
|
m_I2NPMsgs.push_back (block); |
|
|
|
m_I2NPMsgs.push_back (block); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!m_Remaining) m_Remaining = TUNNEL_DATA_MAX_PAYLOAD_SIZE; |
|
|
|
|
|
|
|
if (block->totalLen <= m_Remaining) // message fits
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
block->isFragmented = false; |
|
|
|
|
|
|
|
m_Remaining -= block->totalLen; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else // message doesn't fit
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (block->deliveryInstructionsLen + 4 <= m_Remaining) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// delivery instructions of first fragment fits
|
|
|
|
|
|
|
|
block->isFragmented = true; |
|
|
|
|
|
|
|
block->deliveryInstructionsLen += 4; |
|
|
|
|
|
|
|
block->totalLen += 4; |
|
|
|
|
|
|
|
m_Remaining = m_Remaining + TUNNEL_DATA_MAX_PAYLOAD_SIZE - block->totalLen - 7; // TODO: handle case if more than two fragments
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// delivery instructions of first fragment don't fit
|
|
|
|
|
|
|
|
block->isFragmented = false; |
|
|
|
|
|
|
|
m_Remaining = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs (uint32_t tunnelID) |
|
|
|
std::vector<I2NPMessage *> TunnelGatewayBuffer::GetTunnelDataMsgs (uint32_t tunnelID) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
m_Remaining = 0; |
|
|
|
|
|
|
|
m_NextOffset = 0; |
|
|
|
std::vector<I2NPMessage *> res; |
|
|
|
std::vector<I2NPMessage *> res; |
|
|
|
int cnt = m_I2NPMsgs.size (); |
|
|
|
int cnt = m_I2NPMsgs.size (); |
|
|
|
m_NextOffset = 0; |
|
|
|
|
|
|
|
if (cnt > 0) |
|
|
|
if (cnt > 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (auto m: m_I2NPMsgs) |
|
|
|
int ind = 0; |
|
|
|
{ |
|
|
|
while (ind < cnt) |
|
|
|
if (m->totalLen <= 1003) |
|
|
|
|
|
|
|
res.push_back (CreateNextTunnelMessage (tunnelID, m, m->totalLen)); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
res.push_back (CreateNextTunnelMessage (tunnelID, m, 1003)); |
|
|
|
auto tunnelMsg = CreateNextTunnelMessage (tunnelID, ind); |
|
|
|
size_t remaining = m->data->GetLength () - m_NextOffset; // remaining payload
|
|
|
|
if (!tunnelMsg) break; |
|
|
|
remaining += 7; // follow-on header
|
|
|
|
res.push_back (tunnelMsg); |
|
|
|
res.push_back (CreateNextTunnelMessage (tunnelID, m, remaining)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
delete m; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (auto msg: m_I2NPMsgs) |
|
|
|
|
|
|
|
delete msg; |
|
|
|
m_I2NPMsgs.clear (); |
|
|
|
m_I2NPMsgs.clear (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -64,7 +85,6 @@ namespace tunnel |
|
|
|
|
|
|
|
|
|
|
|
size_t TunnelGatewayBuffer::CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len) |
|
|
|
size_t TunnelGatewayBuffer::CreateFirstFragment (TunnelMessageBlockExt * block, uint8_t * buf, size_t len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (block->deliveryInstructionsLen > len) return 0; // can't put even delivery instructions
|
|
|
|
|
|
|
|
size_t ret = 1; |
|
|
|
size_t ret = 1; |
|
|
|
buf[0] = block->deliveryType << 5; // flag
|
|
|
|
buf[0] = block->deliveryType << 5; // flag
|
|
|
|
if (block->deliveryType == eDeliveryTypeTunnel) |
|
|
|
if (block->deliveryType == eDeliveryTypeTunnel) |
|
|
@ -80,7 +100,6 @@ namespace tunnel |
|
|
|
size_t size = block->data->GetLength (); |
|
|
|
size_t size = block->data->GetLength (); |
|
|
|
if (block->totalLen > len) // entire message doesn't fit
|
|
|
|
if (block->totalLen > len) // entire message doesn't fit
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (ret + 4 > len) return 0; // can't put delivery instructions with msgID
|
|
|
|
|
|
|
|
buf[0] |= 0x08; // set fragmented bit
|
|
|
|
buf[0] |= 0x08; // set fragmented bit
|
|
|
|
m_NextMsgID = block->data->GetHeader ()->msgID; |
|
|
|
m_NextMsgID = block->data->GetHeader ()->msgID; |
|
|
|
*(uint32_t *)(buf + ret) = m_NextMsgID; |
|
|
|
*(uint32_t *)(buf + ret) = m_NextMsgID; |
|
|
@ -120,42 +139,86 @@ namespace tunnel |
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (uint32_t tunnelID, |
|
|
|
I2NPMessage * TunnelGatewayBuffer::CreateNextTunnelMessage (uint32_t tunnelID, int& ind) |
|
|
|
TunnelMessageBlockExt * block, size_t size) |
|
|
|
{ |
|
|
|
|
|
|
|
int cnt = m_I2NPMsgs.size (); |
|
|
|
|
|
|
|
if (ind > cnt - 1) return nullptr; // no more messages
|
|
|
|
|
|
|
|
// calculate payload size
|
|
|
|
|
|
|
|
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 (); |
|
|
|
I2NPMessage * tunnelMsg = NewI2NPMessage (); |
|
|
|
uint8_t * buf = tunnelMsg->GetPayload (); |
|
|
|
uint8_t * buf = tunnelMsg->GetPayload (); |
|
|
|
*(uint32_t *)(buf) = htobe32 (tunnelID); |
|
|
|
*(uint32_t *)(buf) = htobe32 (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 + 1028, buf + 4, 16); // copy IV for checksum
|
|
|
|
memcpy (buf + TUNNEL_DATA_MSG_SIZE, buf + 4, 16); // copy IV for checksum
|
|
|
|
size_t zero = 1028 - size -1; |
|
|
|
size_t zero = TUNNEL_DATA_MSG_SIZE - size -1; |
|
|
|
buf[zero] = 0; // zero
|
|
|
|
buf[zero] = 0; // zero
|
|
|
|
|
|
|
|
size_t s = 0; |
|
|
|
|
|
|
|
while (ind < cnt) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto msg = m_I2NPMsgs[ind]; |
|
|
|
if (m_NextOffset) |
|
|
|
if (m_NextOffset) |
|
|
|
{ |
|
|
|
{ |
|
|
|
size_t s = CreateFollowOnFragment (block, buf + zero + 1, 1003); |
|
|
|
s += CreateFollowOnFragment (msg, buf + zero + 1 + s, size - s); |
|
|
|
if (s != size) |
|
|
|
m_NextOffset = 0; // TODO:
|
|
|
|
LogPrint ("Follow-on fragment size mismatch ", s, "!=", size); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
CreateFirstFragment (block, buf + zero + 1, 1003); |
|
|
|
{ |
|
|
|
|
|
|
|
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, buf+zero+1, size+16); |
|
|
|
memcpy (buf+20, hash, 4); // checksum
|
|
|
|
memcpy (buf+20, hash, 4); // checksum
|
|
|
|
if (zero > 24) |
|
|
|
if (zero > 24) |
|
|
|
memset (buf+24, 1, zero-24); // padding
|
|
|
|
memset (buf+24, 1, zero-24); // padding TODO: fill with random data
|
|
|
|
tunnelMsg->len += 1028; |
|
|
|
tunnelMsg->len += TUNNEL_DATA_MSG_SIZE; |
|
|
|
|
|
|
|
|
|
|
|
// 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) |
|
|
|
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); |
|
|
|
m_Buffer.PutI2NPMsg (gwHash, gwTunnel, msg); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TunnelGateway::SendBuffer () |
|
|
|
|
|
|
|
{ |
|
|
|
auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs (m_Tunnel->GetNextTunnelID ()); |
|
|
|
auto tunnelMsgs = m_Buffer.GetTunnelDataMsgs (m_Tunnel->GetNextTunnelID ()); |
|
|
|
for (auto tunnelMsg : tunnelMsgs) |
|
|
|
for (auto tunnelMsg : tunnelMsgs) |
|
|
|
{ |
|
|
|
{ |
|
|
|