Skip to content

Commit

Permalink
tunnel state
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jul 27, 2014
1 parent 4576efd commit 72e6897
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
15 changes: 8 additions & 7 deletions Tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace i2p
namespace tunnel
{

Tunnel::Tunnel (TunnelConfig * config): m_Config (config), m_Pool (nullptr),
m_IsEstablished (false), m_IsFailed (false)
Tunnel::Tunnel (TunnelConfig * config):
m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending)
{
}

Expand Down Expand Up @@ -111,18 +111,18 @@ namespace tunnel
hop = hop->prev;
}

m_IsEstablished = true;
bool established = true;
hop = m_Config->GetFirstHop ();
while (hop)
{
I2NPBuildResponseRecord * record = (I2NPBuildResponseRecord *)(msg + 1 + hop->recordIndex*sizeof (I2NPBuildResponseRecord));
LogPrint ("Ret code=", (int)record->ret);
if (record->ret)
// if any of participants declined the tunnel is not established
m_IsEstablished = false;
established = false;
hop = hop->next;
}
if (m_IsEstablished)
if (established)
{
// change reply keys to layer keys
hop = m_Config->GetFirstHop ();
Expand All @@ -132,7 +132,8 @@ namespace tunnel
hop = hop->next;
}
}
return m_IsEstablished;
if (established) m_State = eTunnelStateEstablished;
return established;
}

void Tunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
Expand All @@ -148,7 +149,7 @@ namespace tunnel

void InboundTunnel::HandleTunnelDataMsg (I2NPMessage * msg)
{
if (IsFailed ()) SetFailed (false); // incoming messages means a tunnel is alive
if (IsFailed ()) SetState (eTunnelStateEstablished); // incoming messages means a tunnel is alive
msg->from = this;
EncryptTunnelMsg (msg);
m_Endpoint.HandleDecryptedTunnelDataMsg (msg);
Expand Down
17 changes: 13 additions & 4 deletions Tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ namespace i2p
namespace tunnel
{
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes

enum TunnelState
{
eTunnelStatePending,
eTunnelStateEstablished,
eTunnelStateTestFailed,
eTunnelStateFailed
};

class OutboundTunnel;
class InboundTunnel;
Expand All @@ -35,9 +43,10 @@ namespace tunnel
void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0);

TunnelConfig * GetTunnelConfig () const { return m_Config; }
bool IsEstablished () const { return m_IsEstablished; };
bool IsFailed () const { return m_IsFailed; };
void SetFailed (bool failed) { m_IsFailed = failed; }
TunnelState GetState () const { return m_State; };
void SetState (TunnelState state) { m_State = state; };
bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
bool IsFailed () const { return m_State == eTunnelStateFailed; };

TunnelPool * GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; };
Expand All @@ -53,7 +62,7 @@ namespace tunnel

TunnelConfig * m_Config;
TunnelPool * m_Pool; // pool, tunnel belongs to, or null
bool m_IsEstablished, m_IsFailed;
TunnelState m_State;
};

class OutboundTunnel: public Tunnel
Expand Down
25 changes: 19 additions & 6 deletions TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,26 @@ namespace tunnel
for (auto it: m_Tests)
{
LogPrint ("Tunnel test ", (int)it.first, " failed");
// both outbound and inbound tunnels considered as invalid
// if test failed again with another tunnel we consider it failed
if (it.second.first)
{
it.second.first->SetFailed (true);
m_OutboundTunnels.erase (it.second.first);
if (it.second.first->GetState () == eTunnelStateTestFailed)
{
it.second.first->SetState (eTunnelStateFailed);
m_OutboundTunnels.erase (it.second.first);
}
else
it.second.first->SetState (eTunnelStateTestFailed);
}
if (it.second.second)
{
it.second.second->SetFailed (true);
m_InboundTunnels.erase (it.second.second);
if (it.second.second->GetState () == eTunnelStateTestFailed)
{
it.second.second->SetState (eTunnelStateFailed);
m_InboundTunnels.erase (it.second.second);
}
else
it.second.second->SetState (eTunnelStateTestFailed);
}
}
m_Tests.clear ();
Expand All @@ -138,7 +148,7 @@ namespace tunnel
it2++;
}
if (!failed)
{
{
uint32_t msgID = rnd.GenerateWord32 ();
m_Tests[msgID] = std::make_pair (*it1, *it2);
(*it1)->SendTunnelDataMsg ((*it2)->GetNextIdentHash (), (*it2)->GetNextTunnelID (),
Expand All @@ -156,6 +166,9 @@ namespace tunnel
{
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds");
m_Tests.erase (it);
// restore from test failed state if any
it->second.first->SetState (eTunnelStateEstablished);
it->second.second->SetState (eTunnelStateEstablished);
}
else
i2p::garlic::routing.HandleDeliveryStatusMessage (msg->GetPayload (), msg->GetLength ()); // TODO:
Expand Down

0 comments on commit 72e6897

Please sign in to comment.