2014-10-05 16:54:59 +04:00
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cryptopp/dh.h>
|
|
|
|
#include "Log.h"
|
|
|
|
#include "util.h"
|
2014-10-13 00:22:14 +04:00
|
|
|
#include "NetDb.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
#include "Destination.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 20:37:39 +04:00
|
|
|
namespace client
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
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-11-26 21:20:35 +03:00
|
|
|
m_LeaseSet (nullptr), m_IsPublic (isPublic), m_DatagramDestination (nullptr)
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2014-10-15 22:32:19 +04:00
|
|
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
2014-10-05 16:54:59 +04:00
|
|
|
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");
|
2014-10-22 19:46:54 +04:00
|
|
|
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
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-11-26 21:20:35 +03:00
|
|
|
m_LeaseSet (nullptr), m_IsPublic (isPublic), m_DatagramDestination (nullptr)
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
|
|
|
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);
|
2014-10-05 16:54:59 +04:00
|
|
|
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
|
2014-10-22 19:46:54 +04:00
|
|
|
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
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-11-26 21:20:35 +03:00
|
|
|
m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (isPublic), m_DatagramDestination (nullptr)
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
|
|
|
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");
|
2014-10-22 19:46:54 +04:00
|
|
|
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:37:39 +04:00
|
|
|
ClientDestination::~ClientDestination ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
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;
|
2014-10-13 19:21:57 +04:00
|
|
|
if (m_Pool)
|
|
|
|
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
|
2014-10-05 16:54:59 +04:00
|
|
|
delete m_LeaseSet;
|
2014-10-13 20:33:51 +04:00
|
|
|
delete m_Work;
|
|
|
|
delete m_Service;
|
2014-10-23 00:42:26 +04:00
|
|
|
delete m_StreamingDestination;
|
|
|
|
delete m_DatagramDestination;
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
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));
|
2014-10-22 19:46:54 +04:00
|
|
|
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
|
|
|
{
|
2014-10-22 19:46:54 +04:00
|
|
|
m_StreamingDestination->Stop ();
|
2014-10-31 23:44:44 +03:00
|
|
|
if (m_DatagramDestination)
|
|
|
|
{
|
|
|
|
auto d = m_DatagramDestination;
|
|
|
|
m_DatagramDestination = nullptr;
|
|
|
|
delete d;
|
|
|
|
}
|
2014-10-12 02:21:52 +04:00
|
|
|
if (m_Pool)
|
2014-10-13 19:21:57 +04:00
|
|
|
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-05 16:54:59 +04:00
|
|
|
{
|
2014-10-16 20:37:39 +04:00
|
|
|
auto it = m_RemoteLeaseSets.find (ident);
|
|
|
|
if (it != m_RemoteLeaseSets.end ())
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2014-10-16 20:37:39 +04:00
|
|
|
if (it->second->HasNonExpiredLeases ())
|
|
|
|
return it->second;
|
2014-10-05 16:54:59 +04:00
|
|
|
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-05 16:54:59 +04:00
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
else
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
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-05 16:54:59 +04:00
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 20:37:39 +04:00
|
|
|
const i2p::data::LeaseSet * ClientDestination::GetLeaseSet ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
|
|
|
if (!m_Pool) return nullptr;
|
|
|
|
if (!m_LeaseSet)
|
|
|
|
UpdateLeaseSet ();
|
|
|
|
return m_LeaseSet;
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:37:39 +04:00
|
|
|
void ClientDestination::UpdateLeaseSet ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
|
|
|
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::ProcessGarlicMessage (I2NPMessage * msg)
|
|
|
|
{
|
2014-11-24 01:00:45 +03:00
|
|
|
m_Service->post (std::bind (&ClientDestination::HandleGarlicMessage, this, msg));
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg)
|
|
|
|
{
|
2014-11-24 01:00:45 +03:00
|
|
|
m_Service->post (std::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg));
|
2014-10-16 20:37:39 +04: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
|
|
|
|
{
|
|
|
|
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)
|
2014-10-07 00:06:05 +04:00
|
|
|
{
|
|
|
|
uint32_t length = be32toh (*(uint32_t *)buf);
|
|
|
|
buf += 4;
|
|
|
|
// we assume I2CP payload
|
2014-10-22 23:01:30 +04:00
|
|
|
switch (buf[9])
|
|
|
|
{
|
|
|
|
case PROTOCOL_TYPE_STREAMING:
|
|
|
|
// streaming protocol
|
|
|
|
if (m_StreamingDestination)
|
|
|
|
m_StreamingDestination->HandleDataMessagePayload (buf, length);
|
2014-10-22 23:30:25 +04: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");
|
2014-10-22 23:01:30 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint ("Data: unexpected protocol ", buf[9]);
|
|
|
|
}
|
2014-10-22 22:01:23 +04:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:33:58 +03:00
|
|
|
std::shared_ptr<i2p::stream::Stream> ClientDestination::CreateStream (const i2p::data::LeaseSet& remote, int port)
|
2014-10-22 22:01:23 +04:00
|
|
|
{
|
|
|
|
if (m_StreamingDestination)
|
2014-10-23 05:36:11 +04:00
|
|
|
return m_StreamingDestination->CreateNewOutgoingStream (remote, port);
|
2014-10-22 22:01:23 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-18 17:33:58 +03:00
|
|
|
void ClientDestination::AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
2014-10-22 22:01:23 +04:00
|
|
|
{
|
|
|
|
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-23 00:42:26 +04:00
|
|
|
|
2014-10-31 23:44:44 +03:00
|
|
|
i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination ()
|
2014-10-23 00:42:26 +04:00
|
|
|
{
|
|
|
|
if (!m_DatagramDestination)
|
|
|
|
m_DatagramDestination = new i2p::datagram::DatagramDestination (*this);
|
2014-10-31 23:44:44 +03:00
|
|
|
return m_DatagramDestination;
|
2014-10-23 00:42:26 +04:00
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|