2014-01-09 23:56:07 +04:00
|
|
|
#include "I2PEndian.h"
|
2013-11-20 16:46:09 +04:00
|
|
|
#include <fstream>
|
2014-01-21 04:12:59 +04:00
|
|
|
#include <vector>
|
2014-01-23 00:32:50 +04:00
|
|
|
#include <boost/asio.hpp>
|
2013-11-28 06:28:32 +04:00
|
|
|
#include <cryptopp/gzip.h>
|
2013-11-23 05:41:28 +04:00
|
|
|
#include "base64.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
#include "Log.h"
|
2013-11-20 16:46:09 +04:00
|
|
|
#include "Timestamp.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "Tunnel.h"
|
2014-01-27 00:24:21 +04:00
|
|
|
#include "Transports.h"
|
2013-11-19 05:37:38 +04:00
|
|
|
#include "RouterContext.h"
|
2014-01-03 06:22:48 +04:00
|
|
|
#include "Garlic.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
#include "NetDb.h"
|
2014-01-31 10:22:11 +04:00
|
|
|
#include "Reseed.h"
|
2014-02-01 19:10:15 +04:00
|
|
|
#include "util.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2014-10-21 20:25:53 +04:00
|
|
|
using namespace i2p::transport;
|
|
|
|
|
2013-11-13 16:59:21 +04:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2014-11-21 19:37:17 +03:00
|
|
|
I2NPMessage * RequestedDestination::CreateRequestMessage (std::shared_ptr<const RouterInfo> router,
|
2014-01-05 18:53:44 +04:00
|
|
|
const i2p::tunnel::InboundTunnel * replyTunnel)
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (m_Destination,
|
2014-10-07 04:18:18 +04:00
|
|
|
replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory,
|
|
|
|
&m_ExcludedPeers, m_IsLeaseSet, m_Pool);
|
2014-01-05 18:53:44 +04:00
|
|
|
if (m_IsLeaseSet) // wrap lookup message into garlic
|
2014-10-08 00:18:13 +04:00
|
|
|
{
|
|
|
|
if (m_Pool)
|
2014-12-10 15:28:49 +03:00
|
|
|
msg = m_Pool->GetLocalDestination ().WrapMessage (*router, msg);
|
2014-10-08 00:18:13 +04:00
|
|
|
else
|
|
|
|
LogPrint ("Can't create garlic message without destination");
|
|
|
|
}
|
2014-01-05 18:53:44 +04:00
|
|
|
m_ExcludedPeers.insert (router->GetIdentHash ());
|
|
|
|
m_LastRouter = router;
|
2014-07-07 23:35:42 +04:00
|
|
|
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
|
2014-01-05 18:53:44 +04:00
|
|
|
return msg;
|
|
|
|
}
|
2014-01-27 00:24:21 +04:00
|
|
|
|
|
|
|
I2NPMessage * RequestedDestination::CreateRequestMessage (const IdentHash& floodfill)
|
|
|
|
{
|
|
|
|
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (m_Destination,
|
|
|
|
i2p::context.GetRouterInfo ().GetIdentHash () , 0, false, &m_ExcludedPeers);
|
|
|
|
m_ExcludedPeers.insert (floodfill);
|
|
|
|
m_LastRouter = nullptr;
|
2014-07-07 23:35:42 +04:00
|
|
|
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
|
2014-01-27 00:24:21 +04:00
|
|
|
return msg;
|
|
|
|
}
|
2014-02-02 00:57:46 +04:00
|
|
|
|
2014-03-23 19:12:11 +04:00
|
|
|
void RequestedDestination::ClearExcludedPeers ()
|
|
|
|
{
|
|
|
|
m_ExcludedPeers.clear ();
|
|
|
|
}
|
|
|
|
|
2014-02-02 00:57:46 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
const char NetDb::m_NetDbPath[] = "/netDb";
|
|
|
|
#else
|
|
|
|
const char NetDb::m_NetDbPath[] = "\\netDb";
|
|
|
|
#endif
|
2013-11-13 16:59:21 +04:00
|
|
|
NetDb netdb;
|
|
|
|
|
2014-01-31 16:32:34 +04:00
|
|
|
NetDb::NetDb (): m_IsRunning (false), m_ReseedRetries (0), m_Thread (0)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NetDb::~NetDb ()
|
|
|
|
{
|
2014-03-19 22:08:09 +04:00
|
|
|
Stop ();
|
2013-11-13 16:59:21 +04:00
|
|
|
for (auto l:m_LeaseSets)
|
|
|
|
delete l.second;
|
2014-01-05 18:53:44 +04:00
|
|
|
for (auto r:m_RequestedDestinations)
|
|
|
|
delete r.second;
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
|
|
|
|
void NetDb::Start ()
|
2014-02-02 00:57:46 +04:00
|
|
|
{
|
|
|
|
Load (m_NetDbPath);
|
2014-01-31 22:08:33 +04:00
|
|
|
while (m_RouterInfos.size () < 100 && m_ReseedRetries < 10)
|
|
|
|
{
|
|
|
|
Reseeder reseeder;
|
|
|
|
reseeder.reseedNow();
|
|
|
|
m_ReseedRetries++;
|
2014-02-02 00:57:46 +04:00
|
|
|
Load (m_NetDbPath);
|
2014-01-31 22:08:33 +04:00
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
m_Thread = new std::thread (std::bind (&NetDb::Run, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetDb::Stop ()
|
|
|
|
{
|
|
|
|
if (m_Thread)
|
|
|
|
{
|
|
|
|
m_IsRunning = false;
|
2014-09-23 20:02:32 +04:00
|
|
|
m_Queue.WakeUp ();
|
2013-11-19 05:37:38 +04:00
|
|
|
m_Thread->join ();
|
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetDb::Run ()
|
|
|
|
{
|
2014-10-14 18:41:40 +04:00
|
|
|
uint32_t lastSave = 0, lastPublish = 0;
|
2013-11-19 05:37:38 +04:00
|
|
|
m_IsRunning = true;
|
|
|
|
while (m_IsRunning)
|
|
|
|
{
|
2013-11-28 06:28:32 +04:00
|
|
|
try
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
2014-10-07 04:18:18 +04:00
|
|
|
I2NPMessage * msg = m_Queue.GetNextWithTimeout (15000); // 15 sec
|
2013-11-28 06:28:32 +04:00
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
while (msg)
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
2014-07-23 19:10:03 +04:00
|
|
|
switch (msg->GetHeader ()->typeID)
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2014-07-23 19:10:03 +04:00
|
|
|
case eI2NPDatabaseStore:
|
2014-07-24 23:59:00 +04:00
|
|
|
LogPrint ("DatabaseStore");
|
2014-09-30 00:41:41 +04:00
|
|
|
HandleDatabaseStoreMsg (msg);
|
2014-07-23 19:10:03 +04:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseSearchReply:
|
2014-07-24 23:59:00 +04:00
|
|
|
LogPrint ("DatabaseSearchReply");
|
2014-07-23 19:10:03 +04:00
|
|
|
HandleDatabaseSearchReplyMsg (msg);
|
|
|
|
break;
|
|
|
|
case eI2NPDatabaseLookup:
|
2014-07-24 23:59:00 +04:00
|
|
|
LogPrint ("DatabaseLookup");
|
2014-07-23 19:10:03 +04:00
|
|
|
HandleDatabaseLookupMsg (msg);
|
|
|
|
break;
|
|
|
|
default: // WTF?
|
|
|
|
LogPrint ("NetDb: unexpected message type ", msg->GetHeader ()->typeID);
|
|
|
|
i2p::HandleI2NPMessage (msg);
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
|
|
|
msg = m_Queue.Get ();
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
2014-09-23 20:02:32 +04:00
|
|
|
else
|
2014-06-17 06:30:34 +04:00
|
|
|
{
|
2014-09-23 20:02:32 +04:00
|
|
|
if (!m_IsRunning) break;
|
|
|
|
// if no new DatabaseStore coming, explore it
|
2014-06-17 06:30:34 +04:00
|
|
|
auto numRouters = m_RouterInfos.size ();
|
|
|
|
Explore (numRouters < 1500 ? 5 : 1);
|
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
|
2014-01-09 23:56:07 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-07-31 20:59:43 +04:00
|
|
|
if (ts - lastSave >= 60) // save routers, manage leasesets and validate subscriptions every minute
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2014-02-13 07:02:39 +04:00
|
|
|
if (lastSave)
|
2014-02-15 01:10:25 +04:00
|
|
|
{
|
2014-02-02 00:57:46 +04:00
|
|
|
SaveUpdated (m_NetDbPath);
|
2014-07-31 20:59:43 +04:00
|
|
|
ManageLeaseSets ();
|
2014-02-15 01:10:25 +04:00
|
|
|
}
|
2014-02-13 07:02:39 +04:00
|
|
|
lastSave = ts;
|
|
|
|
}
|
|
|
|
if (ts - lastPublish >= 600) // publish every 10 minutes
|
|
|
|
{
|
|
|
|
Publish ();
|
|
|
|
lastPublish = ts;
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
catch (std::exception& ex)
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
2013-11-28 06:28:32 +04:00
|
|
|
LogPrint ("NetDb: ", ex.what ());
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2014-12-11 23:41:04 +03:00
|
|
|
void NetDb::AddRouterInfo (const uint8_t * buf, int len)
|
|
|
|
{
|
|
|
|
IdentityEx identity;
|
|
|
|
identity.FromBuffer (buf, len);
|
|
|
|
AddRouterInfo (identity.GetIdentHash (), buf, len);
|
|
|
|
}
|
|
|
|
|
2014-09-30 00:41:41 +04:00
|
|
|
void NetDb::AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len)
|
2014-07-22 04:14:11 +04:00
|
|
|
{
|
2014-11-21 21:29:19 +03:00
|
|
|
DeleteRequestedDestination (ident);
|
|
|
|
auto r = FindRouter (ident);
|
|
|
|
if (r)
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2014-11-21 21:29:19 +03:00
|
|
|
auto ts = r->GetTimestamp ();
|
|
|
|
r->Update (buf, len);
|
|
|
|
if (r->GetTimestamp () > ts)
|
2013-11-28 06:28:32 +04:00
|
|
|
LogPrint ("RouterInfo updated");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("New RouterInfo added");
|
2014-11-21 21:29:19 +03:00
|
|
|
auto newRouter = std::make_shared<RouterInfo> (buf, len);
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
2014-11-21 21:49:49 +03:00
|
|
|
m_RouterInfos[newRouter->GetIdentHash ()] = newRouter;
|
2014-11-21 21:29:19 +03:00
|
|
|
}
|
|
|
|
if (newRouter->IsFloodfill ())
|
2014-10-06 05:59:05 +04:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
|
2014-11-21 21:29:19 +03:00
|
|
|
m_Floodfills.push_back (newRouter);
|
2014-10-06 05:59:05 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
|
2014-10-14 01:45:07 +04:00
|
|
|
void NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len,
|
|
|
|
i2p::tunnel::InboundTunnel * from)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-10-14 01:45:07 +04:00
|
|
|
DeleteRequestedDestination (ident);
|
|
|
|
if (!from) // unsolicited LS must be received directly
|
2014-01-13 07:31:26 +04:00
|
|
|
{
|
2014-10-14 01:45:07 +04:00
|
|
|
auto it = m_LeaseSets.find(ident);
|
|
|
|
if (it != m_LeaseSets.end ())
|
|
|
|
{
|
|
|
|
it->second->Update (buf, len);
|
|
|
|
LogPrint ("LeaseSet updated");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("New LeaseSet added");
|
|
|
|
m_LeaseSets[ident] = new LeaseSet (buf, len);
|
|
|
|
}
|
2014-01-13 07:31:26 +04:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
|
2014-11-21 19:37:17 +03:00
|
|
|
std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-11-21 21:29:19 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
2013-11-29 16:52:09 +04:00
|
|
|
auto it = m_RouterInfos.find (ident);
|
2013-11-13 16:59:21 +04:00
|
|
|
if (it != m_RouterInfos.end ())
|
2014-11-21 19:37:17 +03:00
|
|
|
return it->second;
|
2013-11-13 16:59:21 +04:00
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-12-22 20:29:57 +04:00
|
|
|
|
|
|
|
LeaseSet * NetDb::FindLeaseSet (const IdentHash& destination) const
|
|
|
|
{
|
|
|
|
auto it = m_LeaseSets.find (destination);
|
|
|
|
if (it != m_LeaseSets.end ())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return nullptr;
|
2014-01-29 22:36:20 +04:00
|
|
|
}
|
|
|
|
|
2014-10-24 23:39:53 +04:00
|
|
|
void NetDb::SetUnreachable (const IdentHash& ident, bool unreachable)
|
|
|
|
{
|
|
|
|
auto it = m_RouterInfos.find (ident);
|
|
|
|
if (it != m_RouterInfos.end ())
|
|
|
|
return it->second->SetUnreachable (unreachable);
|
|
|
|
}
|
|
|
|
|
2014-01-29 22:36:20 +04:00
|
|
|
// TODO: Move to reseed and/or scheduled tasks. (In java version, scheduler fix this as well as sort RIs.)
|
2014-02-01 19:10:15 +04:00
|
|
|
bool NetDb::CreateNetDb(boost::filesystem::path directory)
|
2014-01-29 22:36:20 +04:00
|
|
|
{
|
2014-02-01 19:10:15 +04:00
|
|
|
LogPrint (directory.string(), " doesn't exist, trying to create it.");
|
|
|
|
if (!boost::filesystem::create_directory (directory))
|
2014-01-29 22:36:20 +04:00
|
|
|
{
|
2014-02-01 19:10:15 +04:00
|
|
|
LogPrint("Failed to create directory ", directory.string());
|
2014-01-29 22:36:20 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-30 02:38:11 +04:00
|
|
|
// list of chars might appear in base64 string
|
2014-01-30 02:43:20 +04:00
|
|
|
const char * chars = GetBase64SubstitutionTable (); // 64 bytes
|
2014-01-29 22:36:20 +04:00
|
|
|
boost::filesystem::path suffix;
|
2014-01-30 02:43:20 +04:00
|
|
|
for (int i = 0; i < 64; i++)
|
2014-01-29 22:36:20 +04:00
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
2014-01-30 02:43:20 +04:00
|
|
|
suffix = std::string ("/r") + chars[i];
|
2014-01-29 22:36:20 +04:00
|
|
|
#else
|
2014-01-30 02:43:20 +04:00
|
|
|
suffix = std::string ("\\r") + chars[i];
|
2014-01-29 22:36:20 +04:00
|
|
|
#endif
|
2014-02-01 19:10:15 +04:00
|
|
|
if (!boost::filesystem::create_directory( boost::filesystem::path (directory / suffix) )) return false;
|
2014-01-29 22:36:20 +04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-31 10:22:11 +04:00
|
|
|
|
2014-01-31 22:08:33 +04:00
|
|
|
void NetDb::Load (const char * directory)
|
2014-01-31 10:22:11 +04:00
|
|
|
{
|
2014-02-01 19:10:15 +04:00
|
|
|
boost::filesystem::path p (i2p::util::filesystem::GetDataDir());
|
|
|
|
p /= (directory);
|
2014-01-29 22:36:20 +04:00
|
|
|
if (!boost::filesystem::exists (p))
|
|
|
|
{
|
2014-01-31 22:08:33 +04:00
|
|
|
// seems netDb doesn't exist yet
|
2014-02-01 19:10:15 +04:00
|
|
|
if (!CreateNetDb(p)) return;
|
2014-01-29 22:36:20 +04:00
|
|
|
}
|
2014-01-31 22:08:33 +04:00
|
|
|
// make sure we cleanup netDb from previous attempts
|
|
|
|
m_RouterInfos.clear ();
|
2014-03-19 22:08:09 +04:00
|
|
|
m_Floodfills.clear ();
|
2014-01-31 22:08:33 +04:00
|
|
|
|
|
|
|
// load routers now
|
2014-08-19 19:01:11 +04:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2014-01-29 22:36:20 +04:00
|
|
|
int numRouters = 0;
|
|
|
|
boost::filesystem::directory_iterator end;
|
|
|
|
for (boost::filesystem::directory_iterator it (p); it != end; ++it)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-01-29 22:36:20 +04:00
|
|
|
if (boost::filesystem::is_directory (it->status()))
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-01-29 22:36:20 +04:00
|
|
|
for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-01-10 02:55:53 +04:00
|
|
|
#if BOOST_VERSION > 10500
|
2014-07-25 04:47:12 +04:00
|
|
|
const std::string& fullPath = it1->path().string();
|
2014-01-10 01:51:41 +04:00
|
|
|
#else
|
2014-07-25 04:47:12 +04:00
|
|
|
const std::string& fullPath = it1->path();
|
2014-01-10 01:51:41 +04:00
|
|
|
#endif
|
2014-11-18 19:08:10 +03:00
|
|
|
auto r = std::make_shared<RouterInfo>(fullPath);
|
2014-08-19 19:01:11 +04:00
|
|
|
if (!r->IsUnreachable () && (!r->UsesIntroducer () || ts < r->GetTimestamp () + 3600*1000LL)) // 1 hour
|
2014-07-25 04:47:12 +04:00
|
|
|
{
|
|
|
|
r->DeleteBuffer ();
|
|
|
|
m_RouterInfos[r->GetIdentHash ()] = r;
|
|
|
|
if (r->IsFloodfill ())
|
|
|
|
m_Floodfills.push_back (r);
|
|
|
|
numRouters++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (boost::filesystem::exists (fullPath))
|
|
|
|
boost::filesystem::remove (fullPath);
|
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 22:36:20 +04:00
|
|
|
LogPrint (numRouters, " routers loaded");
|
2014-03-19 22:08:09 +04:00
|
|
|
LogPrint (m_Floodfills.size (), " floodfills loaded");
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
|
2013-11-20 16:46:09 +04:00
|
|
|
void NetDb::SaveUpdated (const char * directory)
|
|
|
|
{
|
2013-12-22 20:29:57 +04:00
|
|
|
auto GetFilePath = [](const char * directory, const RouterInfo * routerInfo)
|
|
|
|
{
|
2014-01-29 22:36:20 +04:00
|
|
|
#ifndef _WIN32
|
2013-12-22 20:29:57 +04:00
|
|
|
return std::string (directory) + "/r" +
|
2014-01-29 22:36:20 +04:00
|
|
|
routerInfo->GetIdentHashBase64 ()[0] + "/routerInfo-" +
|
|
|
|
#else
|
|
|
|
return std::string (directory) + "\\r" +
|
|
|
|
routerInfo->GetIdentHashBase64 ()[0] + "\\routerInfo-" +
|
|
|
|
#endif
|
2013-12-22 20:29:57 +04:00
|
|
|
routerInfo->GetIdentHashBase64 () + ".dat";
|
|
|
|
};
|
2014-02-02 00:57:46 +04:00
|
|
|
|
|
|
|
boost::filesystem::path p (i2p::util::filesystem::GetDataDir());
|
|
|
|
p /= (directory);
|
|
|
|
#if BOOST_VERSION > 10500
|
|
|
|
const char * fullDirectory = p.string().c_str ();
|
|
|
|
#else
|
|
|
|
const char * fullDirectory = p.c_str ();
|
|
|
|
#endif
|
2013-12-22 20:29:57 +04:00
|
|
|
int count = 0, deletedCount = 0;
|
2014-01-23 05:19:39 +04:00
|
|
|
auto total = m_RouterInfos.size ();
|
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2013-11-20 16:46:09 +04:00
|
|
|
for (auto it: m_RouterInfos)
|
2013-12-22 20:29:57 +04:00
|
|
|
{
|
2013-11-20 16:46:09 +04:00
|
|
|
if (it.second->IsUpdated ())
|
|
|
|
{
|
2014-11-18 19:08:10 +03:00
|
|
|
it.second->SaveToFile (GetFilePath(fullDirectory, it.second.get ()));
|
2013-11-20 16:46:09 +04:00
|
|
|
it.second->SetUpdated (false);
|
2014-07-22 06:59:57 +04:00
|
|
|
it.second->DeleteBuffer ();
|
2013-11-20 16:46:09 +04:00
|
|
|
count++;
|
|
|
|
}
|
2014-01-23 05:19:39 +04:00
|
|
|
else
|
2013-12-22 20:29:57 +04:00
|
|
|
{
|
2014-08-19 19:01:11 +04:00
|
|
|
// RouterInfo expires after 1 hour if uses introducer
|
|
|
|
if ((it.second->UsesIntroducer () && ts > it.second->GetTimestamp () + 3600*1000LL) // 1 hour
|
2014-01-23 05:19:39 +04:00
|
|
|
// RouterInfo expires in 72 hours if more than 300
|
2014-08-19 19:01:11 +04:00
|
|
|
|| (total > 300 && ts > it.second->GetTimestamp () + 3*24*3600*1000LL)) // 3 days
|
2014-01-23 05:48:08 +04:00
|
|
|
{
|
|
|
|
total--;
|
2014-01-23 05:19:39 +04:00
|
|
|
it.second->SetUnreachable (true);
|
2014-01-23 05:48:08 +04:00
|
|
|
}
|
2014-01-23 05:19:39 +04:00
|
|
|
|
|
|
|
if (it.second->IsUnreachable ())
|
|
|
|
{
|
2014-11-21 21:02:46 +03:00
|
|
|
// delete RI file
|
2014-11-18 19:08:10 +03:00
|
|
|
if (boost::filesystem::exists (GetFilePath (fullDirectory, it.second.get ())))
|
2014-01-23 05:19:39 +04:00
|
|
|
{
|
2014-11-18 19:08:10 +03:00
|
|
|
boost::filesystem::remove (GetFilePath (fullDirectory, it.second.get ()));
|
2014-01-23 05:19:39 +04:00
|
|
|
deletedCount++;
|
|
|
|
}
|
2014-11-21 21:02:46 +03:00
|
|
|
// delete from floodfills list
|
|
|
|
if (it.second->IsFloodfill ())
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
|
|
|
|
m_Floodfills.remove (it.second);
|
|
|
|
}
|
2014-01-23 05:19:39 +04:00
|
|
|
}
|
2013-12-22 20:29:57 +04:00
|
|
|
}
|
|
|
|
}
|
2013-11-20 16:46:09 +04:00
|
|
|
if (count > 0)
|
2013-11-28 06:28:32 +04:00
|
|
|
LogPrint (count," new/updated routers saved");
|
2013-12-22 20:29:57 +04:00
|
|
|
if (deletedCount > 0)
|
2014-11-21 21:29:19 +03:00
|
|
|
{
|
2013-12-22 20:29:57 +04:00
|
|
|
LogPrint (deletedCount," routers deleted");
|
2014-11-21 21:29:19 +03:00
|
|
|
// clean up RouterInfos table
|
|
|
|
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
|
|
|
for (auto it = m_RouterInfos.begin (); it != m_RouterInfos.end ();)
|
|
|
|
{
|
|
|
|
if (it->second->IsUnreachable ())
|
|
|
|
it = m_RouterInfos.erase (it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
|
2014-08-21 05:47:28 +04:00
|
|
|
void NetDb::RequestDestination (const IdentHash& destination, bool isLeaseSet, i2p::tunnel::TunnelPool * pool)
|
2014-01-03 06:22:48 +04:00
|
|
|
{
|
2014-01-27 00:24:21 +04:00
|
|
|
if (isLeaseSet) // we request LeaseSet through tunnels
|
|
|
|
{
|
2014-08-21 05:47:28 +04:00
|
|
|
i2p::tunnel::OutboundTunnel * outbound = pool ? pool->GetNextOutboundTunnel () : i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
2014-01-27 00:24:21 +04:00
|
|
|
if (outbound)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2014-08-21 05:47:28 +04:00
|
|
|
i2p::tunnel::InboundTunnel * inbound = pool ? pool->GetNextInboundTunnel () :i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
2014-01-27 00:24:21 +04:00
|
|
|
if (inbound)
|
|
|
|
{
|
2014-09-27 03:22:13 +04:00
|
|
|
RequestedDestination * dest = CreateRequestedDestination (destination, true, false, pool);
|
2014-10-15 04:52:40 +04:00
|
|
|
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
|
|
|
|
if (floodfill)
|
|
|
|
{
|
|
|
|
// DatabaseLookup message
|
|
|
|
outbound->SendTunnelDataMsg (
|
|
|
|
{
|
|
|
|
i2p::tunnel::TunnelMessageBlock
|
2014-03-23 00:05:58 +04:00
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
floodfill->GetIdentHash (), 0,
|
|
|
|
dest->CreateRequestMessage (floodfill, inbound)
|
2014-10-15 04:52:40 +04:00
|
|
|
}
|
|
|
|
});
|
2014-01-27 00:24:21 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("No more floodfills found");
|
2014-01-22 16:44:41 +04:00
|
|
|
}
|
|
|
|
else
|
2014-01-27 00:24:21 +04:00
|
|
|
LogPrint ("No inbound tunnels found");
|
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
else
|
2014-01-27 00:24:21 +04:00
|
|
|
LogPrint ("No outbound tunnels found");
|
|
|
|
}
|
|
|
|
else // RouterInfo is requested directly
|
|
|
|
{
|
2014-09-27 03:22:13 +04:00
|
|
|
RequestedDestination * dest = CreateRequestedDestination (destination, false, false, pool);
|
2014-01-27 00:24:21 +04:00
|
|
|
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
|
|
|
|
if (floodfill)
|
2014-10-21 20:25:53 +04:00
|
|
|
transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
|
2014-01-27 00:24:21 +04:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
2013-12-22 20:29:57 +04:00
|
|
|
|
2014-09-30 00:41:41 +04:00
|
|
|
void NetDb::HandleDatabaseStoreMsg (I2NPMessage * m)
|
|
|
|
{
|
|
|
|
const uint8_t * buf = m->GetPayload ();
|
|
|
|
size_t len = be16toh (m->GetHeader ()->size);
|
2013-11-28 06:28:32 +04:00
|
|
|
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)buf;
|
|
|
|
size_t offset = sizeof (I2NPDatabaseStoreMsg);
|
|
|
|
if (msg->replyToken)
|
|
|
|
offset += 36;
|
|
|
|
if (msg->type)
|
2013-11-19 05:37:38 +04:00
|
|
|
{
|
2013-11-28 06:28:32 +04:00
|
|
|
LogPrint ("LeaseSet");
|
2014-10-14 01:45:07 +04:00
|
|
|
AddLeaseSet (msg->key, buf + offset, len - offset, m->from);
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("RouterInfo");
|
|
|
|
size_t size = be16toh (*(uint16_t *)(buf + offset));
|
|
|
|
if (size > 2048)
|
|
|
|
{
|
|
|
|
LogPrint ("Invalid RouterInfo length ", (int)size);
|
|
|
|
return;
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
offset += 2;
|
|
|
|
CryptoPP::Gunzip decompressor;
|
|
|
|
decompressor.Put (buf + offset, size);
|
|
|
|
decompressor.MessageEnd();
|
|
|
|
uint8_t uncompressed[2048];
|
2014-01-09 23:56:07 +04:00
|
|
|
size_t uncomressedSize = decompressor.MaxRetrievable ();
|
2013-11-28 06:28:32 +04:00
|
|
|
decompressor.Get (uncompressed, uncomressedSize);
|
2014-07-22 04:14:11 +04:00
|
|
|
AddRouterInfo (msg->key, uncompressed, uncomressedSize);
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
2014-09-30 00:41:41 +04:00
|
|
|
i2p::DeleteI2NPMessage (m);
|
2013-11-19 05:37:38 +04:00
|
|
|
}
|
|
|
|
|
2013-11-23 05:41:28 +04:00
|
|
|
void NetDb::HandleDatabaseSearchReplyMsg (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
|
|
|
char key[48];
|
|
|
|
int l = i2p::data::ByteStreamToBase64 (buf, 32, key, 48);
|
|
|
|
key[l] = 0;
|
|
|
|
int num = buf[32]; // num
|
|
|
|
LogPrint ("DatabaseSearchReply for ", key, " num=", num);
|
2014-01-05 18:53:44 +04:00
|
|
|
auto it = m_RequestedDestinations.find (IdentHash (buf));
|
|
|
|
if (it != m_RequestedDestinations.end ())
|
|
|
|
{
|
|
|
|
RequestedDestination * dest = it->second;
|
2014-07-10 16:13:47 +04:00
|
|
|
bool deleteDest = true;
|
2014-01-05 18:53:44 +04:00
|
|
|
if (num > 0)
|
|
|
|
{
|
2014-08-25 05:17:21 +04:00
|
|
|
auto pool = dest ? dest->GetTunnelPool () : nullptr;
|
|
|
|
auto outbound = pool ? pool->GetNextOutboundTunnel () : i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
|
|
|
auto inbound = pool ? pool->GetNextInboundTunnel () : i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
2014-01-21 04:12:59 +04:00
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
|
2014-10-15 04:52:40 +04:00
|
|
|
if (!dest->IsExploratory ())
|
|
|
|
{
|
|
|
|
// reply to our destination. Try other floodfills
|
|
|
|
if (outbound && inbound )
|
|
|
|
{
|
|
|
|
auto count = dest->GetExcludedPeers ().size ();
|
|
|
|
if (count < 7)
|
|
|
|
{
|
|
|
|
auto nextFloodfill = GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ());
|
|
|
|
if (nextFloodfill)
|
|
|
|
{
|
|
|
|
if (!dest->IsLeaseSet ())
|
|
|
|
{
|
|
|
|
// tell floodfill about us
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
nextFloodfill->GetIdentHash (), 0,
|
|
|
|
CreateDatabaseStoreMsg ()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// request destination
|
|
|
|
LogPrint ("Try ", key, " at ", count, " floodfill ", nextFloodfill->GetIdentHash ().ToBase64 ());
|
|
|
|
auto msg = dest->CreateRequestMessage (nextFloodfill, inbound);
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
nextFloodfill->GetIdentHash (), 0, msg
|
|
|
|
});
|
|
|
|
deleteDest = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint (key, " was not found on 7 floodfills");
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 04:12:59 +04:00
|
|
|
|
2014-01-05 18:53:44 +04:00
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
uint8_t * router = buf + 33 + i*32;
|
|
|
|
char peerHash[48];
|
|
|
|
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48);
|
|
|
|
peerHash[l1] = 0;
|
|
|
|
LogPrint (i,": ", peerHash);
|
2013-11-23 05:41:28 +04:00
|
|
|
|
2014-01-05 18:53:44 +04:00
|
|
|
if (dest->IsExploratory ())
|
|
|
|
{
|
2014-04-06 05:25:54 +04:00
|
|
|
auto r = FindRouter (router);
|
|
|
|
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
|
2014-01-05 18:53:44 +04:00
|
|
|
{
|
2014-04-06 05:25:54 +04:00
|
|
|
// router with ident not found or too old (1 hour)
|
|
|
|
LogPrint ("Found new/outdated router. Requesting RouterInfo ...");
|
2014-07-10 05:43:33 +04:00
|
|
|
if (outbound && inbound && dest->GetLastRouter ())
|
2014-01-05 18:53:44 +04:00
|
|
|
{
|
2014-08-25 05:17:21 +04:00
|
|
|
RequestedDestination * d1 = CreateRequestedDestination (router, false, false, pool);
|
2014-07-10 05:43:33 +04:00
|
|
|
auto msg = d1->CreateRequestMessage (dest->GetLastRouter (), inbound);
|
2014-01-21 04:12:59 +04:00
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
dest->GetLastRouter ()->GetIdentHash (), 0, msg
|
|
|
|
});
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
2014-08-29 03:41:02 +04:00
|
|
|
else
|
|
|
|
RequestDestination (router, false, pool);
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint ("Bayan");
|
|
|
|
}
|
|
|
|
else
|
2014-10-15 04:52:40 +04:00
|
|
|
{
|
|
|
|
auto r = FindRouter (router);
|
|
|
|
// do we have that floodfill router in our database?
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
// request router
|
|
|
|
LogPrint ("Found new floodfill. Request it");
|
|
|
|
RequestDestination (router, false, pool);
|
2013-11-28 06:28:32 +04:00
|
|
|
}
|
2014-07-10 16:13:47 +04:00
|
|
|
}
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
|
2014-01-27 00:24:21 +04:00
|
|
|
if (outbound && msgs.size () > 0)
|
2014-01-21 04:12:59 +04:00
|
|
|
outbound->SendTunnelDataMsg (msgs);
|
2014-07-10 16:13:47 +04:00
|
|
|
if (deleteDest)
|
|
|
|
{
|
2014-10-15 04:52:40 +04:00
|
|
|
// no more requests for the destinationation. delete it
|
2014-07-10 16:13:47 +04:00
|
|
|
delete it->second;
|
|
|
|
m_RequestedDestinations.erase (it);
|
|
|
|
}
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no more requests for detination possible. delete it
|
|
|
|
delete it->second;
|
2014-01-25 06:14:14 +04:00
|
|
|
m_RequestedDestinations.erase (it);
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-08-10 17:04:17 +04:00
|
|
|
{
|
2014-01-05 18:53:44 +04:00
|
|
|
LogPrint ("Requested destination for ", key, " not found");
|
2014-08-10 17:04:17 +04:00
|
|
|
// it might contain new routers
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
IdentHash router (buf + 33 + i*32);
|
|
|
|
if (!FindRouter (router))
|
|
|
|
{
|
|
|
|
LogPrint ("New router ", router.ToBase64 (), " found. Request it");
|
|
|
|
RequestDestination (router);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-23 05:41:28 +04:00
|
|
|
i2p::DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:10:03 +04:00
|
|
|
void NetDb::HandleDatabaseLookupMsg (I2NPMessage * msg)
|
|
|
|
{
|
|
|
|
uint8_t * buf = msg->GetPayload ();
|
|
|
|
char key[48];
|
|
|
|
int l = i2p::data::ByteStreamToBase64 (buf, 32, key, 48);
|
|
|
|
key[l] = 0;
|
|
|
|
LogPrint ("DatabaseLookup for ", key, " recieved");
|
|
|
|
uint8_t flag = buf[64];
|
2014-07-30 15:32:46 +04:00
|
|
|
uint8_t * excluded = buf + 65;
|
2014-07-23 19:10:03 +04:00
|
|
|
uint32_t replyTunnelID = 0;
|
|
|
|
if (flag & 0x01) //reply to tunnel
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
2014-07-23 19:10:03 +04:00
|
|
|
replyTunnelID = be32toh (*(uint32_t *)(buf + 64));
|
2014-07-28 22:25:14 +04:00
|
|
|
excluded += 4;
|
|
|
|
}
|
|
|
|
uint16_t numExcluded = be16toh (*(uint16_t *)excluded);
|
|
|
|
excluded += 2;
|
|
|
|
if (numExcluded > 512)
|
|
|
|
{
|
2014-07-30 15:32:46 +04:00
|
|
|
LogPrint ("Number of excluded peers", numExcluded, " exceeds 512");
|
2014-07-28 22:25:14 +04:00
|
|
|
numExcluded = 0; // TODO:
|
2014-07-29 06:28:11 +04:00
|
|
|
}
|
2014-07-24 23:59:00 +04:00
|
|
|
|
|
|
|
I2NPMessage * replyMsg = nullptr;
|
2014-07-31 20:59:43 +04:00
|
|
|
|
2014-07-24 23:59:00 +04:00
|
|
|
{
|
2014-07-31 20:59:43 +04:00
|
|
|
auto router = FindRouter (buf);
|
|
|
|
if (router)
|
|
|
|
{
|
|
|
|
LogPrint ("Requested RouterInfo ", key, " found");
|
|
|
|
router->LoadBuffer ();
|
|
|
|
if (router->GetBuffer ())
|
2014-11-21 19:37:17 +03:00
|
|
|
replyMsg = CreateDatabaseStoreMsg (router.get ());
|
2014-07-31 20:59:43 +04:00
|
|
|
}
|
2014-07-30 15:32:46 +04:00
|
|
|
}
|
2014-07-31 20:59:43 +04:00
|
|
|
if (!replyMsg)
|
|
|
|
{
|
|
|
|
auto leaseSet = FindLeaseSet (buf);
|
2014-10-14 01:45:07 +04:00
|
|
|
if (leaseSet) // we don't send back our LeaseSets
|
2014-07-31 20:59:43 +04:00
|
|
|
{
|
|
|
|
LogPrint ("Requested LeaseSet ", key, " found");
|
|
|
|
replyMsg = CreateDatabaseStoreMsg (leaseSet);
|
|
|
|
}
|
2014-07-24 23:59:00 +04:00
|
|
|
}
|
2014-07-31 20:59:43 +04:00
|
|
|
if (!replyMsg)
|
2014-07-25 06:01:07 +04:00
|
|
|
{
|
2014-07-29 06:28:11 +04:00
|
|
|
LogPrint ("Requested ", key, " not found. ", numExcluded, " excluded");
|
|
|
|
std::set<IdentHash> excludedRouters;
|
|
|
|
for (int i = 0; i < numExcluded; i++)
|
|
|
|
{
|
|
|
|
// TODO: check for all zeroes (exploratory)
|
|
|
|
excludedRouters.insert (excluded);
|
|
|
|
excluded += 32;
|
|
|
|
}
|
2014-11-21 19:37:17 +03:00
|
|
|
replyMsg = CreateDatabaseSearchReply (buf, GetClosestFloodfill (buf, excludedRouters).get ());
|
2014-07-31 20:59:43 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
excluded += numExcluded*32; // we don't care about exluded
|
|
|
|
|
2014-07-24 23:59:00 +04:00
|
|
|
if (replyMsg)
|
|
|
|
{
|
|
|
|
if (replyTunnelID)
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
|
|
|
// encryption might be used though tunnel only
|
|
|
|
if (flag & 0x02) // encrypted reply requested
|
|
|
|
{
|
|
|
|
uint8_t * sessionKey = excluded;
|
|
|
|
uint8_t numTags = sessionKey[32];
|
|
|
|
if (numTags > 0)
|
|
|
|
{
|
|
|
|
uint8_t * sessionTag = sessionKey + 33; // take first tag
|
|
|
|
i2p::garlic::GarlicRoutingSession garlic (sessionKey, sessionTag);
|
2014-10-08 20:11:39 +04:00
|
|
|
replyMsg = garlic.WrapSingleMessage (replyMsg);
|
2014-07-28 22:25:14 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 18:13:17 +04:00
|
|
|
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
|
|
|
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
|
|
|
|
if (outbound)
|
|
|
|
outbound->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg);
|
|
|
|
else
|
2014-10-21 20:25:53 +04:00
|
|
|
transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg));
|
2014-07-28 22:25:14 +04:00
|
|
|
}
|
2014-07-24 23:59:00 +04:00
|
|
|
else
|
2014-10-21 20:25:53 +04:00
|
|
|
transports.SendMessage (buf+32, replyMsg);
|
2014-07-24 23:59:00 +04:00
|
|
|
}
|
2014-07-23 19:10:03 +04:00
|
|
|
i2p::DeleteI2NPMessage (msg);
|
|
|
|
}
|
|
|
|
|
2014-06-17 06:30:34 +04:00
|
|
|
void NetDb::Explore (int numDestinations)
|
2014-06-17 05:23:22 +04:00
|
|
|
{
|
2014-07-03 21:41:36 +04:00
|
|
|
// clean up previous exploratories
|
2014-07-07 23:35:42 +04:00
|
|
|
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
|
2014-07-03 21:41:36 +04:00
|
|
|
for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();)
|
|
|
|
{
|
2014-07-07 23:35:42 +04:00
|
|
|
if (it->second->IsExploratory () || ts > it->second->GetCreationTime () + 60) // no response for 1 minute
|
2014-07-03 21:41:36 +04:00
|
|
|
{
|
|
|
|
delete it->second;
|
|
|
|
it = m_RequestedDestinations.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
// new requests
|
2014-06-17 06:30:34 +04:00
|
|
|
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
2014-08-28 21:51:26 +04:00
|
|
|
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
|
|
|
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : i2p::tunnel::tunnels.GetNextInboundTunnel ();
|
2014-06-17 06:30:34 +04:00
|
|
|
bool throughTunnels = outbound && inbound;
|
|
|
|
|
2014-06-17 05:23:22 +04:00
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
|
|
|
uint8_t randomHash[32];
|
2014-06-17 06:30:34 +04:00
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
|
|
|
|
std::set<const RouterInfo *> floodfills;
|
|
|
|
LogPrint ("Exploring new ", numDestinations, " routers ...");
|
|
|
|
for (int i = 0; i < numDestinations; i++)
|
2014-06-17 05:23:22 +04:00
|
|
|
{
|
2014-06-17 06:30:34 +04:00
|
|
|
rnd.GenerateBlock (randomHash, 32);
|
2014-08-21 05:47:28 +04:00
|
|
|
RequestedDestination * dest = CreateRequestedDestination (IdentHash (randomHash), false, true, exploratoryPool);
|
2014-06-17 06:30:34 +04:00
|
|
|
auto floodfill = GetClosestFloodfill (randomHash, dest->GetExcludedPeers ());
|
2014-11-21 19:37:17 +03:00
|
|
|
if (floodfill && !floodfills.count (floodfill.get ())) // request floodfill only once
|
2014-02-12 07:19:51 +04:00
|
|
|
{
|
2014-11-21 19:37:17 +03:00
|
|
|
floodfills.insert (floodfill.get ());
|
2014-06-17 06:30:34 +04:00
|
|
|
if (throughTunnels)
|
|
|
|
{
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
floodfill->GetIdentHash (), 0,
|
|
|
|
CreateDatabaseStoreMsg () // tell floodfill about us
|
|
|
|
});
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
floodfill->GetIdentHash (), 0,
|
|
|
|
dest->CreateRequestMessage (floodfill, inbound) // explore
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
|
2013-11-19 05:37:38 +04:00
|
|
|
}
|
2014-02-12 07:19:51 +04:00
|
|
|
else
|
2014-06-17 06:30:34 +04:00
|
|
|
DeleteRequestedDestination (dest);
|
2014-06-17 05:23:22 +04:00
|
|
|
}
|
2014-06-17 06:30:34 +04:00
|
|
|
if (throughTunnels && msgs.size () > 0)
|
|
|
|
outbound->SendTunnelDataMsg (msgs);
|
2013-11-19 05:37:38 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:02:39 +04:00
|
|
|
void NetDb::Publish ()
|
|
|
|
{
|
|
|
|
std::set<IdentHash> excluded; // TODO: fill up later
|
2014-03-16 16:00:34 +04:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
auto floodfill = GetClosestFloodfill (i2p::context.GetRouterInfo ().GetIdentHash (), excluded);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
|
|
|
LogPrint ("Publishing our RouterInfo to ", floodfill->GetIdentHashAbbreviation ());
|
|
|
|
transports.SendMessage (floodfill->GetIdentHash (), CreateDatabaseStoreMsg ());
|
|
|
|
excluded.insert (floodfill->GetIdentHash ());
|
|
|
|
}
|
2014-02-13 07:02:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 18:53:44 +04:00
|
|
|
RequestedDestination * NetDb::CreateRequestedDestination (const IdentHash& dest,
|
2014-08-21 05:47:28 +04:00
|
|
|
bool isLeaseSet, bool isExploratory, i2p::tunnel::TunnelPool * pool)
|
2014-01-05 18:53:44 +04:00
|
|
|
{
|
2014-09-22 06:00:42 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
|
2014-01-05 18:53:44 +04:00
|
|
|
auto it = m_RequestedDestinations.find (dest);
|
|
|
|
if (it == m_RequestedDestinations.end ()) // not exist yet
|
|
|
|
{
|
2014-08-21 05:47:28 +04:00
|
|
|
RequestedDestination * d = new RequestedDestination (dest, isLeaseSet, isExploratory, pool);
|
2014-01-05 18:53:44 +04:00
|
|
|
m_RequestedDestinations[dest] = d;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:59:43 +04:00
|
|
|
bool NetDb::DeleteRequestedDestination (const IdentHash& dest)
|
2014-01-05 18:53:44 +04:00
|
|
|
{
|
|
|
|
auto it = m_RequestedDestinations.find (dest);
|
|
|
|
if (it != m_RequestedDestinations.end ())
|
|
|
|
{
|
2014-09-13 20:39:02 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
|
2014-09-22 06:00:42 +04:00
|
|
|
delete it->second;
|
2014-01-25 06:14:14 +04:00
|
|
|
m_RequestedDestinations.erase (it);
|
2014-07-31 20:59:43 +04:00
|
|
|
return true;
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
2014-07-31 20:59:43 +04:00
|
|
|
return false;
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
2014-02-12 07:19:51 +04:00
|
|
|
|
|
|
|
void NetDb::DeleteRequestedDestination (RequestedDestination * dest)
|
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
{
|
2014-09-13 20:39:02 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
|
2014-02-12 07:19:51 +04:00
|
|
|
m_RequestedDestinations.erase (dest->GetDestination ());
|
|
|
|
delete dest;
|
|
|
|
}
|
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
|
2014-11-21 00:20:02 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter () const
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
|
|
|
return GetRandomRouter (
|
2014-11-21 00:20:02 +03:00
|
|
|
[](std::shared_ptr<const RouterInfo> router)->bool
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
|
|
|
return !router->IsHidden ();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-21 18:46:11 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const
|
2014-09-25 05:45:19 +04:00
|
|
|
{
|
2014-09-26 05:08:20 +04:00
|
|
|
return GetRandomRouter (
|
2014-11-21 00:20:02 +03:00
|
|
|
[compatibleWith](std::shared_ptr<const RouterInfo> router)->bool
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
return !router->IsHidden () && router != compatibleWith &&
|
2014-10-02 22:43:42 +04:00
|
|
|
router->IsCompatible (*compatibleWith);
|
2014-09-26 05:08:20 +04:00
|
|
|
});
|
2014-09-25 05:45:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-21 18:46:11 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
|
|
|
return GetRandomRouter (
|
2014-11-21 00:20:02 +03:00
|
|
|
[compatibleWith](std::shared_ptr<const RouterInfo> router)->bool
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
return !router->IsHidden () && router != compatibleWith &&
|
2014-10-02 22:43:42 +04:00
|
|
|
router->IsCompatible (*compatibleWith) && (router->GetCaps () & RouterInfo::eHighBandwidth);
|
2014-09-26 05:08:20 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-25 05:45:19 +04:00
|
|
|
template<typename Filter>
|
2014-11-21 00:20:02 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (Filter filter) const
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2013-11-19 05:37:38 +04:00
|
|
|
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
|
2014-01-26 06:47:01 +04:00
|
|
|
uint32_t ind = rnd.GenerateWord32 (0, m_RouterInfos.size () - 1);
|
|
|
|
for (int j = 0; j < 2; j++)
|
2013-11-19 05:37:38 +04:00
|
|
|
{
|
2014-01-26 06:47:01 +04:00
|
|
|
uint32_t i = 0;
|
2014-11-21 21:29:19 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
|
2014-01-26 06:47:01 +04:00
|
|
|
for (auto it: m_RouterInfos)
|
|
|
|
{
|
|
|
|
if (i >= ind)
|
|
|
|
{
|
2014-11-21 00:20:02 +03:00
|
|
|
if (!it.second->IsUnreachable () && filter (it.second))
|
|
|
|
return it.second;
|
2014-01-26 06:47:01 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// we couldn't find anything, try second pass
|
|
|
|
ind = 0;
|
2013-11-19 05:37:38 +04:00
|
|
|
}
|
2014-03-19 22:08:09 +04:00
|
|
|
return nullptr; // seems we have too few routers
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
2014-09-25 05:45:19 +04:00
|
|
|
|
2013-11-23 05:41:28 +04:00
|
|
|
void NetDb::PostI2NPMsg (I2NPMessage * msg)
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
|
|
|
if (msg) m_Queue.Put (msg);
|
|
|
|
}
|
2014-01-04 06:24:20 +04:00
|
|
|
|
2014-11-21 19:37:17 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetClosestFloodfill (const IdentHash& destination,
|
2014-01-22 16:44:41 +04:00
|
|
|
const std::set<IdentHash>& excluded) const
|
2014-01-04 06:24:20 +04:00
|
|
|
{
|
2014-11-21 19:37:17 +03:00
|
|
|
std::shared_ptr<const RouterInfo> r;
|
2014-01-04 06:24:20 +04:00
|
|
|
XORMetric minMetric;
|
2014-10-14 18:41:40 +04:00
|
|
|
IdentHash destKey = CreateRoutingKey (destination);
|
2014-01-04 06:24:20 +04:00
|
|
|
minMetric.SetMax ();
|
2014-10-06 05:59:05 +04:00
|
|
|
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
|
2014-03-19 22:08:09 +04:00
|
|
|
for (auto it: m_Floodfills)
|
2014-01-04 06:24:20 +04:00
|
|
|
{
|
2014-03-19 22:08:09 +04:00
|
|
|
if (!it->IsUnreachable () && !excluded.count (it->GetIdentHash ()))
|
2014-01-04 06:24:20 +04:00
|
|
|
{
|
2014-10-14 18:41:40 +04:00
|
|
|
XORMetric m = destKey ^ it->GetIdentHash ();
|
2014-01-04 06:24:20 +04:00
|
|
|
if (m < minMetric)
|
|
|
|
{
|
|
|
|
minMetric = m;
|
2014-11-21 19:37:17 +03:00
|
|
|
r = it;
|
2014-01-04 06:24:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2014-01-23 00:32:50 +04:00
|
|
|
|
2014-07-31 20:59:43 +04:00
|
|
|
void NetDb::ManageLeaseSets ()
|
|
|
|
{
|
|
|
|
for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();)
|
|
|
|
{
|
2014-10-14 01:45:07 +04:00
|
|
|
if (it->second->HasNonExpiredLeases ()) // all leases expired
|
2014-07-31 20:59:43 +04:00
|
|
|
{
|
|
|
|
LogPrint ("LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
|
|
|
|
delete it->second;
|
|
|
|
it = m_LeaseSets.erase (it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
}
|