i2pd/Destination.cpp

357 lines
10 KiB
C++
Raw Normal View History

#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <cryptopp/dh.h>
#include "Log.h"
#include "util.h"
2014-10-12 20:22:14 +00:00
#include "NetDb.h"
#include "Destination.h"
namespace i2p
{
2014-10-16 16:37:39 +00:00
namespace client
{
ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
const std::map<std::string, std::string> * params):
2014-10-13 16:33:51 +00:00
m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr),
2014-11-28 18:01:35 +00:00
m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0),
m_DatagramDestination (nullptr), m_PublishConfirmationTimer (nullptr)
{
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
int inboundTunnelLen = DEFAULT_INBOUND_TUNNEL_LENGTH;
int outboundTunnelLen = DEFAULT_OUTBOUND_TUNNEL_LENGTH;
if (params)
{
auto it = params->find (I2CP_PARAM_INBOUND_TUNNEL_LENGTH);
if (it != params->end ())
{
int len = boost::lexical_cast<int>(it->second);
if (len > 0)
{
inboundTunnelLen = len;
LogPrint (eLogInfo, "Inbound tunnel length set to ", len);
}
}
it = params->find (I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH);
if (it != params->end ())
{
int len = boost::lexical_cast<int>(it->second);
if (len > 0)
{
outboundTunnelLen = len;
LogPrint (eLogInfo, "Outbound tunnel length set to ", len);
}
}
}
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, inboundTunnelLen, outboundTunnelLen);
if (m_IsPublic)
LogPrint (eLogInfo, "Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created");
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
}
2014-10-16 16:37:39 +00:00
ClientDestination::~ClientDestination ()
{
2014-10-09 14:05:28 +00:00
Stop ();
2014-10-12 20:22:14 +00:00
for (auto it: m_RemoteLeaseSets)
delete it.second;
if (m_Pool)
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
delete m_LeaseSet;
2014-10-13 16:33:51 +00:00
delete m_Work;
2014-11-28 18:01:35 +00:00
delete m_PublishConfirmationTimer;
2014-10-13 16:33:51 +00:00
delete m_Service;
delete m_StreamingDestination;
delete m_DatagramDestination;
}
2014-10-16 16:37:39 +00:00
void ClientDestination::Run ()
2014-10-09 14:05:28 +00:00
{
2014-10-13 16:33:51 +00:00
if (m_Service)
m_Service->run ();
2014-10-09 14:05:28 +00:00
}
2014-10-16 16:37:39 +00:00
void ClientDestination::Start ()
2014-10-09 14:05:28 +00:00
{
2014-10-13 16:33:51 +00:00
m_Service = new boost::asio::io_service;
2014-11-28 18:01:35 +00:00
m_PublishConfirmationTimer = new boost::asio::deadline_timer (*m_Service);
2014-10-13 16:33:51 +00:00
m_Work = new boost::asio::io_service::work (*m_Service);
m_Pool->SetActive (true);
2014-10-09 14:05:28 +00:00
m_IsRunning = true;
2014-10-16 16:37:39 +00:00
m_Thread = new std::thread (std::bind (&ClientDestination::Run, this));
m_StreamingDestination->Start ();
2014-10-09 14:05:28 +00:00
}
2014-10-16 16:37:39 +00:00
void ClientDestination::Stop ()
2014-10-09 14:05:28 +00:00
{
m_StreamingDestination->Stop ();
2014-10-31 20:44:44 +00:00
if (m_DatagramDestination)
{
auto d = m_DatagramDestination;
m_DatagramDestination = nullptr;
delete d;
}
2014-10-11 22:21:52 +00:00
if (m_Pool)
i2p::tunnel::tunnels.StopTunnelPool (m_Pool);
2014-10-09 14:05:28 +00:00
m_IsRunning = false;
2014-10-13 16:35:44 +00:00
if (m_Service)
m_Service->stop ();
2014-10-09 14:05:28 +00:00
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
2014-11-29 13:31:12 +00:00
delete m_PublishConfirmationTimer; m_PublishConfirmationTimer = nullptr;
2014-10-13 16:33:51 +00:00
delete m_Work; m_Work = nullptr;
delete m_Service; m_Service = nullptr;
2014-10-09 14:05:28 +00:00
}
2014-10-16 16:37:39 +00:00
const i2p::data::LeaseSet * ClientDestination::FindLeaseSet (const i2p::data::IdentHash& ident)
{
2014-10-16 16:37:39 +00:00
auto it = m_RemoteLeaseSets.find (ident);
if (it != m_RemoteLeaseSets.end ())
{
2014-10-16 16:37:39 +00:00
if (it->second->HasNonExpiredLeases ())
return it->second;
else
{
2014-10-16 16:37:39 +00:00
LogPrint ("All leases of remote LeaseSet expired. Request it");
i2p::data::netdb.RequestDestination (ident, true, m_Pool);
}
}
2014-10-16 16:37:39 +00:00
else
{
2014-10-16 16:37:39 +00:00
auto ls = i2p::data::netdb.FindLeaseSet (ident);
if (ls)
{
ls = new i2p::data::LeaseSet (*ls);
m_RemoteLeaseSets[ident] = ls;
return ls;
}
2014-10-16 16:37:39 +00:00
}
return nullptr;
}
2014-10-16 16:37:39 +00:00
const i2p::data::LeaseSet * ClientDestination::GetLeaseSet ()
{
if (!m_Pool) return nullptr;
if (!m_LeaseSet)
UpdateLeaseSet ();
return m_LeaseSet;
}
2014-10-16 16:37:39 +00:00
void ClientDestination::UpdateLeaseSet ()
{
auto newLeaseSet = new i2p::data::LeaseSet (*m_Pool);
if (!m_LeaseSet)
m_LeaseSet = newLeaseSet;
else
{
// TODO: implement it better
*m_LeaseSet = *newLeaseSet;
delete newLeaseSet;
}
}
2014-10-16 16:37:39 +00:00
void ClientDestination::ProcessGarlicMessage (I2NPMessage * msg)
{
2014-11-27 02:59:07 +00:00
if (m_Service)
m_Service->post (std::bind (&ClientDestination::HandleGarlicMessage, this, msg));
else
{
LogPrint (eLogWarning, "Destination's thread is not running");
i2p::DeleteI2NPMessage (msg);
}
2014-10-16 16:37:39 +00:00
}
void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg)
{
2014-11-27 02:59:07 +00:00
if (m_Service)
m_Service->post (std::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg));
else
{
LogPrint (eLogWarning, "Destination's thread is not running");
i2p::DeleteI2NPMessage (msg);
}
2014-10-16 16:37:39 +00:00
}
void ClientDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from)
{
I2NPHeader * header = (I2NPHeader *)buf;
switch (header->typeID)
{
case eI2NPData:
HandleDataMessage (buf + sizeof (I2NPHeader), be16toh (header->size));
break;
case eI2NPDatabaseStore:
HandleDatabaseStoreMessage (buf + sizeof (I2NPHeader), be16toh (header->size));
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from)); // TODO: remove
break;
default:
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
}
}
void ClientDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len)
{
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)buf;
size_t offset = sizeof (I2NPDatabaseStoreMsg);
if (msg->replyToken) // TODO:
offset += 36;
if (msg->type == 1) // LeaseSet
{
2014-11-28 18:01:35 +00:00
LogPrint (eLogDebug, "Remote LeaseSet");
2014-10-16 16:37:39 +00:00
auto it = m_RemoteLeaseSets.find (msg->key);
if (it != m_RemoteLeaseSets.end ())
{
it->second->Update (buf + offset, len - offset);
2014-11-28 18:01:35 +00:00
LogPrint (eLogDebug, "Remote LeaseSet updated");
2014-10-16 16:37:39 +00:00
}
else
{
2014-11-28 18:01:35 +00:00
LogPrint (eLogDebug, "New remote LeaseSet added");
2014-10-16 16:37:39 +00:00
m_RemoteLeaseSets[msg->key] = new i2p::data::LeaseSet (buf + offset, len - offset);
}
}
else
2014-11-28 18:01:35 +00:00
LogPrint (eLogError, "Unexpected client's DatabaseStore type ", msg->type, ". Dropped");
}
void ClientDestination::HandleDeliveryStatusMessage (I2NPMessage * msg)
{
I2NPDeliveryStatusMsg * deliveryStatus = (I2NPDeliveryStatusMsg *)msg->GetPayload ();
uint32_t msgID = be32toh (deliveryStatus->msgID);
if (msgID == m_PublishReplyToken)
{
LogPrint (eLogDebug, "Publishing confirmed");
m_PublishReplyToken = 0;
i2p::DeleteI2NPMessage (msg);
}
else
i2p::garlic::GarlicDestination::HandleDeliveryStatusMessage (msg);
2014-10-16 16:37:39 +00:00
}
void ClientDestination::SetLeaseSetUpdated ()
{
i2p::garlic::GarlicDestination::SetLeaseSetUpdated ();
UpdateLeaseSet ();
if (m_IsPublic)
2014-11-28 18:01:35 +00:00
Publish ();
}
void ClientDestination::Publish ()
{
if (!m_LeaseSet || !m_Pool)
{
LogPrint (eLogError, "Can't publish non-existing LeaseSet");
return;
}
if (m_PublishReplyToken)
{
LogPrint (eLogInfo, "Publishing is pending");
return;
}
auto outbound = m_Pool->GetNextOutboundTunnel ();
if (!outbound)
{
LogPrint ("Can't publish LeaseSet. No outbound tunnels");
return;
}
std::set<i2p::data::IdentHash> excluded;
auto floodfill = i2p::data::netdb.GetClosestFloodfill (m_LeaseSet->GetIdentHash (), excluded);
if (!floodfill)
{
LogPrint ("Can't publish LeaseSet. No floodfills found");
return;
}
LogPrint (eLogDebug, "Publish LeaseSet of ", GetIdentHash ().ToBase32 ());
m_PublishReplyToken = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
auto msg = WrapMessage (*floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken));
if (m_PublishConfirmationTimer)
{
m_PublishConfirmationTimer->expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT));
m_PublishConfirmationTimer->async_wait (std::bind (&ClientDestination::HandlePublishConfirmationTimer,
this, std::placeholders::_1));
}
else
{
LogPrint (eLogWarning, "Destination's thread is not running");
m_PublishReplyToken = 0;
}
outbound->SendTunnelDataMsg (floodfill->GetIdentHash (), 0, msg);
}
void ClientDestination::HandlePublishConfirmationTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
if (m_PublishReplyToken)
{
LogPrint (eLogWarning, "Publish confirmation was not received in ", PUBLISH_CONFIRMATION_TIMEOUT, "seconds. Try again");
m_PublishReplyToken = 0;
Publish ();
}
}
2014-10-16 16:37:39 +00:00
}
void ClientDestination::HandleDataMessage (const uint8_t * buf, size_t len)
{
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
// we assume I2CP payload
switch (buf[9])
{
case PROTOCOL_TYPE_STREAMING:
// streaming protocol
if (m_StreamingDestination)
m_StreamingDestination->HandleDataMessagePayload (buf, length);
2014-10-22 19:30:25 +00:00
else
LogPrint ("Missing streaming destination");
break;
case PROTOCOL_TYPE_DATAGRAM:
// datagram protocol
if (m_DatagramDestination)
m_DatagramDestination->HandleDataMessagePayload (buf, length);
else
LogPrint ("Missing streaming destination");
break;
default:
LogPrint ("Data: unexpected protocol ", buf[9]);
}
}
2014-11-23 16:33:58 +00:00
std::shared_ptr<i2p::stream::Stream> ClientDestination::CreateStream (const i2p::data::LeaseSet& remote, int port)
{
if (m_StreamingDestination)
2014-10-23 01:36:11 +00:00
return m_StreamingDestination->CreateNewOutgoingStream (remote, port);
return nullptr;
}
2014-11-18 14:33:58 +00:00
void ClientDestination::AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor)
{
if (m_StreamingDestination)
m_StreamingDestination->SetAcceptor (acceptor);
}
void ClientDestination::StopAcceptingStreams ()
{
if (m_StreamingDestination)
m_StreamingDestination->ResetAcceptor ();
}
bool ClientDestination::IsAcceptingStreams () const
{
if (m_StreamingDestination)
return m_StreamingDestination->IsAcceptorSet ();
return false;
}
2014-10-31 20:44:44 +00:00
i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination ()
{
if (!m_DatagramDestination)
m_DatagramDestination = new i2p::datagram::DatagramDestination (*this);
2014-10-31 20:44:44 +00:00
return m_DatagramDestination;
}
2014-10-16 16:37:39 +00:00
}
}