From c554f47c4d13b0e70cf7ec9f96431e8b8996d93e Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 22 Jul 2025 16:07:24 -0400 Subject: [PATCH] datagramversion param for UDP client tunnel --- libi2pd/Datagram.cpp | 16 ++++++++++------ libi2pd/Datagram.h | 3 ++- libi2pd/Destination.cpp | 7 ++++--- libi2pd/Destination.h | 3 ++- libi2pd_client/ClientContext.cpp | 11 ++++++++--- libi2pd_client/ClientContext.h | 4 +++- libi2pd_client/UDPTunnel.cpp | 8 ++++---- libi2pd_client/UDPTunnel.h | 5 +++-- 8 files changed, 36 insertions(+), 21 deletions(-) diff --git a/libi2pd/Datagram.cpp b/libi2pd/Datagram.cpp index bfcc04a7..e1c9ec7d 100644 --- a/libi2pd/Datagram.cpp +++ b/libi2pd/Datagram.cpp @@ -18,8 +18,10 @@ namespace i2p { namespace datagram { - DatagramDestination::DatagramDestination (std::shared_ptr owner, bool gzip): - m_Owner (owner), m_DefaultReceiver (nullptr), m_DefaultRawReceiver (nullptr), m_Gzip (gzip) + DatagramDestination::DatagramDestination (std::shared_ptr owner, + bool gzip, DatagramVersion version): + m_Owner (owner), m_DefaultReceiver (nullptr), m_DefaultRawReceiver (nullptr), + m_Gzip (gzip), m_Version (version) { if (m_Gzip) m_Deflator.reset (new i2p::data::GzipDeflator); @@ -431,14 +433,16 @@ namespace datagram std::shared_ptr session = nullptr; std::lock_guard lock(m_SessionsMutex); auto itr = m_Sessions.find(identity); - if (itr == m_Sessions.end()) { + if (itr == m_Sessions.end()) + { // not found, create new session session = std::make_shared(m_Owner, identity); + session->SetVersion (m_Version); session->Start (); - m_Sessions[identity] = session; - } else { + m_Sessions.emplace (identity, session); + } + else session = itr->second; - } return session; } diff --git a/libi2pd/Datagram.h b/libi2pd/Datagram.h index e3475225..f962d402 100644 --- a/libi2pd/Datagram.h +++ b/libi2pd/Datagram.h @@ -129,7 +129,7 @@ namespace datagram public: - DatagramDestination (std::shared_ptr owner, bool gzip); + DatagramDestination (std::shared_ptr owner, bool gzip, DatagramVersion version); ~DatagramDestination (); void SendDatagramTo (const uint8_t * payload, size_t len, const i2p::data::IdentHash & ident, uint16_t fromPort = 0, uint16_t toPort = 0); @@ -190,6 +190,7 @@ namespace datagram std::unordered_map m_RawReceiversByPorts; bool m_Gzip; // gzip compression of data messages + DatagramVersion m_Version; // default for destination i2p::data::GzipInflator m_Inflator; std::unique_ptr m_Deflator; std::vector m_From, m_Signature; diff --git a/libi2pd/Destination.cpp b/libi2pd/Destination.cpp index f2717772..dfb8ef05 100644 --- a/libi2pd/Destination.cpp +++ b/libi2pd/Destination.cpp @@ -1397,10 +1397,11 @@ namespace client return nullptr; } - i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination (bool gzip) + i2p::datagram::DatagramDestination * ClientDestination::CreateDatagramDestination (bool gzip, + i2p::datagram::DatagramVersion version) { - if (m_DatagramDestination == nullptr) - m_DatagramDestination = new i2p::datagram::DatagramDestination (GetSharedFromThis (), gzip); + if (!m_DatagramDestination) + m_DatagramDestination = new i2p::datagram::DatagramDestination (GetSharedFromThis (), gzip, version); return m_DatagramDestination; } diff --git a/libi2pd/Destination.h b/libi2pd/Destination.h index 717f35ce..2f700d53 100644 --- a/libi2pd/Destination.h +++ b/libi2pd/Destination.h @@ -274,7 +274,8 @@ namespace client // datagram i2p::datagram::DatagramDestination * GetDatagramDestination () const { return m_DatagramDestination; }; - i2p::datagram::DatagramDestination * CreateDatagramDestination (bool gzip = true); + i2p::datagram::DatagramDestination * CreateDatagramDestination (bool gzip = true, + i2p::datagram::DatagramVersion version = i2p::datagram::eDatagramV1); // implements LocalDestination bool Decrypt (const uint8_t * encrypted, uint8_t * data, i2p::data::CryptoKeyType preferredCrypto) const override; diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index 2d36cc11..f60909f1 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -636,7 +636,8 @@ namespace client } } - if (type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT) { + if (type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT) + { // udp client // TODO: hostnames boost::asio::ip::udp::endpoint end (boost::asio::ip::make_address(address), port); @@ -644,7 +645,9 @@ namespace client localDestination = m_SharedLocalDestination; bool gzip = section.second.get (I2P_CLIENT_TUNNEL_GZIP, true); - auto clientTunnel = std::make_shared (name, dest, end, localDestination, destinationPort, gzip); + int datagramVersion = (i2p::datagram::DatagramVersion)section.second.get (UDP_CLIENT_TUNNEL_DATAGRAM_VERSION, (int)i2p::datagram::eDatagramV1); + auto clientTunnel = std::make_shared (name, dest, end, + localDestination, destinationPort, gzip, (i2p::datagram::DatagramVersion)datagramVersion); auto ins = m_ClientForwards.insert (std::make_pair (end, clientTunnel)); if (ins.second) @@ -666,7 +669,9 @@ namespace client LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists"); } - } else { + } + else + { boost::asio::ip::tcp::endpoint clientEndpoint; std::shared_ptr clientTunnel; if (type == I2P_TUNNELS_SECTION_TYPE_SOCKS) diff --git a/libi2pd_client/ClientContext.h b/libi2pd_client/ClientContext.h index 3f7eaf9a..52790836 100644 --- a/libi2pd_client/ClientContext.h +++ b/libi2pd_client/ClientContext.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "Destination.h" #include "I2PService.h" @@ -61,7 +62,8 @@ namespace client const char I2P_SERVER_TUNNEL_ADDRESS[] = "address"; const char I2P_SERVER_TUNNEL_ENABLE_UNIQUE_LOCAL[] = "enableuniquelocal"; const char I2P_SERVER_TUNNEL_SSL[] = "ssl"; - + const char UDP_CLIENT_TUNNEL_DATAGRAM_VERSION[] = "datagramversion"; + class ClientContext { public: diff --git a/libi2pd_client/UDPTunnel.cpp b/libi2pd_client/UDPTunnel.cpp index b173fc0f..ade7ec75 100644 --- a/libi2pd_client/UDPTunnel.cpp +++ b/libi2pd_client/UDPTunnel.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2024, The PurpleI2P Project +* Copyright (c) 2013-2025, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -224,10 +224,10 @@ namespace client I2PUDPClientTunnel::I2PUDPClientTunnel (const std::string & name, const std::string &remoteDest, const boost::asio::ip::udp::endpoint& localEndpoint, std::shared_ptr localDestination, - uint16_t remotePort, bool gzip) : + uint16_t remotePort, bool gzip, i2p::datagram::DatagramVersion datagramVersion) : m_Name (name), m_RemoteDest (remoteDest), m_LocalDest (localDestination), m_LocalEndpoint (localEndpoint), m_ResolveThread (nullptr), m_LocalSocket (nullptr), RemotePort (remotePort), - m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip) + m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip), m_DatagramVersion (datagramVersion) { } @@ -245,7 +245,7 @@ namespace client m_LocalSocket->set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU)); m_LocalSocket->set_option (boost::asio::socket_base::reuse_address (true)); - auto dgram = m_LocalDest->CreateDatagramDestination (m_Gzip); + auto dgram = m_LocalDest->CreateDatagramDestination (m_Gzip, m_DatagramVersion); dgram->SetReceiver (std::bind (&I2PUDPClientTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, diff --git a/libi2pd_client/UDPTunnel.h b/libi2pd_client/UDPTunnel.h index 5650124c..7303aa9a 100644 --- a/libi2pd_client/UDPTunnel.h +++ b/libi2pd_client/UDPTunnel.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2024, The PurpleI2P Project +* Copyright (c) 2013-2025, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -130,7 +130,7 @@ namespace client I2PUDPClientTunnel (const std::string & name, const std::string &remoteDest, const boost::asio::ip::udp::endpoint& localEndpoint, std::shared_ptr localDestination, - uint16_t remotePort, bool gzip); + uint16_t remotePort, bool gzip, i2p::datagram::DatagramVersion datagramVersion); ~I2PUDPClientTunnel (); void Start (); @@ -175,6 +175,7 @@ namespace client uint16_t RemotePort, m_LastPort; bool m_cancel_resolve; bool m_Gzip; + i2p::datagram::DatagramVersion m_DatagramVersion; std::shared_ptr m_LastSession; public: