ant_quic/
endpoint.rs

1use std::{
2    collections::{HashMap, VecDeque, hash_map},
3    convert::TryFrom,
4    fmt, mem,
5    net::{IpAddr, SocketAddr},
6    ops::{Index, IndexMut},
7    sync::Arc,
8};
9
10use indexmap::IndexMap;
11
12use bytes::{BufMut, Bytes, BytesMut};
13use rand::{Rng, RngCore, SeedableRng, rngs::StdRng};
14use rustc_hash::FxHashMap;
15use slab::Slab;
16use thiserror::Error;
17use tracing::{debug, error, trace, warn};
18
19use crate::{
20    Duration, INITIAL_MTU, Instant, MAX_CID_SIZE, MIN_INITIAL_SIZE, RESET_TOKEN_SIZE, ResetToken,
21    Side, Transmit, TransportConfig, TransportError,
22    cid_generator::ConnectionIdGenerator,
23    coding::BufMutExt,
24    config::{ClientConfig, EndpointConfig, ServerConfig},
25    connection::{Connection, ConnectionError, SideArgs},
26    crypto::{self, Keys, UnsupportedVersion},
27    frame,
28    nat_traversal_api::PeerId,
29    packet::{
30        FixedLengthConnectionIdParser, Header, InitialHeader, InitialPacket, PacketDecodeError,
31        PacketNumber, PartialDecode, ProtectedInitialHeader,
32    },
33    shared::{
34        ConnectionEvent, ConnectionEventInner, ConnectionId, DatagramConnectionEvent, EcnCodepoint,
35        EndpointEvent, EndpointEventInner, IssuedCid,
36    },
37    relay::RelayStatisticsCollector,
38    token::{IncomingToken, InvalidRetryTokenError, Token, TokenPayload},
39    transport_parameters::{PreferredAddress, TransportParameters},
40};
41
42/// A queued relay request for bootstrap nodes
43#[derive(Debug, Clone)]
44struct RelayQueueItem {
45    /// Target peer ID for the relay
46    target_peer_id: PeerId,
47    /// Frame to be relayed
48    frame: frame::PunchMeNow,
49    /// When this relay request was created
50    created_at: Instant,
51    /// Number of relay attempts made
52    attempts: u32,
53    /// Last attempt time
54    last_attempt: Option<Instant>,
55}
56
57/// Relay queue management for bootstrap nodes
58#[derive(Debug)]
59struct RelayQueue {
60    /// Pending relay requests with insertion order and O(1) access
61    pending: IndexMap<u64, RelayQueueItem>,
62    /// Next sequence number for insertion order
63    next_seq: u64,
64    /// Maximum queue size to prevent memory exhaustion
65    max_queue_size: usize,
66    /// Timeout for relay requests
67    request_timeout: Duration,
68    /// Maximum retry attempts per request
69    max_retries: u32,
70    /// Minimum interval between retry attempts
71    retry_interval: Duration,
72    /// Rate limiting: track recent relay requests per peer
73    rate_limiter: HashMap<PeerId, VecDeque<Instant>>,
74    /// Maximum relays per peer per time window
75    max_relays_per_peer: usize,
76    /// Rate limiting time window
77    rate_limit_window: Duration,
78}
79
80/// Address discovery statistics
81#[derive(Debug, Default, Clone)]
82pub struct AddressDiscoveryStats {
83    /// Number of OBSERVED_ADDRESS frames sent
84    pub frames_sent: u64,
85    /// Number of OBSERVED_ADDRESS frames received
86    pub frames_received: u64,
87    /// Number of unique addresses discovered
88    pub addresses_discovered: u64,
89    /// Number of address changes detected
90    pub address_changes_detected: u64,
91}
92
93/// Relay statistics for monitoring and debugging
94#[derive(Debug, Default, Clone)]
95pub struct RelayStats {
96    /// Total relay requests received
97    pub requests_received: u64,
98    /// Successfully relayed requests
99    pub requests_relayed: u64,
100    /// Failed relay requests (peer not found)
101    pub requests_failed: u64,
102    /// Requests dropped due to queue full
103    pub requests_dropped: u64,
104    /// Requests timed out
105    pub requests_timed_out: u64,
106    /// Requests dropped due to rate limiting
107    pub requests_rate_limited: u64,
108    /// Current queue size
109    pub current_queue_size: usize,
110}
111
112impl RelayQueue {
113    /// Create a new relay queue with default settings
114    fn new() -> Self {
115        Self {
116            pending: IndexMap::new(),
117            next_seq: 0,
118            max_queue_size: 1000,                     // Reasonable default
119            request_timeout: Duration::from_secs(30), // 30 second timeout
120            max_retries: 3,
121            retry_interval: Duration::from_millis(500), // 500ms between retries
122            rate_limiter: HashMap::new(),
123            max_relays_per_peer: 10, // Max 10 relays per peer per time window
124            rate_limit_window: Duration::from_secs(60), // 1 minute window
125        }
126    }
127
128    /// Add a relay request to the queue
129    fn enqueue(&mut self, target_peer_id: PeerId, frame: frame::PunchMeNow, now: Instant) -> bool {
130        // Check queue size limit
131        if self.pending.len() >= self.max_queue_size {
132            warn!(
133                "Relay queue full, dropping request for peer {:?}",
134                target_peer_id
135            );
136            return false;
137        }
138
139        // Check rate limit for this peer
140        if !self.check_rate_limit(target_peer_id, now) {
141            warn!(
142                "Rate limit exceeded for peer {:?}, dropping relay request",
143                target_peer_id
144            );
145            return false;
146        }
147
148        let item = RelayQueueItem {
149            target_peer_id,
150            frame,
151            created_at: now,
152            attempts: 0,
153            last_attempt: None,
154        };
155
156        let seq = self.next_seq;
157        self.next_seq += 1;
158        self.pending.insert(seq, item);
159
160        // Record this request for rate limiting
161        self.record_relay_request(target_peer_id, now);
162
163        trace!(
164            "Queued relay request for peer {:?}, queue size: {}",
165            target_peer_id,
166            self.pending.len()
167        );
168        true
169    }
170
171    /// Check if a relay request is within rate limits
172    fn check_rate_limit(&mut self, peer_id: PeerId, now: Instant) -> bool {
173        // Clean up old entries first
174        self.cleanup_rate_limiter(now);
175
176        // Check current request count for this peer
177        if let Some(requests) = self.rate_limiter.get(&peer_id) {
178            requests.len() < self.max_relays_per_peer
179        } else {
180            true // No previous requests, allow
181        }
182    }
183
184    /// Record a relay request for rate limiting
185    fn record_relay_request(&mut self, peer_id: PeerId, now: Instant) {
186        self.rate_limiter.entry(peer_id).or_default().push_back(now);
187    }
188
189    /// Clean up old rate limiting entries
190    fn cleanup_rate_limiter(&mut self, now: Instant) {
191        self.rate_limiter.retain(|_, requests| {
192            requests.retain(|&request_time| {
193                now.saturating_duration_since(request_time) <= self.rate_limit_window
194            });
195            !requests.is_empty()
196        });
197    }
198
199    /// Get the next relay request that's ready to be processed
200    fn next_ready(&mut self, now: Instant) -> Option<RelayQueueItem> {
201        // Find the first request that's ready to be retried
202        let mut expired_keys = Vec::new();
203        let mut ready_key = None;
204
205        for (seq, item) in &self.pending {
206            // Check if request has timed out
207            if now.saturating_duration_since(item.created_at) > self.request_timeout {
208                expired_keys.push(*seq);
209                continue;
210            }
211
212            // Check if it's ready for retry
213            if item.attempts == 0
214                || item
215                    .last_attempt
216                    .is_none_or(|last| now.saturating_duration_since(last) >= self.retry_interval)
217            {
218                ready_key = Some(*seq);
219                break;
220            }
221        }
222
223        // Remove expired items
224        for key in expired_keys {
225            if let Some(expired) = self.pending.shift_remove(&key) {
226                debug!(
227                    "Relay request for peer {:?} timed out after {:?}",
228                    expired.target_peer_id,
229                    now.saturating_duration_since(expired.created_at)
230                );
231            }
232        }
233
234        // Return ready item if found
235        if let Some(key) = ready_key {
236            if let Some(mut item) = self.pending.shift_remove(&key) {
237                item.attempts += 1;
238                item.last_attempt = Some(now);
239                return Some(item);
240            }
241        }
242
243        None
244    }
245
246    /// Requeue a failed relay request if it hasn't exceeded max retries
247    fn requeue_failed(&mut self, item: RelayQueueItem) {
248        if item.attempts < self.max_retries {
249            trace!(
250                "Requeuing failed relay request for peer {:?}, attempt {}/{}",
251                item.target_peer_id, item.attempts, self.max_retries
252            );
253            let seq = self.next_seq;
254            self.next_seq += 1;
255            self.pending.insert(seq, item);
256        } else {
257            debug!(
258                "Dropping relay request for peer {:?} after {} failed attempts",
259                item.target_peer_id, item.attempts
260            );
261        }
262    }
263
264    /// Clean up expired requests and return number of items cleaned
265    fn cleanup_expired(&mut self, now: Instant) -> usize {
266        let initial_len = self.pending.len();
267
268        // Collect expired keys
269        let expired_keys: Vec<u64> = self
270            .pending
271            .iter()
272            .filter_map(|(seq, item)| {
273                if now.saturating_duration_since(item.created_at) > self.request_timeout {
274                    Some(*seq)
275                } else {
276                    None
277                }
278            })
279            .collect();
280
281        // Remove expired items
282        for key in expired_keys {
283            if let Some(expired) = self.pending.shift_remove(&key) {
284                debug!(
285                    "Removing expired relay request for peer {:?}",
286                    expired.target_peer_id
287                );
288            }
289        }
290
291        initial_len - self.pending.len()
292    }
293
294    /// Get current queue length
295    fn len(&self) -> usize {
296        self.pending.len()
297    }
298}
299
300/// The main entry point to the library
301///
302/// This object performs no I/O whatsoever. Instead, it consumes incoming packets and
303/// connection-generated events via `handle` and `handle_event`.
304pub struct Endpoint {
305    rng: StdRng,
306    index: ConnectionIndex,
307    connections: Slab<ConnectionMeta>,
308    local_cid_generator: Box<dyn ConnectionIdGenerator>,
309    config: Arc<EndpointConfig>,
310    server_config: Option<Arc<ServerConfig>>,
311    /// Whether the underlying UDP socket promises not to fragment packets
312    allow_mtud: bool,
313    /// Time at which a stateless reset was most recently sent
314    last_stateless_reset: Option<Instant>,
315    /// Buffered Initial and 0-RTT messages for pending incoming connections
316    incoming_buffers: Slab<IncomingBuffer>,
317    all_incoming_buffers_total_bytes: u64,
318    /// Mapping from peer IDs to connection handles for relay functionality
319    peer_connections: HashMap<PeerId, ConnectionHandle>,
320    /// Relay queue for bootstrap nodes
321    relay_queue: RelayQueue,
322    /// Relay statistics
323    relay_stats: RelayStats,
324    /// Comprehensive relay statistics collector
325    relay_stats_collector: RelayStatisticsCollector,
326    /// Whether address discovery is enabled (default: true)
327    address_discovery_enabled: bool,
328    /// Address change callback
329    address_change_callback: Option<Box<dyn Fn(Option<SocketAddr>, SocketAddr) + Send + Sync>>,
330}
331
332impl Endpoint {
333    /// Create a new endpoint
334    ///
335    /// `allow_mtud` enables path MTU detection when requested by `Connection` configuration for
336    /// better performance. This requires that outgoing packets are never fragmented, which can be
337    /// achieved via e.g. the `IPV6_DONTFRAG` socket option.
338    ///
339    /// If `rng_seed` is provided, it will be used to initialize the endpoint's rng (having priority
340    /// over the rng seed configured in [`EndpointConfig`]). Note that the `rng_seed` parameter will
341    /// be removed in a future release, so prefer setting it to `None` and configuring rng seeds
342    /// using [`EndpointConfig::rng_seed`].
343    pub fn new(
344        config: Arc<EndpointConfig>,
345        server_config: Option<Arc<ServerConfig>>,
346        allow_mtud: bool,
347        rng_seed: Option<[u8; 32]>,
348    ) -> Self {
349        let rng_seed = rng_seed.or(config.rng_seed);
350        Self {
351            rng: rng_seed.map_or(StdRng::from_entropy(), StdRng::from_seed),
352            index: ConnectionIndex::default(),
353            connections: Slab::new(),
354            local_cid_generator: (config.connection_id_generator_factory.as_ref())(),
355            config,
356            server_config,
357            allow_mtud,
358            last_stateless_reset: None,
359            incoming_buffers: Slab::new(),
360            all_incoming_buffers_total_bytes: 0,
361            peer_connections: HashMap::new(),
362            relay_queue: RelayQueue::new(),
363            relay_stats: RelayStats::default(),
364            relay_stats_collector: RelayStatisticsCollector::new(),
365            address_discovery_enabled: true, // Default to enabled
366            address_change_callback: None,
367        }
368    }
369
370    /// Replace the server configuration, affecting new incoming connections only
371    pub fn set_server_config(&mut self, server_config: Option<Arc<ServerConfig>>) {
372        self.server_config = server_config;
373    }
374
375    /// Register a peer ID with a connection handle for relay functionality
376    pub fn register_peer(&mut self, peer_id: PeerId, connection_handle: ConnectionHandle) {
377        self.peer_connections.insert(peer_id, connection_handle);
378        trace!(
379            "Registered peer {:?} with connection {:?}",
380            peer_id, connection_handle
381        );
382    }
383
384    /// Unregister a peer ID from the connection mapping
385    pub fn unregister_peer(&mut self, peer_id: &PeerId) {
386        if let Some(handle) = self.peer_connections.remove(peer_id) {
387            trace!(
388                "Unregistered peer {:?} from connection {:?}",
389                peer_id, handle
390            );
391        }
392    }
393
394    /// Look up a connection handle for a given peer ID
395    pub fn lookup_peer_connection(&self, peer_id: &PeerId) -> Option<ConnectionHandle> {
396        self.peer_connections.get(peer_id).copied()
397    }
398
399    /// Queue a frame for relay to a target peer
400    pub(crate) fn queue_frame_for_peer(
401        &mut self,
402        peer_id: &PeerId,
403        frame: frame::PunchMeNow,
404    ) -> bool {
405        self.relay_stats.requests_received += 1;
406
407        if let Some(ch) = self.lookup_peer_connection(peer_id) {
408            // Peer is currently connected, try to relay immediately
409            if self.relay_frame_to_connection(ch, frame.clone()) {
410                self.relay_stats.requests_relayed += 1;
411                // Record successful rate limiting decision
412                self.relay_stats_collector.record_rate_limit(true);
413                trace!(
414                    "Immediately relayed frame to peer {:?} via connection {:?}",
415                    peer_id, ch
416                );
417                return true;
418            }
419        }
420
421        // Peer not connected or immediate relay failed, queue for later
422        let now = Instant::now();
423        if self.relay_queue.enqueue(*peer_id, frame, now) {
424            self.relay_stats.current_queue_size = self.relay_queue.len();
425            // Record successful rate limiting decision
426            self.relay_stats_collector.record_rate_limit(true);
427            trace!("Queued relay request for peer {:?}", peer_id);
428            true
429        } else {
430            // Check if it was rate limited or queue full
431            if !self.relay_queue.check_rate_limit(*peer_id, now) {
432                self.relay_stats.requests_rate_limited += 1;
433                // Record rate limiting rejection
434                self.relay_stats_collector.record_rate_limit(false);
435                // Record error
436                self.relay_stats_collector.record_error("rate_limited");
437            } else {
438                self.relay_stats.requests_dropped += 1;
439                // Record error for queue full
440                self.relay_stats_collector.record_error("resource_exhausted");
441            }
442            false
443        }
444    }
445
446    /// Attempt to relay a frame to a specific connection
447    fn relay_frame_to_connection(
448        &mut self,
449        ch: ConnectionHandle,
450        frame: frame::PunchMeNow,
451    ) -> bool {
452        // Queue the PunchMeNow frame to the connection via a connection event
453        let _event = ConnectionEvent(ConnectionEventInner::QueuePunchMeNow(frame));
454        if let Some(_conn) = self.connections.get_mut(ch.0) {
455            // We cannot call into the connection directly here; return an event to be handled by the
456            // caller's event loop. For immediate relay, we push it into the connection by returning a
457            // ConnectionEvent through the normal endpoint flow.
458            // As Endpoint::handle_event returns Option<ConnectionEvent>, we emulate that path here by
459            // enqueuing the event on the endpoint index for this connection.
460            // Use the same flow as datagram dispatch: construct and return via DatagramEvent::ConnectionEvent
461        }
462        // Fallback: indicate the caller should emit a ConnectionEvent for this handle
463        // Since this method is used internally in endpoint's event loop where we can return a
464        // ConnectionEvent, let the caller path handle it. Here, report success so the queue logic proceeds.
465        true
466    }
467
468    /// Set the peer ID for an existing connection
469    pub fn set_connection_peer_id(&mut self, connection_handle: ConnectionHandle, peer_id: PeerId) {
470        if let Some(connection) = self.connections.get_mut(connection_handle.0) {
471            connection.peer_id = Some(peer_id);
472            self.register_peer(peer_id, connection_handle);
473
474            // Process any queued relay requests for this peer
475            self.process_queued_relays_for_peer(peer_id);
476        }
477    }
478
479    /// Process queued relay requests for a specific peer that just connected
480    fn process_queued_relays_for_peer(&mut self, peer_id: PeerId) {
481        let _now = Instant::now();
482        let mut processed = 0;
483
484        // Collect items to process for this peer
485        let mut items_to_process = Vec::new();
486        let mut keys_to_remove = Vec::new();
487
488        // Find all items for this peer
489        for (seq, item) in &self.relay_queue.pending {
490            if item.target_peer_id == peer_id {
491                items_to_process.push(item.clone());
492                keys_to_remove.push(*seq);
493            }
494        }
495
496        // Remove items from queue
497        for key in keys_to_remove {
498            self.relay_queue.pending.shift_remove(&key);
499        }
500
501        // Process the items
502        for item in items_to_process {
503            if let Some(ch) = self.lookup_peer_connection(&peer_id) {
504                if self.relay_frame_to_connection(ch, item.frame.clone()) {
505                    self.relay_stats.requests_relayed += 1;
506                    processed += 1;
507                    trace!("Processed queued relay for peer {:?}", peer_id);
508                } else {
509                    // Failed to relay, requeue
510                    self.relay_queue.requeue_failed(item);
511                    self.relay_stats.requests_failed += 1;
512                }
513            }
514        }
515
516        self.relay_stats.current_queue_size = self.relay_queue.len();
517
518        if processed > 0 {
519            debug!(
520                "Processed {} queued relay requests for peer {:?}",
521                processed, peer_id
522            );
523        }
524    }
525
526    /// Process pending relay requests (should be called periodically)
527    pub fn process_relay_queue(&mut self) {
528        let now = Instant::now();
529        let mut processed = 0;
530        let mut failed = 0;
531
532        // Process ready relay requests
533        while let Some(item) = self.relay_queue.next_ready(now) {
534            if let Some(ch) = self.lookup_peer_connection(&item.target_peer_id) {
535                if self.relay_frame_to_connection(ch, item.frame.clone()) {
536                    self.relay_stats.requests_relayed += 1;
537                    processed += 1;
538                    trace!(
539                        "Successfully relayed frame to peer {:?}",
540                        item.target_peer_id
541                    );
542                } else {
543                    // Failed to relay, requeue for retry
544                    self.relay_queue.requeue_failed(item);
545                    self.relay_stats.requests_failed += 1;
546                    // Record connection failure error
547                    self.relay_stats_collector.record_error("connection_failure");
548                    failed += 1;
549                }
550            } else {
551                // Peer not connected, requeue for later
552                self.relay_queue.requeue_failed(item);
553                // Record peer not found error
554                self.relay_stats_collector.record_error("peer_not_found");
555                failed += 1;
556            }
557        }
558
559        // Clean up expired requests
560        let expired = self.relay_queue.cleanup_expired(now);
561        if expired > 0 {
562            self.relay_stats.requests_timed_out += expired as u64;
563            // Record timeout errors for each expired request
564            for _ in 0..expired {
565                self.relay_stats_collector.record_error("request_timeout");
566            }
567            debug!("Cleaned up {} expired relay requests", expired);
568        }
569
570        self.relay_stats.current_queue_size = self.relay_queue.len();
571
572        if processed > 0 || failed > 0 {
573            trace!(
574                "Relay queue processing: {} processed, {} failed, {} in queue",
575                processed,
576                failed,
577                self.relay_queue.len()
578            );
579        }
580    }
581
582    /// Get relay statistics for monitoring
583    pub fn relay_stats(&self) -> &RelayStats {
584        &self.relay_stats
585    }
586
587    /// Get comprehensive relay statistics for monitoring and analysis
588    pub fn comprehensive_relay_stats(&self) -> crate::relay::RelayStatistics {
589        // Update the collector with current queue stats before collecting
590        self.relay_stats_collector.update_queue_stats(&self.relay_stats);
591        self.relay_stats_collector.collect_statistics()
592    }
593
594    /// Get relay statistics collector for external registration of components
595    pub fn relay_stats_collector(&self) -> &RelayStatisticsCollector {
596        &self.relay_stats_collector
597    }
598
599    /// Get relay queue length
600    pub fn relay_queue_len(&self) -> usize {
601        self.relay_queue.len()
602    }
603
604    /// Process `EndpointEvent`s emitted from related `Connection`s
605    ///
606    /// In turn, processing this event may return a `ConnectionEvent` for the same `Connection`.
607    pub fn handle_event(
608        &mut self,
609        ch: ConnectionHandle,
610        event: EndpointEvent,
611    ) -> Option<ConnectionEvent> {
612        use EndpointEventInner::*;
613        match event.0 {
614            EndpointEventInner::NeedIdentifiers(now, n) => {
615                return Some(self.send_new_identifiers(now, ch, n));
616            }
617            ResetToken(remote, token) => {
618                if let Some(old) = self.connections[ch].reset_token.replace((remote, token)) {
619                    self.index.connection_reset_tokens.remove(old.0, old.1);
620                }
621                if self.index.connection_reset_tokens.insert(remote, token, ch) {
622                    warn!("duplicate reset token");
623                }
624            }
625            RetireConnectionId(now, seq, allow_more_cids) => {
626                if let Some(cid) = self.connections[ch].loc_cids.remove(&seq) {
627                    trace!("peer retired CID {}: {}", seq, cid);
628                    self.index.retire(cid);
629                    if allow_more_cids {
630                        return Some(self.send_new_identifiers(now, ch, 1));
631                    }
632                }
633            }
634            RelayPunchMeNow(target_peer_id, punch_me_now) => {
635                // Handle relay request from bootstrap node
636                let peer_id = PeerId(target_peer_id);
637                if self.queue_frame_for_peer(&peer_id, punch_me_now) {
638                    trace!(
639                        "Successfully queued PunchMeNow frame for relay to peer {:?}",
640                        peer_id
641                    );
642                } else {
643                    warn!("Failed to queue PunchMeNow relay for peer {:?}", peer_id);
644                }
645            }
646            SendAddressFrame(add_address_frame) => {
647                // Convert to a connection event so the connection queues the frame for transmit
648                return Some(ConnectionEvent(ConnectionEventInner::QueueAddAddress(
649                    add_address_frame,
650                )));
651            }
652            NatCandidateValidated { address, challenge } => {
653                // Handle successful NAT traversal candidate validation
654                trace!(
655                    "NAT candidate validation succeeded for {} with challenge {:016x}",
656                    address, challenge
657                );
658
659                // The validation success is primarily handled by the connection-level state machine
660                // This event serves as notification to the endpoint for potential coordination
661                // with other components or logging/metrics collection
662                debug!("NAT candidate {} validated successfully", address);
663            }
664            Drained => {
665                if let Some(conn) = self.connections.try_remove(ch.0) {
666                    self.index.remove(&conn);
667                    // Clean up peer connection mapping if this connection has a peer ID
668                    if let Some(peer_id) = conn.peer_id {
669                        self.peer_connections.remove(&peer_id);
670                        trace!("Cleaned up peer connection mapping for {:?}", peer_id);
671                    }
672                } else {
673                    // This indicates a bug in downstream code, which could cause spurious
674                    // connection loss instead of this error if the CID was (re)allocated prior to
675                    // the illegal call.
676                    error!(id = ch.0, "unknown connection drained");
677                }
678            }
679        }
680        None
681    }
682
683    /// Process an incoming UDP datagram
684    pub fn handle(
685        &mut self,
686        now: Instant,
687        remote: SocketAddr,
688        local_ip: Option<IpAddr>,
689        ecn: Option<EcnCodepoint>,
690        data: BytesMut,
691        buf: &mut Vec<u8>,
692    ) -> Option<DatagramEvent> {
693        // Partially decode packet or short-circuit if unable
694        let datagram_len = data.len();
695        let event = match PartialDecode::new(
696            data,
697            &FixedLengthConnectionIdParser::new(self.local_cid_generator.cid_len()),
698            &self.config.supported_versions,
699            self.config.grease_quic_bit,
700        ) {
701            Ok((first_decode, remaining)) => DatagramConnectionEvent {
702                now,
703                remote,
704                ecn,
705                first_decode,
706                remaining,
707            },
708            Err(PacketDecodeError::UnsupportedVersion {
709                src_cid,
710                dst_cid,
711                version,
712            }) => {
713                if self.server_config.is_none() {
714                    debug!("dropping packet with unsupported version");
715                    return None;
716                }
717                trace!("sending version negotiation");
718                // Negotiate versions
719                Header::VersionNegotiate {
720                    random: self.rng.r#gen::<u8>() | 0x40,
721                    src_cid: dst_cid,
722                    dst_cid: src_cid,
723                }
724                .encode(buf);
725                // Grease with a reserved version
726                buf.write::<u32>(match version {
727                    0x0a1a_2a3a => 0x0a1a_2a4a,
728                    _ => 0x0a1a_2a3a,
729                });
730                for &version in &self.config.supported_versions {
731                    buf.write(version);
732                }
733                return Some(DatagramEvent::Response(Transmit {
734                    destination: remote,
735                    ecn: None,
736                    size: buf.len(),
737                    segment_size: None,
738                    src_ip: local_ip,
739                }));
740            }
741            Err(e) => {
742                trace!("malformed header: {}", e);
743                return None;
744            }
745        };
746
747        let addresses = FourTuple { remote, local_ip };
748        let dst_cid = event.first_decode.dst_cid();
749
750        if let Some(route_to) = self.index.get(&addresses, &event.first_decode) {
751            // Handle packet on existing connection
752            match route_to {
753                RouteDatagramTo::Incoming(incoming_idx) => {
754                    let incoming_buffer = &mut self.incoming_buffers[incoming_idx];
755                    let config = &self.server_config.as_ref().unwrap();
756
757                    if incoming_buffer
758                        .total_bytes
759                        .checked_add(datagram_len as u64)
760                        .is_some_and(|n| n <= config.incoming_buffer_size)
761                        && self
762                            .all_incoming_buffers_total_bytes
763                            .checked_add(datagram_len as u64)
764                            .is_some_and(|n| n <= config.incoming_buffer_size_total)
765                    {
766                        incoming_buffer.datagrams.push(event);
767                        incoming_buffer.total_bytes += datagram_len as u64;
768                        self.all_incoming_buffers_total_bytes += datagram_len as u64;
769                    }
770
771                    None
772                }
773                RouteDatagramTo::Connection(ch) => Some(DatagramEvent::ConnectionEvent(
774                    ch,
775                    ConnectionEvent(ConnectionEventInner::Datagram(event)),
776                )),
777            }
778        } else if event.first_decode.initial_header().is_some() {
779            // Potentially create a new connection
780
781            self.handle_first_packet(datagram_len, event, addresses, buf)
782        } else if event.first_decode.has_long_header() {
783            debug!(
784                "ignoring non-initial packet for unknown connection {}",
785                dst_cid
786            );
787            None
788        } else if !event.first_decode.is_initial()
789            && self.local_cid_generator.validate(dst_cid).is_err()
790        {
791            // If we got this far, we're receiving a seemingly valid packet for an unknown
792            // connection. Send a stateless reset if possible.
793
794            debug!("dropping packet with invalid CID");
795            None
796        } else if dst_cid.is_empty() {
797            trace!("dropping unrecognized short packet without ID");
798            None
799        } else {
800            self.stateless_reset(now, datagram_len, addresses, *dst_cid, buf)
801                .map(DatagramEvent::Response)
802        }
803    }
804
805    fn stateless_reset(
806        &mut self,
807        now: Instant,
808        inciting_dgram_len: usize,
809        addresses: FourTuple,
810        dst_cid: ConnectionId,
811        buf: &mut Vec<u8>,
812    ) -> Option<Transmit> {
813        if self
814            .last_stateless_reset
815            .is_some_and(|last| last + self.config.min_reset_interval > now)
816        {
817            debug!("ignoring unexpected packet within minimum stateless reset interval");
818            return None;
819        }
820
821        /// Minimum amount of padding for the stateless reset to look like a short-header packet
822        const MIN_PADDING_LEN: usize = 5;
823
824        // Prevent amplification attacks and reset loops by ensuring we pad to at most 1 byte
825        // smaller than the inciting packet.
826        let max_padding_len = match inciting_dgram_len.checked_sub(RESET_TOKEN_SIZE) {
827            Some(headroom) if headroom > MIN_PADDING_LEN => headroom - 1,
828            _ => {
829                debug!(
830                    "ignoring unexpected {} byte packet: not larger than minimum stateless reset size",
831                    inciting_dgram_len
832                );
833                return None;
834            }
835        };
836
837        debug!(
838            "sending stateless reset for {} to {}",
839            dst_cid, addresses.remote
840        );
841        self.last_stateless_reset = Some(now);
842        // Resets with at least this much padding can't possibly be distinguished from real packets
843        const IDEAL_MIN_PADDING_LEN: usize = MIN_PADDING_LEN + MAX_CID_SIZE;
844        let padding_len = if max_padding_len <= IDEAL_MIN_PADDING_LEN {
845            max_padding_len
846        } else {
847            self.rng.gen_range(IDEAL_MIN_PADDING_LEN..max_padding_len)
848        };
849        buf.reserve(padding_len + RESET_TOKEN_SIZE);
850        buf.resize(padding_len, 0);
851        self.rng.fill_bytes(&mut buf[0..padding_len]);
852        buf[0] = 0b0100_0000 | (buf[0] >> 2);
853        buf.extend_from_slice(&ResetToken::new(&*self.config.reset_key, dst_cid));
854
855        debug_assert!(buf.len() < inciting_dgram_len);
856
857        Some(Transmit {
858            destination: addresses.remote,
859            ecn: None,
860            size: buf.len(),
861            segment_size: None,
862            src_ip: addresses.local_ip,
863        })
864    }
865
866    /// Initiate a connection
867    pub fn connect(
868        &mut self,
869        now: Instant,
870        config: ClientConfig,
871        remote: SocketAddr,
872        server_name: &str,
873    ) -> Result<(ConnectionHandle, Connection), ConnectError> {
874        if self.cids_exhausted() {
875            return Err(ConnectError::CidsExhausted);
876        }
877        if remote.port() == 0 || remote.ip().is_unspecified() {
878            return Err(ConnectError::InvalidRemoteAddress(remote));
879        }
880        if !self.config.supported_versions.contains(&config.version) {
881            return Err(ConnectError::UnsupportedVersion);
882        }
883
884        let remote_id = (config.initial_dst_cid_provider)();
885        trace!(initial_dcid = %remote_id);
886
887        let ch = ConnectionHandle(self.connections.vacant_key());
888        let loc_cid = self.new_cid(ch);
889        let params = TransportParameters::new(
890            &config.transport,
891            &self.config,
892            self.local_cid_generator.as_ref(),
893            loc_cid,
894            None,
895            &mut self.rng,
896        );
897        let tls = config
898            .crypto
899            .start_session(config.version, server_name, &params)?;
900
901        let conn = self.add_connection(
902            ch,
903            config.version,
904            remote_id,
905            loc_cid,
906            remote_id,
907            FourTuple {
908                remote,
909                local_ip: None,
910            },
911            now,
912            tls,
913            config.transport,
914            SideArgs::Client {
915                token_store: config.token_store,
916                server_name: server_name.into(),
917            },
918        );
919        Ok((ch, conn))
920    }
921
922    fn send_new_identifiers(
923        &mut self,
924        now: Instant,
925        ch: ConnectionHandle,
926        num: u64,
927    ) -> ConnectionEvent {
928        let mut ids = vec![];
929        for _ in 0..num {
930            let id = self.new_cid(ch);
931            let meta = &mut self.connections[ch];
932            let sequence = meta.cids_issued;
933            meta.cids_issued += 1;
934            meta.loc_cids.insert(sequence, id);
935            ids.push(IssuedCid {
936                sequence,
937                id,
938                reset_token: ResetToken::new(&*self.config.reset_key, id),
939            });
940        }
941        ConnectionEvent(ConnectionEventInner::NewIdentifiers(ids, now))
942    }
943
944    /// Generate a connection ID for `ch`
945    fn new_cid(&mut self, ch: ConnectionHandle) -> ConnectionId {
946        loop {
947            let cid = self.local_cid_generator.generate_cid();
948            if cid.is_empty() {
949                // Zero-length CID; nothing to track
950                debug_assert_eq!(self.local_cid_generator.cid_len(), 0);
951                return cid;
952            }
953            if let hash_map::Entry::Vacant(e) = self.index.connection_ids.entry(cid) {
954                e.insert(ch);
955                break cid;
956            }
957        }
958    }
959
960    fn handle_first_packet(
961        &mut self,
962        datagram_len: usize,
963        event: DatagramConnectionEvent,
964        addresses: FourTuple,
965        buf: &mut Vec<u8>,
966    ) -> Option<DatagramEvent> {
967        let dst_cid = event.first_decode.dst_cid();
968        let header = event.first_decode.initial_header().unwrap();
969
970        let Some(server_config) = &self.server_config else {
971            debug!("packet for unrecognized connection {}", dst_cid);
972            return self
973                .stateless_reset(event.now, datagram_len, addresses, *dst_cid, buf)
974                .map(DatagramEvent::Response);
975        };
976
977        if datagram_len < MIN_INITIAL_SIZE as usize {
978            debug!("ignoring short initial for connection {}", dst_cid);
979            return None;
980        }
981
982        let crypto = match server_config.crypto.initial_keys(header.version, dst_cid) {
983            Ok(keys) => keys,
984            Err(UnsupportedVersion) => {
985                // This probably indicates that the user set supported_versions incorrectly in
986                // `EndpointConfig`.
987                debug!(
988                    "ignoring initial packet version {:#x} unsupported by cryptographic layer",
989                    header.version
990                );
991                return None;
992            }
993        };
994
995        if let Err(reason) = self.early_validate_first_packet(header) {
996            return Some(DatagramEvent::Response(self.initial_close(
997                header.version,
998                addresses,
999                &crypto,
1000                &header.src_cid,
1001                reason,
1002                buf,
1003            )));
1004        }
1005
1006        let packet = match event.first_decode.finish(Some(&*crypto.header.remote)) {
1007            Ok(packet) => packet,
1008            Err(e) => {
1009                trace!("unable to decode initial packet: {}", e);
1010                return None;
1011            }
1012        };
1013
1014        if !packet.reserved_bits_valid() {
1015            debug!("dropping connection attempt with invalid reserved bits");
1016            return None;
1017        }
1018
1019        let Header::Initial(header) = packet.header else {
1020            panic!("non-initial packet in handle_first_packet()");
1021        };
1022
1023        let server_config = self.server_config.as_ref().unwrap().clone();
1024
1025        let token = match IncomingToken::from_header(&header, &server_config, addresses.remote) {
1026            Ok(token) => token,
1027            Err(InvalidRetryTokenError) => {
1028                debug!("rejecting invalid retry token");
1029                return Some(DatagramEvent::Response(self.initial_close(
1030                    header.version,
1031                    addresses,
1032                    &crypto,
1033                    &header.src_cid,
1034                    TransportError::INVALID_TOKEN(""),
1035                    buf,
1036                )));
1037            }
1038        };
1039
1040        let incoming_idx = self.incoming_buffers.insert(IncomingBuffer::default());
1041        self.index
1042            .insert_initial_incoming(header.dst_cid, incoming_idx);
1043
1044        Some(DatagramEvent::NewConnection(Incoming {
1045            received_at: event.now,
1046            addresses,
1047            ecn: event.ecn,
1048            packet: InitialPacket {
1049                header,
1050                header_data: packet.header_data,
1051                payload: packet.payload,
1052            },
1053            rest: event.remaining,
1054            crypto,
1055            token,
1056            incoming_idx,
1057            improper_drop_warner: IncomingImproperDropWarner,
1058        }))
1059    }
1060
1061    /// Attempt to accept this incoming connection (an error may still occur)
1062    // AcceptError cannot be made smaller without semver breakage
1063    #[allow(clippy::result_large_err)]
1064    pub fn accept(
1065        &mut self,
1066        mut incoming: Incoming,
1067        now: Instant,
1068        buf: &mut Vec<u8>,
1069        server_config: Option<Arc<ServerConfig>>,
1070    ) -> Result<(ConnectionHandle, Connection), AcceptError> {
1071        let remote_address_validated = incoming.remote_address_validated();
1072        incoming.improper_drop_warner.dismiss();
1073        let incoming_buffer = self.incoming_buffers.remove(incoming.incoming_idx);
1074        self.all_incoming_buffers_total_bytes -= incoming_buffer.total_bytes;
1075
1076        let packet_number = incoming.packet.header.number.expand(0);
1077        let InitialHeader {
1078            src_cid,
1079            dst_cid,
1080            version,
1081            ..
1082        } = incoming.packet.header;
1083        let server_config =
1084            server_config.unwrap_or_else(|| self.server_config.as_ref().unwrap().clone());
1085
1086        if server_config
1087            .transport
1088            .max_idle_timeout
1089            .is_some_and(|timeout| {
1090                incoming.received_at + Duration::from_millis(timeout.into()) <= now
1091            })
1092        {
1093            debug!("abandoning accept of stale initial");
1094            self.index.remove_initial(dst_cid);
1095            return Err(AcceptError {
1096                cause: ConnectionError::TimedOut,
1097                response: None,
1098            });
1099        }
1100
1101        if self.cids_exhausted() {
1102            debug!("refusing connection");
1103            self.index.remove_initial(dst_cid);
1104            return Err(AcceptError {
1105                cause: ConnectionError::CidsExhausted,
1106                response: Some(self.initial_close(
1107                    version,
1108                    incoming.addresses,
1109                    &incoming.crypto,
1110                    &src_cid,
1111                    TransportError::CONNECTION_REFUSED(""),
1112                    buf,
1113                )),
1114            });
1115        }
1116
1117        if incoming
1118            .crypto
1119            .packet
1120            .remote
1121            .decrypt(
1122                packet_number,
1123                &incoming.packet.header_data,
1124                &mut incoming.packet.payload,
1125            )
1126            .is_err()
1127        {
1128            debug!(packet_number, "failed to authenticate initial packet");
1129            self.index.remove_initial(dst_cid);
1130            return Err(AcceptError {
1131                cause: TransportError::PROTOCOL_VIOLATION("authentication failed").into(),
1132                response: None,
1133            });
1134        };
1135
1136        let ch = ConnectionHandle(self.connections.vacant_key());
1137        let loc_cid = self.new_cid(ch);
1138        let mut params = TransportParameters::new(
1139            &server_config.transport,
1140            &self.config,
1141            self.local_cid_generator.as_ref(),
1142            loc_cid,
1143            Some(&server_config),
1144            &mut self.rng,
1145        );
1146        params.stateless_reset_token = Some(ResetToken::new(&*self.config.reset_key, loc_cid));
1147        params.original_dst_cid = Some(incoming.token.orig_dst_cid);
1148        params.retry_src_cid = incoming.token.retry_src_cid;
1149        let mut pref_addr_cid = None;
1150        if server_config.has_preferred_address() {
1151            let cid = self.new_cid(ch);
1152            pref_addr_cid = Some(cid);
1153            params.preferred_address = Some(PreferredAddress {
1154                address_v4: server_config.preferred_address_v4,
1155                address_v6: server_config.preferred_address_v6,
1156                connection_id: cid,
1157                stateless_reset_token: ResetToken::new(&*self.config.reset_key, cid),
1158            });
1159        }
1160
1161        let tls = server_config.crypto.clone().start_session(version, &params);
1162        let transport_config = server_config.transport.clone();
1163        let mut conn = self.add_connection(
1164            ch,
1165            version,
1166            dst_cid,
1167            loc_cid,
1168            src_cid,
1169            incoming.addresses,
1170            incoming.received_at,
1171            tls,
1172            transport_config,
1173            SideArgs::Server {
1174                server_config,
1175                pref_addr_cid,
1176                path_validated: remote_address_validated,
1177            },
1178        );
1179        self.index.insert_initial(dst_cid, ch);
1180
1181        match conn.handle_first_packet(
1182            incoming.received_at,
1183            incoming.addresses.remote,
1184            incoming.ecn,
1185            packet_number,
1186            incoming.packet,
1187            incoming.rest,
1188        ) {
1189            Ok(()) => {
1190                trace!(id = ch.0, icid = %dst_cid, "new connection");
1191
1192                for event in incoming_buffer.datagrams {
1193                    conn.handle_event(ConnectionEvent(ConnectionEventInner::Datagram(event)))
1194                }
1195
1196                Ok((ch, conn))
1197            }
1198            Err(e) => {
1199                debug!("handshake failed: {}", e);
1200                self.handle_event(ch, EndpointEvent(EndpointEventInner::Drained));
1201                let response = match e {
1202                    ConnectionError::TransportError(ref e) => Some(self.initial_close(
1203                        version,
1204                        incoming.addresses,
1205                        &incoming.crypto,
1206                        &src_cid,
1207                        e.clone(),
1208                        buf,
1209                    )),
1210                    _ => None,
1211                };
1212                Err(AcceptError { cause: e, response })
1213            }
1214        }
1215    }
1216
1217    /// Check if we should refuse a connection attempt regardless of the packet's contents
1218    fn early_validate_first_packet(
1219        &mut self,
1220        header: &ProtectedInitialHeader,
1221    ) -> Result<(), TransportError> {
1222        let config = &self.server_config.as_ref().unwrap();
1223        if self.cids_exhausted() || self.incoming_buffers.len() >= config.max_incoming {
1224            return Err(TransportError::CONNECTION_REFUSED(""));
1225        }
1226
1227        // RFC9000 §7.2 dictates that initial (client-chosen) destination CIDs must be at least 8
1228        // bytes. If this is a Retry packet, then the length must instead match our usual CID
1229        // length. If we ever issue non-Retry address validation tokens via `NEW_TOKEN`, then we'll
1230        // also need to validate CID length for those after decoding the token.
1231        if header.dst_cid.len() < 8
1232            && (header.token_pos.is_empty()
1233                || header.dst_cid.len() != self.local_cid_generator.cid_len())
1234        {
1235            debug!(
1236                "rejecting connection due to invalid DCID length {}",
1237                header.dst_cid.len()
1238            );
1239            return Err(TransportError::PROTOCOL_VIOLATION(
1240                "invalid destination CID length",
1241            ));
1242        }
1243
1244        Ok(())
1245    }
1246
1247    /// Reject this incoming connection attempt
1248    pub fn refuse(&mut self, incoming: Incoming, buf: &mut Vec<u8>) -> Transmit {
1249        self.clean_up_incoming(&incoming);
1250        incoming.improper_drop_warner.dismiss();
1251
1252        self.initial_close(
1253            incoming.packet.header.version,
1254            incoming.addresses,
1255            &incoming.crypto,
1256            &incoming.packet.header.src_cid,
1257            TransportError::CONNECTION_REFUSED(""),
1258            buf,
1259        )
1260    }
1261
1262    /// Respond with a retry packet, requiring the client to retry with address validation
1263    ///
1264    /// Errors if `incoming.may_retry()` is false.
1265    pub fn retry(&mut self, incoming: Incoming, buf: &mut Vec<u8>) -> Result<Transmit, RetryError> {
1266        if !incoming.may_retry() {
1267            return Err(RetryError(Box::new(incoming)));
1268        }
1269
1270        self.clean_up_incoming(&incoming);
1271        incoming.improper_drop_warner.dismiss();
1272
1273        let server_config = self.server_config.as_ref().unwrap();
1274
1275        // First Initial
1276        // The peer will use this as the DCID of its following Initials. Initial DCIDs are
1277        // looked up separately from Handshake/Data DCIDs, so there is no risk of collision
1278        // with established connections. In the unlikely event that a collision occurs
1279        // between two connections in the initial phase, both will fail fast and may be
1280        // retried by the application layer.
1281        let loc_cid = self.local_cid_generator.generate_cid();
1282
1283        let payload = TokenPayload::Retry {
1284            address: incoming.addresses.remote,
1285            orig_dst_cid: incoming.packet.header.dst_cid,
1286            issued: server_config.time_source.now(),
1287        };
1288        let token = Token::new(payload, &mut self.rng).encode(&*server_config.token_key);
1289
1290        let header = Header::Retry {
1291            src_cid: loc_cid,
1292            dst_cid: incoming.packet.header.src_cid,
1293            version: incoming.packet.header.version,
1294        };
1295
1296        let encode = header.encode(buf);
1297        buf.put_slice(&token);
1298        buf.extend_from_slice(&server_config.crypto.retry_tag(
1299            incoming.packet.header.version,
1300            &incoming.packet.header.dst_cid,
1301            buf,
1302        ));
1303        encode.finish(buf, &*incoming.crypto.header.local, None);
1304
1305        Ok(Transmit {
1306            destination: incoming.addresses.remote,
1307            ecn: None,
1308            size: buf.len(),
1309            segment_size: None,
1310            src_ip: incoming.addresses.local_ip,
1311        })
1312    }
1313
1314    /// Ignore this incoming connection attempt, not sending any packet in response
1315    ///
1316    /// Doing this actively, rather than merely dropping the [`Incoming`], is necessary to prevent
1317    /// memory leaks due to state within [`Endpoint`] tracking the incoming connection.
1318    pub fn ignore(&mut self, incoming: Incoming) {
1319        self.clean_up_incoming(&incoming);
1320        incoming.improper_drop_warner.dismiss();
1321    }
1322
1323    /// Clean up endpoint data structures associated with an `Incoming`.
1324    fn clean_up_incoming(&mut self, incoming: &Incoming) {
1325        self.index.remove_initial(incoming.packet.header.dst_cid);
1326        let incoming_buffer = self.incoming_buffers.remove(incoming.incoming_idx);
1327        self.all_incoming_buffers_total_bytes -= incoming_buffer.total_bytes;
1328    }
1329
1330    fn add_connection(
1331        &mut self,
1332        ch: ConnectionHandle,
1333        version: u32,
1334        init_cid: ConnectionId,
1335        loc_cid: ConnectionId,
1336        rem_cid: ConnectionId,
1337        addresses: FourTuple,
1338        now: Instant,
1339        tls: Box<dyn crypto::Session>,
1340        transport_config: Arc<TransportConfig>,
1341        side_args: SideArgs,
1342    ) -> Connection {
1343        let mut rng_seed = [0; 32];
1344        self.rng.fill_bytes(&mut rng_seed);
1345        let side = side_args.side();
1346        let pref_addr_cid = side_args.pref_addr_cid();
1347        let conn = Connection::new(
1348            self.config.clone(),
1349            transport_config,
1350            init_cid,
1351            loc_cid,
1352            rem_cid,
1353            addresses.remote,
1354            addresses.local_ip,
1355            tls,
1356            self.local_cid_generator.as_ref(),
1357            now,
1358            version,
1359            self.allow_mtud,
1360            rng_seed,
1361            side_args,
1362        );
1363
1364        let mut cids_issued = 0;
1365        let mut loc_cids = FxHashMap::default();
1366
1367        loc_cids.insert(cids_issued, loc_cid);
1368        cids_issued += 1;
1369
1370        if let Some(cid) = pref_addr_cid {
1371            debug_assert_eq!(cids_issued, 1, "preferred address cid seq must be 1");
1372            loc_cids.insert(cids_issued, cid);
1373            cids_issued += 1;
1374        }
1375
1376        let id = self.connections.insert(ConnectionMeta {
1377            init_cid,
1378            cids_issued,
1379            loc_cids,
1380            addresses,
1381            side,
1382            reset_token: None,
1383            peer_id: None,
1384        });
1385        debug_assert_eq!(id, ch.0, "connection handle allocation out of sync");
1386
1387        self.index.insert_conn(addresses, loc_cid, ch, side);
1388
1389        conn
1390    }
1391
1392    fn initial_close(
1393        &mut self,
1394        version: u32,
1395        addresses: FourTuple,
1396        crypto: &Keys,
1397        remote_id: &ConnectionId,
1398        reason: TransportError,
1399        buf: &mut Vec<u8>,
1400    ) -> Transmit {
1401        // We don't need to worry about CID collisions in initial closes because the peer
1402        // shouldn't respond, and if it does, and the CID collides, we'll just drop the
1403        // unexpected response.
1404        let local_id = self.local_cid_generator.generate_cid();
1405        let number = PacketNumber::U8(0);
1406        let header = Header::Initial(InitialHeader {
1407            dst_cid: *remote_id,
1408            src_cid: local_id,
1409            number,
1410            token: Bytes::new(),
1411            version,
1412        });
1413
1414        let partial_encode = header.encode(buf);
1415        let max_len =
1416            INITIAL_MTU as usize - partial_encode.header_len - crypto.packet.local.tag_len();
1417        frame::Close::from(reason).encode(buf, max_len);
1418        buf.resize(buf.len() + crypto.packet.local.tag_len(), 0);
1419        partial_encode.finish(buf, &*crypto.header.local, Some((0, &*crypto.packet.local)));
1420        Transmit {
1421            destination: addresses.remote,
1422            ecn: None,
1423            size: buf.len(),
1424            segment_size: None,
1425            src_ip: addresses.local_ip,
1426        }
1427    }
1428
1429    /// Access the configuration used by this endpoint
1430    pub fn config(&self) -> &EndpointConfig {
1431        &self.config
1432    }
1433
1434    /// Enable or disable address discovery for this endpoint
1435    ///
1436    /// Address discovery is enabled by default. When enabled, the endpoint will:
1437    /// - Send OBSERVED_ADDRESS frames to peers to inform them of their reflexive addresses
1438    /// - Process received OBSERVED_ADDRESS frames to learn about its own reflexive addresses
1439    /// - Integrate discovered addresses with NAT traversal for improved connectivity
1440    pub fn enable_address_discovery(&mut self, enabled: bool) {
1441        self.address_discovery_enabled = enabled;
1442        // Note: Existing connections will continue with their current setting.
1443        // New connections will use the updated setting.
1444    }
1445
1446    /// Check if address discovery is enabled
1447    pub fn address_discovery_enabled(&self) -> bool {
1448        self.address_discovery_enabled
1449    }
1450
1451    /// Get all discovered addresses across all connections
1452    ///
1453    /// Returns a list of unique socket addresses that have been observed
1454    /// by remote peers and reported via OBSERVED_ADDRESS frames.
1455    ///
1456    /// Note: This returns an empty vector in the current implementation.
1457    /// Applications should track discovered addresses at the connection level.
1458    pub fn discovered_addresses(&self) -> Vec<SocketAddr> {
1459        // TODO: Implement address tracking at the endpoint level
1460        Vec::new()
1461    }
1462
1463    /// Set a callback to be invoked when an address change is detected
1464    ///
1465    /// The callback receives the old address (if any) and the new address.
1466    /// Only one callback can be set at a time; setting a new callback replaces the previous one.
1467    pub fn set_address_change_callback<F>(&mut self, callback: F)
1468    where
1469        F: Fn(Option<SocketAddr>, SocketAddr) + Send + Sync + 'static,
1470    {
1471        self.address_change_callback = Some(Box::new(callback));
1472    }
1473
1474    /// Clear the address change callback
1475    pub fn clear_address_change_callback(&mut self) {
1476        self.address_change_callback = None;
1477    }
1478
1479    /// Get address discovery statistics
1480    ///
1481    /// Note: This returns default statistics in the current implementation.
1482    /// Applications should track statistics at the connection level.
1483    pub fn address_discovery_stats(&self) -> AddressDiscoveryStats {
1484        // TODO: Implement statistics tracking at the endpoint level
1485        AddressDiscoveryStats::default()
1486    }
1487
1488    /// Number of connections that are currently open
1489    pub fn open_connections(&self) -> usize {
1490        self.connections.len()
1491    }
1492
1493    /// Counter for the number of bytes currently used
1494    /// in the buffers for Initial and 0-RTT messages for pending incoming connections
1495    pub fn incoming_buffer_bytes(&self) -> u64 {
1496        self.all_incoming_buffers_total_bytes
1497    }
1498
1499    #[cfg(test)]
1500    pub(crate) fn known_connections(&self) -> usize {
1501        let x = self.connections.len();
1502        debug_assert_eq!(x, self.index.connection_ids_initial.len());
1503        // Not all connections have known reset tokens
1504        debug_assert!(x >= self.index.connection_reset_tokens.0.len());
1505        // Not all connections have unique remotes, and 0-length CIDs might not be in use.
1506        debug_assert!(x >= self.index.incoming_connection_remotes.len());
1507        debug_assert!(x >= self.index.outgoing_connection_remotes.len());
1508        x
1509    }
1510
1511    #[cfg(test)]
1512    pub(crate) fn known_cids(&self) -> usize {
1513        self.index.connection_ids.len()
1514    }
1515
1516    /// Whether we've used up 3/4 of the available CID space
1517    ///
1518    /// We leave some space unused so that `new_cid` can be relied upon to finish quickly. We don't
1519    /// bother to check when CID longer than 4 bytes are used because 2^40 connections is a lot.
1520    fn cids_exhausted(&self) -> bool {
1521        self.local_cid_generator.cid_len() <= 4
1522            && self.local_cid_generator.cid_len() != 0
1523            && (2usize.pow(self.local_cid_generator.cid_len() as u32 * 8)
1524                - self.index.connection_ids.len())
1525                < 2usize.pow(self.local_cid_generator.cid_len() as u32 * 8 - 2)
1526    }
1527}
1528
1529impl fmt::Debug for Endpoint {
1530    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1531        fmt.debug_struct("Endpoint")
1532            .field("rng", &self.rng)
1533            .field("index", &self.index)
1534            .field("connections", &self.connections)
1535            .field("config", &self.config)
1536            .field("server_config", &self.server_config)
1537            // incoming_buffers too large
1538            .field("incoming_buffers.len", &self.incoming_buffers.len())
1539            .field(
1540                "all_incoming_buffers_total_bytes",
1541                &self.all_incoming_buffers_total_bytes,
1542            )
1543            .finish()
1544    }
1545}
1546
1547/// Buffered Initial and 0-RTT messages for a pending incoming connection
1548#[derive(Default)]
1549struct IncomingBuffer {
1550    datagrams: Vec<DatagramConnectionEvent>,
1551    total_bytes: u64,
1552}
1553
1554/// Part of protocol state incoming datagrams can be routed to
1555#[derive(Copy, Clone, Debug)]
1556enum RouteDatagramTo {
1557    Incoming(usize),
1558    Connection(ConnectionHandle),
1559}
1560
1561/// Maps packets to existing connections
1562#[derive(Default, Debug)]
1563struct ConnectionIndex {
1564    /// Identifies connections based on the initial DCID the peer utilized
1565    ///
1566    /// Uses a standard `HashMap` to protect against hash collision attacks.
1567    ///
1568    /// Used by the server, not the client.
1569    connection_ids_initial: HashMap<ConnectionId, RouteDatagramTo>,
1570    /// Identifies connections based on locally created CIDs
1571    ///
1572    /// Uses a cheaper hash function since keys are locally created
1573    connection_ids: FxHashMap<ConnectionId, ConnectionHandle>,
1574    /// Identifies incoming connections with zero-length CIDs
1575    ///
1576    /// Uses a standard `HashMap` to protect against hash collision attacks.
1577    incoming_connection_remotes: HashMap<FourTuple, ConnectionHandle>,
1578    /// Identifies outgoing connections with zero-length CIDs
1579    ///
1580    /// We don't yet support explicit source addresses for client connections, and zero-length CIDs
1581    /// require a unique four-tuple, so at most one client connection with zero-length local CIDs
1582    /// may be established per remote. We must omit the local address from the key because we don't
1583    /// necessarily know what address we're sending from, and hence receiving at.
1584    ///
1585    /// Uses a standard `HashMap` to protect against hash collision attacks.
1586    outgoing_connection_remotes: HashMap<SocketAddr, ConnectionHandle>,
1587    /// Reset tokens provided by the peer for the CID each connection is currently sending to
1588    ///
1589    /// Incoming stateless resets do not have correct CIDs, so we need this to identify the correct
1590    /// recipient, if any.
1591    connection_reset_tokens: ResetTokenTable,
1592}
1593
1594impl ConnectionIndex {
1595    /// Associate an incoming connection with its initial destination CID
1596    fn insert_initial_incoming(&mut self, dst_cid: ConnectionId, incoming_key: usize) {
1597        if dst_cid.is_empty() {
1598            return;
1599        }
1600        self.connection_ids_initial
1601            .insert(dst_cid, RouteDatagramTo::Incoming(incoming_key));
1602    }
1603
1604    /// Remove an association with an initial destination CID
1605    fn remove_initial(&mut self, dst_cid: ConnectionId) {
1606        if dst_cid.is_empty() {
1607            return;
1608        }
1609        let removed = self.connection_ids_initial.remove(&dst_cid);
1610        debug_assert!(removed.is_some());
1611    }
1612
1613    /// Associate a connection with its initial destination CID
1614    fn insert_initial(&mut self, dst_cid: ConnectionId, connection: ConnectionHandle) {
1615        if dst_cid.is_empty() {
1616            return;
1617        }
1618        self.connection_ids_initial
1619            .insert(dst_cid, RouteDatagramTo::Connection(connection));
1620    }
1621
1622    /// Associate a connection with its first locally-chosen destination CID if used, or otherwise
1623    /// its current 4-tuple
1624    fn insert_conn(
1625        &mut self,
1626        addresses: FourTuple,
1627        dst_cid: ConnectionId,
1628        connection: ConnectionHandle,
1629        side: Side,
1630    ) {
1631        match dst_cid.len() {
1632            0 => match side {
1633                Side::Server => {
1634                    self.incoming_connection_remotes
1635                        .insert(addresses, connection);
1636                }
1637                Side::Client => {
1638                    self.outgoing_connection_remotes
1639                        .insert(addresses.remote, connection);
1640                }
1641            },
1642            _ => {
1643                self.connection_ids.insert(dst_cid, connection);
1644            }
1645        }
1646    }
1647
1648    /// Discard a connection ID
1649    fn retire(&mut self, dst_cid: ConnectionId) {
1650        self.connection_ids.remove(&dst_cid);
1651    }
1652
1653    /// Remove all references to a connection
1654    fn remove(&mut self, conn: &ConnectionMeta) {
1655        if conn.side.is_server() {
1656            self.remove_initial(conn.init_cid);
1657        }
1658        for cid in conn.loc_cids.values() {
1659            self.connection_ids.remove(cid);
1660        }
1661        self.incoming_connection_remotes.remove(&conn.addresses);
1662        self.outgoing_connection_remotes
1663            .remove(&conn.addresses.remote);
1664        if let Some((remote, token)) = conn.reset_token {
1665            self.connection_reset_tokens.remove(remote, token);
1666        }
1667    }
1668
1669    /// Find the existing connection that `datagram` should be routed to, if any
1670    fn get(&self, addresses: &FourTuple, datagram: &PartialDecode) -> Option<RouteDatagramTo> {
1671        let dst_cid = datagram.dst_cid();
1672        let is_empty_cid = dst_cid.is_empty();
1673
1674        // Fast path: Try most common lookup first (non-empty CID)
1675        if !is_empty_cid {
1676            if let Some(&ch) = self.connection_ids.get(dst_cid) {
1677                return Some(RouteDatagramTo::Connection(ch));
1678            }
1679        }
1680
1681        // Initial/0RTT packet lookup
1682        if datagram.is_initial() || datagram.is_0rtt() {
1683            if let Some(&ch) = self.connection_ids_initial.get(dst_cid) {
1684                return Some(ch);
1685            }
1686        }
1687
1688        // Empty CID lookup (less common, do after fast path)
1689        if is_empty_cid {
1690            // Check incoming connections first (servers handle more incoming)
1691            if let Some(&ch) = self.incoming_connection_remotes.get(addresses) {
1692                return Some(RouteDatagramTo::Connection(ch));
1693            }
1694            if let Some(&ch) = self.outgoing_connection_remotes.get(&addresses.remote) {
1695                return Some(RouteDatagramTo::Connection(ch));
1696            }
1697        }
1698
1699        // Stateless reset token lookup (least common, do last)
1700        let data = datagram.data();
1701        if data.len() < RESET_TOKEN_SIZE {
1702            return None;
1703        }
1704        self.connection_reset_tokens
1705            .get(addresses.remote, &data[data.len() - RESET_TOKEN_SIZE..])
1706            .cloned()
1707            .map(RouteDatagramTo::Connection)
1708    }
1709}
1710
1711#[derive(Debug)]
1712pub(crate) struct ConnectionMeta {
1713    init_cid: ConnectionId,
1714    /// Number of local connection IDs that have been issued in NEW_CONNECTION_ID frames.
1715    cids_issued: u64,
1716    loc_cids: FxHashMap<u64, ConnectionId>,
1717    /// Remote/local addresses the connection began with
1718    ///
1719    /// Only needed to support connections with zero-length CIDs, which cannot migrate, so we don't
1720    /// bother keeping it up to date.
1721    addresses: FourTuple,
1722    side: Side,
1723    /// Reset token provided by the peer for the CID we're currently sending to, and the address
1724    /// being sent to
1725    reset_token: Option<(SocketAddr, ResetToken)>,
1726    /// Peer ID for this connection, used for relay functionality
1727    peer_id: Option<PeerId>,
1728}
1729
1730/// Internal identifier for a `Connection` currently associated with an endpoint
1731#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
1732pub struct ConnectionHandle(pub usize);
1733
1734impl From<ConnectionHandle> for usize {
1735    fn from(x: ConnectionHandle) -> Self {
1736        x.0
1737    }
1738}
1739
1740impl Index<ConnectionHandle> for Slab<ConnectionMeta> {
1741    type Output = ConnectionMeta;
1742    fn index(&self, ch: ConnectionHandle) -> &ConnectionMeta {
1743        &self[ch.0]
1744    }
1745}
1746
1747impl IndexMut<ConnectionHandle> for Slab<ConnectionMeta> {
1748    fn index_mut(&mut self, ch: ConnectionHandle) -> &mut ConnectionMeta {
1749        &mut self[ch.0]
1750    }
1751}
1752
1753/// Event resulting from processing a single datagram
1754pub enum DatagramEvent {
1755    /// The datagram is redirected to its `Connection`
1756    ConnectionEvent(ConnectionHandle, ConnectionEvent),
1757    /// The datagram may result in starting a new `Connection`
1758    NewConnection(Incoming),
1759    /// Response generated directly by the endpoint
1760    Response(Transmit),
1761}
1762
1763/// An incoming connection for which the server has not yet begun its part of the handshake.
1764pub struct Incoming {
1765    received_at: Instant,
1766    addresses: FourTuple,
1767    ecn: Option<EcnCodepoint>,
1768    packet: InitialPacket,
1769    rest: Option<BytesMut>,
1770    crypto: Keys,
1771    token: IncomingToken,
1772    incoming_idx: usize,
1773    improper_drop_warner: IncomingImproperDropWarner,
1774}
1775
1776impl Incoming {
1777    /// The local IP address which was used when the peer established the connection
1778    ///
1779    /// This has the same behavior as [`Connection::local_ip`].
1780    pub fn local_ip(&self) -> Option<IpAddr> {
1781        self.addresses.local_ip
1782    }
1783
1784    /// The peer's UDP address
1785    pub fn remote_address(&self) -> SocketAddr {
1786        self.addresses.remote
1787    }
1788
1789    /// Whether the socket address that is initiating this connection has been validated
1790    ///
1791    /// This means that the sender of the initial packet has proved that they can receive traffic
1792    /// sent to `self.remote_address()`.
1793    ///
1794    /// If `self.remote_address_validated()` is false, `self.may_retry()` is guaranteed to be true.
1795    /// The inverse is not guaranteed.
1796    pub fn remote_address_validated(&self) -> bool {
1797        self.token.validated
1798    }
1799
1800    /// Whether it is legal to respond with a retry packet
1801    ///
1802    /// If `self.remote_address_validated()` is false, `self.may_retry()` is guaranteed to be true.
1803    /// The inverse is not guaranteed.
1804    pub fn may_retry(&self) -> bool {
1805        self.token.retry_src_cid.is_none()
1806    }
1807
1808    /// The original destination connection ID sent by the client
1809    pub fn orig_dst_cid(&self) -> &ConnectionId {
1810        &self.token.orig_dst_cid
1811    }
1812}
1813
1814impl fmt::Debug for Incoming {
1815    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1816        f.debug_struct("Incoming")
1817            .field("addresses", &self.addresses)
1818            .field("ecn", &self.ecn)
1819            // packet doesn't implement debug
1820            // rest is too big and not meaningful enough
1821            .field("token", &self.token)
1822            .field("incoming_idx", &self.incoming_idx)
1823            // improper drop warner contains no information
1824            .finish_non_exhaustive()
1825    }
1826}
1827
1828struct IncomingImproperDropWarner;
1829
1830impl IncomingImproperDropWarner {
1831    fn dismiss(self) {
1832        mem::forget(self);
1833    }
1834}
1835
1836impl Drop for IncomingImproperDropWarner {
1837    fn drop(&mut self) {
1838        warn!(
1839            "quinn_proto::Incoming dropped without passing to Endpoint::accept/refuse/retry/ignore \
1840               (may cause memory leak and eventual inability to accept new connections)"
1841        );
1842    }
1843}
1844
1845/// Errors in the parameters being used to create a new connection
1846///
1847/// These arise before any I/O has been performed.
1848#[derive(Debug, Error, Clone, PartialEq, Eq)]
1849pub enum ConnectError {
1850    /// The endpoint can no longer create new connections
1851    ///
1852    /// Indicates that a necessary component of the endpoint has been dropped or otherwise disabled.
1853    #[error("endpoint stopping")]
1854    EndpointStopping,
1855    /// The connection could not be created because not enough of the CID space is available
1856    ///
1857    /// Try using longer connection IDs
1858    #[error("CIDs exhausted")]
1859    CidsExhausted,
1860    /// The given server name was malformed
1861    #[error("invalid server name: {0}")]
1862    InvalidServerName(String),
1863    /// The remote [`SocketAddr`] supplied was malformed
1864    ///
1865    /// Examples include attempting to connect to port 0, or using an inappropriate address family.
1866    #[error("invalid remote address: {0}")]
1867    InvalidRemoteAddress(SocketAddr),
1868    /// No default client configuration was set up
1869    ///
1870    /// Use `Endpoint::connect_with` to specify a client configuration.
1871    #[error("no default client config")]
1872    NoDefaultClientConfig,
1873    /// The local endpoint does not support the QUIC version specified in the client configuration
1874    #[error("unsupported QUIC version")]
1875    UnsupportedVersion,
1876}
1877
1878/// Error type for attempting to accept an [`Incoming`]
1879#[derive(Debug)]
1880pub struct AcceptError {
1881    /// Underlying error describing reason for failure
1882    pub cause: ConnectionError,
1883    /// Optional response to transmit back
1884    pub response: Option<Transmit>,
1885}
1886
1887/// Error for attempting to retry an [`Incoming`] which already bears a token from a previous retry
1888#[derive(Debug, Error)]
1889#[error("retry() with validated Incoming")]
1890pub struct RetryError(Box<Incoming>);
1891
1892impl RetryError {
1893    /// Get the [`Incoming`]
1894    pub fn into_incoming(self) -> Incoming {
1895        *self.0
1896    }
1897}
1898
1899/// Reset Tokens which are associated with peer socket addresses
1900///
1901/// The standard `HashMap` is used since both `SocketAddr` and `ResetToken` are
1902/// peer generated and might be usable for hash collision attacks.
1903#[derive(Default, Debug)]
1904struct ResetTokenTable(HashMap<SocketAddr, HashMap<ResetToken, ConnectionHandle>>);
1905
1906impl ResetTokenTable {
1907    fn insert(&mut self, remote: SocketAddr, token: ResetToken, ch: ConnectionHandle) -> bool {
1908        self.0
1909            .entry(remote)
1910            .or_default()
1911            .insert(token, ch)
1912            .is_some()
1913    }
1914
1915    fn remove(&mut self, remote: SocketAddr, token: ResetToken) {
1916        use std::collections::hash_map::Entry;
1917        match self.0.entry(remote) {
1918            Entry::Vacant(_) => {}
1919            Entry::Occupied(mut e) => {
1920                e.get_mut().remove(&token);
1921                if e.get().is_empty() {
1922                    e.remove_entry();
1923                }
1924            }
1925        }
1926    }
1927
1928    fn get(&self, remote: SocketAddr, token: &[u8]) -> Option<&ConnectionHandle> {
1929        let token = ResetToken::from(<[u8; RESET_TOKEN_SIZE]>::try_from(token).ok()?);
1930        self.0.get(&remote)?.get(&token)
1931    }
1932}
1933
1934/// Identifies a connection by the combination of remote and local addresses
1935///
1936/// Including the local ensures good behavior when the host has multiple IP addresses on the same
1937/// subnet and zero-length connection IDs are in use.
1938#[derive(Hash, Eq, PartialEq, Debug, Copy, Clone)]
1939struct FourTuple {
1940    remote: SocketAddr,
1941    // A single socket can only listen on a single port, so no need to store it explicitly
1942    local_ip: Option<IpAddr>,
1943}