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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! BT Peer Interaction Manager - Peer connection and initialization
//!
//! This module manages the interaction with BitTorrent peers, including:
//! - Connection establishment (plain and encrypted)
//! - Initial handshake and bitfield exchange
//! - Waiting for unchoke messages
//! - Peer statistics tracking
//!
//! # Architecture Reference
//!
//! Based on original aria2 C++ structure:
//! - `src/PeerConnection.cc/h` - Peer connection management
//! - `src/PeerInteractionCommand.h` - Interaction logic
//! - `src/BtSetup.cc/h` - BT setup and initialization
use std::time::Duration;
use crate::constants;
use crate::engine::bt_peer_connection::BtPeerConn;
use crate::error::{Aria2Error, RecoverableError, Result};
use tracing::{debug, error, info, warn};
/// Delay between peer connection setup and message reading (milliseconds)
pub const PEER_CONNECTION_DELAY_MS: u64 = constants::BT_PEER_CONNECTION_DELAY_MS;
/// Maximum attempts to wait for unchoke from a peer
pub const MAX_UNCHOKE_WAIT_ATTEMPTS: u32 = constants::BT_MAX_UNCHOKE_WAIT_ATTEMPTS as u32;
/// Timeout for each message read from peer (seconds)
pub const PEER_MESSAGE_TIMEOUT_SECS: u64 = constants::BT_PEER_MESSAGE_TIMEOUT_SECS;
/// Result of peer connection attempt
pub struct PeerConnectionResult {
/// Successfully connected peers
pub connections: Vec<BtPeerConn>,
/// Number of failed connections
pub failed_count: usize,
}
/// BT Peer Interaction Manager
///
/// Handles the lifecycle of peer connections from initial connection
/// through the handshake phase until they're ready for data transfer.
pub struct BtPeerInteraction;
impl BtPeerInteraction {
/// Connect to multiple peers with automatic fallback strategies
///
/// Attempts to connect to all provided peer addresses using:
/// 1. MSE encryption if required or forced
/// 2. Plain connection as fallback
///
/// For each successful connection:
/// - Sends initial unchoke and interested messages
/// - Exchanges bitfields
/// - Waits for unchoke from the peer
///
/// # Arguments
/// * `peer_addrs` - List of peer addresses to connect to
/// * `info_hash_raw` - Torrent info hash for handshake
/// * `num_pieces` - Total number of pieces (for bitfield size)
/// * `require_crypto` - Whether to require encrypted connections
/// * `force_encrypt` - Whether to force encryption (fallback to plain)
///
/// # Returns
/// * `PeerConnectionResult` containing connected peers and failure count
pub async fn connect_to_peers(
peer_addrs: &[aria2_protocol::bittorrent::peer::connection::PeerAddr],
info_hash_raw: &[u8; 20],
num_pieces: u32,
require_crypto: bool,
force_encrypt: bool,
) -> Result<PeerConnectionResult> {
info!("[BT] Connecting to {} peers...", peer_addrs.len());
let mut active_connections: Vec<BtPeerConn> = Vec::new();
let mut failed_count = 0usize;
for addr in peer_addrs {
debug!("[BT] Connecting to peer {}:{}", addr.ip, addr.port);
let conn_result =
Self::connect_single_peer(addr, info_hash_raw, require_crypto, force_encrypt).await;
match conn_result {
Ok(mut conn) => {
info!(
"[BT] Connected to peer {}:{} (encrypted={})",
addr.ip,
addr.port,
conn.is_encrypted()
);
// Initialize the connection
if let Err(e) = Self::initialize_connection(&mut conn, num_pieces).await {
warn!("[BT] Failed to initialize peer {}: {}", addr.ip, e);
failed_count += 1;
continue;
}
// Wait for unchoke
match Self::wait_for_unchoke(&mut conn, addr).await {
Ok(()) => {
active_connections.push(conn);
}
Err(e) => {
warn!("[BT] No unchoke from peer {}: {}", addr.ip, e);
// Still add the connection even without unchoke
// (it might unchoke later)
active_connections.push(conn);
}
}
}
Err(e) => {
error!("[BT] Failed to connect peer {}: {}", addr.ip, e);
failed_count += 1;
continue;
}
}
}
info!("[BT] Active connections: {}", active_connections.len());
if active_connections.is_empty() {
return Err(Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure {
message: "All peer connections failed".into(),
},
));
}
Ok(PeerConnectionResult {
connections: active_connections,
failed_count,
})
}
/// Connect to a single peer with encryption fallback logic
async fn connect_single_peer(
addr: &aria2_protocol::bittorrent::peer::connection::PeerAddr,
info_hash_raw: &[u8; 20],
require_crypto: bool,
force_encrypt: bool,
) -> Result<BtPeerConn> {
if force_encrypt || require_crypto {
// Try MSE encrypted connection
BtPeerConn::connect_mse(addr, info_hash_raw, require_crypto).await
} else {
// Try MSE first, fall back to plain
match BtPeerConn::connect_mse(addr, info_hash_raw, false).await {
Ok(conn) => Ok(conn),
Err(_) => {
debug!("[BT] MSE failed, trying plain connection");
BtPeerConn::connect_plain(addr, info_hash_raw).await
}
}
}
}
/// Initialize a newly established connection
///
/// Sends initial protocol messages:
/// - Unchoke (we allow them to request from us)
/// - Interested (we want to download from them)
/// - Bitfield (our current piece possession status)
async fn initialize_connection(conn: &mut BtPeerConn, num_pieces: u32) -> Result<()> {
// Send initial messages
conn.send_unchoke().await?;
conn.send_interested().await?;
// Send empty bitfield (we have nothing yet)
let bf_len = (num_pieces as usize).div_ceil(8);
let empty_bf = vec![0u8; bf_len];
conn.send_bitfield(empty_bf).await?;
// Small delay to allow processing
tokio::time::sleep(Duration::from_millis(PEER_CONNECTION_DELAY_MS)).await;
Ok(())
}
/// Wait for an unchoke message from a peer
///
/// Polls the connection for messages until we receive an Unchoke
/// or hit the timeout/attempts limit.
async fn wait_for_unchoke(
conn: &mut BtPeerConn,
addr: &aria2_protocol::bittorrent::peer::connection::PeerAddr,
) -> Result<()> {
debug!("[BT] Waiting for unchoke from {}:{}", addr.ip, addr.port);
for _ in 0..MAX_UNCHOKE_WAIT_ATTEMPTS {
match tokio::time::timeout(
Duration::from_secs(PEER_MESSAGE_TIMEOUT_SECS),
conn.read_message(),
)
.await
{
Ok(Ok(Some(msg))) => {
use aria2_protocol::bittorrent::message::types::BtMessage;
if matches!(msg, BtMessage::Unchoke) {
info!("[BT] Got unchoke from {}:{}", addr.ip, addr.port);
return Ok(());
}
debug!("[BT] Got message while waiting for unchoke: {:?}", msg);
}
Ok(Ok(None)) => {
warn!("[BT] EOF from peer while waiting for unchoke");
return Err(Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure {
message: "Peer closed connection".into(),
},
));
}
Ok(Err(e)) => {
error!("[BT] Error reading from peer: {}", e);
return Err(Aria2Error::Recoverable(
RecoverableError::TemporaryNetworkFailure {
message: format!("Read error: {}", e),
},
));
}
Err(_) => {
debug!("[BT] Timeout reading from peer, retrying...");
}
}
}
warn!(
"[BT] Did not receive unchoke from {}:{} after {} attempts",
addr.ip, addr.port, MAX_UNCHOKE_WAIT_ATTEMPTS
);
Ok(()) // Continue anyway, might get unchoke later
}
/// Broadcast a HAVE message to all connected peers
///
/// Notifies all peers that we have completed downloading a piece.
///
/// # Arguments
/// * `connections` - Mutable slice of active peer connections
/// * `piece_index` - Index of the completed piece
pub async fn broadcast_have(connections: &mut [BtPeerConn], piece_index: u32) {
for conn in connections.iter_mut() {
if let Err(e) = conn.send_have(piece_index).await {
warn!("[BT] Failed to send HAVE to peer: {}", e);
}
}
}
/// Initialize peer bitfield tracker for all connections
///
/// Sets up tracking of which pieces each peer claims to have.
///
/// # Arguments
/// * `connections` - Slice of active peer connections
/// * `num_pieces` - Total number of pieces in the torrent
/// * `peer_tracker` - Mutable reference to the peer bitfield tracker
pub fn initialize_peer_tracking(
connections: &[BtPeerConn],
num_pieces: u32,
peer_tracker: &mut aria2_protocol::bittorrent::piece::peer_tracker::PeerBitfieldTracker,
) {
for (i, _conn) in connections.iter().enumerate() {
let empty_bf = vec![0xFFu8; (num_pieces as usize).div_ceil(8)];
peer_tracker.update_peer_bitfield(&format!("peer_{}", i), &empty_bf);
}
debug!(
"[BT] Initialized peer tracking for {} peers",
connections.len()
);
}
/// Clean up peer connections (drop them properly)
///
/// Ensures all connections are properly closed.
///
/// # Arguments
/// * `connections` - Mutable slice of peer connections to close
pub fn cleanup_connections(connections: &mut [BtPeerConn]) {
for conn in connections.iter_mut() {
let _ = conn;
}
debug!("[BT] Cleaned up {} connections", connections.len());
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_constants_are_reasonable() {
const _: () = {
assert!(PEER_CONNECTION_DELAY_MS >= 10);
assert!(PEER_CONNECTION_DELAY_MS <= 1000);
assert!(MAX_UNCHOKE_WAIT_ATTEMPTS >= 10);
assert!(MAX_UNCHOKE_WAIT_ATTEMPTS <= 100);
assert!(PEER_MESSAGE_TIMEOUT_SECS >= 1);
assert!(PEER_MESSAGE_TIMEOUT_SECS <= 30);
};
}
#[test]
fn test_peer_connection_result_default() {
let result = PeerConnectionResult {
connections: Vec::new(),
failed_count: 0,
};
assert!(result.connections.is_empty());
assert_eq!(result.failed_count, 0);
}
}