2020-05-22 13:18:41 +00:00
/*
2024-01-14 22:16:31 +00:00
* Copyright ( c ) 2013 - 2024 , The PurpleI2P Project
2020-05-22 13:18:41 +00: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 16:45:00 +00:00
# include "Log.h"
# include "I2NPProtocol.h"
# include "Transports.h"
2017-04-22 00:04:16 +00:00
# include "NetDb.hpp"
2024-01-14 22:16:31 +00:00
# include "ECIESX25519AEADRatchetSession.h"
2024-05-15 17:31:31 +00:00
# include "RouterContext.h"
2024-06-05 19:08:51 +00:00
# include "Timestamp.h"
# include "NetDbRequests.h"
2015-04-09 16:45:00 +00:00
namespace i2p
{
namespace data
{
2024-01-16 00:32:17 +00:00
RequestedDestination : : RequestedDestination ( const IdentHash & destination , bool isExploratory , bool direct ) :
2024-04-25 22:52:10 +00:00
m_Destination ( destination ) , m_IsExploratory ( isExploratory ) , m_IsDirect ( direct ) , m_IsActive ( true ) ,
2024-05-15 17:31:31 +00:00
m_CreationTime ( i2p : : util : : GetSecondsSinceEpoch ( ) ) , m_LastRequestTime ( 0 ) , m_NumAttempts ( 0 )
2024-01-16 00:32:17 +00:00
{
2024-05-15 17:31:31 +00:00
if ( i2p : : context . IsFloodfill ( ) )
m_ExcludedPeers . insert ( i2p : : context . GetIdentHash ( ) ) ; // exclude self if floodfill
2024-01-16 00:32:17 +00:00
}
RequestedDestination : : ~ RequestedDestination ( )
{
2024-05-25 01:49:39 +00:00
InvokeRequestComplete ( nullptr ) ;
2024-01-16 00:32:17 +00:00
}
2015-06-22 19:47:45 +00:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( std : : shared_ptr < const RouterInfo > router ,
2015-04-09 16:45:00 +00:00
std : : shared_ptr < const i2p : : tunnel : : InboundTunnel > replyTunnel )
{
2016-11-14 17:05:44 +00:00
std : : shared_ptr < I2NPMessage > msg ;
if ( replyTunnel )
2018-01-06 03:48:51 +00:00
msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2020-03-01 10:25:50 +00:00
replyTunnel - > GetNextIdentHash ( ) , replyTunnel - > GetNextTunnelID ( ) , m_IsExploratory ,
& m_ExcludedPeers ) ;
2016-11-14 17:05:44 +00:00
else
msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination , i2p : : context . GetIdentHash ( ) , 0 , m_IsExploratory , & m_ExcludedPeers ) ;
if ( router )
m_ExcludedPeers . insert ( router - > GetIdentHash ( ) ) ;
2024-01-16 00:32:17 +00:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2024-05-15 17:31:31 +00:00
m_NumAttempts + + ;
2015-11-03 14:15:49 +00:00
return msg ;
2018-01-06 03:48:51 +00:00
}
2015-04-09 16:45:00 +00:00
2015-06-17 16:25:02 +00:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( const IdentHash & floodfill )
2015-04-09 16:45:00 +00:00
{
2018-01-06 03:48:51 +00:00
auto msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2015-04-09 16:45:00 +00:00
i2p : : context . GetRouterInfo ( ) . GetIdentHash ( ) , 0 , false , & m_ExcludedPeers ) ;
m_ExcludedPeers . insert ( floodfill ) ;
2024-05-15 18:12:57 +00:00
m_NumAttempts + + ;
2024-01-16 00:32:17 +00:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2015-11-03 14:15:49 +00:00
return msg ;
2018-01-06 03:48:51 +00:00
}
2015-04-09 16:45:00 +00:00
2024-04-26 19:35:32 +00:00
bool RequestedDestination : : IsExcluded ( const IdentHash & ident ) const
{
return m_ExcludedPeers . count ( ident ) ;
}
2015-04-09 16:45:00 +00:00
void RequestedDestination : : ClearExcludedPeers ( )
{
m_ExcludedPeers . clear ( ) ;
2018-01-06 03:48:51 +00:00
}
2024-05-25 01:49:39 +00:00
void RequestedDestination : : InvokeRequestComplete ( std : : shared_ptr < RouterInfo > r )
{
if ( ! m_RequestComplete . empty ( ) )
{
for ( auto it : m_RequestComplete )
if ( it ! = nullptr ) it ( r ) ;
m_RequestComplete . clear ( ) ;
}
}
2024-04-27 12:38:43 +00:00
2015-04-09 16:45:00 +00:00
void RequestedDestination : : Success ( std : : shared_ptr < RouterInfo > r )
{
2024-05-03 17:00:30 +00:00
if ( m_IsActive )
{
m_IsActive = false ;
2024-05-25 01:49:39 +00:00
InvokeRequestComplete ( r ) ;
2024-05-03 17:00:30 +00:00
}
2015-04-09 16:45:00 +00:00
}
void RequestedDestination : : Fail ( )
{
2024-05-03 17:00:30 +00:00
if ( m_IsActive )
{
m_IsActive = false ;
2024-05-25 01:49:39 +00:00
InvokeRequestComplete ( nullptr ) ;
2024-05-03 17:00:30 +00:00
}
2015-04-09 16:45:00 +00:00
}
2024-05-22 01:25:19 +00:00
NetDbRequests : : NetDbRequests ( ) :
RunnableServiceWithWork ( " NetDbReq " ) ,
2024-05-23 18:27:39 +00:00
m_ManageRequestsTimer ( GetIOService ( ) ) , m_ExploratoryTimer ( GetIOService ( ) ) ,
2024-06-05 19:08:51 +00:00
m_CleanupTimer ( GetIOService ( ) ) , m_DiscoveredRoutersTimer ( GetIOService ( ) ) ,
m_Rng ( i2p : : util : : GetMonotonicMicroseconds ( ) % 1000000LL )
2024-05-22 01:25:19 +00:00
{
}
NetDbRequests : : ~ NetDbRequests ( )
{
Stop ( ) ;
}
2015-04-09 16:45:00 +00:00
void NetDbRequests : : Start ( )
{
2024-05-22 01:25:19 +00:00
if ( ! IsRunning ( ) )
{
StartIOService ( ) ;
ScheduleManageRequests ( ) ;
2024-05-23 18:27:39 +00:00
ScheduleCleanup ( ) ;
2024-05-22 22:29:40 +00:00
if ( ! i2p : : context . IsHidden ( ) )
ScheduleExploratory ( EXPLORATORY_REQUEST_INTERVAL ) ;
2024-05-22 01:25:19 +00:00
}
2015-04-09 16:45:00 +00:00
}
void NetDbRequests : : Stop ( )
{
2024-05-22 01:25:19 +00:00
if ( IsRunning ( ) )
{
m_ManageRequestsTimer . cancel ( ) ;
2024-05-22 22:29:40 +00:00
m_ExploratoryTimer . cancel ( ) ;
2024-05-23 18:27:39 +00:00
m_CleanupTimer . cancel ( ) ;
2024-05-22 01:25:19 +00:00
StopIOService ( ) ;
m_RequestedDestinations . clear ( ) ;
m_RequestedDestinationsPool . CleanUpMt ( ) ;
}
2015-04-09 16:45:00 +00:00
}
2024-05-23 18:27:39 +00:00
void NetDbRequests : : ScheduleCleanup ( )
{
m_CleanupTimer . expires_from_now ( boost : : posix_time : : seconds ( REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL ) ) ;
m_CleanupTimer . async_wait ( std : : bind ( & NetDbRequests : : HandleCleanupTimer ,
this , std : : placeholders : : _1 ) ) ;
}
void NetDbRequests : : HandleCleanupTimer ( const boost : : system : : error_code & ecode )
{
if ( ecode ! = boost : : asio : : error : : operation_aborted )
{
m_RequestedDestinationsPool . CleanUpMt ( ) ;
ScheduleCleanup ( ) ;
}
}
2024-01-14 22:16:31 +00:00
std : : shared_ptr < RequestedDestination > NetDbRequests : : CreateRequest ( const IdentHash & destination ,
bool isExploratory , bool direct , RequestedDestination : : RequestComplete requestComplete )
2015-04-09 16:45:00 +00:00
{
// request RouterInfo directly
2024-05-05 15:24:44 +00:00
auto dest = m_RequestedDestinationsPool . AcquireSharedMt ( destination , isExploratory , direct ) ;
2024-05-25 01:49:39 +00:00
if ( requestComplete )
dest - > AddRequestComplete ( requestComplete ) ;
2024-06-01 01:11:47 +00:00
auto ret = m_RequestedDestinations . emplace ( destination , dest ) ;
if ( ! ret . second ) // not inserted
{
dest - > ResetRequestComplete ( ) ; // don't call requestComplete in destructor
dest = ret . first - > second ; // existing one
if ( requestComplete )
2024-01-14 23:54:21 +00:00
{
2024-06-01 01:11:47 +00:00
if ( dest - > IsActive ( ) )
dest - > AddRequestComplete ( requestComplete ) ;
else
requestComplete ( nullptr ) ;
2024-01-14 23:54:21 +00:00
}
2024-06-01 01:11:47 +00:00
return nullptr ;
}
2015-04-09 16:45:00 +00:00
return dest ;
2018-01-06 03:48:51 +00:00
}
2015-04-09 16:45:00 +00:00
void NetDbRequests : : RequestComplete ( const IdentHash & ident , std : : shared_ptr < RouterInfo > r )
{
2024-05-22 14:07:01 +00:00
GetIOService ( ) . post ( [ this , ident , r ] ( )
{
std : : shared_ptr < RequestedDestination > request ;
2024-06-01 01:11:47 +00:00
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
2024-05-22 14:07:01 +00:00
{
2024-06-01 01:11:47 +00:00
request = it - > second ;
if ( request - > IsExploratory ( ) )
m_RequestedDestinations . erase ( it ) ;
// otherwise cache for a while
2024-05-22 14:07:01 +00:00
}
if ( request )
{
if ( r )
request - > Success ( r ) ;
else
request - > Fail ( ) ;
}
} ) ;
2015-04-09 16:45:00 +00:00
}
std : : shared_ptr < RequestedDestination > NetDbRequests : : FindRequest ( const IdentHash & ident ) const
{
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
return it - > second ;
return nullptr ;
2018-01-06 03:48:51 +00:00
}
2015-04-09 16:45:00 +00:00
void NetDbRequests : : ManageRequests ( )
{
2018-01-06 03:48:51 +00:00
uint64_t ts = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2015-04-09 16:45:00 +00:00
for ( auto it = m_RequestedDestinations . begin ( ) ; it ! = m_RequestedDestinations . end ( ) ; )
{
auto & dest = it - > second ;
2024-05-03 17:00:30 +00:00
if ( dest - > IsActive ( ) | | ts < dest - > GetCreationTime ( ) + REQUEST_CACHE_TIME )
2024-05-01 16:08:47 +00:00
{
2024-05-03 17:00:30 +00:00
if ( ! dest - > IsExploratory ( ) )
{
// regular request
bool done = false ;
if ( ts < dest - > GetCreationTime ( ) + MAX_REQUEST_TIME )
{
if ( ts > dest - > GetLastRequestTime ( ) + MIN_REQUEST_TIME ) // try next floodfill if no response after min interval
done = ! SendNextRequest ( dest ) ;
}
else // request is expired
done = true ;
if ( done )
dest - > Fail ( ) ;
it + + ;
}
else
{
// exploratory
if ( ts > = dest - > GetCreationTime ( ) + MAX_EXPLORATORY_REQUEST_TIME )
{
dest - > Fail ( ) ;
it = m_RequestedDestinations . erase ( it ) ; // delete expired exploratory request right a way
}
else
it + + ;
}
2024-05-01 16:08:47 +00:00
}
2015-04-09 16:45:00 +00:00
else
2024-05-03 17:00:30 +00:00
it = m_RequestedDestinations . erase ( it ) ;
2018-01-06 03:48:51 +00:00
}
2015-04-09 16:45:00 +00:00
}
2024-01-14 22:16:31 +00:00
bool NetDbRequests : : SendNextRequest ( std : : shared_ptr < RequestedDestination > dest )
{
2024-05-03 17:34:11 +00:00
if ( ! dest | | ! dest - > IsActive ( ) ) return false ;
2024-01-14 22:16:31 +00:00
bool ret = true ;
2024-05-15 17:31:31 +00:00
auto count = dest - > GetNumAttempts ( ) ;
2024-01-14 22:16:31 +00:00
if ( ! dest - > IsExploratory ( ) & & count < MAX_NUM_REQUEST_ATTEMPTS )
{
auto nextFloodfill = netdb . GetClosestFloodfill ( dest - > GetDestination ( ) , dest - > GetExcludedPeers ( ) ) ;
if ( nextFloodfill )
{
bool direct = dest - > IsDirect ( ) ;
if ( direct & & ! nextFloodfill - > IsReachableFrom ( i2p : : context . GetRouterInfo ( ) ) & &
! i2p : : transport : : transports . IsConnected ( nextFloodfill - > GetIdentHash ( ) ) )
direct = false ; // floodfill can't be reached directly
2024-05-22 16:30:01 +00:00
auto s = shared_from_this ( ) ;
auto onDrop = [ s , dest ] ( )
{
if ( dest - > IsActive ( ) )
{
s - > GetIOService ( ) . post ( [ s , dest ] ( )
{
if ( dest - > IsActive ( ) ) s - > SendNextRequest ( dest ) ;
} ) ;
}
} ;
2024-01-14 22:16:31 +00:00
if ( direct )
2024-01-30 00:54:43 +00:00
{
2024-05-02 17:17:15 +00:00
if ( CheckLogLevel ( eLogDebug ) )
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " directly " ) ;
2024-01-30 00:54:43 +00:00
auto msg = dest - > CreateRequestMessage ( nextFloodfill - > GetIdentHash ( ) ) ;
2024-05-22 16:30:01 +00:00
msg - > onDrop = onDrop ;
2024-01-30 00:54:43 +00:00
i2p : : transport : : transports . SendMessage ( nextFloodfill - > GetIdentHash ( ) , msg ) ;
}
2024-01-14 22:16:31 +00:00
else
{
auto pool = i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ;
2024-04-26 17:48:44 +00:00
if ( pool )
{
auto outbound = pool - > GetNextOutboundTunnel ( ) ;
auto inbound = pool - > GetNextInboundTunnel ( ) ;
if ( nextFloodfill & & outbound & & inbound )
{
2024-05-02 17:17:15 +00:00
if ( CheckLogLevel ( eLogDebug ) )
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " through tunnels " ) ;
2024-04-26 17:48:44 +00:00
auto msg = dest - > CreateRequestMessage ( nextFloodfill , inbound ) ;
2024-05-22 16:30:01 +00:00
msg - > onDrop = onDrop ;
2024-04-26 17:48:44 +00:00
outbound - > SendTunnelDataMsgTo ( nextFloodfill - > GetIdentHash ( ) , 0 ,
i2p : : garlic : : WrapECIESX25519MessageForRouter ( msg , nextFloodfill - > GetIdentity ( ) - > GetEncryptionPublicKey ( ) ) ) ;
}
else
{
ret = false ;
if ( ! inbound ) LogPrint ( eLogWarning , " NetDbReq: No inbound tunnels " ) ;
if ( ! outbound ) LogPrint ( eLogWarning , " NetDbReq: No outbound tunnels " ) ;
}
2024-01-14 22:16:31 +00:00
}
else
{
ret = false ;
2024-04-26 17:48:44 +00:00
LogPrint ( eLogWarning , " NetDbReq: Exploratory pool is not ready " ) ;
}
2024-01-14 22:16:31 +00:00
}
}
else
{
ret = false ;
2024-05-13 18:45:41 +00:00
LogPrint ( eLogWarning , " NetDbReq: No more floodfills for " , dest - > GetDestination ( ) . ToBase64 ( ) , " after " , count , " attempts " ) ;
2024-01-14 22:16:31 +00:00
}
}
else
{
if ( ! dest - > IsExploratory ( ) )
2024-05-13 18:45:41 +00:00
LogPrint ( eLogWarning , " NetDbReq: " , dest - > GetDestination ( ) . ToBase64 ( ) , " not found after " , MAX_NUM_REQUEST_ATTEMPTS , " attempts " ) ;
2024-01-14 22:16:31 +00:00
ret = false ;
}
return ret ;
}
2024-05-22 01:25:19 +00:00
void NetDbRequests : : ScheduleManageRequests ( )
{
m_ManageRequestsTimer . expires_from_now ( boost : : posix_time : : seconds ( MANAGE_REQUESTS_INTERVAL ) ) ;
m_ManageRequestsTimer . async_wait ( std : : bind ( & NetDbRequests : : HandleManageRequestsTimer ,
this , std : : placeholders : : _1 ) ) ;
}
void NetDbRequests : : HandleManageRequestsTimer ( const boost : : system : : error_code & ecode )
{
if ( ecode ! = boost : : asio : : error : : operation_aborted )
{
if ( i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ) // expolratory pool is ready?
ManageRequests ( ) ;
ScheduleManageRequests ( ) ;
}
}
2024-05-22 02:19:42 +00:00
void NetDbRequests : : PostDatabaseSearchReplyMsg ( std : : shared_ptr < const I2NPMessage > msg )
{
GetIOService ( ) . post ( [ this , msg ] ( )
{
HandleDatabaseSearchReplyMsg ( msg ) ;
} ) ;
}
void NetDbRequests : : HandleDatabaseSearchReplyMsg ( std : : shared_ptr < const I2NPMessage > msg )
{
const uint8_t * buf = msg - > GetPayload ( ) ;
char key [ 48 ] ;
int l = i2p : : data : : ByteStreamToBase64 ( buf , 32 , key , 48 ) ;
key [ l ] = 0 ;
size_t num = buf [ 32 ] ; // num
LogPrint ( eLogDebug , " NetDbReq: DatabaseSearchReply for " , key , " num= " , num ) ;
IdentHash ident ( buf ) ;
2024-05-25 19:17:09 +00:00
bool isExploratory = false ;
2024-05-22 02:19:42 +00:00
auto dest = FindRequest ( ident ) ;
if ( dest & & dest - > IsActive ( ) )
{
2024-05-25 19:17:09 +00:00
isExploratory = dest - > IsExploratory ( ) ;
if ( ! isExploratory & & ( num > 0 | | dest - > GetNumAttempts ( ) < 3 ) ) // before 3-rd attempt might be just bad luck
2024-05-22 02:19:42 +00:00
{
// try to send next requests
if ( ! SendNextRequest ( dest ) )
RequestComplete ( ident , nullptr ) ;
}
else
// no more requests for destination possible. delete it
RequestComplete ( ident , nullptr ) ;
}
else /*if (!m_FloodfillBootstrap)*/
{
LogPrint ( eLogInfo , " NetDbReq: Unsolicited or late database search reply for " , key ) ;
return ;
}
// try responses
if ( num > NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES )
{
LogPrint ( eLogWarning , " NetDbReq: Too many peer hashes " , num , " in database search reply, Reduced to " , NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES ) ;
num = NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES ;
}
2024-05-25 19:17:09 +00:00
if ( isExploratory & & ! m_DiscoveredRouterHashes . empty ( ) )
{
// request outstanding routers
for ( auto it : m_DiscoveredRouterHashes )
RequestRouter ( it ) ;
m_DiscoveredRouterHashes . clear ( ) ;
m_DiscoveredRoutersTimer . cancel ( ) ;
}
2024-05-22 02:19:42 +00:00
for ( size_t i = 0 ; i < num ; i + + )
{
2024-05-25 19:17:09 +00:00
IdentHash router ( buf + 33 + i * 32 ) ;
if ( CheckLogLevel ( eLogDebug ) )
LogPrint ( eLogDebug , " NetDbReq: " , i , " : " , router . ToBase64 ( ) ) ;
2024-05-22 02:19:42 +00:00
2024-05-25 19:17:09 +00:00
if ( isExploratory )
// postpone request
m_DiscoveredRouterHashes . push_back ( router ) ;
else
// send request right a way
RequestRouter ( router ) ;
2024-05-22 02:19:42 +00:00
}
2024-05-25 19:17:09 +00:00
if ( isExploratory & & ! m_DiscoveredRouterHashes . empty ( ) )
ScheduleDiscoveredRoutersRequest ( ) ;
2024-05-22 02:19:42 +00:00
}
2024-05-22 17:43:00 +00:00
2024-05-25 19:17:09 +00:00
void NetDbRequests : : RequestRouter ( const IdentHash & router )
{
auto r = netdb . FindRouter ( router ) ;
if ( ! r | | i2p : : util : : GetMillisecondsSinceEpoch ( ) > r - > GetTimestamp ( ) + 3600 * 1000LL )
{
// router with ident not found or too old (1 hour)
LogPrint ( eLogDebug , " NetDbReq: Found new/outdated router. Requesting RouterInfo... " ) ;
2024-06-01 01:11:47 +00:00
if ( ! IsRouterBanned ( router ) )
2024-05-25 19:17:09 +00:00
RequestDestination ( router , nullptr , true ) ;
else
LogPrint ( eLogDebug , " NetDbReq: Router " , router . ToBase64 ( ) , " is banned. Skipped " ) ;
}
else
LogPrint ( eLogDebug , " NetDbReq: [:|||:] " ) ;
}
2024-05-22 17:43:00 +00:00
void NetDbRequests : : PostRequestDestination ( const IdentHash & destination ,
const RequestedDestination : : RequestComplete & requestComplete , bool direct )
{
GetIOService ( ) . post ( [ this , destination , requestComplete , direct ] ( )
{
RequestDestination ( destination , requestComplete , direct ) ;
} ) ;
}
void NetDbRequests : : RequestDestination ( const IdentHash & destination , const RequestedDestination : : RequestComplete & requestComplete , bool direct )
{
auto dest = CreateRequest ( destination , false , direct , requestComplete ) ; // non-exploratory
if ( dest )
{
if ( ! SendNextRequest ( dest ) )
RequestComplete ( destination , nullptr ) ;
}
else
LogPrint ( eLogWarning , " NetDbReq: Destination " , destination . ToBase64 ( ) , " is requested already or cached " ) ;
}
2024-05-22 22:29:40 +00:00
void NetDbRequests : : Explore ( int numDestinations )
{
// new requests
auto exploratoryPool = i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ;
auto outbound = exploratoryPool ? exploratoryPool - > GetNextOutboundTunnel ( ) : nullptr ;
auto inbound = exploratoryPool ? exploratoryPool - > GetNextInboundTunnel ( ) : nullptr ;
bool throughTunnels = outbound & & inbound ;
uint8_t randomHash [ 32 ] ;
std : : vector < i2p : : tunnel : : TunnelMessageBlock > msgs ;
LogPrint ( eLogInfo , " NetDbReq: Exploring new " , numDestinations , " routers ... " ) ;
for ( int i = 0 ; i < numDestinations ; i + + )
{
RAND_bytes ( randomHash , 32 ) ;
auto dest = CreateRequest ( randomHash , true , ! throughTunnels ) ; // exploratory
if ( ! dest )
{
LogPrint ( eLogWarning , " NetDbReq: Exploratory destination is requested already " ) ;
return ;
}
auto floodfill = netdb . GetClosestFloodfill ( randomHash , dest - > GetExcludedPeers ( ) ) ;
if ( floodfill )
{
if ( i2p : : transport : : transports . IsConnected ( floodfill - > GetIdentHash ( ) ) )
throughTunnels = false ;
if ( throughTunnels )
{
msgs . push_back ( i2p : : tunnel : : TunnelMessageBlock
{
i2p : : tunnel : : eDeliveryTypeRouter ,
floodfill - > GetIdentHash ( ) , 0 ,
CreateDatabaseStoreMsg ( ) // tell floodfill about us
} ) ;
msgs . push_back ( i2p : : tunnel : : TunnelMessageBlock
{
i2p : : tunnel : : eDeliveryTypeRouter ,
floodfill - > GetIdentHash ( ) , 0 ,
dest - > CreateRequestMessage ( floodfill , inbound ) // explore
} ) ;
}
else
i2p : : transport : : transports . SendMessage ( floodfill - > GetIdentHash ( ) , dest - > CreateRequestMessage ( floodfill - > GetIdentHash ( ) ) ) ;
}
else
RequestComplete ( randomHash , nullptr ) ;
}
if ( throughTunnels & & msgs . size ( ) > 0 )
outbound - > SendTunnelDataMsgs ( msgs ) ;
}
void NetDbRequests : : ScheduleExploratory ( uint64_t interval )
{
m_ExploratoryTimer . expires_from_now ( boost : : posix_time : : seconds ( interval ) ) ;
m_ExploratoryTimer . async_wait ( std : : bind ( & NetDbRequests : : HandleExploratoryTimer ,
this , std : : placeholders : : _1 ) ) ;
}
void NetDbRequests : : HandleExploratoryTimer ( const boost : : system : : error_code & ecode )
{
if ( ecode ! = boost : : asio : : error : : operation_aborted )
{
auto numRouters = netdb . GetNumRouters ( ) ;
2024-06-05 19:08:51 +00:00
auto nextExploratoryInterval = numRouters < 2500 ? ( EXPLORATORY_REQUEST_INTERVAL + m_Rng ( ) % EXPLORATORY_REQUEST_INTERVAL ) / 2 :
EXPLORATORY_REQUEST_INTERVAL + m_Rng ( ) % EXPLORATORY_REQUEST_INTERVAL_VARIANCE ;
2024-05-22 22:29:40 +00:00
if ( numRouters )
{
if ( i2p : : transport : : transports . IsOnline ( ) & & i2p : : transport : : transports . IsRunning ( ) )
{
// explore only if online
numRouters = 800 / numRouters ;
if ( numRouters < 1 ) numRouters = 1 ;
if ( numRouters > 9 ) numRouters = 9 ;
Explore ( numRouters ) ;
}
}
else
LogPrint ( eLogError , " NetDbReq: No known routers, reseed seems to be totally failed " ) ;
ScheduleExploratory ( nextExploratoryInterval ) ;
}
}
2024-05-25 19:17:09 +00:00
void NetDbRequests : : ScheduleDiscoveredRoutersRequest ( )
{
m_DiscoveredRoutersTimer . expires_from_now ( boost : : posix_time : : milliseconds (
2024-06-05 19:08:51 +00:00
DISCOVERED_REQUEST_INTERVAL + m_Rng ( ) % DISCOVERED_REQUEST_INTERVAL_VARIANCE ) ) ;
2024-05-25 19:17:09 +00:00
m_DiscoveredRoutersTimer . async_wait ( std : : bind ( & NetDbRequests : : HandleDiscoveredRoutersTimer ,
this , std : : placeholders : : _1 ) ) ;
}
void NetDbRequests : : HandleDiscoveredRoutersTimer ( const boost : : system : : error_code & ecode )
{
if ( ecode ! = boost : : asio : : error : : operation_aborted )
{
if ( ! m_DiscoveredRouterHashes . empty ( ) )
{
RequestRouter ( m_DiscoveredRouterHashes . front ( ) ) ;
m_DiscoveredRouterHashes . pop_front ( ) ;
if ( ! m_DiscoveredRouterHashes . empty ( ) ) // more hashes to request
ScheduleDiscoveredRoutersRequest ( ) ;
}
}
}
2015-04-09 16:45:00 +00:00
}
}