From 3522a3180b2b7d34b5ab68999efb3e3e16801bb9 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 31 Aug 2025 17:50:02 -0400 Subject: [PATCH] implement PING command --- libi2pd_client/SAM.cpp | 14 ++++++++++++++ libi2pd_client/SAM.h | 6 +++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libi2pd_client/SAM.cpp b/libi2pd_client/SAM.cpp index 22279001..80987c78 100644 --- a/libi2pd_client/SAM.cpp +++ b/libi2pd_client/SAM.cpp @@ -248,6 +248,14 @@ namespace client char * separator = strchr (m_Buffer, ' '); if (separator) { + // one word command + if (std::string_view (m_Buffer, separator - m_Buffer) == SAM_PING) + { + ProcessPing ({ separator + 1, (size_t)(eol - separator - 1) }); + return; + } + + // two words command size_t l = 0; separator = strchr (separator + 1, ' '); if (separator) @@ -955,6 +963,12 @@ namespace client SendSessionI2PError ("Wrong session type"); } + void SAMSocket::ProcessPing (std::string_view text) + { + LogPrint (eLogDebug, "SAM: Ping ", text); + SendReplyWithMessage (SAM_PONG, std::string (text)); + } + void SAMSocket::SendReplyWithMessage (const char * reply, const std::string & msg) { #ifdef _MSC_VER diff --git a/libi2pd_client/SAM.h b/libi2pd_client/SAM.h index 00b7ed03..fd73003f 100644 --- a/libi2pd_client/SAM.h +++ b/libi2pd_client/SAM.h @@ -85,12 +85,15 @@ namespace client const char SAM_VALUE_TRANSIENT[] = "TRANSIENT"; const char SAM_VALUE_TRUE[] = "true"; const char SAM_VALUE_FALSE[] = "false"; - + constexpr std::string_view SAM_VALUE_STREAM { "STREAM" }; constexpr std::string_view SAM_VALUE_DATAGRAM { "DATAGRAM" }; constexpr std::string_view SAM_VALUE_RAW { "RAW" }; constexpr std::string_view SAM_VALUE_MASTER { "MASTER" }; + constexpr std::string_view SAM_PING { "PING" }; + const char SAM_PONG[] = "PONG %s\n"; + constexpr int MAKE_SAM_VERSION_NUMBER (int major, int minor) { return major*10 + minor; } constexpr int MIN_SAM_VERSION = MAKE_SAM_VERSION_NUMBER (3, 0); constexpr int MAX_SAM_VERSION = MAKE_SAM_VERSION_NUMBER (3, 3); @@ -153,6 +156,7 @@ namespace client void ProcessNamingLookup (std::string_view buf); void ProcessSessionAdd (std::string_view buf); void ProcessSessionRemove (std::string_view buf); + void ProcessPing (std::string_view text); void SendReplyWithMessage (const char * reply, const std::string & msg); void SendSessionI2PError(const std::string & msg); void SendStreamI2PError(const std::string & msg);