From dda25d431ce05681181aade642f385e0e4f11459 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 21 Jun 2022 16:20:39 -0400 Subject: [PATCH] check if we can send HolePunch to partcular address --- libi2pd/SSU2.cpp | 15 +++++++++++++++ libi2pd/SSU2.h | 1 + libi2pd/SSU2Session.cpp | 7 ++++++- libi2pd/SSU2Session.h | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index 25f60fb9..6c5e2f82 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -108,6 +108,21 @@ namespace transport else if (localAddress.is_v6 ()) m_AddressV6 = localAddress; } + + bool SSU2Server::IsSupported (const boost::asio::ip::address& addr) const + { + if (addr.is_v4 ()) + { + if (m_SocketV4.is_open ()) + return true; + } + else if (addr.is_v6 ()) + { + if (m_SocketV6.is_open ()) + return true; + } + return false; + } boost::asio::ip::udp::socket& SSU2Server::OpenSocket (const boost::asio::ip::udp::endpoint& localEndpoint) { diff --git a/libi2pd/SSU2.h b/libi2pd/SSU2.h index 1c16f7af..e90922e3 100644 --- a/libi2pd/SSU2.h +++ b/libi2pd/SSU2.h @@ -49,6 +49,7 @@ namespace transport void Stop (); boost::asio::io_service& GetService () { return GetIOService (); }; void SetLocalAddress (const boost::asio::ip::address& localAddress); + bool IsSupported (const boost::asio::ip::address& addr) const; void AddSession (std::shared_ptr session); void RemoveSession (uint64_t connID); diff --git a/libi2pd/SSU2Session.cpp b/libi2pd/SSU2Session.cpp index e8be2c13..7f89c4db 100644 --- a/libi2pd/SSU2Session.cpp +++ b/libi2pd/SSU2Session.cpp @@ -1344,7 +1344,12 @@ namespace transport { auto addr = ep.address ().is_v6 () ? r->GetSSU2V6Address () : r->GetSSU2V4Address (); if (addr) - SendHolePunch (bufbe32toh (buf + 33), ep, addr->i); + { + if (m_Server.IsSupported (ep.address ())) + SendHolePunch (bufbe32toh (buf + 33), ep, addr->i); + else + code = eSSU2RelayResponseCodeCharlieUnsupportedAddress; + } else { LogPrint (eLogWarning, "SSU2: RelayInfo unknown address"); diff --git a/libi2pd/SSU2Session.h b/libi2pd/SSU2Session.h index 30664e5f..5b45253d 100644 --- a/libi2pd/SSU2Session.h +++ b/libi2pd/SSU2Session.h @@ -108,6 +108,7 @@ namespace transport { eSSU2RelayResponseCodeAccept = 0, eSSU2RelayResponseCodeBobRelayTagNotFound = 5, + eSSU2RelayResponseCodeCharlieUnsupportedAddress = 65, eSSU2RelayResponseCodeCharlieSignatureFailure = 67, eSSU2RelayResponseCodeCharlieAliceIsUnknown = 70 };