Browse Source

TransitTunnelParticipant

pull/151/head
orignal 10 years ago
parent
commit
2ab0ff8aea
  1. 43
      TransitTunnel.cpp
  2. 27
      TransitTunnel.h
  3. 11
      Tunnel.cpp
  4. 1
      TunnelBase.h

43
TransitTunnel.cpp

@ -15,7 +15,7 @@ namespace tunnel
const uint8_t * nextIdent, uint32_t nextTunnelID, const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey): const uint8_t * layerKey,const uint8_t * ivKey):
m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID), m_TunnelID (receiveTunnelID), m_NextTunnelID (nextTunnelID),
m_NextIdent (nextIdent), m_NumTransmittedBytes (0) m_NextIdent (nextIdent)
{ {
m_Encryption.SetKeys (layerKey, ivKey); m_Encryption.SetKeys (layerKey, ivKey);
} }
@ -25,24 +25,45 @@ namespace tunnel
m_Encryption.Encrypt (tunnelMsg->GetPayload () + 4); m_Encryption.Encrypt (tunnelMsg->GetPayload () + 4);
} }
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) TransitTunnelParticipant::~TransitTunnelParticipant ()
{
for (auto it: m_TunnelDataMsgs)
i2p::DeleteI2NPMessage (it);
}
void TransitTunnelParticipant::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
{ {
EncryptTunnelMsg (tunnelMsg); EncryptTunnelMsg (tunnelMsg);
LogPrint ("TransitTunnel: ",m_TunnelID,"->", m_NextTunnelID); LogPrint (eLogDebug, "TransitTunnel: ",GetTunnelID (),"->", GetNextTunnelID ());
m_NumTransmittedBytes += tunnelMsg->GetLength (); m_NumTransmittedBytes += tunnelMsg->GetLength ();
htobe32buf (tunnelMsg->GetPayload (), m_NextTunnelID); htobe32buf (tunnelMsg->GetPayload (), GetNextTunnelID ());
FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData); FillI2NPMessageHeader (tunnelMsg, eI2NPTunnelData);
m_TunnelDataMsgs.push_back (tunnelMsg);
}
i2p::transport::transports.SendMessage (m_NextIdent, tunnelMsg); void TransitTunnelParticipant::FlushTunnelDataMsgs ()
{
LogPrint (eLogDebug, "TransitTunnel: flush");
if (!m_TunnelDataMsgs.empty ())
{
i2p::transport::transports.SendMessages (GetNextIdentHash (), m_TunnelDataMsgs);
m_TunnelDataMsgs.clear ();
}
} }
void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg) void TransitTunnel::SendTunnelDataMsg (i2p::I2NPMessage * msg)
{ {
LogPrint ("We are not a gateway for transit tunnel ", m_TunnelID); LogPrint (eLogError, "We are not a gateway for transit tunnel ", m_TunnelID);
i2p::DeleteI2NPMessage (msg); i2p::DeleteI2NPMessage (msg);
} }
void TransitTunnel::HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg)
{
LogPrint (eLogError, "Incoming tunnel message is not supported ", m_TunnelID);
DeleteI2NPMessage (tunnelMsg);
}
void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg) void TransitTunnelGateway::SendTunnelDataMsg (i2p::I2NPMessage * msg)
{ {
TunnelMessageBlock block; TunnelMessageBlock block;
@ -56,7 +77,7 @@ namespace tunnel
{ {
EncryptTunnelMsg (tunnelMsg); EncryptTunnelMsg (tunnelMsg);
LogPrint ("TransitTunnel endpoint for ", GetTunnelID ()); LogPrint (eLogDebug, "TransitTunnel endpoint for ", GetTunnelID ());
m_Endpoint.HandleDecryptedTunnelDataMsg (tunnelMsg); m_Endpoint.HandleDecryptedTunnelDataMsg (tunnelMsg);
} }
@ -67,18 +88,18 @@ namespace tunnel
{ {
if (isEndpoint) if (isEndpoint)
{ {
LogPrint ("TransitTunnel endpoint: ", receiveTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel endpoint: ", receiveTunnelID, " created");
return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return new TransitTunnelEndpoint (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
else if (isGateway) else if (isGateway)
{ {
LogPrint ("TransitTunnel gateway: ", receiveTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel gateway: ", receiveTunnelID, " created");
return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return new TransitTunnelGateway (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
else else
{ {
LogPrint ("TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created"); LogPrint (eLogInfo, "TransitTunnel: ", receiveTunnelID, "->", nextTunnelID, " created");
return new TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey); return new TransitTunnelParticipant (receiveTunnelID, nextIdent, nextTunnelID, layerKey, ivKey);
} }
} }
} }

27
TransitTunnel.h

@ -2,6 +2,7 @@
#define TRANSIT_TUNNEL_H__ #define TRANSIT_TUNNEL_H__
#include <inttypes.h> #include <inttypes.h>
#include <vector>
#include <mutex> #include <mutex>
#include "aes.h" #include "aes.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
@ -13,7 +14,7 @@ namespace i2p
{ {
namespace tunnel namespace tunnel
{ {
class TransitTunnel: public TunnelBase // tunnel patricipant class TransitTunnel: public TunnelBase
{ {
public: public:
@ -22,7 +23,7 @@ namespace tunnel
const uint8_t * layerKey,const uint8_t * ivKey); const uint8_t * layerKey,const uint8_t * ivKey);
virtual void SendTunnelDataMsg (i2p::I2NPMessage * msg); virtual void SendTunnelDataMsg (i2p::I2NPMessage * msg);
virtual size_t GetNumTransmittedBytes () const { return m_NumTransmittedBytes; }; virtual size_t GetNumTransmittedBytes () const { return 0; };
uint32_t GetTunnelID () const { return m_TunnelID; }; uint32_t GetTunnelID () const { return m_TunnelID; };
@ -36,11 +37,31 @@ namespace tunnel
uint32_t m_TunnelID, m_NextTunnelID; uint32_t m_TunnelID, m_NextTunnelID;
i2p::data::IdentHash m_NextIdent; i2p::data::IdentHash m_NextIdent;
size_t m_NumTransmittedBytes;
i2p::crypto::TunnelEncryption m_Encryption; i2p::crypto::TunnelEncryption m_Encryption;
}; };
class TransitTunnelParticipant: public TransitTunnel
{
public:
TransitTunnelParticipant (uint32_t receiveTunnelID,
const uint8_t * nextIdent, uint32_t nextTunnelID,
const uint8_t * layerKey,const uint8_t * ivKey):
TransitTunnel (receiveTunnelID, nextIdent, nextTunnelID,
layerKey, ivKey), m_NumTransmittedBytes (0) {};
~TransitTunnelParticipant ();
size_t GetNumTransmittedBytes () const { return m_NumTransmittedBytes; };
void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg);
void FlushTunnelDataMsgs ();
private:
size_t m_NumTransmittedBytes;
std::vector<i2p::I2NPMessage *> m_TunnelDataMsgs;
};
class TransitTunnelGateway: public TransitTunnel class TransitTunnelGateway: public TransitTunnel
{ {
public: public:

11
Tunnel.cpp

@ -347,14 +347,19 @@ namespace tunnel
try try
{ {
I2NPMessage * msg = m_Queue.GetNextWithTimeout (1000); // 1 sec I2NPMessage * msg = m_Queue.GetNextWithTimeout (1000); // 1 sec
if (msg)
{
uint32_t prevTunnelID = 0; uint32_t prevTunnelID = 0;
TunnelBase * prevTunnel = nullptr; TunnelBase * prevTunnel = nullptr;
while (msg) do
{ {
uint32_t tunnelID = bufbe32toh (msg->GetPayload ()); uint32_t tunnelID = bufbe32toh (msg->GetPayload ());
TunnelBase * tunnel = nullptr; TunnelBase * tunnel = nullptr;
if (tunnelID == prevTunnelID) if (tunnelID == prevTunnelID)
tunnel = prevTunnel; tunnel = prevTunnel;
else if (prevTunnel)
prevTunnel->FlushTunnelDataMsgs ();
if (!tunnel) if (!tunnel)
tunnel = GetInboundTunnel (tunnelID); tunnel = GetInboundTunnel (tunnelID);
if (!tunnel) if (!tunnel)
@ -373,6 +378,10 @@ namespace tunnel
prevTunnelID = tunnelID; prevTunnelID = tunnelID;
prevTunnel = tunnel; prevTunnel = tunnel;
} }
else if (tunnel)
tunnel->FlushTunnelDataMsgs ();
}
while (msg);
} }
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); uint64_t ts = i2p::util::GetSecondsSinceEpoch ();

1
TunnelBase.h

@ -37,6 +37,7 @@ namespace tunnel
virtual ~TunnelBase () {}; virtual ~TunnelBase () {};
virtual void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) = 0; virtual void HandleTunnelDataMsg (i2p::I2NPMessage * tunnelMsg) = 0;
virtual void FlushTunnelDataMsgs () {};
virtual void EncryptTunnelMsg (I2NPMessage * tunnelMsg) = 0; virtual void EncryptTunnelMsg (I2NPMessage * tunnelMsg) = 0;
virtual uint32_t GetNextTunnelID () const = 0; virtual uint32_t GetNextTunnelID () const = 0;
virtual const i2p::data::IdentHash& GetNextIdentHash () const = 0; virtual const i2p::data::IdentHash& GetNextIdentHash () const = 0;

Loading…
Cancel
Save