2014-08-13 23:25:52 +04:00
|
|
|
#include "base64.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "NetDb.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
#include "Destination.h"
|
2014-10-16 04:52:17 +04:00
|
|
|
#include "ClientContext.h"
|
2014-08-13 05:14:19 +04:00
|
|
|
#include "I2PTunnel.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 18:28:44 +04:00
|
|
|
namespace client
|
2014-08-13 05:14:19 +04:00
|
|
|
{
|
2014-09-10 06:40:12 +04:00
|
|
|
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner,
|
|
|
|
boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet):
|
2014-11-24 06:23:17 +03:00
|
|
|
m_Socket (socket), m_Owner (owner), m_RemoteEndpoint (socket->remote_endpoint ())
|
2014-08-13 05:14:19 +04:00
|
|
|
{
|
2014-10-22 22:01:23 +04:00
|
|
|
m_Stream = m_Owner->GetLocalDestination ()->CreateStream (*leaseSet);
|
2014-08-13 05:14:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:33:58 +03:00
|
|
|
I2PTunnelConnection::I2PTunnelConnection (I2PTunnel * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
2014-09-10 06:40:12 +04:00
|
|
|
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target):
|
2014-11-24 06:23:17 +03:00
|
|
|
m_Socket (socket), m_Stream (stream), m_Owner (owner), m_RemoteEndpoint (target)
|
2014-08-20 23:03:10 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:14:19 +04:00
|
|
|
I2PTunnelConnection::~I2PTunnelConnection ()
|
|
|
|
{
|
|
|
|
delete m_Socket;
|
2014-08-14 05:04:23 +04:00
|
|
|
}
|
|
|
|
|
2014-12-04 04:37:20 +03:00
|
|
|
void I2PTunnelConnection::I2PConnect (const uint8_t * msg, size_t len)
|
2014-11-24 06:23:17 +03:00
|
|
|
{
|
2014-12-04 04:37:20 +03:00
|
|
|
if (msg)
|
|
|
|
m_Stream->Send (msg, len); // connect and send
|
|
|
|
else
|
|
|
|
m_Stream->Send (m_Buffer, 0); // connect
|
2014-11-24 06:23:17 +03:00
|
|
|
StreamReceive ();
|
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::Connect ()
|
|
|
|
{
|
|
|
|
if (m_Socket)
|
|
|
|
m_Socket->async_connect (m_RemoteEndpoint, std::bind (&I2PTunnelConnection::HandleConnect,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
2014-08-14 05:04:23 +04:00
|
|
|
void I2PTunnelConnection::Terminate ()
|
|
|
|
{
|
2014-10-09 03:44:12 +04:00
|
|
|
if (m_Stream)
|
|
|
|
{
|
|
|
|
m_Stream->Close ();
|
2014-11-23 19:33:58 +03:00
|
|
|
m_Stream.reset ();
|
2014-10-09 03:44:12 +04:00
|
|
|
}
|
2014-08-18 03:14:40 +04:00
|
|
|
m_Socket->close ();
|
2014-09-10 06:40:12 +04:00
|
|
|
if (m_Owner)
|
2014-11-24 06:23:17 +03:00
|
|
|
m_Owner->RemoveConnection (shared_from_this ());
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::Receive ()
|
|
|
|
{
|
|
|
|
m_Socket->async_read_some (boost::asio::buffer(m_Buffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
2014-11-24 06:23:17 +03:00
|
|
|
std::bind(&I2PTunnelConnection::HandleReceived, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel read error: ", ecode.message ());
|
2014-10-09 03:44:12 +04:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
else
|
2014-08-14 05:04:23 +04:00
|
|
|
{
|
2014-08-13 23:25:52 +04:00
|
|
|
if (m_Stream)
|
2014-10-02 05:18:41 +04:00
|
|
|
m_Stream->Send (m_Buffer, bytes_transferred);
|
2014-08-13 23:25:52 +04:00
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::HandleWrite (const boost::system::error_code& ecode)
|
|
|
|
{
|
2014-08-18 03:14:40 +04:00
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel write error: ", ecode.message ());
|
2014-10-09 03:44:12 +04:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-18 03:14:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
StreamReceive ();
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::StreamReceive ()
|
|
|
|
{
|
|
|
|
if (m_Stream)
|
|
|
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
2014-11-24 06:23:17 +03:00
|
|
|
std::bind (&I2PTunnelConnection::HandleStreamReceive, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2),
|
2014-08-13 23:25:52 +04:00
|
|
|
I2P_TUNNEL_CONNECTION_MAX_IDLE);
|
2014-08-13 05:14:19 +04:00
|
|
|
}
|
|
|
|
|
2014-08-13 23:25:52 +04:00
|
|
|
void I2PTunnelConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel stream read error: ", ecode.message ());
|
2014-10-09 03:44:12 +04:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
2014-11-24 06:23:17 +03:00
|
|
|
std::bind (&I2PTunnelConnection::HandleWrite, shared_from_this (), std::placeholders::_1));
|
2014-08-13 23:25:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:03:10 +04:00
|
|
|
void I2PTunnelConnection::HandleConnect (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel connect error: ", ecode.message ());
|
2014-11-24 06:23:17 +03:00
|
|
|
Terminate ();
|
2014-08-20 23:03:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel connected");
|
|
|
|
StreamReceive ();
|
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 06:23:17 +03:00
|
|
|
void I2PTunnel::AddConnection (std::shared_ptr<I2PTunnelConnection> conn)
|
2014-09-10 06:40:12 +04:00
|
|
|
{
|
|
|
|
m_Connections.insert (conn);
|
|
|
|
}
|
|
|
|
|
2014-11-24 06:23:17 +03:00
|
|
|
void I2PTunnel::RemoveConnection (std::shared_ptr<I2PTunnelConnection> conn)
|
2014-09-10 06:40:12 +04:00
|
|
|
{
|
|
|
|
m_Connections.erase (conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnel::ClearConnections ()
|
|
|
|
{
|
|
|
|
m_Connections.clear ();
|
|
|
|
}
|
|
|
|
|
2014-10-01 18:58:28 +04:00
|
|
|
I2PClientTunnel::I2PClientTunnel (boost::asio::io_service& service, const std::string& destination,
|
2014-10-22 19:46:54 +04:00
|
|
|
int port, ClientDestination * localDestination):
|
2014-10-15 22:32:19 +04:00
|
|
|
I2PTunnel (service, localDestination ? localDestination :
|
2014-10-16 18:28:44 +04:00
|
|
|
i2p::client::context.CreateNewLocalDestination (false, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256)),
|
2014-10-01 18:58:28 +04:00
|
|
|
m_Acceptor (service, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
|
2014-10-15 20:07:06 +04:00
|
|
|
m_Timer (service), m_Destination (destination), m_DestinationIdentHash (nullptr),
|
|
|
|
m_RemoteLeaseSet (nullptr)
|
2014-08-13 05:14:19 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 23:25:52 +04:00
|
|
|
I2PClientTunnel::~I2PClientTunnel ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
2014-08-13 05:14:19 +04:00
|
|
|
|
2014-08-13 23:25:52 +04:00
|
|
|
void I2PClientTunnel::Start ()
|
|
|
|
{
|
2014-10-03 22:22:32 +04:00
|
|
|
i2p::data::IdentHash identHash;
|
2014-10-24 23:22:36 +04:00
|
|
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash))
|
2014-10-03 22:22:32 +04:00
|
|
|
m_DestinationIdentHash = new i2p::data::IdentHash (identHash);
|
2014-10-15 20:07:06 +04:00
|
|
|
if (!m_DestinationIdentHash)
|
2014-08-13 23:25:52 +04:00
|
|
|
LogPrint ("I2PTunnel unknown destination ", m_Destination);
|
|
|
|
m_Acceptor.listen ();
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PClientTunnel::Stop ()
|
|
|
|
{
|
|
|
|
m_Acceptor.close();
|
2014-10-15 20:07:06 +04:00
|
|
|
m_Timer.cancel ();
|
2014-09-10 06:40:12 +04:00
|
|
|
ClearConnections ();
|
2014-08-13 23:25:52 +04:00
|
|
|
m_DestinationIdentHash = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:14:19 +04:00
|
|
|
void I2PClientTunnel::Accept ()
|
|
|
|
{
|
2014-09-10 06:40:12 +04:00
|
|
|
auto newSocket = new boost::asio::ip::tcp::socket (GetService ());
|
2014-11-24 06:23:17 +03:00
|
|
|
m_Acceptor.async_accept (*newSocket, std::bind (&I2PClientTunnel::HandleAccept, this,
|
|
|
|
std::placeholders::_1, newSocket));
|
2014-08-13 05:14:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PClientTunnel::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2014-10-15 20:30:20 +04:00
|
|
|
if (!m_DestinationIdentHash)
|
2014-08-14 22:32:00 +04:00
|
|
|
{
|
2014-10-15 20:30:20 +04:00
|
|
|
i2p::data::IdentHash identHash;
|
2014-10-24 23:22:36 +04:00
|
|
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash))
|
2014-10-15 20:30:20 +04:00
|
|
|
m_DestinationIdentHash = new i2p::data::IdentHash (identHash);
|
|
|
|
}
|
|
|
|
if (m_DestinationIdentHash)
|
|
|
|
{
|
|
|
|
// try to get a LeaseSet
|
|
|
|
m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*m_DestinationIdentHash);
|
|
|
|
if (m_RemoteLeaseSet && m_RemoteLeaseSet->HasNonExpiredLeases ())
|
|
|
|
CreateConnection (socket);
|
2014-08-14 22:32:00 +04:00
|
|
|
else
|
|
|
|
{
|
2014-10-15 20:45:32 +04:00
|
|
|
i2p::data::netdb.RequestDestination (*m_DestinationIdentHash, true, GetLocalDestination ()->GetTunnelPool ());
|
2014-10-15 20:30:20 +04:00
|
|
|
m_Timer.expires_from_now (boost::posix_time::seconds (I2P_TUNNEL_DESTINATION_REQUEST_TIMEOUT));
|
2014-11-24 06:23:17 +03:00
|
|
|
m_Timer.async_wait (std::bind (&I2PClientTunnel::HandleDestinationRequestTimer,
|
|
|
|
this, std::placeholders::_1, socket));
|
2014-08-14 22:32:00 +04:00
|
|
|
}
|
2014-10-15 20:30:20 +04:00
|
|
|
}
|
2014-08-13 05:14:19 +04:00
|
|
|
else
|
2014-10-15 20:30:20 +04:00
|
|
|
{
|
|
|
|
LogPrint ("Remote destination ", m_Destination, " not found");
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:14:19 +04:00
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete socket;
|
|
|
|
}
|
2014-08-20 23:03:10 +04:00
|
|
|
|
2014-10-15 20:07:06 +04:00
|
|
|
void I2PClientTunnel::HandleDestinationRequestTimer (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
if (m_DestinationIdentHash)
|
|
|
|
{
|
|
|
|
m_RemoteLeaseSet = GetLocalDestination ()->FindLeaseSet (*m_DestinationIdentHash);
|
|
|
|
CreateConnection (socket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PClientTunnel::CreateConnection (boost::asio::ip::tcp::socket * socket)
|
|
|
|
{
|
|
|
|
if (m_RemoteLeaseSet) // leaseSet found
|
|
|
|
{
|
|
|
|
LogPrint ("New I2PTunnel connection");
|
2014-11-24 06:23:17 +03:00
|
|
|
auto connection = std::make_shared<I2PTunnelConnection>(this, socket, m_RemoteLeaseSet);
|
2014-10-15 20:07:06 +04:00
|
|
|
AddConnection (connection);
|
2014-11-24 06:23:17 +03:00
|
|
|
connection->I2PConnect ();
|
2014-10-15 20:07:06 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("LeaseSet for I2PTunnel destination not found");
|
|
|
|
delete socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:03:10 +04:00
|
|
|
I2PServerTunnel::I2PServerTunnel (boost::asio::io_service& service, const std::string& address, int port,
|
2014-10-22 19:46:54 +04:00
|
|
|
ClientDestination * localDestination): I2PTunnel (service, localDestination),
|
2014-08-20 23:03:10 +04:00
|
|
|
m_Endpoint (boost::asio::ip::address::from_string (address), port)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Start ()
|
|
|
|
{
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Stop ()
|
|
|
|
{
|
2014-09-10 06:40:12 +04:00
|
|
|
ClearConnections ();
|
2014-08-20 23:03:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Accept ()
|
|
|
|
{
|
2014-10-01 18:58:28 +04:00
|
|
|
auto localDestination = GetLocalDestination ();
|
|
|
|
if (localDestination)
|
2014-10-22 22:01:23 +04:00
|
|
|
localDestination->AcceptStreams (std::bind (&I2PServerTunnel::HandleAccept, this, std::placeholders::_1));
|
2014-10-01 18:58:28 +04:00
|
|
|
else
|
|
|
|
LogPrint ("Local destination not set for server tunnel");
|
2014-08-20 23:03:10 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:33:58 +03:00
|
|
|
void I2PServerTunnel::HandleAccept (std::shared_ptr<i2p::stream::Stream> stream)
|
2014-08-20 23:03:10 +04:00
|
|
|
{
|
|
|
|
if (stream)
|
2014-11-24 06:23:17 +03:00
|
|
|
{
|
|
|
|
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint);
|
|
|
|
AddConnection (conn);
|
|
|
|
conn->Connect ();
|
|
|
|
}
|
2014-08-20 23:03:10 +04:00
|
|
|
}
|
2014-08-13 05:14:19 +04:00
|
|
|
}
|
|
|
|
}
|