2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-01-06 19:31:01 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2014-12-31 17:14:53 +03:00
|
|
|
#include <string.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>
|
2017-07-11 09:55:38 +03:00
|
|
|
#include <stdexcept>
|
2016-06-28 03:00:00 +03:00
|
|
|
|
2016-02-11 03:00:00 +03:00
|
|
|
#include "I2PEndian.h"
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Base.h"
|
2016-05-11 22:12:38 +03:00
|
|
|
#include "Crypto.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"
|
2018-11-21 19:23:48 +03:00
|
|
|
#include "NTCP2.h"
|
2013-11-19 05:37:38 +04:00
|
|
|
#include "RouterContext.h"
|
2014-01-03 06:22:48 +04:00
|
|
|
#include "Garlic.h"
|
2020-04-07 18:40:18 +03:00
|
|
|
#include "ECIESX25519AEADRatchetSession.h"
|
2016-11-14 20:05:44 +03:00
|
|
|
#include "Config.h"
|
2020-04-07 18:40:18 +03:00
|
|
|
#include "NetDb.hpp"
|
2020-12-07 06:22:16 +03: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
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2013-11-13 16:59:21 +04:00
|
|
|
NetDb netdb;
|
|
|
|
|
2023-02-25 02:28:30 +03:00
|
|
|
NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr), m_Storage("netDb", "r", "routerInfo-", "dat"), m_PersistProfiles (true)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2013-11-13 16:59:21 +04:00
|
|
|
NetDb::~NetDb ()
|
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
Stop ();
|
2015-01-19 21:57:37 +03:00
|
|
|
delete m_Reseeder;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
|
|
|
|
void NetDb::Start ()
|
2016-08-29 22:34:59 +03:00
|
|
|
{
|
2016-02-20 04:00:00 +03:00
|
|
|
m_Storage.SetPlace(i2p::fs::GetDataDir());
|
|
|
|
m_Storage.Init(i2p::data::GetBase64SubstitutionTable(), 64);
|
2016-02-20 04:00:00 +03:00
|
|
|
InitProfilesStorage ();
|
2016-08-29 22:26:19 +03:00
|
|
|
m_Families.LoadCertificates ();
|
2015-06-19 22:44:50 +03:00
|
|
|
Load ();
|
2017-02-02 23:40:57 +03:00
|
|
|
|
2018-11-22 00:13:23 +03:00
|
|
|
uint16_t threshold; i2p::config::GetOption("reseed.threshold", threshold);
|
2023-02-22 23:58:20 +03:00
|
|
|
if (m_RouterInfos.size () < threshold || m_Floodfills.GetSize () < NETDB_MIN_FLOODFILLS) // reseed if # of router less than threshold or too few floodfiils
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2015-01-19 21:57:37 +03:00
|
|
|
Reseed ();
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2023-07-11 20:16:35 +03:00
|
|
|
else if (!GetRandomRouter (i2p::context.GetSharedRouterInfo (), false, false))
|
2021-02-10 23:27:13 +03:00
|
|
|
Reseed (); // we don't have a router we can connect to. Trying to reseed
|
2021-07-14 21:46:56 +03:00
|
|
|
|
|
|
|
auto it = m_RouterInfos.find (i2p::context.GetIdentHash ());
|
|
|
|
if (it != m_RouterInfos.end ())
|
|
|
|
{
|
|
|
|
// remove own router
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Remove (it->second->GetIdentHash ());
|
2021-10-18 13:09:56 +03:00
|
|
|
m_RouterInfos.erase (it);
|
2021-07-14 21:46:56 +03:00
|
|
|
}
|
|
|
|
// insert own router
|
|
|
|
m_RouterInfos.emplace (i2p::context.GetIdentHash (), i2p::context.GetSharedRouterInfo ());
|
|
|
|
if (i2p::context.IsFloodfill ())
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Insert (i2p::context.GetSharedRouterInfo ());
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2018-11-22 00:13:23 +03:00
|
|
|
i2p::config::GetOption("persist.profiles", m_PersistProfiles);
|
|
|
|
|
2015-01-15 04:27:19 +03:00
|
|
|
m_IsRunning = true;
|
2013-11-19 05:37:38 +04:00
|
|
|
m_Thread = new std::thread (std::bind (&NetDb::Run, this));
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2013-11-19 05:37:38 +04:00
|
|
|
void NetDb::Stop ()
|
|
|
|
{
|
2015-04-11 22:39:23 +03:00
|
|
|
if (m_IsRunning)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2018-11-22 00:13:23 +03:00
|
|
|
if (m_PersistProfiles)
|
2023-02-12 00:22:02 +03:00
|
|
|
SaveProfiles ();
|
2015-04-11 22:39:23 +03:00
|
|
|
DeleteObsoleteProfiles ();
|
|
|
|
m_RouterInfos.clear ();
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Clear ();
|
2015-04-11 22:39:23 +03:00
|
|
|
if (m_Thread)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-04-11 22:39:23 +03:00
|
|
|
m_IsRunning = false;
|
|
|
|
m_Queue.WakeUp ();
|
2018-01-06 06:48:51 +03:00
|
|
|
m_Thread->join ();
|
2015-04-11 22:39:23 +03:00
|
|
|
delete m_Thread;
|
|
|
|
m_Thread = 0;
|
|
|
|
}
|
|
|
|
m_LeaseSets.clear();
|
|
|
|
m_Requests.Stop ();
|
2016-06-13 18:34:44 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2013-11-19 05:37:38 +04:00
|
|
|
void NetDb::Run ()
|
|
|
|
{
|
2020-12-07 06:22:16 +03:00
|
|
|
i2p::util::SetThreadName("NetDB");
|
2020-12-04 18:36:49 +03:00
|
|
|
|
2024-02-17 02:56:04 +03:00
|
|
|
uint64_t lastManage = 0, lastExploratory = 0, lastManageRequest = 0;
|
2024-04-27 23:00:43 +03:00
|
|
|
uint64_t lastProfilesCleanup = i2p::util::GetMonotonicMilliseconds (), lastObsoleteProfilesCleanup = lastProfilesCleanup;
|
|
|
|
int16_t profilesCleanupVariance = 0, obsoleteProfilesCleanVariance = 0;
|
2022-05-07 03:08:55 +03:00
|
|
|
|
2013-11-19 05:37:38 +04:00
|
|
|
while (m_IsRunning)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2013-11-28 06:28:32 +04:00
|
|
|
try
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2024-04-24 16:44:24 +03:00
|
|
|
auto msg = m_Queue.GetNextWithTimeout (1000); // 1 sec
|
2013-11-28 06:28:32 +04:00
|
|
|
if (msg)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
|
|
|
int numMsgs = 0;
|
2015-02-12 19:40:42 +03:00
|
|
|
while (msg)
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint(eLogDebug, "NetDb: Got request with type ", (int) msg->GetTypeID ());
|
2018-01-06 06:48:51 +03:00
|
|
|
switch (msg->GetTypeID ())
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
case eI2NPDatabaseStore:
|
2015-06-16 20:32:42 +03:00
|
|
|
HandleDatabaseStoreMsg (msg);
|
2014-07-23 19:10:03 +04:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseSearchReply:
|
2015-06-16 20:32:42 +03:00
|
|
|
HandleDatabaseSearchReplyMsg (msg);
|
2014-07-23 19:10:03 +04:00
|
|
|
break;
|
|
|
|
case eI2NPDatabaseLookup:
|
2015-06-16 20:32:42 +03:00
|
|
|
HandleDatabaseLookupMsg (msg);
|
2018-01-06 06:48:51 +03:00
|
|
|
break;
|
2018-11-15 04:52:54 +03:00
|
|
|
case eI2NPDummyMsg:
|
2018-11-21 21:24:54 +03:00
|
|
|
// plain RouterInfo from NTCP2 with flags for now
|
2018-11-21 19:23:48 +03:00
|
|
|
HandleNTCP2RouterInfoMsg (msg);
|
2019-02-22 18:32:50 +03:00
|
|
|
break;
|
2014-07-23 19:10:03 +04:00
|
|
|
default: // WTF?
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: Unexpected message type ", (int) msg->GetTypeID ());
|
2015-06-16 20:32:42 +03:00
|
|
|
//i2p::HandleI2NPMessage (msg);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-02-12 19:40:42 +03:00
|
|
|
if (numMsgs > 100) break;
|
2013-11-28 06:28:32 +04:00
|
|
|
msg = m_Queue.Get ();
|
2015-02-10 17:30:48 +03:00
|
|
|
numMsgs++;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 17:30:48 +03:00
|
|
|
if (!m_IsRunning) break;
|
2020-10-12 00:51:40 +03:00
|
|
|
if (!i2p::transport::transports.IsOnline ()) continue; // don't manage netdb when offline
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2024-04-26 19:43:48 +03:00
|
|
|
uint64_t mts = i2p::util::GetMonotonicMilliseconds ();
|
|
|
|
if (mts >= lastManageRequest + MANAGE_REQUESTS_INTERVAL*1000)
|
2015-02-04 23:34:52 +03:00
|
|
|
{
|
2024-04-26 19:43:48 +03:00
|
|
|
if (lastManageRequest || i2p::tunnel::tunnels.GetExploratoryPool ()) // expolratory pool is ready?
|
|
|
|
{
|
|
|
|
m_Requests.ManageRequests ();
|
|
|
|
lastManageRequest = mts;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2022-05-07 03:08:55 +03:00
|
|
|
|
2024-04-26 19:43:48 +03:00
|
|
|
if (mts >= lastManage + 60000) // manage routers and leasesets every minute
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2023-04-06 04:30:36 +03:00
|
|
|
if (lastManage)
|
2014-02-15 01:10:25 +04:00
|
|
|
{
|
2023-04-06 04:30:36 +03:00
|
|
|
ManageRouterInfos ();
|
2014-07-31 20:59:43 +04:00
|
|
|
ManageLeaseSets ();
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2024-04-26 19:43:48 +03:00
|
|
|
lastManage = mts;
|
2016-06-18 18:10:58 +03:00
|
|
|
}
|
2022-05-07 03:08:55 +03:00
|
|
|
|
2024-04-26 19:43:48 +03:00
|
|
|
if (mts >= lastProfilesCleanup + (uint64_t)(i2p::data::PEER_PROFILE_AUTOCLEAN_TIMEOUT + profilesCleanupVariance)*1000)
|
2022-05-07 03:08:55 +03:00
|
|
|
{
|
2023-08-26 17:57:05 +03:00
|
|
|
m_RouterProfilesPool.CleanUpMt ();
|
2024-04-27 23:00:43 +03:00
|
|
|
if (m_PersistProfiles)
|
|
|
|
{
|
|
|
|
bool isSaving = m_SavingProfiles.valid ();
|
|
|
|
if (isSaving && m_SavingProfiles.wait_for(std::chrono::seconds(0)) == std::future_status::ready) // still active?
|
|
|
|
{
|
|
|
|
m_SavingProfiles.get ();
|
|
|
|
isSaving = false;
|
|
|
|
}
|
|
|
|
if (!isSaving)
|
|
|
|
m_SavingProfiles = PersistProfiles ();
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "NetDb: Can't persist profiles. Profiles are being saved to disk");
|
|
|
|
}
|
2024-04-26 19:43:48 +03:00
|
|
|
lastProfilesCleanup = mts;
|
2022-05-07 16:56:58 +03:00
|
|
|
profilesCleanupVariance = (rand () % (2 * i2p::data::PEER_PROFILE_AUTOCLEAN_VARIANCE) - i2p::data::PEER_PROFILE_AUTOCLEAN_VARIANCE);
|
2022-05-07 03:08:55 +03:00
|
|
|
}
|
|
|
|
|
2024-04-27 23:00:43 +03:00
|
|
|
if (mts >= lastObsoleteProfilesCleanup + (uint64_t)(i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_TIMEOUT + obsoleteProfilesCleanVariance)*1000)
|
|
|
|
{
|
|
|
|
bool isDeleting = m_DeletingProfiles.valid ();
|
|
|
|
if (isDeleting && m_DeletingProfiles.wait_for(std::chrono::seconds(0)) == std::future_status::ready) // still active?
|
|
|
|
{
|
|
|
|
m_DeletingProfiles.get ();
|
|
|
|
isDeleting = false;
|
|
|
|
}
|
|
|
|
if (!isDeleting)
|
|
|
|
m_DeletingProfiles = DeleteObsoleteProfiles ();
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "NetDb: Can't delete profiles. Profiles are being deleted from disk");
|
|
|
|
lastObsoleteProfilesCleanup = mts;
|
|
|
|
obsoleteProfilesCleanVariance = (rand () % (2 * i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_VARIANCE) - i2p::data::PEER_PROFILE_OBSOLETE_PROFILES_CLEAN_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2024-04-26 19:43:48 +03:00
|
|
|
if (mts >= lastExploratory + 30000) // exploratory every 30 seconds
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-01-11 00:08:13 +03:00
|
|
|
auto numRouters = m_RouterInfos.size ();
|
2018-09-21 17:13:18 +03:00
|
|
|
if (!numRouters)
|
2019-02-22 18:32:50 +03:00
|
|
|
throw std::runtime_error("No known routers, reseed seems to be totally failed");
|
2016-11-14 20:05:44 +03:00
|
|
|
else // we have peers now
|
|
|
|
m_FloodfillBootstrap = nullptr;
|
2024-04-26 19:43:48 +03:00
|
|
|
if (numRouters < 2500 || mts >= lastExploratory + 90000) // 90 seconds
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-01-11 00:08:13 +03:00
|
|
|
numRouters = 800/numRouters;
|
|
|
|
if (numRouters < 1) numRouters = 1;
|
2018-01-06 06:48:51 +03:00
|
|
|
if (numRouters > 9) numRouters = 9;
|
2016-10-28 19:50:26 +03:00
|
|
|
m_Requests.ManageRequests ();
|
2023-02-25 02:28:30 +03:00
|
|
|
if(!i2p::context.IsHidden ())
|
2016-10-28 19:50:26 +03:00
|
|
|
Explore (numRouters);
|
2024-04-26 19:43:48 +03:00
|
|
|
lastExploratory = mts;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: Runtime exception: ", ex.what ());
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:06:00 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::AddRouterInfo (const uint8_t * buf, int len)
|
2018-11-21 21:24:54 +03:00
|
|
|
{
|
|
|
|
bool updated;
|
2022-07-19 21:06:00 +03:00
|
|
|
return AddRouterInfo (buf, len, updated);
|
2018-11-21 21:24:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const RouterInfo> NetDb::AddRouterInfo (const uint8_t * buf, int len, bool& updated)
|
2014-12-11 23:41:04 +03:00
|
|
|
{
|
|
|
|
IdentityEx identity;
|
2014-12-12 06:31:39 +03:00
|
|
|
if (identity.FromBuffer (buf, len))
|
2018-11-21 21:24:54 +03:00
|
|
|
return AddRouterInfo (identity.GetIdentHash (), buf, len, updated);
|
|
|
|
updated = false;
|
|
|
|
return nullptr;
|
2014-12-11 23:41:04 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 23:36:55 +03:00
|
|
|
bool NetDb::AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2018-11-21 21:24:54 +03:00
|
|
|
bool updated;
|
2024-02-09 03:55:54 +03:00
|
|
|
if (!AddRouterInfo (ident, buf, len, updated))
|
|
|
|
updated = false;
|
2019-02-22 18:32:50 +03:00
|
|
|
return updated;
|
2018-11-21 21:24:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const RouterInfo> NetDb::AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len, bool& updated)
|
|
|
|
{
|
|
|
|
updated = true;
|
2014-11-21 21:29:19 +03:00
|
|
|
auto r = FindRouter (ident);
|
|
|
|
if (r)
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2016-02-17 23:36:55 +03:00
|
|
|
if (r->IsNewer (buf, len))
|
|
|
|
{
|
2019-02-25 02:26:58 +03:00
|
|
|
bool wasFloodfill = r->IsFloodfill ();
|
2022-07-22 22:16:42 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2023-02-13 02:02:16 +03:00
|
|
|
if (!r->Update (buf, len))
|
|
|
|
{
|
|
|
|
updated = false;
|
2023-02-13 03:10:22 +03:00
|
|
|
m_Requests.RequestComplete (ident, r);
|
2023-02-13 02:02:16 +03:00
|
|
|
return r;
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2024-01-06 19:31:01 +03:00
|
|
|
if (r->IsUnreachable () ||
|
|
|
|
i2p::util::GetMillisecondsSinceEpoch () + NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < r->GetTimestamp ())
|
2023-02-13 02:02:16 +03:00
|
|
|
{
|
2024-01-06 19:31:01 +03:00
|
|
|
// delete router as invalid or from future after update
|
2023-02-13 02:02:16 +03:00
|
|
|
m_RouterInfos.erase (ident);
|
|
|
|
if (wasFloodfill)
|
2023-05-03 16:59:35 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Remove (r->GetIdentHash ());
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2023-02-13 03:10:22 +03:00
|
|
|
m_Requests.RequestComplete (ident, nullptr);
|
2023-05-03 16:59:35 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogInfo))
|
|
|
|
LogPrint (eLogInfo, "NetDb: RouterInfo updated: ", ident.ToBase64());
|
2019-02-25 02:26:58 +03:00
|
|
|
if (wasFloodfill != r->IsFloodfill ()) // if floodfill status updated
|
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogDebug))
|
|
|
|
LogPrint (eLogDebug, "NetDb: RouterInfo floodfill status updated: ", ident.ToBase64());
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2019-02-25 02:26:58 +03:00
|
|
|
if (wasFloodfill)
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Remove (r->GetIdentHash ());
|
2020-11-21 22:27:08 +03:00
|
|
|
else if (r->IsEligibleFloodfill ())
|
2023-06-06 01:00:07 +03:00
|
|
|
{
|
|
|
|
if (m_Floodfills.GetSize () < NETDB_NUM_FLOODFILLS_THRESHOLD || r->GetProfile ()->IsReal ())
|
2023-05-06 01:22:11 +03:00
|
|
|
m_Floodfills.Insert (r);
|
|
|
|
else
|
2024-04-19 01:06:24 +03:00
|
|
|
r->ResetFloodFill ();
|
2023-06-06 01:00:07 +03:00
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2016-02-17 23:36:55 +03:00
|
|
|
}
|
|
|
|
else
|
2016-11-15 20:17:21 +03:00
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogDebug))
|
|
|
|
LogPrint (eLogDebug, "NetDb: RouterInfo is older: ", ident.ToBase64());
|
2016-11-15 20:17:21 +03:00
|
|
|
updated = false;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-15 04:27:19 +03:00
|
|
|
r = std::make_shared<RouterInfo> (buf, len);
|
2023-02-19 18:30:07 +03:00
|
|
|
if (!r->IsUnreachable () && r->HasValidAddresses () && (!r->IsFloodfill () || !r->GetProfile ()->IsUnreachable ()) &&
|
2022-12-21 02:41:19 +03:00
|
|
|
i2p::util::GetMillisecondsSinceEpoch () + NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL > r->GetTimestamp ())
|
2014-11-21 21:29:19 +03:00
|
|
|
{
|
2016-11-15 20:17:21 +03:00
|
|
|
bool inserted = false;
|
2016-01-16 23:36:30 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2016-11-15 20:17:21 +03:00
|
|
|
inserted = m_RouterInfos.insert ({r->GetIdentHash (), r}).second;
|
|
|
|
}
|
|
|
|
if (inserted)
|
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogInfo))
|
|
|
|
LogPrint (eLogInfo, "NetDb: RouterInfo added: ", ident.ToBase64());
|
2023-05-06 01:22:11 +03:00
|
|
|
if (r->IsFloodfill () && r->IsEligibleFloodfill ())
|
2023-06-06 01:00:07 +03:00
|
|
|
{
|
2023-05-06 01:22:11 +03:00
|
|
|
if (m_Floodfills.GetSize () < NETDB_NUM_FLOODFILLS_THRESHOLD ||
|
2023-06-06 01:00:07 +03:00
|
|
|
r->GetProfile ()->IsReal ()) // don't insert floodfill until it's known real if we have enough
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2023-05-06 01:22:11 +03:00
|
|
|
m_Floodfills.Insert (r);
|
|
|
|
}
|
|
|
|
else
|
2024-04-19 01:06:24 +03:00
|
|
|
r->ResetFloodFill ();
|
2016-11-15 20:17:21 +03:00
|
|
|
}
|
2016-01-16 23:36:30 +03:00
|
|
|
}
|
2016-11-15 20:17:21 +03:00
|
|
|
else
|
2016-01-16 23:36:30 +03:00
|
|
|
{
|
2016-11-15 20:17:21 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Duplicated RouterInfo ", ident.ToBase64());
|
|
|
|
updated = false;
|
2016-01-16 23:36:30 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2016-02-17 23:36:55 +03:00
|
|
|
else
|
|
|
|
updated = false;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-01-15 00:11:09 +03:00
|
|
|
// take care about requested destination
|
2015-04-09 19:45:00 +03:00
|
|
|
m_Requests.RequestComplete (ident, r);
|
2018-11-21 21:24:54 +03:00
|
|
|
return r;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2019-01-17 03:00:17 +03:00
|
|
|
bool NetDb::AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len)
|
2013-11-13 16:59:21 +04:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_LeaseSetsMutex);
|
2016-02-17 23:36:55 +03:00
|
|
|
bool updated = false;
|
2019-01-17 03:00:17 +03:00
|
|
|
auto it = m_LeaseSets.find(ident);
|
|
|
|
if (it != m_LeaseSets.end () && it->second->GetStoreType () == i2p::data::NETDB_STORE_TYPE_LEASESET)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2019-01-17 03:00:17 +03:00
|
|
|
// we update only is existing LeaseSet is not LeaseSet2
|
|
|
|
uint64_t expires;
|
|
|
|
if(LeaseSetBufferValidate(buf, len, expires))
|
2014-10-14 01:45:07 +04:00
|
|
|
{
|
2019-01-17 03:00:17 +03:00
|
|
|
if(it->second->GetExpirationTime() < expires)
|
2015-04-08 17:34:16 +03:00
|
|
|
{
|
2019-01-17 03:00:17 +03:00
|
|
|
it->second->Update (buf, len, false); // signature is verified already
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogInfo))
|
|
|
|
LogPrint (eLogInfo, "NetDb: LeaseSet updated: ", ident.ToBase32());
|
2019-01-17 03:00:17 +03:00
|
|
|
updated = true;
|
2016-02-17 23:36:55 +03:00
|
|
|
}
|
2024-02-09 05:56:25 +03:00
|
|
|
else if (CheckLogLevel (eLogDebug))
|
2019-01-17 03:00:17 +03:00
|
|
|
LogPrint(eLogDebug, "NetDb: LeaseSet is older: ", ident.ToBase32());
|
2014-10-14 01:45:07 +04:00
|
|
|
}
|
|
|
|
else
|
2019-01-17 03:00:17 +03:00
|
|
|
LogPrint(eLogError, "NetDb: LeaseSet is invalid: ", ident.ToBase32());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto leaseSet = std::make_shared<LeaseSet> (buf, len, false); // we don't need leases in netdb
|
|
|
|
if (leaseSet->IsValid ())
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogInfo))
|
|
|
|
LogPrint (eLogInfo, "NetDb: LeaseSet added: ", ident.ToBase32());
|
2019-01-17 03:00:17 +03:00
|
|
|
m_LeaseSets[ident] = leaseSet;
|
|
|
|
updated = true;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2019-01-17 03:00:17 +03:00
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: New LeaseSet validation failed: ", ident.ToBase32());
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2016-02-17 23:36:55 +03:00
|
|
|
return updated;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2018-12-21 23:00:03 +03:00
|
|
|
bool NetDb::AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType)
|
|
|
|
{
|
2019-01-17 03:00:17 +03:00
|
|
|
auto leaseSet = std::make_shared<LeaseSet2> (storeType, buf, len, false); // we don't need leases in netdb
|
2019-02-27 00:20:24 +03:00
|
|
|
if (leaseSet->IsValid ())
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_LeaseSetsMutex);
|
2019-02-27 00:20:24 +03:00
|
|
|
auto it = m_LeaseSets.find(ident);
|
2019-03-07 22:51:05 +03:00
|
|
|
if (it == m_LeaseSets.end () || it->second->GetStoreType () != storeType ||
|
2019-02-27 00:20:24 +03:00
|
|
|
leaseSet->GetPublishedTimestamp () > it->second->GetPublishedTimestamp ())
|
|
|
|
{
|
2022-12-21 02:41:19 +03:00
|
|
|
if (leaseSet->IsPublic () && !leaseSet->IsExpired () &&
|
|
|
|
i2p::util::GetSecondsSinceEpoch () + NETDB_EXPIRATION_TIMEOUT_THRESHOLD > leaseSet->GetPublishedTimestamp ())
|
2019-07-16 18:48:30 +03:00
|
|
|
{
|
|
|
|
// TODO: implement actual update
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogInfo))
|
|
|
|
LogPrint (eLogInfo, "NetDb: LeaseSet2 updated: ", ident.ToBase32());
|
2019-07-16 18:48:30 +03:00
|
|
|
m_LeaseSets[ident] = leaseSet;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-21 02:41:19 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Unpublished or expired or future LeaseSet2 received: ", ident.ToBase32());
|
2019-07-16 18:48:30 +03:00
|
|
|
m_LeaseSets.erase (ident);
|
|
|
|
}
|
2019-02-27 00:20:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: New LeaseSet2 validation failed: ", ident.ToBase32());
|
2019-02-27 00:20:24 +03:00
|
|
|
return false;
|
2018-12-21 23:00:03 +03: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
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<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
|
|
|
|
2015-01-27 19:27:58 +03:00
|
|
|
std::shared_ptr<LeaseSet> NetDb::FindLeaseSet (const IdentHash& destination) const
|
2013-12-22 20:29:57 +04:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_LeaseSetsMutex);
|
2013-12-22 20:29:57 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<RouterProfile> NetDb::FindRouterProfile (const IdentHash& ident) const
|
|
|
|
{
|
2019-02-22 18:32:50 +03:00
|
|
|
if (!m_PersistProfiles)
|
|
|
|
return nullptr;
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
auto router = FindRouter (ident);
|
|
|
|
return router ? router->GetProfile () : nullptr;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2014-10-24 23:39:53 +04:00
|
|
|
void NetDb::SetUnreachable (const IdentHash& ident, bool unreachable)
|
|
|
|
{
|
2023-04-18 21:35:13 +03:00
|
|
|
auto r = FindRouter (ident);
|
|
|
|
if (r)
|
2023-02-11 09:41:51 +03:00
|
|
|
{
|
2023-04-18 21:35:13 +03:00
|
|
|
r->SetUnreachable (unreachable);
|
2023-07-18 01:44:51 +03:00
|
|
|
auto profile = r->GetProfile ();
|
|
|
|
if (profile)
|
|
|
|
profile->Unreachable (unreachable);
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2014-10-24 23:39:53 +04:00
|
|
|
}
|
|
|
|
|
2023-04-18 21:35:13 +03:00
|
|
|
void NetDb::ExcludeReachableTransports (const IdentHash& ident, RouterInfo::CompatibleTransports transports)
|
|
|
|
{
|
|
|
|
auto r = FindRouter (ident);
|
|
|
|
if (r)
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2023-04-18 21:35:13 +03:00
|
|
|
r->ExcludeReachableTransports (transports);
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:57:37 +03:00
|
|
|
void NetDb::Reseed ()
|
|
|
|
{
|
|
|
|
if (!m_Reseeder)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-01-19 21:57:37 +03:00
|
|
|
m_Reseeder = new Reseeder ();
|
|
|
|
m_Reseeder->LoadCertificates (); // we need certificates for SU3 verification
|
|
|
|
}
|
2016-11-14 20:05:44 +03:00
|
|
|
|
|
|
|
// try reseeding from floodfill first if specified
|
2023-03-03 22:26:38 +03:00
|
|
|
std::string riPath; i2p::config::GetOption("reseed.floodfill", riPath);
|
|
|
|
if (!riPath.empty())
|
2022-07-19 21:06:00 +03:00
|
|
|
{
|
2016-11-14 20:05:44 +03:00
|
|
|
auto ri = std::make_shared<RouterInfo>(riPath);
|
2022-10-09 20:24:43 +03:00
|
|
|
if (ri->IsFloodfill())
|
2022-07-19 21:06:00 +03:00
|
|
|
{
|
2016-11-14 20:05:44 +03:00
|
|
|
const uint8_t * riData = ri->GetBuffer();
|
|
|
|
int riLen = ri->GetBufferLen();
|
2022-10-09 20:24:43 +03:00
|
|
|
if (!i2p::data::netdb.AddRouterInfo(riData, riLen))
|
2022-07-19 21:06:00 +03:00
|
|
|
{
|
2016-11-14 20:05:44 +03:00
|
|
|
// bad router info
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint(eLogError, "NetDb: Bad router info");
|
2016-11-14 20:05:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_FloodfillBootstrap = ri;
|
2024-04-24 16:44:24 +03:00
|
|
|
//ReseedFromFloodfill(*ri);
|
2019-04-08 22:22:42 +03:00
|
|
|
// don't try reseed servers if trying to bootstrap from floodfill
|
2016-11-14 20:05:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 01:17:25 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
m_Reseeder->Bootstrap ();
|
2015-01-19 21:57:37 +03:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:05:44 +03:00
|
|
|
void NetDb::ReseedFromFloodfill(const RouterInfo & ri, int numRouters, int numFloodfills)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint(eLogInfo, "NetDB: Reseeding from floodfill ", ri.GetIdentHashBase64());
|
2016-11-14 20:05:44 +03:00
|
|
|
std::vector<std::shared_ptr<i2p::I2NPMessage> > requests;
|
|
|
|
|
|
|
|
i2p::data::IdentHash ourIdent = i2p::context.GetIdentHash();
|
|
|
|
i2p::data::IdentHash ih = ri.GetIdentHash();
|
|
|
|
i2p::data::IdentHash randomIdent;
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-11-14 20:05:44 +03:00
|
|
|
// make floodfill lookups
|
|
|
|
while(numFloodfills > 0) {
|
|
|
|
randomIdent.Randomize();
|
|
|
|
auto msg = i2p::CreateRouterInfoDatabaseLookupMsg(randomIdent, ourIdent, 0, false);
|
|
|
|
requests.push_back(msg);
|
|
|
|
numFloodfills --;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-11-14 20:05:44 +03:00
|
|
|
// make regular router lookups
|
|
|
|
while(numRouters > 0) {
|
|
|
|
randomIdent.Randomize();
|
|
|
|
auto msg = i2p::CreateRouterInfoDatabaseLookupMsg(randomIdent, ourIdent, 0, true);
|
|
|
|
requests.push_back(msg);
|
|
|
|
numRouters --;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-11-14 20:05:44 +03:00
|
|
|
// send them off
|
|
|
|
i2p::transport::transports.SendMessages(ih, requests);
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:42:06 +03:00
|
|
|
bool NetDb::LoadRouterInfo (const std::string& path, uint64_t ts)
|
2014-01-31 10:22:11 +04:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
auto r = std::make_shared<RouterInfo>(path);
|
2022-02-13 23:42:06 +03:00
|
|
|
if (r->GetRouterIdentity () && !r->IsUnreachable () && r->HasValidAddresses () &&
|
2022-12-21 02:41:19 +03:00
|
|
|
ts < r->GetTimestamp () + 24*60*60*NETDB_MAX_OFFLINE_EXPIRATION_TIMEOUT*1000LL) // too old
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
r->DeleteBuffer ();
|
2021-07-17 03:12:41 +03:00
|
|
|
if (m_RouterInfos.emplace (r->GetIdentHash (), r).second)
|
2021-11-27 23:30:35 +03:00
|
|
|
{
|
2021-07-17 03:12:41 +03:00
|
|
|
if (r->IsFloodfill () && r->IsEligibleFloodfill ())
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Insert (r);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
else
|
2016-02-22 18:27:43 +03:00
|
|
|
{
|
2022-02-13 23:42:06 +03:00
|
|
|
LogPrint(eLogWarning, "NetDb: RI from ", path, " is invalid or too old. Delete");
|
2016-02-11 03:00:00 +03:00
|
|
|
i2p::fs::Remove(path);
|
2014-01-29 22:36:20 +04:00
|
|
|
}
|
2016-02-11 03:00:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-15 20:52:55 +03:00
|
|
|
void NetDb::VisitLeaseSets(LeaseSetVisitor v)
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_LeaseSetsMutex);
|
2016-07-15 20:52:55 +03:00
|
|
|
for ( auto & entry : m_LeaseSets)
|
|
|
|
v(entry.first, entry.second);
|
|
|
|
}
|
2016-08-29 21:16:29 +03:00
|
|
|
|
|
|
|
void NetDb::VisitStoredRouterInfos(RouterInfoVisitor v)
|
|
|
|
{
|
2019-02-22 18:32:50 +03:00
|
|
|
m_Storage.Iterate([v] (const std::string & filename)
|
|
|
|
{
|
|
|
|
auto ri = std::make_shared<i2p::data::RouterInfo>(filename);
|
2016-08-29 21:16:29 +03:00
|
|
|
v(ri);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetDb::VisitRouterInfos(RouterInfoVisitor v)
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RouterInfosMutex);
|
2016-08-29 21:16:29 +03:00
|
|
|
for ( const auto & item : m_RouterInfos )
|
2016-08-31 02:59:24 +03:00
|
|
|
v(item.second);
|
2016-08-29 21:16:29 +03:00
|
|
|
}
|
2016-08-30 22:54:53 +03:00
|
|
|
|
|
|
|
size_t NetDb::VisitRandomRouterInfos(RouterInfoFilter filter, RouterInfoVisitor v, size_t n)
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<const RouterInfo> > found;
|
|
|
|
const size_t max_iters_per_cyle = 3;
|
|
|
|
size_t iters = max_iters_per_cyle;
|
2016-09-01 22:54:48 +03:00
|
|
|
while(n > 0)
|
2016-08-30 22:54:53 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> lock(m_RouterInfosMutex);
|
2016-08-30 22:54:53 +03:00
|
|
|
uint32_t idx = rand () % m_RouterInfos.size ();
|
|
|
|
uint32_t i = 0;
|
|
|
|
for (const auto & it : m_RouterInfos) {
|
|
|
|
if(i >= idx) // are we at the random start point?
|
|
|
|
{
|
|
|
|
// yes, check if we want this one
|
2016-08-31 02:59:24 +03:00
|
|
|
if(filter(it.second))
|
2016-08-30 22:54:53 +03:00
|
|
|
{
|
|
|
|
// we have a match
|
|
|
|
--n;
|
|
|
|
found.push_back(it.second);
|
|
|
|
// reset max iterations per cycle
|
|
|
|
iters = max_iters_per_cyle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // not there yet
|
|
|
|
++i;
|
|
|
|
}
|
2016-09-01 22:54:48 +03:00
|
|
|
// we have enough
|
|
|
|
if(n == 0) break;
|
2016-08-30 22:54:53 +03:00
|
|
|
--iters;
|
|
|
|
// have we tried enough this cycle ?
|
|
|
|
if(!iters) {
|
|
|
|
// yes let's try the next cycle
|
|
|
|
--n;
|
|
|
|
iters = max_iters_per_cyle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// visit the ones we found
|
|
|
|
size_t visited = 0;
|
|
|
|
for(const auto & ri : found ) {
|
2016-08-31 02:59:24 +03:00
|
|
|
v(ri);
|
2016-08-30 22:54:53 +03:00
|
|
|
++visited;
|
|
|
|
}
|
|
|
|
return visited;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2016-02-11 03:00:00 +03:00
|
|
|
void NetDb::Load ()
|
|
|
|
{
|
2014-01-31 22:08:33 +04:00
|
|
|
// make sure we cleanup netDb from previous attempts
|
2018-01-06 06:48:51 +03:00
|
|
|
m_RouterInfos.clear ();
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Clear ();
|
2014-01-31 22:08:33 +04:00
|
|
|
|
2022-02-13 23:42:06 +03:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch();
|
2016-02-11 03:00:00 +03:00
|
|
|
std::vector<std::string> files;
|
2016-02-20 04:00:00 +03:00
|
|
|
m_Storage.Traverse(files);
|
2016-08-05 21:23:54 +03:00
|
|
|
for (const auto& path : files)
|
2022-02-13 23:42:06 +03:00
|
|
|
LoadRouterInfo (path, ts);
|
2016-02-11 03:00:00 +03:00
|
|
|
|
2023-02-22 23:58:20 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: ", m_RouterInfos.size(), " routers loaded (", m_Floodfills.GetSize (), " floodfils)");
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2015-06-19 22:44:50 +03:00
|
|
|
void NetDb::SaveUpdated ()
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-08-26 22:13:58 +03:00
|
|
|
int updatedCount = 0, deletedCount = 0, deletedFloodfillsCount = 0;
|
2014-01-23 05:19:39 +04:00
|
|
|
auto total = m_RouterInfos.size ();
|
2023-02-22 23:58:20 +03:00
|
|
|
auto totalFloodfills = m_Floodfills.GetSize ();
|
2018-01-06 06:48:51 +03:00
|
|
|
uint64_t expirationTimeout = NETDB_MAX_EXPIRATION_TIMEOUT*1000LL;
|
2016-02-11 03:00:00 +03:00
|
|
|
uint64_t ts = i2p::util::GetMillisecondsSinceEpoch();
|
2020-03-01 13:25:50 +03:00
|
|
|
auto uptime = i2p::context.GetUptime ();
|
2023-05-03 16:59:35 +03:00
|
|
|
double minTunnelCreationSuccessRate;
|
|
|
|
i2p::config::GetOption("limits.zombies", minTunnelCreationSuccessRate);
|
|
|
|
bool isLowRate = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < minTunnelCreationSuccessRate;
|
2018-01-06 06:48:51 +03:00
|
|
|
// routers don't expire if less than 90 or uptime is less than 1 hour
|
2024-04-22 20:03:59 +03:00
|
|
|
bool checkForExpiration = total > NETDB_MIN_ROUTERS && uptime > NETDB_CHECK_FOR_EXPIRATION_UPTIME; // 10 minutes
|
|
|
|
if (checkForExpiration && uptime > i2p::transport::SSU2_TO_INTRODUCER_SESSION_DURATION) // 1 hour
|
2016-02-24 19:31:14 +03:00
|
|
|
expirationTimeout = i2p::context.IsFloodfill () ? NETDB_FLOODFILL_EXPIRATION_TIMEOUT*1000LL :
|
2020-03-01 13:25:50 +03:00
|
|
|
NETDB_MIN_EXPIRATION_TIMEOUT*1000LL + (NETDB_MAX_EXPIRATION_TIMEOUT - NETDB_MIN_EXPIRATION_TIMEOUT)*1000LL*NETDB_MIN_ROUTERS/total;
|
2016-02-11 03:00:00 +03:00
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
auto own = i2p::context.GetSharedRouterInfo ();
|
2016-08-05 21:23:54 +03:00
|
|
|
for (auto& it: m_RouterInfos)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2023-11-23 21:06:50 +03:00
|
|
|
if (!it.second || it.second == own) continue; // skip own
|
2016-02-11 03:00:00 +03:00
|
|
|
std::string ident = it.second->GetIdentHashBase64();
|
2018-01-06 06:48:51 +03:00
|
|
|
if (it.second->IsUpdated ())
|
2016-02-23 04:51:32 +03:00
|
|
|
{
|
2023-02-13 03:10:22 +03:00
|
|
|
if (it.second->GetBuffer ())
|
2023-05-03 16:59:35 +03:00
|
|
|
{
|
2023-02-13 03:10:22 +03:00
|
|
|
// we have something to save
|
|
|
|
it.second->SaveToFile (m_Storage.Path(ident));
|
|
|
|
it.second->SetUnreachable (false);
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update
|
2023-02-13 03:10:22 +03:00
|
|
|
it.second->DeleteBuffer ();
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2013-11-20 16:46:09 +04:00
|
|
|
it.second->SetUpdated (false);
|
2016-02-11 03:00:00 +03:00
|
|
|
updatedCount++;
|
|
|
|
continue;
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2023-02-06 21:19:41 +03:00
|
|
|
if (it.second->GetProfile ()->IsUnreachable ())
|
|
|
|
it.second->SetUnreachable (true);
|
2021-08-26 22:13:58 +03:00
|
|
|
// make router reachable back if too few routers or floodfills
|
2022-12-12 01:03:53 +03:00
|
|
|
if (it.second->IsUnreachable () && (total - deletedCount < NETDB_MIN_ROUTERS || isLowRate ||
|
2022-05-20 19:56:05 +03:00
|
|
|
(it.second->IsFloodfill () && totalFloodfills - deletedFloodfillsCount < NETDB_MIN_FLOODFILLS)))
|
2021-11-27 23:30:35 +03:00
|
|
|
it.second->SetUnreachable (false);
|
2023-02-22 05:33:30 +03:00
|
|
|
if (!it.second->IsUnreachable ())
|
2023-05-03 16:59:35 +03:00
|
|
|
{
|
2023-02-22 05:33:30 +03:00
|
|
|
// find & mark expired routers
|
2023-04-06 04:49:49 +03:00
|
|
|
if (!it.second->GetCompatibleTransports (true)) // non reachable by any transport
|
2023-04-06 04:30:36 +03:00
|
|
|
it.second->SetUnreachable (true);
|
2023-02-22 05:33:30 +03:00
|
|
|
else if (ts + NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < it.second->GetTimestamp ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NetDb: RouterInfo is from future for ", (it.second->GetTimestamp () - ts)/1000LL, " seconds");
|
|
|
|
it.second->SetUnreachable (true);
|
|
|
|
}
|
2023-10-31 03:02:48 +03:00
|
|
|
else if (checkForExpiration)
|
|
|
|
{
|
2023-10-31 20:20:04 +03:00
|
|
|
if (ts > it.second->GetTimestamp () + expirationTimeout)
|
2023-10-31 03:02:48 +03:00
|
|
|
it.second->SetUnreachable (true);
|
2023-10-31 20:20:04 +03:00
|
|
|
else if ((ts > it.second->GetTimestamp () + expirationTimeout/2) && // more than half of expiration
|
|
|
|
total > NETDB_NUM_ROUTERS_THRESHOLD && !it.second->IsHighBandwidth() && // low bandwidth
|
|
|
|
!it.second->IsFloodfill() && (!i2p::context.IsFloodfill () || // non floodfill
|
|
|
|
(CreateRoutingKey (it.second->GetIdentHash ()) ^ i2p::context.GetIdentHash ()).metric[0] >= 0x02)) // different first 7 bits
|
|
|
|
it.second->SetUnreachable (true);
|
2023-10-31 03:02:48 +03:00
|
|
|
}
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2024-02-28 00:10:17 +03:00
|
|
|
// make router reachable back if connected now
|
|
|
|
if (it.second->IsUnreachable () && i2p::transport::transports.IsConnected (it.second->GetIdentHash ()))
|
|
|
|
it.second->SetUnreachable (false);
|
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
if (it.second->IsUnreachable ())
|
2016-02-23 04:51:32 +03:00
|
|
|
{
|
2021-08-26 22:13:58 +03:00
|
|
|
if (it.second->IsFloodfill ()) deletedFloodfillsCount++;
|
2016-02-11 03:00:00 +03:00
|
|
|
// delete RI file
|
2016-02-20 04:00:00 +03:00
|
|
|
m_Storage.Remove(ident);
|
2016-02-11 03:00:00 +03:00
|
|
|
deletedCount++;
|
2021-11-27 23:30:35 +03:00
|
|
|
if (total - deletedCount < NETDB_MIN_ROUTERS) checkForExpiration = false;
|
2016-02-11 03:00:00 +03:00
|
|
|
}
|
|
|
|
} // m_RouterInfos iteration
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2022-01-01 23:12:59 +03:00
|
|
|
m_RouterInfoBuffersPool.CleanUpMt ();
|
2022-12-07 22:08:27 +03:00
|
|
|
m_RouterInfoAddressesPool.CleanUpMt ();
|
2023-01-03 21:25:19 +03:00
|
|
|
m_RouterInfoAddressVectorsPool.CleanUpMt ();
|
2023-05-03 16:59:35 +03:00
|
|
|
m_IdentitiesPool.CleanUpMt ();
|
2023-01-03 21:25:19 +03:00
|
|
|
|
2016-02-11 03:00:00 +03:00
|
|
|
if (updatedCount > 0)
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers");
|
2013-12-22 20:29:57 +04:00
|
|
|
if (deletedCount > 0)
|
2014-11-21 21:29:19 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: Deleting ", deletedCount, " unreachable routers");
|
2014-11-21 21:29:19 +03:00
|
|
|
// clean up RouterInfos table
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2016-02-23 04:51:32 +03:00
|
|
|
for (auto it = m_RouterInfos.begin (); it != m_RouterInfos.end ();)
|
|
|
|
{
|
2023-11-23 21:06:50 +03:00
|
|
|
if (!it->second || it->second->IsUnreachable ())
|
2016-02-23 04:51:32 +03:00
|
|
|
it = m_RouterInfos.erase (it);
|
2023-02-12 00:22:02 +03:00
|
|
|
else
|
2023-05-03 16:59:35 +03:00
|
|
|
{
|
2023-02-12 00:22:02 +03:00
|
|
|
it->second->DropProfile ();
|
|
|
|
it++;
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2016-02-23 04:51:32 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2019-02-25 02:26:58 +03:00
|
|
|
// clean up expired floodfills or not floodfills anymore
|
2016-02-23 04:51:32 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2023-02-22 23:58:20 +03:00
|
|
|
m_Floodfills.Cleanup ([](const std::shared_ptr<RouterInfo>& r)->bool
|
|
|
|
{
|
|
|
|
return r && r->IsFloodfill () && !r->IsUnreachable ();
|
|
|
|
});
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
}
|
2013-11-20 16:46:09 +04:00
|
|
|
}
|
2013-11-28 06:28:32 +04:00
|
|
|
|
2020-10-04 00:20:04 +03:00
|
|
|
void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete, bool direct)
|
2014-01-03 06:22:48 +04:00
|
|
|
{
|
2024-02-09 23:24:48 +03:00
|
|
|
if (direct && i2p::transport::transports.RoutesRestricted ()) direct = false; // always use tunnels for restricted routes
|
2024-01-15 01:16:31 +03:00
|
|
|
auto dest = m_Requests.CreateRequest (destination, false, direct, requestComplete); // non-exploratory
|
2015-04-09 19:45:00 +03:00
|
|
|
if (!dest)
|
2015-02-04 00:14:33 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already");
|
2018-01-06 06:48:51 +03:00
|
|
|
return;
|
2015-02-10 06:19:29 +03:00
|
|
|
}
|
2015-04-09 19:45:00 +03:00
|
|
|
|
2015-04-11 02:49:58 +03:00
|
|
|
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
|
2014-12-26 05:13:51 +03:00
|
|
|
if (floodfill)
|
2020-10-04 00:20:04 +03:00
|
|
|
{
|
2021-03-05 16:41:44 +03:00
|
|
|
if (direct && !floodfill->IsReachableFrom (i2p::context.GetRouterInfo ()) &&
|
2022-05-20 19:56:05 +03:00
|
|
|
!i2p::transport::transports.IsConnected (floodfill->GetIdentHash ()))
|
2021-03-05 16:41:44 +03:00
|
|
|
direct = false; // floodfill can't be reached directly
|
2020-10-04 00:20:04 +03:00
|
|
|
if (direct)
|
2024-01-30 03:54:43 +03:00
|
|
|
{
|
|
|
|
auto msg = dest->CreateRequestMessage (floodfill->GetIdentHash ());
|
2024-04-26 01:52:10 +03:00
|
|
|
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests.SendNextRequest (dest); };
|
2024-01-30 03:54:43 +03:00
|
|
|
transports.SendMessage (floodfill->GetIdentHash (), msg);
|
|
|
|
}
|
2020-10-04 00:20:04 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
2021-11-06 22:44:56 +03:00
|
|
|
auto outbound = pool ? pool->GetNextOutboundTunnel (nullptr, floodfill->GetCompatibleTransports (false)) : nullptr;
|
|
|
|
auto inbound = pool ? pool->GetNextInboundTunnel (nullptr, floodfill->GetCompatibleTransports (true)) : nullptr;
|
2020-10-04 00:20:04 +03:00
|
|
|
if (outbound && inbound)
|
2023-04-25 02:25:26 +03:00
|
|
|
{
|
|
|
|
auto msg = dest->CreateRequestMessage (floodfill, inbound);
|
2024-04-26 01:52:10 +03:00
|
|
|
msg->onDrop = [this, dest]() { if (dest->IsActive ()) this->m_Requests.SendNextRequest (dest); };
|
2023-05-03 16:59:35 +03:00
|
|
|
outbound->SendTunnelDataMsgTo (floodfill->GetIdentHash (), 0,
|
2023-04-25 02:25:26 +03:00
|
|
|
i2p::garlic::WrapECIESX25519MessageForRouter (msg, floodfill->GetIdentity ()->GetEncryptionPublicKey ()));
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2020-10-04 00:20:04 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no tunnels found");
|
2021-11-27 23:30:35 +03:00
|
|
|
m_Requests.RequestComplete (destination, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-26 05:13:51 +03:00
|
|
|
else
|
2014-01-27 00:24:21 +04:00
|
|
|
{
|
2016-01-18 03:00:00 +03:00
|
|
|
LogPrint (eLogError, "NetDb: ", destination.ToBase64(), " destination requested, but no floodfills found");
|
2015-04-09 19:45:00 +03:00
|
|
|
m_Requests.RequestComplete (destination, nullptr);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-11-14 20:05:44 +03:00
|
|
|
|
2024-01-15 01:16:31 +03:00
|
|
|
void NetDb::RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploratory, RequestedDestination::RequestComplete requestComplete)
|
2016-11-14 20:05:44 +03:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2024-01-15 01:16:31 +03:00
|
|
|
auto dest = m_Requests.CreateRequest (destination, exploratory, true, requestComplete); // non-exploratory
|
2016-11-14 20:05:44 +03:00
|
|
|
if (!dest)
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Destination ", destination.ToBase64(), " is requested already");
|
2018-01-06 06:48:51 +03:00
|
|
|
return;
|
2016-11-14 20:05:44 +03:00
|
|
|
}
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint(eLogInfo, "NetDb: Destination ", destination.ToBase64(), " being requested directly from ", from.ToBase64());
|
2016-11-14 20:05:44 +03:00
|
|
|
// direct
|
2018-01-06 06:48:51 +03:00
|
|
|
transports.SendMessage (from, dest->CreateRequestMessage (nullptr, nullptr));
|
|
|
|
}
|
|
|
|
|
2018-11-21 19:23:48 +03:00
|
|
|
void NetDb::HandleNTCP2RouterInfoMsg (std::shared_ptr<const I2NPMessage> m)
|
|
|
|
{
|
|
|
|
uint8_t flood = m->GetPayload ()[0] & NTCP2_ROUTER_INFO_FLAG_REQUEST_FLOOD;
|
2018-11-21 21:24:54 +03:00
|
|
|
bool updated;
|
|
|
|
auto ri = AddRouterInfo (m->GetPayload () + 1, m->GetPayloadLength () - 1, updated); // without flags
|
|
|
|
if (flood && updated && context.IsFloodfill () && ri)
|
2018-11-21 19:23:48 +03:00
|
|
|
{
|
2018-11-21 21:24:54 +03:00
|
|
|
auto floodMsg = CreateDatabaseStoreMsg (ri, 0); // replyToken = 0
|
|
|
|
Flood (ri->GetIdentHash (), floodMsg);
|
2018-11-21 19:23:48 +03:00
|
|
|
}
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2015-07-04 04:27:40 +03:00
|
|
|
void NetDb::HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> m)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-09-30 00:41:41 +04:00
|
|
|
const uint8_t * buf = m->GetPayload ();
|
2018-01-06 06:48:51 +03:00
|
|
|
size_t len = m->GetSize ();
|
2022-11-22 23:40:48 +03:00
|
|
|
if (len < DATABASE_STORE_HEADER_SIZE)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NetDb: Database store msg is too short ", len, ". Dropped");
|
|
|
|
return;
|
2023-01-03 21:25:19 +03:00
|
|
|
}
|
2015-04-12 23:59:59 +03:00
|
|
|
IdentHash ident (buf + DATABASE_STORE_KEY_OFFSET);
|
|
|
|
if (ident.IsZero ())
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: Database store with zero ident, dropped");
|
2015-04-12 23:59:59 +03:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-01-03 05:11:40 +03:00
|
|
|
uint32_t replyToken = bufbe32toh (buf + DATABASE_STORE_REPLY_TOKEN_OFFSET);
|
|
|
|
size_t offset = DATABASE_STORE_HEADER_SIZE;
|
|
|
|
if (replyToken)
|
2015-01-30 23:13:09 +03:00
|
|
|
{
|
2022-11-22 23:40:48 +03:00
|
|
|
if (len < offset + 36) // 32 + 4
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NetDb: Database store msg with reply token is too short ", len, ". Dropped");
|
|
|
|
return;
|
2023-01-03 21:25:19 +03:00
|
|
|
}
|
2015-01-30 23:13:09 +03:00
|
|
|
uint32_t tunnelID = bufbe32toh (buf + offset);
|
2015-02-02 21:06:02 +03:00
|
|
|
offset += 4;
|
2023-04-28 04:37:30 +03:00
|
|
|
if (replyToken != 0xFFFFFFFFU) // if not caught on OBEP or IBGW
|
2023-05-03 16:59:35 +03:00
|
|
|
{
|
2024-03-04 22:30:49 +03:00
|
|
|
IdentHash replyIdent(buf + offset);
|
2023-04-28 04:37:30 +03:00
|
|
|
auto deliveryStatus = CreateDeliveryStatusMsg (replyToken);
|
|
|
|
if (!tunnelID) // send response directly
|
2024-03-04 22:30:49 +03:00
|
|
|
transports.SendMessage (replyIdent, deliveryStatus);
|
2015-01-30 23:13:09 +03:00
|
|
|
else
|
2023-04-28 04:37:30 +03:00
|
|
|
{
|
2024-03-04 22:30:49 +03:00
|
|
|
bool direct = true;
|
|
|
|
if (!i2p::transport::transports.IsConnected (replyIdent))
|
|
|
|
{
|
|
|
|
auto r = FindRouter (replyIdent);
|
|
|
|
if (r && !r->IsReachableFrom (i2p::context.GetRouterInfo ()))
|
|
|
|
direct = false;
|
|
|
|
}
|
|
|
|
if (direct) // send response directly to IBGW
|
|
|
|
transports.SendMessage (replyIdent, i2p::CreateTunnelGatewayMsg (tunnelID, deliveryStatus));
|
2023-04-28 04:37:30 +03:00
|
|
|
else
|
2024-03-04 22:30:49 +03:00
|
|
|
{
|
|
|
|
// send response through exploratory tunnel
|
|
|
|
auto pool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
|
|
|
auto outbound = pool ? pool->GetNextOutboundTunnel () : nullptr;
|
|
|
|
if (outbound)
|
|
|
|
outbound->SendTunnelDataMsgTo (replyIdent, tunnelID, deliveryStatus);
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "NetDb: No outbound tunnels for DatabaseStore reply found");
|
|
|
|
}
|
2023-04-28 04:37:30 +03:00
|
|
|
}
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2015-02-02 21:06:02 +03:00
|
|
|
offset += 32;
|
2015-01-30 23:13:09 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
// we must send reply back before this check
|
2016-06-30 20:15:36 +03:00
|
|
|
if (ident == i2p::context.GetIdentHash ())
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: Database store with own RouterInfo received, dropped");
|
2016-06-30 20:15:36 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
size_t payloadOffset = offset;
|
2016-02-17 23:36:55 +03:00
|
|
|
|
|
|
|
bool updated = false;
|
2019-02-22 18:32:50 +03:00
|
|
|
uint8_t storeType = buf[DATABASE_STORE_TYPE_OFFSET];
|
2018-12-21 23:00:03 +03:00
|
|
|
if (storeType) // LeaseSet or LeaseSet2
|
2013-11-19 05:37:38 +04:00
|
|
|
{
|
2023-01-07 18:54:49 +03:00
|
|
|
if (len > MAX_LS_BUFFER_SIZE + offset)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "NetDb: Database store message is too long ", len);
|
|
|
|
return;
|
2023-02-11 09:41:51 +03:00
|
|
|
}
|
2023-06-06 01:00:07 +03:00
|
|
|
if (!context.IsFloodfill ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo, "NetDb: Not Floodfill, LeaseSet store request ignored for ", ident.ToBase32());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (!m->from) // unsolicited LS must be received directly
|
2019-02-22 18:32:50 +03:00
|
|
|
{
|
2019-01-17 03:00:17 +03:00
|
|
|
if (storeType == NETDB_STORE_TYPE_LEASESET) // 1
|
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogDebug))
|
|
|
|
LogPrint (eLogDebug, "NetDb: Store request: LeaseSet for ", ident.ToBase32());
|
2019-01-17 03:00:17 +03:00
|
|
|
updated = AddLeaseSet (ident, buf + offset, len - offset);
|
|
|
|
}
|
2019-02-22 18:32:50 +03:00
|
|
|
else // all others are considered as LeaseSet2
|
2019-01-17 03:00:17 +03:00
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogDebug))
|
|
|
|
LogPrint (eLogDebug, "NetDb: Store request: LeaseSet2 of type ", int(storeType), " for ", ident.ToBase32());
|
2019-01-17 03:00:17 +03:00
|
|
|
updated = AddLeaseSet2 (ident, buf + offset, len - offset, storeType);
|
|
|
|
}
|
2019-02-22 18:32:50 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2018-12-21 23:00:03 +03:00
|
|
|
else // RouterInfo
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2024-02-09 05:56:25 +03:00
|
|
|
if (CheckLogLevel (eLogDebug))
|
|
|
|
LogPrint (eLogDebug, "NetDb: Store request: RouterInfo ", ident.ToBase64());
|
2014-12-30 01:04:02 +03:00
|
|
|
size_t size = bufbe16toh (buf + offset);
|
2015-02-03 04:15:49 +03:00
|
|
|
offset += 2;
|
2019-05-23 16:32:07 +03:00
|
|
|
if (size > MAX_RI_BUFFER_SIZE || size > len - offset)
|
2013-11-28 06:28:32 +04:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: Invalid RouterInfo length ", (int)size);
|
2013-11-28 06:28:32 +04:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2019-05-23 16:32:07 +03:00
|
|
|
uint8_t uncompressed[MAX_RI_BUFFER_SIZE];
|
|
|
|
size_t uncompressedSize = m_Inflator.Inflate (buf + offset, size, uncompressed, MAX_RI_BUFFER_SIZE);
|
|
|
|
if (uncompressedSize && uncompressedSize < MAX_RI_BUFFER_SIZE)
|
2016-02-17 23:36:55 +03:00
|
|
|
updated = AddRouterInfo (ident, uncompressed, uncompressedSize);
|
2016-09-12 04:36:17 +03:00
|
|
|
else
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: Decompression failed ", uncompressedSize);
|
2016-09-12 04:36:17 +03:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 23:36:55 +03:00
|
|
|
|
|
|
|
if (replyToken && context.IsFloodfill () && updated)
|
|
|
|
{
|
|
|
|
// flood updated
|
|
|
|
auto floodMsg = NewI2NPShortMessage ();
|
2018-01-06 06:48:51 +03:00
|
|
|
uint8_t * payload = floodMsg->GetPayload ();
|
2016-02-17 23:36:55 +03:00
|
|
|
memcpy (payload, buf, 33); // key + type
|
|
|
|
htobe32buf (payload + DATABASE_STORE_REPLY_TOKEN_OFFSET, 0); // zero reply token
|
2016-06-30 16:45:06 +03:00
|
|
|
size_t msgLen = len - payloadOffset;
|
2016-02-17 23:36:55 +03:00
|
|
|
floodMsg->len += DATABASE_STORE_HEADER_SIZE + msgLen;
|
|
|
|
if (floodMsg->len < floodMsg->maxLen)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2016-02-17 23:36:55 +03:00
|
|
|
memcpy (payload + DATABASE_STORE_HEADER_SIZE, buf + payloadOffset, msgLen);
|
2018-01-06 06:48:51 +03:00
|
|
|
floodMsg->FillI2NPMessageHeader (eI2NPDatabaseStore);
|
2018-11-21 21:24:54 +03:00
|
|
|
Flood (ident, floodMsg);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2016-02-17 23:36:55 +03:00
|
|
|
else
|
2016-06-30 16:45:06 +03:00
|
|
|
LogPrint (eLogError, "NetDb: Database store message is too long ", floodMsg->len);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
|
2015-07-04 04:27:40 +03:00
|
|
|
void NetDb::HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg)
|
2013-11-23 05:41:28 +04:00
|
|
|
{
|
2015-07-04 04:27:40 +03:00
|
|
|
const uint8_t * buf = msg->GetPayload ();
|
2013-11-23 05:41:28 +04:00
|
|
|
char key[48];
|
|
|
|
int l = i2p::data::ByteStreamToBase64 (buf, 32, key, 48);
|
|
|
|
key[l] = 0;
|
|
|
|
int num = buf[32]; // num
|
2015-12-18 07:03:07 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: DatabaseSearchReply for ", key, " num=", num);
|
2015-04-09 19:45:00 +03:00
|
|
|
IdentHash ident (buf);
|
2018-01-06 06:48:51 +03:00
|
|
|
auto dest = m_Requests.FindRequest (ident);
|
2015-04-09 19:45:00 +03:00
|
|
|
if (dest)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-01-05 18:53:44 +04:00
|
|
|
if (num > 0)
|
2024-01-15 01:16:31 +03:00
|
|
|
// try to send next requests
|
|
|
|
m_Requests.SendNextRequest (dest);
|
2014-01-05 18:53:44 +04:00
|
|
|
else
|
2018-07-10 12:39:21 +03:00
|
|
|
// no more requests for destination possible. delete it
|
2015-04-09 19:45:00 +03:00
|
|
|
m_Requests.RequestComplete (ident, nullptr);
|
2014-01-05 18:53:44 +04:00
|
|
|
}
|
2016-11-14 20:05:44 +03:00
|
|
|
else if(!m_FloodfillBootstrap)
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Requested destination for ", key, " not found");
|
2015-01-10 17:07:07 +03:00
|
|
|
|
|
|
|
// try responses
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
{
|
2015-07-04 04:27:40 +03:00
|
|
|
const uint8_t * router = buf + 33 + i*32;
|
2015-01-10 17:07:07 +03:00
|
|
|
char peerHash[48];
|
|
|
|
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48);
|
|
|
|
peerHash[l1] = 0;
|
2015-12-18 07:03:07 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: ", i, ": ", peerHash);
|
2015-01-10 17:07:07 +03:00
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
auto r = FindRouter (router);
|
|
|
|
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
|
|
|
|
{
|
2015-01-10 17:07:07 +03:00
|
|
|
// router with ident not found or too old (1 hour)
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: Found new/outdated router. Requesting RouterInfo...");
|
2016-11-14 20:05:44 +03:00
|
|
|
if(m_FloodfillBootstrap)
|
|
|
|
RequestDestinationFrom(router, m_FloodfillBootstrap->GetIdentHash(), true);
|
2024-03-03 15:42:39 +03:00
|
|
|
else if (!IsRouterBanned (router))
|
2016-11-14 20:05:44 +03:00
|
|
|
RequestDestination (router);
|
2024-03-03 15:42:39 +03:00
|
|
|
else
|
|
|
|
LogPrint (eLogDebug, "NetDb: Router ", peerHash, " is banned. Skipped");
|
2015-01-10 17:07:07 +03:00
|
|
|
}
|
|
|
|
else
|
2015-12-18 07:03:07 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: [:|||:]");
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-04 04:27:40 +03:00
|
|
|
void NetDb::HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg)
|
2014-07-23 19:10:03 +04:00
|
|
|
{
|
2015-07-04 04:27:40 +03:00
|
|
|
const uint8_t * buf = msg->GetPayload ();
|
2015-04-12 23:59:59 +03:00
|
|
|
IdentHash ident (buf);
|
|
|
|
if (ident.IsZero ())
|
|
|
|
{
|
2015-12-18 07:03:07 +03:00
|
|
|
LogPrint (eLogError, "NetDb: DatabaseLookup for zero ident. Ignored");
|
2015-04-12 23:59:59 +03:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-07-23 19:10:03 +04:00
|
|
|
char key[48];
|
|
|
|
int l = i2p::data::ByteStreamToBase64 (buf, 32, key, 48);
|
|
|
|
key[l] = 0;
|
2016-06-30 20:25:20 +03:00
|
|
|
|
|
|
|
IdentHash replyIdent(buf + 32);
|
2016-07-15 19:49:45 +03:00
|
|
|
uint8_t flag = buf[64];
|
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2017-12-07 16:26:28 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: DatabaseLookup for ", key, " received flags=", (int)flag);
|
2015-02-03 06:34:55 +03:00
|
|
|
uint8_t lookupType = flag & DATABASE_LOOKUP_TYPE_FLAGS_MASK;
|
2018-01-06 06:48:51 +03:00
|
|
|
const uint8_t * excluded = buf + 65;
|
2014-07-23 19:10:03 +04:00
|
|
|
uint32_t replyTunnelID = 0;
|
2015-02-03 04:15:49 +03:00
|
|
|
if (flag & DATABASE_LOOKUP_DELIVERY_FLAG) //reply to tunnel
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
2016-07-15 19:49:45 +03:00
|
|
|
replyTunnelID = bufbe32toh (excluded);
|
2014-07-28 22:25:14 +04:00
|
|
|
excluded += 4;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
uint16_t numExcluded = bufbe16toh (excluded);
|
2014-07-28 22:25:14 +04:00
|
|
|
excluded += 2;
|
2022-07-30 21:28:09 +03:00
|
|
|
if (numExcluded > 512 || (excluded - buf) + numExcluded*32 > (int)msg->GetPayloadLength ())
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
2022-07-30 21:28:09 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Number of excluded peers", numExcluded, " is too much");
|
2016-07-15 19:49:45 +03:00
|
|
|
return;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2015-06-22 05:29:50 +03:00
|
|
|
std::shared_ptr<I2NPMessage> replyMsg;
|
2015-02-03 06:34:55 +03:00
|
|
|
if (lookupType == DATABASE_LOOKUP_TYPE_EXPLORATORY_LOOKUP)
|
2014-07-25 06:01:07 +04:00
|
|
|
{
|
2023-12-31 03:49:16 +03:00
|
|
|
if (!context.IsFloodfill ())
|
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "NetDb: Exploratory lookup to non-floodfill dropped");
|
|
|
|
return;
|
|
|
|
}
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: Exploratory close to ", key, " ", numExcluded, " excluded");
|
2014-07-29 06:28:11 +04:00
|
|
|
std::set<IdentHash> excludedRouters;
|
2022-07-30 21:28:09 +03:00
|
|
|
const uint8_t * excluded_ident = excluded;
|
2014-07-29 06:28:11 +04:00
|
|
|
for (int i = 0; i < numExcluded; i++)
|
|
|
|
{
|
2022-07-30 21:28:09 +03:00
|
|
|
excludedRouters.insert (excluded_ident);
|
|
|
|
excluded_ident += 32;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2015-02-02 03:58:26 +03:00
|
|
|
std::vector<IdentHash> routers;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2015-04-12 23:59:59 +03:00
|
|
|
auto r = GetClosestNonFloodfill (ident, excludedRouters);
|
2015-02-03 06:34:55 +03:00
|
|
|
if (r)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-02-03 06:34:55 +03:00
|
|
|
routers.push_back (r->GetIdentHash ());
|
|
|
|
excludedRouters.insert (r->GetIdentHash ());
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-03 17:15:49 +03:00
|
|
|
replyMsg = CreateDatabaseSearchReply (ident, routers);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-07-31 20:59:43 +04:00
|
|
|
else
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
if (lookupType == DATABASE_LOOKUP_TYPE_ROUTERINFO_LOOKUP ||
|
2022-05-20 19:56:05 +03:00
|
|
|
lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2023-12-31 03:49:16 +03:00
|
|
|
// try to find router
|
2015-04-12 23:59:59 +03:00
|
|
|
auto router = FindRouter (ident);
|
2023-02-14 03:18:02 +03:00
|
|
|
if (router && !router->IsUnreachable ())
|
2015-04-12 22:54:28 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogDebug, "NetDb: Requested RouterInfo ", key, " found");
|
2023-02-14 23:44:35 +03:00
|
|
|
if (PopulateRouterInfoBuffer (router))
|
2015-11-03 17:15:49 +03:00
|
|
|
replyMsg = CreateDatabaseStoreMsg (router);
|
2015-04-12 22:54:28 +03:00
|
|
|
}
|
2015-02-03 06:34:55 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
if (!replyMsg && (lookupType == DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP ||
|
2019-02-22 18:32:50 +03:00
|
|
|
lookupType == DATABASE_LOOKUP_TYPE_NORMAL_LOOKUP))
|
2015-02-03 06:34:55 +03:00
|
|
|
{
|
2023-12-31 03:49:16 +03:00
|
|
|
// try to find leaseset
|
|
|
|
if (context.IsFloodfill ())
|
|
|
|
{
|
|
|
|
auto leaseSet = FindLeaseSet (ident);
|
|
|
|
if (!leaseSet)
|
|
|
|
{
|
|
|
|
// no leaseset found
|
|
|
|
LogPrint(eLogDebug, "NetDb: Requested LeaseSet not found for ", ident.ToBase32());
|
|
|
|
}
|
|
|
|
else if (!leaseSet->IsExpired ()) // we don't send back expired leasesets
|
|
|
|
{
|
|
|
|
LogPrint (eLogDebug, "NetDb: Requested LeaseSet ", key, " found");
|
|
|
|
replyMsg = CreateDatabaseStoreMsg (ident, leaseSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (lookupType == DATABASE_LOOKUP_TYPE_LEASESET_LOOKUP)
|
2015-02-03 06:34:55 +03:00
|
|
|
{
|
2023-12-31 03:49:16 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Explicit LeaseSet lookup to non-floodfill dropped");
|
|
|
|
return;
|
|
|
|
}
|
2015-02-03 06:34:55 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2015-02-03 06:34:55 +03:00
|
|
|
if (!replyMsg)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2016-12-06 02:39:01 +03:00
|
|
|
std::set<IdentHash> excludedRouters;
|
|
|
|
const uint8_t * exclude_ident = excluded;
|
|
|
|
for (int i = 0; i < numExcluded; i++)
|
|
|
|
{
|
|
|
|
excludedRouters.insert (exclude_ident);
|
|
|
|
exclude_ident += 32;
|
2015-06-11 18:43:35 +03:00
|
|
|
}
|
2016-12-06 02:39:01 +03:00
|
|
|
auto closestFloodfills = GetClosestFloodfills (ident, 3, excludedRouters, true);
|
|
|
|
if (closestFloodfills.empty ())
|
|
|
|
LogPrint (eLogWarning, "NetDb: Requested ", key, " not found, ", numExcluded, " peers excluded");
|
2016-03-22 20:10:02 +03:00
|
|
|
replyMsg = CreateDatabaseSearchReply (ident, closestFloodfills);
|
2022-07-30 21:28:09 +03:00
|
|
|
}
|
2015-02-03 06:34:55 +03:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
excluded += numExcluded * 32;
|
2014-07-24 23:59:00 +04:00
|
|
|
if (replyMsg)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-07-24 23:59:00 +04:00
|
|
|
if (replyTunnelID)
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
|
|
|
// encryption might be used though tunnel only
|
2020-04-21 22:14:03 +03:00
|
|
|
if (flag & (DATABASE_LOOKUP_ENCRYPTION_FLAG | DATABASE_LOOKUP_ECIES_FLAG)) // encrypted reply requested
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
2015-07-04 04:27:40 +03:00
|
|
|
const uint8_t * sessionKey = excluded;
|
2016-07-15 19:49:45 +03:00
|
|
|
const uint8_t numTags = excluded[32];
|
|
|
|
if (numTags)
|
2014-07-28 22:25:14 +04:00
|
|
|
{
|
2020-04-07 18:40:18 +03:00
|
|
|
if (flag & DATABASE_LOOKUP_ECIES_FLAG)
|
|
|
|
{
|
|
|
|
uint64_t tag;
|
|
|
|
memcpy (&tag, excluded + 33, 8);
|
2021-07-10 21:33:23 +03:00
|
|
|
replyMsg = i2p::garlic::WrapECIESX25519Message (replyMsg, sessionKey, tag);
|
2020-04-07 18:40:18 +03:00
|
|
|
}
|
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
2020-04-07 18:40:18 +03:00
|
|
|
const i2p::garlic::SessionTag sessionTag(excluded + 33); // take first tag
|
|
|
|
i2p::garlic::ElGamalAESSession garlic (sessionKey, sessionTag);
|
|
|
|
replyMsg = garlic.WrapSingleMessage (replyMsg);
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
|
|
|
if (!replyMsg)
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogError, "NetDb: Failed to wrap message");
|
2014-07-28 22:25:14 +04:00
|
|
|
}
|
2016-07-15 19:49:45 +03:00
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint(eLogWarning, "NetDb: Encrypted reply requested but no tags provided");
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2024-03-04 21:34:57 +03:00
|
|
|
bool direct = true;
|
2024-03-04 22:30:49 +03:00
|
|
|
if (!i2p::transport::transports.IsConnected (replyIdent))
|
2024-03-04 21:34:57 +03:00
|
|
|
{
|
|
|
|
auto r = FindRouter (replyIdent);
|
|
|
|
if (r && !r->IsReachableFrom (i2p::context.GetRouterInfo ()))
|
|
|
|
direct = false;
|
|
|
|
}
|
|
|
|
if (direct)
|
2016-06-30 20:25:20 +03:00
|
|
|
transports.SendMessage (replyIdent, i2p::CreateTunnelGatewayMsg (replyTunnelID, replyMsg));
|
2024-03-04 21:34:57 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
|
|
|
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
|
|
|
|
if (outbound)
|
|
|
|
outbound->SendTunnelDataMsgTo (replyIdent, replyTunnelID, replyMsg);
|
|
|
|
else
|
|
|
|
LogPrint (eLogWarning, "NetDb: Can't send lookup reply to ", replyIdent.ToBase64 (), ". Non reachable and no outbound tunnels");
|
|
|
|
}
|
2014-07-28 22:25:14 +04:00
|
|
|
}
|
2014-07-24 23:59:00 +04:00
|
|
|
else
|
2016-06-30 20:25:20 +03:00
|
|
|
transports.SendMessage (replyIdent, replyMsg);
|
2014-07-24 23:59:00 +04:00
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-07-23 19:10:03 +04:00
|
|
|
|
2014-06-17 06:30:34 +04:00
|
|
|
void NetDb::Explore (int numDestinations)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-07-03 21:41:36 +04:00
|
|
|
// new requests
|
2014-06-17 06:30:34 +04:00
|
|
|
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
|
2015-01-11 00:08:13 +03:00
|
|
|
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
|
|
|
|
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
|
2014-06-17 06:30:34 +04:00
|
|
|
bool throughTunnels = outbound && inbound;
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2014-06-17 05:23:22 +04:00
|
|
|
uint8_t randomHash[32];
|
2014-06-17 06:30:34 +04:00
|
|
|
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: Exploring new ", numDestinations, " routers ...");
|
2014-06-17 06:30:34 +04:00
|
|
|
for (int i = 0; i < numDestinations; i++)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
RAND_bytes (randomHash, 32);
|
2024-01-15 01:16:31 +03:00
|
|
|
auto dest = m_Requests.CreateRequest (randomHash, true, !throughTunnels); // exploratory
|
2015-04-09 19:45:00 +03:00
|
|
|
if (!dest)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
LogPrint (eLogWarning, "NetDb: Exploratory destination is requested already");
|
2018-01-06 06:48:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-17 06:30:34 +04:00
|
|
|
auto floodfill = GetClosestFloodfill (randomHash, dest->GetExcludedPeers ());
|
2018-01-06 06:48:51 +03:00
|
|
|
if (floodfill)
|
|
|
|
{
|
2015-03-18 03:56:51 +03:00
|
|
|
if (i2p::transport::transports.IsConnected (floodfill->GetIdentHash ()))
|
|
|
|
throughTunnels = false;
|
2014-06-17 06:30:34 +04:00
|
|
|
if (throughTunnels)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
2014-06-17 06:30:34 +04:00
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
|
|
|
floodfill->GetIdentHash (), 0,
|
2018-01-06 06:48:51 +03:00
|
|
|
CreateDatabaseStoreMsg () // tell floodfill about us
|
|
|
|
});
|
|
|
|
msgs.push_back (i2p::tunnel::TunnelMessageBlock
|
|
|
|
{
|
2014-06-17 06:30:34 +04:00
|
|
|
i2p::tunnel::eDeliveryTypeRouter,
|
2018-01-06 06:48:51 +03:00
|
|
|
floodfill->GetIdentHash (), 0,
|
2015-06-22 22:47:45 +03:00
|
|
|
dest->CreateRequestMessage (floodfill, inbound) // explore
|
2018-01-06 06:48:51 +03:00
|
|
|
});
|
|
|
|
}
|
2014-06-17 06:30:34 +04:00
|
|
|
else
|
2014-10-21 20:25:53 +04:00
|
|
|
i2p::transport::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-02-12 07:19:51 +04:00
|
|
|
else
|
2015-04-09 19:45:00 +03:00
|
|
|
m_Requests.RequestComplete (randomHash, nullptr);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-06-17 06:30:34 +04:00
|
|
|
if (throughTunnels && msgs.size () > 0)
|
2023-04-04 20:19:08 +03:00
|
|
|
outbound->SendTunnelDataMsgs (msgs);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2013-11-19 05:37:38 +04:00
|
|
|
|
2018-11-21 21:24:54 +03:00
|
|
|
void NetDb::Flood (const IdentHash& ident, std::shared_ptr<I2NPMessage> floodMsg)
|
|
|
|
{
|
|
|
|
std::set<IdentHash> excluded;
|
|
|
|
excluded.insert (i2p::context.GetIdentHash ()); // don't flood to itself
|
|
|
|
excluded.insert (ident); // don't flood back
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
auto floodfill = GetClosestFloodfill (ident, excluded);
|
|
|
|
if (floodfill)
|
|
|
|
{
|
|
|
|
auto h = floodfill->GetIdentHash();
|
|
|
|
LogPrint(eLogDebug, "NetDb: Flood lease set for ", ident.ToBase32(), " to ", h.ToBase64());
|
|
|
|
transports.SendMessage (h, CopyI2NPMessage(floodMsg));
|
|
|
|
excluded.insert (h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 (
|
2018-01-06 06:48:51 +03:00
|
|
|
[](std::shared_ptr<const RouterInfo> router)->bool
|
|
|
|
{
|
|
|
|
return !router->IsHidden ();
|
2014-09-26 05:08:20 +04:00
|
|
|
});
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2023-07-11 20:16:35 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith,
|
|
|
|
bool reverse, bool endpoint) const
|
2014-09-25 05:45:19 +04:00
|
|
|
{
|
2014-09-26 05:08:20 +04:00
|
|
|
return GetRandomRouter (
|
2023-07-11 20:16:35 +03:00
|
|
|
[compatibleWith, reverse, endpoint](std::shared_ptr<const RouterInfo> router)->bool
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
|
|
|
return !router->IsHidden () && router != compatibleWith &&
|
2023-07-18 02:49:42 +03:00
|
|
|
(reverse ? (compatibleWith->IsReachableFrom (*router) && router->GetCompatibleTransports (true)):
|
2024-04-15 03:09:06 +03:00
|
|
|
router->IsReachableFrom (*compatibleWith)) && !router->IsNAT2NATOnly (*compatibleWith) &&
|
2023-07-11 20:16:35 +03:00
|
|
|
router->IsECIES () && !router->IsHighCongestion (false) &&
|
|
|
|
(!endpoint || (router->IsV4 () && (!reverse || router->IsPublished (true)))); // endpoint must be ipv4 and published if inbound(reverse)
|
2014-09-26 05:08:20 +04:00
|
|
|
});
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-09-25 05:45:19 +04:00
|
|
|
|
2022-06-02 04:51:02 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomSSU2PeerTestRouter (bool v4, const std::set<IdentHash>& excluded) const
|
|
|
|
{
|
|
|
|
return GetRandomRouter (
|
|
|
|
[v4, &excluded](std::shared_ptr<const RouterInfo> router)->bool
|
|
|
|
{
|
|
|
|
return !router->IsHidden () && router->IsECIES () &&
|
|
|
|
router->IsSSU2PeerTesting (v4) && !excluded.count (router->GetIdentHash ());
|
|
|
|
});
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
|
2022-07-20 01:38:58 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomSSU2Introducer (bool v4, const std::set<IdentHash>& excluded) const
|
|
|
|
{
|
|
|
|
return GetRandomRouter (
|
|
|
|
[v4, &excluded](std::shared_ptr<const RouterInfo> router)->bool
|
|
|
|
{
|
2022-10-09 20:24:43 +03:00
|
|
|
return !router->IsHidden () && router->IsSSU2Introducer (v4) &&
|
2022-07-20 01:38:58 +03:00
|
|
|
!excluded.count (router->GetIdentHash ());
|
|
|
|
});
|
|
|
|
}
|
2022-10-09 20:24:43 +03:00
|
|
|
|
2023-07-11 20:16:35 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith,
|
|
|
|
bool reverse, bool endpoint) const
|
2014-09-26 05:08:20 +04:00
|
|
|
{
|
2024-04-22 20:03:59 +03:00
|
|
|
bool checkIsReal = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < NETDB_TUNNEL_CREATION_RATE_THRESHOLD && // too low rate
|
|
|
|
context.GetUptime () > NETDB_CHECK_FOR_EXPIRATION_UPTIME; // after 10 minutes uptime
|
2014-09-26 05:08:20 +04:00
|
|
|
return GetRandomRouter (
|
2024-04-22 20:03:59 +03:00
|
|
|
[compatibleWith, reverse, endpoint, checkIsReal](std::shared_ptr<const RouterInfo> router)->bool
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2014-11-21 18:46:11 +03:00
|
|
|
return !router->IsHidden () && router != compatibleWith &&
|
2023-07-18 02:49:42 +03:00
|
|
|
(reverse ? (compatibleWith->IsReachableFrom (*router) && router->GetCompatibleTransports (true)) :
|
2024-04-15 03:09:06 +03:00
|
|
|
router->IsReachableFrom (*compatibleWith)) && !router->IsNAT2NATOnly (*compatibleWith) &&
|
2021-11-27 23:30:35 +03:00
|
|
|
(router->GetCaps () & RouterInfo::eHighBandwidth) &&
|
2021-09-03 20:30:01 +03:00
|
|
|
router->GetVersion () >= NETDB_MIN_HIGHBANDWIDTH_VERSION &&
|
2023-07-11 20:16:35 +03:00
|
|
|
router->IsECIES () && !router->IsHighCongestion (true) &&
|
2024-04-22 20:03:59 +03:00
|
|
|
(!checkIsReal || router->GetProfile ()->IsReal ()) &&
|
2023-07-11 20:16:35 +03:00
|
|
|
(!endpoint || (router->IsV4 () && (!reverse || router->IsPublished (true)))); // endpoint must be ipv4 and published if inbound(reverse)
|
|
|
|
|
2014-09-26 05:08:20 +04:00
|
|
|
});
|
2018-01-06 06:48:51 +03: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
|
|
|
{
|
2016-06-01 03:00:00 +03:00
|
|
|
if (m_RouterInfos.empty())
|
2024-02-20 18:39:48 +03:00
|
|
|
return nullptr;
|
2021-08-03 22:43:58 +03:00
|
|
|
uint16_t inds[3];
|
|
|
|
RAND_bytes ((uint8_t *)inds, sizeof (inds));
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2023-07-09 03:07:04 +03:00
|
|
|
auto count = m_RouterInfos.size ();
|
|
|
|
if(count == 0) return nullptr;
|
|
|
|
inds[0] %= count;
|
2021-07-27 00:51:32 +03:00
|
|
|
auto it = m_RouterInfos.begin ();
|
2021-08-03 22:43:58 +03:00
|
|
|
std::advance (it, inds[0]);
|
2021-07-27 00:51:32 +03:00
|
|
|
// try random router
|
|
|
|
if (it != m_RouterInfos.end () && !it->second->IsUnreachable () && filter (it->second))
|
|
|
|
return it->second;
|
2021-08-03 22:43:58 +03:00
|
|
|
// try some routers around
|
|
|
|
auto it1 = m_RouterInfos.begin ();
|
2021-11-27 23:30:35 +03:00
|
|
|
if (inds[0])
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2021-08-03 22:43:58 +03:00
|
|
|
// before
|
2021-11-27 23:30:35 +03:00
|
|
|
inds[1] %= inds[0];
|
2021-08-04 02:26:09 +03:00
|
|
|
std::advance (it1, (inds[1] + inds[0])/2);
|
2021-11-27 23:30:35 +03:00
|
|
|
}
|
2021-08-04 02:26:09 +03:00
|
|
|
else
|
|
|
|
it1 = it;
|
2021-08-03 22:43:58 +03:00
|
|
|
auto it2 = it;
|
|
|
|
if (inds[0] < m_RouterInfos.size () - 1)
|
|
|
|
{
|
|
|
|
// after
|
2021-08-04 02:26:09 +03:00
|
|
|
inds[2] %= (m_RouterInfos.size () - 1 - inds[0]); inds[2] /= 2;
|
2021-08-03 22:43:58 +03:00
|
|
|
std::advance (it2, inds[2]);
|
|
|
|
}
|
|
|
|
// it1 - from, it2 - to
|
2021-11-27 23:30:35 +03:00
|
|
|
it = it1;
|
2021-08-03 22:43:58 +03:00
|
|
|
while (it != it2 && it != m_RouterInfos.end ())
|
|
|
|
{
|
|
|
|
if (!it->second->IsUnreachable () && filter (it->second))
|
|
|
|
return it->second;
|
|
|
|
it++;
|
|
|
|
}
|
2021-11-12 19:33:51 +03:00
|
|
|
// still not found, try from the beginning
|
2021-08-03 22:43:58 +03:00
|
|
|
it = m_RouterInfos.begin ();
|
|
|
|
while (it != it1 && it != m_RouterInfos.end ())
|
|
|
|
{
|
|
|
|
if (!it->second->IsUnreachable () && filter (it->second))
|
|
|
|
return it->second;
|
|
|
|
it++;
|
|
|
|
}
|
2021-11-12 19:33:51 +03:00
|
|
|
// still not found, try to the beginning
|
2021-08-03 22:43:58 +03:00
|
|
|
it = it2;
|
|
|
|
while (it != m_RouterInfos.end ())
|
|
|
|
{
|
|
|
|
if (!it->second->IsUnreachable () && filter (it->second))
|
|
|
|
return it->second;
|
|
|
|
it++;
|
|
|
|
}
|
2014-03-19 22:08:09 +04:00
|
|
|
return nullptr; // seems we have too few routers
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2015-07-04 04:27:40 +03:00
|
|
|
void NetDb::PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg)
|
2013-11-20 16:46:09 +04:00
|
|
|
{
|
2018-01-06 06:48:51 +03:00
|
|
|
if (msg) m_Queue.Put (msg);
|
|
|
|
}
|
2014-01-04 06:24:20 +04:00
|
|
|
|
2018-01-06 06:48:51 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetClosestFloodfill (const IdentHash& destination,
|
2023-02-22 03:08:12 +03:00
|
|
|
const std::set<IdentHash>& excluded) const
|
2014-01-04 06:24:20 +04:00
|
|
|
{
|
2014-10-14 18:41:40 +04:00
|
|
|
IdentHash destKey = CreateRoutingKey (destination);
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2023-02-22 23:58:20 +03:00
|
|
|
return m_Floodfills.FindClosest (destKey, [&excluded](const std::shared_ptr<RouterInfo>& r)->bool
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2023-02-22 23:58:20 +03:00
|
|
|
return r && !r->IsUnreachable () && !r->GetProfile ()->IsUnreachable () &&
|
|
|
|
!excluded.count (r->GetIdentHash ());
|
|
|
|
});
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
2014-01-23 00:32:50 +04:00
|
|
|
|
2015-06-11 18:43:35 +03:00
|
|
|
std::vector<IdentHash> NetDb::GetClosestFloodfills (const IdentHash& destination, size_t num,
|
2016-03-03 03:46:32 +03:00
|
|
|
std::set<IdentHash>& excluded, bool closeThanUsOnly) const
|
2015-04-10 23:15:13 +03:00
|
|
|
{
|
2023-02-22 23:58:20 +03:00
|
|
|
std::vector<IdentHash> res;
|
2015-04-10 23:15:13 +03:00
|
|
|
IdentHash destKey = CreateRoutingKey (destination);
|
2023-02-22 23:58:20 +03:00
|
|
|
std::vector<std::shared_ptr<RouterInfo> > v;
|
2015-04-10 23:15:13 +03:00
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_FloodfillsMutex);
|
2023-02-22 23:58:20 +03:00
|
|
|
v = m_Floodfills.FindClosest (destKey, num, [&excluded](const std::shared_ptr<RouterInfo>& r)->bool
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2023-02-22 23:58:20 +03:00
|
|
|
return r && !r->IsUnreachable () && !r->GetProfile ()->IsUnreachable () &&
|
|
|
|
!excluded.count (r->GetIdentHash ());
|
|
|
|
});
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2023-02-22 23:58:20 +03:00
|
|
|
if (v.empty ()) return res;
|
2023-05-03 16:59:35 +03:00
|
|
|
|
2023-02-22 23:58:20 +03:00
|
|
|
XORMetric ourMetric;
|
|
|
|
if (closeThanUsOnly) ourMetric = destKey ^ i2p::context.GetIdentHash ();
|
|
|
|
for (auto& it: v)
|
2015-04-10 23:15:13 +03:00
|
|
|
{
|
2023-02-22 23:58:20 +03:00
|
|
|
if (closeThanUsOnly && ourMetric < (destKey ^ it->GetIdentHash ())) break;
|
|
|
|
res.push_back (it->GetIdentHash ());
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2015-04-10 23:15:13 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:56:05 +03:00
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouterInFamily (FamilyID fam) const
|
2022-03-24 22:50:20 +03:00
|
|
|
{
|
2019-02-22 18:32:50 +03:00
|
|
|
return GetRandomRouter(
|
|
|
|
[fam](std::shared_ptr<const RouterInfo> router)->bool
|
|
|
|
{
|
|
|
|
return router->IsFamily(fam);
|
|
|
|
});
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
|
|
|
|
std::shared_ptr<const RouterInfo> NetDb::GetClosestNonFloodfill (const IdentHash& destination,
|
2015-02-03 06:34:55 +03:00
|
|
|
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
|
2016-08-05 21:23:54 +03:00
|
|
|
for (const auto& it: m_RouterInfos)
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2024-04-24 01:30:56 +03:00
|
|
|
if (!it.second->IsFloodfill () && it.second->GetProfile ()->IsReal ())
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-02-03 06:34:55 +03:00
|
|
|
XORMetric m = destKey ^ it.first;
|
2015-04-11 02:49:58 +03:00
|
|
|
if (m < minMetric && !excluded.count (it.first))
|
2015-02-03 06:34:55 +03:00
|
|
|
{
|
|
|
|
minMetric = m;
|
|
|
|
r = it.second;
|
|
|
|
}
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 06:34:55 +03:00
|
|
|
return r;
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 04:30:36 +03:00
|
|
|
void NetDb::ManageRouterInfos ()
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetSecondsSinceEpoch ();
|
|
|
|
{
|
2024-01-26 02:51:46 +03:00
|
|
|
std::lock_guard<std::mutex> l(m_RouterInfosMutex);
|
2023-04-06 04:30:36 +03:00
|
|
|
for (auto& it: m_RouterInfos)
|
|
|
|
it.second->UpdateIntroducers (ts);
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
2023-04-06 04:30:36 +03:00
|
|
|
SaveUpdated ();
|
2023-05-03 16:59:35 +03:00
|
|
|
}
|
|
|
|
|
2014-07-31 20:59:43 +04:00
|
|
|
void NetDb::ManageLeaseSets ()
|
|
|
|
{
|
2016-02-08 03:45:06 +03:00
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2014-07-31 20:59:43 +04:00
|
|
|
for (auto it = m_LeaseSets.begin (); it != m_LeaseSets.end ();)
|
|
|
|
{
|
2019-01-15 02:37:17 +03:00
|
|
|
if (!it->second->IsValid () || ts > it->second->GetExpirationTime () - LEASE_ENDDATE_THRESHOLD)
|
2014-07-31 20:59:43 +04:00
|
|
|
{
|
2019-04-20 16:44:16 +03:00
|
|
|
LogPrint (eLogInfo, "NetDb: LeaseSet ", it->first.ToBase64 (), " expired or invalid");
|
2014-07-31 20:59:43 +04:00
|
|
|
it = m_LeaseSets.erase (it);
|
2018-01-06 06:48:51 +03:00
|
|
|
}
|
|
|
|
else
|
2016-08-05 21:23:54 +03:00
|
|
|
++it;
|
2014-07-31 20:59:43 +04:00
|
|
|
}
|
2022-08-10 02:40:07 +03:00
|
|
|
m_LeasesPool.CleanUpMt ();
|
2014-07-31 20:59:43 +04:00
|
|
|
}
|
2022-06-15 20:10:49 +03:00
|
|
|
|
2023-02-14 23:44:35 +03:00
|
|
|
bool NetDb::PopulateRouterInfoBuffer (std::shared_ptr<RouterInfo> r)
|
2022-06-15 20:10:49 +03:00
|
|
|
{
|
2023-02-14 23:44:35 +03:00
|
|
|
if (!r) return false;
|
|
|
|
if (r->GetBuffer ()) return true;
|
|
|
|
return r->LoadBuffer (m_Storage.Path (r->GetIdentHashBase64 ()));
|
2022-10-09 20:24:43 +03:00
|
|
|
}
|
2013-11-13 16:59:21 +04:00
|
|
|
}
|
|
|
|
}
|