2014-10-05 16:54:59 +04:00
|
|
|
#include <algorithm>
|
2015-01-02 15:35:38 +03:00
|
|
|
#include <cassert>
|
2016-05-11 22:12:38 +03:00
|
|
|
#include "Crypto.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
#include "Log.h"
|
2016-02-11 03:00:00 +03:00
|
|
|
#include "FS.h"
|
2014-12-26 00:47:15 +03:00
|
|
|
#include "Timestamp.h"
|
2014-10-13 00:22:14 +04:00
|
|
|
#include "NetDb.h"
|
2015-01-06 03:32:46 +03:00
|
|
|
#include "Destination.h"
|
2016-04-27 19:08:08 +03:00
|
|
|
#include "util.h"
|
2014-10-05 16:54:59 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 20:37:39 +04:00
|
|
|
namespace client
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2016-05-23 17:33:01 +03:00
|
|
|
LeaseSetDestination::LeaseSetDestination (bool isPublic, const std::map<std::string, std::string> * params):
|
2016-11-28 21:47:10 +03:00
|
|
|
m_IsRunning (false), m_Thread (nullptr), m_IsPublic (isPublic),
|
2016-05-23 21:31:22 +03:00
|
|
|
m_PublishReplyToken (0), m_PublishConfirmationTimer (m_Service),
|
2016-02-11 22:45:33 +03:00
|
|
|
m_PublishVerificationTimer (m_Service), m_CleanupTimer (m_Service)
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2016-10-26 03:00:00 +03:00
|
|
|
int inLen = DEFAULT_INBOUND_TUNNEL_LENGTH;
|
|
|
|
int inQty = DEFAULT_INBOUND_TUNNELS_QUANTITY;
|
|
|
|
int outLen = DEFAULT_OUTBOUND_TUNNEL_LENGTH;
|
|
|
|
int outQty = DEFAULT_OUTBOUND_TUNNELS_QUANTITY;
|
2016-01-24 04:52:21 +03:00
|
|
|
int numTags = DEFAULT_TAGS_TO_SEND;
|
2015-06-10 22:32:55 +03:00
|
|
|
std::shared_ptr<std::vector<i2p::data::IdentHash> > explicitPeers;
|
2016-10-26 03:00:00 +03:00
|
|
|
try {
|
|
|
|
if (params) {
|
|
|
|
auto it = params->find (I2CP_PARAM_INBOUND_TUNNEL_LENGTH);
|
|
|
|
if (it != params->end ())
|
|
|
|
inLen = std::stoi(it->second);
|
|
|
|
it = params->find (I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH);
|
|
|
|
if (it != params->end ())
|
|
|
|
outLen = std::stoi(it->second);
|
|
|
|
it = params->find (I2CP_PARAM_INBOUND_TUNNELS_QUANTITY);
|
|
|
|
if (it != params->end ())
|
|
|
|
inQty = std::stoi(it->second);
|
|
|
|
it = params->find (I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY);
|
|
|
|
if (it != params->end ())
|
|
|
|
outQty = std::stoi(it->second);
|
|
|
|
it = params->find (I2CP_PARAM_TAGS_TO_SEND);
|
|
|
|
if (it != params->end ())
|
|
|
|
numTags = std::stoi(it->second);
|
|
|
|
LogPrint (eLogInfo, "Destination: parameters for tunnel set to: ", inQty, " inbound (", inLen, " hops), ", outQty, " outbound (", outLen, " hops), ", numTags, " tags");
|
|
|
|
it = params->find (I2CP_PARAM_EXPLICIT_PEERS);
|
|
|
|
if (it != params->end ())
|
2015-06-10 22:32:55 +03:00
|
|
|
{
|
2016-10-26 03:00:00 +03:00
|
|
|
explicitPeers = std::make_shared<std::vector<i2p::data::IdentHash> >();
|
|
|
|
std::stringstream ss(it->second);
|
|
|
|
std::string b64;
|
|
|
|
while (std::getline (ss, b64, ','))
|
|
|
|
{
|
|
|
|
i2p::data::IdentHash ident;
|
|
|
|
ident.FromBase64 (b64);
|
|
|
|
explicitPeers->push_back (ident);
|
|
|
|
LogPrint (eLogInfo, "Destination: Added to explicit peers list: ", b64);
|
|
|
|
}
|
2015-06-10 22:32:55 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 03:00:00 +03:00
|
|
|
} catch (std::exception & ex) {
|
|
|
|
LogPrint(eLogError, "Destination: unable to parse parameters for destination: ", ex.what());
|
|
|
|
}
|
2016-01-24 04:52:21 +03:00
|
|
|
SetNumTags (numTags);
|
2016-10-26 03:00:00 +03:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (inLen, outLen, inQty, outQty);
|
2015-06-10 22:32:55 +03:00
|
|
|
if (explicitPeers)
|
|
|
|
m_Pool->SetExplicitPeers (explicitPeers);
|
2016-11-15 18:20:09 +03:00
|
|
|
if(params)
|
|
|
|
{
|
|
|
|
auto itr = params->find(I2CP_PARAM_MAX_TUNNEL_LATENCY);
|
|
|
|
if (itr != params->end()) {
|
|
|
|
auto maxlatency = std::stoi(itr->second);
|
|
|
|
itr = params->find(I2CP_PARAM_MIN_TUNNEL_LATENCY);
|
|
|
|
if (itr != params->end()) {
|
|
|
|
auto minlatency = std::stoi(itr->second);
|
|
|
|
if ( minlatency > 0 && maxlatency > 0 ) {
|
|
|
|
// set tunnel pool latency
|
|
|
|
LogPrint(eLogInfo, "Destination: requiring tunnel latency [", minlatency, "ms, ", maxlatency, "ms]");
|
|
|
|
m_Pool->RequireLatency(minlatency, maxlatency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
LeaseSetDestination::~LeaseSetDestination ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2014-12-19 20:07:54 +03:00
|
|
|
if (m_IsRunning)
|
|
|
|
Stop ();
|
2014-10-13 19:21:57 +04:00
|
|
|
if (m_Pool)
|
|
|
|
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
|
2016-12-02 03:23:55 +03:00
|
|
|
for (auto& it: m_LeaseSetRequests)
|
|
|
|
it.second->Complete (nullptr);
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::Run ()
|
2014-10-09 18:05:28 +04:00
|
|
|
{
|
2014-12-02 05:26:51 +03:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-12-16 06:50:11 +03:00
|
|
|
m_Service.run ();
|
2014-12-02 05:26:51 +03:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: runtime exception: ", ex.what ());
|
2014-12-02 05:26:51 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 18:05:28 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:31:22 +03:00
|
|
|
bool LeaseSetDestination::Start ()
|
2014-10-09 18:05:28 +04:00
|
|
|
{
|
2015-01-03 23:20:11 +03:00
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
2015-12-16 22:52:48 +03:00
|
|
|
m_Pool->SetLocalDestination (shared_from_this ());
|
2016-11-28 21:47:10 +03:00
|
|
|
m_Pool->SetActive (true);
|
2015-01-23 20:48:25 +03:00
|
|
|
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
|
2016-05-23 17:33:01 +03:00
|
|
|
m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer,
|
2016-11-28 21:47:10 +03:00
|
|
|
shared_from_this (), std::placeholders::_1));
|
|
|
|
m_Thread = new std::thread (std::bind (&LeaseSetDestination::Run, shared_from_this ()));
|
|
|
|
|
2016-05-23 21:31:22 +03:00
|
|
|
return true;
|
2015-01-03 23:20:11 +03:00
|
|
|
}
|
2016-05-23 21:31:22 +03:00
|
|
|
else
|
|
|
|
return false;
|
2014-10-09 18:05:28 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:31:22 +03:00
|
|
|
bool LeaseSetDestination::Stop ()
|
2014-10-09 18:05:28 +04:00
|
|
|
{
|
2015-01-03 23:20:11 +03:00
|
|
|
if (m_IsRunning)
|
2014-10-09 18:05:28 +04:00
|
|
|
{
|
2015-01-23 20:48:25 +03:00
|
|
|
m_CleanupTimer.cancel ();
|
2016-02-11 22:45:33 +03:00
|
|
|
m_PublishConfirmationTimer.cancel ();
|
2016-12-02 03:23:55 +03:00
|
|
|
m_PublishVerificationTimer.cancel ();
|
2016-11-28 22:37:17 +03:00
|
|
|
|
2015-01-03 23:20:11 +03:00
|
|
|
m_IsRunning = false;
|
|
|
|
if (m_Pool)
|
|
|
|
{
|
|
|
|
m_Pool->SetLocalDestination (nullptr);
|
|
|
|
i2p::tunnel::tunnels.StopTunnelPool (m_Pool);
|
|
|
|
}
|
|
|
|
m_Service.stop ();
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
2016-11-29 06:47:37 +03:00
|
|
|
CleanUp (); // GarlicDestination
|
2016-05-23 21:31:22 +03:00
|
|
|
return true;
|
2014-10-09 18:05:28 +04:00
|
|
|
}
|
2016-05-23 21:31:22 +03:00
|
|
|
else
|
|
|
|
return false;
|
2014-10-09 18:05:28 +04:00
|
|
|
}
|
2016-08-30 20:27:57 +03:00
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> LeaseSetDestination::FindLeaseSet (const i2p::data::IdentHash& ident)
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2016-10-12 16:39:16 +03:00
|
|
|
std::shared_ptr<i2p::data::LeaseSet> remoteLS;
|
2016-07-22 16:56:17 +03:00
|
|
|
{
|
2016-10-12 16:39:16 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
|
|
|
|
auto it = m_RemoteLeaseSets.find (ident);
|
|
|
|
if (it != m_RemoteLeaseSets.end ())
|
|
|
|
remoteLS = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remoteLS)
|
|
|
|
{
|
|
|
|
if (!remoteLS->IsExpired ())
|
2016-07-22 16:56:17 +03:00
|
|
|
{
|
2016-10-12 16:39:16 +03:00
|
|
|
if (remoteLS->ExpiresSoon())
|
2016-07-22 16:56:17 +03:00
|
|
|
{
|
|
|
|
LogPrint(eLogDebug, "Destination: Lease Set expires soon, updating before expire");
|
|
|
|
// update now before expiration for smooth handover
|
2016-10-12 16:39:16 +03:00
|
|
|
auto s = shared_from_this ();
|
|
|
|
RequestDestination(ident, [s, ident] (std::shared_ptr<i2p::data::LeaseSet> ls) {
|
2016-07-22 16:56:17 +03:00
|
|
|
if(ls && !ls->IsExpired())
|
|
|
|
{
|
|
|
|
ls->PopulateLeases();
|
2016-10-10 16:04:24 +03:00
|
|
|
{
|
2016-10-12 16:39:16 +03:00
|
|
|
std::lock_guard<std::mutex> _lock(s->m_RemoteLeaseSetsMutex);
|
|
|
|
s->m_RemoteLeaseSets[ident] = ls;
|
2016-10-10 16:04:24 +03:00
|
|
|
}
|
2016-07-22 16:56:17 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-10-12 16:39:16 +03:00
|
|
|
return remoteLS;
|
2016-07-22 16:56:17 +03:00
|
|
|
}
|
2014-10-05 16:54:59 +04:00
|
|
|
else
|
2016-10-25 21:07:34 +03:00
|
|
|
{
|
2016-02-08 03:45:06 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: remote LeaseSet expired");
|
2016-10-25 21:07:34 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
|
|
|
|
m_RemoteLeaseSets.erase (ident);
|
|
|
|
return nullptr;
|
|
|
|
}
|
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);
|
2016-02-13 04:56:29 +03:00
|
|
|
if (ls && !ls->IsExpired ())
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2016-02-08 03:45:06 +03:00
|
|
|
ls->PopulateLeases (); // since we don't store them in netdb
|
2016-10-12 16:39:16 +03:00
|
|
|
std::lock_guard<std::mutex> _lock(m_RemoteLeaseSetsMutex);
|
2016-10-03 18:01:31 +03:00
|
|
|
m_RemoteLeaseSets[ident] = ls;
|
2014-10-16 20:37:39 +04:00
|
|
|
return ls;
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
return nullptr;
|
2016-10-03 18:01:31 +03:00
|
|
|
}
|
2014-10-05 16:54:59 +04:00
|
|
|
|
2016-05-25 22:10:28 +03:00
|
|
|
std::shared_ptr<const i2p::data::LocalLeaseSet> LeaseSetDestination::GetLeaseSet ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
|
|
|
if (!m_Pool) return nullptr;
|
|
|
|
if (!m_LeaseSet)
|
|
|
|
UpdateLeaseSet ();
|
2016-10-25 21:07:34 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_LeaseSetMutex);
|
2014-10-05 16:54:59 +04:00
|
|
|
return m_LeaseSet;
|
|
|
|
}
|
|
|
|
|
2016-05-29 16:33:50 +03:00
|
|
|
void LeaseSetDestination::SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet)
|
|
|
|
{
|
2016-10-25 21:07:34 +03:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(m_LeaseSetMutex);
|
|
|
|
m_LeaseSet.reset (newLeaseSet);
|
|
|
|
}
|
2016-09-01 16:48:04 +03:00
|
|
|
i2p::garlic::GarlicDestination::SetLeaseSetUpdated ();
|
2016-05-29 16:33:50 +03:00
|
|
|
if (m_IsPublic)
|
|
|
|
{
|
|
|
|
m_PublishVerificationTimer.cancel ();
|
|
|
|
Publish ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::UpdateLeaseSet ()
|
2014-10-05 16:54:59 +04:00
|
|
|
{
|
2016-05-25 22:10:28 +03:00
|
|
|
int numTunnels = m_Pool->GetNumInboundTunnels () + 2; // 2 backup tunnels
|
|
|
|
if (numTunnels > i2p::data::MAX_NUM_LEASES) numTunnels = i2p::data::MAX_NUM_LEASES; // 16 tunnels maximum
|
2016-05-29 16:33:50 +03:00
|
|
|
CreateNewLeaseSet (m_Pool->GetInboundTunnels (numTunnels));
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
bool LeaseSetDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag)
|
2014-12-08 23:36:00 +03:00
|
|
|
{
|
2014-12-16 06:50:11 +03:00
|
|
|
struct
|
2014-12-08 23:36:00 +03:00
|
|
|
{
|
2014-12-16 06:50:11 +03:00
|
|
|
uint8_t k[32], t[32];
|
|
|
|
} data;
|
|
|
|
memcpy (data.k, key, 32);
|
|
|
|
memcpy (data.t, tag, 32);
|
2016-02-17 06:57:38 +03:00
|
|
|
auto s = shared_from_this ();
|
|
|
|
m_Service.post ([s,data](void)
|
2014-12-09 22:15:02 +03:00
|
|
|
{
|
2016-02-17 06:57:38 +03:00
|
|
|
s->AddSessionKey (data.k, data.t);
|
2014-12-16 06:50:11 +03:00
|
|
|
});
|
|
|
|
return true;
|
2014-12-08 23:36:00 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2016-05-23 17:33:01 +03:00
|
|
|
m_Service.post (std::bind (&LeaseSetDestination::HandleGarlicMessage, shared_from_this (), msg));
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2016-05-23 17:33:01 +03:00
|
|
|
m_Service.post (std::bind (&LeaseSetDestination::HandleDeliveryStatusMessage, shared_from_this (), msg));
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
|
2016-10-10 15:59:48 +03:00
|
|
|
void LeaseSetDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2015-01-02 07:00:33 +03:00
|
|
|
uint8_t typeID = buf[I2NP_HEADER_TYPEID_OFFSET];
|
|
|
|
switch (typeID)
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
|
|
|
case eI2NPData:
|
2015-01-02 07:00:33 +03:00
|
|
|
HandleDataMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-10-16 20:37:39 +04:00
|
|
|
break;
|
2015-06-30 04:40:43 +03:00
|
|
|
case eI2NPDeliveryStatus:
|
|
|
|
// we assume tunnel tests non-encrypted
|
|
|
|
HandleDeliveryStatusMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
|
|
|
|
break;
|
2014-10-16 20:37:39 +04:00
|
|
|
case eI2NPDatabaseStore:
|
2015-01-02 07:00:33 +03:00
|
|
|
HandleDatabaseStoreMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-12-26 00:47:15 +03:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseSearchReply:
|
2015-01-02 07:00:33 +03:00
|
|
|
HandleDatabaseSearchReplyMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-10-16 20:37:39 +04:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-30 04:40:43 +03:00
|
|
|
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len)
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2015-01-03 05:11:40 +03:00
|
|
|
uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
|
|
|
|
size_t offset = DATABASE_STORE_HEADER_SIZE;
|
2015-01-30 23:29:33 +03:00
|
|
|
if (replyToken)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogInfo, "Destination: Reply token is ignored for DatabaseStore");
|
2014-10-16 20:37:39 +04:00
|
|
|
offset += 36;
|
2015-01-30 23:29:33 +03:00
|
|
|
}
|
2016-11-08 02:32:22 +03:00
|
|
|
i2p::data::IdentHash key (buf + DATABASE_STORE_KEY_OFFSET);
|
2015-04-07 19:02:25 +03:00
|
|
|
std::shared_ptr<i2p::data::LeaseSet> leaseSet;
|
2015-01-03 05:11:40 +03:00
|
|
|
if (buf[DATABASE_STORE_TYPE_OFFSET] == 1) // LeaseSet
|
2014-10-16 20:37:39 +04:00
|
|
|
{
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Remote LeaseSet");
|
2016-07-22 16:56:17 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
|
2016-11-08 02:32:22 +03:00
|
|
|
auto it = m_RemoteLeaseSets.find (key);
|
2014-10-16 20:37:39 +04:00
|
|
|
if (it != m_RemoteLeaseSets.end ())
|
|
|
|
{
|
2015-04-07 19:02:25 +03:00
|
|
|
leaseSet = it->second;
|
2016-02-17 06:57:38 +03:00
|
|
|
if (leaseSet->IsNewer (buf + offset, len - offset))
|
|
|
|
{
|
|
|
|
leaseSet->Update (buf + offset, len - offset);
|
2016-11-08 02:32:22 +03:00
|
|
|
if (leaseSet->IsValid () && leaseSet->GetIdentHash () == key)
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Remote LeaseSet updated");
|
2016-02-17 06:57:38 +03:00
|
|
|
else
|
|
|
|
{
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Remote LeaseSet update failed");
|
2016-02-17 06:57:38 +03:00
|
|
|
m_RemoteLeaseSets.erase (it);
|
|
|
|
leaseSet = nullptr;
|
|
|
|
}
|
2015-04-08 17:34:16 +03:00
|
|
|
}
|
2016-02-17 06:57:38 +03:00
|
|
|
else
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Remote LeaseSet is older. Not updated");
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-07 19:02:25 +03:00
|
|
|
leaseSet = std::make_shared<i2p::data::LeaseSet> (buf + offset, len - offset);
|
2016-11-08 02:32:22 +03:00
|
|
|
if (leaseSet->IsValid () && leaseSet->GetIdentHash () == key)
|
2015-04-08 17:34:16 +03:00
|
|
|
{
|
2016-02-11 22:45:33 +03:00
|
|
|
if (leaseSet->GetIdentHash () != GetIdentHash ())
|
|
|
|
{
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: New remote LeaseSet added");
|
2016-11-08 02:32:22 +03:00
|
|
|
m_RemoteLeaseSets[key] = leaseSet;
|
2016-02-11 22:45:33 +03:00
|
|
|
}
|
|
|
|
else
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Own remote LeaseSet dropped");
|
2015-04-08 17:34:16 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-26 20:02:19 +03:00
|
|
|
LogPrint (eLogError, "Destination: New remote LeaseSet failed");
|
2015-04-08 17:34:16 +03:00
|
|
|
leaseSet = nullptr;
|
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: Unexpected client's DatabaseStore type ", buf[DATABASE_STORE_TYPE_OFFSET], ", dropped");
|
2014-12-28 23:45:58 +03:00
|
|
|
|
2016-11-08 02:32:22 +03:00
|
|
|
auto it1 = m_LeaseSetRequests.find (key);
|
2014-12-28 23:45:58 +03:00
|
|
|
if (it1 != m_LeaseSetRequests.end ())
|
|
|
|
{
|
|
|
|
it1->second->requestTimeoutTimer.cancel ();
|
2016-11-17 06:28:13 +03:00
|
|
|
if (it1->second) it1->second->Complete (leaseSet);
|
2014-12-28 23:45:58 +03:00
|
|
|
m_LeaseSetRequests.erase (it1);
|
|
|
|
}
|
2015-01-05 14:36:09 +03:00
|
|
|
}
|
2014-11-28 21:01:35 +03:00
|
|
|
|
2016-10-10 15:59:48 +03:00
|
|
|
void LeaseSetDestination::HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
i2p::data::IdentHash key (buf);
|
|
|
|
int num = buf[32]; // num
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: DatabaseSearchReply for ", key.ToBase64 (), " num=", num);
|
2014-12-26 00:47:15 +03:00
|
|
|
auto it = m_LeaseSetRequests.find (key);
|
2014-12-27 03:09:44 +03:00
|
|
|
if (it != m_LeaseSetRequests.end ())
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
2015-12-13 18:51:43 +03:00
|
|
|
auto request = it->second;
|
2014-12-26 00:47:15 +03:00
|
|
|
bool found = false;
|
2014-12-27 03:09:44 +03:00
|
|
|
if (request->excluded.size () < MAX_NUM_FLOODFILLS_PER_REQUEST)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num; i++)
|
2016-10-25 21:07:34 +03:00
|
|
|
{
|
|
|
|
i2p::data::IdentHash peerHash (buf + 33 + i*32);
|
|
|
|
if (!request->excluded.count (peerHash) && !i2p::data::netdb.FindRouter (peerHash))
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
2016-10-25 21:07:34 +03:00
|
|
|
LogPrint (eLogInfo, "Destination: Found new floodfill, request it"); // TODO: recheck this message
|
|
|
|
i2p::data::netdb.RequestDestination (peerHash);
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 01:45:20 +03:00
|
|
|
|
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (key, request->excluded);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Destination: Requesting ", key.ToBase64 (), " at ", floodfill->GetIdentHash ().ToBase64 ());
|
|
|
|
if (SendLeaseSetRequest (key, floodfill, request))
|
|
|
|
found = true;
|
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
if (!found)
|
2016-10-18 01:45:20 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Destination: ", key.ToBase64 (), " was not found on ", MAX_NUM_FLOODFILLS_PER_REQUEST, " floodfills");
|
2016-11-17 06:28:13 +03:00
|
|
|
request->Complete (nullptr);
|
2014-12-27 03:09:44 +03:00
|
|
|
m_LeaseSetRequests.erase (key);
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: Request for ", key.ToBase64 (), " not found");
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandleDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
|
2014-11-28 21:01:35 +03:00
|
|
|
{
|
2015-01-03 01:39:35 +03:00
|
|
|
uint32_t msgID = bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET);
|
2014-11-28 21:01:35 +03:00
|
|
|
if (msgID == m_PublishReplyToken)
|
|
|
|
{
|
2016-06-30 00:59:56 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Publishing LeaseSet confirmed for ", GetIdentHash().ToBase32());
|
2014-12-08 00:10:25 +03:00
|
|
|
m_ExcludedFloodfills.clear ();
|
2014-11-28 21:01:35 +03:00
|
|
|
m_PublishReplyToken = 0;
|
2016-02-11 22:45:33 +03:00
|
|
|
// schedule verification
|
|
|
|
m_PublishVerificationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_VERIFICATION_TIMEOUT));
|
2016-05-23 17:33:01 +03:00
|
|
|
m_PublishVerificationTimer.async_wait (std::bind (&LeaseSetDestination::HandlePublishVerificationTimer,
|
2016-02-11 22:45:33 +03:00
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-11-28 21:01:35 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
i2p::garlic::GarlicDestination::HandleDeliveryStatusMessage (msg);
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::SetLeaseSetUpdated ()
|
2016-09-01 16:48:04 +03:00
|
|
|
{
|
2014-10-16 20:37:39 +04:00
|
|
|
UpdateLeaseSet ();
|
2014-11-28 21:01:35 +03:00
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::Publish ()
|
2014-11-28 21:01:35 +03:00
|
|
|
{
|
|
|
|
if (!m_LeaseSet || !m_Pool)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: Can't publish non-existing LeaseSet");
|
2014-11-28 21:01:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_PublishReplyToken)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Publishing LeaseSet is pending");
|
2014-11-28 21:01:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto outbound = m_Pool->GetNextOutboundTunnel ();
|
|
|
|
if (!outbound)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: Can't publish LeaseSet. No outbound tunnels");
|
2014-11-28 21:01:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-25 19:55:58 +03:00
|
|
|
auto inbound = m_Pool->GetNextInboundTunnel ();
|
|
|
|
if (!inbound)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Destination: Can't publish LeaseSet. No inbound tunnels");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-08 00:10:25 +03:00
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (m_LeaseSet->GetIdentHash (), m_ExcludedFloodfills);
|
2014-11-28 21:01:35 +03:00
|
|
|
if (!floodfill)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: Can't publish LeaseSet, no more floodfills found");
|
2014-12-08 00:10:25 +03:00
|
|
|
m_ExcludedFloodfills.clear ();
|
2014-11-28 21:01:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-12-08 00:10:25 +03:00
|
|
|
m_ExcludedFloodfills.insert (floodfill->GetIdentHash ());
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogDebug, "Destination: Publish LeaseSet of ", GetIdentHash ().ToBase32 ());
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes ((uint8_t *)&m_PublishReplyToken, 4);
|
2016-05-25 19:55:58 +03:00
|
|
|
auto msg = WrapMessage (floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken, inbound));
|
2014-12-16 06:50:11 +03:00
|
|
|
m_PublishConfirmationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT));
|
2016-05-23 17:33:01 +03:00
|
|
|
m_PublishConfirmationTimer.async_wait (std::bind (&LeaseSetDestination::HandlePublishConfirmationTimer,
|
2016-02-11 22:45:33 +03:00
|
|
|
shared_from_this (), std::placeholders::_1));
|
2014-11-28 21:01:35 +03:00
|
|
|
outbound->SendTunnelDataMsg (floodfill->GetIdentHash (), 0, msg);
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandlePublishConfirmationTimer (const boost::system::error_code& ecode)
|
2014-11-28 21:01:35 +03:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
if (m_PublishReplyToken)
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: Publish confirmation was not received in ", PUBLISH_CONFIRMATION_TIMEOUT, " seconds, will try again");
|
2014-11-28 21:01:35 +03:00
|
|
|
m_PublishReplyToken = 0;
|
|
|
|
Publish ();
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandlePublishVerificationTimer (const boost::system::error_code& ecode)
|
2016-02-11 22:45:33 +03:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto s = shared_from_this ();
|
|
|
|
RequestLeaseSet (GetIdentHash (),
|
2016-02-22 15:57:25 +03:00
|
|
|
// "this" added due to bug in gcc 4.7-4.8
|
2016-02-20 04:00:00 +03:00
|
|
|
[s,this](std::shared_ptr<i2p::data::LeaseSet> leaseSet)
|
2016-02-11 22:45:33 +03:00
|
|
|
{
|
2017-02-12 18:08:52 +03:00
|
|
|
if (leaseSet)
|
2016-02-11 22:45:33 +03:00
|
|
|
{
|
2017-02-12 18:08:52 +03:00
|
|
|
if (s->m_LeaseSet && *s->m_LeaseSet == *leaseSet)
|
|
|
|
{
|
|
|
|
// we got latest LeasetSet
|
|
|
|
LogPrint (eLogDebug, "Destination: published LeaseSet verified for ", GetIdentHash().ToBase32());
|
|
|
|
s->m_PublishVerificationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_REGULAR_VERIFICATION_INTERNAL));
|
|
|
|
s->m_PublishVerificationTimer.async_wait (std::bind (&LeaseSetDestination::HandlePublishVerificationTimer, s, std::placeholders::_1));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogDebug, "Destination: LeaseSet is different than just published for ", GetIdentHash().ToBase32());
|
2016-02-11 22:45:33 +03:00
|
|
|
}
|
|
|
|
else
|
2016-06-30 01:05:08 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: couldn't find published LeaseSet for ", GetIdentHash().ToBase32());
|
2016-02-11 22:45:33 +03:00
|
|
|
// we have to publish again
|
2016-07-14 23:59:26 +03:00
|
|
|
s->Publish ();
|
2016-02-11 22:45:33 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
bool LeaseSetDestination::RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
2015-01-03 06:37:46 +03:00
|
|
|
if (!m_Pool || !IsReady ())
|
|
|
|
{
|
2016-08-09 05:15:09 +03:00
|
|
|
if (requestComplete)
|
|
|
|
m_Service.post ([requestComplete](void){requestComplete (nullptr);});
|
2015-01-03 06:37:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-23 17:33:01 +03:00
|
|
|
m_Service.post (std::bind (&LeaseSetDestination::RequestLeaseSet, shared_from_this (), dest, requestComplete));
|
2014-12-26 00:47:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:53:38 +03:00
|
|
|
void LeaseSetDestination::CancelDestinationRequest (const i2p::data::IdentHash& dest, bool notify)
|
2015-12-13 22:40:43 +03:00
|
|
|
{
|
|
|
|
auto s = shared_from_this ();
|
2016-08-08 18:53:38 +03:00
|
|
|
m_Service.post ([dest, notify, s](void)
|
2015-12-13 22:40:43 +03:00
|
|
|
{
|
|
|
|
auto it = s->m_LeaseSetRequests.find (dest);
|
|
|
|
if (it != s->m_LeaseSetRequests.end ())
|
2016-02-14 07:02:58 +03:00
|
|
|
{
|
2016-11-17 06:28:13 +03:00
|
|
|
auto requestComplete = it->second;
|
2016-02-14 07:02:58 +03:00
|
|
|
s->m_LeaseSetRequests.erase (it);
|
2016-11-17 06:28:13 +03:00
|
|
|
if (notify && requestComplete) requestComplete->Complete (nullptr);
|
2016-02-14 07:02:58 +03:00
|
|
|
}
|
2015-12-13 22:40:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
std::set<i2p::data::IdentHash> excluded;
|
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
2015-12-13 18:51:43 +03:00
|
|
|
auto request = std::make_shared<LeaseSetRequest> (m_Service);
|
2016-12-03 00:10:49 +03:00
|
|
|
if (requestComplete)
|
|
|
|
request->requestComplete.push_back (requestComplete);
|
2016-11-20 01:24:38 +03:00
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
2015-12-13 18:51:43 +03:00
|
|
|
auto ret = m_LeaseSetRequests.insert (std::pair<i2p::data::IdentHash, std::shared_ptr<LeaseSetRequest> >(dest,request));
|
2015-01-09 06:04:41 +03:00
|
|
|
if (ret.second) // inserted
|
2014-12-27 03:09:44 +03:00
|
|
|
{
|
2016-11-20 01:24:38 +03:00
|
|
|
request->requestTime = ts;
|
2015-01-09 06:04:41 +03:00
|
|
|
if (!SendLeaseSetRequest (dest, floodfill, request))
|
|
|
|
{
|
|
|
|
// request failed
|
2016-11-20 01:24:38 +03:00
|
|
|
m_LeaseSetRequests.erase (ret.first);
|
2016-12-03 00:10:49 +03:00
|
|
|
if (requestComplete) requestComplete (nullptr);
|
2015-01-09 06:04:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // duplicate
|
|
|
|
{
|
2016-11-17 06:28:13 +03:00
|
|
|
LogPrint (eLogInfo, "Destination: Request of LeaseSet ", dest.ToBase64 (), " is pending already");
|
2016-11-20 01:24:38 +03:00
|
|
|
if (ts > ret.first->second->requestTime + MAX_LEASESET_REQUEST_TIMEOUT)
|
2016-12-11 22:17:09 +03:00
|
|
|
{
|
|
|
|
// something went wrong
|
2016-11-20 01:24:38 +03:00
|
|
|
m_LeaseSetRequests.erase (ret.first);
|
2016-12-11 22:17:09 +03:00
|
|
|
if (requestComplete) requestComplete (nullptr);
|
|
|
|
}
|
|
|
|
else if (requestComplete)
|
|
|
|
ret.first->second->requestComplete.push_back (requestComplete);
|
2015-01-09 06:04:41 +03:00
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
else
|
2016-02-14 07:02:58 +03:00
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogError, "Destination: Can't request LeaseSet, no floodfills found");
|
2016-12-03 00:10:49 +03:00
|
|
|
if (requestComplete) requestComplete (nullptr);
|
2016-02-14 07:02:58 +03:00
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
bool LeaseSetDestination::SendLeaseSetRequest (const i2p::data::IdentHash& dest,
|
2015-12-13 18:51:43 +03:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, std::shared_ptr<LeaseSetRequest> request)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
2016-02-27 00:17:29 +03:00
|
|
|
if (!request->replyTunnel || !request->replyTunnel->IsEstablished ())
|
|
|
|
request->replyTunnel = m_Pool->GetNextInboundTunnel ();
|
|
|
|
if (!request->replyTunnel) LogPrint (eLogError, "Destination: Can't send LeaseSet request, no inbound tunnels found");
|
|
|
|
if (!request->outboundTunnel || !request->outboundTunnel->IsEstablished ())
|
|
|
|
request->outboundTunnel = m_Pool->GetNextOutboundTunnel ();
|
|
|
|
if (!request->outboundTunnel) LogPrint (eLogError, "Destination: Can't send LeaseSet request, no outbound tunnels found");
|
2014-12-26 00:47:15 +03:00
|
|
|
|
2016-02-27 00:17:29 +03:00
|
|
|
if (request->replyTunnel && request->outboundTunnel)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
request->excluded.insert (nextFloodfill->GetIdentHash ());
|
|
|
|
request->requestTimeoutTimer.cancel ();
|
|
|
|
|
2014-12-30 20:25:08 +03:00
|
|
|
uint8_t replyKey[32], replyTag[32];
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (replyKey, 32); // random session key
|
|
|
|
RAND_bytes (replyTag, 32); // random session tag
|
2014-12-30 20:25:08 +03:00
|
|
|
AddSessionKey (replyKey, replyTag);
|
|
|
|
|
2015-06-22 05:29:50 +03:00
|
|
|
auto msg = WrapMessage (nextFloodfill,
|
2015-11-03 17:15:49 +03:00
|
|
|
CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
|
2016-02-27 00:17:29 +03:00
|
|
|
request->replyTunnel, replyKey, replyTag));
|
|
|
|
request->outboundTunnel->SendTunnelDataMsg (
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
2015-06-22 05:29:50 +03:00
|
|
|
nextFloodfill->GetIdentHash (), 0, msg
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
request->requestTimeoutTimer.expires_from_now (boost::posix_time::seconds(LEASESET_REQUEST_TIMEOUT));
|
2016-05-23 17:33:01 +03:00
|
|
|
request->requestTimeoutTimer.async_wait (std::bind (&LeaseSetDestination::HandleRequestTimoutTimer,
|
2016-02-11 22:45:33 +03:00
|
|
|
shared_from_this (), std::placeholders::_1, dest));
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
else
|
2014-12-27 03:09:44 +03:00
|
|
|
return false;
|
|
|
|
return true;
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest)
|
2014-12-26 00:47:15 +03:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
auto it = m_LeaseSetRequests.find (dest);
|
|
|
|
if (it != m_LeaseSetRequests.end ())
|
|
|
|
{
|
|
|
|
bool done = false;
|
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
if (ts < it->second->requestTime + MAX_LEASESET_REQUEST_TIMEOUT)
|
|
|
|
{
|
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, it->second->excluded);
|
|
|
|
if (floodfill)
|
2016-02-27 00:17:29 +03:00
|
|
|
{
|
|
|
|
// reset tunnels, because one them might fail
|
|
|
|
it->second->outboundTunnel = nullptr;
|
|
|
|
it->second->replyTunnel = nullptr;
|
|
|
|
done = !SendLeaseSetRequest (dest, floodfill, it->second);
|
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
else
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: ", dest.ToBase64 (), " was not found within ", MAX_LEASESET_REQUEST_TIMEOUT, " seconds");
|
2014-12-26 00:47:15 +03:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
{
|
2016-11-17 06:28:13 +03:00
|
|
|
auto requestComplete = it->second;
|
2014-12-26 00:47:15 +03:00
|
|
|
m_LeaseSetRequests.erase (it);
|
2016-11-17 06:28:13 +03:00
|
|
|
if (requestComplete) requestComplete->Complete (nullptr);
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 20:48:25 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::HandleCleanupTimer (const boost::system::error_code& ecode)
|
2015-01-23 20:48:25 +03:00
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
2016-02-08 01:45:11 +03:00
|
|
|
CleanupExpiredTags ();
|
2015-01-28 22:20:28 +03:00
|
|
|
CleanupRemoteLeaseSets ();
|
2016-09-08 17:16:42 +03:00
|
|
|
CleanupDestination ();
|
2015-01-23 20:48:25 +03:00
|
|
|
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
|
2016-05-23 17:33:01 +03:00
|
|
|
m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer,
|
2015-11-03 17:15:49 +03:00
|
|
|
shared_from_this (), std::placeholders::_1));
|
2015-01-23 20:48:25 +03:00
|
|
|
}
|
2014-12-26 00:47:15 +03:00
|
|
|
}
|
2015-01-28 22:20:28 +03:00
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
void LeaseSetDestination::CleanupRemoteLeaseSets ()
|
2015-01-28 22:20:28 +03:00
|
|
|
{
|
2016-02-08 03:45:06 +03:00
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2016-07-22 16:56:17 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RemoteLeaseSetsMutex);
|
2015-01-28 22:20:28 +03:00
|
|
|
for (auto it = m_RemoteLeaseSets.begin (); it != m_RemoteLeaseSets.end ();)
|
|
|
|
{
|
2016-02-10 06:42:01 +03:00
|
|
|
if (it->second->IsEmpty () || ts > it->second->GetExpirationTime ()) // leaseset expired
|
2015-01-28 22:20:28 +03:00
|
|
|
{
|
2015-12-18 16:11:56 +03:00
|
|
|
LogPrint (eLogWarning, "Destination: Remote LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
|
2015-01-28 22:20:28 +03:00
|
|
|
it = m_RemoteLeaseSets.erase (it);
|
|
|
|
}
|
|
|
|
else
|
2016-08-05 21:23:54 +03:00
|
|
|
++it;
|
2015-01-28 22:20:28 +03:00
|
|
|
}
|
2016-05-26 22:53:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-23 17:33:01 +03:00
|
|
|
ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic, const std::map<std::string, std::string> * params):
|
|
|
|
LeaseSetDestination (isPublic, params),
|
2016-08-30 20:27:57 +03:00
|
|
|
m_Keys (keys), m_DatagramDestination (nullptr),
|
|
|
|
m_ReadyChecker(GetService())
|
2016-05-23 17:33:01 +03:00
|
|
|
{
|
2016-05-29 16:33:50 +03:00
|
|
|
if (isPublic)
|
|
|
|
PersistTemporaryKeys ();
|
|
|
|
else
|
|
|
|
i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2016-05-23 17:33:01 +03:00
|
|
|
if (isPublic)
|
|
|
|
LogPrint (eLogInfo, "Destination: Local address ", GetIdentHash().ToBase32 (), " created");
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:31:22 +03:00
|
|
|
ClientDestination::~ClientDestination ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientDestination::Start ()
|
|
|
|
{
|
|
|
|
if (LeaseSetDestination::Start ())
|
|
|
|
{
|
2016-05-25 23:18:02 +03:00
|
|
|
m_StreamingDestination = std::make_shared<i2p::stream::StreamingDestination> (GetSharedFromThis ()); // TODO:
|
2016-05-23 21:31:22 +03:00
|
|
|
m_StreamingDestination->Start ();
|
2016-08-05 21:23:54 +03:00
|
|
|
for (auto& it: m_StreamingDestinationsByPorts)
|
2016-05-23 21:31:22 +03:00
|
|
|
it.second->Start ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClientDestination::Stop ()
|
|
|
|
{
|
|
|
|
if (LeaseSetDestination::Stop ())
|
|
|
|
{
|
2016-08-30 20:27:57 +03:00
|
|
|
m_ReadyChecker.cancel();
|
2016-05-23 21:31:22 +03:00
|
|
|
m_StreamingDestination->Stop ();
|
2016-12-02 03:23:55 +03:00
|
|
|
//m_StreamingDestination->SetOwner (nullptr);
|
2016-05-23 21:31:22 +03:00
|
|
|
m_StreamingDestination = nullptr;
|
2016-08-05 21:23:54 +03:00
|
|
|
for (auto& it: m_StreamingDestinationsByPorts)
|
2016-11-26 06:36:35 +03:00
|
|
|
{
|
2016-05-23 21:31:22 +03:00
|
|
|
it.second->Stop ();
|
2016-12-02 03:23:55 +03:00
|
|
|
//it.second->SetOwner (nullptr);
|
2016-11-26 06:36:35 +03:00
|
|
|
}
|
|
|
|
m_StreamingDestinationsByPorts.clear ();
|
|
|
|
if (m_DatagramDestination)
|
|
|
|
{
|
|
|
|
delete m_DatagramDestination;
|
|
|
|
m_DatagramDestination = nullptr;
|
|
|
|
}
|
|
|
|
return true;
|
2016-05-23 21:31:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 17:23:02 +03:00
|
|
|
#ifdef I2LUA
|
2016-08-30 20:27:57 +03:00
|
|
|
void ClientDestination::Ready(ReadyPromise & p)
|
|
|
|
{
|
|
|
|
ScheduleCheckForReady(&p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::ScheduleCheckForReady(ReadyPromise * p)
|
|
|
|
{
|
|
|
|
// tick every 100ms
|
|
|
|
m_ReadyChecker.expires_from_now(boost::posix_time::milliseconds(100));
|
|
|
|
m_ReadyChecker.async_wait([&, p] (const boost::system::error_code & ecode) {
|
|
|
|
HandleCheckForReady(ecode, p);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::HandleCheckForReady(const boost::system::error_code & ecode, ReadyPromise * p)
|
|
|
|
{
|
|
|
|
if(ecode) // error happened
|
|
|
|
p->set_value(nullptr);
|
|
|
|
else if(IsReady()) // we are ready
|
|
|
|
p->set_value(std::shared_ptr<ClientDestination>(this));
|
|
|
|
else // we are not ready
|
|
|
|
ScheduleCheckForReady(p);
|
|
|
|
}
|
2016-10-19 17:23:02 +03:00
|
|
|
#endif
|
2016-08-30 20:27:57 +03:00
|
|
|
|
2016-10-10 15:59:48 +03:00
|
|
|
void ClientDestination::HandleDataMessage (const uint8_t * buf, size_t len)
|
2016-05-23 21:31:22 +03:00
|
|
|
{
|
|
|
|
uint32_t length = bufbe32toh (buf);
|
|
|
|
buf += 4;
|
|
|
|
// we assume I2CP payload
|
|
|
|
uint16_t fromPort = bufbe16toh (buf + 4), // source
|
|
|
|
toPort = bufbe16toh (buf + 6); // destination
|
|
|
|
switch (buf[9])
|
|
|
|
{
|
|
|
|
case PROTOCOL_TYPE_STREAMING:
|
|
|
|
{
|
|
|
|
// streaming protocol
|
|
|
|
auto dest = GetStreamingDestination (toPort);
|
|
|
|
if (dest)
|
|
|
|
dest->HandleDataMessagePayload (buf, length);
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Destination: Missing streaming destination");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PROTOCOL_TYPE_DATAGRAM:
|
|
|
|
// datagram protocol
|
|
|
|
if (m_DatagramDestination)
|
|
|
|
m_DatagramDestination->HandleDataMessagePayload (fromPort, toPort, buf, length);
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "Destination: Missing datagram destination");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint (eLogError, "Destination: Data: unexpected protocol ", buf[9]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port)
|
|
|
|
{
|
|
|
|
if (!streamRequestComplete)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Destination: request callback is not specified in CreateStream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto leaseSet = FindLeaseSet (dest);
|
|
|
|
if (leaseSet)
|
|
|
|
streamRequestComplete(CreateStream (leaseSet, port));
|
|
|
|
else
|
|
|
|
{
|
2016-05-25 23:18:02 +03:00
|
|
|
auto s = GetSharedFromThis ();
|
2016-05-23 21:31:22 +03:00
|
|
|
RequestDestination (dest,
|
|
|
|
[s, streamRequestComplete, port](std::shared_ptr<i2p::data::LeaseSet> ls)
|
|
|
|
{
|
|
|
|
if (ls)
|
|
|
|
streamRequestComplete(s->CreateStream (ls, port));
|
|
|
|
else
|
|
|
|
streamRequestComplete (nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<i2p::stream::Stream> ClientDestination::CreateStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port)
|
|
|
|
{
|
|
|
|
if (m_StreamingDestination)
|
|
|
|
return m_StreamingDestination->CreateNewOutgoingStream (remote, port);
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<i2p::stream::StreamingDestination> ClientDestination::GetStreamingDestination (int port) const
|
|
|
|
{
|
|
|
|
if (port)
|
|
|
|
{
|
|
|
|
auto it = m_StreamingDestinationsByPorts.find (port);
|
|
|
|
if (it != m_StreamingDestinationsByPorts.end ())
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
// if port is zero or not found, use default destination
|
|
|
|
return m_StreamingDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:53:35 +03:00
|
|
|
void ClientDestination::AcceptOnce (const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
|
|
|
{
|
|
|
|
if (m_StreamingDestination)
|
|
|
|
m_StreamingDestination->AcceptOnce (acceptor);
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:31:22 +03:00
|
|
|
std::shared_ptr<i2p::stream::StreamingDestination> ClientDestination::CreateStreamingDestination (int port, bool gzip)
|
|
|
|
{
|
2016-05-25 23:18:02 +03:00
|
|
|
auto dest = std::make_shared<i2p::stream::StreamingDestination> (GetSharedFromThis (), port, gzip);
|
2016-05-23 21:31:22 +03:00
|
|
|
if (port)
|
|
|
|
m_StreamingDestinationsByPorts[port] = dest;
|
|
|
|
else // update default
|
|
|
|
m_StreamingDestination = dest;
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2016-08-22 05:29:55 +03:00
|
|
|
i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination ()
|
2016-05-23 21:31:22 +03:00
|
|
|
{
|
2016-08-22 04:17:09 +03:00
|
|
|
if (m_DatagramDestination == nullptr)
|
2016-08-22 05:29:55 +03:00
|
|
|
m_DatagramDestination = new i2p::datagram::DatagramDestination (GetSharedFromThis ());
|
2016-05-23 21:31:22 +03:00
|
|
|
return m_DatagramDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<const i2p::stream::Stream> > ClientDestination::GetAllStreams () const
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<const i2p::stream::Stream> > ret;
|
|
|
|
if (m_StreamingDestination)
|
|
|
|
{
|
|
|
|
for (auto& it: m_StreamingDestination->GetStreams ())
|
|
|
|
ret.push_back (it.second);
|
|
|
|
}
|
|
|
|
for (auto& it: m_StreamingDestinationsByPorts)
|
|
|
|
for (auto& it1: it.second->GetStreams ())
|
|
|
|
ret.push_back (it1.second);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-29 16:33:50 +03:00
|
|
|
|
|
|
|
void ClientDestination::PersistTemporaryKeys ()
|
|
|
|
{
|
|
|
|
std::string ident = GetIdentHash().ToBase32();
|
|
|
|
std::string path = i2p::fs::DataDirPath("destinations", (ident + ".dat"));
|
|
|
|
std::ifstream f(path, std::ifstream::binary);
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
f.read ((char *)m_EncryptionPublicKey, 256);
|
|
|
|
f.read ((char *)m_EncryptionPrivateKey, 256);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogPrint (eLogInfo, "Destination: Creating new temporary keys for address ", ident, ".b32.i2p");
|
|
|
|
i2p::crypto::GenerateElGamalKeyPair(m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
|
|
|
|
|
|
|
std::ofstream f1 (path, std::ofstream::binary | std::ofstream::out);
|
|
|
|
if (f1) {
|
|
|
|
f1.write ((char *)m_EncryptionPublicKey, 256);
|
|
|
|
f1.write ((char *)m_EncryptionPrivateKey, 256);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LogPrint(eLogError, "Destinations: Can't save keys to ", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::CreateNewLeaseSet (std::vector<std::shared_ptr<i2p::tunnel::InboundTunnel> > tunnels)
|
|
|
|
{
|
|
|
|
auto leaseSet = new i2p::data::LocalLeaseSet (GetIdentity (), m_EncryptionPublicKey, tunnels);
|
|
|
|
// sign
|
|
|
|
Sign (leaseSet->GetBuffer (), leaseSet->GetBufferLen () - leaseSet->GetSignatureLen (), leaseSet->GetSignature ()); // TODO
|
|
|
|
SetLeaseSet (leaseSet);
|
|
|
|
}
|
2016-09-08 17:16:42 +03:00
|
|
|
|
|
|
|
void ClientDestination::CleanupDestination ()
|
|
|
|
{
|
|
|
|
if (m_DatagramDestination) m_DatagramDestination->CleanUp ();
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:37:39 +04:00
|
|
|
}
|
2014-10-05 16:54:59 +04:00
|
|
|
}
|