1use crate::crypto::CryptoProvider;
4use crate::packet::VERSION_1;
5use std::sync::Arc;
6use std::time::Duration;
7
8#[derive(Clone, Debug)]
10pub struct TransportConfig {
11 pub max_idle_timeout: Duration,
12 pub max_udp_payload_size: u64,
14 pub max_datagram_size: usize,
16 pub mtu_probing: bool,
18 pub max_data: u64,
19 pub stream_window_bidi_local: u64,
20 pub stream_window_bidi_remote: u64,
21 pub stream_window_uni: u64,
22 pub max_streams_bidi: u64,
23 pub max_streams_uni: u64,
24 pub send_buffer: u64,
26 pub ack_delay_exponent: u64,
27 pub max_ack_delay: Duration,
28 pub active_connection_id_limit: u64,
30 pub max_local_cids: u64,
32 pub disable_active_migration: bool,
33 pub crypto_buffer: usize,
35 pub max_ack_ranges: usize,
37 pub pacing: bool,
38 pub key_update_interval: Option<u64>,
40 pub ack_eliciting_threshold: u32,
42}
43
44impl Default for TransportConfig {
45 fn default() -> Self {
46 TransportConfig {
47 max_idle_timeout: Duration::from_secs(30),
48 max_udp_payload_size: 1472,
49 max_datagram_size: 1452,
50 mtu_probing: true,
51 max_data: 4 << 20,
52 stream_window_bidi_local: 1 << 20,
53 stream_window_bidi_remote: 1 << 20,
54 stream_window_uni: 256 << 10,
55 max_streams_bidi: 100,
56 max_streams_uni: 16,
57 send_buffer: 1 << 20,
58 ack_delay_exponent: 3,
59 max_ack_delay: Duration::from_millis(25),
60 active_connection_id_limit: 4,
61 max_local_cids: 4,
62 disable_active_migration: false,
63 crypto_buffer: 64 << 10,
64 max_ack_ranges: 32,
65 pacing: true,
66 key_update_interval: None,
67 ack_eliciting_threshold: 2,
68 }
69 }
70}
71
72pub struct Config {
74 pub provider: Arc<dyn CryptoProvider>,
75 pub transport: TransportConfig,
76 pub reset_key: [u8; 32],
78 pub token_key: [u8; 32],
80 pub versions: Vec<u32>,
82 pub retry: bool,
84 pub retry_threshold: usize,
86 pub retry_token_lifetime: Duration,
88 pub new_token_lifetime: Option<Duration>,
90 pub cid_len: usize,
92 pub max_connections: usize,
94 pub epoch: std::sync::OnceLock<std::time::Instant>,
96}
97
98impl Config {
99 pub fn new(provider: Arc<dyn CryptoProvider>) -> Self {
101 let mut reset_key = [0u8; 32];
102 let mut token_key = [0u8; 32];
103 provider.fill_random(&mut reset_key);
104 provider.fill_random(&mut token_key);
105 Config {
106 provider,
107 transport: TransportConfig::default(),
108 reset_key,
109 token_key,
110 versions: vec![VERSION_1],
111 retry: false,
112 retry_threshold: 1000,
113 retry_token_lifetime: Duration::from_secs(10),
114 new_token_lifetime: None,
115 cid_len: 8,
116 max_connections: 100_000,
117 epoch: std::sync::OnceLock::new(),
118 }
119 }
120
121 pub fn reset_token(&self, cid: &[u8]) -> [u8; 16] {
123 let h = self.provider.hmac(&self.reset_key, &[b"reset", cid]);
124 h[..16].try_into().unwrap()
125 }
126}