From 6cfb14b422fdff0c2527eb613614efa5d7887394 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 26 Sep 2026 22:05:17 -0400 Subject: [PATCH] correct local NodeID generation. Handle find_node query --- libi2pd_client/TorrentsDHT.cpp | 22 ++++++++++++++++++---- libi2pd_client/TorrentsDHT.h | 2 ++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/libi2pd_client/TorrentsDHT.cpp b/libi2pd_client/TorrentsDHT.cpp index dbf631a5..8c532899 100644 --- a/libi2pd_client/TorrentsDHT.cpp +++ b/libi2pd_client/TorrentsDHT.cpp @@ -228,7 +228,7 @@ namespace torrents auto dest = tunnel.GetLocalDestination (); if (dest) { - memcpy (m_NodeID.data (), dest->GetIdentHash (), 4); + memcpy (m_NodeID.data (), dest->GetIdentHash (), 6); m_NodeID[4] ^= (port >> 8); m_NodeID[5] ^= (port & 0xFF); RAND_bytes (m_NodeID.data () + 6, m_NodeID.size () - 6); @@ -450,9 +450,9 @@ namespace torrents char type = 0; bool isMalformed = false; std::string transactionID, query; - NodeID id; Torrent::InfoHash infoHash; + NodeID id, target; Torrent::InfoHash infoHash; ParseDictionary (std::string_view ((const char *)buf, len), - [&type, &transactionID, &query, &id, &infoHash, &isMalformed] + [&type, &transactionID, &query, &id, &infoHash, &isMalformed, &target] (std::string_view key, std::string_view buf)->size_t { if (key == "y") @@ -476,7 +476,7 @@ namespace torrents else if (key == "a") { return ParseDictionary (buf, - [&id, &infoHash,&isMalformed](std::string_view key, std::string_view buf)->size_t + [&id, &infoHash, &isMalformed, &target](std::string_view key, std::string_view buf)->size_t { if (key == "id") { @@ -490,6 +490,12 @@ namespace torrents if (!success) isMalformed = true; return l; } + else if (key == "target") + { + auto [l, success] = ParseByteArray (buf, target); + if (!success) isMalformed = true; + return l; + } return 0; }); } @@ -506,6 +512,8 @@ namespace torrents HandlePingQuery (from.GetIdentHash (), fromPort, transactionID, id); else if (query == "get_peers") HandleGetPeersQuery (from.GetIdentHash (), fromPort, transactionID, id, infoHash); + else if (query == "find_node") + HandleFindNodeQuery (from.GetIdentHash (), fromPort, transactionID, target); else LogPrint (eLogDebug, "TorrentsDHT: Unexpected query ", query); } @@ -556,6 +564,12 @@ namespace torrents } } + void TorrentsDHT::HandleFindNodeQuery (const i2p::data::IdentHash& fromIdent, uint16_t fromPort, + std::string_view transactionID, const NodeID& target) + { + LogPrint (eLogDebug, "TorrentsDHT: Find node received"); + } + void TorrentsDHT::HandleAnnouncePeer (std::string_view transactionID, const Torrent::InfoHash& infoHash, uint64_t token) { LogPrint (eLogDebug, "TorrentsDHT: Announce peer received"); diff --git a/libi2pd_client/TorrentsDHT.h b/libi2pd_client/TorrentsDHT.h index d09747ce..156d5220 100644 --- a/libi2pd_client/TorrentsDHT.h +++ b/libi2pd_client/TorrentsDHT.h @@ -157,6 +157,8 @@ namespace torrents std::string_view transactionID, const NodeID& nodeID); void HandleGetPeersQuery (const i2p::data::IdentHash& fromIdent, uint16_t fromPort, std::string_view transactionID, const NodeID& nodeID, const Torrent::InfoHash& infoHash); + void HandleFindNodeQuery (const i2p::data::IdentHash& fromIdent, uint16_t fromPort, + std::string_view transactionID, const NodeID& target); void HandleResponse (std::string_view transactionID, const NodeID& nodeID, uint64_t token, const std::vector& values, const NodeInfo& nodeInfo); void HandleAnnouncePeer (std::string_view transactionID, const Torrent::InfoHash& infoHash, uint64_t token);