2015-01-07 18:09:59 +00: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 20:40:50 +00:00
|
|
|
I2PService::I2PService (std::shared_ptr<ClientDestination> localDestination):
|
2015-01-07 18:09:59 +00:00
|
|
|
m_LocalDestination (localDestination ? localDestination :
|
|
|
|
i2p::client::context.CreateNewLocalDestination (false, I2P_SERVICE_DEFAULT_KEY_TYPE))
|
|
|
|
{
|
|
|
|
}
|
2015-01-07 19:44:24 +00:00
|
|
|
|
2015-01-07 20:15:04 +00:00
|
|
|
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) {
|
2015-01-07 19:44:24 +00:00
|
|
|
assert(streamRequestComplete);
|
|
|
|
i2p::data::IdentHash identHash;
|
|
|
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash))
|
2015-01-07 20:15:04 +00:00
|
|
|
m_LocalDestination->CreateStream (streamRequestComplete, identHash, port);
|
2015-01-07 19:44:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Remote destination ", dest, " not found");
|
|
|
|
streamRequestComplete (nullptr);
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 00:31:31 +00:00
|
|
|
|
|
|
|
void TCPIPAcceptor::Start ()
|
|
|
|
{
|
|
|
|
m_Acceptor.listen ();
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPAcceptor::Stop ()
|
|
|
|
{
|
|
|
|
m_Acceptor.close();
|
|
|
|
m_Timer.cancel ();
|
|
|
|
ClearHandlers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPIPAcceptor::Accept ()
|
|
|
|
{
|
2015-04-06 18:41:07 +00:00
|
|
|
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
|
2015-01-08 00:31:31 +00:00
|
|
|
m_Acceptor.async_accept (*newSocket, std::bind (&TCPIPAcceptor::HandleAccept, this,
|
|
|
|
std::placeholders::_1, newSocket));
|
|
|
|
}
|
|
|
|
|
2015-04-06 18:41:07 +00:00
|
|
|
void TCPIPAcceptor::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
2015-01-08 00:31:31 +00:00
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
|
|
|
LogPrint(eLogDebug,"--- ",GetName()," accepted");
|
|
|
|
auto handler = CreateHandler(socket);
|
2015-04-06 18:41:07 +00:00
|
|
|
if (handler)
|
|
|
|
{
|
2015-01-08 00:31:31 +00:00
|
|
|
AddHandler(handler);
|
|
|
|
handler->Handle();
|
2015-04-06 18:41:07 +00:00
|
|
|
}
|
|
|
|
else
|
2015-01-08 00:31:31 +00:00
|
|
|
socket->close();
|
|
|
|
Accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
LogPrint (eLogError,"--- ",GetName()," Closing socket on accept because: ", ecode.message ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
}
|
|
|
|
}
|