2013-11-13 07:59:21 -05:00
|
|
|
#ifndef NETDB_H__
|
|
|
|
#define NETDB_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-01-05 09:53:44 -05:00
|
|
|
#include <set>
|
2013-11-13 07:59:21 -05:00
|
|
|
#include <map>
|
2014-11-21 13:02:46 -05:00
|
|
|
#include <list>
|
2013-11-13 07:59:21 -05:00
|
|
|
#include <string>
|
2013-11-18 20:37:38 -05:00
|
|
|
#include <thread>
|
2014-10-05 21:59:05 -04:00
|
|
|
#include <mutex>
|
2014-02-01 15:57:46 -05:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-11-03 09:15:49 -05:00
|
|
|
#include "Base.h"
|
2013-11-20 07:46:09 -05:00
|
|
|
#include "Queue.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2013-11-13 07:59:21 -05:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "LeaseSet.h"
|
2013-12-25 12:19:46 -05:00
|
|
|
#include "Tunnel.h"
|
2014-08-20 11:12:53 -04:00
|
|
|
#include "TunnelPool.h"
|
2015-01-19 13:57:37 -05:00
|
|
|
#include "Reseed.h"
|
2015-04-09 12:45:00 -04:00
|
|
|
#include "NetDbRequests.h"
|
2013-11-13 07:59:21 -05:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2014-01-05 09:53:44 -05:00
|
|
|
|
2013-11-13 07:59:21 -05:00
|
|
|
class NetDb
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NetDb ();
|
|
|
|
~NetDb ();
|
2013-11-18 20:37:38 -05:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2013-11-13 07:59:21 -05:00
|
|
|
|
2014-12-11 15:41:04 -05:00
|
|
|
void AddRouterInfo (const uint8_t * buf, int len);
|
2014-09-29 16:41:41 -04:00
|
|
|
void AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
|
2015-02-05 18:53:43 -05:00
|
|
|
void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, std::shared_ptr<i2p::tunnel::InboundTunnel> from);
|
2014-11-21 11:37:17 -05:00
|
|
|
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
|
2015-01-27 11:27:58 -05:00
|
|
|
std::shared_ptr<LeaseSet> FindLeaseSet (const IdentHash& destination) const;
|
2015-11-03 09:15:49 -05:00
|
|
|
std::shared_ptr<RouterProfile> FindRouterProfile (const IdentHash& ident) const;
|
2014-03-13 16:26:04 -04:00
|
|
|
|
2015-01-14 16:11:09 -05:00
|
|
|
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr);
|
2014-08-20 11:12:53 -04:00
|
|
|
|
2015-07-03 21:27:40 -04:00
|
|
|
void HandleDatabaseStoreMsg (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
|
|
|
|
void HandleDatabaseLookupMsg (std::shared_ptr<const I2NPMessage> msg);
|
2014-07-23 11:10:03 -04:00
|
|
|
|
2014-11-20 16:20:02 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter () const;
|
2014-11-21 10:46:11 -05: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;
|
2015-02-26 14:17:16 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomPeerTestRouter () const;
|
2015-02-26 21:05:35 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomIntroducer () const;
|
2014-11-28 13:01:35 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2015-06-11 11:43:35 -04:00
|
|
|
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
|
|
|
|
std::set<IdentHash>& excluded) const;
|
2015-02-02 22:34:55 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetClosestNonFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2014-10-24 15:39:53 -04:00
|
|
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
|
|
|
|
2015-07-03 21:27:40 -04:00
|
|
|
void PostI2NPMsg (std::shared_ptr<const I2NPMessage> msg);
|
2014-06-16 21:23:22 -04:00
|
|
|
|
2015-01-19 13:57:37 -05:00
|
|
|
void Reseed ();
|
|
|
|
|
2015-04-05 13:56:41 -04:00
|
|
|
// for web interface
|
2014-06-16 21:23:22 -04:00
|
|
|
int GetNumRouters () const { return m_RouterInfos.size (); };
|
|
|
|
int GetNumFloodfills () const { return m_Floodfills.size (); };
|
|
|
|
int GetNumLeaseSets () const { return m_LeaseSets.size (); };
|
2013-11-13 07:59:21 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-02-01 16:10:15 +01:00
|
|
|
bool CreateNetDb(boost::filesystem::path directory);
|
2015-06-19 14:44:50 -05:00
|
|
|
void Load ();
|
|
|
|
void SaveUpdated ();
|
2013-11-18 20:37:38 -05:00
|
|
|
void Run (); // exploratory thread
|
2014-12-24 11:20:38 -05:00
|
|
|
void Explore (int numDestinations);
|
2014-02-12 22:02:39 -05:00
|
|
|
void Publish ();
|
2014-07-31 12:59:43 -04:00
|
|
|
void ManageLeaseSets ();
|
2014-12-24 11:20:38 -05:00
|
|
|
void ManageRequests ();
|
2014-01-05 09:53:44 -05:00
|
|
|
|
2014-09-24 21:45:19 -04:00
|
|
|
template<typename Filter>
|
2014-11-20 16:20:02 -05:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter (Filter filter) const;
|
2014-07-03 14:47:12 -04:00
|
|
|
|
2013-11-13 07:59:21 -05:00
|
|
|
private:
|
|
|
|
|
2015-01-27 11:27:58 -05:00
|
|
|
std::map<IdentHash, std::shared_ptr<LeaseSet> > m_LeaseSets;
|
2014-11-21 13:29:19 -05:00
|
|
|
mutable std::mutex m_RouterInfosMutex;
|
2014-11-18 11:08:10 -05:00
|
|
|
std::map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
2014-10-05 21:59:05 -04:00
|
|
|
mutable std::mutex m_FloodfillsMutex;
|
2014-11-21 13:02:46 -05:00
|
|
|
std::list<std::shared_ptr<RouterInfo> > m_Floodfills;
|
2014-01-05 09:53:44 -05:00
|
|
|
|
2013-11-18 20:37:38 -05:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
2015-07-03 21:27:40 -04:00
|
|
|
i2p::util::Queue<std::shared_ptr<const I2NPMessage> > m_Queue; // of I2NPDatabaseStoreMsg
|
2014-02-01 15:57:46 -05:00
|
|
|
|
2015-11-03 09:15:49 -05:00
|
|
|
GzipInflator m_Inflator;
|
2015-01-19 13:57:37 -05:00
|
|
|
Reseeder * m_Reseeder;
|
|
|
|
|
2015-05-06 19:18:00 -04:00
|
|
|
friend class NetDbRequests;
|
2015-04-09 12:45:00 -04:00
|
|
|
NetDbRequests m_Requests;
|
|
|
|
|
2014-02-01 15:57:46 -05:00
|
|
|
static const char m_NetDbPath[];
|
2013-11-13 07:59:21 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern NetDb netdb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|