i2pd/NetDb.cpp

985 lines
27 KiB
C++
Raw Normal View History

2014-12-31 14:14:53 +00:00
#include <string.h>
#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>
2015-11-03 14:15:49 +00:00
#include <openssl/rand.h>
#include <zlib.h>
#include "Base.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"
2014-02-01 15:10:15 +00:00
#include "util.h"
2013-11-13 12:59:21 +00:00
using namespace i2p::transport;
2013-11-13 12:59:21 +00:00
namespace i2p
{
namespace data
{
const char NetDb::m_NetDbPath[] = "netDb";
2013-11-13 12:59:21 +00:00
NetDb netdb;
2015-01-19 18:57:37 +00:00
NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr)
2013-11-13 12:59:21 +00:00
{
}
NetDb::~NetDb ()
{
2014-03-19 18:08:09 +00:00
Stop ();
2015-01-19 18:57:37 +00:00
delete m_Reseeder;
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 ();
2015-04-15 11:30:37 +00:00
if (m_RouterInfos.size () < 25) // reseed if # of router less than 50
2015-01-19 18:57:37 +00:00
Reseed ();
2014-12-15 17:38:35 +00:00
2015-01-15 01:27:19 +00:00
m_IsRunning = true;
2013-11-19 01:37:38 +00:00
m_Thread = new std::thread (std::bind (&NetDb::Run, this));
}
void NetDb::Stop ()
{
2015-04-11 19:39:23 +00:00
if (m_IsRunning)
2013-11-19 01:37:38 +00:00
{
2015-04-11 19:39:23 +00:00
for (auto it: m_RouterInfos)
it.second->SaveProfile ();
DeleteObsoleteProfiles ();
m_RouterInfos.clear ();
m_Floodfills.clear ();
if (m_Thread)
{
m_IsRunning = false;
m_Queue.WakeUp ();
m_Thread->join ();
delete m_Thread;
m_Thread = 0;
}
m_LeaseSets.clear();
m_Requests.Stop ();
}
2013-11-19 01:37:38 +00:00
}
void NetDb::Run ()
{
uint32_t lastSave = 0, lastPublish = 0, lastExploratory = 0, lastManageRequest = 0;
2013-11-19 01:37:38 +00:00
while (m_IsRunning)
{
try
2013-11-20 12:46:09 +00:00
{
2015-06-16 17:32:42 +00:00
auto msg = m_Queue.GetNextWithTimeout (15000); // 15 sec
if (msg)
{
int numMsgs = 0;
2015-02-12 16:40:42 +00:00
while (msg)
2013-11-20 12:46:09 +00:00
{
2015-01-02 04:00:33 +00:00
switch (msg->GetTypeID ())
{
case eI2NPDatabaseStore:
LogPrint ("DatabaseStore");
2015-06-16 17:32:42 +00:00
HandleDatabaseStoreMsg (msg);
break;
case eI2NPDatabaseSearchReply:
LogPrint ("DatabaseSearchReply");
2015-06-16 17:32:42 +00:00
HandleDatabaseSearchReplyMsg (msg);
break;
case eI2NPDatabaseLookup:
LogPrint ("DatabaseLookup");
2015-06-16 17:32:42 +00:00
HandleDatabaseLookupMsg (msg);
break;
default: // WTF?
LogPrint (eLogError, "NetDb: unexpected message type ", msg->GetTypeID ());
2015-06-16 17:32:42 +00:00
//i2p::HandleI2NPMessage (msg);
}
2015-02-12 16:40:42 +00:00
if (numMsgs > 100) break;
msg = m_Queue.Get ();
numMsgs++;
2013-11-20 12:46:09 +00:00
}
}
if (!m_IsRunning) break;
uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts - lastManageRequest >= 15) // manage requests every 15 seconds
{
2015-04-09 16:45:00 +00:00
m_Requests.ManageRequests ();
lastManageRequest = ts;
}
2014-07-31 16:59:43 +00:00
if (ts - lastSave >= 60) // save routers, manage leasesets and validate subscriptions every minute
{
2014-02-13 03:02:39 +00:00
if (lastSave)
2014-02-14 21:10:25 +00:00
{
SaveUpdated ();
2014-07-31 16:59:43 +00:00
ManageLeaseSets ();
2014-02-14 21:10:25 +00:00
}
2014-02-13 03:02:39 +00:00
lastSave = ts;
}
if (ts - lastPublish >= 2400) // publish every 40 minutes
2014-02-13 03:02:39 +00:00
{
Publish ();
lastPublish = ts;
2013-11-20 12:46:09 +00:00
}
2015-01-10 21:08:13 +00:00
if (ts - lastExploratory >= 30) // exploratory every 30 seconds
{
auto numRouters = m_RouterInfos.size ();
if (numRouters < 2500 || ts - lastExploratory >= 90)
{
numRouters = 800/numRouters;
if (numRouters < 1) numRouters = 1;
if (numRouters > 9) numRouters = 9;
2015-04-09 16:45:00 +00:00
m_Requests.ManageRequests ();
2015-01-10 21:08:13 +00:00
Explore (numRouters);
lastExploratory = 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-12-11 20:41:04 +00:00
void NetDb::AddRouterInfo (const uint8_t * buf, int len)
{
IdentityEx identity;
2014-12-12 03:31:39 +00:00
if (identity.FromBuffer (buf, len))
AddRouterInfo (identity.GetIdentHash (), buf, len);
2014-12-11 20:41:04 +00:00
}
void NetDb::AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len)
2014-07-22 00:14:11 +00:00
{
2014-11-21 18:29:19 +00:00
auto r = FindRouter (ident);
if (r)
{
2014-11-21 18:29:19 +00:00
auto ts = r->GetTimestamp ();
r->Update (buf, len);
if (r->GetTimestamp () > ts)
LogPrint ("RouterInfo updated");
}
else
{
LogPrint ("New RouterInfo added");
2015-01-15 01:27:19 +00:00
r = std::make_shared<RouterInfo> (buf, len);
2014-11-21 18:29:19 +00:00
{
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
2015-01-14 21:11:09 +00:00
m_RouterInfos[r->GetIdentHash ()] = r;
2014-11-21 18:29:19 +00:00
}
2015-01-14 21:11:09 +00:00
if (r->IsFloodfill ())
2014-10-06 01:59:05 +00:00
{
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
2015-01-14 21:11:09 +00:00
m_Floodfills.push_back (r);
2014-10-06 01:59:05 +00:00
}
}
2015-01-14 21:11:09 +00:00
// take care about requested destination
2015-04-09 16:45:00 +00:00
m_Requests.RequestComplete (ident, r);
2013-11-13 12:59:21 +00:00
}
void NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len,
2015-02-05 23:53:43 +00:00
std::shared_ptr<i2p::tunnel::InboundTunnel> from)
2013-11-13 12:59:21 +00:00
{
if (!from) // unsolicited LS must be received directly
2014-01-13 03:31:26 +00:00
{
auto it = m_LeaseSets.find(ident);
if (it != m_LeaseSets.end ())
{
it->second->Update (buf, len);
2015-04-08 14:34:16 +00:00
if (it->second->IsValid ())
LogPrint (eLogInfo, "LeaseSet updated");
else
{
LogPrint (eLogInfo, "LeaseSet update failed");
m_LeaseSets.erase (it);
}
}
else
{
2015-04-08 14:34:16 +00:00
auto leaseSet = std::make_shared<LeaseSet> (buf, len);
if (leaseSet->IsValid ())
{
LogPrint (eLogInfo, "New LeaseSet added");
m_LeaseSets[ident] = leaseSet;
}
else
LogPrint (eLogError, "New LeaseSet validation failed");
}
2014-01-13 03:31:26 +00:00
}
2013-11-13 12:59:21 +00:00
}
std::shared_ptr<RouterInfo> NetDb::FindRouter (const IdentHash& ident) const
2013-11-13 12:59:21 +00:00
{
2014-11-21 18:29:19 +00:00
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
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;
2013-11-13 12:59:21 +00:00
else
return nullptr;
}
2013-12-22 16:29:57 +00:00
2015-01-27 16:27:58 +00:00
std::shared_ptr<LeaseSet> NetDb::FindLeaseSet (const IdentHash& destination) const
2013-12-22 16:29:57 +00:00
{
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
}
2015-11-03 14:15:49 +00:00
std::shared_ptr<RouterProfile> NetDb::FindRouterProfile (const IdentHash& ident) const
{
auto router = FindRouter (ident);
return router ? router->GetProfile () : nullptr;
}
2014-10-24 19:39:53 +00: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 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;
}
2015-01-19 18:57:37 +00:00
void NetDb::Reseed ()
{
if (!m_Reseeder)
{
m_Reseeder = new Reseeder ();
m_Reseeder->LoadCertificates (); // we need certificates for SU3 verification
}
int reseedRetries = 0;
while (reseedRetries < 10 && !m_Reseeder->ReseedNowSU3 ())
reseedRetries++;
if (reseedRetries >= 10)
LogPrint (eLogWarning, "Failed to reseed after 10 attempts");
}
void NetDb::Load ()
{
boost::filesystem::path p(i2p::util::filesystem::GetDataDir() / m_NetDbPath);
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
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-08-19 15:01:11 +00:00
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch ();
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-07-25 00:47:12 +00:00
const std::string& fullPath = it1->path().string();
2014-01-09 21:51:41 +00:00
#else
2014-07-25 00:47:12 +00:00
const std::string& fullPath = it1->path();
2014-01-09 21:51:41 +00:00
#endif
2014-11-18 16:08:10 +00:00
auto r = std::make_shared<RouterInfo>(fullPath);
2014-08-19 15:01:11 +00:00
if (!r->IsUnreachable () && (!r->UsesIntroducer () || ts < r->GetTimestamp () + 3600*1000LL)) // 1 hour
2014-07-25 00:47:12 +00:00
{
r->DeleteBuffer ();
2015-03-12 20:26:08 +00:00
r->ClearProperties (); // properties are not used for regular routers
2014-07-25 00:47:12 +00:00
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 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
}
void NetDb::SaveUpdated ()
2013-11-20 12:46:09 +00:00
{
auto GetFilePath = [](const boost::filesystem::path& directory, const RouterInfo * routerInfo)
2013-12-22 16:29:57 +00:00
{
std::string s(routerInfo->GetIdentHashBase64());
return directory / (std::string("r") + s[0]) / ("routerInfo-" + s + ".dat");
2013-12-22 16:29:57 +00:00
};
2014-02-01 20:57:46 +00:00
boost::filesystem::path fullDirectory (i2p::util::filesystem::GetDataDir() / m_NetDbPath);
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 ())
{
std::string f = GetFilePath(fullDirectory, it.second.get()).string();
it.second->SaveToFile (f);
2013-11-20 12:46:09 +00:00
it.second->SetUpdated (false);
2015-02-02 14:40:03 +00:00
it.second->SetUnreachable (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-08-19 15:01:11 +00:00
// RouterInfo expires after 1 hour if uses introducer
if (it.second->UsesIntroducer () && ts > it.second->GetTimestamp () + 3600*1000LL) // 1 hour
2014-01-23 01:19:39 +00:00
it.second->SetUnreachable (true);
2015-04-15 11:30:37 +00:00
else if (total > 75 && ts > (i2p::context.GetStartupTime () + 600)*1000LL) // routers don't expire if less than 25 or uptime is less than 10 minutes
{
if (i2p::context.IsFloodfill ())
{
if (ts > it.second->GetTimestamp () + 3600*1000LL) // 1 hours
2015-04-15 11:30:37 +00:00
{
it.second->SetUnreachable (true);
2015-04-15 11:30:37 +00:00
total--;
}
}
else if (total > 300)
{
if (ts > it.second->GetTimestamp () + 30*3600*1000LL) // 30 hours
2015-04-15 11:30:37 +00:00
{
it.second->SetUnreachable (true);
2015-04-15 11:30:37 +00:00
total--;
}
}
else if (total > 120)
{
if (ts > it.second->GetTimestamp () + 72*3600*1000LL) // 72 hours
2015-04-15 11:30:37 +00:00
{
it.second->SetUnreachable (true);
2015-04-15 11:30:37 +00:00
total--;
}
}
}
2014-01-23 01:19:39 +00:00
if (it.second->IsUnreachable ())
{
total--;
2014-11-21 18:02:46 +00:00
// delete RI file
2014-11-18 16:08:10 +00:00
if (boost::filesystem::exists (GetFilePath (fullDirectory, it.second.get ())))
2014-01-23 01:19:39 +00:00
{
2014-11-18 16:08:10 +00:00
boost::filesystem::remove (GetFilePath (fullDirectory, it.second.get ()));
2014-01-23 01:19:39 +00:00
deletedCount++;
}
2014-11-21 18:02:46 +00: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 01:19:39 +00:00
}
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)
2014-11-21 18:29:19 +00:00
{
2013-12-22 16:29:57 +00:00
LogPrint (deletedCount," routers deleted");
2014-11-21 18:29:19 +00: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->second->SaveProfile ();
2014-11-21 18:29:19 +00:00
it = m_RouterInfos.erase (it);
}
2014-11-21 18:29:19 +00:00
else
it++;
}
}
2013-11-20 12:46:09 +00:00
}
2015-01-14 21:11:09 +00:00
void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete)
2014-01-03 02:22:48 +00:00
{
2015-04-09 16:45:00 +00:00
auto dest = m_Requests.CreateRequest (destination, false, requestComplete); // non-exploratory
if (!dest)
2015-02-03 21:14:33 +00:00
{
2015-04-09 16:45:00 +00:00
LogPrint (eLogWarning, "Destination ", destination.ToBase64(), " is requested already");
return;
2015-02-10 03:19:29 +00:00
}
2015-04-09 16:45:00 +00:00
2015-04-10 23:49:58 +00:00
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
if (floodfill)
transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
else
{
LogPrint (eLogError, "No floodfills found");
2015-04-09 16:45:00 +00:00
m_Requests.RequestComplete (destination, nullptr);
}
2013-11-13 12:59:21 +00:00
}
2013-12-22 16:29:57 +00:00
2015-07-04 01:27:40 +00:00
void NetDb::HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> m)
{
const uint8_t * buf = m->GetPayload ();
2015-01-02 04:00:33 +00:00
size_t len = m->GetSize ();
2015-04-12 20:59:59 +00:00
IdentHash ident (buf + DATABASE_STORE_KEY_OFFSET);
if (ident.IsZero ())
{
LogPrint (eLogError, "Database store with zero ident. Dropped");
return;
}
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;
if (replyToken)
2015-01-30 20:13:09 +00:00
{
auto deliveryStatus = CreateDeliveryStatusMsg (replyToken);
2015-01-30 20:13:09 +00:00
uint32_t tunnelID = bufbe32toh (buf + offset);
offset += 4;
2015-01-30 20:13:09 +00:00
if (!tunnelID) // send response directly
2015-06-24 14:45:58 +00:00
transports.SendMessage (buf + offset, deliveryStatus);
2015-01-30 20:13:09 +00:00
else
{
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = pool ? pool->GetNextOutboundTunnel () : nullptr;
if (outbound)
2015-06-24 14:45:58 +00:00
outbound->SendTunnelDataMsg (buf + offset, tunnelID, deliveryStatus);
2015-01-30 20:13:09 +00:00
else
LogPrint (eLogError, "No outbound tunnels for DatabaseStore reply found");
}
offset += 32;
2015-01-30 20:13:09 +00:00
2015-02-01 23:30:27 +00:00
if (context.IsFloodfill ())
{
// flood it
2015-07-01 18:13:42 +00:00
auto floodMsg = ToSharedI2NPMessage (NewI2NPShortMessage ());
uint8_t * payload = floodMsg->GetPayload ();
memcpy (payload, buf, 33); // key + type
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0); // zero reply token
memcpy (payload + DATABASE_STORE_HEADER_SIZE, buf + offset, len - offset);
floodMsg->len += DATABASE_STORE_HEADER_SIZE + len -offset;
floodMsg->FillI2NPMessageHeader (eI2NPDatabaseStore);
2015-02-01 23:30:27 +00:00
std::set<IdentHash> excluded;
for (int i = 0; i < 3; i++)
{
2015-04-12 20:59:59 +00:00
auto floodfill = GetClosestFloodfill (ident, excluded);
2015-02-01 23:30:27 +00:00
if (floodfill)
2015-07-01 18:13:42 +00:00
transports.SendMessage (floodfill->GetIdentHash (), floodMsg);
2015-02-01 23:30:27 +00:00
}
}
2015-01-30 20:13:09 +00:00
}
2015-02-01 23:30:27 +00:00
2015-01-03 02:11:40 +00:00
if (buf[DATABASE_STORE_TYPE_OFFSET]) // type
2013-11-19 01:37:38 +00:00
{
LogPrint ("LeaseSet");
2015-04-12 20:59:59 +00:00
AddLeaseSet (ident, buf + offset, len - offset, m->from);
}
else
{
LogPrint ("RouterInfo");
size_t size = bufbe16toh (buf + offset);
2015-02-03 01:15:49 +00:00
offset += 2;
if (size > 2048 || size > len - offset)
{
LogPrint ("Invalid RouterInfo length ", (int)size);
return;
2013-11-20 12:46:09 +00:00
}
2015-11-03 14:15:49 +00:00
uint8_t uncompressed[2048];
size_t uncompressedSize = m_Inflator.Inflate (buf + offset, size, uncompressed, 2048);
if (uncompressedSize)
AddRouterInfo (ident, uncompressed, uncompressedSize);
}
2013-11-19 01:37:38 +00:00
}
2015-07-04 01:27:40 +00:00
void NetDb::HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg)
{
2015-07-04 01:27:40 +00:00
const 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);
2015-04-09 16:45:00 +00:00
IdentHash ident (buf);
auto dest = m_Requests.FindRequest (ident);
if (dest)
2014-01-05 14:53:44 +00:00
{
bool deleteDest = true;
2014-01-05 14:53:44 +00:00
if (num > 0)
{
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
2015-01-11 22:40:11 +00:00
auto outbound = pool ? pool->GetNextOutboundTunnel () : nullptr;
auto inbound = pool ? pool->GetNextInboundTunnel () : nullptr;
2014-10-15 00:52:40 +00:00
if (!dest->IsExploratory ())
{
// reply to our destination. Try other floodfills
if (outbound && inbound )
{
2015-02-12 16:40:42 +00:00
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
2014-10-15 00:52:40 +00:00
auto count = dest->GetExcludedPeers ().size ();
if (count < 7)
{
auto nextFloodfill = GetClosestFloodfill (dest->GetDestination (), dest->GetExcludedPeers ());
if (nextFloodfill)
{
// tell floodfill about us
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
nextFloodfill->GetIdentHash (), 0,
2015-11-03 14:15:49 +00:00
CreateDatabaseStoreMsg ()
});
2014-10-15 00:52:40 +00:00
// 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,
2015-06-22 19:47:45 +00:00
nextFloodfill->GetIdentHash (), 0, msg
2014-10-15 00:52:40 +00:00
});
deleteDest = false;
}
}
else
LogPrint (key, " was not found on 7 floodfills");
2015-02-12 16:40:42 +00:00
if (msgs.size () > 0)
outbound->SendTunnelDataMsg (msgs);
2014-10-15 00:52:40 +00:00
}
}
if (deleteDest)
2014-10-15 00:52:40 +00:00
// no more requests for the destinationation. delete it
2015-04-09 16:45:00 +00:00
m_Requests.RequestComplete (ident, nullptr);
2014-01-05 14:53:44 +00:00
}
else
// no more requests for detination possible. delete it
2015-04-09 16:45:00 +00:00
m_Requests.RequestComplete (ident, nullptr);
2014-01-05 14:53:44 +00:00
}
2015-01-10 14:07:07 +00:00
else
2014-01-05 14:53:44 +00:00
LogPrint ("Requested destination for ", key, " not found");
2015-01-10 14:07:07 +00:00
// try responses
for (int i = 0; i < num; i++)
{
2015-07-04 01:27:40 +00:00
const uint8_t * router = buf + 33 + i*32;
2015-01-10 14:07:07 +00:00
char peerHash[48];
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48);
peerHash[l1] = 0;
LogPrint (i,": ", peerHash);
auto r = FindRouter (router);
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
{
// router with ident not found or too old (1 hour)
LogPrint ("Found new/outdated router. Requesting RouterInfo ...");
RequestDestination (router);
}
else
LogPrint ("Bayan");
2014-08-10 13:04:17 +00:00
}
}
2015-07-04 01:27:40 +00:00
void NetDb::HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg)
{
2015-07-04 01:27:40 +00:00
const uint8_t * buf = msg->GetPayload ();
2015-04-12 20:59:59 +00:00
IdentHash ident (buf);
if (ident.IsZero ())
{
LogPrint (eLogError, "DatabaseLookup for zero ident. Ignored");
return;
}
char key[48];
int l = i2p::data::ByteStreamToBase64 (buf, 32, key, 48);
key[l] = 0;
uint8_t flag = buf[64];
2015-02-03 01:15:49 +00:00
LogPrint ("DatabaseLookup for ", key, " recieved flags=", (int)flag);
2015-02-03 03:34:55 +00:00
uint8_t lookupType = flag & DATABASE_LOOKUP_TYPE_FLAGS_MASK;
2015-07-04 01:27:40 +00:00
const uint8_t * excluded = buf + 65;
uint32_t replyTunnelID = 0;
2015-02-03 01:15:49 +00:00
if (flag & DATABASE_LOOKUP_DELIVERY_FLAG) //reply to tunnel
{
replyTunnelID = bufbe32toh (buf + 64);
excluded += 4;
}
uint16_t numExcluded = bufbe16toh (excluded);
excluded += 2;
if (numExcluded > 512)
{
2014-07-30 11:32:46 +00:00
LogPrint ("Number of excluded peers", numExcluded, " exceeds 512");
numExcluded = 0; // TODO:
}
2015-02-02 00:58:26 +00:00
2015-06-22 02:29:50 +00:00
std::shared_ptr<I2NPMessage> replyMsg;
2015-02-03 03:34:55 +00:00
if (lookupType == DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP)
2014-07-25 02:01:07 +00:00
{
2015-02-03 03:34:55 +00:00
LogPrint ("Exploratory close to ", key, " ", numExcluded, " excluded");
std::set<IdentHash> excludedRouters;
for (int i = 0; i < numExcluded; i++)
{
excludedRouters.insert (excluded);
excluded += 32;
}
2015-02-02 00:58:26 +00:00
std::vector<IdentHash> routers;
for (int i = 0; i < 3; i++)
{
2015-04-12 20:59:59 +00:00
auto r = GetClosestNonFloodfill (ident, excludedRouters);
2015-02-03 03:34:55 +00:00
if (r)
{
routers.push_back (r->GetIdentHash ());
excludedRouters.insert (r->GetIdentHash ());
}
2015-02-02 00:58:26 +00:00
}
2015-11-03 14:15:49 +00:00
replyMsg = CreateDatabaseSearchReply (ident, routers);
2015-02-03 03:34:55 +00:00
}
2014-07-31 16:59:43 +00:00
else
2015-02-03 03:34:55 +00:00
{
2015-04-12 19:54:28 +00:00
if (lookupType == DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP ||
lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP)
{
2015-04-12 20:59:59 +00:00
auto router = FindRouter (ident);
2015-04-12 19:54:28 +00:00
if (router)
{
LogPrint ("Requested RouterInfo ", key, " found");
router->LoadBuffer ();
if (router->GetBuffer ())
2015-11-03 14:15:49 +00:00
replyMsg = CreateDatabaseStoreMsg (router);
2015-04-12 19:54:28 +00:00
}
2015-02-03 03:34:55 +00:00
}
2015-04-12 19:54:28 +00:00
if (!replyMsg && (lookupType == DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP ||
lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP))
2015-02-03 03:34:55 +00:00
{
2015-04-12 20:59:59 +00:00
auto leaseSet = FindLeaseSet (ident);
2015-02-03 03:34:55 +00:00
if (leaseSet) // we don't send back our LeaseSets
{
LogPrint ("Requested LeaseSet ", key, " found");
2015-11-03 14:15:49 +00:00
replyMsg = CreateDatabaseStoreMsg (leaseSet);
2015-02-03 03:34:55 +00:00
}
}
2015-04-12 19:54:28 +00:00
2015-02-03 03:34:55 +00:00
if (!replyMsg)
{
LogPrint ("Requested ", key, " not found. ", numExcluded, " excluded");
2015-06-11 15:43:35 +00:00
std::set<IdentHash> excludedRouters;
for (int i = 0; i < numExcluded; i++)
{
excludedRouters.insert (excluded);
excluded += 32;
}
2015-11-03 14:15:49 +00:00
replyMsg = CreateDatabaseSearchReply (ident, GetClosestFloodfills (ident, 3, excludedRouters));
2015-02-03 03:34:55 +00:00
}
}
if (replyMsg)
{
if (replyTunnelID)
{
// encryption might be used though tunnel only
2015-02-03 01:15:49 +00:00
if (flag & DATABASE_LOOKUP_ENCYPTION_FLAG) // encrypted reply requested
{
2015-07-04 01:27:40 +00:00
const uint8_t * sessionKey = excluded;
uint8_t numTags = sessionKey[32];
if (numTags > 0)
{
2015-07-04 01:27:40 +00:00
const uint8_t * sessionTag = sessionKey + 33; // take first tag
i2p::garlic::GarlicRoutingSession garlic (sessionKey, sessionTag);
replyMsg = garlic.WrapSingleMessage (replyMsg);
}
}
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
if (outbound)
outbound->SendTunnelDataMsg (buf+32, replyTunnelID, replyMsg);
else
2015-06-22 02:29:50 +00:00
transports.SendMessage (buf+32, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg));
}
else
transports.SendMessage (buf+32, replyMsg);
}
}
2014-06-17 02:30:34 +00:00
void NetDb::Explore (int numDestinations)
{
2014-07-03 17:41:36 +00:00
// new requests
2014-06-17 02:30:34 +00:00
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
2015-01-10 21:08:13 +00:00
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
2014-06-17 02:30:34 +00:00
bool throughTunnels = outbound && inbound;
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++)
{
2015-11-03 14:15:49 +00:00
RAND_bytes (randomHash, 32);
2015-04-09 16:45:00 +00:00
auto dest = m_Requests.CreateRequest (randomHash, true); // exploratory
if (!dest)
{
LogPrint (eLogWarning, "Exploratory destination is requested already");
return;
2015-02-10 03:19:29 +00:00
}
2014-06-17 02:30:34 +00:00
auto floodfill = GetClosestFloodfill (randomHash, dest->GetExcludedPeers ());
if (floodfill && !floodfills.count (floodfill.get ())) // request floodfill only once
2014-02-12 03:19:51 +00:00
{
floodfills.insert (floodfill.get ());
if (i2p::transport::transports.IsConnected (floodfill->GetIdentHash ()))
throughTunnels = false;
2014-06-17 02:30:34 +00:00
if (throughTunnels)
{
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
floodfill->GetIdentHash (), 0,
2015-11-03 14:15:49 +00:00
CreateDatabaseStoreMsg () // tell floodfill about us
2014-06-17 02:30:34 +00:00
});
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
floodfill->GetIdentHash (), 0,
2015-06-22 19:47:45 +00:00
dest->CreateRequestMessage (floodfill, inbound) // explore
2014-06-17 02:30:34 +00:00
});
}
else
i2p::transport::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
2015-04-09 16:45:00 +00:00
m_Requests.RequestComplete (randomHash, nullptr);
}
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
2015-01-29 02:01:16 +00:00
for (int i = 0; i < 2; i++)
2014-03-16 12:00:34 +00:00
{
auto floodfill = GetClosestFloodfill (i2p::context.GetRouterInfo ().GetIdentHash (), excluded);
if (floodfill)
{
2015-11-03 14:15:49 +00:00
uint32_t replyToken;
RAND_bytes ((uint8_t *)&replyToken, 4);
LogPrint ("Publishing our RouterInfo to ", i2p::data::GetIdentHashAbbreviation(floodfill->GetIdentHash ()), ". reply token=", replyToken);
transports.SendMessage (floodfill->GetIdentHash (), CreateDatabaseStoreMsg (i2p::context.GetSharedRouterInfo (), replyToken));
2014-03-16 12:00:34 +00:00
excluded.insert (floodfill->GetIdentHash ());
}
2014-02-13 03:02:39 +00:00
}
2015-02-10 03:19:29 +00:00
}
2013-11-19 01:37:38 +00:00
2014-11-20 21:20:02 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter () const
{
return GetRandomRouter (
2014-11-20 21:20:02 +00:00
[](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden ();
});
}
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const
2014-09-25 01:45:19 +00:00
{
return GetRandomRouter (
2014-11-20 21:20:02 +00:00
[compatibleWith](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router != compatibleWith &&
2014-10-02 18:43:42 +00:00
router->IsCompatible (*compatibleWith);
});
2014-09-25 01:45:19 +00:00
}
2015-02-26 19:17:16 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetRandomPeerTestRouter () const
{
return GetRandomRouter (
[](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router->IsPeerTesting ();
});
}
2015-02-27 02:05:35 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetRandomIntroducer () const
{
return GetRandomRouter (
[](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router->IsIntroducer ();
});
}
std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const
{
return GetRandomRouter (
2014-11-20 21:20:02 +00:00
[compatibleWith](std::shared_ptr<const RouterInfo> router)->bool
{
return !router->IsHidden () && router != compatibleWith &&
router->IsCompatible (*compatibleWith) &&
2015-05-04 17:01:27 +00:00
(router->GetCaps () & RouterInfo::eHighBandwidth);
});
}
2014-09-25 01:45:19 +00:00
template<typename Filter>
2014-11-20 21:20:02 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (Filter filter) const
2013-11-13 12:59:21 +00:00
{
2015-11-03 14:15:49 +00:00
if (!m_RouterInfos.size ()) return 0;
uint32_t ind = rand () % m_RouterInfos.size ();
for (int j = 0; j < 2; j++)
2013-11-19 01:37:38 +00:00
{
uint32_t i = 0;
2014-11-21 18:29:19 +00:00
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
for (auto it: m_RouterInfos)
{
if (i >= ind)
{
if (!it.second->IsUnreachable () && filter (it.second))
2014-11-20 21:20:02 +00:00
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
}
2014-09-25 01:45:19 +00:00
2015-07-04 01:27:40 +00:00
void NetDb::PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg)
2013-11-20 12:46:09 +00:00
{
if (msg) m_Queue.Put (msg);
}
2014-01-04 02:24:20 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetClosestFloodfill (const IdentHash& destination,
const std::set<IdentHash>& excluded) const
2014-01-04 02:24:20 +00:00
{
std::shared_ptr<const RouterInfo> r;
2014-01-04 02:24:20 +00:00
XORMetric minMetric;
IdentHash destKey = CreateRoutingKey (destination);
2014-01-04 02:24:20 +00:00
minMetric.SetMax ();
2014-10-06 01:59:05 +00:00
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
2014-03-19 18:08:09 +00:00
for (auto it: m_Floodfills)
2014-01-04 02:24:20 +00:00
{
2015-04-10 23:49:58 +00:00
if (!it->IsUnreachable ())
2014-01-04 02:24:20 +00:00
{
XORMetric m = destKey ^ it->GetIdentHash ();
2015-04-10 23:49:58 +00:00
if (m < minMetric && !excluded.count (it->GetIdentHash ()))
2014-01-04 02:24:20 +00:00
{
minMetric = m;
r = it;
2014-01-04 02:24:20 +00:00
}
}
}
return r;
}
2014-01-22 20:32:50 +00:00
2015-06-11 15:43:35 +00:00
std::vector<IdentHash> NetDb::GetClosestFloodfills (const IdentHash& destination, size_t num,
std::set<IdentHash>& excluded) const
2015-04-10 20:15:13 +00:00
{
struct Sorted
{
std::shared_ptr<const RouterInfo> r;
XORMetric metric;
bool operator< (const Sorted& other) const { return metric < other.metric; };
};
std::set<Sorted> sorted;
IdentHash destKey = CreateRoutingKey (destination);
{
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
for (auto it: m_Floodfills)
{
if (!it->IsUnreachable ())
{
XORMetric m = destKey ^ it->GetIdentHash ();
if (sorted.size () < num)
sorted.insert ({it, m});
else if (m < sorted.rbegin ()->metric)
{
sorted.insert ({it, m});
sorted.erase (std::prev (sorted.end ()));
}
}
}
}
std::vector<IdentHash> res;
size_t i = 0;
for (auto it: sorted)
{
if (i < num)
{
2015-06-11 15:43:35 +00:00
auto& ident = it.r->GetIdentHash ();
if (!excluded.count (ident))
{
res.push_back (ident);
i++;
}
2015-04-10 20:15:13 +00:00
}
else
break;
}
return res;
}
2015-02-03 03:34:55 +00:00
std::shared_ptr<const RouterInfo> NetDb::GetClosestNonFloodfill (const IdentHash& destination,
const std::set<IdentHash>& excluded) const
{
std::shared_ptr<const RouterInfo> r;
XORMetric minMetric;
IdentHash destKey = CreateRoutingKey (destination);
minMetric.SetMax ();
// must be called from NetDb thread only
for (auto it: m_RouterInfos)
{
2015-04-10 23:49:58 +00:00
if (!it.second->IsFloodfill ())
2015-02-03 03:34:55 +00:00
{
XORMetric m = destKey ^ it.first;
2015-04-10 23:49:58 +00:00
if (m < minMetric && !excluded.count (it.first))
2015-02-03 03:34:55 +00:00
{
minMetric = m;
r = it.second;
}
}
}
return r;
}
2014-07-31 16:59:43 +00:00
void NetDb::ManageLeaseSets ()
{
for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();)
{
2015-01-28 19:20:28 +00:00
if (!it->second->HasNonExpiredLeases ()) // all leases expired
2014-07-31 16:59:43 +00:00
{
LogPrint ("LeaseSet ", it->second->GetIdentHash ().ToBase64 (), " expired");
it = m_LeaseSets.erase (it);
}
else
it++;
}
}
2013-11-13 12:59:21 +00:00
}
}