2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2024-04-22 17:03:59 +00:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 13:18:41 +00: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
|
|
|
|
*/
|
|
|
|
|
2013-11-13 12:59:21 +00:00
|
|
|
#ifndef NETDB_H__
|
|
|
|
#define NETDB_H__
|
2017-04-22 00:04:16 +00:00
|
|
|
// this file is called NetDb.hpp to resolve conflict with libc's netdb.h on case insensitive fs
|
2013-11-13 12:59:21 +00:00
|
|
|
#include <inttypes.h>
|
2024-05-15 17:31:31 +00:00
|
|
|
#include <unordered_set>
|
2020-07-07 19:38:20 +00:00
|
|
|
#include <unordered_map>
|
2013-11-13 12:59:21 +00:00
|
|
|
#include <string>
|
2013-11-19 01:37:38 +00:00
|
|
|
#include <thread>
|
2014-10-06 01:59:05 +00:00
|
|
|
#include <mutex>
|
2024-04-27 20:00:43 +00:00
|
|
|
#include <future>
|
2016-06-28 00:00:00 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
#include "Base.h"
|
2016-06-28 00:00:00 +00:00
|
|
|
#include "Gzip.h"
|
2016-02-20 01:00:00 +00:00
|
|
|
#include "FS.h"
|
2013-11-20 12:46:09 +00:00
|
|
|
#include "Queue.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2013-11-13 12:59:21 +00:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "LeaseSet.h"
|
2013-12-25 17:19:46 +00:00
|
|
|
#include "Tunnel.h"
|
2014-08-20 15:12:53 +00:00
|
|
|
#include "TunnelPool.h"
|
2015-01-19 18:57:37 +00:00
|
|
|
#include "Reseed.h"
|
2015-04-09 16:45:00 +00:00
|
|
|
#include "NetDbRequests.h"
|
2016-02-18 20:57:43 +00:00
|
|
|
#include "Family.h"
|
2020-05-20 18:59:18 +00:00
|
|
|
#include "version.h"
|
2021-12-30 20:16:13 +00:00
|
|
|
#include "util.h"
|
2023-02-22 20:58:20 +00:00
|
|
|
#include "KadDHT.h"
|
2013-11-13 12:59:21 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
2018-01-06 03:48:51 +00:00
|
|
|
{
|
2016-02-24 16:31:14 +00:00
|
|
|
const int NETDB_MIN_ROUTERS = 90;
|
2021-08-26 19:13:58 +00:00
|
|
|
const int NETDB_MIN_FLOODFILLS = 5;
|
2024-04-22 17:03:59 +00:00
|
|
|
const int NETDB_NUM_FLOODFILLS_THRESHOLD = 1000;
|
2023-10-31 00:02:48 +00:00
|
|
|
const int NETDB_NUM_ROUTERS_THRESHOLD = 4*NETDB_NUM_FLOODFILLS_THRESHOLD;
|
2024-04-22 17:03:59 +00:00
|
|
|
const int NETDB_TUNNEL_CREATION_RATE_THRESHOLD = 10; // in %
|
|
|
|
const int NETDB_CHECK_FOR_EXPIRATION_UPTIME = 600; // 10 minutes, in seconds
|
2020-03-01 10:25:50 +00:00
|
|
|
const int NETDB_FLOODFILL_EXPIRATION_TIMEOUT = 60 * 60; // 1 hour, in seconds
|
|
|
|
const int NETDB_MIN_EXPIRATION_TIMEOUT = 90 * 60; // 1.5 hours
|
|
|
|
const int NETDB_MAX_EXPIRATION_TIMEOUT = 27 * 60 * 60; // 27 hours
|
2022-02-13 20:42:06 +00:00
|
|
|
const int NETDB_MAX_OFFLINE_EXPIRATION_TIMEOUT = 180; // in days
|
2022-12-20 20:23:54 +00:00
|
|
|
const int NETDB_EXPIRATION_TIMEOUT_THRESHOLD = 2*60; // 2 minutes
|
2024-05-01 18:10:34 +00:00
|
|
|
const int NETDB_EXPLORATORY_INTERVAL = 55; // in seconds
|
|
|
|
const int NETDB_EXPLORATORY_INTERVAL_VARIANCE = 170; // in seconds
|
2023-02-13 18:25:02 +00:00
|
|
|
const int NETDB_MIN_HIGHBANDWIDTH_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
|
|
|
|
const int NETDB_MIN_FLOODFILL_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
|
2021-07-23 00:58:35 +00:00
|
|
|
const int NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
|
2024-05-02 23:16:48 +00:00
|
|
|
const size_t NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES = 16;
|
2024-05-03 01:38:51 +00:00
|
|
|
const size_t NETDB_MAX_EXPLORATORY_SELECTION_SIZE = 500;
|
2024-05-03 12:54:55 +00:00
|
|
|
const int NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL = 82; // in seconds
|
2016-07-15 13:38:21 +00:00
|
|
|
|
2016-07-15 17:52:55 +00:00
|
|
|
/** function for visiting a leaseset stored in a floodfill */
|
|
|
|
typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor;
|
2016-08-29 18:16:29 +00:00
|
|
|
|
|
|
|
/** function for visiting a router info we have locally */
|
2018-01-06 03:48:51 +00:00
|
|
|
typedef std::function<void(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoVisitor;
|
2016-08-30 19:54:53 +00:00
|
|
|
|
|
|
|
/** function for visiting a router info and determining if we want to use it */
|
2016-08-30 23:59:24 +00:00
|
|
|
typedef std::function<bool(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoFilter;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-11-13 12:59:21 +00:00
|
|
|
class NetDb
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NetDb ();
|
|
|
|
~NetDb ();
|
2013-11-19 01:37:38 +00:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2022-07-19 18:06:00 +00:00
|
|
|
std::shared_ptr<const RouterInfo> AddRouterInfo (const uint8_t * buf, int len);
|
2016-02-17 20:36:55 +00:00
|
|
|
bool AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
|
2019-01-17 00:00:17 +00:00
|
|
|
bool AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len);
|
2018-12-21 20:00:03 +00:00
|
|
|
bool AddLeaseSet2 (const IdentHash& ident, const uint8_t * buf, int len, uint8_t storeType);
|
2014-11-21 16:37:17 +00:00
|
|
|
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
|
2015-01-27 16:27:58 +00:00
|
|
|
std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const;
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const;
|
2014-03-13 20:26:04 +00:00
|
|
|
|
2020-10-03 21:20:04 +00:00
|
|
|
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr, bool direct = true);
|
2020-03-01 10:25:50 +00:00
|
|
|
void RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploritory, RequestedDestination::RequestComplete requestComplete = nullptr);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2014-11-20 21:20:02 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter () const;
|
2023-07-11 17:16:35 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse, bool endpoint) const;
|
|
|
|
std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith, bool reverse, bool endpoint) const;
|
2024-05-15 17:31:31 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomSSU2PeerTestRouter (bool v4, const std::unordered_set<IdentHash>& excluded) const;
|
|
|
|
std::shared_ptr<const RouterInfo> GetRandomSSU2Introducer (bool v4, const std::unordered_set<IdentHash>& excluded) const;
|
|
|
|
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::unordered_set<IdentHash>& excluded) const;
|
2015-06-11 15:43:35 +00:00
|
|
|
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
|
2024-05-15 17:31:31 +00:00
|
|
|
std::unordered_set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
|
|
|
|
std::vector<IdentHash> GetExploratoryNonFloodfill (const IdentHash& destination, size_t num, const std::unordered_set<IdentHash>& excluded);
|
2022-03-24 19:50:20 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouterInFamily (FamilyID fam) const;
|
2018-01-06 03:48:51 +00:00
|
|
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
2023-04-18 18:35:13 +00:00
|
|
|
void ExcludeReachableTransports (const IdentHash& ident, RouterInfo::CompatibleTransports transports);
|
2014-10-24 19:39:53 +00:00
|
|
|
|
2015-07-04 01:27:40 +00:00
|
|
|
void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg);
|
2024-05-22 02:19:42 +00:00
|
|
|
void PostDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg); // to NetdbReq thread
|
2014-06-17 01:23:22 +00:00
|
|
|
|
2015-01-19 18:57:37 +00:00
|
|
|
void Reseed ();
|
2016-02-19 21:37:41 +00:00
|
|
|
Families& GetFamilies () { return m_Families; };
|
2015-01-19 18:57:37 +00:00
|
|
|
|
2015-04-05 17:56:41 +00:00
|
|
|
// for web interface
|
2014-06-17 01:23:22 +00:00
|
|
|
int GetNumRouters () const { return m_RouterInfos.size (); };
|
2023-02-22 20:58:20 +00:00
|
|
|
int GetNumFloodfills () const { return m_Floodfills.GetSize (); };
|
2014-06-17 01:23:22 +00:00
|
|
|
int GetNumLeaseSets () const { return m_LeaseSets.size (); };
|
2016-07-15 13:38:21 +00:00
|
|
|
|
2016-07-15 17:52:55 +00:00
|
|
|
/** visit all lease sets we currently store */
|
|
|
|
void VisitLeaseSets(LeaseSetVisitor v);
|
2016-08-29 18:16:29 +00:00
|
|
|
/** visit all router infos we have currently on disk, usually insanely expensive, does not access in memory RI */
|
|
|
|
void VisitStoredRouterInfos(RouterInfoVisitor v);
|
|
|
|
/** visit all router infos we have loaded in memory, cheaper than VisitLocalRouterInfos but locks access while visiting */
|
|
|
|
void VisitRouterInfos(RouterInfoVisitor v);
|
2016-08-30 19:54:53 +00:00
|
|
|
/** visit N random router that match using filter, then visit them with a visitor, return number of RouterInfos that were visited */
|
|
|
|
size_t VisitRandomRouterInfos(RouterInfoFilter f, RouterInfoVisitor v, size_t n);
|
2016-11-14 17:05:44 +00:00
|
|
|
|
2016-11-14 21:23:42 +00:00
|
|
|
void ClearRouterInfos () { m_RouterInfos.clear (); };
|
2021-12-30 20:16:13 +00:00
|
|
|
std::shared_ptr<RouterInfo::Buffer> NewRouterInfoBuffer () { return m_RouterInfoBuffersPool.AcquireSharedMt (); };
|
2023-02-14 20:44:35 +00:00
|
|
|
bool PopulateRouterInfoBuffer (std::shared_ptr<RouterInfo> r);
|
2022-12-07 19:08:27 +00:00
|
|
|
std::shared_ptr<RouterInfo::Address> NewRouterInfoAddress () { return m_RouterInfoAddressesPool.AcquireSharedMt (); };
|
2023-01-03 18:25:19 +00:00
|
|
|
boost::shared_ptr<RouterInfo::Addresses> NewRouterInfoAddresses ()
|
|
|
|
{
|
|
|
|
return boost::shared_ptr<RouterInfo::Addresses>(m_RouterInfoAddressVectorsPool.AcquireMt (),
|
2022-12-07 19:08:27 +00:00
|
|
|
std::bind <void (i2p::util::MemoryPoolMt<RouterInfo::Addresses>::*)(RouterInfo::Addresses *)>
|
2023-01-03 18:25:19 +00:00
|
|
|
(&i2p::util::MemoryPoolMt<RouterInfo::Addresses>::ReleaseMt,
|
2022-12-07 19:08:27 +00:00
|
|
|
&m_RouterInfoAddressVectorsPool, std::placeholders::_1));
|
|
|
|
};
|
2022-10-09 17:24:43 +00:00
|
|
|
std::shared_ptr<Lease> NewLease (const Lease& lease) { return m_LeasesPool.AcquireSharedMt (lease); };
|
2023-03-17 01:32:53 +00:00
|
|
|
std::shared_ptr<IdentityEx> NewIdentity (const uint8_t * buf, size_t len) { return m_IdentitiesPool.AcquireSharedMt (buf, len); };
|
2023-08-26 14:57:05 +00:00
|
|
|
std::shared_ptr<RouterProfile> NewRouterProfile () { return m_RouterProfilesPool.AcquireSharedMt (); };
|
2022-05-20 16:56:05 +00:00
|
|
|
|
2013-11-13 12:59:21 +00:00
|
|
|
private:
|
|
|
|
|
2015-06-19 19:44:50 +00:00
|
|
|
void Load ();
|
2022-02-13 20:42:06 +00:00
|
|
|
bool LoadRouterInfo (const std::string& path, uint64_t ts);
|
2015-06-19 19:44:50 +00:00
|
|
|
void SaveUpdated ();
|
2024-05-08 23:09:03 +00:00
|
|
|
void PersistRouters (std::list<std::pair<std::string, std::shared_ptr<RouterInfo::Buffer> > >&& update,
|
2024-05-08 20:19:00 +00:00
|
|
|
std::list<std::string>&& remove);
|
2013-11-19 01:37:38 +00:00
|
|
|
void Run (); // exploratory thread
|
2018-01-06 03:48:51 +00:00
|
|
|
void Explore (int numDestinations);
|
2018-11-21 18:24:54 +00:00
|
|
|
void Flood (const IdentHash& ident, std::shared_ptr<I2NPMessage> floodMsg);
|
2023-04-06 01:30:36 +00:00
|
|
|
void ManageRouterInfos ();
|
2014-07-31 16:59:43 +00:00
|
|
|
void ManageLeaseSets ();
|
2014-12-24 16:20:38 +00:00
|
|
|
void ManageRequests ();
|
2014-01-05 14:53:44 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
void ReseedFromFloodfill(const RouterInfo & ri, int numRouters = 40, int numFloodfills = 20);
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2018-11-21 18:24:54 +00:00
|
|
|
std::shared_ptr<const RouterInfo> AddRouterInfo (const uint8_t * buf, int len, bool& updated);
|
|
|
|
std::shared_ptr<const RouterInfo> AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len, bool& updated);
|
2020-03-01 10:25:50 +00:00
|
|
|
|
|
|
|
template<typename Filter>
|
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter (Filter filter) const;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2024-05-22 02:19:42 +00:00
|
|
|
void HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
void HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
void HandleNTCP2RouterInfoMsg (std::shared_ptr<const I2NPMessage> m);
|
|
|
|
|
2013-11-13 12:59:21 +00:00
|
|
|
private:
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2016-07-15 17:52:55 +00:00
|
|
|
mutable std::mutex m_LeaseSetsMutex;
|
2020-07-07 19:38:20 +00:00
|
|
|
std::unordered_map<IdentHash, std::shared_ptr<LeaseSet> > m_LeaseSets;
|
2014-11-21 18:29:19 +00:00
|
|
|
mutable std::mutex m_RouterInfosMutex;
|
2020-07-07 19:38:20 +00:00
|
|
|
std::unordered_map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
2014-10-06 01:59:05 +00:00
|
|
|
mutable std::mutex m_FloodfillsMutex;
|
2023-02-22 20:58:20 +00:00
|
|
|
DHTTable m_Floodfills;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2013-11-19 01:37:38 +00:00
|
|
|
bool m_IsRunning;
|
2018-01-06 03:48:51 +00:00
|
|
|
std::thread * m_Thread;
|
2015-07-04 01:27:40 +00:00
|
|
|
i2p::util::Queue<std::shared_ptr<const I2NPMessage> > m_Queue; // of I2NPDatabaseStoreMsg
|
2014-02-01 20:57:46 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
GzipInflator m_Inflator;
|
2015-01-19 18:57:37 +00:00
|
|
|
Reseeder * m_Reseeder;
|
2016-02-18 20:57:43 +00:00
|
|
|
Families m_Families;
|
2016-02-20 01:00:00 +00:00
|
|
|
i2p::fs::HashedStorage m_Storage;
|
2015-01-19 18:57:37 +00:00
|
|
|
|
2024-05-06 22:23:20 +00:00
|
|
|
std::shared_ptr<NetDbRequests> m_Requests;
|
2016-03-22 17:10:02 +00:00
|
|
|
|
2018-11-21 21:13:23 +00:00
|
|
|
bool m_PersistProfiles;
|
2024-05-08 20:19:00 +00:00
|
|
|
std::future<void> m_SavingProfiles, m_DeletingProfiles, m_PersistingRouters;
|
2018-11-21 21:13:23 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
/** router info we are bootstrapping from or nullptr if we are not currently doing that*/
|
|
|
|
std::shared_ptr<RouterInfo> m_FloodfillBootstrap;
|
2018-01-06 03:48:51 +00:00
|
|
|
|
2024-05-03 12:54:55 +00:00
|
|
|
std::vector<std::shared_ptr<const RouterInfo> > m_ExploratorySelection;
|
|
|
|
uint64_t m_LastExploratorySelectionUpdateTime; // in monotonic seconds
|
|
|
|
|
2021-12-30 20:16:13 +00:00
|
|
|
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
|
2022-12-07 19:08:27 +00:00
|
|
|
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
|
|
|
|
i2p::util::MemoryPoolMt<RouterInfo::Addresses> m_RouterInfoAddressVectorsPool;
|
2022-08-09 23:40:07 +00:00
|
|
|
i2p::util::MemoryPoolMt<Lease> m_LeasesPool;
|
2023-03-17 01:32:53 +00:00
|
|
|
i2p::util::MemoryPoolMt<IdentityEx> m_IdentitiesPool;
|
2023-08-26 14:57:05 +00:00
|
|
|
i2p::util::MemoryPoolMt<RouterProfile> m_RouterProfilesPool;
|
2013-11-13 12:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern NetDb netdb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|