2014-10-05 12:54:59 +00:00
|
|
|
#include <algorithm>
|
2015-01-02 12:35:38 +00:00
|
|
|
#include <cassert>
|
2014-11-30 15:51:22 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2014-10-05 12:54:59 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "util.h"
|
2015-01-06 00:32:46 +00:00
|
|
|
#include "ElGamal.h"
|
2014-12-25 21:47:15 +00:00
|
|
|
#include "Timestamp.h"
|
2014-10-12 20:22:14 +00:00
|
|
|
#include "NetDb.h"
|
2015-01-07 20:50:12 +00:00
|
|
|
#include "AddressBook.h"
|
2015-01-06 00:32:46 +00:00
|
|
|
#include "Destination.h"
|
2014-10-05 12:54:59 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 16:37:39 +00:00
|
|
|
namespace client
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2014-11-30 15:51:22 +00:00
|
|
|
ClientDestination::ClientDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
|
|
|
const std::map<std::string, std::string> * params):
|
2014-12-16 03:50:11 +00:00
|
|
|
m_IsRunning (false), m_Thread (nullptr), m_Work (m_Service),
|
2014-11-28 18:01:35 +00:00
|
|
|
m_Keys (keys), m_LeaseSet (nullptr), m_IsPublic (isPublic), m_PublishReplyToken (0),
|
2015-01-23 17:48:25 +00:00
|
|
|
m_DatagramDestination (nullptr), m_PublishConfirmationTimer (m_Service), m_CleanupTimer (m_Service)
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2015-01-06 00:32:46 +00:00
|
|
|
i2p::crypto::GenerateElGamalKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey);
|
2014-11-30 15:51:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 02:24:01 +00:00
|
|
|
m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (this, inboundTunnelLen, outboundTunnelLen);
|
2014-10-05 12:54:59 +00:00
|
|
|
if (m_IsPublic)
|
2015-01-07 20:50:12 +00:00
|
|
|
LogPrint (eLogInfo, "Local address ", i2p::client::GetB32Address(GetIdentHash()), " created");
|
2014-10-22 15:46:54 +00:00
|
|
|
m_StreamingDestination = new i2p::stream::StreamingDestination (*this); // TODO:
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:37:39 +00:00
|
|
|
ClientDestination::~ClientDestination ()
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2014-12-19 17:07:54 +00:00
|
|
|
if (m_IsRunning)
|
|
|
|
Stop ();
|
2014-12-25 21:47:15 +00:00
|
|
|
for (auto it: m_LeaseSetRequests)
|
|
|
|
delete it.second;
|
2014-10-13 15:21:57 +00:00
|
|
|
if (m_Pool)
|
|
|
|
i2p::tunnel::tunnels.DeleteTunnelPool (m_Pool);
|
2014-12-19 17:07:54 +00:00
|
|
|
if (m_StreamingDestination)
|
|
|
|
delete m_StreamingDestination;
|
|
|
|
if (m_DatagramDestination)
|
|
|
|
delete m_DatagramDestination;
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:37:39 +00:00
|
|
|
void ClientDestination::Run ()
|
2014-10-09 14:05:28 +00:00
|
|
|
{
|
2014-12-02 02:26:51 +00:00
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
m_Service.run ();
|
2014-12-02 02:26:51 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint ("Destination: ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2015-01-03 20:20:11 +00:00
|
|
|
if (!m_IsRunning)
|
|
|
|
{
|
|
|
|
m_IsRunning = true;
|
|
|
|
m_Pool->SetLocalDestination (this);
|
|
|
|
m_Pool->SetActive (true);
|
|
|
|
m_Thread = new std::thread (std::bind (&ClientDestination::Run, this));
|
|
|
|
m_StreamingDestination->Start ();
|
2015-01-23 17:48:25 +00:00
|
|
|
|
|
|
|
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
|
|
|
|
m_CleanupTimer.async_wait (std::bind (&ClientDestination::HandleCleanupTimer,
|
|
|
|
this, std::placeholders::_1));
|
2015-01-03 20:20:11 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-01-03 20:20:11 +00:00
|
|
|
if (m_IsRunning)
|
2014-10-09 14:05:28 +00:00
|
|
|
{
|
2015-01-23 17:48:25 +00:00
|
|
|
m_CleanupTimer.cancel ();
|
2015-01-03 20:20:11 +00:00
|
|
|
m_IsRunning = false;
|
|
|
|
m_StreamingDestination->Stop ();
|
|
|
|
if (m_DatagramDestination)
|
|
|
|
{
|
|
|
|
auto d = m_DatagramDestination;
|
|
|
|
m_DatagramDestination = nullptr;
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2014-10-09 14:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<const i2p::data::LeaseSet> ClientDestination::FindLeaseSet (const i2p::data::IdentHash& ident)
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2014-10-16 16:37:39 +00:00
|
|
|
auto it = m_RemoteLeaseSets.find (ident);
|
|
|
|
if (it != m_RemoteLeaseSets.end ())
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2014-10-16 16:37:39 +00:00
|
|
|
if (it->second->HasNonExpiredLeases ())
|
|
|
|
return it->second;
|
2014-10-05 12:54:59 +00:00
|
|
|
else
|
2015-01-19 17:31:14 +00:00
|
|
|
LogPrint ("All leases of remote LeaseSet expired");
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|
2014-10-16 16:37:39 +00:00
|
|
|
else
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
2014-10-16 16:37:39 +00:00
|
|
|
auto ls = i2p::data::netdb.FindLeaseSet (ident);
|
|
|
|
if (ls)
|
|
|
|
{
|
|
|
|
m_RemoteLeaseSets[ident] = ls;
|
|
|
|
return ls;
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|
2014-10-16 16:37:39 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:37:39 +00:00
|
|
|
const i2p::data::LeaseSet * ClientDestination::GetLeaseSet ()
|
2014-10-05 12:54:59 +00:00
|
|
|
{
|
|
|
|
if (!m_Pool) return nullptr;
|
|
|
|
if (!m_LeaseSet)
|
|
|
|
UpdateLeaseSet ();
|
|
|
|
return m_LeaseSet;
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:37:39 +00:00
|
|
|
void ClientDestination::UpdateLeaseSet ()
|
2014-10-05 12:54:59 +00: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 16:37:39 +00:00
|
|
|
|
2014-12-08 20:36:00 +00:00
|
|
|
bool ClientDestination::SubmitSessionKey (const uint8_t * key, const uint8_t * tag)
|
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
struct
|
2014-12-08 20:36:00 +00:00
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
uint8_t k[32], t[32];
|
|
|
|
} data;
|
|
|
|
memcpy (data.k, key, 32);
|
|
|
|
memcpy (data.t, tag, 32);
|
|
|
|
m_Service.post ([this,data](void)
|
2014-12-09 19:15:02 +00:00
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
this->AddSessionKey (data.k, data.t);
|
|
|
|
});
|
|
|
|
return true;
|
2014-12-08 20:36:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 16:37:39 +00:00
|
|
|
void ClientDestination::ProcessGarlicMessage (I2NPMessage * msg)
|
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
m_Service.post (std::bind (&ClientDestination::HandleGarlicMessage, this, msg));
|
2014-10-16 16:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::ProcessDeliveryStatusMessage (I2NPMessage * msg)
|
|
|
|
{
|
2014-12-16 03:50:11 +00:00
|
|
|
m_Service.post (std::bind (&ClientDestination::HandleDeliveryStatusMessage, this, msg));
|
2014-10-16 16:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::HandleI2NPMessage (const uint8_t * buf, size_t len, i2p::tunnel::InboundTunnel * from)
|
|
|
|
{
|
2015-01-02 04:00:33 +00:00
|
|
|
uint8_t typeID = buf[I2NP_HEADER_TYPEID_OFFSET];
|
|
|
|
switch (typeID)
|
2014-10-16 16:37:39 +00:00
|
|
|
{
|
|
|
|
case eI2NPData:
|
2015-01-02 04:00:33 +00:00
|
|
|
HandleDataMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-10-16 16:37:39 +00:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseStore:
|
2015-01-02 04:00:33 +00:00
|
|
|
HandleDatabaseStoreMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-12-25 21:47:15 +00:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseSearchReply:
|
2015-01-02 04:00:33 +00:00
|
|
|
HandleDatabaseSearchReplyMessage (buf + I2NP_HEADER_SIZE, bufbe16toh (buf + I2NP_HEADER_SIZE_OFFSET));
|
2014-10-16 16:37:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf), from));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::HandleDatabaseStoreMessage (const uint8_t * buf, size_t len)
|
|
|
|
{
|
2015-01-03 02:11:40 +00:00
|
|
|
uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
|
|
|
|
size_t offset = DATABASE_STORE_HEADER_SIZE;
|
2015-01-30 20:29:33 +00:00
|
|
|
if (replyToken)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Reply token is ignored for DatabaseStore");
|
2014-10-16 16:37:39 +00:00
|
|
|
offset += 36;
|
2015-01-30 20:29:33 +00:00
|
|
|
}
|
2015-01-03 02:11:40 +00:00
|
|
|
if (buf[DATABASE_STORE_TYPE_OFFSET] == 1) // LeaseSet
|
2014-10-16 16:37:39 +00:00
|
|
|
{
|
2014-11-28 18:01:35 +00:00
|
|
|
LogPrint (eLogDebug, "Remote LeaseSet");
|
2015-01-03 02:11:40 +00:00
|
|
|
auto it = m_RemoteLeaseSets.find (buf + DATABASE_STORE_KEY_OFFSET);
|
2014-10-16 16:37:39 +00:00
|
|
|
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");
|
2015-01-27 16:27:58 +00:00
|
|
|
m_RemoteLeaseSets[buf + DATABASE_STORE_KEY_OFFSET] = std::make_shared<i2p::data::LeaseSet> (buf + offset, len - offset);
|
2014-10-16 16:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-01-03 02:11:40 +00:00
|
|
|
LogPrint (eLogError, "Unexpected client's DatabaseStore type ", buf[DATABASE_STORE_TYPE_OFFSET], ". Dropped");
|
2014-12-28 20:45:58 +00:00
|
|
|
|
2015-01-03 02:11:40 +00:00
|
|
|
auto it1 = m_LeaseSetRequests.find (buf + DATABASE_STORE_KEY_OFFSET);
|
2014-12-28 20:45:58 +00:00
|
|
|
if (it1 != m_LeaseSetRequests.end ())
|
|
|
|
{
|
|
|
|
it1->second->requestTimeoutTimer.cancel ();
|
|
|
|
if (it1->second->requestComplete) it1->second->requestComplete (true);
|
|
|
|
delete it1->second;
|
|
|
|
m_LeaseSetRequests.erase (it1);
|
|
|
|
}
|
2015-01-05 11:36:09 +00:00
|
|
|
}
|
2014-11-28 18:01:35 +00:00
|
|
|
|
2014-12-25 21:47:15 +00:00
|
|
|
void ClientDestination::HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
i2p::data::IdentHash key (buf);
|
|
|
|
int num = buf[32]; // num
|
|
|
|
LogPrint ("DatabaseSearchReply for ", key.ToBase64 (), " num=", num);
|
|
|
|
auto it = m_LeaseSetRequests.find (key);
|
2014-12-27 00:09:44 +00:00
|
|
|
if (it != m_LeaseSetRequests.end ())
|
2014-12-25 21:47:15 +00:00
|
|
|
{
|
2014-12-27 00:09:44 +00:00
|
|
|
LeaseSetRequest * request = it->second;
|
2014-12-25 21:47:15 +00:00
|
|
|
bool found = false;
|
2014-12-27 00:09:44 +00:00
|
|
|
if (request->excluded.size () < MAX_NUM_FLOODFILLS_PER_REQUEST)
|
2014-12-25 21:47:15 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
i2p::data::IdentHash peerHash (buf + 33 + i*32);
|
|
|
|
auto floodfill = i2p::data::netdb.FindRouter (peerHash);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
2014-12-27 00:09:44 +00:00
|
|
|
LogPrint (eLogInfo, "Requesting ", key.ToBase64 (), " at ", peerHash.ToBase64 ());
|
|
|
|
if (SendLeaseSetRequest (key, floodfill, request))
|
|
|
|
found = true;
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "Found new floodfill. Request it");
|
|
|
|
i2p::data::netdb.RequestDestination (peerHash);
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 00:09:44 +00:00
|
|
|
if (!found)
|
|
|
|
LogPrint (eLogError, "Suggested floodfills are not presented in netDb");
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogInfo, key.ToBase64 (), " was not found on ", MAX_NUM_FLOODFILLS_PER_REQUEST," floodfills");
|
|
|
|
if (!found)
|
|
|
|
{
|
2014-12-27 00:09:44 +00:00
|
|
|
if (request->requestComplete) request->requestComplete (false);
|
|
|
|
delete request;
|
|
|
|
m_LeaseSetRequests.erase (key);
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Request for ", key.ToBase64 (), " not found");
|
|
|
|
}
|
|
|
|
|
2014-11-28 18:01:35 +00:00
|
|
|
void ClientDestination::HandleDeliveryStatusMessage (I2NPMessage * msg)
|
|
|
|
{
|
2015-01-02 22:39:35 +00:00
|
|
|
uint32_t msgID = bufbe32toh (msg->GetPayload () + DELIVERY_STATUS_MSGID_OFFSET);
|
2014-11-28 18:01:35 +00:00
|
|
|
if (msgID == m_PublishReplyToken)
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "Publishing confirmed");
|
2014-12-07 21:10:25 +00:00
|
|
|
m_ExcludedFloodfills.clear ();
|
2014-11-28 18:01:35 +00:00
|
|
|
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 ();
|
|
|
|
}
|
2014-12-25 21:47:15 +00:00
|
|
|
|
2014-11-28 18:01:35 +00:00
|
|
|
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;
|
2014-12-07 21:10:25 +00:00
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (m_LeaseSet->GetIdentHash (), m_ExcludedFloodfills);
|
2014-11-28 18:01:35 +00:00
|
|
|
if (!floodfill)
|
|
|
|
{
|
2014-12-07 21:10:25 +00:00
|
|
|
LogPrint ("Can't publish LeaseSet. No more floodfills found");
|
|
|
|
m_ExcludedFloodfills.clear ();
|
2014-11-28 18:01:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-07 21:10:25 +00:00
|
|
|
m_ExcludedFloodfills.insert (floodfill->GetIdentHash ());
|
2014-11-28 18:01:35 +00:00
|
|
|
LogPrint (eLogDebug, "Publish LeaseSet of ", GetIdentHash ().ToBase32 ());
|
|
|
|
m_PublishReplyToken = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
2015-01-29 02:37:08 +00:00
|
|
|
auto msg = WrapMessage (floodfill, i2p::CreateDatabaseStoreMsg (m_LeaseSet, m_PublishReplyToken));
|
2014-12-16 03:50:11 +00:00
|
|
|
m_PublishConfirmationTimer.expires_from_now (boost::posix_time::seconds(PUBLISH_CONFIRMATION_TIMEOUT));
|
|
|
|
m_PublishConfirmationTimer.async_wait (std::bind (&ClientDestination::HandlePublishConfirmationTimer,
|
|
|
|
this, std::placeholders::_1));
|
2014-11-28 18:01:35 +00:00
|
|
|
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)
|
2014-10-06 20:06:05 +00:00
|
|
|
{
|
2014-12-29 22:04:02 +00:00
|
|
|
uint32_t length = bufbe32toh (buf);
|
2014-10-06 20:06:05 +00:00
|
|
|
buf += 4;
|
|
|
|
// we assume I2CP payload
|
2014-10-22 19:01:30 +00:00
|
|
|
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");
|
2014-10-22 19:01:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogPrint ("Data: unexpected protocol ", buf[9]);
|
|
|
|
}
|
2014-10-22 18:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 12:35:38 +00:00
|
|
|
void ClientDestination::CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port) {
|
|
|
|
assert(streamRequestComplete);
|
2015-01-27 16:27:58 +00:00
|
|
|
auto leaseSet = FindLeaseSet (dest);
|
2015-01-02 12:35:38 +00:00
|
|
|
if (leaseSet)
|
2015-01-27 16:27:58 +00:00
|
|
|
streamRequestComplete(CreateStream (leaseSet, port));
|
2015-01-02 12:35:38 +00:00
|
|
|
else
|
2015-01-01 23:56:26 +00:00
|
|
|
{
|
|
|
|
RequestDestination (dest,
|
2015-01-02 12:35:38 +00:00
|
|
|
[this, streamRequestComplete, dest, port](bool success)
|
2015-01-01 23:56:26 +00:00
|
|
|
{
|
2015-01-02 12:35:38 +00:00
|
|
|
if (!success)
|
|
|
|
streamRequestComplete (nullptr);
|
|
|
|
else
|
|
|
|
{
|
2015-01-27 16:27:58 +00:00
|
|
|
auto leaseSet = FindLeaseSet (dest);
|
2015-01-02 12:35:38 +00:00
|
|
|
if (leaseSet)
|
2015-01-27 16:27:58 +00:00
|
|
|
streamRequestComplete(CreateStream (leaseSet, port));
|
2015-01-02 12:35:38 +00:00
|
|
|
else
|
|
|
|
streamRequestComplete (nullptr);
|
|
|
|
}
|
2015-01-01 23:56:26 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<i2p::stream::Stream> ClientDestination::CreateStream (std::shared_ptr<const i2p::data::LeaseSet> remote, int port)
|
2014-10-22 18:01:23 +00:00
|
|
|
{
|
|
|
|
if (m_StreamingDestination)
|
2014-10-23 01:36:11 +00:00
|
|
|
return m_StreamingDestination->CreateNewOutgoingStream (remote, port);
|
2015-01-02 12:35:38 +00:00
|
|
|
else
|
|
|
|
return nullptr;
|
2015-01-01 23:56:26 +00:00
|
|
|
}
|
2014-10-22 18:01:23 +00:00
|
|
|
|
2014-11-18 14:33:58 +00:00
|
|
|
void ClientDestination::AcceptStreams (const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
2014-10-22 18:01:23 +00: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-22 20:42:26 +00:00
|
|
|
|
2014-10-31 20:44:44 +00:00
|
|
|
i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination ()
|
2014-10-22 20:42:26 +00:00
|
|
|
{
|
|
|
|
if (!m_DatagramDestination)
|
|
|
|
m_DatagramDestination = new i2p::datagram::DatagramDestination (*this);
|
2014-10-31 20:44:44 +00:00
|
|
|
return m_DatagramDestination;
|
2014-10-22 20:42:26 +00:00
|
|
|
}
|
2014-12-25 21:47:15 +00:00
|
|
|
|
2014-12-27 00:09:44 +00:00
|
|
|
bool ClientDestination::RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
|
2014-12-25 21:47:15 +00:00
|
|
|
{
|
2015-01-03 03:37:46 +00:00
|
|
|
if (!m_Pool || !IsReady ())
|
|
|
|
{
|
|
|
|
if (requestComplete) requestComplete (false);
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-27 00:09:44 +00:00
|
|
|
m_Service.post (std::bind (&ClientDestination::RequestLeaseSet, this, dest, requestComplete));
|
2014-12-25 21:47:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-27 00:09:44 +00:00
|
|
|
void ClientDestination::RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
|
2014-12-25 21:47:15 +00:00
|
|
|
{
|
|
|
|
std::set<i2p::data::IdentHash> excluded;
|
|
|
|
auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
|
|
|
LeaseSetRequest * request = new LeaseSetRequest (m_Service);
|
2014-12-27 00:09:44 +00:00
|
|
|
request->requestComplete = requestComplete;
|
2015-01-09 03:04:41 +00:00
|
|
|
auto ret = m_LeaseSetRequests.insert (std::pair<i2p::data::IdentHash, LeaseSetRequest *>(dest,request));
|
|
|
|
if (ret.second) // inserted
|
2014-12-27 00:09:44 +00:00
|
|
|
{
|
2015-01-09 03:04:41 +00:00
|
|
|
if (!SendLeaseSetRequest (dest, floodfill, request))
|
|
|
|
{
|
|
|
|
// request failed
|
|
|
|
if (request->requestComplete) request->requestComplete (false);
|
|
|
|
delete request;
|
|
|
|
m_LeaseSetRequests.erase (dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // duplicate
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Request of ", dest.ToBase64 (), " is pending already");
|
|
|
|
// TODO: queue up requests
|
2014-12-27 00:09:44 +00:00
|
|
|
if (request->requestComplete) request->requestComplete (false);
|
|
|
|
delete request;
|
2015-01-09 03:04:41 +00:00
|
|
|
}
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (eLogError, "No floodfills found");
|
|
|
|
}
|
|
|
|
|
2014-12-27 00:09:44 +00:00
|
|
|
bool ClientDestination::SendLeaseSetRequest (const i2p::data::IdentHash& dest,
|
2014-12-25 21:47:15 +00:00
|
|
|
std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, LeaseSetRequest * request)
|
|
|
|
{
|
|
|
|
auto replyTunnel = m_Pool->GetNextInboundTunnel ();
|
|
|
|
if (!replyTunnel) LogPrint (eLogError, "No inbound tunnels found");
|
|
|
|
|
|
|
|
auto outboundTunnel = m_Pool->GetNextOutboundTunnel ();
|
|
|
|
if (!outboundTunnel) LogPrint (eLogError, "No outbound tunnels found");
|
|
|
|
|
|
|
|
if (replyTunnel && outboundTunnel)
|
|
|
|
{
|
|
|
|
request->excluded.insert (nextFloodfill->GetIdentHash ());
|
|
|
|
request->requestTime = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
request->requestTimeoutTimer.cancel ();
|
|
|
|
|
2014-12-30 17:25:08 +00:00
|
|
|
CryptoPP::AutoSeededRandomPool rnd;
|
|
|
|
uint8_t replyKey[32], replyTag[32];
|
|
|
|
rnd.GenerateBlock (replyKey, 32); // random session key
|
|
|
|
rnd.GenerateBlock (replyTag, 32); // random session tag
|
|
|
|
AddSessionKey (replyKey, replyTag);
|
|
|
|
|
2015-01-29 02:37:08 +00:00
|
|
|
I2NPMessage * msg = WrapMessage (nextFloodfill,
|
2014-12-30 17:25:08 +00:00
|
|
|
CreateLeaseSetDatabaseLookupMsg (dest, request->excluded,
|
2015-01-27 19:55:46 +00:00
|
|
|
replyTunnel.get (), replyKey, replyTag));
|
2014-12-25 21:47:15 +00:00
|
|
|
outboundTunnel->SendTunnelDataMsg (
|
|
|
|
{
|
|
|
|
i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
nextFloodfill->GetIdentHash (), 0, msg
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request->requestTimeoutTimer.expires_from_now (boost::posix_time::seconds(LEASESET_REQUEST_TIMEOUT));
|
|
|
|
request->requestTimeoutTimer.async_wait (std::bind (&ClientDestination::HandleRequestTimoutTimer,
|
|
|
|
this, std::placeholders::_1, dest));
|
|
|
|
}
|
|
|
|
else
|
2014-12-27 00:09:44 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest)
|
|
|
|
{
|
|
|
|
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)
|
2015-02-01 21:18:08 +00:00
|
|
|
done = !SendLeaseSetRequest (dest, floodfill, it->second);
|
2014-12-25 21:47:15 +00:00
|
|
|
else
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, dest.ToBase64 (), " was not found within ", MAX_LEASESET_REQUEST_TIMEOUT, " seconds");
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
{
|
2014-12-27 00:09:44 +00:00
|
|
|
if (it->second->requestComplete) it->second->requestComplete (false);
|
2014-12-25 21:47:15 +00:00
|
|
|
delete it->second;
|
|
|
|
m_LeaseSetRequests.erase (it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-23 17:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientDestination::HandleCleanupTimer (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
{
|
|
|
|
CleanupRoutingSessions ();
|
2015-01-28 19:20:28 +00:00
|
|
|
CleanupRemoteLeaseSets ();
|
2015-01-23 17:48:25 +00:00
|
|
|
m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT));
|
|
|
|
m_CleanupTimer.async_wait (std::bind (&ClientDestination::HandleCleanupTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
2014-12-25 21:47:15 +00:00
|
|
|
}
|
2015-01-28 19:20:28 +00:00
|
|
|
|
|
|
|
void ClientDestination::CleanupRemoteLeaseSets ()
|
|
|
|
{
|
|
|
|
for (auto it = m_RemoteLeaseSets.begin (); it != m_RemoteLeaseSets.end ();)
|
|
|
|
{
|
|
|
|
if (!it->second->HasNonExpiredLeases ()) // all leases expired
|
|
|
|
{
|
|
|
|
LogPrint ("Remote LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
|
|
|
|
it = m_RemoteLeaseSets.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2014-10-16 16:37:39 +00:00
|
|
|
}
|
2014-10-05 12:54:59 +00:00
|
|
|
}
|