2013-11-13 12:59:21 +00:00
|
|
|
#ifndef NETDB_H__
|
|
|
|
#define NETDB_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-01-05 14:53:44 +00:00
|
|
|
#include <set>
|
2013-11-13 12:59:21 +00:00
|
|
|
#include <map>
|
2014-11-21 18:02:46 +00:00
|
|
|
#include <list>
|
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>
|
2014-02-01 20:57:46 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
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"
|
2013-11-13 12:59:21 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2014-01-05 14:53:44 +00:00
|
|
|
class RequestedDestination
|
2015-01-14 21:11:09 +00:00
|
|
|
{
|
2014-01-05 14:53:44 +00:00
|
|
|
public:
|
|
|
|
|
2015-01-14 21:11:09 +00:00
|
|
|
typedef std::function<void (std::shared_ptr<RouterInfo>)> RequestComplete;
|
|
|
|
|
2014-12-28 01:36:32 +00:00
|
|
|
RequestedDestination (const IdentHash& destination, bool isExploratory = false):
|
|
|
|
m_Destination (destination), m_IsExploratory (isExploratory), m_CreationTime (0) {};
|
2015-01-14 21:11:09 +00:00
|
|
|
~RequestedDestination () { if (m_RequestComplete) m_RequestComplete (nullptr); };
|
|
|
|
|
2014-01-05 14:53:44 +00:00
|
|
|
const IdentHash& GetDestination () const { return m_Destination; };
|
|
|
|
int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
|
2014-01-22 12:44:41 +00:00
|
|
|
const std::set<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
|
2014-03-23 15:12:11 +00:00
|
|
|
void ClearExcludedPeers ();
|
2014-01-05 14:53:44 +00:00
|
|
|
bool IsExploratory () const { return m_IsExploratory; };
|
|
|
|
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
|
2014-07-07 19:35:42 +00:00
|
|
|
uint64_t GetCreationTime () const { return m_CreationTime; };
|
2014-11-21 16:37:17 +00:00
|
|
|
I2NPMessage * CreateRequestMessage (std::shared_ptr<const RouterInfo>, const i2p::tunnel::InboundTunnel * replyTunnel);
|
2014-01-26 20:24:21 +00:00
|
|
|
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
|
2015-01-14 21:11:09 +00:00
|
|
|
|
|
|
|
void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; };
|
|
|
|
void Success (std::shared_ptr<RouterInfo> r);
|
|
|
|
void Fail ();
|
|
|
|
|
2014-01-05 14:53:44 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
IdentHash m_Destination;
|
2014-12-26 02:13:51 +00:00
|
|
|
bool m_IsExploratory;
|
2014-01-05 14:53:44 +00:00
|
|
|
std::set<IdentHash> m_ExcludedPeers;
|
2014-07-07 19:35:42 +00:00
|
|
|
uint64_t m_CreationTime;
|
2015-01-14 21:11:09 +00:00
|
|
|
RequestComplete m_RequestComplete;
|
2014-01-05 14:53:44 +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 ();
|
2013-11-13 12:59:21 +00:00
|
|
|
|
2014-12-11 20:41:04 +00:00
|
|
|
void AddRouterInfo (const uint8_t * buf, int len);
|
2014-09-29 20:41:41 +00:00
|
|
|
void AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
|
2014-10-13 21:45:07 +00:00
|
|
|
void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from);
|
2014-11-21 16:37:17 +00:00
|
|
|
std::shared_ptr<RouterInfo> FindRouter (const IdentHash& ident) const;
|
2013-12-22 16:29:57 +00:00
|
|
|
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
|
2014-03-13 20:26:04 +00:00
|
|
|
|
2015-01-14 21:11:09 +00:00
|
|
|
void RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete = nullptr);
|
2014-08-20 15:12:53 +00:00
|
|
|
|
2014-09-29 20:41:41 +00:00
|
|
|
void HandleDatabaseStoreMsg (I2NPMessage * msg);
|
2013-11-23 01:41:28 +00:00
|
|
|
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg);
|
2014-07-23 15:10:03 +00:00
|
|
|
void HandleDatabaseLookupMsg (I2NPMessage * msg);
|
|
|
|
|
2014-11-20 21:20:02 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter () const;
|
2014-11-21 15:46:11 +00: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;
|
2014-11-28 18:01:35 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2014-10-24 19:39:53 +00:00
|
|
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
|
|
|
|
2013-11-23 01:41:28 +00:00
|
|
|
void PostI2NPMsg (I2NPMessage * msg);
|
2014-06-17 01:23:22 +00:00
|
|
|
|
|
|
|
// for web interface
|
|
|
|
int GetNumRouters () const { return m_RouterInfos.size (); };
|
|
|
|
int GetNumFloodfills () const { return m_Floodfills.size (); };
|
|
|
|
int GetNumLeaseSets () const { return m_LeaseSets.size (); };
|
2013-11-13 12:59:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-02-01 15:10:15 +00:00
|
|
|
bool CreateNetDb(boost::filesystem::path directory);
|
2013-11-13 12:59:21 +00:00
|
|
|
void Load (const char * directory);
|
2013-11-20 12:46:09 +00:00
|
|
|
void SaveUpdated (const char * directory);
|
2013-11-19 01:37:38 +00:00
|
|
|
void Run (); // exploratory thread
|
2014-12-24 16:20:38 +00:00
|
|
|
void Explore (int numDestinations);
|
2014-02-13 03:02:39 +00:00
|
|
|
void Publish ();
|
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
|
|
|
|
2014-12-28 01:36:32 +00:00
|
|
|
RequestedDestination * CreateRequestedDestination (const IdentHash& dest, bool isExploratory = false);
|
2014-02-12 03:19:51 +00:00
|
|
|
void DeleteRequestedDestination (RequestedDestination * dest);
|
2014-09-25 01:45:19 +00:00
|
|
|
|
|
|
|
template<typename Filter>
|
2014-11-20 21:20:02 +00:00
|
|
|
std::shared_ptr<const RouterInfo> GetRandomRouter (Filter filter) const;
|
2014-07-03 18:47:12 +00:00
|
|
|
|
2013-11-13 12:59:21 +00:00
|
|
|
private:
|
|
|
|
|
2013-11-29 12:52:09 +00:00
|
|
|
std::map<IdentHash, LeaseSet *> m_LeaseSets;
|
2014-11-21 18:29:19 +00:00
|
|
|
mutable std::mutex m_RouterInfosMutex;
|
2014-11-18 16:08:10 +00:00
|
|
|
std::map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
2014-10-06 01:59:05 +00:00
|
|
|
mutable std::mutex m_FloodfillsMutex;
|
2014-11-21 18:02:46 +00:00
|
|
|
std::list<std::shared_ptr<RouterInfo> > m_Floodfills;
|
2014-09-13 16:39:02 +00:00
|
|
|
std::mutex m_RequestedDestinationsMutex;
|
2014-01-05 14:53:44 +00:00
|
|
|
std::map<IdentHash, RequestedDestination *> m_RequestedDestinations;
|
|
|
|
|
2013-11-19 01:37:38 +00:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
2013-11-20 12:46:09 +00:00
|
|
|
i2p::util::Queue<I2NPMessage> m_Queue; // of I2NPDatabaseStoreMsg
|
2014-02-01 20:57:46 +00:00
|
|
|
|
|
|
|
static const char m_NetDbPath[];
|
2013-11-13 12:59:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern NetDb netdb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|