i2pd/NetDb.h

130 lines
4.4 KiB
C
Raw Normal View History

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