correct local NodeID generation. Handle find_node query

This commit is contained in:
orignal
2026-09-26 22:05:17 -04:00
parent a8b4000490
commit 6cfb14b422
2 changed files with 20 additions and 4 deletions
+18 -4
View File
@@ -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");
+2
View File
@@ -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<std::string_view>& values, const NodeInfo& nodeInfo);
void HandleAnnouncePeer (std::string_view transactionID, const Torrent::InfoHash& infoHash, uint64_t token);