From 634ad8ce17c46d5ad36d49279e2bd3517fe351fb Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 20 Sep 2026 18:59:58 -0400 Subject: [PATCH] Added Node, Bucket, RoutingTable --- libi2pd_client/TorrentsDHT.cpp | 25 +++++++++++++----- libi2pd_client/TorrentsDHT.h | 47 +++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/libi2pd_client/TorrentsDHT.cpp b/libi2pd_client/TorrentsDHT.cpp index af5c6dcc..861241a9 100644 --- a/libi2pd_client/TorrentsDHT.cpp +++ b/libi2pd_client/TorrentsDHT.cpp @@ -17,22 +17,35 @@ namespace i2p { namespace torrents { + NodeDistance operator^(const NodeID& node1, const NodeID& node2) + { + NodeDistance d; + + d.l[0] = node1.l[0] ^ node2.l[0]; + d.l[1] = node1.l[1] ^ node2.l[1]; + d.l[2] = node1.l[2] ^ node2.l[2]; + d.l[3] = node1.l[3] ^ node2.l[3]; + d.l[4] = node1.l[4] ^ node2.l[4]; + + return d; + } + TorrentsDHT::TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port): m_Tunnel (tunnel), m_Port (port) { auto dest = tunnel.GetLocalDestination (); if (dest) { - memcpy (m_NodeID.data (), dest->GetIdentHash (), m_NodeID.size ()); - m_NodeID[4] ^= (port >> 8); - m_NodeID[5] ^= (port & 0xFF); - memcpy (m_NodeInfo.data (), m_NodeID.data (), m_NodeID.size ()); - memcpy (m_NodeInfo.data () + m_NodeID.size (), dest->GetIdentHash (), i2p::data::IdentHash::len); + memcpy (m_NodeID.buf, dest->GetIdentHash (), m_NodeID.len); + m_NodeID.buf[4] ^= (port >> 8); + m_NodeID.buf[5] ^= (port & 0xFF); + memcpy (m_NodeInfo.data (), m_NodeID.buf, m_NodeID.len); + memcpy (m_NodeInfo.data () + m_NodeID.len, dest->GetIdentHash (), i2p::data::IdentHash::len); htobe16buf (m_NodeInfo.data () + m_NodeInfo.size () - 2, port); } else { - m_NodeID.fill (0); + memset (m_NodeID.buf, 0, m_NodeID.len); m_NodeInfo.fill (0); } } diff --git a/libi2pd_client/TorrentsDHT.h b/libi2pd_client/TorrentsDHT.h index 9c2b7988..04f7dcee 100644 --- a/libi2pd_client/TorrentsDHT.h +++ b/libi2pd_client/TorrentsDHT.h @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include "Identity.h" #include "I2PService.h" #include "util.h" @@ -24,8 +24,48 @@ namespace i2p { namespace torrents { - using NodeID = std::array; - using NodeInfo = std::array::value + i2p::data::IdentHash::len + 2>; + struct NodeID + { + static constexpr size_t len = SHA_DIGEST_LENGTH; + + union // 4 bytes aligned + { + uint8_t buf[len]; + uint32_t l[len/4]; + }; + }; + + struct NodeDistance: public NodeID + { + auto operator<=>(const NodeDistance& other) const { return memcmp (buf, other.buf, len) <=> 0; } + }; + NodeDistance operator^(const NodeID& node1, const NodeID& node2); + + struct Node + { + NodeID id; + i2p::data::IdentHash peerInfo; + }; + + constexpr size_t MAX_BUCKET_CAPACITY = 8; + struct Bucket + { + std::list > nodes; + NodeID start; + }; + + class RoutingTable + { + public: + + RoutingTable () = default; + + private: + + std::list > m_Buckets; + }; + + using NodeInfo = std::array; class TorrentsTunnel; class TorrentsDHT @@ -51,6 +91,7 @@ namespace torrents uint16_t m_Port; NodeID m_NodeID; NodeInfo m_NodeInfo; // 20 byte Node ID + 32 byte IdentHash + 2 byte port + RoutingTable m_RoutingTable; }; } }