1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! Distributed Hash Table ([BEP-5]).
//!
//! This module implements the Kademlia-based DHT used by BitTorrent for
//! trackerless peer discovery. The DHT allows finding peers without relying
//! on centralized trackers.
//!
//! # Overview
//!
//! The BitTorrent DHT is a distributed database of peers for each torrent.
//! Nodes are identified by 160-bit node IDs, and torrents by their 20-byte
//! info hash. The DHT uses the Kademlia protocol with XOR distance metric.
//!
//! # Getting Started
//!
//! ```no_run
//! use rbit::dht::DhtServer;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Bind to a UDP port
//! let dht = DhtServer::bind(6881).await?;
//!
//! // Bootstrap from known nodes
//! dht.bootstrap().await?;
//!
//! // Find peers for a torrent
//! let info_hash = [0u8; 20]; // Your torrent's info hash
//! let peers = dht.get_peers(info_hash).await?;
//!
//! for peer in peers {
//! println!("Found peer: {}", peer);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # DHT Operations
//!
//! ## Queries
//!
//! - **ping** - Check if a node is alive
//! - **find_node** - Find nodes close to a target ID
//! - **get_peers** - Find peers for an info hash
//! - **announce_peer** - Announce that we have a torrent
//!
//! ## Bootstrap Nodes
//!
//! The DHT starts empty and needs to bootstrap from known nodes.
//! Default bootstrap nodes are provided:
//!
//! - `router.bittorrent.com:6881`
//! - `dht.transmissionbt.com:6881`
//! - `router.utorrent.com:6881`
//!
//! # Architecture
//!
//! The DHT uses several components:
//!
//! - [`DhtServer`] - Main server handling UDP communication
//! - [`RoutingTable`] - K-bucket storage for known nodes
//! - [`NodeId`] - 160-bit node identifier
//! - [`Node`] - A known DHT node (ID + address)
//!
//! # Routing Table
//!
//! The routing table uses 160 k-buckets (one per bit of distance from our ID).
//! Each bucket holds up to 8 nodes. Nodes are categorized as:
//!
//! - **Good** - Recently seen, no failures
//! - **Questionable** - Not seen recently but no failures
//! - **Bad** - Multiple consecutive failures
//!
//! [BEP-5]: http://bittorrent.org/beps/bep_0005.html
pub use DhtError;
pub use ;
pub use ;
pub use ;
pub use RoutingTable;
pub use ;