I2P: End-to-End encrypted and anonymous Internet https://i2pd.website/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.7 KiB

11 years ago
#ifndef NETDB_H__
#define NETDB_H__
#include <inttypes.h>
11 years ago
#include <set>
11 years ago
#include <map>
#include <vector>
11 years ago
#include <string>
11 years ago
#include <thread>
#include <mutex>
#include <boost/filesystem.hpp>
#include "Queue.h"
#include "I2NPProtocol.h"
11 years ago
#include "RouterInfo.h"
#include "LeaseSet.h"
#include "Tunnel.h"
#include "TunnelPool.h"
11 years ago
#include "AddressBook.h"
11 years ago
namespace i2p
{
namespace data
{
11 years ago
class RequestedDestination
{
public:
RequestedDestination (const IdentHash& destination, bool isLeaseSet,
bool isExploratory = false, i2p::tunnel::TunnelPool * pool = nullptr):
11 years ago
m_Destination (destination), m_IsLeaseSet (isLeaseSet), m_IsExploratory (isExploratory),
m_Pool (pool), m_LastRouter (nullptr), m_CreationTime (0) {};
11 years ago
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; };
11 years ago
bool IsExploratory () const { return m_IsExploratory; };
bool IsLeaseSet () const { return m_IsLeaseSet; };
11 years ago
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
uint64_t GetCreationTime () const { return m_CreationTime; };
11 years ago
I2NPMessage * CreateRequestMessage (const RouterInfo * router, const i2p::tunnel::InboundTunnel * replyTunnel);
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);
11 years ago
private:
IdentHash m_Destination;
bool m_IsLeaseSet, m_IsExploratory;
i2p::tunnel::TunnelPool * m_Pool;
11 years ago
std::set<IdentHash> m_ExcludedPeers;
const RouterInfo * m_LastRouter;
uint64_t m_CreationTime;
11 years ago
};
11 years ago
class NetDb
{
public:
NetDb ();
~NetDb ();
11 years ago
void Start ();
void Stop ();
11 years ago
void AddRouterInfo (const IdentHash& ident, const uint8_t * buf, int len);
void AddLeaseSet (const IdentHash& ident, const uint8_t * buf, int len, i2p::tunnel::InboundTunnel * from);
RouterInfo * FindRouter (const IdentHash& ident) const;
LeaseSet * FindLeaseSet (const IdentHash& destination) const;
AddressBook& GetAddressBook () { return m_AddressBook; };// TODO: move AddressBook away from NetDb
11 years ago
void Subscribe (const IdentHash& ident, i2p::tunnel::TunnelPool * pool = nullptr); // keep LeaseSets upto date
void Unsubscribe (const IdentHash& ident);
void PublishLeaseSet (const LeaseSet * leaseSet, i2p::tunnel::TunnelPool * pool);
void RequestDestination (const IdentHash& destination, bool isLeaseSet = false,
i2p::tunnel::TunnelPool * pool = nullptr);
void HandleDatabaseStoreMsg (I2NPMessage * msg);
void HandleDatabaseSearchReplyMsg (I2NPMessage * msg);
void HandleDatabaseLookupMsg (I2NPMessage * msg);
const RouterInfo * GetRandomRouter () const;
const RouterInfo * GetRandomRouter (const RouterInfo * compatibleWith) const;
const RouterInfo * GetHighBandwidthRandomRouter (const RouterInfo * compatibleWith) const;
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 (); };
11 years ago
private:
bool CreateNetDb(boost::filesystem::path directory);
11 years ago
void Load (const char * directory);
void SaveUpdated (const char * directory);
11 years ago
void Run (); // exploratory thread
void Explore (int numDestinations);
11 years ago
void Publish ();
void ValidateSubscriptions ();
const RouterInfo * GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
void ManageLeaseSets ();
11 years ago
RequestedDestination * CreateRequestedDestination (const IdentHash& dest,
bool isLeaseSet, bool isExploratory = false, i2p::tunnel::TunnelPool * pool = nullptr);
bool DeleteRequestedDestination (const IdentHash& dest); // returns true if found
void DeleteRequestedDestination (RequestedDestination * dest);
template<typename Filter>
const RouterInfo * GetRandomRouter (Filter filter) const;
11 years ago
11 years ago
private:
std::map<IdentHash, LeaseSet *> m_LeaseSets;
std::map<IdentHash, RouterInfo *> m_RouterInfos;
mutable std::mutex m_FloodfillsMutex;
std::vector<RouterInfo *> m_Floodfills;
std::mutex m_RequestedDestinationsMutex;
11 years ago
std::map<IdentHash, RequestedDestination *> m_RequestedDestinations;
std::map<IdentHash, i2p::tunnel::TunnelPool *> m_Subscriptions;
11 years ago
11 years ago
bool m_IsRunning;
int m_ReseedRetries;
11 years ago
std::thread * m_Thread;
i2p::util::Queue<I2NPMessage> m_Queue; // of I2NPDatabaseStoreMsg
11 years ago
AddressBook m_AddressBook;
static const char m_NetDbPath[];
11 years ago
};
extern NetDb netdb;
}
}
#endif