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. 1
      libi2pd/TunnelBase.cpp
  4. 40
      libi2pd/TunnelEndpoint.cpp
  5. 17
      libi2pd/TunnelEndpoint.h

24
libi2pd/TransitTunnel.cpp

@ -133,6 +133,30 @@ namespace tunnel @@ -133,6 +133,30 @@ namespace tunnel
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,
const i2p::data::IdentHash& nextIdent, uint32_t nextTunnelID,
const i2p::crypto::AESKey& layerKey, const i2p::crypto::AESKey& ivKey,

3
libi2pd/TransitTunnel.h

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

1
libi2pd/TunnelBase.cpp

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

40
libi2pd/TunnelEndpoint.cpp

@ -1,5 +1,5 @@ @@ -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
*
@ -21,9 +21,6 @@ namespace i2p @@ -21,9 +21,6 @@ namespace i2p
{
namespace tunnel
{
TunnelEndpoint::~TunnelEndpoint ()
{
}
void TunnelEndpoint::HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg)
{
@ -331,13 +328,13 @@ namespace tunnel @@ -331,13 +328,13 @@ namespace tunnel
break;
case eDeliveryTypeTunnel:
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
LogPrint (eLogError, "TunnelMessage: Delivery type 'tunnel' arrived from an inbound tunnel, dropped");
break;
case eDeliveryTypeRouter:
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
LogPrint (eLogError, "TunnelMessage: Delivery type 'router' arrived from an inbound tunnel, dropped");
break;
@ -366,5 +363,36 @@ namespace tunnel @@ -366,5 +363,36 @@ namespace tunnel
++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 @@ @@ -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
*
@ -11,8 +11,10 @@ @@ -11,8 +11,10 @@
#include <inttypes.h>
#include <vector>
#include <list>
#include <string>
#include <unordered_map>
#include <memory>
#include "I2NPProtocol.h"
#include "TunnelBase.h"
@ -20,7 +22,7 @@ namespace i2p @@ -20,7 +22,7 @@ namespace i2p
{
namespace tunnel
{
class TunnelEndpoint
class TunnelEndpoint final
{
struct TunnelMessageBlockEx: public TunnelMessageBlock
{
@ -39,11 +41,15 @@ namespace tunnel @@ -39,11 +41,15 @@ namespace tunnel
public:
TunnelEndpoint (bool isInbound): m_IsInbound (isInbound), m_NumReceivedBytes (0), m_CurrentMsgID (0) {};
~TunnelEndpoint ();
~TunnelEndpoint () = default;
size_t GetNumReceivedBytes () const { return m_NumReceivedBytes; };
void Cleanup ();
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:
@ -51,6 +57,7 @@ namespace tunnel @@ -51,6 +57,7 @@ namespace tunnel
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 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);
bool ConcatNextOutOfSequenceFragment (uint32_t msgID, TunnelMessageBlockEx& msg); // true if something added
@ -65,6 +72,10 @@ namespace tunnel @@ -65,6 +72,10 @@ namespace tunnel
size_t m_NumReceivedBytes;
TunnelMessageBlockEx m_CurrentMessage;
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