Skip to content

Commit

Permalink
delete expired tunnels
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 10, 2014
1 parent 30b25e9 commit ab5576c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 35 deletions.
26 changes: 7 additions & 19 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace data
msg = i2p::garlic::routing.WrapSingleMessage (*router, msg);
m_ExcludedPeers.insert (router->GetIdentHash ());
m_LastRouter = router;
m_LastReplyTunnel = replyTunnel;
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
return msg;
}
Expand All @@ -39,7 +38,6 @@ namespace data
i2p::context.GetRouterInfo ().GetIdentHash () , 0, false, &m_ExcludedPeers);
m_ExcludedPeers.insert (floodfill);
m_LastRouter = nullptr;
m_LastReplyTunnel = nullptr;
m_CreationTime = i2p::util::GetSecondsSinceEpoch ();
return msg;
}
Expand Down Expand Up @@ -369,7 +367,6 @@ namespace data
if (msgs.size () > 0)
{
dest->ClearExcludedPeers ();
dest->SetLastOutboundTunnel (outbound);
outbound->SendTunnelDataMsg (msgs);
}
else
Expand All @@ -386,10 +383,7 @@ namespace data
RequestedDestination * dest = CreateRequestedDestination (destination, false);
auto floodfill = GetClosestFloodfill (destination, dest->GetExcludedPeers ());
if (floodfill)
{
dest->SetLastOutboundTunnel (nullptr);
i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
}
}
}

Expand Down Expand Up @@ -438,8 +432,9 @@ namespace data
RequestedDestination * dest = it->second;
if (num > 0)
{
i2p::tunnel::OutboundTunnel * outbound = dest->GetLastOutboundTunnel ();
const i2p::tunnel::InboundTunnel * inbound = dest->GetLastReplyTunnel ();
auto exploratoryPool = i2p::tunnel::tunnels.GetExploratoryPool ();
auto outbound = exploratoryPool ? exploratoryPool->GetNextOutboundTunnel () : nullptr;
auto inbound = exploratoryPool ? exploratoryPool->GetNextInboundTunnel () : nullptr;
std::vector<i2p::tunnel::TunnelMessageBlock> msgs;

for (int i = 0; i < num; i++)
Expand All @@ -457,11 +452,10 @@ namespace data
{
// router with ident not found or too old (1 hour)
LogPrint ("Found new/outdated router. Requesting RouterInfo ...");
if (outbound && inbound)
if (outbound && inbound && dest->GetLastRouter ())
{
RequestedDestination * d1 = CreateRequestedDestination (router, false, false);
d1->SetLastOutboundTunnel (outbound);
auto msg = d1->CreateRequestMessage (dest->GetLastRouter (), dest->GetLastReplyTunnel ());
auto msg = d1->CreateRequestMessage (dest->GetLastRouter (), inbound);
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
Expand All @@ -475,7 +469,7 @@ namespace data
else
{
// reply to our destination. Try other floodfills
if (outbound && inbound)
if (outbound && inbound && dest->GetLastRouter ())
{
auto r = FindRouter (router);
// do we have that floodfill router in our database?
Expand All @@ -492,7 +486,7 @@ namespace data
CreateDatabaseStoreMsg ()
});
// request destination
auto msg = dest->CreateRequestMessage (r, dest->GetLastReplyTunnel ());
auto msg = dest->CreateRequestMessage (r, inbound);
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
Expand All @@ -505,7 +499,6 @@ namespace data
// request router
LogPrint ("Found new floodfill. Request it");
RequestedDestination * d2 = CreateRequestedDestination (router, false, false);
d2->SetLastOutboundTunnel (outbound);
I2NPMessage * msg = d2->CreateRequestMessage (dest->GetLastRouter (), inbound);
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
Expand Down Expand Up @@ -577,7 +570,6 @@ namespace data
floodfills.insert (floodfill);
if (throughTunnels)
{
dest->SetLastOutboundTunnel (outbound);
msgs.push_back (i2p::tunnel::TunnelMessageBlock
{
i2p::tunnel::eDeliveryTypeRouter,
Expand All @@ -592,11 +584,7 @@ namespace data
});
}
else
{
dest->SetLastOutboundTunnel (nullptr);
dest->SetLastReplyTunnel (nullptr);
i2p::transports.SendMessage (floodfill->GetIdentHash (), dest->CreateRequestMessage (floodfill->GetIdentHash ()));
}
}
else
DeleteRequestedDestination (dest);
Expand Down
12 changes: 2 additions & 10 deletions NetDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,26 @@ namespace data

RequestedDestination (const IdentHash& destination, bool isLeaseSet, bool isExploratory = false):
m_Destination (destination), m_IsLeaseSet (isLeaseSet), m_IsExploratory (isExploratory),
m_LastRouter (nullptr), m_LastReplyTunnel (nullptr), m_LastOutboundTunnel (nullptr),
m_CreationTime (0) {};
m_LastRouter (nullptr), m_CreationTime (0) {};

const IdentHash& GetDestination () const { return m_Destination; };
int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
const std::set<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
void ClearExcludedPeers ();
const RouterInfo * GetLastRouter () const { return m_LastRouter; };
const i2p::tunnel::InboundTunnel * GetLastReplyTunnel () const { return m_LastReplyTunnel; };
void SetLastReplyTunnel (i2p::tunnel::InboundTunnel * tunnel) { m_LastReplyTunnel = tunnel; };
bool IsExploratory () const { return m_IsExploratory; };
bool IsLeaseSet () const { return m_IsLeaseSet; };
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
uint64_t GetCreationTime () const { return m_CreationTime; };
I2NPMessage * CreateRequestMessage (const RouterInfo * router, const i2p::tunnel::InboundTunnel * replyTunnel);
I2NPMessage * CreateRequestMessage (const IdentHash& floodfill);

i2p::tunnel::OutboundTunnel * GetLastOutboundTunnel () const { return m_LastOutboundTunnel; };
void SetLastOutboundTunnel (i2p::tunnel::OutboundTunnel * tunnel) { m_LastOutboundTunnel = tunnel; };


private:

IdentHash m_Destination;
bool m_IsLeaseSet, m_IsExploratory;
std::set<IdentHash> m_ExcludedPeers;
const RouterInfo * m_LastRouter;
const i2p::tunnel::InboundTunnel * m_LastReplyTunnel;
i2p::tunnel::OutboundTunnel * m_LastOutboundTunnel;
uint64_t m_CreationTime;
};

Expand Down
4 changes: 2 additions & 2 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ namespace tunnel
auto pool = (*it)->GetTunnelPool ();
if (pool)
pool->TunnelExpired (*it);
delete *it;
it = m_OutboundTunnels.erase (it);
// TODO: delete tunnel, but make nobody uses it
}
else
it++;
Expand Down Expand Up @@ -431,8 +431,8 @@ namespace tunnel
auto pool = it->second->GetTunnelPool ();
if (pool)
pool->TunnelExpired (it->second);
delete it->second;
it = m_InboundTunnels.erase (it);
// TODO: delete tunnel, but make nobody uses it
}
else
it++;
Expand Down
19 changes: 15 additions & 4 deletions TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace tunnel
{
expiredTunnel->SetTunnelPool (nullptr);
m_InboundTunnels.erase (expiredTunnel);
for (auto it: m_Tests)
if (it.second.second == expiredTunnel) it.second.second = nullptr;

}
m_LocalDestination.UpdateLeaseSet ();
}
Expand All @@ -49,6 +52,8 @@ namespace tunnel
{
expiredTunnel->SetTunnelPool (nullptr);
m_OutboundTunnels.erase (expiredTunnel);
for (auto it: m_Tests)
if (it.second.first == expiredTunnel) it.second.first = nullptr;
}
}

Expand Down Expand Up @@ -105,10 +110,16 @@ namespace tunnel
{
LogPrint ("Tunnel test ", (int)it.first, " failed");
// both outbound and inbound tunnels considered as invalid
it.second.first->SetFailed (true);
it.second.second->SetFailed (true);
m_OutboundTunnels.erase (it.second.first);
m_InboundTunnels.erase (it.second.second);
if (it.second.first)
{
it.second.first->SetFailed (true);
m_OutboundTunnels.erase (it.second.first);
}
if (it.second.second)
{
it.second.second->SetFailed (true);
m_InboundTunnels.erase (it.second.second);
}
}
m_Tests.clear ();
auto it1 = m_OutboundTunnels.begin ();
Expand Down

0 comments on commit ab5576c

Please sign in to comment.