From ca007dc57dad3ea743e018ed7f652a8671ec8bed Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 9 Oct 2026 22:24:25 -0400 Subject: [PATCH] store incoming tokens for follow on announce_peer --- libi2pd_client/TorrentsDHT.cpp | 29 ++++++++++++++++++++++++++++- libi2pd_client/TorrentsDHT.h | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/libi2pd_client/TorrentsDHT.cpp b/libi2pd_client/TorrentsDHT.cpp index e205b72b..358fd2a1 100644 --- a/libi2pd_client/TorrentsDHT.cpp +++ b/libi2pd_client/TorrentsDHT.cpp @@ -719,6 +719,7 @@ namespace torrents } uint64_t token = m_Tunnel.GetLocalDestination () ? m_Tunnel.GetLocalDestination ()->GetRng ()() : 1; torrent->AddIncomingGetPeerNode (token, from); + m_IncomingTokens.emplace (token, std::make_pair(from, i2p::util::GetMonotonicSeconds ())); if (torrent->HasPeers ()) SendGetPeersResponse (transactionID, torrent, token, fromIdent, fromPort + 1); // to rport @@ -788,7 +789,23 @@ namespace torrents LogPrint (eLogInfo, "TorrentsDHT: Announce peer token not found"); } else - LogPrint (eLogInfo, "TorrentsDHT: Torrent for announce not found"); + { + auto it = m_IncomingTokens.find (token); + if (it != m_IncomingTokens.end ()) + { + auto node = it->second.first; + auto torrent = std::make_shared(); + torrent->AddIncomingGetPeerNode (token, node); + torrent->AddPeer (node->peer); + m_Torrents.emplace (infoHash, torrent); + SendResponseMsg (CreateDictionary ({ + { "id", CreateByteString (std::string_view ((const char *)m_NodeID.data (), m_NodeID.size ())) }, + }), + transactionID, node->peer, node->port + 1); // to rport + } + else + LogPrint (eLogInfo, "TorrentsDHT: Token for announce not found"); + } } void TorrentsDHT::HandleResponse (std::string_view transactionID, const NodeID& nodeID, @@ -1181,6 +1198,16 @@ namespace torrents it++; } } + { + auto it = m_IncomingTokens.begin (); + while (it != m_IncomingTokens.end ()) + { + if (ts > it->second.second + DHT_INCOMING_GET_PEERS_TOKEN_EXPIRATION_TIME) + it = m_IncomingTokens.erase (it); + else + it++; + } + } ScheduleDHTExpirationCheck (); } } diff --git a/libi2pd_client/TorrentsDHT.h b/libi2pd_client/TorrentsDHT.h index 7d6f9656..386c8be2 100644 --- a/libi2pd_client/TorrentsDHT.h +++ b/libi2pd_client/TorrentsDHT.h @@ -304,6 +304,7 @@ namespace torrents NodeID m_NodeID; std::unique_ptr m_RoutingTable; std::map m_HeardFrom; // nodeid -> time in monotonic seconds + std::unordered_map, uint64_t> > m_IncomingTokens; // token ->(node, time in monotonic second) // transactionID -> (ident, port, query, request info, time in monotonic seconds) std::unordered_map, uint64_t > > m_Queries; std::map > m_Torrents;