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>
|
|
|
|
#include <string>
|
2013-11-19 01:37:38 +00:00
|
|
|
#include <thread>
|
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"
|
2013-11-13 12:59:21 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2014-01-05 14:53:44 +00:00
|
|
|
class RequestedDestination
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
RequestedDestination (const IdentHash& destination, bool isLeaseSet, bool isExploratory = false):
|
|
|
|
m_Destination (destination), m_IsLeaseSet (isLeaseSet), m_IsExploratory (isExploratory),
|
|
|
|
m_LastRouter (nullptr), m_LastReplyTunnel (nullptr), m_LastOutboundTunnel (nullptr) {};
|
|
|
|
|
|
|
|
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; };
|
|
|
|
const RouterInfo * GetLastRouter () const { return m_LastRouter; };
|
2014-01-05 14:53:44 +00:00
|
|
|
const i2p::tunnel::InboundTunnel * GetLastReplyTunnel () const { return m_LastReplyTunnel; };
|
|
|
|
bool IsExploratory () const { return m_IsExploratory; };
|
2014-01-26 20:24:21 +00:00
|
|
|
bool IsLeaseSet () const { return m_IsLeaseSet; };
|
2014-01-05 14:53:44 +00:00
|
|
|
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
|
|
|
|
I2NPMessage * CreateRequestMessage (const RouterInfo * router, const i2p::tunnel::InboundTunnel * replyTunnel);
|
2014-01-26 20:24:21 +00:00
|
|
|
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
|
|
|
|
|
2014-01-05 14:53:44 +00:00
|
|
|
i2p::tunnel::OutboundTunnel * GetLastOutboundTunnel () const { return m_LastOutboundTunnel; };
|
|
|
|
void SetLastOutboundTunnel (i2p::tunnel::OutboundTunnel * tunnel) { m_LastOutboundTunnel = tunnel; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
IdentHash m_Destination;
|
|
|
|
bool m_IsLeaseSet, m_IsExploratory;
|
|
|
|
std::set<IdentHash> m_ExcludedPeers;
|
|
|
|
const RouterInfo * m_LastRouter;
|
|
|
|
const i2p::tunnel::InboundTunnel * m_LastReplyTunnel;
|
|
|
|
i2p::tunnel::OutboundTunnel * m_LastOutboundTunnel;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
void AddRouterInfo (uint8_t * buf, int len);
|
|
|
|
void AddLeaseSet (uint8_t * buf, int len);
|
2013-11-29 12:52:09 +00:00
|
|
|
RouterInfo * FindRouter (const IdentHash& ident) const;
|
2013-12-22 16:29:57 +00:00
|
|
|
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
|
|
|
|
|
2014-01-03 02:22:48 +00:00
|
|
|
void RequestDestination (const char * b32); // in base32
|
|
|
|
void RequestDestination (const IdentHash& destination, bool isLeaseSet = false);
|
2014-01-22 12:44:41 +00:00
|
|
|
|
2013-11-28 02:28:32 +00:00
|
|
|
void HandleDatabaseStoreMsg (uint8_t * buf, size_t len);
|
2013-11-23 01:41:28 +00:00
|
|
|
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg);
|
2013-11-13 12:59:21 +00:00
|
|
|
|
2013-11-19 01:37:38 +00:00
|
|
|
const RouterInfo * GetRandomNTCPRouter (bool floodfillOnly = false) const;
|
2014-01-26 02:47:01 +00:00
|
|
|
const RouterInfo * GetRandomRouter (const RouterInfo * compatibleWith = nullptr, bool floodfillOnly = false) const;
|
2013-11-20 12:46:09 +00:00
|
|
|
|
2013-11-23 01:41:28 +00:00
|
|
|
void PostI2NPMsg (I2NPMessage * msg);
|
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
|
|
|
|
void Explore ();
|
2014-01-22 12:44:41 +00:00
|
|
|
const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
|
2014-01-05 14:53:44 +00:00
|
|
|
|
|
|
|
RequestedDestination * CreateRequestedDestination (const IdentHash& dest,
|
|
|
|
bool isLeaseSet, bool isExploratory = false);
|
|
|
|
void DeleteRequestedDestination (const IdentHash& dest);
|
2013-11-19 01:37:38 +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;
|
|
|
|
std::map<IdentHash, RouterInfo *> m_RouterInfos;
|
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;
|
2014-01-31 12:32:34 +00:00
|
|
|
int m_ReseedRetries;
|
2013-11-19 01:37:38 +00:00
|
|
|
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
|