From 0a33c18e367a23dff697e7fc514dea5d4c3c1cc9 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 2 Aug 2018 15:31:15 -0400 Subject: [PATCH] send termination message --- libi2pd/NTCP2.cpp | 24 +++++++++++++++++++----- libi2pd/NTCP2.h | 4 +++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 7a3df9bf..00d874cc 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -570,8 +570,8 @@ namespace transport i2p::data::RouterInfo ri (buf.data () + 4, size - 1); // 1 byte block type + 2 bytes size + 1 byte flag if (ri.IsUnreachable ()) { - LogPrint (eLogError, "NTCP2: Signature verification failed in SessionConfirmed"); - Terminate (); + LogPrint (eLogError, "NTCP2: Signature verification failed in SessionConfirmed"); + SendTerminationAndTerminate (eNTCP2RouterInfoSignatureVerificationFail); return; } auto addr = ri.GetNTCP2Address (); @@ -583,8 +583,8 @@ namespace transport } if (memcmp (addr->ntcp2->staticKey, m_Establisher->m_RemoteStaticKey, 32)) { - LogPrint (eLogError, "NTCP2: Static key mistmatch in SessionConfirmed"); - Terminate (); + LogPrint (eLogError, "NTCP2: Static key mistmatch in SessionConfirmed"); + SendTerminationAndTerminate (eNTCP2IncorrectSParameter); return; } @@ -728,7 +728,7 @@ namespace transport case eNTCP2BlkTermination: if (size >= 9) { - LogPrint (eLogDebug, "NTCP2: termination. reason=", (int)(frame[offset + 9])); + LogPrint (eLogDebug, "NTCP2: termination. reason=", (int)(frame[offset + 8])); Terminate (); } else @@ -828,6 +828,20 @@ namespace transport delete[] payload; } + void NTCP2Session::SendTermination (NTCP2TerminationReason reason) + { + uint8_t payload[12] = { eNTCP2BlkTermination, 0, 9 }; + htobe64buf (payload + 3, m_ReceiveSequenceNumber); + payload[11] = (uint8_t)reason; + SendNextFrame (payload, 12); + } + + void NTCP2Session::SendTerminationAndTerminate (NTCP2TerminationReason reason) + { + SendTermination (reason); + m_Server.GetService ().post (std::bind (&NTCP2Session::Terminate, shared_from_this ())); // let termination message go + } + void NTCP2Session::SendI2NPMessages (const std::vector >& msgs) { m_Server.GetService ().post (std::bind (&NTCP2Session::PostI2NPMessages, shared_from_this (), msgs)); diff --git a/libi2pd/NTCP2.h b/libi2pd/NTCP2.h index 5ac6a280..c7c53b53 100644 --- a/libi2pd/NTCP2.h +++ b/libi2pd/NTCP2.h @@ -40,7 +40,7 @@ namespace transport eNTCP2ClockSkew, // 7 eNTCP2PaddingViolation, // 8 eNTCP2AEADFraminError, // 9 - eNTCP2PayloadFromatError, // 10 + eNTCP2PayloadFormatError, // 10 eNTCP2Message1Error, // 11 eNTCP2Message2Error, // 12 eNTCP2Message3Error, // 13 @@ -135,6 +135,8 @@ namespace transport void HandleNextFrameSent (const boost::system::error_code& ecode, std::size_t bytes_transferred); void SendQueue (); void SendRouterInfo (); + void SendTermination (NTCP2TerminationReason reason); + void SendTerminationAndTerminate (NTCP2TerminationReason reason); void PostI2NPMessages (std::vector > msgs); private: