mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-08 22:13:48 +01:00
maintain connections list for I2PTunnel
This commit is contained in:
parent
e734280c0c
commit
9e759a59a9
2 changed files with 60 additions and 22 deletions
|
@ -8,8 +8,9 @@ namespace i2p
|
||||||
{
|
{
|
||||||
namespace stream
|
namespace stream
|
||||||
{
|
{
|
||||||
I2PTunnelConnection::I2PTunnelConnection (boost::asio::ip::tcp::socket * socket,
|
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner,
|
||||||
const i2p::data::LeaseSet * leaseSet): m_Socket (socket)
|
boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet):
|
||||||
|
m_Socket (socket), m_Owner (owner)
|
||||||
{
|
{
|
||||||
m_Stream = i2p::stream::CreateStream (*leaseSet);
|
m_Stream = i2p::stream::CreateStream (*leaseSet);
|
||||||
m_Stream->Send (m_Buffer, 0, 0); // connect
|
m_Stream->Send (m_Buffer, 0, 0); // connect
|
||||||
|
@ -17,9 +18,9 @@ namespace stream
|
||||||
Receive ();
|
Receive ();
|
||||||
}
|
}
|
||||||
|
|
||||||
I2PTunnelConnection::I2PTunnelConnection (Stream * stream, boost::asio::ip::tcp::socket * socket,
|
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, Stream * stream,
|
||||||
const boost::asio::ip::tcp::endpoint& target):
|
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target):
|
||||||
m_Socket (socket), m_Stream (stream)
|
m_Socket (socket), m_Stream (stream), m_Owner (owner)
|
||||||
{
|
{
|
||||||
if (m_Socket)
|
if (m_Socket)
|
||||||
m_Socket->async_connect (target, boost::bind (&I2PTunnelConnection::HandleConnect,
|
m_Socket->async_connect (target, boost::bind (&I2PTunnelConnection::HandleConnect,
|
||||||
|
@ -40,7 +41,9 @@ namespace stream
|
||||||
void I2PTunnelConnection::Terminate ()
|
void I2PTunnelConnection::Terminate ()
|
||||||
{
|
{
|
||||||
m_Socket->close ();
|
m_Socket->close ();
|
||||||
// TODO: remove from I2PTunnel
|
if (m_Owner)
|
||||||
|
m_Owner->RemoveConnection (this);
|
||||||
|
// TODO: delete
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PTunnelConnection::Receive ()
|
void I2PTunnelConnection::Receive ()
|
||||||
|
@ -118,8 +121,25 @@ namespace stream
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void I2PTunnel::AddConnection (I2PTunnelConnection * conn)
|
||||||
|
{
|
||||||
|
m_Connections.insert (conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void I2PTunnel::RemoveConnection (I2PTunnelConnection * conn)
|
||||||
|
{
|
||||||
|
m_Connections.erase (conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void I2PTunnel::ClearConnections ()
|
||||||
|
{
|
||||||
|
for (auto it: m_Connections)
|
||||||
|
delete it;
|
||||||
|
m_Connections.clear ();
|
||||||
|
}
|
||||||
|
|
||||||
I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination, int port):
|
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)),
|
I2PTunnel (service), m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
|
||||||
m_Destination (destination), m_DestinationIdentHash (nullptr), m_RemoteLeaseSet (nullptr)
|
m_Destination (destination), m_DestinationIdentHash (nullptr), m_RemoteLeaseSet (nullptr)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -162,15 +182,13 @@ namespace stream
|
||||||
void I2PClientTunnel::Stop ()
|
void I2PClientTunnel::Stop ()
|
||||||
{
|
{
|
||||||
m_Acceptor.close();
|
m_Acceptor.close();
|
||||||
for (auto it: m_Connections)
|
ClearConnections ();
|
||||||
delete it;
|
|
||||||
m_Connections.clear ();
|
|
||||||
m_DestinationIdentHash = nullptr;
|
m_DestinationIdentHash = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PClientTunnel::Accept ()
|
void I2PClientTunnel::Accept ()
|
||||||
{
|
{
|
||||||
auto newSocket = new boost::asio::ip::tcp::socket (m_Service);
|
auto newSocket = new boost::asio::ip::tcp::socket (GetService ());
|
||||||
m_Acceptor.async_accept (*newSocket, boost::bind (&I2PClientTunnel::HandleAccept, this,
|
m_Acceptor.async_accept (*newSocket, boost::bind (&I2PClientTunnel::HandleAccept, this,
|
||||||
boost::asio::placeholders::error, newSocket));
|
boost::asio::placeholders::error, newSocket));
|
||||||
}
|
}
|
||||||
|
@ -198,8 +216,8 @@ namespace stream
|
||||||
if (m_RemoteLeaseSet) // leaseSet found
|
if (m_RemoteLeaseSet) // leaseSet found
|
||||||
{
|
{
|
||||||
LogPrint ("New I2PTunnel connection");
|
LogPrint ("New I2PTunnel connection");
|
||||||
auto connection = new I2PTunnelConnection (socket, m_RemoteLeaseSet);
|
auto connection = new I2PTunnelConnection (this, socket, m_RemoteLeaseSet);
|
||||||
m_Connections.insert (connection);
|
AddConnection (connection);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -213,7 +231,7 @@ namespace stream
|
||||||
}
|
}
|
||||||
|
|
||||||
I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port,
|
I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port,
|
||||||
const i2p::data::IdentHash& localDestination): m_Service (service),
|
const i2p::data::IdentHash& localDestination): I2PTunnel (service),
|
||||||
m_Endpoint (boost::asio::ip::address::from_string (address), port)
|
m_Endpoint (boost::asio::ip::address::from_string (address), port)
|
||||||
{
|
{
|
||||||
m_LocalDestination = FindLocalDestination (localDestination);
|
m_LocalDestination = FindLocalDestination (localDestination);
|
||||||
|
@ -228,6 +246,7 @@ namespace stream
|
||||||
|
|
||||||
void I2PServerTunnel::Stop ()
|
void I2PServerTunnel::Stop ()
|
||||||
{
|
{
|
||||||
|
ClearConnections ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PServerTunnel::Accept ()
|
void I2PServerTunnel::Accept ()
|
||||||
|
@ -239,7 +258,7 @@ namespace stream
|
||||||
void I2PServerTunnel::HandleAccept (i2p::stream::Stream * stream)
|
void I2PServerTunnel::HandleAccept (i2p::stream::Stream * stream)
|
||||||
{
|
{
|
||||||
if (stream)
|
if (stream)
|
||||||
new I2PTunnelConnection (stream, new boost::asio::ip::tcp::socket (m_Service), m_Endpoint);
|
new I2PTunnelConnection (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
I2PTunnel.h
33
I2PTunnel.h
|
@ -14,13 +14,15 @@ namespace stream
|
||||||
{
|
{
|
||||||
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 8192;
|
const size_t I2P_TUNNEL_CONNECTION_BUFFER_SIZE = 8192;
|
||||||
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
|
const int I2P_TUNNEL_CONNECTION_MAX_IDLE = 3600; // in seconds
|
||||||
|
|
||||||
|
class I2PTunnel;
|
||||||
class I2PTunnelConnection
|
class I2PTunnelConnection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
I2PTunnelConnection (boost::asio::ip::tcp::socket * socket,
|
I2PTunnelConnection (I2PTunnel * owner, boost::asio::ip::tcp::socket * socket,
|
||||||
const i2p::data::LeaseSet * leaseSet);
|
const i2p::data::LeaseSet * leaseSet);
|
||||||
I2PTunnelConnection (Stream * stream, boost::asio::ip::tcp::socket * socket,
|
I2PTunnelConnection (I2PTunnel * owner, Stream * stream, boost::asio::ip::tcp::socket * socket,
|
||||||
const boost::asio::ip::tcp::endpoint& target);
|
const boost::asio::ip::tcp::endpoint& target);
|
||||||
~I2PTunnelConnection ();
|
~I2PTunnelConnection ();
|
||||||
|
|
||||||
|
@ -41,9 +43,29 @@ namespace stream
|
||||||
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
|
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
|
||||||
boost::asio::ip::tcp::socket * m_Socket;
|
boost::asio::ip::tcp::socket * m_Socket;
|
||||||
Stream * m_Stream;
|
Stream * m_Stream;
|
||||||
|
I2PTunnel * m_Owner;
|
||||||
};
|
};
|
||||||
|
|
||||||
class I2PClientTunnel
|
class I2PTunnel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
I2PTunnel (boost::asio::io_service& service): m_Service (service) {};
|
||||||
|
virtual ~I2PTunnel () { ClearConnections (); };
|
||||||
|
|
||||||
|
void AddConnection (I2PTunnelConnection * conn);
|
||||||
|
void RemoveConnection (I2PTunnelConnection * conn);
|
||||||
|
void ClearConnections ();
|
||||||
|
|
||||||
|
boost::asio::io_service& GetService () { return m_Service; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
boost::asio::io_service& m_Service;
|
||||||
|
std::set<I2PTunnelConnection *> m_Connections;
|
||||||
|
};
|
||||||
|
|
||||||
|
class I2PClientTunnel: public I2PTunnel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -60,15 +82,13 @@ namespace stream
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
boost::asio::io_service& m_Service;
|
|
||||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||||||
std::string m_Destination;
|
std::string m_Destination;
|
||||||
const i2p::data::IdentHash * m_DestinationIdentHash;
|
const i2p::data::IdentHash * m_DestinationIdentHash;
|
||||||
const i2p::data::LeaseSet * m_RemoteLeaseSet;
|
const i2p::data::LeaseSet * m_RemoteLeaseSet;
|
||||||
std::set<I2PTunnelConnection *> m_Connections;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class I2PServerTunnel
|
class I2PServerTunnel: public I2PTunnel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -85,7 +105,6 @@ namespace stream
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
boost::asio::io_service& m_Service;
|
|
||||||
StreamingDestination * m_LocalDestination;
|
StreamingDestination * m_LocalDestination;
|
||||||
boost::asio::ip::tcp::endpoint m_Endpoint;
|
boost::asio::ip::tcp::endpoint m_Endpoint;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue