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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//! # rbit
//!
//! A comprehensive BitTorrent library implementing core BEP (BitTorrent Enhancement
//! Proposals) specifications in pure Rust.
//!
//! ## Overview
//!
//! `rbit` provides building blocks for creating BitTorrent applications, including:
//!
//! - **Torrent parsing** - Read `.torrent` files and magnet links
//! - **Peer communication** - Connect to and exchange data with peers
//! - **Tracker protocols** - Discover peers via HTTP and UDP trackers
//! - **DHT** - Trackerless peer discovery using a distributed hash table
//! - **Storage management** - Efficient disk I/O with piece verification
//! - **Caching** - Memory-efficient caching using the ARC algorithm
//!
//! ## Quick Start
//!
//! ### Parsing a torrent file
//!
//! ```no_run
//! use rbit::Metainfo;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let torrent_data = std::fs::read("example.torrent")?;
//! let metainfo = Metainfo::from_bytes(&torrent_data)?;
//!
//! println!("Name: {}", metainfo.info.name);
//! println!("Info hash: {}", metainfo.info_hash);
//! println!("Total size: {} bytes", metainfo.info.total_length);
//! println!("Piece count: {}", metainfo.info.piece_count());
//!
//! for tracker in metainfo.trackers() {
//! println!("Tracker: {}", tracker);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Parsing a magnet link
//!
//! ```
//! use rbit::MagnetLink;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let magnet = MagnetLink::parse(
//! "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn=Example"
//! )?;
//!
//! println!("Info hash: {}", magnet.info_hash);
//! println!("Display name: {:?}", magnet.display_name);
//! # Ok(())
//! # }
//! ```
//!
//! ### Connecting to a peer
//!
//! ```no_run
//! use rbit::{PeerConnection, PeerId, Message};
//! use std::net::SocketAddr;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let peer_addr: SocketAddr = "192.168.1.100:6881".parse()?;
//! let info_hash = [0u8; 20]; // Your torrent's info hash
//! let our_peer_id = PeerId::generate();
//!
//! let mut conn = PeerConnection::connect(
//! peer_addr,
//! info_hash,
//! *our_peer_id.as_bytes()
//! ).await?;
//!
//! // Express interest in downloading
//! conn.send(Message::Interested).await?;
//!
//! // Wait for unchoke before requesting pieces
//! loop {
//! match conn.receive().await? {
//! Message::Unchoke => break,
//! Message::Bitfield(bits) => println!("Peer has {} bytes of bitfield", bits.len()),
//! _ => {}
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Announcing to an HTTP tracker
//!
//! ```no_run
//! use rbit::{HttpTracker, TrackerEvent};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let tracker = HttpTracker::new("http://tracker.example.com/announce")?;
//!
//! let response = tracker.announce(
//! &[0u8; 20], // info_hash
//! &[0u8; 20], // peer_id
//! 6881, // port
//! 0, // uploaded
//! 0, // downloaded
//! 1000, // left
//! TrackerEvent::Started,
//! ).await?;
//!
//! println!("Found {} peers", response.peers.len());
//! println!("Re-announce in {} seconds", response.interval);
//! # Ok(())
//! # }
//! ```
//!
//! ### Using the DHT for trackerless peer discovery
//!
//! ```no_run
//! use rbit::DhtServer;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let dht = DhtServer::bind(6881).await?;
//!
//! // Bootstrap from well-known nodes
//! dht.bootstrap().await?;
//!
//! // Find peers for a specific info hash
//! let info_hash = [0u8; 20];
//! let peers = dht.get_peers(info_hash).await?;
//!
//! for peer in peers {
//! println!("Found peer: {}", peer);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Supported BEPs
//!
//! | BEP | Description | Module |
//! |-----|-------------|--------|
//! | [BEP-3](http://bittorrent.org/beps/bep_0003.html) | BitTorrent Protocol | [`peer`], [`metainfo`], [`tracker`] |
//! | [BEP-5](http://bittorrent.org/beps/bep_0005.html) | DHT Protocol | [`dht`] |
//! | [BEP-6](http://bittorrent.org/beps/bep_0006.html) | Fast Extension | [`peer`] |
//! | [BEP-9](http://bittorrent.org/beps/bep_0009.html) | Magnet Links | [`metainfo`] |
//! | [BEP-10](http://bittorrent.org/beps/bep_0010.html) | Extension Protocol | [`peer`] |
//! | [BEP-11](http://bittorrent.org/beps/bep_0011.html) | Peer Exchange (PEX) | [`pex`] |
//! | [BEP-14](http://bittorrent.org/beps/bep_0014.html) | Local Service Discovery | [`lsd`] |
//! | [BEP-15](http://bittorrent.org/beps/bep_0015.html) | UDP Tracker Protocol | [`tracker`] |
//! | [BEP-23](http://bittorrent.org/beps/bep_0023.html) | Compact Peer Lists | [`tracker`] |
//! | [BEP-52](http://bittorrent.org/beps/bep_0052.html) | BitTorrent v2 | [`metainfo`], [`storage`] |
//!
//! ## Module Overview
//!
//! - [`bencode`] - Bencode serialization format used throughout BitTorrent
//! - [`metainfo`] - Torrent file parsing, magnet links, and info hashes
//! - [`peer`] - Peer wire protocol for data exchange between clients
//! - [`tracker`] - HTTP and UDP tracker clients for peer discovery
//! - [`dht`] - Kademlia-based distributed hash table for trackerless operation
//! - [`pex`] - Peer Exchange for sharing peer lists between connected peers
//! - [`lsd`] - Local Service Discovery via multicast for LAN peers
//! - [`storage`] - Disk I/O management with piece verification
//! - [`cache`] - Memory caching for pieces and blocks using ARC
//!
//! ## Feature Highlights
//!
//! - **Async/await** - Built on tokio for efficient async I/O
//! - **Zero-copy** - Uses `bytes::Bytes` for efficient buffer handling
//! - **Memory-safe** - Pure Rust with no unsafe code in the public API
//! - **Concurrent** - Thread-safe primitives from `parking_lot` and `dashmap`
//!
//! ## Architecture Notes
//!
//! This library provides low-level building blocks rather than a complete
//! BitTorrent client. You are responsible for:
//!
//! - Coordinating peer connections and piece selection
//! - Managing download/upload state across peers
//! - Implementing rate limiting and choking algorithms
//! - Handling torrent lifecycle (start, pause, resume, remove)
//!
//! For a complete client implementation, you would combine these modules
//! with your own orchestration logic.
// Bandwidth limiting
pub use ;
// Bencode
pub use ;
// Caching
pub use ;
// DHT
pub use ;
// LSD
pub use ;
// Metainfo
pub use ;
// Peer
pub use ;
// PEX
pub use ;
// Storage
pub use ;
// Tracker
pub use ;
// UPnP
pub use ;
// WebSeed (BEP-19)
pub use ;