From 6c510fadf4c6c2aa0bba0513de7fc37ab77fd0f5 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 20 Aug 2014 15:03:10 -0400 Subject: [PATCH] server I2PTunnel --- I2PTunnel.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ I2PTunnel.h | 25 +++++++++++++++++++++++ Streaming.cpp | 17 ++++++++++++++-- Streaming.h | 6 ++++-- 4 files changed, 100 insertions(+), 4 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index e48ef64b..f87a3c1e 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -17,6 +17,15 @@ namespace stream Receive (); } + I2PTunnelConnection::I2PTunnelConnection (Stream * stream, boost::asio::ip::tcp::socket * socket, + const boost::asio::ip::tcp::endpoint& target): + m_Socket (socket), m_Stream (stream) + { + if (m_Socket) + m_Socket->async_connect (target, boost::bind (&I2PTunnelConnection::HandleConnect, + this, boost::asio::placeholders::error)); + } + I2PTunnelConnection::~I2PTunnelConnection () { if (m_Stream) @@ -92,6 +101,23 @@ namespace stream } } + void I2PTunnelConnection::HandleConnect (const boost::system::error_code& ecode) + { + if (ecode) + { + LogPrint ("I2PTunnel connect error: ", ecode.message ()); + if (m_Stream) m_Stream->Close (); + DeleteStream (m_Stream); + m_Stream = nullptr; + } + else + { + LogPrint ("I2PTunnel connected"); + StreamReceive (); + Receive (); + } + } + I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, int port): m_Service (service), m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)), m_Destination (destination), m_DestinationIdentHash (nullptr), m_RemoteLeaseSet (nullptr) @@ -185,5 +211,35 @@ namespace stream else delete socket; } + + I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, + const i2p::data::IdentHash& localDestination): m_Service (service), + m_Endpoint (boost::asio::ip::address::from_string (address), port) + { + m_LocalDestination = FindLocalDestination (localDestination); + if (!m_LocalDestination) + LogPrint ("Local destination ", localDestination.ToBase64 (), " not found"); + } + + void I2PServerTunnel::Start () + { + Accept (); + } + + void I2PServerTunnel::Stop () + { + } + + void I2PServerTunnel::Accept () + { + if (m_LocalDestination) + m_LocalDestination->SetAcceptor (std::bind (&I2PServerTunnel::HandleAccept, this, std::placeholders::_1)); + } + + void I2PServerTunnel::HandleAccept (i2p::stream::Stream * stream) + { + if (stream) + new I2PTunnelConnection (stream, new boost::asio::ip::tcp::socket (m_Service), m_Endpoint); + } } } diff --git a/I2PTunnel.h b/I2PTunnel.h index 9882d4a1..e26f43db 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -20,6 +20,8 @@ namespace stream I2PTunnelConnection (boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet); + I2PTunnelConnection (Stream * stream, boost::asio::ip::tcp::socket * socket, + const boost::asio::ip::tcp::endpoint& target); ~I2PTunnelConnection (); private: @@ -32,6 +34,7 @@ namespace stream void StreamReceive (); void HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred); + void HandleConnect (const boost::system::error_code& ecode); private: @@ -64,6 +67,28 @@ namespace stream const i2p::data::LeaseSet * m_RemoteLeaseSet; std::set m_Connections; }; + + class I2PServerTunnel + { + public: + + I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port, + const i2p::data::IdentHash& localDestination); + + void Start (); + void Stop (); + + private: + + void Accept (); + void HandleAccept (Stream * stream); + + private: + + boost::asio::io_service& m_Service; + StreamingDestination * m_LocalDestination; + boost::asio::ip::tcp::endpoint m_Endpoint; + }; } } diff --git a/Streaming.cpp b/Streaming.cpp index 28774010..3279484a 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -707,7 +707,15 @@ namespace stream delete packet; } } - + + StreamingDestination * StreamingDestinations::FindLocalDestination (const i2p::data::IdentHash& destination) const + { + auto it = m_Destinations.find (destination); + if (it != m_Destinations.end ()) + return it->second; + return nullptr; + } + Stream * CreateStream (const i2p::data::LeaseSet& remote) { return destinations.CreateClientStream (remote); @@ -732,7 +740,12 @@ namespace stream { return destinations.GetSharedLocalDestination (); } - + + StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) + { + return destinations.FindLocalDestination (destination); + } + void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len) { uint32_t length = be32toh (*(uint32_t *)buf); diff --git a/Streaming.h b/Streaming.h index 7d1f47fb..2c83bd21 100644 --- a/Streaming.h +++ b/Streaming.h @@ -197,7 +197,8 @@ namespace stream Stream * CreateClientStream (const i2p::data::LeaseSet& remote); void DeleteStream (Stream * stream); StreamingDestination * GetSharedLocalDestination () const { return m_SharedLocalDestination; }; - + StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination) const; + private: void Run (); @@ -220,7 +221,8 @@ namespace stream void StartStreaming (); void StopStreaming (); StreamingDestination * GetSharedLocalDestination (); - + StreamingDestination * FindLocalDestination (const i2p::data::IdentHash& destination); + // assuming data is I2CP message void HandleDataMessage (i2p::data::IdentHash destination, const uint8_t * buf, size_t len); I2NPMessage * CreateDataMessage (Stream * s, const uint8_t * payload, size_t len);