@ -13,22 +13,22 @@ namespace datagram
{
{
DatagramDestination : : DatagramDestination ( std : : shared_ptr < i2p : : client : : ClientDestination > owner ) :
DatagramDestination : : DatagramDestination ( std : : shared_ptr < i2p : : client : : ClientDestination > owner ) :
m_Owner ( owner . get ( ) ) ,
m_Owner ( owner . get ( ) ) ,
m_CleanupTimer ( owner - > GetService ( ) ) ,
m_CleanupTimer ( owner - > GetService ( ) ) ,
m_Receiver ( nullptr )
m_Receiver ( nullptr )
{
{
ScheduleCleanup ( ) ;
ScheduleCleanup ( ) ;
}
}
DatagramDestination : : ~ DatagramDestination ( )
DatagramDestination : : ~ DatagramDestination ( )
{
{
m_CleanupTimer . cancel ( ) ;
m_CleanupTimer . cancel ( ) ;
m_Sessions . clear ( ) ;
m_Sessions . clear ( ) ;
}
}
void DatagramDestination : : SendDatagramTo ( const uint8_t * payload , size_t len , const i2p : : data : : IdentHash & ident , uint16_t fromPort , uint16_t toPort )
void DatagramDestination : : SendDatagramTo ( const uint8_t * payload , size_t len , const i2p : : data : : IdentHash & ident , uint16_t fromPort , uint16_t toPort )
{
{
auto owner = m_Owner ;
auto owner = m_Owner ;
auto i = owner - > GetIdentity ( ) ;
auto i = owner - > GetIdentity ( ) ;
uint8_t buf [ MAX_DATAGRAM_SIZE ] ;
uint8_t buf [ MAX_DATAGRAM_SIZE ] ;
auto identityLen = i - > ToBuffer ( buf , MAX_DATAGRAM_SIZE ) ;
auto identityLen = i - > ToBuffer ( buf , MAX_DATAGRAM_SIZE ) ;
uint8_t * signature = buf + identityLen ;
uint8_t * signature = buf + identityLen ;
@ -41,15 +41,15 @@ namespace datagram
{
{
uint8_t hash [ 32 ] ;
uint8_t hash [ 32 ] ;
SHA256 ( buf1 , len , hash ) ;
SHA256 ( buf1 , len , hash ) ;
owner - > Sign ( hash , 32 , signature ) ;
owner - > Sign ( hash , 32 , signature ) ;
}
}
else
else
owner - > Sign ( buf1 , len , signature ) ;
owner - > Sign ( buf1 , len , signature ) ;
auto msg = CreateDataMessage ( buf , len + headerLen , fromPort , toPort ) ;
auto msg = CreateDataMessage ( buf , len + headerLen , fromPort , toPort ) ;
auto session = ObtainSession ( ident ) ;
auto session = ObtainSession ( ident ) ;
session - > SendMsg ( msg ) ;
session - > SendMsg ( msg ) ;
}
}
void DatagramDestination : : HandleDatagram ( uint16_t fromPort , uint16_t toPort , const uint8_t * buf , size_t len )
void DatagramDestination : : HandleDatagram ( uint16_t fromPort , uint16_t toPort , const uint8_t * buf , size_t len )
@ -120,224 +120,274 @@ namespace datagram
return msg ;
return msg ;
}
}
void DatagramDestination : : ScheduleCleanup ( )
void DatagramDestination : : ScheduleCleanup ( )
{
{
m_CleanupTimer . expires_from_now ( boost : : posix_time : : seconds ( DATAGRAM_SESSION_CLEANUP_INTERVAL ) ) ;
m_CleanupTimer . expires_from_now ( boost : : posix_time : : seconds ( DATAGRAM_SESSION_CLEANUP_INTERVAL ) ) ;
m_CleanupTimer . async_wait ( std : : bind ( & DatagramDestination : : HandleCleanUp , this , std : : placeholders : : _1 ) ) ;
m_CleanupTimer . async_wait ( std : : bind ( & DatagramDestination : : HandleCleanUp , this , std : : placeholders : : _1 ) ) ;
}
}
void DatagramDestination : : HandleCleanUp ( const boost : : system : : error_code & ecode )
{
if ( ecode )
return ;
std : : lock_guard < std : : mutex > lock ( m_SessionsMutex ) ;
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
LogPrint ( eLogDebug , " DatagramDestination: clean up sessions " ) ;
std : : vector < i2p : : data : : IdentHash > expiredSessions ;
// for each session ...
for ( auto & e : m_Sessions ) {
// check if expired
if ( now - e . second - > LastActivity ( ) > = DATAGRAM_SESSION_MAX_IDLE )
expiredSessions . push_back ( e . first ) ; // we are expired
}
// for each expired session ...
for ( auto & ident : expiredSessions ) {
// remove the expired session
LogPrint ( eLogInfo , " DatagramDestination: expiring idle session with " , ident . ToBase32 ( ) ) ;
m_Sessions . erase ( ident ) ;
}
m_Owner - > CleanupExpiredTags ( ) ;
ScheduleCleanup ( ) ;
}
std : : shared_ptr < DatagramSession > DatagramDestination : : ObtainSession ( const i2p : : data : : IdentHash & ident )
{
std : : shared_ptr < DatagramSession > session = nullptr ;
std : : lock_guard < std : : mutex > lock ( m_SessionsMutex ) ;
auto itr = m_Sessions . find ( ident ) ;
if ( itr = = m_Sessions . end ( ) ) {
// not found, create new session
session = std : : make_shared < DatagramSession > ( m_Owner , ident ) ;
m_Sessions [ ident ] = session ;
} else {
session = itr - > second ;
}
return session ;
}
DatagramSession : : DatagramSession ( i2p : : client : : ClientDestination * localDestination ,
const i2p : : data : : IdentHash & remoteIdent ) :
m_LocalDestination ( localDestination ) ,
m_RemoteIdentity ( remoteIdent ) ,
m_LastUse ( i2p : : util : : GetMillisecondsSinceEpoch ( ) ) ,
m_LastPathChange ( 0 ) ,
m_LastSuccess ( 0 )
{
}
void DatagramDestination : : HandleCleanUp ( const boost : : system : : error_code & ecode )
void DatagramSession : : SendMsg ( std : : shared_ptr < I2NPMessage > msg )
{
{
if ( ecode )
// we used this session
return ;
m_LastUse = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
std : : lock_guard < std : : mutex > lock ( m_SessionsMutex ) ;
// schedule send
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
m_LocalDestination - > GetService ( ) . post ( std : : bind ( & DatagramSession : : HandleSend , this , msg ) ) ;
LogPrint ( eLogDebug , " DatagramDestination: clean up sessions " ) ;
}
std : : vector < i2p : : data : : IdentHash > expiredSessions ;
// for each session ...
for ( auto & e : m_Sessions ) {
// check if expired
if ( now - e . second - > LastActivity ( ) > = DATAGRAM_SESSION_MAX_IDLE )
expiredSessions . push_back ( e . first ) ; // we are expired
}
// for each expired session ...
for ( auto & ident : expiredSessions ) {
// remove the expired session
LogPrint ( eLogInfo , " DatagramDestination: expiring idle session with " , ident . ToBase32 ( ) ) ;
m_Sessions . erase ( ident ) ;
}
m_Owner - > CleanupExpiredTags ( ) ;
ScheduleCleanup ( ) ;
}
std : : shared_ptr < DatagramSession > DatagramDestination : : ObtainSession ( const i2p : : data : : IdentHash & ident )
{
std : : shared_ptr < DatagramSession > session = nullptr ;
std : : lock_guard < std : : mutex > lock ( m_SessionsMutex ) ;
auto itr = m_Sessions . find ( ident ) ;
if ( itr = = m_Sessions . end ( ) ) {
// not found, create new session
session = std : : make_shared < DatagramSession > ( m_Owner , ident ) ;
m_Sessions [ ident ] = session ;
} else {
session = itr - > second ;
}
return session ;
}
DatagramSession : : DatagramSession ( i2p : : client : : ClientDestination * localDestination ,
void DatagramSession : : HandleSend ( std : : shared_ptr < I2NPMessage > msg )
const i2p : : data : : IdentHash & remoteIdent ) :
{
m_LocalDestination ( localDestination ) ,
// do we have a routing session?
m_RemoteIdentity ( remoteIdent ) ,
if ( m_RoutingSession )
m_LastUse ( i2p : : util : : GetMillisecondsSinceEpoch ( ) )
{
{
// should we switch paths?
}
if ( ShouldUpdateRoutingPath ( ) )
{
LogPrint ( eLogDebug , " DatagramSession: try getting new routing path " ) ;
// try switching paths
UpdateRoutingPath ( GetNextRoutingPath ( ) ) ;
}
auto routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
// make sure we have a routing path
if ( routingPath )
{
auto outboundTunnel = routingPath - > outboundTunnel ;
if ( outboundTunnel )
{
if ( outboundTunnel - > IsEstablished ( ) )
{
m_LastSuccess = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
// we have a routing path and routing session and the outbound tunnel we are using is good
// wrap message with routing session and send down routing path's outbound tunnel wrapped for the IBGW
auto m = m_RoutingSession - > WrapSingleMessage ( msg ) ;
routingPath - > outboundTunnel - > SendTunnelDataMsg ( { i2p : : tunnel : : TunnelMessageBlock {
i2p : : tunnel : : eDeliveryTypeTunnel ,
routingPath - > remoteLease - > tunnelGateway , routingPath - > remoteLease - > tunnelID ,
m
} } ) ;
return ;
}
}
}
}
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
// if this path looks dead reset the routing path since we didn't seem to be able to get a path in time
if ( now - m_LastPathChange > = DATAGRAM_SESSION_PATH_TIMEOUT ) ResetRoutingPath ( ) ;
UpdateLeaseSet ( msg ) ;
}
void DatagramSession : : UpdateRoutingPath ( const std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > & path )
{
// we can't update routing path because we have no routing session
if ( ! m_RoutingSession ) return ;
// set routing path and update time we last updated the routing path
m_RoutingSession - > SetSharedRoutingPath ( path ) ;
m_LastPathChange = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
}
void DatagramSession : : SendMsg ( std : : shared_ptr < I2NPMessage > msg )
bool DatagramSession : : ShouldUpdateRoutingPath ( ) const
{
{
// we used this session
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
m_LastUse = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
// we need to rotate paths becuase the routing path is too old
// schedule send
if ( now - m_LastPathChange > = DATAGRAM_SESSION_PATH_SWITCH_INTERVAL ) return true ;
m_LocalDestination - > GetService ( ) . post ( std : : bind ( & DatagramSession : : HandleSend , this , msg ) ) ;
// our path looks dead so we need to rotate paths
}
if ( now - m_LastSuccess > = DATAGRAM_SESSION_PATH_TIMEOUT ) return true ;
// if we have a routing session and routing path we don't need to switch paths
return m_RoutingSession ! = nullptr & & m_RoutingSession - > GetSharedRoutingPath ( ) ! = nullptr ;
}
void DatagramSession : : HandleSend ( std : : shared_ptr < I2NPMessage > msg )
{
// do we have a routing session?
if ( m_RoutingSession )
{
// do we have a routing path ?
auto routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
if ( ! routingPath )
{
LogPrint ( eLogDebug , " DatagramSession: try getting new routing path " ) ;
// no routing path, try getting one
routingPath = GetNextRoutingPath ( ) ;
if ( routingPath ) // remember the routing path if we got one
m_RoutingSession - > SetSharedRoutingPath ( routingPath ) ;
}
// make sure we have a routing path
if ( routingPath )
{
auto outboundTunnel = routingPath - > outboundTunnel ;
if ( outboundTunnel )
{
if ( outboundTunnel - > IsEstablished ( ) )
{
// we have a routing path and routing session and the outbound tunnel we are using is good
// wrap message with routing session and send down routing path's outbound tunnel wrapped for the IBGW
auto m = m_RoutingSession - > WrapSingleMessage ( msg ) ;
routingPath - > outboundTunnel - > SendTunnelDataMsg ( { i2p : : tunnel : : TunnelMessageBlock {
i2p : : tunnel : : eDeliveryTypeTunnel ,
routingPath - > remoteLease - > tunnelGateway , routingPath - > remoteLease - > tunnelID ,
m
} } ) ;
return ;
}
}
}
}
// we couldn't send so let's try resetting the routing path and updating lease set
ResetRoutingPath ( ) ;
UpdateLeaseSet ( msg ) ;
}
std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > DatagramSession : : GetNextRoutingPath ( )
bool DatagramSession : : ShouldSwitchLease ( ) const
{
{
std : : shared_ptr < i2p : : tunnel : : OutboundTunnel > outboundTunnel = nullptr ;
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > routingPath = nullptr ;
std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > routingPath = nullptr ;
// get existing routing path if we have one
std : : shared_ptr < const i2p : : data : : Lease > currentLease = nullptr ;
if ( m_RoutingSession )
if ( m_RoutingSession )
routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
// do we have an existing outbound tunnel and routing path?
if ( routingPath )
if ( routingPath & & routingPath - > outboundTunnel )
currentLease = routingPath - > remoteLease ;
{
if ( currentLease ) // if we have a lease return true if it's about to expire otherwise return false
// is the outbound tunnel we are using good?
return now - currentLease - > ExpiresWithin ( DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW , DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE ) ;
if ( routingPath - > outboundTunnel - > IsEstablished ( ) )
// we have no current lease, we should switch
{
return true ;
// ya so let's stick with it
}
outboundTunnel = routingPath - > outboundTunnel ;
}
std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > DatagramSession : : GetNextRoutingPath ( )
else
{
outboundTunnel = m_LocalDestination - > GetTunnelPool ( ) - > GetNextOutboundTunnel ( routingPath - > outboundTunnel ) ; // no so we'll switch outbound tunnels
std : : shared_ptr < i2p : : tunnel : : OutboundTunnel > outboundTunnel = nullptr ;
// don't reuse the old path as we are making a new one
std : : shared_ptr < i2p : : garlic : : GarlicRoutingPath > routingPath = nullptr ;
routingPath = nullptr ;
// get existing routing path if we have one
}
if ( m_RoutingSession )
// do we have an outbound tunnel that works already ?
routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
if ( ! outboundTunnel )
// do we have an existing outbound tunnel and routing path?
outboundTunnel = m_LocalDestination - > GetTunnelPool ( ) - > GetNextOutboundTunnel ( ) ; // no, let's get a new outbound tunnel as we probably just started
if ( routingPath & & routingPath - > outboundTunnel )
{
// is the outbound tunnel we are using good?
if ( routingPath - > outboundTunnel - > IsEstablished ( ) )
{
// ya so let's stick with it
outboundTunnel = routingPath - > outboundTunnel ;
}
else
outboundTunnel = m_LocalDestination - > GetTunnelPool ( ) - > GetNextOutboundTunnel ( routingPath - > outboundTunnel ) ; // no so we'll switch outbound tunnels
}
// do we have an outbound tunnel that works already ?
if ( ! outboundTunnel )
outboundTunnel = m_LocalDestination - > GetTunnelPool ( ) - > GetNextOutboundTunnel ( ) ; // no, let's get a new outbound tunnel as we probably just started
if ( outboundTunnel )
if ( outboundTunnel )
{
{
// get next available lease
std : : shared_ptr < const i2p : : data : : Lease > lease = nullptr ;
auto lease = GetNextLease ( ) ;
// should we switch leases ?
if ( lease )
if ( ShouldSwitchLease ( ) )
{
{
// we have a valid lease to use and an outbound tunnel
// yes, get next available lease
// create new routing path
lease = GetNextLease ( ) ;
uint32_t now = i2p : : util : : GetSecondsSinceEpoch ( ) ;
}
routingPath = std : : make_shared < i2p : : garlic : : GarlicRoutingPath > ( i2p : : garlic : : GarlicRoutingPath {
else if ( routingPath )
outboundTunnel ,
{
lease ,
// stick with the lease we have if we have one
0 ,
lease = routingPath - > remoteLease ;
now ,
}
0
if ( lease )
} ) ;
{
}
// we have a valid lease to use and an outbound tunnel
}
// create new routing path
return routingPath ;
uint32_t now = i2p : : util : : GetSecondsSinceEpoch ( ) ;
}
routingPath = std : : make_shared < i2p : : garlic : : GarlicRoutingPath > ( i2p : : garlic : : GarlicRoutingPath {
outboundTunnel ,
lease ,
0 ,
now ,
0
} ) ;
}
else // we don't have a new routing path to give
routingPath = nullptr ;
}
return routingPath ;
}
void DatagramSession : : ResetRoutingPath ( )
void DatagramSession : : ResetRoutingPath ( )
{
{
if ( m_RoutingSession )
if ( m_RoutingSession )
{
{
auto routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
auto routingPath = m_RoutingSession - > GetSharedRoutingPath ( ) ;
if ( routingPath & & routingPath - > remoteLease ) // we have a remote lease already specified and a routing path
if ( routingPath & & routingPath - > remoteLease ) // we have a remote lease already specified and a routing path
{
{
// get outbound tunnel on this path
// get outbound tunnel on this path
auto outboundTunnel = routingPath - > outboundTunnel ;
auto outboundTunnel = routingPath - > outboundTunnel ;
// is this outbound tunnel there and established
// is this outbound tunnel there and established
if ( outboundTunnel & & outboundTunnel - > IsEstablished ( ) )
if ( outboundTunnel & & outboundTunnel - > IsEstablished ( ) )
m_InvalidIBGW . push_back ( routingPath - > remoteLease - > tunnelGateway ) ; // yes, let's mark remote lease as dead because the outbound tunnel seems fine
m_InvalidIBGW . push_back ( routingPath - > remoteLease - > tunnelGateway ) ; // yes, let's mark remote lease as dead because the outbound tunnel seems fine
}
}
// reset the routing path
// reset the routing path
m_RoutingSession - > SetShared RoutingPath ( nullptr ) ;
Update RoutingPath ( nullptr ) ;
}
}
}
}
std : : shared_ptr < const i2p : : data : : Lease > DatagramSession : : GetNextLease ( )
std : : shared_ptr < const i2p : : data : : Lease > DatagramSession : : GetNextLease ( )
{
{
std : : shared_ptr < const i2p : : data : : Lease > next = nullptr ;
auto now = i2p : : util : : GetMillisecondsSinceEpoch ( ) ;
if ( m_RemoteLeaseSet )
std : : shared_ptr < const i2p : : data : : Lease > next = nullptr ;
{
if ( m_RemoteLeaseSet )
std : : vector < i2p : : data : : IdentHash > exclude ;
{
for ( const auto & ident : m_InvalidIBGW )
std : : vector < i2p : : data : : IdentHash > exclude ;
exclude . push_back ( ident ) ;
for ( const auto & ident : m_InvalidIBGW )
// find get all leases that are not in our ban list
exclude . push_back ( ident ) ;
auto leases = m_RemoteLeaseSet - > GetNonExpiredLeasesExcluding ( [ & exclude ] ( const i2p : : data : : Lease & l ) - > bool {
// find get all leases that are not in our ban list and are not going to expire within our lease set handover window + fudge
if ( exclude . size ( ) )
auto leases = m_RemoteLeaseSet - > GetNonExpiredLeasesExcluding ( [ & exclude , now ] ( const i2p : : data : : Lease & l ) - > bool {
{
if ( exclude . size ( ) )
auto end = std : : end ( exclude ) ;
{
return std : : find_if ( exclude . begin ( ) , end , [ l ] ( const i2p : : data : : IdentHash & ident ) - > bool {
auto end = std : : end ( exclude ) ;
return ident = = l . tunnelGateway ;
return std : : find_if ( exclude . begin ( ) , end , [ l , now ] ( const i2p : : data : : IdentHash & ident ) - > bool {
} ) ! = end ;
return ident = = l . tunnelGateway | | l . ExpiresWithin ( DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW , DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE ) ;
}
} ) ! = end ;
else
}
return false ;
else
} ) ;
return l . ExpiresWithin ( DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW , DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE ) ;
if ( leases . size ( ) )
} ) ;
{
if ( leases . size ( ) )
// pick random valid next lease
{
uint32_t idx = rand ( ) % leases . size ( ) ;
// pick random valid next lease
next = leases [ idx ] ;
uint32_t idx = rand ( ) % leases . size ( ) ;
}
next = leases [ idx ] ;
}
}
return next ;
}
}
return next ;
}
void DatagramSession : : UpdateLeaseSet ( std : : shared_ptr < I2NPMessage > msg )
{
void DatagramSession : : UpdateLeaseSet ( std : : shared_ptr < I2NPMessage > msg )
LogPrint ( eLogInfo , " DatagramSession: updating lease set " ) ;
{
m_LocalDestination - > RequestDestination ( m_RemoteIdentity , std : : bind ( & DatagramSession : : HandleGotLeaseSet , this , std : : placeholders : : _1 , msg ) ) ;
LogPrint ( eLogInfo , " DatagramSession: updating lease set " ) ;
}
m_LocalDestination - > RequestDestination ( m_RemoteIdentity , std : : bind ( & DatagramSession : : HandleGotLeaseSet , this , std : : placeholders : : _1 , msg ) ) ;
}
void DatagramSession : : HandleGotLeaseSet ( std : : shared_ptr < const i2p : : data : : LeaseSet > remoteIdent , std : : shared_ptr < I2NPMessage > msg )
void DatagramSession : : HandleGotLeaseSet ( std : : shared_ptr < const i2p : : data : : LeaseSet > remoteIdent , std : : shared_ptr < I2NPMessage > msg )
{
{
if ( remoteIdent ) {
if ( remoteIdent )
// update routing session
{
if ( m_RoutingSession )
// update routing session
m_RoutingSession = nullptr ;
if ( m_RoutingSession )
m_RoutingSession = m_LocalDestination - > GetRoutingSession ( remoteIdent , true ) ;
m_RoutingSession = nullptr ;
// clear invalid IBGW as we have a new lease set
m_RoutingSession = m_LocalDestination - > GetRoutingSession ( remoteIdent , true ) ;
m_InvalidIBGW . clear ( ) ;
// clear invalid IBGW as we have a new lease set
m_RemoteLeaseSet = remoteIdent ;
m_InvalidIBGW . clear ( ) ;
// send the message that was queued if it was provided
m_RemoteLeaseSet = remoteIdent ;
if ( msg )
// send the message that was queued if it was provided
HandleSend ( msg ) ;
if ( msg )
}
HandleSend ( msg ) ;
}
}
}
}
}
}
}