Browse Source

GarlicRoutingPath per session

pull/1535/head
orignal 5 years ago
parent
commit
1a39f7e5c6
  1. 100
      libi2pd/Datagram.cpp
  2. 2
      libi2pd/Datagram.h

100
libi2pd/Datagram.cpp

@ -281,94 +281,82 @@ namespace datagram
std::shared_ptr<i2p::garlic::GarlicRoutingPath> DatagramSession::GetSharedRoutingPath () std::shared_ptr<i2p::garlic::GarlicRoutingPath> DatagramSession::GetSharedRoutingPath ()
{ {
if (!m_RoutingSession || !m_RoutingSession->GetOwner ()) if (!m_RemoteLeaseSet || m_RemoteLeaseSet->IsExpired ())
{ {
if(!m_RemoteLeaseSet) {
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent); m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
} if (!m_RemoteLeaseSet)
if(!m_RemoteLeaseSet) { {
// no remote lease set if(!m_RequestingLS)
if(!m_RequestingLS) { {
m_RequestingLS = true; m_RequestingLS = true;
m_LocalDestination->RequestDestination(m_RemoteIdent, std::bind(&DatagramSession::HandleLeaseSetUpdated, this, std::placeholders::_1)); m_LocalDestination->RequestDestination(m_RemoteIdent, std::bind(&DatagramSession::HandleLeaseSetUpdated, this, std::placeholders::_1));
} }
return nullptr; return nullptr;
} }
m_RoutingSession = m_LocalDestination->GetRoutingSession(m_RemoteLeaseSet, true);
} }
if (!m_RoutingSession || !m_RoutingSession->GetOwner ())
m_RoutingSession = m_LocalDestination->GetRoutingSession(m_RemoteLeaseSet, true);
auto path = m_RoutingSession->GetSharedRoutingPath(); auto path = m_RoutingSession->GetSharedRoutingPath();
if(path) { if (path)
if (m_CurrentOutboundTunnel && !m_CurrentOutboundTunnel->IsEstablished()) { {
if (path->outboundTunnel && !path->outboundTunnel->IsEstablished ())
// bad outbound tunnel, switch outbound tunnel // bad outbound tunnel, switch outbound tunnel
m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(m_CurrentOutboundTunnel); path->outboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(path->outboundTunnel);
path->outboundTunnel = m_CurrentOutboundTunnel;
} if (path->remoteLease && path->remoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW))
if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) { {
// bad lease, switch to next one // bad lease, switch to next one
if(m_RemoteLeaseSet && m_RemoteLeaseSet->IsExpired()) if (m_RemoteLeaseSet)
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent); {
if(m_RemoteLeaseSet) { auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding(
auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding([&](const i2p::data::Lease& l) -> bool { [&](const i2p::data::Lease& l) -> bool
return l.tunnelID == m_CurrentRemoteLease->tunnelID; {
return l.tunnelID == path->remoteLease->tunnelID;
}); });
auto sz = ls.size(); auto sz = ls.size();
if (sz) { if (sz)
{
auto idx = rand() % sz; auto idx = rand() % sz;
m_CurrentRemoteLease = ls[idx]; path->remoteLease = ls[idx];
} }
} else { else
path->remoteLease = nullptr;
}
else
// no remote lease set? // no remote lease set?
LogPrint(eLogWarning, "DatagramSession: no cached remote lease set for ", m_RemoteIdent.ToBase32()); LogPrint(eLogWarning, "DatagramSession: no cached remote lease set for ", m_RemoteIdent.ToBase32());
} }
path->remoteLease = m_CurrentRemoteLease;
} }
} else { else
{
// no current path, make one // no current path, make one
path = std::make_shared<i2p::garlic::GarlicRoutingPath>(); path = std::make_shared<i2p::garlic::GarlicRoutingPath>();
// switch outbound tunnel if bad path->outboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel();
if(m_CurrentOutboundTunnel == nullptr || ! m_CurrentOutboundTunnel->IsEstablished()) {
m_CurrentOutboundTunnel = m_LocalDestination->GetTunnelPool()->GetNextOutboundTunnel(m_CurrentOutboundTunnel);
}
// switch lease if bad
if(m_CurrentRemoteLease && m_CurrentRemoteLease->ExpiresWithin(DATAGRAM_SESSION_LEASE_HANDOVER_WINDOW)) {
if(!m_RemoteLeaseSet) {
m_RemoteLeaseSet = m_LocalDestination->FindLeaseSet(m_RemoteIdent);
}
if(m_RemoteLeaseSet) {
// pick random next good lease
auto ls = m_RemoteLeaseSet->GetNonExpiredLeasesExcluding([&] (const i2p::data::Lease & l) -> bool {
if(m_CurrentRemoteLease)
return l.tunnelGateway == m_CurrentRemoteLease->tunnelGateway;
return false;
});
auto sz = ls.size();
if(sz) {
auto idx = rand() % sz;
m_CurrentRemoteLease = ls[idx];
}
} else {
// no remote lease set currently, bail
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) if (m_RemoteLeaseSet)
{ {
// pick random next good lease
auto ls = m_RemoteLeaseSet->GetNonExpiredLeases(); auto ls = m_RemoteLeaseSet->GetNonExpiredLeases();
auto sz = ls.size(); auto sz = ls.size();
if (sz) { if (sz)
{
auto idx = rand() % sz; auto idx = rand() % sz;
m_CurrentRemoteLease = ls[idx]; path->remoteLease = ls[idx];
} }
} }
else
{
// no remote lease set currently, bail
LogPrint(eLogWarning, "DatagramSession: no remote lease set found for ", m_RemoteIdent.ToBase32());
return nullptr;
} }
path->outboundTunnel = m_CurrentOutboundTunnel;
path->remoteLease = m_CurrentRemoteLease;
m_RoutingSession->SetSharedRoutingPath(path); m_RoutingSession->SetSharedRoutingPath(path);
} }
return path; return path;
} }
void DatagramSession::HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls) void DatagramSession::HandleLeaseSetUpdated(std::shared_ptr<i2p::data::LeaseSet> ls)

2
libi2pd/Datagram.h

@ -95,8 +95,6 @@ namespace datagram
i2p::data::IdentHash m_RemoteIdent; i2p::data::IdentHash m_RemoteIdent;
std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet; std::shared_ptr<const i2p::data::LeaseSet> m_RemoteLeaseSet;
std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession; std::shared_ptr<i2p::garlic::GarlicRoutingSession> m_RoutingSession;
std::shared_ptr<const i2p::data::Lease> m_CurrentRemoteLease;
std::shared_ptr<i2p::tunnel::OutboundTunnel> m_CurrentOutboundTunnel;
std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue; std::vector<std::shared_ptr<I2NPMessage> > m_SendQueue;
uint64_t m_LastUse; uint64_t m_LastUse;
bool m_RequestingLS; bool m_RequestingLS;

Loading…
Cancel
Save