From 169e5d33cd97533bcd5e679ec6e1401a2c4571f5 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 27 Apr 2023 19:15:58 -0400 Subject: [PATCH] Redo wire protocol race condition fix. In commit a76acd49560f9845c2ad9a839364f2834afc7756 we attempted to fix issues on service nodes that related to spamming service nodes with traffc, but this caused a regression where clients could not connect to the network. However, we also did something idiotic with the protocol handshake. The handshake logic was modified and caused client connections to fail. This was due to a change in how we decided what a timed out session is. We redo the close bug and revert the change to the protocol handshake. (cherry picked from commit b0b6e2b198ba32e8c5641e99494d2874b2243b8f) --- llarp/iwp/session.cpp | 9 ++++----- llarp/iwp/session.hpp | 1 - llarp/router/router.cpp | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/llarp/iwp/session.cpp b/llarp/iwp/session.cpp index 75f2356a5..c8c7e7fa8 100644 --- a/llarp/iwp/session.cpp +++ b/llarp/iwp/session.cpp @@ -144,6 +144,8 @@ namespace llarp void Session::EncryptAndSend(ILinkSession::Packet_t data) { + if (m_State == State::Closed) + return; m_EncryptNext.emplace_back(std::move(data)); TriggerPump(); if (!IsEstablished()) @@ -179,12 +181,9 @@ namespace llarp return; auto close_msg = CreatePacket(Command::eCLOS, 0, 16, 16); m_Parent->UnmapAddr(m_RemoteAddr); - m_State = State::Closed; - if (m_SentClosed.test_and_set()) - return; EncryptAndSend(std::move(close_msg)); - LogInfo(m_Parent->PrintableName(), " closing connection to ", m_RemoteAddr); + m_State = State::Closed; } bool @@ -355,7 +354,7 @@ namespace llarp bool Session::TimedOut(llarp_time_t now) const { - if (m_State == State::Ready) + if (m_State == State::Ready || m_State == State::LinkIntro) { return now > m_LastRX && now - m_LastRX diff --git a/llarp/iwp/session.hpp b/llarp/iwp/session.hpp index 210a37a1e..1e0e6b3c4 100644 --- a/llarp/iwp/session.hpp +++ b/llarp/iwp/session.hpp @@ -206,7 +206,6 @@ namespace llarp std::atomic_flag m_PlaintextEmpty; llarp::thread::Queue m_PlaintextRecv; - std::atomic_flag m_SentClosed; void EncryptWorker(CryptoQueue_t msgs); diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index c1d8e8595..9313f03cc 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -482,8 +482,8 @@ namespace llarp LogError("RC is invalid, not saving"); return false; } - if (m_isServiceNode) - _nodedb->Put(_rc); + if (IsServiceNode()) + _nodedb->Put(rc()); QueueDiskIO([&]() { HandleSaveRC(); }); return true; }