From d541572882664b5be416bb3e1d37be16619d69b8 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 2 Mar 2016 22:41:53 -0500 Subject: [PATCH] enable zero-hops inbound tunnel --- Tunnel.cpp | 34 +++++++++++++++++++++++++++------- Tunnel.h | 15 ++++++++++++--- TunnelConfig.h | 7 +++---- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Tunnel.cpp b/Tunnel.cpp index 92b20cdd..feaaddc4 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -200,15 +200,25 @@ namespace tunnel m_Endpoint.HandleDecryptedTunnelDataMsg (newMsg); } - void InboundTunnel::SendTunnelDataMsg (std::shared_ptr msg) + void InboundTunnel::Print (std::stringstream& s) const + { + PrintHops (s); + s << " ⇒ " << GetTunnelID () << ":me"; + } + + ZeroHopsInboundTunnel::ZeroHopsInboundTunnel (): + InboundTunnel (std::make_shared ()) + { + } + + void ZeroHopsInboundTunnel::SendTunnelDataMsg (std::shared_ptr msg) { - // assume zero-hops tunnel + msg->from = shared_from_this (); m_Endpoint.HandleDecryptedTunnelDataMsg (msg); - } + } - void InboundTunnel::Print (std::stringstream& s) const + void ZeroHopsInboundTunnel::Print (std::stringstream& s) const { - PrintHops (s); s << " ⇒ " << GetTunnelID () << ":me"; } @@ -771,11 +781,21 @@ namespace tunnel void Tunnels::CreateZeroHopsInboundTunnel () { - CreateTunnel ( + /*CreateTunnel ( + std::make_shared (std::vector > + { + i2p::context.GetIdentity () + }));*/ + auto inboundTunnel = std::make_shared (); + m_InboundTunnels.push_back (inboundTunnel); + m_Tunnels[inboundTunnel->GetTunnelID ()] = inboundTunnel; + + // create paired outbound tunnel, TODO: move to separate function + CreateTunnel ( std::make_shared (std::vector > { i2p::context.GetIdentity () - })); + }, inboundTunnel->GetNextTunnelID (), inboundTunnel->GetNextIdentHash ())); } int Tunnels::GetTransitTunnelsExpirationTimeout () diff --git a/Tunnel.h b/Tunnel.h index c41d53b2..7fa22a63 100644 --- a/Tunnel.h +++ b/Tunnel.h @@ -72,6 +72,8 @@ namespace tunnel void SetTunnelPool (std::shared_ptr pool) { m_Pool = pool; }; bool HandleTunnelBuildResponse (uint8_t * msg, size_t len); + + virtual void Print (std::stringstream& s) const {}; // implements TunnelBase void SendTunnelDataMsg (std::shared_ptr msg); @@ -112,22 +114,29 @@ namespace tunnel TunnelGateway m_Gateway; i2p::data::IdentHash m_EndpointIdentHash; }; - + class InboundTunnel: public Tunnel, public std::enable_shared_from_this { public: InboundTunnel (std::shared_ptr config): Tunnel (config), m_Endpoint (true) {}; void HandleTunnelDataMsg (std::shared_ptr msg); - void SendTunnelDataMsg (std::shared_ptr msg); // for zero-hops only size_t GetNumReceivedBytes () const { return m_Endpoint.GetNumReceivedBytes (); }; void Print (std::stringstream& s) const; - private: + protected: TunnelEndpoint m_Endpoint; }; + + class ZeroHopsInboundTunnel: public InboundTunnel + { + public: + ZeroHopsInboundTunnel (); + void SendTunnelDataMsg (std::shared_ptr msg); + void Print (std::stringstream& s) const; + }; class Tunnels { diff --git a/TunnelConfig.h b/TunnelConfig.h index ac493f24..c089b13c 100644 --- a/TunnelConfig.h +++ b/TunnelConfig.h @@ -226,14 +226,13 @@ namespace tunnel TunnelHopConfig * m_FirstHop, * m_LastHop; }; - class ZeroHopTunnelConfig: public TunnelConfig + class ZeroHopsTunnelConfig: public TunnelConfig { public: - ZeroHopTunnelConfig (uint32_t tunnelID = 0): // 0 means outbound - m_TunnelID (tunnelID) {}; + ZeroHopsTunnelConfig () { RAND_bytes ((uint8_t *)&m_TunnelID, 4);}; - bool IsInbound () const { return m_TunnelID; }; + bool IsInbound () const { return true; }; // TODO: uint32_t GetTunnelID () const { return m_TunnelID; }; uint32_t GetNextTunnelID () const { return m_TunnelID; }; const i2p::data::IdentHash& GetNextIdentHash () const { return i2p::context.GetIdentHash (); };