mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
6.2 KiB
217 lines
6.2 KiB
10 years ago
|
#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;
|
||
|
|
||
10 years ago
|
I2PService::I2PService (std::shared_ptr<ClientDestination> localDestination):
|
||
10 years ago
|
m_LocalDestination (localDestination ? localDestination :
|
||
|
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE))
|
||
|
{
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
I2PService::I2PService (i2p::data::SigningKeyType kt):
|
||
|
m_LocalDestination (i2p::client::context.CreateNewLocalDestination (false, kt))
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) {
|
||
10 years ago
|
assert(streamRequestComplete);
|
||
|
i2p::data::IdentHash identHash;
|
||
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash))
|
||
10 years ago
|
m_LocalDestination->CreateStream (streamRequestComplete, identHash, port);
|
||
10 years ago
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest);
|
||
10 years ago
|
streamRequestComplete (nullptr);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
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);
|
||
|
}
|
||
9 years ago
|
|
||
|
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;
|
||
|
}
|
||
8 years ago
|
Done(shared_from_this());
|
||
9 years ago
|
}
|
||
|
|
||
|
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 {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: upstream receive: no socket");
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
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 {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: downstream receive: no socket");
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void TCPIPPipe::UpstreamWrite(size_t len)
|
||
9 years ago
|
{
|
||
|
if (m_up) {
|
||
9 years ago
|
LogPrint(eLogDebug, "TCPIPPipe: upstream: ", (int) len, " bytes written");
|
||
8 years ago
|
boost::asio::async_write(*m_up, boost::asio::buffer(m_upstream_buf, len),
|
||
9 years ago
|
boost::asio::transfer_all(),
|
||
|
std::bind(&TCPIPPipe::HandleUpstreamWrite,
|
||
|
shared_from_this(),
|
||
|
std::placeholders::_1)
|
||
|
);
|
||
|
} else {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: upstream write: no socket");
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
void TCPIPPipe::DownstreamWrite(size_t len)
|
||
9 years ago
|
{
|
||
|
if (m_down) {
|
||
9 years ago
|
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) len, " bytes written");
|
||
8 years ago
|
boost::asio::async_write(*m_down, boost::asio::buffer(m_downstream_buf, len),
|
||
9 years ago
|
boost::asio::transfer_all(),
|
||
|
std::bind(&TCPIPPipe::HandleDownstreamWrite,
|
||
|
shared_from_this(),
|
||
|
std::placeholders::_1)
|
||
|
);
|
||
|
} else {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: downstream write: no socket");
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void TCPIPPipe::HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) bytes_transfered, " bytes received");
|
||
9 years ago
|
if (ecode) {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: downstream read error:" , ecode.message());
|
||
9 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate();
|
||
|
} else {
|
||
|
if (bytes_transfered > 0 ) {
|
||
|
memcpy(m_upstream_buf, m_downstream_to_up_buf, bytes_transfered);
|
||
|
}
|
||
8 years ago
|
UpstreamWrite(bytes_transfered);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) {
|
||
|
if (ecode) {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: downstream write error:" , ecode.message());
|
||
9 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate();
|
||
8 years ago
|
} else {
|
||
|
AsyncReceiveUpstream();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) {
|
||
|
if (ecode) {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: upstream write error:" , ecode.message());
|
||
9 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate();
|
||
8 years ago
|
} else {
|
||
|
AsyncReceiveDownstream();
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void TCPIPPipe::HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "TCPIPPipe: upstream ", (int)bytes_transfered, " bytes received");
|
||
9 years ago
|
if (ecode) {
|
||
9 years ago
|
LogPrint(eLogError, "TCPIPPipe: upstream read error:" , ecode.message());
|
||
9 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate();
|
||
|
} else {
|
||
|
if (bytes_transfered > 0 ) {
|
||
8 years ago
|
memcpy(m_downstream_buf, m_upstream_to_down_buf, bytes_transfered);
|
||
9 years ago
|
}
|
||
8 years ago
|
DownstreamWrite(bytes_transfered);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void TCPIPAcceptor::Start ()
|
||
|
{
|
||
|
m_Acceptor.listen ();
|
||
|
Accept ();
|
||
|
}
|
||
|
|
||
|
void TCPIPAcceptor::Stop ()
|
||
|
{
|
||
|
m_Acceptor.close();
|
||
|
m_Timer.cancel ();
|
||
|
ClearHandlers();
|
||
|
}
|
||
|
|
||
|
void TCPIPAcceptor::Accept ()
|
||
|
{
|
||
10 years ago
|
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
|
||
10 years ago
|
m_Acceptor.async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this,
|
||
|
std::placeholders::_1, newSocket));
|
||
|
}
|
||
|
|
||
10 years ago
|
void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||
10 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "I2PService: ", GetName(), " accepted");
|
||
10 years ago
|
auto handler = CreateHandler(socket);
|
||
10 years ago
|
if (handler)
|
||
|
{
|
||
10 years ago
|
AddHandler(handler);
|
||
|
handler->Handle();
|
||
10 years ago
|
}
|
||
|
else
|
||
10 years ago
|
socket->close();
|
||
|
Accept();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (ecode != boost::asio::error::operation_aborted)
|
||
9 years ago
|
LogPrint (eLogError, "I2PService: ", GetName(), " closing socket on accept because: ", ecode.message ());
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|