Browse Source

tunnel state

pull/93/head
orignal 10 years ago
parent
commit
72e689791c
  1. 15
      Tunnel.cpp
  2. 17
      Tunnel.h
  3. 23
      TunnelPool.cpp

15
Tunnel.cpp

@ -14,8 +14,8 @@ namespace i2p
namespace tunnel namespace tunnel
{ {
Tunnel::Tunnel (TunnelConfig * config): m_Config (config), m_Pool (nullptr), Tunnel::Tunnel (TunnelConfig * config):
m_IsEstablished (false), m_IsFailed (false) m_Config (config), m_Pool (nullptr), m_State (eTunnelStatePending)
{ {
} }
@ -111,7 +111,7 @@ namespace tunnel
hop = hop->prev; hop = hop->prev;
} }
m_IsEstablished = true; bool established = true;
hop = m_Config->GetFirstHop (); hop = m_Config->GetFirstHop ();
while (hop) while (hop)
{ {
@ -119,10 +119,10 @@ namespace tunnel
LogPrint ("Ret code=", (int)record->ret); LogPrint ("Ret code=", (int)record->ret);
if (record->ret) if (record->ret)
// if any of participants declined the tunnel is not established // if any of participants declined the tunnel is not established
m_IsEstablished = false; established = false;
hop = hop->next; hop = hop->next;
} }
if (m_IsEstablished) if (established)
{ {
// change reply keys to layer keys // change reply keys to layer keys
hop = m_Config->GetFirstHop (); hop = m_Config->GetFirstHop ();
@ -132,7 +132,8 @@ namespace tunnel
hop = hop->next; hop = hop->next;
} }
} }
return m_IsEstablished; if (established) m_State = eTunnelStateEstablished;
return established;
} }
void Tunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg) void Tunnel::EncryptTunnelMsg (I2NPMessage * tunnelMsg)
@ -148,7 +149,7 @@ namespace tunnel
void InboundTunnel::HandleTunnelDataMsg (I2NPMessage * msg) 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; msg->from = this;
EncryptTunnelMsg (msg); EncryptTunnelMsg (msg);
m_Endpoint.HandleDecryptedTunnelDataMsg (msg); m_Endpoint.HandleDecryptedTunnelDataMsg (msg);

17
Tunnel.h

@ -23,6 +23,14 @@ namespace tunnel
{ {
const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes const int TUNNEL_EXPIRATION_TIMEOUT = 660; // 11 minutes
enum TunnelState
{
eTunnelStatePending,
eTunnelStateEstablished,
eTunnelStateTestFailed,
eTunnelStateFailed
};
class OutboundTunnel; class OutboundTunnel;
class InboundTunnel; class InboundTunnel;
class Tunnel: public TunnelBase class Tunnel: public TunnelBase
@ -35,9 +43,10 @@ namespace tunnel
void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0); void Build (uint32_t replyMsgID, OutboundTunnel * outboundTunnel = 0);
TunnelConfig * GetTunnelConfig () const { return m_Config; } TunnelConfig * GetTunnelConfig () const { return m_Config; }
bool IsEstablished () const { return m_IsEstablished; }; TunnelState GetState () const { return m_State; };
bool IsFailed () const { return m_IsFailed; }; void SetState (TunnelState state) { m_State = state; };
void SetFailed (bool failed) { m_IsFailed = failed; } bool IsEstablished () const { return m_State == eTunnelStateEstablished; };
bool IsFailed () const { return m_State == eTunnelStateFailed; };
TunnelPool * GetTunnelPool () const { return m_Pool; }; TunnelPool * GetTunnelPool () const { return m_Pool; };
void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; }; void SetTunnelPool (TunnelPool * pool) { m_Pool = pool; };
@ -53,7 +62,7 @@ namespace tunnel
TunnelConfig * m_Config; TunnelConfig * m_Config;
TunnelPool * m_Pool; // pool, tunnel belongs to, or null TunnelPool * m_Pool; // pool, tunnel belongs to, or null
bool m_IsEstablished, m_IsFailed; TunnelState m_State;
}; };
class OutboundTunnel: public Tunnel class OutboundTunnel: public Tunnel

23
TunnelPool.cpp

@ -109,16 +109,26 @@ namespace tunnel
for (auto it: m_Tests) for (auto it: m_Tests)
{ {
LogPrint ("Tunnel test ", (int)it.first, " failed"); 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) if (it.second.first)
{ {
it.second.first->SetFailed (true); if (it.second.first->GetState () == eTunnelStateTestFailed)
m_OutboundTunnels.erase (it.second.first); {
it.second.first->SetState (eTunnelStateFailed);
m_OutboundTunnels.erase (it.second.first);
}
else
it.second.first->SetState (eTunnelStateTestFailed);
} }
if (it.second.second) if (it.second.second)
{ {
it.second.second->SetFailed (true); if (it.second.second->GetState () == eTunnelStateTestFailed)
m_InboundTunnels.erase (it.second.second); {
it.second.second->SetState (eTunnelStateFailed);
m_InboundTunnels.erase (it.second.second);
}
else
it.second.second->SetState (eTunnelStateTestFailed);
} }
} }
m_Tests.clear (); m_Tests.clear ();
@ -156,6 +166,9 @@ namespace tunnel
{ {
LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds"); LogPrint ("Tunnel test ", it->first, " successive. ", i2p::util::GetMillisecondsSinceEpoch () - be64toh (deliveryStatus->timestamp), " milliseconds");
m_Tests.erase (it); m_Tests.erase (it);
// restore from test failed state if any
it->second.first->SetState (eTunnelStateEstablished);
it->second.second->SetState (eTunnelStateEstablished);
} }
else else
i2p::garlic::routing.HandleDeliveryStatusMessage (msg->GetPayload (), msg->GetLength ()); // TODO: i2p::garlic::routing.HandleDeliveryStatusMessage (msg->GetPayload (), msg->GetLength ()); // TODO:

Loading…
Cancel
Save