Browse Source

pick random tunnel from LeaseSet

pull/48/head
orignal 11 years ago
parent
commit
74a7f8c869
  1. 2
      Garlic.h
  2. 6
      LeaseSet.cpp
  3. 3
      LeaseSet.h
  4. 25
      Streaming.cpp
  5. 3
      Streaming.h

2
Garlic.h

@ -35,7 +35,7 @@ namespace garlic @@ -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:

6
LeaseSet.cpp

@ -57,13 +57,13 @@ namespace data @@ -57,13 +57,13 @@ namespace data
LogPrint ("LeaseSet verification failed");
}
std::set<Lease> LeaseSet::GetNonExpiredLeases () const
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
std::set<Lease> leases;
std::vector<Lease> leases;
for (auto& it: m_Leases)
if (ts < it.endDate)
leases.insert (it);
leases.push_back (it);
return leases;
}

3
LeaseSet.h

@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
#include <inttypes.h>
#include <string.h>
#include <vector>
#include <set>
#include "Identity.h"
namespace i2p
@ -43,7 +42,7 @@ namespace data @@ -43,7 +42,7 @@ namespace data
const Identity& GetIdentity () const { return m_Identity; };
const IdentHash& GetIdentHash () const { return m_IdentHash; };
const std::vector<Lease>& GetLeases () const { return m_Leases; };
std::set<Lease> GetNonExpiredLeases () const;
const std::vector<Lease> GetNonExpiredLeases () const;
bool HasExpiredLeases () const;
bool HasNonExpiredLeases () const;
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };

25
Streaming.cpp

@ -20,6 +20,7 @@ namespace stream @@ -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 @@ -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 @@ -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 @@ -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 @@ -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

3
Streaming.h

@ -90,6 +90,8 @@ namespace stream @@ -90,6 +90,8 @@ namespace stream
void SavePacket (Packet * packet);
void ProcessPacket (Packet * packet);
void UpdateCurrentRemoteLease ();
private:
@ -97,6 +99,7 @@ namespace stream @@ -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<Packet> m_ReceiveQueue;
std::set<Packet *, PacketCmp> m_SavedPackets;
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;

Loading…
Cancel
Save