torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
Documentation
/// Bencode and wire payload bounds.
pub const MAX_PEER_MESSAGE_PAYLOAD: usize = 256 * 1024;
pub const MAX_PEER_READ_BUF: usize = 512 * 1024;
pub const MAX_DHT_DATAGRAM: usize = 8192;
pub const MAX_EXTENDED_PAYLOAD: usize = 64 * 1024;
pub const MAX_METADATA_BYTES: usize = 10 * 1024 * 1024;
pub const MAX_TORRENT_FILE_BYTES: usize = 64 * 1024 * 1024;

/// Minimum / maximum piece length accepted from untrusted torrent metadata.
pub const MIN_PIECE_LENGTH: u32 = 16 * 1024;
pub const MAX_PIECE_LENGTH: u32 = 16 * 1024 * 1024;

/// Hard cap on total declared content size (guards `set_len` / allocation DoS).
pub const MAX_TORRENT_CONTENT_BYTES: u64 = 1 << 40; // 1 TiB

/// Cap on tracker HTTP(S) announce/scrape response bodies.
pub const MAX_TRACKER_HTTP_RESPONSE: usize = 1024 * 1024;

/// Extra allowance above Range length for HTTP headers on web-seed responses.
pub const MAX_HTTP_HEADER_SLACK: usize = 64 * 1024;

#[derive(Debug, Clone, Copy)]
pub struct DecodeLimits {
    pub max_depth: usize,
    pub max_bytes: usize,
    pub max_items: usize,
}

impl DecodeLimits {
    pub const NETWORK: Self = Self {
        max_depth: 24,
        max_bytes: 64 * 1024,
        max_items: 4096,
    };

    pub const TORRENT_FILE: Self = Self {
        max_depth: 32,
        max_bytes: MAX_TORRENT_FILE_BYTES,
        max_items: 65_536,
    };

    pub const LOCAL_SIDECAR: Self = Self {
        max_depth: 16,
        max_bytes: 4 * 1024 * 1024,
        max_items: 1_048_576,
    };

    /// BEP 9 ut_metadata info-dict decode (aligned with [`MAX_METADATA_BYTES`]).
    pub const METADATA: Self = Self {
        max_depth: 32,
        max_bytes: MAX_METADATA_BYTES,
        max_items: 65_536,
    };

    /// BEP 15 / HTTP tracker response bencode (aligned with [`MAX_TRACKER_HTTP_RESPONSE`]).
    pub const TRACKER: Self = Self {
        max_depth: 16,
        max_bytes: MAX_TRACKER_HTTP_RESPONSE,
        max_items: 8_192,
    };
}