Added Node, Bucket, RoutingTable

This commit is contained in:
orignal committed 2026-09-20 18:59:58 -04:00
1 parent 9dbbe9629c
commit 634ad8ce17
2 files changed
+63 -9

No files matched your search

+19 -6
View File
@@ -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);
}
}
+44 -3
View File
@@ -15,7 +15,7 @@
#include <openssl/sha.h>
#include <memory>
#include <array>
#include <utility>
#include <list>
#include "Identity.h"
#include "I2PService.h"
#include "util.h"
@@ -24,8 +24,48 @@ namespace i2p
{
namespace torrents
{
using NodeID = std::array<uint8_t, SHA_DIGEST_LENGTH>;
using NodeInfo = std::array<uint8_t, std::tuple_size<NodeID>::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<std::shared_ptr<Node> > nodes;
NodeID start;
};
class RoutingTable
{
public:
RoutingTable () = default;
private:
std::list<std::shared_ptr<Bucket> > m_Buckets;
};
using NodeInfo = std::array<uint8_t, NodeID::len + i2p::data::IdentHash::len + 2>;
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;
};
}
}