2013-11-13 13:59:21 +01:00
|
|
|
#ifndef NETDB_H__
|
|
|
|
#define NETDB_H__
|
2017-04-22 02:04:16 +02:00
|
|
|
// this file is called NetDb.hpp to resolve conflict with libc's netdb.h on case insensitive fs
|
2013-11-13 13:59:21 +01:00
|
|
|
#include <inttypes.h>
|
2014-01-05 15:53:44 +01:00
|
|
|
#include <set>
|
2013-11-13 13:59:21 +01:00
|
|
|
#include <map>
|
2014-11-21 19:02:46 +01:00
|
|
|
#include <list>
|
2013-11-13 13:59:21 +01:00
|
|
|
#include <string>
|
2013-11-19 02:37:38 +01:00
|
|
|
#include <thread>
|
2014-10-06 03:59:05 +02:00
|
|
|
#include <mutex>
|
2016-06-28 02:00:00 +02:00
|
|
|
|
2015-11-03 15:15:49 +01:00
|
|
|
#include "Base.h"
|
2016-06-28 02:00:00 +02:00
|
|
|
#include "Gzip.h"
|
2016-02-20 02:00:00 +01:00
|
|
|
#include "FS.h"
|
2013-11-20 13:46:09 +01:00
|
|
|
#include "Queue.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2013-11-13 13:59:21 +01:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "LeaseSet.h"
|
2013-12-25 18:19:46 +01:00
|
|
|
#include "Tunnel.h"
|
2014-08-20 17:12:53 +02:00
|
|
|
#include "TunnelPool.h"
|
2015-01-19 19:57:37 +01:00
|
|
|
#include "Reseed.h"
|
2015-04-09 18:45:00 +02:00
|
|
|
#include "NetDbRequests.h"
|
2016-02-18 21:57:43 +01:00
|
|
|
#include "Family.h"
|
2013-11-13 13:59:21 +01:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
2018-01-06 04:48:51 +01:00
|
|
|
{
|
2016-02-24 17:31:14 +01:00
|
|
|
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
|
2016-06-30 18:27:23 +02:00
|
|
|
const int NETDB_PUBLISH_INTERVAL = 60*40;
|
2016-07-15 15:38:21 +02:00
|
|
|
|
2016-07-15 19:52:55 +02:00
|
|
|
/** function for visiting a leaseset stored in a floodfill */
|
|
|
|
typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor;
|
2016-08-29 20:16:29 +02:00
|
|
|
|
|
|
|
/** function for visiting a router info we have locally */
|
2018-01-06 04:48:51 +01:00
|
|
|
typedef std::function<void(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoVisitor;
|
2016-08-30 21:54:53 +02:00
|
|
|
|
|
|
|
/** function for visiting a router info and determining if we want to use it */
|
2016-08-31 01:59:24 +02:00
|
|
|
typedef std::function<bool(std::shared_ptr<const i2p::data::RouterInfo>)> RouterInfoFilter;
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2013-11-13 13:59:21 +01:00
|
|
|
class NetDb
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NetDb ();
|
|
|
|
~NetDb ();
|
2013-11-19 02:37:38 +01:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2016-02-17 21:36:55 +01:00
|
|
|
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);
|
2014-11-21 17:37:17 +01:00
|
|
|
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
|
2015-01-27 17:27:58 +01:00
|
|
|
std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const;
|
2015-11-03 15:15:49 +01:00
|
|
|
std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const;
|
2014-03-13 21:26:04 +01:00
|
|
|
|
2018-01-06 04:48:51 +01:00
|
|
|
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr);
|
|
|
|
void RequestDestinationFrom (const IdentHash& destination, const IdentHash & from, bool exploritory, RequestedDestination::RequestComplete requestComplete = nullptr);
|
|
|
|
|
2015-07-04 03:27:40 +02:00
|
|
|
void HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
|
2018-01-06 04:48:51 +01:00
|
|
|
void HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg);
|
2018-11-21 17:23:48 +01:00
|
|
|
void HandleNTCP2RouterInfoMsg (std::shared_ptr<const I2NPMessage> m);
|
|
|
|
|
2014-11-20 22:20:02 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter () const;
|
2014-11-21 16:46:11 +01:00
|
|
|
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;
|
2016-12-02 17:17:22 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter (bool v4only = true) const;
|
2015-02-27 03:05:35 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomIntroducer () const;
|
2016-02-11 21:05:46 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
|
2015-06-11 17:43:35 +02:00
|
|
|
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
|
2016-03-03 01:46:32 +01:00
|
|
|
std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
|
2015-02-03 04:34:55 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2016-06-17 17:03:33 +02:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouterInFamily(const std::string & fam) const;
|
2018-01-06 04:48:51 +01:00
|
|
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
2014-10-24 21:39:53 +02:00
|
|
|
|
2015-07-04 03:27:40 +02:00
|
|
|
void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg);
|
2014-06-17 03:23:22 +02:00
|
|
|
|
2016-06-18 17:10:58 +02:00
|
|
|
/** set hidden mode, aka don't publish our RI to netdb and don't explore */
|
2018-01-06 04:48:51 +01:00
|
|
|
void SetHidden(bool hide);
|
|
|
|
|
2015-01-19 19:57:37 +01:00
|
|
|
void Reseed ();
|
2016-02-19 22:37:41 +01:00
|
|
|
Families& GetFamilies () { return m_Families; };
|
2015-01-19 19:57:37 +01:00
|
|
|
|
2015-04-05 19:56:41 +02:00
|
|
|
// for web interface
|
2014-06-17 03:23:22 +02:00
|
|
|
int GetNumRouters () const { return m_RouterInfos.size (); };
|
|
|
|
int GetNumFloodfills () const { return m_Floodfills.size (); };
|
|
|
|
int GetNumLeaseSets () const { return m_LeaseSets.size (); };
|
2016-07-15 15:38:21 +02:00
|
|
|
|
2016-07-15 19:52:55 +02:00
|
|
|
/** visit all lease sets we currently store */
|
|
|
|
void VisitLeaseSets(LeaseSetVisitor v);
|
2016-08-29 20:16:29 +02: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 21:54:53 +02: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 18:05:44 +01:00
|
|
|
|
2016-11-14 22:23:42 +01:00
|
|
|
void ClearRouterInfos () { m_RouterInfos.clear (); };
|
2016-11-14 22:26:37 +01:00
|
|
|
|
2013-11-13 13:59:21 +01:00
|
|
|
private:
|
|
|
|
|
2015-06-19 21:44:50 +02:00
|
|
|
void Load ();
|
2016-02-11 01:00:00 +01:00
|
|
|
bool LoadRouterInfo (const std::string & path);
|
2015-06-19 21:44:50 +02:00
|
|
|
void SaveUpdated ();
|
2013-11-19 02:37:38 +01:00
|
|
|
void Run (); // exploratory thread
|
2018-01-06 04:48:51 +01:00
|
|
|
void Explore (int numDestinations);
|
2014-02-13 04:02:39 +01:00
|
|
|
void Publish ();
|
2014-07-31 18:59:43 +02:00
|
|
|
void ManageLeaseSets ();
|
2014-12-24 17:20:38 +01:00
|
|
|
void ManageRequests ();
|
2014-01-05 15:53:44 +01:00
|
|
|
|
2016-11-14 18:05:44 +01:00
|
|
|
void ReseedFromFloodfill(const RouterInfo & ri, int numRouters=40, int numFloodfills=20);
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2016-06-17 17:03:33 +02:00
|
|
|
template<typename Filter>
|
2018-01-06 04:48:51 +01:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter (Filter filter) const;
|
|
|
|
|
2013-11-13 13:59:21 +01:00
|
|
|
private:
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2016-07-15 19:52:55 +02:00
|
|
|
mutable std::mutex m_LeaseSetsMutex;
|
2015-01-27 17:27:58 +01:00
|
|
|
std::map<IdentHash, std::shared_ptr<LeaseSet> > m_LeaseSets;
|
2014-11-21 19:29:19 +01:00
|
|
|
mutable std::mutex m_RouterInfosMutex;
|
2014-11-18 17:08:10 +01:00
|
|
|
std::map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
2014-10-06 03:59:05 +02:00
|
|
|
mutable std::mutex m_FloodfillsMutex;
|
2014-11-21 19:02:46 +01:00
|
|
|
std::list<std::shared_ptr<RouterInfo> > m_Floodfills;
|
2018-01-06 04:48:51 +01:00
|
|
|
|
2013-11-19 02:37:38 +01:00
|
|
|
bool m_IsRunning;
|
2016-02-11 01:00:00 +01:00
|
|
|
uint64_t m_LastLoad;
|
2018-01-06 04:48:51 +01:00
|
|
|
std::thread * m_Thread;
|
2015-07-04 03:27:40 +02:00
|
|
|
i2p::util::Queue<std::shared_ptr<const I2NPMessage> > m_Queue; // of I2NPDatabaseStoreMsg
|
2014-02-01 21:57:46 +01:00
|
|
|
|
2015-11-03 15:15:49 +01:00
|
|
|
GzipInflator m_Inflator;
|
2015-01-19 19:57:37 +01:00
|
|
|
Reseeder * m_Reseeder;
|
2016-02-18 21:57:43 +01:00
|
|
|
Families m_Families;
|
2016-02-20 02:00:00 +01:00
|
|
|
i2p::fs::HashedStorage m_Storage;
|
2015-01-19 19:57:37 +01:00
|
|
|
|
2018-01-06 04:48:51 +01:00
|
|
|
friend class NetDbRequests;
|
2015-04-09 18:45:00 +02:00
|
|
|
NetDbRequests m_Requests;
|
2016-03-22 18:10:02 +01:00
|
|
|
|
2016-11-14 18:05:44 +01: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 04:48:51 +01:00
|
|
|
|
2016-06-18 17:10:58 +02:00
|
|
|
|
|
|
|
/** true if in hidden mode */
|
|
|
|
bool m_HiddenMode;
|
2013-11-13 13:59:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
extern NetDb netdb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|