i2pd/NetDb.cpp

743 lines
21 KiB
C++
Raw Normal View History

#include "I2PEndian.h"
2013-11-20 12:46:09 +00:00
#include <fstream>
2014-01-21 00:12:59 +00:00
#include <vector>
2014-01-22 20:32:50 +00:00
#include <boost/asio.hpp>
#include <cryptopp/gzip.h>
#include "base64.h"
2013-11-13 12:59:21 +00:00
#include "Log.h"
2013-11-20 12:46:09 +00:00
#include "Timestamp.h"
2013-11-13 12:59:21 +00:00
#include "I2NPProtocol.h"
#include "Tunnel.h"
#include "Transports.h"
2013-11-19 01:37:38 +00:00
#include "RouterContext.h"
2014-01-03 02:22:48 +00:00
#include "Garlic.h"
2013-11-13 12:59:21 +00:00
#include "NetDb.h"
#include "Reseed.h"
2014-02-01 15:10:15 +00:00
#include "util.h"
2013-11-13 12:59:21 +00:00
namespace i2p
{
namespace data
{
2014-01-05 14:53:44 +00:00
I2NPMessage * RequestedDestination::CreateRequestMessage (const RouterInfo * router,
const i2p::tunnel::InboundTunnel * replyTunnel)
{
I2NPMessage * msg = i2p::CreateDatabaseLookupMsg (m_Destination,
2014-07-08 11:45:16 +00:00
replyTunnel->GetNextIdentHash (), replyTunnel->GetNextTunnelID (), m_IsExploratory, &m_ExcludedPeers, m_IsLeaseSet);
2014-01-05 14:53:44 +00:00
if (m_IsLeaseSet) // wrap lookup message into garlic
msg = i2p::garlic::routing.WrapSingleMessage (*router, msg);
2014-01-05 14:53:44 +00:00
m_ExcludedPeers.insert (router->GetIdentHash ());
m_LastRouter = router;
2014-07-07 19:35:42 +00:00
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
2014-01-05 14:53:44 +00:00
return msg;
}
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 19:35:42 +00:00
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
return msg;
}
2014-02-01 20:57:46 +00:00
void RequestedDestination::ClearExcludedPeers ()
{
m_ExcludedPeers.clear ();
}
2014-02-01 20:57:46 +00:00
#ifndef _WIN32
const char NetDb::m_NetDbPath[] = "/netDb";
#else
const char NetDb::m_NetDbPath[] = "\\netDb";
#endif
2013-11-13 12:59:21 +00:00
NetDb netdb;
2014-01-31 12:32:34 +00:00
NetDb::NetDb (): m_IsRunning (false), m_ReseedRetries (0), m_Thread (0)
2013-11-13 12:59:21 +00:00
{
}
NetDb::~NetDb ()
{
2014-03-19 18:08:09 +00:00
Stop ();
2013-11-13 12:59:21 +00:00
for (auto l:m_LeaseSets)
delete l.second;
for (auto r:m_RouterInfos)
delete r.second;
2014-01-05 14:53:44 +00:00
for (auto r:m_RequestedDestinations)
delete r.second;
2013-11-13 12:59:21 +00:00
}
2013-11-19 01:37:38 +00:00
void NetDb::Start ()
2014-02-01 20:57:46 +00:00
{
Load (m_NetDbPath);
2014-01-31 18:08:33 +00:00
while (m_RouterInfos.size () < 100 && m_ReseedRetries < 10)
{
Reseeder reseeder;
reseeder.reseedNow();
m_ReseedRetries++;
2014-02-01 20:57:46 +00:00
Load (m_NetDbPath);
2014-01-31 18:08:33 +00:00
}
2013-11-19 01:37:38 +00:00
m_Thread = new std::thread (std::bind (&NetDb::Run, this));
}
void NetDb::Stop ()
{
if (m_Thread)
{
m_IsRunning = false;
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
}
void NetDb::Run ()
{
2014-07-03 18:47:12 +00:00
uint32_t lastSave = 0, lastPublish = 0, lastKeyspaceRotation = 0;
2013-11-19 01:37:38 +00:00
m_IsRunning = true;
while (m_IsRunning)
{
try
2013-11-20 12:46:09 +00:00
{
I2NPMessage * msg = m_Queue.GetNextWithTimeout (10000); // 10 sec
if (msg)
{
while (msg)
2013-11-20 12:46:09 +00:00
{
if (msg->GetHeader ()->typeID == eI2NPDatabaseStore)
{
HandleDatabaseStoreMsg (msg->GetPayload (), msg->GetLength ()); // TODO
i2p::DeleteI2NPMessage (msg);
}
else if (msg->GetHeader ()->typeID == eI2NPDatabaseSearchReply)
HandleDatabaseSearchReplyMsg (msg);
else // WTF?
{
LogPrint ("NetDb: unexpected message type ", msg->GetHeader ()->typeID);
i2p::HandleI2NPMessage (msg);
}
msg = m_Queue.Get ();
2013-11-20 12:46:09 +00:00
}
}
else // if no new DatabaseStore coming, explore it
2014-06-17 02:30:34 +00:00
{
auto numRouters = m_RouterInfos.size ();
Explore (numRouters < 1500 ? 5 : 1);
}
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
2014-02-14 21:10:25 +00:00
if (ts - lastSave >= 60) // save routers and validate subscriptions every minute
{
2014-02-13 03:02:39 +00:00
if (lastSave)
2014-02-14 21:10:25 +00:00
{
2014-02-01 20:57:46 +00:00
SaveUpdated (m_NetDbPath);
2014-02-14 21:10:25 +00:00
ValidateSubscriptions ();
}
2014-02-13 03:02:39 +00:00
lastSave = ts;
}
if (ts - lastPublish >= 600) // publish every 10 minutes
{
Publish ();
lastPublish = ts;
2013-11-20 12:46:09 +00:00
}
2014-07-03 18:47:12 +00:00
if (ts % 86400 < 60 && ts - lastKeyspaceRotation >= 60) // wihhin 1 minutes since midnight (86400 = 24*3600)
{
KeyspaceRotation ();
lastKeyspaceRotation = ts;
}
2013-11-20 12:46:09 +00:00
}
catch (std::exception& ex)
2013-11-20 12:46:09 +00:00
{
LogPrint ("NetDb: ", ex.what ());
2013-11-20 12:46:09 +00:00
}
2013-11-19 01:37:38 +00:00
}
}
2013-11-13 12:59:21 +00:00
2014-07-22 00:14:11 +00:00
void NetDb::AddRouterInfo (const IdentHash& ident, uint8_t * buf, int len)
{
DeleteRequestedDestination (ident);
auto it = m_RouterInfos.find(ident);
if (it != m_RouterInfos.end ())
{
2014-07-22 00:14:11 +00:00
auto ts = it->second->GetTimestamp ();
it->second->Update (buf, len);
if (it->second->GetTimestamp () > ts)
LogPrint ("RouterInfo updated");
}
else
{
LogPrint ("New RouterInfo added");
2014-07-22 00:14:11 +00:00
RouterInfo * r = new RouterInfo (buf, len);
2013-11-29 12:52:09 +00:00
m_RouterInfos[r->GetIdentHash ()] = r;
2014-03-19 18:08:09 +00:00
if (r->IsFloodfill ())
m_Floodfills.push_back (r);
}
2013-11-13 12:59:21 +00:00
}
2014-07-22 00:14:11 +00:00
void NetDb::AddLeaseSet (const IdentHash& ident, uint8_t * buf, int len)
2013-11-13 12:59:21 +00:00
{
2014-07-22 00:14:11 +00:00
DeleteRequestedDestination (ident);
auto it = m_LeaseSets.find(ident);
2014-01-13 03:31:26 +00:00
if (it != m_LeaseSets.end ())
{
2014-07-22 00:14:11 +00:00
it->second->Update (buf, len);
2014-01-13 03:31:26 +00:00
LogPrint ("LeaseSet updated");
}
else
{
LogPrint ("New LeaseSet added");
2014-07-22 00:14:11 +00:00
m_LeaseSets[ident] = new LeaseSet (buf, len);
2014-01-13 03:31:26 +00:00
}
2013-11-13 12:59:21 +00:00
}
2013-11-29 12:52:09 +00:00
RouterInfo * NetDb::FindRouter (const IdentHash& ident) const
2013-11-13 12:59:21 +00:00
{
2013-11-29 12:52:09 +00:00
auto it = m_RouterInfos.find (ident);
2013-11-13 12:59:21 +00:00
if (it != m_RouterInfos.end ())
return it->second;
else
return nullptr;
}
2013-12-22 16:29:57 +00: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 18:36:20 +00:00
}
// TODO: Move to reseed and/or scheduled tasks. (In java version, scheduler fix this as well as sort RIs.)
2014-02-01 15:10:15 +00:00
bool NetDb::CreateNetDb(boost::filesystem::path directory)
2014-01-29 18:36:20 +00:00
{
2014-02-01 15:10:15 +00:00
LogPrint (directory.string(), " doesn't exist, trying to create it.");
if (!boost::filesystem::create_directory (directory))
2014-01-29 18:36:20 +00:00
{
2014-02-01 15:10:15 +00:00
LogPrint("Failed to create directory ", directory.string());
2014-01-29 18:36:20 +00:00
return false;
}
2014-01-29 22:38:11 +00:00
// list of chars might appear in base64 string
2014-01-29 22:43:20 +00:00
const char * chars = GetBase64SubstitutionTable (); // 64 bytes
2014-01-29 18:36:20 +00:00
boost::filesystem::path suffix;
2014-01-29 22:43:20 +00:00
for (int i = 0; i < 64; i++)
2014-01-29 18:36:20 +00:00
{
#ifndef _WIN32
2014-01-29 22:43:20 +00:00
suffix = std::string ("/r") + chars[i];
2014-01-29 18:36:20 +00:00
#else
2014-01-29 22:43:20 +00:00
suffix = std::string ("\\r") + chars[i];
2014-01-29 18:36:20 +00:00
#endif
2014-02-01 15:10:15 +00:00
if (!boost::filesystem::create_directory( boost::filesystem::path (directory / suffix) )) return false;
2014-01-29 18:36:20 +00:00
}
return true;
}
2014-01-31 18:08:33 +00:00
void NetDb::Load (const char * directory)
{
2014-02-01 15:10:15 +00:00
boost::filesystem::path p (i2p::util::filesystem::GetDataDir());
p /= (directory);
2014-01-29 18:36:20 +00:00
if (!boost::filesystem::exists (p))
{
2014-01-31 18:08:33 +00:00
// seems netDb doesn't exist yet
2014-02-01 15:10:15 +00:00
if (!CreateNetDb(p)) return;
2014-01-29 18:36:20 +00:00
}
2014-01-31 18:08:33 +00:00
// make sure we cleanup netDb from previous attempts
for (auto r: m_RouterInfos)
delete r.second;
m_RouterInfos.clear ();
2014-03-19 18:08:09 +00:00
m_Floodfills.clear ();
2014-01-31 18:08:33 +00:00
// load routers now
2014-01-29 18:36:20 +00:00
int numRouters = 0;
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator it (p); it != end; ++it)
2013-11-13 12:59:21 +00:00
{
2014-01-29 18:36:20 +00:00
if (boost::filesystem::is_directory (it->status()))
2013-11-13 12:59:21 +00:00
{
2014-01-29 18:36:20 +00:00
for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1)
2013-11-13 12:59:21 +00:00
{
2014-01-09 22:55:53 +00:00
#if BOOST_VERSION > 10500
2014-01-29 18:36:20 +00:00
RouterInfo * r = new RouterInfo (it1->path().string().c_str ());
2014-01-09 21:51:41 +00:00
#else
2014-01-29 18:36:20 +00:00
RouterInfo * r = new RouterInfo(it1->path().c_str());
2014-01-09 21:51:41 +00:00
#endif
r->DeleteBuffer ();
2014-01-29 18:36:20 +00:00
m_RouterInfos[r->GetIdentHash ()] = r;
2014-03-19 18:08:09 +00:00
if (r->IsFloodfill ())
m_Floodfills.push_back (r);
2014-01-29 18:36:20 +00:00
numRouters++;
2013-11-13 12:59:21 +00:00
}
}
}
2014-01-29 18:36:20 +00:00
LogPrint (numRouters, " routers loaded");
2014-03-19 18:08:09 +00:00
LogPrint (m_Floodfills.size (), " floodfills loaded");
2013-11-13 12:59:21 +00:00
}
2013-11-20 12:46:09 +00:00
void NetDb::SaveUpdated (const char * directory)
{
2013-12-22 16:29:57 +00:00
auto GetFilePath = [](const char * directory, const RouterInfo * routerInfo)
{
2014-01-29 18:36:20 +00:00
#ifndef _WIN32
2013-12-22 16:29:57 +00:00
return std::string (directory) + "/r" +
2014-01-29 18:36:20 +00:00
routerInfo->GetIdentHashBase64 ()[0] + "/routerInfo-" +
#else
return std::string (directory) + "\\r" +
routerInfo->GetIdentHashBase64 ()[0] + "\\routerInfo-" +
#endif
2013-12-22 16:29:57 +00:00
routerInfo->GetIdentHashBase64 () + ".dat";
};
2014-02-01 20:57:46 +00: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 16:29:57 +00:00
int count = 0, deletedCount = 0;
2014-01-23 01:19:39 +00:00
auto total = m_RouterInfos.size ();
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
2013-11-20 12:46:09 +00:00
for (auto it: m_RouterInfos)
2013-12-22 16:29:57 +00:00
{
2013-11-20 12:46:09 +00:00
if (it.second->IsUpdated ())
{
2014-07-10 19:33:42 +00:00
it.second->SaveToFile (GetFilePath(fullDirectory, it.second));
2013-11-20 12:46:09 +00:00
it.second->SetUpdated (false);
it.second->DeleteBuffer ();
2013-11-20 12:46:09 +00:00
count++;
}
2014-01-23 01:19:39 +00:00
else
2013-12-22 16:29:57 +00:00
{
2014-01-23 01:19:39 +00:00
// RouterInfo expires in 72 hours if more than 300
if (total > 300 && ts > it.second->GetTimestamp () + 3*24*3600*1000LL) // 3 days
{
total--;
2014-01-23 01:19:39 +00:00
it.second->SetUnreachable (true);
}
2014-01-23 01:19:39 +00:00
if (it.second->IsUnreachable ())
{
2014-02-01 20:57:46 +00:00
if (boost::filesystem::exists (GetFilePath (fullDirectory, it.second)))
2014-01-23 01:19:39 +00:00
{
2014-02-01 20:57:46 +00:00
boost::filesystem::remove (GetFilePath (fullDirectory, it.second));
2014-01-23 01:19:39 +00:00
deletedCount++;
}
}
2013-12-22 16:29:57 +00:00
}
}
2013-11-20 12:46:09 +00:00
if (count > 0)
LogPrint (count," new/updated routers saved");
2013-12-22 16:29:57 +00:00
if (deletedCount > 0)
LogPrint (deletedCount," routers deleted");
2013-11-20 12:46:09 +00:00
}
2014-01-03 02:22:48 +00:00
void NetDb::RequestDestination (const IdentHash& destination, bool isLeaseSet)
{
if (isLeaseSet) // we request LeaseSet through tunnels
{
i2p::tunnel::OutboundTunnel * outbound = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
if (outbound)
2013-11-13 12:59:21 +00:00
{
i2p::tunnel::InboundTunnel * inbound = i2p::tunnel::tunnels.GetNextInboundTunnel ();
if (inbound)
{
RequestedDestination * dest = CreateRequestedDestination (destination, isLeaseSet);
2014-03-22 20:05:58 +00:00
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
// request 3 closests floodfills
for (int i = 0; i < 3; i++)
{
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
if (floodfill)
{
// DatabaseLookup message
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
floodfill->GetIdentHash (), 0,
dest->CreateRequestMessage (floodfill, inbound)
});
}
}
if (msgs.size () > 0)
{
dest->ClearExcludedPeers ();
outbound->SendTunnelDataMsg (msgs);
}
else
LogPrint ("No more floodfills found");
}
else
LogPrint ("No inbound tunnels found");
}
2013-11-13 12:59:21 +00:00
else
LogPrint ("No outbound tunnels found");
}
else // RouterInfo is requested directly
{
RequestedDestination * dest = CreateRequestedDestination (destination, false);
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
if (floodfill)
i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
}
2013-11-13 12:59:21 +00:00
}
2013-12-22 16:29:57 +00:00
void NetDb::HandleDatabaseStoreMsg (uint8_t * buf, size_t len)
{
I2NPDatabaseStoreMsg * msg = (I2NPDatabaseStoreMsg *)buf;
size_t offset = sizeof (I2NPDatabaseStoreMsg);
if (msg->replyToken)
offset += 36;
if (msg->type)
2013-11-19 01:37:38 +00:00
{
LogPrint ("LeaseSet");
2014-07-22 00:14:11 +00:00
AddLeaseSet (msg->key, buf + offset, len - offset);
}
else
{
LogPrint ("RouterInfo");
size_t size = be16toh (*(uint16_t *)(buf + offset));
if (size > 2048)
{
LogPrint ("Invalid RouterInfo length ", (int)size);
return;
2013-11-20 12:46:09 +00:00
}
offset += 2;
CryptoPP::Gunzip decompressor;
decompressor.Put (buf + offset, size);
decompressor.MessageEnd();
uint8_t uncompressed[2048];
size_t uncomressedSize = decompressor.MaxRetrievable ();
decompressor.Get (uncompressed, uncomressedSize);
2014-07-22 00:14:11 +00:00
AddRouterInfo (msg->key, uncompressed, uncomressedSize);
}
2013-11-19 01:37:38 +00: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 14:53:44 +00:00
auto it = m_RequestedDestinations.find (IdentHash (buf));
if (it != m_RequestedDestinations.end ())
{
RequestedDestination * dest = it->second;
bool deleteDest = true;
2014-01-05 14:53:44 +00:00
if (num > 0)
{
2014-07-10 01:43:33 +00:00
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
2014-01-21 00:12:59 +00:00
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
2014-01-05 14:53:44 +00: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);
2014-01-05 14:53:44 +00:00
if (dest->IsExploratory ())
{
2014-04-06 01:25:54 +00:00
auto r = FindRouter (router);
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
2014-01-05 14:53:44 +00:00
{
2014-04-06 01:25:54 +00:00
// router with ident not found or too old (1 hour)
LogPrint ("Found new/outdated router. Requesting RouterInfo ...");
2014-07-10 01:43:33 +00:00
if (outbound && inbound && dest->GetLastRouter ())
2014-01-05 14:53:44 +00:00
{
RequestedDestination * d1 = CreateRequestedDestination (router, false, false);
2014-07-10 01:43:33 +00:00
auto msg = d1->CreateRequestMessage (dest->GetLastRouter (), inbound);
2014-01-21 00:12:59 +00:00
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
dest->GetLastRouter ()->GetIdentHash (), 0, msg
});
2014-01-05 14:53:44 +00:00
}
}
else
LogPrint ("Bayan");
}
else
{
2014-01-05 14:53:44 +00:00
// reply to our destination. Try other floodfills
2014-07-10 01:43:33 +00:00
if (outbound && inbound && dest->GetLastRouter ())
{
2014-01-05 14:53:44 +00:00
auto r = FindRouter (router);
// do we have that floodfill router in our database?
2014-02-12 03:19:51 +00:00
if (r)
2014-01-05 14:53:44 +00:00
{
2014-02-12 03:19:51 +00:00
// we do
2014-01-19 15:05:54 +00:00
if (!dest->IsExcluded (r->GetIdentHash ()) && dest->GetNumExcludedPeers () < 30) // TODO: fix TunnelGateway first
2014-01-05 14:53:44 +00:00
{
LogPrint ("Try ", key, " at floodfill ", peerHash);
2014-02-12 03:19:51 +00:00
// tell floodfill about us
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
r->GetIdentHash (), 0,
CreateDatabaseStoreMsg ()
});
2014-01-05 14:53:44 +00:00
// request destination
2014-07-10 01:43:33 +00:00
auto msg = dest->CreateRequestMessage (r, inbound);
2014-01-21 00:12:59 +00:00
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
r->GetIdentHash (), 0, msg
});
deleteDest = false;
2014-01-05 14:53:44 +00:00
}
}
else
{
// request router
LogPrint ("Found new floodfill. Request it");
RequestedDestination * d2 = CreateRequestedDestination (router, false, false);
I2NPMessage * msg = d2->CreateRequestMessage (dest->GetLastRouter (), inbound);
2014-01-21 00:12:59 +00:00
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
dest->GetLastRouter ()->GetIdentHash (), 0, msg
});
2014-01-05 14:53:44 +00:00
}
}
else // we should send directly
{
if (!dest->IsLeaseSet ()) // if not LeaseSet
{
if (!dest->IsExcluded (router) && dest->GetNumExcludedPeers () < 30)
{
LogPrint ("Try ", key, " at floodfill ", peerHash, " directly");
i2p::transports.SendMessage (router, dest->CreateRequestMessage (router));
deleteDest = false;
}
}
else
LogPrint ("Can't request LeaseSet");
}
}
2014-01-05 14:53:44 +00:00
}
if (outbound && msgs.size () > 0)
2014-01-21 00:12:59 +00:00
outbound->SendTunnelDataMsg (msgs);
if (deleteDest)
{
// no more requests for tha destinationation. delete it
delete it->second;
m_RequestedDestinations.erase (it);
}
2014-01-05 14:53:44 +00:00
}
else
{
// no more requests for detination possible. delete it
delete it->second;
2014-01-25 02:14:14 +00:00
m_RequestedDestinations.erase (it);
2014-01-05 14:53:44 +00:00
}
}
else
LogPrint ("Requested destination for ", key, " not found");
i2p::DeleteI2NPMessage (msg);
}
2014-06-17 02:30:34 +00:00
void NetDb::Explore (int numDestinations)
{
2014-07-03 17:41:36 +00:00
// clean up previous exploratories
2014-07-07 19:35:42 +00:00
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
2014-07-03 17:41:36 +00:00
for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();)
{
2014-07-07 19:35:42 +00:00
if (it->second->IsExploratory () || ts > it->second->GetCreationTime () + 60) // no response for 1 minute
2014-07-03 17:41:36 +00:00
{
delete it->second;
it = m_RequestedDestinations.erase (it);
}
else
it++;
}
// new requests
2014-06-17 02:30:34 +00:00
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
bool throughTunnels = outbound && inbound;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
uint8_t randomHash[32];
2014-06-17 02:30:34 +00: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 02:30:34 +00:00
rnd.GenerateBlock (randomHash, 32);
RequestedDestination * dest = CreateRequestedDestination (IdentHash (randomHash), false, true);
auto floodfill = GetClosestFloodfill (randomHash, dest->GetExcludedPeers ());
if (floodfill && !floodfills.count (floodfill)) // request floodfill only once
2014-02-12 03:19:51 +00:00
{
2014-06-17 02:30:34 +00:00
floodfills.insert (floodfill);
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
i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
2013-11-19 01:37:38 +00:00
}
2014-02-12 03:19:51 +00:00
else
2014-06-17 02:30:34 +00:00
DeleteRequestedDestination (dest);
}
2014-06-17 02:30:34 +00:00
if (throughTunnels && msgs.size () > 0)
outbound->SendTunnelDataMsg (msgs);
2013-11-19 01:37:38 +00:00
}
2014-02-13 03:02:39 +00:00
void NetDb::Publish ()
{
std::set<IdentHash> excluded; // TODO: fill up later
2014-03-16 12:00:34 +00: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 03:02:39 +00:00
}
}
2014-01-05 14:53:44 +00:00
RequestedDestination * NetDb::CreateRequestedDestination (const IdentHash& dest,
bool isLeaseSet, bool isExploratory)
{
auto it = m_RequestedDestinations.find (dest);
if (it == m_RequestedDestinations.end ()) // not exist yet
{
RequestedDestination * d = new RequestedDestination (dest, isLeaseSet, isExploratory);
m_RequestedDestinations[dest] = d;
return d;
}
else
return it->second;
}
void NetDb::DeleteRequestedDestination (const IdentHash& dest)
{
auto it = m_RequestedDestinations.find (dest);
if (it != m_RequestedDestinations.end ())
{
delete it->second;
2014-01-25 02:14:14 +00:00
m_RequestedDestinations.erase (it);
2014-01-05 14:53:44 +00:00
}
}
2014-02-12 03:19:51 +00:00
void NetDb::DeleteRequestedDestination (RequestedDestination * dest)
{
if (dest)
{
m_RequestedDestinations.erase (dest->GetDestination ());
delete dest;
}
}
2013-11-19 01:37:38 +00:00
2014-06-18 14:41:59 +00:00
const RouterInfo * NetDb::GetRandomRouter (const RouterInfo * compatibleWith) const
2013-11-13 12:59:21 +00:00
{
2013-11-19 01:37:38 +00:00
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator ();
uint32_t ind = rnd.GenerateWord32 (0, m_RouterInfos.size () - 1);
for (int j = 0; j < 2; j++)
2013-11-19 01:37:38 +00:00
{
uint32_t i = 0;
for (auto it: m_RouterInfos)
{
if (i >= ind)
{
2014-06-18 14:41:59 +00:00
if (!it.second->IsUnreachable () && !it.second->IsHidden () &&
(!compatibleWith || it.second->IsCompatible (*compatibleWith)))
return it.second;
}
else
i++;
}
// we couldn't find anything, try second pass
ind = 0;
2013-11-19 01:37:38 +00:00
}
2014-03-19 18:08:09 +00:00
return nullptr; // seems we have too few routers
2013-11-13 12:59:21 +00:00
}
2013-11-20 12:46:09 +00:00
void NetDb::PostI2NPMsg (I2NPMessage * msg)
2013-11-20 12:46:09 +00:00
{
if (msg) m_Queue.Put (msg);
}
2014-01-04 02:24:20 +00:00
const RouterInfo * NetDb::GetClosestFloodfill (const IdentHash& destination,
const std::set<IdentHash>& excluded) const
2014-01-04 02:24:20 +00:00
{
RouterInfo * r = nullptr;
XORMetric minMetric;
RoutingKey destKey = CreateRoutingKey (destination);
minMetric.SetMax ();
2014-03-19 18:08:09 +00:00
for (auto it: m_Floodfills)
2014-01-04 02:24:20 +00:00
{
2014-03-19 18:08:09 +00:00
if (!it->IsUnreachable () && !excluded.count (it->GetIdentHash ()))
2014-01-04 02:24:20 +00:00
{
2014-03-19 18:08:09 +00:00
XORMetric m = destKey ^ it->GetRoutingKey ();
2014-01-04 02:24:20 +00:00
if (m < minMetric)
{
minMetric = m;
2014-03-19 18:08:09 +00:00
r = it;
2014-01-04 02:24:20 +00:00
}
}
}
return r;
}
2014-01-22 20:32:50 +00:00
2014-02-14 21:10:25 +00:00
void NetDb::Subscribe (const IdentHash& ident)
{
LeaseSet * leaseSet = FindLeaseSet (ident);
if (!leaseSet)
{
LogPrint ("LeaseSet requested");
RequestDestination (ident, true);
}
m_Subscriptions.insert (ident);
}
void NetDb::Unsubscribe (const IdentHash& ident)
{
m_Subscriptions.erase (ident);
}
void NetDb::ValidateSubscriptions ()
{
for (auto it : m_Subscriptions)
{
LeaseSet * leaseSet = FindLeaseSet (it);
if (!leaseSet || leaseSet->HasExpiredLeases ())
{
LogPrint ("LeaseSet re-requested");
RequestDestination (it, true);
}
}
}
2014-07-03 18:47:12 +00:00
void NetDb::KeyspaceRotation ()
{
for (auto it: m_RouterInfos)
it.second->UpdateRoutingKey ();
LogPrint ("Keyspace rotation complete");
Publish ();
}
2013-11-13 12:59:21 +00:00
}
}