From 3bdef5f58dad4237a9e6040583d256426452d2fb Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 7 Sep 2022 19:11:33 -0400 Subject: [PATCH] update remote endpoint and send path challenge --- libi2pd/SSU2.cpp | 4 ++-- libi2pd/SSU2Session.cpp | 22 +++++++++++++++++++++- libi2pd/SSU2Session.h | 3 ++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 156bc2b0..245169d8 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -410,7 +410,7 @@ namespace transport { case eSSU2SessionStateEstablished: case eSSU2SessionStateSessionConfirmedSent: - m_LastSession->ProcessData (buf, len); + m_LastSession->ProcessData (buf, len, senderEndpoint); break; case eSSU2SessionStateSessionCreatedSent: if (!m_LastSession->ProcessSessionConfirmed (buf, len)) @@ -437,7 +437,7 @@ namespace transport m_LastSession->ProcessPeerTest (buf, len); break; case eSSU2SessionStateClosing: - m_LastSession->ProcessData (buf, len); // we might receive termintaion block + m_LastSession->ProcessData (buf, len, senderEndpoint); // we might receive termintaion block if (m_LastSession && m_LastSession->GetState () != eSSU2SessionStateTerminated) m_LastSession->RequestTermination (eSSU2TerminationReasonIdleTimeout); // send termination again break; diff --git a/libi2pd/SSU2Session.cpp b/libi2pd/SSU2Session.cpp index 4df9a076..e5430054 100644 --- a/libi2pd/SSU2Session.cpp +++ b/libi2pd/SSU2Session.cpp @@ -1301,7 +1301,7 @@ namespace transport return m_SendPacketNum - 1; } - void SSU2Session::ProcessData (uint8_t * buf, size_t len) + void SSU2Session::ProcessData (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from) { Header header; header.ll[0] = m_SourceConnID; @@ -1316,6 +1316,12 @@ namespace transport ResendHandshakePacket (); // assume we receive return; } + if (from != m_RemoteEndpoint) + { + LogPrint (eLogInfo, "SSU2: Remote endpoint update ", m_RemoteEndpoint, "->", from); + m_RemoteEndpoint = from; + SendPathChallenge (); + } uint8_t payload[SSU2_MAX_PACKET_SIZE]; size_t payloadSize = len - 32; uint32_t packetNum = be32toh (header.h.packetNum); @@ -2625,6 +2631,20 @@ namespace transport memcpy (payload + 3, data, len); SendData (payload, len + 3); } + + void SSU2Session::SendPathChallenge () + { + uint8_t payload[SSU2_MAX_PACKET_SIZE]; + payload[0] = eSSU2BlkPathChallenge; + size_t len = rand () % (m_MaxPayloadSize - 3); + htobe16buf (payload + 1, len); + if (len > 0) + RAND_bytes (payload + 3, len); + len += 3; + if (len < m_MaxPayloadSize) + len += CreatePaddingBlock (payload + len, m_MaxPayloadSize - len); + SendData (payload, len); + } void SSU2Session::CleanUp (uint64_t ts) { diff --git a/libi2pd/SSU2Session.h b/libi2pd/SSU2Session.h index 72211bf6..a7aa5557 100644 --- a/libi2pd/SSU2Session.h +++ b/libi2pd/SSU2Session.h @@ -255,7 +255,7 @@ namespace transport bool ProcessRetry (uint8_t * buf, size_t len); bool ProcessHolePunch (uint8_t * buf, size_t len); bool ProcessPeerTest (uint8_t * buf, size_t len); - void ProcessData (uint8_t * buf, size_t len); + void ProcessData (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& from); private: @@ -284,6 +284,7 @@ namespace transport void SendHolePunch (uint32_t nonce, const boost::asio::ip::udp::endpoint& ep, const uint8_t * introKey, uint64_t token); void SendPeerTest (uint8_t msg, const uint8_t * signedData, size_t signedDataLen, const uint8_t * introKey); // PeerTest message void SendPathResponse (const uint8_t * data, size_t len); + void SendPathChallenge (); void HandlePayload (const uint8_t * buf, size_t len); void HandleDateTime (const uint8_t * buf, size_t len);