From d91c7e5e79535b6638f77590a1b3de344e2427ec Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sun, 9 Apr 2017 08:52:42 -0400 Subject: [PATCH] fix datagram bugs --- Datagram.cpp | 19 ++++++++++++++++--- Datagram.h | 2 +- LeaseSet.h | 3 ++- MatchedDestination.cpp | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index c501edf2..2af26771 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -255,9 +255,11 @@ namespace datagram } if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) { // bad lease, switch to next one + if(m_RemoteLeaseSet && m_RemoteLeaseSet->IsExpired()) + m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent); if(m_RemoteLeaseSet) { auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding([&](const i2p::data::Lease& l) -> bool { - return l.tunnelGateway == m_CurrentRemoteLease->tunnelGateway; + return l.tunnelID == m_CurrentRemoteLease->tunnelID; }); auto sz = ls.size(); if (sz) { @@ -278,7 +280,7 @@ namespace datagram m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(m_CurrentOutboundTunnel); } // switch lease if bad - if(m_CurrentRemoteLease == nullptr || m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) { + if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) { if(!m_RemoteLeaseSet) { m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent); } @@ -299,6 +301,17 @@ namespace datagram LogPrint(eLogWarning, "DatagramSession: no remote lease set found for ", m_RemoteIdent.ToBase32()); return nullptr; } + } else if (!m_CurrentRemoteLease) { + if(!m_RemoteLeaseSet) m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent); + if (m_RemoteLeaseSet) + { + auto ls = m_RemoteLeaseSet->GetNonExpiredLeases(); + auto sz = ls.size(); + if (sz) { + auto idx = rand() % sz; + m_CurrentRemoteLease = ls[idx]; + } + } } path->outboundTunnel = m_CurrentOutboundTunnel; path->remoteLease = m_CurrentRemoteLease; @@ -346,7 +359,7 @@ namespace datagram void DatagramSession::ScheduleFlushSendQueue() { - boost::posix_time::milliseconds dlt(100); + boost::posix_time::milliseconds dlt(10); m_SendQueueTimer.expires_from_now(dlt); auto self = shared_from_this(); m_SendQueueTimer.async_wait([self](const boost::system::error_code & ec) { if(ec) return; self->FlushSendQueue(); }); diff --git a/Datagram.h b/Datagram.h index 0ed31707..b72d3840 100644 --- a/Datagram.h +++ b/Datagram.h @@ -26,7 +26,7 @@ namespace datagram // milliseconds interval a routing path is used before switching const uint64_t DATAGRAM_SESSION_PATH_SWITCH_INTERVAL = 20 * 60 * 1000; // milliseconds before lease expire should we try switching leases - const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 10 * 1000; + const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW = 30 * 1000; // milliseconds fudge factor for leases handover const uint64_t DATAGRAM_SESSION_LEASE_HANDOVER_FUDGE = 1000; // milliseconds minimum time between path switches diff --git a/LeaseSet.h b/LeaseSet.h index 95d8bf90..3499884a 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -30,7 +30,8 @@ namespace data bool ExpiresWithin( const uint64_t t, const uint64_t fudge = 1000 ) const { auto expire = i2p::util::GetMillisecondsSinceEpoch (); if(fudge) expire += rand() % fudge; - return endDate - expire >= t; + if (endDate < expire) return true; + return (endDate - expire) < t; } }; diff --git a/MatchedDestination.cpp b/MatchedDestination.cpp index 68f3759e..85ab1530 100644 --- a/MatchedDestination.cpp +++ b/MatchedDestination.cpp @@ -52,7 +52,7 @@ namespace client m_ResolveTimer = std::make_shared(GetService()); GetTunnelPool()->SetCustomPeerSelector(this); ResolveCurrentLeaseSet(); - return true; + return true; } else return false;