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
//! Peer wire protocol ([BEP-3], [BEP-6], [BEP-10]).
//!
//! This module implements the BitTorrent peer wire protocol for exchanging
//! data between clients, including support for the Fast Extension and
//! Extension Protocol.
//!
//! # Overview
//!
//! The peer wire protocol operates over TCP connections. After connecting,
//! peers exchange a handshake, then communicate using length-prefixed messages.
//!
//! ## Connection Lifecycle
//!
//! 1. **Handshake** - Exchange protocol identifier, info hash, and peer ID
//! 2. **Bitfield** - (Optional) Announce which pieces the peer has
//! 3. **Interest/Choking** - Negotiate data transfer readiness
//! 4. **Data Transfer** - Request and receive piece data blocks
//!
//! # Examples
//!
//! ## Connecting to a peer
//!
//! ```no_run
//! use rbit::peer::{PeerConnection, PeerId, Message};
//! use std::net::SocketAddr;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let addr: SocketAddr = "192.168.1.100:6881".parse()?;
//! let info_hash = [0u8; 20];
//! let peer_id = PeerId::generate();
//!
//! let mut conn = PeerConnection::connect(addr, info_hash, *peer_id.as_bytes()).await?;
//!
//! // Send interest
//! conn.send(Message::Interested).await?;
//!
//! // Receive messages
//! loop {
//! match conn.receive().await? {
//! Message::Unchoke => {
//! println!("Peer unchoked us, can request pieces");
//! break;
//! }
//! Message::Bitfield(bits) => {
//! println!("Peer has bitfield of {} bytes", bits.len());
//! }
//! Message::Have { piece } => {
//! println!("Peer has piece {}", piece);
//! }
//! _ => {}
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Requesting a block
//!
//! ```no_run
//! use rbit::peer::{PeerConnection, Message};
//! # use std::net::SocketAddr;
//!
//! # async fn example(conn: &mut PeerConnection) -> Result<(), Box<dyn std::error::Error>> {
//! // Request block: piece 0, offset 0, 16KB
//! conn.send(Message::Request {
//! index: 0,
//! begin: 0,
//! length: 16384,
//! }).await?;
//!
//! // Receive the piece data
//! if let Message::Piece { index, begin, data } = conn.receive().await? {
//! println!("Received {} bytes for piece {} at offset {}", data.len(), index, begin);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Message Types
//!
//! | Message | ID | Description |
//! |---------|----|-------------|
//! | KeepAlive | - | Empty message to maintain connection |
//! | Choke | 0 | Stop sending requests |
//! | Unchoke | 1 | Ready to receive requests |
//! | Interested | 2 | Want to download from peer |
//! | NotInterested | 3 | Don't want to download |
//! | Have | 4 | Announce a new piece |
//! | Bitfield | 5 | Announce all available pieces |
//! | Request | 6 | Request a data block |
//! | Piece | 7 | Send piece data |
//! | Cancel | 8 | Cancel a pending request |
//! | Port | 9 | DHT port announcement |
//!
//! ## Fast Extension ([BEP-6])
//!
//! | Message | ID | Description |
//! |---------|----|-------------|
//! | Suggest | 13 | Suggest a piece to download |
//! | HaveAll | 14 | Peer has all pieces (seeder) |
//! | HaveNone | 15 | Peer has no pieces |
//! | Reject | 16 | Reject a block request |
//! | AllowedFast | 17 | Allow downloading while choked |
//!
//! ## Extension Protocol ([BEP-10])
//!
//! | Message | ID | Description |
//! |---------|----|-------------|
//! | Extended | 20 | Extension message with sub-ID |
//!
//! # Choking Algorithm
//!
//! BitTorrent uses a 4-way handshake for data transfer:
//!
//! - **am_choking** - Are we choking the peer (not sending data)?
//! - **am_interested** - Are we interested in the peer's data?
//! - **peer_choking** - Is the peer choking us?
//! - **peer_interested** - Is the peer interested in our data?
//!
//! Data transfer requires: `!peer_choking && am_interested`
//!
//! [BEP-3]: http://bittorrent.org/beps/bep_0003.html
//! [BEP-6]: http://bittorrent.org/beps/bep_0006.html
//! [BEP-10]: http://bittorrent.org/beps/bep_0010.html
pub use Bitfield;
pub use ;
pub use ;
pub use PeerError;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use PeerId;
pub use ;
pub use PeerTransport;