2020-05-22 15:18:41 +02:00
/*
2024-01-14 23:16:31 +01:00
* Copyright ( c ) 2013 - 2024 , The PurpleI2P Project
2020-05-22 15:18:41 +02: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 18:45:00 +02:00
# include "Log.h"
2024-05-22 04:19:42 +02:00
# include "Base.h"
2015-04-09 18:45:00 +02:00
# include "I2NPProtocol.h"
# include "Transports.h"
2017-04-22 02:04:16 +02:00
# include "NetDb.hpp"
2015-04-09 18:45:00 +02:00
# include "NetDbRequests.h"
2024-01-14 23:16:31 +01:00
# include "ECIESX25519AEADRatchetSession.h"
2024-05-15 19:31:31 +02:00
# include "RouterContext.h"
2015-04-09 18:45:00 +02:00
namespace i2p
{
namespace data
{
2024-01-16 01:32:17 +01:00
RequestedDestination : : RequestedDestination ( const IdentHash & destination , bool isExploratory , bool direct ) :
2024-04-26 00:52:10 +02:00
m_Destination ( destination ) , m_IsExploratory ( isExploratory ) , m_IsDirect ( direct ) , m_IsActive ( true ) ,
2024-05-15 19:31:31 +02:00
m_CreationTime ( i2p : : util : : GetSecondsSinceEpoch ( ) ) , m_LastRequestTime ( 0 ) , m_NumAttempts ( 0 )
2024-01-16 01:32:17 +01:00
{
2024-05-15 19:31:31 +02:00
if ( i2p : : context . IsFloodfill ( ) )
m_ExcludedPeers . insert ( i2p : : context . GetIdentHash ( ) ) ; // exclude self if floodfill
2024-01-16 01:32:17 +01:00
}
RequestedDestination : : ~ RequestedDestination ( )
{
if ( m_RequestComplete ) m_RequestComplete ( nullptr ) ;
}
2015-06-22 21:47:45 +02:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( std : : shared_ptr < const RouterInfo > router ,
2015-04-09 18:45:00 +02:00
std : : shared_ptr < const i2p : : tunnel : : InboundTunnel > replyTunnel )
{
2024-04-27 00:11:05 +02:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2016-11-14 18:05:44 +01:00
std : : shared_ptr < I2NPMessage > msg ;
if ( replyTunnel )
2018-01-06 04:48:51 +01:00
msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2020-03-01 11:25:50 +01:00
replyTunnel - > GetNextIdentHash ( ) , replyTunnel - > GetNextTunnelID ( ) , m_IsExploratory ,
& m_ExcludedPeers ) ;
2016-11-14 18:05:44 +01: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 01:32:17 +01:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2024-05-15 19:31:31 +02:00
m_NumAttempts + + ;
2015-11-03 15:15:49 +01:00
return msg ;
2018-01-06 04:48:51 +01:00
}
2015-04-09 18:45:00 +02:00
2015-06-17 18:25:02 +02:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( const IdentHash & floodfill )
2015-04-09 18:45:00 +02:00
{
2024-04-27 00:11:05 +02:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2018-01-06 04:48:51 +01:00
auto msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2015-04-09 18:45:00 +02:00
i2p : : context . GetRouterInfo ( ) . GetIdentHash ( ) , 0 , false , & m_ExcludedPeers ) ;
m_ExcludedPeers . insert ( floodfill ) ;
2024-05-15 20:12:57 +02:00
m_NumAttempts + + ;
2024-01-16 01:32:17 +01:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2015-11-03 15:15:49 +01:00
return msg ;
2018-01-06 04:48:51 +01:00
}
2015-04-09 18:45:00 +02:00
2024-04-26 21:35:32 +02:00
bool RequestedDestination : : IsExcluded ( const IdentHash & ident ) const
{
2024-04-27 00:11:05 +02:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2024-04-26 21:35:32 +02:00
return m_ExcludedPeers . count ( ident ) ;
}
2015-04-09 18:45:00 +02:00
void RequestedDestination : : ClearExcludedPeers ( )
{
2024-04-27 00:11:05 +02:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2015-04-09 18:45:00 +02:00
m_ExcludedPeers . clear ( ) ;
2018-01-06 04:48:51 +01:00
}
2024-05-15 19:31:31 +02:00
std : : unordered_set < IdentHash > RequestedDestination : : GetExcludedPeers ( ) const
2024-04-27 14:38:43 +02:00
{
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
return m_ExcludedPeers ;
}
2015-04-09 18:45:00 +02:00
void RequestedDestination : : Success ( std : : shared_ptr < RouterInfo > r )
{
2024-05-03 19:00:30 +02:00
if ( m_IsActive )
{
m_IsActive = false ;
if ( m_RequestComplete )
{
m_RequestComplete ( r ) ;
m_RequestComplete = nullptr ;
}
}
2015-04-09 18:45:00 +02:00
}
void RequestedDestination : : Fail ( )
{
2024-05-03 19:00:30 +02:00
if ( m_IsActive )
{
m_IsActive = false ;
if ( m_RequestComplete )
{
m_RequestComplete ( nullptr ) ;
m_RequestComplete = nullptr ;
}
}
2015-04-09 18:45:00 +02:00
}
2024-05-22 03:25:19 +02:00
NetDbRequests : : NetDbRequests ( ) :
RunnableServiceWithWork ( " NetDbReq " ) ,
m_ManageRequestsTimer ( GetIOService ( ) )
{
}
NetDbRequests : : ~ NetDbRequests ( )
{
Stop ( ) ;
}
2015-04-09 18:45:00 +02:00
void NetDbRequests : : Start ( )
{
2024-05-05 17:24:44 +02:00
m_LastPoolCleanUpTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2024-05-22 03:25:19 +02:00
if ( ! IsRunning ( ) )
{
StartIOService ( ) ;
ScheduleManageRequests ( ) ;
}
2015-04-09 18:45:00 +02:00
}
void NetDbRequests : : Stop ( )
{
2024-05-22 03:25:19 +02:00
if ( IsRunning ( ) )
{
m_ManageRequestsTimer . cancel ( ) ;
StopIOService ( ) ;
m_RequestedDestinations . clear ( ) ;
m_RequestedDestinationsPool . CleanUpMt ( ) ;
}
2015-04-09 18:45:00 +02:00
}
2024-01-14 23:16:31 +01:00
std : : shared_ptr < RequestedDestination > NetDbRequests : : CreateRequest ( const IdentHash & destination ,
bool isExploratory , bool direct , RequestedDestination : : RequestComplete requestComplete )
2015-04-09 18:45:00 +02:00
{
// request RouterInfo directly
2024-05-05 17:24:44 +02:00
auto dest = m_RequestedDestinationsPool . AcquireSharedMt ( destination , isExploratory , direct ) ;
2015-04-09 18:45:00 +02:00
dest - > SetRequestComplete ( requestComplete ) ;
{
2024-01-14 23:16:31 +01:00
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2024-01-15 00:54:21 +01:00
auto ret = m_RequestedDestinations . emplace ( destination , dest ) ;
if ( ! ret . second ) // not inserted
{
dest - > SetRequestComplete ( nullptr ) ; // don't call requestComplete in destructor
2024-05-03 19:34:11 +02:00
dest = ret . first - > second ; // existing one
if ( requestComplete & & dest - > IsActive ( ) )
2024-01-15 00:54:21 +01:00
{
2024-05-03 19:34:11 +02:00
auto prev = dest - > GetRequestComplete ( ) ;
2024-01-15 00:54:21 +01:00
if ( prev ) // if already set
2024-05-03 19:34:11 +02:00
dest - > SetRequestComplete (
2024-01-15 00:54:21 +01:00
[ requestComplete , prev ] ( std : : shared_ptr < RouterInfo > r )
{
prev ( r ) ; // call previous
requestComplete ( r ) ; // then new
} ) ;
else
2024-05-03 19:34:11 +02:00
dest - > SetRequestComplete ( requestComplete ) ;
2024-01-15 00:54:21 +01:00
}
2018-01-06 04:48:51 +01:00
return nullptr ;
2024-01-15 00:54:21 +01:00
}
2015-04-09 18:45:00 +02:00
}
return dest ;
2018-01-06 04:48:51 +01:00
}
2015-04-09 18:45:00 +02:00
void NetDbRequests : : RequestComplete ( const IdentHash & ident , std : : shared_ptr < RouterInfo > r )
{
2024-05-22 16:07:01 +02:00
GetIOService ( ) . post ( [ this , ident , r ] ( )
{
std : : shared_ptr < RequestedDestination > request ;
{
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
{
request = it - > second ;
if ( request - > IsExploratory ( ) )
m_RequestedDestinations . erase ( it ) ;
// otherwise cache for a while
}
}
if ( request )
{
if ( r )
request - > Success ( r ) ;
else
request - > Fail ( ) ;
}
} ) ;
2015-04-09 18:45:00 +02:00
}
std : : shared_ptr < RequestedDestination > NetDbRequests : : FindRequest ( const IdentHash & ident ) const
{
2016-08-16 04:36:58 +02:00
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2015-04-09 18:45:00 +02:00
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
return it - > second ;
return nullptr ;
2018-01-06 04:48:51 +01:00
}
2015-04-09 18:45:00 +02:00
void NetDbRequests : : ManageRequests ( )
{
2018-01-06 04:48:51 +01:00
uint64_t ts = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2024-05-05 17:24:44 +02:00
if ( ts > m_LastPoolCleanUpTime + REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL )
{
m_RequestedDestinationsPool . CleanUpMt ( ) ;
m_LastPoolCleanUpTime = ts ;
}
2018-01-06 04:48:51 +01:00
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2015-04-09 18:45:00 +02:00
for ( auto it = m_RequestedDestinations . begin ( ) ; it ! = m_RequestedDestinations . end ( ) ; )
{
auto & dest = it - > second ;
2024-05-03 19:00:30 +02:00
if ( dest - > IsActive ( ) | | ts < dest - > GetCreationTime ( ) + REQUEST_CACHE_TIME )
2024-05-01 18:08:47 +02:00
{
2024-05-03 19:00:30 +02: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 18:08:47 +02:00
}
2015-04-09 18:45:00 +02:00
else
2024-05-03 19:00:30 +02:00
it = m_RequestedDestinations . erase ( it ) ;
2018-01-06 04:48:51 +01:00
}
2015-04-09 18:45:00 +02:00
}
2024-01-14 23:16:31 +01:00
bool NetDbRequests : : SendNextRequest ( std : : shared_ptr < RequestedDestination > dest )
{
2024-05-03 19:34:11 +02:00
if ( ! dest | | ! dest - > IsActive ( ) ) return false ;
2024-01-14 23:16:31 +01:00
bool ret = true ;
2024-05-15 19:31:31 +02:00
auto count = dest - > GetNumAttempts ( ) ;
2024-01-14 23:16:31 +01: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
if ( direct )
2024-01-30 01:54:43 +01:00
{
2024-05-02 19:17:15 +02:00
if ( CheckLogLevel ( eLogDebug ) )
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " directly " ) ;
2024-01-30 01:54:43 +01:00
auto msg = dest - > CreateRequestMessage ( nextFloodfill - > GetIdentHash ( ) ) ;
2024-05-07 00:23:20 +02:00
auto s = shared_from_this ( ) ;
msg - > onDrop = [ s , dest ] ( ) { if ( dest - > IsActive ( ) ) s - > SendNextRequest ( dest ) ; } ;
2024-01-30 01:54:43 +01:00
i2p : : transport : : transports . SendMessage ( nextFloodfill - > GetIdentHash ( ) , msg ) ;
}
2024-01-14 23:16:31 +01:00
else
{
auto pool = i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ;
2024-04-26 19:48:44 +02:00
if ( pool )
{
auto outbound = pool - > GetNextOutboundTunnel ( ) ;
auto inbound = pool - > GetNextInboundTunnel ( ) ;
if ( nextFloodfill & & outbound & & inbound )
{
2024-05-02 19:17:15 +02:00
if ( CheckLogLevel ( eLogDebug ) )
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " through tunnels " ) ;
2024-04-26 19:48:44 +02:00
auto msg = dest - > CreateRequestMessage ( nextFloodfill , inbound ) ;
2024-05-07 00:23:20 +02:00
auto s = shared_from_this ( ) ;
msg - > onDrop = [ s , dest ] ( ) { if ( dest - > IsActive ( ) ) s - > SendNextRequest ( dest ) ; } ;
2024-04-26 19:48:44 +02: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 23:16:31 +01:00
}
else
{
ret = false ;
2024-04-26 19:48:44 +02:00
LogPrint ( eLogWarning , " NetDbReq: Exploratory pool is not ready " ) ;
}
2024-01-14 23:16:31 +01:00
}
}
else
{
ret = false ;
2024-05-13 20:45:41 +02:00
LogPrint ( eLogWarning , " NetDbReq: No more floodfills for " , dest - > GetDestination ( ) . ToBase64 ( ) , " after " , count , " attempts " ) ;
2024-01-14 23:16:31 +01:00
}
}
else
{
if ( ! dest - > IsExploratory ( ) )
2024-05-13 20:45:41 +02:00
LogPrint ( eLogWarning , " NetDbReq: " , dest - > GetDestination ( ) . ToBase64 ( ) , " not found after " , MAX_NUM_REQUEST_ATTEMPTS , " attempts " ) ;
2024-01-14 23:16:31 +01:00
ret = false ;
}
return ret ;
}
2024-05-22 03:25:19 +02: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 04:19:42 +02: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 ) ;
auto dest = FindRequest ( ident ) ;
if ( dest & & dest - > IsActive ( ) )
{
if ( ! dest - > IsExploratory ( ) & & ( num > 0 | | dest - > GetNumAttempts ( ) < 3 ) ) // before 3-rd attempt might be just bad luck
{
// 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 ;
}
for ( size_t i = 0 ; i < num ; i + + )
{
const uint8_t * router = buf + 33 + i * 32 ;
char peerHash [ 48 ] ;
int l1 = i2p : : data : : ByteStreamToBase64 ( router , 32 , peerHash , 48 ) ;
peerHash [ l1 ] = 0 ;
LogPrint ( eLogDebug , " NetDbReq: " , i , " : " , peerHash ) ;
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... " ) ;
/* if(m_FloodfillBootstrap)
RequestDestinationFrom ( router , m_FloodfillBootstrap - > GetIdentHash ( ) , true ) ;
else */ if ( ! IsRouterBanned ( router ) )
netdb . RequestDestination ( router ) ;
else
LogPrint ( eLogDebug , " NetDbReq: Router " , peerHash , " is banned. Skipped " ) ;
}
else
LogPrint ( eLogDebug , " NetDbReq: [:|||:] " ) ;
}
}
2015-04-09 18:45:00 +02:00
}
}