i2pd/libi2pd_client/AddressBook.h

175 lines
5.8 KiB
C
Raw Permalink Normal View History

/*
2024-09-11 19:06:55 +03:00
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2014-03-13 22:22:32 +04:00
#ifndef ADDRESS_BOOK_H__
#define ADDRESS_BOOK_H__
#include <string.h>
#include <string>
#include <map>
#include <vector>
2014-12-20 06:03:34 +03:00
#include <iostream>
2014-12-21 17:33:02 +03:00
#include <mutex>
2015-11-03 17:15:49 +03:00
#include <memory>
#include <boost/asio.hpp>
2015-11-03 17:15:49 +03:00
#include "Base.h"
2014-03-13 22:22:32 +04:00
#include "Identity.h"
#include "Log.h"
2016-03-24 21:48:07 +03:00
#include "Destination.h"
2019-03-27 22:19:10 +03:00
#include "LeaseSet.h"
2014-03-13 22:22:32 +04:00
namespace i2p
{
2014-10-24 23:22:36 +04:00
namespace client
2014-03-13 22:22:32 +04:00
{
2018-01-06 06:48:51 +03:00
const int INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT = 3; // in minutes
const int INITIAL_SUBSCRIPTION_RETRY_TIMEOUT = 1; // in minutes
const int CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT = 720; // in minutes (12 hours)
const int CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT = 5; // in minutes
const int CONTINIOUS_SUBSCRIPTION_MAX_NUM_RETRIES = 10; // then update timeout
2018-01-24 15:34:32 +03:00
const int SUBSCRIPTION_REQUEST_TIMEOUT = 120; //in second
2016-03-24 21:48:07 +03:00
2018-01-06 06:48:51 +03:00
const uint16_t ADDRESS_RESOLVER_DATAGRAM_PORT = 53;
2016-03-26 17:31:47 +03:00
const uint16_t ADDRESS_RESPONSE_DATAGRAM_PORT = 54;
2018-01-06 06:48:51 +03:00
2019-03-29 01:48:38 +03:00
const size_t B33_ADDRESS_THRESHOLD = 52; // characters
2019-03-27 22:19:10 +03:00
struct Address
{
enum { eAddressIndentHash, eAddressBlindedPublicKey, eAddressInvalid } addressType;
2019-03-27 22:19:10 +03:00
i2p::data::IdentHash identHash;
std::shared_ptr<i2p::data::BlindedPublicKey> blindedPublicKey;
Address (const std::string& b32);
Address (const i2p::data::IdentHash& hash);
2019-03-27 22:19:10 +03:00
bool IsIdentHash () const { return addressType == eAddressIndentHash; };
bool IsValid () const { return addressType != eAddressInvalid; };
2019-03-27 22:19:10 +03:00
};
2015-01-07 23:50:12 +03:00
inline std::string GetB32Address(const i2p::data::IdentHash& ident) { return ident.ToBase32().append(".b32.i2p"); }
2014-11-27 00:19:36 +03:00
class AddressBookStorage // interface for storage
{
public:
virtual ~AddressBookStorage () {};
2018-01-06 06:48:51 +03:00
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
2015-11-03 17:15:49 +03:00
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
2014-11-27 00:19:36 +03:00
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
2018-01-06 06:48:51 +03:00
virtual bool Init () = 0;
2019-03-27 22:19:10 +03:00
virtual int Load (std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual int LoadLocal (std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual int Save (const std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual void SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) = 0;
2016-03-15 21:37:07 +03:00
virtual bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) = 0;
virtual void ResetEtags () = 0;
2018-01-06 06:48:51 +03:00
};
2014-11-27 00:19:36 +03:00
2014-12-20 06:03:34 +03:00
class AddressBookSubscription;
2016-03-24 21:48:07 +03:00
class AddressResolver;
2014-03-13 22:22:32 +04:00
class AddressBook
{
public:
2014-04-01 23:18:14 +04:00
AddressBook ();
2014-11-27 00:19:36 +03:00
~AddressBook ();
2015-03-30 17:21:52 +03:00
void Start ();
2016-03-26 17:31:47 +03:00
void StartResolvers ();
2015-03-30 17:21:52 +03:00
void Stop ();
2019-03-28 16:57:34 +03:00
std::shared_ptr<const Address> GetAddress (const std::string& address);
2019-03-27 22:19:10 +03:00
std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address);
std::shared_ptr<const Address> FindAddress (const std::string& address);
2016-03-26 17:31:47 +03:00
void LookupAddress (const std::string& address);
2019-04-02 20:11:49 +03:00
void InsertAddress (const std::string& address, const std::string& jump); // for jump links
2019-03-27 22:19:10 +03:00
void InsertFullAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
2014-12-20 06:03:34 +03:00
bool RecordExists (const std::string& address, const std::string& jump);
2016-07-16 03:00:00 +03:00
bool LoadHostsFromStream (std::istream& f, bool is_update);
void DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified);
//This method returns the ".b32.i2p" address
2015-01-07 23:50:12 +03:00
std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); }
2015-11-03 17:15:49 +03:00
std::string ToAddress(std::shared_ptr<const i2p::data::IdentityEx> ident) { return ToAddress(ident->GetIdentHash ()); }
2016-03-15 21:37:07 +03:00
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified);
2024-09-11 19:06:55 +03:00
bool IsEnabled () const { return m_IsEnabled; }
2014-03-13 22:22:32 +04:00
private:
2014-11-27 00:51:36 +03:00
2015-03-30 17:21:52 +03:00
void StartSubscriptions ();
void StopSubscriptions ();
2018-01-06 06:48:51 +03:00
2014-04-01 23:18:14 +04:00
void LoadHosts ();
2014-12-22 23:06:54 +03:00
void LoadSubscriptions ();
2016-03-24 21:48:07 +03:00
void LoadLocal ();
2014-03-13 22:22:32 +04:00
void HandleSubscriptionsUpdateTimer (const boost::system::error_code& ecode);
2016-03-26 17:31:47 +03:00
void StartLookups ();
void StopLookups ();
void HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);
2018-01-06 06:48:51 +03:00
private:
2014-12-21 17:33:02 +03:00
std::mutex m_AddressBookMutex;
std::map<std::string, std::shared_ptr<Address> > m_Addresses;
2016-03-24 21:48:07 +03:00
std::map<i2p::data::IdentHash, std::shared_ptr<AddressResolver> > m_Resolvers; // local destination->resolver
2016-03-26 17:31:47 +03:00
std::mutex m_LookupsMutex;
std::map<uint32_t, std::string> m_Lookups; // nonce -> address
2014-11-27 00:19:36 +03:00
AddressBookStorage * m_Storage;
2014-12-21 17:33:02 +03:00
volatile bool m_IsLoaded, m_IsDownloading;
int m_NumRetries;
std::vector<std::shared_ptr<AddressBookSubscription> > m_Subscriptions;
std::shared_ptr<AddressBookSubscription> m_DefaultSubscription; // in case if we don't know any addresses yet
boost::asio::deadline_timer * m_SubscriptionsUpdateTimer;
2022-05-27 20:17:06 +03:00
bool m_IsEnabled;
2014-03-13 22:22:32 +04:00
};
class AddressBookSubscription
{
public:
AddressBookSubscription (AddressBook& book, const std::string& link);
void CheckUpdates ();
private:
bool MakeRequest ();
2018-01-06 06:48:51 +03:00
private:
AddressBook& m_Book;
2014-12-22 23:06:54 +03:00
std::string m_Link, m_Etag, m_LastModified;
i2p::data::IdentHash m_Ident;
2016-03-15 05:00:05 +03:00
// m_Etag must be surrounded by ""
};
2016-03-24 21:48:07 +03:00
class AddressResolver
{
public:
AddressResolver (std::shared_ptr<ClientDestination> destination);
2016-03-26 17:31:47 +03:00
~AddressResolver ();
2016-03-24 21:48:07 +03:00
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;
2016-03-24 21:48:07 +03:00
};
2014-03-13 22:22:32 +04:00
}
}
#endif