Browse Source

create different I2NP tunnel messages for endpoint and non-endpoint

pull/1677/head
orignal 3 years ago
parent
commit
25f63ac22a
  1. 26
      libi2pd/I2NPProtocol.cpp
  2. 4
      libi2pd/I2NPProtocol.h
  3. 4
      libi2pd/TransitTunnel.cpp
  4. 2
      libi2pd/Tunnel.cpp
  5. 4
      libi2pd/TunnelEndpoint.cpp
  6. 2
      libi2pd/TunnelGateway.cpp

26
libi2pd/I2NPProtocol.cpp

@ -36,11 +36,21 @@ namespace i2p @@ -36,11 +36,21 @@ namespace i2p
return std::make_shared<I2NPMessageBuffer<I2NP_MAX_SHORT_MESSAGE_SIZE> >();
}
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ()
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint)
{
// should fit two tunnel message, enough for one garlic encrypted streaming packet
auto msg = new I2NPMessageBuffer<2*i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
msg->Align (12);
I2NPMessage * msg = nullptr;
if (endpoint)
{
// should fit two tunnel message + tunnel gateway header, enough for one garlic encrypted streaming packet
msg = new I2NPMessageBuffer<2*i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE + 28>(); // reserved for alignment and NTCP 16 + 6 + 6
msg->Align (6);
msg->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
}
else
{
msg = new I2NPMessageBuffer<i2p::tunnel::TUNNEL_DATA_MSG_SIZE + I2NP_HEADER_SIZE + 34>(); // reserved for alignment and NTCP 16 + 6 + 12
msg->Align (12);
}
return std::shared_ptr<I2NPMessage>(msg);
}
@ -692,7 +702,7 @@ namespace i2p @@ -692,7 +702,7 @@ namespace i2p
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (const uint8_t * buf)
{
auto msg = NewI2NPTunnelMessage ();
auto msg = NewI2NPTunnelMessage (false);
msg->Concat (buf, i2p::tunnel::TUNNEL_DATA_MSG_SIZE);
msg->FillI2NPMessageHeader (eI2NPTunnelData);
return msg;
@ -700,7 +710,7 @@ namespace i2p @@ -700,7 +710,7 @@ namespace i2p
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload)
{
auto msg = NewI2NPTunnelMessage ();
auto msg = NewI2NPTunnelMessage (false);
htobe32buf (msg->GetPayload (), tunnelID);
msg->len += 4; // tunnelID
msg->Concat (payload, i2p::tunnel::TUNNEL_DATA_MSG_SIZE - 4);
@ -708,9 +718,9 @@ namespace i2p @@ -708,9 +718,9 @@ namespace i2p
return msg;
}
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg ()
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg (bool endpoint)
{
auto msg = NewI2NPTunnelMessage ();
auto msg = NewI2NPTunnelMessage (endpoint);
msg->len += i2p::tunnel::TUNNEL_DATA_MSG_SIZE;
return msg;
}

4
libi2pd/I2NPProtocol.h

@ -278,7 +278,7 @@ namespace tunnel @@ -278,7 +278,7 @@ namespace tunnel
std::shared_ptr<I2NPMessage> NewI2NPMessage ();
std::shared_ptr<I2NPMessage> NewI2NPShortMessage ();
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage ();
std::shared_ptr<I2NPMessage> NewI2NPTunnelMessage (bool endpoint);
std::shared_ptr<I2NPMessage> NewI2NPMessage (size_t len);
std::shared_ptr<I2NPMessage> CreateI2NPMessage (I2NPMessageType msgType, const uint8_t * buf, size_t len, uint32_t replyMsgID = 0);
@ -307,7 +307,7 @@ namespace tunnel @@ -307,7 +307,7 @@ namespace tunnel
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (const uint8_t * buf);
std::shared_ptr<I2NPMessage> CreateTunnelDataMsg (uint32_t tunnelID, const uint8_t * payload);
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg ();
std::shared_ptr<I2NPMessage> CreateEmptyTunnelDataMsg (bool endpoint);
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, const uint8_t * buf, size_t len);
std::shared_ptr<I2NPMessage> CreateTunnelGatewayMsg (uint32_t tunnelID, I2NPMessageType msgType,

4
libi2pd/TransitTunnel.cpp

@ -39,7 +39,7 @@ namespace tunnel @@ -39,7 +39,7 @@ namespace tunnel
void TransitTunnelParticipant::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
{
auto newMsg = CreateEmptyTunnelDataMsg ();
auto newMsg = CreateEmptyTunnelDataMsg (false);
EncryptTunnelMsg (tunnelMsg, newMsg);
m_NumTransmittedBytes += tunnelMsg->GetLength ();
@ -87,7 +87,7 @@ namespace tunnel @@ -87,7 +87,7 @@ namespace tunnel
void TransitTunnelEndpoint::HandleTunnelDataMsg (std::shared_ptr<const i2p::I2NPMessage> tunnelMsg)
{
auto newMsg = CreateEmptyTunnelDataMsg ();
auto newMsg = CreateEmptyTunnelDataMsg (true);
EncryptTunnelMsg (tunnelMsg, newMsg);
LogPrint (eLogDebug, "TransitTunnel: handle msg for endpoint ", GetTunnelID ());

2
libi2pd/Tunnel.cpp

@ -234,7 +234,7 @@ namespace tunnel @@ -234,7 +234,7 @@ namespace tunnel
void InboundTunnel::HandleTunnelDataMsg (std::shared_ptr<const I2NPMessage> msg)
{
if (IsFailed ()) SetState (eTunnelStateEstablished); // incoming messages means a tunnel is alive
auto newMsg = CreateEmptyTunnelDataMsg ();
auto newMsg = CreateEmptyTunnelDataMsg (true);
EncryptTunnelMsg (msg, newMsg);
newMsg->from = shared_from_this ();
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);

4
libi2pd/TunnelEndpoint.cpp

@ -126,9 +126,7 @@ namespace tunnel @@ -126,9 +126,7 @@ namespace tunnel
if (fragment + size < decrypted + TUNNEL_DATA_ENCRYPTED_SIZE)
{
// this is not last message. we have to copy it
m_CurrentMessage.data = NewI2NPTunnelMessage ();
m_CurrentMessage.data->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
m_CurrentMessage.data->len += TUNNEL_GATEWAY_HEADER_SIZE;
m_CurrentMessage.data = NewI2NPTunnelMessage (true);
*(m_CurrentMessage.data) = *msg;
}
else

2
libi2pd/TunnelGateway.cpp

@ -215,7 +215,7 @@ namespace tunnel @@ -215,7 +215,7 @@ namespace tunnel
const auto& tunnelDataMsgs = m_Buffer.GetTunnelDataMsgs ();
for (auto& tunnelMsg : tunnelDataMsgs)
{
auto newMsg = CreateEmptyTunnelDataMsg ();
auto newMsg = CreateEmptyTunnelDataMsg (false);
m_Tunnel->EncryptTunnelMsg (tunnelMsg, newMsg);
htobe32buf (newMsg->GetPayload (), m_Tunnel->GetNextTunnelID ());
newMsg->FillI2NPMessageHeader (eI2NPTunnelData);

Loading…
Cancel
Save