aria2-core 0.3.5

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! BT Peer Interaction Manager - Peer connection, initialization, and per-peer
//! interaction loop
//!
//! This module manages the interaction with BitTorrent peers, including:
//! - Connection establishment (plain and encrypted)
//! - Initial handshake and bitfield exchange
//! - Waiting for unchoke messages
//! - Per-peer interaction loop (`BtPeerInteractive`)
//! - Peer connection lifecycle state machine (`PeerConnectionState`)
//!
//! # Architecture Reference
//!
//! Based on original aria2 C++ structure:
//! - `src/DefaultBtInteractive.h/.cc` — Per-peer interaction loop
//! - `src/PeerInteractionCommand.h/.cc` — Peer connection lifecycle command
//! - `src/PeerConnection.cc/h` — Peer connection management
//! - `src/BtSetup.cc/h` — BT setup and initialization

pub mod interactive;
pub mod piece_provider;
pub mod types;

// Re-export all public items from sub-modules so that
// `use crate::engine::bt_peer_interaction::X` still works for external code.
pub use interactive::BtPeerInteractive;
pub use piece_provider::PieceProvider;
pub use types::{
    BtPeerConnectionOptions, BtPeerCryptoPolicy, CheckHaveResult, ChokingDecision,
    DEFAULT_ALLOWED_FAST_SET_SIZE, DEFAULT_KEEP_ALIVE_INTERVAL_SECS,
    DEFAULT_MAX_OUTSTANDING_REQUEST, DispatchUpdate, FLOODING_CHECK_INTERVAL_SECS,
    INACTIVITY_TIMEOUT_SECS, InteractionResult, InterestDecision, MAX_UNCHOKE_WAIT_ATTEMPTS,
    MUTUAL_UNINTERESTED_TIMEOUT_SECS, PEER_CONNECTION_DELAY_MS, PEER_MESSAGE_TIMEOUT_SECS,
    PER_SEC_INTERVAL_SECS, PEX_INTERVAL_SECS, PeerConnectionResult, PeerConnectionState,
    PeerIdCheckResult, PostHandshakeActions, UB_MAX_OUTSTANDING_REQUEST,
};

// ======================================================================
// BtPeerInteraction — legacy static helper (preserved for backward compat)
// ======================================================================

use std::sync::Arc;
use std::time::Duration;

use aria2_protocol::bittorrent::message::types::BtMessage;
use futures::stream::{self, StreamExt};
use tokio::sync::Mutex;

use crate::engine::bt_peer_connection::BtPeerConn;
use crate::error::{Aria2Error, RecoverableError, Result};
use tracing::{debug, error, info, warn};

/// 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;

const HAVE_BROADCAST_CONCURRENCY: usize = 64;

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,
        piece_length: u32,
        total_length: u64,
        connection_options: &BtPeerConnectionOptions,
        utp_socket: Option<Arc<Mutex<aria2_protocol::bittorrent::utp::UtpSocket>>>,
    ) -> Result<PeerConnectionResult> {
        info!("[BT] Connecting to {} peers...", peer_addrs.len());

        // A peer can legitimately delay Unchoke while the tracker has already
        // supplied other usable peers. Establish each candidate independently
        // so one slow peer cannot prevent the piece scheduler from starting.
        let results = stream::iter(peer_addrs.iter().cloned())
            .map(|addr| {
                let utp_socket = utp_socket.clone();
                async move {
                    debug!("[BT] Connecting to peer {}:{}", addr.ip, addr.port);
                    let result = Self::connect_peer_ready(
                        &addr,
                        info_hash_raw,
                        connection_options,
                        num_pieces,
                        piece_length,
                        total_length,
                        utp_socket.clone(),
                    )
                    .await;
                    (addr, result)
                }
            })
            .buffer_unordered(peer_addrs.len().max(1))
            .collect::<Vec<_>>()
            .await;

        let mut active_connections = Vec::with_capacity(results.len());
        let mut failed_count = 0usize;
        for (addr, result) in results {
            match result {
                Ok(conn) => active_connections.push(conn),
                Err(e) => {
                    error!("[BT] Failed to connect peer {}: {}", addr.ip, e);
                    failed_count += 1;
                }
            }
        }

        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,
        })
    }

    /// Establish and initialize one peer using the same crypto and protocol path
    /// as the initial peer batch.
    pub async fn connect_peer_ready(
        addr: &aria2_protocol::bittorrent::peer::connection::PeerAddr,
        info_hash_raw: &[u8; 20],
        connection_options: &BtPeerConnectionOptions,
        num_pieces: u32,
        piece_length: u32,
        total_length: u64,
        utp_socket: Option<Arc<Mutex<aria2_protocol::bittorrent::utp::UtpSocket>>>,
    ) -> Result<BtPeerConn> {
        let mut conn =
            Self::connect_single_peer(addr, info_hash_raw, connection_options, utp_socket).await?;
        conn.set_timeouts(
            connection_options.keep_alive_interval,
            connection_options.peer_timeout,
        );
        conn.sync_peer_identity();
        conn.allocate_session_resource(piece_length, total_length);
        info!(
            "[BT] Connected to peer {}:{} (encrypted={}, piece_length={}, total_length={})",
            addr.ip,
            addr.port,
            conn.is_encrypted(),
            piece_length,
            total_length
        );
        Self::initialize_connection(&mut conn, num_pieces, connection_options).await?;
        if let Err(error) = Self::wait_for_unchoke(&mut conn, addr).await {
            warn!("[BT] No unchoke from peer {}: {}", addr.ip, error);
        }
        Ok(conn)
    }

    /// 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],
        connection_options: &BtPeerConnectionOptions,
        utp_socket: Option<Arc<Mutex<aria2_protocol::bittorrent::utp::UtpSocket>>>,
    ) -> Result<BtPeerConn> {
        if connection_options.enable_utp && !connection_options.crypto.require_mse {
            let endpoint = format!("{}:{}", addr.ip, addr.port)
                .parse::<std::net::SocketAddr>()
                .map_err(|error| {
                    Aria2Error::Fatal(crate::error::FatalError::Config(format!(
                        "Invalid peer address '{}:{}': {error}",
                        addr.ip, addr.port
                    )))
                })?;
            match BtPeerConn::connect_utp_with_options(
                endpoint,
                info_hash_raw,
                &connection_options.local_peer_id,
                connection_options.connection_timeout,
                connection_options.utp_listen_port,
                utp_socket,
            )
            .await
            {
                Ok(conn) => {
                    debug!("[BT] Connected to peer {}:{} over uTP", addr.ip, addr.port);
                    return Ok(conn);
                }
                Err(error) => {
                    debug!(
                        "[BT] uTP connection to {}:{} failed, trying TCP: {}",
                        addr.ip, addr.port, error
                    );
                }
            }
        }

        if connection_options.crypto.require_mse {
            // Try MSE encrypted connection
            BtPeerConn::connect_mse_with_options(
                addr,
                info_hash_raw,
                connection_options.crypto.force_encryption,
                connection_options.crypto.prefer_encryption,
                &connection_options.local_peer_id,
                connection_options.connection_timeout,
            )
            .await
        } else {
            // Try MSE first, fall back to plain
            match BtPeerConn::connect_mse_with_options(
                addr,
                info_hash_raw,
                connection_options.crypto.force_encryption,
                connection_options.crypto.prefer_encryption,
                &connection_options.local_peer_id,
                connection_options.connection_timeout,
            )
            .await
            {
                Ok(conn) => Ok(conn),
                Err(_) => {
                    debug!("[BT] MSE failed, trying plain connection");
                    BtPeerConn::connect_plain_with_options(
                        addr,
                        info_hash_raw,
                        &connection_options.local_peer_id,
                        connection_options.connection_timeout,
                    )
                    .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,
        connection_options: &BtPeerConnectionOptions,
    ) -> Result<()> {
        // Send initial messages
        conn.send_unchoke().await?;
        conn.send_interested().await?;

        // BEP 10 is part of the real connection setup. The peer-agent option
        // therefore travels on the wire before the piece loop starts.
        conn.send_extension_handshake(&connection_options.peer_agent)
            .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(())
    }

    /// Apply peer state messages consumed while waiting for the initial
    /// unchoke. Peers commonly send their bitfield or HaveAll before Unchoke;
    /// dropping those messages leaves the piece selector with no availability.
    fn apply_setup_message(conn: &mut BtPeerConn, msg: BtMessage) -> bool {
        match msg {
            BtMessage::Have { piece_index } => {
                conn.update_peer_bitfield(piece_index as usize, 1);
                if conn
                    .session_resource
                    .as_ref()
                    .is_some_and(|resource| resource.is_seeder())
                {
                    conn.seeder = true;
                }
            }
            BtMessage::Bitfield { data } => {
                conn.set_peer_bitfield(&data);
                conn.seeder = conn
                    .session_resource
                    .as_ref()
                    .is_some_and(|resource| resource.is_seeder());
            }
            BtMessage::HaveAll => conn.mark_seeder(),
            BtMessage::HaveNone => {
                conn.set_peer_bitfield(&[]);
                conn.seeder = false;
            }
            BtMessage::Choke => conn.stats.peer_choking = true,
            BtMessage::Unchoke => {
                conn.stats.peer_choking = false;
                return true;
            }
            BtMessage::AllowedFast { index } => conn.add_allowed_fast(index),
            _ => {}
        }
        false
    }

    /// 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);

        // Read only the initial setup burst here. A peer that delays Unchoke
        // must not hold the whole initial peer batch hostage; the piece loop
        // owns the connection after this point and can consume late setup
        // messages normally.
        let first_message = tokio::time::timeout(
            Duration::from_secs(PEER_MESSAGE_TIMEOUT_SECS),
            conn.read_message(),
        )
        .await;
        let mut got_unchoke = false;
        match first_message {
            Ok(Ok(Some(msg))) => {
                if Self::apply_setup_message(conn, msg) {
                    got_unchoke = true;
                    info!("[BT] Got unchoke from {}:{}", addr.ip, addr.port);
                }
                debug!("[BT] Applied message while waiting for unchoke");

                // Consume the rest of the setup burst without waiting for a
                // second full peer timeout. This preserves HaveAll/bitfield
                // state while still bounding slow peers to one initial wait.
                while let Ok(Ok(Some(msg))) =
                    tokio::time::timeout(Duration::from_millis(100), conn.read_message()).await
                {
                    if Self::apply_setup_message(conn, msg) {
                        got_unchoke = true;
                        info!("[BT] Got unchoke from {}:{}", addr.ip, addr.port);
                    }
                    debug!("[BT] Applied setup message while waiting for unchoke");
                }
            }
            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] Setup message wait elapsed; continuing with peer");
            }
        }

        if got_unchoke {
            return Ok(());
        }

        warn!(
            "[BT] Did not receive unchoke from {}:{} after {} attempts",
            addr.ip, addr.port, 1
        );
        Ok(()) // Continue anyway; the piece loop can receive it 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) {
        let frame = aria2_protocol::bittorrent::message::serializer::serialize_have(piece_index);
        stream::iter(connections.iter_mut())
            .for_each_concurrent(HAVE_BROADCAST_CONCURRENCY, |conn| {
                let frame = &frame;
                async move {
                    if let Err(e) = conn.send_have_frame(frame).await {
                        warn!("[BT] Failed to send HAVE to peer: {}", e);
                    }
                }
            })
            .await;
    }

    /// Return the stable key used by the peer bitfield tracker.
    pub fn peer_tracker_key(conn: &BtPeerConn) -> String {
        conn.remote_peer_id()
            .map(|id| String::from_utf8_lossy(&id).into_owned())
            .unwrap_or_else(|| format!("{}:{}", conn.ip_addr, conn.port))
    }

    /// 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 crate::engine::bt_piece::PeerBitfieldTracker,
    ) {
        for conn in connections {
            let peer_key = Self::peer_tracker_key(conn);
            let bitfield = conn
                .session_resource
                .as_ref()
                .map_or(&[][..], |resource| resource.bitfield());
            peer_tracker.update_peer_bitfield(&peer_key, bitfield);
        }

        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;