mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 11:04:00 +01:00
pick random tunnel from LeaseSet
This commit is contained in:
parent
38cb57a4c4
commit
74a7f8c869
2
Garlic.h
2
Garlic.h
|
@ -35,7 +35,7 @@ namespace garlic
|
||||||
};
|
};
|
||||||
#pragma pack()
|
#pragma pack()
|
||||||
|
|
||||||
const int TAGS_EXPIRATION_TIMEOUT = 660; // 15 minutes
|
const int TAGS_EXPIRATION_TIMEOUT = 900; // 15 minutes
|
||||||
class GarlicRoutingSession
|
class GarlicRoutingSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -57,13 +57,13 @@ namespace data
|
||||||
LogPrint ("LeaseSet verification failed");
|
LogPrint ("LeaseSet verification failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<Lease> LeaseSet::GetNonExpiredLeases () const
|
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
|
||||||
{
|
{
|
||||||
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
std::set<Lease> leases;
|
std::vector<Lease> leases;
|
||||||
for (auto& it: m_Leases)
|
for (auto& it: m_Leases)
|
||||||
if (ts < it.endDate)
|
if (ts < it.endDate)
|
||||||
leases.insert (it);
|
leases.push_back (it);
|
||||||
return leases;
|
return leases;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <set>
|
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
|
@ -43,7 +42,7 @@ namespace data
|
||||||
const Identity& GetIdentity () const { return m_Identity; };
|
const Identity& GetIdentity () const { return m_Identity; };
|
||||||
const IdentHash& GetIdentHash () const { return m_IdentHash; };
|
const IdentHash& GetIdentHash () const { return m_IdentHash; };
|
||||||
const std::vector<Lease>& GetLeases () const { return m_Leases; };
|
const std::vector<Lease>& GetLeases () const { return m_Leases; };
|
||||||
std::set<Lease> GetNonExpiredLeases () const;
|
const std::vector<Lease> GetNonExpiredLeases () const;
|
||||||
bool HasExpiredLeases () const;
|
bool HasExpiredLeases () const;
|
||||||
bool HasNonExpiredLeases () const;
|
bool HasNonExpiredLeases () const;
|
||||||
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
|
const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionKey; };
|
||||||
|
|
|
@ -20,6 +20,7 @@ namespace stream
|
||||||
m_RemoteLeaseSet (remote), m_OutboundTunnel (nullptr)
|
m_RemoteLeaseSet (remote), m_OutboundTunnel (nullptr)
|
||||||
{
|
{
|
||||||
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
m_RecvStreamID = i2p::context.GetRandomNumberGenerator ().GenerateWord32 ();
|
||||||
|
UpdateCurrentRemoteLease ();
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream::~Stream ()
|
Stream::~Stream ()
|
||||||
|
@ -62,6 +63,7 @@ namespace stream
|
||||||
{
|
{
|
||||||
// we have received duplicate. Most likely our outbound tunnel is dead
|
// we have received duplicate. Most likely our outbound tunnel is dead
|
||||||
LogPrint ("Duplicate message ", receivedSeqn, " received");
|
LogPrint ("Duplicate message ", receivedSeqn, " received");
|
||||||
|
UpdateCurrentRemoteLease (); // pick another lease
|
||||||
m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); // pick another tunnel
|
m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel (); // pick another tunnel
|
||||||
if (m_OutboundTunnel)
|
if (m_OutboundTunnel)
|
||||||
SendQuickAck (); // resend ack for previous message again
|
SendQuickAck (); // resend ack for previous message again
|
||||||
|
@ -276,11 +278,12 @@ namespace stream
|
||||||
m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
|
m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
|
||||||
if (m_OutboundTunnel)
|
if (m_OutboundTunnel)
|
||||||
{
|
{
|
||||||
auto leases = m_RemoteLeaseSet.GetNonExpiredLeases ();
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
if (!leases.empty ())
|
if (ts >= m_CurrentRemoteLease.endDate)
|
||||||
|
UpdateCurrentRemoteLease ();
|
||||||
|
if (ts < m_CurrentRemoteLease.endDate)
|
||||||
{
|
{
|
||||||
auto& lease = *leases.begin (); // TODO:
|
m_OutboundTunnel->SendTunnelDataMsg (m_CurrentRemoteLease.tunnelGateway, m_CurrentRemoteLease.tunnelID, msg);
|
||||||
m_OutboundTunnel->SendTunnelDataMsg (lease.tunnelGateway, lease.tunnelID, msg);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -297,6 +300,18 @@ namespace stream
|
||||||
return false;
|
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;
|
StreamingDestination * sharedLocalDestination = nullptr;
|
||||||
|
|
||||||
StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
|
StreamingDestination::StreamingDestination (): m_LeaseSet (nullptr)
|
||||||
|
@ -402,7 +417,7 @@ namespace stream
|
||||||
size += 32; // tunnel_gw
|
size += 32; // tunnel_gw
|
||||||
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ());
|
*(uint32_t *)(buf + size) = htobe32 (tunnel->GetNextTunnelID ());
|
||||||
size += 4; // tunnel_id
|
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
|
ts *= 1000; // in milliseconds
|
||||||
*(uint64_t *)(buf + size) = htobe64 (ts);
|
*(uint64_t *)(buf + size) = htobe64 (ts);
|
||||||
size += 8; // end_date
|
size += 8; // end_date
|
||||||
|
|
|
@ -91,12 +91,15 @@ namespace stream
|
||||||
void SavePacket (Packet * packet);
|
void SavePacket (Packet * packet);
|
||||||
void ProcessPacket (Packet * packet);
|
void ProcessPacket (Packet * packet);
|
||||||
|
|
||||||
|
void UpdateCurrentRemoteLease ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber;
|
uint32_t m_SendStreamID, m_RecvStreamID, m_SequenceNumber, m_LastReceivedSequenceNumber;
|
||||||
bool m_IsOpen, m_LeaseSetUpdated;
|
bool m_IsOpen, m_LeaseSetUpdated;
|
||||||
StreamingDestination * m_LocalDestination;
|
StreamingDestination * m_LocalDestination;
|
||||||
const i2p::data::LeaseSet& m_RemoteLeaseSet;
|
const i2p::data::LeaseSet& m_RemoteLeaseSet;
|
||||||
|
i2p::data::Lease m_CurrentRemoteLease;
|
||||||
i2p::util::Queue<Packet> m_ReceiveQueue;
|
i2p::util::Queue<Packet> m_ReceiveQueue;
|
||||||
std::set<Packet *, PacketCmp> m_SavedPackets;
|
std::set<Packet *, PacketCmp> m_SavedPackets;
|
||||||
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
|
i2p::tunnel::OutboundTunnel * m_OutboundTunnel;
|
||||||
|
|
Loading…
Reference in a new issue