1#![cfg_attr(not(fuzzing), warn(missing_docs))]
15#![cfg_attr(test, allow(dead_code))]
16#![warn(unreachable_pub)]
17#![allow(clippy::cognitive_complexity)]
18#![allow(clippy::too_many_arguments)]
19#![warn(clippy::use_self)]
20
21use std::{
22 fmt,
23 net::{IpAddr, SocketAddr},
24 ops,
25};
26
27mod cid_queue;
29pub mod coding;
30mod constant_time;
31mod range_set;
32pub mod transport_parameters;
33mod varint;
34
35pub use varint::{VarInt, VarIntBoundsExceeded};
36
37pub mod connection;
41pub mod config;
42pub mod frame;
43pub mod endpoint;
44pub mod packet;
45pub mod shared;
46pub mod transport_error;
47mod congestion;
49pub mod cid_generator;
50mod token;
51mod token_memory_cache;
52pub mod candidate_discovery;
53mod connection_establishment_simple;
54pub mod nat_traversal_api;
55
56pub mod transport;
58pub mod nat_traversal;
59pub mod discovery;
60pub mod crypto;
61pub mod api;
62
63pub mod quic_node;
65pub mod terminal_ui;
66pub mod workflow;
67pub mod monitoring;
68pub mod optimization;
69
70pub mod quinn_high_level;
72
73#[cfg(feature = "production-ready")]
75pub use quinn_high_level::{
76 Endpoint,
77 Connection as HighLevelConnection,
78 Connecting,
79 Accept,
80 RecvStream as HighLevelRecvStream,
81 SendStream as HighLevelSendStream,
82};
83
84#[cfg(not(feature = "production-ready"))]
86pub use endpoint::Endpoint;
87
88pub use crypto::raw_public_keys::key_utils::{
90 generate_ed25519_keypair, derive_peer_id_from_public_key,
91 derive_peer_id_from_key_bytes, verify_peer_id,
92 public_key_to_bytes, public_key_from_bytes,
93};
94
95pub use connection::{
97 Connection, ConnectionError, ConnectionStats, Event,
98 RecvStream, SendStream, Streams, StreamEvent, SendDatagramError,
99 Chunk, Chunks, ClosedStream, FinishError, ReadError, ReadableError,
100 WriteError, Written, Datagrams,
101};
102pub use endpoint::{Endpoint as LowLevelEndpoint, ConnectionHandle, Incoming, AcceptError, ConnectError, DatagramEvent};
103pub use shared::{ConnectionId, EcnCodepoint, EndpointEvent};
104pub use transport_error::{Code as TransportErrorCode, Error as TransportError};
105pub use candidate_discovery::{
106 CandidateDiscoveryManager, DiscoveryConfig, DiscoveryEvent, DiscoveryError,
107 NetworkInterface, ValidatedCandidate,
108};
109pub use connection_establishment_simple::{
110 SimpleConnectionEstablishmentManager, SimpleEstablishmentConfig,
111 SimpleConnectionEvent,
112};
113pub use nat_traversal_api::{
114 NatTraversalEndpoint, NatTraversalConfig, EndpointRole, PeerId, BootstrapNode,
115 CandidateAddress, NatTraversalEvent, NatTraversalError, NatTraversalStatistics,
116};
117pub use connection::nat_traversal::{CandidateSource, CandidateState, NatTraversalRole};
118pub use quic_node::{QuicP2PNode, QuicNodeConfig, NodeStats as QuicNodeStats};
119
120#[cfg(fuzzing)]
121pub mod fuzzing;
122
123pub const DEFAULT_SUPPORTED_VERSIONS: &[u32] = &[
129 0x00000001, 0xff00_001d, ];
132
133#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
135#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
136pub enum Side {
137 Client = 0,
139 Server = 1,
141}
142
143impl Side {
144 #[inline]
145 pub fn is_client(self) -> bool {
147 self == Self::Client
148 }
149
150 #[inline]
151 pub fn is_server(self) -> bool {
153 self == Self::Server
154 }
155}
156
157impl ops::Not for Side {
158 type Output = Self;
159 fn not(self) -> Self {
160 match self {
161 Self::Client => Self::Server,
162 Self::Server => Self::Client,
163 }
164 }
165}
166
167#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
169#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
170pub enum Dir {
171 Bi = 0,
173 Uni = 1,
175}
176
177impl Dir {
178 fn iter() -> impl Iterator<Item = Self> {
179 [Self::Bi, Self::Uni].iter().cloned()
180 }
181}
182
183impl fmt::Display for Dir {
184 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185 use Dir::*;
186 f.pad(match *self {
187 Bi => "bidirectional",
188 Uni => "unidirectional",
189 })
190 }
191}
192
193#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
195#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
196pub struct StreamId(u64);
197
198impl fmt::Display for StreamId {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200 let initiator = match self.initiator() {
201 Side::Client => "client",
202 Side::Server => "server",
203 };
204 let dir = match self.dir() {
205 Dir::Uni => "uni",
206 Dir::Bi => "bi",
207 };
208 write!(
209 f,
210 "{} {}directional stream {}",
211 initiator,
212 dir,
213 self.index()
214 )
215 }
216}
217
218impl StreamId {
219 pub fn new(initiator: Side, dir: Dir, index: u64) -> Self {
221 Self((index << 2) | ((dir as u64) << 1) | initiator as u64)
222 }
223 pub fn initiator(self) -> Side {
225 if self.0 & 0x1 == 0 {
226 Side::Client
227 } else {
228 Side::Server
229 }
230 }
231 pub fn dir(self) -> Dir {
233 if self.0 & 0x2 == 0 { Dir::Bi } else { Dir::Uni }
234 }
235 pub fn index(self) -> u64 {
237 self.0 >> 2
238 }
239}
240
241impl From<StreamId> for VarInt {
242 fn from(x: StreamId) -> Self {
243 unsafe { Self::from_u64_unchecked(x.0) }
244 }
245}
246
247impl From<VarInt> for StreamId {
248 fn from(v: VarInt) -> Self {
249 Self(v.0)
250 }
251}
252
253impl From<StreamId> for u64 {
254 fn from(x: StreamId) -> Self {
255 x.0
256 }
257}
258
259impl coding::Codec for StreamId {
260 fn decode<B: bytes::Buf>(buf: &mut B) -> coding::Result<Self> {
261 VarInt::decode(buf).map(|x| Self(x.into_inner()))
262 }
263 fn encode<B: bytes::BufMut>(&self, buf: &mut B) {
264 VarInt::from_u64(self.0).unwrap().encode(buf);
265 }
266}
267
268#[derive(Debug)]
270#[must_use]
271pub struct Transmit {
272 pub destination: SocketAddr,
274 pub ecn: Option<EcnCodepoint>,
276 pub size: usize,
278 pub segment_size: Option<usize>,
281 pub src_ip: Option<IpAddr>,
283}
284
285#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
287pub(crate) use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
288#[cfg(all(target_family = "wasm", target_os = "unknown"))]
289pub(crate) use web_time::{Duration, Instant, SystemTime, UNIX_EPOCH};
290
291pub(crate) const LOC_CID_COUNT: u64 = 8;
297pub(crate) const RESET_TOKEN_SIZE: usize = 16;
298pub(crate) const MAX_CID_SIZE: usize = 20;
299pub(crate) const MIN_INITIAL_SIZE: u16 = 1200;
300pub(crate) const INITIAL_MTU: u16 = 1200;
302pub(crate) const MAX_UDP_PAYLOAD: u16 = 65527;
303pub(crate) const TIMER_GRANULARITY: Duration = Duration::from_millis(1);
304pub(crate) const MAX_STREAM_COUNT: u64 = 1 << 60;
306
307pub(crate) use token::{ResetToken, TokenStore, TokenLog, NoneTokenLog};
309pub(crate) use token_memory_cache::TokenMemoryCache;
310pub(crate) use frame::Frame;
311pub use config::{EndpointConfig, TransportConfig, ServerConfig, AckFrequencyConfig, MtuDiscoveryConfig, ClientConfig};
312pub use cid_generator::RandomConnectionIdGenerator;