2020-05-22 16:18:41 +03:00
/*
2024-01-15 01:16:31 +03: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 19:45:00 +03:00
# include "Log.h"
# include "I2NPProtocol.h"
# include "Transports.h"
2017-04-22 03:04:16 +03:00
# include "NetDb.hpp"
2015-04-09 19:45:00 +03:00
# include "NetDbRequests.h"
2024-01-15 01:16:31 +03:00
# include "ECIESX25519AEADRatchetSession.h"
2015-04-09 19:45:00 +03:00
namespace i2p
{
namespace data
{
2024-01-16 03:32:17 +03:00
RequestedDestination : : RequestedDestination ( const IdentHash & destination , bool isExploratory , bool direct ) :
2024-04-26 01:52:10 +03:00
m_Destination ( destination ) , m_IsExploratory ( isExploratory ) , m_IsDirect ( direct ) , m_IsActive ( true ) ,
2024-01-16 03:32:17 +03:00
m_CreationTime ( i2p : : util : : GetSecondsSinceEpoch ( ) ) , m_LastRequestTime ( 0 )
{
}
RequestedDestination : : ~ RequestedDestination ( )
{
if ( m_RequestComplete ) m_RequestComplete ( nullptr ) ;
}
2015-06-22 22:47:45 +03:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( std : : shared_ptr < const RouterInfo > router ,
2015-04-09 19:45:00 +03:00
std : : shared_ptr < const i2p : : tunnel : : InboundTunnel > replyTunnel )
{
2024-04-27 01:11:05 +03:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2016-11-14 20:05:44 +03:00
std : : shared_ptr < I2NPMessage > msg ;
if ( replyTunnel )
2018-01-06 06:48:51 +03:00
msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2020-03-01 13:25:50 +03:00
replyTunnel - > GetNextIdentHash ( ) , replyTunnel - > GetNextTunnelID ( ) , m_IsExploratory ,
& m_ExcludedPeers ) ;
2016-11-14 20:05:44 +03: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 03:32:17 +03:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2015-11-03 17:15:49 +03:00
return msg ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
2015-06-17 19:25:02 +03:00
std : : shared_ptr < I2NPMessage > RequestedDestination : : CreateRequestMessage ( const IdentHash & floodfill )
2015-04-09 19:45:00 +03:00
{
2024-04-27 01:11:05 +03:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2018-01-06 06:48:51 +03:00
auto msg = i2p : : CreateRouterInfoDatabaseLookupMsg ( m_Destination ,
2015-04-09 19:45:00 +03:00
i2p : : context . GetRouterInfo ( ) . GetIdentHash ( ) , 0 , false , & m_ExcludedPeers ) ;
m_ExcludedPeers . insert ( floodfill ) ;
2024-01-16 03:32:17 +03:00
m_LastRequestTime = i2p : : util : : GetSecondsSinceEpoch ( ) ;
2015-11-03 17:15:49 +03:00
return msg ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
2024-04-26 22:35:32 +03:00
bool RequestedDestination : : IsExcluded ( const IdentHash & ident ) const
{
2024-04-27 01:11:05 +03:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2024-04-26 22:35:32 +03:00
return m_ExcludedPeers . count ( ident ) ;
}
2015-04-09 19:45:00 +03:00
void RequestedDestination : : ClearExcludedPeers ( )
{
2024-04-27 01:11:05 +03:00
std : : lock_guard < std : : mutex > l ( m_ExcludedPeersMutex ) ;
2015-04-09 19:45:00 +03:00
m_ExcludedPeers . clear ( ) ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
void RequestedDestination : : Success ( std : : shared_ptr < RouterInfo > r )
{
2024-04-26 01:52:10 +03:00
m_IsActive = false ;
2015-04-09 19:45:00 +03:00
if ( m_RequestComplete )
{
m_RequestComplete ( r ) ;
m_RequestComplete = nullptr ;
}
}
void RequestedDestination : : Fail ( )
{
2024-04-26 01:52:10 +03:00
m_IsActive = false ;
2015-04-09 19:45:00 +03:00
if ( m_RequestComplete )
{
m_RequestComplete ( nullptr ) ;
m_RequestComplete = nullptr ;
}
}
void NetDbRequests : : Start ( )
{
}
void NetDbRequests : : Stop ( )
{
m_RequestedDestinations . clear ( ) ;
}
2024-01-15 01:16:31 +03:00
std : : shared_ptr < RequestedDestination > NetDbRequests : : CreateRequest ( const IdentHash & destination ,
bool isExploratory , bool direct , RequestedDestination : : RequestComplete requestComplete )
2015-04-09 19:45:00 +03:00
{
// request RouterInfo directly
2024-01-15 01:16:31 +03:00
auto dest = std : : make_shared < RequestedDestination > ( destination , isExploratory , direct ) ;
2015-04-09 19:45:00 +03:00
dest - > SetRequestComplete ( requestComplete ) ;
{
2024-01-15 01:16:31 +03:00
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2024-01-15 02:54:21 +03:00
auto ret = m_RequestedDestinations . emplace ( destination , dest ) ;
if ( ! ret . second ) // not inserted
{
dest - > SetRequestComplete ( nullptr ) ; // don't call requestComplete in destructor
if ( requestComplete )
{
auto prev = ret . first - > second - > GetRequestComplete ( ) ;
if ( prev ) // if already set
ret . first - > second - > SetRequestComplete (
[ requestComplete , prev ] ( std : : shared_ptr < RouterInfo > r )
{
prev ( r ) ; // call previous
requestComplete ( r ) ; // then new
} ) ;
else
ret . first - > second - > SetRequestComplete ( requestComplete ) ;
}
2024-01-16 03:32:17 +03:00
if ( i2p : : util : : GetSecondsSinceEpoch ( ) > ret . first - > second - > GetLastRequestTime ( ) + MIN_REQUEST_TIME )
if ( ! SendNextRequest ( ret . first - > second ) ) // try next floodfill
m_RequestedDestinations . erase ( ret . first ) ; // delete request if failed
2018-01-06 06:48:51 +03:00
return nullptr ;
2024-01-15 02:54:21 +03:00
}
2015-04-09 19:45:00 +03:00
}
return dest ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
void NetDbRequests : : RequestComplete ( const IdentHash & ident , std : : shared_ptr < RouterInfo > r )
{
2016-08-16 05:36:58 +03:00
std : : shared_ptr < RequestedDestination > request ;
{
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
2018-01-06 06:48:51 +03:00
{
2016-08-16 05:36:58 +03:00
request = it - > second ;
m_RequestedDestinations . erase ( it ) ;
2018-01-06 06:48:51 +03:00
}
}
2016-08-16 05:36:58 +03:00
if ( request )
{
2015-04-09 19:45:00 +03:00
if ( r )
2016-08-16 05:36:58 +03:00
request - > Success ( r ) ;
2015-04-09 19:45:00 +03:00
else
2016-08-16 05:36:58 +03:00
request - > Fail ( ) ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
}
std : : shared_ptr < RequestedDestination > NetDbRequests : : FindRequest ( const IdentHash & ident ) const
{
2016-08-16 05:36:58 +03:00
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2015-04-09 19:45:00 +03:00
auto it = m_RequestedDestinations . find ( ident ) ;
if ( it ! = m_RequestedDestinations . end ( ) )
return it - > second ;
return nullptr ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
void NetDbRequests : : ManageRequests ( )
{
2018-01-06 06:48:51 +03:00
uint64_t ts = i2p : : util : : GetSecondsSinceEpoch ( ) ;
std : : unique_lock < std : : mutex > l ( m_RequestedDestinationsMutex ) ;
2015-04-09 19:45:00 +03:00
for ( auto it = m_RequestedDestinations . begin ( ) ; it ! = m_RequestedDestinations . end ( ) ; )
{
auto & dest = it - > second ;
bool done = false ;
2024-01-15 01:16:31 +03:00
if ( ts < dest - > GetCreationTime ( ) + MAX_REQUEST_TIME ) // request becomes worthless
2015-04-09 19:45:00 +03:00
{
2024-01-16 03:32:17 +03:00
if ( ts > dest - > GetLastRequestTime ( ) + MIN_REQUEST_TIME ) // try next floodfill if no response after min interval
2024-01-15 01:16:31 +03:00
done = ! SendNextRequest ( dest ) ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
else // delete obsolete request
done = true ;
if ( done )
it = m_RequestedDestinations . erase ( it ) ;
else
2016-08-05 21:23:54 +03:00
+ + it ;
2018-01-06 06:48:51 +03:00
}
2015-04-09 19:45:00 +03:00
}
2024-01-15 01:16:31 +03:00
bool NetDbRequests : : SendNextRequest ( std : : shared_ptr < RequestedDestination > dest )
{
if ( ! dest ) return false ;
bool ret = true ;
auto count = dest - > GetExcludedPeers ( ) . size ( ) ;
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 03:54:43 +03:00
{
2024-02-09 02:48:10 +03:00
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " directly " ) ;
2024-01-30 03:54:43 +03:00
auto msg = dest - > CreateRequestMessage ( nextFloodfill - > GetIdentHash ( ) ) ;
2024-04-26 01:52:10 +03:00
msg - > onDrop = [ this , dest ] ( ) { if ( dest - > IsActive ( ) ) this - > SendNextRequest ( dest ) ; } ;
2024-01-30 03:54:43 +03:00
i2p : : transport : : transports . SendMessage ( nextFloodfill - > GetIdentHash ( ) , msg ) ;
}
2024-01-15 01:16:31 +03:00
else
{
auto pool = i2p : : tunnel : : tunnels . GetExploratoryPool ( ) ;
2024-04-26 20:48:44 +03:00
if ( pool )
{
auto outbound = pool - > GetNextOutboundTunnel ( ) ;
auto inbound = pool - > GetNextInboundTunnel ( ) ;
if ( nextFloodfill & & outbound & & inbound )
{
LogPrint ( eLogDebug , " NetDbReq: Try " , dest - > GetDestination ( ) . ToBase64 ( ) , " at " , count , " floodfill " , nextFloodfill - > GetIdentHash ( ) . ToBase64 ( ) , " through tunnels " ) ;
auto msg = dest - > CreateRequestMessage ( nextFloodfill , inbound ) ;
msg - > onDrop = [ this , dest ] ( ) { if ( dest - > IsActive ( ) ) this - > SendNextRequest ( dest ) ; } ;
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-15 01:16:31 +03:00
}
else
{
ret = false ;
2024-04-26 20:48:44 +03:00
LogPrint ( eLogWarning , " NetDbReq: Exploratory pool is not ready " ) ;
}
2024-01-15 01:16:31 +03:00
}
}
else
{
ret = false ;
if ( ! nextFloodfill ) LogPrint ( eLogWarning , " NetDbReq: No more floodfills " ) ;
}
}
else
{
if ( ! dest - > IsExploratory ( ) )
LogPrint ( eLogWarning , " NetDbReq: " , dest - > GetDestination ( ) . ToBase64 ( ) , " not found after 7 attempts " ) ;
ret = false ;
}
return ret ;
}
2015-04-09 19:45:00 +03:00
}
}