Browse Source

fixed race condition

pull/157/head
orignal 10 years ago
parent
commit
5730b15f01
  1. 36
      NTCPSession.cpp
  2. 3
      NTCPSession.h
  3. 5
      SSUSession.cpp
  4. 2
      SSUSession.h
  5. 2
      TransportSession.h
  6. 2
      Transports.cpp
  7. 2
      Transports.h

36
NTCPSession.cpp

@ -20,8 +20,9 @@ namespace transport @@ -20,8 +20,9 @@ namespace transport
{
NTCPSession::NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter):
TransportSession (in_RemoteRouter), m_Server (server), m_Socket (m_Server.GetService ()),
m_TerminationTimer (m_Server.GetService ()), m_IsEstablished (false), m_ReceiveBufferOffset (0),
m_NextMessage (nullptr), m_IsSending (false), m_NumSentBytes (0), m_NumReceivedBytes (0)
m_TerminationTimer (m_Server.GetService ()), m_IsEstablished (false), m_IsTerminated (false),
m_ReceiveBufferOffset (0), m_NextMessage (nullptr), m_IsSending (false),
m_NumSentBytes (0), m_NumReceivedBytes (0)
{
m_DHKeysPair = transports.GetNextDHKeysPair ();
m_Establisher = new Establisher;
@ -30,10 +31,6 @@ namespace transport @@ -30,10 +31,6 @@ namespace transport
NTCPSession::~NTCPSession ()
{
delete m_Establisher;
if (m_NextMessage)
i2p::DeleteI2NPMessage (m_NextMessage);
for (auto it: m_SendQueue)
DeleteI2NPMessage (it);
}
void NTCPSession::CreateAESKey (uint8_t * pubKey, i2p::crypto::AESKey& key)
@ -72,13 +69,30 @@ namespace transport @@ -72,13 +69,30 @@ namespace transport
}
}
void NTCPSession::Done ()
{
m_Server.GetService ().post (std::bind (&NTCPSession::Terminate, shared_from_this ()));
}
void NTCPSession::Terminate ()
{
m_IsEstablished = false;
m_Socket.close ();
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCPSession (shared_from_this ());
LogPrint (eLogInfo, "NTCP session terminated");
if (!m_IsTerminated)
{
m_IsTerminated = true;
m_IsEstablished = false;
m_Socket.close ();
transports.PeerDisconnected (shared_from_this ());
m_Server.RemoveNTCPSession (shared_from_this ());
for (auto it: m_SendQueue)
DeleteI2NPMessage (it);
m_SendQueue.clear ();
if (m_NextMessage)
{
i2p::DeleteI2NPMessage (m_NextMessage);
m_NextMessage = nullptr;
}
LogPrint (eLogInfo, "NTCP session terminated");
}
}
void NTCPSession::Connected ()

3
NTCPSession.h

@ -54,6 +54,7 @@ namespace transport @@ -54,6 +54,7 @@ namespace transport
NTCPSession (NTCPServer& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter = nullptr);
~NTCPSession ();
void Terminate ();
void Done ();
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
bool IsEstablished () const { return m_IsEstablished; };
@ -113,7 +114,7 @@ namespace transport @@ -113,7 +114,7 @@ namespace transport
NTCPServer& m_Server;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished;
bool m_IsEstablished, m_IsTerminated;
i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption;

5
SSUSession.cpp

@ -759,9 +759,10 @@ namespace transport @@ -759,9 +759,10 @@ namespace transport
transports.PeerDisconnected (shared_from_this ());
}
void SSUSession::Terminate ()
void SSUSession::Done ()
{
m_Server.DeleteSession (shared_from_this ());
boost::asio::io_service& service = IsV6 () ? m_Server.GetServiceV6 () : m_Server.GetService ();
service.post (std::bind (&SSUSession::Failed, shared_from_this ()));
}
void SSUSession::Established ()

2
SSUSession.h

@ -63,7 +63,7 @@ namespace transport @@ -63,7 +63,7 @@ namespace transport
void Introduce (uint32_t iTag, const uint8_t * iKey);
void WaitForIntroduction ();
void Close ();
void Terminate ();
void Done ();
boost::asio::ip::udp::endpoint& GetRemoteEndpoint () { return m_RemoteEndpoint; };
bool IsV6 () const { return m_RemoteEndpoint.address ().is_v6 (); };
void SendI2NPMessage (I2NPMessage * msg);

2
TransportSession.h

@ -62,7 +62,7 @@ namespace transport @@ -62,7 +62,7 @@ namespace transport
}
virtual ~TransportSession () { delete m_DHKeysPair; };
virtual void Terminate () = 0;
virtual void Done () = 0;
std::shared_ptr<const i2p::data::RouterInfo> GetRemoteRouter () { return m_RemoteRouter; };
const i2p::data::IdentityEx& GetRemoteIdentity () { return m_RemoteIdentity; };

2
Transports.cpp

@ -414,7 +414,7 @@ namespace transport @@ -414,7 +414,7 @@ namespace transport
else
{
LogPrint (eLogError, "Session for ", ident.ToBase64 ().substr (0, 4), " already exists");
session->Terminate ();
session->Done ();
}
}
else // incoming connection

2
Transports.h

@ -61,7 +61,7 @@ namespace transport @@ -61,7 +61,7 @@ namespace transport
~Peer ()
{
for (auto it :delayedMessages)
i2p::DeleteI2NPMessage (it);
i2p::DeleteI2NPMessage (it);
}
};

Loading…
Cancel
Save