2015-01-07 21:09:59 +03:00
|
|
|
#include "Destination.h"
|
|
|
|
#include "Identity.h"
|
|
|
|
#include "ClientContext.h"
|
|
|
|
#include "I2PService.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
|
|
|
static const i2p::data::SigningKeyType I2P_SERVICE_DEFAULT_KEY_TYPE = i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256;
|
|
|
|
|
2015-02-24 23:40:50 +03:00
|
|
|
I2PService::I2PService (std::shared_ptr<ClientDestination> localDestination):
|
2015-01-07 21:09:59 +03:00
|
|
|
m_LocalDestination (localDestination ? localDestination :
|
2017-07-28 22:12:15 +03:00
|
|
|
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE)), isUpdated (true)
|
2015-01-07 21:09:59 +03:00
|
|
|
{
|
2017-07-06 23:12:06 +03:00
|
|
|
m_LocalDestination->Acquire ();
|
2015-01-07 21:09:59 +03:00
|
|
|
}
|
2015-01-07 22:44:24 +03:00
|
|
|
|
2015-01-07 23:15:04 +03:00
|
|
|
I2PService::I2PService (i2p::data::SigningKeyType kt):
|
2017-07-28 22:12:15 +03:00
|
|
|
m_LocalDestination (i2p::client::context.CreateNewLocalDestination (false, kt)),
|
|
|
|
isUpdated (true)
|
2015-01-07 23:15:04 +03:00
|
|
|
{
|
2017-07-06 23:12:06 +03:00
|
|
|
m_LocalDestination->Acquire ();
|
2015-01-07 23:15:04 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:12:06 +03:00
|
|
|
I2PService::~I2PService ()
|
|
|
|
{
|
|
|
|
ClearHandlers ();
|
|
|
|
if (m_LocalDestination) m_LocalDestination->Release ();
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:15:04 +03:00
|
|
|
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) {
|
2015-01-07 22:44:24 +03:00
|
|
|
assert(streamRequestComplete);
|
|
|
|
i2p::data::IdentHash identHash;
|
|
|
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash))
|
2015-01-07 23:15:04 +03:00
|
|
|
m_LocalDestination->CreateStream (streamRequestComplete, identHash, port);
|
2015-01-07 22:44:24 +03:00
|
|
|
else
|
|
|
|
{
|
2016-06-03 03:00:00 +03:00
|
|
|
LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest);
|
2015-01-07 22:44:24 +03:00
|
|
|
streamRequestComplete (nullptr);
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 03:31:31 +03:00
|
|
|
|
2016-07-28 16:25:05 +03:00
|
|
|
TCPIPPipe::TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream) : I2PServiceHandler(owner), m_up(upstream), m_down(downstream)
|
|
|
|
{
|
|
|
|
boost::asio::socket_base::receive_buffer_size option(TCP_IP_PIPE_BUFFER_SIZE);
|
|
|
|
upstream->set_option(option);
|
|
|
|
downstream->set_option(option);
|
|
|
|
}
|
2016-02-27 01:06:11 +03:00
|
|
|
|
|
|
|
TCPIPPipe::~TCPIPPipe()
|
|
|
|
{
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::Start()
|
|
|
|
{
|
|
|
|
AsyncReceiveUpstream();
|
|
|
|
AsyncReceiveDownstream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::Terminate()
|
|
|
|
{
|
|
|
|
if(Kill()) return;
|
|
|
|
if (m_up) {
|
|
|
|
if (m_up->is_open()) {
|
|
|
|
m_up->close();
|
|
|
|
}
|
|
|
|
m_up = nullptr;
|
|
|
|
}
|
|
|
|
if (m_down) {
|
|
|
|
if (m_down->is_open()) {
|
|
|
|
m_down->close();
|
|
|
|
}
|
|
|
|
m_down = nullptr;
|
|
|
|
}
|
2016-11-20 20:13:11 +03:00
|
|
|
Done(shared_from_this());
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::AsyncReceiveUpstream()
|
|
|
|
{
|
|
|
|
if (m_up) {
|
|
|
|
m_up->async_read_some(boost::asio::buffer(m_upstream_to_down_buf, TCP_IP_PIPE_BUFFER_SIZE),
|
|
|
|
std::bind(&TCPIPPipe::HandleUpstreamReceived, shared_from_this(),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
|
|
|
} else {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: upstream receive: no socket");
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::AsyncReceiveDownstream()
|
|
|
|
{
|
|
|
|
if (m_down) {
|
|
|
|
m_down->async_read_some(boost::asio::buffer(m_downstream_to_up_buf, TCP_IP_PIPE_BUFFER_SIZE),
|
|
|
|
std::bind(&TCPIPPipe::HandleDownstreamReceived, shared_from_this(),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
|
|
|
} else {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: downstream receive: no socket");
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 20:13:11 +03:00
|
|
|
void TCPIPPipe::UpstreamWrite(size_t len)
|
2016-02-27 01:06:11 +03:00
|
|
|
{
|
|
|
|
if (m_up) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogDebug, "TCPIPPipe: upstream: ", (int) len, " bytes written");
|
2016-11-20 20:13:11 +03:00
|
|
|
boost::asio::async_write(*m_up, boost::asio::buffer(m_upstream_buf, len),
|
2016-02-27 01:06:11 +03:00
|
|
|
boost::asio::transfer_all(),
|
|
|
|
std::bind(&TCPIPPipe::HandleUpstreamWrite,
|
|
|
|
shared_from_this(),
|
|
|
|
std::placeholders::_1)
|
|
|
|
);
|
|
|
|
} else {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: upstream write: no socket");
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-20 20:13:11 +03:00
|
|
|
void TCPIPPipe::DownstreamWrite(size_t len)
|
2016-02-27 01:06:11 +03:00
|
|
|
{
|
|
|
|
if (m_down) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) len, " bytes written");
|
2016-11-20 20:13:11 +03:00
|
|
|
boost::asio::async_write(*m_down, boost::asio::buffer(m_downstream_buf, len),
|
2016-02-27 01:06:11 +03:00
|
|
|
boost::asio::transfer_all(),
|
|
|
|
std::bind(&TCPIPPipe::HandleDownstreamWrite,
|
|
|
|
shared_from_this(),
|
|
|
|
std::placeholders::_1)
|
|
|
|
);
|
|
|
|
} else {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: downstream write: no socket");
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TCPIPPipe::HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
|
|
|
{
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) bytes_transfered, " bytes received");
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: downstream read error:" , ecode.message());
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate();
|
|
|
|
} else {
|
|
|
|
if (bytes_transfered > 0 ) {
|
|
|
|
memcpy(m_upstream_buf, m_downstream_to_up_buf, bytes_transfered);
|
|
|
|
}
|
2016-11-20 20:13:11 +03:00
|
|
|
UpstreamWrite(bytes_transfered);
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) {
|
|
|
|
if (ecode) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: downstream write error:" , ecode.message());
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate();
|
2016-11-20 20:13:11 +03:00
|
|
|
} else {
|
|
|
|
AsyncReceiveUpstream();
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) {
|
|
|
|
if (ecode) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: upstream write error:" , ecode.message());
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate();
|
2016-11-20 20:13:11 +03:00
|
|
|
} else {
|
|
|
|
AsyncReceiveDownstream();
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPPipe::HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
|
|
|
{
|
2016-05-31 04:42:25 +03:00
|
|
|
LogPrint(eLogDebug, "TCPIPPipe: upstream ", (int)bytes_transfered, " bytes received");
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode) {
|
2016-05-31 03:00:00 +03:00
|
|
|
LogPrint(eLogError, "TCPIPPipe: upstream read error:" , ecode.message());
|
2016-02-27 01:06:11 +03:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate();
|
|
|
|
} else {
|
|
|
|
if (bytes_transfered > 0 ) {
|
2016-11-20 20:13:11 +03:00
|
|
|
memcpy(m_downstream_buf, m_upstream_to_down_buf, bytes_transfered);
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
2016-11-20 20:13:11 +03:00
|
|
|
DownstreamWrite(bytes_transfered);
|
2016-02-27 01:06:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:31:31 +03:00
|
|
|
void TCPIPAcceptor::Start ()
|
|
|
|
{
|
2017-08-03 04:00:04 +03:00
|
|
|
m_Acceptor.reset (new boost::asio::ip::tcp::acceptor (GetService (), m_LocalEndpoint));
|
|
|
|
m_Acceptor->listen ();
|
2015-01-08 03:31:31 +03:00
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPAcceptor::Stop ()
|
|
|
|
{
|
2017-08-03 04:00:04 +03:00
|
|
|
if (m_Acceptor)
|
|
|
|
{
|
|
|
|
m_Acceptor->close();
|
|
|
|
m_Acceptor.reset (nullptr);
|
|
|
|
}
|
2015-01-08 03:31:31 +03:00
|
|
|
m_Timer.cancel ();
|
|
|
|
ClearHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPAcceptor::Accept ()
|
|
|
|
{
|
2015-04-06 21:41:07 +03:00
|
|
|
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
|
2017-08-03 04:00:04 +03:00
|
|
|
m_Acceptor->async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this,
|
2015-01-08 03:31:31 +03:00
|
|
|
std::placeholders::_1, newSocket));
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:41:07 +03:00
|
|
|
void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
2015-01-08 03:31:31 +03:00
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
LogPrint(eLogDebug, "I2PService: ", GetName(), " accepted");
|
2015-01-08 03:31:31 +03:00
|
|
|
auto handler = CreateHandler(socket);
|
2015-04-06 21:41:07 +03:00
|
|
|
if (handler)
|
|
|
|
{
|
2015-01-08 03:31:31 +03:00
|
|
|
AddHandler(handler);
|
|
|
|
handler->Handle();
|
2015-04-06 21:41:07 +03:00
|
|
|
}
|
|
|
|
else
|
2015-01-08 03:31:31 +03:00
|
|
|
socket->close();
|
|
|
|
Accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
2016-02-11 03:00:00 +03:00
|
|
|
LogPrint (eLogError, "I2PService: ", GetName(), " closing socket on accept because: ", ecode.message ());
|
2015-01-08 03:31:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-01-07 21:09:59 +03:00
|
|
|
}
|
|
|
|
}
|