aria2-core 0.2.8

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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! DHT message tracker for matching queries to responses.
//!
//! Tracks outbound DHT queries by transaction ID, matching incoming
//! responses to their original queries. Handles timeout detection
//! and cleanup.
//!
//! # Design
//!
//! The C++ implementation uses inheritance-based `DHTMessageCallback`
//! for response handling. This Rust version decouples the tracker from
//! the routing table and message factory: `match_response()` returns a
//! [`MatchResult`] containing the method name and target node info, which
//! the caller uses to route the response appropriately. Similarly,
//! `handle_timeout()` returns timed-out entries for the caller to process
//! (e.g., update node RTT, mark nodes bad, drop from routing table).
//!
//! # C++ Reference
//!
//! - `DHTMessageTracker.h/cc` -> [`DhtMessageTracker`]
//! - `DHTMessageTrackerEntry.h/cc` -> [`TrackerEntry`]

use std::collections::VecDeque;
use std::net::SocketAddr;
use std::time::{Duration, Instant};

use tracing::{debug, trace, warn};

use super::constants::MESSAGE_TIMEOUT_SECS;
use super::node_id::NodeId;

// ---------------------------------------------------------------------------
// TrackerEntry
// ---------------------------------------------------------------------------

/// A tracked outbound DHT query entry.
///
/// Maps a transaction ID to the original query context, enabling
/// response matching and timeout detection.
///
/// C++: `DHTMessageTrackerEntry`
pub struct TrackerEntry {
    /// The target node's ID (may be zero-ID if unknown at query time).
    target_node_id: NodeId,
    /// The target node's address.
    target_addr: SocketAddr,
    /// The transaction ID of the outbound query.
    transaction_id: Vec<u8>,
    /// The DHT method name (e.g., "ping", "find_node", "get_peers", "announce_peer").
    method: String,
    /// When the query was dispatched.
    dispatched_at: Instant,
    /// Timeout duration for this query.
    timeout: Duration,
}

impl TrackerEntry {
    /// Create a new tracker entry.
    pub fn new(
        target_node_id: NodeId,
        target_addr: SocketAddr,
        transaction_id: Vec<u8>,
        method: String,
        timeout: Duration,
    ) -> Self {
        Self {
            target_node_id,
            target_addr,
            transaction_id,
            method,
            dispatched_at: Instant::now(),
            timeout,
        }
    }

    /// Check if this entry has timed out.
    ///
    /// C++: `DHTMessageTrackerEntry::isTimeout()`
    pub fn is_timeout(&self) -> bool {
        self.dispatched_at.elapsed() >= self.timeout
    }

    /// Extend the timeout by resetting the dispatch time to now.
    ///
    /// Note: The C++ `extendTimeout()` is a no-op, but we implement it
    /// as a reset since the method exists and future callers may need it.
    pub fn extend_timeout(&mut self) {
        self.dispatched_at = Instant::now();
    }

    /// Check if an incoming response matches this entry.
    ///
    /// Matches by transaction ID and sender address. The C++ version
    /// also handles IPv4-mapped IPv6 addresses; in Rust, `SocketAddr`
    /// comparison handles this directly since we store the address as
    /// seen when dispatching the query.
    ///
    /// C++: `DHTMessageTrackerEntry::match()`
    pub fn matches(&self, transaction_id: &[u8], sender_addr: &SocketAddr) -> bool {
        self.transaction_id == transaction_id && self.target_addr == *sender_addr
    }

    /// Get the method name for this tracked query.
    pub fn method(&self) -> &str {
        &self.method
    }

    /// Get the target node's ID.
    pub fn target_node_id(&self) -> &NodeId {
        &self.target_node_id
    }

    /// Get the target address.
    pub fn target_addr(&self) -> SocketAddr {
        self.target_addr
    }

    /// Get the transaction ID.
    pub fn transaction_id(&self) -> &[u8] {
        &self.transaction_id
    }

    /// Get elapsed time since dispatch.
    ///
    /// C++: `DHTMessageTrackerEntry::getElapsed()`
    pub fn elapsed(&self) -> Duration {
        self.dispatched_at.elapsed()
    }
}

// ---------------------------------------------------------------------------
// MatchResult
// ---------------------------------------------------------------------------

/// Result of matching an incoming response to a tracked query.
///
/// The caller uses this information to route the response to the
/// appropriate handler (replaces the C++ callback mechanism).
pub struct MatchResult {
    /// The method name of the original query.
    pub method: String,
    /// The target node's ID from the tracked query.
    pub target_node_id: NodeId,
    /// The target address from the tracked query.
    pub target_addr: SocketAddr,
    /// Elapsed time since the query was dispatched (for RTT update).
    ///
    /// C++: `DHTMessageTracker::messageArrived()` computes
    /// `entry->getElapsed()` and calls `node->updateRTT(rtt)`.
    pub elapsed: Duration,
}

// ---------------------------------------------------------------------------
// TimeoutEntry
// ---------------------------------------------------------------------------

/// Information about a timed-out query, returned to the caller for processing.
///
/// The caller should:
/// - Update the node's RTT from `elapsed`
/// - Call `node.timeout()` to increment the failure counter
/// - Drop the node from the routing table if it becomes bad
/// - Notify any waiting tasks of the timeout
pub struct TimeoutEntry {
    /// The target node's ID.
    pub target_node_id: NodeId,
    /// The target address.
    pub target_addr: SocketAddr,
    /// The DHT method name of the timed-out query.
    pub method: String,
    /// Elapsed time since dispatch (useful for RTT estimation on failure).
    pub elapsed: Duration,
}

// ---------------------------------------------------------------------------
// DhtMessageTracker
// ---------------------------------------------------------------------------

/// DHT message tracker for query/response matching.
///
/// Maintains a deque of tracker entries. When a query is sent,
/// it is registered via `add_query()`. When a response arrives,
/// `match_response()` is called to find the matching entry.
/// Timed-out entries are cleaned up via `handle_timeout()`.
///
/// C++: `DHTMessageTracker`
pub struct DhtMessageTracker {
    entries: VecDeque<TrackerEntry>,
    default_timeout: Duration,
}

impl DhtMessageTracker {
    /// Create a new tracker with the default message timeout.
    pub fn new() -> Self {
        Self {
            entries: VecDeque::new(),
            default_timeout: Duration::from_secs(MESSAGE_TIMEOUT_SECS),
        }
    }

    /// Create a new tracker with a custom default timeout.
    pub fn with_timeout(timeout: Duration) -> Self {
        Self {
            entries: VecDeque::new(),
            default_timeout: timeout,
        }
    }

    /// Register a new outbound DHT query using the default timeout.
    ///
    /// C++: `DHTMessageTracker::addMessage()` (without custom callback)
    pub fn add_query(
        &mut self,
        target_node_id: NodeId,
        target_addr: SocketAddr,
        transaction_id: Vec<u8>,
        method: String,
    ) {
        self.add_query_with_timeout(
            target_node_id,
            target_addr,
            transaction_id,
            method,
            self.default_timeout,
        );
    }

    /// Register a new outbound DHT query with a custom timeout.
    ///
    /// C++: `DHTMessageTracker::addMessage()` (with custom timeout)
    pub fn add_query_with_timeout(
        &mut self,
        target_node_id: NodeId,
        target_addr: SocketAddr,
        transaction_id: Vec<u8>,
        method: String,
        timeout: Duration,
    ) {
        trace!(
            tid = ?transaction_id,
            method = %method,
            addr = %target_addr,
            "Tracking DHT query"
        );
        let entry = TrackerEntry::new(target_node_id, target_addr, transaction_id, method, timeout);
        self.entries.push_back(entry);
    }

    /// Try to match an incoming response to a tracked query.
    ///
    /// Searches entries for a match by transaction ID and sender address.
    /// If found, removes the entry and returns the match result.
    /// If not found, returns `None`.
    ///
    /// C++: `DHTMessageTracker::messageArrived()` (simplified — the C++
    /// version also creates a response message via the factory and handles
    /// node ID changes; here we return the match data for the caller to
    /// process).
    pub fn match_response(
        &mut self,
        transaction_id: &[u8],
        sender_addr: &SocketAddr,
    ) -> Option<MatchResult> {
        let pos = self
            .entries
            .iter()
            .position(|e| e.matches(transaction_id, sender_addr))?;
        let entry = self.entries.remove(pos)?;
        let elapsed = entry.elapsed();

        debug!(
            tid = ?transaction_id,
            method = %entry.method,
            addr = %sender_addr,
            elapsed_ms = elapsed.as_millis(),
            "DHT response matched to query"
        );

        Some(MatchResult {
            method: entry.method,
            target_node_id: entry.target_node_id,
            target_addr: entry.target_addr,
            elapsed,
        })
    }

    /// Handle timed-out entries.
    ///
    /// Removes entries whose timeout has expired and returns
    /// the list of [`TimeoutEntry`] for the caller to process
    /// (e.g., update node RTT, mark nodes bad, drop from routing table).
    ///
    /// C++: `DHTMessageTracker::handleTimeout()`
    pub fn handle_timeout(&mut self) -> Vec<TimeoutEntry> {
        let mut timed_out = Vec::new();
        let mut remaining = VecDeque::with_capacity(self.entries.len());

        while let Some(entry) = self.entries.pop_front() {
            if entry.is_timeout() {
                warn!(
                    tid = ?entry.transaction_id,
                    method = %entry.method,
                    addr = %entry.target_addr,
                    elapsed = ?entry.elapsed(),
                    "DHT query timed out"
                );
                let elapsed = entry.elapsed();
                timed_out.push(TimeoutEntry {
                    target_node_id: entry.target_node_id,
                    target_addr: entry.target_addr,
                    method: entry.method,
                    elapsed,
                });
            } else {
                remaining.push_back(entry);
            }
        }

        self.entries = remaining;
        timed_out
    }

    /// Get the number of tracked entries.
    ///
    /// C++: `DHTMessageTracker::countEntry()`
    pub fn count(&self) -> usize {
        self.entries.len()
    }

    /// Check if there are any tracked entries.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Look up an entry by transaction ID.
    ///
    /// C++: `DHTMessageTracker::getEntryFor()` (test-only in C++ as well)
    pub fn get_entry(&self, transaction_id: &[u8]) -> Option<&TrackerEntry> {
        self.entries
            .iter()
            .find(|e| e.transaction_id == transaction_id)
    }
}

impl Default for DhtMessageTracker {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, SocketAddrV4};

    use super::*;

    fn make_addr(port: u16) -> SocketAddr {
        SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))
    }

    fn make_node_id(byte: u8) -> NodeId {
        let mut id = [0u8; 20];
        id[0] = byte;
        NodeId(id)
    }

    // -- TrackerEntry tests --

    #[test]
    fn entry_not_timeout_initially() {
        let entry = TrackerEntry::new(
            NodeId::ZERO,
            make_addr(1234),
            vec![1, 2, 3, 4],
            "ping".into(),
            Duration::from_secs(10),
        );
        assert!(!entry.is_timeout());
    }

    #[test]
    fn entry_matches_same_tid_and_addr() {
        let entry = TrackerEntry::new(
            make_node_id(1),
            make_addr(5000),
            vec![0xAA, 0xBB],
            "find_node".into(),
            Duration::from_secs(10),
        );
        assert!(entry.matches(&[0xAA, 0xBB], &make_addr(5000)));
    }

    #[test]
    fn entry_no_match_different_tid() {
        let entry = TrackerEntry::new(
            make_node_id(1),
            make_addr(5000),
            vec![0xAA, 0xBB],
            "find_node".into(),
            Duration::from_secs(10),
        );
        assert!(!entry.matches(&[0xCC, 0xDD], &make_addr(5000)));
    }

    #[test]
    fn entry_no_match_different_addr() {
        let entry = TrackerEntry::new(
            make_node_id(1),
            make_addr(5000),
            vec![0xAA, 0xBB],
            "find_node".into(),
            Duration::from_secs(10),
        );
        assert!(!entry.matches(&[0xAA, 0xBB], &make_addr(6000)));
    }

    #[test]
    fn entry_extend_timeout_resets_dispatch() {
        let mut entry = TrackerEntry::new(
            NodeId::ZERO,
            make_addr(1234),
            vec![1],
            "ping".into(),
            Duration::from_millis(50),
        );
        assert!(!entry.is_timeout());
        std::thread::sleep(Duration::from_millis(60));
        assert!(entry.is_timeout());
        entry.extend_timeout();
        assert!(!entry.is_timeout());
    }

    #[test]
    fn entry_accessors() {
        let entry = TrackerEntry::new(
            make_node_id(42),
            make_addr(7000),
            vec![0x01, 0x02],
            "get_peers".into(),
            Duration::from_secs(5),
        );
        assert_eq!(entry.method(), "get_peers");
        assert_eq!(entry.target_node_id(), &make_node_id(42));
        assert_eq!(entry.target_addr(), make_addr(7000));
        assert_eq!(entry.transaction_id(), &[0x01, 0x02]);
    }

    // -- DhtMessageTracker tests --

    #[test]
    fn tracker_add_and_match() {
        let mut tracker = DhtMessageTracker::new();
        let addr = make_addr(5000);
        let tid = vec![1, 2, 3, 4];

        tracker.add_query(make_node_id(1), addr, tid.clone(), "ping".into());
        assert_eq!(tracker.count(), 1);

        let result = tracker.match_response(&tid, &addr).unwrap();
        assert_eq!(result.method, "ping");
        assert_eq!(result.target_node_id, make_node_id(1));
        assert_eq!(result.target_addr, addr);
        // Elapsed should be very small for an immediately-matched entry
        assert!(result.elapsed < Duration::from_secs(1));
        assert_eq!(tracker.count(), 0);
    }

    #[test]
    fn tracker_no_match_returns_none() {
        let mut tracker = DhtMessageTracker::new();
        tracker.add_query(make_node_id(1), make_addr(5000), vec![1, 2], "ping".into());

        assert!(tracker.match_response(&[1, 2], &make_addr(9999)).is_none());
        assert!(tracker.match_response(&[9, 9], &make_addr(5000)).is_none());
        let mut empty_tracker = DhtMessageTracker::new();
        assert!(
            empty_tracker
                .match_response(&[1, 2], &make_addr(5000))
                .is_none()
        );
    }

    #[test]
    fn tracker_multiple_entries_match_correct() {
        let mut tracker = DhtMessageTracker::new();
        let addr1 = make_addr(5001);
        let addr2 = make_addr(5002);

        tracker.add_query(make_node_id(1), addr1, vec![1], "ping".into());
        tracker.add_query(make_node_id(2), addr2, vec![2], "find_node".into());
        tracker.add_query(make_node_id(3), addr1, vec![3], "get_peers".into());

        assert_eq!(tracker.count(), 3);

        let result = tracker.match_response(&[2], &addr2).unwrap();
        assert_eq!(result.method, "find_node");
        assert_eq!(tracker.count(), 2);

        let result = tracker.match_response(&[1], &addr1).unwrap();
        assert_eq!(result.method, "ping");
        assert_eq!(tracker.count(), 1);

        let result = tracker.match_response(&[3], &addr1).unwrap();
        assert_eq!(result.method, "get_peers");
        assert!(tracker.is_empty());
    }

    #[test]
    fn tracker_timeout_removes_expired() {
        let mut tracker = DhtMessageTracker::with_timeout(Duration::from_millis(30));
        tracker.add_query(make_node_id(1), make_addr(5000), vec![1], "ping".into());
        tracker.add_query(
            make_node_id(2),
            make_addr(5001),
            vec![2],
            "find_node".into(),
        );

        assert_eq!(tracker.count(), 2);

        std::thread::sleep(Duration::from_millis(40));

        let timed_out = tracker.handle_timeout();
        assert_eq!(timed_out.len(), 2);
        assert_eq!(timed_out[0].method, "ping");
        assert_eq!(timed_out[1].method, "find_node");
        assert!(tracker.is_empty());
    }

    #[test]
    fn tracker_timeout_keeps_non_expired() {
        let mut tracker = DhtMessageTracker::new();
        tracker.add_query_with_timeout(
            make_node_id(1),
            make_addr(5000),
            vec![1],
            "ping".into(),
            Duration::from_millis(20),
        );
        tracker.add_query_with_timeout(
            make_node_id(2),
            make_addr(5001),
            vec![2],
            "find_node".into(),
            Duration::from_secs(300),
        );

        std::thread::sleep(Duration::from_millis(30));

        let timed_out = tracker.handle_timeout();
        assert_eq!(timed_out.len(), 1);
        assert_eq!(timed_out[0].method, "ping");
        assert_eq!(tracker.count(), 1);

        let entry = tracker.get_entry(&[2]).unwrap();
        assert_eq!(entry.method(), "find_node");
    }

    #[test]
    fn tracker_timeout_entry_has_elapsed() {
        let mut tracker = DhtMessageTracker::with_timeout(Duration::from_millis(10));
        tracker.add_query(make_node_id(1), make_addr(5000), vec![1], "ping".into());

        std::thread::sleep(Duration::from_millis(20));

        let timed_out = tracker.handle_timeout();
        assert_eq!(timed_out.len(), 1);
        assert!(timed_out[0].elapsed >= Duration::from_millis(10));
    }

    #[test]
    fn tracker_get_entry_by_tid() {
        let mut tracker = DhtMessageTracker::new();
        tracker.add_query(make_node_id(1), make_addr(5000), vec![0xAA], "ping".into());
        tracker.add_query(
            make_node_id(2),
            make_addr(5001),
            vec![0xBB],
            "find_node".into(),
        );

        let entry = tracker.get_entry(&[0xBB]).unwrap();
        assert_eq!(entry.method(), "find_node");
        assert_eq!(*entry.target_node_id(), make_node_id(2));

        assert!(tracker.get_entry(&[0xCC]).is_none());
    }

    #[test]
    fn tracker_match_removes_entry_so_no_double_match() {
        let mut tracker = DhtMessageTracker::new();
        let addr = make_addr(5000);
        tracker.add_query(make_node_id(1), addr, vec![1], "ping".into());

        assert!(tracker.match_response(&[1], &addr).is_some());
        assert!(tracker.match_response(&[1], &addr).is_none());
    }

    #[test]
    fn tracker_default_impl() {
        let tracker = DhtMessageTracker::default();
        assert!(tracker.is_empty());
        assert_eq!(tracker.count(), 0);
    }
}