quiche/lib.rs
1// Copyright (C) 2018-2019, Cloudflare, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27//! 🥧 Savoury implementation of the QUIC transport protocol and HTTP/3.
28//!
29//! [quiche] is an implementation of the QUIC transport protocol and HTTP/3 as
30//! specified by the [IETF]. It provides a low level API for processing QUIC
31//! packets and handling connection state. The application is responsible for
32//! providing I/O (e.g. sockets handling) as well as an event loop with support
33//! for timers.
34//!
35//! [quiche]: https://github.com/cloudflare/quiche/
36//! [ietf]: https://quicwg.org/
37//!
38//! ## Configuring connections
39//!
40//! The first step in establishing a QUIC connection using quiche is creating a
41//! [`Config`] object:
42//!
43//! ```
44//! let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
45//! config.set_application_protos(&[b"example-proto"]);
46//!
47//! // Additional configuration specific to application and use case...
48//! # Ok::<(), quiche::Error>(())
49//! ```
50//!
51//! The [`Config`] object controls important aspects of the QUIC connection such
52//! as QUIC version, ALPN IDs, flow control, congestion control, idle timeout
53//! and other properties or features.
54//!
55//! QUIC is a general-purpose transport protocol and there are several
56//! configuration properties where there is no reasonable default value. For
57//! example, the permitted number of concurrent streams of any particular type
58//! is dependent on the application running over QUIC, and other use-case
59//! specific concerns.
60//!
61//! quiche defaults several properties to zero, applications most likely need
62//! to set these to something else to satisfy their needs using the following:
63//!
64//! - [`set_initial_max_streams_bidi()`]
65//! - [`set_initial_max_streams_uni()`]
66//! - [`set_initial_max_data()`]
67//! - [`set_initial_max_stream_data_bidi_local()`]
68//! - [`set_initial_max_stream_data_bidi_remote()`]
69//! - [`set_initial_max_stream_data_uni()`]
70//!
71//! [`Config`] also holds TLS configuration. This can be changed by mutators on
72//! the an existing object, or by constructing a TLS context manually and
73//! creating a configuration using [`with_boring_ssl_ctx_builder()`].
74//!
75//! A configuration object can be shared among multiple connections.
76//!
77//! ### Connection setup
78//!
79//! On the client-side the [`connect()`] utility function can be used to create
80//! a new connection, while [`accept()`] is for servers:
81//!
82//! ```
83//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
84//! # let server_name = "quic.tech";
85//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
86//! # let peer = "127.0.0.1:1234".parse().unwrap();
87//! # let local = "127.0.0.1:4321".parse().unwrap();
88//! // Client connection.
89//! let conn =
90//! quiche::connect(Some(&server_name), &scid, local, peer, &mut config)?;
91//!
92//! // Server connection.
93//! # let peer = "127.0.0.1:1234".parse().unwrap();
94//! # let local = "127.0.0.1:4321".parse().unwrap();
95//! let conn = quiche::accept(&scid, None, local, peer, &mut config)?;
96//! # Ok::<(), quiche::Error>(())
97//! ```
98//!
99//! In both cases, the application is responsible for generating a new source
100//! connection ID that will be used to identify the new connection.
101//!
102//! The application also need to pass the address of the remote peer of the
103//! connection: in the case of a client that would be the address of the server
104//! it is trying to connect to, and for a server that is the address of the
105//! client that initiated the connection.
106//!
107//! ## Handling incoming packets
108//!
109//! Using the connection's [`recv()`] method the application can process
110//! incoming packets that belong to that connection from the network:
111//!
112//! ```no_run
113//! # let mut buf = [0; 512];
114//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
115//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
116//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
117//! # let peer = "127.0.0.1:1234".parse().unwrap();
118//! # let local = "127.0.0.1:4321".parse().unwrap();
119//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
120//! let to = socket.local_addr().unwrap();
121//!
122//! loop {
123//! let (read, from) = socket.recv_from(&mut buf).unwrap();
124//!
125//! let recv_info = quiche::RecvInfo { from, to };
126//!
127//! let read = match conn.recv(&mut buf[..read], recv_info) {
128//! Ok(v) => v,
129//!
130//! Err(quiche::Error::Done) => {
131//! // Done reading.
132//! break;
133//! },
134//!
135//! Err(e) => {
136//! // An error occurred, handle it.
137//! break;
138//! },
139//! };
140//! }
141//! # Ok::<(), quiche::Error>(())
142//! ```
143//!
144//! The application has to pass a [`RecvInfo`] structure in order to provide
145//! additional information about the received packet (such as the address it
146//! was received from).
147//!
148//! ## Generating outgoing packets
149//!
150//! Outgoing packet are generated using the connection's [`send()`] method
151//! instead:
152//!
153//! ```no_run
154//! # let mut out = [0; 512];
155//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
156//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
157//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
158//! # let peer = "127.0.0.1:1234".parse().unwrap();
159//! # let local = "127.0.0.1:4321".parse().unwrap();
160//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
161//! loop {
162//! let (write, send_info) = match conn.send(&mut out) {
163//! Ok(v) => v,
164//!
165//! Err(quiche::Error::Done) => {
166//! // Done writing.
167//! break;
168//! },
169//!
170//! Err(e) => {
171//! // An error occurred, handle it.
172//! break;
173//! },
174//! };
175//!
176//! socket.send_to(&out[..write], &send_info.to).unwrap();
177//! }
178//! # Ok::<(), quiche::Error>(())
179//! ```
180//!
181//! The application will be provided with a [`SendInfo`] structure providing
182//! additional information about the newly created packet (such as the address
183//! the packet should be sent to).
184//!
185//! When packets are sent, the application is responsible for maintaining a
186//! timer to react to time-based connection events. The timer expiration can be
187//! obtained using the connection's [`timeout()`] method.
188//!
189//! ```
190//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
191//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
192//! # let peer = "127.0.0.1:1234".parse().unwrap();
193//! # let local = "127.0.0.1:4321".parse().unwrap();
194//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
195//! let timeout = conn.timeout();
196//! # Ok::<(), quiche::Error>(())
197//! ```
198//!
199//! The application is responsible for providing a timer implementation, which
200//! can be specific to the operating system or networking framework used. When
201//! a timer expires, the connection's [`on_timeout()`] method should be called,
202//! after which additional packets might need to be sent on the network:
203//!
204//! ```no_run
205//! # let mut out = [0; 512];
206//! # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
207//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
208//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
209//! # let peer = "127.0.0.1:1234".parse().unwrap();
210//! # let local = "127.0.0.1:4321".parse().unwrap();
211//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
212//! // Timeout expired, handle it.
213//! conn.on_timeout();
214//!
215//! // Send more packets as needed after timeout.
216//! loop {
217//! let (write, send_info) = match conn.send(&mut out) {
218//! Ok(v) => v,
219//!
220//! Err(quiche::Error::Done) => {
221//! // Done writing.
222//! break;
223//! },
224//!
225//! Err(e) => {
226//! // An error occurred, handle it.
227//! break;
228//! },
229//! };
230//!
231//! socket.send_to(&out[..write], &send_info.to).unwrap();
232//! }
233//! # Ok::<(), quiche::Error>(())
234//! ```
235//!
236//! ### Pacing
237//!
238//! It is recommended that applications [pace] sending of outgoing packets to
239//! avoid creating packet bursts that could cause short-term congestion and
240//! losses in the network.
241//!
242//! quiche exposes pacing hints for outgoing packets through the [`at`] field
243//! of the [`SendInfo`] structure that is returned by the [`send()`] method.
244//! This field represents the time when a specific packet should be sent into
245//! the network.
246//!
247//! Applications can use these hints by artificially delaying the sending of
248//! packets through platform-specific mechanisms (such as the [`SO_TXTIME`]
249//! socket option on Linux), or custom methods (for example by using user-space
250//! timers).
251//!
252//! [pace]: https://datatracker.ietf.org/doc/html/rfc9002#section-7.7
253//! [`SO_TXTIME`]: https://man7.org/linux/man-pages/man8/tc-etf.8.html
254//!
255//! ## Sending and receiving stream data
256//!
257//! After some back and forth, the connection will complete its handshake and
258//! will be ready for sending or receiving application data.
259//!
260//! Data can be sent on a stream by using the [`stream_send()`] method:
261//!
262//! ```no_run
263//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
264//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
265//! # let peer = "127.0.0.1:1234".parse().unwrap();
266//! # let local = "127.0.0.1:4321".parse().unwrap();
267//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
268//! if conn.is_established() {
269//! // Handshake completed, send some data on stream 0.
270//! conn.stream_send(0, b"hello", true)?;
271//! }
272//! # Ok::<(), quiche::Error>(())
273//! ```
274//!
275//! The application can check whether there are any readable streams by using
276//! the connection's [`readable()`] method, which returns an iterator over all
277//! the streams that have outstanding data to read.
278//!
279//! The [`stream_recv()`] method can then be used to retrieve the application
280//! data from the readable stream:
281//!
282//! ```no_run
283//! # let mut buf = [0; 512];
284//! # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
285//! # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
286//! # let peer = "127.0.0.1:1234".parse().unwrap();
287//! # let local = "127.0.0.1:4321".parse().unwrap();
288//! # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
289//! if conn.is_established() {
290//! // Iterate over readable streams.
291//! for stream_id in conn.readable() {
292//! // Stream is readable, read until there's no more data.
293//! while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
294//! println!("Got {} bytes on stream {}", read, stream_id);
295//! }
296//! }
297//! }
298//! # Ok::<(), quiche::Error>(())
299//! ```
300//!
301//! ## HTTP/3
302//!
303//! The quiche [HTTP/3 module] provides a high level API for sending and
304//! receiving HTTP requests and responses on top of the QUIC transport protocol.
305//!
306//! [`Config`]: https://docs.quic.tech/quiche/struct.Config.html
307//! [`set_initial_max_streams_bidi()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_streams_bidi
308//! [`set_initial_max_streams_uni()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_streams_uni
309//! [`set_initial_max_data()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_data
310//! [`set_initial_max_stream_data_bidi_local()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_stream_data_bidi_local
311//! [`set_initial_max_stream_data_bidi_remote()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_stream_data_bidi_remote
312//! [`set_initial_max_stream_data_uni()`]: https://docs.rs/quiche/latest/quiche/struct.Config.html#method.set_initial_max_stream_data_uni
313//! [`with_boring_ssl_ctx_builder()`]: https://docs.quic.tech/quiche/struct.Config.html#method.with_boring_ssl_ctx_builder
314//! [`connect()`]: fn.connect.html
315//! [`accept()`]: fn.accept.html
316//! [`recv()`]: struct.Connection.html#method.recv
317//! [`RecvInfo`]: struct.RecvInfo.html
318//! [`send()`]: struct.Connection.html#method.send
319//! [`SendInfo`]: struct.SendInfo.html
320//! [`at`]: struct.SendInfo.html#structfield.at
321//! [`timeout()`]: struct.Connection.html#method.timeout
322//! [`on_timeout()`]: struct.Connection.html#method.on_timeout
323//! [`stream_send()`]: struct.Connection.html#method.stream_send
324//! [`readable()`]: struct.Connection.html#method.readable
325//! [`stream_recv()`]: struct.Connection.html#method.stream_recv
326//! [HTTP/3 module]: h3/index.html
327//!
328//! ## Congestion Control
329//!
330//! The quiche library provides a high-level API for configuring which
331//! congestion control algorithm to use throughout the QUIC connection.
332//!
333//! When a QUIC connection is created, the application can optionally choose
334//! which CC algorithm to use. See [`CongestionControlAlgorithm`] for currently
335//! available congestion control algorithms.
336//!
337//! For example:
338//!
339//! ```
340//! let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
341//! config.set_cc_algorithm(quiche::CongestionControlAlgorithm::Reno);
342//! ```
343//!
344//! Alternatively, you can configure the congestion control algorithm to use
345//! by its name.
346//!
347//! ```
348//! let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION).unwrap();
349//! config.set_cc_algorithm_name("reno").unwrap();
350//! ```
351//!
352//! Note that the CC algorithm should be configured before calling [`connect()`]
353//! or [`accept()`]. Otherwise the connection will use a default CC algorithm.
354//!
355//! [`CongestionControlAlgorithm`]: enum.CongestionControlAlgorithm.html
356//!
357//! ## Feature flags
358//!
359//! quiche defines a number of [feature flags] to reduce the amount of compiled
360//! code and dependencies:
361//!
362//! * `boringssl-boring-crate` (default): Use the BoringSSL library provided by
363//! the [boring] crate.
364//!
365//! * `pkg-config-meta`: Generate pkg-config metadata file for libquiche.
366//!
367//! * `ffi`: Build and expose the FFI API.
368//!
369//! * `qlog`: Enable support for the [qlog] logging format.
370//!
371//! * `custom-client-dcid`: Allow clients to supply a custom DCID when
372//! initiating a connection. Dangerous if the DCID does not meet QUIC's
373//! unpredictability and length requirements.
374//!
375//! * `congestion_window_unchecked_available`: Expose a congestion control
376//! algorithm with a fixed, configurable window that defaults to `usize::MAX`.
377//!
378//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
379//! [boring]: https://crates.io/crates/boring
380//! [qlog]: https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema
381
382#![allow(clippy::upper_case_acronyms)]
383#![warn(missing_docs)]
384#![warn(unused_qualifications)]
385#![cfg_attr(docsrs, feature(doc_cfg))]
386
387#[macro_use]
388extern crate log;
389
390use std::cmp;
391
392use std::collections::VecDeque;
393
394use debug_panic::debug_panic;
395
396use std::net::SocketAddr;
397
398use std::num::NonZeroUsize;
399
400use std::str::FromStr;
401
402use std::sync::Arc;
403
404use std::time::Duration;
405use std::time::Instant;
406
407#[cfg(feature = "qlog")]
408use qlog::events::quic::DataMovedAdditionalInfo;
409#[cfg(feature = "qlog")]
410use qlog::events::quic::QuicEventType;
411#[cfg(feature = "qlog")]
412use qlog::events::quic::TransportInitiator;
413#[cfg(feature = "qlog")]
414use qlog::events::DataRecipient;
415#[cfg(feature = "qlog")]
416use qlog::events::Event;
417#[cfg(feature = "qlog")]
418use qlog::events::EventData;
419#[cfg(feature = "qlog")]
420use qlog::events::EventImportance;
421#[cfg(feature = "qlog")]
422use qlog::events::EventType;
423#[cfg(feature = "qlog")]
424use qlog::events::RawInfo;
425
426use smallvec::SmallVec;
427
428use crate::buffers::DefaultBufFactory;
429
430use crate::recovery::OnAckReceivedOutcome;
431use crate::recovery::OnLossDetectionTimeoutOutcome;
432use crate::recovery::RecoveryOps;
433use crate::recovery::ReleaseDecision;
434
435use crate::stream::RecvAction;
436use crate::stream::StreamPriorityKey;
437
438/// The current QUIC wire version.
439pub const PROTOCOL_VERSION: u32 = PROTOCOL_VERSION_V1;
440
441/// Supported QUIC versions.
442const PROTOCOL_VERSION_V1: u32 = 0x0000_0001;
443
444/// The maximum length of a connection ID.
445pub const MAX_CONN_ID_LEN: usize = packet::MAX_CID_LEN as usize;
446
447/// The minimum length of Initial packets sent by a client.
448pub const MIN_CLIENT_INITIAL_LEN: usize = 1200;
449
450/// The default initial RTT.
451const DEFAULT_INITIAL_RTT: Duration = Duration::from_millis(333);
452
453const PAYLOAD_MIN_LEN: usize = 4;
454
455// PATH_CHALLENGE (9 bytes) + AEAD tag (16 bytes).
456const MIN_PROBING_SIZE: usize = 25;
457
458const MAX_AMPLIFICATION_FACTOR: usize = 3;
459
460// The maximum number of tracked packet number ranges that need to be acked.
461//
462// This represents more or less how many ack blocks can fit in a typical packet.
463const MAX_ACK_RANGES: usize = 68;
464
465// The highest possible stream ID allowed.
466const MAX_STREAM_ID: u64 = 1 << 60;
467
468// The default max_datagram_size used in congestion control.
469const MAX_SEND_UDP_PAYLOAD_SIZE: usize = 1200;
470
471// The default length of DATAGRAM queues.
472const DEFAULT_MAX_DGRAM_QUEUE_LEN: usize = 0;
473
474// The default length of PATH_CHALLENGE receive queue.
475const DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN: usize = 3;
476
477// The DATAGRAM standard recommends either none or 65536 as maximum DATAGRAM
478// frames size. We enforce the recommendation for forward compatibility.
479const MAX_DGRAM_FRAME_SIZE: u64 = 65536;
480
481// The length of the payload length field.
482const PAYLOAD_LENGTH_LEN: usize = 2;
483
484// The number of undecryptable that can be buffered.
485const MAX_UNDECRYPTABLE_PACKETS: usize = 10;
486
487const RESERVED_VERSION_MASK: u32 = 0xfafafafa;
488
489// The maximum size of the receiver connection flow control window.
490const MAX_CONNECTION_WINDOW: u64 = 24 * 1024 * 1024;
491
492// How much larger the connection flow control window need to be larger than
493// the stream flow control window.
494const CONNECTION_WINDOW_FACTOR: f64 = 1.5;
495
496// How many probing packet timeouts do we tolerate before considering the path
497// validation as failed.
498const MAX_PROBING_TIMEOUTS: usize = 3;
499
500// The default initial congestion window size in terms of packet count.
501const DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS: usize = 10;
502
503/// The default number of sent packets that can be tracked for loss detection
504/// before the connection is closed.
505pub const DEFAULT_MAX_OUTSTANDING_SENT_PACKETS: NonZeroUsize =
506 NonZeroUsize::new(22_000).unwrap();
507
508// The maximum data offset that can be stored in a crypto stream.
509const MAX_CRYPTO_STREAM_OFFSET: u64 = 1 << 16;
510
511// TODO: Remove once https://github.com/cloudflare/quiche/pull/2453 is done.
512// Preserve the previous app-limited threshold independently of STREAM frame
513// header sizing.
514const LEGACY_APP_LIMITED_BYTE_THRESHOLD: usize = 12;
515
516// The send capacity factor.
517const TX_CAP_FACTOR: f64 = 1.0;
518
519/// Ancillary information about incoming packets.
520#[derive(Clone, Copy, Debug, PartialEq, Eq)]
521pub struct RecvInfo {
522 /// The remote address the packet was received from.
523 pub from: SocketAddr,
524
525 /// The local address the packet was received on.
526 pub to: SocketAddr,
527}
528
529/// Ancillary information about outgoing packets.
530#[derive(Clone, Copy, Debug, PartialEq, Eq)]
531pub struct SendInfo {
532 /// The local address the packet should be sent from.
533 pub from: SocketAddr,
534
535 /// The remote address the packet should be sent to.
536 pub to: SocketAddr,
537
538 /// The time to send the packet out.
539 ///
540 /// See [Pacing] for more details.
541 ///
542 /// [Pacing]: index.html#pacing
543 pub at: Instant,
544}
545
546/// The side of the stream to be shut down.
547///
548/// This should be used when calling [`stream_shutdown()`].
549///
550/// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
551#[repr(C)]
552#[derive(PartialEq, Eq)]
553pub enum Shutdown {
554 /// Stop receiving stream data.
555 Read = 0,
556
557 /// Stop sending stream data.
558 Write = 1,
559}
560
561/// Qlog logging level.
562#[repr(C)]
563#[cfg(feature = "qlog")]
564#[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
565pub enum QlogLevel {
566 /// Logs any events of Core importance.
567 Core = 0,
568
569 /// Logs any events of Core and Base importance.
570 Base = 1,
571
572 /// Logs any events of Core, Base and Extra importance
573 Extra = 2,
574}
575
576/// Stores configuration shared between multiple connections.
577pub struct Config {
578 local_transport_params: TransportParams,
579
580 version: u32,
581
582 tls_ctx: tls::Context,
583
584 application_protos: Vec<Vec<u8>>,
585
586 grease: bool,
587
588 cc_algorithm: CongestionControlAlgorithm,
589 custom_bbr_params: Option<BbrParams>,
590 initial_congestion_window_packets: usize,
591 unchecked_congestion_window: usize,
592 enable_relaxed_loss_threshold: bool,
593 enable_cubic_idle_restart_fix: bool,
594 enable_send_streams_blocked: bool,
595
596 pmtud: bool,
597 pmtud_max_probes: u8,
598
599 hystart: bool,
600
601 pacing: bool,
602 /// Send rate limit in Mbps
603 max_pacing_rate: Option<u64>,
604
605 tx_cap_factor: f64,
606
607 dgram_recv_max_queue_len: usize,
608 dgram_send_max_queue_len: usize,
609
610 path_challenge_recv_max_queue_len: usize,
611
612 max_send_udp_payload_size: usize,
613
614 max_connection_window: u64,
615 max_stream_window: u64,
616
617 max_amplification_factor: usize,
618
619 disable_dcid_reuse: bool,
620
621 track_unknown_transport_params: Option<usize>,
622
623 initial_rtt: Duration,
624
625 max_outstanding_sent_packets: Option<NonZeroUsize>,
626}
627
628// See https://quicwg.org/base-drafts/rfc9000.html#section-15
629fn is_reserved_version(version: u32) -> bool {
630 version & RESERVED_VERSION_MASK == version
631}
632
633impl Config {
634 /// Creates a config object with the given version.
635 ///
636 /// ## Examples:
637 ///
638 /// ```
639 /// let config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
640 /// # Ok::<(), quiche::Error>(())
641 /// ```
642 pub fn new(version: u32) -> Result<Config> {
643 Self::with_tls_ctx(version, tls::Context::new()?)
644 }
645
646 /// Creates a config object with the given version and
647 /// [`SslContextBuilder`].
648 ///
649 /// This is useful for applications that wish to manually configure
650 /// [`SslContextBuilder`].
651 ///
652 /// [`SslContextBuilder`]: https://docs.rs/boring/latest/boring/ssl/struct.SslContextBuilder.html
653 #[cfg(feature = "boringssl-boring-crate")]
654 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
655 pub fn with_boring_ssl_ctx_builder(
656 version: u32, tls_ctx_builder: boring::ssl::SslContextBuilder,
657 ) -> Result<Config> {
658 Self::with_tls_ctx(version, tls::Context::from_boring(tls_ctx_builder)?)
659 }
660
661 fn with_tls_ctx(version: u32, tls_ctx: tls::Context) -> Result<Config> {
662 if !is_reserved_version(version) && !version_is_supported(version) {
663 return Err(Error::UnknownVersion);
664 }
665
666 Ok(Config {
667 local_transport_params: TransportParams::default(),
668 version,
669 tls_ctx,
670 application_protos: Vec::new(),
671 grease: true,
672 cc_algorithm: CongestionControlAlgorithm::CUBIC,
673 custom_bbr_params: None,
674 initial_congestion_window_packets:
675 DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS,
676 unchecked_congestion_window: usize::MAX,
677 enable_relaxed_loss_threshold: false,
678 enable_cubic_idle_restart_fix: true,
679 enable_send_streams_blocked: false,
680 pmtud: false,
681 pmtud_max_probes: pmtud::MAX_PROBES_DEFAULT,
682 hystart: true,
683 pacing: true,
684 max_pacing_rate: None,
685
686 tx_cap_factor: TX_CAP_FACTOR,
687
688 dgram_recv_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
689 dgram_send_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
690
691 path_challenge_recv_max_queue_len:
692 DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN,
693
694 max_send_udp_payload_size: MAX_SEND_UDP_PAYLOAD_SIZE,
695
696 max_connection_window: MAX_CONNECTION_WINDOW,
697 max_stream_window: stream::MAX_STREAM_WINDOW,
698
699 max_amplification_factor: MAX_AMPLIFICATION_FACTOR,
700
701 disable_dcid_reuse: false,
702
703 track_unknown_transport_params: None,
704 initial_rtt: DEFAULT_INITIAL_RTT,
705
706 max_outstanding_sent_packets: Some(
707 DEFAULT_MAX_OUTSTANDING_SENT_PACKETS,
708 ),
709 })
710 }
711
712 /// Configures the given certificate chain.
713 ///
714 /// The content of `file` is parsed as a PEM-encoded leaf certificate,
715 /// followed by optional intermediate certificates.
716 ///
717 /// ## Examples:
718 ///
719 /// ```no_run
720 /// # let mut config = quiche::Config::new(0xbabababa)?;
721 /// config.load_cert_chain_from_pem_file("/path/to/cert.pem")?;
722 /// # Ok::<(), quiche::Error>(())
723 /// ```
724 pub fn load_cert_chain_from_pem_file(&mut self, file: &str) -> Result<()> {
725 self.tls_ctx.use_certificate_chain_file(file)
726 }
727
728 /// Configures the given private key.
729 ///
730 /// The content of `file` is parsed as a PEM-encoded private key.
731 ///
732 /// ## Examples:
733 ///
734 /// ```no_run
735 /// # let mut config = quiche::Config::new(0xbabababa)?;
736 /// config.load_priv_key_from_pem_file("/path/to/key.pem")?;
737 /// # Ok::<(), quiche::Error>(())
738 /// ```
739 pub fn load_priv_key_from_pem_file(&mut self, file: &str) -> Result<()> {
740 self.tls_ctx.use_privkey_file(file)
741 }
742
743 /// Specifies a file where trusted CA certificates are stored for the
744 /// purposes of certificate verification.
745 ///
746 /// The content of `file` is parsed as a PEM-encoded certificate chain.
747 ///
748 /// ## Examples:
749 ///
750 /// ```no_run
751 /// # let mut config = quiche::Config::new(0xbabababa)?;
752 /// config.load_verify_locations_from_file("/path/to/cert.pem")?;
753 /// # Ok::<(), quiche::Error>(())
754 /// ```
755 pub fn load_verify_locations_from_file(&mut self, file: &str) -> Result<()> {
756 self.tls_ctx.load_verify_locations_from_file(file)
757 }
758
759 /// Specifies a directory where trusted CA certificates are stored for the
760 /// purposes of certificate verification.
761 ///
762 /// The content of `dir` a set of PEM-encoded certificate chains.
763 ///
764 /// ## Examples:
765 ///
766 /// ```no_run
767 /// # let mut config = quiche::Config::new(0xbabababa)?;
768 /// config.load_verify_locations_from_directory("/path/to/certs")?;
769 /// # Ok::<(), quiche::Error>(())
770 /// ```
771 pub fn load_verify_locations_from_directory(
772 &mut self, dir: &str,
773 ) -> Result<()> {
774 self.tls_ctx.load_verify_locations_from_directory(dir)
775 }
776
777 /// Configures the TLS curve preference list.
778 ///
779 /// `curves` is a colon-separated list of curve (a.k.a. group) names, in
780 /// order of preference, e.g. `"X25519MLKEM768:X25519:P-256:P-384"`.
781 /// Corresponds to `SSL_CTX_set1_curves_list` (a.k.a.
782 /// `SSL_CTX_set1_groups_list`).
783 pub fn set_curves_list(&mut self, curves: &str) -> Result<()> {
784 self.tls_ctx.set_curves_list(curves)
785 }
786
787 /// Configures whether to verify the peer's certificate.
788 ///
789 /// This should usually be `true` for client-side connections and `false`
790 /// for server-side ones.
791 ///
792 /// Note that by default, no verification is performed.
793 ///
794 /// Also note that on the server-side, enabling verification of the peer
795 /// will trigger a certificate request and make authentication errors
796 /// fatal, but will still allow anonymous clients (i.e. clients that
797 /// don't present a certificate at all). Servers can check whether a
798 /// client presented a certificate by calling [`peer_cert()`] if they
799 /// need to.
800 ///
801 /// [`peer_cert()`]: struct.Connection.html#method.peer_cert
802 pub fn verify_peer(&mut self, verify: bool) {
803 self.tls_ctx.set_verify(verify);
804 }
805
806 /// Configures whether to do path MTU discovery.
807 ///
808 /// PMTUD-driven packet limit updates are reported to the application
809 /// through [`PathEvent::PmtuUpdated`].
810 ///
811 /// The default value is `false`.
812 pub fn discover_pmtu(&mut self, discover: bool) {
813 self.pmtud = discover;
814 }
815
816 /// Configures the maximum number of PMTUD probe attempts before treating
817 /// a probe size as failed.
818 ///
819 /// Defaults to 3 per [RFC 8899 Section 5.1.2](https://datatracker.ietf.org/doc/html/rfc8899#section-5.1.2).
820 /// If 0 is passed, the default value is used.
821 pub fn set_pmtud_max_probes(&mut self, max_probes: u8) {
822 self.pmtud_max_probes = max_probes;
823 }
824
825 /// Configures whether to send GREASE values.
826 ///
827 /// The default value is `true`.
828 pub fn grease(&mut self, grease: bool) {
829 self.grease = grease;
830 }
831
832 /// Enables logging of secrets.
833 ///
834 /// When logging is enabled, the [`set_keylog()`] method must be called on
835 /// the connection for its cryptographic secrets to be logged in the
836 /// [keylog] format to the specified writer.
837 ///
838 /// [`set_keylog()`]: struct.Connection.html#method.set_keylog
839 /// [keylog]: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
840 pub fn log_keys(&mut self) {
841 self.tls_ctx.enable_keylog();
842 }
843
844 /// Configures the session ticket key material.
845 ///
846 /// On the server this key will be used to encrypt and decrypt session
847 /// tickets, used to perform session resumption without server-side state.
848 ///
849 /// By default a key is generated internally, and rotated regularly, so
850 /// applications don't need to call this unless they need to use a
851 /// specific key (e.g. in order to support resumption across multiple
852 /// servers), in which case the application is also responsible for
853 /// rotating the key to provide forward secrecy.
854 pub fn set_ticket_key(&mut self, key: &[u8]) -> Result<()> {
855 self.tls_ctx.set_ticket_key(key)
856 }
857
858 /// Enables sending or receiving early data.
859 pub fn enable_early_data(&mut self) {
860 self.tls_ctx.set_early_data_enabled(true);
861 }
862
863 /// Configures the list of supported application protocols.
864 ///
865 /// On the client this configures the list of protocols to send to the
866 /// server as part of the ALPN extension.
867 ///
868 /// On the server this configures the list of supported protocols to match
869 /// against the client-supplied list.
870 ///
871 /// Applications must set a value, but no default is provided.
872 ///
873 /// ## Examples:
874 ///
875 /// ```
876 /// # let mut config = quiche::Config::new(0xbabababa)?;
877 /// config.set_application_protos(&[b"http/1.1", b"http/0.9"]);
878 /// # Ok::<(), quiche::Error>(())
879 /// ```
880 pub fn set_application_protos(
881 &mut self, protos_list: &[&[u8]],
882 ) -> Result<()> {
883 self.application_protos =
884 protos_list.iter().map(|s| s.to_vec()).collect();
885
886 self.tls_ctx.set_alpn(protos_list)
887 }
888
889 /// Configures the list of supported application protocols using wire
890 /// format.
891 ///
892 /// The list of protocols `protos` must be a series of non-empty, 8-bit
893 /// length-prefixed strings.
894 ///
895 /// See [`set_application_protos`](Self::set_application_protos) for more
896 /// background about application protocols.
897 ///
898 /// ## Examples:
899 ///
900 /// ```
901 /// # let mut config = quiche::Config::new(0xbabababa)?;
902 /// config.set_application_protos_wire_format(b"\x08http/1.1\x08http/0.9")?;
903 /// # Ok::<(), quiche::Error>(())
904 /// ```
905 pub fn set_application_protos_wire_format(
906 &mut self, protos: &[u8],
907 ) -> Result<()> {
908 let mut b = octets::Octets::with_slice(protos);
909
910 let mut protos_list = Vec::new();
911
912 while let Ok(proto) = b.get_bytes_with_u8_length() {
913 protos_list.push(proto.buf());
914 }
915
916 self.set_application_protos(&protos_list)
917 }
918
919 /// Sets the anti-amplification limit factor.
920 ///
921 /// The default value is `3`.
922 pub fn set_max_amplification_factor(&mut self, v: usize) {
923 self.max_amplification_factor = v;
924 }
925
926 /// Sets the send capacity factor.
927 ///
928 /// The default value is `1`.
929 pub fn set_send_capacity_factor(&mut self, v: f64) {
930 self.tx_cap_factor = v;
931 }
932
933 /// Sets the connection's initial RTT.
934 ///
935 /// The default value is `333`.
936 pub fn set_initial_rtt(&mut self, v: Duration) {
937 self.initial_rtt = v;
938 }
939
940 /// Sets the number of sent packets that can be tracked for loss detection
941 /// before the connection is closed.
942 ///
943 /// This limits the memory used to track sent packets. Packets that only
944 /// carry ACK frames are counted too. The limit should be larger than the
945 /// expected maximum congestion window, in packets, otherwise connections
946 /// that make progress can be closed.
947 ///
948 /// The connection is closed with a `PROTOCOL_VIOLATION` transport error.
949 ///
950 /// `None` disables this limit. The default value is `22,000`.
951 pub fn set_max_outstanding_sent_packets(
952 &mut self, max_packets: Option<NonZeroUsize>,
953 ) {
954 self.max_outstanding_sent_packets = max_packets;
955 }
956
957 /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
958 ///
959 /// The default value is infinite, that is, no timeout is used.
960 pub fn set_max_idle_timeout(&mut self, v: u64) {
961 self.local_transport_params.max_idle_timeout =
962 cmp::min(v, octets::MAX_VAR_INT);
963 }
964
965 /// Sets the `max_udp_payload_size transport` parameter.
966 ///
967 /// The default value is `65527`.
968 pub fn set_max_recv_udp_payload_size(&mut self, v: usize) {
969 self.local_transport_params.max_udp_payload_size =
970 cmp::min(v as u64, octets::MAX_VAR_INT);
971 }
972
973 /// Sets the maximum outgoing UDP payload size.
974 ///
975 /// The default and minimum value is `1200`.
976 pub fn set_max_send_udp_payload_size(&mut self, v: usize) {
977 self.max_send_udp_payload_size = cmp::max(v, MAX_SEND_UDP_PAYLOAD_SIZE);
978 }
979
980 /// Sets the `initial_max_data` transport parameter.
981 ///
982 /// When set to a non-zero value quiche will only allow at most `v` bytes of
983 /// incoming stream data to be buffered for the whole connection (that is,
984 /// data that is not yet read by the application) and will allow more data
985 /// to be received as the buffer is consumed by the application.
986 ///
987 /// When set to zero, either explicitly or via the default, quiche will not
988 /// give any flow control to the peer, preventing it from sending any stream
989 /// data.
990 ///
991 /// The default value is `0`.
992 pub fn set_initial_max_data(&mut self, v: u64) {
993 self.local_transport_params.initial_max_data =
994 cmp::min(v, octets::MAX_VAR_INT);
995 }
996
997 /// Sets the `initial_max_stream_data_bidi_local` transport parameter.
998 ///
999 /// When set to a non-zero value quiche will only allow at most `v` bytes
1000 /// of incoming stream data to be buffered for each locally-initiated
1001 /// bidirectional stream (that is, data that is not yet read by the
1002 /// application) and will allow more data to be received as the buffer is
1003 /// consumed by the application.
1004 ///
1005 /// When set to zero, either explicitly or via the default, quiche will not
1006 /// give any flow control to the peer, preventing it from sending any stream
1007 /// data.
1008 ///
1009 /// The default value is `0`.
1010 pub fn set_initial_max_stream_data_bidi_local(&mut self, v: u64) {
1011 self.local_transport_params
1012 .initial_max_stream_data_bidi_local =
1013 cmp::min(v, octets::MAX_VAR_INT);
1014 }
1015
1016 /// Sets the `initial_max_stream_data_bidi_remote` transport parameter.
1017 ///
1018 /// When set to a non-zero value quiche will only allow at most `v` bytes
1019 /// of incoming stream data to be buffered for each remotely-initiated
1020 /// bidirectional stream (that is, data that is not yet read by the
1021 /// application) and will allow more data to be received as the buffer is
1022 /// consumed by the application.
1023 ///
1024 /// When set to zero, either explicitly or via the default, quiche will not
1025 /// give any flow control to the peer, preventing it from sending any stream
1026 /// data.
1027 ///
1028 /// The default value is `0`.
1029 pub fn set_initial_max_stream_data_bidi_remote(&mut self, v: u64) {
1030 self.local_transport_params
1031 .initial_max_stream_data_bidi_remote =
1032 cmp::min(v, octets::MAX_VAR_INT);
1033 }
1034
1035 /// Sets the `initial_max_stream_data_uni` transport parameter.
1036 ///
1037 /// When set to a non-zero value quiche will only allow at most `v` bytes
1038 /// of incoming stream data to be buffered for each unidirectional stream
1039 /// (that is, data that is not yet read by the application) and will allow
1040 /// more data to be received as the buffer is consumed by the application.
1041 ///
1042 /// When set to zero, either explicitly or via the default, quiche will not
1043 /// give any flow control to the peer, preventing it from sending any stream
1044 /// data.
1045 ///
1046 /// The default value is `0`.
1047 pub fn set_initial_max_stream_data_uni(&mut self, v: u64) {
1048 self.local_transport_params.initial_max_stream_data_uni =
1049 cmp::min(v, octets::MAX_VAR_INT);
1050 }
1051
1052 /// Sets the `initial_max_streams_bidi` transport parameter.
1053 ///
1054 /// When set to a non-zero value quiche will only allow `v` number of
1055 /// concurrent remotely-initiated bidirectional streams to be open at any
1056 /// given time and will increase the limit automatically as streams are
1057 /// completed.
1058 ///
1059 /// When set to zero, either explicitly or via the default, quiche will not
1060 /// not allow the peer to open any bidirectional streams.
1061 ///
1062 /// A bidirectional stream is considered completed when all incoming data
1063 /// has been read by the application (up to the `fin` offset) or the
1064 /// stream's read direction has been shutdown, and all outgoing data has
1065 /// been acked by the peer (up to the `fin` offset) or the stream's write
1066 /// direction has been shutdown and the resulting `RESET_STREAM` frame has
1067 /// been acked by the peer.
1068 ///
1069 /// The default value is `0`.
1070 pub fn set_initial_max_streams_bidi(&mut self, v: u64) {
1071 self.local_transport_params.initial_max_streams_bidi =
1072 cmp::min(v, octets::MAX_VAR_INT);
1073 }
1074
1075 /// Sets the `initial_max_streams_uni` transport parameter.
1076 ///
1077 /// When set to a non-zero value quiche will only allow `v` number of
1078 /// concurrent remotely-initiated unidirectional streams to be open at any
1079 /// given time and will increase the limit automatically as streams are
1080 /// completed.
1081 ///
1082 /// When set to zero, either explicitly or via the default, quiche will not
1083 /// not allow the peer to open any unidirectional streams.
1084 ///
1085 /// A unidirectional stream is considered completed when all incoming data
1086 /// has been read by the application (up to the `fin` offset) or the
1087 /// stream's read direction has been shutdown.
1088 ///
1089 /// The default value is `0`.
1090 pub fn set_initial_max_streams_uni(&mut self, v: u64) {
1091 self.local_transport_params.initial_max_streams_uni =
1092 cmp::min(v, octets::MAX_VAR_INT);
1093 }
1094
1095 /// Sets the `ack_delay_exponent` transport parameter.
1096 ///
1097 /// Values above the RFC 9000 maximum of
1098 /// [`MAX_ACK_DELAY_EXPONENT`] (20) are clamped to that
1099 /// maximum.
1100 ///
1101 /// The default value is `3`.
1102 pub fn set_ack_delay_exponent(&mut self, v: u64) {
1103 self.local_transport_params.ack_delay_exponent =
1104 cmp::min(v, MAX_ACK_DELAY_EXPONENT);
1105 }
1106
1107 /// Sets the `max_ack_delay` transport parameter.
1108 ///
1109 /// The default value is `25`.
1110 pub fn set_max_ack_delay(&mut self, v: u64) {
1111 self.local_transport_params.max_ack_delay =
1112 cmp::min(v, octets::MAX_VAR_INT);
1113 }
1114
1115 /// Sets the `active_connection_id_limit` transport parameter.
1116 ///
1117 /// The default value is `2`. Lower values will be ignored.
1118 pub fn set_active_connection_id_limit(&mut self, v: u64) {
1119 if v >= 2 {
1120 self.local_transport_params.active_conn_id_limit =
1121 cmp::min(v, octets::MAX_VAR_INT);
1122 }
1123 }
1124
1125 /// Sets the `disable_active_migration` transport parameter.
1126 ///
1127 /// The default value is `false`.
1128 pub fn set_disable_active_migration(&mut self, v: bool) {
1129 self.local_transport_params.disable_active_migration = v;
1130 }
1131
1132 /// Sets the congestion control algorithm used.
1133 ///
1134 /// The default value is `CongestionControlAlgorithm::CUBIC`.
1135 pub fn set_cc_algorithm(&mut self, algo: CongestionControlAlgorithm) {
1136 self.cc_algorithm = algo;
1137 }
1138
1139 /// Sets custom BBR settings.
1140 ///
1141 /// This API is experimental and will be removed in the future.
1142 ///
1143 /// Currently this only applies if cc_algorithm is
1144 /// `CongestionControlAlgorithm::Bbr2Gcongestion` is set.
1145 ///
1146 /// The default value is `None`.
1147 #[cfg(feature = "internal")]
1148 #[doc(hidden)]
1149 pub fn set_custom_bbr_params(&mut self, custom_bbr_settings: BbrParams) {
1150 self.custom_bbr_params = Some(custom_bbr_settings);
1151 }
1152
1153 /// Sets the congestion control algorithm used by string.
1154 ///
1155 /// The default value is `cubic`. On error `Error::CongestionControl`
1156 /// will be returned.
1157 ///
1158 /// ## Examples:
1159 ///
1160 /// ```
1161 /// # let mut config = quiche::Config::new(0xbabababa)?;
1162 /// config.set_cc_algorithm_name("reno");
1163 /// # Ok::<(), quiche::Error>(())
1164 /// ```
1165 pub fn set_cc_algorithm_name(&mut self, name: &str) -> Result<()> {
1166 self.cc_algorithm = CongestionControlAlgorithm::from_str(name)?;
1167
1168 Ok(())
1169 }
1170
1171 /// Sets initial congestion window size in terms of packet count.
1172 ///
1173 /// The default value is 10.
1174 pub fn set_initial_congestion_window_packets(&mut self, packets: usize) {
1175 self.initial_congestion_window_packets = packets;
1176 }
1177
1178 /// Sets the fixed congestion window in bytes for the
1179 /// `congestion_window_unchecked` algorithm.
1180 ///
1181 /// The default is `usize::MAX`. Other congestion control algorithms ignore
1182 /// this setting. Recovery probes can temporarily bypass this window.
1183 ///
1184 /// Values are not clamped. Zero can prevent connection establishment, and
1185 /// windows too small for outgoing packets or PMTU probes can stall
1186 /// transmission. At least two maximum-sized datagrams are recommended.
1187 ///
1188 /// This API is available even when the
1189 /// `congestion_window_unchecked_available` feature is disabled, in which
1190 /// case the setting has no effect.
1191 pub fn set_unchecked_congestion_window(&mut self, window: usize) {
1192 self.unchecked_congestion_window = window;
1193 }
1194
1195 /// Configure whether to enable relaxed loss detection on spurious loss.
1196 ///
1197 /// The default value is false.
1198 pub fn set_enable_relaxed_loss_threshold(&mut self, enable: bool) {
1199 self.enable_relaxed_loss_threshold = enable;
1200 }
1201
1202 /// Configure whether to enable the CUBIC idle restart fix.
1203 ///
1204 /// When enabled, the epoch shift on idle restart uses the later of
1205 /// the last ACK time and last send time, avoiding an inflated delta
1206 /// when bytes-in-flight transiently hits zero.
1207 ///
1208 /// The default value is `true`.
1209 pub fn set_enable_cubic_idle_restart_fix(&mut self, enable: bool) {
1210 self.enable_cubic_idle_restart_fix = enable;
1211 }
1212
1213 /// Configure whether to enable sending STREAMS_BLOCKED frames.
1214 ///
1215 /// STREAMS_BLOCKED frames are an optional advisory signal in the QUIC
1216 /// protocol which SHOULD be sent when the sender wishes to open a stream
1217 /// but is unable to do so due to the maximum stream limit set by its peer.
1218 ///
1219 /// The default value is false.
1220 pub fn set_enable_send_streams_blocked(&mut self, enable: bool) {
1221 self.enable_send_streams_blocked = enable;
1222 }
1223
1224 /// Configures whether to enable HyStart++.
1225 ///
1226 /// The default value is `true`.
1227 pub fn enable_hystart(&mut self, v: bool) {
1228 self.hystart = v;
1229 }
1230
1231 /// Configures whether to enable pacing.
1232 ///
1233 /// The default value is `true`.
1234 pub fn enable_pacing(&mut self, v: bool) {
1235 self.pacing = v;
1236 }
1237
1238 /// Sets the max value for pacing rate.
1239 ///
1240 /// By default pacing rate is not limited.
1241 pub fn set_max_pacing_rate(&mut self, v: u64) {
1242 self.max_pacing_rate = Some(v);
1243 }
1244
1245 /// Configures whether to enable receiving DATAGRAM frames.
1246 ///
1247 /// When enabled, the `max_datagram_frame_size` transport parameter is set
1248 /// to 65536 as recommended by draft-ietf-quic-datagram-01.
1249 ///
1250 /// The default is `false`.
1251 pub fn enable_dgram(
1252 &mut self, enabled: bool, recv_queue_len: usize, send_queue_len: usize,
1253 ) {
1254 self.local_transport_params.max_datagram_frame_size = if enabled {
1255 Some(MAX_DGRAM_FRAME_SIZE)
1256 } else {
1257 None
1258 };
1259 self.dgram_recv_max_queue_len = recv_queue_len;
1260 self.dgram_send_max_queue_len = send_queue_len;
1261 }
1262
1263 /// Configures the max number of queued received PATH_CHALLENGE frames.
1264 ///
1265 /// When an endpoint receives a PATH_CHALLENGE frame and the queue is full,
1266 /// the frame is discarded.
1267 ///
1268 /// The default is 3.
1269 pub fn set_path_challenge_recv_max_queue_len(&mut self, queue_len: usize) {
1270 self.path_challenge_recv_max_queue_len = queue_len;
1271 }
1272
1273 /// Sets the maximum size of the connection window.
1274 ///
1275 /// The default value is MAX_CONNECTION_WINDOW (24MBytes).
1276 pub fn set_max_connection_window(&mut self, v: u64) {
1277 self.max_connection_window = v;
1278 }
1279
1280 /// Sets the maximum size of the stream window.
1281 ///
1282 /// The default value is MAX_STREAM_WINDOW (16MBytes).
1283 pub fn set_max_stream_window(&mut self, v: u64) {
1284 self.max_stream_window = v;
1285 }
1286
1287 /// Sets the initial stateless reset token.
1288 ///
1289 /// This value is only advertised by servers. Setting a stateless retry
1290 /// token as a client has no effect on the connection.
1291 ///
1292 /// The default value is `None`.
1293 pub fn set_stateless_reset_token(&mut self, v: Option<u128>) {
1294 self.local_transport_params.stateless_reset_token = v;
1295 }
1296
1297 /// Sets whether the QUIC connection should avoid reusing DCIDs over
1298 /// different paths.
1299 ///
1300 /// When set to `true`, it ensures that a destination Connection ID is never
1301 /// reused on different paths. Such behaviour may lead to connection stall
1302 /// if the peer performs a non-voluntary migration (e.g., NAT rebinding) and
1303 /// does not provide additional destination Connection IDs to handle such
1304 /// event.
1305 ///
1306 /// The default value is `false`.
1307 pub fn set_disable_dcid_reuse(&mut self, v: bool) {
1308 self.disable_dcid_reuse = v;
1309 }
1310
1311 /// Enables tracking unknown transport parameters.
1312 ///
1313 /// Specify the maximum number of bytes used to track unknown transport
1314 /// parameters. The size includes the identifier and its value. If storing a
1315 /// transport parameter would cause the limit to be exceeded, it is quietly
1316 /// dropped.
1317 ///
1318 /// The default is that the feature is disabled.
1319 pub fn enable_track_unknown_transport_parameters(&mut self, size: usize) {
1320 self.track_unknown_transport_params = Some(size);
1321 }
1322
1323 /// Sets whether the initial max data value should be used as the initial
1324 /// flow control window.
1325 ///
1326 /// This is now always enabled and this method is a no-op. It will be
1327 /// removed in a future release.
1328 #[deprecated(note = "This is now always enabled. This method is a no-op.")]
1329 pub fn set_use_initial_max_data_as_flow_control_win(&mut self, _v: bool) {}
1330}
1331
1332/// Tracks the health of the tx_buffered value.
1333#[derive(Clone, Copy, Debug, Default, PartialEq)]
1334pub enum TxBufferTrackingState {
1335 /// The send buffer is in a good state
1336 #[default]
1337 Ok,
1338 /// The send buffer is in an inconsistent state, which could lead to
1339 /// connection stalls or excess buffering due to bugs we haven't
1340 /// tracked down yet.
1341 Inconsistent,
1342}
1343
1344/// Tracks if the connection hit the peer stream limit and which
1345/// STREAMS_BLOCKED frames have been sent.
1346#[derive(Default)]
1347struct StreamsBlockedState {
1348 /// The peer's max_streams limit at which we last became blocked on
1349 /// opening new local streams, if any.
1350 blocked_at: Option<u64>,
1351
1352 /// The stream limit sent on the most recently sent STREAMS_BLOCKED
1353 /// frame. If != to blocked_at, the connection has pending STREAMS_BLOCKED
1354 /// frames to send.
1355 blocked_sent: Option<u64>,
1356}
1357
1358impl StreamsBlockedState {
1359 /// Returns true if there is a STREAMS_BLOCKED frame that needs sending.
1360 fn has_pending_stream_blocked_frame(&self) -> bool {
1361 self.blocked_sent < self.blocked_at
1362 }
1363
1364 /// Update the stream blocked limit.
1365 fn update_at(&mut self, limit: u64) {
1366 self.blocked_at = self.blocked_at.max(Some(limit));
1367 }
1368
1369 /// Clear blocked_sent to force retransmission of the most recently sent
1370 /// STREAMS_BLOCKED frame.
1371 fn force_retransmit_sent_limit_eq(&mut self, limit: u64) {
1372 // Only clear blocked_sent if the lost frame had the most recently sent
1373 // limit.
1374 if self.blocked_sent == Some(limit) {
1375 self.blocked_sent = None;
1376 }
1377 }
1378}
1379
1380/// A QUIC connection.
1381pub struct Connection<F = DefaultBufFactory>
1382where
1383 F: BufFactory,
1384{
1385 /// QUIC wire version used for the connection.
1386 version: u32,
1387
1388 /// Connection Identifiers.
1389 ids: cid::ConnectionIdentifiers,
1390
1391 /// Unique opaque ID for the connection that can be used for logging.
1392 trace_id: String,
1393
1394 /// Packet number spaces.
1395 pkt_num_spaces: [packet::PktNumSpace; packet::Epoch::count()],
1396
1397 /// The crypto context.
1398 crypto_ctx: [packet::CryptoContext; packet::Epoch::count()],
1399
1400 /// Next packet number.
1401 next_pkt_num: u64,
1402
1403 // TODO
1404 // combine with `next_pkt_num`
1405 /// Track the packet skip context
1406 pkt_num_manager: packet::PktNumManager,
1407
1408 /// Peer's transport parameters.
1409 peer_transport_params: TransportParams,
1410
1411 /// If tracking unknown transport parameters from a peer, how much space to
1412 /// use in bytes.
1413 peer_transport_params_track_unknown: Option<usize>,
1414
1415 /// Local transport parameters.
1416 local_transport_params: TransportParams,
1417
1418 /// TLS handshake state.
1419 handshake: tls::Handshake,
1420
1421 /// Serialized TLS session buffer.
1422 ///
1423 /// This field is populated when a new session ticket is processed on the
1424 /// client. On the server this is empty.
1425 session: Option<Vec<u8>>,
1426
1427 /// The configuration for recovery.
1428 recovery_config: recovery::RecoveryConfig,
1429
1430 /// The path manager.
1431 paths: path::PathMap,
1432
1433 /// PATH_CHALLENGE receive queue max length.
1434 path_challenge_recv_max_queue_len: usize,
1435
1436 /// Total number of received PATH_CHALLENGE frames.
1437 path_challenge_rx_count: u64,
1438
1439 /// List of supported application protocols.
1440 application_protos: Vec<Vec<u8>>,
1441
1442 /// Total number of received packets.
1443 recv_count: usize,
1444
1445 /// Total number of sent packets.
1446 sent_count: usize,
1447
1448 /// Total number of lost packets.
1449 lost_count: usize,
1450
1451 /// Total number of lost packets that were later acked.
1452 spurious_lost_count: usize,
1453
1454 /// Total number of packets sent with data retransmitted.
1455 retrans_count: usize,
1456
1457 /// Total number of sent DATAGRAM frames.
1458 dgram_sent_count: usize,
1459
1460 /// Total number of received DATAGRAM frames.
1461 dgram_recv_count: usize,
1462
1463 /// Total number of bytes received from the peer.
1464 rx_data: u64,
1465
1466 /// Receiver flow controller.
1467 flow_control: flowcontrol::FlowControl,
1468
1469 /// Whether we send MAX_DATA frame.
1470 should_send_max_data: bool,
1471
1472 /// True if there is a pending MAX_STREAMS_BIDI frame to send.
1473 should_send_max_streams_bidi: bool,
1474
1475 /// True if there is a pending MAX_STREAMS_UNI frame to send.
1476 should_send_max_streams_uni: bool,
1477
1478 /// Number of stream data bytes that can be buffered.
1479 tx_cap: usize,
1480
1481 /// The send capacity factor.
1482 tx_cap_factor: f64,
1483
1484 /// Total number of bytes sent to the peer.
1485 tx_data: u64,
1486
1487 /// Peer's flow control limit for the connection.
1488 max_tx_data: u64,
1489
1490 /// Last tx_data before running a full send() loop.
1491 last_tx_data: u64,
1492
1493 /// Total number of bytes retransmitted over the connection.
1494 /// This counts only STREAM and CRYPTO data.
1495 stream_retrans_bytes: u64,
1496
1497 /// Total number of bytes sent over the connection.
1498 sent_bytes: u64,
1499
1500 /// Total number of bytes received over the connection.
1501 recv_bytes: u64,
1502
1503 /// Total number of bytes sent acked over the connection.
1504 acked_bytes: u64,
1505
1506 /// Total number of bytes sent lost over the connection.
1507 lost_bytes: u64,
1508
1509 /// Streams map, indexed by stream ID.
1510 pub(crate) streams: stream::StreamMap<F>,
1511
1512 /// Peer's original destination connection ID. Used by the client to
1513 /// validate the server's transport parameter.
1514 odcid: Option<ConnectionId<'static>>,
1515
1516 /// Peer's retry source connection ID. Used by the client during stateless
1517 /// retry to validate the server's transport parameter.
1518 rscid: Option<ConnectionId<'static>>,
1519
1520 /// Received address verification token.
1521 token: Option<Vec<u8>>,
1522
1523 /// Error code and reason to be sent to the peer in a CONNECTION_CLOSE
1524 /// frame.
1525 local_error: Option<ConnectionError>,
1526
1527 /// Error code and reason received from the peer in a CONNECTION_CLOSE
1528 /// frame.
1529 peer_error: Option<ConnectionError>,
1530
1531 /// The connection-level limit at which send blocking occurred.
1532 blocked_limit: Option<u64>,
1533
1534 /// Idle timeout expiration time.
1535 idle_timer: Option<Instant>,
1536
1537 /// Draining timeout expiration time.
1538 draining_timer: Option<Instant>,
1539
1540 /// List of raw packets that were received before they could be decrypted.
1541 undecryptable_pkts: VecDeque<(Vec<u8>, RecvInfo)>,
1542
1543 /// The negotiated ALPN protocol.
1544 alpn: Vec<u8>,
1545
1546 /// Whether this is a server-side connection.
1547 is_server: bool,
1548
1549 /// Whether the initial secrets have been derived.
1550 derived_initial_secrets: bool,
1551
1552 /// Whether a version negotiation packet has already been received. Only
1553 /// relevant for client connections.
1554 did_version_negotiation: bool,
1555
1556 /// Whether stateless retry has been performed.
1557 did_retry: bool,
1558
1559 /// Whether the peer already updated its connection ID.
1560 got_peer_conn_id: bool,
1561
1562 /// Whether the peer verified our initial address.
1563 peer_verified_initial_address: bool,
1564
1565 /// Whether the peer's transport parameters were parsed.
1566 parsed_peer_transport_params: bool,
1567
1568 /// Whether the connection handshake has been completed.
1569 handshake_completed: bool,
1570
1571 /// Whether the HANDSHAKE_DONE frame has been sent.
1572 handshake_done_sent: bool,
1573
1574 /// Whether the HANDSHAKE_DONE frame has been acked.
1575 handshake_done_acked: bool,
1576
1577 /// Whether the connection handshake has been confirmed.
1578 handshake_confirmed: bool,
1579
1580 /// Key phase bit used for outgoing protected packets.
1581 key_phase: bool,
1582
1583 /// Whether an ack-eliciting packet has been sent since last receiving a
1584 /// packet.
1585 ack_eliciting_sent: bool,
1586
1587 /// Whether the connection is closed.
1588 closed: bool,
1589
1590 /// Whether the connection was timed out.
1591 timed_out: bool,
1592
1593 /// Whether to send GREASE.
1594 grease: bool,
1595
1596 /// Whether to send STREAMS_BLOCKED frames when bidi or uni stream quota
1597 /// exhausted.
1598 enable_send_streams_blocked: bool,
1599
1600 /// TLS keylog writer.
1601 keylog: Option<Box<dyn std::io::Write + Send + Sync>>,
1602
1603 #[cfg(feature = "qlog")]
1604 qlog: QlogInfo,
1605
1606 /// DATAGRAM queues.
1607 dgram_recv_queue: dgram::DatagramQueue<F>,
1608 dgram_send_queue: dgram::DatagramQueue<F>,
1609
1610 /// Whether to emit DATAGRAM frames in the next packet.
1611 emit_dgram: bool,
1612
1613 /// Whether the connection should prevent from reusing destination
1614 /// Connection IDs when the peer migrates.
1615 disable_dcid_reuse: bool,
1616
1617 /// The number of streams reset by local.
1618 reset_stream_local_count: u64,
1619
1620 /// The number of streams stopped by local.
1621 stopped_stream_local_count: u64,
1622
1623 /// The number of streams reset by remote.
1624 reset_stream_remote_count: u64,
1625
1626 /// The number of streams stopped by remote.
1627 stopped_stream_remote_count: u64,
1628
1629 /// The number of DATA_BLOCKED frames sent due to hitting the connection
1630 /// flow control limit.
1631 data_blocked_sent_count: u64,
1632
1633 /// The number of STREAM_DATA_BLOCKED frames sent due to a stream hitting
1634 /// the stream flow control limit.
1635 stream_data_blocked_sent_count: u64,
1636
1637 /// The number of DATA_BLOCKED frames received from the remote endpoint.
1638 data_blocked_recv_count: u64,
1639
1640 /// The number of STREAM_DATA_BLOCKED frames received from the remote
1641 /// endpoint.
1642 stream_data_blocked_recv_count: u64,
1643
1644 /// The number of STREAMS_BLOCKED frames received from the remote endpoint
1645 /// indicating the peer is blocked on opening new bidirectional streams.
1646 streams_blocked_bidi_recv_count: u64,
1647
1648 /// The number of STREAMS_BLOCKED frames received from the remote endpoint
1649 /// indicating the peer is blocked on opening new unidirectional streams.
1650 streams_blocked_uni_recv_count: u64,
1651
1652 /// The number of times send() was blocked because the anti-amplification
1653 /// budget (bytes received × max_amplification_factor) was exhausted.
1654 amplification_limited_count: u64,
1655
1656 /// Tracks if the connection hit the peer's bidi or uni stream limit, and if
1657 /// STREAMS_BLOCKED frames are pending transmission.
1658 streams_blocked_bidi_state: StreamsBlockedState,
1659 streams_blocked_uni_state: StreamsBlockedState,
1660
1661 /// The anti-amplification limit factor.
1662 max_amplification_factor: usize,
1663
1664 /// The number of tracked sent packets after which the connection is
1665 /// closed.
1666 max_outstanding_sent_packets: Option<NonZeroUsize>,
1667}
1668
1669/// Creates a new server-side connection.
1670///
1671/// The `scid` parameter represents the server's source connection ID, while
1672/// the optional `odcid` parameter represents the original destination ID the
1673/// client sent before a Retry packet (this is only required when using the
1674/// [`retry()`] function). See also the [`accept_with_retry()`] function for
1675/// more advanced retry cases.
1676///
1677/// [`retry()`]: fn.retry.html
1678///
1679/// ## Examples:
1680///
1681/// ```no_run
1682/// # let mut config = quiche::Config::new(0xbabababa)?;
1683/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1684/// # let local = "127.0.0.1:0".parse().unwrap();
1685/// # let peer = "127.0.0.1:1234".parse().unwrap();
1686/// let conn = quiche::accept(&scid, None, local, peer, &mut config)?;
1687/// # Ok::<(), quiche::Error>(())
1688/// ```
1689#[inline(always)]
1690pub fn accept(
1691 scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1692 peer: SocketAddr, config: &mut Config,
1693) -> Result<Connection> {
1694 accept_with_buf_factory(scid, odcid, local, peer, config)
1695}
1696
1697/// Creates a server-side connection with custom buffer generation.
1698///
1699/// The buffers generated can be anything that can be dereferenced as a byte
1700/// slice. See [`accept`] and [`BufFactory`] for more information.
1701#[inline]
1702pub fn accept_with_buf_factory<F: BufFactory>(
1703 scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1704 peer: SocketAddr, config: &mut Config,
1705) -> Result<Connection<F>> {
1706 // Connections with `odcid` historically used `scid` as the retry source
1707 // CID. Preserve this behavior for backwards compatibility.
1708 // `accept_with_retry` allows the SCIDs to be specified separately.
1709 let retry_cids = odcid.map(|odcid| RetryConnectionIds {
1710 original_destination_cid: odcid,
1711 retry_source_cid: scid,
1712 });
1713 Connection::new(scid, retry_cids, None, local, peer, config, true)
1714}
1715
1716/// A wrapper for connection IDs used in [`accept_with_retry`].
1717pub struct RetryConnectionIds<'a> {
1718 /// The DCID of the first Initial packet received by the server, which
1719 /// triggered the Retry packet.
1720 pub original_destination_cid: &'a ConnectionId<'a>,
1721 /// The SCID of the Retry packet sent by the server. This can be different
1722 /// from the new connection's SCID.
1723 pub retry_source_cid: &'a ConnectionId<'a>,
1724}
1725
1726/// Creates a new server-side connection after the client responded to a Retry
1727/// packet.
1728///
1729/// To generate a Retry packet in the first place, use the [`retry()`] function.
1730///
1731/// The `scid` parameter represents the server's source connection ID, which can
1732/// be freshly generated after the application has successfully verified the
1733/// Retry. `retry_cids` is used to tie the new connection to the Initial + Retry
1734/// exchange that preceded the connection's creation.
1735///
1736/// The DCID of the client's Initial packet is inherently untrusted data. It is
1737/// safe to use the DCID in the `retry_source_cid` field of the
1738/// `RetryConnectionIds` provided to this function. However, using the Initial's
1739/// DCID for the `scid` parameter carries risks. Applications are advised to
1740/// implement their own DCID validation steps before using the DCID in that
1741/// manner.
1742#[inline]
1743pub fn accept_with_retry<F: BufFactory>(
1744 scid: &ConnectionId, retry_cids: RetryConnectionIds, local: SocketAddr,
1745 peer: SocketAddr, config: &mut Config,
1746) -> Result<Connection<F>> {
1747 Connection::new(scid, Some(retry_cids), None, local, peer, config, true)
1748}
1749
1750/// Creates a new client-side connection.
1751///
1752/// The `scid` parameter is used as the connection's source connection ID,
1753/// while the optional `server_name` parameter is used to verify the peer's
1754/// certificate.
1755///
1756/// ## Examples:
1757///
1758/// ```no_run
1759/// # let mut config = quiche::Config::new(0xbabababa)?;
1760/// # let server_name = "quic.tech";
1761/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1762/// # let local = "127.0.0.1:4321".parse().unwrap();
1763/// # let peer = "127.0.0.1:1234".parse().unwrap();
1764/// let conn =
1765/// quiche::connect(Some(&server_name), &scid, local, peer, &mut config)?;
1766/// # Ok::<(), quiche::Error>(())
1767/// ```
1768#[inline]
1769pub fn connect(
1770 server_name: Option<&str>, scid: &ConnectionId, local: SocketAddr,
1771 peer: SocketAddr, config: &mut Config,
1772) -> Result<Connection> {
1773 let mut conn = Connection::new(scid, None, None, local, peer, config, false)?;
1774
1775 if let Some(server_name) = server_name {
1776 conn.handshake.set_host_name(server_name)?;
1777 }
1778
1779 Ok(conn)
1780}
1781
1782/// Creates a new client-side connection using the given DCID initially.
1783///
1784/// Be aware that [RFC 9000] places requirements for unpredictability and length
1785/// on the client DCID field. This function is dangerous if these requirements
1786/// are not satisfied.
1787///
1788/// The `scid` parameter is used as the connection's source connection ID, while
1789/// the optional `server_name` parameter is used to verify the peer's
1790/// certificate.
1791///
1792/// [RFC 9000]: <https://datatracker.ietf.org/doc/html/rfc9000#section-7.2-3>
1793#[cfg(feature = "custom-client-dcid")]
1794#[cfg_attr(docsrs, doc(cfg(feature = "custom-client-dcid")))]
1795pub fn connect_with_dcid(
1796 server_name: Option<&str>, scid: &ConnectionId, dcid: &ConnectionId,
1797 local: SocketAddr, peer: SocketAddr, config: &mut Config,
1798) -> Result<Connection> {
1799 let mut conn =
1800 Connection::new(scid, None, Some(dcid), local, peer, config, false)?;
1801
1802 if let Some(server_name) = server_name {
1803 conn.handshake.set_host_name(server_name)?;
1804 }
1805
1806 Ok(conn)
1807}
1808
1809/// Creates a new client-side connection, with a custom buffer generation
1810/// method.
1811///
1812/// The buffers generated can be anything that can be drereferenced as a byte
1813/// slice. See [`connect`] and [`BufFactory`] for more info.
1814#[inline]
1815pub fn connect_with_buffer_factory<F: BufFactory>(
1816 server_name: Option<&str>, scid: &ConnectionId, local: SocketAddr,
1817 peer: SocketAddr, config: &mut Config,
1818) -> Result<Connection<F>> {
1819 let mut conn = Connection::new(scid, None, None, local, peer, config, false)?;
1820
1821 if let Some(server_name) = server_name {
1822 conn.handshake.set_host_name(server_name)?;
1823 }
1824
1825 Ok(conn)
1826}
1827
1828/// Creates a new client-side connection, with a custom buffer generation
1829/// method using the given dcid initially.
1830/// Be aware the RFC places requirements for unpredictability and length
1831/// on the client DCID field.
1832/// [`RFC9000`]: https://datatracker.ietf.org/doc/html/rfc9000#section-7.2-3
1833///
1834/// The buffers generated can be anything that can be drereferenced as a byte
1835/// slice. See [`connect`] and [`BufFactory`] for more info.
1836#[cfg(feature = "custom-client-dcid")]
1837#[cfg_attr(docsrs, doc(cfg(feature = "custom-client-dcid")))]
1838pub fn connect_with_dcid_and_buffer_factory<F: BufFactory>(
1839 server_name: Option<&str>, scid: &ConnectionId, dcid: &ConnectionId,
1840 local: SocketAddr, peer: SocketAddr, config: &mut Config,
1841) -> Result<Connection<F>> {
1842 let mut conn =
1843 Connection::new(scid, None, Some(dcid), local, peer, config, false)?;
1844
1845 if let Some(server_name) = server_name {
1846 conn.handshake.set_host_name(server_name)?;
1847 }
1848
1849 Ok(conn)
1850}
1851
1852/// Writes a version negotiation packet.
1853///
1854/// The `scid` and `dcid` parameters are the source connection ID and the
1855/// destination connection ID extracted from the received client's Initial
1856/// packet that advertises an unsupported version.
1857///
1858/// ## Examples:
1859///
1860/// ```no_run
1861/// # let mut buf = [0; 512];
1862/// # let mut out = [0; 512];
1863/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1864/// let (len, src) = socket.recv_from(&mut buf).unwrap();
1865///
1866/// let hdr =
1867/// quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1868///
1869/// if hdr.version != quiche::PROTOCOL_VERSION {
1870/// let len = quiche::negotiate_version(&hdr.scid, &hdr.dcid, &mut out)?;
1871/// socket.send_to(&out[..len], &src).unwrap();
1872/// }
1873/// # Ok::<(), quiche::Error>(())
1874/// ```
1875#[inline]
1876pub fn negotiate_version(
1877 scid: &ConnectionId, dcid: &ConnectionId, out: &mut [u8],
1878) -> Result<usize> {
1879 packet::negotiate_version(scid, dcid, out)
1880}
1881
1882/// Writes a stateless retry packet.
1883///
1884/// The `scid` and `dcid` parameters are the source connection ID and the
1885/// destination connection ID extracted from the received client's Initial
1886/// packet, while `new_scid` is the server's new source connection ID and
1887/// `token` is the address validation token the client needs to echo back.
1888///
1889/// The application is responsible for generating the address validation
1890/// token to be sent to the client, and verifying tokens sent back by the
1891/// client. The generated token should include the `dcid` parameter, such
1892/// that it can be later extracted from the token and passed to the
1893/// [`accept()`] function as its `odcid` parameter.
1894///
1895/// [`accept()`]: fn.accept.html
1896///
1897/// ## Examples:
1898///
1899/// ```no_run
1900/// # let mut config = quiche::Config::new(0xbabababa)?;
1901/// # let mut buf = [0; 512];
1902/// # let mut out = [0; 512];
1903/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1904/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1905/// # let local = socket.local_addr().unwrap();
1906/// # fn mint_token(hdr: &quiche::Header, src: &std::net::SocketAddr) -> Vec<u8> {
1907/// # vec![]
1908/// # }
1909/// # fn validate_token<'a>(src: &std::net::SocketAddr, token: &'a [u8]) -> Option<quiche::ConnectionId<'a>> {
1910/// # None
1911/// # }
1912/// let (len, peer) = socket.recv_from(&mut buf).unwrap();
1913///
1914/// let hdr = quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1915///
1916/// let token = hdr.token.as_ref().unwrap();
1917///
1918/// // No token sent by client, create a new one.
1919/// if token.is_empty() {
1920/// let new_token = mint_token(&hdr, &peer);
1921///
1922/// let len = quiche::retry(
1923/// &hdr.scid, &hdr.dcid, &scid, &new_token, hdr.version, &mut out,
1924/// )?;
1925///
1926/// socket.send_to(&out[..len], &peer).unwrap();
1927/// return Ok(());
1928/// }
1929///
1930/// // Client sent token, validate it.
1931/// let odcid = validate_token(&peer, token);
1932///
1933/// if odcid.is_none() {
1934/// // Invalid address validation token.
1935/// return Ok(());
1936/// }
1937///
1938/// let conn = quiche::accept(&scid, odcid.as_ref(), local, peer, &mut config)?;
1939/// # Ok::<(), quiche::Error>(())
1940/// ```
1941#[inline]
1942pub fn retry(
1943 scid: &ConnectionId, dcid: &ConnectionId, new_scid: &ConnectionId,
1944 token: &[u8], version: u32, out: &mut [u8],
1945) -> Result<usize> {
1946 packet::retry(scid, dcid, new_scid, token, version, out)
1947}
1948
1949/// Returns true if the given protocol version is supported.
1950#[inline]
1951pub fn version_is_supported(version: u32) -> bool {
1952 matches!(version, PROTOCOL_VERSION_V1)
1953}
1954
1955/// Pushes a frame to the output packet if there is enough space.
1956///
1957/// Returns `true` on success, `false` otherwise. In case of failure it means
1958/// there is no room to add the frame in the packet. You may retry to add the
1959/// frame later.
1960macro_rules! push_frame_to_pkt {
1961 ($out:expr, $frames:expr, $frame:expr, $left:expr) => {{
1962 if $frame.wire_len() <= $left {
1963 $left -= $frame.wire_len();
1964
1965 $frame.to_bytes(&mut $out)?;
1966
1967 $frames.push($frame);
1968
1969 true
1970 } else {
1971 false
1972 }
1973 }};
1974}
1975
1976/// Executes the provided body if the qlog feature is enabled, quiche has been
1977/// configured with a log writer, the event's importance is within the
1978/// configured level.
1979macro_rules! qlog_with_type {
1980 ($ty:expr, $qlog:expr, $qlog_streamer_ref:ident, $body:block) => {{
1981 #[cfg(feature = "qlog")]
1982 {
1983 if EventImportance::from($ty).is_contained_in(&$qlog.level) {
1984 if let Some($qlog_streamer_ref) = &mut $qlog.streamer {
1985 $body
1986 }
1987 }
1988 }
1989 }};
1990}
1991
1992#[cfg(feature = "qlog")]
1993const QLOG_PARAMS_SET: EventType =
1994 EventType::QuicEventType(QuicEventType::ParametersSet);
1995
1996#[cfg(feature = "qlog")]
1997const QLOG_PACKET_RX: EventType =
1998 EventType::QuicEventType(QuicEventType::PacketReceived);
1999
2000#[cfg(feature = "qlog")]
2001const QLOG_PACKET_TX: EventType =
2002 EventType::QuicEventType(QuicEventType::PacketSent);
2003
2004#[cfg(feature = "qlog")]
2005const QLOG_DATA_MV: EventType =
2006 EventType::QuicEventType(QuicEventType::StreamDataMoved);
2007
2008#[cfg(feature = "qlog")]
2009const QLOG_METRICS: EventType =
2010 EventType::QuicEventType(QuicEventType::RecoveryMetricsUpdated);
2011
2012#[cfg(feature = "qlog")]
2013const QLOG_CONNECTION_CLOSED: EventType =
2014 EventType::QuicEventType(QuicEventType::ConnectionClosed);
2015
2016#[cfg(feature = "qlog")]
2017struct QlogInfo {
2018 streamer: Option<qlog::streamer::QlogStreamer>,
2019 logged_peer_params: bool,
2020 level: EventImportance,
2021}
2022
2023#[cfg(feature = "qlog")]
2024impl Default for QlogInfo {
2025 fn default() -> Self {
2026 QlogInfo {
2027 streamer: None,
2028 logged_peer_params: false,
2029 level: EventImportance::Base,
2030 }
2031 }
2032}
2033
2034impl<F: BufFactory> Connection<F> {
2035 fn new(
2036 scid: &ConnectionId, retry_cids: Option<RetryConnectionIds>,
2037 client_dcid: Option<&ConnectionId>, local: SocketAddr, peer: SocketAddr,
2038 config: &mut Config, is_server: bool,
2039 ) -> Result<Connection<F>> {
2040 let tls = config.tls_ctx.new_handshake()?;
2041 Connection::with_tls(
2042 scid,
2043 retry_cids,
2044 client_dcid,
2045 local,
2046 peer,
2047 config,
2048 tls,
2049 is_server,
2050 )
2051 }
2052
2053 #[allow(clippy::too_many_arguments)]
2054 fn with_tls(
2055 scid: &ConnectionId, retry_cids: Option<RetryConnectionIds>,
2056 client_dcid: Option<&ConnectionId>, local: SocketAddr, peer: SocketAddr,
2057 config: &Config, tls: tls::Handshake, is_server: bool,
2058 ) -> Result<Connection<F>> {
2059 if retry_cids.is_some() && client_dcid.is_some() {
2060 // These are exclusive, the caller should only specify one or the
2061 // other.
2062 return Err(Error::InvalidDcidInitialization);
2063 }
2064 #[cfg(feature = "custom-client-dcid")]
2065 if let Some(client_dcid) = client_dcid {
2066 // The Minimum length is 8.
2067 // See https://datatracker.ietf.org/doc/html/rfc9000#section-7.2-3
2068 if client_dcid.to_vec().len() < 8 {
2069 return Err(Error::InvalidDcidInitialization);
2070 }
2071 }
2072 #[cfg(not(feature = "custom-client-dcid"))]
2073 if client_dcid.is_some() {
2074 return Err(Error::InvalidDcidInitialization);
2075 }
2076
2077 let max_rx_data = config.local_transport_params.initial_max_data;
2078
2079 let scid_as_hex: Vec<String> =
2080 scid.iter().map(|b| format!("{b:02x}")).collect();
2081
2082 let reset_token = if is_server {
2083 config.local_transport_params.stateless_reset_token
2084 } else {
2085 None
2086 };
2087
2088 let recovery_config = recovery::RecoveryConfig::from_config(config);
2089
2090 let mut path = path::Path::new(
2091 local,
2092 peer,
2093 &recovery_config,
2094 config.path_challenge_recv_max_queue_len,
2095 true,
2096 Some(config),
2097 );
2098
2099 // If we sent a Retry assume the peer's address is verified.
2100 path.verified_peer_address = retry_cids.is_some();
2101 // Assume clients validate the server's address implicitly.
2102 path.peer_verified_local_address = is_server;
2103
2104 // Do not allocate more than the number of active CIDs.
2105 let paths = path::PathMap::new(
2106 path,
2107 config.local_transport_params.active_conn_id_limit as usize,
2108 is_server,
2109 );
2110
2111 let active_path_id = paths.get_active_path_id()?;
2112
2113 let ids = cid::ConnectionIdentifiers::new(
2114 config.local_transport_params.active_conn_id_limit as usize,
2115 scid,
2116 active_path_id,
2117 reset_token,
2118 );
2119
2120 let initial_flow_control_window = max_rx_data;
2121 let mut conn = Connection {
2122 version: config.version,
2123
2124 ids,
2125
2126 trace_id: scid_as_hex.join(""),
2127
2128 pkt_num_spaces: [
2129 packet::PktNumSpace::new(),
2130 packet::PktNumSpace::new(),
2131 packet::PktNumSpace::new(),
2132 ],
2133
2134 crypto_ctx: [
2135 packet::CryptoContext::new(),
2136 packet::CryptoContext::new(),
2137 packet::CryptoContext::new(),
2138 ],
2139
2140 next_pkt_num: 0,
2141
2142 pkt_num_manager: packet::PktNumManager::new(),
2143
2144 peer_transport_params: TransportParams::default(),
2145
2146 peer_transport_params_track_unknown: config
2147 .track_unknown_transport_params,
2148
2149 local_transport_params: config.local_transport_params.clone(),
2150
2151 handshake: tls,
2152
2153 session: None,
2154
2155 recovery_config,
2156
2157 paths,
2158 path_challenge_recv_max_queue_len: config
2159 .path_challenge_recv_max_queue_len,
2160 path_challenge_rx_count: 0,
2161
2162 application_protos: config.application_protos.clone(),
2163
2164 recv_count: 0,
2165 sent_count: 0,
2166 lost_count: 0,
2167 spurious_lost_count: 0,
2168 retrans_count: 0,
2169 dgram_sent_count: 0,
2170 dgram_recv_count: 0,
2171 sent_bytes: 0,
2172 recv_bytes: 0,
2173 acked_bytes: 0,
2174 lost_bytes: 0,
2175
2176 rx_data: 0,
2177 flow_control: flowcontrol::FlowControl::new(
2178 max_rx_data,
2179 initial_flow_control_window,
2180 config.max_connection_window,
2181 ),
2182 should_send_max_data: false,
2183 should_send_max_streams_bidi: false,
2184 should_send_max_streams_uni: false,
2185
2186 tx_cap: 0,
2187 tx_cap_factor: config.tx_cap_factor,
2188
2189 tx_data: 0,
2190 max_tx_data: 0,
2191 last_tx_data: 0,
2192
2193 stream_retrans_bytes: 0,
2194
2195 streams: stream::StreamMap::new(
2196 config.local_transport_params.initial_max_streams_bidi,
2197 config.local_transport_params.initial_max_streams_uni,
2198 config.max_stream_window,
2199 ),
2200
2201 odcid: None,
2202
2203 rscid: None,
2204
2205 token: None,
2206
2207 local_error: None,
2208
2209 peer_error: None,
2210
2211 blocked_limit: None,
2212
2213 idle_timer: None,
2214
2215 draining_timer: None,
2216
2217 undecryptable_pkts: VecDeque::new(),
2218
2219 alpn: Vec::new(),
2220
2221 is_server,
2222
2223 derived_initial_secrets: false,
2224
2225 did_version_negotiation: false,
2226
2227 did_retry: false,
2228
2229 got_peer_conn_id: false,
2230
2231 // Assume clients validate the server's address implicitly.
2232 peer_verified_initial_address: is_server,
2233
2234 parsed_peer_transport_params: false,
2235
2236 handshake_completed: false,
2237
2238 handshake_done_sent: false,
2239 handshake_done_acked: false,
2240
2241 handshake_confirmed: false,
2242
2243 key_phase: false,
2244
2245 ack_eliciting_sent: false,
2246
2247 closed: false,
2248
2249 timed_out: false,
2250
2251 grease: config.grease,
2252
2253 enable_send_streams_blocked: config.enable_send_streams_blocked,
2254
2255 keylog: None,
2256
2257 #[cfg(feature = "qlog")]
2258 qlog: Default::default(),
2259
2260 dgram_recv_queue: dgram::DatagramQueue::new(
2261 config.dgram_recv_max_queue_len,
2262 ),
2263
2264 dgram_send_queue: dgram::DatagramQueue::new(
2265 config.dgram_send_max_queue_len,
2266 ),
2267
2268 emit_dgram: true,
2269
2270 disable_dcid_reuse: config.disable_dcid_reuse,
2271
2272 reset_stream_local_count: 0,
2273 stopped_stream_local_count: 0,
2274 reset_stream_remote_count: 0,
2275 stopped_stream_remote_count: 0,
2276
2277 data_blocked_sent_count: 0,
2278 stream_data_blocked_sent_count: 0,
2279 data_blocked_recv_count: 0,
2280 stream_data_blocked_recv_count: 0,
2281
2282 streams_blocked_bidi_recv_count: 0,
2283 streams_blocked_uni_recv_count: 0,
2284
2285 amplification_limited_count: 0,
2286
2287 streams_blocked_bidi_state: Default::default(),
2288 streams_blocked_uni_state: Default::default(),
2289
2290 max_amplification_factor: config.max_amplification_factor,
2291
2292 max_outstanding_sent_packets: config.max_outstanding_sent_packets,
2293 };
2294 if let Some(retry_cids) = retry_cids {
2295 conn.local_transport_params
2296 .original_destination_connection_id =
2297 Some(retry_cids.original_destination_cid.to_vec().into());
2298
2299 conn.local_transport_params.retry_source_connection_id =
2300 Some(retry_cids.retry_source_cid.to_vec().into());
2301
2302 conn.did_retry = true;
2303 }
2304
2305 conn.local_transport_params.initial_source_connection_id =
2306 Some(conn.ids.get_scid(0)?.cid.to_vec().into());
2307
2308 conn.handshake.init(is_server)?;
2309
2310 conn.handshake
2311 .use_legacy_codepoint(config.version != PROTOCOL_VERSION_V1);
2312
2313 conn.encode_transport_params()?;
2314
2315 if !is_server {
2316 let dcid = if let Some(client_dcid) = client_dcid {
2317 // We already had an dcid generated for us, use it.
2318 client_dcid.to_vec()
2319 } else {
2320 // Derive initial secrets for the client. We can do this here
2321 // because we already generated the random
2322 // destination connection ID.
2323 let mut dcid = [0; 16];
2324 rand::rand_bytes(&mut dcid[..]);
2325 dcid.to_vec()
2326 };
2327
2328 let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2329 &dcid,
2330 conn.version,
2331 conn.is_server,
2332 false,
2333 )?;
2334
2335 let reset_token = conn.peer_transport_params.stateless_reset_token;
2336 conn.set_initial_dcid(
2337 dcid.to_vec().into(),
2338 reset_token,
2339 active_path_id,
2340 )?;
2341
2342 conn.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
2343 conn.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
2344
2345 conn.derived_initial_secrets = true;
2346 }
2347
2348 Ok(conn)
2349 }
2350
2351 /// Sets keylog output to the designated [`Writer`].
2352 ///
2353 /// This needs to be called as soon as the connection is created, to avoid
2354 /// missing some early logs.
2355 ///
2356 /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2357 #[inline]
2358 pub fn set_keylog(&mut self, writer: Box<dyn std::io::Write + Send + Sync>) {
2359 self.keylog = Some(writer);
2360 }
2361
2362 /// Sets qlog output to the designated [`Writer`].
2363 ///
2364 /// Only events included in `QlogLevel::Base` are written. The serialization
2365 /// format is JSON-SEQ.
2366 ///
2367 /// This needs to be called as soon as the connection is created, to avoid
2368 /// missing some early logs.
2369 ///
2370 /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2371 #[cfg(feature = "qlog")]
2372 #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2373 pub fn set_qlog(
2374 &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2375 description: String,
2376 ) {
2377 self.set_qlog_with_level(writer, title, description, QlogLevel::Base)
2378 }
2379
2380 /// Sets qlog output to the designated [`Writer`].
2381 ///
2382 /// Only qlog events included in the specified `QlogLevel` are written. The
2383 /// serialization format is JSON-SEQ.
2384 ///
2385 /// This needs to be called as soon as the connection is created, to avoid
2386 /// missing some early logs.
2387 ///
2388 /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2389 #[cfg(feature = "qlog")]
2390 #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2391 pub fn set_qlog_with_level(
2392 &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2393 description: String, qlog_level: QlogLevel,
2394 ) {
2395 use qlog::events::quic::TransportInitiator;
2396 use qlog::events::HTTP3_URI;
2397 use qlog::events::QUIC_URI;
2398 use qlog::CommonFields;
2399 use qlog::ReferenceTime;
2400
2401 let vp = if self.is_server {
2402 qlog::VantagePointType::Server
2403 } else {
2404 qlog::VantagePointType::Client
2405 };
2406
2407 let level = match qlog_level {
2408 QlogLevel::Core => EventImportance::Core,
2409
2410 QlogLevel::Base => EventImportance::Base,
2411
2412 QlogLevel::Extra => EventImportance::Extra,
2413 };
2414
2415 self.qlog.level = level;
2416
2417 // Best effort to get Instant::now() and SystemTime::now() as closely
2418 // together as possible.
2419 let now = Instant::now();
2420 let now_wall_clock = std::time::SystemTime::now();
2421 let common_fields = CommonFields {
2422 reference_time: ReferenceTime::new_monotonic(Some(now_wall_clock)),
2423 ..Default::default()
2424 };
2425 let trace = qlog::TraceSeq::new(
2426 Some(title.to_string()),
2427 Some(description.to_string()),
2428 Some(common_fields),
2429 Some(qlog::VantagePoint {
2430 name: None,
2431 ty: vp,
2432 flow: None,
2433 }),
2434 vec![QUIC_URI.to_string(), HTTP3_URI.to_string()],
2435 );
2436
2437 let mut streamer = qlog::streamer::QlogStreamer::new(
2438 Some(title),
2439 Some(description),
2440 now,
2441 trace,
2442 self.qlog.level,
2443 qlog::streamer::EventTimePrecision::MicroSeconds,
2444 writer,
2445 );
2446
2447 streamer.start_log().ok();
2448
2449 let ev_data = self
2450 .local_transport_params
2451 .to_qlog(TransportInitiator::Local, self.handshake.cipher());
2452
2453 // This event occurs very early, so just mark the relative time as 0.0.
2454 streamer.add_event(Event::with_time(0.0, ev_data)).ok();
2455
2456 self.qlog.streamer = Some(streamer);
2457 }
2458
2459 /// Returns a mutable reference to the QlogStreamer, if it exists.
2460 #[cfg(feature = "qlog")]
2461 #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2462 pub fn qlog_streamer(&mut self) -> Option<&mut qlog::streamer::QlogStreamer> {
2463 self.qlog.streamer.as_mut()
2464 }
2465
2466 /// Configures the given session for resumption.
2467 ///
2468 /// On the client, this can be used to offer the given serialized session,
2469 /// as returned by [`session()`], for resumption.
2470 ///
2471 /// This must only be called immediately after creating a connection, that
2472 /// is, before any packet is sent or received.
2473 ///
2474 /// [`session()`]: struct.Connection.html#method.session
2475 #[inline]
2476 pub fn set_session(&mut self, session: &[u8]) -> Result<()> {
2477 let mut b = octets::Octets::with_slice(session);
2478
2479 let session_len = b.get_u64()? as usize;
2480 let session_bytes = b.get_bytes(session_len)?;
2481
2482 self.handshake.set_session(session_bytes.as_ref())?;
2483
2484 let raw_params_len = b.get_u64()? as usize;
2485 let raw_params_bytes = b.get_bytes(raw_params_len)?;
2486
2487 let peer_params = TransportParams::decode(
2488 raw_params_bytes.as_ref(),
2489 self.is_server,
2490 self.peer_transport_params_track_unknown,
2491 )?;
2492
2493 self.process_peer_transport_params(peer_params)?;
2494
2495 Ok(())
2496 }
2497
2498 /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
2499 ///
2500 /// This must only be called immediately after creating a connection, that
2501 /// is, before any packet is sent or received.
2502 ///
2503 /// The default value is infinite, that is, no timeout is used unless
2504 /// already configured when creating the connection.
2505 pub fn set_max_idle_timeout(&mut self, v: u64) -> Result<()> {
2506 self.local_transport_params.max_idle_timeout =
2507 cmp::min(v, octets::MAX_VAR_INT);
2508
2509 self.encode_transport_params()
2510 }
2511
2512 /// Sets the congestion control algorithm used.
2513 ///
2514 /// This function can only be called inside one of BoringSSL's handshake
2515 /// callbacks, before any packet has been sent. Calling this function any
2516 /// other time will have no effect.
2517 ///
2518 /// See [`Config::set_cc_algorithm()`].
2519 ///
2520 /// [`Config::set_cc_algorithm()`]: struct.Config.html#method.set_cc_algorithm
2521 #[cfg(feature = "boringssl-boring-crate")]
2522 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2523 pub fn set_cc_algorithm_in_handshake(
2524 ssl: &mut boring::ssl::SslRef, algo: CongestionControlAlgorithm,
2525 ) -> Result<()> {
2526 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2527
2528 ex_data.recovery_config.cc_algorithm = algo;
2529
2530 Ok(())
2531 }
2532
2533 /// Sets custom BBR settings.
2534 ///
2535 /// This API is experimental and will be removed in the future.
2536 ///
2537 /// Currently this only applies if cc_algorithm is
2538 /// `CongestionControlAlgorithm::Bbr2Gcongestion` is set.
2539 ///
2540 /// This function can only be called inside one of BoringSSL's handshake
2541 /// callbacks, before any packet has been sent. Calling this function any
2542 /// other time will have no effect.
2543 ///
2544 /// See [`Config::set_custom_bbr_settings()`].
2545 ///
2546 /// [`Config::set_custom_bbr_settings()`]: struct.Config.html#method.set_custom_bbr_settings
2547 #[cfg(all(feature = "boringssl-boring-crate", feature = "internal"))]
2548 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2549 #[doc(hidden)]
2550 pub fn set_custom_bbr_settings_in_handshake(
2551 ssl: &mut boring::ssl::SslRef, custom_bbr_params: BbrParams,
2552 ) -> Result<()> {
2553 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2554
2555 ex_data.recovery_config.custom_bbr_params = Some(custom_bbr_params);
2556
2557 Ok(())
2558 }
2559
2560 /// Sets the congestion control algorithm used by string.
2561 ///
2562 /// This function can only be called inside one of BoringSSL's handshake
2563 /// callbacks, before any packet has been sent. Calling this function any
2564 /// other time will have no effect.
2565 ///
2566 /// See [`Config::set_cc_algorithm_name()`].
2567 ///
2568 /// [`Config::set_cc_algorithm_name()`]: struct.Config.html#method.set_cc_algorithm_name
2569 #[cfg(feature = "boringssl-boring-crate")]
2570 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2571 pub fn set_cc_algorithm_name_in_handshake(
2572 ssl: &mut boring::ssl::SslRef, name: &str,
2573 ) -> Result<()> {
2574 let cc_algo = CongestionControlAlgorithm::from_str(name)?;
2575 Self::set_cc_algorithm_in_handshake(ssl, cc_algo)
2576 }
2577
2578 /// Sets initial congestion window size in terms of packet count.
2579 ///
2580 /// This function can only be called inside one of BoringSSL's handshake
2581 /// callbacks, before any packet has been sent. Calling this function any
2582 /// other time will have no effect.
2583 ///
2584 /// See [`Config::set_initial_congestion_window_packets()`].
2585 ///
2586 /// [`Config::set_initial_congestion_window_packets()`]: struct.Config.html#method.set_initial_congestion_window_packets
2587 #[cfg(feature = "boringssl-boring-crate")]
2588 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2589 pub fn set_initial_congestion_window_packets_in_handshake(
2590 ssl: &mut boring::ssl::SslRef, packets: usize,
2591 ) -> Result<()> {
2592 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2593
2594 ex_data.recovery_config.initial_congestion_window_packets = packets;
2595
2596 Ok(())
2597 }
2598
2599 /// Configure whether to enable relaxed loss detection on spurious loss.
2600 ///
2601 /// This function can only be called inside one of BoringSSL's handshake
2602 /// callbacks, before any packet has been sent. Calling this function any
2603 /// other time will have no effect.
2604 ///
2605 /// See [`Config::set_enable_relaxed_loss_threshold()`].
2606 ///
2607 /// [`Config::set_enable_relaxed_loss_threshold()`]: struct.Config.html#method.set_enable_relaxed_loss_threshold
2608 #[cfg(feature = "boringssl-boring-crate")]
2609 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2610 pub fn set_enable_relaxed_loss_threshold_in_handshake(
2611 ssl: &mut boring::ssl::SslRef, enable: bool,
2612 ) -> Result<()> {
2613 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2614
2615 ex_data.recovery_config.enable_relaxed_loss_threshold = enable;
2616
2617 Ok(())
2618 }
2619
2620 /// Configure whether to enable the CUBIC idle restart fix.
2621 ///
2622 /// This function can only be called inside one of BoringSSL's handshake
2623 /// callbacks, before any packet has been sent. Calling this function any
2624 /// other time will have no effect.
2625 ///
2626 /// See [`Config::set_enable_cubic_idle_restart_fix()`].
2627 ///
2628 /// [`Config::set_enable_cubic_idle_restart_fix()`]: struct.Config.html#method.set_enable_cubic_idle_restart_fix
2629 #[cfg(feature = "boringssl-boring-crate")]
2630 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2631 pub fn set_enable_cubic_idle_restart_fix_in_handshake(
2632 ssl: &mut boring::ssl::SslRef, enable: bool,
2633 ) -> Result<()> {
2634 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2635
2636 ex_data.recovery_config.enable_cubic_idle_restart_fix = enable;
2637
2638 Ok(())
2639 }
2640
2641 /// Configures whether to enable HyStart++.
2642 ///
2643 /// This function can only be called inside one of BoringSSL's handshake
2644 /// callbacks, before any packet has been sent. Calling this function any
2645 /// other time will have no effect.
2646 ///
2647 /// See [`Config::enable_hystart()`].
2648 ///
2649 /// [`Config::enable_hystart()`]: struct.Config.html#method.enable_hystart
2650 #[cfg(feature = "boringssl-boring-crate")]
2651 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2652 pub fn set_hystart_in_handshake(
2653 ssl: &mut boring::ssl::SslRef, v: bool,
2654 ) -> Result<()> {
2655 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2656
2657 ex_data.recovery_config.hystart = v;
2658
2659 Ok(())
2660 }
2661
2662 /// Configures whether to enable pacing.
2663 ///
2664 /// This function can only be called inside one of BoringSSL's handshake
2665 /// callbacks, before any packet has been sent. Calling this function any
2666 /// other time will have no effect.
2667 ///
2668 /// See [`Config::enable_pacing()`].
2669 ///
2670 /// [`Config::enable_pacing()`]: struct.Config.html#method.enable_pacing
2671 #[cfg(feature = "boringssl-boring-crate")]
2672 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2673 pub fn set_pacing_in_handshake(
2674 ssl: &mut boring::ssl::SslRef, v: bool,
2675 ) -> Result<()> {
2676 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2677
2678 ex_data.recovery_config.pacing = v;
2679
2680 Ok(())
2681 }
2682
2683 /// Sets the max value for pacing rate.
2684 ///
2685 /// This function can only be called inside one of BoringSSL's handshake
2686 /// callbacks, before any packet has been sent. Calling this function any
2687 /// other time will have no effect.
2688 ///
2689 /// See [`Config::set_max_pacing_rate()`].
2690 ///
2691 /// [`Config::set_max_pacing_rate()`]: struct.Config.html#method.set_max_pacing_rate
2692 #[cfg(feature = "boringssl-boring-crate")]
2693 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2694 pub fn set_max_pacing_rate_in_handshake(
2695 ssl: &mut boring::ssl::SslRef, v: Option<u64>,
2696 ) -> Result<()> {
2697 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2698
2699 ex_data.recovery_config.max_pacing_rate = v;
2700
2701 Ok(())
2702 }
2703
2704 /// Sets the maximum outgoing UDP payload size.
2705 ///
2706 /// This function can only be called inside one of BoringSSL's handshake
2707 /// callbacks, before any packet has been sent. Calling this function any
2708 /// other time will have no effect.
2709 ///
2710 /// See [`Config::set_max_send_udp_payload_size()`].
2711 ///
2712 /// [`Config::set_max_send_udp_payload_size()`]: struct.Config.html#method.set_max_send_udp_payload_size
2713 #[cfg(feature = "boringssl-boring-crate")]
2714 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2715 pub fn set_max_send_udp_payload_size_in_handshake(
2716 ssl: &mut boring::ssl::SslRef, v: usize,
2717 ) -> Result<()> {
2718 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2719
2720 ex_data.recovery_config.max_send_udp_payload_size = v;
2721
2722 Ok(())
2723 }
2724
2725 /// Sets the send capacity factor.
2726 ///
2727 /// This function can only be called inside one of BoringSSL's handshake
2728 /// callbacks, before any packet has been sent. Calling this function any
2729 /// other time will have no effect.
2730 ///
2731 /// See [`Config::set_send_capacity_factor()`].
2732 ///
2733 /// [`Config::set_max_send_udp_payload_size()`]: struct.Config.html#method.set_send_capacity_factor
2734 #[cfg(feature = "boringssl-boring-crate")]
2735 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2736 pub fn set_send_capacity_factor_in_handshake(
2737 ssl: &mut boring::ssl::SslRef, v: f64,
2738 ) -> Result<()> {
2739 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2740
2741 ex_data.tx_cap_factor = v;
2742
2743 Ok(())
2744 }
2745
2746 /// Configures whether to do path MTU discovery.
2747 ///
2748 /// This function can only be called inside one of BoringSSL's handshake
2749 /// callbacks, before any packet has been sent. Calling this function any
2750 /// other time will have no effect.
2751 ///
2752 /// See [`Config::discover_pmtu()`].
2753 ///
2754 /// [`Config::discover_pmtu()`]: struct.Config.html#method.discover_pmtu
2755 #[cfg(feature = "boringssl-boring-crate")]
2756 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2757 pub fn set_discover_pmtu_in_handshake(
2758 ssl: &mut boring::ssl::SslRef, discover: bool, max_probes: u8,
2759 ) -> Result<()> {
2760 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2761
2762 ex_data.pmtud = Some((discover, max_probes));
2763
2764 Ok(())
2765 }
2766
2767 /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
2768 ///
2769 /// This function can only be called inside one of BoringSSL's handshake
2770 /// callbacks, before any packet has been sent. Calling this function any
2771 /// other time will have no effect.
2772 ///
2773 /// See [`Config::set_max_idle_timeout()`].
2774 ///
2775 /// [`Config::set_max_idle_timeout()`]: struct.Config.html#method.set_max_idle_timeout
2776 #[cfg(feature = "boringssl-boring-crate")]
2777 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2778 pub fn set_max_idle_timeout_in_handshake(
2779 ssl: &mut boring::ssl::SslRef, v: u64,
2780 ) -> Result<()> {
2781 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2782
2783 ex_data.local_transport_params.max_idle_timeout = v;
2784
2785 Self::set_transport_parameters_in_hanshake(
2786 ex_data.local_transport_params.clone(),
2787 ex_data.is_server,
2788 ssl,
2789 )
2790 }
2791
2792 /// Sets the number of sent packets that can be tracked for loss detection
2793 /// before the connection is closed.
2794 ///
2795 /// This function can only be called inside one of BoringSSL's handshake
2796 /// callbacks, before any packet has been sent. Calling this function any
2797 /// other time will have no effect.
2798 ///
2799 /// See [`Config::set_max_outstanding_sent_packets()`].
2800 #[cfg(feature = "boringssl-boring-crate")]
2801 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2802 pub fn set_max_outstanding_sent_packets_in_handshake(
2803 ssl: &mut boring::ssl::SslRef, max_packets: Option<NonZeroUsize>,
2804 ) -> Result<()> {
2805 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2806
2807 ex_data.max_outstanding_sent_packets = max_packets;
2808
2809 Ok(())
2810 }
2811
2812 /// Sets the `initial_max_streams_bidi` transport parameter.
2813 ///
2814 /// This function can only be called inside one of BoringSSL's handshake
2815 /// callbacks, before any packet has been sent. Calling this function any
2816 /// other time will have no effect.
2817 ///
2818 /// See [`Config::set_initial_max_streams_bidi()`].
2819 ///
2820 /// [`Config::set_initial_max_streams_bidi()`]: struct.Config.html#method.set_initial_max_streams_bidi
2821 #[cfg(feature = "boringssl-boring-crate")]
2822 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2823 pub fn set_initial_max_streams_bidi_in_handshake(
2824 ssl: &mut boring::ssl::SslRef, v: u64,
2825 ) -> Result<()> {
2826 let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2827
2828 ex_data.local_transport_params.initial_max_streams_bidi = v;
2829
2830 Self::set_transport_parameters_in_hanshake(
2831 ex_data.local_transport_params.clone(),
2832 ex_data.is_server,
2833 ssl,
2834 )
2835 }
2836
2837 #[cfg(feature = "boringssl-boring-crate")]
2838 fn set_transport_parameters_in_hanshake(
2839 params: TransportParams, is_server: bool, ssl: &mut boring::ssl::SslRef,
2840 ) -> Result<()> {
2841 use foreign_types_shared::ForeignTypeRef;
2842 use std::mem::ManuallyDrop;
2843
2844 // In order to apply the new parameter to the TLS state before TPs are
2845 // written into a TLS message, we need to re-encode all TPs immediately.
2846 //
2847 // Since we don't have direct access to the main `Connection` object, we
2848 // need to re-create the `Handshake` state from the `SslRef`.
2849 //
2850 // Wrap the temporary `Handshake` in `ManuallyDrop` because this is only
2851 // a borrowed view of `ssl`. The caller retains ownership of the
2852 // underlying BoringSSL object.
2853 let mut handshake = ManuallyDrop::new(unsafe {
2854 tls::Handshake::from_ptr(ssl.as_ptr() as _)?
2855 });
2856
2857 handshake.set_quic_transport_params(¶ms, is_server)
2858 }
2859
2860 /// Sets the `use_initial_max_data_as_flow_control_win` flag during SSL
2861 /// handshake.
2862 ///
2863 /// This is now always enabled and this method is a no-op. It will be
2864 /// removed in a future release.
2865 #[cfg(feature = "boringssl-boring-crate")]
2866 #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2867 #[deprecated(note = "This is now always enabled. This method is a no-op.")]
2868 pub fn set_use_initial_max_data_as_flow_control_win_in_handshake(
2869 _ssl: &mut boring::ssl::SslRef,
2870 ) -> Result<()> {
2871 Ok(())
2872 }
2873
2874 /// Processes QUIC packets received from the peer.
2875 ///
2876 /// On success the number of bytes processed from the input buffer is
2877 /// returned. On error the connection will be closed by calling [`close()`]
2878 /// with the appropriate error code.
2879 ///
2880 /// Coalesced packets will be processed as necessary.
2881 ///
2882 /// Note that the contents of the input buffer `buf` might be modified by
2883 /// this function due to, for example, in-place decryption.
2884 ///
2885 /// [`close()`]: struct.Connection.html#method.close
2886 ///
2887 /// ## Examples:
2888 ///
2889 /// ```no_run
2890 /// # let mut buf = [0; 512];
2891 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
2892 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
2893 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
2894 /// # let peer = "127.0.0.1:1234".parse().unwrap();
2895 /// # let local = socket.local_addr().unwrap();
2896 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
2897 /// loop {
2898 /// let (read, from) = socket.recv_from(&mut buf).unwrap();
2899 ///
2900 /// let recv_info = quiche::RecvInfo {
2901 /// from,
2902 /// to: local,
2903 /// };
2904 ///
2905 /// let read = match conn.recv(&mut buf[..read], recv_info) {
2906 /// Ok(v) => v,
2907 ///
2908 /// Err(e) => {
2909 /// // An error occurred, handle it.
2910 /// break;
2911 /// },
2912 /// };
2913 /// }
2914 /// # Ok::<(), quiche::Error>(())
2915 /// ```
2916 pub fn recv(&mut self, buf: &mut [u8], info: RecvInfo) -> Result<usize> {
2917 let len = buf.len();
2918
2919 if len == 0 {
2920 return Err(Error::BufferTooShort);
2921 }
2922
2923 let recv_pid = self.paths.path_id_from_addrs(&(info.to, info.from));
2924
2925 if let Some(recv_pid) = recv_pid {
2926 let recv_path = self.paths.get_mut(recv_pid)?;
2927
2928 // Keep track of how many bytes we received from the client, so we
2929 // can limit bytes sent back before address validation, to a
2930 // multiple of this. The limit needs to be increased early on, so
2931 // that if there is an error there is enough credit to send a
2932 // CONNECTION_CLOSE.
2933 //
2934 // It doesn't matter if the packets received were valid or not, we
2935 // only need to track the total amount of bytes received.
2936 //
2937 // Note that we also need to limit the number of bytes we sent on a
2938 // path if we are not the host that initiated its usage.
2939 if self.is_server && !recv_path.verified_peer_address {
2940 recv_path.max_send_bytes += len * self.max_amplification_factor;
2941 }
2942 } else if !self.is_server {
2943 // If a client receives packets from an unknown server address,
2944 // the client MUST discard these packets.
2945 trace!(
2946 "{} client received packet from unknown address {:?}, dropping",
2947 self.trace_id,
2948 info,
2949 );
2950
2951 return Ok(len);
2952 }
2953
2954 let mut done = 0;
2955 let mut left = len;
2956
2957 // Process coalesced packets.
2958 while left > 0 {
2959 let read = match self.recv_single(
2960 &mut buf[len - left..len],
2961 &info,
2962 recv_pid,
2963 ) {
2964 Ok(v) => v,
2965
2966 Err(Error::Done) => {
2967 // If the packet can't be processed or decrypted, check if
2968 // it's a stateless reset.
2969 if self.is_stateless_reset(&buf[len - left..len]) {
2970 trace!("{} packet is a stateless reset", self.trace_id);
2971
2972 self.mark_closed();
2973 }
2974
2975 left
2976 },
2977
2978 Err(e) => {
2979 // In case of error processing the incoming packet, close
2980 // the connection.
2981 self.close(false, e.to_wire(), b"").ok();
2982 return Err(e);
2983 },
2984 };
2985
2986 done += read;
2987 left -= read;
2988 }
2989
2990 // Even though the packet was previously "accepted", it
2991 // should be safe to forward the error, as it also comes
2992 // from the `recv()` method.
2993 self.process_undecrypted_0rtt_packets()?;
2994
2995 Ok(done)
2996 }
2997
2998 fn process_undecrypted_0rtt_packets(&mut self) -> Result<()> {
2999 // Process previously undecryptable 0-RTT packets if the decryption key
3000 // is now available.
3001 if self.crypto_ctx[packet::Epoch::Application]
3002 .crypto_0rtt_open
3003 .is_some()
3004 {
3005 while let Some((mut pkt, info)) = self.undecryptable_pkts.pop_front()
3006 {
3007 if let Err(e) = self.recv(&mut pkt, info) {
3008 self.undecryptable_pkts.clear();
3009
3010 return Err(e);
3011 }
3012 }
3013 }
3014 Ok(())
3015 }
3016
3017 /// Returns true if a QUIC packet is a stateless reset.
3018 fn is_stateless_reset(&self, buf: &[u8]) -> bool {
3019 // If the packet is too small, then we just throw it away.
3020 let buf_len = buf.len();
3021 if buf_len < 21 {
3022 return false;
3023 }
3024
3025 // TODO: we should iterate over all active destination connection IDs
3026 // and check against their reset token.
3027 match self.peer_transport_params.stateless_reset_token {
3028 Some(token) => {
3029 let token_len = 16;
3030
3031 crypto::verify_slices_are_equal(
3032 &token.to_be_bytes(),
3033 &buf[buf_len - token_len..buf_len],
3034 )
3035 .is_ok()
3036 },
3037
3038 None => false,
3039 }
3040 }
3041
3042 /// Processes a single QUIC packet received from the peer.
3043 ///
3044 /// On success the number of bytes processed from the input buffer is
3045 /// returned. When the [`Done`] error is returned, processing of the
3046 /// remainder of the incoming UDP datagram should be interrupted.
3047 ///
3048 /// Note that a server might observe a new 4-tuple, preventing to
3049 /// know in advance to which path the incoming packet belongs to (`recv_pid`
3050 /// is `None`). As a client, packets from unknown 4-tuple are dropped
3051 /// beforehand (see `recv()`).
3052 ///
3053 /// On error, an error other than [`Done`] is returned.
3054 ///
3055 /// [`Done`]: enum.Error.html#variant.Done
3056 fn recv_single(
3057 &mut self, buf: &mut [u8], info: &RecvInfo, recv_pid: Option<usize>,
3058 ) -> Result<usize> {
3059 let now = Instant::now();
3060
3061 if buf.is_empty() {
3062 return Err(Error::Done);
3063 }
3064
3065 if self.is_closed() || self.is_draining() {
3066 return Err(Error::Done);
3067 }
3068
3069 let is_closing = self.local_error.is_some();
3070
3071 if is_closing {
3072 return Err(Error::Done);
3073 }
3074
3075 let buf_len = buf.len();
3076
3077 let mut b = octets::OctetsMut::with_slice(buf);
3078
3079 let mut hdr = Header::from_bytes(&mut b, self.source_id().len())
3080 .map_err(|e| {
3081 drop_pkt_on_err(
3082 e,
3083 self.recv_count,
3084 self.is_server,
3085 &self.trace_id,
3086 )
3087 })?;
3088
3089 if hdr.ty == Type::VersionNegotiation {
3090 // Version negotiation packets can only be sent by the server.
3091 if self.is_server {
3092 return Err(Error::Done);
3093 }
3094
3095 // Ignore duplicate version negotiation.
3096 if self.did_version_negotiation {
3097 return Err(Error::Done);
3098 }
3099
3100 // Ignore version negotiation if any other packet has already been
3101 // successfully processed.
3102 if self.recv_count > 0 {
3103 return Err(Error::Done);
3104 }
3105
3106 if hdr.dcid != self.source_id() {
3107 return Err(Error::Done);
3108 }
3109
3110 if hdr.scid != self.destination_id() {
3111 return Err(Error::Done);
3112 }
3113
3114 trace!("{} rx pkt {:?}", self.trace_id, hdr);
3115
3116 let versions = hdr.versions.ok_or(Error::Done)?;
3117
3118 // Ignore version negotiation if the version already selected is
3119 // listed.
3120 if versions.contains(&self.version) {
3121 return Err(Error::Done);
3122 }
3123
3124 let supported_versions =
3125 versions.iter().filter(|&&v| version_is_supported(v));
3126
3127 let mut found_version = false;
3128
3129 for &v in supported_versions {
3130 found_version = true;
3131
3132 // The final version takes precedence over draft ones.
3133 if v == PROTOCOL_VERSION_V1 {
3134 self.version = v;
3135 break;
3136 }
3137
3138 self.version = cmp::max(self.version, v);
3139 }
3140
3141 if !found_version {
3142 // We don't support any of the versions offered.
3143 //
3144 // While a man-in-the-middle attacker might be able to
3145 // inject a version negotiation packet that triggers this
3146 // failure, the window of opportunity is very small and
3147 // this error is quite useful for debugging, so don't just
3148 // ignore the packet.
3149 return Err(Error::UnknownVersion);
3150 }
3151
3152 self.did_version_negotiation = true;
3153
3154 // Derive Initial secrets based on the new version.
3155 let (aead_open, aead_seal) = crypto::derive_initial_key_material(
3156 &self.destination_id(),
3157 self.version,
3158 self.is_server,
3159 true,
3160 )?;
3161
3162 // Reset connection state to force sending another Initial packet.
3163 self.drop_epoch_state(packet::Epoch::Initial, now);
3164 self.got_peer_conn_id = false;
3165 self.handshake.clear()?;
3166
3167 self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
3168 self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
3169
3170 self.handshake
3171 .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
3172
3173 // Encode transport parameters again, as the new version might be
3174 // using a different format.
3175 self.encode_transport_params()?;
3176
3177 return Err(Error::Done);
3178 }
3179
3180 if hdr.ty == Type::Retry {
3181 // Retry packets can only be sent by the server.
3182 if self.is_server {
3183 return Err(Error::Done);
3184 }
3185
3186 // Ignore duplicate retry.
3187 if self.did_retry {
3188 return Err(Error::Done);
3189 }
3190
3191 // Check if Retry packet is valid.
3192 if packet::verify_retry_integrity(
3193 &b,
3194 &self.destination_id(),
3195 self.version,
3196 )
3197 .is_err()
3198 {
3199 return Err(Error::Done);
3200 }
3201
3202 trace!("{} rx pkt {:?}", self.trace_id, hdr);
3203
3204 self.token = hdr.token;
3205 self.did_retry = true;
3206
3207 // Remember peer's new connection ID.
3208 self.odcid = Some(self.destination_id().into_owned());
3209
3210 self.set_initial_dcid(
3211 hdr.scid.clone(),
3212 None,
3213 self.paths.get_active_path_id()?,
3214 )?;
3215
3216 self.rscid = Some(self.destination_id().into_owned());
3217
3218 // Derive Initial secrets using the new connection ID.
3219 let (aead_open, aead_seal) = crypto::derive_initial_key_material(
3220 &hdr.scid,
3221 self.version,
3222 self.is_server,
3223 true,
3224 )?;
3225
3226 // Reset connection state to force sending another Initial packet.
3227 self.drop_epoch_state(packet::Epoch::Initial, now);
3228 self.got_peer_conn_id = false;
3229 self.handshake.clear()?;
3230
3231 self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
3232 self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
3233
3234 return Err(Error::Done);
3235 }
3236
3237 if self.is_server && !self.did_version_negotiation {
3238 if !version_is_supported(hdr.version) {
3239 return Err(Error::UnknownVersion);
3240 }
3241
3242 self.version = hdr.version;
3243 self.did_version_negotiation = true;
3244
3245 self.handshake
3246 .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
3247
3248 // Encode transport parameters again, as the new version might be
3249 // using a different format.
3250 self.encode_transport_params()?;
3251 }
3252
3253 if hdr.ty != Type::Short && hdr.version != self.version {
3254 // At this point version negotiation was already performed, so
3255 // ignore packets that don't match the connection's version.
3256 return Err(Error::Done);
3257 }
3258
3259 // Long header packets have an explicit payload length, but short
3260 // packets don't so just use the remaining capacity in the buffer.
3261 let payload_len = if hdr.ty == Type::Short {
3262 b.cap()
3263 } else {
3264 b.get_varint().map_err(|e| {
3265 drop_pkt_on_err(
3266 e.into(),
3267 self.recv_count,
3268 self.is_server,
3269 &self.trace_id,
3270 )
3271 })? as usize
3272 };
3273
3274 // Make sure the buffer is same or larger than an explicit
3275 // payload length.
3276 if payload_len > b.cap() {
3277 return Err(drop_pkt_on_err(
3278 Error::InvalidPacket,
3279 self.recv_count,
3280 self.is_server,
3281 &self.trace_id,
3282 ));
3283 }
3284
3285 // Derive initial secrets on the server.
3286 if !self.derived_initial_secrets {
3287 let (aead_open, aead_seal) = crypto::derive_initial_key_material(
3288 &hdr.dcid,
3289 self.version,
3290 self.is_server,
3291 false,
3292 )?;
3293
3294 self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
3295 self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
3296
3297 self.derived_initial_secrets = true;
3298 }
3299
3300 // Select packet number space epoch based on the received packet's type.
3301 let epoch = hdr.ty.to_epoch()?;
3302
3303 // Select AEAD context used to open incoming packet.
3304 let aead = if hdr.ty == Type::ZeroRTT {
3305 // Only use 0-RTT key if incoming packet is 0-RTT.
3306 self.crypto_ctx[epoch].crypto_0rtt_open.as_ref()
3307 } else {
3308 // Otherwise use the packet number space's main key.
3309 self.crypto_ctx[epoch].crypto_open.as_ref()
3310 };
3311
3312 // Finally, discard packet if no usable key is available.
3313 let mut aead = match aead {
3314 Some(v) => v,
3315
3316 None => {
3317 if hdr.ty == Type::ZeroRTT &&
3318 self.undecryptable_pkts.len() < MAX_UNDECRYPTABLE_PACKETS &&
3319 !self.is_established()
3320 {
3321 // Buffer 0-RTT packets when the required read key is not
3322 // available yet, and process them later.
3323 //
3324 // TODO: in the future we might want to buffer other types
3325 // of undecryptable packets as well.
3326 let pkt_len = b.off() + payload_len;
3327 let pkt = (b.buf()[..pkt_len]).to_vec();
3328
3329 self.undecryptable_pkts.push_back((pkt, *info));
3330 return Ok(pkt_len);
3331 }
3332
3333 let e = drop_pkt_on_err(
3334 Error::CryptoFail,
3335 self.recv_count,
3336 self.is_server,
3337 &self.trace_id,
3338 );
3339
3340 return Err(e);
3341 },
3342 };
3343
3344 let aead_tag_len = aead.alg().tag_len();
3345
3346 packet::decrypt_hdr(&mut b, &mut hdr, aead).map_err(|e| {
3347 drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
3348 })?;
3349
3350 let pn = packet::decode_pkt_num(
3351 self.pkt_num_spaces[epoch].largest_rx_pkt_num,
3352 hdr.pkt_num,
3353 hdr.pkt_num_len,
3354 );
3355
3356 let pn_len = hdr.pkt_num_len;
3357
3358 trace!(
3359 "{} rx pkt {:?} len={} pn={} {}",
3360 self.trace_id,
3361 hdr,
3362 payload_len,
3363 pn,
3364 AddrTupleFmt(info.from, info.to)
3365 );
3366
3367 #[cfg(feature = "qlog")]
3368 let mut qlog_frames = vec![];
3369
3370 // Check for key update.
3371 let mut aead_next = None;
3372
3373 if self.handshake_confirmed &&
3374 hdr.ty != Type::ZeroRTT &&
3375 hdr.key_phase != self.key_phase
3376 {
3377 // Check if this packet arrived before key update.
3378 if let Some(key_update) = self.crypto_ctx[epoch]
3379 .key_update
3380 .as_ref()
3381 .and_then(|key_update| {
3382 (pn < key_update.pn_on_update).then_some(key_update)
3383 })
3384 {
3385 aead = &key_update.crypto_open;
3386 } else {
3387 trace!("{} peer-initiated key update", self.trace_id);
3388
3389 aead_next = Some((
3390 self.crypto_ctx[epoch]
3391 .crypto_open
3392 .as_ref()
3393 .unwrap()
3394 .derive_next_packet_key()?,
3395 self.crypto_ctx[epoch]
3396 .crypto_seal
3397 .as_ref()
3398 .unwrap()
3399 .derive_next_packet_key()?,
3400 ));
3401
3402 // `aead_next` is always `Some` at this point, so the
3403 // `unwrap()` will never fail.
3404 aead = &aead_next.as_ref().unwrap().0;
3405 }
3406 }
3407
3408 let mut payload = packet::decrypt_pkt(
3409 &mut b,
3410 pn,
3411 pn_len,
3412 payload_len,
3413 aead,
3414 )
3415 .map_err(|e| {
3416 drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
3417 })?;
3418
3419 if self.pkt_num_spaces[epoch].recv_pkt_num.contains(pn) {
3420 trace!("{} ignored duplicate packet {}", self.trace_id, pn);
3421 return Err(Error::Done);
3422 }
3423
3424 // Packets with no frames are invalid.
3425 if payload.cap() == 0 {
3426 return Err(Error::InvalidPacket);
3427 }
3428
3429 // Now that we decrypted the packet, let's see if we can map it to an
3430 // existing path.
3431 let recv_pid = if hdr.ty == Type::Short && self.got_peer_conn_id {
3432 let pkt_dcid = ConnectionId::from_ref(&hdr.dcid);
3433 self.get_or_create_recv_path_id(recv_pid, &pkt_dcid, buf_len, info)?
3434 } else {
3435 // During handshake, we are on the initial path.
3436 self.paths.get_active_path_id()?
3437 };
3438
3439 // The key update is verified once a packet is successfully decrypted
3440 // using the new keys.
3441 if let Some((open_next, seal_next)) = aead_next {
3442 if !self.crypto_ctx[epoch]
3443 .key_update
3444 .as_ref()
3445 .is_none_or(|prev| prev.update_acked)
3446 {
3447 // Peer has updated keys twice without awaiting confirmation.
3448 return Err(Error::KeyUpdate);
3449 }
3450
3451 trace!("{} key update verified", self.trace_id);
3452
3453 let _ = self.crypto_ctx[epoch].crypto_seal.replace(seal_next);
3454
3455 let open_prev = self.crypto_ctx[epoch]
3456 .crypto_open
3457 .replace(open_next)
3458 .unwrap();
3459
3460 let recv_path = self.paths.get_mut(recv_pid)?;
3461
3462 self.crypto_ctx[epoch].key_update = Some(packet::KeyUpdate {
3463 crypto_open: open_prev,
3464 pn_on_update: pn,
3465 update_acked: false,
3466 timer: now + (recv_path.recovery.pto() * 3),
3467 });
3468
3469 self.key_phase = !self.key_phase;
3470
3471 qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
3472 let trigger = Some(
3473 qlog::events::quic::KeyUpdateOrRetiredTrigger::RemoteUpdate,
3474 );
3475
3476 let ev_data_client =
3477 EventData::QuicKeyUpdated(qlog::events::quic::KeyUpdated {
3478 key_type: qlog::events::quic::KeyType::Client1RttSecret,
3479 trigger: trigger.clone(),
3480 ..Default::default()
3481 });
3482
3483 q.add_event_data_with_instant(ev_data_client, now).ok();
3484
3485 let ev_data_server =
3486 EventData::QuicKeyUpdated(qlog::events::quic::KeyUpdated {
3487 key_type: qlog::events::quic::KeyType::Server1RttSecret,
3488 trigger,
3489 ..Default::default()
3490 });
3491
3492 q.add_event_data_with_instant(ev_data_server, now).ok();
3493 });
3494 }
3495
3496 if !self.is_server && !self.got_peer_conn_id {
3497 if self.odcid.is_none() {
3498 self.odcid = Some(self.destination_id().into_owned());
3499 }
3500
3501 // Replace the randomly generated destination connection ID with
3502 // the one supplied by the server.
3503 self.set_initial_dcid(
3504 hdr.scid.clone(),
3505 self.peer_transport_params.stateless_reset_token,
3506 recv_pid,
3507 )?;
3508
3509 self.got_peer_conn_id = true;
3510 }
3511
3512 if self.is_server && !self.got_peer_conn_id {
3513 self.set_initial_dcid(hdr.scid.clone(), None, recv_pid)?;
3514
3515 if !self.did_retry {
3516 self.local_transport_params
3517 .original_destination_connection_id =
3518 Some(hdr.dcid.to_vec().into());
3519
3520 self.encode_transport_params()?;
3521 }
3522
3523 self.got_peer_conn_id = true;
3524 }
3525
3526 // To avoid sending an ACK in response to an ACK-only packet, we need
3527 // to keep track of whether this packet contains any frame other than
3528 // ACK and PADDING.
3529 let mut ack_elicited = false;
3530
3531 // Process packet payload. If a frame cannot be processed, store the
3532 // error and stop further packet processing.
3533 let mut frame_processing_err = None;
3534
3535 // To know if the peer migrated the connection, we need to keep track
3536 // whether this is a non-probing packet.
3537 let mut probing = true;
3538
3539 // Process packet payload.
3540 while payload.cap() > 0 {
3541 let frame = frame::Frame::from_bytes(&mut payload, hdr.ty)?;
3542
3543 qlog_with_type!(QLOG_PACKET_RX, self.qlog, _q, {
3544 qlog_frames.push(frame.to_qlog());
3545 });
3546
3547 if frame.ack_eliciting() {
3548 ack_elicited = true;
3549 }
3550
3551 if !frame.probing() {
3552 probing = false;
3553 }
3554
3555 if let Err(e) = self.process_frame(frame, &hdr, recv_pid, epoch, now)
3556 {
3557 frame_processing_err = Some(e);
3558 break;
3559 }
3560 }
3561
3562 qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
3563 let packet_size = b.len();
3564
3565 let qlog_pkt_hdr = qlog::events::quic::PacketHeader::with_type(
3566 hdr.ty.to_qlog(),
3567 Some(pn),
3568 Some(hdr.version),
3569 Some(&hdr.scid),
3570 Some(&hdr.dcid),
3571 );
3572
3573 let qlog_raw_info = RawInfo {
3574 length: Some(packet_size as u64),
3575 payload_length: Some(payload_len as u64),
3576 data: None,
3577 };
3578
3579 let ev_data = EventData::QuicPacketReceived(
3580 qlog::events::quic::PacketReceived {
3581 header: qlog_pkt_hdr,
3582 frames: Some(qlog_frames),
3583 raw: Some(qlog_raw_info),
3584 ..Default::default()
3585 },
3586 );
3587
3588 q.add_event_data_with_instant(ev_data, now).ok();
3589 });
3590
3591 qlog_with_type!(QLOG_METRICS, self.qlog, q, {
3592 let recv_path = self.paths.get_mut(recv_pid)?;
3593 recv_path.recovery.maybe_qlog(q, now);
3594 });
3595
3596 if let Some(e) = frame_processing_err {
3597 // Any frame error is terminal, so now just return.
3598 return Err(e);
3599 }
3600
3601 // Only log the remote transport parameters once the connection is
3602 // established (i.e. after frames have been fully parsed) and only
3603 // once per connection.
3604 if self.is_established() {
3605 qlog_with_type!(QLOG_PARAMS_SET, self.qlog, q, {
3606 if !self.qlog.logged_peer_params {
3607 let ev_data = self.peer_transport_params.to_qlog(
3608 TransportInitiator::Remote,
3609 self.handshake.cipher(),
3610 );
3611
3612 q.add_event_data_with_instant(ev_data, now).ok();
3613
3614 self.qlog.logged_peer_params = true;
3615 }
3616 });
3617 }
3618
3619 // Process acked frames. Note that several packets from several paths
3620 // might have been acked by the received packet.
3621 let (paths, path_events) = self.paths.iter_mut_and_events();
3622 for (_, p) in paths {
3623 while let Some(acked) = p.recovery.next_acked_frame(epoch) {
3624 match acked {
3625 frame::Frame::Ping {
3626 mtu_probe: Some(mtu_probe),
3627 } => {
3628 trace!(
3629 "{} pmtud probe acked; probe size {:?}",
3630 self.trace_id,
3631 mtu_probe
3632 );
3633
3634 let local = p.local_addr();
3635 let peer = p.peer_addr();
3636 if let Some(pmtud) = p.pmtud.as_mut() {
3637 let old_pmtu = pmtud.get_current_mtu();
3638 let current_mtu = pmtud.successful_probe(mtu_probe);
3639 let new_pmtu = pmtud.get_current_mtu();
3640
3641 // Update the datagram size only after validating
3642 // the MTU.
3643 if let Some(current_mtu) = current_mtu {
3644 qlog_with_type!(
3645 EventType::QuicEventType(
3646 QuicEventType::MtuUpdated
3647 ),
3648 self.qlog,
3649 q,
3650 {
3651 let pmtu_data = EventData::QuicMtuUpdated(
3652 qlog::events::quic::MtuUpdated {
3653 old: Some(
3654 p.recovery.max_datagram_size()
3655 as u32,
3656 ),
3657 new: current_mtu as u32,
3658 done: Some(true),
3659 },
3660 );
3661
3662 q.add_event_data_with_instant(
3663 pmtu_data, now,
3664 )
3665 .ok();
3666 }
3667 );
3668
3669 p.recovery
3670 .pmtud_update_max_datagram_size(current_mtu);
3671 }
3672
3673 if let Some(event) =
3674 path::pmtu_event(local, peer, old_pmtu, new_pmtu)
3675 {
3676 path_events.push_back(event);
3677 }
3678 }
3679 },
3680
3681 frame::Frame::ACK { ranges, .. } => {
3682 // Stop acknowledging packets less than or equal to the
3683 // largest acknowledged in the sent ACK frame that, in
3684 // turn, got acked.
3685 if let Some(largest_acked) = ranges.last() {
3686 self.pkt_num_spaces[epoch]
3687 .recv_pkt_need_ack
3688 .remove_until(largest_acked);
3689 }
3690 },
3691
3692 frame::Frame::CryptoHeader { offset, length } => {
3693 self.crypto_ctx[epoch]
3694 .crypto_stream
3695 .send
3696 .ack_and_drop(offset, length);
3697 },
3698
3699 frame::Frame::StreamHeader {
3700 stream_id,
3701 offset,
3702 length,
3703 fin,
3704 } => {
3705 // Emit qlog before checking if the stream still exists.
3706 // The client does need to ACK frames that were received
3707 // after the client sends a ResetStream.
3708
3709 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
3710 let ev_data = EventData::QuicStreamDataMoved(
3711 qlog::events::quic::StreamDataMoved {
3712 stream_id: Some(stream_id),
3713 offset: Some(offset),
3714 raw: Some(RawInfo {
3715 length: Some(length as u64),
3716 ..Default::default()
3717 }),
3718 from: Some(DataRecipient::Transport),
3719 to: Some(DataRecipient::Dropped),
3720 ..Default::default()
3721 },
3722 );
3723
3724 q.add_event_data_with_instant(ev_data, now).ok();
3725 });
3726
3727 let stream = match self.streams.get_mut(stream_id) {
3728 Some(v) => v,
3729
3730 None => continue,
3731 };
3732
3733 let dropped = stream.send.ack_and_drop(offset, length);
3734
3735 if fin {
3736 stream.send.ack_fin();
3737 }
3738
3739 self.streams.try_collect(stream_id);
3740
3741 // Update `tx_buffered` for data dropped from stream
3742 // buffers, such as retransmission data acknowledged
3743 // before it could be resent.
3744 if dropped > 0 {
3745 self.streams.sub_tx_buffered(dropped);
3746 }
3747 },
3748
3749 frame::Frame::HandshakeDone => {
3750 // Explicitly set this to true, so that if the frame was
3751 // already scheduled for retransmission, it is aborted.
3752 self.handshake_done_sent = true;
3753
3754 self.handshake_done_acked = true;
3755 },
3756
3757 frame::Frame::ResetStream { stream_id, .. } => {
3758 if let Some(stream) = self.streams.get_mut(stream_id) {
3759 stream.send.ack_reset_stream();
3760 }
3761 self.streams.remove_reset(stream_id);
3762 self.streams.try_collect(stream_id);
3763 },
3764
3765 _ => (),
3766 }
3767 }
3768 }
3769
3770 // Now that we processed all the frames, if there is a path that has no
3771 // Destination CID, try to allocate one.
3772 let no_dcid = self
3773 .paths
3774 .iter_mut()
3775 .filter(|(_, p)| p.active_dcid_seq.is_none());
3776
3777 for (pid, p) in no_dcid {
3778 if self.ids.zero_length_dcid() {
3779 p.active_dcid_seq = Some(0);
3780 continue;
3781 }
3782
3783 let dcid_seq = match self.ids.lowest_available_dcid_seq() {
3784 Some(seq) => seq,
3785 None => break,
3786 };
3787
3788 self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
3789
3790 p.active_dcid_seq = Some(dcid_seq);
3791 }
3792
3793 // We only record the time of arrival of the largest packet number
3794 // that still needs to be acked, to be used for ACK delay calculation.
3795 if self.pkt_num_spaces[epoch].recv_pkt_need_ack.last() < Some(pn) {
3796 self.pkt_num_spaces[epoch].largest_rx_pkt_time = now;
3797 }
3798
3799 self.pkt_num_spaces[epoch].recv_pkt_num.insert(pn);
3800
3801 self.pkt_num_spaces[epoch].recv_pkt_need_ack.push_item(pn);
3802
3803 self.pkt_num_spaces[epoch].ack_elicited =
3804 cmp::max(self.pkt_num_spaces[epoch].ack_elicited, ack_elicited);
3805
3806 self.pkt_num_spaces[epoch].largest_rx_pkt_num =
3807 cmp::max(self.pkt_num_spaces[epoch].largest_rx_pkt_num, pn);
3808
3809 if !probing {
3810 self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num = cmp::max(
3811 self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num,
3812 pn,
3813 );
3814
3815 // Did the peer migrated to another path?
3816 let active_path_id = self.paths.get_active_path_id()?;
3817
3818 if self.is_server &&
3819 recv_pid != active_path_id &&
3820 self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num == pn
3821 {
3822 self.on_peer_migrated(recv_pid, self.disable_dcid_reuse, now)?;
3823 }
3824 }
3825
3826 if let Some(idle_timeout) = self.idle_timeout() {
3827 self.idle_timer = Some(now + idle_timeout);
3828 }
3829
3830 // Update send capacity.
3831 self.update_tx_cap();
3832
3833 self.recv_count += 1;
3834 self.paths.get_mut(recv_pid)?.recv_count += 1;
3835
3836 let read = b.off() + aead_tag_len;
3837
3838 self.recv_bytes += read as u64;
3839 self.paths.get_mut(recv_pid)?.recv_bytes += read as u64;
3840
3841 // An Handshake packet has been received from the client and has been
3842 // successfully processed, so we can drop the initial state and consider
3843 // the client's address to be verified.
3844 if self.is_server && hdr.ty == Type::Handshake {
3845 self.drop_epoch_state(packet::Epoch::Initial, now);
3846
3847 self.paths.get_mut(recv_pid)?.verified_peer_address = true;
3848 }
3849
3850 self.ack_eliciting_sent = false;
3851
3852 Ok(read)
3853 }
3854
3855 /// Writes a single QUIC packet to be sent to the peer.
3856 ///
3857 /// On success the number of bytes written to the output buffer is
3858 /// returned, or [`Done`] if there was nothing to write.
3859 ///
3860 /// The application should call `send()` multiple times until [`Done`] is
3861 /// returned, indicating that there are no more packets to send. It is
3862 /// recommended that `send()` be called in the following cases:
3863 ///
3864 /// * When the application receives QUIC packets from the peer (that is,
3865 /// any time [`recv()`] is also called).
3866 ///
3867 /// * When the connection timer expires (that is, any time [`on_timeout()`]
3868 /// is also called).
3869 ///
3870 /// * When the application sends data to the peer (for example, any time
3871 /// [`stream_send()`] or [`stream_shutdown()`] are called).
3872 ///
3873 /// * When the application receives data from the peer (for example any
3874 /// time [`stream_recv()`] is called).
3875 ///
3876 /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3877 /// `send()` and all calls will return [`Done`].
3878 ///
3879 /// [`Done`]: enum.Error.html#variant.Done
3880 /// [`recv()`]: struct.Connection.html#method.recv
3881 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3882 /// [`stream_send()`]: struct.Connection.html#method.stream_send
3883 /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3884 /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3885 /// [`is_draining()`]: struct.Connection.html#method.is_draining
3886 ///
3887 /// ## Examples:
3888 ///
3889 /// ```no_run
3890 /// # let mut out = [0; 512];
3891 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3892 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3893 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3894 /// # let peer = "127.0.0.1:1234".parse().unwrap();
3895 /// # let local = socket.local_addr().unwrap();
3896 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3897 /// loop {
3898 /// let (write, send_info) = match conn.send(&mut out) {
3899 /// Ok(v) => v,
3900 ///
3901 /// Err(quiche::Error::Done) => {
3902 /// // Done writing.
3903 /// break;
3904 /// },
3905 ///
3906 /// Err(e) => {
3907 /// // An error occurred, handle it.
3908 /// break;
3909 /// },
3910 /// };
3911 ///
3912 /// socket.send_to(&out[..write], &send_info.to).unwrap();
3913 /// }
3914 /// # Ok::<(), quiche::Error>(())
3915 /// ```
3916 pub fn send(&mut self, out: &mut [u8]) -> Result<(usize, SendInfo)> {
3917 self.send_on_path(out, None, None)
3918 }
3919
3920 /// Writes a single QUIC packet to be sent to the peer from the specified
3921 /// local address `from` to the destination address `to`.
3922 ///
3923 /// The behavior of this method differs depending on the value of the `from`
3924 /// and `to` parameters:
3925 ///
3926 /// * If both are `Some`, then the method only consider the 4-tuple
3927 /// (`from`, `to`). Application can monitor the 4-tuple availability,
3928 /// either by monitoring [`path_event_next()`] events or by relying on
3929 /// the [`paths_iter()`] method. If the provided 4-tuple does not exist
3930 /// on the connection (anymore), it returns an [`InvalidState`].
3931 ///
3932 /// * If `from` is `Some` and `to` is `None`, then the method only
3933 /// considers sending packets on paths having `from` as local address.
3934 ///
3935 /// * If `to` is `Some` and `from` is `None`, then the method only
3936 /// considers sending packets on paths having `to` as peer address.
3937 ///
3938 /// * If both are `None`, all available paths are considered.
3939 ///
3940 /// On success the number of bytes written to the output buffer is
3941 /// returned, or [`Done`] if there was nothing to write.
3942 ///
3943 /// The application should call `send_on_path()` multiple times until
3944 /// [`Done`] is returned, indicating that there are no more packets to
3945 /// send. It is recommended that `send_on_path()` be called in the
3946 /// following cases:
3947 ///
3948 /// * When the application receives QUIC packets from the peer (that is,
3949 /// any time [`recv()`] is also called).
3950 ///
3951 /// * When the connection timer expires (that is, any time [`on_timeout()`]
3952 /// is also called).
3953 ///
3954 /// * When the application sends data to the peer (for examples, any time
3955 /// [`stream_send()`] or [`stream_shutdown()`] are called).
3956 ///
3957 /// * When the application receives data from the peer (for example any
3958 /// time [`stream_recv()`] is called).
3959 ///
3960 /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3961 /// `send_on_path()` and all calls will return [`Done`].
3962 ///
3963 /// [`Done`]: enum.Error.html#variant.Done
3964 /// [`InvalidState`]: enum.Error.html#InvalidState
3965 /// [`recv()`]: struct.Connection.html#method.recv
3966 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3967 /// [`stream_send()`]: struct.Connection.html#method.stream_send
3968 /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3969 /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3970 /// [`path_event_next()`]: struct.Connection.html#method.path_event_next
3971 /// [`paths_iter()`]: struct.Connection.html#method.paths_iter
3972 /// [`is_draining()`]: struct.Connection.html#method.is_draining
3973 ///
3974 /// ## Examples:
3975 ///
3976 /// ```no_run
3977 /// # let mut out = [0; 512];
3978 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3979 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3980 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3981 /// # let peer = "127.0.0.1:1234".parse().unwrap();
3982 /// # let local = socket.local_addr().unwrap();
3983 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3984 /// loop {
3985 /// let (write, send_info) = match conn.send_on_path(&mut out, Some(local), Some(peer)) {
3986 /// Ok(v) => v,
3987 ///
3988 /// Err(quiche::Error::Done) => {
3989 /// // Done writing.
3990 /// break;
3991 /// },
3992 ///
3993 /// Err(e) => {
3994 /// // An error occurred, handle it.
3995 /// break;
3996 /// },
3997 /// };
3998 ///
3999 /// socket.send_to(&out[..write], &send_info.to).unwrap();
4000 /// }
4001 /// # Ok::<(), quiche::Error>(())
4002 /// ```
4003 pub fn send_on_path(
4004 &mut self, out: &mut [u8], from: Option<SocketAddr>,
4005 to: Option<SocketAddr>,
4006 ) -> Result<(usize, SendInfo)> {
4007 if out.is_empty() {
4008 return Err(Error::BufferTooShort);
4009 }
4010
4011 if self.is_closed() || self.is_draining() {
4012 return Err(Error::Done);
4013 }
4014
4015 let now = Instant::now();
4016
4017 if self.local_error.is_none() {
4018 self.do_handshake(now)?;
4019 }
4020
4021 // Forwarding the error value here could confuse
4022 // applications, as they may not expect getting a `recv()`
4023 // error when calling `send()`.
4024 //
4025 // We simply fall-through to sending packets, which should
4026 // take care of terminating the connection as needed.
4027 let _ = self.process_undecrypted_0rtt_packets();
4028
4029 // There's no point in trying to send a packet if the Initial secrets
4030 // have not been derived yet, so return early.
4031 if !self.derived_initial_secrets {
4032 return Err(Error::Done);
4033 }
4034
4035 let mut has_initial = false;
4036
4037 let mut done = 0;
4038
4039 // Limit output packet size to respect the sender and receiver's
4040 // maximum UDP payload size limit.
4041 let mut left = cmp::min(out.len(), self.max_send_udp_payload_size());
4042
4043 let send_pid = match (from, to) {
4044 (Some(f), Some(t)) => self
4045 .paths
4046 .path_id_from_addrs(&(f, t))
4047 .ok_or(Error::InvalidState)?,
4048
4049 _ => self.get_send_path_id(from, to)?,
4050 };
4051
4052 let send_path = self.paths.get_mut(send_pid)?;
4053
4054 // Increase the maximum datagram size for a PMTUD probe.
4055 if let Some(pmtud) = send_path.pmtud.as_mut() {
4056 if pmtud.should_probe() {
4057 let size = if self.handshake_confirmed || self.handshake_completed
4058 {
4059 pmtud.get_probe_size()
4060 } else {
4061 pmtud.get_current_mtu()
4062 };
4063
4064 send_path.recovery.pmtud_update_max_datagram_size(size);
4065
4066 left =
4067 cmp::min(out.len(), send_path.recovery.max_datagram_size());
4068 }
4069 }
4070
4071 // Limit data sent by the server based on the amount of data received
4072 // from the client before its address is validated.
4073 if !send_path.verified_peer_address && self.is_server {
4074 left = cmp::min(left, send_path.max_send_bytes);
4075 }
4076
4077 // Generate coalesced packets.
4078 while left > 0 {
4079 let (ty, written) = match self.send_single(
4080 &mut out[done..done + left],
4081 send_pid,
4082 has_initial,
4083 now,
4084 ) {
4085 Ok(v) => v,
4086
4087 Err(Error::BufferTooShort) | Err(Error::Done) => break,
4088
4089 Err(e) => return Err(e),
4090 };
4091
4092 done += written;
4093 left -= written;
4094
4095 match ty {
4096 Type::Initial => has_initial = true,
4097
4098 // No more packets can be coalesced after a 1-RTT.
4099 Type::Short => break,
4100
4101 _ => (),
4102 };
4103
4104 // When sending multiple PTO probes, don't coalesce them together,
4105 // so they are sent on separate UDP datagrams.
4106 if let Ok(epoch) = ty.to_epoch() {
4107 if self.paths.get_mut(send_pid)?.recovery.loss_probes(epoch) > 0 {
4108 break;
4109 }
4110 }
4111
4112 // Don't coalesce packets that must go on different paths.
4113 if !(from.is_some() && to.is_some()) &&
4114 self.get_send_path_id(from, to)? != send_pid
4115 {
4116 break;
4117 }
4118 }
4119
4120 if done == 0 {
4121 self.last_tx_data = self.tx_data;
4122
4123 return Err(Error::Done);
4124 }
4125
4126 if has_initial && left > 0 && done < MIN_CLIENT_INITIAL_LEN {
4127 let pad_len = cmp::min(left, MIN_CLIENT_INITIAL_LEN - done);
4128
4129 // Fill padding area with null bytes, to avoid leaking information
4130 // in case the application reuses the packet buffer.
4131 out[done..done + pad_len].fill(0);
4132
4133 done += pad_len;
4134 }
4135
4136 let send_path = self.paths.get(send_pid)?;
4137
4138 let info = SendInfo {
4139 from: send_path.local_addr(),
4140 to: send_path.peer_addr(),
4141
4142 at: send_path.recovery.get_packet_send_time(now),
4143 };
4144
4145 Ok((done, info))
4146 }
4147
4148 fn send_single(
4149 &mut self, out: &mut [u8], send_pid: usize, has_initial: bool,
4150 now: Instant,
4151 ) -> Result<(Type, usize)> {
4152 if out.is_empty() {
4153 return Err(Error::BufferTooShort);
4154 }
4155
4156 if self.is_draining() {
4157 return Err(Error::Done);
4158 }
4159
4160 if self.local_error.is_none() {
4161 for &epoch in packet::Epoch::epochs(
4162 packet::Epoch::Initial..=packet::Epoch::Application,
4163 ) {
4164 let has_probe = self.crypto_ctx[epoch].crypto_seal.is_some() &&
4165 self.paths.get(send_pid)?.recovery.loss_probes(epoch) > 0;
4166
4167 if !has_probe {
4168 continue;
4169 }
4170
4171 match self.send_single_for_type(
4172 out,
4173 send_pid,
4174 has_initial,
4175 now,
4176 Type::from_epoch(epoch),
4177 true,
4178 ) {
4179 Err(Error::Done) => (),
4180
4181 result => return result,
4182 }
4183 }
4184 }
4185
4186 let pkt_type = self.write_pkt_type(send_pid)?;
4187 let epoch = pkt_type.to_epoch()?;
4188 let send_path = self.paths.get(send_pid)?;
4189
4190 // A closing connection sends a single packet with a CONNECTION_CLOSE
4191 // frame before draining, which must not be delayed when the congestion
4192 // window is full of unacknowledged packets.
4193 let sends_close = self.local_error.is_some() &&
4194 (send_path.active() || self.paths.len() == 1);
4195
4196 let bypass_cwnd =
4197 send_path.recovery.loss_probes(epoch) > 0 || sends_close;
4198
4199 self.send_single_for_type(
4200 out,
4201 send_pid,
4202 has_initial,
4203 now,
4204 pkt_type,
4205 bypass_cwnd,
4206 )
4207 }
4208
4209 fn send_single_for_type(
4210 &mut self, out: &mut [u8], send_pid: usize, has_initial: bool,
4211 now: Instant, pkt_type: Type, bypass_cwnd: bool,
4212 ) -> Result<(Type, usize)> {
4213 let is_closing = self.local_error.is_some();
4214
4215 let out_len = out.len();
4216
4217 let mut b = octets::OctetsMut::with_slice(out);
4218
4219 let max_dgram_len = if !self.dgram_send_queue.is_empty() {
4220 self.dgram_max_writable_len()
4221 } else {
4222 None
4223 };
4224
4225 let epoch = pkt_type.to_epoch()?;
4226 let pkt_space = &mut self.pkt_num_spaces[epoch];
4227 let crypto_ctx = &mut self.crypto_ctx[epoch];
4228
4229 // Process lost frames. There might be several paths having lost frames.
4230 let (paths, path_events) = self.paths.iter_mut_and_events();
4231 for (_, p) in paths {
4232 while let Some(lost) = p.recovery.next_lost_frame(epoch) {
4233 match lost {
4234 frame::Frame::CryptoHeader { offset, length } => {
4235 crypto_ctx.crypto_stream.send.retransmit(offset, length);
4236
4237 self.stream_retrans_bytes += length as u64;
4238 p.stream_retrans_bytes += length as u64;
4239
4240 self.retrans_count += 1;
4241 p.retrans_count += 1;
4242 },
4243
4244 frame::Frame::StreamHeader {
4245 stream_id,
4246 offset,
4247 length,
4248 fin,
4249 } => {
4250 let stream = match self.streams.get_mut(stream_id) {
4251 // Only retransmit data if the stream is not closed
4252 // or reset.
4253 Some(v) if !v.send.is_reset() => v,
4254
4255 // Data on a closed stream will not be retransmitted
4256 // or acked after it is declared lost, so just drop
4257 // it.
4258 _ => {
4259 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
4260 let ev_data = EventData::QuicStreamDataMoved(
4261 qlog::events::quic::StreamDataMoved {
4262 stream_id: Some(stream_id),
4263 offset: Some(offset),
4264 raw: Some(RawInfo {
4265 length: Some(length as u64),
4266 ..Default::default()
4267 }),
4268 from: Some(DataRecipient::Transport),
4269 to: Some(DataRecipient::Dropped),
4270 ..Default::default()
4271 },
4272 );
4273
4274 q.add_event_data_with_instant(ev_data, now)
4275 .ok();
4276 });
4277
4278 continue;
4279 },
4280 };
4281
4282 let was_flushable = stream.is_flushable();
4283
4284 let retransmitted =
4285 stream.send.retransmit(offset, length);
4286
4287 if fin {
4288 stream.send.retransmit_fin();
4289 }
4290
4291 // If the stream is now flushable push it to the
4292 // flushable queue, but only if it wasn't already
4293 // queued.
4294 if stream.is_flushable() && !was_flushable {
4295 let priority_key = Arc::clone(&stream.priority_key);
4296 self.streams.insert_flushable(&priority_key);
4297 }
4298
4299 // Update tx_buffered when data is marked for
4300 // retransmission (it was decremented when emitted).
4301 // Only increment by the actual amount retransmitted,
4302 // which may be less than `length` if some data was
4303 // already acked.
4304 self.streams.add_tx_buffered(retransmitted);
4305
4306 self.stream_retrans_bytes += length as u64;
4307 p.stream_retrans_bytes += length as u64;
4308
4309 self.retrans_count += 1;
4310 p.retrans_count += 1;
4311 },
4312
4313 frame::Frame::ACK { .. } => {
4314 pkt_space.ack_elicited = true;
4315 },
4316
4317 frame::Frame::ResetStream {
4318 stream_id,
4319 error_code,
4320 final_size,
4321 } => {
4322 self.streams
4323 .insert_reset(stream_id, error_code, final_size);
4324 },
4325
4326 frame::Frame::StopSending {
4327 stream_id,
4328 error_code,
4329 } =>
4330 // We only need to retransmit the STOP_SENDING frame if
4331 // the stream is still active and not FIN'd. Even if the
4332 // packet was lost, if the application has the final
4333 // size at this point there is no need to retransmit.
4334 if let Some(stream) = self.streams.get(stream_id) {
4335 if !stream.recv.is_fin() {
4336 self.streams
4337 .insert_stopped(stream_id, error_code);
4338 }
4339 },
4340
4341 // Retransmit HANDSHAKE_DONE only if it hasn't been acked at
4342 // least once already.
4343 frame::Frame::HandshakeDone =>
4344 if !self.handshake_done_acked {
4345 self.handshake_done_sent = false;
4346 },
4347
4348 frame::Frame::MaxStreamData { stream_id, .. } => {
4349 if self.streams.get(stream_id).is_some() {
4350 self.streams.insert_almost_full(stream_id);
4351 }
4352 },
4353
4354 frame::Frame::MaxData { .. } => {
4355 self.should_send_max_data = true;
4356 },
4357
4358 frame::Frame::MaxStreamsUni { .. } => {
4359 self.should_send_max_streams_uni = true;
4360 },
4361
4362 frame::Frame::MaxStreamsBidi { .. } => {
4363 self.should_send_max_streams_bidi = true;
4364 },
4365
4366 // Retransmit STREAMS_BLOCKED frames if the frame with the
4367 // most recent limit is lost. These are informational
4368 // signals to the peer, reliably sending them
4369 // ensures the signal is used consistently and helps
4370 // debugging.
4371 frame::Frame::StreamsBlockedBidi { limit } => {
4372 self.streams_blocked_bidi_state
4373 .force_retransmit_sent_limit_eq(limit);
4374 },
4375
4376 frame::Frame::StreamsBlockedUni { limit } => {
4377 self.streams_blocked_uni_state
4378 .force_retransmit_sent_limit_eq(limit);
4379 },
4380
4381 frame::Frame::NewConnectionId { seq_num, .. } => {
4382 self.ids.mark_advertise_new_scid_seq(seq_num, true);
4383 },
4384
4385 frame::Frame::RetireConnectionId { seq_num } => {
4386 self.ids.mark_retire_dcid_seq(seq_num, true)?;
4387 },
4388
4389 frame::Frame::Ping { mtu_probe } => {
4390 // Ping frames are not retransmitted.
4391 if let Some(failed_probe) = mtu_probe {
4392 trace!("pmtud probe dropped: {failed_probe}");
4393
4394 let local = p.local_addr();
4395 let peer = p.peer_addr();
4396 if let Some(pmtud) = p.pmtud.as_mut() {
4397 let old_pmtu = pmtud.get_current_mtu();
4398 pmtud.failed_probe(failed_probe);
4399 let new_pmtu = pmtud.get_current_mtu();
4400
4401 if let Some(event) = path::pmtu_event(
4402 local, peer, old_pmtu, new_pmtu,
4403 ) {
4404 p.recovery
4405 .pmtud_update_max_datagram_size(new_pmtu);
4406 path_events.push_back(event);
4407 }
4408 }
4409 }
4410 },
4411
4412 // Sent as StreamHeader frames. Stream frames are never
4413 // generated by quiche.
4414 frame::Frame::Stream { .. } => {
4415 debug_panic!(
4416 "Unexpected frame lost: Stream. quiche should \
4417 have tracked retransmittable stream data as \
4418 StreamHeader frames."
4419 );
4420 },
4421
4422 // Sent as CryptoHeader frames. Crypto frames are never
4423 // generated by quiche.
4424 frame::Frame::Crypto { .. } => {
4425 debug_panic!(
4426 "Unexpected frame lost: Crypto. quiche should \
4427 have tracked retransmittable crypto data as \
4428 CryptoHeader frames."
4429 );
4430 },
4431
4432 // NewToken frames are never sent by quiche; they are not
4433 // implemented.
4434 frame::Frame::NewToken { .. } => {
4435 debug_panic!(
4436 "Unexpected frame lost: NewToken. quiche used to \
4437 not implement NewToken frames, retransmission of \
4438 these frames is not implemented."
4439 );
4440 },
4441
4442 // Data blocked frames are an optional advisory
4443 // signal. We choose to not retransmit them to
4444 // avoid unnecessary network usage.
4445 frame::Frame::DataBlocked { .. } |
4446 frame::Frame::StreamDataBlocked { .. } => (),
4447
4448 // Path challenge and response have their own
4449 // retry logic. They should not be retransmitted
4450 // normally since according to RFC 9000 Section
4451 // 8.2.2: "An endpoint MUST NOT send more than one
4452 // PATH_RESPONSE frame in response to one
4453 // PATH_CHALLENGE frame".
4454 frame::Frame::PathChallenge { .. } |
4455 frame::Frame::PathResponse { .. } => (),
4456
4457 // From RFC 9000 Section 13.3: CONNECTION_CLOSE
4458 // frames, are not sent again when packet loss is
4459 // detected. Resending these signals is described
4460 // in Section 10.
4461 frame::Frame::ConnectionClose { .. } |
4462 frame::Frame::ApplicationClose { .. } => (),
4463
4464 // Padding doesn't require retransmission.
4465 frame::Frame::Padding { .. } => (),
4466
4467 frame::Frame::DatagramHeader { .. } |
4468 frame::Frame::Datagram { .. } => {
4469 // Datagrams do not require retransmission. Just update
4470 // stats.
4471 p.dgram_lost_count = p.dgram_lost_count.saturating_add(1);
4472 },
4473 // IMPORTANT: Do not add an exhaustive catch
4474 // all. We want to add explicit handling for frame
4475 // types that can be safely ignored when lost.
4476 }
4477 }
4478 }
4479
4480 #[cfg(debug_assertions)]
4481 self.streams.debug_check_tx_buffered_consistency();
4482
4483 let is_app_limited = self.delivery_rate_check_if_app_limited();
4484 let n_paths = self.paths.len();
4485 let path = self.paths.get_mut(send_pid)?;
4486 let flow_control = &mut self.flow_control;
4487 let pkt_space = &mut self.pkt_num_spaces[epoch];
4488 let crypto_ctx = &mut self.crypto_ctx[epoch];
4489 let pkt_num_manager = &mut self.pkt_num_manager;
4490
4491 let mut left = if let Some(pmtud) = path.pmtud.as_mut() {
4492 // Limit output buffer size by estimated path MTU.
4493 cmp::min(pmtud.get_current_mtu(), b.cap())
4494 } else {
4495 b.cap()
4496 };
4497
4498 if pkt_num_manager.should_skip_pn(self.handshake_completed) {
4499 pkt_num_manager.set_skip_pn(Some(self.next_pkt_num));
4500 self.next_pkt_num += 1;
4501 };
4502 let pn = self.next_pkt_num;
4503
4504 let largest_acked_pkt =
4505 path.recovery.get_largest_acked_on_epoch(epoch).unwrap_or(0);
4506 let pn_len = packet::pkt_num_len(pn, largest_acked_pkt);
4507
4508 // The AEAD overhead at the current encryption level.
4509 let crypto_overhead = crypto_ctx.crypto_overhead().ok_or(Error::Done)?;
4510
4511 let dcid_seq = path.active_dcid_seq.ok_or(Error::OutOfIdentifiers)?;
4512
4513 let dcid =
4514 ConnectionId::from_ref(self.ids.get_dcid(dcid_seq)?.cid.as_ref());
4515
4516 let scid = if let Some(scid_seq) = path.active_scid_seq {
4517 ConnectionId::from_ref(self.ids.get_scid(scid_seq)?.cid.as_ref())
4518 } else if pkt_type == Type::Short {
4519 ConnectionId::default()
4520 } else {
4521 return Err(Error::InvalidState);
4522 };
4523
4524 let hdr = Header {
4525 ty: pkt_type,
4526
4527 version: self.version,
4528
4529 dcid,
4530 scid,
4531
4532 pkt_num: 0,
4533 pkt_num_len: pn_len,
4534
4535 // Only clone token for Initial packets, as other packets don't have
4536 // this field (Retry doesn't count, as it's not encoded as part of
4537 // this code path).
4538 token: if pkt_type == Type::Initial {
4539 self.token.clone()
4540 } else {
4541 None
4542 },
4543
4544 versions: None,
4545 key_phase: self.key_phase,
4546 };
4547
4548 hdr.to_bytes(&mut b)?;
4549
4550 let hdr_trace = if log::max_level() == log::LevelFilter::Trace {
4551 Some(format!("{hdr:?}"))
4552 } else {
4553 None
4554 };
4555
4556 let hdr_ty = hdr.ty;
4557
4558 #[cfg(feature = "qlog")]
4559 let qlog_pkt_hdr = self.qlog.streamer.as_ref().map(|_q| {
4560 qlog::events::quic::PacketHeader::with_type(
4561 hdr.ty.to_qlog(),
4562 Some(pn),
4563 Some(hdr.version),
4564 Some(&hdr.scid),
4565 Some(&hdr.dcid),
4566 )
4567 });
4568
4569 // Calculate the space required for the packet, including the header
4570 // the payload length, the packet number and the AEAD overhead.
4571 let mut overhead = b.off() + pn_len + crypto_overhead;
4572
4573 // We assume that the payload length, which is only present in long
4574 // header packets, can always be encoded with a 2-byte varint.
4575 if pkt_type != Type::Short {
4576 overhead += PAYLOAD_LENGTH_LEN;
4577 }
4578
4579 // Make sure we have enough space left for the packet overhead.
4580 match left.checked_sub(overhead) {
4581 Some(v) => left = v,
4582
4583 None => {
4584 // We can't send more because there isn't enough space available
4585 // in the output buffer.
4586 //
4587 // This usually happens when we try to send a new packet but
4588 // failed because cwnd is almost full. In such case app_limited
4589 // is set to false here to make cwnd grow when ACK is received.
4590 path.recovery.update_app_limited(false);
4591 return Err(Error::Done);
4592 },
4593 }
4594
4595 let mut frames: SmallVec<[frame::Frame; 1]> = SmallVec::new();
4596
4597 let mut ack_eliciting = false;
4598 let mut in_flight = false;
4599 let mut is_pmtud_probe = false;
4600 let mut has_data = false;
4601 let mut stream_data_skipped = false;
4602
4603 // Whether a PING frame must explicitly elicit an ACK when no other
4604 // frame does so implicitly.
4605 let ack_elicit_required =
4606 !is_closing && path.recovery.should_elicit_ack(epoch);
4607
4608 let header_offset = b.off();
4609
4610 // Reserve space for payload length in advance. Since we don't yet know
4611 // what the final length will be, we reserve 2 bytes in all cases.
4612 //
4613 // Only long header packets have an explicit length field.
4614 if pkt_type != Type::Short {
4615 b.skip(PAYLOAD_LENGTH_LEN)?;
4616 }
4617
4618 packet::encode_pkt_num(pn, pn_len, &mut b)?;
4619
4620 let payload_offset = b.off();
4621
4622 // `usize::MAX` bypasses cwnd when authorized by packet selection;
4623 // `left` still bounds the packet. Keep this local so other cwnd users
4624 // see the actual window.
4625 let send_limit_from_cwnd = if bypass_cwnd {
4626 usize::MAX
4627 } else {
4628 path.recovery.cwnd_available()
4629 }
4630 .saturating_sub(overhead);
4631
4632 let left_before_packing_ack_frame = left;
4633
4634 // Create ACK frame.
4635 //
4636 // When we need to explicitly elicit an ACK via PING later, go ahead and
4637 // generate an ACK (if there's anything to ACK) since we're going to
4638 // send a packet with PING anyways, even if we haven't received anything
4639 // ACK eliciting.
4640 //
4641 // While closing, PING frames are suppressed, so ACK elicitation must
4642 // not generate ACK-only packets.
4643 if pkt_space.recv_pkt_need_ack.len() > 0 &&
4644 (pkt_space.ack_elicited || ack_elicit_required) &&
4645 (!is_closing ||
4646 (pkt_type == Type::Handshake &&
4647 self.local_error
4648 .as_ref()
4649 .is_some_and(|le| le.is_app))) &&
4650 path.active()
4651 {
4652 #[cfg(not(feature = "fuzzing"))]
4653 let ack_delay = pkt_space.largest_rx_pkt_time.elapsed();
4654
4655 #[cfg(not(feature = "fuzzing"))]
4656 let ack_delay = ack_delay.as_micros() as u64 /
4657 2_u64
4658 .pow(self.local_transport_params.ack_delay_exponent as u32);
4659
4660 // pseudo-random reproducible ack delays when fuzzing
4661 #[cfg(feature = "fuzzing")]
4662 let ack_delay = rand::rand_u8() as u64 + 1;
4663
4664 let frame = frame::Frame::ACK {
4665 ack_delay,
4666 ranges: pkt_space.recv_pkt_need_ack.clone(),
4667 ecn_counts: None, // sending ECN is not supported at this time
4668 };
4669
4670 // When a PING frame needs to be sent, avoid sending the ACK if
4671 // there is not enough cwnd available for both (note that PING
4672 // frames are always 1 byte, so we just need to check that the
4673 // ACK's length is lower than cwnd).
4674 if pkt_space.ack_elicited || frame.wire_len() < send_limit_from_cwnd {
4675 // ACK-only packets are not congestion controlled so ACKs must
4676 // be bundled considering the buffer capacity only, and not the
4677 // available cwnd.
4678 if push_frame_to_pkt!(b, frames, frame, left) {
4679 pkt_space.ack_elicited = false;
4680 }
4681 }
4682 }
4683
4684 // Limit output packet size by congestion window size.
4685 left = cmp::min(
4686 left,
4687 // Bytes consumed by ACK frames.
4688 send_limit_from_cwnd
4689 .saturating_sub(left_before_packing_ack_frame - left),
4690 );
4691
4692 // Make sure there is enough space for the minimum payload length.
4693 // ACK frames are always longer than that.
4694 if frames.is_empty() && left < PAYLOAD_MIN_LEN {
4695 path.recovery.update_app_limited(false);
4696 return Err(Error::Done);
4697 }
4698
4699 let mut challenge_data = None;
4700
4701 if pkt_type == Type::Short {
4702 // Create PMTUD probe.
4703 //
4704 // A PMTUD probe must ignore `left`, which is already limited by the
4705 // current PMTU. The probe remains limited by the output buffer and
4706 // congestion window.
4707 //
4708 // Generate PMTUD probes only after handshake confirmation to avoid
4709 // interference from anti-amplification limits.
4710 if let Ok(active_path) = self.paths.get_active_mut() {
4711 let should_probe_pmtu = active_path.should_send_pmtu_probe(
4712 self.handshake_confirmed,
4713 self.handshake_completed,
4714 out_len,
4715 is_closing,
4716 frames.is_empty(),
4717 );
4718
4719 if should_probe_pmtu {
4720 if let Some(pmtud) = active_path.pmtud.as_mut() {
4721 let probe_size = pmtud.get_probe_size();
4722 trace!(
4723 "{} sending pmtud probe pmtu_probe={} estimated_pmtu={}",
4724 self.trace_id,
4725 probe_size,
4726 pmtud.get_current_mtu(),
4727 );
4728
4729 left = probe_size;
4730
4731 match left.checked_sub(overhead) {
4732 Some(v) => left = v,
4733
4734 None => {
4735 // We can't send more because there isn't enough
4736 // space available in the output buffer.
4737 //
4738 // The congestion window is nearly full. A new
4739 // packet does not fit.
4740 //
4741 // Clear the app-limited state so ACKs can grow
4742 // the congestion window.
4743 active_path.recovery.update_app_limited(false);
4744 return Err(Error::Done);
4745 },
4746 }
4747
4748 let frame = frame::Frame::Padding {
4749 len: probe_size - overhead - 1,
4750 };
4751
4752 if push_frame_to_pkt!(b, frames, frame, left) {
4753 let frame = frame::Frame::Ping {
4754 mtu_probe: Some(probe_size),
4755 };
4756
4757 if push_frame_to_pkt!(b, frames, frame, left) {
4758 ack_eliciting = true;
4759 in_flight = true;
4760 }
4761 }
4762
4763 // Reset probe flag after sending to prevent duplicate
4764 // probes in a single flight.
4765 pmtud.set_in_flight(true);
4766 is_pmtud_probe = true;
4767 }
4768 }
4769 }
4770
4771 let path = self.paths.get_mut(send_pid)?;
4772 // Create PATH_RESPONSE frame if needed.
4773 // We do not try to ensure that these are really sent.
4774 while let Some(challenge) = path.pop_received_challenge() {
4775 let frame = frame::Frame::PathResponse { data: challenge };
4776
4777 if push_frame_to_pkt!(b, frames, frame, left) {
4778 ack_eliciting = true;
4779 in_flight = true;
4780 } else {
4781 // If there are other pending PATH_RESPONSE, don't lose them
4782 // now.
4783 break;
4784 }
4785 }
4786
4787 // Create PATH_CHALLENGE frame if needed.
4788 if path.validation_requested() {
4789 // TODO: ensure that data is unique over paths.
4790 let data = rand::rand_u64().to_be_bytes();
4791
4792 let frame = frame::Frame::PathChallenge { data };
4793
4794 if push_frame_to_pkt!(b, frames, frame, left) {
4795 // Let's notify the path once we know the packet size.
4796 challenge_data = Some(data);
4797
4798 ack_eliciting = true;
4799 in_flight = true;
4800 }
4801 }
4802
4803 if let Some(key_update) = crypto_ctx.key_update.as_mut() {
4804 key_update.update_acked = true;
4805 }
4806 }
4807
4808 let path = self.paths.get_mut(send_pid)?;
4809
4810 if pkt_type == Type::Short && !is_closing {
4811 // Create NEW_CONNECTION_ID frames as needed.
4812 while let Some(seq_num) = self.ids.next_advertise_new_scid_seq() {
4813 let frame = self.ids.get_new_connection_id_frame_for(seq_num)?;
4814
4815 if push_frame_to_pkt!(b, frames, frame, left) {
4816 self.ids.mark_advertise_new_scid_seq(seq_num, false);
4817
4818 ack_eliciting = true;
4819 in_flight = true;
4820 } else {
4821 break;
4822 }
4823 }
4824 }
4825
4826 if pkt_type == Type::Short && !is_closing && path.active() {
4827 // Create HANDSHAKE_DONE frame.
4828 // self.should_send_handshake_done() but without the need to borrow
4829 if self.handshake_completed &&
4830 !self.handshake_done_sent &&
4831 self.is_server
4832 {
4833 let frame = frame::Frame::HandshakeDone;
4834
4835 if push_frame_to_pkt!(b, frames, frame, left) {
4836 self.handshake_done_sent = true;
4837
4838 ack_eliciting = true;
4839 in_flight = true;
4840 }
4841 }
4842
4843 // Create MAX_STREAMS_BIDI frame.
4844 if self.streams.should_update_max_streams_bidi() ||
4845 self.should_send_max_streams_bidi
4846 {
4847 let frame = frame::Frame::MaxStreamsBidi {
4848 max: self.streams.max_streams_bidi_next(),
4849 };
4850
4851 if push_frame_to_pkt!(b, frames, frame, left) {
4852 self.streams.update_max_streams_bidi();
4853 self.should_send_max_streams_bidi = false;
4854
4855 ack_eliciting = true;
4856 in_flight = true;
4857 }
4858 }
4859
4860 // Create MAX_STREAMS_UNI frame.
4861 if self.streams.should_update_max_streams_uni() ||
4862 self.should_send_max_streams_uni
4863 {
4864 let frame = frame::Frame::MaxStreamsUni {
4865 max: self.streams.max_streams_uni_next(),
4866 };
4867
4868 if push_frame_to_pkt!(b, frames, frame, left) {
4869 self.streams.update_max_streams_uni();
4870 self.should_send_max_streams_uni = false;
4871
4872 ack_eliciting = true;
4873 in_flight = true;
4874 }
4875 }
4876
4877 // Create DATA_BLOCKED frame.
4878 if let Some(limit) = self.blocked_limit {
4879 let frame = frame::Frame::DataBlocked { limit };
4880
4881 if push_frame_to_pkt!(b, frames, frame, left) {
4882 self.blocked_limit = None;
4883 self.data_blocked_sent_count =
4884 self.data_blocked_sent_count.saturating_add(1);
4885
4886 ack_eliciting = true;
4887 in_flight = true;
4888 }
4889 }
4890
4891 // Create STREAMS_BLOCKED (bidi) frame when the local endpoint has
4892 // exhausted the peer's bidirectional stream count limit.
4893 if self
4894 .streams_blocked_bidi_state
4895 .has_pending_stream_blocked_frame()
4896 {
4897 if let Some(limit) = self.streams_blocked_bidi_state.blocked_at {
4898 let frame = frame::Frame::StreamsBlockedBidi { limit };
4899
4900 if push_frame_to_pkt!(b, frames, frame, left) {
4901 // Record the limit we just notified the peer about so
4902 // that redundant frames for the same limit are
4903 // suppressed.
4904 self.streams_blocked_bidi_state.blocked_sent =
4905 Some(limit);
4906
4907 ack_eliciting = true;
4908 in_flight = true;
4909 }
4910 }
4911 }
4912
4913 // Create STREAMS_BLOCKED (uni) frame when the local endpoint has
4914 // exhausted the peer's unidirectional stream count limit.
4915 if self
4916 .streams_blocked_uni_state
4917 .has_pending_stream_blocked_frame()
4918 {
4919 if let Some(limit) = self.streams_blocked_uni_state.blocked_at {
4920 let frame = frame::Frame::StreamsBlockedUni { limit };
4921
4922 if push_frame_to_pkt!(b, frames, frame, left) {
4923 // Record the limit we just notified the peer about so
4924 // that redundant frames for the same limit are
4925 // suppressed.
4926 self.streams_blocked_uni_state.blocked_sent = Some(limit);
4927
4928 ack_eliciting = true;
4929 in_flight = true;
4930 }
4931 }
4932 }
4933
4934 // Create MAX_STREAM_DATA frames as needed.
4935 for stream_id in self.streams.almost_full() {
4936 let stream = match self.streams.get_mut(stream_id) {
4937 Some(v) => v,
4938
4939 None => {
4940 // The stream doesn't exist anymore, so remove it from
4941 // the almost full set.
4942 self.streams.remove_almost_full(stream_id);
4943 continue;
4944 },
4945 };
4946
4947 // Autotune the stream window size, but only if this is not a
4948 // retransmission (on a retransmit the stream will be in
4949 // `self.streams.almost_full()` but it's `almost_full()`
4950 // method returns false.
4951 if stream.recv.almost_full() {
4952 stream.recv.autotune_window(now, path.recovery.rtt());
4953 }
4954
4955 let frame = frame::Frame::MaxStreamData {
4956 stream_id,
4957 max: stream.recv.max_data_next(),
4958 };
4959
4960 if push_frame_to_pkt!(b, frames, frame, left) {
4961 let recv_win = stream.recv.window();
4962
4963 stream.recv.update_max_data(now);
4964
4965 self.streams.remove_almost_full(stream_id);
4966
4967 ack_eliciting = true;
4968 in_flight = true;
4969
4970 // Make sure the connection window always has some
4971 // room compared to the stream window.
4972 flow_control.ensure_window_lower_bound(
4973 (recv_win as f64 * CONNECTION_WINDOW_FACTOR) as u64,
4974 );
4975 }
4976 }
4977
4978 // Create MAX_DATA frame as needed.
4979 if flow_control.should_update_max_data() &&
4980 flow_control.max_data() < flow_control.max_data_next()
4981 {
4982 // Autotune the connection window size. We only tune the window
4983 // if we are sending an "organic" update, not on retransmits.
4984 flow_control.autotune_window(now, path.recovery.rtt());
4985 self.should_send_max_data = true;
4986 }
4987
4988 if self.should_send_max_data {
4989 let frame = frame::Frame::MaxData {
4990 max: flow_control.max_data_next(),
4991 };
4992
4993 if push_frame_to_pkt!(b, frames, frame, left) {
4994 self.should_send_max_data = false;
4995
4996 // Commits the new max_rx_data limit.
4997 flow_control.update_max_data(now);
4998
4999 ack_eliciting = true;
5000 in_flight = true;
5001 }
5002 }
5003
5004 // Create STOP_SENDING frames as needed.
5005 for (stream_id, error_code) in self
5006 .streams
5007 .stopped()
5008 .map(|(&k, &v)| (k, v))
5009 .collect::<Vec<(u64, u64)>>()
5010 {
5011 let frame = frame::Frame::StopSending {
5012 stream_id,
5013 error_code,
5014 };
5015
5016 if push_frame_to_pkt!(b, frames, frame, left) {
5017 self.streams.remove_stopped(stream_id);
5018
5019 ack_eliciting = true;
5020 in_flight = true;
5021 }
5022 }
5023
5024 // Create RESET_STREAM frames as needed.
5025 for (stream_id, (error_code, final_size)) in self
5026 .streams
5027 .reset()
5028 .map(|(&k, &v)| (k, v))
5029 .collect::<Vec<(u64, (u64, u64))>>()
5030 {
5031 let frame = frame::Frame::ResetStream {
5032 stream_id,
5033 error_code,
5034 final_size,
5035 };
5036
5037 if push_frame_to_pkt!(b, frames, frame, left) {
5038 self.streams.remove_reset(stream_id);
5039
5040 ack_eliciting = true;
5041 in_flight = true;
5042 }
5043 }
5044
5045 // Create STREAM_DATA_BLOCKED frames as needed.
5046 for (stream_id, limit) in self
5047 .streams
5048 .blocked()
5049 .map(|(&k, &v)| (k, v))
5050 .collect::<Vec<(u64, u64)>>()
5051 {
5052 let frame = frame::Frame::StreamDataBlocked { stream_id, limit };
5053
5054 if push_frame_to_pkt!(b, frames, frame, left) {
5055 self.streams.remove_blocked(stream_id);
5056 self.stream_data_blocked_sent_count =
5057 self.stream_data_blocked_sent_count.saturating_add(1);
5058
5059 ack_eliciting = true;
5060 in_flight = true;
5061 }
5062 }
5063
5064 // Create RETIRE_CONNECTION_ID frames as needed.
5065 let retire_dcid_seqs = self.ids.retire_dcid_seqs();
5066
5067 for seq_num in retire_dcid_seqs {
5068 // The sequence number specified in a RETIRE_CONNECTION_ID frame
5069 // MUST NOT refer to the Destination Connection ID field of the
5070 // packet in which the frame is contained.
5071 let dcid_seq = path.active_dcid_seq.ok_or(Error::InvalidState)?;
5072
5073 if seq_num == dcid_seq {
5074 continue;
5075 }
5076
5077 let frame = frame::Frame::RetireConnectionId { seq_num };
5078
5079 if push_frame_to_pkt!(b, frames, frame, left) {
5080 self.ids.mark_retire_dcid_seq(seq_num, false)?;
5081
5082 ack_eliciting = true;
5083 in_flight = true;
5084 } else {
5085 break;
5086 }
5087 }
5088 }
5089
5090 // Create CONNECTION_CLOSE frame. Try to send this only on the active
5091 // path, unless it is the last one available.
5092 if path.active() || n_paths == 1 {
5093 if let Some(conn_err) = self.local_error.as_ref() {
5094 if conn_err.is_app {
5095 // Create ApplicationClose frame.
5096 if pkt_type == Type::Short {
5097 let frame = frame::Frame::ApplicationClose {
5098 error_code: conn_err.error_code,
5099 reason: conn_err.reason.clone(),
5100 };
5101
5102 if push_frame_to_pkt!(b, frames, frame, left) {
5103 let pto = path.recovery.pto();
5104 self.draining_timer = Some(now + (pto * 3));
5105
5106 ack_eliciting = true;
5107 in_flight = true;
5108 }
5109 }
5110 } else {
5111 // Create ConnectionClose frame.
5112 let frame = frame::Frame::ConnectionClose {
5113 error_code: conn_err.error_code,
5114 frame_type: 0,
5115 reason: conn_err.reason.clone(),
5116 };
5117
5118 if push_frame_to_pkt!(b, frames, frame, left) {
5119 let pto = path.recovery.pto();
5120 self.draining_timer = Some(now + (pto * 3));
5121
5122 ack_eliciting = true;
5123 in_flight = true;
5124 }
5125 }
5126 }
5127 }
5128
5129 // Create CRYPTO frame.
5130 if crypto_ctx.crypto_stream.is_flushable() &&
5131 left > frame::MAX_CRYPTO_OVERHEAD &&
5132 !is_closing &&
5133 path.active()
5134 {
5135 let crypto_off = crypto_ctx.crypto_stream.send.off_front();
5136
5137 // Encode the frame.
5138 //
5139 // Instead of creating a `frame::Frame` object, encode the frame
5140 // directly into the packet buffer.
5141 //
5142 // First we reserve some space in the output buffer for writing the
5143 // frame header (we assume the length field is always a 2-byte
5144 // varint as we don't know the value yet).
5145 //
5146 // Then we emit the data from the crypto stream's send buffer.
5147 //
5148 // Finally we go back and encode the frame header with the now
5149 // available information.
5150 let hdr_off = b.off();
5151 let hdr_len = 1 + // frame type
5152 octets::varint_len(crypto_off) + // offset
5153 2; // length, always encode as 2-byte varint
5154
5155 if let Some(max_len) = left.checked_sub(hdr_len) {
5156 let (mut crypto_hdr, mut crypto_payload) =
5157 b.split_at(hdr_off + hdr_len)?;
5158
5159 // Write stream data into the packet buffer.
5160 let (len, _) = crypto_ctx
5161 .crypto_stream
5162 .send
5163 .emit(&mut crypto_payload.as_mut()[..max_len])?;
5164
5165 // Encode the frame's header.
5166 //
5167 // Due to how `OctetsMut::split_at()` works, `crypto_hdr` starts
5168 // from the initial offset of `b` (rather than the current
5169 // offset), so it needs to be advanced to the
5170 // initial frame offset.
5171 crypto_hdr.skip(hdr_off)?;
5172
5173 frame::encode_crypto_header(
5174 crypto_off,
5175 len as u64,
5176 &mut crypto_hdr,
5177 )?;
5178
5179 // Advance the packet buffer's offset.
5180 b.skip(hdr_len + len)?;
5181
5182 let frame = frame::Frame::CryptoHeader {
5183 offset: crypto_off,
5184 length: len,
5185 };
5186
5187 if push_frame_to_pkt!(b, frames, frame, left) {
5188 ack_eliciting = true;
5189 in_flight = true;
5190 has_data = true;
5191 }
5192 }
5193 }
5194
5195 // The preference of data-bearing frame to include in a packet
5196 // is managed by `self.emit_dgram`. However, whether any frames
5197 // can be sent depends on the state of their buffers. In the case
5198 // where one type is preferred but its buffer is empty, fall back
5199 // to the other type in order not to waste this function call.
5200 let mut dgram_emitted = false;
5201 let dgrams_to_emit = max_dgram_len.is_some();
5202 let stream_to_emit = self.streams.has_flushable();
5203
5204 let mut do_dgram = self.emit_dgram && dgrams_to_emit;
5205 let do_stream = !self.emit_dgram && stream_to_emit;
5206
5207 if !do_stream && dgrams_to_emit {
5208 do_dgram = true;
5209 }
5210
5211 // Create DATAGRAM frame.
5212 if (pkt_type == Type::Short || pkt_type == Type::ZeroRTT) &&
5213 left > frame::MAX_DGRAM_OVERHEAD &&
5214 !is_closing &&
5215 path.active() &&
5216 do_dgram
5217 {
5218 if let Some(max_dgram_payload) = max_dgram_len {
5219 while let Some(len) = self.dgram_send_queue.peek_front_len() {
5220 let hdr_off = b.off();
5221 let hdr_len = 1 + // frame type
5222 2; // length, always encode as 2-byte varint
5223
5224 if (hdr_len + len) <= left {
5225 // Front of the queue fits this packet, send it.
5226 match self.dgram_send_queue.pop() {
5227 Some(data) => {
5228 // Encode the frame.
5229 //
5230 // Instead of creating a `frame::Frame` object,
5231 // encode the frame directly into the packet
5232 // buffer.
5233 //
5234 // First we reserve some space in the output
5235 // buffer for writing the frame header (we
5236 // assume the length field is always a 2-byte
5237 // varint as we don't know the value yet).
5238 //
5239 // Then we emit the data from the DATAGRAM's
5240 // buffer.
5241 //
5242 // Finally we go back and encode the frame
5243 // header with the now available information.
5244 let (mut dgram_hdr, mut dgram_payload) =
5245 b.split_at(hdr_off + hdr_len)?;
5246
5247 dgram_payload.as_mut()[..len]
5248 .copy_from_slice(data.as_ref());
5249
5250 // Encode the frame's header.
5251 //
5252 // Due to how `OctetsMut::split_at()` works,
5253 // `dgram_hdr` starts from the initial offset
5254 // of `b` (rather than the current offset), so
5255 // it needs to be advanced to the initial frame
5256 // offset.
5257 dgram_hdr.skip(hdr_off)?;
5258
5259 frame::encode_dgram_header(
5260 len as u64,
5261 &mut dgram_hdr,
5262 )?;
5263
5264 // Advance the packet buffer's offset.
5265 b.skip(hdr_len + len)?;
5266
5267 let frame =
5268 frame::Frame::DatagramHeader { length: len };
5269
5270 if push_frame_to_pkt!(b, frames, frame, left) {
5271 ack_eliciting = true;
5272 in_flight = true;
5273 dgram_emitted = true;
5274 self.dgram_sent_count =
5275 self.dgram_sent_count.saturating_add(1);
5276 path.dgram_sent_count =
5277 path.dgram_sent_count.saturating_add(1);
5278 }
5279 },
5280
5281 None => continue,
5282 };
5283 } else if len > max_dgram_payload {
5284 // This dgram frame will never fit. Let's purge it.
5285 self.dgram_send_queue.pop();
5286 } else {
5287 break;
5288 }
5289 }
5290 }
5291 }
5292
5293 // Create a single STREAM frame for the first stream that is flushable.
5294 if (pkt_type == Type::Short || pkt_type == Type::ZeroRTT) &&
5295 !is_closing &&
5296 path.active() &&
5297 !dgram_emitted
5298 {
5299 while let Some(priority_key) = self.streams.peek_flushable() {
5300 let stream_id = priority_key.id;
5301 let stream = match self.streams.get_mut(stream_id) {
5302 // A reset can abandon queued data or FINs while the stream
5303 // remains alive waiting for the reset to be acknowledged.
5304 Some(stream) if !stream.send.is_reset() => stream,
5305 _ => {
5306 self.streams.remove_flushable(&priority_key);
5307 continue;
5308 },
5309 };
5310
5311 let stream_off = stream.send.off_front();
5312
5313 // Encode the frame.
5314 //
5315 // Instead of creating a `frame::Frame` object, encode the frame
5316 // directly into the packet buffer.
5317 //
5318 // First we reserve some space in the output buffer for writing
5319 // the frame header (we assume the length field is always a
5320 // 2-byte varint as we don't know the value yet).
5321 //
5322 // Then we emit the data from the stream's send buffer.
5323 //
5324 // Finally we go back and encode the frame header with the now
5325 // available information.
5326 let hdr_off = b.off();
5327 let hdr_len = 1 + // frame type
5328 octets::varint_len(stream_id) + // stream_id
5329 octets::varint_len(stream_off) + // offset
5330 2; // length, always encode as 2-byte varint
5331
5332 // A non-FIN STREAM frame must carry at least one byte. If the
5333 // header and payload don't fit, leave the stream flushable for
5334 // a later packet. Empty FIN frames only need the header.
5335 let max_len = match left.checked_sub(hdr_len) {
5336 Some(v) if v > 0 || stream.send.empty_fin_next() => v,
5337 _ => {
5338 stream_data_skipped = true;
5339
5340 if stream.incremental {
5341 let priority_key = Arc::clone(&stream.priority_key);
5342 self.streams.remove_flushable(&priority_key);
5343 self.streams.insert_flushable(&priority_key);
5344 }
5345
5346 break;
5347 },
5348 };
5349
5350 let (mut stream_hdr, mut stream_payload) =
5351 b.split_at(hdr_off + hdr_len)?;
5352
5353 // Write stream data into the packet buffer.
5354 let (len, fin) =
5355 stream.send.emit(&mut stream_payload.as_mut()[..max_len])?;
5356
5357 // ACK processing can leave a stream in the flushable queue
5358 // after removing all of its retransmission data. Unlink the
5359 // stale entry and continue with the next stream;
5360 // future retransmits or writedata will queue the stream again.
5361 if len == 0 && !fin {
5362 let priority_key = Arc::clone(&stream.priority_key);
5363 self.streams.remove_flushable(&priority_key);
5364 continue;
5365 }
5366
5367 // Encode the frame's header.
5368 //
5369 // Due to how `OctetsMut::split_at()` works, `stream_hdr` starts
5370 // from the initial offset of `b` (rather than the current
5371 // offset), so it needs to be advanced to the initial frame
5372 // offset.
5373 stream_hdr.skip(hdr_off)?;
5374
5375 frame::encode_stream_header(
5376 stream_id,
5377 stream_off,
5378 len as u64,
5379 fin,
5380 &mut stream_hdr,
5381 )?;
5382
5383 // Advance the packet buffer's offset.
5384 b.skip(hdr_len + len)?;
5385
5386 let frame = frame::Frame::StreamHeader {
5387 stream_id,
5388 offset: stream_off,
5389 length: len,
5390 fin,
5391 };
5392
5393 if push_frame_to_pkt!(b, frames, frame, left) {
5394 ack_eliciting = true;
5395 in_flight = true;
5396 has_data = true;
5397 }
5398
5399 let priority_key = Arc::clone(&stream.priority_key);
5400 // Remove the stream when it is no longer flushable.
5401 if !stream.is_flushable() {
5402 self.streams.remove_flushable(&priority_key);
5403 } else if stream.incremental {
5404 // Shuffle the incremental stream to the back of the
5405 // queue.
5406 self.streams.remove_flushable(&priority_key);
5407 self.streams.insert_flushable(&priority_key);
5408 }
5409
5410 // Update tx_buffered when data is emitted.
5411 self.streams.sub_tx_buffered(len);
5412
5413 #[cfg(feature = "fuzzing")]
5414 // Coalesce STREAM frames when fuzzing.
5415 continue;
5416
5417 #[cfg(not(feature = "fuzzing"))]
5418 break;
5419 }
5420 }
5421
5422 // Alternate trying to send DATAGRAMs next time.
5423 self.emit_dgram = !dgram_emitted;
5424
5425 // If no other ack-eliciting frame is sent, include a PING frame
5426 // - if PTO probe needed; OR
5427 // - if we've sent too many non ack-eliciting packets without having
5428 // sent an ACK eliciting one; OR
5429 // - the application requested an ack-eliciting frame be sent.
5430 if (ack_elicit_required || path.needs_ack_eliciting) &&
5431 !ack_eliciting &&
5432 left >= 1 &&
5433 !is_closing
5434 {
5435 let frame = frame::Frame::Ping { mtu_probe: None };
5436
5437 if push_frame_to_pkt!(b, frames, frame, left) {
5438 ack_eliciting = true;
5439 in_flight = true;
5440 }
5441 }
5442
5443 if ack_eliciting && !is_pmtud_probe {
5444 path.needs_ack_eliciting = false;
5445 path.recovery.ping_sent(epoch);
5446 }
5447
5448 // Pending stream data means the sender is size-, not app-limited.
5449 if !has_data &&
5450 !stream_data_skipped &&
5451 !dgram_emitted &&
5452 send_limit_from_cwnd > LEGACY_APP_LIMITED_BYTE_THRESHOLD
5453 {
5454 path.recovery.on_app_limited();
5455 }
5456
5457 if frames.is_empty() {
5458 // When we reach this point we are not able to write more, so set
5459 // app_limited to false.
5460 path.recovery.update_app_limited(false);
5461 return Err(Error::Done);
5462 }
5463
5464 // When coalescing a 1-RTT packet, we can't add padding in the UDP
5465 // datagram, so use PADDING frames instead.
5466 //
5467 // This is only needed if
5468 // 1) an Initial packet has already been written to the UDP datagram,
5469 // as Initial always requires padding.
5470 //
5471 // 2) this is a probing packet towards an unvalidated peer address.
5472 if (has_initial || !path.validated()) &&
5473 pkt_type == Type::Short &&
5474 left >= 1
5475 {
5476 let frame = frame::Frame::Padding { len: left };
5477
5478 if push_frame_to_pkt!(b, frames, frame, left) {
5479 in_flight = true;
5480 }
5481 }
5482
5483 // Pad payload so that it's always at least 4 bytes.
5484 if b.off() - payload_offset < PAYLOAD_MIN_LEN {
5485 let payload_len = b.off() - payload_offset;
5486
5487 let frame = frame::Frame::Padding {
5488 len: PAYLOAD_MIN_LEN - payload_len,
5489 };
5490
5491 #[allow(unused_assignments)]
5492 if push_frame_to_pkt!(b, frames, frame, left) {
5493 in_flight = true;
5494 }
5495 }
5496
5497 let payload_len = b.off() - payload_offset;
5498
5499 // Fill in payload length.
5500 if pkt_type != Type::Short {
5501 let len = pn_len + payload_len + crypto_overhead;
5502
5503 let (_, mut payload_with_len) = b.split_at(header_offset)?;
5504 payload_with_len
5505 .put_varint_with_len(len as u64, PAYLOAD_LENGTH_LEN)?;
5506 }
5507
5508 trace!(
5509 "{} tx pkt {} len={} pn={} {}",
5510 self.trace_id,
5511 hdr_trace.unwrap_or_default(),
5512 payload_len,
5513 pn,
5514 AddrTupleFmt(path.local_addr(), path.peer_addr())
5515 );
5516
5517 #[cfg(feature = "qlog")]
5518 let mut qlog_frames: Vec<qlog::events::quic::QuicFrame> =
5519 Vec::with_capacity(frames.len());
5520
5521 for frame in &mut frames {
5522 trace!("{} tx frm {:?}", self.trace_id, frame);
5523
5524 qlog_with_type!(QLOG_PACKET_TX, self.qlog, _q, {
5525 qlog_frames.push(frame.to_qlog());
5526 });
5527 }
5528
5529 qlog_with_type!(QLOG_PACKET_TX, self.qlog, q, {
5530 if let Some(header) = qlog_pkt_hdr {
5531 // Qlog packet raw info described at
5532 // https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema-00#section-5.1
5533 //
5534 // `length` includes packet headers and trailers (AEAD tag).
5535 let length = payload_len + payload_offset + crypto_overhead;
5536 let qlog_raw_info = RawInfo {
5537 length: Some(length as u64),
5538 payload_length: Some(payload_len as u64),
5539 data: None,
5540 };
5541
5542 let send_at_time =
5543 now.duration_since(q.start_time()).as_secs_f64() * 1000.0;
5544
5545 let ev_data =
5546 EventData::QuicPacketSent(qlog::events::quic::PacketSent {
5547 header,
5548 frames: Some(qlog_frames),
5549 raw: Some(qlog_raw_info),
5550 send_at_time: Some(send_at_time),
5551 ..Default::default()
5552 });
5553
5554 q.add_event_data_with_instant(ev_data, now).ok();
5555 }
5556 });
5557
5558 let aead = match crypto_ctx.crypto_seal {
5559 Some(ref mut v) => v,
5560 None => return Err(Error::InvalidState),
5561 };
5562
5563 let written = packet::encrypt_pkt(
5564 &mut b,
5565 pn,
5566 pn_len,
5567 payload_len,
5568 payload_offset,
5569 None,
5570 aead,
5571 )?;
5572
5573 let sent_pkt_has_data = if path.recovery.gcongestion_enabled() {
5574 has_data || dgram_emitted
5575 } else {
5576 has_data
5577 };
5578
5579 let sent_pkt = recovery::Sent {
5580 pkt_num: pn,
5581 frames,
5582 time_sent: now,
5583 time_acked: None,
5584 time_lost: None,
5585 size: if ack_eliciting { written } else { 0 },
5586 ack_eliciting,
5587 in_flight,
5588 delivered: 0,
5589 delivered_time: now,
5590 first_sent_time: now,
5591 is_app_limited: false,
5592 tx_in_flight: 0,
5593 lost: 0,
5594 has_data: sent_pkt_has_data,
5595 is_pmtud_probe,
5596 };
5597
5598 if in_flight && is_app_limited {
5599 path.recovery.delivery_rate_update_app_limited(true);
5600 }
5601
5602 self.next_pkt_num += 1;
5603
5604 let handshake_status = recovery::HandshakeStatus {
5605 has_handshake_keys: self.crypto_ctx[packet::Epoch::Handshake]
5606 .has_keys(),
5607 peer_verified_address: self.peer_verified_initial_address,
5608 completed: self.handshake_completed,
5609 };
5610
5611 self.on_packet_sent(send_pid, sent_pkt, epoch, handshake_status, now)?;
5612
5613 let path = self.paths.get_mut(send_pid)?;
5614 qlog_with_type!(QLOG_METRICS, self.qlog, q, {
5615 path.recovery.maybe_qlog(q, now);
5616 });
5617
5618 // Record sent packet size if we probe the path.
5619 if let Some(data) = challenge_data {
5620 path.add_challenge_sent(data, written, now);
5621 }
5622
5623 self.sent_count += 1;
5624 self.sent_bytes += written as u64;
5625 path.sent_count += 1;
5626 path.sent_bytes += written as u64;
5627
5628 if self.dgram_send_queue.byte_size() > path.recovery.cwnd_available() {
5629 path.recovery.update_app_limited(false);
5630 }
5631
5632 let had_send_budget = path.max_send_bytes > 0;
5633 path.max_send_bytes = path.max_send_bytes.saturating_sub(written);
5634 if self.is_server &&
5635 !path.verified_peer_address &&
5636 had_send_budget &&
5637 path.max_send_bytes == 0
5638 {
5639 self.amplification_limited_count =
5640 self.amplification_limited_count.saturating_add(1);
5641 }
5642
5643 // On the client, drop initial state after sending an Handshake packet.
5644 if !self.is_server && hdr_ty == Type::Handshake {
5645 self.drop_epoch_state(packet::Epoch::Initial, now);
5646 }
5647
5648 // (Re)start the idle timer if we are sending the first ack-eliciting
5649 // packet since last receiving a packet.
5650 if ack_eliciting && !self.ack_eliciting_sent {
5651 if let Some(idle_timeout) = self.idle_timeout() {
5652 self.idle_timer = Some(now + idle_timeout);
5653 }
5654 }
5655
5656 if ack_eliciting {
5657 self.ack_eliciting_sent = true;
5658 }
5659
5660 Ok((pkt_type, written))
5661 }
5662
5663 fn on_packet_sent(
5664 &mut self, send_pid: usize, sent_pkt: recovery::Sent,
5665 epoch: packet::Epoch, handshake_status: recovery::HandshakeStatus,
5666 now: Instant,
5667 ) -> Result<()> {
5668 let path = self.paths.get_mut(send_pid)?;
5669
5670 // The skip counter may use values from an inactive path.
5671 let cwnd = path.recovery.cwnd();
5672 let max_datagram_size = path.recovery.max_datagram_size();
5673 self.pkt_num_spaces[epoch].on_packet_sent(&sent_pkt);
5674 self.pkt_num_manager.on_packet_sent(
5675 cwnd,
5676 max_datagram_size,
5677 self.handshake_completed,
5678 );
5679
5680 path.recovery.on_packet_sent(
5681 sent_pkt,
5682 epoch,
5683 handshake_status,
5684 now,
5685 &self.trace_id,
5686 );
5687
5688 // Bound recovery metadata when sent packet records accumulate.
5689 if self.local_error.is_none() {
5690 if let Some(max_outstanding_sent_packets) =
5691 self.max_outstanding_sent_packets
5692 {
5693 let tracked_sent_packets: usize = self
5694 .paths
5695 .iter()
5696 .flat_map(|(_, p)| {
5697 packet::Epoch::epochs(
5698 packet::Epoch::Initial..=packet::Epoch::Application,
5699 )
5700 .iter()
5701 .map(|&e| p.recovery.sent_packets_len(e))
5702 })
5703 .sum();
5704
5705 if tracked_sent_packets > max_outstanding_sent_packets.get() {
5706 trace!(
5707 "{} too many outstanding sent packets: {}",
5708 self.trace_id,
5709 tracked_sent_packets
5710 );
5711
5712 self.close(
5713 false,
5714 WireErrorCode::ProtocolViolation as u64,
5715 b"too many outstanding sent packets",
5716 )
5717 .ok();
5718 }
5719 }
5720 }
5721
5722 Ok(())
5723 }
5724
5725 /// Returns the desired send time for the next packet.
5726 #[inline]
5727 pub fn get_next_release_time(&self) -> Option<ReleaseDecision> {
5728 Some(
5729 self.paths
5730 .get_active()
5731 .ok()?
5732 .recovery
5733 .get_next_release_time(),
5734 )
5735 }
5736
5737 /// Returns whether gcongestion is enabled.
5738 #[inline]
5739 pub fn gcongestion_enabled(&self) -> Option<bool> {
5740 Some(self.paths.get_active().ok()?.recovery.gcongestion_enabled())
5741 }
5742
5743 /// Returns the maximum pacing into the future.
5744 ///
5745 /// Equals 1/8 of the smoothed RTT, but at least 1ms and not greater than
5746 /// 5ms.
5747 pub fn max_release_into_future(&self) -> Duration {
5748 self.paths
5749 .get_active()
5750 .map(|p| p.recovery.rtt().mul_f64(0.125))
5751 .unwrap_or(Duration::from_millis(1))
5752 .max(Duration::from_millis(1))
5753 .min(Duration::from_millis(5))
5754 }
5755
5756 /// Returns whether pacing is enabled.
5757 #[inline]
5758 pub fn pacing_enabled(&self) -> bool {
5759 self.recovery_config.pacing
5760 }
5761
5762 /// Returns the size of the send quantum, in bytes.
5763 ///
5764 /// This represents the maximum size of a packet burst as determined by the
5765 /// congestion control algorithm in use.
5766 ///
5767 /// Applications can, for example, use it in conjunction with segmentation
5768 /// offloading mechanisms as the maximum limit for outgoing aggregates of
5769 /// multiple packets.
5770 #[inline]
5771 pub fn send_quantum(&self) -> usize {
5772 match self.paths.get_active() {
5773 Ok(p) => p.recovery.send_quantum(),
5774 _ => 0,
5775 }
5776 }
5777
5778 /// Returns the size of the send quantum over the given 4-tuple, in bytes.
5779 ///
5780 /// This represents the maximum size of a packet burst as determined by the
5781 /// congestion control algorithm in use.
5782 ///
5783 /// Applications can, for example, use it in conjunction with segmentation
5784 /// offloading mechanisms as the maximum limit for outgoing aggregates of
5785 /// multiple packets.
5786 ///
5787 /// If the (`local_addr`, peer_addr`) 4-tuple relates to a non-existing
5788 /// path, this method returns 0.
5789 pub fn send_quantum_on_path(
5790 &self, local_addr: SocketAddr, peer_addr: SocketAddr,
5791 ) -> usize {
5792 self.paths
5793 .path_id_from_addrs(&(local_addr, peer_addr))
5794 .and_then(|pid| self.paths.get(pid).ok())
5795 .map(|path| path.recovery.send_quantum())
5796 .unwrap_or(0)
5797 }
5798
5799 /// Reads contiguous data from a stream into the provided slice.
5800 ///
5801 /// The slice must be sized by the caller and will be populated up to its
5802 /// capacity.
5803 ///
5804 /// On success the amount of bytes read and a flag indicating the fin state
5805 /// is returned as a tuple, or [`Done`] if there is no data to read.
5806 ///
5807 /// Reading data from a stream may trigger queueing of control messages
5808 /// (e.g. MAX_STREAM_DATA). [`send()`] should be called afterwards.
5809 ///
5810 /// [`Done`]: enum.Error.html#variant.Done
5811 /// [`send()`]: struct.Connection.html#method.send
5812 ///
5813 /// ## Examples:
5814 ///
5815 /// ```no_run
5816 /// # let mut buf = [0; 512];
5817 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5818 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5819 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5820 /// # let peer = "127.0.0.1:1234".parse().unwrap();
5821 /// # let local = socket.local_addr().unwrap();
5822 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5823 /// # let stream_id = 0;
5824 /// while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
5825 /// println!("Got {} bytes on stream {}", read, stream_id);
5826 /// }
5827 /// # Ok::<(), quiche::Error>(())
5828 /// ```
5829 #[inline]
5830 pub fn stream_recv(
5831 &mut self, stream_id: u64, out: &mut [u8],
5832 ) -> Result<(usize, bool)> {
5833 self.stream_recv_buf(stream_id, out)
5834 }
5835
5836 /// Reads contiguous data from a stream into the provided [`bytes::BufMut`].
5837 ///
5838 /// **NOTE**:
5839 /// The BufMut will be populated with all available data up to its capacity.
5840 /// Since some BufMut implementations, e.g., [`Vec<u8>`], dynamically
5841 /// allocate additional memory, the caller may use [`BufMut::limit()`]
5842 /// to limit the maximum amount of data that can be written.
5843 ///
5844 /// On success the amount of bytes read and a flag indicating the fin state
5845 /// is returned as a tuple, or [`Done`] if there is no data to read.
5846 /// [`BufMut::advance_mut()`] will have been called with the same number of
5847 /// total bytes.
5848 ///
5849 /// Reading data from a stream may trigger queueing of control messages
5850 /// (e.g. MAX_STREAM_DATA). [`send()`] should be called afterwards.
5851 ///
5852 /// [`BufMut::limit()`]: bytes::BufMut::limit
5853 /// [`BufMut::advance_mut()`]: bytes::BufMut::advance_mut
5854 /// [`Done`]: enum.Error.html#variant.Done
5855 /// [`send()`]: struct.Connection.html#method.send
5856 ///
5857 /// ## Examples:
5858 ///
5859 /// ```no_run
5860 /// # use bytes::BufMut as _;
5861 /// # let mut buf = Vec::new().limit(1024); // Read at most 1024 bytes
5862 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5863 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5864 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5865 /// # let peer = "127.0.0.1:1234".parse().unwrap();
5866 /// # let local = socket.local_addr().unwrap();
5867 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5868 /// # let stream_id = 0;
5869 /// # let mut total_read = 0;
5870 /// while let Ok((read, fin)) = conn.stream_recv_buf(stream_id, &mut buf) {
5871 /// println!("Got {} bytes on stream {}", read, stream_id);
5872 /// total_read += read;
5873 /// assert_eq!(buf.get_ref().len(), total_read);
5874 /// }
5875 /// # Ok::<(), quiche::Error>(())
5876 /// ```
5877 pub fn stream_recv_buf<B: bytes::BufMut>(
5878 &mut self, stream_id: u64, out: B,
5879 ) -> Result<(usize, bool)> {
5880 self.do_stream_recv(stream_id, RecvAction::Emit { out })
5881 }
5882
5883 /// Discard contiguous data from a stream without copying.
5884 ///
5885 /// On success the amount of bytes discarded and a flag indicating the fin
5886 /// state is returned as a tuple, or [`Done`] if there is no data to
5887 /// discard.
5888 ///
5889 /// Discarding data from a stream may trigger queueing of control messages
5890 /// (e.g. MAX_STREAM_DATA). [`send()`] should be called afterwards.
5891 ///
5892 /// [`Done`]: enum.Error.html#variant.Done
5893 /// [`send()`]: struct.Connection.html#method.send
5894 ///
5895 /// ## Examples:
5896 ///
5897 /// ```no_run
5898 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5899 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5900 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5901 /// # let peer = "127.0.0.1:1234".parse().unwrap();
5902 /// # let local = socket.local_addr().unwrap();
5903 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5904 /// # let stream_id = 0;
5905 /// while let Ok((read, fin)) = conn.stream_discard(stream_id, 1) {
5906 /// println!("Discarded {} byte(s) on stream {}", read, stream_id);
5907 /// }
5908 /// # Ok::<(), quiche::Error>(())
5909 /// ```
5910 pub fn stream_discard(
5911 &mut self, stream_id: u64, len: usize,
5912 ) -> Result<(usize, bool)> {
5913 // `do_stream_recv()` is generic on the kind of `BufMut` in RecvAction.
5914 // Since we are discarding, it doesn't matter, but the compiler still
5915 // wants to know, so we say `&mut [u8]`.
5916 self.do_stream_recv::<&mut [u8]>(stream_id, RecvAction::Discard { len })
5917 }
5918
5919 // Reads or discards contiguous data from a stream.
5920 //
5921 // Passing an `action` of `StreamRecvAction::Emit` results in a read into
5922 // the provided slice. It must be sized by the caller and will be populated
5923 // up to its capacity.
5924 //
5925 // Passing an `action` of `StreamRecvAction::Discard` results in discard up
5926 // to the indicated length.
5927 //
5928 // On success the amount of bytes read or discarded, and a flag indicating
5929 // the fin state, is returned as a tuple, or [`Done`] if there is no data to
5930 // read or discard.
5931 //
5932 // Reading or discarding data from a stream may trigger queueing of control
5933 // messages (e.g. MAX_STREAM_DATA). [`send()`] should be called afterwards.
5934 //
5935 // [`Done`]: enum.Error.html#variant.Done
5936 // [`send()`]: struct.Connection.html#method.send
5937 fn do_stream_recv<B: bytes::BufMut>(
5938 &mut self, stream_id: u64, action: RecvAction<B>,
5939 ) -> Result<(usize, bool)> {
5940 // We can't read on our own unidirectional streams.
5941 if !stream::is_bidi(stream_id) &&
5942 stream::is_local(stream_id, self.is_server)
5943 {
5944 return Err(Error::InvalidStreamState(stream_id));
5945 }
5946
5947 let stream = self
5948 .streams
5949 .get_mut(stream_id)
5950 .ok_or(Error::InvalidStreamState(stream_id))?;
5951
5952 if !stream.is_readable() {
5953 return Err(Error::Done);
5954 }
5955
5956 let local = stream.local;
5957 let priority_key = Arc::clone(&stream.priority_key);
5958
5959 #[cfg(feature = "qlog")]
5960 let offset = stream.recv.off_front();
5961
5962 #[cfg(feature = "qlog")]
5963 let to = match action {
5964 RecvAction::Emit { .. } => Some(DataRecipient::Application),
5965
5966 RecvAction::Discard { .. } => Some(DataRecipient::Dropped),
5967 };
5968
5969 let (read, fin) = match stream.recv.emit_or_discard(action) {
5970 Ok(v) => v,
5971
5972 Err(e) => {
5973 // A StreamReset read completes the receive side, but an
5974 // unreported STOP error still needs the send-side state.
5975 if stream.state() == stream::StreamState::Collectable {
5976 self.streams.collect(stream_id, local);
5977 }
5978
5979 self.streams.remove_readable(&priority_key);
5980 return Err(e);
5981 },
5982 };
5983
5984 self.flow_control.add_consumed(read as u64);
5985
5986 let readable = stream.is_readable();
5987
5988 let state = stream.state();
5989
5990 if stream.recv.almost_full() {
5991 self.streams.insert_almost_full(stream_id);
5992 }
5993
5994 if !readable {
5995 self.streams.remove_readable(&priority_key);
5996 }
5997
5998 if state == stream::StreamState::Collectable {
5999 self.streams.collect(stream_id, local);
6000 }
6001
6002 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
6003 let ev_data = EventData::QuicStreamDataMoved(
6004 qlog::events::quic::StreamDataMoved {
6005 stream_id: Some(stream_id),
6006 offset: Some(offset),
6007 raw: Some(RawInfo {
6008 length: Some(read as u64),
6009 ..Default::default()
6010 }),
6011 from: Some(DataRecipient::Transport),
6012 to,
6013 additional_info: fin
6014 .then_some(DataMovedAdditionalInfo::FinSet),
6015 },
6016 );
6017
6018 let now = Instant::now();
6019 q.add_event_data_with_instant(ev_data, now).ok();
6020 });
6021
6022 if priority_key.incremental && readable {
6023 // Shuffle the incremental stream to the back of the queue.
6024 self.streams.remove_readable(&priority_key);
6025 self.streams.insert_readable(&priority_key);
6026 }
6027
6028 Ok((read, fin))
6029 }
6030
6031 /// Writes data to a stream.
6032 ///
6033 /// On success the number of bytes written is returned, or [`Done`] if no
6034 /// data was written (e.g. because the stream has no capacity).
6035 ///
6036 /// Applications can provide a 0-length buffer with the fin flag set to
6037 /// true. This will lead to a 0-length FIN STREAM frame being sent at the
6038 /// latest offset. The `Ok(0)` value is only returned when the application
6039 /// provided a 0-length buffer.
6040 ///
6041 /// In addition, if the peer has signalled that it doesn't want to receive
6042 /// any more data from this stream by sending the `STOP_SENDING` frame, the
6043 /// [`StreamStopped`] error will be returned instead of any data.
6044 ///
6045 /// Note that in order to avoid buffering an infinite amount of data in the
6046 /// stream's send buffer, streams are only allowed to buffer outgoing data
6047 /// up to the amount that the peer allows it to send (that is, up to the
6048 /// stream's outgoing flow control capacity).
6049 ///
6050 /// This means that the number of written bytes returned can be lower than
6051 /// the length of the input buffer when the stream doesn't have enough
6052 /// capacity for the operation to complete. The application should retry the
6053 /// operation once the stream is reported as writable again.
6054 ///
6055 /// Applications should call this method only after the handshake is
6056 /// completed (whenever [`is_established()`] returns `true`) or during
6057 /// early data if enabled (whenever [`is_in_early_data()`] returns `true`).
6058 ///
6059 /// [`Done`]: enum.Error.html#variant.Done
6060 /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
6061 /// [`is_established()`]: struct.Connection.html#method.is_established
6062 /// [`is_in_early_data()`]: struct.Connection.html#method.is_in_early_data
6063 ///
6064 /// ## Examples:
6065 ///
6066 /// ```no_run
6067 /// # let mut buf = [0; 512];
6068 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6069 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6070 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6071 /// # let peer = "127.0.0.1:1234".parse().unwrap();
6072 /// # let local = "127.0.0.1:4321".parse().unwrap();
6073 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6074 /// # let stream_id = 0;
6075 /// conn.stream_send(stream_id, b"hello", true)?;
6076 /// # Ok::<(), quiche::Error>(())
6077 /// ```
6078 pub fn stream_send(
6079 &mut self, stream_id: u64, buf: &[u8], fin: bool,
6080 ) -> Result<usize> {
6081 self.stream_do_send(
6082 stream_id,
6083 buf,
6084 fin,
6085 |stream: &mut stream::Stream<F>,
6086 buf: &[u8],
6087 cap: usize,
6088 fin: bool| {
6089 stream.send.write(&buf[..cap], fin).map(|v| (v, v))
6090 },
6091 )
6092 }
6093
6094 /// Writes data to a stream with zero copying, instead, it appends the
6095 /// provided buffer directly to the send queue if the capacity allows
6096 /// it.
6097 ///
6098 /// When a partial write happens (including when [`Error::Done`] is
6099 /// returned) the remaining (unwritten) buffer will also be returned.
6100 /// The application should retry the operation once the stream is
6101 /// reported as writable again.
6102 pub fn stream_send_zc(
6103 &mut self, stream_id: u64, buf: F::Buf, fin: bool,
6104 ) -> Result<(usize, Option<F::Buf>)>
6105 where
6106 F::Buf: BufSplit,
6107 {
6108 self.stream_do_send(
6109 stream_id,
6110 buf,
6111 fin,
6112 |stream: &mut stream::Stream<F>,
6113 buf: F::Buf,
6114 cap: usize,
6115 fin: bool| {
6116 let (sent, remaining) = stream.send.append_buf(buf, cap, fin)?;
6117 Ok((sent, (sent, remaining)))
6118 },
6119 )
6120 }
6121
6122 fn stream_do_send<B, R, SND>(
6123 &mut self, stream_id: u64, buf: B, fin: bool, write_fn: SND,
6124 ) -> Result<R>
6125 where
6126 B: AsRef<[u8]>,
6127 SND: FnOnce(&mut stream::Stream<F>, B, usize, bool) -> Result<(usize, R)>,
6128 {
6129 // We can't write on the peer's unidirectional streams.
6130 if !stream::is_bidi(stream_id) &&
6131 !stream::is_local(stream_id, self.is_server)
6132 {
6133 return Err(Error::InvalidStreamState(stream_id));
6134 }
6135
6136 let len = buf.as_ref().len();
6137
6138 // Mark the connection as blocked if the connection-level flow control
6139 // limit doesn't let us buffer all the data.
6140 //
6141 // Note that this is separate from "send capacity" as that also takes
6142 // congestion control into consideration.
6143 if self.max_tx_data - self.tx_data < len as u64 {
6144 self.blocked_limit = Some(self.max_tx_data);
6145 }
6146
6147 let cap = self.tx_cap;
6148
6149 // Get existing stream or create a new one.
6150 let stream = match self.get_or_create_stream(stream_id, true) {
6151 Ok(v) => v,
6152
6153 Err(Error::StreamLimit) => {
6154 // If the local endpoint has exhausted the peer's stream count
6155 // limit, record the current limit so that a STREAMS_BLOCKED
6156 // frame can be sent.
6157 if self.enable_send_streams_blocked &&
6158 stream::is_local(stream_id, self.is_server)
6159 {
6160 if stream::is_bidi(stream_id) {
6161 let limit = self.streams.peer_max_streams_bidi();
6162 self.streams_blocked_bidi_state.update_at(limit);
6163 } else {
6164 let limit = self.streams.peer_max_streams_uni();
6165 self.streams_blocked_uni_state.update_at(limit);
6166 }
6167 }
6168
6169 return Err(Error::StreamLimit);
6170 },
6171
6172 Err(e) => return Err(e),
6173 };
6174
6175 #[cfg(feature = "qlog")]
6176 let offset = stream.send.off_back();
6177
6178 let was_writable = stream.is_writable();
6179
6180 let was_flushable = stream.is_flushable();
6181
6182 // Return the stop before collecting a completed stream.
6183 if let Err(Error::StreamStopped(e)) =
6184 stream.send.cap_and_report_stop_sending()
6185 {
6186 self.streams.on_stop_sending_reported(stream_id);
6187 return Err(Error::StreamStopped(e));
6188 };
6189
6190 let priority_key = Arc::clone(&stream.priority_key);
6191
6192 // Truncate the input buffer based on the connection's send capacity if
6193 // necessary.
6194 //
6195 // When the cap is zero, the method returns Ok(0) *only* when the passed
6196 // buffer is empty. We return Error::Done otherwise.
6197 if cap == 0 && len > 0 {
6198 if was_writable {
6199 // When `stream_writable_next()` returns a stream, the writable
6200 // mark is removed, but because the stream is blocked by the
6201 // connection-level send capacity it won't be marked as writable
6202 // again once the capacity increases.
6203 //
6204 // Since the stream is writable already, mark it here instead.
6205 self.streams.insert_writable(&priority_key);
6206 }
6207
6208 return Err(Error::Done);
6209 }
6210
6211 let (cap, fin, blocked_by_cap) = if cap < len {
6212 (cap, false, true)
6213 } else {
6214 (len, fin, false)
6215 };
6216
6217 let (sent, ret) = match write_fn(stream, buf, cap, fin) {
6218 Ok(v) => v,
6219
6220 Err(e) => {
6221 self.streams.remove_writable(&priority_key);
6222 return Err(e);
6223 },
6224 };
6225
6226 let incremental = stream.incremental;
6227 let priority_key = Arc::clone(&stream.priority_key);
6228
6229 let flushable = stream.is_flushable();
6230
6231 let writable = stream.is_writable();
6232
6233 if sent < cap {
6234 let max_off = stream.send.max_off();
6235
6236 if stream.send.blocked_at() != Some(max_off) {
6237 stream.send.update_blocked_at(Some(max_off));
6238 self.streams.insert_blocked(stream_id, max_off);
6239 }
6240 } else {
6241 stream.send.update_blocked_at(None);
6242 self.streams.remove_blocked(stream_id);
6243 }
6244
6245 // If the stream is now flushable push it to the flushable queue, but
6246 // only if it wasn't already queued.
6247 if flushable && !was_flushable {
6248 self.streams.insert_flushable(&priority_key);
6249 }
6250
6251 if !writable {
6252 self.streams.remove_writable(&priority_key);
6253 } else if was_writable && blocked_by_cap {
6254 // When `stream_writable_next()` returns a stream, the writable
6255 // mark is removed, but because the stream is blocked by the
6256 // connection-level send capacity it won't be marked as writable
6257 // again once the capacity increases.
6258 //
6259 // Since the stream is writable already, mark it here instead.
6260 self.streams.insert_writable(&priority_key);
6261 }
6262
6263 self.tx_cap -= sent;
6264
6265 self.tx_data += sent as u64;
6266
6267 self.streams.add_tx_buffered(sent);
6268
6269 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
6270 let ev_data = EventData::QuicStreamDataMoved(
6271 qlog::events::quic::StreamDataMoved {
6272 stream_id: Some(stream_id),
6273 offset: Some(offset),
6274 raw: Some(RawInfo {
6275 length: Some(sent as u64),
6276 ..Default::default()
6277 }),
6278 from: Some(DataRecipient::Application),
6279 to: Some(DataRecipient::Transport),
6280 additional_info: fin
6281 .then_some(DataMovedAdditionalInfo::FinSet),
6282 },
6283 );
6284
6285 let now = Instant::now();
6286 q.add_event_data_with_instant(ev_data, now).ok();
6287 });
6288
6289 if sent == 0 && cap > 0 {
6290 return Err(Error::Done);
6291 }
6292
6293 if incremental && writable {
6294 // Shuffle the incremental stream to the back of the queue.
6295 self.streams.remove_writable(&priority_key);
6296 self.streams.insert_writable(&priority_key);
6297 }
6298
6299 Ok(ret)
6300 }
6301
6302 /// Sets the priority for a stream.
6303 ///
6304 /// A stream's priority determines the order in which stream data is sent
6305 /// on the wire (streams with lower priority are sent first). Streams are
6306 /// created with a default priority of `127`.
6307 ///
6308 /// The target stream is created if it did not exist before calling this
6309 /// method.
6310 pub fn stream_priority(
6311 &mut self, stream_id: u64, urgency: u8, incremental: bool,
6312 ) -> Result<()> {
6313 // Get existing stream or create a new one, but if the stream
6314 // has already been closed and collected, ignore the prioritization.
6315 let stream = match self.get_or_create_stream(stream_id, true) {
6316 Ok(v) => v,
6317
6318 Err(Error::Done) => return Ok(()),
6319
6320 Err(e) => return Err(e),
6321 };
6322
6323 if stream.urgency == urgency && stream.incremental == incremental {
6324 return Ok(());
6325 }
6326
6327 stream.urgency = urgency;
6328 stream.incremental = incremental;
6329
6330 let new_priority_key = Arc::new(StreamPriorityKey {
6331 urgency: stream.urgency,
6332 incremental: stream.incremental,
6333 id: stream_id,
6334 ..Default::default()
6335 });
6336
6337 let old_priority_key =
6338 std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
6339
6340 self.streams
6341 .update_priority(&old_priority_key, &new_priority_key);
6342
6343 Ok(())
6344 }
6345
6346 /// Shuts down reading or writing from/to the specified stream.
6347 ///
6348 /// When the `direction` argument is set to [`Shutdown::Read`], outstanding
6349 /// data in the stream's receive buffer is dropped, and no additional data
6350 /// is added to it. Data received after calling this method is still
6351 /// validated and acked but not stored, and [`stream_recv()`] will not
6352 /// return it to the application. In addition, a `STOP_SENDING` frame will
6353 /// be sent to the peer to signal it to stop sending data.
6354 ///
6355 /// When the `direction` argument is set to [`Shutdown::Write`], outstanding
6356 /// data in the stream's send buffer is dropped, and no additional data is
6357 /// added to it. Data passed to [`stream_send()`] after calling this method
6358 /// will be ignored. In addition, a `RESET_STREAM` frame will be sent to the
6359 /// peer to signal the reset, unless all outgoing data has already been
6360 /// acked by the peer (up to the `fin` offset).
6361 ///
6362 /// Locally-initiated unidirectional streams can only be closed in the
6363 /// [`Shutdown::Write`] direction. Remotely-initiated unidirectional streams
6364 /// can only be closed in the [`Shutdown::Read`] direction. Using an
6365 /// incorrect direction will return [`InvalidStreamState`].
6366 ///
6367 /// [`Shutdown::Read`]: enum.Shutdown.html#variant.Read
6368 /// [`Shutdown::Write`]: enum.Shutdown.html#variant.Write
6369 /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
6370 /// [`stream_send()`]: struct.Connection.html#method.stream_send
6371 /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
6372 pub fn stream_shutdown(
6373 &mut self, stream_id: u64, direction: Shutdown, err: u64,
6374 ) -> Result<()> {
6375 // Don't try to stop a local unidirectional stream.
6376 if direction == Shutdown::Read &&
6377 stream::is_local(stream_id, self.is_server) &&
6378 !stream::is_bidi(stream_id)
6379 {
6380 return Err(Error::InvalidStreamState(stream_id));
6381 }
6382
6383 // Don't try to reset a remote unidirectional stream.
6384 if direction == Shutdown::Write &&
6385 !stream::is_local(stream_id, self.is_server) &&
6386 !stream::is_bidi(stream_id)
6387 {
6388 return Err(Error::InvalidStreamState(stream_id));
6389 }
6390
6391 // Get existing stream.
6392 let stream = self.streams.get_mut(stream_id).ok_or(Error::Done)?;
6393
6394 let priority_key = Arc::clone(&stream.priority_key);
6395
6396 match direction {
6397 Shutdown::Read => {
6398 let consumed = stream.recv.shutdown()?;
6399 self.flow_control.add_consumed(consumed);
6400
6401 // Discarding unread data can complete the receive side, so no
6402 // later read would collect the stream.
6403 let state = stream.state();
6404 let local = stream.local;
6405
6406 if !stream.recv.is_fin() {
6407 self.streams.insert_stopped(stream_id, err);
6408 }
6409
6410 // Once shutdown, the stream is guaranteed to be non-readable.
6411 self.streams.remove_readable(&priority_key);
6412
6413 self.stopped_stream_local_count =
6414 self.stopped_stream_local_count.saturating_add(1);
6415
6416 if state == stream::StreamState::Collectable {
6417 self.streams.collect(stream_id, local);
6418 }
6419 },
6420
6421 Shutdown::Write => {
6422 // Save the buffered length before shutdown (shutdown clears the
6423 // buffer).
6424 let buffered_len = stream.send.buffered_bytes() as usize;
6425
6426 let (final_size, unsent) = stream.send.shutdown()?;
6427
6428 // Claw back some flow control allowance from data that was
6429 // buffered but not actually sent before the stream was reset.
6430 self.tx_data = self.tx_data.saturating_sub(unsent);
6431
6432 // Update tx_buffered: subtract only the buffered data, not
6433 // inflight data.
6434 self.streams.sub_tx_buffered(buffered_len);
6435
6436 // Match App-to-Transport moves with Transport-to-Dropped moves.
6437 // A Network transition would distinguish sent bytes from drops
6438 // before transmission.
6439 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
6440 let ev_data = EventData::QuicStreamDataMoved(
6441 qlog::events::quic::StreamDataMoved {
6442 stream_id: Some(stream_id),
6443 offset: Some(final_size),
6444 raw: Some(RawInfo {
6445 length: Some(unsent),
6446 ..Default::default()
6447 }),
6448 from: Some(DataRecipient::Transport),
6449 to: Some(DataRecipient::Dropped),
6450 ..Default::default()
6451 },
6452 );
6453
6454 q.add_event_data_with_instant(ev_data, Instant::now()).ok();
6455 });
6456
6457 // Update send capacity.
6458 self.update_tx_cap();
6459
6460 self.streams.insert_reset(stream_id, err, final_size);
6461
6462 // Once shutdown, the stream is guaranteed to be non-writable
6463 // and has nothing left to flush. A stale flushable entry could
6464 // make send() return Done while DATAGRAMs are still queued.
6465 self.streams.remove_writable(&priority_key);
6466 self.streams.remove_flushable(&priority_key);
6467
6468 self.reset_stream_local_count =
6469 self.reset_stream_local_count.saturating_add(1);
6470
6471 self.streams.try_collect(stream_id);
6472 },
6473 }
6474
6475 Ok(())
6476 }
6477
6478 /// Returns the stream's send capacity in bytes.
6479 ///
6480 /// The returned capacity takes into account the stream's flow control limit
6481 /// as well as connection level flow and congestion control.
6482 ///
6483 /// If the specified stream doesn't exist (including when it has already
6484 /// been completed and closed), the [`InvalidStreamState`] error will be
6485 /// returned.
6486 ///
6487 /// In addition, if the peer has signalled that it doesn't want to receive
6488 /// any more data from this stream by sending the `STOP_SENDING` frame, the
6489 /// [`StreamStopped`] error will be returned.
6490 ///
6491 /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
6492 /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
6493 #[inline]
6494 pub fn stream_capacity(&mut self, stream_id: u64) -> Result<usize> {
6495 if let Some(stream) = self.streams.get_mut(stream_id) {
6496 let stream_cap = match stream.send.cap_and_report_stop_sending() {
6497 Ok(v) => v,
6498
6499 Err(Error::StreamStopped(e)) => {
6500 self.streams.on_stop_sending_reported(stream_id);
6501 return Err(Error::StreamStopped(e));
6502 },
6503
6504 Err(e) => return Err(e),
6505 };
6506
6507 let cap = cmp::min(self.tx_cap, stream_cap);
6508 return Ok(cap);
6509 };
6510
6511 Err(Error::InvalidStreamState(stream_id))
6512 }
6513
6514 /// Returns the next stream that has data to read.
6515 ///
6516 /// Note that once returned by this method, a stream ID will not be returned
6517 /// again until it is "re-armed".
6518 ///
6519 /// The application will need to read all of the pending data on the stream,
6520 /// and new data has to be received before the stream is reported again.
6521 ///
6522 /// This is unlike the [`readable()`] method, that returns the same list of
6523 /// readable streams when called multiple times in succession.
6524 ///
6525 /// [`readable()`]: struct.Connection.html#method.readable
6526 pub fn stream_readable_next(&mut self) -> Option<u64> {
6527 let priority_key = self.streams.readable.front().clone_pointer()?;
6528
6529 self.streams.remove_readable(&priority_key);
6530
6531 Some(priority_key.id)
6532 }
6533
6534 /// Returns true if the stream has data that can be read.
6535 pub fn stream_readable(&self, stream_id: u64) -> bool {
6536 let stream = match self.streams.get(stream_id) {
6537 Some(v) => v,
6538
6539 None => return false,
6540 };
6541
6542 stream.is_readable()
6543 }
6544
6545 /// Returns the number of contiguous bytes buffered for a stream, up to
6546 /// `max_len`.
6547 ///
6548 /// This is the length of the contiguous, in-order data buffered at the
6549 /// stream's current read offset, i.e. the bytes a call to [`stream_recv`]
6550 /// would return right now. Data received out of order that sits behind a
6551 /// gap is not counted, so this never reports bytes that are not yet
6552 /// readable.
6553 ///
6554 /// This is a companion to [`stream_readable`], which only reports *whether*
6555 /// data is available; this reports *how much*. It is intended for sizing a
6556 /// receive buffer. The cost is proportional to the number of contiguous
6557 /// buffered chunks at the front of the stream, up to `max_len`, and no data
6558 /// is copied.
6559 ///
6560 /// Returns 0 if the stream does not exist.
6561 ///
6562 /// [`stream_recv`]: struct.Connection.html#method.stream_recv
6563 /// [`stream_readable`]: struct.Connection.html#method.stream_readable
6564 pub fn stream_readable_len(&self, stream_id: u64, max_len: usize) -> usize {
6565 match self.streams.get(stream_id) {
6566 Some(s) => s.recv.readable_len(max_len),
6567
6568 None => 0,
6569 }
6570 }
6571
6572 /// Returns the next stream that can be written to.
6573 ///
6574 /// Note that once returned by this method, a stream ID will not be returned
6575 /// again until it is "re-armed".
6576 ///
6577 /// This is unlike the [`writable()`] method, that returns the same list of
6578 /// writable streams when called multiple times in succession. It is not
6579 /// advised to use both `stream_writable_next()` and [`writable()`] on the
6580 /// same connection, as it may lead to unexpected results.
6581 ///
6582 /// The [`stream_writable()`] method can also be used to fine-tune when a
6583 /// stream is reported as writable again.
6584 ///
6585 /// A stopped stream is returned even without send capacity. Querying its
6586 /// capacity or writing to it returns [`StreamStopped`].
6587 ///
6588 /// [`stream_writable()`]: struct.Connection.html#method.stream_writable
6589 /// [`writable()`]: struct.Connection.html#method.writable
6590 /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
6591 pub fn stream_writable_next(&mut self) -> Option<u64> {
6592 if self.tx_cap == 0 {
6593 return self.streams.pop_stopped_writable();
6594 }
6595
6596 let mut cursor = self.streams.writable.front();
6597
6598 while let Some(priority_key) = cursor.clone_pointer() {
6599 if let Some(stream) = self.streams.get(priority_key.id) {
6600 let cap = match stream.send.cap() {
6601 Ok(v) => v,
6602
6603 // Return the stream to the application immediately if it's
6604 // stopped.
6605 Err(_) =>
6606 return {
6607 self.streams.remove_writable(&priority_key);
6608
6609 Some(priority_key.id)
6610 },
6611 };
6612
6613 if cmp::min(self.tx_cap, cap) >= stream.send_lowat {
6614 self.streams.remove_writable(&priority_key);
6615 return Some(priority_key.id);
6616 }
6617 }
6618
6619 cursor.move_next();
6620 }
6621
6622 None
6623 }
6624
6625 /// Returns true if the stream has enough send capacity.
6626 ///
6627 /// When `len` more bytes can be buffered into the given stream's send
6628 /// buffer, `true` will be returned, `false` otherwise.
6629 ///
6630 /// In the latter case, if the additional data can't be buffered due to
6631 /// flow control limits, the peer will also be notified, and a "low send
6632 /// watermark" will be set for the stream, such that it is not going to be
6633 /// reported as writable again by [`stream_writable_next()`] until its send
6634 /// capacity reaches `len`.
6635 ///
6636 /// If the specified stream doesn't exist (including when it has already
6637 /// been completed and closed), the [`InvalidStreamState`] error will be
6638 /// returned.
6639 ///
6640 /// In addition, if the peer has signalled that it doesn't want to receive
6641 /// any more data from this stream by sending the `STOP_SENDING` frame, the
6642 /// [`StreamStopped`] error will be returned.
6643 ///
6644 /// [`stream_writable_next()`]: struct.Connection.html#method.stream_writable_next
6645 /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
6646 /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
6647 #[inline]
6648 pub fn stream_writable(
6649 &mut self, stream_id: u64, len: usize,
6650 ) -> Result<bool> {
6651 if self.stream_capacity(stream_id)? >= len {
6652 return Ok(true);
6653 }
6654
6655 let stream = match self.streams.get_mut(stream_id) {
6656 Some(v) => v,
6657
6658 None => return Err(Error::InvalidStreamState(stream_id)),
6659 };
6660
6661 stream.send_lowat = cmp::max(1, len);
6662
6663 let is_writable = stream.is_writable();
6664
6665 let priority_key = Arc::clone(&stream.priority_key);
6666
6667 if self.max_tx_data - self.tx_data < len as u64 {
6668 self.blocked_limit = Some(self.max_tx_data);
6669 }
6670
6671 if stream.send.cap()? < len {
6672 let max_off = stream.send.max_off();
6673 if stream.send.blocked_at() != Some(max_off) {
6674 stream.send.update_blocked_at(Some(max_off));
6675 self.streams.insert_blocked(stream_id, max_off);
6676 }
6677 } else if is_writable {
6678 // When `stream_writable_next()` returns a stream, the writable
6679 // mark is removed, but because the stream is blocked by the
6680 // connection-level send capacity it won't be marked as writable
6681 // again once the capacity increases.
6682 //
6683 // Since the stream is writable already, mark it here instead.
6684 self.streams.insert_writable(&priority_key);
6685 }
6686
6687 Ok(false)
6688 }
6689
6690 /// Returns true if all the data has been read from the specified stream.
6691 ///
6692 /// This instructs the application that all the data received from the
6693 /// peer on the stream has been read, and there won't be anymore in the
6694 /// future.
6695 ///
6696 /// Basically this returns true when the peer either set the `fin` flag
6697 /// for the stream, or sent `RESET_STREAM`.
6698 #[inline]
6699 pub fn stream_finished(&self, stream_id: u64) -> bool {
6700 let stream = match self.streams.get(stream_id) {
6701 Some(v) => v,
6702
6703 None => return true,
6704 };
6705
6706 stream.recv.is_fin()
6707 }
6708
6709 /// Returns true if the specified stream is closed.
6710 ///
6711 /// For bidirectional streams this happens when both the receive and send
6712 /// sides have signaled `fin`. For unidirectional streams only the
6713 /// relevant direction is checked, depending on whether the stream was
6714 /// created locally or not.
6715 ///
6716 /// This also returns true if the stream has already been collected, but
6717 /// returns false if the stream was never opened.
6718 #[inline]
6719 pub fn stream_closed(&self, stream_id: u64) -> bool {
6720 let Some(stream) = self.streams.get(stream_id) else {
6721 return self.streams.is_collected(stream_id);
6722 };
6723
6724 match (stream.bidi, stream.local) {
6725 // For bidirectional streams both directions must have signaled
6726 // FIN.
6727 (true, _) => stream.recv.is_fin() && stream.send.is_fin(),
6728
6729 // For unidirectional streams created locally, only the send side
6730 // is checked.
6731 (false, true) => stream.send.is_fin(),
6732
6733 // For unidirectional streams created by the peer, only the
6734 // receive side is checked.
6735 (false, false) => stream.recv.is_fin(),
6736 }
6737 }
6738
6739 /// Returns the number of bidirectional streams that can be created
6740 /// before the peer's stream count limit is reached.
6741 ///
6742 /// This can be useful to know if it's possible to create a bidirectional
6743 /// stream without trying it first.
6744 #[inline]
6745 pub fn peer_streams_left_bidi(&self) -> u64 {
6746 self.streams.peer_streams_left_bidi()
6747 }
6748
6749 /// Returns the number of unidirectional streams that can be created
6750 /// before the peer's stream count limit is reached.
6751 ///
6752 /// This can be useful to know if it's possible to create a unidirectional
6753 /// stream without trying it first.
6754 #[inline]
6755 pub fn peer_streams_left_uni(&self) -> u64 {
6756 self.streams.peer_streams_left_uni()
6757 }
6758
6759 /// Returns an iterator over streams that have outstanding data to read.
6760 ///
6761 /// Note that the iterator will only include streams that were readable at
6762 /// the time the iterator itself was created (i.e. when `readable()` was
6763 /// called). To account for newly readable streams, the iterator needs to
6764 /// be created again.
6765 ///
6766 /// ## Examples:
6767 ///
6768 /// ```no_run
6769 /// # let mut buf = [0; 512];
6770 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6771 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6772 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6773 /// # let peer = "127.0.0.1:1234".parse().unwrap();
6774 /// # let local = socket.local_addr().unwrap();
6775 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6776 /// // Iterate over readable streams.
6777 /// for stream_id in conn.readable() {
6778 /// // Stream is readable, read until there's no more data.
6779 /// while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
6780 /// println!("Got {} bytes on stream {}", read, stream_id);
6781 /// }
6782 /// }
6783 /// # Ok::<(), quiche::Error>(())
6784 /// ```
6785 #[inline]
6786 pub fn readable(&self) -> StreamIter {
6787 self.streams.readable()
6788 }
6789
6790 /// Returns an iterator over streams that can be written in priority order.
6791 ///
6792 /// The priority order is based on RFC 9218 scheduling recommendations.
6793 /// Stream priority can be controlled using [`stream_priority()`]. In order
6794 /// to support fairness requirements, each time this method is called,
6795 /// internal state is updated. Therefore the iterator ordering can change
6796 /// between calls, even if no streams were added or removed.
6797 ///
6798 /// A "writable" stream is a stream that has enough flow control capacity to
6799 /// send data to the peer. To avoid buffering an infinite amount of data,
6800 /// streams are only allowed to buffer outgoing data up to the amount that
6801 /// the peer allows to send.
6802 ///
6803 /// Stopped streams are also included so that a write or capacity query can
6804 /// return [`StreamStopped`], even without connection send capacity.
6805 ///
6806 /// Note that the iterator will only include streams that were writable at
6807 /// the time the iterator itself was created (i.e. when `writable()` was
6808 /// called). To account for newly writable streams, the iterator needs to be
6809 /// created again.
6810 ///
6811 /// ## Examples:
6812 ///
6813 /// ```no_run
6814 /// # let mut buf = [0; 512];
6815 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6816 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6817 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6818 /// # let local = socket.local_addr().unwrap();
6819 /// # let peer = "127.0.0.1:1234".parse().unwrap();
6820 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6821 /// // Iterate over writable streams.
6822 /// for stream_id in conn.writable() {
6823 /// // Stream is writable, write some data.
6824 /// if let Ok(written) = conn.stream_send(stream_id, &buf, false) {
6825 /// println!("Written {} bytes on stream {}", written, stream_id);
6826 /// }
6827 /// }
6828 /// # Ok::<(), quiche::Error>(())
6829 /// ```
6830 /// [`stream_priority()`]: struct.Connection.html#method.stream_priority
6831 /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
6832 #[inline]
6833 pub fn writable(&self) -> StreamIter {
6834 // Stopped streams must be reported even without send capacity.
6835 if self.tx_cap == 0 {
6836 return self.streams.stopped_writable();
6837 }
6838
6839 self.streams.writable()
6840 }
6841
6842 /// Returns the maximum possible size of egress UDP payloads.
6843 ///
6844 /// This is the maximum size of UDP payloads that can be sent, and depends
6845 /// on both the configured maximum send payload size of the local endpoint
6846 /// (as configured with [`set_max_send_udp_payload_size()`]), as well as
6847 /// the transport parameter advertised by the remote peer.
6848 ///
6849 /// Note that this value can change during the lifetime of the connection,
6850 /// but should remain stable across consecutive calls to [`send()`].
6851 ///
6852 /// [`set_max_send_udp_payload_size()`]:
6853 /// struct.Config.html#method.set_max_send_udp_payload_size
6854 /// [`send()`]: struct.Connection.html#method.send
6855 pub fn max_send_udp_payload_size(&self) -> usize {
6856 let max_datagram_size = self
6857 .paths
6858 .get_active()
6859 .ok()
6860 .map(|p| p.recovery.max_datagram_size());
6861
6862 if let Some(max_datagram_size) = max_datagram_size {
6863 if self.is_established() {
6864 // Cap the packet size at 16,383 bytes so a two-byte varint can
6865 // always encode it.
6866 return cmp::min(16383, max_datagram_size);
6867 }
6868 }
6869
6870 // Allow for 1200 bytes (minimum QUIC packet size) during the
6871 // handshake.
6872 MIN_CLIENT_INITIAL_LEN
6873 }
6874
6875 /// Schedule an ack-eliciting packet on the active path.
6876 ///
6877 /// QUIC packets might not contain ack-eliciting frames during normal
6878 /// operating conditions. If the packet would already contain
6879 /// ack-eliciting frames, this method does not change any behavior.
6880 /// However, if the packet would not ordinarily contain ack-eliciting
6881 /// frames, this method ensures that a PING frame sent.
6882 ///
6883 /// Calling this method multiple times before [`send()`] has no effect.
6884 ///
6885 /// [`send()`]: struct.Connection.html#method.send
6886 pub fn send_ack_eliciting(&mut self) -> Result<()> {
6887 if self.is_closed() || self.is_draining() {
6888 return Ok(());
6889 }
6890 self.paths.get_active_mut()?.needs_ack_eliciting = true;
6891 Ok(())
6892 }
6893
6894 /// Schedule an ack-eliciting packet on the specified path.
6895 ///
6896 /// See [`send_ack_eliciting()`] for more detail. [`InvalidState`] is
6897 /// returned if there is no record of the path.
6898 ///
6899 /// [`send_ack_eliciting()`]: struct.Connection.html#method.send_ack_eliciting
6900 /// [`InvalidState`]: enum.Error.html#variant.InvalidState
6901 pub fn send_ack_eliciting_on_path(
6902 &mut self, local: SocketAddr, peer: SocketAddr,
6903 ) -> Result<()> {
6904 if self.is_closed() || self.is_draining() {
6905 return Ok(());
6906 }
6907 let path_id = self
6908 .paths
6909 .path_id_from_addrs(&(local, peer))
6910 .ok_or(Error::InvalidState)?;
6911 self.paths.get_mut(path_id)?.needs_ack_eliciting = true;
6912 Ok(())
6913 }
6914
6915 /// Reads the first received DATAGRAM.
6916 ///
6917 /// On success the DATAGRAM's data is returned along with its size.
6918 ///
6919 /// [`Done`] is returned if there is no data to read.
6920 ///
6921 /// [`BufferTooShort`] is returned if the provided buffer is too small for
6922 /// the DATAGRAM.
6923 ///
6924 /// [`Done`]: enum.Error.html#variant.Done
6925 /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
6926 ///
6927 /// ## Examples:
6928 ///
6929 /// ```no_run
6930 /// # let mut buf = [0; 512];
6931 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6932 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6933 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6934 /// # let peer = "127.0.0.1:1234".parse().unwrap();
6935 /// # let local = socket.local_addr().unwrap();
6936 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6937 /// let mut dgram_buf = [0; 512];
6938 /// while let Ok((len)) = conn.dgram_recv(&mut dgram_buf) {
6939 /// println!("Got {} bytes of DATAGRAM", len);
6940 /// }
6941 /// # Ok::<(), quiche::Error>(())
6942 /// ```
6943 #[inline]
6944 pub fn dgram_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
6945 match self.dgram_recv_queue.pop() {
6946 Some(d) => {
6947 if d.as_ref().len() > buf.len() {
6948 return Err(Error::BufferTooShort);
6949 }
6950 let len = d.as_ref().len();
6951
6952 buf[..len].copy_from_slice(d.as_ref());
6953 Ok(len)
6954 },
6955
6956 None => Err(Error::Done),
6957 }
6958 }
6959
6960 /// Reads the first received DATAGRAM.
6961 ///
6962 /// This is the same as [`dgram_recv()`] but returns the DATAGRAM as an
6963 /// owned buffer instead of copying into the provided buffer.
6964 ///
6965 /// [`dgram_recv()`]: struct.Connection.html#method.dgram_recv
6966 #[inline]
6967 pub fn dgram_recv_buf(&mut self) -> Result<F::DgramBuf> {
6968 self.dgram_recv_queue.pop().ok_or(Error::Done)
6969 }
6970
6971 /// Reads the first received DATAGRAM without removing it from the queue.
6972 ///
6973 /// On success the DATAGRAM's data is returned along with the actual number
6974 /// of bytes peeked. The requested length cannot exceed the DATAGRAM's
6975 /// actual length.
6976 ///
6977 /// [`Done`] is returned if there is no data to read.
6978 ///
6979 /// [`BufferTooShort`] is returned if the provided buffer is smaller the
6980 /// number of bytes to peek.
6981 ///
6982 /// [`Done`]: enum.Error.html#variant.Done
6983 /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
6984 #[inline]
6985 pub fn dgram_recv_peek(&self, buf: &mut [u8], len: usize) -> Result<usize> {
6986 self.dgram_recv_queue.peek_front_bytes(buf, len)
6987 }
6988
6989 /// Returns the length of the first stored DATAGRAM.
6990 #[inline]
6991 pub fn dgram_recv_front_len(&self) -> Option<usize> {
6992 self.dgram_recv_queue.peek_front_len()
6993 }
6994
6995 /// Returns the number of items in the DATAGRAM receive queue.
6996 #[inline]
6997 pub fn dgram_recv_queue_len(&self) -> usize {
6998 self.dgram_recv_queue.len()
6999 }
7000
7001 /// Returns the total size of all items in the DATAGRAM receive queue.
7002 #[inline]
7003 pub fn dgram_recv_queue_byte_size(&self) -> usize {
7004 self.dgram_recv_queue.byte_size()
7005 }
7006
7007 /// Returns the number of items in the DATAGRAM send queue.
7008 #[inline]
7009 pub fn dgram_send_queue_len(&self) -> usize {
7010 self.dgram_send_queue.len()
7011 }
7012
7013 /// Returns the total size of all items in the DATAGRAM send queue.
7014 #[inline]
7015 pub fn dgram_send_queue_byte_size(&self) -> usize {
7016 self.dgram_send_queue.byte_size()
7017 }
7018
7019 /// Returns whether or not the DATAGRAM send queue is full.
7020 #[inline]
7021 pub fn is_dgram_send_queue_full(&self) -> bool {
7022 self.dgram_send_queue.is_full()
7023 }
7024
7025 /// Returns whether or not the DATAGRAM recv queue is full.
7026 #[inline]
7027 pub fn is_dgram_recv_queue_full(&self) -> bool {
7028 self.dgram_recv_queue.is_full()
7029 }
7030
7031 /// Sends data in a DATAGRAM frame.
7032 ///
7033 /// [`Done`] is returned if no data was written.
7034 /// [`InvalidState`] is returned if the peer does not support DATAGRAM.
7035 /// [`BufferTooShort`] is returned if the DATAGRAM frame length is larger
7036 /// than peer's supported DATAGRAM frame length. Use
7037 /// [`dgram_max_writable_len()`] to get the largest supported DATAGRAM
7038 /// frame length.
7039 ///
7040 /// Note that there is no flow control of DATAGRAM frames, so in order to
7041 /// avoid buffering an infinite amount of frames we apply an internal
7042 /// limit.
7043 ///
7044 /// [`Done`]: enum.Error.html#variant.Done
7045 /// [`InvalidState`]: enum.Error.html#variant.InvalidState
7046 /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
7047 /// [`dgram_max_writable_len()`]:
7048 /// struct.Connection.html#method.dgram_max_writable_len
7049 ///
7050 /// ## Examples:
7051 ///
7052 /// ```no_run
7053 /// # let mut buf = [0; 512];
7054 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
7055 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
7056 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
7057 /// # let peer = "127.0.0.1:1234".parse().unwrap();
7058 /// # let local = socket.local_addr().unwrap();
7059 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
7060 /// conn.dgram_send(b"hello")?;
7061 /// # Ok::<(), quiche::Error>(())
7062 /// ```
7063 pub fn dgram_send(&mut self, buf: &[u8]) -> Result<()> {
7064 self.dgram_send_buf(F::dgram_buf_from_slice(buf))
7065 }
7066
7067 /// Sends data in a DATAGRAM frame.
7068 ///
7069 /// This is the same as [`dgram_send()`] but takes an owned buffer
7070 /// instead of a slice and avoids copying.
7071 ///
7072 /// [`dgram_send()`]: struct.Connection.html#method.dgram_send
7073 pub fn dgram_send_buf(&mut self, buf: F::DgramBuf) -> Result<()> {
7074 let max_payload_len = match self.dgram_max_writable_len() {
7075 Some(v) => v,
7076
7077 None => return Err(Error::InvalidState),
7078 };
7079
7080 if buf.as_ref().len() > max_payload_len {
7081 return Err(Error::BufferTooShort);
7082 }
7083
7084 self.dgram_send_queue.push(buf)?;
7085
7086 let active_path = self.paths.get_active_mut()?;
7087
7088 if self.dgram_send_queue.byte_size() >
7089 active_path.recovery.cwnd_available()
7090 {
7091 active_path.recovery.update_app_limited(false);
7092 }
7093
7094 Ok(())
7095 }
7096
7097 /// Purges queued outgoing DATAGRAMs matching the predicate.
7098 ///
7099 /// In other words, remove all elements `e` such that `f(&e)` returns true.
7100 ///
7101 /// ## Examples:
7102 /// ```no_run
7103 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
7104 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
7105 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
7106 /// # let peer = "127.0.0.1:1234".parse().unwrap();
7107 /// # let local = socket.local_addr().unwrap();
7108 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
7109 /// conn.dgram_send(b"hello")?;
7110 /// conn.dgram_purge_outgoing(&|d: &[u8]| -> bool { d[0] == 0 });
7111 /// # Ok::<(), quiche::Error>(())
7112 /// ```
7113 #[inline]
7114 pub fn dgram_purge_outgoing<FN: Fn(&[u8]) -> bool>(&mut self, f: FN) {
7115 self.dgram_send_queue.purge(f);
7116 }
7117
7118 /// Returns the maximum DATAGRAM payload that can be sent.
7119 ///
7120 /// [`None`] is returned if the peer hasn't advertised a maximum DATAGRAM
7121 /// frame size.
7122 ///
7123 /// ## Examples:
7124 ///
7125 /// ```no_run
7126 /// # let mut buf = [0; 512];
7127 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
7128 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
7129 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
7130 /// # let peer = "127.0.0.1:1234".parse().unwrap();
7131 /// # let local = socket.local_addr().unwrap();
7132 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
7133 /// if let Some(payload_size) = conn.dgram_max_writable_len() {
7134 /// if payload_size > 5 {
7135 /// conn.dgram_send(b"hello")?;
7136 /// }
7137 /// }
7138 /// # Ok::<(), quiche::Error>(())
7139 /// ```
7140 #[inline]
7141 pub fn dgram_max_writable_len(&self) -> Option<usize> {
7142 match self.peer_transport_params.max_datagram_frame_size {
7143 None => None,
7144 Some(peer_frame_len) => {
7145 let dcid = self.destination_id();
7146 // Start from the maximum packet size...
7147 let mut max_len = self.max_send_udp_payload_size();
7148 // ...subtract the Short packet header overhead...
7149 // (1 byte of pkt_len + len of dcid)
7150 max_len = max_len.saturating_sub(1 + dcid.len());
7151 // ...subtract the packet number (max len)...
7152 max_len = max_len.saturating_sub(packet::MAX_PKT_NUM_LEN);
7153 // ...subtract the crypto overhead...
7154 max_len = max_len.saturating_sub(
7155 self.crypto_ctx[packet::Epoch::Application]
7156 .crypto_overhead()?,
7157 );
7158 // ...clamp to what peer can support...
7159 max_len = cmp::min(peer_frame_len as usize, max_len);
7160 // ...subtract frame overhead, checked for underflow.
7161 // (1 byte of frame type + len of length )
7162 max_len.checked_sub(1 + frame::MAX_DGRAM_OVERHEAD)
7163 },
7164 }
7165 }
7166
7167 fn dgram_enabled(&self) -> bool {
7168 self.local_transport_params
7169 .max_datagram_frame_size
7170 .is_some()
7171 }
7172
7173 /// Returns when the next timeout event will occur.
7174 ///
7175 /// Once the timeout Instant has been reached, the [`on_timeout()`] method
7176 /// should be called. A timeout of `None` means that the timer should be
7177 /// disarmed.
7178 ///
7179 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
7180 pub fn timeout_instant(&self) -> Option<Instant> {
7181 if self.is_closed() {
7182 return None;
7183 }
7184
7185 if self.is_draining() {
7186 // Draining timer takes precedence over all other timers. If it is
7187 // set it means the connection is closing so there's no point in
7188 // processing the other timers.
7189 self.draining_timer
7190 } else {
7191 // Use the lowest timer value (i.e. "sooner") among idle and loss
7192 // detection timers. If they are both unset (i.e. `None`) then the
7193 // result is `None`, but if at least one of them is set then a
7194 // `Some(...)` value is returned.
7195 let path_timer = self
7196 .paths
7197 .iter()
7198 .filter_map(|(_, p)| p.recovery.loss_detection_timer())
7199 .min();
7200
7201 let key_update_timer = self.crypto_ctx[packet::Epoch::Application]
7202 .key_update
7203 .as_ref()
7204 .map(|key_update| key_update.timer);
7205
7206 let timers = [self.idle_timer, path_timer, key_update_timer];
7207
7208 timers.iter().filter_map(|&x| x).min()
7209 }
7210 }
7211
7212 /// Returns the amount of time until the next timeout event.
7213 ///
7214 /// Once the given duration has elapsed, the [`on_timeout()`] method should
7215 /// be called. A timeout of `None` means that the timer should be disarmed.
7216 ///
7217 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
7218 pub fn timeout(&self) -> Option<Duration> {
7219 self.timeout_instant().map(|timeout| {
7220 let now = Instant::now();
7221
7222 if timeout <= now {
7223 Duration::ZERO
7224 } else {
7225 timeout.duration_since(now)
7226 }
7227 })
7228 }
7229
7230 /// Processes a timeout event.
7231 ///
7232 /// If no timeout has occurred it does nothing.
7233 pub fn on_timeout(&mut self) {
7234 let now = Instant::now();
7235
7236 if let Some(draining_timer) = self.draining_timer {
7237 if draining_timer <= now {
7238 trace!("{} draining timeout expired", self.trace_id);
7239
7240 self.mark_closed();
7241 }
7242
7243 // Draining timer takes precedence over all other timers. If it is
7244 // set it means the connection is closing so there's no point in
7245 // processing the other timers.
7246 return;
7247 }
7248
7249 if let Some(timer) = self.idle_timer {
7250 if timer <= now {
7251 trace!("{} idle timeout expired", self.trace_id);
7252
7253 self.mark_closed();
7254 self.timed_out = true;
7255 return;
7256 }
7257 }
7258
7259 if let Some(timer) = self.crypto_ctx[packet::Epoch::Application]
7260 .key_update
7261 .as_ref()
7262 .map(|key_update| key_update.timer)
7263 {
7264 if timer <= now {
7265 // Discard previous key once key update timer expired.
7266 let _ = self.crypto_ctx[packet::Epoch::Application]
7267 .key_update
7268 .take();
7269 }
7270 }
7271
7272 let handshake_status = self.handshake_status();
7273
7274 for (_, p) in self.paths.iter_mut() {
7275 if let Some(timer) = p.recovery.loss_detection_timer() {
7276 if timer <= now {
7277 trace!("{} loss detection timeout expired", self.trace_id);
7278
7279 let OnLossDetectionTimeoutOutcome {
7280 lost_packets,
7281 lost_bytes,
7282 } = p.on_loss_detection_timeout(
7283 handshake_status,
7284 now,
7285 self.is_server,
7286 &self.trace_id,
7287 );
7288
7289 self.lost_count += lost_packets;
7290 self.lost_bytes += lost_bytes as u64;
7291
7292 qlog_with_type!(QLOG_METRICS, self.qlog, q, {
7293 p.recovery.maybe_qlog(q, now);
7294 });
7295 }
7296 }
7297 }
7298
7299 // Notify timeout events to the application.
7300 self.paths.notify_failed_validations();
7301
7302 // If the active path failed, try to find a new candidate.
7303 if self.paths.get_active_path_id().is_err() {
7304 match self.paths.find_candidate_path() {
7305 Some(pid) => {
7306 if self.set_active_path(pid, now).is_err() {
7307 // The connection cannot continue.
7308 self.mark_closed();
7309 }
7310 },
7311
7312 // The connection cannot continue.
7313 None => {
7314 self.mark_closed();
7315 },
7316 }
7317 }
7318 }
7319
7320 /// Requests the stack to perform path validation of the proposed 4-tuple.
7321 ///
7322 /// Probing new paths requires spare Connection IDs at both the host and the
7323 /// peer sides. If it is not the case, it raises an [`OutOfIdentifiers`].
7324 ///
7325 /// The probing of new addresses can only be done by the client. The server
7326 /// can only probe network paths that were previously advertised by
7327 /// [`PathEvent::New`]. If the server tries to probe such an unseen network
7328 /// path, this call raises an [`InvalidState`].
7329 ///
7330 /// The caller might also want to probe an existing path. In such case, it
7331 /// triggers a PATH_CHALLENGE frame, but it does not require spare CIDs.
7332 ///
7333 /// A server always probes a new path it observes. Calling this method is
7334 /// hence not required to validate a new path. However, a server can still
7335 /// request an additional path validation of the proposed 4-tuple.
7336 ///
7337 /// Calling this method several times before calling [`send()`] or
7338 /// [`send_on_path()`] results in a single probe being generated. An
7339 /// application wanting to send multiple in-flight probes must call this
7340 /// method again after having sent packets.
7341 ///
7342 /// Returns the Destination Connection ID sequence number associated to that
7343 /// path.
7344 ///
7345 /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
7346 /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
7347 /// [`InvalidState`]: enum.Error.html#InvalidState
7348 /// [`send()`]: struct.Connection.html#method.send
7349 /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
7350 pub fn probe_path(
7351 &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
7352 ) -> Result<u64> {
7353 // We may want to probe an existing path.
7354 let pid = match self.paths.path_id_from_addrs(&(local_addr, peer_addr)) {
7355 Some(pid) => pid,
7356 None => self.create_path_on_client(local_addr, peer_addr)?,
7357 };
7358
7359 let path = self.paths.get_mut(pid)?;
7360 path.request_validation();
7361
7362 path.active_dcid_seq.ok_or(Error::InvalidState)
7363 }
7364
7365 /// Migrates the connection to a new local address `local_addr`.
7366 ///
7367 /// The behavior is similar to [`migrate()`], with the nuance that the
7368 /// connection only changes the local address, but not the peer one.
7369 ///
7370 /// See [`migrate()`] for the full specification of this method.
7371 ///
7372 /// [`migrate()`]: struct.Connection.html#method.migrate
7373 pub fn migrate_source(&mut self, local_addr: SocketAddr) -> Result<u64> {
7374 let peer_addr = self.paths.get_active()?.peer_addr();
7375 self.migrate(local_addr, peer_addr)
7376 }
7377
7378 /// Migrates the connection over the given network path between `local_addr`
7379 /// and `peer_addr`.
7380 ///
7381 /// Connection migration can only be initiated by the client. Calling this
7382 /// method as a server returns [`InvalidState`].
7383 ///
7384 /// To initiate voluntary migration, there should be enough Connection IDs
7385 /// at both sides. If this requirement is not satisfied, this call returns
7386 /// [`OutOfIdentifiers`].
7387 ///
7388 /// Returns the Destination Connection ID associated to that migrated path.
7389 ///
7390 /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
7391 /// [`InvalidState`]: enum.Error.html#InvalidState
7392 pub fn migrate(
7393 &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
7394 ) -> Result<u64> {
7395 if self.is_server {
7396 return Err(Error::InvalidState);
7397 }
7398
7399 // If the path already exists, mark it as the active one.
7400 let (pid, dcid_seq) = if let Some(pid) =
7401 self.paths.path_id_from_addrs(&(local_addr, peer_addr))
7402 {
7403 let path = self.paths.get_mut(pid)?;
7404
7405 // If it is already active, do nothing.
7406 if path.active() {
7407 return path.active_dcid_seq.ok_or(Error::OutOfIdentifiers);
7408 }
7409
7410 // Ensures that a Source Connection ID has been dedicated to this
7411 // path, or a free one is available. This is only required if the
7412 // host uses non-zero length Source Connection IDs.
7413 if !self.ids.zero_length_scid() &&
7414 path.active_scid_seq.is_none() &&
7415 self.ids.available_scids() == 0
7416 {
7417 return Err(Error::OutOfIdentifiers);
7418 }
7419
7420 // Ensures that the migrated path has a Destination Connection ID.
7421 let dcid_seq = if let Some(dcid_seq) = path.active_dcid_seq {
7422 dcid_seq
7423 } else {
7424 let dcid_seq = self
7425 .ids
7426 .lowest_available_dcid_seq()
7427 .ok_or(Error::OutOfIdentifiers)?;
7428
7429 self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
7430 path.active_dcid_seq = Some(dcid_seq);
7431
7432 dcid_seq
7433 };
7434
7435 (pid, dcid_seq)
7436 } else {
7437 let pid = self.create_path_on_client(local_addr, peer_addr)?;
7438
7439 let dcid_seq = self
7440 .paths
7441 .get(pid)?
7442 .active_dcid_seq
7443 .ok_or(Error::InvalidState)?;
7444
7445 (pid, dcid_seq)
7446 };
7447
7448 // Change the active path.
7449 self.set_active_path(pid, Instant::now())?;
7450
7451 Ok(dcid_seq)
7452 }
7453
7454 /// Provides additional source Connection IDs that the peer can use to reach
7455 /// this host.
7456 ///
7457 /// This triggers sending NEW_CONNECTION_ID frames if the provided Source
7458 /// Connection ID is not already present. In the case the caller tries to
7459 /// reuse a Connection ID with a different reset token, this raises an
7460 /// `InvalidState`.
7461 ///
7462 /// At any time, the peer cannot have more Destination Connection IDs than
7463 /// the maximum number of active Connection IDs it negotiated. In such case
7464 /// (i.e., when [`scids_left()`] returns 0), if the host agrees to
7465 /// request the removal of previous connection IDs, it sets the
7466 /// `retire_if_needed` parameter. Otherwise, an [`IdLimit`] is returned.
7467 ///
7468 /// Note that setting `retire_if_needed` does not prevent this function from
7469 /// returning an [`IdLimit`] in the case the caller wants to retire still
7470 /// unannounced Connection IDs.
7471 ///
7472 /// The caller is responsible for ensuring that the provided `scid` is not
7473 /// repeated several times over the connection. quiche ensures that as long
7474 /// as the provided Connection ID is still in use (i.e., not retired), it
7475 /// does not assign a different sequence number.
7476 ///
7477 /// Note that if the host uses zero-length Source Connection IDs, it cannot
7478 /// advertise Source Connection IDs and calling this method returns an
7479 /// [`InvalidState`].
7480 ///
7481 /// Returns the sequence number associated to the provided Connection ID.
7482 ///
7483 /// [`scids_left()`]: struct.Connection.html#method.scids_left
7484 /// [`IdLimit`]: enum.Error.html#IdLimit
7485 /// [`InvalidState`]: enum.Error.html#InvalidState
7486 pub fn new_scid(
7487 &mut self, scid: &ConnectionId, reset_token: u128, retire_if_needed: bool,
7488 ) -> Result<u64> {
7489 self.ids.new_scid(
7490 scid.to_vec().into(),
7491 Some(reset_token),
7492 true,
7493 None,
7494 retire_if_needed,
7495 )
7496 }
7497
7498 /// Returns the number of source Connection IDs that are active. This is
7499 /// only meaningful if the host uses non-zero length Source Connection IDs.
7500 pub fn active_scids(&self) -> usize {
7501 self.ids.active_source_cids()
7502 }
7503
7504 /// Returns the number of additional source Connection IDs that can be
7505 /// provided to the peer without exceeding the limit it advertised.
7506 ///
7507 /// The limit is the minimum of the locally configured active connection
7508 /// ID limit and the one sent by the peer.
7509 ///
7510 /// Returns `0` when the peer's limit is already reached or temporarily
7511 /// exceeded (e.g. during a SCID rotation where a retirement is in
7512 /// flight and `active_scids()` transiently exceeds the advertised
7513 /// limit).
7514 ///
7515 /// To obtain the maximum possible value allowed by the peer an application
7516 /// can instead inspect the [`peer_active_conn_id_limit`] value.
7517 ///
7518 /// [`peer_active_conn_id_limit`]: struct.Stats.html#structfield.peer_active_conn_id_limit
7519 #[inline]
7520 pub fn scids_left(&self) -> usize {
7521 let max_active_source_cids = cmp::min(
7522 self.peer_transport_params.active_conn_id_limit,
7523 self.local_transport_params.active_conn_id_limit,
7524 ) as usize;
7525
7526 max_active_source_cids.saturating_sub(self.active_scids())
7527 }
7528
7529 /// Requests the retirement of the destination Connection ID used by the
7530 /// host to reach its peer.
7531 ///
7532 /// This triggers sending RETIRE_CONNECTION_ID frames.
7533 ///
7534 /// If the application tries to retire a non-existing Destination Connection
7535 /// ID sequence number, or if it uses zero-length Destination Connection ID,
7536 /// this method returns an [`InvalidState`].
7537 ///
7538 /// At any time, the host must have at least one Destination ID. If the
7539 /// application tries to retire the last one, or if the caller tries to
7540 /// retire the destination Connection ID used by the current active path
7541 /// while having neither spare Destination Connection IDs nor validated
7542 /// network paths, this method returns an [`OutOfIdentifiers`]. This
7543 /// behavior prevents the caller from stalling the connection due to the
7544 /// lack of validated path to send non-probing packets.
7545 ///
7546 /// [`InvalidState`]: enum.Error.html#InvalidState
7547 /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
7548 pub fn retire_dcid(&mut self, dcid_seq: u64) -> Result<()> {
7549 if self.ids.zero_length_dcid() {
7550 return Err(Error::InvalidState);
7551 }
7552
7553 let active_path_dcid_seq = self
7554 .paths
7555 .get_active()?
7556 .active_dcid_seq
7557 .ok_or(Error::InvalidState)?;
7558
7559 let active_path_id = self.paths.get_active_path_id()?;
7560
7561 if active_path_dcid_seq == dcid_seq &&
7562 self.ids.lowest_available_dcid_seq().is_none() &&
7563 !self
7564 .paths
7565 .iter()
7566 .any(|(pid, p)| pid != active_path_id && p.usable())
7567 {
7568 return Err(Error::OutOfIdentifiers);
7569 }
7570
7571 if let Some(pid) = self.ids.retire_dcid(dcid_seq)? {
7572 // The retired Destination CID was associated to a given path. Let's
7573 // find an available DCID to associate to that path.
7574 let path = self.paths.get_mut(pid)?;
7575 let dcid_seq = self.ids.lowest_available_dcid_seq();
7576
7577 if let Some(dcid_seq) = dcid_seq {
7578 self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
7579 }
7580
7581 path.active_dcid_seq = dcid_seq;
7582 }
7583
7584 Ok(())
7585 }
7586
7587 /// Processes path-specific events.
7588 ///
7589 /// On success it returns a [`PathEvent`], or `None` when there are no
7590 /// events to report. Please refer to [`PathEvent`] for the exhaustive event
7591 /// list.
7592 ///
7593 /// Note that all events are edge-triggered, meaning that once reported they
7594 /// will not be reported again by calling this method again, until the event
7595 /// is re-armed.
7596 ///
7597 /// [`PathEvent`]: enum.PathEvent.html
7598 pub fn path_event_next(&mut self) -> Option<PathEvent> {
7599 self.paths.pop_event()
7600 }
7601
7602 /// Returns the number of source Connection IDs that are retired.
7603 pub fn retired_scids(&self) -> usize {
7604 self.ids.retired_source_cids()
7605 }
7606
7607 /// Returns a source `ConnectionId` that has been retired.
7608 ///
7609 /// On success it returns a [`ConnectionId`], or `None` when there are no
7610 /// more retired connection IDs.
7611 ///
7612 /// [`ConnectionId`]: struct.ConnectionId.html
7613 pub fn retired_scid_next(&mut self) -> Option<ConnectionId<'static>> {
7614 self.ids.pop_retired_scid()
7615 }
7616
7617 /// Returns the number of spare Destination Connection IDs, i.e.,
7618 /// Destination Connection IDs that are still unused.
7619 ///
7620 /// Note that this function returns 0 if the host uses zero length
7621 /// Destination Connection IDs.
7622 pub fn available_dcids(&self) -> usize {
7623 self.ids.available_dcids()
7624 }
7625
7626 /// Returns an iterator over destination `SockAddr`s whose association
7627 /// with `from` forms a known QUIC path on which packets can be sent to.
7628 ///
7629 /// This function is typically used in combination with [`send_on_path()`].
7630 ///
7631 /// Note that the iterator includes all the possible combination of
7632 /// destination `SockAddr`s, even those whose sending is not required now.
7633 /// In other words, this is another way for the application to recall from
7634 /// past [`PathEvent::New`] events.
7635 ///
7636 /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
7637 /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
7638 ///
7639 /// ## Examples:
7640 ///
7641 /// ```no_run
7642 /// # let mut out = [0; 512];
7643 /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
7644 /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
7645 /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
7646 /// # let local = socket.local_addr().unwrap();
7647 /// # let peer = "127.0.0.1:1234".parse().unwrap();
7648 /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
7649 /// // Iterate over possible destinations for the given local `SockAddr`.
7650 /// for dest in conn.paths_iter(local) {
7651 /// loop {
7652 /// let (write, send_info) =
7653 /// match conn.send_on_path(&mut out, Some(local), Some(dest)) {
7654 /// Ok(v) => v,
7655 ///
7656 /// Err(quiche::Error::Done) => {
7657 /// // Done writing for this destination.
7658 /// break;
7659 /// },
7660 ///
7661 /// Err(e) => {
7662 /// // An error occurred, handle it.
7663 /// break;
7664 /// },
7665 /// };
7666 ///
7667 /// socket.send_to(&out[..write], &send_info.to).unwrap();
7668 /// }
7669 /// }
7670 /// # Ok::<(), quiche::Error>(())
7671 /// ```
7672 #[inline]
7673 pub fn paths_iter(&self, from: SocketAddr) -> SocketAddrIter {
7674 // Instead of trying to identify whether packets will be sent on the
7675 // given 4-tuple, simply filter paths that cannot be used.
7676 SocketAddrIter {
7677 sockaddrs: self
7678 .paths
7679 .iter()
7680 .filter(|(_, p)| p.active_dcid_seq.is_some())
7681 .filter(|(_, p)| p.usable() || p.probing_required())
7682 .filter(|(_, p)| p.local_addr() == from)
7683 .map(|(_, p)| p.peer_addr())
7684 .collect(),
7685
7686 index: 0,
7687 }
7688 }
7689
7690 /// Closes the connection with the given error and reason.
7691 ///
7692 /// The `app` parameter specifies whether an application close should be
7693 /// sent to the peer. Otherwise a normal connection close is sent.
7694 ///
7695 /// If `app` is true but the connection is not in a state that is safe to
7696 /// send an application error (not established nor in early data), in
7697 /// accordance with [RFC
7698 /// 9000](https://www.rfc-editor.org/rfc/rfc9000.html#section-10.2.3-3), the
7699 /// error code is changed to APPLICATION_ERROR and the reason phrase is
7700 /// cleared.
7701 ///
7702 /// Returns [`Done`] if the connection had already been closed.
7703 ///
7704 /// Note that the connection will not be closed immediately. An application
7705 /// should continue calling the [`recv()`], [`send()`], [`timeout()`] and
7706 /// [`on_timeout()`] methods as normal, until the [`is_closed()`] method
7707 /// returns `true`.
7708 ///
7709 /// [`Done`]: enum.Error.html#variant.Done
7710 /// [`recv()`]: struct.Connection.html#method.recv
7711 /// [`send()`]: struct.Connection.html#method.send
7712 /// [`timeout()`]: struct.Connection.html#method.timeout
7713 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
7714 /// [`is_closed()`]: struct.Connection.html#method.is_closed
7715 pub fn close(&mut self, app: bool, err: u64, reason: &[u8]) -> Result<()> {
7716 if self.is_closed() || self.is_draining() {
7717 return Err(Error::Done);
7718 }
7719
7720 if self.local_error.is_some() {
7721 return Err(Error::Done);
7722 }
7723
7724 let is_safe_to_send_app_data =
7725 self.is_established() || self.is_in_early_data();
7726
7727 if app && !is_safe_to_send_app_data {
7728 // Clear error information.
7729 self.local_error = Some(ConnectionError {
7730 is_app: false,
7731 error_code: 0x0c,
7732 reason: vec![],
7733 });
7734 } else {
7735 self.local_error = Some(ConnectionError {
7736 is_app: app,
7737 error_code: err,
7738 reason: reason.to_vec(),
7739 });
7740 }
7741
7742 // Close immediately if no packet was processed successfully.
7743 if self.recv_count == 0 {
7744 self.mark_closed();
7745 }
7746
7747 Ok(())
7748 }
7749
7750 /// Returns a string uniquely representing the connection.
7751 ///
7752 /// This can be used for logging purposes to differentiate between multiple
7753 /// connections.
7754 #[inline]
7755 pub fn trace_id(&self) -> &str {
7756 &self.trace_id
7757 }
7758
7759 /// Returns the negotiated ALPN protocol.
7760 ///
7761 /// If no protocol has been negotiated, the returned value is empty.
7762 #[inline]
7763 pub fn application_proto(&self) -> &[u8] {
7764 self.alpn.as_ref()
7765 }
7766
7767 /// Returns the server name requested by the client.
7768 #[inline]
7769 pub fn server_name(&self) -> Option<&str> {
7770 self.handshake.server_name()
7771 }
7772
7773 /// Returns the peer's leaf certificate (if any) as a DER-encoded buffer.
7774 #[inline]
7775 pub fn peer_cert(&self) -> Option<&[u8]> {
7776 self.handshake.peer_cert()
7777 }
7778
7779 /// Returns the peer's certificate chain (if any) as a vector of DER-encoded
7780 /// buffers.
7781 ///
7782 /// The certificate at index 0 is the peer's leaf certificate, the other
7783 /// certificates (if any) are the chain certificate authorities used to
7784 /// sign the leaf certificate.
7785 #[inline]
7786 pub fn peer_cert_chain(&self) -> Option<Vec<&[u8]>> {
7787 self.handshake.peer_cert_chain()
7788 }
7789
7790 /// Returns the serialized cryptographic session for the connection.
7791 ///
7792 /// This can be used by a client to cache a connection's session, and resume
7793 /// it later using the [`set_session()`] method.
7794 ///
7795 /// [`set_session()`]: struct.Connection.html#method.set_session
7796 #[inline]
7797 pub fn session(&self) -> Option<&[u8]> {
7798 self.session.as_deref()
7799 }
7800
7801 /// Returns the source connection ID.
7802 ///
7803 /// When there are multiple IDs, and if there is an active path, the ID used
7804 /// on that path is returned. Otherwise the oldest ID is returned.
7805 ///
7806 /// Note that the value returned can change throughout the connection's
7807 /// lifetime.
7808 #[inline]
7809 pub fn source_id(&self) -> ConnectionId<'_> {
7810 if let Ok(path) = self.paths.get_active() {
7811 if let Some(active_scid_seq) = path.active_scid_seq {
7812 if let Ok(e) = self.ids.get_scid(active_scid_seq) {
7813 return ConnectionId::from_ref(e.cid.as_ref());
7814 }
7815 }
7816 }
7817
7818 let e = self.ids.oldest_scid();
7819 ConnectionId::from_ref(e.cid.as_ref())
7820 }
7821
7822 /// Returns all active source connection IDs.
7823 ///
7824 /// An iterator is returned for all active IDs (i.e. ones that have not
7825 /// been explicitly retired yet).
7826 #[inline]
7827 pub fn source_ids(&self) -> impl Iterator<Item = &ConnectionId<'_>> {
7828 self.ids.scids_iter()
7829 }
7830
7831 /// Returns the destination connection ID.
7832 ///
7833 /// Note that the value returned can change throughout the connection's
7834 /// lifetime.
7835 #[inline]
7836 pub fn destination_id(&self) -> ConnectionId<'_> {
7837 if let Ok(path) = self.paths.get_active() {
7838 if let Some(active_dcid_seq) = path.active_dcid_seq {
7839 if let Ok(e) = self.ids.get_dcid(active_dcid_seq) {
7840 return ConnectionId::from_ref(e.cid.as_ref());
7841 }
7842 }
7843 }
7844
7845 let e = self.ids.oldest_dcid();
7846 ConnectionId::from_ref(e.cid.as_ref())
7847 }
7848
7849 /// Returns the PMTU for the active path if it exists.
7850 ///
7851 /// This requires no additonal packets to be sent but simply checks if PMTUD
7852 /// has completed and has found a valid PMTU.
7853 #[inline]
7854 pub fn pmtu(&self) -> Option<usize> {
7855 if let Ok(path) = self.paths.get_active() {
7856 path.pmtud.as_ref().and_then(|pmtud| pmtud.get_pmtu())
7857 } else {
7858 None
7859 }
7860 }
7861
7862 /// Revalidates the PMTU for the active path by sending a new probe packet
7863 /// of PMTU size. If the probe is dropped PMTUD will restart and find a new
7864 /// valid PMTU.
7865 ///
7866 /// If revalidation invalidates a previously discovered larger size, a
7867 /// [`PathEvent::PmtuUpdated`] event is queued with QUIC's minimum packet
7868 /// size. Further events report larger sizes as probes validate them.
7869 #[inline]
7870 pub fn revalidate_pmtu(&mut self) {
7871 let Ok(active_path) = self.paths.get_active_mut() else {
7872 return;
7873 };
7874
7875 let local = active_path.local_addr();
7876 let peer = active_path.peer_addr();
7877 let Some(pmtud) = active_path.pmtud.as_mut() else {
7878 return;
7879 };
7880
7881 let old_pmtu = pmtud.get_current_mtu();
7882 pmtud.revalidate_pmtu();
7883
7884 let Some(event) =
7885 path::pmtu_event(local, peer, old_pmtu, pmtud.get_current_mtu())
7886 else {
7887 return;
7888 };
7889
7890 self.paths.notify_event(event);
7891 }
7892
7893 /// Returns true if the connection handshake is complete.
7894 #[inline]
7895 pub fn is_established(&self) -> bool {
7896 self.handshake_completed
7897 }
7898
7899 /// Returns true if the connection is resumed.
7900 #[inline]
7901 pub fn is_resumed(&self) -> bool {
7902 self.handshake.is_resumed()
7903 }
7904
7905 /// Returns true if the connection has a pending handshake that has
7906 /// progressed enough to send or receive early data.
7907 #[inline]
7908 pub fn is_in_early_data(&self) -> bool {
7909 self.handshake.is_in_early_data()
7910 }
7911
7912 /// Returns the early data reason for the connection.
7913 ///
7914 /// This status can be useful for logging and debugging. See [BoringSSL]
7915 /// documentation for a definition of the reasons.
7916 ///
7917 /// [BoringSSL]: https://commondatastorage.googleapis.com/chromium-boringssl-docs/ssl.h.html#ssl_early_data_reason_t
7918 #[inline]
7919 pub fn early_data_reason(&self) -> u32 {
7920 self.handshake.early_data_reason()
7921 }
7922
7923 /// Returns whether there is stream or DATAGRAM data available to read.
7924 #[inline]
7925 pub fn is_readable(&self) -> bool {
7926 self.streams.has_readable() || self.dgram_recv_front_len().is_some()
7927 }
7928
7929 /// Returns whether the network path with local address `from` and remote
7930 /// address `peer` has been validated.
7931 ///
7932 /// If the 4-tuple does not exist over the connection, returns an
7933 /// [`InvalidState`].
7934 ///
7935 /// [`InvalidState`]: enum.Error.html#variant.InvalidState
7936 pub fn is_path_validated(
7937 &self, from: SocketAddr, to: SocketAddr,
7938 ) -> Result<bool> {
7939 let pid = self
7940 .paths
7941 .path_id_from_addrs(&(from, to))
7942 .ok_or(Error::InvalidState)?;
7943
7944 Ok(self.paths.get(pid)?.validated())
7945 }
7946
7947 /// Returns true if the connection is draining.
7948 ///
7949 /// If this returns `true`, the connection object cannot yet be dropped, but
7950 /// no new application data can be sent or received. An application should
7951 /// continue calling the [`recv()`], [`timeout()`], and [`on_timeout()`]
7952 /// methods as normal, until the [`is_closed()`] method returns `true`.
7953 ///
7954 /// In contrast, once `is_draining()` returns `true`, calling [`send()`]
7955 /// is not required because no new outgoing packets will be generated.
7956 ///
7957 /// [`recv()`]: struct.Connection.html#method.recv
7958 /// [`send()`]: struct.Connection.html#method.send
7959 /// [`timeout()`]: struct.Connection.html#method.timeout
7960 /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
7961 /// [`is_closed()`]: struct.Connection.html#method.is_closed
7962 #[inline]
7963 pub fn is_draining(&self) -> bool {
7964 self.draining_timer.is_some()
7965 }
7966
7967 /// Returns true if the connection is closed.
7968 ///
7969 /// If this returns true, the connection object can be dropped.
7970 #[inline]
7971 pub fn is_closed(&self) -> bool {
7972 self.closed
7973 }
7974
7975 /// Returns true if the connection was closed due to the idle timeout.
7976 #[inline]
7977 pub fn is_timed_out(&self) -> bool {
7978 self.timed_out
7979 }
7980
7981 /// Returns the error received from the peer, if any.
7982 ///
7983 /// Note that a `Some` return value does not necessarily imply
7984 /// [`is_closed()`] or any other connection state.
7985 ///
7986 /// [`is_closed()`]: struct.Connection.html#method.is_closed
7987 #[inline]
7988 pub fn peer_error(&self) -> Option<&ConnectionError> {
7989 self.peer_error.as_ref()
7990 }
7991
7992 /// Returns the error [`close()`] was called with, or internally
7993 /// created quiche errors, if any.
7994 ///
7995 /// Note that a `Some` return value does not necessarily imply
7996 /// [`is_closed()`] or any other connection state.
7997 /// `Some` also does not guarantee that the error has been sent to
7998 /// or received by the peer.
7999 ///
8000 /// [`close()`]: struct.Connection.html#method.close
8001 /// [`is_closed()`]: struct.Connection.html#method.is_closed
8002 #[inline]
8003 pub fn local_error(&self) -> Option<&ConnectionError> {
8004 self.local_error.as_ref()
8005 }
8006
8007 /// Collects and returns statistics about the connection.
8008 #[inline]
8009 pub fn stats(&self) -> Stats {
8010 Stats {
8011 recv: self.recv_count,
8012 sent: self.sent_count,
8013 lost: self.lost_count,
8014 spurious_lost: self.spurious_lost_count,
8015 retrans: self.retrans_count,
8016 sent_bytes: self.sent_bytes,
8017 recv_bytes: self.recv_bytes,
8018 acked_bytes: self.acked_bytes,
8019 lost_bytes: self.lost_bytes,
8020 stream_retrans_bytes: self.stream_retrans_bytes,
8021 dgram_recv: self.dgram_recv_count,
8022 dgram_sent: self.dgram_sent_count,
8023 paths_count: self.paths.len(),
8024 reset_stream_count_local: self.reset_stream_local_count,
8025 stopped_stream_count_local: self.stopped_stream_local_count,
8026 reset_stream_count_remote: self.reset_stream_remote_count,
8027 stopped_stream_count_remote: self.stopped_stream_remote_count,
8028 data_blocked_sent_count: self.data_blocked_sent_count,
8029 stream_data_blocked_sent_count: self.stream_data_blocked_sent_count,
8030 data_blocked_recv_count: self.data_blocked_recv_count,
8031 stream_data_blocked_recv_count: self.stream_data_blocked_recv_count,
8032 streams_blocked_bidi_recv_count: self.streams_blocked_bidi_recv_count,
8033 streams_blocked_uni_recv_count: self.streams_blocked_uni_recv_count,
8034 path_challenge_rx_count: self.path_challenge_rx_count,
8035 amplification_limited_count: self.amplification_limited_count,
8036 bytes_in_flight_duration: self.bytes_in_flight_duration(),
8037 tx_buffered_state: if self.streams.tx_buffered_is_consistent() {
8038 TxBufferTrackingState::Ok
8039 } else {
8040 TxBufferTrackingState::Inconsistent
8041 },
8042 }
8043 }
8044
8045 /// Returns the sum of the durations when each path in the
8046 /// connection was actively sending bytes or waiting for acks.
8047 /// Note that this could result in a duration that is longer than
8048 /// the actual connection duration in cases where multiple paths
8049 /// are active for extended periods of time. In practice only 1
8050 /// path is typically active at a time.
8051 /// TODO revisit computation if in the future multiple paths are
8052 /// often active at the same time.
8053 fn bytes_in_flight_duration(&self) -> Duration {
8054 self.paths.iter().fold(Duration::ZERO, |acc, (_, path)| {
8055 acc + path.bytes_in_flight_duration()
8056 })
8057 }
8058
8059 /// Returns reference to peer's transport parameters. Returns `None` if we
8060 /// have not yet processed the peer's transport parameters.
8061 pub fn peer_transport_params(&self) -> Option<&TransportParams> {
8062 if !self.parsed_peer_transport_params {
8063 return None;
8064 }
8065
8066 Some(&self.peer_transport_params)
8067 }
8068
8069 /// Collects and returns statistics about each known path for the
8070 /// connection.
8071 pub fn path_stats(&self) -> impl Iterator<Item = PathStats> + '_ {
8072 self.paths.iter().map(|(_, p)| p.stats())
8073 }
8074
8075 /// Returns whether or not this is a server-side connection.
8076 pub fn is_server(&self) -> bool {
8077 self.is_server
8078 }
8079
8080 fn encode_transport_params(&mut self) -> Result<()> {
8081 self.handshake.set_quic_transport_params(
8082 &self.local_transport_params,
8083 self.is_server,
8084 )
8085 }
8086
8087 fn parse_peer_transport_params(
8088 &mut self, peer_params: TransportParams,
8089 ) -> Result<()> {
8090 // Validate initial_source_connection_id.
8091 match &peer_params.initial_source_connection_id {
8092 Some(v) if v != &self.destination_id() =>
8093 return Err(Error::InvalidTransportParam),
8094
8095 Some(_) => (),
8096
8097 // initial_source_connection_id must be sent by
8098 // both endpoints.
8099 None => return Err(Error::InvalidTransportParam),
8100 }
8101
8102 // Validate original_destination_connection_id.
8103 if let Some(odcid) = &self.odcid {
8104 match &peer_params.original_destination_connection_id {
8105 Some(v) if v != odcid =>
8106 return Err(Error::InvalidTransportParam),
8107
8108 Some(_) => (),
8109
8110 // original_destination_connection_id must be
8111 // sent by the server.
8112 None if !self.is_server =>
8113 return Err(Error::InvalidTransportParam),
8114
8115 None => (),
8116 }
8117 }
8118
8119 // Validate retry_source_connection_id.
8120 if let Some(rscid) = &self.rscid {
8121 match &peer_params.retry_source_connection_id {
8122 Some(v) if v != rscid =>
8123 return Err(Error::InvalidTransportParam),
8124
8125 Some(_) => (),
8126
8127 // retry_source_connection_id must be sent by
8128 // the server.
8129 None => return Err(Error::InvalidTransportParam),
8130 }
8131 }
8132
8133 self.process_peer_transport_params(peer_params)?;
8134
8135 self.parsed_peer_transport_params = true;
8136
8137 Ok(())
8138 }
8139
8140 fn process_peer_transport_params(
8141 &mut self, peer_params: TransportParams,
8142 ) -> Result<()> {
8143 self.max_tx_data = peer_params.initial_max_data;
8144
8145 // Update send capacity.
8146 self.update_tx_cap();
8147
8148 self.streams
8149 .update_peer_max_streams_bidi(peer_params.initial_max_streams_bidi);
8150 self.streams
8151 .update_peer_max_streams_uni(peer_params.initial_max_streams_uni);
8152
8153 let max_ack_delay = Duration::from_millis(peer_params.max_ack_delay);
8154 let max_udp_payload_size =
8155 usize::try_from(peer_params.max_udp_payload_size)
8156 .unwrap_or(usize::MAX);
8157
8158 self.recovery_config.max_ack_delay = max_ack_delay;
8159 self.recovery_config
8160 .update_max_send_udp_payload_size(max_udp_payload_size);
8161
8162 let active_path = self.paths.get_active_mut()?;
8163
8164 active_path.recovery.update_max_ack_delay(max_ack_delay);
8165
8166 if active_path
8167 .pmtud
8168 .as_ref()
8169 .map(|pmtud| pmtud.should_probe())
8170 .unwrap_or(false)
8171 {
8172 active_path.recovery.pmtud_update_max_datagram_size(
8173 active_path
8174 .pmtud
8175 .as_mut()
8176 .expect("PMTUD existence verified above")
8177 .get_probe_size()
8178 .min(max_udp_payload_size),
8179 );
8180 } else {
8181 active_path
8182 .recovery
8183 .update_max_datagram_size(max_udp_payload_size);
8184 }
8185
8186 // Record the max_active_conn_id parameter advertised by the peer.
8187 self.ids
8188 .set_source_conn_id_limit(peer_params.active_conn_id_limit);
8189
8190 self.peer_transport_params = peer_params;
8191
8192 Ok(())
8193 }
8194
8195 /// Continues the handshake.
8196 ///
8197 /// If the connection is already established, it does nothing.
8198 fn do_handshake(&mut self, now: Instant) -> Result<()> {
8199 let mut ex_data = tls::ExData {
8200 application_protos: &self.application_protos,
8201
8202 crypto_ctx: &mut self.crypto_ctx,
8203
8204 session: &mut self.session,
8205
8206 local_error: &mut self.local_error,
8207
8208 keylog: self.keylog.as_mut(),
8209
8210 trace_id: &self.trace_id,
8211
8212 local_transport_params: self.local_transport_params.clone(),
8213
8214 recovery_config: self.recovery_config,
8215
8216 tx_cap_factor: self.tx_cap_factor,
8217
8218 max_outstanding_sent_packets: self.max_outstanding_sent_packets,
8219
8220 pmtud: None,
8221
8222 is_server: self.is_server,
8223 };
8224
8225 if self.handshake_completed {
8226 return self.handshake.process_post_handshake(&mut ex_data);
8227 }
8228
8229 let handshake_needs_retry =
8230 match self.handshake.do_handshake(&mut ex_data) {
8231 Ok(_) => false,
8232 Err(Error::Done) => true,
8233 Err(e) => return Err(e),
8234 };
8235
8236 // BoringSSL reports success when entering early data before the
8237 // handshake completes. Apply callback configuration after either
8238 // non-fatal outcome so it is not lost on that path.
8239 if self
8240 .paths
8241 .get_active()
8242 .map(|p| p.can_reinit_recovery())
8243 .unwrap_or(false)
8244 {
8245 if ex_data.recovery_config != self.recovery_config {
8246 // If we received peer transport parameters already, respect the
8247 // peer's `max_udp_payload_size`.
8248 if self.parsed_peer_transport_params {
8249 let max_udp_payload_size = usize::try_from(
8250 self.peer_transport_params.max_udp_payload_size,
8251 )
8252 .unwrap_or(usize::MAX);
8253 ex_data
8254 .recovery_config
8255 .update_max_send_udp_payload_size(max_udp_payload_size);
8256 }
8257
8258 if let Ok(path) = self.paths.get_active_mut() {
8259 self.recovery_config = ex_data.recovery_config;
8260 path.reinit_recovery(&self.recovery_config);
8261 }
8262 }
8263
8264 if ex_data.tx_cap_factor != self.tx_cap_factor {
8265 self.tx_cap_factor = ex_data.tx_cap_factor;
8266 }
8267
8268 self.max_outstanding_sent_packets =
8269 ex_data.max_outstanding_sent_packets;
8270
8271 if let Some((discover, max_probes)) = ex_data.pmtud {
8272 self.paths.set_discover_pmtu_on_existing_paths(
8273 discover,
8274 self.recovery_config.max_send_udp_payload_size,
8275 max_probes,
8276 );
8277 }
8278
8279 if ex_data.local_transport_params != self.local_transport_params {
8280 self.streams.set_max_streams_bidi(
8281 ex_data.local_transport_params.initial_max_streams_bidi,
8282 );
8283
8284 self.local_transport_params = ex_data.local_transport_params;
8285 }
8286 }
8287
8288 if handshake_needs_retry {
8289 // Try to parse transport parameters as soon as the first flight of
8290 // handshake data is processed.
8291 //
8292 // This is potentially dangerous as the handshake hasn't been
8293 // completed yet, though it's required to be able to send data in
8294 // 0.5 RTT.
8295 let raw_params = self.handshake.quic_transport_params();
8296
8297 if !self.parsed_peer_transport_params && !raw_params.is_empty() {
8298 let peer_params = TransportParams::decode(
8299 raw_params,
8300 self.is_server,
8301 self.peer_transport_params_track_unknown,
8302 )?;
8303
8304 self.parse_peer_transport_params(peer_params)?;
8305 }
8306
8307 return Ok(());
8308 }
8309
8310 self.handshake_completed = self.handshake.is_completed();
8311
8312 self.alpn = self.handshake.alpn_protocol().to_vec();
8313
8314 let raw_params = self.handshake.quic_transport_params();
8315
8316 if !self.parsed_peer_transport_params && !raw_params.is_empty() {
8317 let peer_params = TransportParams::decode(
8318 raw_params,
8319 self.is_server,
8320 self.peer_transport_params_track_unknown,
8321 )?;
8322
8323 self.parse_peer_transport_params(peer_params)?;
8324 }
8325
8326 if self.handshake_completed {
8327 // The handshake is considered confirmed at the server when the
8328 // handshake completes, at which point we can also drop the
8329 // handshake epoch.
8330 if self.is_server {
8331 self.handshake_confirmed = true;
8332
8333 self.drop_epoch_state(packet::Epoch::Handshake, now);
8334 }
8335
8336 // Once the handshake is completed there's no point in processing
8337 // 0-RTT packets anymore, so clear the buffer now.
8338 self.undecryptable_pkts.clear();
8339
8340 trace!("{} connection established: proto={:?} cipher={:?} curve={:?} sigalg={:?} resumed={} {:?}",
8341 self.trace_id,
8342 std::str::from_utf8(self.application_proto()),
8343 self.handshake.cipher(),
8344 self.handshake.curve(),
8345 self.handshake.sigalg(),
8346 self.handshake.is_resumed(),
8347 self.peer_transport_params);
8348 }
8349
8350 Ok(())
8351 }
8352
8353 /// Selects the packet type for the next outgoing packet.
8354 fn write_pkt_type(&self, send_pid: usize) -> Result<Type> {
8355 // On error send packet in the latest epoch available, but only send
8356 // 1-RTT ones when the handshake is completed.
8357 if self
8358 .local_error
8359 .as_ref()
8360 .is_some_and(|conn_err| !conn_err.is_app)
8361 {
8362 let epoch = match self.handshake.write_level() {
8363 crypto::Level::Initial => packet::Epoch::Initial,
8364 crypto::Level::ZeroRTT => unreachable!(),
8365 crypto::Level::Handshake => packet::Epoch::Handshake,
8366 crypto::Level::OneRTT => packet::Epoch::Application,
8367 };
8368
8369 if !self.handshake_confirmed {
8370 match epoch {
8371 // Downgrade the epoch to Handshake as the handshake is not
8372 // completed yet.
8373 packet::Epoch::Application => return Ok(Type::Handshake),
8374
8375 // Downgrade the epoch to Initial as the remote peer might
8376 // not be able to decrypt handshake packets yet.
8377 packet::Epoch::Handshake
8378 if self.crypto_ctx[packet::Epoch::Initial].has_keys() =>
8379 return Ok(Type::Initial),
8380
8381 _ => (),
8382 };
8383 }
8384
8385 return Ok(Type::from_epoch(epoch));
8386 }
8387
8388 // APPLICATION_CLOSE can only be sent in a 1-RTT packet. Prioritize it
8389 // over obsolete lower-epoch PTO probes, which cannot be consumed while
8390 // closing because PING frames are suppressed.
8391 if self.is_established() &&
8392 self.local_error
8393 .as_ref()
8394 .is_some_and(|conn_err| conn_err.is_app)
8395 {
8396 return Ok(Type::Short);
8397 }
8398
8399 for &epoch in packet::Epoch::epochs(
8400 packet::Epoch::Initial..=packet::Epoch::Application,
8401 ) {
8402 let crypto_ctx = &self.crypto_ctx[epoch];
8403 let pkt_space = &self.pkt_num_spaces[epoch];
8404
8405 // Only send packets in a space when we have the send keys for it.
8406 if crypto_ctx.crypto_seal.is_none() {
8407 continue;
8408 }
8409
8410 // We are ready to send data for this packet number space.
8411 if crypto_ctx.data_available() || pkt_space.ready() {
8412 return Ok(Type::from_epoch(epoch));
8413 }
8414
8415 // There are lost frames in this packet number space.
8416 for (_, p) in self.paths.iter() {
8417 if p.recovery.has_lost_frames(epoch) {
8418 return Ok(Type::from_epoch(epoch));
8419 }
8420
8421 // We need to send PTO probe packets.
8422 if p.recovery.loss_probes(epoch) > 0 {
8423 return Ok(Type::from_epoch(epoch));
8424 }
8425 }
8426 }
8427
8428 // If there are flushable, almost full or blocked streams, use the
8429 // Application epoch.
8430 let send_path = self.paths.get(send_pid)?;
8431 if (self.is_established() || self.is_in_early_data()) &&
8432 (self.should_send_handshake_done() ||
8433 self.flow_control.should_update_max_data() ||
8434 self.should_send_max_data ||
8435 self.blocked_limit.is_some() ||
8436 self.streams_blocked_bidi_state
8437 .has_pending_stream_blocked_frame() ||
8438 self.streams_blocked_uni_state
8439 .has_pending_stream_blocked_frame() ||
8440 self.dgram_send_queue.has_pending() ||
8441 self.local_error
8442 .as_ref()
8443 .is_some_and(|conn_err| conn_err.is_app) ||
8444 self.should_send_max_streams_bidi ||
8445 self.streams.should_update_max_streams_bidi() ||
8446 self.should_send_max_streams_uni ||
8447 self.streams.should_update_max_streams_uni() ||
8448 self.streams.has_flushable() ||
8449 self.streams.has_almost_full() ||
8450 self.streams.has_blocked() ||
8451 self.streams.has_reset() ||
8452 self.streams.has_stopped() ||
8453 self.ids.has_new_scids() ||
8454 self.ids.has_retire_dcids() ||
8455 send_path
8456 .pmtud
8457 .as_ref()
8458 .is_some_and(|pmtud| pmtud.should_probe()) ||
8459 send_path.needs_ack_eliciting ||
8460 send_path.probing_required())
8461 {
8462 // Only clients can send 0-RTT packets.
8463 if !self.is_server && self.is_in_early_data() {
8464 return Ok(Type::ZeroRTT);
8465 }
8466
8467 return Ok(Type::Short);
8468 }
8469
8470 Err(Error::Done)
8471 }
8472
8473 /// Returns the mutable stream with the given ID if it exists, or creates
8474 /// a new one otherwise.
8475 fn get_or_create_stream(
8476 &mut self, id: u64, local: bool,
8477 ) -> Result<&mut stream::Stream<F>> {
8478 self.streams.get_or_create(
8479 id,
8480 &self.local_transport_params,
8481 &self.peer_transport_params,
8482 local,
8483 self.is_server,
8484 )
8485 }
8486
8487 /// Gets or creates a stream referenced by a received frame.
8488 fn get_or_create_stream_for_received_frame(
8489 &mut self, id: u64,
8490 ) -> Result<&mut stream::Stream<F>> {
8491 let local = stream::is_local(id, self.is_server);
8492
8493 // Opening a higher-numbered stream also opens lower streams of the
8494 // same type, even if they do not have Stream objects yet.
8495 if local && !self.streams.local_stream_opened(id) {
8496 return Err(Error::InvalidStreamState(id));
8497 }
8498
8499 self.get_or_create_stream(id, local)
8500 }
8501
8502 /// Processes an incoming frame.
8503 fn process_frame(
8504 &mut self, frame: frame::Frame, hdr: &Header, recv_path_id: usize,
8505 epoch: packet::Epoch, now: Instant,
8506 ) -> Result<()> {
8507 trace!("{} rx frm {:?}", self.trace_id, frame);
8508
8509 match frame {
8510 frame::Frame::Padding { .. } => (),
8511
8512 frame::Frame::Ping { .. } => (),
8513
8514 frame::Frame::ACK {
8515 ranges, ack_delay, ..
8516 } => {
8517 let ack_delay = ack_delay
8518 .checked_mul(2_u64.pow(
8519 self.peer_transport_params.ack_delay_exponent as u32,
8520 ))
8521 .ok_or(Error::InvalidFrame)?;
8522
8523 if epoch == packet::Epoch::Handshake ||
8524 (epoch == packet::Epoch::Application &&
8525 self.is_established())
8526 {
8527 self.peer_verified_initial_address = true;
8528 }
8529
8530 let handshake_status = self.handshake_status();
8531
8532 let is_app_limited = self.delivery_rate_check_if_app_limited();
8533
8534 let largest_acked = ranges.last().expect(
8535 "ACK frames should always have at least one ack range",
8536 );
8537
8538 for (_, p) in self.paths.iter_mut() {
8539 if self.pkt_num_spaces[epoch]
8540 .largest_tx_pkt_num
8541 .is_some_and(|largest_sent| largest_sent < largest_acked)
8542 {
8543 // https://www.rfc-editor.org/rfc/rfc9000#section-13.1
8544 // An endpoint SHOULD treat receipt of an acknowledgment
8545 // for a packet it did not send as
8546 // a connection error of type PROTOCOL_VIOLATION
8547 return Err(Error::InvalidAckRange);
8548 }
8549
8550 if is_app_limited {
8551 p.recovery.delivery_rate_update_app_limited(true);
8552 }
8553
8554 let OnAckReceivedOutcome {
8555 lost_packets,
8556 lost_bytes,
8557 acked_bytes,
8558 spurious_losses,
8559 } = p.recovery.on_ack_received(
8560 &ranges,
8561 ack_delay,
8562 epoch,
8563 handshake_status,
8564 now,
8565 self.pkt_num_manager.skip_pn(),
8566 &self.trace_id,
8567 )?;
8568
8569 let skip_pn = self.pkt_num_manager.skip_pn();
8570 let largest_acked =
8571 p.recovery.get_largest_acked_on_epoch(epoch);
8572
8573 // A higher ACK validates `skip_pn`.
8574 if let Some((largest_acked, skip_pn)) =
8575 largest_acked.zip(skip_pn)
8576 {
8577 if largest_acked > skip_pn {
8578 self.pkt_num_manager.set_skip_pn(None);
8579 }
8580 }
8581
8582 self.lost_count += lost_packets;
8583 self.lost_bytes += lost_bytes as u64;
8584 self.acked_bytes += acked_bytes as u64;
8585 self.spurious_lost_count += spurious_losses;
8586 }
8587 },
8588
8589 frame::Frame::ResetStream {
8590 stream_id,
8591 error_code,
8592 final_size,
8593 } => {
8594 // Peer can't send on our unidirectional streams.
8595 if !stream::is_bidi(stream_id) &&
8596 stream::is_local(stream_id, self.is_server)
8597 {
8598 return Err(Error::InvalidStreamState(stream_id));
8599 }
8600
8601 let max_rx_data_left = self.max_rx_data() - self.rx_data;
8602
8603 // Get existing stream or create a new one, but if the stream
8604 // has already been closed and collected, ignore the frame.
8605 //
8606 // This can happen if e.g. an ACK frame is lost, and the peer
8607 // retransmits another frame before it realizes that the stream
8608 // is gone.
8609 //
8610 // Note that it makes it impossible to check if the frame is
8611 // illegal, since we have no state, but since we ignore the
8612 // frame, it should be fine.
8613 let stream = match self
8614 .get_or_create_stream_for_received_frame(stream_id)
8615 {
8616 Ok(v) => v,
8617
8618 Err(Error::Done) => return Ok(()),
8619
8620 Err(e) => return Err(e),
8621 };
8622
8623 let was_readable = stream.is_readable();
8624 let priority_key = Arc::clone(&stream.priority_key);
8625
8626 let stream::RecvBufResetReturn {
8627 max_data_delta,
8628 consumed_flowcontrol,
8629 } = stream.recv.reset(error_code, final_size)?;
8630
8631 if max_data_delta > max_rx_data_left {
8632 return Err(Error::FlowControl);
8633 }
8634
8635 // The receive side can complete without the stream becoming
8636 // readable, so no later read would collect it.
8637 let state = stream.state();
8638 let local = stream.local;
8639
8640 if !was_readable && stream.is_readable() {
8641 self.streams.insert_readable(&priority_key);
8642 }
8643
8644 self.rx_data += max_data_delta;
8645 // We dropped the receive buffer, return connection level
8646 // flow-control
8647 self.flow_control.add_consumed(consumed_flowcontrol);
8648
8649 self.reset_stream_remote_count =
8650 self.reset_stream_remote_count.saturating_add(1);
8651
8652 if state == stream::StreamState::Collectable {
8653 self.streams.collect(stream_id, local);
8654 }
8655 },
8656
8657 frame::Frame::StopSending {
8658 stream_id,
8659 error_code,
8660 } => {
8661 // STOP_SENDING on a receive-only stream is a fatal error.
8662 if !stream::is_local(stream_id, self.is_server) &&
8663 !stream::is_bidi(stream_id)
8664 {
8665 return Err(Error::InvalidStreamState(stream_id));
8666 }
8667
8668 // Get existing stream or create a new one, but if the stream
8669 // has already been closed and collected, ignore the frame.
8670 //
8671 // This can happen if e.g. an ACK frame is lost, and the peer
8672 // retransmits another frame before it realizes that the stream
8673 // is gone.
8674 //
8675 // Note that it makes it impossible to check if the frame is
8676 // illegal, since we have no state, but since we ignore the
8677 // frame, it should be fine.
8678 let stream = match self
8679 .get_or_create_stream_for_received_frame(stream_id)
8680 {
8681 Ok(v) => v,
8682
8683 Err(Error::Done) => return Ok(()),
8684
8685 Err(e) => return Err(e),
8686 };
8687
8688 let priority_key = Arc::clone(&stream.priority_key);
8689
8690 // Save the buffered length before stopping (stop clears the
8691 // buffer).
8692 let buffered_len = stream.send.buffered_bytes() as usize;
8693
8694 // Try stopping the stream.
8695 if let Ok((final_size, unsent)) = stream.send.stop(error_code) {
8696 // Claw back some flow control allowance from data that was
8697 // buffered but not actually sent before the stream was
8698 // reset.
8699 //
8700 // Note that `tx_cap` will be updated later on, so no need
8701 // to touch it here.
8702 self.tx_data = self.tx_data.saturating_sub(unsent);
8703
8704 // Update tx_buffered: subtract only the buffered data, not
8705 // inflight data.
8706 self.streams.sub_tx_buffered(buffered_len);
8707
8708 // Match moves from App to Transport with moves from
8709 // Transport to Dropped.
8710 // A Network transition would distinguish sent bytes from
8711 // drops before transmission.
8712 qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
8713 let ev_data = EventData::QuicStreamDataMoved(
8714 qlog::events::quic::StreamDataMoved {
8715 stream_id: Some(stream_id),
8716 offset: Some(final_size),
8717 raw: Some(RawInfo {
8718 length: Some(unsent),
8719 ..Default::default()
8720 }),
8721 from: Some(DataRecipient::Transport),
8722 to: Some(DataRecipient::Dropped),
8723 ..Default::default()
8724 },
8725 );
8726
8727 q.add_event_data_with_instant(ev_data, now).ok();
8728 });
8729
8730 self.streams.insert_reset(stream_id, error_code, final_size);
8731
8732 self.streams.insert_stopped_writable(&priority_key);
8733
8734 // A stopped stream has nothing left to flush. A stale
8735 // flushable entry could make send() return Done while
8736 // DATAGRAMs are still queued.
8737 self.streams.remove_flushable(&priority_key);
8738
8739 self.stopped_stream_remote_count =
8740 self.stopped_stream_remote_count.saturating_add(1);
8741 self.reset_stream_local_count =
8742 self.reset_stream_local_count.saturating_add(1);
8743 }
8744 },
8745
8746 frame::Frame::Crypto { data } => {
8747 if data.max_off() >= MAX_CRYPTO_STREAM_OFFSET {
8748 return Err(Error::CryptoBufferExceeded);
8749 }
8750
8751 // Push the data to the stream so it can be re-ordered.
8752 self.crypto_ctx[epoch].crypto_stream.recv.write(data)?;
8753
8754 // Feed crypto data to the TLS state, if there's data
8755 // available at the expected offset.
8756 let mut crypto_buf = [0; 512];
8757
8758 let level = crypto::Level::from_epoch(epoch);
8759
8760 let stream = &mut self.crypto_ctx[epoch].crypto_stream;
8761
8762 while let Ok((read, _)) = stream.recv.emit(&mut crypto_buf) {
8763 let recv_buf = &crypto_buf[..read];
8764 self.handshake.provide_data(level, recv_buf)?;
8765 }
8766
8767 self.do_handshake(now)?;
8768 },
8769
8770 frame::Frame::CryptoHeader { .. } => unreachable!(),
8771
8772 // TODO: implement stateless retry
8773 frame::Frame::NewToken { .. } =>
8774 if self.is_server {
8775 return Err(Error::InvalidPacket);
8776 },
8777
8778 frame::Frame::Stream { stream_id, data } => {
8779 // Peer can't send on our unidirectional streams.
8780 if !stream::is_bidi(stream_id) &&
8781 stream::is_local(stream_id, self.is_server)
8782 {
8783 return Err(Error::InvalidStreamState(stream_id));
8784 }
8785
8786 let max_rx_data_left = self.max_rx_data() - self.rx_data;
8787
8788 // Get existing stream or create a new one, but if the stream
8789 // has already been closed and collected, ignore the frame.
8790 //
8791 // This can happen if e.g. an ACK frame is lost, and the peer
8792 // retransmits another frame before it realizes that the stream
8793 // is gone.
8794 //
8795 // Note that it makes it impossible to check if the frame is
8796 // illegal, since we have no state, but since we ignore the
8797 // frame, it should be fine.
8798 let stream = match self
8799 .get_or_create_stream_for_received_frame(stream_id)
8800 {
8801 Ok(v) => v,
8802
8803 Err(Error::Done) => return Ok(()),
8804
8805 Err(e) => return Err(e),
8806 };
8807
8808 // Check for the connection-level flow control limit.
8809 let max_off_delta =
8810 data.max_off().saturating_sub(stream.recv.max_off());
8811
8812 if max_off_delta > max_rx_data_left {
8813 return Err(Error::FlowControl);
8814 }
8815
8816 let was_readable = stream.is_readable();
8817 let priority_key = Arc::clone(&stream.priority_key);
8818
8819 let was_draining = stream.recv.is_draining();
8820
8821 stream.recv.write(data)?;
8822
8823 // The receive side can complete without the stream becoming
8824 // readable, so no later read would collect it.
8825 let state = stream.state();
8826 let local = stream.local;
8827
8828 if !was_readable && stream.is_readable() {
8829 self.streams.insert_readable(&priority_key);
8830 }
8831
8832 self.rx_data += max_off_delta;
8833
8834 if was_draining {
8835 // When a stream is in draining state it will not queue
8836 // incoming data for the application to read, so consider
8837 // the received data as consumed, which might trigger a flow
8838 // control update.
8839 self.flow_control.add_consumed(max_off_delta);
8840 }
8841
8842 if state == stream::StreamState::Collectable {
8843 self.streams.collect(stream_id, local);
8844 }
8845 },
8846
8847 frame::Frame::StreamHeader { .. } => unreachable!(),
8848
8849 frame::Frame::MaxData { max } => {
8850 self.max_tx_data = cmp::max(self.max_tx_data, max);
8851 },
8852
8853 frame::Frame::MaxStreamData { stream_id, max } => {
8854 // Peer can't receive on its own unidirectional streams.
8855 if !stream::is_bidi(stream_id) &&
8856 !stream::is_local(stream_id, self.is_server)
8857 {
8858 return Err(Error::InvalidStreamState(stream_id));
8859 }
8860
8861 // Get existing stream or create a new one, but if the stream
8862 // has already been closed and collected, ignore the frame.
8863 //
8864 // This can happen if e.g. an ACK frame is lost, and the peer
8865 // retransmits another frame before it realizes that the stream
8866 // is gone.
8867 //
8868 // Note that it makes it impossible to check if the frame is
8869 // illegal, since we have no state, but since we ignore the
8870 // frame, it should be fine.
8871 let stream = match self
8872 .get_or_create_stream_for_received_frame(stream_id)
8873 {
8874 Ok(v) => v,
8875
8876 Err(Error::Done) => return Ok(()),
8877
8878 Err(e) => return Err(e),
8879 };
8880
8881 let was_flushable = stream.is_flushable();
8882
8883 stream.send.update_max_data(max);
8884
8885 let writable = stream.is_writable();
8886
8887 let priority_key = Arc::clone(&stream.priority_key);
8888
8889 // If the stream became flushable, add it to the queue unless it
8890 // is already present.
8891 if stream.is_flushable() && !was_flushable {
8892 let priority_key = Arc::clone(&stream.priority_key);
8893 self.streams.insert_flushable(&priority_key);
8894 }
8895
8896 if writable {
8897 self.streams.insert_writable(&priority_key);
8898 }
8899 },
8900
8901 frame::Frame::MaxStreamsBidi { max } => {
8902 if max > MAX_STREAM_ID {
8903 return Err(Error::InvalidFrame);
8904 }
8905
8906 self.streams.update_peer_max_streams_bidi(max);
8907 },
8908
8909 frame::Frame::MaxStreamsUni { max } => {
8910 if max > MAX_STREAM_ID {
8911 return Err(Error::InvalidFrame);
8912 }
8913
8914 self.streams.update_peer_max_streams_uni(max);
8915 },
8916
8917 frame::Frame::DataBlocked { .. } => {
8918 self.data_blocked_recv_count =
8919 self.data_blocked_recv_count.saturating_add(1);
8920 },
8921
8922 frame::Frame::StreamDataBlocked { .. } => {
8923 self.stream_data_blocked_recv_count =
8924 self.stream_data_blocked_recv_count.saturating_add(1);
8925 },
8926
8927 frame::Frame::StreamsBlockedBidi { limit } => {
8928 if limit > MAX_STREAM_ID {
8929 return Err(Error::InvalidFrame);
8930 }
8931
8932 self.streams_blocked_bidi_recv_count =
8933 self.streams_blocked_bidi_recv_count.saturating_add(1);
8934 },
8935
8936 frame::Frame::StreamsBlockedUni { limit } => {
8937 if limit > MAX_STREAM_ID {
8938 return Err(Error::InvalidFrame);
8939 }
8940
8941 self.streams_blocked_uni_recv_count =
8942 self.streams_blocked_uni_recv_count.saturating_add(1);
8943 },
8944
8945 frame::Frame::NewConnectionId {
8946 seq_num,
8947 retire_prior_to,
8948 conn_id,
8949 reset_token,
8950 } => {
8951 if self.ids.zero_length_dcid() {
8952 return Err(Error::InvalidState);
8953 }
8954
8955 let mut retired_path_ids = SmallVec::new();
8956
8957 // Retire pending path IDs before propagating the error code to
8958 // make sure retired connection IDs are not in use anymore.
8959 let new_dcid_res = self.ids.new_dcid(
8960 conn_id.into(),
8961 seq_num,
8962 u128::from_be_bytes(reset_token),
8963 retire_prior_to,
8964 &mut retired_path_ids,
8965 );
8966
8967 for (dcid_seq, pid) in retired_path_ids {
8968 let path = self.paths.get_mut(pid)?;
8969
8970 // Maybe the path already switched to another DCID.
8971 if path.active_dcid_seq != Some(dcid_seq) {
8972 continue;
8973 }
8974
8975 if let Some(new_dcid_seq) =
8976 self.ids.lowest_available_dcid_seq()
8977 {
8978 path.active_dcid_seq = Some(new_dcid_seq);
8979
8980 self.ids.link_dcid_to_path_id(new_dcid_seq, pid)?;
8981
8982 trace!(
8983 "{} path ID {} changed DCID: old seq num {} new seq num {}",
8984 self.trace_id, pid, dcid_seq, new_dcid_seq,
8985 );
8986 } else {
8987 // We cannot use this path anymore for now.
8988 path.active_dcid_seq = None;
8989
8990 trace!(
8991 "{} path ID {} cannot be used; DCID seq num {} has been retired",
8992 self.trace_id, pid, dcid_seq,
8993 );
8994 }
8995 }
8996
8997 // Propagate error (if any) now...
8998 new_dcid_res?;
8999 },
9000
9001 frame::Frame::RetireConnectionId { seq_num } => {
9002 if self.ids.zero_length_scid() {
9003 return Err(Error::InvalidState);
9004 }
9005
9006 if let Some(pid) = self.ids.retire_scid(seq_num, &hdr.dcid)? {
9007 let path = self.paths.get_mut(pid)?;
9008
9009 // Maybe we already linked a new SCID to that path.
9010 if path.active_scid_seq == Some(seq_num) {
9011 // XXX: We do not remove unused paths now, we instead
9012 // wait until we need to maintain more paths than the
9013 // host is willing to.
9014 path.active_scid_seq = None;
9015 }
9016 }
9017 },
9018
9019 frame::Frame::PathChallenge { data } => {
9020 self.path_challenge_rx_count += 1;
9021
9022 self.paths
9023 .get_mut(recv_path_id)?
9024 .on_challenge_received(data);
9025 },
9026
9027 frame::Frame::PathResponse { data } => {
9028 self.paths.on_response_received(data)?;
9029 },
9030
9031 frame::Frame::ConnectionClose {
9032 error_code, reason, ..
9033 } => {
9034 self.peer_error = Some(ConnectionError {
9035 is_app: false,
9036 error_code,
9037 reason,
9038 });
9039
9040 let path = self.paths.get_active()?;
9041 self.draining_timer = Some(now + (path.recovery.pto() * 3));
9042 },
9043
9044 frame::Frame::ApplicationClose { error_code, reason } => {
9045 self.peer_error = Some(ConnectionError {
9046 is_app: true,
9047 error_code,
9048 reason,
9049 });
9050
9051 let path = self.paths.get_active()?;
9052 self.draining_timer = Some(now + (path.recovery.pto() * 3));
9053 },
9054
9055 frame::Frame::HandshakeDone => {
9056 if self.is_server {
9057 return Err(Error::InvalidPacket);
9058 }
9059
9060 self.peer_verified_initial_address = true;
9061
9062 self.handshake_confirmed = true;
9063
9064 // Once the handshake is confirmed, we can drop Handshake keys.
9065 self.drop_epoch_state(packet::Epoch::Handshake, now);
9066 },
9067
9068 frame::Frame::Datagram { data } => {
9069 // Close the connection if DATAGRAMs are not enabled.
9070 // quiche always advertises support for 64K sized DATAGRAM
9071 // frames, as recommended by the standard, so we don't need a
9072 // size check.
9073 if !self.dgram_enabled() {
9074 return Err(Error::InvalidState);
9075 }
9076
9077 // If recv queue is full, discard oldest
9078 if self.dgram_recv_queue.is_full() {
9079 self.dgram_recv_queue.pop();
9080 }
9081
9082 self.dgram_recv_queue.push(data.into())?;
9083
9084 self.dgram_recv_count = self.dgram_recv_count.saturating_add(1);
9085
9086 let path = self.paths.get_mut(recv_path_id)?;
9087 path.dgram_recv_count = path.dgram_recv_count.saturating_add(1);
9088 },
9089
9090 frame::Frame::DatagramHeader { .. } => unreachable!(),
9091 }
9092
9093 Ok(())
9094 }
9095
9096 /// Drops the keys and recovery state for the given epoch.
9097 fn drop_epoch_state(&mut self, epoch: packet::Epoch, now: Instant) {
9098 let crypto_ctx = &mut self.crypto_ctx[epoch];
9099 if crypto_ctx.crypto_open.is_none() {
9100 return;
9101 }
9102 crypto_ctx.clear();
9103 self.pkt_num_spaces[epoch].clear();
9104
9105 let handshake_status = self.handshake_status();
9106 for (_, p) in self.paths.iter_mut() {
9107 p.recovery
9108 .on_pkt_num_space_discarded(epoch, handshake_status, now);
9109 }
9110
9111 trace!("{} dropped epoch {} state", self.trace_id, epoch);
9112 }
9113
9114 /// Returns the connection level flow control limit.
9115 fn max_rx_data(&self) -> u64 {
9116 self.flow_control.max_data()
9117 }
9118
9119 /// Returns true if the HANDSHAKE_DONE frame needs to be sent.
9120 fn should_send_handshake_done(&self) -> bool {
9121 self.is_established() && !self.handshake_done_sent && self.is_server
9122 }
9123
9124 /// Returns the idle timeout value.
9125 ///
9126 /// `None` is returned if both end-points disabled the idle timeout.
9127 fn idle_timeout(&self) -> Option<Duration> {
9128 // If the transport parameter is set to 0, then the respective endpoint
9129 // decided to disable the idle timeout. If both are disabled we should
9130 // not set any timeout.
9131 if self.local_transport_params.max_idle_timeout == 0 &&
9132 self.peer_transport_params.max_idle_timeout == 0
9133 {
9134 return None;
9135 }
9136
9137 // If the local endpoint or the peer disabled the idle timeout, use the
9138 // other peer's value, otherwise use the minimum of the two values.
9139 let idle_timeout = if self.local_transport_params.max_idle_timeout == 0 {
9140 self.peer_transport_params.max_idle_timeout
9141 } else if self.peer_transport_params.max_idle_timeout == 0 {
9142 self.local_transport_params.max_idle_timeout
9143 } else {
9144 cmp::min(
9145 self.local_transport_params.max_idle_timeout,
9146 self.peer_transport_params.max_idle_timeout,
9147 )
9148 };
9149
9150 let path_pto = match self.paths.get_active() {
9151 Ok(p) => p.recovery.pto(),
9152 Err(_) => Duration::ZERO,
9153 };
9154
9155 let idle_timeout = Duration::from_millis(idle_timeout);
9156 let idle_timeout = cmp::max(idle_timeout, 3 * path_pto);
9157
9158 Some(idle_timeout)
9159 }
9160
9161 /// Returns the connection's handshake status for use in loss recovery.
9162 fn handshake_status(&self) -> recovery::HandshakeStatus {
9163 recovery::HandshakeStatus {
9164 has_handshake_keys: self.crypto_ctx[packet::Epoch::Handshake]
9165 .has_keys(),
9166
9167 peer_verified_address: self.peer_verified_initial_address,
9168
9169 completed: self.is_established(),
9170 }
9171 }
9172
9173 /// Updates send capacity.
9174 fn update_tx_cap(&mut self) {
9175 let cwin_available = match self.paths.get_active() {
9176 Ok(p) => p.recovery.cwnd_available() as u64,
9177 Err(_) => 0,
9178 };
9179
9180 let cap =
9181 cmp::min(cwin_available, self.max_tx_data - self.tx_data) as usize;
9182 self.tx_cap = (cap as f64 * self.tx_cap_factor).ceil() as usize;
9183 }
9184
9185 fn delivery_rate_check_if_app_limited(&self) -> bool {
9186 // Enter the app-limited phase of delivery rate when these conditions
9187 // are met:
9188 //
9189 // - The remaining capacity exceeds the available bytes in CWND (there
9190 // is more room to send).
9191 // - New data since the last `send()` is smaller than available bytes in
9192 // CWND (we queued less than what we can send).
9193 // - CWND has room for more data.
9194 //
9195 // In application-limited phases the transmission rate is limited by the
9196 // application rather than the congestion control algorithm.
9197 //
9198 // This mirrors `CheckIfApplicationLimited()` from the delivery-rate
9199 // draft but affects only delivery-rate calculation, not
9200 // `recovery.app_limited`.
9201 let cwin_available = self
9202 .paths
9203 .iter()
9204 .filter(|&(_, p)| p.active())
9205 .map(|(_, p)| p.recovery.cwnd_available())
9206 .sum();
9207
9208 ((self.streams.tx_buffered() + self.dgram_send_queue_byte_size()) <
9209 cwin_available) &&
9210 (self.tx_data.saturating_sub(self.last_tx_data)) <
9211 cwin_available as u64 &&
9212 cwin_available > 0
9213 }
9214
9215 fn set_initial_dcid(
9216 &mut self, cid: ConnectionId<'static>, reset_token: Option<u128>,
9217 path_id: usize,
9218 ) -> Result<()> {
9219 self.ids.set_initial_dcid(cid, reset_token, Some(path_id));
9220 self.paths.get_mut(path_id)?.active_dcid_seq = Some(0);
9221
9222 Ok(())
9223 }
9224
9225 /// Selects the path that the incoming packet belongs to, or creates a new
9226 /// one if no existing path matches.
9227 fn get_or_create_recv_path_id(
9228 &mut self, recv_pid: Option<usize>, dcid: &ConnectionId, buf_len: usize,
9229 info: &RecvInfo,
9230 ) -> Result<usize> {
9231 let ids = &mut self.ids;
9232
9233 let (in_scid_seq, mut in_scid_pid) =
9234 ids.find_scid_seq(dcid).ok_or(Error::InvalidState)?;
9235
9236 if let Some(recv_pid) = recv_pid {
9237 // If the path observes a change of SCID used, note it.
9238 let recv_path = self.paths.get_mut(recv_pid)?;
9239
9240 let cid_entry =
9241 recv_path.active_scid_seq.and_then(|v| ids.get_scid(v).ok());
9242
9243 if cid_entry.map(|e| &e.cid) != Some(dcid) {
9244 let incoming_cid_entry = ids.get_scid(in_scid_seq)?;
9245
9246 let prev_recv_pid =
9247 incoming_cid_entry.path_id.unwrap_or(recv_pid);
9248
9249 if prev_recv_pid != recv_pid {
9250 trace!(
9251 "{} peer reused CID {:?} from path {} on path {}",
9252 self.trace_id,
9253 dcid,
9254 prev_recv_pid,
9255 recv_pid
9256 );
9257
9258 // TODO: reset congestion control.
9259 }
9260
9261 trace!(
9262 "{} path ID {} now see SCID with seq num {}",
9263 self.trace_id,
9264 recv_pid,
9265 in_scid_seq
9266 );
9267
9268 recv_path.active_scid_seq = Some(in_scid_seq);
9269 ids.link_scid_to_path_id(in_scid_seq, recv_pid)?;
9270 }
9271
9272 return Ok(recv_pid);
9273 }
9274
9275 // This is a new 4-tuple. See if the CID has not been assigned on
9276 // another path.
9277
9278 // Ignore this step if are using zero-length SCID.
9279 if ids.zero_length_scid() {
9280 in_scid_pid = None;
9281 }
9282
9283 // Capture old path info before insert_path() so we can emit the
9284 // ReusedSourceConnectionId event after successful insertion. This
9285 // ensures the event count is bounded by path Slab capacity.
9286 let reused_cid_info = match in_scid_pid {
9287 Some(pid) => {
9288 let old_path = self.paths.get(pid)?;
9289 Some((pid, old_path.local_addr(), old_path.peer_addr()))
9290 },
9291
9292 None => None,
9293 };
9294
9295 // This is a new path using an unassigned CID; create it!
9296 let mut path = path::Path::new(
9297 info.to,
9298 info.from,
9299 &self.recovery_config,
9300 self.path_challenge_recv_max_queue_len,
9301 false,
9302 None,
9303 );
9304
9305 path.max_send_bytes = buf_len * self.max_amplification_factor;
9306 path.active_scid_seq = Some(in_scid_seq);
9307
9308 // Automatically probes the new path.
9309 path.request_validation();
9310
9311 let pid = self.paths.insert_path(path, self.is_server)?;
9312
9313 // Notify the application of CID reuse only after the path was
9314 // successfully admitted. This bounds event queue growth by path Slab
9315 // capacity, preventing an attacker from growing the queue unboundedly
9316 // by rotating source ports.
9317 match reused_cid_info {
9318 Some((old_pid, old_local_addr, old_peer_addr)) => {
9319 trace!(
9320 "{} reused CID seq {} of ({},{}) (path {}) on ({},{})",
9321 self.trace_id,
9322 in_scid_seq,
9323 old_local_addr,
9324 old_peer_addr,
9325 old_pid,
9326 info.to,
9327 info.from
9328 );
9329
9330 self.paths.notify_event(PathEvent::ReusedSourceConnectionId(
9331 in_scid_seq,
9332 (old_local_addr, old_peer_addr),
9333 (info.to, info.from),
9334 ));
9335 },
9336
9337 None => {
9338 ids.link_scid_to_path_id(in_scid_seq, pid)?;
9339 },
9340 }
9341
9342 Ok(pid)
9343 }
9344
9345 /// Selects the path on which the next packet must be sent.
9346 fn get_send_path_id(
9347 &self, from: Option<SocketAddr>, to: Option<SocketAddr>,
9348 ) -> Result<usize> {
9349 // A probing packet must be sent, but only if the connection is fully
9350 // established. A closing connection only sends the CONNECTION_CLOSE
9351 // frame, on the active path.
9352 if self.is_established() && self.local_error.is_none() {
9353 let mut probing = self
9354 .paths
9355 .iter()
9356 .filter(|(_, p)| from.is_none() || Some(p.local_addr()) == from)
9357 .filter(|(_, p)| to.is_none() || Some(p.peer_addr()) == to)
9358 .filter(|(_, p)| p.active_dcid_seq.is_some())
9359 .filter(|(_, p)| p.probing_required())
9360 .map(|(pid, _)| pid);
9361
9362 if let Some(pid) = probing.next() {
9363 return Ok(pid);
9364 }
9365 }
9366
9367 if let Some((pid, p)) = self.paths.get_active_with_pid() {
9368 if from.is_some() && Some(p.local_addr()) != from {
9369 return Err(Error::Done);
9370 }
9371
9372 if to.is_some() && Some(p.peer_addr()) != to {
9373 return Err(Error::Done);
9374 }
9375
9376 return Ok(pid);
9377 };
9378
9379 Err(Error::InvalidState)
9380 }
9381
9382 /// Sets the path with identifier 'path_id' to be active.
9383 fn set_active_path(&mut self, path_id: usize, now: Instant) -> Result<()> {
9384 if let Ok(old_active_path) = self.paths.get_active_mut() {
9385 for &e in packet::Epoch::epochs(
9386 packet::Epoch::Initial..=packet::Epoch::Application,
9387 ) {
9388 let (lost_packets, lost_bytes) = old_active_path
9389 .recovery
9390 .on_path_change(e, now, &self.trace_id);
9391
9392 self.lost_count += lost_packets;
9393 self.lost_bytes += lost_bytes as u64;
9394 }
9395 }
9396
9397 self.paths.set_active_path(path_id)
9398 }
9399
9400 /// Handles potential connection migration.
9401 fn on_peer_migrated(
9402 &mut self, new_pid: usize, disable_dcid_reuse: bool, now: Instant,
9403 ) -> Result<()> {
9404 let active_path_id = self.paths.get_active_path_id()?;
9405
9406 if active_path_id == new_pid {
9407 return Ok(());
9408 }
9409
9410 self.set_active_path(new_pid, now)?;
9411
9412 let no_spare_dcid =
9413 self.paths.get_mut(new_pid)?.active_dcid_seq.is_none();
9414
9415 if no_spare_dcid && !disable_dcid_reuse {
9416 self.paths.get_mut(new_pid)?.active_dcid_seq =
9417 self.paths.get_mut(active_path_id)?.active_dcid_seq;
9418 }
9419
9420 Ok(())
9421 }
9422
9423 /// Creates a new client-side path.
9424 fn create_path_on_client(
9425 &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
9426 ) -> Result<usize> {
9427 if self.is_server {
9428 return Err(Error::InvalidState);
9429 }
9430
9431 // If we use zero-length SCID and go over our local active CID limit,
9432 // the `insert_path()` call will raise an error.
9433 if !self.ids.zero_length_scid() && self.ids.available_scids() == 0 {
9434 return Err(Error::OutOfIdentifiers);
9435 }
9436
9437 // Do we have a spare DCID? If we are using zero-length DCID, just use
9438 // the default having sequence 0 (note that if we exceed our local CID
9439 // limit, the `insert_path()` call will raise an error.
9440 let dcid_seq = if self.ids.zero_length_dcid() {
9441 0
9442 } else {
9443 self.ids
9444 .lowest_available_dcid_seq()
9445 .ok_or(Error::OutOfIdentifiers)?
9446 };
9447
9448 let mut path = path::Path::new(
9449 local_addr,
9450 peer_addr,
9451 &self.recovery_config,
9452 self.path_challenge_recv_max_queue_len,
9453 false,
9454 None,
9455 );
9456 path.active_dcid_seq = Some(dcid_seq);
9457
9458 let pid = self
9459 .paths
9460 .insert_path(path, false)
9461 .map_err(|_| Error::OutOfIdentifiers)?;
9462 self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
9463
9464 Ok(pid)
9465 }
9466
9467 // Marks the connection as closed and does any related tidyup.
9468 fn mark_closed(&mut self) {
9469 #[cfg(feature = "qlog")]
9470 {
9471 let cc = match (self.is_established(), self.timed_out, &self.peer_error, &self.local_error) {
9472 (false, _, _, _) => qlog::events::quic::ConnectionClosed {
9473 initiator: Some(TransportInitiator::Local),
9474 connection_error: None,
9475 application_error: None,
9476 error_code: None,
9477 internal_code: None,
9478 reason: Some("Failed to establish connection".to_string()),
9479 trigger: Some(qlog::events::quic::ConnectionClosedTrigger::HandshakeTimeout)
9480 },
9481
9482 (true, true, _, _) => qlog::events::quic::ConnectionClosed {
9483 initiator: Some(TransportInitiator::Local),
9484 connection_error: None,
9485 application_error: None,
9486 error_code: None,
9487 internal_code: None,
9488 reason: Some("Idle timeout".to_string()),
9489 trigger: Some(qlog::events::quic::ConnectionClosedTrigger::IdleTimeout)
9490 },
9491
9492 (true, false, Some(peer_error), None) => {
9493 let (connection_code, application_error, trigger) = if peer_error.is_app {
9494 (None, Some(qlog::events::ApplicationError::Unknown), None)
9495 } else {
9496 let trigger = if peer_error.error_code == WireErrorCode::NoError as u64 {
9497 Some(qlog::events::quic::ConnectionClosedTrigger::Clean)
9498 } else {
9499 Some(qlog::events::quic::ConnectionClosedTrigger::Error)
9500 };
9501
9502 (Some(qlog::events::ConnectionClosedEventError::TransportError(qlog::events::quic::TransportError::Unknown)), None, trigger)
9503 };
9504
9505 // TODO: select more appopriate connection_code and application_error than unknown.
9506 qlog::events::quic::ConnectionClosed {
9507 initiator: Some(TransportInitiator::Remote),
9508 connection_error: connection_code,
9509 application_error,
9510 error_code: Some(peer_error.error_code),
9511 internal_code: None,
9512 reason: Some(String::from_utf8_lossy(&peer_error.reason).to_string()),
9513 trigger,
9514 }
9515 },
9516
9517 (true, false, None, Some(local_error)) => {
9518 let (connection_code, application_error, trigger) = if local_error.is_app {
9519 (None, Some(qlog::events::ApplicationError::Unknown), None)
9520 } else {
9521 let trigger = if local_error.error_code == WireErrorCode::NoError as u64 {
9522 Some(qlog::events::quic::ConnectionClosedTrigger::Clean)
9523 } else {
9524 Some(qlog::events::quic::ConnectionClosedTrigger::Error)
9525 };
9526
9527 (Some(qlog::events::ConnectionClosedEventError::TransportError(qlog::events::quic::TransportError::Unknown)), None, trigger)
9528 };
9529
9530 // TODO: select more appopriate connection_code and application_error than unknown.
9531 qlog::events::quic::ConnectionClosed {
9532 initiator: Some(TransportInitiator::Local),
9533 connection_error: connection_code,
9534 application_error,
9535 error_code: Some(local_error.error_code),
9536 internal_code: None,
9537 reason: Some(String::from_utf8_lossy(&local_error.reason).to_string()),
9538 trigger,
9539 }
9540 },
9541
9542 _ => qlog::events::quic::ConnectionClosed {
9543 initiator: None,
9544 connection_error: None,
9545 application_error: None,
9546 error_code: None,
9547 internal_code: None,
9548 reason: None,
9549 trigger: None,
9550 },
9551 };
9552
9553 qlog_with_type!(QLOG_CONNECTION_CLOSED, self.qlog, q, {
9554 let ev_data = EventData::QuicConnectionClosed(cc);
9555
9556 q.add_event_data_now(ev_data).ok();
9557 });
9558 self.qlog.streamer = None;
9559 }
9560 self.closed = true;
9561 }
9562}
9563
9564#[cfg(feature = "boringssl-boring-crate")]
9565impl<F: BufFactory> AsMut<boring::ssl::SslRef> for Connection<F> {
9566 fn as_mut(&mut self) -> &mut boring::ssl::SslRef {
9567 self.handshake.ssl_mut()
9568 }
9569}
9570
9571/// Maps an `Error` to `Error::Done`, or itself.
9572///
9573/// When a received packet that hasn't yet been authenticated triggers a failure
9574/// it should, in most cases, be ignored, instead of raising a connection error,
9575/// to avoid potential man-in-the-middle and man-on-the-side attacks.
9576///
9577/// However, if no other packet was previously received, the connection should
9578/// indeed be closed as the received packet might just be network background
9579/// noise, and it shouldn't keep resources occupied indefinitely.
9580///
9581/// This function maps an error to `Error::Done` to ignore a packet failure
9582/// without aborting the connection, except when no other packet was previously
9583/// received, in which case the error itself is returned, but only on the
9584/// server-side as the client will already have armed the idle timer.
9585///
9586/// This must only be used for errors preceding packet authentication. Failures
9587/// happening after a packet has been authenticated should still cause the
9588/// connection to be aborted.
9589fn drop_pkt_on_err(
9590 e: Error, recv_count: usize, is_server: bool, trace_id: &str,
9591) -> Error {
9592 // On the server, if no other packet has been successfully processed, abort
9593 // the connection to avoid keeping the connection open when only junk is
9594 // received.
9595 if is_server && recv_count == 0 {
9596 return e;
9597 }
9598
9599 trace!("{trace_id} dropped invalid packet");
9600
9601 // Ignore other invalid packets that haven't been authenticated to prevent
9602 // man-in-the-middle and man-on-the-side attacks.
9603 Error::Done
9604}
9605
9606struct AddrTupleFmt(SocketAddr, SocketAddr);
9607
9608impl std::fmt::Display for AddrTupleFmt {
9609 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
9610 let AddrTupleFmt(src, dst) = &self;
9611
9612 if src.ip().is_unspecified() || dst.ip().is_unspecified() {
9613 return Ok(());
9614 }
9615
9616 f.write_fmt(format_args!("src:{src} dst:{dst}"))
9617 }
9618}
9619
9620/// Statistics about the connection.
9621///
9622/// A connection's statistics can be collected using the [`stats()`] method.
9623///
9624/// [`stats()`]: struct.Connection.html#method.stats
9625#[derive(Clone, Default)]
9626#[non_exhaustive]
9627pub struct Stats {
9628 /// The number of QUIC packets received.
9629 pub recv: usize,
9630
9631 /// The number of QUIC packets sent.
9632 pub sent: usize,
9633
9634 /// The number of QUIC packets that were lost.
9635 pub lost: usize,
9636
9637 /// The number of QUIC packets that were marked as lost but later acked.
9638 pub spurious_lost: usize,
9639
9640 /// The number of sent QUIC packets with retransmitted data.
9641 pub retrans: usize,
9642
9643 /// The number of sent bytes.
9644 pub sent_bytes: u64,
9645
9646 /// The number of received bytes.
9647 pub recv_bytes: u64,
9648
9649 /// The number of bytes sent acked.
9650 pub acked_bytes: u64,
9651
9652 /// The number of bytes sent lost.
9653 pub lost_bytes: u64,
9654
9655 /// The number of stream bytes retransmitted.
9656 pub stream_retrans_bytes: u64,
9657
9658 /// The number of DATAGRAM frames received.
9659 pub dgram_recv: usize,
9660
9661 /// The number of DATAGRAM frames sent.
9662 pub dgram_sent: usize,
9663
9664 /// The number of known paths for the connection.
9665 pub paths_count: usize,
9666
9667 /// The number of streams reset by local.
9668 pub reset_stream_count_local: u64,
9669
9670 /// The number of streams stopped by local.
9671 pub stopped_stream_count_local: u64,
9672
9673 /// The number of streams reset by remote.
9674 pub reset_stream_count_remote: u64,
9675
9676 /// The number of streams stopped by remote.
9677 pub stopped_stream_count_remote: u64,
9678
9679 /// The number of DATA_BLOCKED frames sent due to hitting the connection
9680 /// flow control limit.
9681 pub data_blocked_sent_count: u64,
9682
9683 /// The number of STREAM_DATA_BLOCKED frames sent due to a stream hitting
9684 /// the stream flow control limit.
9685 pub stream_data_blocked_sent_count: u64,
9686
9687 /// The number of DATA_BLOCKED frames received from the remote.
9688 pub data_blocked_recv_count: u64,
9689
9690 /// The number of STREAM_DATA_BLOCKED frames received from the remote.
9691 pub stream_data_blocked_recv_count: u64,
9692
9693 /// The number of STREAMS_BLOCKED frames for bidirectional streams received
9694 /// from the remote, indicating the peer is blocked on opening new
9695 /// bidirectional streams.
9696 pub streams_blocked_bidi_recv_count: u64,
9697
9698 /// The number of STREAMS_BLOCKED frames for unidirectional streams received
9699 /// from the remote, indicating the peer is blocked on opening new
9700 /// unidirectional streams.
9701 pub streams_blocked_uni_recv_count: u64,
9702
9703 /// The total number of PATH_CHALLENGE frames that were received.
9704 pub path_challenge_rx_count: u64,
9705
9706 /// The number of times send() was blocked because the anti-amplification
9707 /// budget (bytes received × max_amplification_factor) was exhausted.
9708 pub amplification_limited_count: u64,
9709
9710 /// Total duration during which this side of the connection was
9711 /// actively sending bytes or waiting for those bytes to be acked.
9712 pub bytes_in_flight_duration: Duration,
9713
9714 /// Health state of the connection's tx_buffered.
9715 ///
9716 /// Indicates whether the streams.tx_buffered value is consistent with
9717 /// the actual sum of bytes buffered across all stream send buffers.
9718 /// Returns `Ok` if consistent, `Inconsistent` if there's a mismatch.
9719 pub tx_buffered_state: TxBufferTrackingState,
9720}
9721
9722impl std::fmt::Debug for Stats {
9723 #[inline]
9724 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
9725 write!(
9726 f,
9727 "recv={} sent={} lost={} retrans={}",
9728 self.recv, self.sent, self.lost, self.retrans,
9729 )?;
9730
9731 write!(
9732 f,
9733 " sent_bytes={} recv_bytes={} lost_bytes={}",
9734 self.sent_bytes, self.recv_bytes, self.lost_bytes,
9735 )?;
9736
9737 Ok(())
9738 }
9739}
9740
9741#[doc(hidden)]
9742#[cfg(any(test, feature = "internal"))]
9743pub mod test_utils;
9744
9745#[cfg(test)]
9746mod tests;
9747
9748pub use crate::packet::ConnectionId;
9749pub use crate::packet::Header;
9750pub use crate::packet::Type;
9751
9752pub use crate::path::PathEvent;
9753pub use crate::path::PathStats;
9754pub use crate::path::SocketAddrIter;
9755
9756pub use crate::recovery::BbrBwLoReductionStrategy;
9757pub use crate::recovery::BbrParams;
9758#[cfg(feature = "internal")]
9759pub use crate::recovery::BbrRttJumpDetector;
9760pub use crate::recovery::CongestionControlAlgorithm;
9761pub use crate::recovery::StartupExit;
9762pub use crate::recovery::StartupExitReason;
9763
9764pub use crate::stream::StreamIter;
9765
9766pub use crate::transport_params::TransportParams;
9767pub use crate::transport_params::UnknownTransportParameter;
9768pub use crate::transport_params::UnknownTransportParameterIterator;
9769pub use crate::transport_params::UnknownTransportParameters;
9770pub use crate::transport_params::MAX_ACK_DELAY_EXPONENT;
9771
9772pub use crate::buffers::BufFactory;
9773pub use crate::buffers::BufSplit;
9774
9775pub use crate::error::ConnectionError;
9776pub use crate::error::Error;
9777pub use crate::error::Result;
9778pub use crate::error::WireErrorCode;
9779
9780mod buffers;
9781mod cid;
9782mod crypto;
9783mod dgram;
9784mod error;
9785#[cfg(feature = "ffi")]
9786mod ffi;
9787mod flowcontrol;
9788mod frame;
9789pub mod h3;
9790mod minmax;
9791mod packet;
9792mod path;
9793mod pmtud;
9794mod rand;
9795mod range_buf;
9796mod ranges;
9797mod recovery;
9798mod stream;
9799mod tls;
9800mod transport_params;