aria2-core 0.2.2

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
use std::collections::{HashMap, VecDeque};
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::Mutex;
use tracing::{debug, info, warn};

use aria2_protocol::bittorrent::tracker::udp_tracker_protocol::{
    AnnounceResponse, CONNECTION_TIMEOUT_SECS, ConnectResponse, ScrapeResult, UdpAction, UdpError,
    UdpEvent, UdpState, build_announce_request, build_connect_request, build_scrape_request,
    parse_announce_response, parse_connect_response, parse_scrape_response,
};

const REQUEST_TIMEOUT_SECS: u64 = 15;
const MAX_RETRIES: u32 = 3;

struct ConnectionState {
    id: u64,
    updated_at: Instant,
}

#[derive(Debug)]
pub struct UdpTrackerRequest {
    pub remote_addr: SocketAddr,
    pub info_hash: [u8; 20],
    pub peer_id: [u8; 20],
    pub downloaded: i64,
    pub left: i64,
    pub uploaded: i64,
    pub event: UdpEvent,
    pub num_want: i32,
    pub port: u16,
    pub state: UdpState,
    pub error: Option<UdpError>,
    pub dispatched_at: Option<Instant>,
    pub fail_count: u32,
    pub reply: Option<AnnounceResponse>,
    /// Scrape results populated when this is a scrape request
    pub scrape_results: Option<Vec<ScrapeResult>>,
    /// Info hashes for scrape requests (can be multiple)
    pub scrape_info_hashes: Vec<[u8; 20]>,
    txn_id: u32,
}

impl UdpTrackerRequest {
    #[allow(clippy::too_many_arguments)]
    fn new(
        addr: SocketAddr,
        info_hash: [u8; 20],
        peer_id: [u8; 20],
        downloaded: i64,
        left: i64,
        uploaded: i64,
        event: UdpEvent,
        num_want: i32,
        port: u16,
    ) -> Self {
        Self {
            remote_addr: addr,
            info_hash,
            peer_id,
            downloaded,
            left,
            uploaded,
            event,
            num_want,
            port,
            state: UdpState::Pending,
            error: None,
            dispatched_at: None,
            fail_count: 0,
            reply: None,
            scrape_results: None,
            scrape_info_hashes: Vec::new(),
            txn_id: 0,
        }
    }
}

pub struct UdpTrackerClient {
    socket: Arc<tokio::net::UdpSocket>,
    conn_cache: HashMap<SocketAddr, ConnectionState>,
    pending: VecDeque<UdpTrackerRequest>,
    inflight: VecDeque<UdpTrackerRequest>,
    waiting_for_conn: VecDeque<UdpTrackerRequest>,
    txn_map: HashMap<u32, usize>,
    next_txn_id: u32,
}

impl UdpTrackerClient {
    pub async fn new(bind_port: u16) -> Result<Self, String> {
        let addr = format!("0.0.0.0:{}", bind_port);
        let socket = tokio::net::UdpSocket::bind(&addr)
            .await
            .map_err(|e| format!("UDP bind failed on {}: {}", addr, e))?;

        info!("UdpTrackerClient bound to {}", addr);

        Ok(Self {
            socket: Arc::new(socket),
            conn_cache: HashMap::new(),
            pending: VecDeque::new(),
            inflight: VecDeque::new(),
            waiting_for_conn: VecDeque::new(),
            txn_map: HashMap::new(),
            next_txn_id: Self::initial_txn_id(),
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn add_announce(
        &mut self,
        addr: &SocketAddr,
        info_hash: &[u8; 20],
        peer_id: &[u8; 20],
        downloaded: i64,
        left: i64,
        uploaded: i64,
        event: UdpEvent,
        num_want: i32,
        port: u16,
    ) {
        let req = UdpTrackerRequest::new(
            *addr, *info_hash, *peer_id, downloaded, left, uploaded, event, num_want, port,
        );
        self.pending.push_back(req);
        debug!("Added announce request for {}", addr);
    }

    /// Add a SCRAPE request to query statistics for one or more info hashes
    ///
    /// # Arguments
    /// * `addr` - UDP tracker socket address
    /// * `info_hashes` - Slice of 20-byte info hashes to query (max ~74 per request)
    pub async fn add_scrape(&mut self, addr: &SocketAddr, info_hashes: &[[u8; 20]]) {
        // Use first info hash for the request struct (scrape can have multiple)
        let first_ih = if info_hashes.is_empty() {
            [0u8; 20]
        } else {
            info_hashes[0]
        };

        let mut req =
            UdpTrackerRequest::new(*addr, first_ih, [0u8; 20], 0, 0, 0, UdpEvent::None, 0, 0);
        req.scrape_info_hashes = info_hashes.to_vec();
        self.pending.push_back(req);
        debug!(
            "Added scrape request for {} ({} hashes)",
            addr,
            info_hashes.len()
        );
    }

    pub async fn process_one(&mut self) -> bool {
        loop {
            if self.pending.is_empty() && self.waiting_for_conn.is_empty() {
                return false;
            }

            if let Some(mut req) = self.pending.pop_front() {
                let host_key = req.remote_addr;

                if let Some(conn) = self.conn_cache.get(&host_key) {
                    if conn.updated_at.elapsed().as_secs() < CONNECTION_TIMEOUT_SECS {
                        // Route to appropriate send method based on request type
                        if !req.scrape_info_hashes.is_empty() {
                            return self.send_scrape(&mut req, conn.id).await;
                        }
                        return self.send_announce(&mut req, conn.id).await;
                    } else {
                        self.conn_cache.remove(&host_key);
                        debug!("Connection cache expired for {}", host_key);
                    }
                }

                if !self.is_connecting_to(&host_key) {
                    self.waiting_for_conn.push_back(req);
                    return self.send_connect(host_key).await;
                }

                self.waiting_for_conn.push_back(req);
                debug!("Waiting for connection to {}", host_key);
            } else if let Some(req) = self.waiting_for_conn.pop_front() {
                self.pending.push_front(req);
            } else {
                return false;
            }
        }
    }

    async fn send_announce(&mut self, req: &mut UdpTrackerRequest, conn_id: u64) -> bool {
        let txn_id = self.next_txn();
        req.txn_id = txn_id;
        req.dispatched_at = Some(Instant::now());
        req.state = UdpState::Pending;

        let payload = build_announce_request(
            conn_id,
            txn_id,
            &req.info_hash,
            &req.peer_id,
            req.downloaded,
            req.left,
            req.uploaded,
            req.event,
            0,
            0,
            req.num_want,
            req.port,
        );

        match self.socket.send_to(&payload, req.remote_addr).await {
            Ok(len) => {
                self.txn_map.insert(txn_id, self.inflight.len());
                self.inflight.push_back(std::mem::replace(
                    req,
                    UdpTrackerRequest::new(
                        req.remote_addr,
                        req.info_hash,
                        req.peer_id,
                        req.downloaded,
                        req.left,
                        req.uploaded,
                        req.event,
                        req.num_want,
                        req.port,
                    ),
                ));
                debug!(
                    "Sent ANNOUNCE {} bytes to {} (txn={})",
                    len, req.remote_addr, txn_id
                );
                true
            }
            Err(e) => {
                warn!("Send ANNOUNCE to {} failed: {}", req.remote_addr, e);
                req.fail_count += 1;
                req.error = Some(UdpError::Network);
                if req.fail_count < MAX_RETRIES {
                    self.pending.push_front(std::mem::replace(
                        req,
                        UdpTrackerRequest::new(
                            req.remote_addr,
                            req.info_hash,
                            req.peer_id,
                            req.downloaded,
                            req.left,
                            req.uploaded,
                            req.event,
                            req.num_want,
                            req.port,
                        ),
                    ));
                }
                true
            }
        }
    }

    async fn send_scrape(&mut self, req: &mut UdpTrackerRequest, conn_id: u64) -> bool {
        let txn_id = self.next_txn();
        req.txn_id = txn_id;
        req.dispatched_at = Some(Instant::now());
        req.state = UdpState::Pending;

        // Build scrape payload with all info hashes from this request
        let hashes: Vec<[u8; 20]> = req.scrape_info_hashes.clone();
        if hashes.is_empty() {
            warn!("Scrape request with no info hashes for {}", req.remote_addr);
            return true;
        }

        let payload = build_scrape_request(conn_id, txn_id, &hashes);

        match self.socket.send_to(&payload, req.remote_addr).await {
            Ok(len) => {
                self.txn_map.insert(txn_id, self.inflight.len());
                // Preserve scrape_info_hashes when replacing the request
                let mut replacement = UdpTrackerRequest::new(
                    req.remote_addr,
                    req.info_hash,
                    req.peer_id,
                    req.downloaded,
                    req.left,
                    req.uploaded,
                    req.event,
                    req.num_want,
                    req.port,
                );
                replacement.scrape_info_hashes = std::mem::take(&mut req.scrape_info_hashes);
                self.inflight.push_back(std::mem::replace(req, replacement));
                debug!(
                    "Sent SCRAPE {} bytes to {} (txn={}, {} hashes)",
                    len,
                    req.remote_addr,
                    txn_id,
                    hashes.len()
                );
                true
            }
            Err(e) => {
                warn!("Send SCRAPE to {} failed: {}", req.remote_addr, e);
                req.fail_count += 1;
                req.error = Some(UdpError::Network);
                if req.fail_count < MAX_RETRIES {
                    self.pending.push_front(std::mem::replace(
                        req,
                        UdpTrackerRequest::new(
                            req.remote_addr,
                            req.info_hash,
                            req.peer_id,
                            req.downloaded,
                            req.left,
                            req.uploaded,
                            req.event,
                            req.num_want,
                            req.port,
                        ),
                    ));
                }
                true
            }
        }
    }

    async fn send_connect(&mut self, addr: SocketAddr) -> bool {
        let txn_id = self.next_txn();

        let payload = build_connect_request(txn_id);

        match self.socket.send_to(&payload, addr).await {
            Ok(len) => {
                let mut dummy_req = UdpTrackerRequest::new(
                    addr,
                    [0u8; 20],
                    [0u8; 20],
                    0,
                    0,
                    0,
                    UdpEvent::None,
                    0,
                    0,
                );
                dummy_req.txn_id = txn_id;
                dummy_req.dispatched_at = Some(Instant::now());
                dummy_req.state = UdpState::Pending;
                self.txn_map.insert(txn_id, self.inflight.len());
                self.inflight.push_back(dummy_req);
                debug!("Sent CONNECT {} bytes to {} (txn={})", len, addr, txn_id);
                true
            }
            Err(e) => {
                warn!("Send CONNECT to {} failed: {}", addr, e);
                true
            }
        }
    }

    pub async fn handle_response(&mut self, data: &[u8], from: &SocketAddr) {
        if data.len() < 4 {
            warn!("Short response from {}: {} bytes", from, data.len());
            return;
        }

        let action_val = i32::from_be_bytes([data[0], data[1], data[2], data[3]]);
        let txn_id = if data.len() >= 8 {
            u32::from_be_bytes([data[4], data[5], data[6], data[7]])
        } else {
            warn!("Response too short for txn_id from {}", from);
            return;
        };

        let idx = match self.txn_map.remove(&txn_id) {
            Some(i) => i,
            None => {
                debug!("Unknown txn_id {} from {}", txn_id, from);
                return;
            }
        };

        if idx >= self.inflight.len() {
            warn!(
                "Invalid index {} for txn_id {} (inflight={})",
                idx,
                txn_id,
                self.inflight.len()
            );
            return;
        }

        let mut req = self.inflight.remove(idx).unwrap_or_else(|| {
            UdpTrackerRequest::new(*from, [0u8; 20], [0u8; 20], 0, 0, 0, UdpEvent::None, 0, 0)
        });

        match UdpAction::from_i32(action_val) {
            Some(UdpAction::Connect) => match parse_connect_response(data) {
                Ok(ConnectResponse { connection_id, .. }) => {
                    info!(
                        "CONNECT response from {}, conn_id=0x{:016X}",
                        from, connection_id
                    );
                    self.conn_cache.insert(
                        *from,
                        ConnectionState {
                            id: connection_id,
                            updated_at: Instant::now(),
                        },
                    );

                    while let Some(waiting) = self.waiting_for_conn.pop_front() {
                        self.pending.push_front(waiting);
                    }
                }
                Err(e) => {
                    warn!("Parse CONNECT response from {} failed: {}", from, e);
                    req.error = Some(UdpError::TrackerError);
                }
            },
            Some(UdpAction::Announce) => {
                match parse_announce_response(data) {
                    Ok(resp) => {
                        info!(
                            "ANNOUNCE response from {}: {} peers, interval={}s",
                            from,
                            resp.peers.len(),
                            resp.interval
                        );
                        req.reply = Some(resp);
                        req.state = UdpState::Complete;
                        req.error = Some(UdpError::Success);
                    }
                    Err(e) => {
                        warn!("Parse ANNOUNCE response from {} failed: {}", from, e);
                        req.error = Some(UdpError::TrackerError);
                    }
                }
                self.pending.push_back(req);
            }
            Some(UdpAction::Error) => {
                let msg_len = (data.len() - 8).min(256);
                let msg = String::from_utf8_lossy(&data[8..8 + msg_len]);
                warn!("Tracker error from {}: {}", from, msg);
                req.error = Some(UdpError::TrackerError);
                self.pending.push_back(req);
            }
            Some(UdpAction::Scrape) => match parse_scrape_response(data) {
                Ok(results) => {
                    info!(
                        "SCRAPE response from {}: {} info hashes scraped",
                        from,
                        results.len()
                    );
                    // Store scrape results in a dedicated collection
                    for result in &results {
                        debug!(
                            "  seeders={} leechers={} completed={}",
                            result.seeders, result.leechers, result.completed
                        );
                    }
                    req.scrape_results = Some(results);
                    req.state = UdpState::Complete;
                    req.error = Some(UdpError::Success);
                    self.pending.push_back(req);
                }
                Err(e) => {
                    warn!("Parse SCRAPE response from {} failed: {}", from, e);
                    req.error = Some(UdpError::TrackerError);
                    self.pending.push_back(req);
                }
            },
            _ => {
                warn!("Unknown action {} from {}", action_val, from);
                req.error = Some(UdpError::TrackerError);
                self.pending.push_back(req);
            }
        }
    }

    pub async fn handle_timeouts(&mut self) {
        let now = Instant::now();
        let expired: Vec<usize> = self
            .inflight
            .iter()
            .enumerate()
            .filter(|(_, r)| {
                r.dispatched_at.is_some_and(|t| {
                    t.duration_since(now) > Duration::from_secs(REQUEST_TIMEOUT_SECS)
                })
            })
            .map(|(i, _)| i)
            .collect();

        for idx in expired.into_iter().rev() {
            if idx < self.inflight.len() {
                let mut req = self.inflight.remove(idx).unwrap_or_else(|| {
                    UdpTrackerRequest::new(
                        std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
                            std::net::Ipv4Addr::UNSPECIFIED,
                            0,
                        )),
                        [0u8; 20],
                        [0u8; 20],
                        0,
                        0,
                        0,
                        UdpEvent::None,
                        0,
                        0,
                    )
                });
                if req.txn_id != 0 {
                    self.txn_map.remove(&req.txn_id);
                }
                req.fail_count += 1;
                if req.fail_count < MAX_RETRIES {
                    debug!(
                        "Timeout retry {}/{} for txn_id={}",
                        req.fail_count, MAX_RETRIES, req.txn_id
                    );
                    req.dispatched_at = None;
                    self.pending.push_back(req);
                } else {
                    warn!("Max retries exceeded for txn_id={}", req.txn_id);
                    req.error = Some(UdpError::Timeout);
                }
            }
        }

        let stale_addrs: Vec<SocketAddr> = self
            .conn_cache
            .iter()
            .filter(|(_, s)| s.updated_at.elapsed().as_secs() > CONNECTION_TIMEOUT_SECS)
            .map(|(&a, _)| a)
            .collect();

        for addr in stale_addrs {
            self.conn_cache.remove(&addr);
            debug!("Removed stale connection cache for {}", addr);
        }
    }

    pub fn no_pending(&self) -> bool {
        self.pending.is_empty() && self.inflight.is_empty() && self.waiting_for_conn.is_empty()
    }

    pub fn completed_requests(&self) -> Vec<&AnnounceResponse> {
        self.pending
            .iter()
            .filter_map(|r| r.reply.as_ref())
            .collect()
    }

    /// Get all completed scrape results from pending requests
    pub fn completed_scrape_results(&self) -> Vec<&Vec<ScrapeResult>> {
        self.pending
            .iter()
            .filter_map(|r| r.scrape_results.as_ref())
            .collect()
    }

    pub fn socket(&self) -> Arc<tokio::net::UdpSocket> {
        Arc::clone(&self.socket)
    }

    fn is_connecting_to(&self, addr: &SocketAddr) -> bool {
        self.inflight
            .iter()
            .any(|r| &r.remote_addr == addr && r.reply.is_none())
    }

    pub(crate) fn next_txn(&mut self) -> u32 {
        let id = self.next_txn_id;
        self.next_txn_id = id.wrapping_add(1);
        if self.next_txn_id == 0 {
            self.next_txn_id = 1;
        }
        id
    }

    fn initial_txn_id() -> u32 {
        use std::time::{SystemTime, UNIX_EPOCH};
        let dur = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default();
        ((dur.as_nanos() & 0xFFFFFFFF) as u32).max(1)
    }
}

pub type SharedUdpClient = Arc<Mutex<UdpTrackerClient>>;

impl UdpTrackerClient {
    pub async fn create_shared(bind_port: u16) -> Result<SharedUdpClient, String> {
        let client = Self::new(bind_port).await?;
        Ok(Arc::new(Mutex::new(client)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_client_creation() {
        let client = UdpTrackerClient::new(0).await;
        assert!(
            client.is_ok(),
            "UDP client creation should succeed with port 0"
        );
        let c = client.unwrap();
        assert!(c.no_pending());
        assert!(c.completed_requests().is_empty());
    }

    #[tokio::test]
    async fn test_add_announce_request() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let ih = [0xABu8; 20];
        let pid = [0xCDu8; 20];

        client
            .add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881)
            .await;
        assert_eq!(client.pending.len(), 1);

        client
            .add_announce(&addr, &ih, &pid, 500, 500, 0, UdpEvent::None, -1, 6881)
            .await;
        assert_eq!(client.pending.len(), 2);
    }

    #[tokio::test]
    async fn test_process_one_needs_connection() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let ih = [0x12u8; 20];
        let pid = [0x34u8; 20];

        client
            .add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881)
            .await;
        let processed = client.process_one().await;
        assert!(processed, "Should have processed the connect step");
        assert!(
            !client.inflight.is_empty(),
            "Should have an in-flight CONNECT"
        );
    }

    #[tokio::test]
    async fn test_no_pending_returns_false_when_empty() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        assert!(client.no_pending());
        let processed = client.process_one().await;
        assert!(!processed, "process_one should return false when empty");
    }

    #[tokio::test]
    async fn test_handle_connect_response() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let ih = [0x11u8; 20];
        let pid = [0x22u8; 20];

        client
            .add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881)
            .await;
        client.process_one().await;

        let mut resp_data = vec![0u8; 16];
        resp_data[0..4].copy_from_slice(&0i32.to_be_bytes());
        let txn_id = client.inflight.front().map(|r| r.txn_id).unwrap_or(0);
        resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        resp_data[8..16].copy_from_slice(&0x123456789ABCDEF0u64.to_be_bytes());

        client.handle_response(&resp_data, &addr).await;
        assert!(
            client.conn_cache.contains_key(&addr),
            "Should cache connection after CONNECT response"
        );
    }

    #[tokio::test]
    async fn test_handle_announce_response_with_peers() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();

        let ih = [0x33u8; 20];
        let pid = [0x44u8; 20];
        let txn_id = client.next_txn();

        client.txn_map.insert(txn_id, 0);
        let mut dummy_req =
            UdpTrackerRequest::new(addr, ih, pid, 0, 1000, 0, UdpEvent::Started, 50, 6881);
        dummy_req.txn_id = txn_id;
        dummy_req.dispatched_at = Some(Instant::now());
        client.inflight.push_back(dummy_req);

        let mut resp_data = vec![0u8; 26];
        resp_data[0..4].copy_from_slice(&1i32.to_be_bytes());
        resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        resp_data[8..12].copy_from_slice(&900u32.to_be_bytes());
        resp_data[12..16].copy_from_slice(&5u32.to_be_bytes());
        resp_data[16..20].copy_from_slice(&3u32.to_be_bytes());
        resp_data.extend_from_slice(&[10, 0, 0, 1, 0x1A, 0x04, 192, 168, 1, 100, 0x1F, 0x90]);

        client.handle_response(&resp_data, &addr).await;

        let completed = client.completed_requests();
        assert!(
            !completed.is_empty(),
            "Should have at least one completed announce"
        );
        assert!(
            completed[0].peers.len() >= 2,
            "Should have at least 2 peers"
        );
        assert_eq!(completed[0].interval, 900);
        assert_eq!(completed[0].leechers, 5);
        assert_eq!(completed[0].seeders, 3);
    }

    #[tokio::test]
    async fn test_handle_error_response() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let ih = [0x55u8; 20];
        let pid = [0x66u8; 20];

        client
            .add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881)
            .await;
        client.process_one().await;

        let txn_id = client.inflight.front().map(|r| r.txn_id).unwrap_or(0);
        let mut err_data = vec![0u8; 23];
        err_data[0..4].copy_from_slice(&3i32.to_be_bytes());
        err_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        err_data[8..23].copy_from_slice(b"tracker offline");

        client.handle_response(&err_data, &addr).await;
        assert!(
            !client.conn_cache.contains_key(&addr),
            "Error should not create cache entry"
        );
    }

    #[tokio::test]
    async fn test_timeout_cleaning() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let ih = [0x77u8; 20];
        let pid = [0x88u8; 20];

        client
            .add_announce(&addr, &ih, &pid, 0, 1000, 0, UdpEvent::Started, 50, 6881)
            .await;
        client.process_one().await;
        assert_eq!(client.inflight.len(), 1);

        tokio::time::sleep(Duration::from_millis(100)).await;
        client.handle_timeouts().await;
        assert_eq!(client.inflight.len(), 1, "Not yet timed out");

        for req in &mut client.inflight {
            req.dispatched_at =
                Some(Instant::now() - Duration::from_secs(REQUEST_TIMEOUT_SECS + 1));
        }
        client.handle_timeouts().await;
        assert!(
            client.inflight.is_empty()
                || !client.pending.is_empty()
                || !client.waiting_for_conn.is_empty(),
            "Timed-out request should be moved"
        );
    }

    #[tokio::test]
    async fn test_txn_id_generation() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let mut txn_ids = Vec::new();
        for _ in 0..5 {
            txn_ids.push(client.next_txn());
            client.next_txn();
        }
        let unique: std::collections::HashSet<_> = txn_ids.iter().cloned().collect();
        assert_eq!(
            unique.len(),
            txn_ids.len(),
            "Transaction IDs should all be unique"
        );
    }

    #[tokio::test]
    async fn test_shared_client_creation() {
        let shared = UdpTrackerClient::create_shared(0).await;
        assert!(shared.is_ok(), "Shared client creation should succeed");
    }

    // --- Scrape tests ---

    #[tokio::test]
    async fn test_add_scrape_request() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let hashes = [[0xAAu8; 20], [0xBBu8; 20], [0xCCu8; 20]];

        client.add_scrape(&addr, &hashes).await;
        assert_eq!(client.pending.len(), 1);

        let req = &client.pending[0];
        assert!(!req.scrape_info_hashes.is_empty());
        assert_eq!(req.scrape_info_hashes.len(), 3);
        assert_eq!(req.scrape_info_hashes[0], [0xAAu8; 20]);
    }

    #[tokio::test]
    async fn test_handle_scrape_response_single_hash() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let hashes = [[0x11u8; 20]];

        // Manually set up an in-flight scrape request
        let txn_id = client.next_txn();
        let mut req =
            UdpTrackerRequest::new(addr, hashes[0], [0u8; 20], 0, 0, 0, UdpEvent::None, 0, 0);
        req.txn_id = txn_id;
        req.dispatched_at = Some(Instant::now());
        req.scrape_info_hashes = hashes.to_vec();
        client.inflight.push_back(req);
        client.txn_map.insert(txn_id, 0);

        // Build scrape response: action=2, txn_id, seeders=42, leechers=10, completed=999
        let mut resp_data = vec![0u8; 20];
        resp_data[0..4].copy_from_slice(&(UdpAction::Scrape as i32).to_be_bytes());
        resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        resp_data[8..12].copy_from_slice(&42u32.to_be_bytes());
        resp_data[12..16].copy_from_slice(&10u32.to_be_bytes());
        resp_data[16..20].copy_from_slice(&999u32.to_be_bytes());

        client.handle_response(&resp_data, &addr).await;

        let scrape_results = client.completed_scrape_results();
        assert_eq!(scrape_results.len(), 1);
        assert_eq!(scrape_results[0].len(), 1);
        assert_eq!(scrape_results[0][0].seeders, 42);
        assert_eq!(scrape_results[0][0].leechers, 10);
        assert_eq!(scrape_results[0][0].completed, 999);
    }

    #[tokio::test]
    async fn test_handle_scrape_response_multi_hash() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();
        let hashes = [[0x22u8; 20], [0x33u8; 20]];

        let txn_id = client.next_txn();
        let mut req =
            UdpTrackerRequest::new(addr, hashes[0], [0u8; 20], 0, 0, 0, UdpEvent::None, 0, 0);
        req.txn_id = txn_id;
        req.dispatched_at = Some(Instant::now());
        req.scrape_info_hashes = hashes.to_vec();
        client.inflight.push_back(req);
        client.txn_map.insert(txn_id, 0);

        // Response for 2 info hashes
        let mut resp_data = vec![0u8; 32]; // 8 header + 12*2
        resp_data[0..4].copy_from_slice(&(UdpAction::Scrape as i32).to_be_bytes());
        resp_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        // Hash 1
        resp_data[8..12].copy_from_slice(&100u32.to_be_bytes());
        resp_data[12..16].copy_from_slice(&50u32.to_be_bytes());
        resp_data[16..20].copy_from_slice(&200u32.to_be_bytes());
        // Hash 2
        resp_data[20..24].copy_from_slice(&5u32.to_be_bytes());
        resp_data[24..28].copy_from_slice(&3u32.to_be_bytes());
        resp_data[28..32].copy_from_slice(&7u32.to_be_bytes());

        client.handle_response(&resp_data, &addr).await;

        let scrape_results = client.completed_scrape_results();
        assert_eq!(scrape_results.len(), 1);
        assert_eq!(scrape_results[0].len(), 2);
        assert_eq!(scrape_results[0][0].seeders, 100);
        assert_eq!(scrape_results[0][1].seeders, 5);
    }

    #[tokio::test]
    async fn test_scrape_error_action_returns_error() {
        let mut client = UdpTrackerClient::new(0).await.unwrap();
        let addr: SocketAddr = "127.0.0.1:6969".parse().unwrap();

        let txn_id = client.next_txn();
        let mut req =
            UdpTrackerRequest::new(addr, [0x99u8; 20], [0u8; 20], 0, 0, 0, UdpEvent::None, 0, 0);
        req.txn_id = txn_id;
        req.dispatched_at = Some(Instant::now());
        req.scrape_info_hashes = vec![[0x99u8; 20]];
        client.inflight.push_back(req);
        client.txn_map.insert(txn_id, 0);

        // Send error action instead of scrape action
        let mut err_data = vec![0u8; 23];
        err_data[0..4].copy_from_slice(&3i32.to_be_bytes()); // Error action
        err_data[4..8].copy_from_slice(&txn_id.to_be_bytes());
        err_data[8..23].copy_from_slice(b"scrape failed!!");

        client.handle_response(&err_data, &addr).await;

        // Should not have successful scrape results
        let scrape_results = client.completed_scrape_results();
        assert!(
            scrape_results.is_empty(),
            "Error response should not produce scrape results"
        );
    }
}