Skip to content

Commit

Permalink
re-create expired tunnels
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Aug 9, 2014
1 parent 58fdae9 commit 1a72292
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
25 changes: 23 additions & 2 deletions TunnelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace tunnel
nextTunnelID = rnd.GenerateWord32 ();
}

void SetReplyHop (TunnelHopConfig * replyFirstHop)
void SetReplyHop (const TunnelHopConfig * replyFirstHop)
{
nextRouter = replyFirstHop->router;
nextTunnelID = replyFirstHop->tunnelID;
Expand Down Expand Up @@ -89,7 +89,7 @@ namespace tunnel


TunnelConfig (std::vector<const i2p::data::RouterInfo *> peers,
TunnelConfig * replyTunnelConfig = 0) // replyTunnelConfig=0 means inbound
const TunnelConfig * replyTunnelConfig = nullptr) // replyTunnelConfig=0 means inbound
{
TunnelHopConfig * prev = nullptr;
for (auto it: peers)
Expand Down Expand Up @@ -192,6 +192,27 @@ namespace tunnel
}
return newConfig;
}

TunnelConfig * Clone (const TunnelConfig * replyTunnelConfig = nullptr) const
{
TunnelConfig * newConfig = new TunnelConfig ();
TunnelHopConfig * hop = m_FirstHop, * prev = nullptr;
while (hop)
{
TunnelHopConfig * newHop = new TunnelHopConfig (hop->router);
newHop->SetPrev (prev);
newHop->SetNextRouter (hop->nextRouter);
newHop->isGateway = hop->isGateway;
newHop->isEndpoint = hop->isEndpoint;
prev = newHop;
if (!newConfig->m_FirstHop) newConfig->m_FirstHop = newHop;
hop = hop->next;
}
newConfig->m_LastHop = prev;
if (replyTunnelConfig && newConfig->m_LastHop)
newConfig->m_LastHop->SetReplyHop (replyTunnelConfig->GetFirstHop ());
return newConfig;
}

private:

Expand Down
24 changes: 23 additions & 1 deletion TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace tunnel
m_InboundTunnels.erase (expiredTunnel);
for (auto it: m_Tests)
if (it.second.second == expiredTunnel) it.second.second = nullptr;

RecreateInboundTunnel (expiredTunnel);
}
}

Expand All @@ -53,6 +53,7 @@ namespace tunnel
m_OutboundTunnels.erase (expiredTunnel);
for (auto it: m_Tests)
if (it.second.first == expiredTunnel) it.second.first = nullptr;
RecreateOutboundTunnel (expiredTunnel);
}
}

Expand Down Expand Up @@ -204,6 +205,16 @@ namespace tunnel
tunnel->SetTunnelPool (this);
}

void TunnelPool::RecreateInboundTunnel (InboundTunnel * tunnel)
{
OutboundTunnel * outboundTunnel = GetNextOutboundTunnel ();
if (!outboundTunnel)
outboundTunnel = tunnels.GetNextOutboundTunnel ();
LogPrint ("Re-creating destination inbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<InboundTunnel> (tunnel->GetTunnelConfig ()->Clone (), outboundTunnel);
newTunnel->SetTunnelPool (this);
}

void TunnelPool::CreateOutboundTunnel ()
{
InboundTunnel * inboundTunnel = m_InboundTunnels.size () > 0 ?
Expand All @@ -226,5 +237,16 @@ namespace tunnel
tunnel->SetTunnelPool (this);
}
}

void TunnelPool::RecreateOutboundTunnel (OutboundTunnel * tunnel)
{
InboundTunnel * inboundTunnel = GetNextInboundTunnel ();
if (!inboundTunnel)
inboundTunnel = tunnels.GetNextInboundTunnel ();
LogPrint ("Re-creating destination outbound tunnel...");
auto * newTunnel = tunnels.CreateTunnel<OutboundTunnel> (
tunnel->GetTunnelConfig ()->Clone (inboundTunnel->GetTunnelConfig ()));
newTunnel->SetTunnelPool (this);
}
}
}
2 changes: 2 additions & 0 deletions TunnelPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace tunnel

void CreateInboundTunnel ();
void CreateOutboundTunnel ();
void RecreateInboundTunnel (InboundTunnel * tunnel);
void RecreateOutboundTunnel (OutboundTunnel * tunnel);
template<class TTunnels>
typename TTunnels::value_type GetNextTunnel (TTunnels& tunnels);

Expand Down

0 comments on commit 1a72292

Please sign in to comment.