Files
i2pd/libi2pd_client/TorrentsDHT.h
T

227 lines
7.8 KiB
C++

/*
* Copyright (c) 2026, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#ifndef TORRENTS_DHT_H__
#define TORRENTS_DHT_H__
#ifndef NO_TORRENTS
#include <inttypes.h>
#include <openssl/sha.h>
#include <string>
#include <string_view>
#include <tuple>
#include <memory>
#include <array>
#include <list>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <optional>
#include <filesystem>
#include <boost/asio.hpp>
#include "Identity.h"
#include "I2PService.h"
#include "util.h"
#include "Torrents.h"
namespace i2p
{
namespace torrents
{
constexpr int DHT_UPDATE_CHECK_INTERVAL = 24; // in seconds
constexpr int DHT_EXPIRATION_CHECK_INTERVAL = 73; // in seconds
constexpr int DHT_EXPLORATORY_INTERVAL = 4*60; // in seconds
constexpr int DHT_EXPLORATORY_INTERVAL_VARIANCE = 40; // in seconds
constexpr int DHT_INITIAL_EXPLORATORY_INTERVAL = 90; // in seconds
constexpr int DHT_NODE_EXPIRATION_TIME = 855; // in seconds
constexpr int DHT_BUCKET_EXPIRATION_THRESHOLD = 290; // in seconds
using Distance = Torrent::InfoHash;
struct NodeID: public Torrent::InfoHash
{
static constexpr size_t len = std::tuple_size<Torrent::InfoHash>::value;
constexpr Distance operator^(const Torrent::InfoHash& hash) const
{
Distance d;
for (size_t i = 0; i < size (); i++)
d[i] = (*this)[i] ^ hash[i];
return d;
}
constexpr int FindLowestBit () const // -1 in not found
{
for (int i = size () - 1; i >= 0; i--)
{
uint8_t byte = (*this)[i];
if (byte)
{
for (int j = 7; j >= 0; j--)
if (byte & (0x80 >> j))
return i*8 + j;
}
}
return -1;
}
};
using NodeInfo = std::array<uint8_t, NodeID::len + i2p::data::IdentHash::len + 2>;
struct Node
{
NodeID id;
i2p::data::IdentHash peer;
uint16_t port;
Node (const NodeID& id1, const i2p::data::IdentHash& peer1, uint16_t port1):
id (id1), peer (peer1), port (port1) {}
Node (const NodeInfo& nodeInfo);
NodeInfo GetNodeInfo () const;
};
constexpr size_t MAX_BUCKET_CAPACITY = 8;
struct Bucket
{
Bucket * next;
std::map<NodeID, uint64_t> nodes; // nodeID->update time in monotonic seconds
NodeID start;
uint64_t lastUpdateTime = 0; // monotonic seconds
Bucket (): next (nullptr), start{} {}
Bucket (const NodeID& start1): next (nullptr), start (start1) {}
bool IsFull () const { return nodes.size () >= MAX_BUCKET_CAPACITY; }
bool IsInBucket (const NodeID& id) const { return id >= start && (!next || id < next->start); }
bool ContainsNode (const NodeID& id) const;
void UpdateNode (const NodeID& id);
std::optional<NodeID> GetMiddleID () const;
bool Split ();
};
class RoutingTable
{
public:
RoutingTable (const NodeID& ourNode);
~RoutingTable ();
void CleanUp ();
size_t GetNumBuckets () const;
bool AddNode (const NodeID& id);
std::list<std::pair<NodeID, Distance> > FindClosestNodes (const Torrent::InfoHash& infoHash, size_t num = 1) const;
std::optional<NodeID> FindClosestNode (const Torrent::InfoHash& infoHash) const;
std::list<std::pair<NodeID, NodeID> > GetExploratoryTargets () const; // (target, node to send find_node to)
NodeID FindClosestNodeInBucket (const NodeID& target) const;
std::list<NodeID> DeleteExpiredNodes ();
private:
Bucket * FindBucket (const Torrent::InfoHash& id) const;
void RemoveEmptyBuckets ();
private:
Bucket * m_Buckets;
NodeID m_OurNode;
};
using GetPeersToken = uint64_t;
class DHTTorrent
{
public:
DHTTorrent () = default;
std::string GetBEncodedPeers () const;
void AddIncomingGetPeerNode (GetPeersToken token, std::shared_ptr<Node> node);
void AddOutgoingGetPeerNode (GetPeersToken token, std::shared_ptr<Node> node);
std::shared_ptr<Node> GetIncomingGetPeerNode (GetPeersToken token) const;
void AddPeer (const i2p::data::IdentHash& peer);
private:
std::list<std::pair<i2p::data::IdentHash, uint64_t> > m_Peers; // (ident, update time in monotonic seconds)
std::unordered_map<GetPeersToken, std::weak_ptr<Node> > m_IncomingGetPeers; // they request peers and send announces to us
std::unordered_map<GetPeersToken, std::weak_ptr<Node> > m_OutgoingGetPeers; // we request peers from and send announces to
};
class TorrentsTunnel;
class TorrentsDHT
{
public:
TorrentsDHT (TorrentsTunnel& tunnel, uint16_t port);
void Start ();
void Stop ();
uint16_t GetPort () const { return m_Port; };
uint16_t GetRPort () const { return m_Port + 1; };
void HandleRawDatagram (const uint8_t * buf, size_t len);
void SendPingQuery (const i2p::data::IdentHash& toIdent, uint16_t toPort);
private:
void HandleDatagram (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort,
const uint8_t * buf, size_t len, const i2p::util::Mapping * options);
void HandlePingQuery (const i2p::data::IdentHash& fromIdent, uint16_t fromPort,
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);
void SendDatagram (std::string_view msg, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendRawDatagram (std::string_view msg, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendQueryMsg (std::string_view query, std::string_view arguments,
const i2p::data::IdentHash& toIdent, uint16_t toPort, bool isRaw = false);
void SendFindNodeQuery (const NodeID& target, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendResponseMsg (std::string_view response, std::string_view transactionID, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendPingResponse (std::string_view transactionID, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendGetPeersResponse (std::string_view transactionID, std::shared_ptr<DHTTorrent> torrent,
uint64_t token, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendGetPeersResponse (std::string_view transactionID, std::shared_ptr<const Node> node,
uint64_t token, const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendFindNodeResponse (std::string_view transactionID, std::shared_ptr<const Node> node,
const i2p::data::IdentHash& toIdent, uint16_t toPort);
void SendAnnouncePeerQuery (const Torrent::InfoHash& infoHash, uint64_t token, const i2p::data::IdentHash& toIdent, uint16_t toPort);
std::filesystem::path GetDHTFilePath (std::string_view filename) const;
void Save (const std::filesystem::path& file);
void Load (const std::filesystem::path& file);
void Explore ();
void ScheduleDHTUpdateCheck ();
void HandleDHTUpdateCheckTimer (const boost::system::error_code& ecode);
void ScheduleDHTExpirationCheck ();
void HandleDHTExpirationCheckTimer (const boost::system::error_code& ecode);
private:
TorrentsTunnel& m_Tunnel;
boost::asio::steady_timer m_DHTUpdateCheckTimer, m_DHTExpirationCheckTimer;
uint16_t m_Port;
NodeID m_NodeID;
std::unique_ptr<RoutingTable> m_RoutingTable;
std::map<NodeID, std::shared_ptr<Node> > m_Nodes;
std::unordered_map<uint16_t, std::tuple<i2p::data::IdentHash, uint16_t, std::string, std::weak_ptr<Torrent> > > m_Queries;
std::map<Torrent::InfoHash, std::shared_ptr<DHTTorrent> > m_Torrents;
uint64_t m_NextDHTExploratoryTime; // monotonic seconds
};
}
}
#endif
#endif