mirror of https://github.com/PurpleI2P/i2pd.git
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.
147 lines
4.9 KiB
147 lines
4.9 KiB
11 years ago
|
#ifndef ADDRESS_BOOK_H__
|
||
|
#define ADDRESS_BOOK_H__
|
||
|
|
||
|
#include <string.h>
|
||
|
#include <string>
|
||
|
#include <map>
|
||
10 years ago
|
#include <vector>
|
||
10 years ago
|
#include <iostream>
|
||
10 years ago
|
#include <mutex>
|
||
9 years ago
|
#include <memory>
|
||
10 years ago
|
#include <boost/asio.hpp>
|
||
9 years ago
|
#include "Base.h"
|
||
11 years ago
|
#include "Identity.h"
|
||
|
#include "Log.h"
|
||
9 years ago
|
#include "Destination.h"
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
10 years ago
|
namespace client
|
||
11 years ago
|
{
|
||
10 years ago
|
const int INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT = 3; // in minutes
|
||
|
const int INITIAL_SUBSCRIPTION_RETRY_TIMEOUT = 1; // in minutes
|
||
10 years ago
|
const int CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT = 720; // in minutes (12 hours)
|
||
10 years ago
|
const int CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT = 5; // in minutes
|
||
10 years ago
|
const int SUBSCRIPTION_REQUEST_TIMEOUT = 60; //in second
|
||
9 years ago
|
|
||
9 years ago
|
const uint16_t ADDRESS_RESOLVER_DATAGRAM_PORT = 53;
|
||
|
const uint16_t ADDRESS_RESPONSE_DATAGRAM_PORT = 54;
|
||
|
|
||
10 years ago
|
inline std::string GetB32Address(const i2p::data::IdentHash& ident) { return ident.ToBase32().append(".b32.i2p"); }
|
||
10 years ago
|
|
||
10 years ago
|
class AddressBookStorage // interface for storage
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
virtual ~AddressBookStorage () {};
|
||
9 years ago
|
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
|
||
|
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
|
||
10 years ago
|
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
||
10 years ago
|
|
||
9 years ago
|
virtual bool Init () = 0;
|
||
10 years ago
|
virtual int Load (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||
9 years ago
|
virtual int LoadLocal (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||
10 years ago
|
virtual int Save (const std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||
9 years ago
|
|
||
|
virtual void SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) = 0;
|
||
9 years ago
|
virtual bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) = 0;
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
class AddressBookSubscription;
|
||
9 years ago
|
class AddressResolver;
|
||
11 years ago
|
class AddressBook
|
||
|
{
|
||
|
public:
|
||
|
|
||
11 years ago
|
AddressBook ();
|
||
10 years ago
|
~AddressBook ();
|
||
10 years ago
|
void Start ();
|
||
9 years ago
|
void StartResolvers ();
|
||
10 years ago
|
void Stop ();
|
||
10 years ago
|
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
|
||
9 years ago
|
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const std::string& address);
|
||
10 years ago
|
const i2p::data::IdentHash * FindAddress (const std::string& address);
|
||
9 years ago
|
void LookupAddress (const std::string& address);
|
||
10 years ago
|
void InsertAddress (const std::string& address, const std::string& base64); // for jump service
|
||
9 years ago
|
void InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
|
||
10 years ago
|
|
||
9 years ago
|
bool LoadHostsFromStream (std::istream& f, bool is_update);
|
||
9 years ago
|
void DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified);
|
||
10 years ago
|
//This method returns the ".b32.i2p" address
|
||
10 years ago
|
std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); }
|
||
9 years ago
|
std::string ToAddress(std::shared_ptr<const i2p::data::IdentityEx> ident) { return ToAddress(ident->GetIdentHash ()); }
|
||
9 years ago
|
|
||
|
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified);
|
||
|
|
||
11 years ago
|
private:
|
||
10 years ago
|
|
||
10 years ago
|
void StartSubscriptions ();
|
||
|
void StopSubscriptions ();
|
||
|
|
||
11 years ago
|
void LoadHosts ();
|
||
10 years ago
|
void LoadSubscriptions ();
|
||
9 years ago
|
void LoadLocal ();
|
||
11 years ago
|
|
||
10 years ago
|
void HandleSubscriptionsUpdateTimer (const boost::system::error_code& ecode);
|
||
|
|
||
9 years ago
|
void StartLookups ();
|
||
|
void StopLookups ();
|
||
|
void HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
|
|
||
10 years ago
|
private:
|
||
10 years ago
|
|
||
|
std::mutex m_AddressBookMutex;
|
||
9 years ago
|
std::map<std::string, i2p::data::IdentHash> m_Addresses;
|
||
|
std::map<i2p::data::IdentHash, std::shared_ptr<AddressResolver> > m_Resolvers; // local destination->resolver
|
||
9 years ago
|
std::mutex m_LookupsMutex;
|
||
|
std::map<uint32_t, std::string> m_Lookups; // nonce -> address
|
||
10 years ago
|
AddressBookStorage * m_Storage;
|
||
10 years ago
|
volatile bool m_IsLoaded, m_IsDownloading;
|
||
9 years ago
|
std::vector<std::shared_ptr<AddressBookSubscription> > m_Subscriptions;
|
||
|
std::shared_ptr<AddressBookSubscription> m_DefaultSubscription; // in case if we don't know any addresses yet
|
||
10 years ago
|
boost::asio::deadline_timer * m_SubscriptionsUpdateTimer;
|
||
11 years ago
|
};
|
||
10 years ago
|
|
||
|
class AddressBookSubscription
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
AddressBookSubscription (AddressBook& book, const std::string& link);
|
||
9 years ago
|
void CheckUpdates ();
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
9 years ago
|
bool MakeRequest ();
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
AddressBook& m_Book;
|
||
10 years ago
|
std::string m_Link, m_Etag, m_LastModified;
|
||
9 years ago
|
i2p::data::IdentHash m_Ident;
|
||
9 years ago
|
// m_Etag must be surrounded by ""
|
||
10 years ago
|
};
|
||
9 years ago
|
|
||
|
class AddressResolver
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
AddressResolver (std::shared_ptr<ClientDestination> destination);
|
||
9 years ago
|
~AddressResolver ();
|
||
9 years ago
|
void AddAddress (const std::string& name, const i2p::data::IdentHash& ident);
|
||
|
|
||
|
private:
|
||
|
|
||
|
void HandleRequest (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
|
||
|
|
||
|
private:
|
||
|
|
||
|
std::shared_ptr<ClientDestination> m_LocalDestination;
|
||
|
std::map<std::string, i2p::data::IdentHash> m_LocalAddresses;
|
||
|
};
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|