Back to Site
Loading...
Searching...
No Matches
librats::DhtClient Class Reference

DHT Kademlia implementation. More...

#include <dht.h>

Public Member Functions

 DhtClient (int port=DHT_PORT, const std::string &bind_address="", const std::string &data_directory="")
 Constructor.
 
 ~DhtClient ()
 Destructor.
 
bool start ()
 Start the DHT client.
 
void stop ()
 Stop the DHT client.
 
void shutdown_immediate ()
 Trigger immediate shutdown of all background threads.
 
bool bootstrap (const std::vector< Peer > &bootstrap_nodes)
 Bootstrap the DHT with known nodes.
 
bool find_peers (const InfoHash &info_hash, PeerDiscoveryCallback callback)
 Find peers for a specific info hash.
 
bool announce_peer (const InfoHash &info_hash, uint16_t port=0, PeerDiscoveryCallback callback=nullptr)
 Announce that this node is a peer for a specific info hash.
 
void cancel_search (const InfoHash &info_hash)
 Cancel an active search or announce for a specific info hash Should be called when a torrent is removed to clean up DHT state.
 
const NodeIdget_node_id () const
 Get our node ID.
 
size_t get_routing_table_size () const
 Get number of nodes in routing table.
 
size_t get_pending_ping_verifications_count () const
 Get number of pending ping verifications.
 
bool is_search_active (const InfoHash &info_hash) const
 Check if a search is currently active for an info hash.
 
bool is_announce_active (const InfoHash &info_hash) const
 Check if an announce is currently active for an info hash.
 
size_t get_active_searches_count () const
 Get number of active searches.
 
size_t get_active_announces_count () const
 Get number of active announces.
 
bool is_running () const
 Check if DHT is running.
 
void set_spider_mode (bool enable)
 Enable spider mode In spider mode:
 
bool is_spider_mode () const
 Check if spider mode is enabled.
 
void set_spider_announce_callback (SpiderAnnounceCallback callback)
 Set callback for announce_peer requests (spider mode) Called when other peers announce they have a torrent.
 
void set_spider_ignore (bool ignore)
 Set spider ignore mode - when true, incoming requests are not processed Similar to rate limiting in the original spider implementation.
 
bool is_spider_ignoring () const
 Check if spider ignore mode is enabled.
 
void spider_walk ()
 Trigger a single spider walk iteration Sends find_node to a random node from the spider pool Should be called from external loop at desired frequency.
 
size_t get_spider_pool_size () const
 Get the size of the spider node pool.
 
size_t get_spider_visited_count () const
 Get the number of visited nodes in spider mode.
 
void clear_spider_state ()
 Clear spider state (pool and visited nodes) Useful for resetting the spider walk.
 
bool save_routing_table ()
 Save routing table to disk.
 
bool load_routing_table ()
 Load routing table from disk.
 
void set_data_directory (const std::string &directory)
 Set data directory for persistence.
 

Static Public Member Functions

static std::vector< Peerget_default_bootstrap_nodes ()
 Get default BitTorrent DHT bootstrap nodes.
 

Detailed Description

DHT Kademlia implementation.

Definition at line 162 of file dht.h.

Constructor & Destructor Documentation

◆ DhtClient()

librats::DhtClient::DhtClient ( int  port = DHT_PORT,
const std::string &  bind_address = "",
const std::string &  data_directory = "" 
)

Constructor.

Parameters
portThe UDP port to bind to (default: 6881)
bind_addressThe interface IP address to bind to (empty for all interfaces)

◆ ~DhtClient()

librats::DhtClient::~DhtClient ( )

Destructor.

Member Function Documentation

◆ announce_peer()

bool librats::DhtClient::announce_peer ( const InfoHash info_hash,
uint16_t  port = 0,
PeerDiscoveryCallback  callback = nullptr 
)

Announce that this node is a peer for a specific info hash.

Parameters
info_hashThe info hash to announce
portThe port to announce (0 for DHT port)
callbackOptional callback to receive discovered peers during traversal
Returns
true if announcement started successfully, false otherwise

◆ bootstrap()

bool librats::DhtClient::bootstrap ( const std::vector< Peer > &  bootstrap_nodes)

Bootstrap the DHT with known nodes.

Parameters
bootstrap_nodesVector of bootstrap nodes
Returns
true if successful, false otherwise

◆ cancel_search()

void librats::DhtClient::cancel_search ( const InfoHash info_hash)

Cancel an active search or announce for a specific info hash Should be called when a torrent is removed to clean up DHT state.

Parameters
info_hashThe info hash to cancel search for

◆ clear_spider_state()

void librats::DhtClient::clear_spider_state ( )

Clear spider state (pool and visited nodes) Useful for resetting the spider walk.

◆ find_peers()

bool librats::DhtClient::find_peers ( const InfoHash info_hash,
PeerDiscoveryCallback  callback 
)

Find peers for a specific info hash.

Parameters
info_hashThe info hash to search for
callbackCallback to receive discovered peers
Returns
true if search started successfully, false otherwise

◆ get_active_announces_count()

size_t librats::DhtClient::get_active_announces_count ( ) const

Get number of active announces.

Returns
Number of active announces

◆ get_active_searches_count()

size_t librats::DhtClient::get_active_searches_count ( ) const

Get number of active searches.

Returns
Number of active searches

◆ get_default_bootstrap_nodes()

static std::vector< Peer > librats::DhtClient::get_default_bootstrap_nodes ( )
static

Get default BitTorrent DHT bootstrap nodes.

Returns
Vector of bootstrap nodes

◆ get_node_id()

const NodeId & librats::DhtClient::get_node_id ( ) const
inline

Get our node ID.

Returns
The node ID

Definition at line 227 of file dht.h.

◆ get_pending_ping_verifications_count()

size_t librats::DhtClient::get_pending_ping_verifications_count ( ) const

Get number of pending ping verifications.

Returns
Number of pending ping verifications

◆ get_routing_table_size()

size_t librats::DhtClient::get_routing_table_size ( ) const

Get number of nodes in routing table.

Returns
Number of nodes

◆ get_spider_pool_size()

size_t librats::DhtClient::get_spider_pool_size ( ) const

Get the size of the spider node pool.

Returns
Number of nodes in spider pool

◆ get_spider_visited_count()

size_t librats::DhtClient::get_spider_visited_count ( ) const

Get the number of visited nodes in spider mode.

Returns
Number of visited nodes

◆ is_announce_active()

bool librats::DhtClient::is_announce_active ( const InfoHash info_hash) const

Check if an announce is currently active for an info hash.

Parameters
info_hashThe info hash to check
Returns
true if announce is active, false otherwise

◆ is_running()

bool librats::DhtClient::is_running ( ) const
inline

Check if DHT is running.

Returns
true if running, false otherwise

Definition at line 271 of file dht.h.

◆ is_search_active()

bool librats::DhtClient::is_search_active ( const InfoHash info_hash) const

Check if a search is currently active for an info hash.

Parameters
info_hashThe info hash to check
Returns
true if search is active, false otherwise

◆ is_spider_ignoring()

bool librats::DhtClient::is_spider_ignoring ( ) const
inline

Check if spider ignore mode is enabled.

Returns
true if ignoring incoming requests

Definition at line 311 of file dht.h.

◆ is_spider_mode()

bool librats::DhtClient::is_spider_mode ( ) const
inline

Check if spider mode is enabled.

Returns
true if spider mode is enabled

Definition at line 291 of file dht.h.

◆ load_routing_table()

bool librats::DhtClient::load_routing_table ( )

Load routing table from disk.

Returns
true if successful, false otherwise

◆ save_routing_table()

bool librats::DhtClient::save_routing_table ( )

Save routing table to disk.

Returns
true if successful, false otherwise

◆ set_data_directory()

void librats::DhtClient::set_data_directory ( const std::string &  directory)

Set data directory for persistence.

Parameters
directoryDirectory path

◆ set_spider_announce_callback()

void librats::DhtClient::set_spider_announce_callback ( SpiderAnnounceCallback  callback)

Set callback for announce_peer requests (spider mode) Called when other peers announce they have a torrent.

Parameters
callbackThe callback to invoke

◆ set_spider_ignore()

void librats::DhtClient::set_spider_ignore ( bool  ignore)

Set spider ignore mode - when true, incoming requests are not processed Similar to rate limiting in the original spider implementation.

Parameters
ignoretrue to ignore incoming requests, false to process them

◆ set_spider_mode()

void librats::DhtClient::set_spider_mode ( bool  enable)

Enable spider mode In spider mode:

  • Nodes are added to routing table without ping verification
  • All announce_peer requests from other peers are collected via callback
    Parameters
    enabletrue to enable spider mode, false to disable

◆ shutdown_immediate()

void librats::DhtClient::shutdown_immediate ( )

Trigger immediate shutdown of all background threads.

◆ spider_walk()

void librats::DhtClient::spider_walk ( )

Trigger a single spider walk iteration Sends find_node to a random node from the spider pool Should be called from external loop at desired frequency.

◆ start()

bool librats::DhtClient::start ( )

Start the DHT client.

Returns
true if successful, false otherwise

◆ stop()

void librats::DhtClient::stop ( )

Stop the DHT client.


The documentation for this class was generated from the following file: