2013-11-13 16:59:21 +04:00
|
|
|
#ifndef NETDB_H__
|
|
|
|
#define NETDB_H__
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2014-01-05 18:53:44 +04:00
|
|
|
#include <set>
|
2013-11-13 16:59:21 +04:00
|
|
|
#include <map>
|
2014-03-19 22:08:09 +04:00
|
|
|
#include <vector>
|
2013-11-13 16:59:21 +04:00
|
|
|
#include <string>
|
2013-11-19 05:37:38 +04:00
|
|
|
#include <thread>
|
2014-10-06 05:59:05 +04:00
|
|
|
#include <mutex>
|
2014-02-02 00:57:46 +04:00
|
|
|
#include <boost/filesystem.hpp>
|
2013-11-20 16:46:09 +04:00
|
|
|
#include "Queue.h"
|
|
|
|
#include "I2NPProtocol.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
#include "RouterInfo.h"
|
|
|
|
#include "LeaseSet.h"
|
2013-12-25 21:19:46 +04:00
|
|
|
#include "Tunnel.h"
|
2014-08-20 19:12:53 +04:00
|
|
|
#include "TunnelPool.h"
|
2013-11-13 16:59:21 +04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2014-01-05 18:53:44 +04:00
|
|
|
class RequestedDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-08-21 05:47:28 +04:00
|
|
|
RequestedDestination (const IdentHash& destination, bool isLeaseSet,
|
|
|
|
bool isExploratory = false, i2p::tunnel::TunnelPool * pool = nullptr):
|
2014-01-05 18:53:44 +04:00
|
|
|
m_Destination (destination), m_IsLeaseSet (isLeaseSet), m_IsExploratory (isExploratory),
|
2014-08-21 05:47:28 +04:00
|
|
|
m_Pool (pool), m_LastRouter (nullptr), m_CreationTime (0) {};
|
2014-01-05 18:53:44 +04:00
|
|
|
|
|
|
|
const IdentHash& GetDestination () const { return m_Destination; };
|
|
|
|
int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
|
2014-01-22 16:44:41 +04:00
|
|
|
const std::set<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
|
2014-03-23 19:12:11 +04:00
|
|
|
void ClearExcludedPeers ();
|
|
|
|
const RouterInfo * GetLastRouter () const { return m_LastRouter; };
|
2014-08-21 05:47:28 +04:00
|
|
|
i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; };
|
2014-01-05 18:53:44 +04:00
|
|
|
bool IsExploratory () const { return m_IsExploratory; };
|
2014-01-27 00:24:21 +04:00
|
|
|
bool IsLeaseSet () const { return m_IsLeaseSet; };
|
2014-01-05 18:53:44 +04:00
|
|
|
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
|
2014-07-07 23:35:42 +04:00
|
|
|
uint64_t GetCreationTime () const { return m_CreationTime; };
|
2014-01-05 18:53:44 +04:00
|
|
|
I2NPMessage * CreateRequestMessage (const RouterInfo * router, const i2p::tunnel::InboundTunnel * replyTunnel);
|
2014-01-27 00:24:21 +04:00
|
|
|
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
|
2014-07-10 05:43:33 +04:00
|
|
|
|
2014-01-05 18:53:44 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
IdentHash m_Destination;
|
|
|
|
bool m_IsLeaseSet, m_IsExploratory;
|
2014-08-21 05:47:28 +04:00
|
|
|
i2p::tunnel::TunnelPool * m_Pool;
|
2014-01-05 18:53:44 +04:00
|
|
|
std::set<IdentHash> m_ExcludedPeers;
|
|
|
|
const RouterInfo * m_LastRouter;
|
2014-07-07 23:35:42 +04:00
|
|
|
uint64_t m_CreationTime;
|
2014-01-05 18:53:44 +04:00
|
|
|
};
|
|
|
|
|
2013-11-13 16:59:21 +04:00
|
|
|
class NetDb
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NetDb ();
|
|
|
|
~NetDb ();
|
2013-11-19 05:37:38 +04:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
2013-11-13 16:59:21 +04:00
|
|
|
|
2014-09-30 00:41:41 +04:00
|
|
|
void AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
|
2014-10-14 01:45:07 +04:00
|
|
|
void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from);
|
2013-11-29 16:52:09 +04:00
|
|
|
RouterInfo * FindRouter (const IdentHash& ident) const;
|
2013-12-22 20:29:57 +04:00
|
|
|
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
|
2014-03-14 00:26:04 +04:00
|
|
|
|
2014-08-20 19:12:53 +04:00
|
|
|
void PublishLeaseSet (const LeaseSet * leaseSet, i2p::tunnel::TunnelPool * pool);
|
2014-08-21 05:47:28 +04:00
|
|
|
void RequestDestination (const IdentHash& destination, bool isLeaseSet = false,
|
|
|
|
i2p::tunnel::TunnelPool * pool = nullptr);
|
2014-08-20 19:12:53 +04:00
|
|
|
|
2014-09-30 00:41:41 +04:00
|
|
|
void HandleDatabaseStoreMsg (I2NPMessage * msg);
|
2013-11-23 05:41:28 +04:00
|
|
|
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg);
|
2014-07-23 19:10:03 +04:00
|
|
|
void HandleDatabaseLookupMsg (I2NPMessage * msg);
|
|
|
|
|
2014-09-26 05:08:20 +04:00
|
|
|
const RouterInfo * GetRandomRouter () const;
|
|
|
|
const RouterInfo * GetRandomRouter (const RouterInfo * compatibleWith) const;
|
|
|
|
const RouterInfo * GetHighBandwidthRandomRouter (const RouterInfo * compatibleWith) const;
|
2014-10-24 23:39:53 +04:00
|
|
|
void SetUnreachable (const IdentHash& ident, bool unreachable);
|
|
|
|
|
2013-11-23 05:41:28 +04:00
|
|
|
void PostI2NPMsg (I2NPMessage * msg);
|
2014-06-17 05:23:22 +04: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 16:59:21 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-02-01 19:10:15 +04:00
|
|
|
bool CreateNetDb(boost::filesystem::path directory);
|
2013-11-13 16:59:21 +04:00
|
|
|
void Load (const char * directory);
|
2013-11-20 16:46:09 +04:00
|
|
|
void SaveUpdated (const char * directory);
|
2013-11-19 05:37:38 +04:00
|
|
|
void Run (); // exploratory thread
|
2014-06-17 06:30:34 +04:00
|
|
|
void Explore (int numDestinations);
|
2014-02-13 07:02:39 +04:00
|
|
|
void Publish ();
|
2014-01-22 16:44:41 +04:00
|
|
|
const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2014-07-31 20:59:43 +04:00
|
|
|
void ManageLeaseSets ();
|
2014-01-05 18:53:44 +04:00
|
|
|
|
|
|
|
RequestedDestination * CreateRequestedDestination (const IdentHash& dest,
|
2014-08-21 05:47:28 +04:00
|
|
|
bool isLeaseSet, bool isExploratory = false, i2p::tunnel::TunnelPool * pool = nullptr);
|
2014-07-31 20:59:43 +04:00
|
|
|
bool DeleteRequestedDestination (const IdentHash& dest); // returns true if found
|
2014-02-12 07:19:51 +04:00
|
|
|
void DeleteRequestedDestination (RequestedDestination * dest);
|
2014-09-25 05:45:19 +04:00
|
|
|
|
|
|
|
template<typename Filter>
|
|
|
|
const RouterInfo * GetRandomRouter (Filter filter) const;
|
2014-07-03 22:47:12 +04:00
|
|
|
|
2013-11-13 16:59:21 +04:00
|
|
|
private:
|
|
|
|
|
2013-11-29 16:52:09 +04:00
|
|
|
std::map<IdentHash, LeaseSet *> m_LeaseSets;
|
2014-11-18 19:08:10 +03:00
|
|
|
std::map<IdentHash, std::shared_ptr<RouterInfo> > m_RouterInfos;
|
2014-10-06 05:59:05 +04:00
|
|
|
mutable std::mutex m_FloodfillsMutex;
|
2014-11-18 19:08:10 +03:00
|
|
|
std::vector<std::shared_ptr<RouterInfo> > m_Floodfills;
|
2014-09-13 20:39:02 +04:00
|
|
|
std::mutex m_RequestedDestinationsMutex;
|
2014-01-05 18:53:44 +04:00
|
|
|
std::map<IdentHash, RequestedDestination *> m_RequestedDestinations;
|
|
|
|
|
2013-11-19 05:37:38 +04:00
|
|
|
bool m_IsRunning;
|
2014-01-31 16:32:34 +04:00
|
|
|
int m_ReseedRetries;
|
2013-11-19 05:37:38 +04:00
|
|
|
std::thread * m_Thread;
|
2013-11-20 16:46:09 +04:00
|
|
|
i2p::util::Queue<I2NPMessage> m_Queue; // of I2NPDatabaseStoreMsg
|
2014-02-02 00:57:46 +04:00
|
|
|
|
|
|
|
static const char m_NetDbPath[];
|
2013-11-13 16:59:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
extern NetDb netdb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|