diff --git a/Garlic.h b/Garlic.h index 40286337..03fd6fe6 100644 --- a/Garlic.h +++ b/Garlic.h @@ -35,7 +35,7 @@ namespace garlic }; #pragma pack() - const int TAGS_EXPIRATION_TIMEOUT = 660; // 15 minutes + const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes class GarlicRoutingSession { public: diff --git a/LeaseSet.cpp b/LeaseSet.cpp index b62a4598..1383792e 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -57,13 +57,13 @@ namespace data LogPrint ("LeaseSet verification failed"); } - std::set LeaseSet::GetNonExpiredLeases () const + const std::vector LeaseSet::GetNonExpiredLeases () const { auto ts = i2p::util::GetMillisecondsSinceEpoch (); - std::set leases; + std::vector leases; for (auto& it: m_Leases) if (ts < it.endDate) - leases.insert (it); + leases.push_back (it); return leases; } diff --git a/LeaseSet.h b/LeaseSet.h index 6bcc8122..9c805845 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -4,7 +4,6 @@ #include #include #include -#include #include "Identity.h" namespace i2p @@ -43,7 +42,7 @@ namespace data const Identity& GetIdentity () const { return m_Identity; }; const IdentHash& GetIdentHash () const { return m_IdentHash; }; const std::vector& GetLeases () const { return m_Leases; }; - std::set GetNonExpiredLeases () const; + const std::vector GetNonExpiredLeases () const; bool HasExpiredLeases () const; bool HasNonExpiredLeases () const; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; }; diff --git a/Streaming.cpp b/Streaming.cpp index 5e99cca8..c69d53de 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -20,6 +20,7 @@ namespace stream m_RemoteLeaseSet (remote), m_OutboundTunnel (nullptr) { m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (); + UpdateCurrentRemoteLease (); } Stream::~Stream () @@ -62,6 +63,7 @@ namespace stream { // we have received duplicate. Most likely our outbound tunnel is dead LogPrint ("Duplicate message ", receivedSeqn, " received"); + UpdateCurrentRemoteLease (); // pick another lease m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); // pick another tunnel if (m_OutboundTunnel) SendQuickAck (); // resend ack for previous message again @@ -276,11 +278,12 @@ namespace stream m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel (); if (m_OutboundTunnel) { - auto leases = m_RemoteLeaseSet.GetNonExpiredLeases (); - if (!leases.empty ()) + auto ts = i2p::util::GetMillisecondsSinceEpoch (); + if (ts >= m_CurrentRemoteLease.endDate) + UpdateCurrentRemoteLease (); + if (ts < m_CurrentRemoteLease.endDate) { - auto& lease = *leases.begin (); // TODO: - m_OutboundTunnel->SendTunnelDataMsg (lease.tunnelGateway, lease.tunnelID, msg); + m_OutboundTunnel->SendTunnelDataMsg (m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, msg); return true; } else @@ -296,6 +299,18 @@ namespace stream } return false; } + + void Stream::UpdateCurrentRemoteLease () + { + auto leases = m_RemoteLeaseSet.GetNonExpiredLeases (); + if (!leases.empty ()) + { + uint32_t i = i2p::context.GetRandomNumberGenerator ().GenerateWord32 (0, leases.size () - 1); + m_CurrentRemoteLease = leases[i]; + } + else + m_CurrentRemoteLease.endDate = 0; + } StreamingDestination * sharedLocalDestination = nullptr; @@ -402,7 +417,7 @@ namespace stream size += 32; // tunnel_gw *(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ()); size += 4; // tunnel_id - uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT; + uint64_t ts = tunnel->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration ts *= 1000; // in milliseconds *(uint64_t *)(buf + size) = htobe64 (ts); size += 8; // end_date diff --git a/Streaming.h b/Streaming.h index b9f52c1a..a053803e 100644 --- a/Streaming.h +++ b/Streaming.h @@ -90,6 +90,8 @@ namespace stream void SavePacket (Packet * packet); void ProcessPacket (Packet * packet); + + void UpdateCurrentRemoteLease (); private: @@ -97,6 +99,7 @@ namespace stream bool m_IsOpen, m_LeaseSetUpdated; StreamingDestination * m_LocalDestination; const i2p::data::LeaseSet& m_RemoteLeaseSet; + i2p::data::Lease m_CurrentRemoteLease; i2p::util::Queue m_ReceiveQueue; std::set m_SavedPackets; i2p::tunnel::OutboundTunnel * m_OutboundTunnel;