mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
6.0 KiB
156 lines
6.0 KiB
11 years ago
|
#ifndef NETDB_H__
|
||
|
#define NETDB_H__
|
||
8 years ago
|
// this file is called NetDb.hpp to resolve conflict with libc's netdb.h on case insensitive fs
|
||
11 years ago
|
#include <inttypes.h>
|
||
11 years ago
|
#include <set>
|
||
11 years ago
|
#include <map>
|
||
10 years ago
|
#include <list>
|
||
11 years ago
|
#include <string>
|
||
11 years ago
|
#include <thread>
|
||
10 years ago
|
#include <mutex>
|
||
9 years ago
|
|
||
9 years ago
|
#include "Base.h"
|
||
9 years ago
|
#include "Gzip.h"
|
||
9 years ago
|
#include "FS.h"
|
||
11 years ago
|
#include "Queue.h"
|
||
|
#include "I2NPProtocol.h"
|
||
11 years ago
|
#include "RouterInfo.h"
|
||
|
#include "LeaseSet.h"
|
||
11 years ago
|
#include "Tunnel.h"
|
||
10 years ago
|
#include "TunnelPool.h"
|
||
10 years ago
|
#include "Reseed.h"
|
||
10 years ago
|
#include "NetDbRequests.h"
|
||
9 years ago
|
#include "Family.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
|
{
|
||
9 years ago
|
const int NETDB_MIN_ROUTERS = 90;
|
||
|
const int NETDB_FLOODFILL_EXPIRATION_TIMEOUT = 60*60; // 1 hour, in seconds
|
||
|
const int NETDB_INTRODUCEE_EXPIRATION_TIMEOUT = 65*60;
|
||
|
const int NETDB_MIN_EXPIRATION_TIMEOUT = 90*60; // 1.5 hours
|
||
|
const int NETDB_MAX_EXPIRATION_TIMEOUT = 27*60*60; // 27 hours
|
||
9 years ago
|
const int NETDB_PUBLISH_INTERVAL = 60*40;
|
||
8 years ago
|
|
||
8 years ago
|
/** function for visiting a leaseset stored in a floodfill */
|
||
|
typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor;
|
||
8 years ago
|
|
||
|
/** function for visiting a router info we have locally */
|
||
8 years ago
|
typedef std::function<void(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoVisitor;
|
||
8 years ago
|
|
||
|
/** function for visiting a router info and determining if we want to use it */
|
||
8 years ago
|
typedef std::function<bool(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoFilter;
|
||
8 years ago
|
|
||
11 years ago
|
class NetDb
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
NetDb ();
|
||
|
~NetDb ();
|
||
11 years ago
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
8 years ago
|
|
||
9 years ago
|
bool AddRouterInfo (const uint8_t * buf, int len);
|
||
|
bool AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
|
||
|
bool AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
||
10 years ago
|
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
|
||
10 years ago
|
std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const;
|
||
9 years ago
|
std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const;
|
||
11 years ago
|
|
||
10 years ago
|
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr);
|
||
8 years ago
|
void RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploritory, RequestedDestination::RequestComplete requestComplete = nullptr);
|
||
10 years ago
|
|
||
10 years ago
|
void HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> msg);
|
||
|
void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
|
||
|
void HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg);
|
||
10 years ago
|
|
||
10 years ago
|
std::shared_ptr<const RouterInfo> GetRandomRouter () const;
|
||
10 years ago
|
std::shared_ptr<const RouterInfo> GetRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const;
|
||
|
std::shared_ptr<const RouterInfo> GetHighBandwidthRandomRouter (std::shared_ptr<const RouterInfo> compatibleWith) const;
|
||
8 years ago
|
std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter (bool v4only = true) const;
|
||
10 years ago
|
std::shared_ptr<const RouterInfo> GetRandomIntroducer () const;
|
||
9 years ago
|
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
|
||
10 years ago
|
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
|
||
9 years ago
|
std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
|
||
10 years ago
|
std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
||
9 years ago
|
std::shared_ptr<const RouterInfo> GetRandomRouterInFamily(const std::string & fam) const;
|
||
10 years ago
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
||
|
|
||
10 years ago
|
void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg);
|
||
11 years ago
|
|
||
9 years ago
|
/** set hidden mode, aka don't publish our RI to netdb and don't explore */
|
||
|
void SetHidden(bool hide);
|
||
|
|
||
10 years ago
|
void Reseed ();
|
||
9 years ago
|
Families& GetFamilies () { return m_Families; };
|
||
10 years ago
|
|
||
10 years ago
|
// for web interface
|
||
11 years ago
|
int GetNumRouters () const { return m_RouterInfos.size (); };
|
||
|
int GetNumFloodfills () const { return m_Floodfills.size (); };
|
||
|
int GetNumLeaseSets () const { return m_LeaseSets.size (); };
|
||
8 years ago
|
|
||
8 years ago
|
/** visit all lease sets we currently store */
|
||
|
void VisitLeaseSets(LeaseSetVisitor v);
|
||
8 years ago
|
/** 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);
|
||
8 years ago
|
/** 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);
|
||
8 years ago
|
|
||
8 years ago
|
void ClearRouterInfos () { m_RouterInfos.clear (); };
|
||
8 years ago
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
void Load ();
|
||
9 years ago
|
bool LoadRouterInfo (const std::string & path);
|
||
10 years ago
|
void SaveUpdated ();
|
||
11 years ago
|
void Run (); // exploratory thread
|
||
10 years ago
|
void Explore (int numDestinations);
|
||
11 years ago
|
void Publish ();
|
||
10 years ago
|
void ManageLeaseSets ();
|
||
10 years ago
|
void ManageRequests ();
|
||
11 years ago
|
|
||
8 years ago
|
void ReseedFromFloodfill(const RouterInfo & ri, int numRouters=40, int numFloodfills=20);
|
||
|
|
||
9 years ago
|
template<typename Filter>
|
||
|
std::shared_ptr<const RouterInfo> GetRandomRouter (Filter filter) const;
|
||
11 years ago
|
|
||
11 years ago
|
private:
|
||
8 years ago
|
|
||
8 years ago
|
mutable std::mutex m_LeaseSetsMutex;
|
||
10 years ago
|
std::map<IdentHash, std::shared_ptr<LeaseSet> > m_LeaseSets;
|
||
10 years ago
|
mutable std::mutex m_RouterInfosMutex;
|
||
10 years ago
|
std::map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
||
10 years ago
|
mutable std::mutex m_FloodfillsMutex;
|
||
10 years ago
|
std::list<std::shared_ptr<RouterInfo> > m_Floodfills;
|
||
11 years ago
|
|
||
11 years ago
|
bool m_IsRunning;
|
||
9 years ago
|
uint64_t m_LastLoad;
|
||
11 years ago
|
std::thread * m_Thread;
|
||
10 years ago
|
i2p::util::Queue<std::shared_ptr<const I2NPMessage> > m_Queue; // of I2NPDatabaseStoreMsg
|
||
11 years ago
|
|
||
9 years ago
|
GzipInflator m_Inflator;
|
||
10 years ago
|
Reseeder * m_Reseeder;
|
||
9 years ago
|
Families m_Families;
|
||
9 years ago
|
i2p::fs::HashedStorage m_Storage;
|
||
10 years ago
|
|
||
10 years ago
|
friend class NetDbRequests;
|
||
10 years ago
|
NetDbRequests m_Requests;
|
||
9 years ago
|
|
||
8 years ago
|
/** router info we are bootstrapping from or nullptr if we are not currently doing that*/
|
||
|
std::shared_ptr<RouterInfo> m_FloodfillBootstrap;
|
||
8 years ago
|
|
||
9 years ago
|
|
||
|
/** true if in hidden mode */
|
||
|
bool m_HiddenMode;
|
||
11 years ago
|
};
|
||
|
|
||
|
extern NetDb netdb;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|