2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-01-14 17:16:31 -05:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-04-09 12:45:00 -04:00
|
|
|
#ifndef NETDB_REQUESTS_H__
|
|
|
|
#define NETDB_REQUESTS_H__
|
|
|
|
|
2024-01-14 17:16:31 -05:00
|
|
|
#include <inttypes.h>
|
2015-04-09 12:45:00 -04:00
|
|
|
#include <memory>
|
2024-06-05 15:08:51 -04:00
|
|
|
#include <random>
|
2024-05-15 13:31:31 -04:00
|
|
|
#include <unordered_set>
|
2024-01-14 17:16:31 -05:00
|
|
|
#include <unordered_map>
|
2024-05-24 21:49:39 -04:00
|
|
|
#include <list>
|
2015-04-09 12:45:00 -04:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "RouterInfo.h"
|
2024-05-05 11:24:44 -04:00
|
|
|
#include "util.h"
|
2015-04-09 12:45:00 -04:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2024-05-15 13:31:31 -04:00
|
|
|
const int MAX_NUM_REQUEST_ATTEMPTS = 5;
|
2024-12-23 17:52:14 -05:00
|
|
|
const uint64_t MANAGE_REQUESTS_INTERVAL = 400; // in milliseconds
|
|
|
|
const uint64_t MANAGE_REQUESTS_INTERVAL_VARIANCE = 300; // in milliseconds
|
|
|
|
const uint64_t MIN_REQUEST_TIME = 1200; // in milliseconds
|
|
|
|
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL + MANAGE_REQUESTS_INTERVAL_VARIANCE);
|
2024-05-22 18:29:40 -04:00
|
|
|
const uint64_t EXPLORATORY_REQUEST_INTERVAL = 55; // in seconds
|
|
|
|
const uint64_t EXPLORATORY_REQUEST_INTERVAL_VARIANCE = 170; // in seconds
|
2024-05-25 15:17:09 -04:00
|
|
|
const uint64_t DISCOVERED_REQUEST_INTERVAL = 360; // in milliseconds
|
|
|
|
const uint64_t DISCOVERED_REQUEST_INTERVAL_VARIANCE = 540; // in milliseconds
|
2024-12-23 17:52:14 -05:00
|
|
|
const uint64_t MAX_EXPLORATORY_REQUEST_TIME = 30000; // in milliseconds
|
|
|
|
const uint64_t REQUEST_CACHE_TIME = MAX_REQUEST_TIME + 40000; // in milliseconds
|
2024-05-05 11:24:44 -04:00
|
|
|
const uint64_t REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL = 191; // in seconds
|
2024-01-14 17:16:31 -05:00
|
|
|
|
2015-04-09 12:45:00 -04:00
|
|
|
class RequestedDestination
|
2018-01-06 11:48:51 +08:00
|
|
|
{
|
2015-04-09 12:45:00 -04:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::function<void (std::shared_ptr<RouterInfo>)> RequestComplete;
|
|
|
|
|
2024-01-15 19:32:17 -05:00
|
|
|
RequestedDestination (const IdentHash& destination, bool isExploratory = false, bool direct = true);
|
|
|
|
~RequestedDestination ();
|
2015-04-09 12:45:00 -04:00
|
|
|
|
|
|
|
const IdentHash& GetDestination () const { return m_Destination; };
|
2024-05-23 13:36:29 -04:00
|
|
|
const std::unordered_set<IdentHash>& GetExcludedPeers () const { return m_ExcludedPeers; };
|
2024-05-15 13:31:31 -04:00
|
|
|
int GetNumAttempts () const { return m_NumAttempts; };
|
2015-04-09 12:45:00 -04:00
|
|
|
void ClearExcludedPeers ();
|
|
|
|
bool IsExploratory () const { return m_IsExploratory; };
|
2024-01-14 17:16:31 -05:00
|
|
|
bool IsDirect () const { return m_IsDirect; };
|
2024-04-25 18:52:10 -04:00
|
|
|
bool IsActive () const { return m_IsActive; };
|
2024-04-26 15:35:32 -04:00
|
|
|
bool IsExcluded (const IdentHash& ident) const;
|
2015-04-09 12:45:00 -04:00
|
|
|
uint64_t GetCreationTime () const { return m_CreationTime; };
|
2024-01-15 19:32:17 -05:00
|
|
|
uint64_t GetLastRequestTime () const { return m_LastRequestTime; };
|
2015-06-22 15:47:45 -04:00
|
|
|
std::shared_ptr<I2NPMessage> CreateRequestMessage (std::shared_ptr<const RouterInfo>, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel);
|
2015-06-17 12:25:02 -04:00
|
|
|
std::shared_ptr<I2NPMessage> CreateRequestMessage (const IdentHash& floodfill);
|
2018-01-06 11:48:51 +08:00
|
|
|
|
2024-05-24 21:49:39 -04:00
|
|
|
void AddRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete.push_back (requestComplete); };
|
|
|
|
void ResetRequestComplete () { m_RequestComplete.clear (); };
|
2015-04-09 12:45:00 -04:00
|
|
|
void Success (std::shared_ptr<RouterInfo> r);
|
|
|
|
void Fail ();
|
2018-01-06 11:48:51 +08:00
|
|
|
|
2024-05-24 21:49:39 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
void InvokeRequestComplete (std::shared_ptr<RouterInfo> r);
|
|
|
|
|
2015-04-09 12:45:00 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
IdentHash m_Destination;
|
2024-04-25 18:52:10 -04:00
|
|
|
bool m_IsExploratory, m_IsDirect, m_IsActive;
|
2024-05-15 13:31:31 -04:00
|
|
|
std::unordered_set<IdentHash> m_ExcludedPeers;
|
2024-12-23 17:52:14 -05:00
|
|
|
uint64_t m_CreationTime, m_LastRequestTime; // in milliseconds
|
2024-05-24 21:49:39 -04:00
|
|
|
std::list<RequestComplete> m_RequestComplete;
|
2024-05-15 13:31:31 -04:00
|
|
|
int m_NumAttempts;
|
2018-01-06 11:48:51 +08:00
|
|
|
};
|
2015-04-09 12:45:00 -04:00
|
|
|
|
2024-05-21 21:25:19 -04:00
|
|
|
class NetDbRequests: public std::enable_shared_from_this<NetDbRequests>,
|
|
|
|
private i2p::util::RunnableServiceWithWork
|
2015-04-09 12:45:00 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2024-05-21 21:25:19 -04:00
|
|
|
NetDbRequests ();
|
|
|
|
~NetDbRequests ();
|
|
|
|
|
2015-04-09 12:45:00 -04:00
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
void RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r);
|
2024-05-21 22:19:42 -04:00
|
|
|
void PostDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
|
2024-05-22 13:43:00 -04:00
|
|
|
void PostRequestDestination (const IdentHash& destination, const RequestedDestination::RequestComplete& requestComplete, bool direct);
|
2024-05-21 22:19:42 -04:00
|
|
|
|
2024-05-21 21:25:19 -04:00
|
|
|
private:
|
|
|
|
|
2024-05-31 21:11:47 -04:00
|
|
|
std::shared_ptr<RequestedDestination> CreateRequest (const IdentHash& destination, bool isExploratory,
|
|
|
|
bool direct = false, RequestedDestination::RequestComplete requestComplete = nullptr);
|
2024-05-23 13:36:29 -04:00
|
|
|
std::shared_ptr<RequestedDestination> FindRequest (const IdentHash& ident) const;
|
|
|
|
bool SendNextRequest (std::shared_ptr<RequestedDestination> dest);
|
|
|
|
|
2024-05-21 22:19:42 -04:00
|
|
|
void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
|
2024-05-25 15:17:09 -04:00
|
|
|
void RequestRouter (const IdentHash& router);
|
2024-05-22 13:43:00 -04:00
|
|
|
void RequestDestination (const IdentHash& destination, const RequestedDestination::RequestComplete& requestComplete, bool direct);
|
2024-05-22 18:29:40 -04:00
|
|
|
void Explore (int numDestinations);
|
2024-05-21 21:25:19 -04:00
|
|
|
void ManageRequests ();
|
|
|
|
// timer
|
|
|
|
void ScheduleManageRequests ();
|
|
|
|
void HandleManageRequestsTimer (const boost::system::error_code& ecode);
|
2024-05-22 18:29:40 -04:00
|
|
|
void ScheduleExploratory (uint64_t interval);
|
|
|
|
void HandleExploratoryTimer (const boost::system::error_code& ecode);
|
2024-05-23 14:27:39 -04:00
|
|
|
void ScheduleCleanup ();
|
|
|
|
void HandleCleanupTimer (const boost::system::error_code& ecode);
|
2024-05-25 15:17:09 -04:00
|
|
|
void ScheduleDiscoveredRoutersRequest ();
|
|
|
|
void HandleDiscoveredRoutersTimer (const boost::system::error_code& ecode);
|
2024-01-14 17:16:31 -05:00
|
|
|
|
2015-04-09 12:45:00 -04:00
|
|
|
private:
|
|
|
|
|
2024-12-23 17:52:14 -05:00
|
|
|
i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool;
|
2024-01-14 17:16:31 -05:00
|
|
|
std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations;
|
2024-05-25 15:17:09 -04:00
|
|
|
std::list<IdentHash> m_DiscoveredRouterHashes;
|
|
|
|
boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer,
|
|
|
|
m_CleanupTimer, m_DiscoveredRoutersTimer;
|
2024-06-05 15:08:51 -04:00
|
|
|
std::mt19937 m_Rng;
|
2015-04-09 12:45:00 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|