i2pd/Destination.cpp

321 lines
9.8 KiB
C++
Raw Normal View History

#include <fstream>
#include <algorithm>
#include <cryptopp/dh.h>
#include <cryptopp/gzip.h>
#include "Log.h"
#include "util.h"
2014-10-13 00:22:14 +04:00
#include "NetDb.h"
#include "Destination.h"
namespace i2p
{
2014-10-16 20:37:39 +04:00
namespace client
{
2014-10-16 20:37:39 +04:00
ClientDestination::ClientDestination (bool isPublic, i2p::data::SigningKeyType sigType):
2014-10-13 20:33:51 +04:00
m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr),
2014-10-09 18:05:28 +04:00
m_CurrentOutboundTunnel (nullptr), m_LeaseSet (nullptr), m_IsPublic (isPublic)
{
2014-10-15 22:32:19 +04:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
if (m_IsPublic)
LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created");
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
}
2014-10-16 20:37:39 +04:00
ClientDestination::ClientDestination (const std::string& fullPath, bool isPublic):
2014-10-13 20:33:51 +04:00
m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr),
2014-10-09 18:05:28 +04:00
m_CurrentOutboundTunnel (nullptr), m_LeaseSet (nullptr), m_IsPublic (isPublic)
{
std::ifstream s(fullPath.c_str (), std::ifstream::binary);
if (s.is_open ())
{
s.seekg (0, std::ios::end);
size_t len = s.tellg();
s.seekg (0, std::ios::beg);
uint8_t * buf = new uint8_t[len];
s.read ((char *)buf, len);
m_Keys.FromBuffer (buf, len);
delete[] buf;
LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p loaded");
}
else
{
LogPrint ("Can't open file ", fullPath, " Creating new one");
2014-10-15 22:32:19 +04:00
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_DSA_SHA1);
std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
size_t len = m_Keys.GetFullLen ();
uint8_t * buf = new uint8_t[len];
len = m_Keys.ToBuffer (buf, len);
f.write ((char *)buf, len);
delete[] buf;
LogPrint ("New private keys file ", fullPath, " for ", m_Keys.GetPublic ().GetIdentHash ().ToBase32 (), ".b32.i2p created");
}
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
}
2014-10-16 20:37:39 +04:00
ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic):
2014-10-13 20:33:51 +04:00
m_IsRunning (false), m_Thread (nullptr), m_Service (nullptr), m_Work (nullptr),
2014-10-09 18:05:28 +04:00
m_Keys (keys), m_CurrentOutboundTunnel (nullptr), m_LeaseSet (nullptr), m_IsPublic (isPublic)
{
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel
if (m_IsPublic)
LogPrint ("Local address ", GetIdentHash ().ToBase32 (), ".b32.i2p created");
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
}
2014-10-16 20:37:39 +04:00
ClientDestination::~ClientDestination ()
{
2014-10-09 18:05:28 +04:00
Stop ();
2014-10-13 00:22:14 +04:00
for (auto it: m_RemoteLeaseSets)
delete it.second;
if (m_Pool)
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
delete m_LeaseSet;
2014-10-13 20:33:51 +04:00
delete m_Work;
delete m_Service;
delete m_StreamingDestination; // TODO
}
2014-10-16 20:37:39 +04:00
void ClientDestination::Run ()
2014-10-09 18:05:28 +04:00
{
2014-10-13 20:33:51 +04:00
if (m_Service)
m_Service->run ();
2014-10-09 18:05:28 +04:00
}
2014-10-16 20:37:39 +04:00
void ClientDestination::Start ()
2014-10-09 18:05:28 +04:00
{
2014-10-13 20:33:51 +04:00
m_Service = new boost::asio::io_service;
m_Work = new boost::asio::io_service::work (*m_Service);
m_Pool->SetActive (true);
2014-10-09 18:05:28 +04:00
m_IsRunning = true;
2014-10-16 20:37:39 +04:00
m_Thread = new std::thread (std::bind (&ClientDestination::Run, this));
m_StreamingDestination->Start ();
2014-10-09 18:05:28 +04:00
}
2014-10-16 20:37:39 +04:00
void ClientDestination::Stop ()
2014-10-09 18:05:28 +04:00
{
m_StreamingDestination->Stop ();
2014-10-12 02:21:52 +04:00
if (m_Pool)
i2p::tunnel::tunnels.StopTunnelPool (m_Pool);
2014-10-09 18:05:28 +04:00
m_IsRunning = false;
2014-10-13 20:35:44 +04:00
if (m_Service)
m_Service->stop ();
2014-10-09 18:05:28 +04:00
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
2014-10-13 20:33:51 +04:00
delete m_Work; m_Work = nullptr;
delete m_Service; m_Service = nullptr;
2014-10-09 18:05:28 +04:00
}
2014-10-16 20:37:39 +04:00
const i2p::data::LeaseSet * ClientDestination::FindLeaseSet (const i2p::data::IdentHash& ident)
{
2014-10-16 20:37:39 +04:00
auto it = m_RemoteLeaseSets.find (ident);
if (it != m_RemoteLeaseSets.end ())
{
2014-10-16 20:37:39 +04:00
if (it->second->HasNonExpiredLeases ())
return it->second;
else
{
2014-10-16 20:37:39 +04:00
LogPrint ("All leases of remote LeaseSet expired. Request it");
i2p::data::netdb.RequestDestination (ident, true, m_Pool);
}
}
2014-10-16 20:37:39 +04:00
else
{
2014-10-16 20:37:39 +04: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 20:37:39 +04:00
}
return nullptr;
}
2014-10-16 20:37:39 +04:00
const i2p::data::LeaseSet * ClientDestination::GetLeaseSet ()
{
if (!m_Pool) return nullptr;
if (!m_LeaseSet)
UpdateLeaseSet ();
return m_LeaseSet;
}
2014-10-16 20:37:39 +04: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 20:37:39 +04:00
void ClientDestination::SendTunnelDataMsgs (const std::vector<i2p::tunnel::TunnelMessageBlock>& msgs)
{
m_CurrentOutboundTunnel = m_Pool->GetNextOutboundTunnel (m_CurrentOutboundTunnel);
if (m_CurrentOutboundTunnel)
m_CurrentOutboundTunnel->SendTunnelDataMsg (msgs);
else
{
LogPrint ("No outbound tunnels in the pool");
for (auto it: msgs)
DeleteI2NPMessage (it.data);
}
}
void ClientDestination::ProcessGarlicMessage (I2NPMessage * msg)
{
m_Service->post (boost::bind (&ClientDestination::HandleGarlicMessage, this, msg));
}
void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg)
{
m_Service->post (boost::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg));
}
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
{
LogPrint ("Remote LeaseSet");
auto it = m_RemoteLeaseSets.find (msg->key);
if (it != m_RemoteLeaseSets.end ())
{
it->second->Update (buf + offset, len - offset);
LogPrint ("Remote LeaseSet updated");
}
else
{
LogPrint ("New remote LeaseSet added");
m_RemoteLeaseSets[msg->key] = new i2p::data::LeaseSet (buf + offset, len - offset);
}
}
else
LogPrint ("Unexpected client's DatabaseStore type ", msg->type, ". Dropped");
}
void ClientDestination::SetLeaseSetUpdated ()
{
i2p::garlic::GarlicDestination::SetLeaseSetUpdated ();
UpdateLeaseSet ();
if (m_IsPublic)
i2p::data::netdb.PublishLeaseSet (m_LeaseSet, m_Pool);
}
void ClientDestination::HandleDataMessage (const uint8_t * buf, size_t len)
{
uint32_t length = be32toh (*(uint32_t *)buf);
buf += 4;
// we assume I2CP payload
if (buf[9] == PROTOCOL_TYPE_STREAMING && m_StreamingDestination) // streaming protocol
{
// unzip it
CryptoPP::Gunzip decompressor;
decompressor.Put (buf, length);
decompressor.MessageEnd();
2014-10-16 20:37:39 +04:00
i2p::stream::Packet * uncompressed = new i2p::stream::Packet;
uncompressed->offset = 0;
uncompressed->len = decompressor.MaxRetrievable ();
2014-10-16 20:37:39 +04:00
if (uncompressed->len <= i2p::stream::MAX_PACKET_SIZE)
{
decompressor.Get (uncompressed->buf, uncompressed->len);
m_StreamingDestination->HandleNextPacket (uncompressed);
2014-10-07 22:24:31 +04:00
}
else
{
LogPrint ("Received packet size ", uncompressed->len, " exceeds max packet size. Skipped");
delete uncompressed;
}
}
else
LogPrint ("Data: unexpected protocol ", buf[9]);
}
2014-10-16 20:37:39 +04:00
I2NPMessage * ClientDestination::CreateDataMessage (const uint8_t * payload, size_t len)
2014-10-07 22:06:50 +04:00
{
I2NPMessage * msg = NewI2NPShortMessage ();
CryptoPP::Gzip compressor;
2014-10-16 20:37:39 +04:00
if (len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE)
compressor.SetDeflateLevel (CryptoPP::Gzip::MIN_DEFLATE_LEVEL);
2014-10-07 22:24:31 +04:00
else
compressor.SetDeflateLevel (CryptoPP::Gzip::DEFAULT_DEFLATE_LEVEL);
compressor.Put (payload, len);
compressor.MessageEnd();
int size = compressor.MaxRetrievable ();
2014-10-07 22:06:50 +04:00
uint8_t * buf = msg->GetPayload ();
*(uint32_t *)buf = htobe32 (size); // length
buf += 4;
compressor.Get (buf, size);
2014-10-07 22:06:50 +04:00
memset (buf + 4, 0, 4); // source and destination ports. TODO: fill with proper values later
buf[9] = PROTOCOL_TYPE_STREAMING; // streaming protocol. TODO:
2014-10-07 22:06:50 +04:00
msg->len += size + 4;
FillI2NPMessageHeader (msg, eI2NPData);
return msg;
}
i2p::stream::Stream * ClientDestination::CreateStream (const i2p::data::LeaseSet& remote)
{
if (m_StreamingDestination)
return m_StreamingDestination->CreateNewOutgoingStream (remote);
return nullptr;
}
void ClientDestination::AcceptStreams (const std::function<void (i2p::stream::Stream *)>& 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-16 20:37:39 +04:00
}
}