Browse Source

server I2PTunnel

pull/93/head
orignal 10 years ago
parent
commit
6c510fadf4
  1. 56
      I2PTunnel.cpp
  2. 25
      I2PTunnel.h
  3. 17
      Streaming.cpp
  4. 6
      Streaming.h

56
I2PTunnel.cpp

@ -17,6 +17,15 @@ namespace stream @@ -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 @@ -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 @@ -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);
}
}
}

25
I2PTunnel.h

@ -20,6 +20,8 @@ namespace stream @@ -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 @@ -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 @@ -64,6 +67,28 @@ namespace stream
const i2p::data::LeaseSet * m_RemoteLeaseSet;
std::set<I2PTunnelConnection *> 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;
};
}
}

17
Streaming.cpp

@ -707,7 +707,15 @@ namespace stream @@ -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 @@ -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);

6
Streaming.h

@ -197,7 +197,8 @@ namespace stream @@ -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 @@ -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);

Loading…
Cancel
Save