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
//! `no_std + alloc` download-engine core: **BitTorrent v1/v2** (peer wire,
//! DHT, PEX, LSD, uTP, web seeds, UDP/HTTP trackers, magnet, SOCKS5) — plus
//! provable download receipts, a utility scheduler and a disk cache.
//! [`links::parse_link`] also *parses* eD2k / Thunder / QQDL / FlashGet /
//! IPFS / Kad / Baidu / Xunlei-Netdisk links into typed [`links::DownloadLink`]
//! values (parsing only — no fetch adapters for those protocols yet, see the
//! README protocol matrix). Transport is injected via [`platform::Host`];
//! crypto is in-tree under [`crypto`].
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "ffi"), deny(unsafe_code))]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#[macro_use]
extern crate alloc;
pub mod bencode;
pub mod bitfield;
pub mod crypto;
pub mod dht;
pub mod disk_cache;
pub mod engine;
pub mod error;
pub mod geo;
pub mod leech;
pub mod links;
pub mod lsd;
pub mod magnet;
pub mod metainfo;
pub mod monitoring;
pub mod peer_id;
pub mod pex;
pub mod picker;
pub mod piece;
pub mod platform;
pub mod portmap;
pub mod ratelimit;
pub mod receipt;
pub mod scheduler;
pub mod session;
pub mod socks;
pub mod state;
pub mod swarm;
pub mod tracker;
pub mod trackerlist;
pub mod utp;
pub mod verify;
pub mod wire;
// OS-backed host (feature "std") and C ABI bridge (feature "ffi") are
// compiled into the same crate so the whole engine ships as one package.
#[cfg(feature = "ffi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
pub mod ffi;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod host_std;
pub use engine::{Engine, EngineConfig, EngineEvent};
pub use error::{Error, Result};
pub use metainfo::InfoHash;
pub use platform::{Host, LogLevel, NetAddr};
/// Protocol constants.
pub mod consts {
/// Standard BitTorrent piece block size.
pub const BLOCK_LEN: u32 = 16 * 1024;
/// The peer-wire handshake protocol identifier (BEP-3).
pub const PSTR: &[u8] = b"BitTorrent protocol";
/// Default listen port advertised to the swarm.
pub const DEFAULT_PORT: u16 = 6881;
/// Default piece length used by v1 torrents when absent.
pub const DEFAULT_PIECE_LEN: u32 = 256 * 1024;
/// Version tag embedded in the peer id.
pub const VERSION_TAG: &str = "TB10";
/// DHT bootstrap nodes (BEP-5) — the well-known, permanently online
/// routers used by the mainstream clients (uTorrent, qBittorrent,
/// Aria2, Transmission, BitComet). More seeds = more initial diversity:
/// a client that only pings a couple of alive routers snowballs far
/// slower than qBittorrent/libtorrent (which also carry a large
/// persisted table across restarts).
pub const DHT_BOOTSTRAP: &[(&str, u16)] = &[
("router.bittorrent.com", 6881),
("dht.transmissionbt.com", 6881),
("router.utorrent.com", 6881),
("router.transmissionbt.com", 6881),
("dht.libtorrent.org", 25401),
("dht.aelitis.com", 6881),
("dht.bitcomet.com", 6881),
("router.bitcomet.com", 6881),
("dht.dhtool.com", 6881),
];
/// Source for the community tracker list (qBittorrent/BitComet
/// compatible; the host refreshes it at runtime via `http_get`).
pub const TRACKERS_LIST_URL: &str = "https://cf.trackerslist.com/all.txt";
/// Default maximum number of outstanding request blocks per peer
/// connection (the "request pipeline depth"). 32 blocks × 16 KiB =
/// ≤ 512 KiB in flight per peer — enough to saturate a fat pipe while
/// bounding memory and making slow peers recoverable. This is the
/// value libtorrent-class clients use; the session config can override
/// it per torrent.
pub const REQUEST_PIPELINE: u32 = 32;
/// Default per-request timeout (ms): a block requested from a peer that
/// is not answered within this window is released and re-issued to
/// another peer. 20 s is the classic libtorrent block timeout.
pub const REQUEST_TIMEOUT_MS: u64 = 20_000;
/// Default number of consecutive request timeouts on one peer before it
/// is disconnected and blacklisted (mechanism 2's punishment limit).
///
/// Deliberately forgiving (8): the request timeout is pipeline-aware, and
/// the counter resets on ANY delivered block, so a peer must deliver
/// NOTHING across 8 full timeout windows to earn a ban. Aggressive
/// values (e.g. 2–3) burn healthy seeds on congested links and collapse
/// the swarm mid-download.
pub const MAX_REQUEST_TIMEOUTS: u32 = 8;
/// Recommended write-back cache budget (bytes).
pub const DEFAULT_CACHE_BYTES: u64 = 256 * 1024 * 1024;
/// Upper bound on magnet metadata size (a .torrent info dict is small).
pub const MAX_METADATA_SIZE: u32 = 64 * 1024 * 1024;
}