Browse Source

send tunnel endpoint data to transport session to gateway directly

pull/2094/merge
orignal 5 days ago
parent
commit
36939898fe
  1. 24
      libi2pd/TransitTunnel.cpp
  2. 3
      libi2pd/TransitTunnel.h
  3. 3
      libi2pd/TunnelBase.cpp
  4. 42
      libi2pd/TunnelEndpoint.cpp
  5. 17
      libi2pd/TunnelEndpoint.h

24
libi2pd/TransitTunnel.cpp

@ -133,6 +133,30 @@ namespace tunnel
m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg); m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg);
} }
void TransitTunnelEndpoint::FlushTunnelDataMsgs ()
{
m_Endpoint.FlushI2NPMsgs ();
}
std::string TransitTunnelEndpoint::GetNextPeerName () const
{
auto hash = m_Endpoint.GetCurrentHash ();
if (hash)
{
const auto& sender = m_Endpoint.GetSender ();
if (sender)
{
auto transport = sender->GetCurrentTransport ();
if (transport)
return i2p::data::GetIdentHashAbbreviation (*hash) + "-" +
i2p::data::RouterInfo::GetTransportName (transport->GetTransportType ());
else
return i2p::data::GetIdentHashAbbreviation (*hash);
}
}
return "";
}
std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID, std::shared_ptr<TransitTunnel> CreateTransitTunnel (uint32_t receiveTunnelID,
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID, const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey, const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey,

3
libi2pd/TransitTunnel.h

@ -103,8 +103,9 @@ namespace tunnel
void Cleanup () override { m_Endpoint.Cleanup (); } void Cleanup () override { m_Endpoint.Cleanup (); }
void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override; void HandleTunnelDataMsg (std::shared_ptr<i2p::I2NPMessage>&& tunnelMsg) override;
void FlushTunnelDataMsgs () override;
size_t GetNumTransmittedBytes () const override { return m_Endpoint.GetNumReceivedBytes (); } size_t GetNumTransmittedBytes () const override { return m_Endpoint.GetNumReceivedBytes (); }
std::string GetNextPeerName () const override { return ""; } std::string GetNextPeerName () const override;
private: private:

3
libi2pd/TunnelBase.cpp

@ -64,7 +64,8 @@ namespace tunnel
void TunnelTransportSender::Reset () void TunnelTransportSender::Reset ()
{ {
m_CurrentTransport.reset (); m_CurrentTransport.reset ();
m_PendingTransport = std::future<std::shared_ptr<i2p::transport::TransportSession> >(); if (m_PendingTransport.valid ())
m_PendingTransport = std::future<std::shared_ptr<i2p::transport::TransportSession> >();
} }
} }
} }

42
libi2pd/TunnelEndpoint.cpp

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2023, The PurpleI2P Project * Copyright (c) 2013-2024, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -21,10 +21,7 @@ namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
TunnelEndpoint::~TunnelEndpoint ()
{
}
void TunnelEndpoint::HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg) void TunnelEndpoint::HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg)
{ {
m_NumReceivedBytes += TUNNEL_DATA_MSG_SIZE; m_NumReceivedBytes += TUNNEL_DATA_MSG_SIZE;
@ -331,13 +328,13 @@ namespace tunnel
break; break;
case eDeliveryTypeTunnel: case eDeliveryTypeTunnel:
if (!m_IsInbound) // outbound transit tunnel if (!m_IsInbound) // outbound transit tunnel
i2p::transport::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data)); SendMessageTo (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data));
else else
LogPrint (eLogError, "TunnelMessage: Delivery type 'tunnel' arrived from an inbound tunnel, dropped"); LogPrint (eLogError, "TunnelMessage: Delivery type 'tunnel' arrived from an inbound tunnel, dropped");
break; break;
case eDeliveryTypeRouter: case eDeliveryTypeRouter:
if (!m_IsInbound) // outbound transit tunnel if (!m_IsInbound) // outbound transit tunnel
i2p::transport::transports.SendMessage (msg.hash, msg.data); i2p::transport::transports.SendMessage (msg.hash, msg.data); // send right away, because most likely it's single message
else // we shouldn't send this message. possible leakage else // we shouldn't send this message. possible leakage
LogPrint (eLogError, "TunnelMessage: Delivery type 'router' arrived from an inbound tunnel, dropped"); LogPrint (eLogError, "TunnelMessage: Delivery type 'router' arrived from an inbound tunnel, dropped");
break; break;
@ -366,5 +363,36 @@ namespace tunnel
++it; ++it;
} }
} }
void TunnelEndpoint::SendMessageTo (const i2p::data::IdentHash& to, std::shared_ptr<i2p::I2NPMessage> msg)
{
if (msg)
{
if (!m_Sender && m_I2NPMsgs.empty ()) // first message
m_CurrentHash = to;
else if (m_CurrentHash != to) // new target router
{
FlushI2NPMsgs (); // flush message to previous
if (m_Sender) m_Sender->Reset (); // reset sender
m_CurrentHash = to; // set new target router
} // otherwise add msg to the list for current target router
m_I2NPMsgs.push_back (msg);
i2p::transport::transports.SendMessage (to, msg);
}
}
void TunnelEndpoint::FlushI2NPMsgs ()
{
if (!m_I2NPMsgs.empty ())
{
if (!m_Sender) m_Sender = std::make_unique<TunnelTransportSender>();
m_Sender->SendMessagesTo (m_CurrentHash, m_I2NPMsgs); // send and clear
}
}
const i2p::data::IdentHash * TunnelEndpoint::GetCurrentHash () const
{
return (m_Sender || !m_I2NPMsgs.empty ()) ? &m_CurrentHash : nullptr;
}
} }
} }

17
libi2pd/TunnelEndpoint.h

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2021, The PurpleI2P Project * Copyright (c) 2013-2024, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -11,8 +11,10 @@
#include <inttypes.h> #include <inttypes.h>
#include <vector> #include <vector>
#include <list>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <memory>
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "TunnelBase.h" #include "TunnelBase.h"
@ -20,7 +22,7 @@ namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
class TunnelEndpoint class TunnelEndpoint final
{ {
struct TunnelMessageBlockEx: public TunnelMessageBlock struct TunnelMessageBlockEx: public TunnelMessageBlock
{ {
@ -39,18 +41,23 @@ namespace tunnel
public: public:
TunnelEndpoint (bool isInbound): m_IsInbound (isInbound), m_NumReceivedBytes (0), m_CurrentMsgID (0) {}; TunnelEndpoint (bool isInbound): m_IsInbound (isInbound), m_NumReceivedBytes (0), m_CurrentMsgID (0) {};
~TunnelEndpoint (); ~TunnelEndpoint () = default;
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; }; size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
void Cleanup (); void Cleanup ();
void HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg); void HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg);
void FlushI2NPMsgs ();
const i2p::data::IdentHash * GetCurrentHash () const; // return null if not avaiable
const std::unique_ptr<TunnelTransportSender>& GetSender () const { return m_Sender; };
private: private:
void HandleFollowOnFragment (uint32_t msgID, bool isLastFragment, uint8_t fragmentNum, const uint8_t * fragment, size_t size); void HandleFollowOnFragment (uint32_t msgID, bool isLastFragment, uint8_t fragmentNum, const uint8_t * fragment, size_t size);
bool ConcatFollowOnFragment (TunnelMessageBlockEx& msg, const uint8_t * fragment, size_t size) const; // true if success bool ConcatFollowOnFragment (TunnelMessageBlockEx& msg, const uint8_t * fragment, size_t size) const; // true if success
void HandleCurrenMessageFollowOnFragment (const uint8_t * fragment, size_t size, bool isLastFragment); void HandleCurrenMessageFollowOnFragment (const uint8_t * fragment, size_t size, bool isLastFragment);
void HandleNextMessage (const TunnelMessageBlock& msg); void HandleNextMessage (const TunnelMessageBlock& msg);
void SendMessageTo (const i2p::data::IdentHash& to, std::shared_ptr<i2p::I2NPMessage> msg);
void AddOutOfSequenceFragment (uint32_t msgID, uint8_t fragmentNum, bool isLastFragment, const uint8_t * fragment, size_t size); void AddOutOfSequenceFragment (uint32_t msgID, uint8_t fragmentNum, bool isLastFragment, const uint8_t * fragment, size_t size);
bool ConcatNextOutOfSequenceFragment (uint32_t msgID, TunnelMessageBlockEx& msg); // true if something added bool ConcatNextOutOfSequenceFragment (uint32_t msgID, TunnelMessageBlockEx& msg); // true if something added
@ -65,6 +72,10 @@ namespace tunnel
size_t m_NumReceivedBytes; size_t m_NumReceivedBytes;
TunnelMessageBlockEx m_CurrentMessage; TunnelMessageBlockEx m_CurrentMessage;
uint32_t m_CurrentMsgID; uint32_t m_CurrentMsgID;
// I2NP messages to send
std::list<std::shared_ptr<i2p::I2NPMessage> > m_I2NPMsgs; // to send
i2p::data::IdentHash m_CurrentHash; // send msgs to
std::unique_ptr<TunnelTransportSender> m_Sender;
}; };
} }
} }

Loading…
Cancel
Save