aria2-core 0.3.13

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
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicUsize, Ordering};

use tokio::net::TcpStream;

use super::connect_from;

/// The process-wide policy used by every outbound TCP adapter.
///
/// A policy owns an immutable set of source addresses and only keeps small
/// per-source counters on the hot path.  New connections choose the least
/// loaded healthy source for their address family; established connections are
/// still reused by the protocol-specific pools above this seam.
#[derive(Clone, Debug)]
pub struct OutboundNetworkPolicy {
    sources: Arc<[SourceState]>,
}

#[derive(Debug)]
struct SourceState {
    address: IpAddr,
    in_flight: AtomicUsize,
    failures: AtomicU32,
}

struct InFlightConnectionGuard<'a>(&'a AtomicUsize);

impl InFlightConnectionGuard<'_> {
    fn new(in_flight: &AtomicUsize) -> InFlightConnectionGuard<'_> {
        in_flight.fetch_add(1, Ordering::Relaxed);
        InFlightConnectionGuard(in_flight)
    }
}

impl Drop for InFlightConnectionGuard<'_> {
    fn drop(&mut self) {
        self.0.fetch_sub(1, Ordering::Relaxed);
    }
}

impl SourceState {
    fn new(address: IpAddr) -> Self {
        Self {
            address,
            in_flight: AtomicUsize::new(0),
            failures: AtomicU32::new(0),
        }
    }

    fn score(&self) -> usize {
        self.in_flight
            .load(Ordering::Relaxed)
            .saturating_mul(16)
            .saturating_add(self.failures.load(Ordering::Relaxed) as usize)
    }
}

impl OutboundNetworkPolicy {
    /// Create the direct-routing policy used when `--interface` is absent.
    pub fn direct() -> Self {
        Self {
            sources: Arc::new([]),
        }
    }

    pub fn new(addresses: Vec<IpAddr>) -> Result<Self, String> {
        let mut unique = Vec::with_capacity(addresses.len());
        for address in addresses {
            if !unique.contains(&address) {
                unique.push(address);
            }
        }
        if unique.is_empty() {
            return Err("at least one outbound source address is required".to_string());
        }
        Ok(Self {
            sources: unique
                .into_iter()
                .map(SourceState::new)
                .collect::<Vec<_>>()
                .into(),
        })
    }

    pub fn single(address: IpAddr) -> Self {
        Self::new(vec![address]).expect("a single source address is never empty")
    }

    pub fn is_direct(&self) -> bool {
        self.sources.is_empty()
    }

    pub fn addresses(&self) -> Vec<IpAddr> {
        self.sources.iter().map(|source| source.address).collect()
    }

    /// Resolve an aria2-style interface specification into source addresses.
    ///
    /// IP literals are retained as-is.  Host names are resolved through Tokio's
    /// resolver, preserving the existing CLI behaviour for host-based
    /// interface values while keeping resolution out of connection hot paths.
    pub async fn resolve_spec(spec: &str) -> Result<Self, String> {
        let mut addresses = Vec::new();
        for value in spec
            .split(',')
            .map(str::trim)
            .filter(|value| !value.is_empty())
        {
            if let Ok(address) = value.parse::<IpAddr>() {
                if !addresses.contains(&address) {
                    addresses.push(address);
                }
                continue;
            }

            let interface_addresses = interface_addresses(value);
            if !interface_addresses.is_empty() {
                for address in interface_addresses {
                    if !addresses.contains(&address) {
                        addresses.push(address);
                    }
                }
                continue;
            }

            let resolved = tokio::net::lookup_host((value, 0))
                .await
                .map_err(|error| format!("cannot resolve '{value}': {error}"))?;
            for address in resolved.map(|address| address.ip()) {
                if !addresses.contains(&address) {
                    addresses.push(address);
                }
            }
        }
        Self::new(addresses)
    }

    /// Select a source for a protocol client that owns its own connection
    /// pool, such as reqwest.  `None` means the OS should choose the source.
    pub fn source_for(&self, remote: SocketAddr) -> io::Result<Option<IpAddr>> {
        let Some(source) = self.best_source(remote) else {
            if self.is_direct() {
                return Ok(None);
            }
            return Err(family_mismatch(remote));
        };
        Ok(Some(source.address))
    }

    /// Select a compatible local source for a client-owned connection pool.
    /// The direct policy returns `None` without resolving the host, leaving
    /// DNS and proxy routing to that client.
    pub async fn source_for_host(&self, host: &str, port: u16) -> io::Result<Option<IpAddr>> {
        // With no explicit source/interface constraint, the HTTP client owns
        // hostname resolution (including its proxy and DNS configuration).
        // Pre-resolving here is both unnecessary and can reject a request
        // before that client gets a chance to resolve it.
        if self.is_direct() {
            return Ok(None);
        }

        let addresses = tokio::net::lookup_host((host, port)).await?;
        for remote in addresses {
            if let Ok(source) = self.source_for(remote) {
                return Ok(source);
            }
        }
        Err(family_mismatch(SocketAddr::new(
            IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED),
            port,
        )))
    }

    /// Resolve a UDP peer and retain the first address compatible with this
    /// policy's configured source families.
    pub async fn resolve_udp_host(&self, host: &str, port: u16) -> io::Result<SocketAddr> {
        let host = host
            .strip_prefix('[')
            .and_then(|host| host.strip_suffix(']'))
            .unwrap_or(host);
        if let Ok(address) = host.parse::<IpAddr>() {
            let remote = SocketAddr::new(address, port);
            if self.is_direct() || self.best_source(remote).is_some() {
                return Ok(remote);
            }
            return Err(family_mismatch(remote));
        }

        let addresses = tokio::net::lookup_host((host, port)).await?;
        for remote in addresses {
            if self.is_direct() || self.best_source(remote).is_some() {
                return Ok(remote);
            }
        }
        Err(family_mismatch(SocketAddr::new(
            IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED),
            port,
        )))
    }

    /// Establish a TCP connection to a hostname through the policy.
    pub async fn connect_host(&self, host: &str, port: u16) -> io::Result<TcpStream> {
        let addresses = tokio::net::lookup_host((host, port)).await?;
        let mut last_error = None;
        for remote in addresses {
            if !self.is_direct() && self.best_source(remote).is_none() {
                continue;
            }
            match self.connect(remote).await {
                Ok(stream) => return Ok(stream),
                Err(error) => last_error = Some(error),
            }
        }
        Err(last_error.unwrap_or_else(|| {
            io::Error::new(io::ErrorKind::AddrNotAvailable, "no compatible address")
        }))
    }

    /// Establish a TCP connection through the policy.  This is the common
    /// seam used by FTP, SFTP and BitTorrent adapters.
    pub async fn connect(&self, remote: SocketAddr) -> io::Result<TcpStream> {
        let Some(source) = self.best_source(remote) else {
            if self.is_direct() {
                return TcpStream::connect(remote).await;
            }
            return Err(family_mismatch(remote));
        };

        let in_flight_guard = InFlightConnectionGuard::new(&source.in_flight);
        let result = connect_from(source.address, remote).await;
        drop(in_flight_guard);
        let failed = result.is_err();
        let mut failures = source.failures.load(Ordering::Relaxed);
        loop {
            let updated = if failed {
                failures.saturating_add(1)
            } else {
                failures.saturating_sub(1)
            };
            match source.failures.compare_exchange_weak(
                failures,
                updated,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(observed) => failures = observed,
            }
        }
        result
    }

    /// Bind an outbound UDP socket to a configured source address. UDP
    /// clients keep this socket and reuse it for all datagrams.
    pub async fn bind_udp(&self, port: u16) -> io::Result<tokio::net::UdpSocket> {
        let local = self
            .sources
            .first()
            .map(|source| SocketAddr::new(source.address, port))
            .unwrap_or_else(|| SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED), port));
        tokio::net::UdpSocket::bind(local).await
    }

    /// Bind an outbound UDP socket for a specific address family.
    ///
    /// UDP sockets are family-specific, so callers that talk to both IPv4 and
    /// IPv6 endpoints must create one socket per family instead of relying on
    /// the first configured source address.
    pub async fn bind_udp_for_family(
        &self,
        port: u16,
        ipv6: bool,
    ) -> io::Result<tokio::net::UdpSocket> {
        let local = if self.is_direct() {
            SocketAddr::new(
                if ipv6 {
                    IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED)
                } else {
                    IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)
                },
                port,
            )
        } else {
            let source = self
                .sources
                .iter()
                .filter(|source| source.address.is_ipv6() == ipv6)
                .min_by_key(|source| source.score())
                .ok_or_else(|| {
                    family_mismatch(SocketAddr::new(
                        if ipv6 {
                            IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED)
                        } else {
                            IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)
                        },
                        port,
                    ))
                })?;
            SocketAddr::new(source.address, port)
        };
        tokio::net::UdpSocket::bind(local).await
    }

    fn best_source(&self, remote: SocketAddr) -> Option<&SourceState> {
        self.sources
            .iter()
            .filter(|source| source.address.is_ipv4() == remote.is_ipv4())
            .min_by_key(|source| source.score())
    }
}

#[cfg(unix)]
fn interface_addresses(name: &str) -> Vec<IpAddr> {
    use std::ffi::CStr;
    use std::ptr;

    let mut result = Vec::new();
    unsafe {
        let mut head = ptr::null_mut();
        if libc::getifaddrs(&mut head) != 0 {
            return result;
        }
        let mut current = head;
        while !current.is_null() {
            let interface = &*current;
            let matches = !interface.ifa_name.is_null()
                && CStr::from_ptr(interface.ifa_name).to_bytes() == name.as_bytes();
            if matches && !interface.ifa_addr.is_null() {
                match (*interface.ifa_addr).sa_family as i32 {
                    libc::AF_INET => {
                        let address = *(interface.ifa_addr as *const libc::sockaddr_in);
                        result.push(IpAddr::V4(std::net::Ipv4Addr::from(
                            u32::from_be(address.sin_addr.s_addr).to_be_bytes(),
                        )));
                    }
                    libc::AF_INET6 => {
                        let address = *(interface.ifa_addr as *const libc::sockaddr_in6);
                        result.push(IpAddr::V6(std::net::Ipv6Addr::from(
                            address.sin6_addr.s6_addr,
                        )));
                    }
                    _ => {}
                }
            }
            current = interface.ifa_next;
        }
        libc::freeifaddrs(head);
    }
    result
}

#[cfg(windows)]
fn interface_addresses(name: &str) -> Vec<IpAddr> {
    use std::ffi::CStr;
    use windows_sys::Win32::NetworkManagement::IpHelper::{
        GetAdaptersAddresses, IP_ADAPTER_ADDRESSES_LH,
    };
    use windows_sys::Win32::Networking::WinSock::{
        AF_INET, AF_INET6, AF_UNSPEC, SOCKADDR_IN, SOCKADDR_IN6,
    };

    let mut size = 15_000u32;
    let mut buffer = vec![0u8; size as usize];
    let status = unsafe {
        GetAdaptersAddresses(
            AF_UNSPEC as u32,
            0,
            std::ptr::null(),
            buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
            &mut size,
        )
    };
    if status != 0 {
        return Vec::new();
    }

    let mut result = Vec::new();
    let mut adapter = buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH;
    while !adapter.is_null() {
        let current = unsafe { &*adapter };
        let adapter_name = if current.AdapterName.is_null() {
            "".to_string()
        } else {
            unsafe { CStr::from_ptr(current.AdapterName as *const i8) }
                .to_string_lossy()
                .into_owned()
        };
        let friendly_name = if current.FriendlyName.is_null() {
            String::new()
        } else {
            let mut length = 0;
            unsafe {
                while *current.FriendlyName.add(length) != 0 {
                    length += 1;
                }
                String::from_utf16_lossy(std::slice::from_raw_parts(current.FriendlyName, length))
            }
        };
        if adapter_name == name || friendly_name == name {
            let mut address = current.FirstUnicastAddress;
            while !address.is_null() {
                let socket_address = unsafe { (*address).Address.lpSockaddr };
                if !socket_address.is_null() {
                    let family = unsafe { (*socket_address).sa_family };
                    unsafe {
                        if family == AF_INET {
                            let value = *(socket_address as *const SOCKADDR_IN);
                            result.push(IpAddr::V4(std::net::Ipv4Addr::from(
                                value.sin_addr.S_un.S_addr.to_ne_bytes(),
                            )));
                        } else if family == AF_INET6 {
                            let value = *(socket_address as *const SOCKADDR_IN6);
                            result
                                .push(IpAddr::V6(std::net::Ipv6Addr::from(value.sin6_addr.u.Byte)));
                        }
                    }
                }
                address = unsafe { (*address).Next };
            }
            break;
        }
        adapter = current.Next;
    }
    result
}

fn family_mismatch(remote: SocketAddr) -> io::Error {
    io::Error::new(
        io::ErrorKind::AddrNotAvailable,
        format!("no configured outbound source matches {remote}"),
    )
}

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

    #[tokio::test]
    async fn direct_policy_does_not_require_hostname_resolution_for_source_selection() {
        let policy = OutboundNetworkPolicy::direct();

        assert_eq!(policy.source_for_host("\0", 80).await.unwrap(), None);
    }

    #[tokio::test]
    async fn policy_connect_uses_configured_source_address() {
        let listener = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0))
            .await
            .unwrap();
        let remote = listener.local_addr().unwrap();
        let policy = OutboundNetworkPolicy::single(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));

        let client = policy.connect(remote).await.unwrap();
        let (_, peer) = listener.accept().await.unwrap();
        assert_eq!(client.local_addr().unwrap().ip(), peer.ip());
        assert_eq!(peer.ip(), IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));
    }

    #[tokio::test]
    async fn policy_rejects_a_family_without_a_matching_source() {
        let policy = OutboundNetworkPolicy::single(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));
        let error = policy
            .connect(SocketAddr::new(std::net::Ipv6Addr::LOCALHOST.into(), 1))
            .await
            .unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::AddrNotAvailable);
    }

    #[tokio::test]
    async fn udp_host_resolution_accepts_bracketed_ipv6_literals_with_matching_source() {
        let policy = OutboundNetworkPolicy::new(vec![
            IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 2)),
            IpAddr::V6(std::net::Ipv6Addr::LOCALHOST),
        ])
        .unwrap();

        assert_eq!(
            policy.resolve_udp_host("[::1]", 6881).await.unwrap(),
            SocketAddr::new(IpAddr::V6(std::net::Ipv6Addr::LOCALHOST), 6881)
        );
    }

    #[test]
    fn source_selection_prefers_the_less_loaded_source() {
        let policy = OutboundNetworkPolicy::new(vec![
            IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
            IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 2)),
        ])
        .unwrap();
        policy.sources[0].in_flight.store(3, Ordering::Relaxed);
        assert_eq!(
            policy.source_for("127.0.0.1:1".parse().unwrap()).unwrap(),
            Some(IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 2)))
        );
    }

    #[test]
    fn cancelled_connection_releases_its_in_flight_source_slot() {
        let policy = OutboundNetworkPolicy::single(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));
        {
            let _guard = InFlightConnectionGuard::new(&policy.sources[0].in_flight);
            assert_eq!(policy.sources[0].in_flight.load(Ordering::Relaxed), 1);
        }
        assert_eq!(policy.sources[0].in_flight.load(Ordering::Relaxed), 0);
    }
}