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-vendored` (default): Build the vendored BoringSSL library.
363//!
364//! * `boringssl-boring-crate`: Use the BoringSSL library provided by the
365//!   [boring] crate. It takes precedence over `boringssl-vendored` if both
366//!   features are enabled.
367//!
368//! * `pkg-config-meta`: Generate pkg-config metadata file for libquiche.
369//!
370//! * `ffi`: Build and expose the FFI API.
371//!
372//! * `qlog`: Enable support for the [qlog] logging format.
373//!
374//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
375//! [boring]: https://crates.io/crates/boring
376//! [qlog]: https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema
377
378#![allow(clippy::upper_case_acronyms)]
379#![warn(missing_docs)]
380#![cfg_attr(docsrs, feature(doc_cfg))]
381
382#[macro_use]
383extern crate log;
384
385use octets::BufferTooShortError;
386#[cfg(feature = "qlog")]
387use qlog::events::connectivity::ConnectivityEventType;
388#[cfg(feature = "qlog")]
389use qlog::events::connectivity::TransportOwner;
390#[cfg(feature = "qlog")]
391use qlog::events::quic::RecoveryEventType;
392#[cfg(feature = "qlog")]
393use qlog::events::quic::TransportEventType;
394#[cfg(feature = "qlog")]
395use qlog::events::DataRecipient;
396#[cfg(feature = "qlog")]
397use qlog::events::Event;
398#[cfg(feature = "qlog")]
399use qlog::events::EventData;
400#[cfg(feature = "qlog")]
401use qlog::events::EventImportance;
402#[cfg(feature = "qlog")]
403use qlog::events::EventType;
404#[cfg(feature = "qlog")]
405use qlog::events::RawInfo;
406use stream::StreamPriorityKey;
407
408use std::cmp;
409use std::convert::TryInto;
410use std::time;
411
412use std::sync::Arc;
413
414use std::net::SocketAddr;
415
416use std::str::FromStr;
417
418use std::collections::HashSet;
419use std::collections::VecDeque;
420use std::time::Duration;
421
422use range_buf::DefaultBufFactory;
423use smallvec::SmallVec;
424
425use crate::recovery::OnAckReceivedOutcome;
426use crate::recovery::ReleaseDecision;
427
428/// The current QUIC wire version.
429pub const PROTOCOL_VERSION: u32 = PROTOCOL_VERSION_V1;
430
431/// Supported QUIC versions.
432const PROTOCOL_VERSION_V1: u32 = 0x0000_0001;
433
434/// The maximum length of a connection ID.
435pub const MAX_CONN_ID_LEN: usize = crate::packet::MAX_CID_LEN as usize;
436
437/// The minimum length of Initial packets sent by a client.
438pub const MIN_CLIENT_INITIAL_LEN: usize = 1200;
439
440#[cfg(not(feature = "fuzzing"))]
441const PAYLOAD_MIN_LEN: usize = 4;
442
443#[cfg(feature = "fuzzing")]
444// Due to the fact that in fuzzing mode we use a zero-length AEAD tag (which
445// would normally be 16 bytes), we need to adjust the minimum payload size to
446// account for that.
447const PAYLOAD_MIN_LEN: usize = 20;
448
449// PATH_CHALLENGE (9 bytes) + AEAD tag (16 bytes).
450const MIN_PROBING_SIZE: usize = 25;
451
452const MAX_AMPLIFICATION_FACTOR: usize = 3;
453
454// The maximum number of tracked packet number ranges that need to be acked.
455//
456// This represents more or less how many ack blocks can fit in a typical packet.
457const MAX_ACK_RANGES: usize = 68;
458
459// The highest possible stream ID allowed.
460const MAX_STREAM_ID: u64 = 1 << 60;
461
462// The default max_datagram_size used in congestion control.
463const MAX_SEND_UDP_PAYLOAD_SIZE: usize = 1200;
464
465// The default length of DATAGRAM queues.
466const DEFAULT_MAX_DGRAM_QUEUE_LEN: usize = 0;
467
468// The default length of PATH_CHALLENGE receive queue.
469const DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN: usize = 3;
470
471// The DATAGRAM standard recommends either none or 65536 as maximum DATAGRAM
472// frames size. We enforce the recommendation for forward compatibility.
473const MAX_DGRAM_FRAME_SIZE: u64 = 65536;
474
475// The length of the payload length field.
476const PAYLOAD_LENGTH_LEN: usize = 2;
477
478// The number of undecryptable that can be buffered.
479const MAX_UNDECRYPTABLE_PACKETS: usize = 10;
480
481const RESERVED_VERSION_MASK: u32 = 0xfafafafa;
482
483// The default size of the receiver connection flow control window.
484const DEFAULT_CONNECTION_WINDOW: u64 = 48 * 1024;
485
486// The maximum size of the receiver connection flow control window.
487const MAX_CONNECTION_WINDOW: u64 = 24 * 1024 * 1024;
488
489// How much larger the connection flow control window need to be larger than
490// the stream flow control window.
491const CONNECTION_WINDOW_FACTOR: f64 = 1.5;
492
493// How many probing packet timeouts do we tolerate before considering the path
494// validation as failed.
495const MAX_PROBING_TIMEOUTS: usize = 3;
496
497// The default initial congestion window size in terms of packet count.
498const DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS: usize = 10;
499
500// The maximum data offset that can be stored in a crypto stream.
501const MAX_CRYPTO_STREAM_OFFSET: u64 = 1 << 16;
502
503// The send capacity factor.
504const TX_CAP_FACTOR: f64 = 1.0;
505
506/// A specialized [`Result`] type for quiche operations.
507///
508/// This type is used throughout quiche's public API for any operation that
509/// can produce an error.
510///
511/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
512pub type Result<T> = std::result::Result<T, Error>;
513
514/// A QUIC error.
515#[derive(Clone, Copy, Debug, PartialEq, Eq)]
516pub enum Error {
517    /// There is no more work to do.
518    Done,
519
520    /// The provided buffer is too short.
521    BufferTooShort,
522
523    /// The provided packet cannot be parsed because its version is unknown.
524    UnknownVersion,
525
526    /// The provided packet cannot be parsed because it contains an invalid
527    /// frame.
528    InvalidFrame,
529
530    /// The provided packet cannot be parsed.
531    InvalidPacket,
532
533    /// The operation cannot be completed because the connection is in an
534    /// invalid state.
535    InvalidState,
536
537    /// The operation cannot be completed because the stream is in an
538    /// invalid state.
539    ///
540    /// The stream ID is provided as associated data.
541    InvalidStreamState(u64),
542
543    /// The peer's transport params cannot be parsed.
544    InvalidTransportParam,
545
546    /// A cryptographic operation failed.
547    CryptoFail,
548
549    /// The TLS handshake failed.
550    TlsFail,
551
552    /// The peer violated the local flow control limits.
553    FlowControl,
554
555    /// The peer violated the local stream limits.
556    StreamLimit,
557
558    /// The specified stream was stopped by the peer.
559    ///
560    /// The error code sent as part of the `STOP_SENDING` frame is provided as
561    /// associated data.
562    StreamStopped(u64),
563
564    /// The specified stream was reset by the peer.
565    ///
566    /// The error code sent as part of the `RESET_STREAM` frame is provided as
567    /// associated data.
568    StreamReset(u64),
569
570    /// The received data exceeds the stream's final size.
571    FinalSize,
572
573    /// Error in congestion control.
574    CongestionControl,
575
576    /// Too many identifiers were provided.
577    IdLimit,
578
579    /// Not enough available identifiers.
580    OutOfIdentifiers,
581
582    /// Error in key update.
583    KeyUpdate,
584
585    /// The peer sent more data in CRYPTO frames than we can buffer.
586    CryptoBufferExceeded,
587}
588
589/// QUIC error codes sent on the wire.
590///
591/// As defined in [RFC9000](https://www.rfc-editor.org/rfc/rfc9000.html#name-error-codes).
592#[derive(Copy, Clone, Debug, Eq, PartialEq)]
593pub enum WireErrorCode {
594    /// An endpoint uses this with CONNECTION_CLOSE to signal that the
595    /// connection is being closed abruptly in the absence of any error.
596    NoError              = 0x0,
597    /// The endpoint encountered an internal error and cannot continue with the
598    /// connection.
599    InternalError        = 0x1,
600    /// The server refused to accept a new connection.
601    ConnectionRefused    = 0x2,
602    /// An endpoint received more data than it permitted in its advertised data
603    /// limits; see Section 4.
604    FlowControlError     = 0x3,
605    /// An endpoint received a frame for a stream identifier that exceeded its
606    /// advertised stream limit for the corresponding stream type.
607    StreamLimitError     = 0x4,
608    /// An endpoint received a frame for a stream that was not in a state that
609    /// permitted that frame.
610    StreamStateError     = 0x5,
611    /// (1) An endpoint received a STREAM frame containing data that exceeded
612    /// the previously established final size, (2) an endpoint received a
613    /// STREAM frame or a RESET_STREAM frame containing a final size that
614    /// was lower than the size of stream data that was already received, or
615    /// (3) an endpoint received a STREAM frame or a RESET_STREAM frame
616    /// containing a different final size to the one already established.
617    FinalSizeError       = 0x6,
618    /// An endpoint received a frame that was badly formatted -- for instance, a
619    /// frame of an unknown type or an ACK frame that has more
620    /// acknowledgment ranges than the remainder of the packet could carry.
621    FrameEncodingError   = 0x7,
622    /// An endpoint received transport parameters that were badly formatted,
623    /// included an invalid value, omitted a mandatory transport parameter,
624    /// included a forbidden transport parameter, or were otherwise in
625    /// error.
626    TransportParameterError = 0x8,
627    /// An endpoint received transport parameters that were badly formatted,
628    /// included an invalid value, omitted a mandatory transport parameter,
629    /// included a forbidden transport parameter, or were otherwise in
630    /// error.
631    ConnectionIdLimitError = 0x9,
632    /// An endpoint detected an error with protocol compliance that was not
633    /// covered by more specific error codes.
634    ProtocolViolation    = 0xa,
635    /// A server received a client Initial that contained an invalid Token
636    /// field.
637    InvalidToken         = 0xb,
638    /// The application or application protocol caused the connection to be
639    /// closed.
640    ApplicationError     = 0xc,
641    /// An endpoint has received more data in CRYPTO frames than it can buffer.
642    CryptoBufferExceeded = 0xd,
643    /// An endpoint detected errors in performing key updates.
644    KeyUpdateError       = 0xe,
645    /// An endpoint has reached the confidentiality or integrity limit for the
646    /// AEAD algorithm used by the given connection.
647    AeadLimitReached     = 0xf,
648    /// An endpoint has determined that the network path is incapable of
649    /// supporting QUIC. An endpoint is unlikely to receive a
650    /// CONNECTION_CLOSE frame carrying this code except when the path does
651    /// not support a large enough MTU.
652    NoViablePath         = 0x10,
653}
654
655impl Error {
656    fn to_wire(self) -> u64 {
657        match self {
658            Error::Done => WireErrorCode::NoError as u64,
659            Error::InvalidFrame => WireErrorCode::FrameEncodingError as u64,
660            Error::InvalidStreamState(..) =>
661                WireErrorCode::StreamStateError as u64,
662            Error::InvalidTransportParam =>
663                WireErrorCode::TransportParameterError as u64,
664            Error::FlowControl => WireErrorCode::FlowControlError as u64,
665            Error::StreamLimit => WireErrorCode::StreamLimitError as u64,
666            Error::IdLimit => WireErrorCode::ConnectionIdLimitError as u64,
667            Error::FinalSize => WireErrorCode::FinalSizeError as u64,
668            Error::CryptoBufferExceeded =>
669                WireErrorCode::CryptoBufferExceeded as u64,
670            Error::KeyUpdate => WireErrorCode::KeyUpdateError as u64,
671            _ => WireErrorCode::ProtocolViolation as u64,
672        }
673    }
674
675    #[cfg(feature = "ffi")]
676    fn to_c(self) -> libc::ssize_t {
677        match self {
678            Error::Done => -1,
679            Error::BufferTooShort => -2,
680            Error::UnknownVersion => -3,
681            Error::InvalidFrame => -4,
682            Error::InvalidPacket => -5,
683            Error::InvalidState => -6,
684            Error::InvalidStreamState(_) => -7,
685            Error::InvalidTransportParam => -8,
686            Error::CryptoFail => -9,
687            Error::TlsFail => -10,
688            Error::FlowControl => -11,
689            Error::StreamLimit => -12,
690            Error::FinalSize => -13,
691            Error::CongestionControl => -14,
692            Error::StreamStopped { .. } => -15,
693            Error::StreamReset { .. } => -16,
694            Error::IdLimit => -17,
695            Error::OutOfIdentifiers => -18,
696            Error::KeyUpdate => -19,
697            Error::CryptoBufferExceeded => -20,
698        }
699    }
700}
701
702impl std::fmt::Display for Error {
703    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
704        write!(f, "{self:?}")
705    }
706}
707
708impl std::error::Error for Error {
709    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
710        None
711    }
712}
713
714impl std::convert::From<octets::BufferTooShortError> for Error {
715    fn from(_err: octets::BufferTooShortError) -> Self {
716        Error::BufferTooShort
717    }
718}
719
720/// Ancillary information about incoming packets.
721#[derive(Clone, Copy, Debug, PartialEq, Eq)]
722pub struct RecvInfo {
723    /// The remote address the packet was received from.
724    pub from: SocketAddr,
725
726    /// The local address the packet was received on.
727    pub to: SocketAddr,
728}
729
730/// Ancillary information about outgoing packets.
731#[derive(Clone, Copy, Debug, PartialEq, Eq)]
732pub struct SendInfo {
733    /// The local address the packet should be sent from.
734    pub from: SocketAddr,
735
736    /// The remote address the packet should be sent to.
737    pub to: SocketAddr,
738
739    /// The time to send the packet out.
740    ///
741    /// See [Pacing] for more details.
742    ///
743    /// [Pacing]: index.html#pacing
744    pub at: time::Instant,
745}
746
747/// Represents information carried by `CONNECTION_CLOSE` frames.
748#[derive(Clone, Debug, PartialEq, Eq)]
749pub struct ConnectionError {
750    /// Whether the error came from the application or the transport layer.
751    pub is_app: bool,
752
753    /// The error code carried by the `CONNECTION_CLOSE` frame.
754    pub error_code: u64,
755
756    /// The reason carried by the `CONNECTION_CLOSE` frame.
757    pub reason: Vec<u8>,
758}
759
760/// The side of the stream to be shut down.
761///
762/// This should be used when calling [`stream_shutdown()`].
763///
764/// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
765#[repr(C)]
766#[derive(PartialEq, Eq)]
767pub enum Shutdown {
768    /// Stop receiving stream data.
769    Read  = 0,
770
771    /// Stop sending stream data.
772    Write = 1,
773}
774
775/// Qlog logging level.
776#[repr(C)]
777#[cfg(feature = "qlog")]
778#[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
779pub enum QlogLevel {
780    /// Logs any events of Core importance.
781    Core  = 0,
782
783    /// Logs any events of Core and Base importance.
784    Base  = 1,
785
786    /// Logs any events of Core, Base and Extra importance
787    Extra = 2,
788}
789
790/// Stores configuration shared between multiple connections.
791pub struct Config {
792    local_transport_params: TransportParams,
793
794    version: u32,
795
796    tls_ctx: tls::Context,
797
798    application_protos: Vec<Vec<u8>>,
799
800    grease: bool,
801
802    cc_algorithm: CongestionControlAlgorithm,
803    custom_bbr_params: Option<BbrParams>,
804    initial_congestion_window_packets: usize,
805
806    pmtud: bool,
807
808    hystart: bool,
809
810    pacing: bool,
811    /// Send rate limit in Mbps
812    max_pacing_rate: Option<u64>,
813
814    tx_cap_factor: f64,
815
816    dgram_recv_max_queue_len: usize,
817    dgram_send_max_queue_len: usize,
818
819    path_challenge_recv_max_queue_len: usize,
820
821    max_send_udp_payload_size: usize,
822
823    max_connection_window: u64,
824    max_stream_window: u64,
825
826    max_amplification_factor: usize,
827
828    disable_dcid_reuse: bool,
829
830    track_unknown_transport_params: Option<usize>,
831}
832
833// See https://quicwg.org/base-drafts/rfc9000.html#section-15
834fn is_reserved_version(version: u32) -> bool {
835    version & RESERVED_VERSION_MASK == version
836}
837
838impl Config {
839    /// Creates a config object with the given version.
840    ///
841    /// ## Examples:
842    ///
843    /// ```
844    /// let config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
845    /// # Ok::<(), quiche::Error>(())
846    /// ```
847    pub fn new(version: u32) -> Result<Config> {
848        Self::with_tls_ctx(version, tls::Context::new()?)
849    }
850
851    /// Creates a config object with the given version and
852    /// [`SslContextBuilder`].
853    ///
854    /// This is useful for applications that wish to manually configure
855    /// [`SslContextBuilder`].
856    ///
857    /// [`SslContextBuilder`]: https://docs.rs/boring/latest/boring/ssl/struct.SslContextBuilder.html
858    #[cfg(feature = "boringssl-boring-crate")]
859    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
860    pub fn with_boring_ssl_ctx_builder(
861        version: u32, tls_ctx_builder: boring::ssl::SslContextBuilder,
862    ) -> Result<Config> {
863        Self::with_tls_ctx(version, tls::Context::from_boring(tls_ctx_builder))
864    }
865
866    fn with_tls_ctx(version: u32, tls_ctx: tls::Context) -> Result<Config> {
867        if !is_reserved_version(version) && !version_is_supported(version) {
868            return Err(Error::UnknownVersion);
869        }
870
871        Ok(Config {
872            local_transport_params: TransportParams::default(),
873            version,
874            tls_ctx,
875            application_protos: Vec::new(),
876            grease: true,
877            cc_algorithm: CongestionControlAlgorithm::CUBIC,
878            custom_bbr_params: None,
879            initial_congestion_window_packets:
880                DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS,
881            pmtud: false,
882            hystart: true,
883            pacing: true,
884            max_pacing_rate: None,
885
886            tx_cap_factor: TX_CAP_FACTOR,
887
888            dgram_recv_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
889            dgram_send_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
890
891            path_challenge_recv_max_queue_len:
892                DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN,
893
894            max_send_udp_payload_size: MAX_SEND_UDP_PAYLOAD_SIZE,
895
896            max_connection_window: MAX_CONNECTION_WINDOW,
897            max_stream_window: stream::MAX_STREAM_WINDOW,
898
899            max_amplification_factor: MAX_AMPLIFICATION_FACTOR,
900
901            disable_dcid_reuse: false,
902
903            track_unknown_transport_params: None,
904        })
905    }
906
907    /// Configures the given certificate chain.
908    ///
909    /// The content of `file` is parsed as a PEM-encoded leaf certificate,
910    /// followed by optional intermediate certificates.
911    ///
912    /// ## Examples:
913    ///
914    /// ```no_run
915    /// # let mut config = quiche::Config::new(0xbabababa)?;
916    /// config.load_cert_chain_from_pem_file("/path/to/cert.pem")?;
917    /// # Ok::<(), quiche::Error>(())
918    /// ```
919    pub fn load_cert_chain_from_pem_file(&mut self, file: &str) -> Result<()> {
920        self.tls_ctx.use_certificate_chain_file(file)
921    }
922
923    /// Configures the given private key.
924    ///
925    /// The content of `file` is parsed as a PEM-encoded private key.
926    ///
927    /// ## Examples:
928    ///
929    /// ```no_run
930    /// # let mut config = quiche::Config::new(0xbabababa)?;
931    /// config.load_priv_key_from_pem_file("/path/to/key.pem")?;
932    /// # Ok::<(), quiche::Error>(())
933    /// ```
934    pub fn load_priv_key_from_pem_file(&mut self, file: &str) -> Result<()> {
935        self.tls_ctx.use_privkey_file(file)
936    }
937
938    /// Specifies a file where trusted CA certificates are stored for the
939    /// purposes of certificate verification.
940    ///
941    /// The content of `file` is parsed as a PEM-encoded certificate chain.
942    ///
943    /// ## Examples:
944    ///
945    /// ```no_run
946    /// # let mut config = quiche::Config::new(0xbabababa)?;
947    /// config.load_verify_locations_from_file("/path/to/cert.pem")?;
948    /// # Ok::<(), quiche::Error>(())
949    /// ```
950    pub fn load_verify_locations_from_file(&mut self, file: &str) -> Result<()> {
951        self.tls_ctx.load_verify_locations_from_file(file)
952    }
953
954    /// Specifies a directory where trusted CA certificates are stored for the
955    /// purposes of certificate verification.
956    ///
957    /// The content of `dir` a set of PEM-encoded certificate chains.
958    ///
959    /// ## Examples:
960    ///
961    /// ```no_run
962    /// # let mut config = quiche::Config::new(0xbabababa)?;
963    /// config.load_verify_locations_from_directory("/path/to/certs")?;
964    /// # Ok::<(), quiche::Error>(())
965    /// ```
966    pub fn load_verify_locations_from_directory(
967        &mut self, dir: &str,
968    ) -> Result<()> {
969        self.tls_ctx.load_verify_locations_from_directory(dir)
970    }
971
972    /// Configures whether to verify the peer's certificate.
973    ///
974    /// This should usually be `true` for client-side connections and `false`
975    /// for server-side ones.
976    ///
977    /// Note that by default, no verification is performed.
978    ///
979    /// Also note that on the server-side, enabling verification of the peer
980    /// will trigger a certificate request and make authentication errors
981    /// fatal, but will still allow anonymous clients (i.e. clients that
982    /// don't present a certificate at all). Servers can check whether a
983    /// client presented a certificate by calling [`peer_cert()`] if they
984    /// need to.
985    ///
986    /// [`peer_cert()`]: struct.Connection.html#method.peer_cert
987    pub fn verify_peer(&mut self, verify: bool) {
988        self.tls_ctx.set_verify(verify);
989    }
990
991    /// Configures whether to do path MTU discovery.
992    ///
993    /// The default value is `false`.
994    pub fn discover_pmtu(&mut self, discover: bool) {
995        self.pmtud = discover;
996    }
997
998    /// Configures whether to send GREASE values.
999    ///
1000    /// The default value is `true`.
1001    pub fn grease(&mut self, grease: bool) {
1002        self.grease = grease;
1003    }
1004
1005    /// Enables logging of secrets.
1006    ///
1007    /// When logging is enabled, the [`set_keylog()`] method must be called on
1008    /// the connection for its cryptographic secrets to be logged in the
1009    /// [keylog] format to the specified writer.
1010    ///
1011    /// [`set_keylog()`]: struct.Connection.html#method.set_keylog
1012    /// [keylog]: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
1013    pub fn log_keys(&mut self) {
1014        self.tls_ctx.enable_keylog();
1015    }
1016
1017    /// Configures the session ticket key material.
1018    ///
1019    /// On the server this key will be used to encrypt and decrypt session
1020    /// tickets, used to perform session resumption without server-side state.
1021    ///
1022    /// By default a key is generated internally, and rotated regularly, so
1023    /// applications don't need to call this unless they need to use a
1024    /// specific key (e.g. in order to support resumption across multiple
1025    /// servers), in which case the application is also responsible for
1026    /// rotating the key to provide forward secrecy.
1027    pub fn set_ticket_key(&mut self, key: &[u8]) -> Result<()> {
1028        self.tls_ctx.set_ticket_key(key)
1029    }
1030
1031    /// Enables sending or receiving early data.
1032    pub fn enable_early_data(&mut self) {
1033        self.tls_ctx.set_early_data_enabled(true);
1034    }
1035
1036    /// Configures the list of supported application protocols.
1037    ///
1038    /// On the client this configures the list of protocols to send to the
1039    /// server as part of the ALPN extension.
1040    ///
1041    /// On the server this configures the list of supported protocols to match
1042    /// against the client-supplied list.
1043    ///
1044    /// Applications must set a value, but no default is provided.
1045    ///
1046    /// ## Examples:
1047    ///
1048    /// ```
1049    /// # let mut config = quiche::Config::new(0xbabababa)?;
1050    /// config.set_application_protos(&[b"http/1.1", b"http/0.9"]);
1051    /// # Ok::<(), quiche::Error>(())
1052    /// ```
1053    pub fn set_application_protos(
1054        &mut self, protos_list: &[&[u8]],
1055    ) -> Result<()> {
1056        self.application_protos =
1057            protos_list.iter().map(|s| s.to_vec()).collect();
1058
1059        self.tls_ctx.set_alpn(protos_list)
1060    }
1061
1062    /// Configures the list of supported application protocols using wire
1063    /// format.
1064    ///
1065    /// The list of protocols `protos` must be a series of non-empty, 8-bit
1066    /// length-prefixed strings.
1067    ///
1068    /// See [`set_application_protos`](Self::set_application_protos) for more
1069    /// background about application protocols.
1070    ///
1071    /// ## Examples:
1072    ///
1073    /// ```
1074    /// # let mut config = quiche::Config::new(0xbabababa)?;
1075    /// config.set_application_protos_wire_format(b"\x08http/1.1\x08http/0.9")?;
1076    /// # Ok::<(), quiche::Error>(())
1077    /// ```
1078    pub fn set_application_protos_wire_format(
1079        &mut self, protos: &[u8],
1080    ) -> Result<()> {
1081        let mut b = octets::Octets::with_slice(protos);
1082
1083        let mut protos_list = Vec::new();
1084
1085        while let Ok(proto) = b.get_bytes_with_u8_length() {
1086            protos_list.push(proto.buf());
1087        }
1088
1089        self.set_application_protos(&protos_list)
1090    }
1091
1092    /// Sets the anti-amplification limit factor.
1093    ///
1094    /// The default value is `3`.
1095    pub fn set_max_amplification_factor(&mut self, v: usize) {
1096        self.max_amplification_factor = v;
1097    }
1098
1099    /// Sets the send capacity factor.
1100    ///
1101    /// The default value is `1`.
1102    pub fn set_send_capacity_factor(&mut self, v: f64) {
1103        self.tx_cap_factor = v;
1104    }
1105
1106    /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
1107    ///
1108    /// The default value is infinite, that is, no timeout is used.
1109    pub fn set_max_idle_timeout(&mut self, v: u64) {
1110        self.local_transport_params.max_idle_timeout = v;
1111    }
1112
1113    /// Sets the `max_udp_payload_size transport` parameter.
1114    ///
1115    /// The default value is `65527`.
1116    pub fn set_max_recv_udp_payload_size(&mut self, v: usize) {
1117        self.local_transport_params.max_udp_payload_size = v as u64;
1118    }
1119
1120    /// Sets the maximum outgoing UDP payload size.
1121    ///
1122    /// The default and minimum value is `1200`.
1123    pub fn set_max_send_udp_payload_size(&mut self, v: usize) {
1124        self.max_send_udp_payload_size = cmp::max(v, MAX_SEND_UDP_PAYLOAD_SIZE);
1125    }
1126
1127    /// Sets the `initial_max_data` transport parameter.
1128    ///
1129    /// When set to a non-zero value quiche will only allow at most `v` bytes of
1130    /// incoming stream data to be buffered for the whole connection (that is,
1131    /// data that is not yet read by the application) and will allow more data
1132    /// to be received as the buffer is consumed by the application.
1133    ///
1134    /// When set to zero, either explicitly or via the default, quiche will not
1135    /// give any flow control to the peer, preventing it from sending any stream
1136    /// data.
1137    ///
1138    /// The default value is `0`.
1139    pub fn set_initial_max_data(&mut self, v: u64) {
1140        self.local_transport_params.initial_max_data = v;
1141    }
1142
1143    /// Sets the `initial_max_stream_data_bidi_local` transport parameter.
1144    ///
1145    /// When set to a non-zero value quiche will only allow at most `v` bytes
1146    /// of incoming stream data to be buffered for each locally-initiated
1147    /// bidirectional stream (that is, data that is not yet read by the
1148    /// application) and will allow more data to be received as the buffer is
1149    /// consumed by the application.
1150    ///
1151    /// When set to zero, either explicitly or via the default, quiche will not
1152    /// give any flow control to the peer, preventing it from sending any stream
1153    /// data.
1154    ///
1155    /// The default value is `0`.
1156    pub fn set_initial_max_stream_data_bidi_local(&mut self, v: u64) {
1157        self.local_transport_params
1158            .initial_max_stream_data_bidi_local = v;
1159    }
1160
1161    /// Sets the `initial_max_stream_data_bidi_remote` transport parameter.
1162    ///
1163    /// When set to a non-zero value quiche will only allow at most `v` bytes
1164    /// of incoming stream data to be buffered for each remotely-initiated
1165    /// bidirectional stream (that is, data that is not yet read by the
1166    /// application) and will allow more data to be received as the buffer is
1167    /// consumed by the application.
1168    ///
1169    /// When set to zero, either explicitly or via the default, quiche will not
1170    /// give any flow control to the peer, preventing it from sending any stream
1171    /// data.
1172    ///
1173    /// The default value is `0`.
1174    pub fn set_initial_max_stream_data_bidi_remote(&mut self, v: u64) {
1175        self.local_transport_params
1176            .initial_max_stream_data_bidi_remote = v;
1177    }
1178
1179    /// Sets the `initial_max_stream_data_uni` transport parameter.
1180    ///
1181    /// When set to a non-zero value quiche will only allow at most `v` bytes
1182    /// of incoming stream data to be buffered for each unidirectional stream
1183    /// (that is, data that is not yet read by the application) and will allow
1184    /// more data to be received as the buffer is consumed by the application.
1185    ///
1186    /// When set to zero, either explicitly or via the default, quiche will not
1187    /// give any flow control to the peer, preventing it from sending any stream
1188    /// data.
1189    ///
1190    /// The default value is `0`.
1191    pub fn set_initial_max_stream_data_uni(&mut self, v: u64) {
1192        self.local_transport_params.initial_max_stream_data_uni = v;
1193    }
1194
1195    /// Sets the `initial_max_streams_bidi` transport parameter.
1196    ///
1197    /// When set to a non-zero value quiche will only allow `v` number of
1198    /// concurrent remotely-initiated bidirectional streams to be open at any
1199    /// given time and will increase the limit automatically as streams are
1200    /// completed.
1201    ///
1202    /// When set to zero, either explicitly or via the default, quiche will not
1203    /// not allow the peer to open any bidirectional streams.
1204    ///
1205    /// A bidirectional stream is considered completed when all incoming data
1206    /// has been read by the application (up to the `fin` offset) or the
1207    /// stream's read direction has been shutdown, and all outgoing data has
1208    /// been acked by the peer (up to the `fin` offset) or the stream's write
1209    /// direction has been shutdown.
1210    ///
1211    /// The default value is `0`.
1212    pub fn set_initial_max_streams_bidi(&mut self, v: u64) {
1213        self.local_transport_params.initial_max_streams_bidi = v;
1214    }
1215
1216    /// Sets the `initial_max_streams_uni` transport parameter.
1217    ///
1218    /// When set to a non-zero value quiche will only allow `v` number of
1219    /// concurrent remotely-initiated unidirectional streams to be open at any
1220    /// given time and will increase the limit automatically as streams are
1221    /// completed.
1222    ///
1223    /// When set to zero, either explicitly or via the default, quiche will not
1224    /// not allow the peer to open any unidirectional streams.
1225    ///
1226    /// A unidirectional stream is considered completed when all incoming data
1227    /// has been read by the application (up to the `fin` offset) or the
1228    /// stream's read direction has been shutdown.
1229    ///
1230    /// The default value is `0`.
1231    pub fn set_initial_max_streams_uni(&mut self, v: u64) {
1232        self.local_transport_params.initial_max_streams_uni = v;
1233    }
1234
1235    /// Sets the `ack_delay_exponent` transport parameter.
1236    ///
1237    /// The default value is `3`.
1238    pub fn set_ack_delay_exponent(&mut self, v: u64) {
1239        self.local_transport_params.ack_delay_exponent = v;
1240    }
1241
1242    /// Sets the `max_ack_delay` transport parameter.
1243    ///
1244    /// The default value is `25`.
1245    pub fn set_max_ack_delay(&mut self, v: u64) {
1246        self.local_transport_params.max_ack_delay = v;
1247    }
1248
1249    /// Sets the `active_connection_id_limit` transport parameter.
1250    ///
1251    /// The default value is `2`. Lower values will be ignored.
1252    pub fn set_active_connection_id_limit(&mut self, v: u64) {
1253        if v >= 2 {
1254            self.local_transport_params.active_conn_id_limit = v;
1255        }
1256    }
1257
1258    /// Sets the `disable_active_migration` transport parameter.
1259    ///
1260    /// The default value is `false`.
1261    pub fn set_disable_active_migration(&mut self, v: bool) {
1262        self.local_transport_params.disable_active_migration = v;
1263    }
1264
1265    /// Sets the congestion control algorithm used.
1266    ///
1267    /// The default value is `CongestionControlAlgorithm::CUBIC`.
1268    pub fn set_cc_algorithm(&mut self, algo: CongestionControlAlgorithm) {
1269        self.cc_algorithm = algo;
1270    }
1271
1272    /// Sets custom BBR settings.
1273    ///
1274    /// This API is experimental and will be removed in the future.
1275    ///
1276    /// Currently this only applies if cc_algorithm is
1277    /// `CongestionControlAlgorithm::Bbr2Gcongestion` is set.
1278    ///
1279    /// The default value is `None`.
1280    #[cfg(feature = "internal")]
1281    #[doc(hidden)]
1282    pub fn set_custom_bbr_params(&mut self, custom_bbr_settings: BbrParams) {
1283        self.custom_bbr_params = Some(custom_bbr_settings);
1284    }
1285
1286    /// Sets the congestion control algorithm used by string.
1287    ///
1288    /// The default value is `cubic`. On error `Error::CongestionControl`
1289    /// will be returned.
1290    ///
1291    /// ## Examples:
1292    ///
1293    /// ```
1294    /// # let mut config = quiche::Config::new(0xbabababa)?;
1295    /// config.set_cc_algorithm_name("reno");
1296    /// # Ok::<(), quiche::Error>(())
1297    /// ```
1298    pub fn set_cc_algorithm_name(&mut self, name: &str) -> Result<()> {
1299        self.cc_algorithm = CongestionControlAlgorithm::from_str(name)?;
1300
1301        Ok(())
1302    }
1303
1304    /// Sets initial congestion window size in terms of packet count.
1305    ///
1306    /// The default value is 10.
1307    pub fn set_initial_congestion_window_packets(&mut self, packets: usize) {
1308        self.initial_congestion_window_packets = packets;
1309    }
1310
1311    /// Configures whether to enable HyStart++.
1312    ///
1313    /// The default value is `true`.
1314    pub fn enable_hystart(&mut self, v: bool) {
1315        self.hystart = v;
1316    }
1317
1318    /// Configures whether to enable pacing.
1319    ///
1320    /// The default value is `true`.
1321    pub fn enable_pacing(&mut self, v: bool) {
1322        self.pacing = v;
1323    }
1324
1325    /// Sets the max value for pacing rate.
1326    ///
1327    /// By default pacing rate is not limited.
1328    pub fn set_max_pacing_rate(&mut self, v: u64) {
1329        self.max_pacing_rate = Some(v);
1330    }
1331
1332    /// Configures whether to enable receiving DATAGRAM frames.
1333    ///
1334    /// When enabled, the `max_datagram_frame_size` transport parameter is set
1335    /// to 65536 as recommended by draft-ietf-quic-datagram-01.
1336    ///
1337    /// The default is `false`.
1338    pub fn enable_dgram(
1339        &mut self, enabled: bool, recv_queue_len: usize, send_queue_len: usize,
1340    ) {
1341        self.local_transport_params.max_datagram_frame_size = if enabled {
1342            Some(MAX_DGRAM_FRAME_SIZE)
1343        } else {
1344            None
1345        };
1346        self.dgram_recv_max_queue_len = recv_queue_len;
1347        self.dgram_send_max_queue_len = send_queue_len;
1348    }
1349
1350    /// Configures the max number of queued received PATH_CHALLENGE frames.
1351    ///
1352    /// When an endpoint receives a PATH_CHALLENGE frame and the queue is full,
1353    /// the frame is discarded.
1354    ///
1355    /// The default is 3.
1356    pub fn set_path_challenge_recv_max_queue_len(&mut self, queue_len: usize) {
1357        self.path_challenge_recv_max_queue_len = queue_len;
1358    }
1359
1360    /// Sets the maximum size of the connection window.
1361    ///
1362    /// The default value is MAX_CONNECTION_WINDOW (24MBytes).
1363    pub fn set_max_connection_window(&mut self, v: u64) {
1364        self.max_connection_window = v;
1365    }
1366
1367    /// Sets the maximum size of the stream window.
1368    ///
1369    /// The default value is MAX_STREAM_WINDOW (16MBytes).
1370    pub fn set_max_stream_window(&mut self, v: u64) {
1371        self.max_stream_window = v;
1372    }
1373
1374    /// Sets the initial stateless reset token.
1375    ///
1376    /// This value is only advertised by servers. Setting a stateless retry
1377    /// token as a client has no effect on the connection.
1378    ///
1379    /// The default value is `None`.
1380    pub fn set_stateless_reset_token(&mut self, v: Option<u128>) {
1381        self.local_transport_params.stateless_reset_token = v;
1382    }
1383
1384    /// Sets whether the QUIC connection should avoid reusing DCIDs over
1385    /// different paths.
1386    ///
1387    /// When set to `true`, it ensures that a destination Connection ID is never
1388    /// reused on different paths. Such behaviour may lead to connection stall
1389    /// if the peer performs a non-voluntary migration (e.g., NAT rebinding) and
1390    /// does not provide additional destination Connection IDs to handle such
1391    /// event.
1392    ///
1393    /// The default value is `false`.
1394    pub fn set_disable_dcid_reuse(&mut self, v: bool) {
1395        self.disable_dcid_reuse = v;
1396    }
1397
1398    /// Enables tracking unknown transport parameters.
1399    ///
1400    /// Specify the maximum number of bytes used to track unknown transport
1401    /// parameters. The size includes the identifier and its value. If storing a
1402    /// transport parameter would cause the limit to be exceeded, it is quietly
1403    /// dropped.
1404    ///
1405    /// The default is that the feature is disabled.
1406    pub fn enable_track_unknown_transport_parameters(&mut self, size: usize) {
1407        self.track_unknown_transport_params = Some(size);
1408    }
1409}
1410
1411/// A QUIC connection.
1412pub struct Connection<F = DefaultBufFactory>
1413where
1414    F: BufFactory,
1415{
1416    /// QUIC wire version used for the connection.
1417    version: u32,
1418
1419    /// Connection Identifiers.
1420    ids: cid::ConnectionIdentifiers,
1421
1422    /// Unique opaque ID for the connection that can be used for logging.
1423    trace_id: String,
1424
1425    /// Packet number spaces.
1426    pkt_num_spaces: [packet::PktNumSpace; packet::Epoch::count()],
1427
1428    /// The crypto context.
1429    crypto_ctx: [packet::CryptoContext; packet::Epoch::count()],
1430
1431    /// Next packet number.
1432    next_pkt_num: u64,
1433
1434    /// Peer's transport parameters.
1435    peer_transport_params: TransportParams,
1436
1437    /// If tracking unknown transport parameters from a peer, how much space to
1438    /// use in bytes.
1439    peer_transport_params_track_unknown: Option<usize>,
1440
1441    /// Local transport parameters.
1442    local_transport_params: TransportParams,
1443
1444    /// TLS handshake state.
1445    handshake: tls::Handshake,
1446
1447    /// Serialized TLS session buffer.
1448    ///
1449    /// This field is populated when a new session ticket is processed on the
1450    /// client. On the server this is empty.
1451    session: Option<Vec<u8>>,
1452
1453    /// The configuration for recovery.
1454    recovery_config: recovery::RecoveryConfig,
1455
1456    /// The path manager.
1457    paths: path::PathMap,
1458
1459    /// PATH_CHALLENGE receive queue max length.
1460    path_challenge_recv_max_queue_len: usize,
1461
1462    /// Total number of received PATH_CHALLENGE frames.
1463    path_challenge_rx_count: u64,
1464
1465    /// List of supported application protocols.
1466    application_protos: Vec<Vec<u8>>,
1467
1468    /// Total number of received packets.
1469    recv_count: usize,
1470
1471    /// Total number of sent packets.
1472    sent_count: usize,
1473
1474    /// Total number of lost packets.
1475    lost_count: usize,
1476
1477    /// Total number of lost packets that were later acked.
1478    spurious_lost_count: usize,
1479
1480    /// Total number of packets sent with data retransmitted.
1481    retrans_count: usize,
1482
1483    /// Total number of sent DATAGRAM frames.
1484    dgram_sent_count: usize,
1485
1486    /// Total number of received DATAGRAM frames.
1487    dgram_recv_count: usize,
1488
1489    /// Total number of bytes received from the peer.
1490    rx_data: u64,
1491
1492    /// Receiver flow controller.
1493    flow_control: flowcontrol::FlowControl,
1494
1495    /// Whether we send MAX_DATA frame.
1496    almost_full: bool,
1497
1498    /// Number of stream data bytes that can be buffered.
1499    tx_cap: usize,
1500
1501    /// The send capacity factor.
1502    tx_cap_factor: f64,
1503
1504    /// Number of bytes buffered in the send buffer.
1505    tx_buffered: usize,
1506
1507    /// Total number of bytes sent to the peer.
1508    tx_data: u64,
1509
1510    /// Peer's flow control limit for the connection.
1511    max_tx_data: u64,
1512
1513    /// Last tx_data before running a full send() loop.
1514    last_tx_data: u64,
1515
1516    /// Total number of bytes retransmitted over the connection.
1517    /// This counts only STREAM and CRYPTO data.
1518    stream_retrans_bytes: u64,
1519
1520    /// Total number of bytes sent over the connection.
1521    sent_bytes: u64,
1522
1523    /// Total number of bytes received over the connection.
1524    recv_bytes: u64,
1525
1526    /// Total number of bytes sent acked over the connection.
1527    acked_bytes: u64,
1528
1529    /// Total number of bytes sent lost over the connection.
1530    lost_bytes: u64,
1531
1532    /// Streams map, indexed by stream ID.
1533    streams: stream::StreamMap<F>,
1534
1535    /// Peer's original destination connection ID. Used by the client to
1536    /// validate the server's transport parameter.
1537    odcid: Option<ConnectionId<'static>>,
1538
1539    /// Peer's retry source connection ID. Used by the client during stateless
1540    /// retry to validate the server's transport parameter.
1541    rscid: Option<ConnectionId<'static>>,
1542
1543    /// Received address verification token.
1544    token: Option<Vec<u8>>,
1545
1546    /// Error code and reason to be sent to the peer in a CONNECTION_CLOSE
1547    /// frame.
1548    local_error: Option<ConnectionError>,
1549
1550    /// Error code and reason received from the peer in a CONNECTION_CLOSE
1551    /// frame.
1552    peer_error: Option<ConnectionError>,
1553
1554    /// The connection-level limit at which send blocking occurred.
1555    blocked_limit: Option<u64>,
1556
1557    /// Idle timeout expiration time.
1558    idle_timer: Option<time::Instant>,
1559
1560    /// Draining timeout expiration time.
1561    draining_timer: Option<time::Instant>,
1562
1563    /// List of raw packets that were received before they could be decrypted.
1564    undecryptable_pkts: VecDeque<(Vec<u8>, RecvInfo)>,
1565
1566    /// The negotiated ALPN protocol.
1567    alpn: Vec<u8>,
1568
1569    /// Whether this is a server-side connection.
1570    is_server: bool,
1571
1572    /// Whether the initial secrets have been derived.
1573    derived_initial_secrets: bool,
1574
1575    /// Whether a version negotiation packet has already been received. Only
1576    /// relevant for client connections.
1577    did_version_negotiation: bool,
1578
1579    /// Whether stateless retry has been performed.
1580    did_retry: bool,
1581
1582    /// Whether the peer already updated its connection ID.
1583    got_peer_conn_id: bool,
1584
1585    /// Whether the peer verified our initial address.
1586    peer_verified_initial_address: bool,
1587
1588    /// Whether the peer's transport parameters were parsed.
1589    parsed_peer_transport_params: bool,
1590
1591    /// Whether the connection handshake has been completed.
1592    handshake_completed: bool,
1593
1594    /// Whether the HANDSHAKE_DONE frame has been sent.
1595    handshake_done_sent: bool,
1596
1597    /// Whether the HANDSHAKE_DONE frame has been acked.
1598    handshake_done_acked: bool,
1599
1600    /// Whether the connection handshake has been confirmed.
1601    handshake_confirmed: bool,
1602
1603    /// Key phase bit used for outgoing protected packets.
1604    key_phase: bool,
1605
1606    /// Whether an ack-eliciting packet has been sent since last receiving a
1607    /// packet.
1608    ack_eliciting_sent: bool,
1609
1610    /// Whether the connection is closed.
1611    closed: bool,
1612
1613    /// Whether the connection was timed out.
1614    timed_out: bool,
1615
1616    /// Whether to send GREASE.
1617    grease: bool,
1618
1619    /// TLS keylog writer.
1620    keylog: Option<Box<dyn std::io::Write + Send + Sync>>,
1621
1622    #[cfg(feature = "qlog")]
1623    qlog: QlogInfo,
1624
1625    /// DATAGRAM queues.
1626    dgram_recv_queue: dgram::DatagramQueue,
1627    dgram_send_queue: dgram::DatagramQueue,
1628
1629    /// Whether to emit DATAGRAM frames in the next packet.
1630    emit_dgram: bool,
1631
1632    /// Whether the connection should prevent from reusing destination
1633    /// Connection IDs when the peer migrates.
1634    disable_dcid_reuse: bool,
1635
1636    /// The number of streams reset by local.
1637    reset_stream_local_count: u64,
1638
1639    /// The number of streams stopped by local.
1640    stopped_stream_local_count: u64,
1641
1642    /// The number of streams reset by remote.
1643    reset_stream_remote_count: u64,
1644
1645    /// The number of streams stopped by remote.
1646    stopped_stream_remote_count: u64,
1647
1648    /// The anti-amplification limit factor.
1649    max_amplification_factor: usize,
1650}
1651
1652/// Creates a new server-side connection.
1653///
1654/// The `scid` parameter represents the server's source connection ID, while
1655/// the optional `odcid` parameter represents the original destination ID the
1656/// client sent before a stateless retry (this is only required when using
1657/// the [`retry()`] function).
1658///
1659/// [`retry()`]: fn.retry.html
1660///
1661/// ## Examples:
1662///
1663/// ```no_run
1664/// # let mut config = quiche::Config::new(0xbabababa)?;
1665/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1666/// # let local = "127.0.0.1:0".parse().unwrap();
1667/// # let peer = "127.0.0.1:1234".parse().unwrap();
1668/// let conn = quiche::accept(&scid, None, local, peer, &mut config)?;
1669/// # Ok::<(), quiche::Error>(())
1670/// ```
1671#[inline]
1672pub fn accept(
1673    scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1674    peer: SocketAddr, config: &mut Config,
1675) -> Result<Connection> {
1676    let conn = Connection::new(scid, odcid, local, peer, config, true)?;
1677
1678    Ok(conn)
1679}
1680
1681/// Creates a new server-side connection, with a custom buffer generation
1682/// method.
1683///
1684/// The buffers generated can be anything that can be drereferenced as a byte
1685/// slice. See [`accept`] and [`BufFactory`] for more info.
1686#[inline]
1687pub fn accept_with_buf_factory<F: BufFactory>(
1688    scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1689    peer: SocketAddr, config: &mut Config,
1690) -> Result<Connection<F>> {
1691    let conn = Connection::new(scid, odcid, local, peer, config, true)?;
1692
1693    Ok(conn)
1694}
1695
1696/// Creates a new client-side connection.
1697///
1698/// The `scid` parameter is used as the connection's source connection ID,
1699/// while the optional `server_name` parameter is used to verify the peer's
1700/// certificate.
1701///
1702/// ## Examples:
1703///
1704/// ```no_run
1705/// # let mut config = quiche::Config::new(0xbabababa)?;
1706/// # let server_name = "quic.tech";
1707/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1708/// # let local = "127.0.0.1:4321".parse().unwrap();
1709/// # let peer = "127.0.0.1:1234".parse().unwrap();
1710/// let conn =
1711///     quiche::connect(Some(&server_name), &scid, local, peer, &mut config)?;
1712/// # Ok::<(), quiche::Error>(())
1713/// ```
1714#[inline]
1715pub fn connect(
1716    server_name: Option<&str>, scid: &ConnectionId, local: SocketAddr,
1717    peer: SocketAddr, config: &mut Config,
1718) -> Result<Connection> {
1719    let mut conn = Connection::new(scid, None, local, peer, config, false)?;
1720
1721    if let Some(server_name) = server_name {
1722        conn.handshake.set_host_name(server_name)?;
1723    }
1724
1725    Ok(conn)
1726}
1727
1728/// Creates a new client-side connection, with a custom buffer generation
1729/// method.
1730///
1731/// The buffers generated can be anything that can be drereferenced as a byte
1732/// slice. See [`connect`] and [`BufFactory`] for more info.
1733#[inline]
1734pub fn connect_with_buffer_factory<F: BufFactory>(
1735    server_name: Option<&str>, scid: &ConnectionId, local: SocketAddr,
1736    peer: SocketAddr, config: &mut Config,
1737) -> Result<Connection<F>> {
1738    let mut conn = Connection::new(scid, None, local, peer, config, false)?;
1739
1740    if let Some(server_name) = server_name {
1741        conn.handshake.set_host_name(server_name)?;
1742    }
1743
1744    Ok(conn)
1745}
1746
1747/// Writes a version negotiation packet.
1748///
1749/// The `scid` and `dcid` parameters are the source connection ID and the
1750/// destination connection ID extracted from the received client's Initial
1751/// packet that advertises an unsupported version.
1752///
1753/// ## Examples:
1754///
1755/// ```no_run
1756/// # let mut buf = [0; 512];
1757/// # let mut out = [0; 512];
1758/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1759/// let (len, src) = socket.recv_from(&mut buf).unwrap();
1760///
1761/// let hdr =
1762///     quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1763///
1764/// if hdr.version != quiche::PROTOCOL_VERSION {
1765///     let len = quiche::negotiate_version(&hdr.scid, &hdr.dcid, &mut out)?;
1766///     socket.send_to(&out[..len], &src).unwrap();
1767/// }
1768/// # Ok::<(), quiche::Error>(())
1769/// ```
1770#[inline]
1771pub fn negotiate_version(
1772    scid: &ConnectionId, dcid: &ConnectionId, out: &mut [u8],
1773) -> Result<usize> {
1774    packet::negotiate_version(scid, dcid, out)
1775}
1776
1777/// Writes a stateless retry packet.
1778///
1779/// The `scid` and `dcid` parameters are the source connection ID and the
1780/// destination connection ID extracted from the received client's Initial
1781/// packet, while `new_scid` is the server's new source connection ID and
1782/// `token` is the address validation token the client needs to echo back.
1783///
1784/// The application is responsible for generating the address validation
1785/// token to be sent to the client, and verifying tokens sent back by the
1786/// client. The generated token should include the `dcid` parameter, such
1787/// that it can be later extracted from the token and passed to the
1788/// [`accept()`] function as its `odcid` parameter.
1789///
1790/// [`accept()`]: fn.accept.html
1791///
1792/// ## Examples:
1793///
1794/// ```no_run
1795/// # let mut config = quiche::Config::new(0xbabababa)?;
1796/// # let mut buf = [0; 512];
1797/// # let mut out = [0; 512];
1798/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1799/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1800/// # let local = socket.local_addr().unwrap();
1801/// # fn mint_token(hdr: &quiche::Header, src: &std::net::SocketAddr) -> Vec<u8> {
1802/// #     vec![]
1803/// # }
1804/// # fn validate_token<'a>(src: &std::net::SocketAddr, token: &'a [u8]) -> Option<quiche::ConnectionId<'a>> {
1805/// #     None
1806/// # }
1807/// let (len, peer) = socket.recv_from(&mut buf).unwrap();
1808///
1809/// let hdr = quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1810///
1811/// let token = hdr.token.as_ref().unwrap();
1812///
1813/// // No token sent by client, create a new one.
1814/// if token.is_empty() {
1815///     let new_token = mint_token(&hdr, &peer);
1816///
1817///     let len = quiche::retry(
1818///         &hdr.scid, &hdr.dcid, &scid, &new_token, hdr.version, &mut out,
1819///     )?;
1820///
1821///     socket.send_to(&out[..len], &peer).unwrap();
1822///     return Ok(());
1823/// }
1824///
1825/// // Client sent token, validate it.
1826/// let odcid = validate_token(&peer, token);
1827///
1828/// if odcid.is_none() {
1829///     // Invalid address validation token.
1830///     return Ok(());
1831/// }
1832///
1833/// let conn = quiche::accept(&scid, odcid.as_ref(), local, peer, &mut config)?;
1834/// # Ok::<(), quiche::Error>(())
1835/// ```
1836#[inline]
1837pub fn retry(
1838    scid: &ConnectionId, dcid: &ConnectionId, new_scid: &ConnectionId,
1839    token: &[u8], version: u32, out: &mut [u8],
1840) -> Result<usize> {
1841    packet::retry(scid, dcid, new_scid, token, version, out)
1842}
1843
1844/// Returns true if the given protocol version is supported.
1845#[inline]
1846pub fn version_is_supported(version: u32) -> bool {
1847    matches!(version, PROTOCOL_VERSION_V1)
1848}
1849
1850/// Pushes a frame to the output packet if there is enough space.
1851///
1852/// Returns `true` on success, `false` otherwise. In case of failure it means
1853/// there is no room to add the frame in the packet. You may retry to add the
1854/// frame later.
1855macro_rules! push_frame_to_pkt {
1856    ($out:expr, $frames:expr, $frame:expr, $left:expr) => {{
1857        if $frame.wire_len() <= $left {
1858            $left -= $frame.wire_len();
1859
1860            $frame.to_bytes(&mut $out)?;
1861
1862            $frames.push($frame);
1863
1864            true
1865        } else {
1866            false
1867        }
1868    }};
1869}
1870
1871/// Executes the provided body if the qlog feature is enabled, quiche has been
1872/// configured with a log writer, the event's importance is within the
1873/// configured level.
1874macro_rules! qlog_with_type {
1875    ($ty:expr, $qlog:expr, $qlog_streamer_ref:ident, $body:block) => {{
1876        #[cfg(feature = "qlog")]
1877        {
1878            if EventImportance::from($ty).is_contained_in(&$qlog.level) {
1879                if let Some($qlog_streamer_ref) = &mut $qlog.streamer {
1880                    $body
1881                }
1882            }
1883        }
1884    }};
1885}
1886
1887#[cfg(feature = "qlog")]
1888const QLOG_PARAMS_SET: EventType =
1889    EventType::TransportEventType(TransportEventType::ParametersSet);
1890
1891#[cfg(feature = "qlog")]
1892const QLOG_PACKET_RX: EventType =
1893    EventType::TransportEventType(TransportEventType::PacketReceived);
1894
1895#[cfg(feature = "qlog")]
1896const QLOG_PACKET_TX: EventType =
1897    EventType::TransportEventType(TransportEventType::PacketSent);
1898
1899#[cfg(feature = "qlog")]
1900const QLOG_DATA_MV: EventType =
1901    EventType::TransportEventType(TransportEventType::DataMoved);
1902
1903#[cfg(feature = "qlog")]
1904const QLOG_METRICS: EventType =
1905    EventType::RecoveryEventType(RecoveryEventType::MetricsUpdated);
1906
1907#[cfg(feature = "qlog")]
1908const QLOG_CONNECTION_CLOSED: EventType =
1909    EventType::ConnectivityEventType(ConnectivityEventType::ConnectionClosed);
1910
1911#[cfg(feature = "qlog")]
1912struct QlogInfo {
1913    streamer: Option<qlog::streamer::QlogStreamer>,
1914    logged_peer_params: bool,
1915    level: EventImportance,
1916}
1917
1918#[cfg(feature = "qlog")]
1919impl Default for QlogInfo {
1920    fn default() -> Self {
1921        QlogInfo {
1922            streamer: None,
1923            logged_peer_params: false,
1924            level: EventImportance::Base,
1925        }
1926    }
1927}
1928
1929impl<F: BufFactory> Connection<F> {
1930    fn new(
1931        scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1932        peer: SocketAddr, config: &mut Config, is_server: bool,
1933    ) -> Result<Connection<F>> {
1934        let tls = config.tls_ctx.new_handshake()?;
1935        Connection::with_tls(scid, odcid, local, peer, config, tls, is_server)
1936    }
1937
1938    fn with_tls(
1939        scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1940        peer: SocketAddr, config: &Config, tls: tls::Handshake, is_server: bool,
1941    ) -> Result<Connection<F>> {
1942        let max_rx_data = config.local_transport_params.initial_max_data;
1943
1944        let scid_as_hex: Vec<String> =
1945            scid.iter().map(|b| format!("{b:02x}")).collect();
1946
1947        let reset_token = if is_server {
1948            config.local_transport_params.stateless_reset_token
1949        } else {
1950            None
1951        };
1952
1953        let recovery_config = recovery::RecoveryConfig::from_config(config);
1954
1955        let mut path = path::Path::new(
1956            local,
1957            peer,
1958            &recovery_config,
1959            config.path_challenge_recv_max_queue_len,
1960            MIN_CLIENT_INITIAL_LEN,
1961            true,
1962        );
1963
1964        // If we did stateless retry assume the peer's address is verified.
1965        path.verified_peer_address = odcid.is_some();
1966        // Assume clients validate the server's address implicitly.
1967        path.peer_verified_local_address = is_server;
1968
1969        // Do not allocate more than the number of active CIDs.
1970        let paths = path::PathMap::new(
1971            path,
1972            config.local_transport_params.active_conn_id_limit as usize,
1973            is_server,
1974            config.pmtud,
1975            config.max_send_udp_payload_size,
1976        );
1977
1978        let active_path_id = paths.get_active_path_id()?;
1979
1980        let ids = cid::ConnectionIdentifiers::new(
1981            config.local_transport_params.active_conn_id_limit as usize,
1982            scid,
1983            active_path_id,
1984            reset_token,
1985        );
1986
1987        let mut conn = Connection {
1988            version: config.version,
1989
1990            ids,
1991
1992            trace_id: scid_as_hex.join(""),
1993
1994            pkt_num_spaces: [
1995                packet::PktNumSpace::new(),
1996                packet::PktNumSpace::new(),
1997                packet::PktNumSpace::new(),
1998            ],
1999
2000            crypto_ctx: [
2001                packet::CryptoContext::new(),
2002                packet::CryptoContext::new(),
2003                packet::CryptoContext::new(),
2004            ],
2005
2006            next_pkt_num: 0,
2007
2008            peer_transport_params: TransportParams::default(),
2009
2010            peer_transport_params_track_unknown: config
2011                .track_unknown_transport_params,
2012
2013            local_transport_params: config.local_transport_params.clone(),
2014
2015            handshake: tls,
2016
2017            session: None,
2018
2019            recovery_config,
2020
2021            paths,
2022            path_challenge_recv_max_queue_len: config
2023                .path_challenge_recv_max_queue_len,
2024            path_challenge_rx_count: 0,
2025
2026            application_protos: config.application_protos.clone(),
2027
2028            recv_count: 0,
2029            sent_count: 0,
2030            lost_count: 0,
2031            spurious_lost_count: 0,
2032            retrans_count: 0,
2033            dgram_sent_count: 0,
2034            dgram_recv_count: 0,
2035            sent_bytes: 0,
2036            recv_bytes: 0,
2037            acked_bytes: 0,
2038            lost_bytes: 0,
2039
2040            rx_data: 0,
2041            flow_control: flowcontrol::FlowControl::new(
2042                max_rx_data,
2043                cmp::min(max_rx_data / 2 * 3, DEFAULT_CONNECTION_WINDOW),
2044                config.max_connection_window,
2045            ),
2046            almost_full: false,
2047
2048            tx_cap: 0,
2049            tx_cap_factor: config.tx_cap_factor,
2050
2051            tx_buffered: 0,
2052
2053            tx_data: 0,
2054            max_tx_data: 0,
2055            last_tx_data: 0,
2056
2057            stream_retrans_bytes: 0,
2058
2059            streams: stream::StreamMap::new(
2060                config.local_transport_params.initial_max_streams_bidi,
2061                config.local_transport_params.initial_max_streams_uni,
2062                config.max_stream_window,
2063            ),
2064
2065            odcid: None,
2066
2067            rscid: None,
2068
2069            token: None,
2070
2071            local_error: None,
2072
2073            peer_error: None,
2074
2075            blocked_limit: None,
2076
2077            idle_timer: None,
2078
2079            draining_timer: None,
2080
2081            undecryptable_pkts: VecDeque::new(),
2082
2083            alpn: Vec::new(),
2084
2085            is_server,
2086
2087            derived_initial_secrets: false,
2088
2089            did_version_negotiation: false,
2090
2091            did_retry: false,
2092
2093            got_peer_conn_id: false,
2094
2095            // Assume clients validate the server's address implicitly.
2096            peer_verified_initial_address: is_server,
2097
2098            parsed_peer_transport_params: false,
2099
2100            handshake_completed: false,
2101
2102            handshake_done_sent: false,
2103            handshake_done_acked: false,
2104
2105            handshake_confirmed: false,
2106
2107            key_phase: false,
2108
2109            ack_eliciting_sent: false,
2110
2111            closed: false,
2112
2113            timed_out: false,
2114
2115            grease: config.grease,
2116
2117            keylog: None,
2118
2119            #[cfg(feature = "qlog")]
2120            qlog: Default::default(),
2121
2122            dgram_recv_queue: dgram::DatagramQueue::new(
2123                config.dgram_recv_max_queue_len,
2124            ),
2125
2126            dgram_send_queue: dgram::DatagramQueue::new(
2127                config.dgram_send_max_queue_len,
2128            ),
2129
2130            emit_dgram: true,
2131
2132            disable_dcid_reuse: config.disable_dcid_reuse,
2133
2134            reset_stream_local_count: 0,
2135            stopped_stream_local_count: 0,
2136            reset_stream_remote_count: 0,
2137            stopped_stream_remote_count: 0,
2138
2139            max_amplification_factor: config.max_amplification_factor,
2140        };
2141
2142        if let Some(odcid) = odcid {
2143            conn.local_transport_params
2144                .original_destination_connection_id = Some(odcid.to_vec().into());
2145
2146            conn.local_transport_params.retry_source_connection_id =
2147                Some(conn.ids.get_scid(0)?.cid.to_vec().into());
2148
2149            conn.did_retry = true;
2150        }
2151
2152        conn.local_transport_params.initial_source_connection_id =
2153            Some(conn.ids.get_scid(0)?.cid.to_vec().into());
2154
2155        conn.handshake.init(is_server)?;
2156
2157        conn.handshake
2158            .use_legacy_codepoint(config.version != PROTOCOL_VERSION_V1);
2159
2160        conn.encode_transport_params()?;
2161
2162        // Derive initial secrets for the client. We can do this here because
2163        // we already generated the random destination connection ID.
2164        if !is_server {
2165            let mut dcid = [0; 16];
2166            rand::rand_bytes(&mut dcid[..]);
2167
2168            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2169                &dcid,
2170                conn.version,
2171                conn.is_server,
2172                false,
2173            )?;
2174
2175            let reset_token = conn.peer_transport_params.stateless_reset_token;
2176            conn.set_initial_dcid(
2177                dcid.to_vec().into(),
2178                reset_token,
2179                active_path_id,
2180            )?;
2181
2182            conn.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
2183            conn.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
2184
2185            conn.derived_initial_secrets = true;
2186        }
2187
2188        Ok(conn)
2189    }
2190
2191    /// Sets keylog output to the designated [`Writer`].
2192    ///
2193    /// This needs to be called as soon as the connection is created, to avoid
2194    /// missing some early logs.
2195    ///
2196    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2197    #[inline]
2198    pub fn set_keylog(&mut self, writer: Box<dyn std::io::Write + Send + Sync>) {
2199        self.keylog = Some(writer);
2200    }
2201
2202    /// Sets qlog output to the designated [`Writer`].
2203    ///
2204    /// Only events included in `QlogLevel::Base` are written. The serialization
2205    /// format is JSON-SEQ.
2206    ///
2207    /// This needs to be called as soon as the connection is created, to avoid
2208    /// missing some early logs.
2209    ///
2210    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2211    #[cfg(feature = "qlog")]
2212    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2213    pub fn set_qlog(
2214        &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2215        description: String,
2216    ) {
2217        self.set_qlog_with_level(writer, title, description, QlogLevel::Base)
2218    }
2219
2220    /// Sets qlog output to the designated [`Writer`].
2221    ///
2222    /// Only qlog events included in the specified `QlogLevel` are written. The
2223    /// serialization format is JSON-SEQ.
2224    ///
2225    /// This needs to be called as soon as the connection is created, to avoid
2226    /// missing some early logs.
2227    ///
2228    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2229    #[cfg(feature = "qlog")]
2230    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2231    pub fn set_qlog_with_level(
2232        &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2233        description: String, qlog_level: QlogLevel,
2234    ) {
2235        let vp = if self.is_server {
2236            qlog::VantagePointType::Server
2237        } else {
2238            qlog::VantagePointType::Client
2239        };
2240
2241        let level = match qlog_level {
2242            QlogLevel::Core => EventImportance::Core,
2243
2244            QlogLevel::Base => EventImportance::Base,
2245
2246            QlogLevel::Extra => EventImportance::Extra,
2247        };
2248
2249        self.qlog.level = level;
2250
2251        let trace = qlog::TraceSeq::new(
2252            qlog::VantagePoint {
2253                name: None,
2254                ty: vp,
2255                flow: None,
2256            },
2257            Some(title.to_string()),
2258            Some(description.to_string()),
2259            Some(qlog::Configuration {
2260                time_offset: Some(0.0),
2261                original_uris: None,
2262            }),
2263            None,
2264        );
2265
2266        let mut streamer = qlog::streamer::QlogStreamer::new(
2267            qlog::QLOG_VERSION.to_string(),
2268            Some(title),
2269            Some(description),
2270            None,
2271            time::Instant::now(),
2272            trace,
2273            self.qlog.level,
2274            writer,
2275        );
2276
2277        streamer.start_log().ok();
2278
2279        let ev_data = self
2280            .local_transport_params
2281            .to_qlog(TransportOwner::Local, self.handshake.cipher());
2282
2283        // This event occurs very early, so just mark the relative time as 0.0.
2284        streamer.add_event(Event::with_time(0.0, ev_data)).ok();
2285
2286        self.qlog.streamer = Some(streamer);
2287    }
2288
2289    /// Returns a mutable reference to the QlogStreamer, if it exists.
2290    #[cfg(feature = "qlog")]
2291    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2292    pub fn qlog_streamer(&mut self) -> Option<&mut qlog::streamer::QlogStreamer> {
2293        self.qlog.streamer.as_mut()
2294    }
2295
2296    /// Configures the given session for resumption.
2297    ///
2298    /// On the client, this can be used to offer the given serialized session,
2299    /// as returned by [`session()`], for resumption.
2300    ///
2301    /// This must only be called immediately after creating a connection, that
2302    /// is, before any packet is sent or received.
2303    ///
2304    /// [`session()`]: struct.Connection.html#method.session
2305    #[inline]
2306    pub fn set_session(&mut self, session: &[u8]) -> Result<()> {
2307        let mut b = octets::Octets::with_slice(session);
2308
2309        let session_len = b.get_u64()? as usize;
2310        let session_bytes = b.get_bytes(session_len)?;
2311
2312        self.handshake.set_session(session_bytes.as_ref())?;
2313
2314        let raw_params_len = b.get_u64()? as usize;
2315        let raw_params_bytes = b.get_bytes(raw_params_len)?;
2316
2317        let peer_params = TransportParams::decode(
2318            raw_params_bytes.as_ref(),
2319            self.is_server,
2320            self.peer_transport_params_track_unknown,
2321        )?;
2322
2323        self.process_peer_transport_params(peer_params)?;
2324
2325        Ok(())
2326    }
2327
2328    /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
2329    ///
2330    /// This must only be called immediately after creating a connection, that
2331    /// is, before any packet is sent or received.
2332    ///
2333    /// The default value is infinite, that is, no timeout is used unless
2334    /// already configured when creating the connection.
2335    pub fn set_max_idle_timeout(&mut self, v: u64) -> Result<()> {
2336        self.local_transport_params.max_idle_timeout = v;
2337
2338        self.encode_transport_params()
2339    }
2340
2341    /// Sets the congestion control algorithm used.
2342    ///
2343    /// This function can only be called inside one of BoringSSL's handshake
2344    /// callbacks, before any packet has been sent. Calling this function any
2345    /// other time will have no effect.
2346    ///
2347    /// See [`Config::set_cc_algorithm()`].
2348    ///
2349    /// [`Config::set_cc_algorithm()`]: struct.Config.html#method.set_cc_algorithm
2350    #[cfg(feature = "boringssl-boring-crate")]
2351    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2352    pub fn set_cc_algorithm_in_handshake(
2353        ssl: &mut boring::ssl::SslRef, algo: CongestionControlAlgorithm,
2354    ) -> Result<()> {
2355        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2356
2357        ex_data.recovery_config.cc_algorithm = algo;
2358
2359        Ok(())
2360    }
2361
2362    /// Sets custom BBR settings.
2363    ///
2364    /// This API is experimental and will be removed in the future.
2365    ///
2366    /// Currently this only applies if cc_algorithm is
2367    /// `CongestionControlAlgorithm::Bbr2Gcongestion` is set.
2368    ///
2369    /// This function can only be called inside one of BoringSSL's handshake
2370    /// callbacks, before any packet has been sent. Calling this function any
2371    /// other time will have no effect.
2372    ///
2373    /// See [`Config::set_custom_bbr_settings()`].
2374    ///
2375    /// [`Config::set_custom_bbr_settings()`]: struct.Config.html#method.set_custom_bbr_settings
2376    #[cfg(all(feature = "boringssl-boring-crate", feature = "internal"))]
2377    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2378    #[doc(hidden)]
2379    pub fn set_custom_bbr_settings_in_handshake(
2380        ssl: &mut boring::ssl::SslRef, custom_bbr_params: BbrParams,
2381    ) -> Result<()> {
2382        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2383
2384        ex_data.recovery_config.custom_bbr_params = Some(custom_bbr_params);
2385
2386        Ok(())
2387    }
2388
2389    /// Sets the congestion control algorithm used by string.
2390    ///
2391    /// This function can only be called inside one of BoringSSL's handshake
2392    /// callbacks, before any packet has been sent. Calling this function any
2393    /// other time will have no effect.
2394    ///
2395    /// See [`Config::set_cc_algorithm_name()`].
2396    ///
2397    /// [`Config::set_cc_algorithm_name()`]: struct.Config.html#method.set_cc_algorithm_name
2398    #[cfg(feature = "boringssl-boring-crate")]
2399    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2400    pub fn set_cc_algorithm_name_in_handshake(
2401        ssl: &mut boring::ssl::SslRef, name: &str,
2402    ) -> Result<()> {
2403        let cc_algo = CongestionControlAlgorithm::from_str(name)?;
2404        Self::set_cc_algorithm_in_handshake(ssl, cc_algo)
2405    }
2406
2407    /// Sets initial congestion window size in terms of packet count.
2408    ///
2409    /// This function can only be called inside one of BoringSSL's handshake
2410    /// callbacks, before any packet has been sent. Calling this function any
2411    /// other time will have no effect.
2412    ///
2413    /// See [`Config::set_initial_congestion_window_packets()`].
2414    ///
2415    /// [`Config::set_initial_congestion_window_packets()`]: struct.Config.html#method.set_initial_congestion_window_packets
2416    #[cfg(feature = "boringssl-boring-crate")]
2417    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2418    pub fn set_initial_congestion_window_packets_in_handshake(
2419        ssl: &mut boring::ssl::SslRef, packets: usize,
2420    ) -> Result<()> {
2421        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2422
2423        ex_data.recovery_config.initial_congestion_window_packets = packets;
2424
2425        Ok(())
2426    }
2427
2428    /// Configures whether to enable HyStart++.
2429    ///
2430    /// This function can only be called inside one of BoringSSL's handshake
2431    /// callbacks, before any packet has been sent. Calling this function any
2432    /// other time will have no effect.
2433    ///
2434    /// See [`Config::enable_hystart()`].
2435    ///
2436    /// [`Config::enable_hystart()`]: struct.Config.html#method.enable_hystart
2437    #[cfg(feature = "boringssl-boring-crate")]
2438    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2439    pub fn set_hystart_in_handshake(
2440        ssl: &mut boring::ssl::SslRef, v: bool,
2441    ) -> Result<()> {
2442        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2443
2444        ex_data.recovery_config.hystart = v;
2445
2446        Ok(())
2447    }
2448
2449    /// Configures whether to enable pacing.
2450    ///
2451    /// This function can only be called inside one of BoringSSL's handshake
2452    /// callbacks, before any packet has been sent. Calling this function any
2453    /// other time will have no effect.
2454    ///
2455    /// See [`Config::enable_pacing()`].
2456    ///
2457    /// [`Config::enable_pacing()`]: struct.Config.html#method.enable_pacing
2458    #[cfg(feature = "boringssl-boring-crate")]
2459    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2460    pub fn set_pacing_in_handshake(
2461        ssl: &mut boring::ssl::SslRef, v: bool,
2462    ) -> Result<()> {
2463        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2464
2465        ex_data.recovery_config.pacing = v;
2466
2467        Ok(())
2468    }
2469
2470    /// Sets the max value for pacing rate.
2471    ///
2472    /// This function can only be called inside one of BoringSSL's handshake
2473    /// callbacks, before any packet has been sent. Calling this function any
2474    /// other time will have no effect.
2475    ///
2476    /// See [`Config::set_max_pacing_rate()`].
2477    ///
2478    /// [`Config::set_max_pacing_rate()`]: struct.Config.html#method.set_max_pacing_rate
2479    #[cfg(feature = "boringssl-boring-crate")]
2480    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2481    pub fn set_max_pacing_rate_in_handshake(
2482        ssl: &mut boring::ssl::SslRef, v: Option<u64>,
2483    ) -> Result<()> {
2484        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2485
2486        ex_data.recovery_config.max_pacing_rate = v;
2487
2488        Ok(())
2489    }
2490
2491    /// Sets the maximum outgoing UDP payload size.
2492    ///
2493    /// This function can only be called inside one of BoringSSL's handshake
2494    /// callbacks, before any packet has been sent. Calling this function any
2495    /// other time will have no effect.
2496    ///
2497    /// See [`Config::set_max_send_udp_payload_size()`].
2498    ///
2499    /// [`Config::set_max_send_udp_payload_size()`]: struct.Config.html#method.set_max_send_udp_payload_size
2500    #[cfg(feature = "boringssl-boring-crate")]
2501    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2502    pub fn set_max_send_udp_payload_size_in_handshake(
2503        ssl: &mut boring::ssl::SslRef, v: usize,
2504    ) -> Result<()> {
2505        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2506
2507        ex_data.recovery_config.max_send_udp_payload_size = v;
2508
2509        Ok(())
2510    }
2511
2512    /// Sets the send capacity factor.
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_send_capacity_factor()`].
2519    ///
2520    /// [`Config::set_max_send_udp_payload_size()`]: struct.Config.html#method.set_send_capacity_factor
2521    #[cfg(feature = "boringssl-boring-crate")]
2522    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
2523    pub fn set_send_capacity_factor_in_handshake(
2524        ssl: &mut boring::ssl::SslRef, v: f64,
2525    ) -> Result<()> {
2526        let ex_data = tls::ExData::from_ssl_ref(ssl).ok_or(Error::TlsFail)?;
2527
2528        ex_data.tx_cap_factor = v;
2529
2530        Ok(())
2531    }
2532
2533    /// Processes QUIC packets received from the peer.
2534    ///
2535    /// On success the number of bytes processed from the input buffer is
2536    /// returned. On error the connection will be closed by calling [`close()`]
2537    /// with the appropriate error code.
2538    ///
2539    /// Coalesced packets will be processed as necessary.
2540    ///
2541    /// Note that the contents of the input buffer `buf` might be modified by
2542    /// this function due to, for example, in-place decryption.
2543    ///
2544    /// [`close()`]: struct.Connection.html#method.close
2545    ///
2546    /// ## Examples:
2547    ///
2548    /// ```no_run
2549    /// # let mut buf = [0; 512];
2550    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
2551    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
2552    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
2553    /// # let peer = "127.0.0.1:1234".parse().unwrap();
2554    /// # let local = socket.local_addr().unwrap();
2555    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
2556    /// loop {
2557    ///     let (read, from) = socket.recv_from(&mut buf).unwrap();
2558    ///
2559    ///     let recv_info = quiche::RecvInfo {
2560    ///         from,
2561    ///         to: local,
2562    ///     };
2563    ///
2564    ///     let read = match conn.recv(&mut buf[..read], recv_info) {
2565    ///         Ok(v) => v,
2566    ///
2567    ///         Err(e) => {
2568    ///             // An error occurred, handle it.
2569    ///             break;
2570    ///         },
2571    ///     };
2572    /// }
2573    /// # Ok::<(), quiche::Error>(())
2574    /// ```
2575    pub fn recv(&mut self, buf: &mut [u8], info: RecvInfo) -> Result<usize> {
2576        let len = buf.len();
2577
2578        if len == 0 {
2579            return Err(Error::BufferTooShort);
2580        }
2581
2582        let recv_pid = self.paths.path_id_from_addrs(&(info.to, info.from));
2583
2584        if let Some(recv_pid) = recv_pid {
2585            let recv_path = self.paths.get_mut(recv_pid)?;
2586
2587            // Keep track of how many bytes we received from the client, so we
2588            // can limit bytes sent back before address validation, to a
2589            // multiple of this. The limit needs to be increased early on, so
2590            // that if there is an error there is enough credit to send a
2591            // CONNECTION_CLOSE.
2592            //
2593            // It doesn't matter if the packets received were valid or not, we
2594            // only need to track the total amount of bytes received.
2595            //
2596            // Note that we also need to limit the number of bytes we sent on a
2597            // path if we are not the host that initiated its usage.
2598            if self.is_server && !recv_path.verified_peer_address {
2599                recv_path.max_send_bytes += len * self.max_amplification_factor;
2600            }
2601        } else if !self.is_server {
2602            // If a client receives packets from an unknown server address,
2603            // the client MUST discard these packets.
2604            trace!(
2605                "{} client received packet from unknown address {:?}, dropping",
2606                self.trace_id,
2607                info,
2608            );
2609
2610            return Ok(len);
2611        }
2612
2613        let mut done = 0;
2614        let mut left = len;
2615
2616        // Process coalesced packets.
2617        while left > 0 {
2618            let read = match self.recv_single(
2619                &mut buf[len - left..len],
2620                &info,
2621                recv_pid,
2622            ) {
2623                Ok(v) => v,
2624
2625                Err(Error::Done) => {
2626                    // If the packet can't be processed or decrypted, check if
2627                    // it's a stateless reset.
2628                    if self.is_stateless_reset(&buf[len - left..len]) {
2629                        trace!("{} packet is a stateless reset", self.trace_id);
2630
2631                        self.mark_closed();
2632                    }
2633
2634                    left
2635                },
2636
2637                Err(e) => {
2638                    // In case of error processing the incoming packet, close
2639                    // the connection.
2640                    self.close(false, e.to_wire(), b"").ok();
2641                    return Err(e);
2642                },
2643            };
2644
2645            done += read;
2646            left -= read;
2647        }
2648
2649        // Even though the packet was previously "accepted", it
2650        // should be safe to forward the error, as it also comes
2651        // from the `recv()` method.
2652        self.process_undecrypted_0rtt_packets()?;
2653
2654        Ok(done)
2655    }
2656
2657    fn process_undecrypted_0rtt_packets(&mut self) -> Result<()> {
2658        // Process previously undecryptable 0-RTT packets if the decryption key
2659        // is now available.
2660        if self.crypto_ctx[packet::Epoch::Application]
2661            .crypto_0rtt_open
2662            .is_some()
2663        {
2664            while let Some((mut pkt, info)) = self.undecryptable_pkts.pop_front()
2665            {
2666                if let Err(e) = self.recv(&mut pkt, info) {
2667                    self.undecryptable_pkts.clear();
2668
2669                    return Err(e);
2670                }
2671            }
2672        }
2673        Ok(())
2674    }
2675
2676    /// Returns true if a QUIC packet is a stateless reset.
2677    fn is_stateless_reset(&self, buf: &[u8]) -> bool {
2678        // If the packet is too small, then we just throw it away.
2679        let buf_len = buf.len();
2680        if buf_len < 21 {
2681            return false;
2682        }
2683
2684        // TODO: we should iterate over all active destination connection IDs
2685        // and check against their reset token.
2686        match self.peer_transport_params.stateless_reset_token {
2687            Some(token) => {
2688                let token_len = 16;
2689
2690                crypto::verify_slices_are_equal(
2691                    &token.to_be_bytes(),
2692                    &buf[buf_len - token_len..buf_len],
2693                )
2694                .is_ok()
2695            },
2696
2697            None => false,
2698        }
2699    }
2700
2701    /// Processes a single QUIC packet received from the peer.
2702    ///
2703    /// On success the number of bytes processed from the input buffer is
2704    /// returned. When the [`Done`] error is returned, processing of the
2705    /// remainder of the incoming UDP datagram should be interrupted.
2706    ///
2707    /// Note that a server might observe a new 4-tuple, preventing to
2708    /// know in advance to which path the incoming packet belongs to (`recv_pid`
2709    /// is `None`). As a client, packets from unknown 4-tuple are dropped
2710    /// beforehand (see `recv()`).
2711    ///
2712    /// On error, an error other than [`Done`] is returned.
2713    ///
2714    /// [`Done`]: enum.Error.html#variant.Done
2715    fn recv_single(
2716        &mut self, buf: &mut [u8], info: &RecvInfo, recv_pid: Option<usize>,
2717    ) -> Result<usize> {
2718        let now = time::Instant::now();
2719
2720        if buf.is_empty() {
2721            return Err(Error::Done);
2722        }
2723
2724        if self.is_closed() || self.is_draining() {
2725            return Err(Error::Done);
2726        }
2727
2728        let is_closing = self.local_error.is_some();
2729
2730        if is_closing {
2731            return Err(Error::Done);
2732        }
2733
2734        let buf_len = buf.len();
2735
2736        let mut b = octets::OctetsMut::with_slice(buf);
2737
2738        let mut hdr = Header::from_bytes(&mut b, self.source_id().len())
2739            .map_err(|e| {
2740                drop_pkt_on_err(
2741                    e,
2742                    self.recv_count,
2743                    self.is_server,
2744                    &self.trace_id,
2745                )
2746            })?;
2747
2748        if hdr.ty == packet::Type::VersionNegotiation {
2749            // Version negotiation packets can only be sent by the server.
2750            if self.is_server {
2751                return Err(Error::Done);
2752            }
2753
2754            // Ignore duplicate version negotiation.
2755            if self.did_version_negotiation {
2756                return Err(Error::Done);
2757            }
2758
2759            // Ignore version negotiation if any other packet has already been
2760            // successfully processed.
2761            if self.recv_count > 0 {
2762                return Err(Error::Done);
2763            }
2764
2765            if hdr.dcid != self.source_id() {
2766                return Err(Error::Done);
2767            }
2768
2769            if hdr.scid != self.destination_id() {
2770                return Err(Error::Done);
2771            }
2772
2773            trace!("{} rx pkt {:?}", self.trace_id, hdr);
2774
2775            let versions = hdr.versions.ok_or(Error::Done)?;
2776
2777            // Ignore version negotiation if the version already selected is
2778            // listed.
2779            if versions.contains(&self.version) {
2780                return Err(Error::Done);
2781            }
2782
2783            let supported_versions =
2784                versions.iter().filter(|&&v| version_is_supported(v));
2785
2786            let mut found_version = false;
2787
2788            for &v in supported_versions {
2789                found_version = true;
2790
2791                // The final version takes precedence over draft ones.
2792                if v == PROTOCOL_VERSION_V1 {
2793                    self.version = v;
2794                    break;
2795                }
2796
2797                self.version = cmp::max(self.version, v);
2798            }
2799
2800            if !found_version {
2801                // We don't support any of the versions offered.
2802                //
2803                // While a man-in-the-middle attacker might be able to
2804                // inject a version negotiation packet that triggers this
2805                // failure, the window of opportunity is very small and
2806                // this error is quite useful for debugging, so don't just
2807                // ignore the packet.
2808                return Err(Error::UnknownVersion);
2809            }
2810
2811            self.did_version_negotiation = true;
2812
2813            // Derive Initial secrets based on the new version.
2814            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2815                &self.destination_id(),
2816                self.version,
2817                self.is_server,
2818                true,
2819            )?;
2820
2821            // Reset connection state to force sending another Initial packet.
2822            self.drop_epoch_state(packet::Epoch::Initial, now);
2823            self.got_peer_conn_id = false;
2824            self.handshake.clear()?;
2825
2826            self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
2827            self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
2828
2829            self.handshake
2830                .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
2831
2832            // Encode transport parameters again, as the new version might be
2833            // using a different format.
2834            self.encode_transport_params()?;
2835
2836            return Err(Error::Done);
2837        }
2838
2839        if hdr.ty == packet::Type::Retry {
2840            // Retry packets can only be sent by the server.
2841            if self.is_server {
2842                return Err(Error::Done);
2843            }
2844
2845            // Ignore duplicate retry.
2846            if self.did_retry {
2847                return Err(Error::Done);
2848            }
2849
2850            // Check if Retry packet is valid.
2851            if packet::verify_retry_integrity(
2852                &b,
2853                &self.destination_id(),
2854                self.version,
2855            )
2856            .is_err()
2857            {
2858                return Err(Error::Done);
2859            }
2860
2861            trace!("{} rx pkt {:?}", self.trace_id, hdr);
2862
2863            self.token = hdr.token;
2864            self.did_retry = true;
2865
2866            // Remember peer's new connection ID.
2867            self.odcid = Some(self.destination_id().into_owned());
2868
2869            self.set_initial_dcid(
2870                hdr.scid.clone(),
2871                None,
2872                self.paths.get_active_path_id()?,
2873            )?;
2874
2875            self.rscid = Some(self.destination_id().into_owned());
2876
2877            // Derive Initial secrets using the new connection ID.
2878            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2879                &hdr.scid,
2880                self.version,
2881                self.is_server,
2882                true,
2883            )?;
2884
2885            // Reset connection state to force sending another Initial packet.
2886            self.drop_epoch_state(packet::Epoch::Initial, now);
2887            self.got_peer_conn_id = false;
2888            self.handshake.clear()?;
2889
2890            self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
2891            self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
2892
2893            return Err(Error::Done);
2894        }
2895
2896        if self.is_server && !self.did_version_negotiation {
2897            if !version_is_supported(hdr.version) {
2898                return Err(Error::UnknownVersion);
2899            }
2900
2901            self.version = hdr.version;
2902            self.did_version_negotiation = true;
2903
2904            self.handshake
2905                .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
2906
2907            // Encode transport parameters again, as the new version might be
2908            // using a different format.
2909            self.encode_transport_params()?;
2910        }
2911
2912        if hdr.ty != packet::Type::Short && hdr.version != self.version {
2913            // At this point version negotiation was already performed, so
2914            // ignore packets that don't match the connection's version.
2915            return Err(Error::Done);
2916        }
2917
2918        // Long header packets have an explicit payload length, but short
2919        // packets don't so just use the remaining capacity in the buffer.
2920        let payload_len = if hdr.ty == packet::Type::Short {
2921            b.cap()
2922        } else {
2923            b.get_varint().map_err(|e| {
2924                drop_pkt_on_err(
2925                    e.into(),
2926                    self.recv_count,
2927                    self.is_server,
2928                    &self.trace_id,
2929                )
2930            })? as usize
2931        };
2932
2933        // Make sure the buffer is same or larger than an explicit
2934        // payload length.
2935        if payload_len > b.cap() {
2936            return Err(drop_pkt_on_err(
2937                Error::InvalidPacket,
2938                self.recv_count,
2939                self.is_server,
2940                &self.trace_id,
2941            ));
2942        }
2943
2944        // Derive initial secrets on the server.
2945        if !self.derived_initial_secrets {
2946            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2947                &hdr.dcid,
2948                self.version,
2949                self.is_server,
2950                false,
2951            )?;
2952
2953            self.crypto_ctx[packet::Epoch::Initial].crypto_open = Some(aead_open);
2954            self.crypto_ctx[packet::Epoch::Initial].crypto_seal = Some(aead_seal);
2955
2956            self.derived_initial_secrets = true;
2957        }
2958
2959        // Select packet number space epoch based on the received packet's type.
2960        let epoch = hdr.ty.to_epoch()?;
2961
2962        // Select AEAD context used to open incoming packet.
2963        let aead = if hdr.ty == packet::Type::ZeroRTT {
2964            // Only use 0-RTT key if incoming packet is 0-RTT.
2965            self.crypto_ctx[epoch].crypto_0rtt_open.as_ref()
2966        } else {
2967            // Otherwise use the packet number space's main key.
2968            self.crypto_ctx[epoch].crypto_open.as_ref()
2969        };
2970
2971        // Finally, discard packet if no usable key is available.
2972        let mut aead = match aead {
2973            Some(v) => v,
2974
2975            None => {
2976                if hdr.ty == packet::Type::ZeroRTT &&
2977                    self.undecryptable_pkts.len() < MAX_UNDECRYPTABLE_PACKETS &&
2978                    !self.is_established()
2979                {
2980                    // Buffer 0-RTT packets when the required read key is not
2981                    // available yet, and process them later.
2982                    //
2983                    // TODO: in the future we might want to buffer other types
2984                    // of undecryptable packets as well.
2985                    let pkt_len = b.off() + payload_len;
2986                    let pkt = (b.buf()[..pkt_len]).to_vec();
2987
2988                    self.undecryptable_pkts.push_back((pkt, *info));
2989                    return Ok(pkt_len);
2990                }
2991
2992                let e = drop_pkt_on_err(
2993                    Error::CryptoFail,
2994                    self.recv_count,
2995                    self.is_server,
2996                    &self.trace_id,
2997                );
2998
2999                return Err(e);
3000            },
3001        };
3002
3003        let aead_tag_len = aead.alg().tag_len();
3004
3005        packet::decrypt_hdr(&mut b, &mut hdr, aead).map_err(|e| {
3006            drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
3007        })?;
3008
3009        let pn = packet::decode_pkt_num(
3010            self.pkt_num_spaces[epoch].largest_rx_pkt_num,
3011            hdr.pkt_num,
3012            hdr.pkt_num_len,
3013        );
3014
3015        let pn_len = hdr.pkt_num_len;
3016
3017        trace!(
3018            "{} rx pkt {:?} len={} pn={} {}",
3019            self.trace_id,
3020            hdr,
3021            payload_len,
3022            pn,
3023            AddrTupleFmt(info.from, info.to)
3024        );
3025
3026        #[cfg(feature = "qlog")]
3027        let mut qlog_frames = vec![];
3028
3029        // Check for key update.
3030        let mut aead_next = None;
3031
3032        if self.handshake_confirmed &&
3033            hdr.ty != Type::ZeroRTT &&
3034            hdr.key_phase != self.key_phase
3035        {
3036            // Check if this packet arrived before key update.
3037            if let Some(key_update) = self.crypto_ctx[epoch]
3038                .key_update
3039                .as_ref()
3040                .and_then(|key_update| {
3041                    (pn < key_update.pn_on_update).then_some(key_update)
3042                })
3043            {
3044                aead = &key_update.crypto_open;
3045            } else {
3046                trace!("{} peer-initiated key update", self.trace_id);
3047
3048                aead_next = Some((
3049                    self.crypto_ctx[epoch]
3050                        .crypto_open
3051                        .as_ref()
3052                        .unwrap()
3053                        .derive_next_packet_key()?,
3054                    self.crypto_ctx[epoch]
3055                        .crypto_seal
3056                        .as_ref()
3057                        .unwrap()
3058                        .derive_next_packet_key()?,
3059                ));
3060
3061                // `aead_next` is always `Some()` at this point, so the `unwrap()`
3062                // will never fail.
3063                aead = &aead_next.as_ref().unwrap().0;
3064            }
3065        }
3066
3067        let mut payload = packet::decrypt_pkt(
3068            &mut b,
3069            pn,
3070            pn_len,
3071            payload_len,
3072            aead,
3073        )
3074        .map_err(|e| {
3075            drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
3076        })?;
3077
3078        if self.pkt_num_spaces[epoch].recv_pkt_num.contains(pn) {
3079            trace!("{} ignored duplicate packet {}", self.trace_id, pn);
3080            return Err(Error::Done);
3081        }
3082
3083        // Packets with no frames are invalid.
3084        if payload.cap() == 0 {
3085            return Err(Error::InvalidPacket);
3086        }
3087
3088        // Now that we decrypted the packet, let's see if we can map it to an
3089        // existing path.
3090        let recv_pid = if hdr.ty == packet::Type::Short && self.got_peer_conn_id {
3091            let pkt_dcid = ConnectionId::from_ref(&hdr.dcid);
3092            self.get_or_create_recv_path_id(recv_pid, &pkt_dcid, buf_len, info)?
3093        } else {
3094            // During handshake, we are on the initial path.
3095            self.paths.get_active_path_id()?
3096        };
3097
3098        // The key update is verified once a packet is successfully decrypted
3099        // using the new keys.
3100        if let Some((open_next, seal_next)) = aead_next {
3101            if !self.crypto_ctx[epoch]
3102                .key_update
3103                .as_ref()
3104                .is_none_or(|prev| prev.update_acked)
3105            {
3106                // Peer has updated keys twice without awaiting confirmation.
3107                return Err(Error::KeyUpdate);
3108            }
3109
3110            trace!("{} key update verified", self.trace_id);
3111
3112            let _ = self.crypto_ctx[epoch].crypto_seal.replace(seal_next);
3113
3114            let open_prev = self.crypto_ctx[epoch]
3115                .crypto_open
3116                .replace(open_next)
3117                .unwrap();
3118
3119            let recv_path = self.paths.get_mut(recv_pid)?;
3120
3121            self.crypto_ctx[epoch].key_update = Some(packet::KeyUpdate {
3122                crypto_open: open_prev,
3123                pn_on_update: pn,
3124                update_acked: false,
3125                timer: now + (recv_path.recovery.pto() * 3),
3126            });
3127
3128            self.key_phase = !self.key_phase;
3129
3130            qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
3131                let trigger = Some(
3132                    qlog::events::security::KeyUpdateOrRetiredTrigger::RemoteUpdate,
3133                );
3134
3135                let ev_data_client =
3136                    EventData::KeyUpdated(qlog::events::security::KeyUpdated {
3137                        key_type:
3138                            qlog::events::security::KeyType::Client1RttSecret,
3139                        trigger: trigger.clone(),
3140                        ..Default::default()
3141                    });
3142
3143                q.add_event_data_with_instant(ev_data_client, now).ok();
3144
3145                let ev_data_server =
3146                    EventData::KeyUpdated(qlog::events::security::KeyUpdated {
3147                        key_type:
3148                            qlog::events::security::KeyType::Server1RttSecret,
3149                        trigger,
3150                        ..Default::default()
3151                    });
3152
3153                q.add_event_data_with_instant(ev_data_server, now).ok();
3154            });
3155        }
3156
3157        if !self.is_server && !self.got_peer_conn_id {
3158            if self.odcid.is_none() {
3159                self.odcid = Some(self.destination_id().into_owned());
3160            }
3161
3162            // Replace the randomly generated destination connection ID with
3163            // the one supplied by the server.
3164            self.set_initial_dcid(
3165                hdr.scid.clone(),
3166                self.peer_transport_params.stateless_reset_token,
3167                recv_pid,
3168            )?;
3169
3170            self.got_peer_conn_id = true;
3171        }
3172
3173        if self.is_server && !self.got_peer_conn_id {
3174            self.set_initial_dcid(hdr.scid.clone(), None, recv_pid)?;
3175
3176            if !self.did_retry {
3177                self.local_transport_params
3178                    .original_destination_connection_id =
3179                    Some(hdr.dcid.to_vec().into());
3180
3181                self.encode_transport_params()?;
3182            }
3183
3184            self.got_peer_conn_id = true;
3185        }
3186
3187        // To avoid sending an ACK in response to an ACK-only packet, we need
3188        // to keep track of whether this packet contains any frame other than
3189        // ACK and PADDING.
3190        let mut ack_elicited = false;
3191
3192        // Process packet payload. If a frame cannot be processed, store the
3193        // error and stop further packet processing.
3194        let mut frame_processing_err = None;
3195
3196        // To know if the peer migrated the connection, we need to keep track
3197        // whether this is a non-probing packet.
3198        let mut probing = true;
3199
3200        // Process packet payload.
3201        while payload.cap() > 0 {
3202            let frame = frame::Frame::from_bytes(&mut payload, hdr.ty)?;
3203
3204            qlog_with_type!(QLOG_PACKET_RX, self.qlog, _q, {
3205                qlog_frames.push(frame.to_qlog());
3206            });
3207
3208            if frame.ack_eliciting() {
3209                ack_elicited = true;
3210            }
3211
3212            if !frame.probing() {
3213                probing = false;
3214            }
3215
3216            if let Err(e) = self.process_frame(frame, &hdr, recv_pid, epoch, now)
3217            {
3218                frame_processing_err = Some(e);
3219                break;
3220            }
3221        }
3222
3223        qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
3224            let packet_size = b.len();
3225
3226            let qlog_pkt_hdr = qlog::events::quic::PacketHeader::with_type(
3227                hdr.ty.to_qlog(),
3228                Some(pn),
3229                Some(hdr.version),
3230                Some(&hdr.scid),
3231                Some(&hdr.dcid),
3232            );
3233
3234            let qlog_raw_info = RawInfo {
3235                length: Some(packet_size as u64),
3236                payload_length: Some(payload_len as u64),
3237                data: None,
3238            };
3239
3240            let ev_data =
3241                EventData::PacketReceived(qlog::events::quic::PacketReceived {
3242                    header: qlog_pkt_hdr,
3243                    frames: Some(qlog_frames),
3244                    raw: Some(qlog_raw_info),
3245                    ..Default::default()
3246                });
3247
3248            q.add_event_data_with_instant(ev_data, now).ok();
3249        });
3250
3251        qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
3252            let recv_path = self.paths.get_mut(recv_pid)?;
3253            if let Some(ev_data) = recv_path.recovery.maybe_qlog() {
3254                q.add_event_data_with_instant(ev_data, now).ok();
3255            }
3256        });
3257
3258        if let Some(e) = frame_processing_err {
3259            // Any frame error is terminal, so now just return.
3260            return Err(e);
3261        }
3262
3263        // Only log the remote transport parameters once the connection is
3264        // established (i.e. after frames have been fully parsed) and only
3265        // once per connection.
3266        if self.is_established() {
3267            qlog_with_type!(QLOG_PARAMS_SET, self.qlog, q, {
3268                if !self.qlog.logged_peer_params {
3269                    let ev_data = self
3270                        .peer_transport_params
3271                        .to_qlog(TransportOwner::Remote, self.handshake.cipher());
3272
3273                    q.add_event_data_with_instant(ev_data, now).ok();
3274
3275                    self.qlog.logged_peer_params = true;
3276                }
3277            });
3278        }
3279
3280        // Following flag used to upgrade datagram size, if probe is successful.
3281        let mut pmtud_probe = false;
3282
3283        // Process acked frames. Note that several packets from several paths
3284        // might have been acked by the received packet.
3285        for (_, p) in self.paths.iter_mut() {
3286            for acked in p.recovery.get_acked_frames(epoch) {
3287                match acked {
3288                    frame::Frame::Ping {
3289                        mtu_probe: Some(mtu_probe),
3290                    } => {
3291                        let pmtud_next = p.pmtud.get_current();
3292                        p.pmtud.set_current(cmp::max(pmtud_next, mtu_probe));
3293
3294                        // Stop sending path MTU probes after successful probe.
3295                        p.pmtud.should_probe(false);
3296                        pmtud_probe = true;
3297
3298                        trace!(
3299                            "{} pmtud acked; pmtu size {:?}",
3300                            self.trace_id,
3301                            p.pmtud.get_current()
3302                        );
3303                    },
3304
3305                    frame::Frame::ACK { ranges, .. } => {
3306                        // Stop acknowledging packets less than or equal to the
3307                        // largest acknowledged in the sent ACK frame that, in
3308                        // turn, got acked.
3309                        if let Some(largest_acked) = ranges.last() {
3310                            self.pkt_num_spaces[epoch]
3311                                .recv_pkt_need_ack
3312                                .remove_until(largest_acked);
3313                        }
3314                    },
3315
3316                    frame::Frame::CryptoHeader { offset, length } => {
3317                        self.crypto_ctx[epoch]
3318                            .crypto_stream
3319                            .send
3320                            .ack_and_drop(offset, length);
3321                    },
3322
3323                    frame::Frame::StreamHeader {
3324                        stream_id,
3325                        offset,
3326                        length,
3327                        ..
3328                    } => {
3329                        let stream = match self.streams.get_mut(stream_id) {
3330                            Some(v) => v,
3331
3332                            None => continue,
3333                        };
3334
3335                        stream.send.ack_and_drop(offset, length);
3336
3337                        self.tx_buffered =
3338                            self.tx_buffered.saturating_sub(length);
3339
3340                        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
3341                            let ev_data = EventData::DataMoved(
3342                                qlog::events::quic::DataMoved {
3343                                    stream_id: Some(stream_id),
3344                                    offset: Some(offset),
3345                                    length: Some(length as u64),
3346                                    from: Some(DataRecipient::Transport),
3347                                    to: Some(DataRecipient::Dropped),
3348                                    ..Default::default()
3349                                },
3350                            );
3351
3352                            q.add_event_data_with_instant(ev_data, now).ok();
3353                        });
3354
3355                        // Only collect the stream if it is complete and not
3356                        // readable. If it is readable, it will get collected when
3357                        // stream_recv() is used.
3358                        if stream.is_complete() && !stream.is_readable() {
3359                            let local = stream.local;
3360                            self.streams.collect(stream_id, local);
3361                        }
3362                    },
3363
3364                    frame::Frame::HandshakeDone => {
3365                        // Explicitly set this to true, so that if the frame was
3366                        // already scheduled for retransmission, it is aborted.
3367                        self.handshake_done_sent = true;
3368
3369                        self.handshake_done_acked = true;
3370                    },
3371
3372                    frame::Frame::ResetStream { stream_id, .. } => {
3373                        let stream = match self.streams.get_mut(stream_id) {
3374                            Some(v) => v,
3375
3376                            None => continue,
3377                        };
3378
3379                        // Only collect the stream if it is complete and not
3380                        // readable. If it is readable, it will get collected when
3381                        // stream_recv() is used.
3382                        if stream.is_complete() && !stream.is_readable() {
3383                            let local = stream.local;
3384                            self.streams.collect(stream_id, local);
3385                        }
3386                    },
3387
3388                    _ => (),
3389                }
3390            }
3391
3392            // Update max datagram send size with newly acked probe size.
3393            if pmtud_probe {
3394                trace!(
3395                    "{} updating pmtu {:?}",
3396                    p.pmtud.get_current(),
3397                    self.trace_id
3398                );
3399
3400                qlog_with_type!(
3401                    EventType::ConnectivityEventType(
3402                        ConnectivityEventType::MtuUpdated
3403                    ),
3404                    self.qlog,
3405                    q,
3406                    {
3407                        let pmtu_data = EventData::MtuUpdated(
3408                            qlog::events::connectivity::MtuUpdated {
3409                                old: Some(p.recovery.max_datagram_size() as u16),
3410                                new: p.pmtud.get_current() as u16,
3411                                done: Some(pmtud_probe),
3412                            },
3413                        );
3414
3415                        q.add_event_data_with_instant(pmtu_data, now).ok();
3416                    }
3417                );
3418
3419                p.recovery
3420                    .pmtud_update_max_datagram_size(p.pmtud.get_current());
3421            }
3422        }
3423
3424        // Now that we processed all the frames, if there is a path that has no
3425        // Destination CID, try to allocate one.
3426        let no_dcid = self
3427            .paths
3428            .iter_mut()
3429            .filter(|(_, p)| p.active_dcid_seq.is_none());
3430
3431        for (pid, p) in no_dcid {
3432            if self.ids.zero_length_dcid() {
3433                p.active_dcid_seq = Some(0);
3434                continue;
3435            }
3436
3437            let dcid_seq = match self.ids.lowest_available_dcid_seq() {
3438                Some(seq) => seq,
3439                None => break,
3440            };
3441
3442            self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
3443
3444            p.active_dcid_seq = Some(dcid_seq);
3445        }
3446
3447        // We only record the time of arrival of the largest packet number
3448        // that still needs to be acked, to be used for ACK delay calculation.
3449        if self.pkt_num_spaces[epoch].recv_pkt_need_ack.last() < Some(pn) {
3450            self.pkt_num_spaces[epoch].largest_rx_pkt_time = now;
3451        }
3452
3453        self.pkt_num_spaces[epoch].recv_pkt_num.insert(pn);
3454
3455        self.pkt_num_spaces[epoch].recv_pkt_need_ack.push_item(pn);
3456
3457        self.pkt_num_spaces[epoch].ack_elicited =
3458            cmp::max(self.pkt_num_spaces[epoch].ack_elicited, ack_elicited);
3459
3460        self.pkt_num_spaces[epoch].largest_rx_pkt_num =
3461            cmp::max(self.pkt_num_spaces[epoch].largest_rx_pkt_num, pn);
3462
3463        if !probing {
3464            self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num = cmp::max(
3465                self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num,
3466                pn,
3467            );
3468
3469            // Did the peer migrated to another path?
3470            let active_path_id = self.paths.get_active_path_id()?;
3471
3472            if self.is_server &&
3473                recv_pid != active_path_id &&
3474                self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num == pn
3475            {
3476                self.on_peer_migrated(recv_pid, self.disable_dcid_reuse, now)?;
3477            }
3478        }
3479
3480        if let Some(idle_timeout) = self.idle_timeout() {
3481            self.idle_timer = Some(now + idle_timeout);
3482        }
3483
3484        // Update send capacity.
3485        self.update_tx_cap();
3486
3487        self.recv_count += 1;
3488        self.paths.get_mut(recv_pid)?.recv_count += 1;
3489
3490        let read = b.off() + aead_tag_len;
3491
3492        self.recv_bytes += read as u64;
3493        self.paths.get_mut(recv_pid)?.recv_bytes += read as u64;
3494
3495        // An Handshake packet has been received from the client and has been
3496        // successfully processed, so we can drop the initial state and consider
3497        // the client's address to be verified.
3498        if self.is_server && hdr.ty == packet::Type::Handshake {
3499            self.drop_epoch_state(packet::Epoch::Initial, now);
3500
3501            self.paths.get_mut(recv_pid)?.verified_peer_address = true;
3502        }
3503
3504        self.ack_eliciting_sent = false;
3505
3506        Ok(read)
3507    }
3508
3509    /// Writes a single QUIC packet to be sent to the peer.
3510    ///
3511    /// On success the number of bytes written to the output buffer is
3512    /// returned, or [`Done`] if there was nothing to write.
3513    ///
3514    /// The application should call `send()` multiple times until [`Done`] is
3515    /// returned, indicating that there are no more packets to send. It is
3516    /// recommended that `send()` be called in the following cases:
3517    ///
3518    ///  * When the application receives QUIC packets from the peer (that is,
3519    ///    any time [`recv()`] is also called).
3520    ///
3521    ///  * When the connection timer expires (that is, any time [`on_timeout()`]
3522    ///    is also called).
3523    ///
3524    ///  * When the application sends data to the peer (for example, any time
3525    ///    [`stream_send()`] or [`stream_shutdown()`] are called).
3526    ///
3527    ///  * When the application receives data from the peer (for example any
3528    ///    time [`stream_recv()`] is called).
3529    ///
3530    /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3531    /// `send()` and all calls will return [`Done`].
3532    ///
3533    /// [`Done`]: enum.Error.html#variant.Done
3534    /// [`recv()`]: struct.Connection.html#method.recv
3535    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3536    /// [`stream_send()`]: struct.Connection.html#method.stream_send
3537    /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3538    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3539    /// [`is_draining()`]: struct.Connection.html#method.is_draining
3540    ///
3541    /// ## Examples:
3542    ///
3543    /// ```no_run
3544    /// # let mut out = [0; 512];
3545    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3546    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3547    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3548    /// # let peer = "127.0.0.1:1234".parse().unwrap();
3549    /// # let local = socket.local_addr().unwrap();
3550    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3551    /// loop {
3552    ///     let (write, send_info) = match conn.send(&mut out) {
3553    ///         Ok(v) => v,
3554    ///
3555    ///         Err(quiche::Error::Done) => {
3556    ///             // Done writing.
3557    ///             break;
3558    ///         },
3559    ///
3560    ///         Err(e) => {
3561    ///             // An error occurred, handle it.
3562    ///             break;
3563    ///         },
3564    ///     };
3565    ///
3566    ///     socket.send_to(&out[..write], &send_info.to).unwrap();
3567    /// }
3568    /// # Ok::<(), quiche::Error>(())
3569    /// ```
3570    pub fn send(&mut self, out: &mut [u8]) -> Result<(usize, SendInfo)> {
3571        self.send_on_path(out, None, None)
3572    }
3573
3574    /// Writes a single QUIC packet to be sent to the peer from the specified
3575    /// local address `from` to the destination address `to`.
3576    ///
3577    /// The behavior of this method differs depending on the value of the `from`
3578    /// and `to` parameters:
3579    ///
3580    ///  * If both are `Some`, then the method only consider the 4-tuple
3581    ///    (`from`, `to`). Application can monitor the 4-tuple availability,
3582    ///    either by monitoring [`path_event_next()`] events or by relying on
3583    ///    the [`paths_iter()`] method. If the provided 4-tuple does not exist
3584    ///    on the connection (anymore), it returns an [`InvalidState`].
3585    ///
3586    ///  * If `from` is `Some` and `to` is `None`, then the method only
3587    ///    considers sending packets on paths having `from` as local address.
3588    ///
3589    ///  * If `to` is `Some` and `from` is `None`, then the method only
3590    ///    considers sending packets on paths having `to` as peer address.
3591    ///
3592    ///  * If both are `None`, all available paths are considered.
3593    ///
3594    /// On success the number of bytes written to the output buffer is
3595    /// returned, or [`Done`] if there was nothing to write.
3596    ///
3597    /// The application should call `send_on_path()` multiple times until
3598    /// [`Done`] is returned, indicating that there are no more packets to
3599    /// send. It is recommended that `send_on_path()` be called in the
3600    /// following cases:
3601    ///
3602    ///  * When the application receives QUIC packets from the peer (that is,
3603    ///    any time [`recv()`] is also called).
3604    ///
3605    ///  * When the connection timer expires (that is, any time [`on_timeout()`]
3606    ///    is also called).
3607    ///
3608    ///  * When the application sends data to the peer (for examples, any time
3609    ///    [`stream_send()`] or [`stream_shutdown()`] are called).
3610    ///
3611    ///  * When the application receives data from the peer (for example any
3612    ///    time [`stream_recv()`] is called).
3613    ///
3614    /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3615    /// `send_on_path()` and all calls will return [`Done`].
3616    ///
3617    /// [`Done`]: enum.Error.html#variant.Done
3618    /// [`InvalidState`]: enum.Error.html#InvalidState
3619    /// [`recv()`]: struct.Connection.html#method.recv
3620    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3621    /// [`stream_send()`]: struct.Connection.html#method.stream_send
3622    /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3623    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3624    /// [`path_event_next()`]: struct.Connection.html#method.path_event_next
3625    /// [`paths_iter()`]: struct.Connection.html#method.paths_iter
3626    /// [`is_draining()`]: struct.Connection.html#method.is_draining
3627    ///
3628    /// ## Examples:
3629    ///
3630    /// ```no_run
3631    /// # let mut out = [0; 512];
3632    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3633    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3634    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3635    /// # let peer = "127.0.0.1:1234".parse().unwrap();
3636    /// # let local = socket.local_addr().unwrap();
3637    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3638    /// loop {
3639    ///     let (write, send_info) = match conn.send_on_path(&mut out, Some(local), Some(peer)) {
3640    ///         Ok(v) => v,
3641    ///
3642    ///         Err(quiche::Error::Done) => {
3643    ///             // Done writing.
3644    ///             break;
3645    ///         },
3646    ///
3647    ///         Err(e) => {
3648    ///             // An error occurred, handle it.
3649    ///             break;
3650    ///         },
3651    ///     };
3652    ///
3653    ///     socket.send_to(&out[..write], &send_info.to).unwrap();
3654    /// }
3655    /// # Ok::<(), quiche::Error>(())
3656    /// ```
3657    pub fn send_on_path(
3658        &mut self, out: &mut [u8], from: Option<SocketAddr>,
3659        to: Option<SocketAddr>,
3660    ) -> Result<(usize, SendInfo)> {
3661        if out.is_empty() {
3662            return Err(Error::BufferTooShort);
3663        }
3664
3665        if self.is_closed() || self.is_draining() {
3666            return Err(Error::Done);
3667        }
3668
3669        let now = time::Instant::now();
3670
3671        if self.local_error.is_none() {
3672            self.do_handshake(now)?;
3673        }
3674
3675        // Forwarding the error value here could confuse
3676        // applications, as they may not expect getting a `recv()`
3677        // error when calling `send()`.
3678        //
3679        // We simply fall-through to sending packets, which should
3680        // take care of terminating the connection as needed.
3681        let _ = self.process_undecrypted_0rtt_packets();
3682
3683        // There's no point in trying to send a packet if the Initial secrets
3684        // have not been derived yet, so return early.
3685        if !self.derived_initial_secrets {
3686            return Err(Error::Done);
3687        }
3688
3689        let mut has_initial = false;
3690
3691        let mut done = 0;
3692
3693        // Limit output packet size to respect the sender and receiver's
3694        // maximum UDP payload size limit.
3695        let mut left = cmp::min(out.len(), self.max_send_udp_payload_size());
3696
3697        let send_pid = match (from, to) {
3698            (Some(f), Some(t)) => self
3699                .paths
3700                .path_id_from_addrs(&(f, t))
3701                .ok_or(Error::InvalidState)?,
3702
3703            _ => self.get_send_path_id(from, to)?,
3704        };
3705
3706        let send_path = self.paths.get_mut(send_pid)?;
3707
3708        // Update max datagram size to allow path MTU discovery probe to be sent.
3709        if send_path.pmtud.get_probe_status() {
3710            let size = if self.handshake_confirmed || self.handshake_done_sent {
3711                send_path.pmtud.get_probe_size()
3712            } else {
3713                send_path.pmtud.get_current()
3714            };
3715
3716            send_path.recovery.pmtud_update_max_datagram_size(size);
3717
3718            left = cmp::min(out.len(), send_path.recovery.max_datagram_size());
3719        }
3720
3721        // Limit data sent by the server based on the amount of data received
3722        // from the client before its address is validated.
3723        if !send_path.verified_peer_address && self.is_server {
3724            left = cmp::min(left, send_path.max_send_bytes);
3725        }
3726
3727        // Generate coalesced packets.
3728        while left > 0 {
3729            let (ty, written) = match self.send_single(
3730                &mut out[done..done + left],
3731                send_pid,
3732                has_initial,
3733                now,
3734            ) {
3735                Ok(v) => v,
3736
3737                Err(Error::BufferTooShort) | Err(Error::Done) => break,
3738
3739                Err(e) => return Err(e),
3740            };
3741
3742            done += written;
3743            left -= written;
3744
3745            match ty {
3746                packet::Type::Initial => has_initial = true,
3747
3748                // No more packets can be coalesced after a 1-RTT.
3749                packet::Type::Short => break,
3750
3751                _ => (),
3752            };
3753
3754            // When sending multiple PTO probes, don't coalesce them together,
3755            // so they are sent on separate UDP datagrams.
3756            if let Ok(epoch) = ty.to_epoch() {
3757                if self.paths.get_mut(send_pid)?.recovery.loss_probes(epoch) > 0 {
3758                    break;
3759                }
3760            }
3761
3762            // Don't coalesce packets that must go on different paths.
3763            if !(from.is_some() && to.is_some()) &&
3764                self.get_send_path_id(from, to)? != send_pid
3765            {
3766                break;
3767            }
3768        }
3769
3770        if done == 0 {
3771            self.last_tx_data = self.tx_data;
3772
3773            return Err(Error::Done);
3774        }
3775
3776        // Pad UDP datagram if it contains a QUIC Initial packet.
3777        #[cfg(not(feature = "fuzzing"))]
3778        if has_initial && left > 0 && done < MIN_CLIENT_INITIAL_LEN {
3779            let pad_len = cmp::min(left, MIN_CLIENT_INITIAL_LEN - done);
3780
3781            // Fill padding area with null bytes, to avoid leaking information
3782            // in case the application reuses the packet buffer.
3783            out[done..done + pad_len].fill(0);
3784
3785            done += pad_len;
3786        }
3787
3788        let send_path = self.paths.get(send_pid)?;
3789
3790        let info = SendInfo {
3791            from: send_path.local_addr(),
3792            to: send_path.peer_addr(),
3793
3794            at: send_path.recovery.get_packet_send_time(now),
3795        };
3796
3797        Ok((done, info))
3798    }
3799
3800    fn send_single(
3801        &mut self, out: &mut [u8], send_pid: usize, has_initial: bool,
3802        now: time::Instant,
3803    ) -> Result<(packet::Type, usize)> {
3804        if out.is_empty() {
3805            return Err(Error::BufferTooShort);
3806        }
3807
3808        if self.is_draining() {
3809            return Err(Error::Done);
3810        }
3811
3812        let is_closing = self.local_error.is_some();
3813
3814        let out_len = out.len();
3815
3816        let mut b = octets::OctetsMut::with_slice(out);
3817
3818        let pkt_type = self.write_pkt_type(send_pid)?;
3819
3820        let max_dgram_len = if !self.dgram_send_queue.is_empty() {
3821            self.dgram_max_writable_len()
3822        } else {
3823            None
3824        };
3825
3826        let epoch = pkt_type.to_epoch()?;
3827        let pkt_space = &mut self.pkt_num_spaces[epoch];
3828        let crypto_ctx = &mut self.crypto_ctx[epoch];
3829
3830        // Process lost frames. There might be several paths having lost frames.
3831        for (_, p) in self.paths.iter_mut() {
3832            for lost in p.recovery.get_lost_frames(epoch) {
3833                match lost {
3834                    frame::Frame::CryptoHeader { offset, length } => {
3835                        crypto_ctx.crypto_stream.send.retransmit(offset, length);
3836
3837                        self.stream_retrans_bytes += length as u64;
3838                        p.stream_retrans_bytes += length as u64;
3839
3840                        self.retrans_count += 1;
3841                        p.retrans_count += 1;
3842                    },
3843
3844                    frame::Frame::StreamHeader {
3845                        stream_id,
3846                        offset,
3847                        length,
3848                        fin,
3849                    } => {
3850                        let stream = match self.streams.get_mut(stream_id) {
3851                            Some(v) => v,
3852
3853                            None => continue,
3854                        };
3855
3856                        let was_flushable = stream.is_flushable();
3857
3858                        let empty_fin = length == 0 && fin;
3859
3860                        stream.send.retransmit(offset, length);
3861
3862                        // If the stream is now flushable push it to the
3863                        // flushable queue, but only if it wasn't already
3864                        // queued.
3865                        //
3866                        // Consider the stream flushable also when we are
3867                        // sending a zero-length frame that has the fin flag
3868                        // set.
3869                        if (stream.is_flushable() || empty_fin) && !was_flushable
3870                        {
3871                            let priority_key = Arc::clone(&stream.priority_key);
3872                            self.streams.insert_flushable(&priority_key);
3873                        }
3874
3875                        self.stream_retrans_bytes += length as u64;
3876                        p.stream_retrans_bytes += length as u64;
3877
3878                        self.retrans_count += 1;
3879                        p.retrans_count += 1;
3880                    },
3881
3882                    frame::Frame::ACK { .. } => {
3883                        pkt_space.ack_elicited = true;
3884                    },
3885
3886                    frame::Frame::ResetStream {
3887                        stream_id,
3888                        error_code,
3889                        final_size,
3890                    } =>
3891                        if self.streams.get(stream_id).is_some() {
3892                            self.streams
3893                                .insert_reset(stream_id, error_code, final_size);
3894                        },
3895
3896                    // Retransmit HANDSHAKE_DONE only if it hasn't been acked at
3897                    // least once already.
3898                    frame::Frame::HandshakeDone if !self.handshake_done_acked => {
3899                        self.handshake_done_sent = false;
3900                    },
3901
3902                    frame::Frame::MaxStreamData { stream_id, .. } => {
3903                        if self.streams.get(stream_id).is_some() {
3904                            self.streams.insert_almost_full(stream_id);
3905                        }
3906                    },
3907
3908                    frame::Frame::MaxData { .. } => {
3909                        self.almost_full = true;
3910                    },
3911
3912                    frame::Frame::NewConnectionId { seq_num, .. } => {
3913                        self.ids.mark_advertise_new_scid_seq(seq_num, true);
3914                    },
3915
3916                    frame::Frame::RetireConnectionId { seq_num } => {
3917                        self.ids.mark_retire_dcid_seq(seq_num, true)?;
3918                    },
3919
3920                    frame::Frame::Ping { mtu_probe } if mtu_probe.is_some() => {
3921                        p.pmtud.pmtu_probe_lost();
3922                    },
3923
3924                    _ => (),
3925                }
3926            }
3927        }
3928
3929        let is_app_limited = self.delivery_rate_check_if_app_limited();
3930        let n_paths = self.paths.len();
3931        let path = self.paths.get_mut(send_pid)?;
3932        let flow_control = &mut self.flow_control;
3933        let pkt_space = &mut self.pkt_num_spaces[epoch];
3934        let crypto_ctx = &mut self.crypto_ctx[epoch];
3935
3936        let mut left = if path.pmtud.is_enabled() {
3937            // Limit output buffer size by estimated path MTU.
3938            cmp::min(path.pmtud.get_current(), b.cap())
3939        } else {
3940            b.cap()
3941        };
3942
3943        let pn = self.next_pkt_num;
3944        let largest_acked_pkt =
3945            path.recovery.get_largest_acked_on_epoch(epoch).unwrap_or(0);
3946        let pn_len = packet::pkt_num_len(pn, largest_acked_pkt);
3947
3948        // The AEAD overhead at the current encryption level.
3949        let crypto_overhead = crypto_ctx.crypto_overhead().ok_or(Error::Done)?;
3950
3951        let dcid_seq = path.active_dcid_seq.ok_or(Error::OutOfIdentifiers)?;
3952
3953        let dcid =
3954            ConnectionId::from_ref(self.ids.get_dcid(dcid_seq)?.cid.as_ref());
3955
3956        let scid = if let Some(scid_seq) = path.active_scid_seq {
3957            ConnectionId::from_ref(self.ids.get_scid(scid_seq)?.cid.as_ref())
3958        } else if pkt_type == packet::Type::Short {
3959            ConnectionId::default()
3960        } else {
3961            return Err(Error::InvalidState);
3962        };
3963
3964        let hdr = Header {
3965            ty: pkt_type,
3966
3967            version: self.version,
3968
3969            dcid,
3970            scid,
3971
3972            pkt_num: 0,
3973            pkt_num_len: pn_len,
3974
3975            // Only clone token for Initial packets, as other packets don't have
3976            // this field (Retry doesn't count, as it's not encoded as part of
3977            // this code path).
3978            token: if pkt_type == packet::Type::Initial {
3979                self.token.clone()
3980            } else {
3981                None
3982            },
3983
3984            versions: None,
3985            key_phase: self.key_phase,
3986        };
3987
3988        hdr.to_bytes(&mut b)?;
3989
3990        let hdr_trace = if log::max_level() == log::LevelFilter::Trace {
3991            Some(format!("{hdr:?}"))
3992        } else {
3993            None
3994        };
3995
3996        let hdr_ty = hdr.ty;
3997
3998        #[cfg(feature = "qlog")]
3999        let qlog_pkt_hdr = self.qlog.streamer.as_ref().map(|_q| {
4000            qlog::events::quic::PacketHeader::with_type(
4001                hdr.ty.to_qlog(),
4002                Some(pn),
4003                Some(hdr.version),
4004                Some(&hdr.scid),
4005                Some(&hdr.dcid),
4006            )
4007        });
4008
4009        // Calculate the space required for the packet, including the header
4010        // the payload length, the packet number and the AEAD overhead.
4011        let mut overhead = b.off() + pn_len + crypto_overhead;
4012
4013        // We assume that the payload length, which is only present in long
4014        // header packets, can always be encoded with a 2-byte varint.
4015        if pkt_type != packet::Type::Short {
4016            overhead += PAYLOAD_LENGTH_LEN;
4017        }
4018
4019        // Make sure we have enough space left for the packet overhead.
4020        match left.checked_sub(overhead) {
4021            Some(v) => left = v,
4022
4023            None => {
4024                // We can't send more because there isn't enough space available
4025                // in the output buffer.
4026                //
4027                // This usually happens when we try to send a new packet but
4028                // failed because cwnd is almost full. In such case app_limited
4029                // is set to false here to make cwnd grow when ACK is received.
4030                path.recovery.update_app_limited(false);
4031                return Err(Error::Done);
4032            },
4033        }
4034
4035        // Make sure there is enough space for the minimum payload length.
4036        if left < PAYLOAD_MIN_LEN {
4037            path.recovery.update_app_limited(false);
4038            return Err(Error::Done);
4039        }
4040
4041        let mut frames: SmallVec<[frame::Frame; 1]> = SmallVec::new();
4042
4043        let mut ack_eliciting = false;
4044        let mut in_flight = false;
4045        // Foll. flag used to upgrade datagram size, if probe successful
4046        let mut pmtud_probe = false;
4047        let mut has_data = false;
4048
4049        // Whether or not we should explicitly elicit an ACK via PING frame if we
4050        // implicitly elicit one otherwise.
4051        let ack_elicit_required = path.recovery.should_elicit_ack(epoch);
4052
4053        let header_offset = b.off();
4054
4055        // Reserve space for payload length in advance. Since we don't yet know
4056        // what the final length will be, we reserve 2 bytes in all cases.
4057        //
4058        // Only long header packets have an explicit length field.
4059        if pkt_type != packet::Type::Short {
4060            b.skip(PAYLOAD_LENGTH_LEN)?;
4061        }
4062
4063        packet::encode_pkt_num(pn, pn_len, &mut b)?;
4064
4065        let payload_offset = b.off();
4066
4067        let cwnd_available =
4068            path.recovery.cwnd_available().saturating_sub(overhead);
4069
4070        let left_before_packing_ack_frame = left;
4071
4072        // Create ACK frame.
4073        //
4074        // When we need to explicitly elicit an ACK via PING later, go ahead and
4075        // generate an ACK (if there's anything to ACK) since we're going to
4076        // send a packet with PING anyways, even if we haven't received anything
4077        // ACK eliciting.
4078        if pkt_space.recv_pkt_need_ack.len() > 0 &&
4079            (pkt_space.ack_elicited || ack_elicit_required) &&
4080            (!is_closing ||
4081                (pkt_type == Type::Handshake &&
4082                    self.local_error
4083                        .as_ref()
4084                        .is_some_and(|le| le.is_app))) &&
4085            path.active()
4086        {
4087            let ack_delay = pkt_space.largest_rx_pkt_time.elapsed();
4088
4089            let ack_delay = ack_delay.as_micros() as u64 /
4090                2_u64
4091                    .pow(self.local_transport_params.ack_delay_exponent as u32);
4092
4093            let frame = frame::Frame::ACK {
4094                ack_delay,
4095                ranges: pkt_space.recv_pkt_need_ack.clone(),
4096                ecn_counts: None, // sending ECN is not supported at this time
4097            };
4098
4099            // When a PING frame needs to be sent, avoid sending the ACK if
4100            // there is not enough cwnd available for both (note that PING
4101            // frames are always 1 byte, so we just need to check that the
4102            // ACK's length is lower than cwnd).
4103            if pkt_space.ack_elicited || frame.wire_len() < cwnd_available {
4104                // ACK-only packets are not congestion controlled so ACKs must
4105                // be bundled considering the buffer capacity only, and not the
4106                // available cwnd.
4107                if push_frame_to_pkt!(b, frames, frame, left) {
4108                    pkt_space.ack_elicited = false;
4109                }
4110            }
4111        }
4112
4113        // Limit output packet size by congestion window size.
4114        left = cmp::min(
4115            left,
4116            // Bytes consumed by ACK frames.
4117            cwnd_available.saturating_sub(left_before_packing_ack_frame - left),
4118        );
4119
4120        let mut challenge_data = None;
4121
4122        let active_path = self.paths.get_active_mut()?;
4123
4124        if pkt_type == packet::Type::Short {
4125            // Create PMTUD probe.
4126            //
4127            // In order to send a PMTUD probe the current `left` value, which was
4128            // already limited by the current PMTU measure, needs to be ignored,
4129            // but the outgoing packet still needs to be limited by
4130            // the output buffer size, as well as the congestion
4131            // window.
4132            //
4133            // In addition, the PMTUD probe is only generated when the handshake
4134            // is confirmed, to avoid interfering with the handshake
4135            // (e.g. due to the anti-amplification limits).
4136
4137            let pmtu_probe = active_path.should_send_pmtu_probe(
4138                self.handshake_confirmed,
4139                self.handshake_done_sent,
4140                out_len,
4141                is_closing,
4142                frames.is_empty(),
4143            );
4144
4145            trace!("{} pmtud probe status {} hs_con={} hs_sent={} cwnd_avail={} out_len={} left={}", self.trace_id, pmtu_probe, self.handshake_confirmed, self.handshake_done_sent,
4146                    active_path.recovery.cwnd_available(), out_len, left);
4147
4148            if pmtu_probe {
4149                trace!(
4150                    "{} sending pmtud probe pmtu_probe={} next_size={} pmtu={}",
4151                    self.trace_id,
4152                    active_path.pmtud.get_probe_size(),
4153                    active_path.pmtud.get_probe_status(),
4154                    active_path.pmtud.get_current(),
4155                );
4156
4157                left = active_path.pmtud.get_probe_size();
4158
4159                match left.checked_sub(overhead) {
4160                    Some(v) => left = v,
4161
4162                    None => {
4163                        // We can't send more because there isn't enough space
4164                        // available in the output buffer.
4165                        //
4166                        // This usually happens when we try to send a new packet
4167                        // but failed because cwnd is almost full.
4168                        //
4169                        // In such case app_limited is set to false here to make
4170                        // cwnd grow when ACK is received.
4171                        active_path.recovery.update_app_limited(false);
4172                        return Err(Error::Done);
4173                    },
4174                }
4175
4176                let frame = frame::Frame::Padding {
4177                    len: active_path.pmtud.get_probe_size() - overhead - 1,
4178                };
4179
4180                if push_frame_to_pkt!(b, frames, frame, left) {
4181                    let frame = frame::Frame::Ping {
4182                        mtu_probe: Some(active_path.pmtud.get_probe_size()),
4183                    };
4184
4185                    if push_frame_to_pkt!(b, frames, frame, left) {
4186                        ack_eliciting = true;
4187                        in_flight = true;
4188                    }
4189                }
4190
4191                pmtud_probe = true;
4192            }
4193
4194            let path = self.paths.get_mut(send_pid)?;
4195            // Create PATH_RESPONSE frame if needed.
4196            // We do not try to ensure that these are really sent.
4197            while let Some(challenge) = path.pop_received_challenge() {
4198                let frame = frame::Frame::PathResponse { data: challenge };
4199
4200                if push_frame_to_pkt!(b, frames, frame, left) {
4201                    ack_eliciting = true;
4202                    in_flight = true;
4203                } else {
4204                    // If there are other pending PATH_RESPONSE, don't lose them
4205                    // now.
4206                    break;
4207                }
4208            }
4209
4210            // Create PATH_CHALLENGE frame if needed.
4211            if path.validation_requested() {
4212                // TODO: ensure that data is unique over paths.
4213                let data = rand::rand_u64().to_be_bytes();
4214
4215                let frame = frame::Frame::PathChallenge { data };
4216
4217                if push_frame_to_pkt!(b, frames, frame, left) {
4218                    // Let's notify the path once we know the packet size.
4219                    challenge_data = Some(data);
4220
4221                    ack_eliciting = true;
4222                    in_flight = true;
4223                }
4224            }
4225
4226            if let Some(key_update) = crypto_ctx.key_update.as_mut() {
4227                key_update.update_acked = true;
4228            }
4229        }
4230
4231        let path = self.paths.get_mut(send_pid)?;
4232
4233        if pkt_type == packet::Type::Short && !is_closing {
4234            // Create NEW_CONNECTION_ID frames as needed.
4235            while let Some(seq_num) = self.ids.next_advertise_new_scid_seq() {
4236                let frame = self.ids.get_new_connection_id_frame_for(seq_num)?;
4237
4238                if push_frame_to_pkt!(b, frames, frame, left) {
4239                    self.ids.mark_advertise_new_scid_seq(seq_num, false);
4240
4241                    ack_eliciting = true;
4242                    in_flight = true;
4243                } else {
4244                    break;
4245                }
4246            }
4247        }
4248
4249        if pkt_type == packet::Type::Short && !is_closing && path.active() {
4250            // Create HANDSHAKE_DONE frame.
4251            // self.should_send_handshake_done() but without the need to borrow
4252            if self.handshake_completed &&
4253                !self.handshake_done_sent &&
4254                self.is_server
4255            {
4256                let frame = frame::Frame::HandshakeDone;
4257
4258                if push_frame_to_pkt!(b, frames, frame, left) {
4259                    self.handshake_done_sent = true;
4260
4261                    ack_eliciting = true;
4262                    in_flight = true;
4263                }
4264            }
4265
4266            // Create MAX_STREAMS_BIDI frame.
4267            if self.streams.should_update_max_streams_bidi() {
4268                let frame = frame::Frame::MaxStreamsBidi {
4269                    max: self.streams.max_streams_bidi_next(),
4270                };
4271
4272                if push_frame_to_pkt!(b, frames, frame, left) {
4273                    self.streams.update_max_streams_bidi();
4274
4275                    ack_eliciting = true;
4276                    in_flight = true;
4277                }
4278            }
4279
4280            // Create MAX_STREAMS_UNI frame.
4281            if self.streams.should_update_max_streams_uni() {
4282                let frame = frame::Frame::MaxStreamsUni {
4283                    max: self.streams.max_streams_uni_next(),
4284                };
4285
4286                if push_frame_to_pkt!(b, frames, frame, left) {
4287                    self.streams.update_max_streams_uni();
4288
4289                    ack_eliciting = true;
4290                    in_flight = true;
4291                }
4292            }
4293
4294            // Create DATA_BLOCKED frame.
4295            if let Some(limit) = self.blocked_limit {
4296                let frame = frame::Frame::DataBlocked { limit };
4297
4298                if push_frame_to_pkt!(b, frames, frame, left) {
4299                    self.blocked_limit = None;
4300
4301                    ack_eliciting = true;
4302                    in_flight = true;
4303                }
4304            }
4305
4306            // Create MAX_STREAM_DATA frames as needed.
4307            for stream_id in self.streams.almost_full() {
4308                let stream = match self.streams.get_mut(stream_id) {
4309                    Some(v) => v,
4310
4311                    None => {
4312                        // The stream doesn't exist anymore, so remove it from
4313                        // the almost full set.
4314                        self.streams.remove_almost_full(stream_id);
4315                        continue;
4316                    },
4317                };
4318
4319                // Autotune the stream window size.
4320                stream.recv.autotune_window(now, path.recovery.rtt());
4321
4322                let frame = frame::Frame::MaxStreamData {
4323                    stream_id,
4324                    max: stream.recv.max_data_next(),
4325                };
4326
4327                if push_frame_to_pkt!(b, frames, frame, left) {
4328                    let recv_win = stream.recv.window();
4329
4330                    stream.recv.update_max_data(now);
4331
4332                    self.streams.remove_almost_full(stream_id);
4333
4334                    ack_eliciting = true;
4335                    in_flight = true;
4336
4337                    // Make sure the connection window always has some
4338                    // room compared to the stream window.
4339                    flow_control.ensure_window_lower_bound(
4340                        (recv_win as f64 * CONNECTION_WINDOW_FACTOR) as u64,
4341                    );
4342
4343                    // Also send MAX_DATA when MAX_STREAM_DATA is sent, to avoid a
4344                    // potential race condition.
4345                    self.almost_full = true;
4346                }
4347            }
4348
4349            // Create MAX_DATA frame as needed.
4350            if self.almost_full &&
4351                flow_control.max_data() < flow_control.max_data_next()
4352            {
4353                // Autotune the connection window size.
4354                flow_control.autotune_window(now, path.recovery.rtt());
4355
4356                let frame = frame::Frame::MaxData {
4357                    max: flow_control.max_data_next(),
4358                };
4359
4360                if push_frame_to_pkt!(b, frames, frame, left) {
4361                    self.almost_full = false;
4362
4363                    // Commits the new max_rx_data limit.
4364                    flow_control.update_max_data(now);
4365
4366                    ack_eliciting = true;
4367                    in_flight = true;
4368                }
4369            }
4370
4371            // Create STOP_SENDING frames as needed.
4372            for (stream_id, error_code) in self
4373                .streams
4374                .stopped()
4375                .map(|(&k, &v)| (k, v))
4376                .collect::<Vec<(u64, u64)>>()
4377            {
4378                let frame = frame::Frame::StopSending {
4379                    stream_id,
4380                    error_code,
4381                };
4382
4383                if push_frame_to_pkt!(b, frames, frame, left) {
4384                    self.streams.remove_stopped(stream_id);
4385
4386                    ack_eliciting = true;
4387                    in_flight = true;
4388                }
4389            }
4390
4391            // Create RESET_STREAM frames as needed.
4392            for (stream_id, (error_code, final_size)) in self
4393                .streams
4394                .reset()
4395                .map(|(&k, &v)| (k, v))
4396                .collect::<Vec<(u64, (u64, u64))>>()
4397            {
4398                let frame = frame::Frame::ResetStream {
4399                    stream_id,
4400                    error_code,
4401                    final_size,
4402                };
4403
4404                if push_frame_to_pkt!(b, frames, frame, left) {
4405                    self.streams.remove_reset(stream_id);
4406
4407                    ack_eliciting = true;
4408                    in_flight = true;
4409                }
4410            }
4411
4412            // Create STREAM_DATA_BLOCKED frames as needed.
4413            for (stream_id, limit) in self
4414                .streams
4415                .blocked()
4416                .map(|(&k, &v)| (k, v))
4417                .collect::<Vec<(u64, u64)>>()
4418            {
4419                let frame = frame::Frame::StreamDataBlocked { stream_id, limit };
4420
4421                if push_frame_to_pkt!(b, frames, frame, left) {
4422                    self.streams.remove_blocked(stream_id);
4423
4424                    ack_eliciting = true;
4425                    in_flight = true;
4426                }
4427            }
4428
4429            // Create RETIRE_CONNECTION_ID frames as needed.
4430            while let Some(seq_num) = self.ids.next_retire_dcid_seq() {
4431                // The sequence number specified in a RETIRE_CONNECTION_ID frame
4432                // MUST NOT refer to the Destination Connection ID field of the
4433                // packet in which the frame is contained.
4434                let dcid_seq = path.active_dcid_seq.ok_or(Error::InvalidState)?;
4435
4436                if seq_num == dcid_seq {
4437                    continue;
4438                }
4439
4440                let frame = frame::Frame::RetireConnectionId { seq_num };
4441
4442                if push_frame_to_pkt!(b, frames, frame, left) {
4443                    self.ids.mark_retire_dcid_seq(seq_num, false)?;
4444
4445                    ack_eliciting = true;
4446                    in_flight = true;
4447                } else {
4448                    break;
4449                }
4450            }
4451        }
4452
4453        // Create CONNECTION_CLOSE frame. Try to send this only on the active
4454        // path, unless it is the last one available.
4455        if path.active() || n_paths == 1 {
4456            if let Some(conn_err) = self.local_error.as_ref() {
4457                if conn_err.is_app {
4458                    // Create ApplicationClose frame.
4459                    if pkt_type == packet::Type::Short {
4460                        let frame = frame::Frame::ApplicationClose {
4461                            error_code: conn_err.error_code,
4462                            reason: conn_err.reason.clone(),
4463                        };
4464
4465                        if push_frame_to_pkt!(b, frames, frame, left) {
4466                            let pto = path.recovery.pto();
4467                            self.draining_timer = Some(now + (pto * 3));
4468
4469                            ack_eliciting = true;
4470                            in_flight = true;
4471                        }
4472                    }
4473                } else {
4474                    // Create ConnectionClose frame.
4475                    let frame = frame::Frame::ConnectionClose {
4476                        error_code: conn_err.error_code,
4477                        frame_type: 0,
4478                        reason: conn_err.reason.clone(),
4479                    };
4480
4481                    if push_frame_to_pkt!(b, frames, frame, left) {
4482                        let pto = path.recovery.pto();
4483                        self.draining_timer = Some(now + (pto * 3));
4484
4485                        ack_eliciting = true;
4486                        in_flight = true;
4487                    }
4488                }
4489            }
4490        }
4491
4492        // Create CRYPTO frame.
4493        if crypto_ctx.crypto_stream.is_flushable() &&
4494            left > frame::MAX_CRYPTO_OVERHEAD &&
4495            !is_closing &&
4496            path.active()
4497        {
4498            let crypto_off = crypto_ctx.crypto_stream.send.off_front();
4499
4500            // Encode the frame.
4501            //
4502            // Instead of creating a `frame::Frame` object, encode the frame
4503            // directly into the packet buffer.
4504            //
4505            // First we reserve some space in the output buffer for writing the
4506            // frame header (we assume the length field is always a 2-byte
4507            // varint as we don't know the value yet).
4508            //
4509            // Then we emit the data from the crypto stream's send buffer.
4510            //
4511            // Finally we go back and encode the frame header with the now
4512            // available information.
4513            let hdr_off = b.off();
4514            let hdr_len = 1 + // frame type
4515                octets::varint_len(crypto_off) + // offset
4516                2; // length, always encode as 2-byte varint
4517
4518            if let Some(max_len) = left.checked_sub(hdr_len) {
4519                let (mut crypto_hdr, mut crypto_payload) =
4520                    b.split_at(hdr_off + hdr_len)?;
4521
4522                // Write stream data into the packet buffer.
4523                let (len, _) = crypto_ctx
4524                    .crypto_stream
4525                    .send
4526                    .emit(&mut crypto_payload.as_mut()[..max_len])?;
4527
4528                // Encode the frame's header.
4529                //
4530                // Due to how `OctetsMut::split_at()` works, `crypto_hdr` starts
4531                // from the initial offset of `b` (rather than the current
4532                // offset), so it needs to be advanced to the
4533                // initial frame offset.
4534                crypto_hdr.skip(hdr_off)?;
4535
4536                frame::encode_crypto_header(
4537                    crypto_off,
4538                    len as u64,
4539                    &mut crypto_hdr,
4540                )?;
4541
4542                // Advance the packet buffer's offset.
4543                b.skip(hdr_len + len)?;
4544
4545                let frame = frame::Frame::CryptoHeader {
4546                    offset: crypto_off,
4547                    length: len,
4548                };
4549
4550                if push_frame_to_pkt!(b, frames, frame, left) {
4551                    ack_eliciting = true;
4552                    in_flight = true;
4553                    has_data = true;
4554                }
4555            }
4556        }
4557
4558        // The preference of data-bearing frame to include in a packet
4559        // is managed by `self.emit_dgram`. However, whether any frames
4560        // can be sent depends on the state of their buffers. In the case
4561        // where one type is preferred but its buffer is empty, fall back
4562        // to the other type in order not to waste this function call.
4563        let mut dgram_emitted = false;
4564        let dgrams_to_emit = max_dgram_len.is_some();
4565        let stream_to_emit = self.streams.has_flushable();
4566
4567        let mut do_dgram = self.emit_dgram && dgrams_to_emit;
4568        let do_stream = !self.emit_dgram && stream_to_emit;
4569
4570        if !do_stream && dgrams_to_emit {
4571            do_dgram = true;
4572        }
4573
4574        // Create DATAGRAM frame.
4575        if (pkt_type == packet::Type::Short || pkt_type == packet::Type::ZeroRTT) &&
4576            left > frame::MAX_DGRAM_OVERHEAD &&
4577            !is_closing &&
4578            path.active() &&
4579            do_dgram
4580        {
4581            if let Some(max_dgram_payload) = max_dgram_len {
4582                while let Some(len) = self.dgram_send_queue.peek_front_len() {
4583                    let hdr_off = b.off();
4584                    let hdr_len = 1 + // frame type
4585                        2; // length, always encode as 2-byte varint
4586
4587                    if (hdr_len + len) <= left {
4588                        // Front of the queue fits this packet, send it.
4589                        match self.dgram_send_queue.pop() {
4590                            Some(data) => {
4591                                // Encode the frame.
4592                                //
4593                                // Instead of creating a `frame::Frame` object,
4594                                // encode the frame directly into the packet
4595                                // buffer.
4596                                //
4597                                // First we reserve some space in the output
4598                                // buffer for writing the frame header (we
4599                                // assume the length field is always a 2-byte
4600                                // varint as we don't know the value yet).
4601                                //
4602                                // Then we emit the data from the DATAGRAM's
4603                                // buffer.
4604                                //
4605                                // Finally we go back and encode the frame
4606                                // header with the now available information.
4607                                let (mut dgram_hdr, mut dgram_payload) =
4608                                    b.split_at(hdr_off + hdr_len)?;
4609
4610                                dgram_payload.as_mut()[..len]
4611                                    .copy_from_slice(&data);
4612
4613                                // Encode the frame's header.
4614                                //
4615                                // Due to how `OctetsMut::split_at()` works,
4616                                // `dgram_hdr` starts from the initial offset
4617                                // of `b` (rather than the current offset), so
4618                                // it needs to be advanced to the initial frame
4619                                // offset.
4620                                dgram_hdr.skip(hdr_off)?;
4621
4622                                frame::encode_dgram_header(
4623                                    len as u64,
4624                                    &mut dgram_hdr,
4625                                )?;
4626
4627                                // Advance the packet buffer's offset.
4628                                b.skip(hdr_len + len)?;
4629
4630                                let frame =
4631                                    frame::Frame::DatagramHeader { length: len };
4632
4633                                if push_frame_to_pkt!(b, frames, frame, left) {
4634                                    ack_eliciting = true;
4635                                    in_flight = true;
4636                                    dgram_emitted = true;
4637                                    let _ =
4638                                        self.dgram_sent_count.saturating_add(1);
4639                                    let _ =
4640                                        path.dgram_sent_count.saturating_add(1);
4641                                }
4642                            },
4643
4644                            None => continue,
4645                        };
4646                    } else if len > max_dgram_payload {
4647                        // This dgram frame will never fit. Let's purge it.
4648                        self.dgram_send_queue.pop();
4649                    } else {
4650                        break;
4651                    }
4652                }
4653            }
4654        }
4655
4656        // Create a single STREAM frame for the first stream that is flushable.
4657        if (pkt_type == packet::Type::Short || pkt_type == packet::Type::ZeroRTT) &&
4658            left > frame::MAX_STREAM_OVERHEAD &&
4659            !is_closing &&
4660            path.active() &&
4661            !dgram_emitted
4662        {
4663            while let Some(priority_key) = self.streams.peek_flushable() {
4664                let stream_id = priority_key.id;
4665                let stream = match self.streams.get_mut(stream_id) {
4666                    // Avoid sending frames for streams that were already stopped.
4667                    //
4668                    // This might happen if stream data was buffered but not yet
4669                    // flushed on the wire when a STOP_SENDING frame is received.
4670                    Some(v) if !v.send.is_stopped() => v,
4671                    _ => {
4672                        self.streams.remove_flushable(&priority_key);
4673                        continue;
4674                    },
4675                };
4676
4677                let stream_off = stream.send.off_front();
4678
4679                // Encode the frame.
4680                //
4681                // Instead of creating a `frame::Frame` object, encode the frame
4682                // directly into the packet buffer.
4683                //
4684                // First we reserve some space in the output buffer for writing
4685                // the frame header (we assume the length field is always a
4686                // 2-byte varint as we don't know the value yet).
4687                //
4688                // Then we emit the data from the stream's send buffer.
4689                //
4690                // Finally we go back and encode the frame header with the now
4691                // available information.
4692                let hdr_off = b.off();
4693                let hdr_len = 1 + // frame type
4694                    octets::varint_len(stream_id) + // stream_id
4695                    octets::varint_len(stream_off) + // offset
4696                    2; // length, always encode as 2-byte varint
4697
4698                let max_len = match left.checked_sub(hdr_len) {
4699                    Some(v) => v,
4700                    None => {
4701                        let priority_key = Arc::clone(&stream.priority_key);
4702                        self.streams.remove_flushable(&priority_key);
4703
4704                        continue;
4705                    },
4706                };
4707
4708                let (mut stream_hdr, mut stream_payload) =
4709                    b.split_at(hdr_off + hdr_len)?;
4710
4711                // Write stream data into the packet buffer.
4712                let (len, fin) =
4713                    stream.send.emit(&mut stream_payload.as_mut()[..max_len])?;
4714
4715                // Encode the frame's header.
4716                //
4717                // Due to how `OctetsMut::split_at()` works, `stream_hdr` starts
4718                // from the initial offset of `b` (rather than the current
4719                // offset), so it needs to be advanced to the initial frame
4720                // offset.
4721                stream_hdr.skip(hdr_off)?;
4722
4723                frame::encode_stream_header(
4724                    stream_id,
4725                    stream_off,
4726                    len as u64,
4727                    fin,
4728                    &mut stream_hdr,
4729                )?;
4730
4731                // Advance the packet buffer's offset.
4732                b.skip(hdr_len + len)?;
4733
4734                let frame = frame::Frame::StreamHeader {
4735                    stream_id,
4736                    offset: stream_off,
4737                    length: len,
4738                    fin,
4739                };
4740
4741                if push_frame_to_pkt!(b, frames, frame, left) {
4742                    ack_eliciting = true;
4743                    in_flight = true;
4744                    has_data = true;
4745                }
4746
4747                let priority_key = Arc::clone(&stream.priority_key);
4748                // If the stream is no longer flushable, remove it from the queue
4749                if !stream.is_flushable() {
4750                    self.streams.remove_flushable(&priority_key);
4751                } else if stream.incremental {
4752                    // Shuffle the incremental stream to the back of the
4753                    // queue.
4754                    self.streams.remove_flushable(&priority_key);
4755                    self.streams.insert_flushable(&priority_key);
4756                }
4757
4758                #[cfg(feature = "fuzzing")]
4759                // Coalesce STREAM frames when fuzzing.
4760                if left > frame::MAX_STREAM_OVERHEAD {
4761                    continue;
4762                }
4763
4764                break;
4765            }
4766        }
4767
4768        // Alternate trying to send DATAGRAMs next time.
4769        self.emit_dgram = !dgram_emitted;
4770
4771        // If no other ack-eliciting frame is sent, include a PING frame
4772        // - if PTO probe needed; OR
4773        // - if we've sent too many non ack-eliciting packets without having
4774        // sent an ACK eliciting one; OR
4775        // - the application requested an ack-eliciting frame be sent.
4776        if (ack_elicit_required || path.needs_ack_eliciting) &&
4777            !ack_eliciting &&
4778            left >= 1 &&
4779            !is_closing
4780        {
4781            let frame = frame::Frame::Ping { mtu_probe: None };
4782
4783            if push_frame_to_pkt!(b, frames, frame, left) {
4784                ack_eliciting = true;
4785                in_flight = true;
4786            }
4787        }
4788
4789        if ack_eliciting && !pmtud_probe {
4790            path.needs_ack_eliciting = false;
4791            path.recovery.ping_sent(epoch);
4792        }
4793
4794        if !has_data &&
4795            !dgram_emitted &&
4796            cwnd_available > frame::MAX_STREAM_OVERHEAD
4797        {
4798            path.recovery.on_app_limited();
4799        }
4800
4801        if frames.is_empty() {
4802            // When we reach this point we are not able to write more, so set
4803            // app_limited to false.
4804            path.recovery.update_app_limited(false);
4805            return Err(Error::Done);
4806        }
4807
4808        // When coalescing a 1-RTT packet, we can't add padding in the UDP
4809        // datagram, so use PADDING frames instead.
4810        //
4811        // This is only needed if
4812        // 1) an Initial packet has already been written to the UDP datagram,
4813        // as Initial always requires padding.
4814        //
4815        // 2) this is a probing packet towards an unvalidated peer address.
4816        if (has_initial || !path.validated()) &&
4817            pkt_type == packet::Type::Short &&
4818            left >= 1
4819        {
4820            let frame = frame::Frame::Padding { len: left };
4821
4822            if push_frame_to_pkt!(b, frames, frame, left) {
4823                in_flight = true;
4824            }
4825        }
4826
4827        // Pad payload so that it's always at least 4 bytes.
4828        if b.off() - payload_offset < PAYLOAD_MIN_LEN {
4829            let payload_len = b.off() - payload_offset;
4830
4831            let frame = frame::Frame::Padding {
4832                len: PAYLOAD_MIN_LEN - payload_len,
4833            };
4834
4835            #[allow(unused_assignments)]
4836            if push_frame_to_pkt!(b, frames, frame, left) {
4837                in_flight = true;
4838            }
4839        }
4840
4841        let payload_len = b.off() - payload_offset;
4842
4843        // Fill in payload length.
4844        if pkt_type != packet::Type::Short {
4845            let len = pn_len + payload_len + crypto_overhead;
4846
4847            let (_, mut payload_with_len) = b.split_at(header_offset)?;
4848            payload_with_len
4849                .put_varint_with_len(len as u64, PAYLOAD_LENGTH_LEN)?;
4850        }
4851
4852        trace!(
4853            "{} tx pkt {} len={} pn={} {}",
4854            self.trace_id,
4855            hdr_trace.unwrap_or_default(),
4856            payload_len,
4857            pn,
4858            AddrTupleFmt(path.local_addr(), path.peer_addr())
4859        );
4860
4861        #[cfg(feature = "qlog")]
4862        let mut qlog_frames: SmallVec<
4863            [qlog::events::quic::QuicFrame; 1],
4864        > = SmallVec::with_capacity(frames.len());
4865
4866        for frame in &mut frames {
4867            trace!("{} tx frm {:?}", self.trace_id, frame);
4868
4869            qlog_with_type!(QLOG_PACKET_TX, self.qlog, _q, {
4870                qlog_frames.push(frame.to_qlog());
4871            });
4872        }
4873
4874        qlog_with_type!(QLOG_PACKET_TX, self.qlog, q, {
4875            if let Some(header) = qlog_pkt_hdr {
4876                // Qlog packet raw info described at
4877                // https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema-00#section-5.1
4878                //
4879                // `length` includes packet headers and trailers (AEAD tag).
4880                let length = payload_len + payload_offset + crypto_overhead;
4881                let qlog_raw_info = RawInfo {
4882                    length: Some(length as u64),
4883                    payload_length: Some(payload_len as u64),
4884                    data: None,
4885                };
4886
4887                let send_at_time =
4888                    now.duration_since(q.start_time()).as_secs_f32() * 1000.0;
4889
4890                let ev_data =
4891                    EventData::PacketSent(qlog::events::quic::PacketSent {
4892                        header,
4893                        frames: Some(qlog_frames),
4894                        raw: Some(qlog_raw_info),
4895                        send_at_time: Some(send_at_time),
4896                        ..Default::default()
4897                    });
4898
4899                q.add_event_data_with_instant(ev_data, now).ok();
4900            }
4901        });
4902
4903        let aead = match crypto_ctx.crypto_seal {
4904            Some(ref v) => v,
4905            None => return Err(Error::InvalidState),
4906        };
4907
4908        let written = packet::encrypt_pkt(
4909            &mut b,
4910            pn,
4911            pn_len,
4912            payload_len,
4913            payload_offset,
4914            None,
4915            aead,
4916        )?;
4917
4918        let sent_pkt_has_data = if path.recovery.gcongestion_enabled() {
4919            has_data || dgram_emitted
4920        } else {
4921            has_data
4922        };
4923
4924        let sent_pkt = recovery::Sent {
4925            pkt_num: pn,
4926            frames,
4927            time_sent: now,
4928            time_acked: None,
4929            time_lost: None,
4930            size: if ack_eliciting { written } else { 0 },
4931            ack_eliciting,
4932            in_flight,
4933            delivered: 0,
4934            delivered_time: now,
4935            first_sent_time: now,
4936            is_app_limited: false,
4937            tx_in_flight: 0,
4938            lost: 0,
4939            has_data: sent_pkt_has_data,
4940            pmtud: pmtud_probe,
4941        };
4942
4943        if in_flight && is_app_limited {
4944            path.recovery.delivery_rate_update_app_limited(true);
4945        }
4946
4947        self.next_pkt_num += 1;
4948
4949        let handshake_status = recovery::HandshakeStatus {
4950            has_handshake_keys: self.crypto_ctx[packet::Epoch::Handshake]
4951                .has_keys(),
4952            peer_verified_address: self.peer_verified_initial_address,
4953            completed: self.handshake_completed,
4954        };
4955
4956        path.recovery.on_packet_sent(
4957            sent_pkt,
4958            epoch,
4959            handshake_status,
4960            now,
4961            &self.trace_id,
4962        );
4963
4964        qlog_with_type!(QLOG_METRICS, self.qlog, q, {
4965            if let Some(ev_data) = path.recovery.maybe_qlog() {
4966                q.add_event_data_with_instant(ev_data, now).ok();
4967            }
4968        });
4969
4970        // Record sent packet size if we probe the path.
4971        if let Some(data) = challenge_data {
4972            path.add_challenge_sent(data, written, now);
4973        }
4974
4975        self.sent_count += 1;
4976        self.sent_bytes += written as u64;
4977        path.sent_count += 1;
4978        path.sent_bytes += written as u64;
4979
4980        if self.dgram_send_queue.byte_size() > path.recovery.cwnd_available() {
4981            path.recovery.update_app_limited(false);
4982        }
4983
4984        path.max_send_bytes = path.max_send_bytes.saturating_sub(written);
4985
4986        // On the client, drop initial state after sending an Handshake packet.
4987        if !self.is_server && hdr_ty == packet::Type::Handshake {
4988            self.drop_epoch_state(packet::Epoch::Initial, now);
4989        }
4990
4991        // (Re)start the idle timer if we are sending the first ack-eliciting
4992        // packet since last receiving a packet.
4993        if ack_eliciting && !self.ack_eliciting_sent {
4994            if let Some(idle_timeout) = self.idle_timeout() {
4995                self.idle_timer = Some(now + idle_timeout);
4996            }
4997        }
4998
4999        if ack_eliciting {
5000            self.ack_eliciting_sent = true;
5001        }
5002
5003        let active_path = self.paths.get_active_mut()?;
5004        if active_path.pmtud.is_enabled() {
5005            active_path
5006                .recovery
5007                .pmtud_update_max_datagram_size(active_path.pmtud.get_current());
5008        }
5009
5010        Ok((pkt_type, written))
5011    }
5012
5013    /// Returns the desired send time for the next packet.
5014    #[inline]
5015    pub fn get_next_release_time(&self) -> Option<ReleaseDecision> {
5016        Some(
5017            self.paths
5018                .get_active()
5019                .ok()?
5020                .recovery
5021                .get_next_release_time(),
5022        )
5023    }
5024
5025    /// Returns whether gcongestion is enabled.
5026    #[inline]
5027    pub fn gcongestion_enabled(&self) -> Option<bool> {
5028        Some(self.paths.get_active().ok()?.recovery.gcongestion_enabled())
5029    }
5030
5031    /// Returns the maximum pacing into the future.
5032    ///
5033    /// Equals 1/8 of the smoothed RTT, but at least 1ms and not greater than
5034    /// 5ms.
5035    pub fn max_release_into_future(&self) -> time::Duration {
5036        self.paths
5037            .get_active()
5038            .map(|p| p.recovery.rtt().mul_f64(0.125))
5039            .unwrap_or(time::Duration::from_millis(1))
5040            .max(Duration::from_millis(1))
5041            .min(Duration::from_millis(5))
5042    }
5043
5044    /// Returns whether pacing is enabled.
5045    #[inline]
5046    pub fn pacing_enabled(&self) -> bool {
5047        self.recovery_config.pacing
5048    }
5049
5050    /// Returns the size of the send quantum, in bytes.
5051    ///
5052    /// This represents the maximum size of a packet burst as determined by the
5053    /// congestion control algorithm in use.
5054    ///
5055    /// Applications can, for example, use it in conjunction with segmentation
5056    /// offloading mechanisms as the maximum limit for outgoing aggregates of
5057    /// multiple packets.
5058    #[inline]
5059    pub fn send_quantum(&self) -> usize {
5060        match self.paths.get_active() {
5061            Ok(p) => p.recovery.send_quantum(),
5062            _ => 0,
5063        }
5064    }
5065
5066    /// Returns the size of the send quantum over the given 4-tuple, in bytes.
5067    ///
5068    /// This represents the maximum size of a packet burst as determined by the
5069    /// congestion control algorithm in use.
5070    ///
5071    /// Applications can, for example, use it in conjunction with segmentation
5072    /// offloading mechanisms as the maximum limit for outgoing aggregates of
5073    /// multiple packets.
5074    ///
5075    /// If the (`local_addr`, peer_addr`) 4-tuple relates to a non-existing
5076    /// path, this method returns 0.
5077    pub fn send_quantum_on_path(
5078        &self, local_addr: SocketAddr, peer_addr: SocketAddr,
5079    ) -> usize {
5080        self.paths
5081            .path_id_from_addrs(&(local_addr, peer_addr))
5082            .and_then(|pid| self.paths.get(pid).ok())
5083            .map(|path| path.recovery.send_quantum())
5084            .unwrap_or(0)
5085    }
5086
5087    /// Reads contiguous data from a stream into the provided slice.
5088    ///
5089    /// The slice must be sized by the caller and will be populated up to its
5090    /// capacity.
5091    ///
5092    /// On success the amount of bytes read and a flag indicating the fin state
5093    /// is returned as a tuple, or [`Done`] if there is no data to read.
5094    ///
5095    /// Reading data from a stream may trigger queueing of control messages
5096    /// (e.g. MAX_STREAM_DATA). [`send()`] should be called after reading.
5097    ///
5098    /// [`Done`]: enum.Error.html#variant.Done
5099    /// [`send()`]: struct.Connection.html#method.send
5100    ///
5101    /// ## Examples:
5102    ///
5103    /// ```no_run
5104    /// # let mut buf = [0; 512];
5105    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5106    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5107    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5108    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5109    /// # let local = socket.local_addr().unwrap();
5110    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5111    /// # let stream_id = 0;
5112    /// while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
5113    ///     println!("Got {} bytes on stream {}", read, stream_id);
5114    /// }
5115    /// # Ok::<(), quiche::Error>(())
5116    /// ```
5117    pub fn stream_recv(
5118        &mut self, stream_id: u64, out: &mut [u8],
5119    ) -> Result<(usize, bool)> {
5120        // We can't read on our own unidirectional streams.
5121        if !stream::is_bidi(stream_id) &&
5122            stream::is_local(stream_id, self.is_server)
5123        {
5124            return Err(Error::InvalidStreamState(stream_id));
5125        }
5126
5127        let stream = self
5128            .streams
5129            .get_mut(stream_id)
5130            .ok_or(Error::InvalidStreamState(stream_id))?;
5131
5132        if !stream.is_readable() {
5133            return Err(Error::Done);
5134        }
5135
5136        let local = stream.local;
5137        let priority_key = Arc::clone(&stream.priority_key);
5138
5139        #[cfg(feature = "qlog")]
5140        let offset = stream.recv.off_front();
5141
5142        let (read, fin) = match stream.recv.emit(out) {
5143            Ok(v) => v,
5144
5145            Err(e) => {
5146                // Collect the stream if it is now complete. This can happen if
5147                // we got a `StreamReset` error which will now be propagated to
5148                // the application, so we don't need to keep the stream's state
5149                // anymore.
5150                if stream.is_complete() {
5151                    self.streams.collect(stream_id, local);
5152                }
5153
5154                self.streams.remove_readable(&priority_key);
5155                return Err(e);
5156            },
5157        };
5158
5159        self.flow_control.add_consumed(read as u64);
5160
5161        let readable = stream.is_readable();
5162
5163        let complete = stream.is_complete();
5164
5165        if stream.recv.almost_full() {
5166            self.streams.insert_almost_full(stream_id);
5167        }
5168
5169        if !readable {
5170            self.streams.remove_readable(&priority_key);
5171        }
5172
5173        if complete {
5174            self.streams.collect(stream_id, local);
5175        }
5176
5177        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
5178            let ev_data = EventData::DataMoved(qlog::events::quic::DataMoved {
5179                stream_id: Some(stream_id),
5180                offset: Some(offset),
5181                length: Some(read as u64),
5182                from: Some(DataRecipient::Transport),
5183                to: Some(DataRecipient::Application),
5184                ..Default::default()
5185            });
5186
5187            let now = time::Instant::now();
5188            q.add_event_data_with_instant(ev_data, now).ok();
5189        });
5190
5191        if self.should_update_max_data() {
5192            self.almost_full = true;
5193        }
5194
5195        if priority_key.incremental && readable {
5196            // Shuffle the incremental stream to the back of the queue.
5197            self.streams.remove_readable(&priority_key);
5198            self.streams.insert_readable(&priority_key);
5199        }
5200
5201        Ok((read, fin))
5202    }
5203
5204    /// Writes data to a stream.
5205    ///
5206    /// On success the number of bytes written is returned, or [`Done`] if no
5207    /// data was written (e.g. because the stream has no capacity).
5208    ///
5209    /// Applications can provide a 0-length buffer with the fin flag set to
5210    /// true. This will lead to a 0-length FIN STREAM frame being sent at the
5211    /// latest offset. The `Ok(0)` value is only returned when the application
5212    /// provided a 0-length buffer.
5213    ///
5214    /// In addition, if the peer has signalled that it doesn't want to receive
5215    /// any more data from this stream by sending the `STOP_SENDING` frame, the
5216    /// [`StreamStopped`] error will be returned instead of any data.
5217    ///
5218    /// Note that in order to avoid buffering an infinite amount of data in the
5219    /// stream's send buffer, streams are only allowed to buffer outgoing data
5220    /// up to the amount that the peer allows it to send (that is, up to the
5221    /// stream's outgoing flow control capacity).
5222    ///
5223    /// This means that the number of written bytes returned can be lower than
5224    /// the length of the input buffer when the stream doesn't have enough
5225    /// capacity for the operation to complete. The application should retry the
5226    /// operation once the stream is reported as writable again.
5227    ///
5228    /// Applications should call this method only after the handshake is
5229    /// completed (whenever [`is_established()`] returns `true`) or during
5230    /// early data if enabled (whenever [`is_in_early_data()`] returns `true`).
5231    ///
5232    /// [`Done`]: enum.Error.html#variant.Done
5233    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
5234    /// [`is_established()`]: struct.Connection.html#method.is_established
5235    /// [`is_in_early_data()`]: struct.Connection.html#method.is_in_early_data
5236    ///
5237    /// ## Examples:
5238    ///
5239    /// ```no_run
5240    /// # let mut buf = [0; 512];
5241    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5242    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5243    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5244    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5245    /// # let local = "127.0.0.1:4321".parse().unwrap();
5246    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5247    /// # let stream_id = 0;
5248    /// conn.stream_send(stream_id, b"hello", true)?;
5249    /// # Ok::<(), quiche::Error>(())
5250    /// ```
5251    pub fn stream_send(
5252        &mut self, stream_id: u64, buf: &[u8], fin: bool,
5253    ) -> Result<usize> {
5254        self.stream_do_send(
5255            stream_id,
5256            buf,
5257            fin,
5258            |stream: &mut stream::Stream<F>,
5259             buf: &[u8],
5260             cap: usize,
5261             fin: bool| {
5262                stream.send.write(&buf[..cap], fin).map(|v| (v, v))
5263            },
5264        )
5265    }
5266
5267    /// Writes data to a stream with zero copying, instead, it appends the
5268    /// provided buffer directly to the send queue if the capacity allows
5269    /// it.
5270    ///
5271    /// When a partial write happens (including when [`Done`] is returned) the
5272    /// remaining (unwrittent) buffer will also be returned. The application
5273    /// should retry the operation once the stream is reported as writable
5274    /// again.
5275    pub fn stream_send_zc(
5276        &mut self, stream_id: u64, buf: F::Buf, len: Option<usize>, fin: bool,
5277    ) -> Result<(usize, Option<F::Buf>)>
5278    where
5279        F::Buf: BufSplit,
5280    {
5281        self.stream_do_send(
5282            stream_id,
5283            buf,
5284            fin,
5285            |stream: &mut stream::Stream<F>,
5286             buf: F::Buf,
5287             cap: usize,
5288             fin: bool| {
5289                let len = len.unwrap_or(usize::MAX).min(cap);
5290                let (sent, remaining) = stream.send.append_buf(buf, len, fin)?;
5291                Ok((sent, (sent, remaining)))
5292            },
5293        )
5294    }
5295
5296    fn stream_do_send<B, R, SND>(
5297        &mut self, stream_id: u64, buf: B, fin: bool, write_fn: SND,
5298    ) -> Result<R>
5299    where
5300        B: AsRef<[u8]>,
5301        SND: FnOnce(&mut stream::Stream<F>, B, usize, bool) -> Result<(usize, R)>,
5302    {
5303        // We can't write on the peer's unidirectional streams.
5304        if !stream::is_bidi(stream_id) &&
5305            !stream::is_local(stream_id, self.is_server)
5306        {
5307            return Err(Error::InvalidStreamState(stream_id));
5308        }
5309
5310        let len = buf.as_ref().len();
5311
5312        // Mark the connection as blocked if the connection-level flow control
5313        // limit doesn't let us buffer all the data.
5314        //
5315        // Note that this is separate from "send capacity" as that also takes
5316        // congestion control into consideration.
5317        if self.max_tx_data - self.tx_data < len as u64 {
5318            self.blocked_limit = Some(self.max_tx_data);
5319        }
5320
5321        let cap = self.tx_cap;
5322
5323        // Get existing stream or create a new one.
5324        let stream = self.get_or_create_stream(stream_id, true)?;
5325
5326        #[cfg(feature = "qlog")]
5327        let offset = stream.send.off_back();
5328
5329        let was_writable = stream.is_writable();
5330
5331        let was_flushable = stream.is_flushable();
5332
5333        let priority_key = Arc::clone(&stream.priority_key);
5334
5335        // Truncate the input buffer based on the connection's send capacity if
5336        // necessary.
5337        //
5338        // When the cap is zero, the method returns Ok(0) *only* when the passed
5339        // buffer is empty. We return Error::Done otherwise.
5340        if cap == 0 && len > 0 {
5341            if was_writable {
5342                // When `stream_writable_next()` returns a stream, the writable
5343                // mark is removed, but because the stream is blocked by the
5344                // connection-level send capacity it won't be marked as writable
5345                // again once the capacity increases.
5346                //
5347                // Since the stream is writable already, mark it here instead.
5348                self.streams.insert_writable(&priority_key);
5349            }
5350
5351            return Err(Error::Done);
5352        }
5353
5354        let (cap, fin, blocked_by_cap) = if cap < len {
5355            (cap, false, true)
5356        } else {
5357            (len, fin, false)
5358        };
5359
5360        let (sent, ret) = match write_fn(stream, buf, cap, fin) {
5361            Ok(v) => v,
5362
5363            Err(e) => {
5364                self.streams.remove_writable(&priority_key);
5365                return Err(e);
5366            },
5367        };
5368
5369        let incremental = stream.incremental;
5370        let priority_key = Arc::clone(&stream.priority_key);
5371
5372        let flushable = stream.is_flushable();
5373
5374        let writable = stream.is_writable();
5375
5376        let empty_fin = len == 0 && fin;
5377
5378        if sent < cap {
5379            let max_off = stream.send.max_off();
5380
5381            if stream.send.blocked_at() != Some(max_off) {
5382                stream.send.update_blocked_at(Some(max_off));
5383                self.streams.insert_blocked(stream_id, max_off);
5384            }
5385        } else {
5386            stream.send.update_blocked_at(None);
5387            self.streams.remove_blocked(stream_id);
5388        }
5389
5390        // If the stream is now flushable push it to the flushable queue, but
5391        // only if it wasn't already queued.
5392        //
5393        // Consider the stream flushable also when we are sending a zero-length
5394        // frame that has the fin flag set.
5395        if (flushable || empty_fin) && !was_flushable {
5396            self.streams.insert_flushable(&priority_key);
5397        }
5398
5399        if !writable {
5400            self.streams.remove_writable(&priority_key);
5401        } else if was_writable && blocked_by_cap {
5402            // When `stream_writable_next()` returns a stream, the writable
5403            // mark is removed, but because the stream is blocked by the
5404            // connection-level send capacity it won't be marked as writable
5405            // again once the capacity increases.
5406            //
5407            // Since the stream is writable already, mark it here instead.
5408            self.streams.insert_writable(&priority_key);
5409        }
5410
5411        self.tx_cap -= sent;
5412
5413        self.tx_data += sent as u64;
5414
5415        self.tx_buffered += sent;
5416
5417        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
5418            let ev_data = EventData::DataMoved(qlog::events::quic::DataMoved {
5419                stream_id: Some(stream_id),
5420                offset: Some(offset),
5421                length: Some(sent as u64),
5422                from: Some(DataRecipient::Application),
5423                to: Some(DataRecipient::Transport),
5424                ..Default::default()
5425            });
5426
5427            let now = time::Instant::now();
5428            q.add_event_data_with_instant(ev_data, now).ok();
5429        });
5430
5431        if sent == 0 && cap > 0 {
5432            return Err(Error::Done);
5433        }
5434
5435        if incremental && writable {
5436            // Shuffle the incremental stream to the back of the queue.
5437            self.streams.remove_writable(&priority_key);
5438            self.streams.insert_writable(&priority_key);
5439        }
5440
5441        Ok(ret)
5442    }
5443
5444    /// Sets the priority for a stream.
5445    ///
5446    /// A stream's priority determines the order in which stream data is sent
5447    /// on the wire (streams with lower priority are sent first). Streams are
5448    /// created with a default priority of `127`.
5449    ///
5450    /// The target stream is created if it did not exist before calling this
5451    /// method.
5452    pub fn stream_priority(
5453        &mut self, stream_id: u64, urgency: u8, incremental: bool,
5454    ) -> Result<()> {
5455        // Get existing stream or create a new one, but if the stream
5456        // has already been closed and collected, ignore the prioritization.
5457        let stream = match self.get_or_create_stream(stream_id, true) {
5458            Ok(v) => v,
5459
5460            Err(Error::Done) => return Ok(()),
5461
5462            Err(e) => return Err(e),
5463        };
5464
5465        if stream.urgency == urgency && stream.incremental == incremental {
5466            return Ok(());
5467        }
5468
5469        stream.urgency = urgency;
5470        stream.incremental = incremental;
5471
5472        let new_priority_key = Arc::new(StreamPriorityKey {
5473            urgency: stream.urgency,
5474            incremental: stream.incremental,
5475            id: stream_id,
5476            ..Default::default()
5477        });
5478
5479        let old_priority_key =
5480            std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
5481
5482        self.streams
5483            .update_priority(&old_priority_key, &new_priority_key);
5484
5485        Ok(())
5486    }
5487
5488    /// Shuts down reading or writing from/to the specified stream.
5489    ///
5490    /// When the `direction` argument is set to [`Shutdown::Read`], outstanding
5491    /// data in the stream's receive buffer is dropped, and no additional data
5492    /// is added to it. Data received after calling this method is still
5493    /// validated and acked but not stored, and [`stream_recv()`] will not
5494    /// return it to the application. In addition, a `STOP_SENDING` frame will
5495    /// be sent to the peer to signal it to stop sending data.
5496    ///
5497    /// When the `direction` argument is set to [`Shutdown::Write`], outstanding
5498    /// data in the stream's send buffer is dropped, and no additional data is
5499    /// added to it. Data passed to [`stream_send()`] after calling this method
5500    /// will be ignored. In addition, a `RESET_STREAM` frame will be sent to the
5501    /// peer to signal the reset.
5502    ///
5503    /// Locally-initiated unidirectional streams can only be closed in the
5504    /// [`Shutdown::Write`] direction. Remotely-initiated unidirectional streams
5505    /// can only be closed in the [`Shutdown::Read`] direction. Using an
5506    /// incorrect direction will return [`InvalidStreamState`].
5507    ///
5508    /// [`Shutdown::Read`]: enum.Shutdown.html#variant.Read
5509    /// [`Shutdown::Write`]: enum.Shutdown.html#variant.Write
5510    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
5511    /// [`stream_send()`]: struct.Connection.html#method.stream_send
5512    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5513    pub fn stream_shutdown(
5514        &mut self, stream_id: u64, direction: Shutdown, err: u64,
5515    ) -> Result<()> {
5516        // Don't try to stop a local unidirectional stream.
5517        if direction == Shutdown::Read &&
5518            stream::is_local(stream_id, self.is_server) &&
5519            !stream::is_bidi(stream_id)
5520        {
5521            return Err(Error::InvalidStreamState(stream_id));
5522        }
5523
5524        // Don't try to reset a remote unidirectional stream.
5525        if direction == Shutdown::Write &&
5526            !stream::is_local(stream_id, self.is_server) &&
5527            !stream::is_bidi(stream_id)
5528        {
5529            return Err(Error::InvalidStreamState(stream_id));
5530        }
5531
5532        // Get existing stream.
5533        let stream = self.streams.get_mut(stream_id).ok_or(Error::Done)?;
5534
5535        let priority_key = Arc::clone(&stream.priority_key);
5536
5537        match direction {
5538            Shutdown::Read => {
5539                stream.recv.shutdown()?;
5540
5541                if !stream.recv.is_fin() {
5542                    self.streams.insert_stopped(stream_id, err);
5543                }
5544
5545                // Once shutdown, the stream is guaranteed to be non-readable.
5546                self.streams.remove_readable(&priority_key);
5547
5548                self.stopped_stream_local_count =
5549                    self.stopped_stream_local_count.saturating_add(1);
5550            },
5551
5552            Shutdown::Write => {
5553                let (final_size, unsent) = stream.send.shutdown()?;
5554
5555                // Claw back some flow control allowance from data that was
5556                // buffered but not actually sent before the stream was reset.
5557                self.tx_data = self.tx_data.saturating_sub(unsent);
5558
5559                self.tx_buffered =
5560                    self.tx_buffered.saturating_sub(unsent as usize);
5561
5562                // Update send capacity.
5563                self.update_tx_cap();
5564
5565                self.streams.insert_reset(stream_id, err, final_size);
5566
5567                // Once shutdown, the stream is guaranteed to be non-writable.
5568                self.streams.remove_writable(&priority_key);
5569
5570                self.reset_stream_local_count =
5571                    self.reset_stream_local_count.saturating_add(1);
5572            },
5573        }
5574
5575        Ok(())
5576    }
5577
5578    /// Returns the stream's send capacity in bytes.
5579    ///
5580    /// If the specified stream doesn't exist (including when it has already
5581    /// been completed and closed), the [`InvalidStreamState`] error will be
5582    /// returned.
5583    ///
5584    /// In addition, if the peer has signalled that it doesn't want to receive
5585    /// any more data from this stream by sending the `STOP_SENDING` frame, the
5586    /// [`StreamStopped`] error will be returned.
5587    ///
5588    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5589    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
5590    #[inline]
5591    pub fn stream_capacity(&self, stream_id: u64) -> Result<usize> {
5592        if let Some(stream) = self.streams.get(stream_id) {
5593            let cap = cmp::min(self.tx_cap, stream.send.cap()?);
5594            return Ok(cap);
5595        };
5596
5597        Err(Error::InvalidStreamState(stream_id))
5598    }
5599
5600    /// Returns the next stream that has data to read.
5601    ///
5602    /// Note that once returned by this method, a stream ID will not be returned
5603    /// again until it is "re-armed".
5604    ///
5605    /// The application will need to read all of the pending data on the stream,
5606    /// and new data has to be received before the stream is reported again.
5607    ///
5608    /// This is unlike the [`readable()`] method, that returns the same list of
5609    /// readable streams when called multiple times in succession.
5610    ///
5611    /// [`readable()`]: struct.Connection.html#method.readable
5612    pub fn stream_readable_next(&mut self) -> Option<u64> {
5613        let priority_key = self.streams.readable.front().clone_pointer()?;
5614
5615        self.streams.remove_readable(&priority_key);
5616
5617        Some(priority_key.id)
5618    }
5619
5620    /// Returns true if the stream has data that can be read.
5621    pub fn stream_readable(&self, stream_id: u64) -> bool {
5622        let stream = match self.streams.get(stream_id) {
5623            Some(v) => v,
5624
5625            None => return false,
5626        };
5627
5628        stream.is_readable()
5629    }
5630
5631    /// Returns the next stream that can be written to.
5632    ///
5633    /// Note that once returned by this method, a stream ID will not be returned
5634    /// again until it is "re-armed".
5635    ///
5636    /// This is unlike the [`writable()`] method, that returns the same list of
5637    /// writable streams when called multiple times in succession. It is not
5638    /// advised to use both `stream_writable_next()` and [`writable()`] on the
5639    /// same connection, as it may lead to unexpected results.
5640    ///
5641    /// The [`stream_writable()`] method can also be used to fine-tune when a
5642    /// stream is reported as writable again.
5643    ///
5644    /// [`stream_writable()`]: struct.Connection.html#method.stream_writable
5645    /// [`writable()`]: struct.Connection.html#method.writable
5646    pub fn stream_writable_next(&mut self) -> Option<u64> {
5647        // If there is not enough connection-level send capacity, none of the
5648        // streams are writable.
5649        if self.tx_cap == 0 {
5650            return None;
5651        }
5652
5653        let mut cursor = self.streams.writable.front();
5654
5655        while let Some(priority_key) = cursor.clone_pointer() {
5656            if let Some(stream) = self.streams.get(priority_key.id) {
5657                let cap = match stream.send.cap() {
5658                    Ok(v) => v,
5659
5660                    // Return the stream to the application immediately if it's
5661                    // stopped.
5662                    Err(_) =>
5663                        return {
5664                            self.streams.remove_writable(&priority_key);
5665
5666                            Some(priority_key.id)
5667                        },
5668                };
5669
5670                if cmp::min(self.tx_cap, cap) >= stream.send_lowat {
5671                    self.streams.remove_writable(&priority_key);
5672                    return Some(priority_key.id);
5673                }
5674            }
5675
5676            cursor.move_next();
5677        }
5678
5679        None
5680    }
5681
5682    /// Returns true if the stream has enough send capacity.
5683    ///
5684    /// When `len` more bytes can be buffered into the given stream's send
5685    /// buffer, `true` will be returned, `false` otherwise.
5686    ///
5687    /// In the latter case, if the additional data can't be buffered due to
5688    /// flow control limits, the peer will also be notified, and a "low send
5689    /// watermark" will be set for the stream, such that it is not going to be
5690    /// reported as writable again by [`stream_writable_next()`] until its send
5691    /// capacity reaches `len`.
5692    ///
5693    /// If the specified stream doesn't exist (including when it has already
5694    /// been completed and closed), the [`InvalidStreamState`] error will be
5695    /// returned.
5696    ///
5697    /// In addition, if the peer has signalled that it doesn't want to receive
5698    /// any more data from this stream by sending the `STOP_SENDING` frame, the
5699    /// [`StreamStopped`] error will be returned.
5700    ///
5701    /// [`stream_writable_next()`]: struct.Connection.html#method.stream_writable_next
5702    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5703    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
5704    #[inline]
5705    pub fn stream_writable(
5706        &mut self, stream_id: u64, len: usize,
5707    ) -> Result<bool> {
5708        if self.stream_capacity(stream_id)? >= len {
5709            return Ok(true);
5710        }
5711
5712        let stream = match self.streams.get_mut(stream_id) {
5713            Some(v) => v,
5714
5715            None => return Err(Error::InvalidStreamState(stream_id)),
5716        };
5717
5718        stream.send_lowat = cmp::max(1, len);
5719
5720        let is_writable = stream.is_writable();
5721
5722        let priority_key = Arc::clone(&stream.priority_key);
5723
5724        if self.max_tx_data - self.tx_data < len as u64 {
5725            self.blocked_limit = Some(self.max_tx_data);
5726        }
5727
5728        if stream.send.cap()? < len {
5729            let max_off = stream.send.max_off();
5730            if stream.send.blocked_at() != Some(max_off) {
5731                stream.send.update_blocked_at(Some(max_off));
5732                self.streams.insert_blocked(stream_id, max_off);
5733            }
5734        } else if is_writable {
5735            // When `stream_writable_next()` returns a stream, the writable
5736            // mark is removed, but because the stream is blocked by the
5737            // connection-level send capacity it won't be marked as writable
5738            // again once the capacity increases.
5739            //
5740            // Since the stream is writable already, mark it here instead.
5741            self.streams.insert_writable(&priority_key);
5742        }
5743
5744        Ok(false)
5745    }
5746
5747    /// Returns true if all the data has been read from the specified stream.
5748    ///
5749    /// This instructs the application that all the data received from the
5750    /// peer on the stream has been read, and there won't be anymore in the
5751    /// future.
5752    ///
5753    /// Basically this returns true when the peer either set the `fin` flag
5754    /// for the stream, or sent `RESET_STREAM`.
5755    #[inline]
5756    pub fn stream_finished(&self, stream_id: u64) -> bool {
5757        let stream = match self.streams.get(stream_id) {
5758            Some(v) => v,
5759
5760            None => return true,
5761        };
5762
5763        stream.recv.is_fin()
5764    }
5765
5766    /// Returns the number of bidirectional streams that can be created
5767    /// before the peer's stream count limit is reached.
5768    ///
5769    /// This can be useful to know if it's possible to create a bidirectional
5770    /// stream without trying it first.
5771    #[inline]
5772    pub fn peer_streams_left_bidi(&self) -> u64 {
5773        self.streams.peer_streams_left_bidi()
5774    }
5775
5776    /// Returns the number of unidirectional streams that can be created
5777    /// before the peer's stream count limit is reached.
5778    ///
5779    /// This can be useful to know if it's possible to create a unidirectional
5780    /// stream without trying it first.
5781    #[inline]
5782    pub fn peer_streams_left_uni(&self) -> u64 {
5783        self.streams.peer_streams_left_uni()
5784    }
5785
5786    /// Returns an iterator over streams that have outstanding data to read.
5787    ///
5788    /// Note that the iterator will only include streams that were readable at
5789    /// the time the iterator itself was created (i.e. when `readable()` was
5790    /// called). To account for newly readable streams, the iterator needs to
5791    /// be created again.
5792    ///
5793    /// ## Examples:
5794    ///
5795    /// ```no_run
5796    /// # let mut buf = [0; 512];
5797    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5798    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5799    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5800    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5801    /// # let local = socket.local_addr().unwrap();
5802    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5803    /// // Iterate over readable streams.
5804    /// for stream_id in conn.readable() {
5805    ///     // Stream is readable, read until there's no more data.
5806    ///     while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
5807    ///         println!("Got {} bytes on stream {}", read, stream_id);
5808    ///     }
5809    /// }
5810    /// # Ok::<(), quiche::Error>(())
5811    /// ```
5812    #[inline]
5813    pub fn readable(&self) -> StreamIter {
5814        self.streams.readable()
5815    }
5816
5817    /// Returns an iterator over streams that can be written in priority order.
5818    ///
5819    /// The priority order is based on RFC 9218 scheduling recommendations.
5820    /// Stream priority can be controlled using [`stream_priority()`]. In order
5821    /// to support fairness requirements, each time this method is called,
5822    /// internal state is updated. Therefore the iterator ordering can change
5823    /// between calls, even if no streams were added or removed.
5824    ///
5825    /// A "writable" stream is a stream that has enough flow control capacity to
5826    /// send data to the peer. To avoid buffering an infinite amount of data,
5827    /// streams are only allowed to buffer outgoing data up to the amount that
5828    /// the peer allows to send.
5829    ///
5830    /// Note that the iterator will only include streams that were writable at
5831    /// the time the iterator itself was created (i.e. when `writable()` was
5832    /// called). To account for newly writable streams, the iterator needs to be
5833    /// created again.
5834    ///
5835    /// ## Examples:
5836    ///
5837    /// ```no_run
5838    /// # let mut buf = [0; 512];
5839    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5840    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5841    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5842    /// # let local = socket.local_addr().unwrap();
5843    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5844    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5845    /// // Iterate over writable streams.
5846    /// for stream_id in conn.writable() {
5847    ///     // Stream is writable, write some data.
5848    ///     if let Ok(written) = conn.stream_send(stream_id, &buf, false) {
5849    ///         println!("Written {} bytes on stream {}", written, stream_id);
5850    ///     }
5851    /// }
5852    /// # Ok::<(), quiche::Error>(())
5853    /// ```
5854    /// [`stream_priority()`]: struct.Connection.html#method.stream_priority
5855    #[inline]
5856    pub fn writable(&self) -> StreamIter {
5857        // If there is not enough connection-level send capacity, none of the
5858        // streams are writable, so return an empty iterator.
5859        if self.tx_cap == 0 {
5860            return StreamIter::default();
5861        }
5862
5863        self.streams.writable()
5864    }
5865
5866    /// Returns the maximum possible size of egress UDP payloads.
5867    ///
5868    /// This is the maximum size of UDP payloads that can be sent, and depends
5869    /// on both the configured maximum send payload size of the local endpoint
5870    /// (as configured with [`set_max_send_udp_payload_size()`]), as well as
5871    /// the transport parameter advertised by the remote peer.
5872    ///
5873    /// Note that this value can change during the lifetime of the connection,
5874    /// but should remain stable across consecutive calls to [`send()`].
5875    ///
5876    /// [`set_max_send_udp_payload_size()`]:
5877    ///     struct.Config.html#method.set_max_send_udp_payload_size
5878    /// [`send()`]: struct.Connection.html#method.send
5879    pub fn max_send_udp_payload_size(&self) -> usize {
5880        let max_datagram_size = self
5881            .paths
5882            .get_active()
5883            .ok()
5884            .map(|p| p.recovery.max_datagram_size());
5885
5886        if let Some(max_datagram_size) = max_datagram_size {
5887            if self.is_established() {
5888                // We cap the maximum packet size to 16KB or so, so that it can be
5889                // always encoded with a 2-byte varint.
5890                return cmp::min(16383, max_datagram_size);
5891            }
5892        }
5893
5894        // Allow for 1200 bytes (minimum QUIC packet size) during the
5895        // handshake.
5896        MIN_CLIENT_INITIAL_LEN
5897    }
5898
5899    /// Schedule an ack-eliciting packet on the active path.
5900    ///
5901    /// QUIC packets might not contain ack-eliciting frames during normal
5902    /// operating conditions. If the packet would already contain
5903    /// ack-eliciting frames, this method does not change any behavior.
5904    /// However, if the packet would not ordinarily contain ack-eliciting
5905    /// frames, this method ensures that a PING frame sent.
5906    ///
5907    /// Calling this method multiple times before [`send()`] has no effect.
5908    ///
5909    /// [`send()`]: struct.Connection.html#method.send
5910    pub fn send_ack_eliciting(&mut self) -> Result<()> {
5911        if self.is_closed() || self.is_draining() {
5912            return Ok(());
5913        }
5914        self.paths.get_active_mut()?.needs_ack_eliciting = true;
5915        Ok(())
5916    }
5917
5918    /// Schedule an ack-eliciting packet on the specified path.
5919    ///
5920    /// See [`send_ack_eliciting()`] for more detail. [`InvalidState`] is
5921    /// returned if there is no record of the path.
5922    ///
5923    /// [`send_ack_eliciting()`]: struct.Connection.html#method.send_ack_eliciting
5924    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
5925    pub fn send_ack_eliciting_on_path(
5926        &mut self, local: SocketAddr, peer: SocketAddr,
5927    ) -> Result<()> {
5928        if self.is_closed() || self.is_draining() {
5929            return Ok(());
5930        }
5931        let path_id = self
5932            .paths
5933            .path_id_from_addrs(&(local, peer))
5934            .ok_or(Error::InvalidState)?;
5935        self.paths.get_mut(path_id)?.needs_ack_eliciting = true;
5936        Ok(())
5937    }
5938
5939    /// Reads the first received DATAGRAM.
5940    ///
5941    /// On success the DATAGRAM's data is returned along with its size.
5942    ///
5943    /// [`Done`] is returned if there is no data to read.
5944    ///
5945    /// [`BufferTooShort`] is returned if the provided buffer is too small for
5946    /// the DATAGRAM.
5947    ///
5948    /// [`Done`]: enum.Error.html#variant.Done
5949    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
5950    ///
5951    /// ## Examples:
5952    ///
5953    /// ```no_run
5954    /// # let mut buf = [0; 512];
5955    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5956    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5957    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5958    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5959    /// # let local = socket.local_addr().unwrap();
5960    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5961    /// let mut dgram_buf = [0; 512];
5962    /// while let Ok((len)) = conn.dgram_recv(&mut dgram_buf) {
5963    ///     println!("Got {} bytes of DATAGRAM", len);
5964    /// }
5965    /// # Ok::<(), quiche::Error>(())
5966    /// ```
5967    #[inline]
5968    pub fn dgram_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
5969        match self.dgram_recv_queue.pop() {
5970            Some(d) => {
5971                if d.len() > buf.len() {
5972                    return Err(Error::BufferTooShort);
5973                }
5974
5975                buf[..d.len()].copy_from_slice(&d);
5976                Ok(d.len())
5977            },
5978
5979            None => Err(Error::Done),
5980        }
5981    }
5982
5983    /// Reads the first received DATAGRAM.
5984    ///
5985    /// This is the same as [`dgram_recv()`] but returns the DATAGRAM as a
5986    /// `Vec<u8>` instead of copying into the provided buffer.
5987    ///
5988    /// [`dgram_recv()`]: struct.Connection.html#method.dgram_recv
5989    #[inline]
5990    pub fn dgram_recv_vec(&mut self) -> Result<Vec<u8>> {
5991        match self.dgram_recv_queue.pop() {
5992            Some(d) => Ok(d),
5993
5994            None => Err(Error::Done),
5995        }
5996    }
5997
5998    /// Reads the first received DATAGRAM without removing it from the queue.
5999    ///
6000    /// On success the DATAGRAM's data is returned along with the actual number
6001    /// of bytes peeked. The requested length cannot exceed the DATAGRAM's
6002    /// actual length.
6003    ///
6004    /// [`Done`] is returned if there is no data to read.
6005    ///
6006    /// [`BufferTooShort`] is returned if the provided buffer is smaller the
6007    /// number of bytes to peek.
6008    ///
6009    /// [`Done`]: enum.Error.html#variant.Done
6010    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
6011    #[inline]
6012    pub fn dgram_recv_peek(&self, buf: &mut [u8], len: usize) -> Result<usize> {
6013        self.dgram_recv_queue.peek_front_bytes(buf, len)
6014    }
6015
6016    /// Returns the length of the first stored DATAGRAM.
6017    #[inline]
6018    pub fn dgram_recv_front_len(&self) -> Option<usize> {
6019        self.dgram_recv_queue.peek_front_len()
6020    }
6021
6022    /// Returns the number of items in the DATAGRAM receive queue.
6023    #[inline]
6024    pub fn dgram_recv_queue_len(&self) -> usize {
6025        self.dgram_recv_queue.len()
6026    }
6027
6028    /// Returns the total size of all items in the DATAGRAM receive queue.
6029    #[inline]
6030    pub fn dgram_recv_queue_byte_size(&self) -> usize {
6031        self.dgram_recv_queue.byte_size()
6032    }
6033
6034    /// Returns the number of items in the DATAGRAM send queue.
6035    #[inline]
6036    pub fn dgram_send_queue_len(&self) -> usize {
6037        self.dgram_send_queue.len()
6038    }
6039
6040    /// Returns the total size of all items in the DATAGRAM send queue.
6041    #[inline]
6042    pub fn dgram_send_queue_byte_size(&self) -> usize {
6043        self.dgram_send_queue.byte_size()
6044    }
6045
6046    /// Returns whether or not the DATAGRAM send queue is full.
6047    #[inline]
6048    pub fn is_dgram_send_queue_full(&self) -> bool {
6049        self.dgram_send_queue.is_full()
6050    }
6051
6052    /// Returns whether or not the DATAGRAM recv queue is full.
6053    #[inline]
6054    pub fn is_dgram_recv_queue_full(&self) -> bool {
6055        self.dgram_recv_queue.is_full()
6056    }
6057
6058    /// Sends data in a DATAGRAM frame.
6059    ///
6060    /// [`Done`] is returned if no data was written.
6061    /// [`InvalidState`] is returned if the peer does not support DATAGRAM.
6062    /// [`BufferTooShort`] is returned if the DATAGRAM frame length is larger
6063    /// than peer's supported DATAGRAM frame length. Use
6064    /// [`dgram_max_writable_len()`] to get the largest supported DATAGRAM
6065    /// frame length.
6066    ///
6067    /// Note that there is no flow control of DATAGRAM frames, so in order to
6068    /// avoid buffering an infinite amount of frames we apply an internal
6069    /// limit.
6070    ///
6071    /// [`Done`]: enum.Error.html#variant.Done
6072    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
6073    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
6074    /// [`dgram_max_writable_len()`]:
6075    /// struct.Connection.html#method.dgram_max_writable_len
6076    ///
6077    /// ## Examples:
6078    ///
6079    /// ```no_run
6080    /// # let mut buf = [0; 512];
6081    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6082    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6083    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6084    /// # let peer = "127.0.0.1:1234".parse().unwrap();
6085    /// # let local = socket.local_addr().unwrap();
6086    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6087    /// conn.dgram_send(b"hello")?;
6088    /// # Ok::<(), quiche::Error>(())
6089    /// ```
6090    pub fn dgram_send(&mut self, buf: &[u8]) -> Result<()> {
6091        let max_payload_len = match self.dgram_max_writable_len() {
6092            Some(v) => v,
6093
6094            None => return Err(Error::InvalidState),
6095        };
6096
6097        if buf.len() > max_payload_len {
6098            return Err(Error::BufferTooShort);
6099        }
6100
6101        self.dgram_send_queue.push(buf.to_vec())?;
6102
6103        let active_path = self.paths.get_active_mut()?;
6104
6105        if self.dgram_send_queue.byte_size() >
6106            active_path.recovery.cwnd_available()
6107        {
6108            active_path.recovery.update_app_limited(false);
6109        }
6110
6111        Ok(())
6112    }
6113
6114    /// Sends data in a DATAGRAM frame.
6115    ///
6116    /// This is the same as [`dgram_send()`] but takes a `Vec<u8>` instead of
6117    /// a slice.
6118    ///
6119    /// [`dgram_send()`]: struct.Connection.html#method.dgram_send
6120    pub fn dgram_send_vec(&mut self, buf: Vec<u8>) -> Result<()> {
6121        let max_payload_len = match self.dgram_max_writable_len() {
6122            Some(v) => v,
6123
6124            None => return Err(Error::InvalidState),
6125        };
6126
6127        if buf.len() > max_payload_len {
6128            return Err(Error::BufferTooShort);
6129        }
6130
6131        self.dgram_send_queue.push(buf)?;
6132
6133        let active_path = self.paths.get_active_mut()?;
6134
6135        if self.dgram_send_queue.byte_size() >
6136            active_path.recovery.cwnd_available()
6137        {
6138            active_path.recovery.update_app_limited(false);
6139        }
6140
6141        Ok(())
6142    }
6143
6144    /// Purges queued outgoing DATAGRAMs matching the predicate.
6145    ///
6146    /// In other words, remove all elements `e` such that `f(&e)` returns true.
6147    ///
6148    /// ## Examples:
6149    /// ```no_run
6150    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6151    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6152    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6153    /// # let peer = "127.0.0.1:1234".parse().unwrap();
6154    /// # let local = socket.local_addr().unwrap();
6155    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6156    /// conn.dgram_send(b"hello")?;
6157    /// conn.dgram_purge_outgoing(&|d: &[u8]| -> bool { d[0] == 0 });
6158    /// # Ok::<(), quiche::Error>(())
6159    /// ```
6160    #[inline]
6161    pub fn dgram_purge_outgoing<FN: Fn(&[u8]) -> bool>(&mut self, f: FN) {
6162        self.dgram_send_queue.purge(f);
6163    }
6164
6165    /// Returns the maximum DATAGRAM payload that can be sent.
6166    ///
6167    /// [`None`] is returned if the peer hasn't advertised a maximum DATAGRAM
6168    /// frame size.
6169    ///
6170    /// ## Examples:
6171    ///
6172    /// ```no_run
6173    /// # let mut buf = [0; 512];
6174    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6175    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6176    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6177    /// # let peer = "127.0.0.1:1234".parse().unwrap();
6178    /// # let local = socket.local_addr().unwrap();
6179    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6180    /// if let Some(payload_size) = conn.dgram_max_writable_len() {
6181    ///     if payload_size > 5 {
6182    ///         conn.dgram_send(b"hello")?;
6183    ///     }
6184    /// }
6185    /// # Ok::<(), quiche::Error>(())
6186    /// ```
6187    #[inline]
6188    pub fn dgram_max_writable_len(&self) -> Option<usize> {
6189        match self.peer_transport_params.max_datagram_frame_size {
6190            None => None,
6191            Some(peer_frame_len) => {
6192                let dcid = self.destination_id();
6193                // Start from the maximum packet size...
6194                let mut max_len = self.max_send_udp_payload_size();
6195                // ...subtract the Short packet header overhead...
6196                // (1 byte of pkt_len + len of dcid)
6197                max_len = max_len.saturating_sub(1 + dcid.len());
6198                // ...subtract the packet number (max len)...
6199                max_len = max_len.saturating_sub(packet::MAX_PKT_NUM_LEN);
6200                // ...subtract the crypto overhead...
6201                max_len = max_len.saturating_sub(
6202                    self.crypto_ctx[packet::Epoch::Application]
6203                        .crypto_overhead()?,
6204                );
6205                // ...clamp to what peer can support...
6206                max_len = cmp::min(peer_frame_len as usize, max_len);
6207                // ...subtract frame overhead, checked for underflow.
6208                // (1 byte of frame type + len of length )
6209                max_len.checked_sub(1 + frame::MAX_DGRAM_OVERHEAD)
6210            },
6211        }
6212    }
6213
6214    fn dgram_enabled(&self) -> bool {
6215        self.local_transport_params
6216            .max_datagram_frame_size
6217            .is_some()
6218    }
6219
6220    /// Returns when the next timeout event will occur.
6221    ///
6222    /// Once the timeout Instant has been reached, the [`on_timeout()`] method
6223    /// should be called. A timeout of `None` means that the timer should be
6224    /// disarmed.
6225    ///
6226    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6227    pub fn timeout_instant(&self) -> Option<time::Instant> {
6228        if self.is_closed() {
6229            return None;
6230        }
6231
6232        if self.is_draining() {
6233            // Draining timer takes precedence over all other timers. If it is
6234            // set it means the connection is closing so there's no point in
6235            // processing the other timers.
6236            self.draining_timer
6237        } else {
6238            // Use the lowest timer value (i.e. "sooner") among idle and loss
6239            // detection timers. If they are both unset (i.e. `None`) then the
6240            // result is `None`, but if at least one of them is set then a
6241            // `Some(...)` value is returned.
6242            let path_timer = self
6243                .paths
6244                .iter()
6245                .filter_map(|(_, p)| p.recovery.loss_detection_timer())
6246                .min();
6247
6248            let key_update_timer = self.crypto_ctx[packet::Epoch::Application]
6249                .key_update
6250                .as_ref()
6251                .map(|key_update| key_update.timer);
6252
6253            let timers = [self.idle_timer, path_timer, key_update_timer];
6254
6255            timers.iter().filter_map(|&x| x).min()
6256        }
6257    }
6258
6259    /// Returns the amount of time until the next timeout event.
6260    ///
6261    /// Once the given duration has elapsed, the [`on_timeout()`] method should
6262    /// be called. A timeout of `None` means that the timer should be disarmed.
6263    ///
6264    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6265    pub fn timeout(&self) -> Option<time::Duration> {
6266        self.timeout_instant().map(|timeout| {
6267            let now = time::Instant::now();
6268
6269            if timeout <= now {
6270                time::Duration::ZERO
6271            } else {
6272                timeout.duration_since(now)
6273            }
6274        })
6275    }
6276
6277    /// Processes a timeout event.
6278    ///
6279    /// If no timeout has occurred it does nothing.
6280    pub fn on_timeout(&mut self) {
6281        let now = time::Instant::now();
6282
6283        if let Some(draining_timer) = self.draining_timer {
6284            if draining_timer <= now {
6285                trace!("{} draining timeout expired", self.trace_id);
6286
6287                self.mark_closed();
6288            }
6289
6290            // Draining timer takes precedence over all other timers. If it is
6291            // set it means the connection is closing so there's no point in
6292            // processing the other timers.
6293            return;
6294        }
6295
6296        if let Some(timer) = self.idle_timer {
6297            if timer <= now {
6298                trace!("{} idle timeout expired", self.trace_id);
6299
6300                self.mark_closed();
6301                self.timed_out = true;
6302                return;
6303            }
6304        }
6305
6306        if let Some(timer) = self.crypto_ctx[packet::Epoch::Application]
6307            .key_update
6308            .as_ref()
6309            .map(|key_update| key_update.timer)
6310        {
6311            if timer <= now {
6312                // Discard previous key once key update timer expired.
6313                let _ = self.crypto_ctx[packet::Epoch::Application]
6314                    .key_update
6315                    .take();
6316            }
6317        }
6318
6319        let handshake_status = self.handshake_status();
6320
6321        for (_, p) in self.paths.iter_mut() {
6322            if let Some(timer) = p.recovery.loss_detection_timer() {
6323                if timer <= now {
6324                    trace!("{} loss detection timeout expired", self.trace_id);
6325
6326                    let (lost_packets, lost_bytes) = p.on_loss_detection_timeout(
6327                        handshake_status,
6328                        now,
6329                        self.is_server,
6330                        &self.trace_id,
6331                    );
6332
6333                    self.lost_count += lost_packets;
6334                    self.lost_bytes += lost_bytes as u64;
6335
6336                    qlog_with_type!(QLOG_METRICS, self.qlog, q, {
6337                        if let Some(ev_data) = p.recovery.maybe_qlog() {
6338                            q.add_event_data_with_instant(ev_data, now).ok();
6339                        }
6340                    });
6341                }
6342            }
6343        }
6344
6345        // Notify timeout events to the application.
6346        self.paths.notify_failed_validations();
6347
6348        // If the active path failed, try to find a new candidate.
6349        if self.paths.get_active_path_id().is_err() {
6350            match self.paths.find_candidate_path() {
6351                Some(pid) => {
6352                    if self.set_active_path(pid, now).is_err() {
6353                        // The connection cannot continue.
6354                        self.mark_closed();
6355                    }
6356                },
6357
6358                // The connection cannot continue.
6359                None => {
6360                    self.mark_closed();
6361                },
6362            }
6363        }
6364    }
6365
6366    /// Requests the stack to perform path validation of the proposed 4-tuple.
6367    ///
6368    /// Probing new paths requires spare Connection IDs at both the host and the
6369    /// peer sides. If it is not the case, it raises an [`OutOfIdentifiers`].
6370    ///
6371    /// The probing of new addresses can only be done by the client. The server
6372    /// can only probe network paths that were previously advertised by
6373    /// [`PathEvent::New`]. If the server tries to probe such an unseen network
6374    /// path, this call raises an [`InvalidState`].
6375    ///
6376    /// The caller might also want to probe an existing path. In such case, it
6377    /// triggers a PATH_CHALLENGE frame, but it does not require spare CIDs.
6378    ///
6379    /// A server always probes a new path it observes. Calling this method is
6380    /// hence not required to validate a new path. However, a server can still
6381    /// request an additional path validation of the proposed 4-tuple.
6382    ///
6383    /// Calling this method several times before calling [`send()`] or
6384    /// [`send_on_path()`] results in a single probe being generated. An
6385    /// application wanting to send multiple in-flight probes must call this
6386    /// method again after having sent packets.
6387    ///
6388    /// Returns the Destination Connection ID sequence number associated to that
6389    /// path.
6390    ///
6391    /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
6392    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6393    /// [`InvalidState`]: enum.Error.html#InvalidState
6394    /// [`send()`]: struct.Connection.html#method.send
6395    /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
6396    pub fn probe_path(
6397        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
6398    ) -> Result<u64> {
6399        // We may want to probe an existing path.
6400        let pid = match self.paths.path_id_from_addrs(&(local_addr, peer_addr)) {
6401            Some(pid) => pid,
6402            None => self.create_path_on_client(local_addr, peer_addr)?,
6403        };
6404
6405        let path = self.paths.get_mut(pid)?;
6406        path.request_validation();
6407
6408        path.active_dcid_seq.ok_or(Error::InvalidState)
6409    }
6410
6411    /// Migrates the connection to a new local address `local_addr`.
6412    ///
6413    /// The behavior is similar to [`migrate()`], with the nuance that the
6414    /// connection only changes the local address, but not the peer one.
6415    ///
6416    /// See [`migrate()`] for the full specification of this method.
6417    ///
6418    /// [`migrate()`]: struct.Connection.html#method.migrate
6419    pub fn migrate_source(&mut self, local_addr: SocketAddr) -> Result<u64> {
6420        let peer_addr = self.paths.get_active()?.peer_addr();
6421        self.migrate(local_addr, peer_addr)
6422    }
6423
6424    /// Migrates the connection over the given network path between `local_addr`
6425    /// and `peer_addr`.
6426    ///
6427    /// Connection migration can only be initiated by the client. Calling this
6428    /// method as a server returns [`InvalidState`].
6429    ///
6430    /// To initiate voluntary migration, there should be enough Connection IDs
6431    /// at both sides. If this requirement is not satisfied, this call returns
6432    /// [`OutOfIdentifiers`].
6433    ///
6434    /// Returns the Destination Connection ID associated to that migrated path.
6435    ///
6436    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6437    /// [`InvalidState`]: enum.Error.html#InvalidState
6438    pub fn migrate(
6439        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
6440    ) -> Result<u64> {
6441        if self.is_server {
6442            return Err(Error::InvalidState);
6443        }
6444
6445        // If the path already exists, mark it as the active one.
6446        let (pid, dcid_seq) = if let Some(pid) =
6447            self.paths.path_id_from_addrs(&(local_addr, peer_addr))
6448        {
6449            let path = self.paths.get_mut(pid)?;
6450
6451            // If it is already active, do nothing.
6452            if path.active() {
6453                return path.active_dcid_seq.ok_or(Error::OutOfIdentifiers);
6454            }
6455
6456            // Ensures that a Source Connection ID has been dedicated to this
6457            // path, or a free one is available. This is only required if the
6458            // host uses non-zero length Source Connection IDs.
6459            if !self.ids.zero_length_scid() &&
6460                path.active_scid_seq.is_none() &&
6461                self.ids.available_scids() == 0
6462            {
6463                return Err(Error::OutOfIdentifiers);
6464            }
6465
6466            // Ensures that the migrated path has a Destination Connection ID.
6467            let dcid_seq = if let Some(dcid_seq) = path.active_dcid_seq {
6468                dcid_seq
6469            } else {
6470                let dcid_seq = self
6471                    .ids
6472                    .lowest_available_dcid_seq()
6473                    .ok_or(Error::OutOfIdentifiers)?;
6474
6475                self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
6476                path.active_dcid_seq = Some(dcid_seq);
6477
6478                dcid_seq
6479            };
6480
6481            (pid, dcid_seq)
6482        } else {
6483            let pid = self.create_path_on_client(local_addr, peer_addr)?;
6484
6485            let dcid_seq = self
6486                .paths
6487                .get(pid)?
6488                .active_dcid_seq
6489                .ok_or(Error::InvalidState)?;
6490
6491            (pid, dcid_seq)
6492        };
6493
6494        // Change the active path.
6495        self.set_active_path(pid, time::Instant::now())?;
6496
6497        Ok(dcid_seq)
6498    }
6499
6500    /// Provides additional source Connection IDs that the peer can use to reach
6501    /// this host.
6502    ///
6503    /// This triggers sending NEW_CONNECTION_ID frames if the provided Source
6504    /// Connection ID is not already present. In the case the caller tries to
6505    /// reuse a Connection ID with a different reset token, this raises an
6506    /// `InvalidState`.
6507    ///
6508    /// At any time, the peer cannot have more Destination Connection IDs than
6509    /// the maximum number of active Connection IDs it negotiated. In such case
6510    /// (i.e., when [`scids_left()`] returns 0), if the host agrees to
6511    /// request the removal of previous connection IDs, it sets the
6512    /// `retire_if_needed` parameter. Otherwise, an [`IdLimit`] is returned.
6513    ///
6514    /// Note that setting `retire_if_needed` does not prevent this function from
6515    /// returning an [`IdLimit`] in the case the caller wants to retire still
6516    /// unannounced Connection IDs.
6517    ///
6518    /// The caller is responsible for ensuring that the provided `scid` is not
6519    /// repeated several times over the connection. quiche ensures that as long
6520    /// as the provided Connection ID is still in use (i.e., not retired), it
6521    /// does not assign a different sequence number.
6522    ///
6523    /// Note that if the host uses zero-length Source Connection IDs, it cannot
6524    /// advertise Source Connection IDs and calling this method returns an
6525    /// [`InvalidState`].
6526    ///
6527    /// Returns the sequence number associated to the provided Connection ID.
6528    ///
6529    /// [`scids_left()`]: struct.Connection.html#method.scids_left
6530    /// [`IdLimit`]: enum.Error.html#IdLimit
6531    /// [`InvalidState`]: enum.Error.html#InvalidState
6532    pub fn new_scid(
6533        &mut self, scid: &ConnectionId, reset_token: u128, retire_if_needed: bool,
6534    ) -> Result<u64> {
6535        self.ids.new_scid(
6536            scid.to_vec().into(),
6537            Some(reset_token),
6538            true,
6539            None,
6540            retire_if_needed,
6541        )
6542    }
6543
6544    /// Returns the number of source Connection IDs that are active. This is
6545    /// only meaningful if the host uses non-zero length Source Connection IDs.
6546    pub fn active_scids(&self) -> usize {
6547        self.ids.active_source_cids()
6548    }
6549
6550    /// Returns the number of source Connection IDs that should be provided
6551    /// to the peer without exceeding the limit it advertised.
6552    ///
6553    /// This will automatically limit the number of Connection IDs to the
6554    /// minimum between the locally configured active connection ID limit,
6555    /// and the one sent by the peer.
6556    ///
6557    /// To obtain the maximum possible value allowed by the peer an application
6558    /// can instead inspect the [`peer_active_conn_id_limit`] value.
6559    ///
6560    /// [`peer_active_conn_id_limit`]: struct.Stats.html#structfield.peer_active_conn_id_limit
6561    #[inline]
6562    pub fn scids_left(&self) -> usize {
6563        let max_active_source_cids = cmp::min(
6564            self.peer_transport_params.active_conn_id_limit,
6565            self.local_transport_params.active_conn_id_limit,
6566        ) as usize;
6567
6568        max_active_source_cids - self.active_scids()
6569    }
6570
6571    /// Requests the retirement of the destination Connection ID used by the
6572    /// host to reach its peer.
6573    ///
6574    /// This triggers sending RETIRE_CONNECTION_ID frames.
6575    ///
6576    /// If the application tries to retire a non-existing Destination Connection
6577    /// ID sequence number, or if it uses zero-length Destination Connection ID,
6578    /// this method returns an [`InvalidState`].
6579    ///
6580    /// At any time, the host must have at least one Destination ID. If the
6581    /// application tries to retire the last one, or if the caller tries to
6582    /// retire the destination Connection ID used by the current active path
6583    /// while having neither spare Destination Connection IDs nor validated
6584    /// network paths, this method returns an [`OutOfIdentifiers`]. This
6585    /// behavior prevents the caller from stalling the connection due to the
6586    /// lack of validated path to send non-probing packets.
6587    ///
6588    /// [`InvalidState`]: enum.Error.html#InvalidState
6589    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6590    pub fn retire_dcid(&mut self, dcid_seq: u64) -> Result<()> {
6591        if self.ids.zero_length_dcid() {
6592            return Err(Error::InvalidState);
6593        }
6594
6595        let active_path_dcid_seq = self
6596            .paths
6597            .get_active()?
6598            .active_dcid_seq
6599            .ok_or(Error::InvalidState)?;
6600
6601        let active_path_id = self.paths.get_active_path_id()?;
6602
6603        if active_path_dcid_seq == dcid_seq &&
6604            self.ids.lowest_available_dcid_seq().is_none() &&
6605            !self
6606                .paths
6607                .iter()
6608                .any(|(pid, p)| pid != active_path_id && p.usable())
6609        {
6610            return Err(Error::OutOfIdentifiers);
6611        }
6612
6613        if let Some(pid) = self.ids.retire_dcid(dcid_seq)? {
6614            // The retired Destination CID was associated to a given path. Let's
6615            // find an available DCID to associate to that path.
6616            let path = self.paths.get_mut(pid)?;
6617            let dcid_seq = self.ids.lowest_available_dcid_seq();
6618
6619            if let Some(dcid_seq) = dcid_seq {
6620                self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
6621            }
6622
6623            path.active_dcid_seq = dcid_seq;
6624        }
6625
6626        Ok(())
6627    }
6628
6629    /// Processes path-specific events.
6630    ///
6631    /// On success it returns a [`PathEvent`], or `None` when there are no
6632    /// events to report. Please refer to [`PathEvent`] for the exhaustive event
6633    /// list.
6634    ///
6635    /// Note that all events are edge-triggered, meaning that once reported they
6636    /// will not be reported again by calling this method again, until the event
6637    /// is re-armed.
6638    ///
6639    /// [`PathEvent`]: enum.PathEvent.html
6640    pub fn path_event_next(&mut self) -> Option<PathEvent> {
6641        self.paths.pop_event()
6642    }
6643
6644    /// Returns the number of source Connection IDs that are retired.
6645    pub fn retired_scids(&self) -> usize {
6646        self.ids.retired_source_cids()
6647    }
6648
6649    /// Returns a source `ConnectionId` that has been retired.
6650    ///
6651    /// On success it returns a [`ConnectionId`], or `None` when there are no
6652    /// more retired connection IDs.
6653    ///
6654    /// [`ConnectionId`]: struct.ConnectionId.html
6655    pub fn retired_scid_next(&mut self) -> Option<ConnectionId<'static>> {
6656        self.ids.pop_retired_scid()
6657    }
6658
6659    /// Returns the number of spare Destination Connection IDs, i.e.,
6660    /// Destination Connection IDs that are still unused.
6661    ///
6662    /// Note that this function returns 0 if the host uses zero length
6663    /// Destination Connection IDs.
6664    pub fn available_dcids(&self) -> usize {
6665        self.ids.available_dcids()
6666    }
6667
6668    /// Returns an iterator over destination `SockAddr`s whose association
6669    /// with `from` forms a known QUIC path on which packets can be sent to.
6670    ///
6671    /// This function is typically used in combination with [`send_on_path()`].
6672    ///
6673    /// Note that the iterator includes all the possible combination of
6674    /// destination `SockAddr`s, even those whose sending is not required now.
6675    /// In other words, this is another way for the application to recall from
6676    /// past [`PathEvent::New`] events.
6677    ///
6678    /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
6679    /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
6680    ///
6681    /// ## Examples:
6682    ///
6683    /// ```no_run
6684    /// # let mut out = [0; 512];
6685    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6686    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6687    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6688    /// # let local = socket.local_addr().unwrap();
6689    /// # let peer = "127.0.0.1:1234".parse().unwrap();
6690    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6691    /// // Iterate over possible destinations for the given local `SockAddr`.
6692    /// for dest in conn.paths_iter(local) {
6693    ///     loop {
6694    ///         let (write, send_info) =
6695    ///             match conn.send_on_path(&mut out, Some(local), Some(dest)) {
6696    ///                 Ok(v) => v,
6697    ///
6698    ///                 Err(quiche::Error::Done) => {
6699    ///                     // Done writing for this destination.
6700    ///                     break;
6701    ///                 },
6702    ///
6703    ///                 Err(e) => {
6704    ///                     // An error occurred, handle it.
6705    ///                     break;
6706    ///                 },
6707    ///             };
6708    ///
6709    ///         socket.send_to(&out[..write], &send_info.to).unwrap();
6710    ///     }
6711    /// }
6712    /// # Ok::<(), quiche::Error>(())
6713    /// ```
6714    #[inline]
6715    pub fn paths_iter(&self, from: SocketAddr) -> SocketAddrIter {
6716        // Instead of trying to identify whether packets will be sent on the
6717        // given 4-tuple, simply filter paths that cannot be used.
6718        SocketAddrIter {
6719            sockaddrs: self
6720                .paths
6721                .iter()
6722                .filter(|(_, p)| p.active_dcid_seq.is_some())
6723                .filter(|(_, p)| p.usable() || p.probing_required())
6724                .filter(|(_, p)| p.local_addr() == from)
6725                .map(|(_, p)| p.peer_addr())
6726                .collect(),
6727
6728            index: 0,
6729        }
6730    }
6731
6732    /// Closes the connection with the given error and reason.
6733    ///
6734    /// The `app` parameter specifies whether an application close should be
6735    /// sent to the peer. Otherwise a normal connection close is sent.
6736    ///
6737    /// If `app` is true but the connection is not in a state that is safe to
6738    /// send an application error (not established nor in early data), in
6739    /// accordance with [RFC
6740    /// 9000](https://www.rfc-editor.org/rfc/rfc9000.html#section-10.2.3-3), the
6741    /// error code is changed to APPLICATION_ERROR and the reason phrase is
6742    /// cleared.
6743    ///
6744    /// Returns [`Done`] if the connection had already been closed.
6745    ///
6746    /// Note that the connection will not be closed immediately. An application
6747    /// should continue calling the [`recv()`], [`send()`], [`timeout()`] and
6748    /// [`on_timeout()`] methods as normal, until the [`is_closed()`] method
6749    /// returns `true`.
6750    ///
6751    /// [`Done`]: enum.Error.html#variant.Done
6752    /// [`recv()`]: struct.Connection.html#method.recv
6753    /// [`send()`]: struct.Connection.html#method.send
6754    /// [`timeout()`]: struct.Connection.html#method.timeout
6755    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6756    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6757    pub fn close(&mut self, app: bool, err: u64, reason: &[u8]) -> Result<()> {
6758        if self.is_closed() || self.is_draining() {
6759            return Err(Error::Done);
6760        }
6761
6762        if self.local_error.is_some() {
6763            return Err(Error::Done);
6764        }
6765
6766        let is_safe_to_send_app_data =
6767            self.is_established() || self.is_in_early_data();
6768
6769        if app && !is_safe_to_send_app_data {
6770            // Clear error information.
6771            self.local_error = Some(ConnectionError {
6772                is_app: false,
6773                error_code: 0x0c,
6774                reason: vec![],
6775            });
6776        } else {
6777            self.local_error = Some(ConnectionError {
6778                is_app: app,
6779                error_code: err,
6780                reason: reason.to_vec(),
6781            });
6782        }
6783
6784        // When no packet was successfully processed close connection immediately.
6785        if self.recv_count == 0 {
6786            self.mark_closed();
6787        }
6788
6789        Ok(())
6790    }
6791
6792    /// Returns a string uniquely representing the connection.
6793    ///
6794    /// This can be used for logging purposes to differentiate between multiple
6795    /// connections.
6796    #[inline]
6797    pub fn trace_id(&self) -> &str {
6798        &self.trace_id
6799    }
6800
6801    /// Returns the negotiated ALPN protocol.
6802    ///
6803    /// If no protocol has been negotiated, the returned value is empty.
6804    #[inline]
6805    pub fn application_proto(&self) -> &[u8] {
6806        self.alpn.as_ref()
6807    }
6808
6809    /// Returns the server name requested by the client.
6810    #[inline]
6811    pub fn server_name(&self) -> Option<&str> {
6812        self.handshake.server_name()
6813    }
6814
6815    /// Returns the peer's leaf certificate (if any) as a DER-encoded buffer.
6816    #[inline]
6817    pub fn peer_cert(&self) -> Option<&[u8]> {
6818        self.handshake.peer_cert()
6819    }
6820
6821    /// Returns the peer's certificate chain (if any) as a vector of DER-encoded
6822    /// buffers.
6823    ///
6824    /// The certificate at index 0 is the peer's leaf certificate, the other
6825    /// certificates (if any) are the chain certificate authorities used to
6826    /// sign the leaf certificate.
6827    #[inline]
6828    pub fn peer_cert_chain(&self) -> Option<Vec<&[u8]>> {
6829        self.handshake.peer_cert_chain()
6830    }
6831
6832    /// Returns the serialized cryptographic session for the connection.
6833    ///
6834    /// This can be used by a client to cache a connection's session, and resume
6835    /// it later using the [`set_session()`] method.
6836    ///
6837    /// [`set_session()`]: struct.Connection.html#method.set_session
6838    #[inline]
6839    pub fn session(&self) -> Option<&[u8]> {
6840        self.session.as_deref()
6841    }
6842
6843    /// Returns the source connection ID.
6844    ///
6845    /// When there are multiple IDs, and if there is an active path, the ID used
6846    /// on that path is returned. Otherwise the oldest ID is returned.
6847    ///
6848    /// Note that the value returned can change throughout the connection's
6849    /// lifetime.
6850    #[inline]
6851    pub fn source_id(&self) -> ConnectionId {
6852        if let Ok(path) = self.paths.get_active() {
6853            if let Some(active_scid_seq) = path.active_scid_seq {
6854                if let Ok(e) = self.ids.get_scid(active_scid_seq) {
6855                    return ConnectionId::from_ref(e.cid.as_ref());
6856                }
6857            }
6858        }
6859
6860        let e = self.ids.oldest_scid();
6861        ConnectionId::from_ref(e.cid.as_ref())
6862    }
6863
6864    /// Returns all active source connection IDs.
6865    ///
6866    /// An iterator is returned for all active IDs (i.e. ones that have not
6867    /// been explicitly retired yet).
6868    #[inline]
6869    pub fn source_ids(&self) -> impl Iterator<Item = &ConnectionId> {
6870        self.ids.scids_iter()
6871    }
6872
6873    /// Returns the destination connection ID.
6874    ///
6875    /// Note that the value returned can change throughout the connection's
6876    /// lifetime.
6877    #[inline]
6878    pub fn destination_id(&self) -> ConnectionId {
6879        if let Ok(path) = self.paths.get_active() {
6880            if let Some(active_dcid_seq) = path.active_dcid_seq {
6881                if let Ok(e) = self.ids.get_dcid(active_dcid_seq) {
6882                    return ConnectionId::from_ref(e.cid.as_ref());
6883                }
6884            }
6885        }
6886
6887        let e = self.ids.oldest_dcid();
6888        ConnectionId::from_ref(e.cid.as_ref())
6889    }
6890
6891    /// Returns true if the connection handshake is complete.
6892    #[inline]
6893    pub fn is_established(&self) -> bool {
6894        self.handshake_completed
6895    }
6896
6897    /// Returns true if the connection is resumed.
6898    #[inline]
6899    pub fn is_resumed(&self) -> bool {
6900        self.handshake.is_resumed()
6901    }
6902
6903    /// Returns true if the connection has a pending handshake that has
6904    /// progressed enough to send or receive early data.
6905    #[inline]
6906    pub fn is_in_early_data(&self) -> bool {
6907        self.handshake.is_in_early_data()
6908    }
6909
6910    /// Returns whether there is stream or DATAGRAM data available to read.
6911    #[inline]
6912    pub fn is_readable(&self) -> bool {
6913        self.streams.has_readable() || self.dgram_recv_front_len().is_some()
6914    }
6915
6916    /// Returns whether the network path with local address `from` and remote
6917    /// address `peer` has been validated.
6918    ///
6919    /// If the 4-tuple does not exist over the connection, returns an
6920    /// [`InvalidState`].
6921    ///
6922    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
6923    pub fn is_path_validated(
6924        &self, from: SocketAddr, to: SocketAddr,
6925    ) -> Result<bool> {
6926        let pid = self
6927            .paths
6928            .path_id_from_addrs(&(from, to))
6929            .ok_or(Error::InvalidState)?;
6930
6931        Ok(self.paths.get(pid)?.validated())
6932    }
6933
6934    /// Returns true if the connection is draining.
6935    ///
6936    /// If this returns `true`, the connection object cannot yet be dropped, but
6937    /// no new application data can be sent or received. An application should
6938    /// continue calling the [`recv()`], [`timeout()`], and [`on_timeout()`]
6939    /// methods as normal, until the [`is_closed()`] method returns `true`.
6940    ///
6941    /// In contrast, once `is_draining()` returns `true`, calling [`send()`]
6942    /// is not required because no new outgoing packets will be generated.
6943    ///
6944    /// [`recv()`]: struct.Connection.html#method.recv
6945    /// [`send()`]: struct.Connection.html#method.send
6946    /// [`timeout()`]: struct.Connection.html#method.timeout
6947    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6948    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6949    #[inline]
6950    pub fn is_draining(&self) -> bool {
6951        self.draining_timer.is_some()
6952    }
6953
6954    /// Returns true if the connection is closed.
6955    ///
6956    /// If this returns true, the connection object can be dropped.
6957    #[inline]
6958    pub fn is_closed(&self) -> bool {
6959        self.closed
6960    }
6961
6962    /// Returns true if the connection was closed due to the idle timeout.
6963    #[inline]
6964    pub fn is_timed_out(&self) -> bool {
6965        self.timed_out
6966    }
6967
6968    /// Returns the error received from the peer, if any.
6969    ///
6970    /// Note that a `Some` return value does not necessarily imply
6971    /// [`is_closed()`] or any other connection state.
6972    ///
6973    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6974    #[inline]
6975    pub fn peer_error(&self) -> Option<&ConnectionError> {
6976        self.peer_error.as_ref()
6977    }
6978
6979    /// Returns the error [`close()`] was called with, or internally
6980    /// created quiche errors, if any.
6981    ///
6982    /// Note that a `Some` return value does not necessarily imply
6983    /// [`is_closed()`] or any other connection state.
6984    /// `Some` also does not guarantee that the error has been sent to
6985    /// or received by the peer.
6986    ///
6987    /// [`close()`]: struct.Connection.html#method.close
6988    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6989    #[inline]
6990    pub fn local_error(&self) -> Option<&ConnectionError> {
6991        self.local_error.as_ref()
6992    }
6993
6994    /// Collects and returns statistics about the connection.
6995    #[inline]
6996    pub fn stats(&self) -> Stats {
6997        Stats {
6998            recv: self.recv_count,
6999            sent: self.sent_count,
7000            lost: self.lost_count,
7001            spurious_lost: self.spurious_lost_count,
7002            retrans: self.retrans_count,
7003            sent_bytes: self.sent_bytes,
7004            recv_bytes: self.recv_bytes,
7005            acked_bytes: self.acked_bytes,
7006            lost_bytes: self.lost_bytes,
7007            stream_retrans_bytes: self.stream_retrans_bytes,
7008            dgram_recv: self.dgram_recv_count,
7009            dgram_sent: self.dgram_sent_count,
7010            paths_count: self.paths.len(),
7011            reset_stream_count_local: self.reset_stream_local_count,
7012            stopped_stream_count_local: self.stopped_stream_local_count,
7013            reset_stream_count_remote: self.reset_stream_remote_count,
7014            stopped_stream_count_remote: self.stopped_stream_remote_count,
7015            path_challenge_rx_count: self.path_challenge_rx_count,
7016        }
7017    }
7018
7019    /// Returns reference to peer's transport parameters. Returns `None` if we
7020    /// have not yet processed the peer's transport parameters.
7021    pub fn peer_transport_params(&self) -> Option<&TransportParams> {
7022        if !self.parsed_peer_transport_params {
7023            return None;
7024        }
7025
7026        Some(&self.peer_transport_params)
7027    }
7028
7029    /// Collects and returns statistics about each known path for the
7030    /// connection.
7031    pub fn path_stats(&self) -> impl Iterator<Item = PathStats> + '_ {
7032        self.paths.iter().map(|(_, p)| p.stats())
7033    }
7034
7035    /// Returns whether or not this is a server-side connection.
7036    pub fn is_server(&self) -> bool {
7037        self.is_server
7038    }
7039
7040    fn encode_transport_params(&mut self) -> Result<()> {
7041        let mut raw_params = [0; 128];
7042
7043        let raw_params = TransportParams::encode(
7044            &self.local_transport_params,
7045            self.is_server,
7046            &mut raw_params,
7047        )?;
7048
7049        self.handshake.set_quic_transport_params(raw_params)?;
7050
7051        Ok(())
7052    }
7053
7054    fn parse_peer_transport_params(
7055        &mut self, peer_params: TransportParams,
7056    ) -> Result<()> {
7057        // Validate initial_source_connection_id.
7058        match &peer_params.initial_source_connection_id {
7059            Some(v) if v != &self.destination_id() =>
7060                return Err(Error::InvalidTransportParam),
7061
7062            Some(_) => (),
7063
7064            // initial_source_connection_id must be sent by
7065            // both endpoints.
7066            None => return Err(Error::InvalidTransportParam),
7067        }
7068
7069        // Validate original_destination_connection_id.
7070        if let Some(odcid) = &self.odcid {
7071            match &peer_params.original_destination_connection_id {
7072                Some(v) if v != odcid =>
7073                    return Err(Error::InvalidTransportParam),
7074
7075                Some(_) => (),
7076
7077                // original_destination_connection_id must be
7078                // sent by the server.
7079                None if !self.is_server =>
7080                    return Err(Error::InvalidTransportParam),
7081
7082                None => (),
7083            }
7084        }
7085
7086        // Validate retry_source_connection_id.
7087        if let Some(rscid) = &self.rscid {
7088            match &peer_params.retry_source_connection_id {
7089                Some(v) if v != rscid =>
7090                    return Err(Error::InvalidTransportParam),
7091
7092                Some(_) => (),
7093
7094                // retry_source_connection_id must be sent by
7095                // the server.
7096                None => return Err(Error::InvalidTransportParam),
7097            }
7098        }
7099
7100        self.process_peer_transport_params(peer_params)?;
7101
7102        self.parsed_peer_transport_params = true;
7103
7104        Ok(())
7105    }
7106
7107    fn process_peer_transport_params(
7108        &mut self, peer_params: TransportParams,
7109    ) -> Result<()> {
7110        self.max_tx_data = peer_params.initial_max_data;
7111
7112        // Update send capacity.
7113        self.update_tx_cap();
7114
7115        self.streams
7116            .update_peer_max_streams_bidi(peer_params.initial_max_streams_bidi);
7117        self.streams
7118            .update_peer_max_streams_uni(peer_params.initial_max_streams_uni);
7119
7120        let max_ack_delay =
7121            time::Duration::from_millis(peer_params.max_ack_delay);
7122
7123        self.recovery_config.max_ack_delay = max_ack_delay;
7124
7125        let active_path = self.paths.get_active_mut()?;
7126
7127        active_path.recovery.update_max_ack_delay(max_ack_delay);
7128
7129        if active_path.pmtud.get_probe_status() {
7130            active_path.recovery.pmtud_update_max_datagram_size(
7131                active_path
7132                    .pmtud
7133                    .get_probe_size()
7134                    .min(peer_params.max_udp_payload_size as usize),
7135            );
7136        } else {
7137            active_path.recovery.update_max_datagram_size(
7138                peer_params.max_udp_payload_size as usize,
7139            );
7140        }
7141
7142        // Record the max_active_conn_id parameter advertised by the peer.
7143        self.ids
7144            .set_source_conn_id_limit(peer_params.active_conn_id_limit);
7145
7146        self.peer_transport_params = peer_params;
7147
7148        Ok(())
7149    }
7150
7151    /// Continues the handshake.
7152    ///
7153    /// If the connection is already established, it does nothing.
7154    fn do_handshake(&mut self, now: time::Instant) -> Result<()> {
7155        let mut ex_data = tls::ExData {
7156            application_protos: &self.application_protos,
7157
7158            crypto_ctx: &mut self.crypto_ctx,
7159
7160            session: &mut self.session,
7161
7162            local_error: &mut self.local_error,
7163
7164            keylog: self.keylog.as_mut(),
7165
7166            trace_id: &self.trace_id,
7167
7168            recovery_config: self.recovery_config,
7169
7170            tx_cap_factor: self.tx_cap_factor,
7171
7172            is_server: self.is_server,
7173        };
7174
7175        if self.handshake_completed {
7176            return self.handshake.process_post_handshake(&mut ex_data);
7177        }
7178
7179        match self.handshake.do_handshake(&mut ex_data) {
7180            Ok(_) => (),
7181
7182            Err(Error::Done) => {
7183                // Apply in-handshake configuration from callbacks before any
7184                // packet has been sent.
7185                if self.sent_count == 0 {
7186                    if ex_data.recovery_config != self.recovery_config {
7187                        if let Ok(path) = self.paths.get_active_mut() {
7188                            self.recovery_config = ex_data.recovery_config;
7189                            path.reinit_recovery(&self.recovery_config);
7190                        }
7191                    }
7192
7193                    if ex_data.tx_cap_factor != self.tx_cap_factor {
7194                        self.tx_cap_factor = ex_data.tx_cap_factor;
7195                    }
7196                }
7197
7198                // Try to parse transport parameters as soon as the first flight
7199                // of handshake data is processed.
7200                //
7201                // This is potentially dangerous as the handshake hasn't been
7202                // completed yet, though it's required to be able to send data
7203                // in 0.5 RTT.
7204                let raw_params = self.handshake.quic_transport_params();
7205
7206                if !self.parsed_peer_transport_params && !raw_params.is_empty() {
7207                    let peer_params = TransportParams::decode(
7208                        raw_params,
7209                        self.is_server,
7210                        self.peer_transport_params_track_unknown,
7211                    )?;
7212
7213                    self.parse_peer_transport_params(peer_params)?;
7214                }
7215
7216                return Ok(());
7217            },
7218
7219            Err(e) => return Err(e),
7220        };
7221
7222        self.handshake_completed = self.handshake.is_completed();
7223
7224        self.alpn = self.handshake.alpn_protocol().to_vec();
7225
7226        let raw_params = self.handshake.quic_transport_params();
7227
7228        if !self.parsed_peer_transport_params && !raw_params.is_empty() {
7229            let peer_params = TransportParams::decode(
7230                raw_params,
7231                self.is_server,
7232                self.peer_transport_params_track_unknown,
7233            )?;
7234
7235            self.parse_peer_transport_params(peer_params)?;
7236        }
7237
7238        if self.handshake_completed {
7239            // The handshake is considered confirmed at the server when the
7240            // handshake completes, at which point we can also drop the
7241            // handshake epoch.
7242            if self.is_server {
7243                self.handshake_confirmed = true;
7244
7245                self.drop_epoch_state(packet::Epoch::Handshake, now);
7246            }
7247
7248            // Once the handshake is completed there's no point in processing
7249            // 0-RTT packets anymore, so clear the buffer now.
7250            self.undecryptable_pkts.clear();
7251
7252            trace!("{} connection established: proto={:?} cipher={:?} curve={:?} sigalg={:?} resumed={} {:?}",
7253                   &self.trace_id,
7254                   std::str::from_utf8(self.application_proto()),
7255                   self.handshake.cipher(),
7256                   self.handshake.curve(),
7257                   self.handshake.sigalg(),
7258                   self.handshake.is_resumed(),
7259                   self.peer_transport_params);
7260        }
7261
7262        Ok(())
7263    }
7264
7265    /// Selects the packet type for the next outgoing packet.
7266    fn write_pkt_type(&self, send_pid: usize) -> Result<packet::Type> {
7267        // On error send packet in the latest epoch available, but only send
7268        // 1-RTT ones when the handshake is completed.
7269        if self
7270            .local_error
7271            .as_ref()
7272            .is_some_and(|conn_err| !conn_err.is_app)
7273        {
7274            let epoch = match self.handshake.write_level() {
7275                crypto::Level::Initial => packet::Epoch::Initial,
7276                crypto::Level::ZeroRTT => unreachable!(),
7277                crypto::Level::Handshake => packet::Epoch::Handshake,
7278                crypto::Level::OneRTT => packet::Epoch::Application,
7279            };
7280
7281            if !self.handshake_confirmed {
7282                match epoch {
7283                    // Downgrade the epoch to Handshake as the handshake is not
7284                    // completed yet.
7285                    packet::Epoch::Application =>
7286                        return Ok(packet::Type::Handshake),
7287
7288                    // Downgrade the epoch to Initial as the remote peer might
7289                    // not be able to decrypt handshake packets yet.
7290                    packet::Epoch::Handshake
7291                        if self.crypto_ctx[packet::Epoch::Initial].has_keys() =>
7292                        return Ok(packet::Type::Initial),
7293
7294                    _ => (),
7295                };
7296            }
7297
7298            return Ok(packet::Type::from_epoch(epoch));
7299        }
7300
7301        for &epoch in packet::Epoch::epochs(
7302            packet::Epoch::Initial..=packet::Epoch::Application,
7303        ) {
7304            let crypto_ctx = &self.crypto_ctx[epoch];
7305            let pkt_space = &self.pkt_num_spaces[epoch];
7306
7307            // Only send packets in a space when we have the send keys for it.
7308            if crypto_ctx.crypto_seal.is_none() {
7309                continue;
7310            }
7311
7312            // We are ready to send data for this packet number space.
7313            if crypto_ctx.data_available() || pkt_space.ready() {
7314                return Ok(packet::Type::from_epoch(epoch));
7315            }
7316
7317            // There are lost frames in this packet number space.
7318            for (_, p) in self.paths.iter() {
7319                if p.recovery.has_lost_frames(epoch) {
7320                    return Ok(packet::Type::from_epoch(epoch));
7321                }
7322
7323                // We need to send PTO probe packets.
7324                if p.recovery.loss_probes(epoch) > 0 {
7325                    return Ok(packet::Type::from_epoch(epoch));
7326                }
7327            }
7328        }
7329
7330        // If there are flushable, almost full or blocked streams, use the
7331        // Application epoch.
7332        let send_path = self.paths.get(send_pid)?;
7333        if (self.is_established() || self.is_in_early_data()) &&
7334            (self.should_send_handshake_done() ||
7335                self.almost_full ||
7336                self.blocked_limit.is_some() ||
7337                self.dgram_send_queue.has_pending() ||
7338                self.local_error
7339                    .as_ref()
7340                    .is_some_and(|conn_err| conn_err.is_app) ||
7341                self.streams.should_update_max_streams_bidi() ||
7342                self.streams.should_update_max_streams_uni() ||
7343                self.streams.has_flushable() ||
7344                self.streams.has_almost_full() ||
7345                self.streams.has_blocked() ||
7346                self.streams.has_reset() ||
7347                self.streams.has_stopped() ||
7348                self.ids.has_new_scids() ||
7349                self.ids.has_retire_dcids() ||
7350                send_path.pmtud.get_probe_status() ||
7351                send_path.needs_ack_eliciting ||
7352                send_path.probing_required())
7353        {
7354            // Only clients can send 0-RTT packets.
7355            if !self.is_server && self.is_in_early_data() {
7356                return Ok(packet::Type::ZeroRTT);
7357            }
7358
7359            return Ok(packet::Type::Short);
7360        }
7361
7362        Err(Error::Done)
7363    }
7364
7365    /// Returns the mutable stream with the given ID if it exists, or creates
7366    /// a new one otherwise.
7367    fn get_or_create_stream(
7368        &mut self, id: u64, local: bool,
7369    ) -> Result<&mut stream::Stream<F>> {
7370        self.streams.get_or_create(
7371            id,
7372            &self.local_transport_params,
7373            &self.peer_transport_params,
7374            local,
7375            self.is_server,
7376        )
7377    }
7378
7379    /// Processes an incoming frame.
7380    fn process_frame(
7381        &mut self, frame: frame::Frame, hdr: &packet::Header,
7382        recv_path_id: usize, epoch: packet::Epoch, now: time::Instant,
7383    ) -> Result<()> {
7384        trace!("{} rx frm {:?}", self.trace_id, frame);
7385
7386        match frame {
7387            frame::Frame::Padding { .. } => (),
7388
7389            frame::Frame::Ping { .. } => (),
7390
7391            frame::Frame::ACK {
7392                ranges, ack_delay, ..
7393            } => {
7394                let ack_delay = ack_delay
7395                    .checked_mul(2_u64.pow(
7396                        self.peer_transport_params.ack_delay_exponent as u32,
7397                    ))
7398                    .ok_or(Error::InvalidFrame)?;
7399
7400                if epoch == packet::Epoch::Handshake ||
7401                    (epoch == packet::Epoch::Application &&
7402                        self.is_established())
7403                {
7404                    self.peer_verified_initial_address = true;
7405                }
7406
7407                let handshake_status = self.handshake_status();
7408
7409                let is_app_limited = self.delivery_rate_check_if_app_limited();
7410
7411                for (_, p) in self.paths.iter_mut() {
7412                    if is_app_limited {
7413                        p.recovery.delivery_rate_update_app_limited(true);
7414                    }
7415
7416                    let OnAckReceivedOutcome {
7417                        lost_packets,
7418                        lost_bytes,
7419                        acked_bytes,
7420                        spurious_losses,
7421                    } = p.recovery.on_ack_received(
7422                        &ranges,
7423                        ack_delay,
7424                        epoch,
7425                        handshake_status,
7426                        now,
7427                        &self.trace_id,
7428                    );
7429
7430                    self.lost_count += lost_packets;
7431                    self.lost_bytes += lost_bytes as u64;
7432                    self.acked_bytes += acked_bytes as u64;
7433                    self.spurious_lost_count += spurious_losses;
7434                }
7435            },
7436
7437            frame::Frame::ResetStream {
7438                stream_id,
7439                error_code,
7440                final_size,
7441            } => {
7442                // Peer can't send on our unidirectional streams.
7443                if !stream::is_bidi(stream_id) &&
7444                    stream::is_local(stream_id, self.is_server)
7445                {
7446                    return Err(Error::InvalidStreamState(stream_id));
7447                }
7448
7449                let max_rx_data_left = self.max_rx_data() - self.rx_data;
7450
7451                // Get existing stream or create a new one, but if the stream
7452                // has already been closed and collected, ignore the frame.
7453                //
7454                // This can happen if e.g. an ACK frame is lost, and the peer
7455                // retransmits another frame before it realizes that the stream
7456                // is gone.
7457                //
7458                // Note that it makes it impossible to check if the frame is
7459                // illegal, since we have no state, but since we ignore the
7460                // frame, it should be fine.
7461                let stream = match self.get_or_create_stream(stream_id, false) {
7462                    Ok(v) => v,
7463
7464                    Err(Error::Done) => return Ok(()),
7465
7466                    Err(e) => return Err(e),
7467                };
7468
7469                let was_readable = stream.is_readable();
7470                let priority_key = Arc::clone(&stream.priority_key);
7471
7472                let max_off_delta =
7473                    stream.recv.reset(error_code, final_size)? as u64;
7474
7475                if max_off_delta > max_rx_data_left {
7476                    return Err(Error::FlowControl);
7477                }
7478
7479                if !was_readable && stream.is_readable() {
7480                    self.streams.insert_readable(&priority_key);
7481                }
7482
7483                self.rx_data += max_off_delta;
7484
7485                self.reset_stream_remote_count =
7486                    self.reset_stream_remote_count.saturating_add(1);
7487            },
7488
7489            frame::Frame::StopSending {
7490                stream_id,
7491                error_code,
7492            } => {
7493                // STOP_SENDING on a receive-only stream is a fatal error.
7494                if !stream::is_local(stream_id, self.is_server) &&
7495                    !stream::is_bidi(stream_id)
7496                {
7497                    return Err(Error::InvalidStreamState(stream_id));
7498                }
7499
7500                // Get existing stream or create a new one, but if the stream
7501                // has already been closed and collected, ignore the frame.
7502                //
7503                // This can happen if e.g. an ACK frame is lost, and the peer
7504                // retransmits another frame before it realizes that the stream
7505                // is gone.
7506                //
7507                // Note that it makes it impossible to check if the frame is
7508                // illegal, since we have no state, but since we ignore the
7509                // frame, it should be fine.
7510                let stream = match self.get_or_create_stream(stream_id, false) {
7511                    Ok(v) => v,
7512
7513                    Err(Error::Done) => return Ok(()),
7514
7515                    Err(e) => return Err(e),
7516                };
7517
7518                let was_writable = stream.is_writable();
7519
7520                let priority_key = Arc::clone(&stream.priority_key);
7521
7522                // Try stopping the stream.
7523                if let Ok((final_size, unsent)) = stream.send.stop(error_code) {
7524                    // Claw back some flow control allowance from data that was
7525                    // buffered but not actually sent before the stream was
7526                    // reset.
7527                    //
7528                    // Note that `tx_cap` will be updated later on, so no need
7529                    // to touch it here.
7530                    self.tx_data = self.tx_data.saturating_sub(unsent);
7531
7532                    self.tx_buffered =
7533                        self.tx_buffered.saturating_sub(unsent as usize);
7534
7535                    self.streams.insert_reset(stream_id, error_code, final_size);
7536
7537                    if !was_writable {
7538                        self.streams.insert_writable(&priority_key);
7539                    }
7540
7541                    self.stopped_stream_remote_count =
7542                        self.stopped_stream_remote_count.saturating_add(1);
7543                    self.reset_stream_local_count =
7544                        self.reset_stream_local_count.saturating_add(1);
7545                }
7546            },
7547
7548            frame::Frame::Crypto { data } => {
7549                if data.max_off() >= MAX_CRYPTO_STREAM_OFFSET {
7550                    return Err(Error::CryptoBufferExceeded);
7551                }
7552
7553                // Push the data to the stream so it can be re-ordered.
7554                self.crypto_ctx[epoch].crypto_stream.recv.write(data)?;
7555
7556                // Feed crypto data to the TLS state, if there's data
7557                // available at the expected offset.
7558                let mut crypto_buf = [0; 512];
7559
7560                let level = crypto::Level::from_epoch(epoch);
7561
7562                let stream = &mut self.crypto_ctx[epoch].crypto_stream;
7563
7564                while let Ok((read, _)) = stream.recv.emit(&mut crypto_buf) {
7565                    let recv_buf = &crypto_buf[..read];
7566                    self.handshake.provide_data(level, recv_buf)?;
7567                }
7568
7569                self.do_handshake(now)?;
7570            },
7571
7572            frame::Frame::CryptoHeader { .. } => unreachable!(),
7573
7574            // TODO: implement stateless retry
7575            frame::Frame::NewToken { .. } =>
7576                if self.is_server {
7577                    return Err(Error::InvalidPacket);
7578                },
7579
7580            frame::Frame::Stream { stream_id, data } => {
7581                // Peer can't send on our unidirectional streams.
7582                if !stream::is_bidi(stream_id) &&
7583                    stream::is_local(stream_id, self.is_server)
7584                {
7585                    return Err(Error::InvalidStreamState(stream_id));
7586                }
7587
7588                let max_rx_data_left = self.max_rx_data() - self.rx_data;
7589
7590                // Get existing stream or create a new one, but if the stream
7591                // has already been closed and collected, ignore the frame.
7592                //
7593                // This can happen if e.g. an ACK frame is lost, and the peer
7594                // retransmits another frame before it realizes that the stream
7595                // is gone.
7596                //
7597                // Note that it makes it impossible to check if the frame is
7598                // illegal, since we have no state, but since we ignore the
7599                // frame, it should be fine.
7600                let stream = match self.get_or_create_stream(stream_id, false) {
7601                    Ok(v) => v,
7602
7603                    Err(Error::Done) => return Ok(()),
7604
7605                    Err(e) => return Err(e),
7606                };
7607
7608                // Check for the connection-level flow control limit.
7609                let max_off_delta =
7610                    data.max_off().saturating_sub(stream.recv.max_off());
7611
7612                if max_off_delta > max_rx_data_left {
7613                    return Err(Error::FlowControl);
7614                }
7615
7616                let was_readable = stream.is_readable();
7617                let priority_key = Arc::clone(&stream.priority_key);
7618
7619                let was_draining = stream.recv.is_draining();
7620
7621                stream.recv.write(data)?;
7622
7623                if !was_readable && stream.is_readable() {
7624                    self.streams.insert_readable(&priority_key);
7625                }
7626
7627                self.rx_data += max_off_delta;
7628
7629                if was_draining {
7630                    // When a stream is in draining state it will not queue
7631                    // incoming data for the application to read, so consider
7632                    // the received data as consumed, which might trigger a flow
7633                    // control update.
7634                    self.flow_control.add_consumed(max_off_delta);
7635
7636                    if self.should_update_max_data() {
7637                        self.almost_full = true;
7638                    }
7639                }
7640            },
7641
7642            frame::Frame::StreamHeader { .. } => unreachable!(),
7643
7644            frame::Frame::MaxData { max } => {
7645                self.max_tx_data = cmp::max(self.max_tx_data, max);
7646            },
7647
7648            frame::Frame::MaxStreamData { stream_id, max } => {
7649                // Peer can't receive on its own unidirectional streams.
7650                if !stream::is_bidi(stream_id) &&
7651                    !stream::is_local(stream_id, self.is_server)
7652                {
7653                    return Err(Error::InvalidStreamState(stream_id));
7654                }
7655
7656                // Get existing stream or create a new one, but if the stream
7657                // has already been closed and collected, ignore the frame.
7658                //
7659                // This can happen if e.g. an ACK frame is lost, and the peer
7660                // retransmits another frame before it realizes that the stream
7661                // is gone.
7662                //
7663                // Note that it makes it impossible to check if the frame is
7664                // illegal, since we have no state, but since we ignore the
7665                // frame, it should be fine.
7666                let stream = match self.get_or_create_stream(stream_id, false) {
7667                    Ok(v) => v,
7668
7669                    Err(Error::Done) => return Ok(()),
7670
7671                    Err(e) => return Err(e),
7672                };
7673
7674                let was_flushable = stream.is_flushable();
7675
7676                stream.send.update_max_data(max);
7677
7678                let writable = stream.is_writable();
7679
7680                let priority_key = Arc::clone(&stream.priority_key);
7681
7682                // If the stream is now flushable push it to the flushable queue,
7683                // but only if it wasn't already queued.
7684                if stream.is_flushable() && !was_flushable {
7685                    let priority_key = Arc::clone(&stream.priority_key);
7686                    self.streams.insert_flushable(&priority_key);
7687                }
7688
7689                if writable {
7690                    self.streams.insert_writable(&priority_key);
7691                }
7692            },
7693
7694            frame::Frame::MaxStreamsBidi { max } => {
7695                if max > MAX_STREAM_ID {
7696                    return Err(Error::InvalidFrame);
7697                }
7698
7699                self.streams.update_peer_max_streams_bidi(max);
7700            },
7701
7702            frame::Frame::MaxStreamsUni { max } => {
7703                if max > MAX_STREAM_ID {
7704                    return Err(Error::InvalidFrame);
7705                }
7706
7707                self.streams.update_peer_max_streams_uni(max);
7708            },
7709
7710            frame::Frame::DataBlocked { .. } => (),
7711
7712            frame::Frame::StreamDataBlocked { .. } => (),
7713
7714            frame::Frame::StreamsBlockedBidi { limit } => {
7715                if limit > MAX_STREAM_ID {
7716                    return Err(Error::InvalidFrame);
7717                }
7718            },
7719
7720            frame::Frame::StreamsBlockedUni { limit } => {
7721                if limit > MAX_STREAM_ID {
7722                    return Err(Error::InvalidFrame);
7723                }
7724            },
7725
7726            frame::Frame::NewConnectionId {
7727                seq_num,
7728                retire_prior_to,
7729                conn_id,
7730                reset_token,
7731            } => {
7732                if self.ids.zero_length_dcid() {
7733                    return Err(Error::InvalidState);
7734                }
7735
7736                let mut retired_path_ids = SmallVec::new();
7737
7738                // Retire pending path IDs before propagating the error code to
7739                // make sure retired connection IDs are not in use anymore.
7740                let new_dcid_res = self.ids.new_dcid(
7741                    conn_id.into(),
7742                    seq_num,
7743                    u128::from_be_bytes(reset_token),
7744                    retire_prior_to,
7745                    &mut retired_path_ids,
7746                );
7747
7748                for (dcid_seq, pid) in retired_path_ids {
7749                    let path = self.paths.get_mut(pid)?;
7750
7751                    // Maybe the path already switched to another DCID.
7752                    if path.active_dcid_seq != Some(dcid_seq) {
7753                        continue;
7754                    }
7755
7756                    if let Some(new_dcid_seq) =
7757                        self.ids.lowest_available_dcid_seq()
7758                    {
7759                        path.active_dcid_seq = Some(new_dcid_seq);
7760
7761                        self.ids.link_dcid_to_path_id(new_dcid_seq, pid)?;
7762
7763                        trace!(
7764                            "{} path ID {} changed DCID: old seq num {} new seq num {}",
7765                            self.trace_id, pid, dcid_seq, new_dcid_seq,
7766                        );
7767                    } else {
7768                        // We cannot use this path anymore for now.
7769                        path.active_dcid_seq = None;
7770
7771                        trace!(
7772                            "{} path ID {} cannot be used; DCID seq num {} has been retired",
7773                            self.trace_id, pid, dcid_seq,
7774                        );
7775                    }
7776                }
7777
7778                // Propagate error (if any) now...
7779                new_dcid_res?;
7780            },
7781
7782            frame::Frame::RetireConnectionId { seq_num } => {
7783                if self.ids.zero_length_scid() {
7784                    return Err(Error::InvalidState);
7785                }
7786
7787                if let Some(pid) = self.ids.retire_scid(seq_num, &hdr.dcid)? {
7788                    let path = self.paths.get_mut(pid)?;
7789
7790                    // Maybe we already linked a new SCID to that path.
7791                    if path.active_scid_seq == Some(seq_num) {
7792                        // XXX: We do not remove unused paths now, we instead
7793                        // wait until we need to maintain more paths than the
7794                        // host is willing to.
7795                        path.active_scid_seq = None;
7796                    }
7797                }
7798            },
7799
7800            frame::Frame::PathChallenge { data } => {
7801                self.path_challenge_rx_count += 1;
7802
7803                self.paths
7804                    .get_mut(recv_path_id)?
7805                    .on_challenge_received(data);
7806            },
7807
7808            frame::Frame::PathResponse { data } => {
7809                self.paths.on_response_received(data)?;
7810            },
7811
7812            frame::Frame::ConnectionClose {
7813                error_code, reason, ..
7814            } => {
7815                self.peer_error = Some(ConnectionError {
7816                    is_app: false,
7817                    error_code,
7818                    reason,
7819                });
7820
7821                let path = self.paths.get_active()?;
7822                self.draining_timer = Some(now + (path.recovery.pto() * 3));
7823            },
7824
7825            frame::Frame::ApplicationClose { error_code, reason } => {
7826                self.peer_error = Some(ConnectionError {
7827                    is_app: true,
7828                    error_code,
7829                    reason,
7830                });
7831
7832                let path = self.paths.get_active()?;
7833                self.draining_timer = Some(now + (path.recovery.pto() * 3));
7834            },
7835
7836            frame::Frame::HandshakeDone => {
7837                if self.is_server {
7838                    return Err(Error::InvalidPacket);
7839                }
7840
7841                self.peer_verified_initial_address = true;
7842
7843                self.handshake_confirmed = true;
7844
7845                // Once the handshake is confirmed, we can drop Handshake keys.
7846                self.drop_epoch_state(packet::Epoch::Handshake, now);
7847            },
7848
7849            frame::Frame::Datagram { data } => {
7850                // Close the connection if DATAGRAMs are not enabled.
7851                // quiche always advertises support for 64K sized DATAGRAM
7852                // frames, as recommended by the standard, so we don't need a
7853                // size check.
7854                if !self.dgram_enabled() {
7855                    return Err(Error::InvalidState);
7856                }
7857
7858                // If recv queue is full, discard oldest
7859                if self.dgram_recv_queue.is_full() {
7860                    self.dgram_recv_queue.pop();
7861                }
7862
7863                self.dgram_recv_queue.push(data)?;
7864
7865                let _ = self.dgram_recv_count.saturating_add(1);
7866                let _ = self
7867                    .paths
7868                    .get_mut(recv_path_id)?
7869                    .dgram_recv_count
7870                    .saturating_add(1);
7871            },
7872
7873            frame::Frame::DatagramHeader { .. } => unreachable!(),
7874        }
7875
7876        Ok(())
7877    }
7878
7879    /// Drops the keys and recovery state for the given epoch.
7880    fn drop_epoch_state(&mut self, epoch: packet::Epoch, now: time::Instant) {
7881        let crypto_ctx = &mut self.crypto_ctx[epoch];
7882        if crypto_ctx.crypto_open.is_none() {
7883            return;
7884        }
7885        crypto_ctx.clear();
7886        self.pkt_num_spaces[epoch].clear();
7887
7888        let handshake_status = self.handshake_status();
7889        for (_, p) in self.paths.iter_mut() {
7890            p.recovery
7891                .on_pkt_num_space_discarded(epoch, handshake_status, now);
7892        }
7893
7894        trace!("{} dropped epoch {} state", self.trace_id, epoch);
7895    }
7896
7897    /// Returns true if the connection-level flow control needs to be updated.
7898    ///
7899    /// This happens when the new max data limit is at least double the amount
7900    /// of data that can be received before blocking.
7901    fn should_update_max_data(&self) -> bool {
7902        self.flow_control.should_update_max_data()
7903    }
7904
7905    /// Returns the connection level flow control limit.
7906    fn max_rx_data(&self) -> u64 {
7907        self.flow_control.max_data()
7908    }
7909
7910    /// Returns true if the HANDSHAKE_DONE frame needs to be sent.
7911    fn should_send_handshake_done(&self) -> bool {
7912        self.is_established() && !self.handshake_done_sent && self.is_server
7913    }
7914
7915    /// Returns the idle timeout value.
7916    ///
7917    /// `None` is returned if both end-points disabled the idle timeout.
7918    fn idle_timeout(&self) -> Option<time::Duration> {
7919        // If the transport parameter is set to 0, then the respective endpoint
7920        // decided to disable the idle timeout. If both are disabled we should
7921        // not set any timeout.
7922        if self.local_transport_params.max_idle_timeout == 0 &&
7923            self.peer_transport_params.max_idle_timeout == 0
7924        {
7925            return None;
7926        }
7927
7928        // If the local endpoint or the peer disabled the idle timeout, use the
7929        // other peer's value, otherwise use the minimum of the two values.
7930        let idle_timeout = if self.local_transport_params.max_idle_timeout == 0 {
7931            self.peer_transport_params.max_idle_timeout
7932        } else if self.peer_transport_params.max_idle_timeout == 0 {
7933            self.local_transport_params.max_idle_timeout
7934        } else {
7935            cmp::min(
7936                self.local_transport_params.max_idle_timeout,
7937                self.peer_transport_params.max_idle_timeout,
7938            )
7939        };
7940
7941        let path_pto = match self.paths.get_active() {
7942            Ok(p) => p.recovery.pto(),
7943            Err(_) => time::Duration::ZERO,
7944        };
7945
7946        let idle_timeout = time::Duration::from_millis(idle_timeout);
7947        let idle_timeout = cmp::max(idle_timeout, 3 * path_pto);
7948
7949        Some(idle_timeout)
7950    }
7951
7952    /// Returns the connection's handshake status for use in loss recovery.
7953    fn handshake_status(&self) -> recovery::HandshakeStatus {
7954        recovery::HandshakeStatus {
7955            has_handshake_keys: self.crypto_ctx[packet::Epoch::Handshake]
7956                .has_keys(),
7957
7958            peer_verified_address: self.peer_verified_initial_address,
7959
7960            completed: self.is_established(),
7961        }
7962    }
7963
7964    /// Updates send capacity.
7965    fn update_tx_cap(&mut self) {
7966        let cwin_available = match self.paths.get_active() {
7967            Ok(p) => p.recovery.cwnd_available() as u64,
7968            Err(_) => 0,
7969        };
7970
7971        let cap =
7972            cmp::min(cwin_available, self.max_tx_data - self.tx_data) as usize;
7973        self.tx_cap = (cap as f64 * self.tx_cap_factor).ceil() as usize;
7974    }
7975
7976    fn delivery_rate_check_if_app_limited(&self) -> bool {
7977        // Enter the app-limited phase of delivery rate when these conditions
7978        // are met:
7979        //
7980        // - The remaining capacity is higher than available bytes in cwnd (there
7981        //   is more room to send).
7982        // - New data since the last send() is smaller than available bytes in
7983        //   cwnd (we queued less than what we can send).
7984        // - There is room to send more data in cwnd.
7985        //
7986        // In application-limited phases the transmission rate is limited by the
7987        // application rather than the congestion control algorithm.
7988        //
7989        // Note that this is equivalent to CheckIfApplicationLimited() from the
7990        // delivery rate draft. This is also separate from `recovery.app_limited`
7991        // and only applies to delivery rate calculation.
7992        let cwin_available = self
7993            .paths
7994            .iter()
7995            .filter(|&(_, p)| p.active())
7996            .map(|(_, p)| p.recovery.cwnd_available())
7997            .sum();
7998
7999        ((self.tx_buffered + self.dgram_send_queue_byte_size()) < cwin_available) &&
8000            (self.tx_data.saturating_sub(self.last_tx_data)) <
8001                cwin_available as u64 &&
8002            cwin_available > 0
8003    }
8004
8005    fn set_initial_dcid(
8006        &mut self, cid: ConnectionId<'static>, reset_token: Option<u128>,
8007        path_id: usize,
8008    ) -> Result<()> {
8009        self.ids.set_initial_dcid(cid, reset_token, Some(path_id));
8010        self.paths.get_mut(path_id)?.active_dcid_seq = Some(0);
8011
8012        Ok(())
8013    }
8014
8015    /// Selects the path that the incoming packet belongs to, or creates a new
8016    /// one if no existing path matches.
8017    fn get_or_create_recv_path_id(
8018        &mut self, recv_pid: Option<usize>, dcid: &ConnectionId, buf_len: usize,
8019        info: &RecvInfo,
8020    ) -> Result<usize> {
8021        let ids = &mut self.ids;
8022
8023        let (in_scid_seq, mut in_scid_pid) =
8024            ids.find_scid_seq(dcid).ok_or(Error::InvalidState)?;
8025
8026        if let Some(recv_pid) = recv_pid {
8027            // If the path observes a change of SCID used, note it.
8028            let recv_path = self.paths.get_mut(recv_pid)?;
8029
8030            let cid_entry =
8031                recv_path.active_scid_seq.and_then(|v| ids.get_scid(v).ok());
8032
8033            if cid_entry.map(|e| &e.cid) != Some(dcid) {
8034                let incoming_cid_entry = ids.get_scid(in_scid_seq)?;
8035
8036                let prev_recv_pid =
8037                    incoming_cid_entry.path_id.unwrap_or(recv_pid);
8038
8039                if prev_recv_pid != recv_pid {
8040                    trace!(
8041                        "{} peer reused CID {:?} from path {} on path {}",
8042                        self.trace_id,
8043                        dcid,
8044                        prev_recv_pid,
8045                        recv_pid
8046                    );
8047
8048                    // TODO: reset congestion control.
8049                }
8050
8051                trace!(
8052                    "{} path ID {} now see SCID with seq num {}",
8053                    self.trace_id,
8054                    recv_pid,
8055                    in_scid_seq
8056                );
8057
8058                recv_path.active_scid_seq = Some(in_scid_seq);
8059                ids.link_scid_to_path_id(in_scid_seq, recv_pid)?;
8060            }
8061
8062            return Ok(recv_pid);
8063        }
8064
8065        // This is a new 4-tuple. See if the CID has not been assigned on
8066        // another path.
8067
8068        // Ignore this step if are using zero-length SCID.
8069        if ids.zero_length_scid() {
8070            in_scid_pid = None;
8071        }
8072
8073        if let Some(in_scid_pid) = in_scid_pid {
8074            // This CID has been used by another path. If we have the
8075            // room to do so, create a new `Path` structure holding this
8076            // new 4-tuple. Otherwise, drop the packet.
8077            let old_path = self.paths.get_mut(in_scid_pid)?;
8078            let old_local_addr = old_path.local_addr();
8079            let old_peer_addr = old_path.peer_addr();
8080
8081            trace!(
8082                "{} reused CID seq {} of ({},{}) (path {}) on ({},{})",
8083                self.trace_id,
8084                in_scid_seq,
8085                old_local_addr,
8086                old_peer_addr,
8087                in_scid_pid,
8088                info.to,
8089                info.from
8090            );
8091
8092            // Notify the application.
8093            self.paths
8094                .notify_event(path::PathEvent::ReusedSourceConnectionId(
8095                    in_scid_seq,
8096                    (old_local_addr, old_peer_addr),
8097                    (info.to, info.from),
8098                ));
8099        }
8100
8101        // This is a new path using an unassigned CID; create it!
8102        let mut path = path::Path::new(
8103            info.to,
8104            info.from,
8105            &self.recovery_config,
8106            self.path_challenge_recv_max_queue_len,
8107            MIN_CLIENT_INITIAL_LEN,
8108            false,
8109        );
8110
8111        path.max_send_bytes = buf_len * self.max_amplification_factor;
8112        path.active_scid_seq = Some(in_scid_seq);
8113
8114        // Automatically probes the new path.
8115        path.request_validation();
8116
8117        let pid = self.paths.insert_path(path, self.is_server)?;
8118
8119        // Do not record path reuse.
8120        if in_scid_pid.is_none() {
8121            ids.link_scid_to_path_id(in_scid_seq, pid)?;
8122        }
8123
8124        Ok(pid)
8125    }
8126
8127    /// Selects the path on which the next packet must be sent.
8128    fn get_send_path_id(
8129        &self, from: Option<SocketAddr>, to: Option<SocketAddr>,
8130    ) -> Result<usize> {
8131        // A probing packet must be sent, but only if the connection is fully
8132        // established.
8133        if self.is_established() {
8134            let mut probing = self
8135                .paths
8136                .iter()
8137                .filter(|(_, p)| from.is_none() || Some(p.local_addr()) == from)
8138                .filter(|(_, p)| to.is_none() || Some(p.peer_addr()) == to)
8139                .filter(|(_, p)| p.active_dcid_seq.is_some())
8140                .filter(|(_, p)| p.probing_required())
8141                .map(|(pid, _)| pid);
8142
8143            if let Some(pid) = probing.next() {
8144                return Ok(pid);
8145            }
8146        }
8147
8148        if let Some((pid, p)) = self.paths.get_active_with_pid() {
8149            if from.is_some() && Some(p.local_addr()) != from {
8150                return Err(Error::Done);
8151            }
8152
8153            if to.is_some() && Some(p.peer_addr()) != to {
8154                return Err(Error::Done);
8155            }
8156
8157            return Ok(pid);
8158        };
8159
8160        Err(Error::InvalidState)
8161    }
8162
8163    /// Sets the path with identifier 'path_id' to be active.
8164    fn set_active_path(
8165        &mut self, path_id: usize, now: time::Instant,
8166    ) -> Result<()> {
8167        if let Ok(old_active_path) = self.paths.get_active_mut() {
8168            for &e in packet::Epoch::epochs(
8169                packet::Epoch::Initial..=packet::Epoch::Application,
8170            ) {
8171                let (lost_packets, lost_bytes) = old_active_path
8172                    .recovery
8173                    .on_path_change(e, now, &self.trace_id);
8174
8175                self.lost_count += lost_packets;
8176                self.lost_bytes += lost_bytes as u64;
8177            }
8178        }
8179
8180        self.paths.set_active_path(path_id)
8181    }
8182
8183    /// Handles potential connection migration.
8184    fn on_peer_migrated(
8185        &mut self, new_pid: usize, disable_dcid_reuse: bool, now: time::Instant,
8186    ) -> Result<()> {
8187        let active_path_id = self.paths.get_active_path_id()?;
8188
8189        if active_path_id == new_pid {
8190            return Ok(());
8191        }
8192
8193        self.set_active_path(new_pid, now)?;
8194
8195        let no_spare_dcid =
8196            self.paths.get_mut(new_pid)?.active_dcid_seq.is_none();
8197
8198        if no_spare_dcid && !disable_dcid_reuse {
8199            self.paths.get_mut(new_pid)?.active_dcid_seq =
8200                self.paths.get_mut(active_path_id)?.active_dcid_seq;
8201        }
8202
8203        Ok(())
8204    }
8205
8206    /// Creates a new client-side path.
8207    fn create_path_on_client(
8208        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
8209    ) -> Result<usize> {
8210        if self.is_server {
8211            return Err(Error::InvalidState);
8212        }
8213
8214        // If we use zero-length SCID and go over our local active CID limit,
8215        // the `insert_path()` call will raise an error.
8216        if !self.ids.zero_length_scid() && self.ids.available_scids() == 0 {
8217            return Err(Error::OutOfIdentifiers);
8218        }
8219
8220        // Do we have a spare DCID? If we are using zero-length DCID, just use
8221        // the default having sequence 0 (note that if we exceed our local CID
8222        // limit, the `insert_path()` call will raise an error.
8223        let dcid_seq = if self.ids.zero_length_dcid() {
8224            0
8225        } else {
8226            self.ids
8227                .lowest_available_dcid_seq()
8228                .ok_or(Error::OutOfIdentifiers)?
8229        };
8230
8231        let mut path = path::Path::new(
8232            local_addr,
8233            peer_addr,
8234            &self.recovery_config,
8235            self.path_challenge_recv_max_queue_len,
8236            MIN_CLIENT_INITIAL_LEN,
8237            false,
8238        );
8239        path.active_dcid_seq = Some(dcid_seq);
8240
8241        let pid = self
8242            .paths
8243            .insert_path(path, false)
8244            .map_err(|_| Error::OutOfIdentifiers)?;
8245        self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
8246
8247        Ok(pid)
8248    }
8249
8250    // Marks the connection as closed and does any related tidyup.
8251    fn mark_closed(&mut self) {
8252        #[cfg(feature = "qlog")]
8253        {
8254            let cc = match (self.is_established(), self.timed_out, &self.peer_error, &self.local_error) {
8255                (false, _, _, _) => qlog::events::connectivity::ConnectionClosed {
8256                    owner: Some(TransportOwner::Local),
8257                    connection_code: None,
8258                    application_code: None,
8259                    internal_code: None,
8260                    reason: Some("Failed to establish connection".to_string()),
8261                    trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::HandshakeTimeout)
8262                },
8263
8264                (true, true, _, _) => qlog::events::connectivity::ConnectionClosed {
8265                    owner: Some(TransportOwner::Local),
8266                    connection_code: None,
8267                    application_code: None,
8268                    internal_code: None,
8269                    reason: Some("Idle timeout".to_string()),
8270                    trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::IdleTimeout)
8271                },
8272
8273                (true, false, Some(peer_error), None) => {
8274                    let (connection_code, application_code, trigger) = if peer_error.is_app {
8275                        (None, Some(qlog::events::ApplicationErrorCode::Value(peer_error.error_code)), None)
8276                    } else {
8277                        let trigger = if peer_error.error_code == WireErrorCode::NoError as u64 {
8278                            Some(qlog::events::connectivity::ConnectionClosedTrigger::Clean)
8279                        } else {
8280                            Some(qlog::events::connectivity::ConnectionClosedTrigger::Error)
8281                        };
8282
8283                        (Some(qlog::events::ConnectionErrorCode::Value(peer_error.error_code)), None, trigger)
8284                    };
8285
8286                    qlog::events::connectivity::ConnectionClosed {
8287                        owner: Some(TransportOwner::Remote),
8288                        connection_code,
8289                        application_code,
8290                        internal_code: None,
8291                        reason: Some(String::from_utf8_lossy(&peer_error.reason).to_string()),
8292                        trigger,
8293                    }
8294                },
8295
8296                (true, false, None, Some(local_error)) => {
8297                    let (connection_code, application_code, trigger) = if local_error.is_app {
8298                        (None, Some(qlog::events::ApplicationErrorCode::Value(local_error.error_code)), None)
8299                    } else {
8300                        let trigger = if local_error.error_code == WireErrorCode::NoError as u64 {
8301                            Some(qlog::events::connectivity::ConnectionClosedTrigger::Clean)
8302                        } else {
8303                            Some(qlog::events::connectivity::ConnectionClosedTrigger::Error)
8304                        };
8305
8306                        (Some(qlog::events::ConnectionErrorCode::Value(local_error.error_code)), None, trigger)
8307                    };
8308
8309                    qlog::events::connectivity::ConnectionClosed {
8310                        owner: Some(TransportOwner::Local),
8311                        connection_code,
8312                        application_code,
8313                        internal_code: None,
8314                        reason: Some(String::from_utf8_lossy(&local_error.reason).to_string()),
8315                        trigger,
8316                    }
8317                },
8318
8319                _ => qlog::events::connectivity::ConnectionClosed {
8320                    owner: None,
8321                    connection_code: None,
8322                    application_code: None,
8323                    internal_code: None,
8324                    reason: None,
8325                    trigger: None,
8326                },
8327            };
8328
8329            qlog_with_type!(QLOG_CONNECTION_CLOSED, self.qlog, q, {
8330                let ev_data = qlog::events::EventData::ConnectionClosed(cc);
8331
8332                q.add_event_data_now(ev_data).ok();
8333            });
8334            self.qlog.streamer = None;
8335        }
8336        self.closed = true;
8337    }
8338}
8339
8340#[cfg(feature = "boringssl-boring-crate")]
8341impl<F: BufFactory> AsMut<boring::ssl::SslRef> for Connection<F> {
8342    fn as_mut(&mut self) -> &mut boring::ssl::SslRef {
8343        self.handshake.ssl_mut()
8344    }
8345}
8346
8347/// Maps an `Error` to `Error::Done`, or itself.
8348///
8349/// When a received packet that hasn't yet been authenticated triggers a failure
8350/// it should, in most cases, be ignored, instead of raising a connection error,
8351/// to avoid potential man-in-the-middle and man-on-the-side attacks.
8352///
8353/// However, if no other packet was previously received, the connection should
8354/// indeed be closed as the received packet might just be network background
8355/// noise, and it shouldn't keep resources occupied indefinitely.
8356///
8357/// This function maps an error to `Error::Done` to ignore a packet failure
8358/// without aborting the connection, except when no other packet was previously
8359/// received, in which case the error itself is returned, but only on the
8360/// server-side as the client will already have armed the idle timer.
8361///
8362/// This must only be used for errors preceding packet authentication. Failures
8363/// happening after a packet has been authenticated should still cause the
8364/// connection to be aborted.
8365fn drop_pkt_on_err(
8366    e: Error, recv_count: usize, is_server: bool, trace_id: &str,
8367) -> Error {
8368    // On the server, if no other packet has been successfully processed, abort
8369    // the connection to avoid keeping the connection open when only junk is
8370    // received.
8371    if is_server && recv_count == 0 {
8372        return e;
8373    }
8374
8375    trace!("{} dropped invalid packet", trace_id);
8376
8377    // Ignore other invalid packets that haven't been authenticated to prevent
8378    // man-in-the-middle and man-on-the-side attacks.
8379    Error::Done
8380}
8381
8382struct AddrTupleFmt(SocketAddr, SocketAddr);
8383
8384impl std::fmt::Display for AddrTupleFmt {
8385    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8386        let AddrTupleFmt(src, dst) = &self;
8387
8388        if src.ip().is_unspecified() || dst.ip().is_unspecified() {
8389            return Ok(());
8390        }
8391
8392        f.write_fmt(format_args!("src:{src} dst:{dst}"))
8393    }
8394}
8395
8396/// Statistics about the connection.
8397///
8398/// A connection's statistics can be collected using the [`stats()`] method.
8399///
8400/// [`stats()`]: struct.Connection.html#method.stats
8401#[derive(Clone, Default)]
8402pub struct Stats {
8403    /// The number of QUIC packets received.
8404    pub recv: usize,
8405
8406    /// The number of QUIC packets sent.
8407    pub sent: usize,
8408
8409    /// The number of QUIC packets that were lost.
8410    pub lost: usize,
8411
8412    /// The number of QUIC packets that were marked as lost but later acked.
8413    pub spurious_lost: usize,
8414
8415    /// The number of sent QUIC packets with retransmitted data.
8416    pub retrans: usize,
8417
8418    /// The number of sent bytes.
8419    pub sent_bytes: u64,
8420
8421    /// The number of received bytes.
8422    pub recv_bytes: u64,
8423
8424    /// The number of bytes sent acked.
8425    pub acked_bytes: u64,
8426
8427    /// The number of bytes sent lost.
8428    pub lost_bytes: u64,
8429
8430    /// The number of stream bytes retransmitted.
8431    pub stream_retrans_bytes: u64,
8432
8433    /// The number of DATAGRAM frames received.
8434    pub dgram_recv: usize,
8435
8436    /// The number of DATAGRAM frames sent.
8437    pub dgram_sent: usize,
8438
8439    /// The number of known paths for the connection.
8440    pub paths_count: usize,
8441
8442    /// The number of streams reset by local.
8443    pub reset_stream_count_local: u64,
8444
8445    /// The number of streams stopped by local.
8446    pub stopped_stream_count_local: u64,
8447
8448    /// The number of streams reset by remote.
8449    pub reset_stream_count_remote: u64,
8450
8451    /// The number of streams stopped by remote.
8452    pub stopped_stream_count_remote: u64,
8453
8454    /// The total number of PATH_CHALLENGE frames that were received.
8455    pub path_challenge_rx_count: u64,
8456}
8457
8458impl std::fmt::Debug for Stats {
8459    #[inline]
8460    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8461        write!(
8462            f,
8463            "recv={} sent={} lost={} retrans={}",
8464            self.recv, self.sent, self.lost, self.retrans,
8465        )?;
8466
8467        write!(
8468            f,
8469            " sent_bytes={} recv_bytes={} lost_bytes={}",
8470            self.sent_bytes, self.recv_bytes, self.lost_bytes,
8471        )?;
8472
8473        Ok(())
8474    }
8475}
8476
8477/// QUIC Unknown Transport Parameter.
8478///
8479/// A QUIC transport parameter that is not specifically recognized
8480/// by this implementation.
8481#[derive(Clone, Debug, PartialEq)]
8482pub struct UnknownTransportParameter<T> {
8483    /// The ID of the unknown transport parameter.
8484    pub id: u64,
8485
8486    /// Original data representing the value of the unknown transport parameter.
8487    pub value: T,
8488}
8489
8490impl<T> UnknownTransportParameter<T> {
8491    /// Checks whether an unknown Transport Parameter's ID is in the reserved
8492    /// space.
8493    ///
8494    /// See Section 18.1 in [RFC9000](https://datatracker.ietf.org/doc/html/rfc9000#name-reserved-transport-paramete).
8495    pub fn is_reserved(&self) -> bool {
8496        let n = (self.id - 27) / 31;
8497        self.id == 31 * n + 27
8498    }
8499}
8500
8501#[cfg(feature = "qlog")]
8502impl From<UnknownTransportParameter<Vec<u8>>>
8503    for qlog::events::quic::UnknownTransportParameter
8504{
8505    fn from(value: UnknownTransportParameter<Vec<u8>>) -> Self {
8506        Self {
8507            id: value.id,
8508            value: qlog::HexSlice::maybe_string(Some(value.value.as_slice()))
8509                .unwrap_or_default(),
8510        }
8511    }
8512}
8513
8514impl From<UnknownTransportParameter<&[u8]>>
8515    for UnknownTransportParameter<Vec<u8>>
8516{
8517    // When an instance of an UnknownTransportParameter is actually
8518    // stored in UnknownTransportParameters, then we make a copy
8519    // of the bytes if the source is an instance of an UnknownTransportParameter
8520    // whose value is not owned.
8521    fn from(value: UnknownTransportParameter<&[u8]>) -> Self {
8522        Self {
8523            id: value.id,
8524            value: value.value.to_vec(),
8525        }
8526    }
8527}
8528
8529/// Track unknown transport parameters, up to a limit.
8530#[derive(Clone, Debug, PartialEq, Default)]
8531pub struct UnknownTransportParameters {
8532    /// The space remaining for storing unknown transport parameters.
8533    pub capacity: usize,
8534    /// The unknown transport parameters.
8535    pub parameters: Vec<UnknownTransportParameter<Vec<u8>>>,
8536}
8537
8538impl UnknownTransportParameters {
8539    /// Pushes an unknown transport parameter into storage if there is space
8540    /// remaining.
8541    pub fn push(&mut self, new: UnknownTransportParameter<&[u8]>) -> Result<()> {
8542        let new_unknown_tp_size = new.value.len() + std::mem::size_of::<u64>();
8543        if new_unknown_tp_size < self.capacity {
8544            self.capacity -= new_unknown_tp_size;
8545            self.parameters.push(new.into());
8546            Ok(())
8547        } else {
8548            Err(BufferTooShortError.into())
8549        }
8550    }
8551}
8552
8553/// An Iterator over unknown transport parameters.
8554pub struct UnknownTransportParameterIterator<'a> {
8555    index: usize,
8556    parameters: &'a Vec<UnknownTransportParameter<Vec<u8>>>,
8557}
8558
8559impl<'a> IntoIterator for &'a UnknownTransportParameters {
8560    type IntoIter = UnknownTransportParameterIterator<'a>;
8561    type Item = &'a UnknownTransportParameter<Vec<u8>>;
8562
8563    fn into_iter(self) -> Self::IntoIter {
8564        UnknownTransportParameterIterator {
8565            index: 0,
8566            parameters: &self.parameters,
8567        }
8568    }
8569}
8570
8571impl<'a> Iterator for UnknownTransportParameterIterator<'a> {
8572    type Item = &'a UnknownTransportParameter<Vec<u8>>;
8573
8574    fn next(&mut self) -> Option<Self::Item> {
8575        let result = self.parameters.get(self.index);
8576        self.index += 1;
8577        result
8578    }
8579}
8580
8581/// QUIC Transport Parameters
8582#[derive(Clone, Debug, PartialEq)]
8583pub struct TransportParams {
8584    /// Value of Destination CID field from first Initial packet sent by client
8585    pub original_destination_connection_id: Option<ConnectionId<'static>>,
8586    /// The maximum idle timeout.
8587    pub max_idle_timeout: u64,
8588    /// Token used for verifying stateless resets
8589    pub stateless_reset_token: Option<u128>,
8590    /// The maximum UDP payload size.
8591    pub max_udp_payload_size: u64,
8592    /// The initial flow control maximum data for the connection.
8593    pub initial_max_data: u64,
8594    /// The initial flow control maximum data for local bidirectional streams.
8595    pub initial_max_stream_data_bidi_local: u64,
8596    /// The initial flow control maximum data for remote bidirectional streams.
8597    pub initial_max_stream_data_bidi_remote: u64,
8598    /// The initial flow control maximum data for unidirectional streams.
8599    pub initial_max_stream_data_uni: u64,
8600    /// The initial maximum bidirectional streams.
8601    pub initial_max_streams_bidi: u64,
8602    /// The initial maximum unidirectional streams.
8603    pub initial_max_streams_uni: u64,
8604    /// The ACK delay exponent.
8605    pub ack_delay_exponent: u64,
8606    /// The max ACK delay.
8607    pub max_ack_delay: u64,
8608    /// Whether active migration is disabled.
8609    pub disable_active_migration: bool,
8610    /// The active connection ID limit.
8611    pub active_conn_id_limit: u64,
8612    /// The value that the endpoint included in the Source CID field of a Retry
8613    /// Packet.
8614    pub initial_source_connection_id: Option<ConnectionId<'static>>,
8615    /// The value that the server included in the Source CID field of a Retry
8616    /// Packet.
8617    pub retry_source_connection_id: Option<ConnectionId<'static>>,
8618    /// DATAGRAM frame extension parameter, if any.
8619    pub max_datagram_frame_size: Option<u64>,
8620    /// Unknown peer transport parameters and values, if any.
8621    pub unknown_params: Option<UnknownTransportParameters>,
8622    // pub preferred_address: ...,
8623}
8624
8625impl Default for TransportParams {
8626    fn default() -> TransportParams {
8627        TransportParams {
8628            original_destination_connection_id: None,
8629            max_idle_timeout: 0,
8630            stateless_reset_token: None,
8631            max_udp_payload_size: 65527,
8632            initial_max_data: 0,
8633            initial_max_stream_data_bidi_local: 0,
8634            initial_max_stream_data_bidi_remote: 0,
8635            initial_max_stream_data_uni: 0,
8636            initial_max_streams_bidi: 0,
8637            initial_max_streams_uni: 0,
8638            ack_delay_exponent: 3,
8639            max_ack_delay: 25,
8640            disable_active_migration: false,
8641            active_conn_id_limit: 2,
8642            initial_source_connection_id: None,
8643            retry_source_connection_id: None,
8644            max_datagram_frame_size: None,
8645            unknown_params: Default::default(),
8646        }
8647    }
8648}
8649
8650impl TransportParams {
8651    fn decode(
8652        buf: &[u8], is_server: bool, unknown_size: Option<usize>,
8653    ) -> Result<TransportParams> {
8654        let mut params = octets::Octets::with_slice(buf);
8655        let mut seen_params = HashSet::new();
8656
8657        let mut tp = TransportParams::default();
8658
8659        if let Some(unknown_transport_param_tracking_size) = unknown_size {
8660            tp.unknown_params = Some(UnknownTransportParameters {
8661                capacity: unknown_transport_param_tracking_size,
8662                parameters: vec![],
8663            });
8664        }
8665
8666        while params.cap() > 0 {
8667            let id = params.get_varint()?;
8668
8669            if seen_params.contains(&id) {
8670                return Err(Error::InvalidTransportParam);
8671            }
8672            seen_params.insert(id);
8673
8674            let mut val = params.get_bytes_with_varint_length()?;
8675
8676            match id {
8677                0x0000 => {
8678                    if is_server {
8679                        return Err(Error::InvalidTransportParam);
8680                    }
8681
8682                    tp.original_destination_connection_id =
8683                        Some(val.to_vec().into());
8684                },
8685
8686                0x0001 => {
8687                    tp.max_idle_timeout = val.get_varint()?;
8688                },
8689
8690                0x0002 => {
8691                    if is_server {
8692                        return Err(Error::InvalidTransportParam);
8693                    }
8694
8695                    tp.stateless_reset_token = Some(u128::from_be_bytes(
8696                        val.get_bytes(16)?
8697                            .to_vec()
8698                            .try_into()
8699                            .map_err(|_| Error::BufferTooShort)?,
8700                    ));
8701                },
8702
8703                0x0003 => {
8704                    tp.max_udp_payload_size = val.get_varint()?;
8705
8706                    if tp.max_udp_payload_size < 1200 {
8707                        return Err(Error::InvalidTransportParam);
8708                    }
8709                },
8710
8711                0x0004 => {
8712                    tp.initial_max_data = val.get_varint()?;
8713                },
8714
8715                0x0005 => {
8716                    tp.initial_max_stream_data_bidi_local = val.get_varint()?;
8717                },
8718
8719                0x0006 => {
8720                    tp.initial_max_stream_data_bidi_remote = val.get_varint()?;
8721                },
8722
8723                0x0007 => {
8724                    tp.initial_max_stream_data_uni = val.get_varint()?;
8725                },
8726
8727                0x0008 => {
8728                    let max = val.get_varint()?;
8729
8730                    if max > MAX_STREAM_ID {
8731                        return Err(Error::InvalidTransportParam);
8732                    }
8733
8734                    tp.initial_max_streams_bidi = max;
8735                },
8736
8737                0x0009 => {
8738                    let max = val.get_varint()?;
8739
8740                    if max > MAX_STREAM_ID {
8741                        return Err(Error::InvalidTransportParam);
8742                    }
8743
8744                    tp.initial_max_streams_uni = max;
8745                },
8746
8747                0x000a => {
8748                    let ack_delay_exponent = val.get_varint()?;
8749
8750                    if ack_delay_exponent > 20 {
8751                        return Err(Error::InvalidTransportParam);
8752                    }
8753
8754                    tp.ack_delay_exponent = ack_delay_exponent;
8755                },
8756
8757                0x000b => {
8758                    let max_ack_delay = val.get_varint()?;
8759
8760                    if max_ack_delay >= 2_u64.pow(14) {
8761                        return Err(Error::InvalidTransportParam);
8762                    }
8763
8764                    tp.max_ack_delay = max_ack_delay;
8765                },
8766
8767                0x000c => {
8768                    tp.disable_active_migration = true;
8769                },
8770
8771                0x000d => {
8772                    if is_server {
8773                        return Err(Error::InvalidTransportParam);
8774                    }
8775
8776                    // TODO: decode preferred_address
8777                },
8778
8779                0x000e => {
8780                    let limit = val.get_varint()?;
8781
8782                    if limit < 2 {
8783                        return Err(Error::InvalidTransportParam);
8784                    }
8785
8786                    tp.active_conn_id_limit = limit;
8787                },
8788
8789                0x000f => {
8790                    tp.initial_source_connection_id = Some(val.to_vec().into());
8791                },
8792
8793                0x00010 => {
8794                    if is_server {
8795                        return Err(Error::InvalidTransportParam);
8796                    }
8797
8798                    tp.retry_source_connection_id = Some(val.to_vec().into());
8799                },
8800
8801                0x0020 => {
8802                    tp.max_datagram_frame_size = Some(val.get_varint()?);
8803                },
8804
8805                // Track unknown transport parameters specially.
8806                unknown_tp_id => {
8807                    if let Some(unknown_params) = &mut tp.unknown_params {
8808                        // It is _not_ an error not to have space enough to track
8809                        // an unknown parameter.
8810                        let _ = unknown_params.push(UnknownTransportParameter {
8811                            id: unknown_tp_id,
8812                            value: val.buf(),
8813                        });
8814                    }
8815                },
8816            }
8817        }
8818
8819        Ok(tp)
8820    }
8821
8822    fn encode_param(
8823        b: &mut octets::OctetsMut, ty: u64, len: usize,
8824    ) -> Result<()> {
8825        b.put_varint(ty)?;
8826        b.put_varint(len as u64)?;
8827
8828        Ok(())
8829    }
8830
8831    fn encode<'a>(
8832        tp: &TransportParams, is_server: bool, out: &'a mut [u8],
8833    ) -> Result<&'a mut [u8]> {
8834        let mut b = octets::OctetsMut::with_slice(out);
8835
8836        if is_server {
8837            if let Some(ref odcid) = tp.original_destination_connection_id {
8838                TransportParams::encode_param(&mut b, 0x0000, odcid.len())?;
8839                b.put_bytes(odcid)?;
8840            }
8841        };
8842
8843        if tp.max_idle_timeout != 0 {
8844            TransportParams::encode_param(
8845                &mut b,
8846                0x0001,
8847                octets::varint_len(tp.max_idle_timeout),
8848            )?;
8849            b.put_varint(tp.max_idle_timeout)?;
8850        }
8851
8852        if is_server {
8853            if let Some(ref token) = tp.stateless_reset_token {
8854                TransportParams::encode_param(&mut b, 0x0002, 16)?;
8855                b.put_bytes(&token.to_be_bytes())?;
8856            }
8857        }
8858
8859        if tp.max_udp_payload_size != 0 {
8860            TransportParams::encode_param(
8861                &mut b,
8862                0x0003,
8863                octets::varint_len(tp.max_udp_payload_size),
8864            )?;
8865            b.put_varint(tp.max_udp_payload_size)?;
8866        }
8867
8868        if tp.initial_max_data != 0 {
8869            TransportParams::encode_param(
8870                &mut b,
8871                0x0004,
8872                octets::varint_len(tp.initial_max_data),
8873            )?;
8874            b.put_varint(tp.initial_max_data)?;
8875        }
8876
8877        if tp.initial_max_stream_data_bidi_local != 0 {
8878            TransportParams::encode_param(
8879                &mut b,
8880                0x0005,
8881                octets::varint_len(tp.initial_max_stream_data_bidi_local),
8882            )?;
8883            b.put_varint(tp.initial_max_stream_data_bidi_local)?;
8884        }
8885
8886        if tp.initial_max_stream_data_bidi_remote != 0 {
8887            TransportParams::encode_param(
8888                &mut b,
8889                0x0006,
8890                octets::varint_len(tp.initial_max_stream_data_bidi_remote),
8891            )?;
8892            b.put_varint(tp.initial_max_stream_data_bidi_remote)?;
8893        }
8894
8895        if tp.initial_max_stream_data_uni != 0 {
8896            TransportParams::encode_param(
8897                &mut b,
8898                0x0007,
8899                octets::varint_len(tp.initial_max_stream_data_uni),
8900            )?;
8901            b.put_varint(tp.initial_max_stream_data_uni)?;
8902        }
8903
8904        if tp.initial_max_streams_bidi != 0 {
8905            TransportParams::encode_param(
8906                &mut b,
8907                0x0008,
8908                octets::varint_len(tp.initial_max_streams_bidi),
8909            )?;
8910            b.put_varint(tp.initial_max_streams_bidi)?;
8911        }
8912
8913        if tp.initial_max_streams_uni != 0 {
8914            TransportParams::encode_param(
8915                &mut b,
8916                0x0009,
8917                octets::varint_len(tp.initial_max_streams_uni),
8918            )?;
8919            b.put_varint(tp.initial_max_streams_uni)?;
8920        }
8921
8922        if tp.ack_delay_exponent != 0 {
8923            TransportParams::encode_param(
8924                &mut b,
8925                0x000a,
8926                octets::varint_len(tp.ack_delay_exponent),
8927            )?;
8928            b.put_varint(tp.ack_delay_exponent)?;
8929        }
8930
8931        if tp.max_ack_delay != 0 {
8932            TransportParams::encode_param(
8933                &mut b,
8934                0x000b,
8935                octets::varint_len(tp.max_ack_delay),
8936            )?;
8937            b.put_varint(tp.max_ack_delay)?;
8938        }
8939
8940        if tp.disable_active_migration {
8941            TransportParams::encode_param(&mut b, 0x000c, 0)?;
8942        }
8943
8944        // TODO: encode preferred_address
8945
8946        if tp.active_conn_id_limit != 2 {
8947            TransportParams::encode_param(
8948                &mut b,
8949                0x000e,
8950                octets::varint_len(tp.active_conn_id_limit),
8951            )?;
8952            b.put_varint(tp.active_conn_id_limit)?;
8953        }
8954
8955        if let Some(scid) = &tp.initial_source_connection_id {
8956            TransportParams::encode_param(&mut b, 0x000f, scid.len())?;
8957            b.put_bytes(scid)?;
8958        }
8959
8960        if is_server {
8961            if let Some(scid) = &tp.retry_source_connection_id {
8962                TransportParams::encode_param(&mut b, 0x0010, scid.len())?;
8963                b.put_bytes(scid)?;
8964            }
8965        }
8966
8967        if let Some(max_datagram_frame_size) = tp.max_datagram_frame_size {
8968            TransportParams::encode_param(
8969                &mut b,
8970                0x0020,
8971                octets::varint_len(max_datagram_frame_size),
8972            )?;
8973            b.put_varint(max_datagram_frame_size)?;
8974        }
8975
8976        let out_len = b.off();
8977
8978        Ok(&mut out[..out_len])
8979    }
8980
8981    /// Creates a qlog event for connection transport parameters and TLS fields
8982    #[cfg(feature = "qlog")]
8983    pub fn to_qlog(
8984        &self, owner: TransportOwner, cipher: Option<crypto::Algorithm>,
8985    ) -> EventData {
8986        let original_destination_connection_id = qlog::HexSlice::maybe_string(
8987            self.original_destination_connection_id.as_ref(),
8988        );
8989
8990        let stateless_reset_token = qlog::HexSlice::maybe_string(
8991            self.stateless_reset_token.map(|s| s.to_be_bytes()).as_ref(),
8992        );
8993
8994        let tls_cipher: Option<String> = cipher.map(|f| format!("{f:?}"));
8995
8996        EventData::TransportParametersSet(
8997            qlog::events::quic::TransportParametersSet {
8998                owner: Some(owner),
8999                tls_cipher,
9000                original_destination_connection_id,
9001                stateless_reset_token,
9002                disable_active_migration: Some(self.disable_active_migration),
9003                max_idle_timeout: Some(self.max_idle_timeout),
9004                max_udp_payload_size: Some(self.max_udp_payload_size as u32),
9005                ack_delay_exponent: Some(self.ack_delay_exponent as u16),
9006                max_ack_delay: Some(self.max_ack_delay as u16),
9007                active_connection_id_limit: Some(
9008                    self.active_conn_id_limit as u32,
9009                ),
9010
9011                initial_max_data: Some(self.initial_max_data),
9012                initial_max_stream_data_bidi_local: Some(
9013                    self.initial_max_stream_data_bidi_local,
9014                ),
9015                initial_max_stream_data_bidi_remote: Some(
9016                    self.initial_max_stream_data_bidi_remote,
9017                ),
9018                initial_max_stream_data_uni: Some(
9019                    self.initial_max_stream_data_uni,
9020                ),
9021                initial_max_streams_bidi: Some(self.initial_max_streams_bidi),
9022                initial_max_streams_uni: Some(self.initial_max_streams_uni),
9023
9024                unknown_parameters: self
9025                    .unknown_params
9026                    .as_ref()
9027                    .map(|unknown_params| {
9028                        unknown_params
9029                            .into_iter()
9030                            .cloned()
9031                            .map(
9032                                Into::<
9033                                    qlog::events::quic::UnknownTransportParameter,
9034                                >::into,
9035                            )
9036                            .collect()
9037                    })
9038                    .unwrap_or_default(),
9039
9040                ..Default::default()
9041            },
9042        )
9043    }
9044}
9045
9046#[doc(hidden)]
9047pub mod testing {
9048    use super::*;
9049    use crate::recovery::Sent;
9050    use smallvec::smallvec;
9051    use std::time::Instant;
9052
9053    pub struct Pipe {
9054        pub client: Connection,
9055        pub server: Connection,
9056    }
9057
9058    impl Pipe {
9059        pub fn new(cc_algorithm_name: &str) -> Result<Pipe> {
9060            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
9061            assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
9062            config.load_cert_chain_from_pem_file("examples/cert.crt")?;
9063            config.load_priv_key_from_pem_file("examples/cert.key")?;
9064            config.set_application_protos(&[b"proto1", b"proto2"])?;
9065            config.set_initial_max_data(30);
9066            config.set_initial_max_stream_data_bidi_local(15);
9067            config.set_initial_max_stream_data_bidi_remote(15);
9068            config.set_initial_max_stream_data_uni(10);
9069            config.set_initial_max_streams_bidi(3);
9070            config.set_initial_max_streams_uni(3);
9071            config.set_max_idle_timeout(180_000);
9072            config.verify_peer(false);
9073            config.set_ack_delay_exponent(8);
9074
9075            Pipe::with_config(&mut config)
9076        }
9077
9078        pub fn client_addr() -> SocketAddr {
9079            "127.0.0.1:1234".parse().unwrap()
9080        }
9081
9082        pub fn server_addr() -> SocketAddr {
9083            "127.0.0.1:4321".parse().unwrap()
9084        }
9085
9086        pub fn with_config(config: &mut Config) -> Result<Pipe> {
9087            let mut client_scid = [0; 16];
9088            rand::rand_bytes(&mut client_scid[..]);
9089            let client_scid = ConnectionId::from_ref(&client_scid);
9090            let client_addr = Pipe::client_addr();
9091
9092            let mut server_scid = [0; 16];
9093            rand::rand_bytes(&mut server_scid[..]);
9094            let server_scid = ConnectionId::from_ref(&server_scid);
9095            let server_addr = Pipe::server_addr();
9096
9097            Ok(Pipe {
9098                client: connect(
9099                    Some("quic.tech"),
9100                    &client_scid,
9101                    client_addr,
9102                    server_addr,
9103                    config,
9104                )?,
9105                server: accept(
9106                    &server_scid,
9107                    None,
9108                    server_addr,
9109                    client_addr,
9110                    config,
9111                )?,
9112            })
9113        }
9114
9115        pub fn with_config_and_scid_lengths(
9116            config: &mut Config, client_scid_len: usize, server_scid_len: usize,
9117        ) -> Result<Pipe> {
9118            let mut client_scid = vec![0; client_scid_len];
9119            rand::rand_bytes(&mut client_scid[..]);
9120            let client_scid = ConnectionId::from_ref(&client_scid);
9121            let client_addr = Pipe::client_addr();
9122
9123            let mut server_scid = vec![0; server_scid_len];
9124            rand::rand_bytes(&mut server_scid[..]);
9125            let server_scid = ConnectionId::from_ref(&server_scid);
9126            let server_addr = Pipe::server_addr();
9127
9128            Ok(Pipe {
9129                client: connect(
9130                    Some("quic.tech"),
9131                    &client_scid,
9132                    client_addr,
9133                    server_addr,
9134                    config,
9135                )?,
9136                server: accept(
9137                    &server_scid,
9138                    None,
9139                    server_addr,
9140                    client_addr,
9141                    config,
9142                )?,
9143            })
9144        }
9145
9146        pub fn with_client_config(client_config: &mut Config) -> Result<Pipe> {
9147            let mut client_scid = [0; 16];
9148            rand::rand_bytes(&mut client_scid[..]);
9149            let client_scid = ConnectionId::from_ref(&client_scid);
9150            let client_addr = Pipe::client_addr();
9151
9152            let mut server_scid = [0; 16];
9153            rand::rand_bytes(&mut server_scid[..]);
9154            let server_scid = ConnectionId::from_ref(&server_scid);
9155            let server_addr = Pipe::server_addr();
9156
9157            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
9158            config.load_cert_chain_from_pem_file("examples/cert.crt")?;
9159            config.load_priv_key_from_pem_file("examples/cert.key")?;
9160            config.set_application_protos(&[b"proto1", b"proto2"])?;
9161            config.set_initial_max_data(30);
9162            config.set_initial_max_stream_data_bidi_local(15);
9163            config.set_initial_max_stream_data_bidi_remote(15);
9164            config.set_initial_max_streams_bidi(3);
9165            config.set_initial_max_streams_uni(3);
9166            config.set_ack_delay_exponent(8);
9167
9168            Ok(Pipe {
9169                client: connect(
9170                    Some("quic.tech"),
9171                    &client_scid,
9172                    client_addr,
9173                    server_addr,
9174                    client_config,
9175                )?,
9176                server: accept(
9177                    &server_scid,
9178                    None,
9179                    server_addr,
9180                    client_addr,
9181                    &mut config,
9182                )?,
9183            })
9184        }
9185
9186        pub fn with_server_config(server_config: &mut Config) -> Result<Pipe> {
9187            let mut client_scid = [0; 16];
9188            rand::rand_bytes(&mut client_scid[..]);
9189            let client_scid = ConnectionId::from_ref(&client_scid);
9190            let client_addr = Pipe::client_addr();
9191
9192            let mut server_scid = [0; 16];
9193            rand::rand_bytes(&mut server_scid[..]);
9194            let server_scid = ConnectionId::from_ref(&server_scid);
9195            let server_addr = Pipe::server_addr();
9196
9197            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
9198            config.set_application_protos(&[b"proto1", b"proto2"])?;
9199            config.set_initial_max_data(30);
9200            config.set_initial_max_stream_data_bidi_local(15);
9201            config.set_initial_max_stream_data_bidi_remote(15);
9202            config.set_initial_max_streams_bidi(3);
9203            config.set_initial_max_streams_uni(3);
9204            config.set_ack_delay_exponent(8);
9205
9206            Ok(Pipe {
9207                client: connect(
9208                    Some("quic.tech"),
9209                    &client_scid,
9210                    client_addr,
9211                    server_addr,
9212                    &mut config,
9213                )?,
9214                server: accept(
9215                    &server_scid,
9216                    None,
9217                    server_addr,
9218                    client_addr,
9219                    server_config,
9220                )?,
9221            })
9222        }
9223
9224        pub fn with_client_and_server_config(
9225            client_config: &mut Config, server_config: &mut Config,
9226        ) -> Result<Pipe> {
9227            let mut client_scid = [0; 16];
9228            rand::rand_bytes(&mut client_scid[..]);
9229            let client_scid = ConnectionId::from_ref(&client_scid);
9230            let client_addr = Pipe::client_addr();
9231
9232            let mut server_scid = [0; 16];
9233            rand::rand_bytes(&mut server_scid[..]);
9234            let server_scid = ConnectionId::from_ref(&server_scid);
9235            let server_addr = Pipe::server_addr();
9236
9237            Ok(Pipe {
9238                client: connect(
9239                    Some("quic.tech"),
9240                    &client_scid,
9241                    client_addr,
9242                    server_addr,
9243                    client_config,
9244                )?,
9245                server: accept(
9246                    &server_scid,
9247                    None,
9248                    server_addr,
9249                    client_addr,
9250                    server_config,
9251                )?,
9252            })
9253        }
9254
9255        pub fn handshake(&mut self) -> Result<()> {
9256            while !self.client.is_established() || !self.server.is_established() {
9257                let flight = emit_flight(&mut self.client)?;
9258                process_flight(&mut self.server, flight)?;
9259
9260                let flight = emit_flight(&mut self.server)?;
9261                process_flight(&mut self.client, flight)?;
9262            }
9263
9264            Ok(())
9265        }
9266
9267        pub fn advance(&mut self) -> Result<()> {
9268            let mut client_done = false;
9269            let mut server_done = false;
9270
9271            while !client_done || !server_done {
9272                match emit_flight(&mut self.client) {
9273                    Ok(flight) => process_flight(&mut self.server, flight)?,
9274
9275                    Err(Error::Done) => client_done = true,
9276
9277                    Err(e) => return Err(e),
9278                };
9279
9280                match emit_flight(&mut self.server) {
9281                    Ok(flight) => process_flight(&mut self.client, flight)?,
9282
9283                    Err(Error::Done) => server_done = true,
9284
9285                    Err(e) => return Err(e),
9286                };
9287            }
9288
9289            Ok(())
9290        }
9291
9292        pub fn client_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
9293            let server_path = &self.server.paths.get_active().unwrap();
9294            let info = RecvInfo {
9295                to: server_path.peer_addr(),
9296                from: server_path.local_addr(),
9297            };
9298
9299            self.client.recv(buf, info)
9300        }
9301
9302        pub fn server_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
9303            let client_path = &self.client.paths.get_active().unwrap();
9304            let info = RecvInfo {
9305                to: client_path.peer_addr(),
9306                from: client_path.local_addr(),
9307            };
9308
9309            self.server.recv(buf, info)
9310        }
9311
9312        pub fn send_pkt_to_server(
9313            &mut self, pkt_type: packet::Type, frames: &[frame::Frame],
9314            buf: &mut [u8],
9315        ) -> Result<usize> {
9316            let written = encode_pkt(&mut self.client, pkt_type, frames, buf)?;
9317            recv_send(&mut self.server, buf, written)
9318        }
9319
9320        pub fn client_update_key(&mut self) -> Result<()> {
9321            let crypto_ctx =
9322                &mut self.client.crypto_ctx[packet::Epoch::Application];
9323
9324            let open_next = crypto_ctx
9325                .crypto_open
9326                .as_ref()
9327                .unwrap()
9328                .derive_next_packet_key()
9329                .unwrap();
9330
9331            let seal_next = crypto_ctx
9332                .crypto_seal
9333                .as_ref()
9334                .unwrap()
9335                .derive_next_packet_key()?;
9336
9337            let open_prev = crypto_ctx.crypto_open.replace(open_next);
9338            crypto_ctx.crypto_seal.replace(seal_next);
9339
9340            crypto_ctx.key_update = Some(packet::KeyUpdate {
9341                crypto_open: open_prev.unwrap(),
9342                pn_on_update: self.client.next_pkt_num,
9343                update_acked: true,
9344                timer: time::Instant::now(),
9345            });
9346
9347            self.client.key_phase = !self.client.key_phase;
9348
9349            Ok(())
9350        }
9351    }
9352
9353    pub fn recv_send<F: BufFactory>(
9354        conn: &mut Connection<F>, buf: &mut [u8], len: usize,
9355    ) -> Result<usize> {
9356        let active_path = conn.paths.get_active()?;
9357        let info = RecvInfo {
9358            to: active_path.local_addr(),
9359            from: active_path.peer_addr(),
9360        };
9361
9362        conn.recv(&mut buf[..len], info)?;
9363
9364        let mut off = 0;
9365
9366        match conn.send(&mut buf[off..]) {
9367            Ok((write, _)) => off += write,
9368
9369            Err(Error::Done) => (),
9370
9371            Err(e) => return Err(e),
9372        }
9373
9374        Ok(off)
9375    }
9376
9377    pub fn process_flight(
9378        conn: &mut Connection, flight: Vec<(Vec<u8>, SendInfo)>,
9379    ) -> Result<()> {
9380        for (mut pkt, si) in flight {
9381            let info = RecvInfo {
9382                to: si.to,
9383                from: si.from,
9384            };
9385
9386            conn.recv(&mut pkt, info)?;
9387        }
9388
9389        Ok(())
9390    }
9391
9392    pub fn emit_flight_with_max_buffer(
9393        conn: &mut Connection, out_size: usize, from: Option<SocketAddr>,
9394        to: Option<SocketAddr>,
9395    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
9396        let mut flight = Vec::new();
9397
9398        loop {
9399            let mut out = vec![0u8; out_size];
9400
9401            let info = match conn.send_on_path(&mut out, from, to) {
9402                Ok((written, info)) => {
9403                    out.truncate(written);
9404                    info
9405                },
9406
9407                Err(Error::Done) => break,
9408
9409                Err(e) => return Err(e),
9410            };
9411
9412            flight.push((out, info));
9413        }
9414
9415        if flight.is_empty() {
9416            return Err(Error::Done);
9417        }
9418
9419        Ok(flight)
9420    }
9421
9422    pub fn emit_flight_on_path(
9423        conn: &mut Connection, from: Option<SocketAddr>, to: Option<SocketAddr>,
9424    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
9425        emit_flight_with_max_buffer(conn, 65535, from, to)
9426    }
9427
9428    pub fn emit_flight(
9429        conn: &mut Connection,
9430    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
9431        emit_flight_on_path(conn, None, None)
9432    }
9433
9434    pub fn encode_pkt(
9435        conn: &mut Connection, pkt_type: packet::Type, frames: &[frame::Frame],
9436        buf: &mut [u8],
9437    ) -> Result<usize> {
9438        let mut b = octets::OctetsMut::with_slice(buf);
9439
9440        let epoch = pkt_type.to_epoch()?;
9441
9442        let crypto_ctx = &mut conn.crypto_ctx[epoch];
9443
9444        let pn = conn.next_pkt_num;
9445        let pn_len = 4;
9446
9447        let send_path = conn.paths.get_active()?;
9448        let active_dcid_seq = send_path
9449            .active_dcid_seq
9450            .as_ref()
9451            .ok_or(Error::InvalidState)?;
9452        let active_scid_seq = send_path
9453            .active_scid_seq
9454            .as_ref()
9455            .ok_or(Error::InvalidState)?;
9456
9457        let hdr = Header {
9458            ty: pkt_type,
9459            version: conn.version,
9460            dcid: ConnectionId::from_ref(
9461                conn.ids.get_dcid(*active_dcid_seq)?.cid.as_ref(),
9462            ),
9463            scid: ConnectionId::from_ref(
9464                conn.ids.get_scid(*active_scid_seq)?.cid.as_ref(),
9465            ),
9466            pkt_num: pn,
9467            pkt_num_len: pn_len,
9468            token: conn.token.clone(),
9469            versions: None,
9470            key_phase: conn.key_phase,
9471        };
9472
9473        hdr.to_bytes(&mut b)?;
9474
9475        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
9476
9477        if pkt_type != packet::Type::Short {
9478            let len =
9479                pn_len + payload_len + crypto_ctx.crypto_overhead().unwrap();
9480            b.put_varint(len as u64)?;
9481        }
9482
9483        // Always encode packet number in 4 bytes, to allow encoding packets
9484        // with empty payloads.
9485        b.put_u32(pn as u32)?;
9486
9487        let payload_offset = b.off();
9488
9489        for frame in frames {
9490            frame.to_bytes(&mut b)?;
9491        }
9492
9493        let aead = match crypto_ctx.crypto_seal {
9494            Some(ref v) => v,
9495            None => return Err(Error::InvalidState),
9496        };
9497
9498        let written = packet::encrypt_pkt(
9499            &mut b,
9500            pn,
9501            pn_len,
9502            payload_len,
9503            payload_offset,
9504            None,
9505            aead,
9506        )?;
9507
9508        conn.next_pkt_num += 1;
9509
9510        Ok(written)
9511    }
9512
9513    pub fn decode_pkt(
9514        conn: &mut Connection, buf: &mut [u8],
9515    ) -> Result<Vec<frame::Frame>> {
9516        let mut b = octets::OctetsMut::with_slice(buf);
9517
9518        let mut hdr = Header::from_bytes(&mut b, conn.source_id().len()).unwrap();
9519
9520        let epoch = hdr.ty.to_epoch()?;
9521
9522        let aead = conn.crypto_ctx[epoch].crypto_open.as_ref().unwrap();
9523
9524        let payload_len = b.cap();
9525
9526        packet::decrypt_hdr(&mut b, &mut hdr, aead).unwrap();
9527
9528        let pn = packet::decode_pkt_num(
9529            conn.pkt_num_spaces[epoch].largest_rx_pkt_num,
9530            hdr.pkt_num,
9531            hdr.pkt_num_len,
9532        );
9533
9534        let mut payload =
9535            packet::decrypt_pkt(&mut b, pn, hdr.pkt_num_len, payload_len, aead)
9536                .unwrap();
9537
9538        let mut frames = Vec::new();
9539
9540        while payload.cap() > 0 {
9541            let frame = frame::Frame::from_bytes(&mut payload, hdr.ty)?;
9542            frames.push(frame);
9543        }
9544
9545        Ok(frames)
9546    }
9547
9548    pub fn create_cid_and_reset_token(
9549        cid_len: usize,
9550    ) -> (ConnectionId<'static>, u128) {
9551        let mut cid = vec![0; cid_len];
9552        rand::rand_bytes(&mut cid[..]);
9553        let cid = ConnectionId::from_ref(&cid).into_owned();
9554
9555        let mut reset_token = [0; 16];
9556        rand::rand_bytes(&mut reset_token);
9557        let reset_token = u128::from_be_bytes(reset_token);
9558
9559        (cid, reset_token)
9560    }
9561
9562    pub fn helper_packet_sent(pkt_num: u64, now: Instant, size: usize) -> Sent {
9563        Sent {
9564            pkt_num,
9565            frames: smallvec![],
9566            time_sent: now,
9567            time_acked: None,
9568            time_lost: None,
9569            size,
9570            ack_eliciting: true,
9571            in_flight: true,
9572            delivered: 0,
9573            delivered_time: now,
9574            first_sent_time: now,
9575            is_app_limited: false,
9576            tx_in_flight: 0,
9577            lost: 0,
9578            has_data: false,
9579            pmtud: false,
9580        }
9581    }
9582}
9583
9584#[cfg(test)]
9585mod tests {
9586    use crate::range_buf::RangeBuf;
9587    use rstest::rstest;
9588
9589    use super::*;
9590
9591    #[test]
9592    fn transport_params() {
9593        // Server encodes, client decodes.
9594        let tp = TransportParams {
9595            original_destination_connection_id: None,
9596            max_idle_timeout: 30,
9597            stateless_reset_token: Some(u128::from_be_bytes([0xba; 16])),
9598            max_udp_payload_size: 23_421,
9599            initial_max_data: 424_645_563,
9600            initial_max_stream_data_bidi_local: 154_323_123,
9601            initial_max_stream_data_bidi_remote: 6_587_456,
9602            initial_max_stream_data_uni: 2_461_234,
9603            initial_max_streams_bidi: 12_231,
9604            initial_max_streams_uni: 18_473,
9605            ack_delay_exponent: 20,
9606            max_ack_delay: 2_u64.pow(14) - 1,
9607            disable_active_migration: true,
9608            active_conn_id_limit: 8,
9609            initial_source_connection_id: Some(b"woot woot".to_vec().into()),
9610            retry_source_connection_id: Some(b"retry".to_vec().into()),
9611            max_datagram_frame_size: Some(32),
9612            unknown_params: Default::default(),
9613        };
9614
9615        let mut raw_params = [42; 256];
9616        let raw_params =
9617            TransportParams::encode(&tp, true, &mut raw_params).unwrap();
9618        assert_eq!(raw_params.len(), 94);
9619
9620        let new_tp = TransportParams::decode(raw_params, false, None).unwrap();
9621
9622        assert_eq!(new_tp, tp);
9623
9624        // Client encodes, server decodes.
9625        let tp = TransportParams {
9626            original_destination_connection_id: None,
9627            max_idle_timeout: 30,
9628            stateless_reset_token: None,
9629            max_udp_payload_size: 23_421,
9630            initial_max_data: 424_645_563,
9631            initial_max_stream_data_bidi_local: 154_323_123,
9632            initial_max_stream_data_bidi_remote: 6_587_456,
9633            initial_max_stream_data_uni: 2_461_234,
9634            initial_max_streams_bidi: 12_231,
9635            initial_max_streams_uni: 18_473,
9636            ack_delay_exponent: 20,
9637            max_ack_delay: 2_u64.pow(14) - 1,
9638            disable_active_migration: true,
9639            active_conn_id_limit: 8,
9640            initial_source_connection_id: Some(b"woot woot".to_vec().into()),
9641            retry_source_connection_id: None,
9642            max_datagram_frame_size: Some(32),
9643            unknown_params: Default::default(),
9644        };
9645
9646        let mut raw_params = [42; 256];
9647        let raw_params =
9648            TransportParams::encode(&tp, false, &mut raw_params).unwrap();
9649        assert_eq!(raw_params.len(), 69);
9650
9651        let new_tp = TransportParams::decode(raw_params, true, None).unwrap();
9652
9653        assert_eq!(new_tp, tp);
9654    }
9655
9656    #[test]
9657    fn transport_params_forbid_duplicates() {
9658        // Given an encoded param.
9659        let initial_source_connection_id = b"id";
9660        let initial_source_connection_id_raw = [
9661            15,
9662            initial_source_connection_id.len() as u8,
9663            initial_source_connection_id[0],
9664            initial_source_connection_id[1],
9665        ];
9666
9667        // No error when decoding the param.
9668        let tp = TransportParams::decode(
9669            initial_source_connection_id_raw.as_slice(),
9670            true,
9671            None,
9672        )
9673        .unwrap();
9674
9675        assert_eq!(
9676            tp.initial_source_connection_id,
9677            Some(initial_source_connection_id.to_vec().into())
9678        );
9679
9680        // Duplicate the param.
9681        let mut raw_params = Vec::new();
9682        raw_params.append(&mut initial_source_connection_id_raw.to_vec());
9683        raw_params.append(&mut initial_source_connection_id_raw.to_vec());
9684
9685        // Decoding fails.
9686        assert_eq!(
9687            TransportParams::decode(raw_params.as_slice(), true, None),
9688            Err(Error::InvalidTransportParam)
9689        );
9690    }
9691
9692    #[test]
9693    fn transport_params_unknown_zero_space() {
9694        let mut unknown_params: UnknownTransportParameters =
9695            UnknownTransportParameters {
9696                capacity: 0,
9697                parameters: vec![],
9698            };
9699        let massive_unknown_param = UnknownTransportParameter::<&[u8]> {
9700            id: 5,
9701            value: &[0xau8; 280],
9702        };
9703        assert!(unknown_params.push(massive_unknown_param).is_err());
9704        assert!(unknown_params.capacity == 0);
9705        assert!(unknown_params.parameters.is_empty());
9706    }
9707
9708    #[test]
9709    fn transport_params_unknown_max_space_respected() {
9710        let mut unknown_params: UnknownTransportParameters =
9711            UnknownTransportParameters {
9712                capacity: 256,
9713                parameters: vec![],
9714            };
9715
9716        let massive_unknown_param = UnknownTransportParameter::<&[u8]> {
9717            id: 5,
9718            value: &[0xau8; 280],
9719        };
9720        let big_unknown_param = UnknownTransportParameter::<&[u8]> {
9721            id: 5,
9722            value: &[0xau8; 232],
9723        };
9724        let little_unknown_param = UnknownTransportParameter::<&[u8]> {
9725            id: 6,
9726            value: &[0xau8; 7],
9727        };
9728
9729        assert!(unknown_params.push(massive_unknown_param).is_err());
9730        assert!(unknown_params.capacity == 256);
9731        assert!(unknown_params.parameters.is_empty());
9732
9733        unknown_params.push(big_unknown_param).unwrap();
9734        assert!(unknown_params.capacity == 16);
9735        assert!(unknown_params.parameters.len() == 1);
9736
9737        unknown_params.push(little_unknown_param.clone()).unwrap();
9738        assert!(unknown_params.capacity == 1);
9739        assert!(unknown_params.parameters.len() == 2);
9740
9741        assert!(unknown_params.push(little_unknown_param).is_err());
9742
9743        let mut unknown_params_iter = unknown_params.into_iter();
9744
9745        let unknown_params_first = unknown_params_iter
9746            .next()
9747            .expect("Should have a 0th element.");
9748        assert!(
9749            unknown_params_first.id == 5 &&
9750                unknown_params_first.value == vec![0xau8; 232]
9751        );
9752
9753        let unknown_params_second = unknown_params_iter
9754            .next()
9755            .expect("Should have a 1th element.");
9756        assert!(
9757            unknown_params_second.id == 6 &&
9758                unknown_params_second.value == vec![0xau8; 7]
9759        );
9760    }
9761
9762    #[test]
9763    fn transport_params_unknown_is_reserved() {
9764        let reserved_unknown_param = UnknownTransportParameter::<&[u8]> {
9765            id: 31 * 17 + 27,
9766            value: &[0xau8; 280],
9767        };
9768        let not_reserved_unknown_param = UnknownTransportParameter::<&[u8]> {
9769            id: 32 * 17 + 27,
9770            value: &[0xau8; 280],
9771        };
9772
9773        assert!(reserved_unknown_param.is_reserved());
9774        assert!(!not_reserved_unknown_param.is_reserved());
9775    }
9776    #[test]
9777    fn unknown_version() {
9778        let mut config = Config::new(0xbabababa).unwrap();
9779        config
9780            .set_application_protos(&[b"proto1", b"proto2"])
9781            .unwrap();
9782        config.verify_peer(false);
9783
9784        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9785        assert_eq!(pipe.handshake(), Err(Error::UnknownVersion));
9786    }
9787
9788    #[test]
9789    fn config_version_reserved() {
9790        Config::new(0xbabababa).unwrap();
9791        Config::new(0x1a2a3a4a).unwrap();
9792    }
9793
9794    #[test]
9795    fn config_version_invalid() {
9796        assert_eq!(
9797            Config::new(0xb1bababa).err().unwrap(),
9798            Error::UnknownVersion
9799        );
9800    }
9801
9802    #[test]
9803    fn version_negotiation() {
9804        let mut buf = [0; 65535];
9805
9806        let mut config = Config::new(0xbabababa).unwrap();
9807        config
9808            .set_application_protos(&[b"proto1", b"proto2"])
9809            .unwrap();
9810        config.verify_peer(false);
9811
9812        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9813
9814        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
9815
9816        let hdr = packet::Header::from_slice(&mut buf[..len], 0).unwrap();
9817        len = crate::negotiate_version(&hdr.scid, &hdr.dcid, &mut buf).unwrap();
9818
9819        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
9820
9821        assert_eq!(pipe.handshake(), Ok(()));
9822
9823        assert_eq!(pipe.client.version, PROTOCOL_VERSION);
9824        assert_eq!(pipe.server.version, PROTOCOL_VERSION);
9825    }
9826
9827    #[test]
9828    fn verify_custom_root() {
9829        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
9830        config.verify_peer(true);
9831        config
9832            .load_verify_locations_from_file("examples/rootca.crt")
9833            .unwrap();
9834        config
9835            .set_application_protos(&[b"proto1", b"proto2"])
9836            .unwrap();
9837
9838        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9839        assert_eq!(pipe.handshake(), Ok(()));
9840    }
9841
9842    // Disable this for openssl as it seems to fail for some reason. It could be
9843    // because of the way the get_certs API differs from bssl.
9844    #[cfg(not(feature = "openssl"))]
9845    #[test]
9846    fn verify_client_invalid() {
9847        let mut server_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9848        server_config
9849            .load_cert_chain_from_pem_file("examples/cert.crt")
9850            .unwrap();
9851        server_config
9852            .load_priv_key_from_pem_file("examples/cert.key")
9853            .unwrap();
9854        server_config
9855            .set_application_protos(&[b"proto1", b"proto2"])
9856            .unwrap();
9857        server_config.set_initial_max_data(30);
9858        server_config.set_initial_max_stream_data_bidi_local(15);
9859        server_config.set_initial_max_stream_data_bidi_remote(15);
9860        server_config.set_initial_max_streams_bidi(3);
9861
9862        // The server shouldn't be able to verify the client's certificate due
9863        // to missing CA.
9864        server_config.verify_peer(true);
9865
9866        let mut client_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9867        client_config
9868            .load_cert_chain_from_pem_file("examples/cert.crt")
9869            .unwrap();
9870        client_config
9871            .load_priv_key_from_pem_file("examples/cert.key")
9872            .unwrap();
9873        client_config
9874            .set_application_protos(&[b"proto1", b"proto2"])
9875            .unwrap();
9876        client_config.set_initial_max_data(30);
9877        client_config.set_initial_max_stream_data_bidi_local(15);
9878        client_config.set_initial_max_stream_data_bidi_remote(15);
9879        client_config.set_initial_max_streams_bidi(3);
9880
9881        // The client is able to verify the server's certificate with the
9882        // appropriate CA.
9883        client_config
9884            .load_verify_locations_from_file("examples/rootca.crt")
9885            .unwrap();
9886        client_config.verify_peer(true);
9887
9888        let mut pipe = testing::Pipe::with_client_and_server_config(
9889            &mut client_config,
9890            &mut server_config,
9891        )
9892        .unwrap();
9893        assert_eq!(pipe.handshake(), Err(Error::TlsFail));
9894
9895        // Client did send a certificate.
9896        assert!(pipe.server.peer_cert().is_some());
9897    }
9898
9899    #[test]
9900    fn verify_client_anonymous() {
9901        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9902        config
9903            .load_cert_chain_from_pem_file("examples/cert.crt")
9904            .unwrap();
9905        config
9906            .load_priv_key_from_pem_file("examples/cert.key")
9907            .unwrap();
9908        config
9909            .set_application_protos(&[b"proto1", b"proto2"])
9910            .unwrap();
9911        config.set_initial_max_data(30);
9912        config.set_initial_max_stream_data_bidi_local(15);
9913        config.set_initial_max_stream_data_bidi_remote(15);
9914        config.set_initial_max_streams_bidi(3);
9915
9916        // Try to validate client certificate.
9917        config.verify_peer(true);
9918
9919        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
9920        assert_eq!(pipe.handshake(), Ok(()));
9921
9922        // Client didn't send a certificate.
9923        assert!(pipe.server.peer_cert().is_none());
9924    }
9925
9926    #[rstest]
9927    fn missing_initial_source_connection_id(
9928        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
9929    ) {
9930        let mut buf = [0; 65535];
9931
9932        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
9933
9934        // Reset initial_source_connection_id.
9935        pipe.client
9936            .local_transport_params
9937            .initial_source_connection_id = None;
9938        assert_eq!(pipe.client.encode_transport_params(), Ok(()));
9939
9940        // Client sends initial flight.
9941        let (len, _) = pipe.client.send(&mut buf).unwrap();
9942
9943        // Server rejects transport parameters.
9944        assert_eq!(
9945            pipe.server_recv(&mut buf[..len]),
9946            Err(Error::InvalidTransportParam)
9947        );
9948    }
9949
9950    #[rstest]
9951    fn invalid_initial_source_connection_id(
9952        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
9953    ) {
9954        let mut buf = [0; 65535];
9955
9956        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
9957
9958        // Scramble initial_source_connection_id.
9959        pipe.client
9960            .local_transport_params
9961            .initial_source_connection_id = Some(b"bogus value".to_vec().into());
9962        assert_eq!(pipe.client.encode_transport_params(), Ok(()));
9963
9964        // Client sends initial flight.
9965        let (len, _) = pipe.client.send(&mut buf).unwrap();
9966
9967        // Server rejects transport parameters.
9968        assert_eq!(
9969            pipe.server_recv(&mut buf[..len]),
9970            Err(Error::InvalidTransportParam)
9971        );
9972    }
9973
9974    #[rstest]
9975    fn change_idle_timeout(
9976        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
9977    ) {
9978        let mut config = Config::new(0x1).unwrap();
9979        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
9980        config
9981            .set_application_protos(&[b"proto1", b"proto2"])
9982            .unwrap();
9983        config.set_max_idle_timeout(999999);
9984        config.verify_peer(false);
9985
9986        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9987        assert_eq!(pipe.client.local_transport_params.max_idle_timeout, 999999);
9988        assert_eq!(pipe.client.peer_transport_params.max_idle_timeout, 0);
9989        assert_eq!(pipe.server.local_transport_params.max_idle_timeout, 0);
9990        assert_eq!(pipe.server.peer_transport_params.max_idle_timeout, 0);
9991
9992        pipe.client.set_max_idle_timeout(456000).unwrap();
9993        pipe.server.set_max_idle_timeout(234000).unwrap();
9994        assert_eq!(pipe.client.local_transport_params.max_idle_timeout, 456000);
9995        assert_eq!(pipe.client.peer_transport_params.max_idle_timeout, 0);
9996        assert_eq!(pipe.server.local_transport_params.max_idle_timeout, 234000);
9997        assert_eq!(pipe.server.peer_transport_params.max_idle_timeout, 0);
9998
9999        assert_eq!(pipe.handshake(), Ok(()));
10000
10001        assert_eq!(
10002            pipe.client.idle_timeout(),
10003            Some(time::Duration::from_millis(234000))
10004        );
10005        assert_eq!(
10006            pipe.server.idle_timeout(),
10007            Some(time::Duration::from_millis(234000))
10008        );
10009    }
10010
10011    #[rstest]
10012    fn handshake(
10013        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10014    ) {
10015        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10016        assert_eq!(pipe.handshake(), Ok(()));
10017
10018        assert_eq!(
10019            pipe.client.application_proto(),
10020            pipe.server.application_proto()
10021        );
10022
10023        assert_eq!(pipe.server.server_name(), Some("quic.tech"));
10024    }
10025
10026    #[rstest]
10027    fn handshake_done(
10028        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10029    ) {
10030        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10031
10032        // Disable session tickets on the server (SSL_OP_NO_TICKET) to avoid
10033        // triggering 1-RTT packet send with a CRYPTO frame.
10034        pipe.server.handshake.set_options(0x0000_4000);
10035
10036        assert_eq!(pipe.handshake(), Ok(()));
10037
10038        assert!(pipe.server.handshake_done_sent);
10039    }
10040
10041    #[rstest]
10042    fn handshake_confirmation(
10043        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10044    ) {
10045        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10046
10047        // Client sends initial flight.
10048        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10049        testing::process_flight(&mut pipe.server, flight).unwrap();
10050
10051        // Server sends initial flight.
10052        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10053
10054        assert!(!pipe.client.is_established());
10055        assert!(!pipe.client.handshake_confirmed);
10056
10057        assert!(!pipe.server.is_established());
10058        assert!(!pipe.server.handshake_confirmed);
10059
10060        testing::process_flight(&mut pipe.client, flight).unwrap();
10061
10062        // Client sends Handshake packet and completes handshake.
10063        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10064
10065        assert!(pipe.client.is_established());
10066        assert!(!pipe.client.handshake_confirmed);
10067
10068        assert!(!pipe.server.is_established());
10069        assert!(!pipe.server.handshake_confirmed);
10070
10071        testing::process_flight(&mut pipe.server, flight).unwrap();
10072
10073        // Server completes and confirms handshake, and sends HANDSHAKE_DONE.
10074        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10075
10076        assert!(pipe.client.is_established());
10077        assert!(!pipe.client.handshake_confirmed);
10078
10079        assert!(pipe.server.is_established());
10080        assert!(pipe.server.handshake_confirmed);
10081
10082        testing::process_flight(&mut pipe.client, flight).unwrap();
10083
10084        // Client acks 1-RTT packet, and confirms handshake.
10085        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10086
10087        assert!(pipe.client.is_established());
10088        assert!(pipe.client.handshake_confirmed);
10089
10090        assert!(pipe.server.is_established());
10091        assert!(pipe.server.handshake_confirmed);
10092
10093        testing::process_flight(&mut pipe.server, flight).unwrap();
10094
10095        assert!(pipe.client.is_established());
10096        assert!(pipe.client.handshake_confirmed);
10097
10098        assert!(pipe.server.is_established());
10099        assert!(pipe.server.handshake_confirmed);
10100    }
10101
10102    #[rstest]
10103    fn handshake_resumption(
10104        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10105    ) {
10106        #[cfg(not(feature = "openssl"))]
10107        const SESSION_TICKET_KEY: [u8; 48] = [0xa; 48];
10108
10109        // 80-byte key(AES 256)
10110        // TODO: We can set the default? or query the ticket size by calling
10111        // the same API(SSL_CTX_set_tlsext_ticket_keys) twice to fetch the size.
10112        #[cfg(feature = "openssl")]
10113        const SESSION_TICKET_KEY: [u8; 80] = [0xa; 80];
10114
10115        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10116        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10117
10118        config
10119            .load_cert_chain_from_pem_file("examples/cert.crt")
10120            .unwrap();
10121        config
10122            .load_priv_key_from_pem_file("examples/cert.key")
10123            .unwrap();
10124        config
10125            .set_application_protos(&[b"proto1", b"proto2"])
10126            .unwrap();
10127        config.set_initial_max_data(30);
10128        config.set_initial_max_stream_data_bidi_local(15);
10129        config.set_initial_max_stream_data_bidi_remote(15);
10130        config.set_initial_max_streams_bidi(3);
10131        config.set_ticket_key(&SESSION_TICKET_KEY).unwrap();
10132
10133        // Perform initial handshake.
10134        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10135        assert_eq!(pipe.handshake(), Ok(()));
10136
10137        assert!(pipe.client.is_established());
10138        assert!(pipe.server.is_established());
10139
10140        assert!(!pipe.client.is_resumed());
10141        assert!(!pipe.server.is_resumed());
10142
10143        // Extract session,
10144        let session = pipe.client.session().unwrap();
10145
10146        // Configure session on new connection and perform handshake.
10147        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10148        config
10149            .load_cert_chain_from_pem_file("examples/cert.crt")
10150            .unwrap();
10151        config
10152            .load_priv_key_from_pem_file("examples/cert.key")
10153            .unwrap();
10154        config
10155            .set_application_protos(&[b"proto1", b"proto2"])
10156            .unwrap();
10157        config.set_initial_max_data(30);
10158        config.set_initial_max_stream_data_bidi_local(15);
10159        config.set_initial_max_stream_data_bidi_remote(15);
10160        config.set_initial_max_streams_bidi(3);
10161        config.set_ticket_key(&SESSION_TICKET_KEY).unwrap();
10162
10163        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10164
10165        assert_eq!(pipe.client.set_session(session), Ok(()));
10166        assert_eq!(pipe.handshake(), Ok(()));
10167
10168        assert!(pipe.client.is_established());
10169        assert!(pipe.server.is_established());
10170
10171        assert!(pipe.client.is_resumed());
10172        assert!(pipe.server.is_resumed());
10173    }
10174
10175    #[rstest]
10176    fn handshake_alpn_mismatch(
10177        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10178    ) {
10179        let mut buf = [0; 65535];
10180
10181        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
10182        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10183        config
10184            .set_application_protos(&[b"proto3\x06proto4"])
10185            .unwrap();
10186        config.verify_peer(false);
10187
10188        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
10189        assert_eq!(pipe.handshake(), Err(Error::TlsFail));
10190
10191        assert_eq!(pipe.client.application_proto(), b"");
10192        assert_eq!(pipe.server.application_proto(), b"");
10193
10194        // Server should only send one packet in response to ALPN mismatch.
10195        let (len, _) = pipe.server.send(&mut buf).unwrap();
10196        assert_eq!(len, 1200);
10197
10198        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
10199        assert_eq!(pipe.server.sent_count, 1);
10200    }
10201
10202    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
10203    #[rstest]
10204    fn handshake_0rtt(
10205        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10206    ) {
10207        let mut buf = [0; 65535];
10208
10209        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10210        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10211        config
10212            .load_cert_chain_from_pem_file("examples/cert.crt")
10213            .unwrap();
10214        config
10215            .load_priv_key_from_pem_file("examples/cert.key")
10216            .unwrap();
10217        config
10218            .set_application_protos(&[b"proto1", b"proto2"])
10219            .unwrap();
10220        config.set_initial_max_data(30);
10221        config.set_initial_max_stream_data_bidi_local(15);
10222        config.set_initial_max_stream_data_bidi_remote(15);
10223        config.set_initial_max_streams_bidi(3);
10224        config.enable_early_data();
10225        config.verify_peer(false);
10226
10227        // Perform initial handshake.
10228        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10229        assert_eq!(pipe.handshake(), Ok(()));
10230
10231        // Extract session,
10232        let session = pipe.client.session().unwrap();
10233
10234        // Configure session on new connection.
10235        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10236        assert_eq!(pipe.client.set_session(session), Ok(()));
10237
10238        // Client sends initial flight.
10239        let (len, _) = pipe.client.send(&mut buf).unwrap();
10240        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
10241
10242        // Client sends 0-RTT packet.
10243        let pkt_type = packet::Type::ZeroRTT;
10244
10245        let frames = [frame::Frame::Stream {
10246            stream_id: 4,
10247            data: <RangeBuf>::from(b"aaaaa", 0, true),
10248        }];
10249
10250        assert_eq!(
10251            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10252            Ok(1200)
10253        );
10254
10255        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
10256
10257        // 0-RTT stream data is readable.
10258        let mut r = pipe.server.readable();
10259        assert_eq!(r.next(), Some(4));
10260        assert_eq!(r.next(), None);
10261
10262        let mut b = [0; 15];
10263        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
10264        assert_eq!(&b[..5], b"aaaaa");
10265    }
10266
10267    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
10268    #[rstest]
10269    fn handshake_0rtt_reordered(
10270        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10271    ) {
10272        let mut buf = [0; 65535];
10273
10274        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10275        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10276        config
10277            .load_cert_chain_from_pem_file("examples/cert.crt")
10278            .unwrap();
10279        config
10280            .load_priv_key_from_pem_file("examples/cert.key")
10281            .unwrap();
10282        config
10283            .set_application_protos(&[b"proto1", b"proto2"])
10284            .unwrap();
10285        config.set_initial_max_data(30);
10286        config.set_initial_max_stream_data_bidi_local(15);
10287        config.set_initial_max_stream_data_bidi_remote(15);
10288        config.set_initial_max_streams_bidi(3);
10289        config.enable_early_data();
10290        config.verify_peer(false);
10291
10292        // Perform initial handshake.
10293        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10294        assert_eq!(pipe.handshake(), Ok(()));
10295
10296        // Extract session,
10297        let session = pipe.client.session().unwrap();
10298
10299        // Configure session on new connection.
10300        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10301        assert_eq!(pipe.client.set_session(session), Ok(()));
10302
10303        // Client sends initial flight.
10304        let (len, _) = pipe.client.send(&mut buf).unwrap();
10305        let mut initial = buf[..len].to_vec();
10306
10307        // Client sends 0-RTT packet.
10308        let pkt_type = packet::Type::ZeroRTT;
10309
10310        let frames = [frame::Frame::Stream {
10311            stream_id: 4,
10312            data: <RangeBuf>::from(b"aaaaa", 0, true),
10313        }];
10314
10315        let len =
10316            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
10317                .unwrap();
10318        let mut zrtt = buf[..len].to_vec();
10319
10320        // 0-RTT packet is received before the Initial one.
10321        assert_eq!(pipe.server_recv(&mut zrtt), Ok(zrtt.len()));
10322
10323        assert_eq!(pipe.server.undecryptable_pkts.len(), 1);
10324        assert_eq!(pipe.server.undecryptable_pkts[0].0.len(), zrtt.len());
10325
10326        let mut r = pipe.server.readable();
10327        assert_eq!(r.next(), None);
10328
10329        // Initial packet is also received.
10330        assert_eq!(pipe.server_recv(&mut initial), Ok(initial.len()));
10331
10332        // 0-RTT stream data is readable.
10333        let mut r = pipe.server.readable();
10334        assert_eq!(r.next(), Some(4));
10335        assert_eq!(r.next(), None);
10336
10337        let mut b = [0; 15];
10338        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
10339        assert_eq!(&b[..5], b"aaaaa");
10340    }
10341
10342    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
10343    #[rstest]
10344    fn handshake_0rtt_truncated(
10345        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10346    ) {
10347        let mut buf = [0; 65535];
10348
10349        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10350        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10351        config
10352            .load_cert_chain_from_pem_file("examples/cert.crt")
10353            .unwrap();
10354        config
10355            .load_priv_key_from_pem_file("examples/cert.key")
10356            .unwrap();
10357        config
10358            .set_application_protos(&[b"proto1", b"proto2"])
10359            .unwrap();
10360        config.set_initial_max_data(30);
10361        config.set_initial_max_stream_data_bidi_local(15);
10362        config.set_initial_max_stream_data_bidi_remote(15);
10363        config.set_initial_max_streams_bidi(3);
10364        config.enable_early_data();
10365        config.verify_peer(false);
10366
10367        // Perform initial handshake.
10368        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10369        assert_eq!(pipe.handshake(), Ok(()));
10370
10371        // Extract session,
10372        let session = pipe.client.session().unwrap();
10373
10374        // Configure session on new connection.
10375        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10376        assert_eq!(pipe.client.set_session(session), Ok(()));
10377
10378        // Client sends initial flight.
10379        pipe.client.send(&mut buf).unwrap();
10380
10381        // Client sends 0-RTT packet.
10382        let pkt_type = packet::Type::ZeroRTT;
10383
10384        let frames = [frame::Frame::Stream {
10385            stream_id: 4,
10386            data: <RangeBuf>::from(b"aaaaa", 0, true),
10387        }];
10388
10389        let len =
10390            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
10391                .unwrap();
10392
10393        // Simulate a truncated packet by sending one byte less.
10394        let mut zrtt = buf[..len - 1].to_vec();
10395
10396        // 0-RTT packet is received before the Initial one.
10397        assert_eq!(pipe.server_recv(&mut zrtt), Err(Error::InvalidPacket));
10398
10399        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
10400
10401        assert!(pipe.server.is_closed());
10402    }
10403
10404    #[rstest]
10405    fn crypto_limit(
10406        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10407    ) {
10408        let mut buf = [0; 65535];
10409
10410        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10411        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10412        config
10413            .load_cert_chain_from_pem_file("examples/cert.crt")
10414            .unwrap();
10415        config
10416            .load_priv_key_from_pem_file("examples/cert.key")
10417            .unwrap();
10418        config
10419            .set_application_protos(&[b"proto1", b"proto2"])
10420            .unwrap();
10421        config.set_initial_max_data(30);
10422        config.set_initial_max_stream_data_bidi_local(15);
10423        config.set_initial_max_stream_data_bidi_remote(15);
10424        config.set_initial_max_streams_bidi(3);
10425        config.enable_early_data();
10426        config.verify_peer(false);
10427
10428        // Perform initial handshake.
10429        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10430        assert_eq!(pipe.handshake(), Ok(()));
10431
10432        // Client send a 1-byte frame that starts from the crypto stream offset
10433        // limit.
10434        let frames = [frame::Frame::Crypto {
10435            data: RangeBuf::from(b"a", MAX_CRYPTO_STREAM_OFFSET, false),
10436        }];
10437
10438        let pkt_type = packet::Type::Short;
10439
10440        let written =
10441            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
10442                .unwrap();
10443
10444        let active_path = pipe.server.paths.get_active().unwrap();
10445        let info = RecvInfo {
10446            to: active_path.local_addr(),
10447            from: active_path.peer_addr(),
10448        };
10449
10450        assert_eq!(
10451            pipe.server.recv(&mut buf[..written], info),
10452            Err(Error::CryptoBufferExceeded)
10453        );
10454
10455        let written = match pipe.server.send(&mut buf) {
10456            Ok((write, _)) => write,
10457
10458            Err(_) => unreachable!(),
10459        };
10460
10461        let frames =
10462            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
10463        let mut iter = frames.iter();
10464
10465        assert_eq!(
10466            iter.next(),
10467            Some(&frame::Frame::ConnectionClose {
10468                error_code: 0x0d,
10469                frame_type: 0,
10470                reason: Vec::new(),
10471            })
10472        );
10473    }
10474
10475    #[rstest]
10476    fn limit_handshake_data(
10477        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10478    ) {
10479        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
10480        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10481        config
10482            .load_cert_chain_from_pem_file("examples/cert-big.crt")
10483            .unwrap();
10484        config
10485            .load_priv_key_from_pem_file("examples/cert.key")
10486            .unwrap();
10487        config
10488            .set_application_protos(&[b"proto1", b"proto2"])
10489            .unwrap();
10490
10491        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10492
10493        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10494        let client_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10495        testing::process_flight(&mut pipe.server, flight).unwrap();
10496
10497        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10498        let server_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10499
10500        assert_eq!(server_sent, client_sent * MAX_AMPLIFICATION_FACTOR);
10501    }
10502
10503    #[rstest]
10504    fn custom_limit_handshake_data(
10505        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10506    ) {
10507        const CUSTOM_AMPLIFICATION_FACTOR: usize = 2;
10508
10509        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
10510        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10511        config
10512            .load_cert_chain_from_pem_file("examples/cert-big.crt")
10513            .unwrap();
10514        config
10515            .load_priv_key_from_pem_file("examples/cert.key")
10516            .unwrap();
10517        config
10518            .set_application_protos(&[b"proto1", b"proto2"])
10519            .unwrap();
10520        config.set_max_amplification_factor(CUSTOM_AMPLIFICATION_FACTOR);
10521
10522        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10523
10524        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10525        let client_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10526        testing::process_flight(&mut pipe.server, flight).unwrap();
10527
10528        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10529        let server_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10530
10531        assert_eq!(server_sent, client_sent * CUSTOM_AMPLIFICATION_FACTOR);
10532    }
10533
10534    #[rstest]
10535    fn streamio(
10536        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10537    ) {
10538        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10539        assert_eq!(pipe.handshake(), Ok(()));
10540
10541        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10542        assert_eq!(pipe.advance(), Ok(()));
10543
10544        assert!(!pipe.server.stream_finished(4));
10545
10546        let mut r = pipe.server.readable();
10547        assert_eq!(r.next(), Some(4));
10548        assert_eq!(r.next(), None);
10549
10550        let mut b = [0; 15];
10551        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((12, true)));
10552        assert_eq!(&b[..12], b"hello, world");
10553
10554        assert!(pipe.server.stream_finished(4));
10555    }
10556
10557    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
10558    #[rstest]
10559    fn zero_rtt(
10560        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10561    ) {
10562        let mut buf = [0; 65535];
10563
10564        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10565        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10566        config
10567            .load_cert_chain_from_pem_file("examples/cert.crt")
10568            .unwrap();
10569        config
10570            .load_priv_key_from_pem_file("examples/cert.key")
10571            .unwrap();
10572        config
10573            .set_application_protos(&[b"proto1", b"proto2"])
10574            .unwrap();
10575        config.set_initial_max_data(30);
10576        config.set_initial_max_stream_data_bidi_local(15);
10577        config.set_initial_max_stream_data_bidi_remote(15);
10578        config.set_initial_max_streams_bidi(3);
10579        config.enable_early_data();
10580        config.verify_peer(false);
10581
10582        // Perform initial handshake.
10583        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10584        assert_eq!(pipe.handshake(), Ok(()));
10585
10586        // Extract session,
10587        let session = pipe.client.session().unwrap();
10588
10589        // Configure session on new connection.
10590        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10591        assert_eq!(pipe.client.set_session(session), Ok(()));
10592
10593        // Client sends initial flight.
10594        let (len, _) = pipe.client.send(&mut buf).unwrap();
10595        let mut initial = buf[..len].to_vec();
10596
10597        assert!(pipe.client.is_in_early_data());
10598
10599        // Client sends 0-RTT data.
10600        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10601
10602        let (len, _) = pipe.client.send(&mut buf).unwrap();
10603        let mut zrtt = buf[..len].to_vec();
10604
10605        // Server receives packets.
10606        assert_eq!(pipe.server_recv(&mut initial), Ok(initial.len()));
10607        assert!(pipe.server.is_in_early_data());
10608
10609        assert_eq!(pipe.server_recv(&mut zrtt), Ok(zrtt.len()));
10610
10611        // 0-RTT stream data is readable.
10612        let mut r = pipe.server.readable();
10613        assert_eq!(r.next(), Some(4));
10614        assert_eq!(r.next(), None);
10615
10616        let mut b = [0; 15];
10617        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((12, true)));
10618        assert_eq!(&b[..12], b"hello, world");
10619    }
10620
10621    #[rstest]
10622    fn stream_send_on_32bit_arch(
10623        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10624    ) {
10625        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10626        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
10627        config
10628            .load_cert_chain_from_pem_file("examples/cert.crt")
10629            .unwrap();
10630        config
10631            .load_priv_key_from_pem_file("examples/cert.key")
10632            .unwrap();
10633        config
10634            .set_application_protos(&[b"proto1", b"proto2"])
10635            .unwrap();
10636        config.set_initial_max_data(2_u64.pow(32) + 5);
10637        config.set_initial_max_stream_data_bidi_local(15);
10638        config.set_initial_max_stream_data_bidi_remote(15);
10639        config.set_initial_max_stream_data_uni(10);
10640        config.set_initial_max_streams_bidi(3);
10641        config.set_initial_max_streams_uni(0);
10642        config.verify_peer(false);
10643
10644        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10645        assert_eq!(pipe.handshake(), Ok(()));
10646
10647        // In 32bit arch, send_capacity() should be min(2^32+5, cwnd),
10648        // not min(5, cwnd)
10649        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10650
10651        assert_eq!(pipe.advance(), Ok(()));
10652
10653        assert!(!pipe.server.stream_finished(4));
10654    }
10655
10656    #[rstest]
10657    fn empty_stream_frame(
10658        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10659    ) {
10660        let mut buf = [0; 65535];
10661
10662        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10663        assert_eq!(pipe.handshake(), Ok(()));
10664
10665        let frames = [frame::Frame::Stream {
10666            stream_id: 4,
10667            data: <RangeBuf>::from(b"aaaaa", 0, false),
10668        }];
10669
10670        let pkt_type = packet::Type::Short;
10671        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
10672
10673        let mut readable = pipe.server.readable();
10674        assert_eq!(readable.next(), Some(4));
10675
10676        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((5, false)));
10677
10678        let frames = [frame::Frame::Stream {
10679            stream_id: 4,
10680            data: <RangeBuf>::from(b"", 5, true),
10681        }];
10682
10683        let pkt_type = packet::Type::Short;
10684        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
10685
10686        let mut readable = pipe.server.readable();
10687        assert_eq!(readable.next(), Some(4));
10688
10689        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((0, true)));
10690
10691        let frames = [frame::Frame::Stream {
10692            stream_id: 4,
10693            data: <RangeBuf>::from(b"", 15, true),
10694        }];
10695
10696        let pkt_type = packet::Type::Short;
10697        assert_eq!(
10698            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10699            Err(Error::FinalSize)
10700        );
10701    }
10702
10703    #[rstest]
10704    fn update_key_request(
10705        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10706    ) {
10707        let mut b = [0; 15];
10708
10709        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10710        assert_eq!(pipe.handshake(), Ok(()));
10711        assert_eq!(pipe.advance(), Ok(()));
10712
10713        // Client sends message with key update request.
10714        assert_eq!(pipe.client_update_key(), Ok(()));
10715        assert_eq!(pipe.client.stream_send(4, b"hello", false), Ok(5));
10716        assert_eq!(pipe.advance(), Ok(()));
10717
10718        // Ensure server updates key and it correctly decrypts the message.
10719        let mut r = pipe.server.readable();
10720        assert_eq!(r.next(), Some(4));
10721        assert_eq!(r.next(), None);
10722        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, false)));
10723        assert_eq!(&b[..5], b"hello");
10724
10725        // Ensure ACK for key update.
10726        assert!(
10727            pipe.server.crypto_ctx[packet::Epoch::Application]
10728                .key_update
10729                .as_ref()
10730                .unwrap()
10731                .update_acked
10732        );
10733
10734        // Server sends message with the new key.
10735        assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
10736        assert_eq!(pipe.advance(), Ok(()));
10737
10738        // Ensure update key is completed and client can decrypt packet.
10739        let mut r = pipe.client.readable();
10740        assert_eq!(r.next(), Some(4));
10741        assert_eq!(r.next(), None);
10742        assert_eq!(pipe.client.stream_recv(4, &mut b), Ok((5, false)));
10743        assert_eq!(&b[..5], b"world");
10744
10745        // Server keeps sending packets to ensure encryption still works.
10746        for _ in 0..10 {
10747            assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
10748            assert_eq!(pipe.advance(), Ok(()));
10749
10750            let mut r = pipe.client.readable();
10751            assert_eq!(r.next(), Some(4));
10752            assert_eq!(r.next(), None);
10753            assert_eq!(pipe.client.stream_recv(4, &mut b), Ok((5, false)));
10754            assert_eq!(&b[..5], b"world");
10755        }
10756    }
10757
10758    #[rstest]
10759    fn update_key_request_twice_error(
10760        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10761    ) {
10762        let mut buf = [0; 65535];
10763
10764        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10765        assert_eq!(pipe.handshake(), Ok(()));
10766        assert_eq!(pipe.advance(), Ok(()));
10767
10768        let frames = [frame::Frame::Stream {
10769            stream_id: 4,
10770            data: <RangeBuf>::from(b"hello", 0, false),
10771        }];
10772
10773        // Client sends stream frame with key update request.
10774        assert_eq!(pipe.client_update_key(), Ok(()));
10775        let written = testing::encode_pkt(
10776            &mut pipe.client,
10777            packet::Type::Short,
10778            &frames,
10779            &mut buf,
10780        )
10781        .unwrap();
10782
10783        // Server correctly decode with new key.
10784        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
10785
10786        // Client sends stream frame with another key update request before server
10787        // ACK.
10788        assert_eq!(pipe.client_update_key(), Ok(()));
10789        let written = testing::encode_pkt(
10790            &mut pipe.client,
10791            packet::Type::Short,
10792            &frames,
10793            &mut buf,
10794        )
10795        .unwrap();
10796
10797        // Check server correctly closes the connection with a key update error
10798        // for the peer.
10799        assert_eq!(pipe.server_recv(&mut buf[..written]), Err(Error::KeyUpdate));
10800    }
10801
10802    #[rstest]
10803    /// Tests that receiving a MAX_STREAM_DATA frame for a receive-only
10804    /// unidirectional stream is forbidden.
10805    fn max_stream_data_receive_uni(
10806        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10807    ) {
10808        let mut buf = [0; 65535];
10809
10810        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10811        assert_eq!(pipe.handshake(), Ok(()));
10812
10813        // Client opens unidirectional stream.
10814        assert_eq!(pipe.client.stream_send(2, b"hello", false), Ok(5));
10815        assert_eq!(pipe.advance(), Ok(()));
10816
10817        // Client sends MAX_STREAM_DATA on local unidirectional stream.
10818        let frames = [frame::Frame::MaxStreamData {
10819            stream_id: 2,
10820            max: 1024,
10821        }];
10822
10823        let pkt_type = packet::Type::Short;
10824        assert_eq!(
10825            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10826            Err(Error::InvalidStreamState(2)),
10827        );
10828    }
10829
10830    #[rstest]
10831    fn empty_payload(
10832        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10833    ) {
10834        let mut buf = [0; 65535];
10835
10836        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10837        assert_eq!(pipe.handshake(), Ok(()));
10838
10839        // Send a packet with no frames.
10840        let pkt_type = packet::Type::Short;
10841        assert_eq!(
10842            pipe.send_pkt_to_server(pkt_type, &[], &mut buf),
10843            Err(Error::InvalidPacket)
10844        );
10845    }
10846
10847    #[rstest]
10848    fn min_payload(
10849        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10850    ) {
10851        let mut buf = [0; 65535];
10852
10853        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10854
10855        // Send a non-ack-eliciting packet.
10856        let frames = [frame::Frame::Padding { len: 4 }];
10857
10858        let pkt_type = packet::Type::Initial;
10859        let written =
10860            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
10861                .unwrap();
10862        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
10863
10864        let initial_path = pipe
10865            .server
10866            .paths
10867            .get_active()
10868            .expect("initial path not found");
10869
10870        assert_eq!(initial_path.max_send_bytes, 195);
10871
10872        // Force server to send a single PING frame.
10873        pipe.server
10874            .paths
10875            .get_active_mut()
10876            .expect("no active path")
10877            .recovery
10878            .inc_loss_probes(packet::Epoch::Initial);
10879
10880        let initial_path = pipe
10881            .server
10882            .paths
10883            .get_active_mut()
10884            .expect("initial path not found");
10885
10886        // Artificially limit the amount of bytes the server can send.
10887        initial_path.max_send_bytes = 60;
10888
10889        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
10890    }
10891
10892    #[rstest]
10893    fn flow_control_limit(
10894        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10895    ) {
10896        let mut buf = [0; 65535];
10897
10898        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10899        assert_eq!(pipe.handshake(), Ok(()));
10900
10901        let frames = [
10902            frame::Frame::Stream {
10903                stream_id: 0,
10904                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
10905            },
10906            frame::Frame::Stream {
10907                stream_id: 4,
10908                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
10909            },
10910            frame::Frame::Stream {
10911                stream_id: 8,
10912                data: <RangeBuf>::from(b"a", 0, false),
10913            },
10914        ];
10915
10916        let pkt_type = packet::Type::Short;
10917        assert_eq!(
10918            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10919            Err(Error::FlowControl),
10920        );
10921    }
10922
10923    #[rstest]
10924    fn flow_control_limit_dup(
10925        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10926    ) {
10927        let mut buf = [0; 65535];
10928
10929        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10930        assert_eq!(pipe.handshake(), Ok(()));
10931
10932        let frames = [
10933            // One byte less than stream limit.
10934            frame::Frame::Stream {
10935                stream_id: 0,
10936                data: <RangeBuf>::from(b"aaaaaaaaaaaaaa", 0, false),
10937            },
10938            // Same stream, but one byte more.
10939            frame::Frame::Stream {
10940                stream_id: 0,
10941                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
10942            },
10943            frame::Frame::Stream {
10944                stream_id: 8,
10945                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
10946            },
10947        ];
10948
10949        let pkt_type = packet::Type::Short;
10950        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10951    }
10952
10953    #[rstest]
10954    fn flow_control_update(
10955        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
10956    ) {
10957        let mut buf = [0; 65535];
10958
10959        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
10960        assert_eq!(pipe.handshake(), Ok(()));
10961
10962        let frames = [
10963            frame::Frame::Stream {
10964                stream_id: 0,
10965                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
10966            },
10967            frame::Frame::Stream {
10968                stream_id: 4,
10969                data: <RangeBuf>::from(b"a", 0, false),
10970            },
10971        ];
10972
10973        let pkt_type = packet::Type::Short;
10974
10975        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10976
10977        pipe.server.stream_recv(0, &mut buf).unwrap();
10978        pipe.server.stream_recv(4, &mut buf).unwrap();
10979
10980        let frames = [frame::Frame::Stream {
10981            stream_id: 4,
10982            data: <RangeBuf>::from(b"a", 1, false),
10983        }];
10984
10985        let len = pipe
10986            .send_pkt_to_server(pkt_type, &frames, &mut buf)
10987            .unwrap();
10988
10989        assert!(len > 0);
10990
10991        let frames =
10992            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
10993        let mut iter = frames.iter();
10994
10995        // Ignore ACK.
10996        iter.next().unwrap();
10997
10998        assert_eq!(
10999            iter.next(),
11000            Some(&frame::Frame::MaxStreamData {
11001                stream_id: 0,
11002                max: 30
11003            })
11004        );
11005        assert_eq!(iter.next(), Some(&frame::Frame::MaxData { max: 61 }));
11006    }
11007
11008    #[rstest]
11009    /// Tests that flow control is properly updated even when a stream is shut
11010    /// down.
11011    fn flow_control_drain(
11012        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11013    ) {
11014        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11015        assert_eq!(pipe.handshake(), Ok(()));
11016
11017        // Client opens a stream and sends some data.
11018        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
11019        assert_eq!(pipe.advance(), Ok(()));
11020
11021        // Server receives data, without reading it.
11022        let mut r = pipe.server.readable();
11023        assert_eq!(r.next(), Some(4));
11024        assert_eq!(r.next(), None);
11025
11026        // In the meantime, client sends more data.
11027        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
11028        assert_eq!(pipe.client.stream_send(4, b"aaaaa", true), Ok(5));
11029
11030        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
11031        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
11032        assert_eq!(pipe.client.stream_send(8, b"aaaaa", true), Ok(5));
11033
11034        // Server shuts down one stream.
11035        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
11036
11037        let mut r = pipe.server.readable();
11038        assert_eq!(r.next(), None);
11039
11040        // Flush connection.
11041        assert_eq!(pipe.advance(), Ok(()));
11042    }
11043
11044    #[rstest]
11045    fn stream_flow_control_limit_bidi(
11046        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11047    ) {
11048        let mut buf = [0; 65535];
11049
11050        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11051        assert_eq!(pipe.handshake(), Ok(()));
11052
11053        let frames = [frame::Frame::Stream {
11054            stream_id: 4,
11055            data: <RangeBuf>::from(b"aaaaaaaaaaaaaaaa", 0, true),
11056        }];
11057
11058        let pkt_type = packet::Type::Short;
11059        assert_eq!(
11060            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11061            Err(Error::FlowControl),
11062        );
11063    }
11064
11065    #[rstest]
11066    fn stream_flow_control_limit_uni(
11067        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11068    ) {
11069        let mut buf = [0; 65535];
11070
11071        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11072        assert_eq!(pipe.handshake(), Ok(()));
11073
11074        let frames = [frame::Frame::Stream {
11075            stream_id: 2,
11076            data: <RangeBuf>::from(b"aaaaaaaaaaa", 0, true),
11077        }];
11078
11079        let pkt_type = packet::Type::Short;
11080        assert_eq!(
11081            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11082            Err(Error::FlowControl),
11083        );
11084    }
11085
11086    #[rstest]
11087    fn stream_flow_control_update(
11088        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11089    ) {
11090        let mut buf = [0; 65535];
11091
11092        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11093        assert_eq!(pipe.handshake(), Ok(()));
11094
11095        let frames = [frame::Frame::Stream {
11096            stream_id: 4,
11097            data: <RangeBuf>::from(b"aaaaaaaaa", 0, false),
11098        }];
11099
11100        let pkt_type = packet::Type::Short;
11101
11102        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11103
11104        pipe.server.stream_recv(4, &mut buf).unwrap();
11105
11106        let frames = [frame::Frame::Stream {
11107            stream_id: 4,
11108            data: <RangeBuf>::from(b"a", 9, false),
11109        }];
11110
11111        let len = pipe
11112            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11113            .unwrap();
11114
11115        assert!(len > 0);
11116
11117        let frames =
11118            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11119        let mut iter = frames.iter();
11120
11121        // Ignore ACK.
11122        iter.next().unwrap();
11123
11124        assert_eq!(
11125            iter.next(),
11126            Some(&frame::Frame::MaxStreamData {
11127                stream_id: 4,
11128                max: 24,
11129            })
11130        );
11131    }
11132
11133    #[rstest]
11134    fn stream_left_bidi(
11135        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11136    ) {
11137        let mut buf = [0; 65535];
11138
11139        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11140        assert_eq!(pipe.handshake(), Ok(()));
11141
11142        assert_eq!(3, pipe.client.peer_streams_left_bidi());
11143        assert_eq!(3, pipe.server.peer_streams_left_bidi());
11144
11145        pipe.server.stream_send(1, b"a", false).ok();
11146        assert_eq!(2, pipe.server.peer_streams_left_bidi());
11147        pipe.server.stream_send(5, b"a", false).ok();
11148        assert_eq!(1, pipe.server.peer_streams_left_bidi());
11149
11150        pipe.server.stream_send(9, b"a", false).ok();
11151        assert_eq!(0, pipe.server.peer_streams_left_bidi());
11152
11153        let frames = [frame::Frame::MaxStreamsBidi { max: MAX_STREAM_ID }];
11154
11155        let pkt_type = packet::Type::Short;
11156        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11157
11158        assert_eq!(MAX_STREAM_ID - 3, pipe.server.peer_streams_left_bidi());
11159    }
11160
11161    #[rstest]
11162    fn stream_left_uni(
11163        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11164    ) {
11165        let mut buf = [0; 65535];
11166
11167        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11168        assert_eq!(pipe.handshake(), Ok(()));
11169
11170        assert_eq!(3, pipe.client.peer_streams_left_uni());
11171        assert_eq!(3, pipe.server.peer_streams_left_uni());
11172
11173        pipe.server.stream_send(3, b"a", false).ok();
11174        assert_eq!(2, pipe.server.peer_streams_left_uni());
11175        pipe.server.stream_send(7, b"a", false).ok();
11176        assert_eq!(1, pipe.server.peer_streams_left_uni());
11177
11178        pipe.server.stream_send(11, b"a", false).ok();
11179        assert_eq!(0, pipe.server.peer_streams_left_uni());
11180
11181        let frames = [frame::Frame::MaxStreamsUni { max: MAX_STREAM_ID }];
11182
11183        let pkt_type = packet::Type::Short;
11184        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11185
11186        assert_eq!(MAX_STREAM_ID - 3, pipe.server.peer_streams_left_uni());
11187    }
11188
11189    #[rstest]
11190    fn stream_limit_bidi(
11191        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11192    ) {
11193        let mut buf = [0; 65535];
11194
11195        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11196        assert_eq!(pipe.handshake(), Ok(()));
11197
11198        let frames = [
11199            frame::Frame::Stream {
11200                stream_id: 4,
11201                data: <RangeBuf>::from(b"a", 0, false),
11202            },
11203            frame::Frame::Stream {
11204                stream_id: 8,
11205                data: <RangeBuf>::from(b"a", 0, false),
11206            },
11207            frame::Frame::Stream {
11208                stream_id: 12,
11209                data: <RangeBuf>::from(b"a", 0, false),
11210            },
11211            frame::Frame::Stream {
11212                stream_id: 16,
11213                data: <RangeBuf>::from(b"a", 0, false),
11214            },
11215            frame::Frame::Stream {
11216                stream_id: 20,
11217                data: <RangeBuf>::from(b"a", 0, false),
11218            },
11219            frame::Frame::Stream {
11220                stream_id: 24,
11221                data: <RangeBuf>::from(b"a", 0, false),
11222            },
11223            frame::Frame::Stream {
11224                stream_id: 28,
11225                data: <RangeBuf>::from(b"a", 0, false),
11226            },
11227        ];
11228
11229        let pkt_type = packet::Type::Short;
11230        assert_eq!(
11231            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11232            Err(Error::StreamLimit),
11233        );
11234    }
11235
11236    #[rstest]
11237    fn stream_limit_max_bidi(
11238        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11239    ) {
11240        let mut buf = [0; 65535];
11241
11242        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11243        assert_eq!(pipe.handshake(), Ok(()));
11244
11245        let frames = [frame::Frame::MaxStreamsBidi { max: MAX_STREAM_ID }];
11246
11247        let pkt_type = packet::Type::Short;
11248        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11249
11250        let frames = [frame::Frame::MaxStreamsBidi {
11251            max: MAX_STREAM_ID + 1,
11252        }];
11253
11254        let pkt_type = packet::Type::Short;
11255        assert_eq!(
11256            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11257            Err(Error::InvalidFrame),
11258        );
11259    }
11260
11261    #[rstest]
11262    fn stream_limit_uni(
11263        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11264    ) {
11265        let mut buf = [0; 65535];
11266
11267        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11268        assert_eq!(pipe.handshake(), Ok(()));
11269
11270        let frames = [
11271            frame::Frame::Stream {
11272                stream_id: 2,
11273                data: <RangeBuf>::from(b"a", 0, false),
11274            },
11275            frame::Frame::Stream {
11276                stream_id: 6,
11277                data: <RangeBuf>::from(b"a", 0, false),
11278            },
11279            frame::Frame::Stream {
11280                stream_id: 10,
11281                data: <RangeBuf>::from(b"a", 0, false),
11282            },
11283            frame::Frame::Stream {
11284                stream_id: 14,
11285                data: <RangeBuf>::from(b"a", 0, false),
11286            },
11287            frame::Frame::Stream {
11288                stream_id: 18,
11289                data: <RangeBuf>::from(b"a", 0, false),
11290            },
11291            frame::Frame::Stream {
11292                stream_id: 22,
11293                data: <RangeBuf>::from(b"a", 0, false),
11294            },
11295            frame::Frame::Stream {
11296                stream_id: 26,
11297                data: <RangeBuf>::from(b"a", 0, false),
11298            },
11299        ];
11300
11301        let pkt_type = packet::Type::Short;
11302        assert_eq!(
11303            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11304            Err(Error::StreamLimit),
11305        );
11306    }
11307
11308    #[rstest]
11309    fn stream_limit_max_uni(
11310        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11311    ) {
11312        let mut buf = [0; 65535];
11313
11314        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11315        assert_eq!(pipe.handshake(), Ok(()));
11316
11317        let frames = [frame::Frame::MaxStreamsUni { max: MAX_STREAM_ID }];
11318
11319        let pkt_type = packet::Type::Short;
11320        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11321
11322        let frames = [frame::Frame::MaxStreamsUni {
11323            max: MAX_STREAM_ID + 1,
11324        }];
11325
11326        let pkt_type = packet::Type::Short;
11327        assert_eq!(
11328            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11329            Err(Error::InvalidFrame),
11330        );
11331    }
11332
11333    #[rstest]
11334    fn stream_left_reset_bidi(
11335        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11336    ) {
11337        let mut buf = [0; 65535];
11338
11339        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11340        assert_eq!(pipe.handshake(), Ok(()));
11341
11342        assert_eq!(3, pipe.client.peer_streams_left_bidi());
11343        assert_eq!(3, pipe.server.peer_streams_left_bidi());
11344
11345        pipe.client.stream_send(0, b"a", false).ok();
11346        assert_eq!(2, pipe.client.peer_streams_left_bidi());
11347        pipe.client.stream_send(4, b"a", false).ok();
11348        assert_eq!(1, pipe.client.peer_streams_left_bidi());
11349        pipe.client.stream_send(8, b"a", false).ok();
11350        assert_eq!(0, pipe.client.peer_streams_left_bidi());
11351
11352        // Client resets the stream.
11353        pipe.client
11354            .stream_shutdown(0, Shutdown::Write, 1001)
11355            .unwrap();
11356        pipe.advance().unwrap();
11357
11358        assert_eq!(0, pipe.client.peer_streams_left_bidi());
11359        let mut r = pipe.server.readable();
11360        assert_eq!(Some(0), r.next());
11361        assert_eq!(Some(4), r.next());
11362        assert_eq!(Some(8), r.next());
11363        assert_eq!(None, r.next());
11364
11365        assert_eq!(
11366            pipe.server.stream_recv(0, &mut buf),
11367            Err(Error::StreamReset(1001))
11368        );
11369
11370        let mut r = pipe.server.readable();
11371        assert_eq!(Some(4), r.next());
11372        assert_eq!(Some(8), r.next());
11373        assert_eq!(None, r.next());
11374
11375        // Server resets the stream in reaction.
11376        pipe.server
11377            .stream_shutdown(0, Shutdown::Write, 1001)
11378            .unwrap();
11379        pipe.advance().unwrap();
11380
11381        assert_eq!(1, pipe.client.peer_streams_left_bidi());
11382
11383        // Repeat for the other 2 streams
11384        pipe.client
11385            .stream_shutdown(4, Shutdown::Write, 1001)
11386            .unwrap();
11387        pipe.client
11388            .stream_shutdown(8, Shutdown::Write, 1001)
11389            .unwrap();
11390        pipe.advance().unwrap();
11391
11392        let mut r = pipe.server.readable();
11393        assert_eq!(Some(4), r.next());
11394        assert_eq!(Some(8), r.next());
11395        assert_eq!(None, r.next());
11396
11397        assert_eq!(
11398            pipe.server.stream_recv(4, &mut buf),
11399            Err(Error::StreamReset(1001))
11400        );
11401
11402        assert_eq!(
11403            pipe.server.stream_recv(8, &mut buf),
11404            Err(Error::StreamReset(1001))
11405        );
11406
11407        let mut r = pipe.server.readable();
11408        assert_eq!(None, r.next());
11409
11410        pipe.server
11411            .stream_shutdown(4, Shutdown::Write, 1001)
11412            .unwrap();
11413        pipe.server
11414            .stream_shutdown(8, Shutdown::Write, 1001)
11415            .unwrap();
11416        pipe.advance().unwrap();
11417
11418        assert_eq!(3, pipe.client.peer_streams_left_bidi());
11419    }
11420
11421    #[rstest]
11422    fn stream_reset_counts(
11423        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11424    ) {
11425        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11426        assert_eq!(pipe.handshake(), Ok(()));
11427
11428        pipe.client.stream_send(0, b"a", false).ok();
11429        pipe.client.stream_send(2, b"a", false).ok();
11430        pipe.client.stream_send(4, b"a", false).ok();
11431        pipe.client.stream_send(8, b"a", false).ok();
11432        pipe.advance().unwrap();
11433
11434        let stats = pipe.client.stats();
11435        assert_eq!(stats.reset_stream_count_local, 0);
11436
11437        // Client resets the stream.
11438        pipe.client
11439            .stream_shutdown(0, Shutdown::Write, 1001)
11440            .unwrap();
11441        pipe.advance().unwrap();
11442
11443        let stats = pipe.client.stats();
11444        assert_eq!(stats.reset_stream_count_local, 1);
11445        assert_eq!(stats.reset_stream_count_remote, 0);
11446        let stats = pipe.server.stats();
11447        assert_eq!(stats.reset_stream_count_local, 0);
11448        assert_eq!(stats.reset_stream_count_remote, 1);
11449
11450        // Server resets the stream in reaction.
11451        pipe.server
11452            .stream_shutdown(0, Shutdown::Write, 1001)
11453            .unwrap();
11454        pipe.advance().unwrap();
11455
11456        let stats = pipe.client.stats();
11457        assert_eq!(stats.reset_stream_count_local, 1);
11458        assert_eq!(stats.reset_stream_count_remote, 1);
11459        let stats = pipe.server.stats();
11460        assert_eq!(stats.reset_stream_count_local, 1);
11461        assert_eq!(stats.reset_stream_count_remote, 1);
11462
11463        // Repeat for the other streams
11464        pipe.client
11465            .stream_shutdown(2, Shutdown::Write, 1001)
11466            .unwrap();
11467        pipe.client
11468            .stream_shutdown(4, Shutdown::Write, 1001)
11469            .unwrap();
11470        pipe.client
11471            .stream_shutdown(8, Shutdown::Write, 1001)
11472            .unwrap();
11473        pipe.advance().unwrap();
11474
11475        pipe.server
11476            .stream_shutdown(4, Shutdown::Write, 1001)
11477            .unwrap();
11478        pipe.server
11479            .stream_shutdown(8, Shutdown::Write, 1001)
11480            .unwrap();
11481        pipe.advance().unwrap();
11482
11483        let stats = pipe.client.stats();
11484        assert_eq!(stats.reset_stream_count_local, 4);
11485        assert_eq!(stats.reset_stream_count_remote, 3);
11486        let stats = pipe.server.stats();
11487        assert_eq!(stats.reset_stream_count_local, 3);
11488        assert_eq!(stats.reset_stream_count_remote, 4);
11489    }
11490
11491    #[rstest]
11492    fn stream_stop_counts(
11493        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11494    ) {
11495        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11496        assert_eq!(pipe.handshake(), Ok(()));
11497
11498        pipe.client.stream_send(0, b"a", false).ok();
11499        pipe.client.stream_send(2, b"a", false).ok();
11500        pipe.client.stream_send(4, b"a", false).ok();
11501        pipe.client.stream_send(8, b"a", false).ok();
11502        pipe.advance().unwrap();
11503
11504        let stats = pipe.client.stats();
11505        assert_eq!(stats.reset_stream_count_local, 0);
11506
11507        // Server stops the stream and client automatically resets.
11508        pipe.server
11509            .stream_shutdown(0, Shutdown::Read, 1001)
11510            .unwrap();
11511        pipe.advance().unwrap();
11512
11513        let stats = pipe.client.stats();
11514        assert_eq!(stats.stopped_stream_count_local, 0);
11515        assert_eq!(stats.stopped_stream_count_remote, 1);
11516        assert_eq!(stats.reset_stream_count_local, 1);
11517        assert_eq!(stats.reset_stream_count_remote, 0);
11518
11519        let stats = pipe.server.stats();
11520        assert_eq!(stats.stopped_stream_count_local, 1);
11521        assert_eq!(stats.stopped_stream_count_remote, 0);
11522        assert_eq!(stats.reset_stream_count_local, 0);
11523        assert_eq!(stats.reset_stream_count_remote, 1);
11524
11525        // Repeat for the other streams
11526        pipe.server
11527            .stream_shutdown(2, Shutdown::Read, 1001)
11528            .unwrap();
11529        pipe.server
11530            .stream_shutdown(4, Shutdown::Read, 1001)
11531            .unwrap();
11532        pipe.server
11533            .stream_shutdown(8, Shutdown::Read, 1001)
11534            .unwrap();
11535        pipe.advance().unwrap();
11536
11537        let stats = pipe.client.stats();
11538        assert_eq!(stats.stopped_stream_count_local, 0);
11539        assert_eq!(stats.stopped_stream_count_remote, 4);
11540        assert_eq!(stats.reset_stream_count_local, 4);
11541        assert_eq!(stats.reset_stream_count_remote, 0);
11542
11543        let stats = pipe.server.stats();
11544        assert_eq!(stats.stopped_stream_count_local, 4);
11545        assert_eq!(stats.stopped_stream_count_remote, 0);
11546        assert_eq!(stats.reset_stream_count_local, 0);
11547        assert_eq!(stats.reset_stream_count_remote, 4);
11548    }
11549
11550    #[rstest]
11551    fn streams_blocked_max_bidi(
11552        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11553    ) {
11554        let mut buf = [0; 65535];
11555
11556        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11557        assert_eq!(pipe.handshake(), Ok(()));
11558
11559        let frames = [frame::Frame::StreamsBlockedBidi {
11560            limit: MAX_STREAM_ID,
11561        }];
11562
11563        let pkt_type = packet::Type::Short;
11564        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11565
11566        let frames = [frame::Frame::StreamsBlockedBidi {
11567            limit: MAX_STREAM_ID + 1,
11568        }];
11569
11570        let pkt_type = packet::Type::Short;
11571        assert_eq!(
11572            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11573            Err(Error::InvalidFrame),
11574        );
11575    }
11576
11577    #[rstest]
11578    fn streams_blocked_max_uni(
11579        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11580    ) {
11581        let mut buf = [0; 65535];
11582
11583        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11584        assert_eq!(pipe.handshake(), Ok(()));
11585
11586        let frames = [frame::Frame::StreamsBlockedUni {
11587            limit: MAX_STREAM_ID,
11588        }];
11589
11590        let pkt_type = packet::Type::Short;
11591        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11592
11593        let frames = [frame::Frame::StreamsBlockedUni {
11594            limit: MAX_STREAM_ID + 1,
11595        }];
11596
11597        let pkt_type = packet::Type::Short;
11598        assert_eq!(
11599            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11600            Err(Error::InvalidFrame),
11601        );
11602    }
11603
11604    #[rstest]
11605    fn stream_data_overlap(
11606        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11607    ) {
11608        let mut buf = [0; 65535];
11609
11610        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11611        assert_eq!(pipe.handshake(), Ok(()));
11612
11613        let frames = [
11614            frame::Frame::Stream {
11615                stream_id: 0,
11616                data: <RangeBuf>::from(b"aaaaa", 0, false),
11617            },
11618            frame::Frame::Stream {
11619                stream_id: 0,
11620                data: <RangeBuf>::from(b"bbbbb", 3, false),
11621            },
11622            frame::Frame::Stream {
11623                stream_id: 0,
11624                data: <RangeBuf>::from(b"ccccc", 6, false),
11625            },
11626        ];
11627
11628        let pkt_type = packet::Type::Short;
11629        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11630
11631        let mut b = [0; 15];
11632        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((11, false)));
11633        assert_eq!(&b[..11], b"aaaaabbbccc");
11634    }
11635
11636    #[rstest]
11637    fn stream_data_overlap_with_reordering(
11638        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11639    ) {
11640        let mut buf = [0; 65535];
11641
11642        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11643        assert_eq!(pipe.handshake(), Ok(()));
11644
11645        let frames = [
11646            frame::Frame::Stream {
11647                stream_id: 0,
11648                data: <RangeBuf>::from(b"aaaaa", 0, false),
11649            },
11650            frame::Frame::Stream {
11651                stream_id: 0,
11652                data: <RangeBuf>::from(b"ccccc", 6, false),
11653            },
11654            frame::Frame::Stream {
11655                stream_id: 0,
11656                data: <RangeBuf>::from(b"bbbbb", 3, false),
11657            },
11658        ];
11659
11660        let pkt_type = packet::Type::Short;
11661        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11662
11663        let mut b = [0; 15];
11664        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((11, false)));
11665        assert_eq!(&b[..11], b"aaaaabccccc");
11666    }
11667
11668    #[rstest]
11669    /// Tests that receiving a valid RESET_STREAM frame when all data has
11670    /// already been read, notifies the application.
11671    fn reset_stream_data_recvd(
11672        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11673    ) {
11674        let mut b = [0; 15];
11675        let mut buf = [0; 65535];
11676
11677        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11678        assert_eq!(pipe.handshake(), Ok(()));
11679
11680        // Client sends some data.
11681        assert_eq!(pipe.client.stream_send(0, b"hello", false), Ok(5));
11682        assert_eq!(pipe.advance(), Ok(()));
11683
11684        // Server gets data and sends data back, closing stream.
11685        let mut r = pipe.server.readable();
11686        assert_eq!(r.next(), Some(0));
11687        assert_eq!(r.next(), None);
11688
11689        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, false)));
11690        assert!(!pipe.server.stream_finished(0));
11691
11692        let mut r = pipe.server.readable();
11693        assert_eq!(r.next(), None);
11694
11695        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
11696        assert_eq!(pipe.advance(), Ok(()));
11697
11698        let mut r = pipe.client.readable();
11699        assert_eq!(r.next(), Some(0));
11700        assert_eq!(r.next(), None);
11701
11702        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((0, true)));
11703        assert!(pipe.client.stream_finished(0));
11704
11705        // Client sends RESET_STREAM, closing stream.
11706        let frames = [frame::Frame::ResetStream {
11707            stream_id: 0,
11708            error_code: 42,
11709            final_size: 5,
11710        }];
11711
11712        let pkt_type = packet::Type::Short;
11713        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11714
11715        // Server is notified of stream readability, due to reset.
11716        let mut r = pipe.server.readable();
11717        assert_eq!(r.next(), Some(0));
11718        assert_eq!(r.next(), None);
11719
11720        assert_eq!(
11721            pipe.server.stream_recv(0, &mut b),
11722            Err(Error::StreamReset(42))
11723        );
11724
11725        assert!(pipe.server.stream_finished(0));
11726
11727        // Sending RESET_STREAM again shouldn't make stream readable again.
11728        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
11729            .unwrap();
11730
11731        let mut r = pipe.server.readable();
11732        assert_eq!(r.next(), None);
11733    }
11734
11735    #[rstest]
11736    /// Tests that receiving a valid RESET_STREAM frame when all data has _not_
11737    /// been read, discards all buffered data and notifies the application.
11738    fn reset_stream_data_not_recvd(
11739        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11740    ) {
11741        let mut b = [0; 15];
11742        let mut buf = [0; 65535];
11743
11744        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11745        assert_eq!(pipe.handshake(), Ok(()));
11746
11747        // Client sends some data.
11748        assert_eq!(pipe.client.stream_send(0, b"h", false), Ok(1));
11749        assert_eq!(pipe.advance(), Ok(()));
11750
11751        // Server gets data and sends data back, closing stream.
11752        let mut r = pipe.server.readable();
11753        assert_eq!(r.next(), Some(0));
11754        assert_eq!(r.next(), None);
11755
11756        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((1, false)));
11757        assert!(!pipe.server.stream_finished(0));
11758
11759        let mut r = pipe.server.readable();
11760        assert_eq!(r.next(), None);
11761
11762        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
11763        assert_eq!(pipe.advance(), Ok(()));
11764
11765        let mut r = pipe.client.readable();
11766        assert_eq!(r.next(), Some(0));
11767        assert_eq!(r.next(), None);
11768
11769        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((0, true)));
11770        assert!(pipe.client.stream_finished(0));
11771
11772        // Client sends RESET_STREAM, closing stream.
11773        let frames = [frame::Frame::ResetStream {
11774            stream_id: 0,
11775            error_code: 42,
11776            final_size: 5,
11777        }];
11778
11779        let pkt_type = packet::Type::Short;
11780        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11781
11782        // Server is notified of stream readability, due to reset.
11783        let mut r = pipe.server.readable();
11784        assert_eq!(r.next(), Some(0));
11785        assert_eq!(r.next(), None);
11786
11787        assert_eq!(
11788            pipe.server.stream_recv(0, &mut b),
11789            Err(Error::StreamReset(42))
11790        );
11791
11792        assert!(pipe.server.stream_finished(0));
11793
11794        // Sending RESET_STREAM again shouldn't make stream readable again.
11795        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11796
11797        let mut r = pipe.server.readable();
11798        assert_eq!(r.next(), None);
11799    }
11800
11801    #[rstest]
11802    /// Tests that RESET_STREAM frames exceeding the connection-level flow
11803    /// control limit cause an error.
11804    fn reset_stream_flow_control(
11805        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11806    ) {
11807        let mut buf = [0; 65535];
11808
11809        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11810        assert_eq!(pipe.handshake(), Ok(()));
11811
11812        let frames = [
11813            frame::Frame::Stream {
11814                stream_id: 0,
11815                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
11816            },
11817            frame::Frame::Stream {
11818                stream_id: 4,
11819                data: <RangeBuf>::from(b"a", 0, false),
11820            },
11821            frame::Frame::ResetStream {
11822                stream_id: 4,
11823                error_code: 0,
11824                final_size: 15,
11825            },
11826            frame::Frame::Stream {
11827                stream_id: 8,
11828                data: <RangeBuf>::from(b"a", 0, false),
11829            },
11830        ];
11831
11832        let pkt_type = packet::Type::Short;
11833        assert_eq!(
11834            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11835            Err(Error::FlowControl),
11836        );
11837    }
11838
11839    #[rstest]
11840    /// Tests that RESET_STREAM frames exceeding the stream-level flow control
11841    /// limit cause an error.
11842    fn reset_stream_flow_control_stream(
11843        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11844    ) {
11845        let mut buf = [0; 65535];
11846
11847        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11848        assert_eq!(pipe.handshake(), Ok(()));
11849
11850        let frames = [
11851            frame::Frame::Stream {
11852                stream_id: 4,
11853                data: <RangeBuf>::from(b"a", 0, false),
11854            },
11855            frame::Frame::ResetStream {
11856                stream_id: 4,
11857                error_code: 0,
11858                final_size: 16, // Past stream's flow control limit.
11859            },
11860        ];
11861
11862        let pkt_type = packet::Type::Short;
11863        assert_eq!(
11864            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11865            Err(Error::FlowControl),
11866        );
11867    }
11868
11869    #[rstest]
11870    fn path_challenge(
11871        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11872    ) {
11873        let mut buf = [0; 65535];
11874
11875        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11876        assert_eq!(pipe.handshake(), Ok(()));
11877
11878        let frames = [frame::Frame::PathChallenge { data: [0xba; 8] }];
11879
11880        let pkt_type = packet::Type::Short;
11881
11882        let len = pipe
11883            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11884            .unwrap();
11885
11886        assert!(len > 0);
11887
11888        let frames =
11889            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11890        let mut iter = frames.iter();
11891
11892        // Ignore ACK.
11893        iter.next().unwrap();
11894
11895        assert_eq!(
11896            iter.next(),
11897            Some(&frame::Frame::PathResponse { data: [0xba; 8] })
11898        );
11899    }
11900
11901    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
11902    #[rstest]
11903    /// Simulates reception of an early 1-RTT packet on the server, by
11904    /// delaying the client's Handshake packet that completes the handshake.
11905    fn early_1rtt_packet(
11906        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11907    ) {
11908        let mut buf = [0; 65535];
11909
11910        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11911
11912        // Client sends initial flight
11913        let flight = testing::emit_flight(&mut pipe.client).unwrap();
11914        testing::process_flight(&mut pipe.server, flight).unwrap();
11915
11916        // Server sends initial flight.
11917        let flight = testing::emit_flight(&mut pipe.server).unwrap();
11918        testing::process_flight(&mut pipe.client, flight).unwrap();
11919
11920        // Client sends Handshake packet.
11921        let flight = testing::emit_flight(&mut pipe.client).unwrap();
11922
11923        // Emulate handshake packet delay by not making server process client
11924        // packet.
11925        let delayed = flight;
11926
11927        testing::emit_flight(&mut pipe.server).ok();
11928
11929        assert!(pipe.client.is_established());
11930
11931        // Send 1-RTT packet #0.
11932        let frames = [frame::Frame::Stream {
11933            stream_id: 0,
11934            data: <RangeBuf>::from(b"hello, world", 0, true),
11935        }];
11936
11937        let pkt_type = packet::Type::Short;
11938        let written =
11939            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
11940                .unwrap();
11941
11942        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
11943
11944        // Send 1-RTT packet #1.
11945        let frames = [frame::Frame::Stream {
11946            stream_id: 4,
11947            data: <RangeBuf>::from(b"hello, world", 0, true),
11948        }];
11949
11950        let written =
11951            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
11952                .unwrap();
11953
11954        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
11955
11956        assert!(!pipe.server.is_established());
11957
11958        // Client sent 1-RTT packets 0 and 1, but server hasn't received them.
11959        //
11960        // Note that `largest_rx_pkt_num` is initialized to 0, so we need to
11961        // send another 1-RTT packet to make this check meaningful.
11962        assert_eq!(
11963            pipe.server.pkt_num_spaces[packet::Epoch::Application]
11964                .largest_rx_pkt_num,
11965            0
11966        );
11967
11968        // Process delayed packet.
11969        testing::process_flight(&mut pipe.server, delayed).unwrap();
11970
11971        assert!(pipe.server.is_established());
11972
11973        assert_eq!(
11974            pipe.server.pkt_num_spaces[packet::Epoch::Application]
11975                .largest_rx_pkt_num,
11976            0
11977        );
11978    }
11979
11980    #[rstest]
11981    fn stop_sending(
11982        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
11983    ) {
11984        let mut b = [0; 15];
11985
11986        let mut buf = [0; 65535];
11987
11988        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
11989        assert_eq!(pipe.handshake(), Ok(()));
11990
11991        // Client sends some data, and closes stream.
11992        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
11993        assert_eq!(pipe.advance(), Ok(()));
11994
11995        // Server gets data.
11996        let mut r = pipe.server.readable();
11997        assert_eq!(r.next(), Some(0));
11998        assert_eq!(r.next(), None);
11999
12000        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
12001        assert!(pipe.server.stream_finished(0));
12002
12003        let mut r = pipe.server.readable();
12004        assert_eq!(r.next(), None);
12005
12006        // Server sends data, until blocked.
12007        let mut r = pipe.server.writable();
12008        assert_eq!(r.next(), Some(0));
12009        assert_eq!(r.next(), None);
12010
12011        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {
12012            assert_eq!(pipe.advance(), Ok(()));
12013        }
12014
12015        let mut r = pipe.server.writable();
12016        assert_eq!(r.next(), None);
12017
12018        // Client sends STOP_SENDING.
12019        let frames = [frame::Frame::StopSending {
12020            stream_id: 0,
12021            error_code: 42,
12022        }];
12023
12024        let pkt_type = packet::Type::Short;
12025        let len = pipe
12026            .send_pkt_to_server(pkt_type, &frames, &mut buf)
12027            .unwrap();
12028
12029        // Server sent a RESET_STREAM frame in response.
12030        let frames =
12031            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
12032
12033        let mut iter = frames.iter();
12034
12035        // Skip ACK frame.
12036        iter.next();
12037
12038        assert_eq!(
12039            iter.next(),
12040            Some(&frame::Frame::ResetStream {
12041                stream_id: 0,
12042                error_code: 42,
12043                final_size: 15,
12044            })
12045        );
12046
12047        // Stream is writable, but writing returns an error.
12048        let mut r = pipe.server.writable();
12049        assert_eq!(r.next(), Some(0));
12050        assert_eq!(r.next(), None);
12051
12052        assert_eq!(
12053            pipe.server.stream_send(0, b"world", true),
12054            Err(Error::StreamStopped(42)),
12055        );
12056
12057        assert_eq!(pipe.server.streams.len(), 1);
12058
12059        // Client acks RESET_STREAM frame.
12060        let mut ranges = ranges::RangeSet::default();
12061        ranges.insert(pipe.server.next_pkt_num - 5..pipe.server.next_pkt_num);
12062
12063        let frames = [frame::Frame::ACK {
12064            ack_delay: 15,
12065            ranges,
12066            ecn_counts: None,
12067        }];
12068
12069        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
12070
12071        // Stream is collected on the server after RESET_STREAM is acked.
12072        assert_eq!(pipe.server.streams.len(), 0);
12073
12074        // Sending STOP_SENDING again shouldn't trigger RESET_STREAM again.
12075        let frames = [frame::Frame::StopSending {
12076            stream_id: 0,
12077            error_code: 42,
12078        }];
12079
12080        let len = pipe
12081            .send_pkt_to_server(pkt_type, &frames, &mut buf)
12082            .unwrap();
12083
12084        let frames =
12085            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
12086
12087        assert_eq!(frames.len(), 1);
12088
12089        match frames.first() {
12090            Some(frame::Frame::ACK { .. }) => (),
12091
12092            f => panic!("expected ACK frame, got {:?}", f),
12093        };
12094
12095        let mut r = pipe.server.writable();
12096        assert_eq!(r.next(), None);
12097    }
12098
12099    #[rstest]
12100    fn stop_sending_fin(
12101        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12102    ) {
12103        let mut b = [0; 15];
12104
12105        let mut buf = [0; 65535];
12106
12107        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12108        assert_eq!(pipe.handshake(), Ok(()));
12109
12110        // Client sends some data, and closes stream.
12111        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
12112        assert_eq!(pipe.advance(), Ok(()));
12113
12114        // Server gets data.
12115        let mut r = pipe.server.readable();
12116        assert_eq!(r.next(), Some(4));
12117        assert_eq!(r.next(), None);
12118
12119        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
12120        assert!(pipe.server.stream_finished(4));
12121
12122        let mut r = pipe.server.readable();
12123        assert_eq!(r.next(), None);
12124
12125        // Server sends data...
12126        let mut r = pipe.server.writable();
12127        assert_eq!(r.next(), Some(4));
12128        assert_eq!(r.next(), None);
12129
12130        assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
12131        assert_eq!(pipe.advance(), Ok(()));
12132
12133        // ...and buffers more, and closes stream.
12134        assert_eq!(pipe.server.stream_send(4, b"world", true), Ok(5));
12135
12136        // Client sends STOP_SENDING before server flushes stream.
12137        let frames = [frame::Frame::StopSending {
12138            stream_id: 4,
12139            error_code: 42,
12140        }];
12141
12142        let pkt_type = packet::Type::Short;
12143        let len = pipe
12144            .send_pkt_to_server(pkt_type, &frames, &mut buf)
12145            .unwrap();
12146
12147        // Server sent a RESET_STREAM frame in response.
12148        let frames =
12149            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
12150
12151        let mut iter = frames.iter();
12152
12153        // Skip ACK frame.
12154        iter.next();
12155
12156        assert_eq!(
12157            iter.next(),
12158            Some(&frame::Frame::ResetStream {
12159                stream_id: 4,
12160                error_code: 42,
12161                final_size: 5,
12162            })
12163        );
12164
12165        // No more frames are sent by the server.
12166        assert_eq!(iter.next(), None);
12167    }
12168
12169    #[rstest]
12170    /// Tests that resetting a stream restores flow control for unsent data.
12171    fn stop_sending_unsent_tx_cap(
12172        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12173    ) {
12174        let mut buf = [0; 65535];
12175
12176        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12177        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
12178        config
12179            .load_cert_chain_from_pem_file("examples/cert.crt")
12180            .unwrap();
12181        config
12182            .load_priv_key_from_pem_file("examples/cert.key")
12183            .unwrap();
12184        config
12185            .set_application_protos(&[b"proto1", b"proto2"])
12186            .unwrap();
12187        config.set_initial_max_data(15);
12188        config.set_initial_max_stream_data_bidi_local(30);
12189        config.set_initial_max_stream_data_bidi_remote(30);
12190        config.set_initial_max_stream_data_uni(30);
12191        config.set_initial_max_streams_bidi(3);
12192        config.set_initial_max_streams_uni(0);
12193        config.verify_peer(false);
12194
12195        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12196        assert_eq!(pipe.handshake(), Ok(()));
12197
12198        // Client sends some data.
12199        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
12200        assert_eq!(pipe.advance(), Ok(()));
12201
12202        let mut r = pipe.server.readable();
12203        assert_eq!(r.next(), Some(4));
12204        assert_eq!(r.next(), None);
12205
12206        let mut b = [0; 15];
12207        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
12208
12209        // Server sends some data.
12210        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12211        assert_eq!(pipe.advance(), Ok(()));
12212
12213        // Server buffers some data, until send capacity limit reached.
12214        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12215        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12216        assert_eq!(
12217            pipe.server.stream_send(4, b"hello", false),
12218            Err(Error::Done)
12219        );
12220
12221        // Client sends STOP_SENDING.
12222        let frames = [frame::Frame::StopSending {
12223            stream_id: 4,
12224            error_code: 42,
12225        }];
12226
12227        let pkt_type = packet::Type::Short;
12228        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
12229            .unwrap();
12230
12231        // Server can now send more data (on a different stream).
12232        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
12233        assert_eq!(pipe.advance(), Ok(()));
12234
12235        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12236        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12237        assert_eq!(
12238            pipe.server.stream_send(8, b"hello", false),
12239            Err(Error::Done)
12240        );
12241        assert_eq!(pipe.advance(), Ok(()));
12242    }
12243
12244    #[rstest]
12245    fn stream_shutdown_read(
12246        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12247    ) {
12248        let mut buf = [0; 65535];
12249
12250        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12251        assert_eq!(pipe.handshake(), Ok(()));
12252
12253        // Client sends some data.
12254        assert_eq!(pipe.client.stream_send(4, b"hello, world", false), Ok(12));
12255        assert_eq!(pipe.advance(), Ok(()));
12256
12257        let mut r = pipe.server.readable();
12258        assert_eq!(r.next(), Some(4));
12259        assert_eq!(r.next(), None);
12260
12261        assert_eq!(pipe.client.streams.len(), 1);
12262        assert_eq!(pipe.server.streams.len(), 1);
12263
12264        // Server shuts down stream.
12265        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
12266
12267        let mut r = pipe.server.readable();
12268        assert_eq!(r.next(), None);
12269
12270        let (len, _) = pipe.server.send(&mut buf).unwrap();
12271
12272        let mut dummy = buf[..len].to_vec();
12273
12274        let frames =
12275            testing::decode_pkt(&mut pipe.client, &mut dummy[..len]).unwrap();
12276        let mut iter = frames.iter();
12277
12278        assert_eq!(
12279            iter.next(),
12280            Some(&frame::Frame::StopSending {
12281                stream_id: 4,
12282                error_code: 42,
12283            })
12284        );
12285
12286        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
12287
12288        assert_eq!(pipe.advance(), Ok(()));
12289
12290        // Sending more data is forbidden.
12291        let mut r = pipe.client.writable();
12292        assert_eq!(r.next(), Some(4));
12293        assert_eq!(r.next(), None);
12294
12295        assert_eq!(
12296            pipe.client.stream_send(4, b"bye", false),
12297            Err(Error::StreamStopped(42))
12298        );
12299
12300        // Server sends some data, without reading the incoming data, and closes
12301        // the stream.
12302        assert_eq!(pipe.server.stream_send(4, b"hello, world", true), Ok(12));
12303        assert_eq!(pipe.advance(), Ok(()));
12304
12305        // Client reads the data.
12306        let mut r = pipe.client.readable();
12307        assert_eq!(r.next(), Some(4));
12308        assert_eq!(r.next(), None);
12309
12310        assert_eq!(pipe.client.stream_recv(4, &mut buf), Ok((12, true)));
12311
12312        // Stream is collected on both sides.
12313        assert_eq!(pipe.client.streams.len(), 0);
12314        assert_eq!(pipe.server.streams.len(), 0);
12315
12316        assert_eq!(
12317            pipe.server.stream_shutdown(4, Shutdown::Read, 0),
12318            Err(Error::Done)
12319        );
12320    }
12321
12322    #[rstest]
12323    fn stream_shutdown_read_after_fin(
12324        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12325    ) {
12326        let mut buf = [0; 65535];
12327
12328        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12329        assert_eq!(pipe.handshake(), Ok(()));
12330
12331        // Client sends some data.
12332        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
12333        assert_eq!(pipe.advance(), Ok(()));
12334
12335        let mut r = pipe.server.readable();
12336        assert_eq!(r.next(), Some(4));
12337        assert_eq!(r.next(), None);
12338
12339        assert_eq!(pipe.client.streams.len(), 1);
12340        assert_eq!(pipe.server.streams.len(), 1);
12341
12342        // Server shuts down stream.
12343        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
12344
12345        let mut r = pipe.server.readable();
12346        assert_eq!(r.next(), None);
12347
12348        // Server has nothing to send.
12349        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
12350
12351        assert_eq!(pipe.advance(), Ok(()));
12352
12353        // Server sends some data, without reading the incoming data, and closes
12354        // the stream.
12355        assert_eq!(pipe.server.stream_send(4, b"hello, world", true), Ok(12));
12356        assert_eq!(pipe.advance(), Ok(()));
12357
12358        // Client reads the data.
12359        let mut r = pipe.client.readable();
12360        assert_eq!(r.next(), Some(4));
12361        assert_eq!(r.next(), None);
12362
12363        assert_eq!(pipe.client.stream_recv(4, &mut buf), Ok((12, true)));
12364
12365        // Stream is collected on both sides.
12366        assert_eq!(pipe.client.streams.len(), 0);
12367        assert_eq!(pipe.server.streams.len(), 0);
12368
12369        assert_eq!(
12370            pipe.server.stream_shutdown(4, Shutdown::Read, 0),
12371            Err(Error::Done)
12372        );
12373    }
12374
12375    #[rstest]
12376    fn stream_shutdown_read_update_max_data(
12377        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12378    ) {
12379        let mut buf = [0; 65535];
12380
12381        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12382        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
12383        config
12384            .load_cert_chain_from_pem_file("examples/cert.crt")
12385            .unwrap();
12386        config
12387            .load_priv_key_from_pem_file("examples/cert.key")
12388            .unwrap();
12389        config
12390            .set_application_protos(&[b"proto1", b"proto2"])
12391            .unwrap();
12392        config.set_initial_max_data(30);
12393        config.set_initial_max_stream_data_bidi_local(10000);
12394        config.set_initial_max_stream_data_bidi_remote(10000);
12395        config.set_initial_max_streams_bidi(10);
12396        config.verify_peer(false);
12397
12398        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12399        assert_eq!(pipe.handshake(), Ok(()));
12400
12401        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
12402        assert_eq!(pipe.advance(), Ok(()));
12403
12404        assert_eq!(pipe.server.stream_recv(0, &mut buf), Ok((1, false)));
12405        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Read, 123), Ok(()));
12406
12407        assert_eq!(pipe.server.rx_data, 1);
12408        assert_eq!(pipe.client.tx_data, 1);
12409        assert_eq!(pipe.client.max_tx_data, 30);
12410
12411        assert_eq!(
12412            pipe.client
12413                .stream_send(0, &buf[..pipe.client.tx_cap], false),
12414            Ok(29)
12415        );
12416        assert_eq!(pipe.advance(), Ok(()));
12417
12418        assert!(!pipe.server.stream_readable(0)); // nothing can be consumed
12419
12420        // The client has increased its tx_data, and server has received it, so
12421        // it increases flow control accordingly.
12422        assert_eq!(pipe.client.tx_data, 30);
12423        assert_eq!(pipe.server.rx_data, 30);
12424        assert_eq!(pipe.client.tx_cap, 45);
12425    }
12426
12427    #[rstest]
12428    fn stream_shutdown_uni(
12429        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12430    ) {
12431        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12432        assert_eq!(pipe.handshake(), Ok(()));
12433
12434        // Exchange some data on uni streams.
12435        assert_eq!(pipe.client.stream_send(2, b"hello, world", false), Ok(10));
12436        assert_eq!(pipe.server.stream_send(3, b"hello, world", false), Ok(10));
12437        assert_eq!(pipe.advance(), Ok(()));
12438
12439        // Test local and remote shutdown.
12440        assert_eq!(pipe.client.stream_shutdown(2, Shutdown::Write, 42), Ok(()));
12441        assert_eq!(
12442            pipe.client.stream_shutdown(2, Shutdown::Read, 42),
12443            Err(Error::InvalidStreamState(2))
12444        );
12445
12446        assert_eq!(
12447            pipe.client.stream_shutdown(3, Shutdown::Write, 42),
12448            Err(Error::InvalidStreamState(3))
12449        );
12450        assert_eq!(pipe.client.stream_shutdown(3, Shutdown::Read, 42), Ok(()));
12451    }
12452
12453    #[rstest]
12454    fn stream_shutdown_write(
12455        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12456    ) {
12457        let mut buf = [0; 65535];
12458
12459        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12460        assert_eq!(pipe.handshake(), Ok(()));
12461
12462        // Client sends some data.
12463        assert_eq!(pipe.client.stream_send(4, b"hello, world", false), Ok(12));
12464        assert_eq!(pipe.advance(), Ok(()));
12465
12466        let mut r = pipe.server.readable();
12467        assert_eq!(r.next(), Some(4));
12468        assert_eq!(r.next(), None);
12469
12470        let mut r = pipe.server.writable();
12471        assert_eq!(r.next(), Some(4));
12472        assert_eq!(r.next(), None);
12473
12474        assert_eq!(pipe.client.streams.len(), 1);
12475        assert_eq!(pipe.server.streams.len(), 1);
12476
12477        // Server sends some data.
12478        assert_eq!(pipe.server.stream_send(4, b"goodbye, world", false), Ok(14));
12479        assert_eq!(pipe.advance(), Ok(()));
12480
12481        // Server shuts down stream.
12482        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Write, 42), Ok(()));
12483
12484        let mut r = pipe.server.writable();
12485        assert_eq!(r.next(), None);
12486
12487        let (len, _) = pipe.server.send(&mut buf).unwrap();
12488
12489        let mut dummy = buf[..len].to_vec();
12490
12491        let frames =
12492            testing::decode_pkt(&mut pipe.client, &mut dummy[..len]).unwrap();
12493        let mut iter = frames.iter();
12494
12495        assert_eq!(
12496            iter.next(),
12497            Some(&frame::Frame::ResetStream {
12498                stream_id: 4,
12499                error_code: 42,
12500                final_size: 14,
12501            })
12502        );
12503
12504        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
12505
12506        assert_eq!(pipe.advance(), Ok(()));
12507
12508        // Sending more data is forbidden.
12509        assert_eq!(
12510            pipe.server.stream_send(4, b"bye", false),
12511            Err(Error::FinalSize)
12512        );
12513
12514        // Client sends some data and closes the stream.
12515        assert_eq!(pipe.client.stream_send(4, b"bye", true), Ok(3));
12516        assert_eq!(pipe.advance(), Ok(()));
12517
12518        // Server reads the data.
12519        let mut r = pipe.server.readable();
12520        assert_eq!(r.next(), Some(4));
12521        assert_eq!(r.next(), None);
12522
12523        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((15, true)));
12524
12525        // Client processes readable streams.
12526        let mut r = pipe.client.readable();
12527        assert_eq!(r.next(), Some(4));
12528        assert_eq!(r.next(), None);
12529
12530        assert_eq!(
12531            pipe.client.stream_recv(4, &mut buf),
12532            Err(Error::StreamReset(42))
12533        );
12534
12535        // Stream is collected on both sides.
12536        assert_eq!(pipe.client.streams.len(), 0);
12537        assert_eq!(pipe.server.streams.len(), 0);
12538
12539        assert_eq!(
12540            pipe.server.stream_shutdown(4, Shutdown::Write, 0),
12541            Err(Error::Done)
12542        );
12543    }
12544
12545    #[rstest]
12546    /// Tests that shutting down a stream restores flow control for unsent data.
12547    fn stream_shutdown_write_unsent_tx_cap(
12548        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12549    ) {
12550        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12551        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
12552        config
12553            .load_cert_chain_from_pem_file("examples/cert.crt")
12554            .unwrap();
12555        config
12556            .load_priv_key_from_pem_file("examples/cert.key")
12557            .unwrap();
12558        config
12559            .set_application_protos(&[b"proto1", b"proto2"])
12560            .unwrap();
12561        config.set_initial_max_data(15);
12562        config.set_initial_max_stream_data_bidi_local(30);
12563        config.set_initial_max_stream_data_bidi_remote(30);
12564        config.set_initial_max_stream_data_uni(30);
12565        config.set_initial_max_streams_bidi(3);
12566        config.set_initial_max_streams_uni(0);
12567        config.verify_peer(false);
12568
12569        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12570        assert_eq!(pipe.handshake(), Ok(()));
12571
12572        // Client sends some data.
12573        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
12574        assert_eq!(pipe.advance(), Ok(()));
12575
12576        let mut r = pipe.server.readable();
12577        assert_eq!(r.next(), Some(4));
12578        assert_eq!(r.next(), None);
12579
12580        let mut b = [0; 15];
12581        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
12582
12583        // Server sends some data.
12584        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12585        assert_eq!(pipe.advance(), Ok(()));
12586
12587        // Server buffers some data, until send capacity limit reached.
12588        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12589        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12590        assert_eq!(
12591            pipe.server.stream_send(4, b"hello", false),
12592            Err(Error::Done)
12593        );
12594
12595        // Client shouldn't update flow control.
12596        assert!(!pipe.client.should_update_max_data());
12597
12598        // Server shuts down stream.
12599        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Write, 42), Ok(()));
12600        assert_eq!(pipe.advance(), Ok(()));
12601
12602        // Server can now send more data (on a different stream).
12603        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
12604        assert_eq!(pipe.advance(), Ok(()));
12605
12606        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12607        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12608        assert_eq!(
12609            pipe.server.stream_send(8, b"hello", false),
12610            Err(Error::Done)
12611        );
12612        assert_eq!(pipe.advance(), Ok(()));
12613    }
12614
12615    #[rstest]
12616    /// Tests that the order of flushable streams scheduled on the wire is the
12617    /// same as the order of `stream_send()` calls done by the application.
12618    fn stream_round_robin(
12619        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12620    ) {
12621        let mut buf = [0; 65535];
12622
12623        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12624        assert_eq!(pipe.handshake(), Ok(()));
12625
12626        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12627        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12628        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12629
12630        let (len, _) = pipe.client.send(&mut buf).unwrap();
12631
12632        let frames =
12633            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12634
12635        let mut iter = frames.iter();
12636
12637        // Skip ACK frame.
12638        iter.next();
12639
12640        assert_eq!(
12641            iter.next(),
12642            Some(&frame::Frame::Stream {
12643                stream_id: 8,
12644                data: <RangeBuf>::from(b"aaaaa", 0, false),
12645            })
12646        );
12647
12648        let (len, _) = pipe.client.send(&mut buf).unwrap();
12649
12650        let frames =
12651            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12652
12653        assert_eq!(
12654            frames.first(),
12655            Some(&frame::Frame::Stream {
12656                stream_id: 0,
12657                data: <RangeBuf>::from(b"aaaaa", 0, false),
12658            })
12659        );
12660
12661        let (len, _) = pipe.client.send(&mut buf).unwrap();
12662
12663        let frames =
12664            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12665
12666        assert_eq!(
12667            frames.first(),
12668            Some(&frame::Frame::Stream {
12669                stream_id: 4,
12670                data: <RangeBuf>::from(b"aaaaa", 0, false),
12671            })
12672        );
12673    }
12674
12675    #[rstest]
12676    /// Tests the readable iterator.
12677    fn stream_readable(
12678        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12679    ) {
12680        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12681        assert_eq!(pipe.handshake(), Ok(()));
12682
12683        // No readable streams.
12684        let mut r = pipe.client.readable();
12685        assert_eq!(r.next(), None);
12686
12687        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12688
12689        let mut r = pipe.client.readable();
12690        assert_eq!(r.next(), None);
12691
12692        let mut r = pipe.server.readable();
12693        assert_eq!(r.next(), None);
12694
12695        assert_eq!(pipe.advance(), Ok(()));
12696
12697        // Server received stream.
12698        let mut r = pipe.server.readable();
12699        assert_eq!(r.next(), Some(0));
12700        assert_eq!(r.next(), None);
12701
12702        assert_eq!(
12703            pipe.server.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12704            Ok(15)
12705        );
12706        assert_eq!(pipe.advance(), Ok(()));
12707
12708        let mut r = pipe.client.readable();
12709        assert_eq!(r.next(), Some(0));
12710        assert_eq!(r.next(), None);
12711
12712        // Client drains stream.
12713        let mut b = [0; 15];
12714        pipe.client.stream_recv(0, &mut b).unwrap();
12715        assert_eq!(pipe.advance(), Ok(()));
12716
12717        let mut r = pipe.client.readable();
12718        assert_eq!(r.next(), None);
12719
12720        // Server shuts down stream.
12721        let mut r = pipe.server.readable();
12722        assert_eq!(r.next(), Some(0));
12723        assert_eq!(r.next(), None);
12724
12725        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Read, 0), Ok(()));
12726
12727        let mut r = pipe.server.readable();
12728        assert_eq!(r.next(), None);
12729
12730        // Client creates multiple streams.
12731        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12732        assert_eq!(pipe.advance(), Ok(()));
12733
12734        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12735        assert_eq!(pipe.advance(), Ok(()));
12736
12737        let mut r = pipe.server.readable();
12738        assert_eq!(r.len(), 2);
12739
12740        assert!(r.next().is_some());
12741        assert!(r.next().is_some());
12742        assert!(r.next().is_none());
12743
12744        assert_eq!(r.len(), 0);
12745    }
12746
12747    #[rstest]
12748    /// Tests the writable iterator.
12749    fn stream_writable(
12750        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12751    ) {
12752        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12753        assert_eq!(pipe.handshake(), Ok(()));
12754
12755        // No writable streams.
12756        let mut w = pipe.client.writable();
12757        assert_eq!(w.next(), None);
12758
12759        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12760
12761        // Client created stream.
12762        let mut w = pipe.client.writable();
12763        assert_eq!(w.next(), Some(0));
12764        assert_eq!(w.next(), None);
12765
12766        assert_eq!(pipe.advance(), Ok(()));
12767
12768        // Server created stream.
12769        let mut w = pipe.server.writable();
12770        assert_eq!(w.next(), Some(0));
12771        assert_eq!(w.next(), None);
12772
12773        assert_eq!(
12774            pipe.server.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12775            Ok(15)
12776        );
12777
12778        // Server stream is full.
12779        let mut w = pipe.server.writable();
12780        assert_eq!(w.next(), None);
12781
12782        assert_eq!(pipe.advance(), Ok(()));
12783
12784        // Client drains stream.
12785        let mut b = [0; 15];
12786        pipe.client.stream_recv(0, &mut b).unwrap();
12787        assert_eq!(pipe.advance(), Ok(()));
12788
12789        // Server stream is writable again.
12790        let mut w = pipe.server.writable();
12791        assert_eq!(w.next(), Some(0));
12792        assert_eq!(w.next(), None);
12793
12794        // Server shuts down stream.
12795        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Write, 0), Ok(()));
12796
12797        let mut w = pipe.server.writable();
12798        assert_eq!(w.next(), None);
12799
12800        // Client creates multiple streams.
12801        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12802        assert_eq!(pipe.advance(), Ok(()));
12803
12804        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12805        assert_eq!(pipe.advance(), Ok(()));
12806
12807        let mut w = pipe.server.writable();
12808        assert_eq!(w.len(), 2);
12809
12810        assert!(w.next().is_some());
12811        assert!(w.next().is_some());
12812        assert!(w.next().is_none());
12813
12814        assert_eq!(w.len(), 0);
12815
12816        // Server finishes stream.
12817        assert_eq!(pipe.server.stream_send(8, b"aaaaa", true), Ok(5));
12818
12819        let mut w = pipe.server.writable();
12820        assert_eq!(w.next(), Some(4));
12821        assert_eq!(w.next(), None);
12822    }
12823
12824    #[rstest]
12825    fn stream_writable_blocked(
12826        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12827    ) {
12828        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
12829        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
12830        config
12831            .load_cert_chain_from_pem_file("examples/cert.crt")
12832            .unwrap();
12833        config
12834            .load_priv_key_from_pem_file("examples/cert.key")
12835            .unwrap();
12836        config.set_application_protos(&[b"h3"]).unwrap();
12837        config.set_initial_max_data(70);
12838        config.set_initial_max_stream_data_bidi_local(150000);
12839        config.set_initial_max_stream_data_bidi_remote(150000);
12840        config.set_initial_max_stream_data_uni(150000);
12841        config.set_initial_max_streams_bidi(100);
12842        config.set_initial_max_streams_uni(5);
12843        config.verify_peer(false);
12844
12845        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12846        assert_eq!(pipe.handshake(), Ok(()));
12847
12848        // Client creates stream and sends some data.
12849        let send_buf = [0; 35];
12850        assert_eq!(pipe.client.stream_send(0, &send_buf, false), Ok(35));
12851
12852        // Stream is still writable as it still has capacity.
12853        assert_eq!(pipe.client.stream_writable_next(), Some(0));
12854        assert_eq!(pipe.client.stream_writable_next(), None);
12855
12856        // Client fills stream, which becomes unwritable due to connection
12857        // capacity.
12858        let send_buf = [0; 36];
12859        assert_eq!(pipe.client.stream_send(0, &send_buf, false), Ok(35));
12860
12861        assert_eq!(pipe.client.stream_writable_next(), None);
12862
12863        assert_eq!(pipe.client.tx_cap, 0);
12864
12865        assert_eq!(pipe.advance(), Ok(()));
12866
12867        let mut b = [0; 70];
12868        pipe.server.stream_recv(0, &mut b).unwrap();
12869
12870        assert_eq!(pipe.advance(), Ok(()));
12871
12872        // The connection capacity has increased and the stream is now writable
12873        // again.
12874        assert_ne!(pipe.client.tx_cap, 0);
12875
12876        assert_eq!(pipe.client.stream_writable_next(), Some(0));
12877        assert_eq!(pipe.client.stream_writable_next(), None);
12878    }
12879
12880    #[rstest]
12881    /// Tests that we don't exceed the per-connection flow control limit set by
12882    /// the peer.
12883    fn flow_control_limit_send(
12884        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12885    ) {
12886        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12887        assert_eq!(pipe.handshake(), Ok(()));
12888
12889        assert_eq!(
12890            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12891            Ok(15)
12892        );
12893        assert_eq!(pipe.advance(), Ok(()));
12894        assert_eq!(
12895            pipe.client.stream_send(4, b"aaaaaaaaaaaaaaa", false),
12896            Ok(15)
12897        );
12898        assert_eq!(pipe.advance(), Ok(()));
12899        assert_eq!(pipe.client.stream_send(8, b"a", false), Err(Error::Done));
12900        assert_eq!(pipe.advance(), Ok(()));
12901
12902        let mut r = pipe.server.readable();
12903        assert!(r.next().is_some());
12904        assert!(r.next().is_some());
12905        assert!(r.next().is_none());
12906    }
12907
12908    #[rstest]
12909    /// Tests that invalid packets received before any other valid ones cause
12910    /// the server to close the connection immediately.
12911    fn invalid_initial_server(
12912        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12913    ) {
12914        let mut buf = [0; 65535];
12915        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12916
12917        let frames = [frame::Frame::Padding { len: 10 }];
12918
12919        let written = testing::encode_pkt(
12920            &mut pipe.client,
12921            packet::Type::Initial,
12922            &frames,
12923            &mut buf,
12924        )
12925        .unwrap();
12926
12927        // Corrupt the packets's last byte to make decryption fail (the last
12928        // byte is part of the AEAD tag, so changing it means that the packet
12929        // cannot be authenticated during decryption).
12930        buf[written - 1] = !buf[written - 1];
12931
12932        assert_eq!(pipe.server.timeout(), None);
12933
12934        assert_eq!(
12935            pipe.server_recv(&mut buf[..written]),
12936            Err(Error::CryptoFail)
12937        );
12938
12939        assert!(pipe.server.is_closed());
12940    }
12941
12942    #[rstest]
12943    /// Tests that invalid Initial packets received to cause
12944    /// the client to close the connection immediately.
12945    fn invalid_initial_client(
12946        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12947    ) {
12948        let mut buf = [0; 65535];
12949        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12950
12951        // Client sends initial flight.
12952        let (len, _) = pipe.client.send(&mut buf).unwrap();
12953
12954        // Server sends initial flight.
12955        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(1200));
12956
12957        let frames = [frame::Frame::Padding { len: 10 }];
12958
12959        let written = testing::encode_pkt(
12960            &mut pipe.server,
12961            packet::Type::Initial,
12962            &frames,
12963            &mut buf,
12964        )
12965        .unwrap();
12966
12967        // Corrupt the packets's last byte to make decryption fail (the last
12968        // byte is part of the AEAD tag, so changing it means that the packet
12969        // cannot be authenticated during decryption).
12970        buf[written - 1] = !buf[written - 1];
12971
12972        // Client will ignore invalid packet.
12973        assert_eq!(pipe.client_recv(&mut buf[..written]), Ok(71));
12974
12975        // The connection should be alive...
12976        assert!(!pipe.client.is_closed());
12977
12978        // ...and the idle timeout should be armed.
12979        assert!(pipe.client.idle_timer.is_some());
12980    }
12981
12982    #[rstest]
12983    /// Tests that packets with invalid payload length received before any other
12984    /// valid packet cause the server to close the connection immediately.
12985    fn invalid_initial_payload(
12986        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
12987    ) {
12988        let mut buf = [0; 65535];
12989        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
12990
12991        let mut b = octets::OctetsMut::with_slice(&mut buf);
12992
12993        let epoch = packet::Type::Initial.to_epoch().unwrap();
12994
12995        let pn = 0;
12996        let pn_len = packet::pkt_num_len(pn, 0);
12997
12998        let dcid = pipe.client.destination_id();
12999        let scid = pipe.client.source_id();
13000
13001        let hdr = Header {
13002            ty: packet::Type::Initial,
13003            version: pipe.client.version,
13004            dcid: ConnectionId::from_ref(&dcid),
13005            scid: ConnectionId::from_ref(&scid),
13006            pkt_num: 0,
13007            pkt_num_len: pn_len,
13008            token: pipe.client.token.clone(),
13009            versions: None,
13010            key_phase: false,
13011        };
13012
13013        hdr.to_bytes(&mut b).unwrap();
13014
13015        // Payload length is invalid!!!
13016        let payload_len = 4096;
13017
13018        let len = pn_len + payload_len;
13019        b.put_varint(len as u64).unwrap();
13020
13021        packet::encode_pkt_num(pn, pn_len, &mut b).unwrap();
13022
13023        let payload_offset = b.off();
13024
13025        let frames = [frame::Frame::Padding { len: 10 }];
13026
13027        for frame in &frames {
13028            frame.to_bytes(&mut b).unwrap();
13029        }
13030
13031        let crypto_ctx = &mut pipe.client.crypto_ctx[epoch];
13032
13033        // Use correct payload length when encrypting the packet.
13034        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
13035
13036        let aead = crypto_ctx.crypto_seal.as_ref().unwrap();
13037
13038        let written = packet::encrypt_pkt(
13039            &mut b,
13040            pn,
13041            pn_len,
13042            payload_len,
13043            payload_offset,
13044            None,
13045            aead,
13046        )
13047        .unwrap();
13048
13049        assert_eq!(pipe.server.timeout(), None);
13050
13051        assert_eq!(
13052            pipe.server_recv(&mut buf[..written]),
13053            Err(Error::InvalidPacket)
13054        );
13055
13056        assert!(pipe.server.is_closed());
13057    }
13058
13059    #[rstest]
13060    /// Tests that invalid packets don't cause the connection to be closed.
13061    fn invalid_packet(
13062        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13063    ) {
13064        let mut buf = [0; 65535];
13065
13066        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13067        assert_eq!(pipe.handshake(), Ok(()));
13068
13069        let frames = [frame::Frame::Padding { len: 10 }];
13070
13071        let written = testing::encode_pkt(
13072            &mut pipe.client,
13073            packet::Type::Short,
13074            &frames,
13075            &mut buf,
13076        )
13077        .unwrap();
13078
13079        // Corrupt the packets's last byte to make decryption fail (the last
13080        // byte is part of the AEAD tag, so changing it means that the packet
13081        // cannot be authenticated during decryption).
13082        buf[written - 1] = !buf[written - 1];
13083
13084        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
13085
13086        // Corrupt the packets's first byte to make the header fail decoding.
13087        buf[0] = 255;
13088
13089        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
13090    }
13091
13092    #[rstest]
13093    fn recv_empty_buffer(
13094        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13095    ) {
13096        let mut buf = [0; 65535];
13097
13098        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13099        assert_eq!(pipe.handshake(), Ok(()));
13100
13101        assert_eq!(pipe.server_recv(&mut buf[..0]), Err(Error::BufferTooShort));
13102    }
13103
13104    #[rstest]
13105    fn stop_sending_before_flushed_packets(
13106        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13107    ) {
13108        let mut b = [0; 15];
13109
13110        let mut buf = [0; 65535];
13111
13112        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13113        assert_eq!(pipe.handshake(), Ok(()));
13114
13115        // Client sends some data, and closes stream.
13116        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
13117        assert_eq!(pipe.advance(), Ok(()));
13118
13119        // Server gets data.
13120        let mut r = pipe.server.readable();
13121        assert_eq!(r.next(), Some(0));
13122        assert_eq!(r.next(), None);
13123
13124        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
13125        assert!(pipe.server.stream_finished(0));
13126
13127        let mut r = pipe.server.readable();
13128        assert_eq!(r.next(), None);
13129
13130        // Server sends data, until blocked.
13131        let mut r = pipe.server.writable();
13132        assert_eq!(r.next(), Some(0));
13133        assert_eq!(r.next(), None);
13134
13135        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {}
13136
13137        let mut r = pipe.server.writable();
13138        assert_eq!(r.next(), None);
13139
13140        // Client sends STOP_SENDING.
13141        let frames = [frame::Frame::StopSending {
13142            stream_id: 0,
13143            error_code: 42,
13144        }];
13145
13146        let pkt_type = packet::Type::Short;
13147        let len = pipe
13148            .send_pkt_to_server(pkt_type, &frames, &mut buf)
13149            .unwrap();
13150
13151        // Server sent a RESET_STREAM frame in response.
13152        let frames =
13153            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
13154
13155        let mut iter = frames.iter();
13156
13157        // Skip ACK frame.
13158        iter.next();
13159
13160        assert_eq!(
13161            iter.next(),
13162            Some(&frame::Frame::ResetStream {
13163                stream_id: 0,
13164                error_code: 42,
13165                final_size: 0,
13166            })
13167        );
13168
13169        // Stream is writable, but writing returns an error.
13170        let mut r = pipe.server.writable();
13171        assert_eq!(r.next(), Some(0));
13172        assert_eq!(r.next(), None);
13173
13174        assert_eq!(
13175            pipe.server.stream_send(0, b"world", true),
13176            Err(Error::StreamStopped(42)),
13177        );
13178
13179        assert_eq!(pipe.server.streams.len(), 1);
13180
13181        // Client acks RESET_STREAM frame.
13182        let mut ranges = ranges::RangeSet::default();
13183        ranges.insert(0..6);
13184
13185        let frames = [frame::Frame::ACK {
13186            ack_delay: 15,
13187            ranges,
13188            ecn_counts: None,
13189        }];
13190
13191        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
13192
13193        // Client has ACK'd the RESET_STREAM so the stream is collected.
13194        assert_eq!(pipe.server.streams.len(), 0);
13195    }
13196
13197    #[rstest]
13198    fn reset_before_flushed_packets(
13199        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13200    ) {
13201        let mut b = [0; 15];
13202
13203        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13204        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13205        config
13206            .load_cert_chain_from_pem_file("examples/cert.crt")
13207            .unwrap();
13208        config
13209            .load_priv_key_from_pem_file("examples/cert.key")
13210            .unwrap();
13211        config
13212            .set_application_protos(&[b"proto1", b"proto2"])
13213            .unwrap();
13214        config.set_initial_max_data(30);
13215        config.set_initial_max_stream_data_bidi_local(5);
13216        config.set_initial_max_stream_data_bidi_remote(15);
13217        config.set_initial_max_streams_bidi(3);
13218        config.verify_peer(false);
13219
13220        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13221        assert_eq!(pipe.handshake(), Ok(()));
13222
13223        // Client sends some data, and closes stream.
13224        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
13225        assert_eq!(pipe.advance(), Ok(()));
13226
13227        // Server gets data.
13228        let mut r = pipe.server.readable();
13229        assert_eq!(r.next(), Some(0));
13230        assert_eq!(r.next(), None);
13231
13232        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
13233        assert!(pipe.server.stream_finished(0));
13234
13235        let mut r = pipe.server.readable();
13236        assert_eq!(r.next(), None);
13237
13238        // Server sends data and is blocked by small stream flow control.
13239        let mut r = pipe.server.writable();
13240        assert_eq!(r.next(), Some(0));
13241        assert_eq!(r.next(), None);
13242
13243        assert_eq!(pipe.server.stream_send(0, b"helloworld", false), Ok(5));
13244        assert_eq!(pipe.advance(), Ok(()));
13245
13246        // Client reads to give flow control back.
13247        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((5, false)));
13248        assert_eq!(pipe.advance(), Ok(()));
13249
13250        // Server writes stream data and resets the stream before sending a
13251        // packet.
13252        assert_eq!(pipe.server.stream_send(0, b"world", false), Ok(5));
13253        pipe.server.stream_shutdown(0, Shutdown::Write, 42).unwrap();
13254        assert_eq!(pipe.advance(), Ok(()));
13255
13256        // Client has ACK'd the RESET_STREAM so the stream is collected.
13257        assert_eq!(pipe.server.streams.len(), 0);
13258    }
13259
13260    #[rstest]
13261    /// Tests that the MAX_STREAMS frame is sent for bidirectional streams.
13262    fn stream_limit_update_bidi(
13263        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13264    ) {
13265        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13266        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13267        config
13268            .load_cert_chain_from_pem_file("examples/cert.crt")
13269            .unwrap();
13270        config
13271            .load_priv_key_from_pem_file("examples/cert.key")
13272            .unwrap();
13273        config
13274            .set_application_protos(&[b"proto1", b"proto2"])
13275            .unwrap();
13276        config.set_initial_max_data(30);
13277        config.set_initial_max_stream_data_bidi_local(15);
13278        config.set_initial_max_stream_data_bidi_remote(15);
13279        config.set_initial_max_stream_data_uni(10);
13280        config.set_initial_max_streams_bidi(3);
13281        config.set_initial_max_streams_uni(0);
13282        config.verify_peer(false);
13283
13284        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13285        assert_eq!(pipe.handshake(), Ok(()));
13286
13287        // Client sends stream data.
13288        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
13289        assert_eq!(pipe.advance(), Ok(()));
13290
13291        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
13292        assert_eq!(pipe.advance(), Ok(()));
13293
13294        assert_eq!(pipe.client.stream_send(4, b"b", true), Ok(1));
13295        assert_eq!(pipe.advance(), Ok(()));
13296
13297        assert_eq!(pipe.client.stream_send(0, b"b", true), Ok(1));
13298        assert_eq!(pipe.advance(), Ok(()));
13299
13300        // Server reads stream data.
13301        let mut b = [0; 15];
13302        pipe.server.stream_recv(0, &mut b).unwrap();
13303        pipe.server.stream_recv(4, &mut b).unwrap();
13304        assert_eq!(pipe.advance(), Ok(()));
13305
13306        // Server sends stream data, with fin.
13307        assert_eq!(pipe.server.stream_send(0, b"a", false), Ok(1));
13308        assert_eq!(pipe.advance(), Ok(()));
13309
13310        assert_eq!(pipe.server.stream_send(4, b"a", false), Ok(1));
13311        assert_eq!(pipe.advance(), Ok(()));
13312
13313        assert_eq!(pipe.server.stream_send(4, b"b", true), Ok(1));
13314        assert_eq!(pipe.advance(), Ok(()));
13315
13316        assert_eq!(pipe.server.stream_send(0, b"b", true), Ok(1));
13317
13318        // Server sends MAX_STREAMS.
13319        assert_eq!(pipe.advance(), Ok(()));
13320
13321        // Client tries to create new streams.
13322        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
13323        assert_eq!(pipe.advance(), Ok(()));
13324
13325        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
13326        assert_eq!(pipe.advance(), Ok(()));
13327
13328        assert_eq!(pipe.client.stream_send(16, b"a", false), Ok(1));
13329        assert_eq!(pipe.advance(), Ok(()));
13330
13331        assert_eq!(
13332            pipe.client.stream_send(20, b"a", false),
13333            Err(Error::StreamLimit)
13334        );
13335
13336        assert_eq!(pipe.server.readable().len(), 3);
13337    }
13338
13339    #[rstest]
13340    /// Tests that the MAX_STREAMS frame is sent for unidirectional streams.
13341    fn stream_limit_update_uni(
13342        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13343    ) {
13344        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13345        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13346        config
13347            .load_cert_chain_from_pem_file("examples/cert.crt")
13348            .unwrap();
13349        config
13350            .load_priv_key_from_pem_file("examples/cert.key")
13351            .unwrap();
13352        config
13353            .set_application_protos(&[b"proto1", b"proto2"])
13354            .unwrap();
13355        config.set_initial_max_data(30);
13356        config.set_initial_max_stream_data_bidi_local(15);
13357        config.set_initial_max_stream_data_bidi_remote(15);
13358        config.set_initial_max_stream_data_uni(10);
13359        config.set_initial_max_streams_bidi(0);
13360        config.set_initial_max_streams_uni(3);
13361        config.verify_peer(false);
13362
13363        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13364        assert_eq!(pipe.handshake(), Ok(()));
13365
13366        // Client sends stream data.
13367        assert_eq!(pipe.client.stream_send(2, b"a", false), Ok(1));
13368        assert_eq!(pipe.advance(), Ok(()));
13369
13370        assert_eq!(pipe.client.stream_send(6, b"a", false), Ok(1));
13371        assert_eq!(pipe.advance(), Ok(()));
13372
13373        assert_eq!(pipe.client.stream_send(6, b"b", true), Ok(1));
13374        assert_eq!(pipe.advance(), Ok(()));
13375
13376        assert_eq!(pipe.client.stream_send(2, b"b", true), Ok(1));
13377        assert_eq!(pipe.advance(), Ok(()));
13378
13379        // Server reads stream data.
13380        let mut b = [0; 15];
13381        pipe.server.stream_recv(2, &mut b).unwrap();
13382        pipe.server.stream_recv(6, &mut b).unwrap();
13383
13384        // Server sends MAX_STREAMS.
13385        assert_eq!(pipe.advance(), Ok(()));
13386
13387        // Client tries to create new streams.
13388        assert_eq!(pipe.client.stream_send(10, b"a", false), Ok(1));
13389        assert_eq!(pipe.advance(), Ok(()));
13390
13391        assert_eq!(pipe.client.stream_send(14, b"a", false), Ok(1));
13392        assert_eq!(pipe.advance(), Ok(()));
13393
13394        assert_eq!(pipe.client.stream_send(18, b"a", false), Ok(1));
13395        assert_eq!(pipe.advance(), Ok(()));
13396
13397        assert_eq!(
13398            pipe.client.stream_send(22, b"a", false),
13399            Err(Error::StreamLimit)
13400        );
13401
13402        assert_eq!(pipe.server.readable().len(), 3);
13403    }
13404
13405    #[rstest]
13406    /// Tests that the stream's fin flag is properly flushed even if there's no
13407    /// data in the buffer, and that the buffer becomes readable on the other
13408    /// side.
13409    fn stream_zero_length_fin(
13410        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13411    ) {
13412        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13413        assert_eq!(pipe.handshake(), Ok(()));
13414
13415        assert_eq!(
13416            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
13417            Ok(15)
13418        );
13419        assert_eq!(pipe.advance(), Ok(()));
13420
13421        let mut r = pipe.server.readable();
13422        assert_eq!(r.next(), Some(0));
13423        assert!(r.next().is_none());
13424
13425        let mut b = [0; 15];
13426        pipe.server.stream_recv(0, &mut b).unwrap();
13427        assert_eq!(pipe.advance(), Ok(()));
13428
13429        // Client sends zero-length frame.
13430        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
13431        assert_eq!(pipe.advance(), Ok(()));
13432
13433        // Stream should be readable on the server after receiving empty fin.
13434        let mut r = pipe.server.readable();
13435        assert_eq!(r.next(), Some(0));
13436        assert!(r.next().is_none());
13437
13438        let mut b = [0; 15];
13439        pipe.server.stream_recv(0, &mut b).unwrap();
13440        assert_eq!(pipe.advance(), Ok(()));
13441
13442        // Client sends zero-length frame (again).
13443        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
13444        assert_eq!(pipe.advance(), Ok(()));
13445
13446        // Stream should _not_ be readable on the server after receiving empty
13447        // fin, because it was already finished.
13448        let mut r = pipe.server.readable();
13449        assert_eq!(r.next(), None);
13450    }
13451
13452    #[rstest]
13453    /// Tests that the stream's fin flag is properly flushed even if there's no
13454    /// data in the buffer, that the buffer becomes readable on the other
13455    /// side and stays readable even if the stream is fin'd locally.
13456    fn stream_zero_length_fin_deferred_collection(
13457        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13458    ) {
13459        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13460        assert_eq!(pipe.handshake(), Ok(()));
13461
13462        assert_eq!(
13463            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
13464            Ok(15)
13465        );
13466        assert_eq!(pipe.advance(), Ok(()));
13467
13468        let mut r = pipe.server.readable();
13469        assert_eq!(r.next(), Some(0));
13470        assert!(r.next().is_none());
13471
13472        let mut b = [0; 15];
13473        pipe.server.stream_recv(0, &mut b).unwrap();
13474        assert_eq!(pipe.advance(), Ok(()));
13475
13476        // Client sends zero-length frame.
13477        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
13478        assert_eq!(pipe.advance(), Ok(()));
13479
13480        // Server sends zero-length frame.
13481        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
13482        assert_eq!(pipe.advance(), Ok(()));
13483
13484        // Stream should be readable on the server after receiving empty fin.
13485        let mut r = pipe.server.readable();
13486        assert_eq!(r.next(), Some(0));
13487        assert!(r.next().is_none());
13488
13489        let mut b = [0; 15];
13490        pipe.server.stream_recv(0, &mut b).unwrap();
13491        assert_eq!(pipe.advance(), Ok(()));
13492
13493        // Client sends zero-length frame (again).
13494        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
13495        assert_eq!(pipe.advance(), Ok(()));
13496
13497        // Stream should _not_ be readable on the server after receiving empty
13498        // fin, because it was already finished.
13499        let mut r = pipe.server.readable();
13500        assert_eq!(r.next(), None);
13501
13502        // Stream _is_readable on the client side.
13503        let mut r = pipe.client.readable();
13504        assert_eq!(r.next(), Some(0));
13505
13506        pipe.client.stream_recv(0, &mut b).unwrap();
13507        assert_eq!(pipe.advance(), Ok(()));
13508
13509        // Stream is completed and _is not_ readable.
13510        let mut r = pipe.client.readable();
13511        assert_eq!(r.next(), None);
13512    }
13513
13514    #[rstest]
13515    /// Tests that the stream gets created with stream_send() even if there's
13516    /// no data in the buffer and the fin flag is not set.
13517    fn stream_zero_length_non_fin(
13518        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13519    ) {
13520        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13521        assert_eq!(pipe.handshake(), Ok(()));
13522
13523        assert_eq!(pipe.client.stream_send(0, b"", false), Ok(0));
13524
13525        // The stream now should have been created.
13526        assert_eq!(pipe.client.streams.len(), 1);
13527        assert_eq!(pipe.advance(), Ok(()));
13528
13529        // Sending an empty non-fin should not change any stream state on the
13530        // other side.
13531        let mut r = pipe.server.readable();
13532        assert!(r.next().is_none());
13533    }
13534
13535    #[rstest]
13536    /// Tests that completed streams are garbage collected.
13537    fn collect_streams(
13538        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13539    ) {
13540        let mut buf = [0; 65535];
13541
13542        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13543        assert_eq!(pipe.handshake(), Ok(()));
13544
13545        assert_eq!(pipe.client.streams.len(), 0);
13546        assert_eq!(pipe.server.streams.len(), 0);
13547
13548        assert_eq!(pipe.client.stream_send(0, b"aaaaa", true), Ok(5));
13549        assert_eq!(pipe.advance(), Ok(()));
13550
13551        assert!(!pipe.client.stream_finished(0));
13552        assert!(!pipe.server.stream_finished(0));
13553
13554        assert_eq!(pipe.client.streams.len(), 1);
13555        assert_eq!(pipe.server.streams.len(), 1);
13556
13557        let mut b = [0; 5];
13558        pipe.server.stream_recv(0, &mut b).unwrap();
13559        assert_eq!(pipe.advance(), Ok(()));
13560
13561        assert_eq!(pipe.server.stream_send(0, b"aaaaa", true), Ok(5));
13562        assert_eq!(pipe.advance(), Ok(()));
13563
13564        assert!(!pipe.client.stream_finished(0));
13565        assert!(pipe.server.stream_finished(0));
13566
13567        assert_eq!(pipe.client.streams.len(), 1);
13568        assert_eq!(pipe.server.streams.len(), 0);
13569
13570        let mut b = [0; 5];
13571        pipe.client.stream_recv(0, &mut b).unwrap();
13572        assert_eq!(pipe.advance(), Ok(()));
13573
13574        assert_eq!(pipe.client.streams.len(), 0);
13575        assert_eq!(pipe.server.streams.len(), 0);
13576
13577        assert!(pipe.client.stream_finished(0));
13578        assert!(pipe.server.stream_finished(0));
13579
13580        assert_eq!(pipe.client.stream_send(0, b"", true), Err(Error::Done));
13581
13582        let frames = [frame::Frame::Stream {
13583            stream_id: 0,
13584            data: <RangeBuf>::from(b"aa", 0, false),
13585        }];
13586
13587        let pkt_type = packet::Type::Short;
13588        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
13589    }
13590
13591    #[test]
13592    fn config_set_cc_algorithm_name() {
13593        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13594
13595        assert_eq!(config.set_cc_algorithm_name("reno"), Ok(()));
13596
13597        // Unknown name.
13598        assert_eq!(
13599            config.set_cc_algorithm_name("???"),
13600            Err(Error::CongestionControl)
13601        );
13602    }
13603
13604    #[rstest]
13605    fn peer_cert(
13606        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13607    ) {
13608        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13609        assert_eq!(pipe.handshake(), Ok(()));
13610
13611        match pipe.client.peer_cert() {
13612            Some(c) => assert_eq!(c.len(), 753),
13613
13614            None => panic!("missing server certificate"),
13615        }
13616    }
13617
13618    #[rstest]
13619    fn peer_cert_chain(
13620        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13621    ) {
13622        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13623        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13624        config
13625            .load_cert_chain_from_pem_file("examples/cert-big.crt")
13626            .unwrap();
13627        config
13628            .load_priv_key_from_pem_file("examples/cert.key")
13629            .unwrap();
13630        config
13631            .set_application_protos(&[b"proto1", b"proto2"])
13632            .unwrap();
13633
13634        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13635        assert_eq!(pipe.handshake(), Ok(()));
13636
13637        match pipe.client.peer_cert_chain() {
13638            Some(c) => assert_eq!(c.len(), 5),
13639
13640            None => panic!("missing server certificate chain"),
13641        }
13642    }
13643
13644    #[rstest]
13645    fn retry(
13646        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13647    ) {
13648        let mut buf = [0; 65535];
13649
13650        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13651        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13652        config
13653            .load_cert_chain_from_pem_file("examples/cert.crt")
13654            .unwrap();
13655        config
13656            .load_priv_key_from_pem_file("examples/cert.key")
13657            .unwrap();
13658        config
13659            .set_application_protos(&[b"proto1", b"proto2"])
13660            .unwrap();
13661
13662        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13663
13664        // Client sends initial flight.
13665        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13666
13667        // Server sends Retry packet.
13668        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13669
13670        let odcid = hdr.dcid.clone();
13671
13672        let mut scid = [0; MAX_CONN_ID_LEN];
13673        rand::rand_bytes(&mut scid[..]);
13674        let scid = ConnectionId::from_ref(&scid);
13675
13676        let token = b"quiche test retry token";
13677
13678        len = packet::retry(
13679            &hdr.scid,
13680            &hdr.dcid,
13681            &scid,
13682            token,
13683            hdr.version,
13684            &mut buf,
13685        )
13686        .unwrap();
13687
13688        // Client receives Retry and sends new Initial.
13689        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13690
13691        let (len, send_info) = pipe.client.send(&mut buf).unwrap();
13692
13693        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13694        assert_eq!(&hdr.token.unwrap(), token);
13695
13696        // Server accepts connection.
13697        pipe.server = accept(
13698            &scid,
13699            Some(&odcid),
13700            testing::Pipe::server_addr(),
13701            send_info.from,
13702            &mut config,
13703        )
13704        .unwrap();
13705        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13706
13707        assert_eq!(pipe.advance(), Ok(()));
13708
13709        assert!(pipe.client.is_established());
13710        assert!(pipe.server.is_established());
13711    }
13712
13713    #[rstest]
13714    fn retry_with_pto(
13715        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13716    ) {
13717        let mut buf = [0; 65535];
13718
13719        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13720        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13721        config
13722            .load_cert_chain_from_pem_file("examples/cert.crt")
13723            .unwrap();
13724        config
13725            .load_priv_key_from_pem_file("examples/cert.key")
13726            .unwrap();
13727        config
13728            .set_application_protos(&[b"proto1", b"proto2"])
13729            .unwrap();
13730
13731        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13732
13733        // Client sends initial flight.
13734        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13735
13736        // Server sends Retry packet.
13737        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13738
13739        let odcid = hdr.dcid.clone();
13740
13741        let mut scid = [0; MAX_CONN_ID_LEN];
13742        rand::rand_bytes(&mut scid[..]);
13743        let scid = ConnectionId::from_ref(&scid);
13744
13745        let token = b"quiche test retry token";
13746
13747        len = packet::retry(
13748            &hdr.scid,
13749            &hdr.dcid,
13750            &scid,
13751            token,
13752            hdr.version,
13753            &mut buf,
13754        )
13755        .unwrap();
13756
13757        // Client receives Retry and sends new Initial.
13758        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13759
13760        let (len, send_info) = pipe.client.send(&mut buf).unwrap();
13761
13762        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13763        assert_eq!(&hdr.token.unwrap(), token);
13764
13765        // Server accepts connection.
13766        pipe.server = accept(
13767            &scid,
13768            Some(&odcid),
13769            testing::Pipe::server_addr(),
13770            send_info.from,
13771            &mut config,
13772        )
13773        .unwrap();
13774        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13775
13776        // Wait for the client's PTO so it will try to send an Initial again.
13777        let timer = pipe.client.timeout().unwrap();
13778        std::thread::sleep(timer + time::Duration::from_millis(1));
13779        pipe.client.on_timeout();
13780
13781        assert_eq!(pipe.advance(), Ok(()));
13782
13783        assert!(pipe.client.is_established());
13784        assert!(pipe.server.is_established());
13785    }
13786
13787    #[rstest]
13788    fn missing_retry_source_connection_id(
13789        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13790    ) {
13791        let mut buf = [0; 65535];
13792
13793        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13794        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13795        config
13796            .load_cert_chain_from_pem_file("examples/cert.crt")
13797            .unwrap();
13798        config
13799            .load_priv_key_from_pem_file("examples/cert.key")
13800            .unwrap();
13801        config
13802            .set_application_protos(&[b"proto1", b"proto2"])
13803            .unwrap();
13804
13805        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13806
13807        // Client sends initial flight.
13808        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13809
13810        // Server sends Retry packet.
13811        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13812
13813        let mut scid = [0; MAX_CONN_ID_LEN];
13814        rand::rand_bytes(&mut scid[..]);
13815        let scid = ConnectionId::from_ref(&scid);
13816
13817        let token = b"quiche test retry token";
13818
13819        len = packet::retry(
13820            &hdr.scid,
13821            &hdr.dcid,
13822            &scid,
13823            token,
13824            hdr.version,
13825            &mut buf,
13826        )
13827        .unwrap();
13828
13829        // Client receives Retry and sends new Initial.
13830        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13831
13832        let (len, _) = pipe.client.send(&mut buf).unwrap();
13833
13834        // Server accepts connection and send first flight. But original
13835        // destination connection ID is ignored.
13836        let from = "127.0.0.1:1234".parse().unwrap();
13837        pipe.server =
13838            accept(&scid, None, testing::Pipe::server_addr(), from, &mut config)
13839                .unwrap();
13840        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13841
13842        let flight = testing::emit_flight(&mut pipe.server).unwrap();
13843
13844        assert_eq!(
13845            testing::process_flight(&mut pipe.client, flight),
13846            Err(Error::InvalidTransportParam)
13847        );
13848    }
13849
13850    #[rstest]
13851    fn invalid_retry_source_connection_id(
13852        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13853    ) {
13854        let mut buf = [0; 65535];
13855
13856        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13857        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13858        config
13859            .load_cert_chain_from_pem_file("examples/cert.crt")
13860            .unwrap();
13861        config
13862            .load_priv_key_from_pem_file("examples/cert.key")
13863            .unwrap();
13864        config
13865            .set_application_protos(&[b"proto1", b"proto2"])
13866            .unwrap();
13867
13868        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13869
13870        // Client sends initial flight.
13871        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13872
13873        // Server sends Retry packet.
13874        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13875
13876        let mut scid = [0; MAX_CONN_ID_LEN];
13877        rand::rand_bytes(&mut scid[..]);
13878        let scid = ConnectionId::from_ref(&scid);
13879
13880        let token = b"quiche test retry token";
13881
13882        len = packet::retry(
13883            &hdr.scid,
13884            &hdr.dcid,
13885            &scid,
13886            token,
13887            hdr.version,
13888            &mut buf,
13889        )
13890        .unwrap();
13891
13892        // Client receives Retry and sends new Initial.
13893        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13894
13895        let (len, _) = pipe.client.send(&mut buf).unwrap();
13896
13897        // Server accepts connection and send first flight. But original
13898        // destination connection ID is invalid.
13899        let from = "127.0.0.1:1234".parse().unwrap();
13900        let odcid = ConnectionId::from_ref(b"bogus value");
13901        pipe.server = accept(
13902            &scid,
13903            Some(&odcid),
13904            testing::Pipe::server_addr(),
13905            from,
13906            &mut config,
13907        )
13908        .unwrap();
13909        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13910
13911        let flight = testing::emit_flight(&mut pipe.server).unwrap();
13912
13913        assert_eq!(
13914            testing::process_flight(&mut pipe.client, flight),
13915            Err(Error::InvalidTransportParam)
13916        );
13917    }
13918
13919    #[rstest]
13920    /// Tests that a zero-length NEW_TOKEN frame is detected as an error.
13921    fn zero_length_new_token(
13922        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13923    ) {
13924        let mut buf = [0; 65535];
13925
13926        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13927        assert_eq!(pipe.handshake(), Ok(()));
13928
13929        let frames = vec![frame::Frame::NewToken { token: vec![] }];
13930
13931        let pkt_type = packet::Type::Short;
13932
13933        let written =
13934            testing::encode_pkt(&mut pipe.server, pkt_type, &frames, &mut buf)
13935                .unwrap();
13936
13937        assert_eq!(
13938            pipe.client_recv(&mut buf[..written]),
13939            Err(Error::InvalidFrame)
13940        );
13941    }
13942
13943    #[rstest]
13944    /// Tests that a NEW_TOKEN frame sent by client is detected as an error.
13945    fn client_sent_new_token(
13946        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13947    ) {
13948        let mut buf = [0; 65535];
13949
13950        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13951        assert_eq!(pipe.handshake(), Ok(()));
13952
13953        let frames = vec![frame::Frame::NewToken {
13954            token: vec![1, 2, 3],
13955        }];
13956
13957        let pkt_type = packet::Type::Short;
13958
13959        let written =
13960            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
13961                .unwrap();
13962
13963        assert_eq!(
13964            pipe.server_recv(&mut buf[..written]),
13965            Err(Error::InvalidPacket)
13966        );
13967    }
13968
13969    fn check_send(_: &mut impl Send) {}
13970
13971    #[rstest]
13972    fn config_must_be_send(
13973        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13974    ) {
13975        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13976        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13977        check_send(&mut config);
13978    }
13979
13980    #[rstest]
13981    fn connection_must_be_send(
13982        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13983    ) {
13984        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
13985        check_send(&mut pipe.client);
13986    }
13987
13988    fn check_sync(_: &mut impl Sync) {}
13989
13990    #[rstest]
13991    fn config_must_be_sync(
13992        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
13993    ) {
13994        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13995        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
13996        check_sync(&mut config);
13997    }
13998
13999    #[rstest]
14000    fn connection_must_be_sync(
14001        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14002    ) {
14003        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
14004        check_sync(&mut pipe.client);
14005    }
14006
14007    #[rstest]
14008    fn data_blocked(
14009        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14010    ) {
14011        let mut buf = [0; 65535];
14012
14013        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
14014        assert_eq!(pipe.handshake(), Ok(()));
14015
14016        assert_eq!(pipe.client.stream_send(0, b"aaaaaaaaaa", false), Ok(10));
14017        assert_eq!(pipe.client.blocked_limit, None);
14018        assert_eq!(pipe.advance(), Ok(()));
14019
14020        assert_eq!(pipe.client.stream_send(4, b"aaaaaaaaaa", false), Ok(10));
14021        assert_eq!(pipe.client.blocked_limit, None);
14022        assert_eq!(pipe.advance(), Ok(()));
14023
14024        assert_eq!(pipe.client.stream_send(8, b"aaaaaaaaaaa", false), Ok(10));
14025        assert_eq!(pipe.client.blocked_limit, Some(30));
14026
14027        let (len, _) = pipe.client.send(&mut buf).unwrap();
14028        assert_eq!(pipe.client.blocked_limit, None);
14029
14030        let frames =
14031            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
14032
14033        let mut iter = frames.iter();
14034
14035        assert_eq!(iter.next(), Some(&frame::Frame::DataBlocked { limit: 30 }));
14036
14037        assert_eq!(
14038            iter.next(),
14039            Some(&frame::Frame::Stream {
14040                stream_id: 8,
14041                data: <RangeBuf>::from(b"aaaaaaaaaa", 0, false),
14042            })
14043        );
14044
14045        assert_eq!(iter.next(), None);
14046    }
14047
14048    #[rstest]
14049    fn stream_data_blocked(
14050        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14051    ) {
14052        let mut buf = [0; 65535];
14053
14054        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
14055        assert_eq!(pipe.handshake(), Ok(()));
14056
14057        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
14058        assert_eq!(pipe.client.streams.blocked().len(), 0);
14059
14060        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
14061        assert_eq!(pipe.client.streams.blocked().len(), 0);
14062
14063        assert_eq!(pipe.client.stream_send(0, b"aaaaaa", false), Ok(5));
14064        assert_eq!(pipe.client.streams.blocked().len(), 1);
14065
14066        let (len, _) = pipe.client.send(&mut buf).unwrap();
14067        assert_eq!(pipe.client.streams.blocked().len(), 0);
14068
14069        let frames =
14070            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
14071
14072        let mut iter = frames.iter();
14073
14074        // Skip ACK frame.
14075        iter.next();
14076
14077        assert_eq!(
14078            iter.next(),
14079            Some(&frame::Frame::StreamDataBlocked {
14080                stream_id: 0,
14081                limit: 15,
14082            })
14083        );
14084
14085        assert_eq!(
14086            iter.next(),
14087            Some(&frame::Frame::Stream {
14088                stream_id: 0,
14089                data: <RangeBuf>::from(b"aaaaaaaaaaaaaaa", 0, false),
14090            })
14091        );
14092
14093        assert_eq!(iter.next(), None);
14094
14095        // Send from another stream, make sure we don't send STREAM_DATA_BLOCKED
14096        // again.
14097        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
14098
14099        let (len, _) = pipe.client.send(&mut buf).unwrap();
14100        assert_eq!(pipe.client.streams.blocked().len(), 0);
14101
14102        let frames =
14103            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
14104
14105        let mut iter = frames.iter();
14106
14107        assert_eq!(
14108            iter.next(),
14109            Some(&frame::Frame::Stream {
14110                stream_id: 4,
14111                data: <RangeBuf>::from(b"a", 0, false),
14112            })
14113        );
14114
14115        assert_eq!(iter.next(), None);
14116
14117        // Send again from blocked stream and make sure it is not marked as
14118        // blocked again.
14119        assert_eq!(
14120            pipe.client.stream_send(0, b"aaaaaa", false),
14121            Err(Error::Done)
14122        );
14123        assert_eq!(pipe.client.streams.blocked().len(), 0);
14124        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
14125    }
14126
14127    #[rstest]
14128    fn stream_data_blocked_unblocked_flow_control(
14129        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14130    ) {
14131        let mut buf = [0; 65535];
14132        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
14133        assert_eq!(pipe.handshake(), Ok(()));
14134
14135        assert_eq!(
14136            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaah", false),
14137            Ok(15)
14138        );
14139        assert_eq!(pipe.client.streams.blocked().len(), 1);
14140        assert_eq!(pipe.advance(), Ok(()));
14141        assert_eq!(pipe.client.streams.blocked().len(), 0);
14142
14143        // Send again on blocked stream. It's blocked at the same offset as
14144        // previously, so it should not be marked as blocked again.
14145        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
14146        assert_eq!(pipe.client.streams.blocked().len(), 0);
14147
14148        // No matter how many times we try to write stream data tried, no
14149        // packets containing STREAM_BLOCKED should be emitted.
14150        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
14151        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
14152
14153        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
14154        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
14155
14156        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
14157        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
14158
14159        // Now read some data at the server to release flow control.
14160        let mut r = pipe.server.readable();
14161        assert_eq!(r.next(), Some(0));
14162        assert_eq!(r.next(), None);
14163
14164        let mut b = [0; 10];
14165        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((10, false)));
14166        assert_eq!(&b[..10], b"aaaaaaaaaa");
14167        assert_eq!(pipe.advance(), Ok(()));
14168
14169        assert_eq!(pipe.client.stream_send(0, b"hhhhhhhhhh!", false), Ok(10));
14170        assert_eq!(pipe.client.streams.blocked().len(), 1);
14171
14172        let (len, _) = pipe.client.send(&mut buf).unwrap();
14173        assert_eq!(pipe.client.streams.blocked().len(), 0);
14174
14175        let frames =
14176            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
14177
14178        let mut iter = frames.iter();
14179
14180        assert_eq!(
14181            iter.next(),
14182            Some(&frame::Frame::StreamDataBlocked {
14183                stream_id: 0,
14184                limit: 25,
14185            })
14186        );
14187
14188        // don't care about remaining received frames
14189
14190        assert_eq!(pipe.client.stream_send(0, b"!", false), Err(Error::Done));
14191        assert_eq!(pipe.client.streams.blocked().len(), 0);
14192        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
14193    }
14194
14195    #[rstest]
14196    fn app_limited_true(
14197        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14198    ) {
14199        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14200        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14201        config
14202            .set_application_protos(&[b"proto1", b"proto2"])
14203            .unwrap();
14204        config.set_initial_max_data(50000);
14205        config.set_initial_max_stream_data_bidi_local(50000);
14206        config.set_initial_max_stream_data_bidi_remote(50000);
14207        config.set_max_recv_udp_payload_size(1200);
14208        config.verify_peer(false);
14209
14210        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
14211        assert_eq!(pipe.handshake(), Ok(()));
14212
14213        // Client sends stream data.
14214        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14215        assert_eq!(pipe.advance(), Ok(()));
14216
14217        // Server reads stream data.
14218        let mut b = [0; 15];
14219        pipe.server.stream_recv(0, &mut b).unwrap();
14220        assert_eq!(pipe.advance(), Ok(()));
14221
14222        // Server sends stream data smaller than cwnd.
14223        let send_buf = [0; 10000];
14224        assert_eq!(pipe.server.stream_send(0, &send_buf, false), Ok(10000));
14225        assert_eq!(pipe.advance(), Ok(()));
14226
14227        // app_limited should be true because we send less than cwnd.
14228        assert!(pipe
14229            .server
14230            .paths
14231            .get_active()
14232            .expect("no active")
14233            .recovery
14234            .app_limited());
14235    }
14236
14237    #[rstest]
14238    fn app_limited_false(
14239        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14240    ) {
14241        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14242        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14243        config
14244            .set_application_protos(&[b"proto1", b"proto2"])
14245            .unwrap();
14246        config.set_initial_max_data(50000);
14247        config.set_initial_max_stream_data_bidi_local(50000);
14248        config.set_initial_max_stream_data_bidi_remote(50000);
14249        config.set_max_recv_udp_payload_size(1200);
14250        config.verify_peer(false);
14251
14252        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
14253        assert_eq!(pipe.handshake(), Ok(()));
14254
14255        // Client sends stream data.
14256        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14257        assert_eq!(pipe.advance(), Ok(()));
14258
14259        // Server reads stream data.
14260        let mut b = [0; 15];
14261        pipe.server.stream_recv(0, &mut b).unwrap();
14262        assert_eq!(pipe.advance(), Ok(()));
14263
14264        // Server sends stream data bigger than cwnd.
14265        let send_buf1 = [0; 20000];
14266        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
14267
14268        testing::emit_flight(&mut pipe.server).ok();
14269
14270        // We can't create a new packet header because there is no room by cwnd.
14271        // app_limited should be false because we can't send more by cwnd.
14272        assert!(!pipe
14273            .server
14274            .paths
14275            .get_active()
14276            .expect("no active")
14277            .recovery
14278            .app_limited());
14279    }
14280
14281    #[test]
14282    fn tx_cap_factor() {
14283        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14284        config
14285            .set_application_protos(&[b"proto1", b"proto2"])
14286            .unwrap();
14287        config
14288            .load_cert_chain_from_pem_file("examples/cert.crt")
14289            .unwrap();
14290        config
14291            .load_priv_key_from_pem_file("examples/cert.key")
14292            .unwrap();
14293        config.set_initial_max_data(50000);
14294        config.set_initial_max_stream_data_bidi_local(12000);
14295        config.set_initial_max_stream_data_bidi_remote(12000);
14296        config.set_initial_max_streams_bidi(3);
14297        config.set_initial_max_streams_uni(3);
14298        config.set_max_recv_udp_payload_size(1200);
14299        config.verify_peer(false);
14300
14301        config.set_send_capacity_factor(2.0);
14302
14303        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14304        assert_eq!(pipe.handshake(), Ok(()));
14305
14306        // Client sends stream data.
14307        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14308        assert_eq!(pipe.client.stream_send(4, b"a", true), Ok(1));
14309        assert_eq!(pipe.advance(), Ok(()));
14310
14311        let mut b = [0; 50000];
14312
14313        // Server reads stream data.
14314        pipe.server.stream_recv(0, &mut b).unwrap();
14315        assert_eq!(pipe.advance(), Ok(()));
14316
14317        // Server sends stream data bigger than cwnd.
14318        let send_buf = [0; 50000];
14319        assert_eq!(pipe.server.stream_send(0, &send_buf, false), Ok(12000));
14320        assert_eq!(pipe.server.stream_send(4, &send_buf, false), Ok(12000));
14321        assert_eq!(pipe.advance(), Ok(()));
14322
14323        let mut r = pipe.client.readable();
14324        assert_eq!(r.next(), Some(0));
14325        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((12000, false)));
14326
14327        assert_eq!(r.next(), Some(4));
14328        assert_eq!(pipe.client.stream_recv(4, &mut b), Ok((12000, false)));
14329
14330        assert_eq!(r.next(), None);
14331    }
14332
14333    #[rstest]
14334    fn sends_ack_only_pkt_when_full_cwnd_and_ack_elicited(
14335        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14336    ) {
14337        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14338        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14339        config
14340            .load_cert_chain_from_pem_file("examples/cert.crt")
14341            .unwrap();
14342        config
14343            .load_priv_key_from_pem_file("examples/cert.key")
14344            .unwrap();
14345        config
14346            .set_application_protos(&[b"proto1", b"proto2"])
14347            .unwrap();
14348        config.set_initial_max_data(50000);
14349        config.set_initial_max_stream_data_bidi_local(50000);
14350        config.set_initial_max_stream_data_bidi_remote(50000);
14351        config.set_initial_max_streams_bidi(3);
14352        config.set_initial_max_streams_uni(3);
14353        config.set_max_recv_udp_payload_size(1200);
14354        config.verify_peer(false);
14355
14356        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14357        assert_eq!(pipe.handshake(), Ok(()));
14358
14359        // Client sends stream data bigger than cwnd (it will never arrive to the
14360        // server).
14361        let send_buf1 = [0; 20000];
14362        assert_eq!(
14363            pipe.client.stream_send(0, &send_buf1, false),
14364            if cc_algorithm_name == "cubic" {
14365                Ok(12000)
14366            } else {
14367                if cfg!(feature = "openssl") {
14368                    Ok(12345)
14369                } else {
14370                    Ok(12299)
14371                }
14372            }
14373        );
14374
14375        testing::emit_flight(&mut pipe.client).ok();
14376
14377        // Server sends some stream data that will need ACKs.
14378        assert_eq!(
14379            pipe.server.stream_send(1, &send_buf1[..500], false),
14380            Ok(500)
14381        );
14382
14383        testing::process_flight(
14384            &mut pipe.client,
14385            testing::emit_flight(&mut pipe.server).unwrap(),
14386        )
14387        .unwrap();
14388
14389        let mut buf = [0; 2000];
14390
14391        let ret = pipe.client.send(&mut buf);
14392
14393        assert_eq!(pipe.client.tx_cap, 0);
14394
14395        assert!(matches!(ret, Ok((_, _))), "the client should at least send one packet to acknowledge the newly received data");
14396
14397        let (sent, _) = ret.unwrap();
14398
14399        assert_ne!(sent, 0, "the client should at least send a pure ACK packet");
14400
14401        let frames =
14402            testing::decode_pkt(&mut pipe.server, &mut buf[..sent]).unwrap();
14403        assert_eq!(1, frames.len());
14404        assert!(
14405            matches!(frames[0], frame::Frame::ACK { .. }),
14406            "the packet sent by the client must be an ACK only packet"
14407        );
14408    }
14409
14410    /// Like sends_ack_only_pkt_when_full_cwnd_and_ack_elicited, but when
14411    /// ack_eliciting is explicitly requested.
14412    #[rstest]
14413    fn sends_ack_only_pkt_when_full_cwnd_and_ack_elicited_despite_max_unacknowledging(
14414        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14415    ) {
14416        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14417        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14418        config
14419            .load_cert_chain_from_pem_file("examples/cert.crt")
14420            .unwrap();
14421        config
14422            .load_priv_key_from_pem_file("examples/cert.key")
14423            .unwrap();
14424        config
14425            .set_application_protos(&[b"proto1", b"proto2"])
14426            .unwrap();
14427        config.set_initial_max_data(50000);
14428        config.set_initial_max_stream_data_bidi_local(50000);
14429        config.set_initial_max_stream_data_bidi_remote(50000);
14430        config.set_initial_max_streams_bidi(3);
14431        config.set_initial_max_streams_uni(3);
14432        config.set_max_recv_udp_payload_size(1200);
14433        config.verify_peer(false);
14434
14435        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14436        assert_eq!(pipe.handshake(), Ok(()));
14437
14438        // Client sends stream data bigger than cwnd (it will never arrive to the
14439        // server). This exhausts the congestion window.
14440        let send_buf1 = [0; 20000];
14441        assert_eq!(
14442            pipe.client.stream_send(0, &send_buf1, false),
14443            if cc_algorithm_name == "cubic" {
14444                Ok(12000)
14445            } else {
14446                if cfg!(feature = "openssl") {
14447                    Ok(12345)
14448                } else {
14449                    Ok(12299)
14450                }
14451            }
14452        );
14453
14454        testing::emit_flight(&mut pipe.client).ok();
14455
14456        // Client gets PING frames from server, which elicit ACK
14457        let mut buf = [0; 2000];
14458        for _ in 0..recovery::MAX_OUTSTANDING_NON_ACK_ELICITING {
14459            let written = testing::encode_pkt(
14460                &mut pipe.server,
14461                packet::Type::Short,
14462                &[frame::Frame::Ping { mtu_probe: None }],
14463                &mut buf,
14464            )
14465            .unwrap();
14466
14467            pipe.client_recv(&mut buf[..written])
14468                .expect("client recv ping");
14469
14470            // Client acknowledges despite a full congestion window
14471            let ret = pipe.client.send(&mut buf);
14472
14473            assert!(matches!(ret, Ok((_, _))), "the client should at least send one packet to acknowledge the newly received data");
14474
14475            let (sent, _) = ret.unwrap();
14476
14477            assert_ne!(
14478                sent, 0,
14479                "the client should at least send a pure ACK packet"
14480            );
14481
14482            let frames =
14483                testing::decode_pkt(&mut pipe.server, &mut buf[..sent]).unwrap();
14484
14485            assert_eq!(1, frames.len());
14486
14487            assert!(
14488                matches!(frames[0], frame::Frame::ACK { .. }),
14489                "the packet sent by the client must be an ACK only packet"
14490            );
14491        }
14492
14493        // The client shouldn't need to send any more packets after the ACK only
14494        // packet it just sent.
14495        assert_eq!(
14496            pipe.client.send(&mut buf),
14497            Err(Error::Done),
14498            "nothing for client to send after ACK-only packet"
14499        );
14500    }
14501
14502    #[rstest]
14503    fn app_limited_false_no_frame(
14504        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14505    ) {
14506        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14507        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14508        config
14509            .set_application_protos(&[b"proto1", b"proto2"])
14510            .unwrap();
14511        config.set_initial_max_data(50000);
14512        config.set_initial_max_stream_data_bidi_local(50000);
14513        config.set_initial_max_stream_data_bidi_remote(50000);
14514        config.set_max_recv_udp_payload_size(1405);
14515        config.verify_peer(false);
14516
14517        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
14518        assert_eq!(pipe.handshake(), Ok(()));
14519
14520        // Client sends stream data.
14521        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14522        assert_eq!(pipe.advance(), Ok(()));
14523
14524        // Server reads stream data.
14525        let mut b = [0; 15];
14526        pipe.server.stream_recv(0, &mut b).unwrap();
14527        assert_eq!(pipe.advance(), Ok(()));
14528
14529        // Server sends stream data bigger than cwnd.
14530        let send_buf1 = [0; 20000];
14531        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
14532
14533        testing::emit_flight(&mut pipe.server).ok();
14534
14535        // We can't create a new packet header because there is no room by cwnd.
14536        // app_limited should be false because we can't send more by cwnd.
14537        assert!(!pipe
14538            .server
14539            .paths
14540            .get_active()
14541            .expect("no active")
14542            .recovery
14543            .app_limited());
14544    }
14545
14546    #[rstest]
14547    fn app_limited_false_no_header(
14548        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14549    ) {
14550        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14551        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14552        config
14553            .set_application_protos(&[b"proto1", b"proto2"])
14554            .unwrap();
14555        config.set_initial_max_data(50000);
14556        config.set_initial_max_stream_data_bidi_local(50000);
14557        config.set_initial_max_stream_data_bidi_remote(50000);
14558        config.set_max_recv_udp_payload_size(1406);
14559        config.verify_peer(false);
14560
14561        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
14562        assert_eq!(pipe.handshake(), Ok(()));
14563
14564        // Client sends stream data.
14565        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14566        assert_eq!(pipe.advance(), Ok(()));
14567
14568        // Server reads stream data.
14569        let mut b = [0; 15];
14570        pipe.server.stream_recv(0, &mut b).unwrap();
14571        assert_eq!(pipe.advance(), Ok(()));
14572
14573        // Server sends stream data bigger than cwnd.
14574        let send_buf1 = [0; 20000];
14575        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
14576
14577        testing::emit_flight(&mut pipe.server).ok();
14578
14579        // We can't create a new frame because there is no room by cwnd.
14580        // app_limited should be false because we can't send more by cwnd.
14581        assert!(!pipe
14582            .server
14583            .paths
14584            .get_active()
14585            .expect("no active")
14586            .recovery
14587            .app_limited());
14588    }
14589
14590    #[rstest]
14591    fn app_limited_not_changed_on_no_new_frames(
14592        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14593    ) {
14594        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14595        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14596        config
14597            .set_application_protos(&[b"proto1", b"proto2"])
14598            .unwrap();
14599        config.set_initial_max_data(50000);
14600        config.set_initial_max_stream_data_bidi_local(50000);
14601        config.set_initial_max_stream_data_bidi_remote(50000);
14602        config.set_max_recv_udp_payload_size(1200);
14603        config.verify_peer(false);
14604
14605        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
14606        assert_eq!(pipe.handshake(), Ok(()));
14607
14608        // Client sends stream data.
14609        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
14610        assert_eq!(pipe.advance(), Ok(()));
14611
14612        // Server reads stream data.
14613        let mut b = [0; 15];
14614        pipe.server.stream_recv(0, &mut b).unwrap();
14615        assert_eq!(pipe.advance(), Ok(()));
14616
14617        // Client's app_limited is true because its bytes-in-flight
14618        // is much smaller than the current cwnd.
14619        assert!(pipe
14620            .client
14621            .paths
14622            .get_active()
14623            .expect("no active")
14624            .recovery
14625            .app_limited());
14626
14627        // Client has no new frames to send - returns Done.
14628        assert_eq!(testing::emit_flight(&mut pipe.client), Err(Error::Done));
14629
14630        // Client's app_limited should remain the same.
14631        assert!(pipe
14632            .client
14633            .paths
14634            .get_active()
14635            .expect("no active")
14636            .recovery
14637            .app_limited());
14638    }
14639
14640    #[rstest]
14641    fn limit_ack_ranges(
14642        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14643    ) {
14644        let mut buf = [0; 65535];
14645
14646        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
14647        assert_eq!(pipe.handshake(), Ok(()));
14648
14649        let epoch = packet::Epoch::Application;
14650
14651        assert_eq!(pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.len(), 0);
14652
14653        let frames = [
14654            frame::Frame::Ping { mtu_probe: None },
14655            frame::Frame::Padding { len: 3 },
14656        ];
14657
14658        let pkt_type = packet::Type::Short;
14659
14660        let mut last_packet_sent = 0;
14661
14662        for _ in 0..512 {
14663            let recv_count = pipe.server.recv_count;
14664
14665            last_packet_sent = pipe.client.next_pkt_num;
14666
14667            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
14668                .unwrap();
14669
14670            assert_eq!(pipe.server.recv_count, recv_count + 1);
14671
14672            // Skip packet number.
14673            pipe.client.next_pkt_num += 1;
14674        }
14675
14676        assert_eq!(
14677            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.len(),
14678            MAX_ACK_RANGES
14679        );
14680
14681        assert_eq!(
14682            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.first(),
14683            Some(last_packet_sent - ((MAX_ACK_RANGES as u64) - 1) * 2)
14684        );
14685
14686        assert_eq!(
14687            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.last(),
14688            Some(last_packet_sent)
14689        );
14690    }
14691
14692    #[rstest]
14693    /// Tests that streams are correctly scheduled based on their priority.
14694    fn stream_priority(
14695        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14696    ) {
14697        // Limit 1-RTT packet size to avoid congestion control interference.
14698        const MAX_TEST_PACKET_SIZE: usize = 540;
14699
14700        let mut buf = [0; 65535];
14701
14702        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14703        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14704        config
14705            .load_cert_chain_from_pem_file("examples/cert.crt")
14706            .unwrap();
14707        config
14708            .load_priv_key_from_pem_file("examples/cert.key")
14709            .unwrap();
14710        config
14711            .set_application_protos(&[b"proto1", b"proto2"])
14712            .unwrap();
14713        config.set_initial_max_data(1_000_000);
14714        config.set_initial_max_stream_data_bidi_local(1_000_000);
14715        config.set_initial_max_stream_data_bidi_remote(1_000_000);
14716        config.set_initial_max_stream_data_uni(0);
14717        config.set_initial_max_streams_bidi(100);
14718        config.set_initial_max_streams_uni(0);
14719        config.verify_peer(false);
14720
14721        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14722        assert_eq!(pipe.handshake(), Ok(()));
14723
14724        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
14725        assert_eq!(pipe.advance(), Ok(()));
14726
14727        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
14728        assert_eq!(pipe.advance(), Ok(()));
14729
14730        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
14731        assert_eq!(pipe.advance(), Ok(()));
14732
14733        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
14734        assert_eq!(pipe.advance(), Ok(()));
14735
14736        assert_eq!(pipe.client.stream_send(16, b"a", false), Ok(1));
14737        assert_eq!(pipe.advance(), Ok(()));
14738
14739        assert_eq!(pipe.client.stream_send(20, b"a", false), Ok(1));
14740        assert_eq!(pipe.advance(), Ok(()));
14741
14742        let mut b = [0; 1];
14743
14744        let out = [b'b'; 500];
14745
14746        // Server prioritizes streams as follows:
14747        //  * Stream 8 and 16 have the same priority but are non-incremental.
14748        //  * Stream 4, 12 and 20 have the same priority but 20 is non-incremental
14749        //    and 4 and 12 are incremental.
14750        //  * Stream 0 is on its own.
14751
14752        pipe.server.stream_recv(0, &mut b).unwrap();
14753        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
14754        pipe.server.stream_send(0, &out, false).unwrap();
14755        pipe.server.stream_send(0, &out, false).unwrap();
14756        pipe.server.stream_send(0, &out, false).unwrap();
14757
14758        pipe.server.stream_recv(12, &mut b).unwrap();
14759        assert_eq!(pipe.server.stream_priority(12, 42, true), Ok(()));
14760        pipe.server.stream_send(12, &out, false).unwrap();
14761        pipe.server.stream_send(12, &out, false).unwrap();
14762        pipe.server.stream_send(12, &out, false).unwrap();
14763
14764        pipe.server.stream_recv(16, &mut b).unwrap();
14765        assert_eq!(pipe.server.stream_priority(16, 10, false), Ok(()));
14766        pipe.server.stream_send(16, &out, false).unwrap();
14767        pipe.server.stream_send(16, &out, false).unwrap();
14768        pipe.server.stream_send(16, &out, false).unwrap();
14769
14770        pipe.server.stream_recv(4, &mut b).unwrap();
14771        assert_eq!(pipe.server.stream_priority(4, 42, true), Ok(()));
14772        pipe.server.stream_send(4, &out, false).unwrap();
14773        pipe.server.stream_send(4, &out, false).unwrap();
14774        pipe.server.stream_send(4, &out, false).unwrap();
14775
14776        pipe.server.stream_recv(8, &mut b).unwrap();
14777        assert_eq!(pipe.server.stream_priority(8, 10, false), Ok(()));
14778        pipe.server.stream_send(8, &out, false).unwrap();
14779        pipe.server.stream_send(8, &out, false).unwrap();
14780        pipe.server.stream_send(8, &out, false).unwrap();
14781
14782        pipe.server.stream_recv(20, &mut b).unwrap();
14783        assert_eq!(pipe.server.stream_priority(20, 42, false), Ok(()));
14784        pipe.server.stream_send(20, &out, false).unwrap();
14785        pipe.server.stream_send(20, &out, false).unwrap();
14786        pipe.server.stream_send(20, &out, false).unwrap();
14787
14788        // First is stream 8.
14789        let mut off = 0;
14790
14791        for _ in 1..=3 {
14792            let (len, _) =
14793                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14794
14795            let frames =
14796                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14797            let stream = frames.first().unwrap();
14798
14799            assert_eq!(stream, &frame::Frame::Stream {
14800                stream_id: 8,
14801                data: <RangeBuf>::from(&out, off, false),
14802            });
14803
14804            off = match stream {
14805                frame::Frame::Stream { data, .. } => data.max_off(),
14806
14807                _ => unreachable!(),
14808            };
14809        }
14810
14811        // Then is stream 16.
14812        let mut off = 0;
14813
14814        for _ in 1..=3 {
14815            let (len, _) =
14816                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14817
14818            let frames =
14819                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14820            let stream = frames.first().unwrap();
14821
14822            assert_eq!(stream, &frame::Frame::Stream {
14823                stream_id: 16,
14824                data: <RangeBuf>::from(&out, off, false),
14825            });
14826
14827            off = match stream {
14828                frame::Frame::Stream { data, .. } => data.max_off(),
14829
14830                _ => unreachable!(),
14831            };
14832        }
14833
14834        // Then is stream 20.
14835        let mut off = 0;
14836
14837        for _ in 1..=3 {
14838            let (len, _) =
14839                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14840
14841            let frames =
14842                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14843            let stream = frames.first().unwrap();
14844
14845            assert_eq!(stream, &frame::Frame::Stream {
14846                stream_id: 20,
14847                data: <RangeBuf>::from(&out, off, false),
14848            });
14849
14850            off = match stream {
14851                frame::Frame::Stream { data, .. } => data.max_off(),
14852
14853                _ => unreachable!(),
14854            };
14855        }
14856
14857        // Then are stream 12 and 4, with the same priority, incrementally.
14858        let mut off = 0;
14859
14860        for _ in 1..=3 {
14861            let (len, _) =
14862                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14863
14864            let frames =
14865                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14866
14867            assert_eq!(
14868                frames.first(),
14869                Some(&frame::Frame::Stream {
14870                    stream_id: 12,
14871                    data: <RangeBuf>::from(&out, off, false),
14872                })
14873            );
14874
14875            let (len, _) =
14876                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14877
14878            let frames =
14879                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14880
14881            let stream = frames.first().unwrap();
14882
14883            assert_eq!(stream, &frame::Frame::Stream {
14884                stream_id: 4,
14885                data: <RangeBuf>::from(&out, off, false),
14886            });
14887
14888            off = match stream {
14889                frame::Frame::Stream { data, .. } => data.max_off(),
14890
14891                _ => unreachable!(),
14892            };
14893        }
14894
14895        // Final is stream 0.
14896        let mut off = 0;
14897
14898        for _ in 1..=3 {
14899            let (len, _) =
14900                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14901
14902            let frames =
14903                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14904            let stream = frames.first().unwrap();
14905
14906            assert_eq!(stream, &frame::Frame::Stream {
14907                stream_id: 0,
14908                data: <RangeBuf>::from(&out, off, false),
14909            });
14910
14911            off = match stream {
14912                frame::Frame::Stream { data, .. } => data.max_off(),
14913
14914                _ => unreachable!(),
14915            };
14916        }
14917
14918        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
14919    }
14920
14921    #[rstest]
14922    /// Tests that changing a stream's priority is correctly propagated.
14923    fn stream_reprioritize(
14924        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
14925    ) {
14926        let mut buf = [0; 65535];
14927
14928        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14929        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
14930        config
14931            .load_cert_chain_from_pem_file("examples/cert.crt")
14932            .unwrap();
14933        config
14934            .load_priv_key_from_pem_file("examples/cert.key")
14935            .unwrap();
14936        config
14937            .set_application_protos(&[b"proto1", b"proto2"])
14938            .unwrap();
14939        config.set_initial_max_data(30);
14940        config.set_initial_max_stream_data_bidi_local(15);
14941        config.set_initial_max_stream_data_bidi_remote(15);
14942        config.set_initial_max_stream_data_uni(0);
14943        config.set_initial_max_streams_bidi(5);
14944        config.set_initial_max_streams_uni(0);
14945        config.verify_peer(false);
14946
14947        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14948        assert_eq!(pipe.handshake(), Ok(()));
14949
14950        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
14951        assert_eq!(pipe.advance(), Ok(()));
14952
14953        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
14954        assert_eq!(pipe.advance(), Ok(()));
14955
14956        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
14957        assert_eq!(pipe.advance(), Ok(()));
14958
14959        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
14960        assert_eq!(pipe.advance(), Ok(()));
14961
14962        let mut b = [0; 1];
14963
14964        pipe.server.stream_recv(0, &mut b).unwrap();
14965        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
14966        pipe.server.stream_send(0, b"b", false).unwrap();
14967
14968        pipe.server.stream_recv(12, &mut b).unwrap();
14969        assert_eq!(pipe.server.stream_priority(12, 42, true), Ok(()));
14970        pipe.server.stream_send(12, b"b", false).unwrap();
14971
14972        pipe.server.stream_recv(8, &mut b).unwrap();
14973        assert_eq!(pipe.server.stream_priority(8, 10, true), Ok(()));
14974        pipe.server.stream_send(8, b"b", false).unwrap();
14975
14976        pipe.server.stream_recv(4, &mut b).unwrap();
14977        assert_eq!(pipe.server.stream_priority(4, 42, true), Ok(()));
14978        pipe.server.stream_send(4, b"b", false).unwrap();
14979
14980        // Stream 0 is re-prioritized!!!
14981        assert_eq!(pipe.server.stream_priority(0, 20, true), Ok(()));
14982
14983        // First is stream 8.
14984        let (len, _) = pipe.server.send(&mut buf).unwrap();
14985
14986        let frames =
14987            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14988
14989        assert_eq!(
14990            frames.first(),
14991            Some(&frame::Frame::Stream {
14992                stream_id: 8,
14993                data: <RangeBuf>::from(b"b", 0, false),
14994            })
14995        );
14996
14997        // Then is stream 0.
14998        let (len, _) = pipe.server.send(&mut buf).unwrap();
14999
15000        let frames =
15001            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15002
15003        assert_eq!(
15004            frames.first(),
15005            Some(&frame::Frame::Stream {
15006                stream_id: 0,
15007                data: <RangeBuf>::from(b"b", 0, false),
15008            })
15009        );
15010
15011        // Then are stream 12 and 4, with the same priority.
15012        let (len, _) = pipe.server.send(&mut buf).unwrap();
15013
15014        let frames =
15015            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15016
15017        assert_eq!(
15018            frames.first(),
15019            Some(&frame::Frame::Stream {
15020                stream_id: 12,
15021                data: <RangeBuf>::from(b"b", 0, false),
15022            })
15023        );
15024
15025        let (len, _) = pipe.server.send(&mut buf).unwrap();
15026
15027        let frames =
15028            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15029
15030        assert_eq!(
15031            frames.first(),
15032            Some(&frame::Frame::Stream {
15033                stream_id: 4,
15034                data: <RangeBuf>::from(b"b", 0, false),
15035            })
15036        );
15037
15038        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
15039    }
15040
15041    #[rstest]
15042    /// Tests that streams and datagrams are correctly scheduled.
15043    fn stream_datagram_priority(
15044        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15045    ) {
15046        // Limit 1-RTT packet size to avoid congestion control interference.
15047        const MAX_TEST_PACKET_SIZE: usize = 540;
15048
15049        let mut buf = [0; 65535];
15050
15051        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15052        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15053        config
15054            .load_cert_chain_from_pem_file("examples/cert.crt")
15055            .unwrap();
15056        config
15057            .load_priv_key_from_pem_file("examples/cert.key")
15058            .unwrap();
15059        config
15060            .set_application_protos(&[b"proto1", b"proto2"])
15061            .unwrap();
15062        config.set_initial_max_data(1_000_000);
15063        config.set_initial_max_stream_data_bidi_local(1_000_000);
15064        config.set_initial_max_stream_data_bidi_remote(1_000_000);
15065        config.set_initial_max_stream_data_uni(0);
15066        config.set_initial_max_streams_bidi(100);
15067        config.set_initial_max_streams_uni(0);
15068        config.enable_dgram(true, 10, 10);
15069        config.verify_peer(false);
15070
15071        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15072        assert_eq!(pipe.handshake(), Ok(()));
15073
15074        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
15075        assert_eq!(pipe.advance(), Ok(()));
15076
15077        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
15078        assert_eq!(pipe.advance(), Ok(()));
15079
15080        let mut b = [0; 1];
15081
15082        let out = [b'b'; 500];
15083
15084        // Server prioritizes Stream 0 and 4 with the same urgency with
15085        // incremental, meaning the frames should be sent in round-robin
15086        // fashion. It also sends DATAGRAMS which are always interleaved with
15087        // STREAM frames. So we'll expect a mix of frame types regardless
15088        // of the order that the application writes things in.
15089
15090        pipe.server.stream_recv(0, &mut b).unwrap();
15091        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
15092        pipe.server.stream_send(0, &out, false).unwrap();
15093        pipe.server.stream_send(0, &out, false).unwrap();
15094        pipe.server.stream_send(0, &out, false).unwrap();
15095
15096        assert_eq!(pipe.server.stream_priority(4, 255, true), Ok(()));
15097        pipe.server.stream_send(4, &out, false).unwrap();
15098        pipe.server.stream_send(4, &out, false).unwrap();
15099        pipe.server.stream_send(4, &out, false).unwrap();
15100
15101        for _ in 1..=6 {
15102            assert_eq!(pipe.server.dgram_send(&out), Ok(()));
15103        }
15104
15105        let mut off_0 = 0;
15106        let mut off_4 = 0;
15107
15108        for _ in 1..=3 {
15109            // DATAGRAM
15110            let (len, _) =
15111                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
15112
15113            let frames =
15114                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15115            let mut frame_iter = frames.iter();
15116
15117            assert_eq!(frame_iter.next().unwrap(), &frame::Frame::Datagram {
15118                data: out.into()
15119            });
15120            assert_eq!(frame_iter.next(), None);
15121
15122            // STREAM 0
15123            let (len, _) =
15124                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
15125
15126            let frames =
15127                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15128            let mut frame_iter = frames.iter();
15129            let stream = frame_iter.next().unwrap();
15130
15131            assert_eq!(stream, &frame::Frame::Stream {
15132                stream_id: 0,
15133                data: <RangeBuf>::from(&out, off_0, false),
15134            });
15135
15136            off_0 = match stream {
15137                frame::Frame::Stream { data, .. } => data.max_off(),
15138
15139                _ => unreachable!(),
15140            };
15141            assert_eq!(frame_iter.next(), None);
15142
15143            // DATAGRAM
15144            let (len, _) =
15145                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
15146
15147            let frames =
15148                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15149            let mut frame_iter = frames.iter();
15150
15151            assert_eq!(frame_iter.next().unwrap(), &frame::Frame::Datagram {
15152                data: out.into()
15153            });
15154            assert_eq!(frame_iter.next(), None);
15155
15156            // STREAM 4
15157            let (len, _) =
15158                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
15159
15160            let frames =
15161                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
15162            let mut frame_iter = frames.iter();
15163            let stream = frame_iter.next().unwrap();
15164
15165            assert_eq!(stream, &frame::Frame::Stream {
15166                stream_id: 4,
15167                data: <RangeBuf>::from(&out, off_4, false),
15168            });
15169
15170            off_4 = match stream {
15171                frame::Frame::Stream { data, .. } => data.max_off(),
15172
15173                _ => unreachable!(),
15174            };
15175            assert_eq!(frame_iter.next(), None);
15176        }
15177    }
15178
15179    #[rstest]
15180    /// Tests that old data is retransmitted on PTO.
15181    fn early_retransmit(
15182        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15183    ) {
15184        let mut buf = [0; 65535];
15185
15186        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15187        assert_eq!(pipe.handshake(), Ok(()));
15188
15189        // Client sends stream data.
15190        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
15191        assert_eq!(pipe.advance(), Ok(()));
15192
15193        // Client sends more stream data, but packet is lost
15194        assert_eq!(pipe.client.stream_send(4, b"b", false), Ok(1));
15195        assert!(pipe.client.send(&mut buf).is_ok());
15196
15197        // Wait until PTO expires. Since the RTT is very low, wait a bit more.
15198        let timer = pipe.client.timeout().unwrap();
15199        std::thread::sleep(timer + time::Duration::from_millis(1));
15200
15201        pipe.client.on_timeout();
15202
15203        let epoch = packet::Epoch::Application;
15204        assert_eq!(
15205            pipe.client
15206                .paths
15207                .get_active()
15208                .expect("no active")
15209                .recovery
15210                .loss_probes(epoch),
15211            1,
15212        );
15213
15214        // Client retransmits stream data in PTO probe.
15215        let (len, _) = pipe.client.send(&mut buf).unwrap();
15216        assert_eq!(
15217            pipe.client
15218                .paths
15219                .get_active()
15220                .expect("no active")
15221                .recovery
15222                .loss_probes(epoch),
15223            0,
15224        );
15225
15226        let frames =
15227            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
15228
15229        let mut iter = frames.iter();
15230
15231        // Skip ACK frame.
15232        iter.next();
15233
15234        assert_eq!(
15235            iter.next(),
15236            Some(&frame::Frame::Stream {
15237                stream_id: 4,
15238                data: <RangeBuf>::from(b"b", 0, false),
15239            })
15240        );
15241        assert_eq!(pipe.client.stats().retrans, 1);
15242    }
15243
15244    #[rstest]
15245    /// Tests that PTO probe packets are not coalesced together.
15246    fn dont_coalesce_probes(
15247        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15248    ) {
15249        let mut buf = [0; 65535];
15250
15251        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15252
15253        // Client sends Initial packet.
15254        let (len, _) = pipe.client.send(&mut buf).unwrap();
15255        assert_eq!(len, 1200);
15256
15257        // Wait for PTO to expire.
15258        let timer = pipe.client.timeout().unwrap();
15259        std::thread::sleep(timer + time::Duration::from_millis(1));
15260
15261        pipe.client.on_timeout();
15262
15263        let epoch = packet::Epoch::Initial;
15264        assert_eq!(
15265            pipe.client
15266                .paths
15267                .get_active()
15268                .expect("no active")
15269                .recovery
15270                .loss_probes(epoch),
15271            1,
15272        );
15273
15274        // Client sends PTO probe.
15275        let (len, _) = pipe.client.send(&mut buf).unwrap();
15276        assert_eq!(len, 1200);
15277        assert_eq!(
15278            pipe.client
15279                .paths
15280                .get_active()
15281                .expect("no active")
15282                .recovery
15283                .loss_probes(epoch),
15284            0,
15285        );
15286
15287        // Wait for PTO to expire.
15288        let timer = pipe.client.timeout().unwrap();
15289        std::thread::sleep(timer + time::Duration::from_millis(1));
15290
15291        pipe.client.on_timeout();
15292
15293        assert_eq!(
15294            pipe.client
15295                .paths
15296                .get_active()
15297                .expect("no active")
15298                .recovery
15299                .loss_probes(epoch),
15300            2,
15301        );
15302
15303        // Client sends first PTO probe.
15304        let (len, _) = pipe.client.send(&mut buf).unwrap();
15305        assert_eq!(len, 1200);
15306        assert_eq!(
15307            pipe.client
15308                .paths
15309                .get_active()
15310                .expect("no active")
15311                .recovery
15312                .loss_probes(epoch),
15313            1,
15314        );
15315
15316        // Client sends second PTO probe.
15317        let (len, _) = pipe.client.send(&mut buf).unwrap();
15318        assert_eq!(len, 1200);
15319        assert_eq!(
15320            pipe.client
15321                .paths
15322                .get_active()
15323                .expect("no active")
15324                .recovery
15325                .loss_probes(epoch),
15326            0,
15327        );
15328    }
15329
15330    #[rstest]
15331    fn coalesce_padding_short(
15332        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15333    ) {
15334        let mut buf = [0; 65535];
15335
15336        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15337
15338        // Client sends first flight.
15339        let (len, _) = pipe.client.send(&mut buf).unwrap();
15340        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
15341        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15342
15343        // Server sends first flight.
15344        let (len, _) = pipe.server.send(&mut buf).unwrap();
15345        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
15346        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
15347
15348        let (len, _) = pipe.server.send(&mut buf).unwrap();
15349        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
15350
15351        // Client sends stream data.
15352        assert!(pipe.client.is_established());
15353        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
15354
15355        // Client sends second flight.
15356        let (len, _) = pipe.client.send(&mut buf).unwrap();
15357        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
15358        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15359
15360        // None of the sent packets should have been dropped.
15361        assert_eq!(pipe.client.sent_count, pipe.server.recv_count);
15362        assert_eq!(pipe.server.sent_count, pipe.client.recv_count);
15363    }
15364
15365    #[rstest]
15366    /// Tests that client avoids handshake deadlock by arming PTO.
15367    fn handshake_anti_deadlock(
15368        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15369    ) {
15370        let mut buf = [0; 65535];
15371
15372        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
15373        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15374        config
15375            .load_cert_chain_from_pem_file("examples/cert-big.crt")
15376            .unwrap();
15377        config
15378            .load_priv_key_from_pem_file("examples/cert.key")
15379            .unwrap();
15380        config
15381            .set_application_protos(&[b"proto1", b"proto2"])
15382            .unwrap();
15383
15384        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
15385
15386        assert!(!pipe.client.handshake_status().has_handshake_keys);
15387        assert!(!pipe.client.handshake_status().peer_verified_address);
15388        assert!(!pipe.server.handshake_status().has_handshake_keys);
15389        assert!(pipe.server.handshake_status().peer_verified_address);
15390
15391        // Client sends padded Initial.
15392        let (len, _) = pipe.client.send(&mut buf).unwrap();
15393        assert_eq!(len, 1200);
15394
15395        // Server receives client's Initial and sends own Initial and Handshake
15396        // until it's blocked by the anti-amplification limit.
15397        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15398        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15399
15400        assert!(!pipe.client.handshake_status().has_handshake_keys);
15401        assert!(!pipe.client.handshake_status().peer_verified_address);
15402        assert!(pipe.server.handshake_status().has_handshake_keys);
15403        assert!(pipe.server.handshake_status().peer_verified_address);
15404
15405        // Client receives the server flight and sends Handshake ACK, but it is
15406        // lost.
15407        testing::process_flight(&mut pipe.client, flight).unwrap();
15408        testing::emit_flight(&mut pipe.client).unwrap();
15409
15410        assert!(pipe.client.handshake_status().has_handshake_keys);
15411        assert!(!pipe.client.handshake_status().peer_verified_address);
15412        assert!(pipe.server.handshake_status().has_handshake_keys);
15413        assert!(pipe.server.handshake_status().peer_verified_address);
15414
15415        // Make sure client's PTO timer is armed.
15416        assert!(pipe.client.timeout().is_some());
15417    }
15418
15419    #[rstest]
15420    /// Tests that packets with corrupted type (from Handshake to Initial) are
15421    /// properly ignored.
15422    fn handshake_packet_type_corruption(
15423        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15424    ) {
15425        let mut buf = [0; 65535];
15426
15427        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15428
15429        // Client sends padded Initial.
15430        let (len, _) = pipe.client.send(&mut buf).unwrap();
15431        assert_eq!(len, 1200);
15432
15433        // Server receives client's Initial and sends own Initial and Handshake.
15434        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15435
15436        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15437        testing::process_flight(&mut pipe.client, flight).unwrap();
15438
15439        // Client sends Initial packet with ACK.
15440        let active_pid =
15441            pipe.client.paths.get_active_path_id().expect("no active");
15442        let (ty, len) = pipe
15443            .client
15444            .send_single(&mut buf, active_pid, false, time::Instant::now())
15445            .unwrap();
15446        assert_eq!(ty, Type::Initial);
15447
15448        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15449
15450        // Client sends Handshake packet.
15451        let (ty, len) = pipe
15452            .client
15453            .send_single(&mut buf, active_pid, false, time::Instant::now())
15454            .unwrap();
15455        assert_eq!(ty, Type::Handshake);
15456
15457        // Packet type is corrupted to Initial.
15458        buf[0] &= !(0x20);
15459
15460        let hdr = Header::from_slice(&mut buf[..len], 0).unwrap();
15461        assert_eq!(hdr.ty, Type::Initial);
15462
15463        // Server receives corrupted packet without returning an error.
15464        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15465    }
15466
15467    #[rstest]
15468    fn dgram_send_fails_invalidstate(
15469        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15470    ) {
15471        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15472        assert_eq!(pipe.handshake(), Ok(()));
15473
15474        assert_eq!(
15475            pipe.client.dgram_send(b"hello, world"),
15476            Err(Error::InvalidState)
15477        );
15478    }
15479
15480    #[rstest]
15481    fn dgram_send_app_limited(
15482        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15483    ) {
15484        let mut buf = [0; 65535];
15485        let send_buf = [0xcf; 1000];
15486
15487        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15488        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15489        config
15490            .load_cert_chain_from_pem_file("examples/cert.crt")
15491            .unwrap();
15492        config
15493            .load_priv_key_from_pem_file("examples/cert.key")
15494            .unwrap();
15495        config
15496            .set_application_protos(&[b"proto1", b"proto2"])
15497            .unwrap();
15498        config.set_initial_max_data(30);
15499        config.set_initial_max_stream_data_bidi_local(15);
15500        config.set_initial_max_stream_data_bidi_remote(15);
15501        config.set_initial_max_stream_data_uni(10);
15502        config.set_initial_max_streams_bidi(3);
15503        config.set_initial_max_streams_uni(3);
15504        config.enable_dgram(true, 1000, 1000);
15505        config.set_max_recv_udp_payload_size(1200);
15506        config.verify_peer(false);
15507
15508        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15509        assert_eq!(pipe.handshake(), Ok(()));
15510
15511        for _ in 0..1000 {
15512            assert_eq!(pipe.client.dgram_send(&send_buf), Ok(()));
15513        }
15514
15515        assert_eq!(
15516            !pipe
15517                .client
15518                .paths
15519                .get_active()
15520                .expect("no active")
15521                .recovery
15522                .app_limited(),
15523            // bbr2_gcongestion uses different logic to set app_limited
15524            // TODO fix
15525            cc_algorithm_name != "bbr2_gcongestion"
15526        );
15527        assert_eq!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
15528
15529        let (len, _) = pipe.client.send(&mut buf).unwrap();
15530
15531        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 0);
15532        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
15533        assert_eq!(
15534            !pipe
15535                .client
15536                .paths
15537                .get_active()
15538                .expect("no active")
15539                .recovery
15540                .app_limited(),
15541            cc_algorithm_name != "bbr2_gcongestion"
15542        );
15543
15544        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
15545
15546        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15547        testing::process_flight(&mut pipe.server, flight).unwrap();
15548
15549        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15550        testing::process_flight(&mut pipe.client, flight).unwrap();
15551
15552        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 0);
15553        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
15554
15555        assert_eq!(
15556            !pipe
15557                .client
15558                .paths
15559                .get_active()
15560                .expect("no active")
15561                .recovery
15562                .app_limited(),
15563            cc_algorithm_name != "bbr2_gcongestion"
15564        );
15565    }
15566
15567    #[rstest]
15568    fn dgram_single_datagram(
15569        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15570    ) {
15571        let mut buf = [0; 65535];
15572
15573        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15574        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15575        config
15576            .load_cert_chain_from_pem_file("examples/cert.crt")
15577            .unwrap();
15578        config
15579            .load_priv_key_from_pem_file("examples/cert.key")
15580            .unwrap();
15581        config
15582            .set_application_protos(&[b"proto1", b"proto2"])
15583            .unwrap();
15584        config.set_initial_max_data(30);
15585        config.set_initial_max_stream_data_bidi_local(15);
15586        config.set_initial_max_stream_data_bidi_remote(15);
15587        config.set_initial_max_stream_data_uni(10);
15588        config.set_initial_max_streams_bidi(3);
15589        config.set_initial_max_streams_uni(3);
15590        config.enable_dgram(true, 10, 10);
15591        config.verify_peer(false);
15592
15593        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15594        assert_eq!(pipe.handshake(), Ok(()));
15595
15596        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
15597
15598        assert_eq!(pipe.advance(), Ok(()));
15599
15600        let result1 = pipe.server.dgram_recv(&mut buf);
15601        assert_eq!(result1, Ok(12));
15602
15603        let result2 = pipe.server.dgram_recv(&mut buf);
15604        assert_eq!(result2, Err(Error::Done));
15605    }
15606
15607    #[rstest]
15608    fn dgram_multiple_datagrams(
15609        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15610    ) {
15611        let mut buf = [0; 65535];
15612
15613        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15614        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15615        config
15616            .load_cert_chain_from_pem_file("examples/cert.crt")
15617            .unwrap();
15618        config
15619            .load_priv_key_from_pem_file("examples/cert.key")
15620            .unwrap();
15621        config
15622            .set_application_protos(&[b"proto1", b"proto2"])
15623            .unwrap();
15624        config.set_initial_max_data(30);
15625        config.set_initial_max_stream_data_bidi_local(15);
15626        config.set_initial_max_stream_data_bidi_remote(15);
15627        config.set_initial_max_stream_data_uni(10);
15628        config.set_initial_max_streams_bidi(3);
15629        config.set_initial_max_streams_uni(3);
15630        config.enable_dgram(true, 2, 3);
15631        config.verify_peer(false);
15632
15633        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15634        assert_eq!(pipe.handshake(), Ok(()));
15635
15636        assert_eq!(pipe.client.dgram_send_queue_len(), 0);
15637        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 0);
15638
15639        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
15640        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
15641        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Ok(()));
15642        assert!(pipe.client.is_dgram_send_queue_full());
15643
15644        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 34);
15645
15646        pipe.client
15647            .dgram_purge_outgoing(|d: &[u8]| -> bool { d[0] == b'c' });
15648
15649        assert_eq!(pipe.client.dgram_send_queue_len(), 2);
15650        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 23);
15651        assert!(!pipe.client.is_dgram_send_queue_full());
15652
15653        // Before packets exchanged, no dgrams on server receive side.
15654        assert_eq!(pipe.server.dgram_recv_queue_len(), 0);
15655
15656        assert_eq!(pipe.advance(), Ok(()));
15657
15658        // After packets exchanged, no dgrams on client send side.
15659        assert_eq!(pipe.client.dgram_send_queue_len(), 0);
15660        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 0);
15661
15662        assert_eq!(pipe.server.dgram_recv_queue_len(), 2);
15663        assert_eq!(pipe.server.dgram_recv_queue_byte_size(), 23);
15664        assert!(pipe.server.is_dgram_recv_queue_full());
15665
15666        let result1 = pipe.server.dgram_recv(&mut buf);
15667        assert_eq!(result1, Ok(12));
15668        assert_eq!(buf[0], b'h');
15669        assert_eq!(buf[1], b'e');
15670        assert!(!pipe.server.is_dgram_recv_queue_full());
15671
15672        let result2 = pipe.server.dgram_recv(&mut buf);
15673        assert_eq!(result2, Ok(11));
15674        assert_eq!(buf[0], b'h');
15675        assert_eq!(buf[1], b'o');
15676
15677        let result3 = pipe.server.dgram_recv(&mut buf);
15678        assert_eq!(result3, Err(Error::Done));
15679
15680        assert_eq!(pipe.server.dgram_recv_queue_len(), 0);
15681        assert_eq!(pipe.server.dgram_recv_queue_byte_size(), 0);
15682    }
15683
15684    #[rstest]
15685    fn dgram_send_queue_overflow(
15686        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15687    ) {
15688        let mut buf = [0; 65535];
15689
15690        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15691        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15692        config
15693            .load_cert_chain_from_pem_file("examples/cert.crt")
15694            .unwrap();
15695        config
15696            .load_priv_key_from_pem_file("examples/cert.key")
15697            .unwrap();
15698        config
15699            .set_application_protos(&[b"proto1", b"proto2"])
15700            .unwrap();
15701        config.set_initial_max_data(30);
15702        config.set_initial_max_stream_data_bidi_local(15);
15703        config.set_initial_max_stream_data_bidi_remote(15);
15704        config.set_initial_max_stream_data_uni(10);
15705        config.set_initial_max_streams_bidi(3);
15706        config.set_initial_max_streams_uni(3);
15707        config.enable_dgram(true, 10, 2);
15708        config.verify_peer(false);
15709
15710        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15711        assert_eq!(pipe.handshake(), Ok(()));
15712
15713        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
15714        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
15715        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Err(Error::Done));
15716
15717        assert_eq!(pipe.advance(), Ok(()));
15718
15719        let result1 = pipe.server.dgram_recv(&mut buf);
15720        assert_eq!(result1, Ok(12));
15721        assert_eq!(buf[0], b'h');
15722        assert_eq!(buf[1], b'e');
15723
15724        let result2 = pipe.server.dgram_recv(&mut buf);
15725        assert_eq!(result2, Ok(11));
15726        assert_eq!(buf[0], b'c');
15727        assert_eq!(buf[1], b'i');
15728
15729        let result3 = pipe.server.dgram_recv(&mut buf);
15730        assert_eq!(result3, Err(Error::Done));
15731    }
15732
15733    #[rstest]
15734    fn dgram_recv_queue_overflow(
15735        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15736    ) {
15737        let mut buf = [0; 65535];
15738
15739        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15740        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15741        config
15742            .load_cert_chain_from_pem_file("examples/cert.crt")
15743            .unwrap();
15744        config
15745            .load_priv_key_from_pem_file("examples/cert.key")
15746            .unwrap();
15747        config
15748            .set_application_protos(&[b"proto1", b"proto2"])
15749            .unwrap();
15750        config.set_initial_max_data(30);
15751        config.set_initial_max_stream_data_bidi_local(15);
15752        config.set_initial_max_stream_data_bidi_remote(15);
15753        config.set_initial_max_stream_data_uni(10);
15754        config.set_initial_max_streams_bidi(3);
15755        config.set_initial_max_streams_uni(3);
15756        config.enable_dgram(true, 2, 10);
15757        config.set_max_recv_udp_payload_size(1200);
15758        config.verify_peer(false);
15759
15760        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15761        assert_eq!(pipe.handshake(), Ok(()));
15762
15763        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
15764        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
15765        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Ok(()));
15766
15767        assert_eq!(pipe.advance(), Ok(()));
15768
15769        let result1 = pipe.server.dgram_recv(&mut buf);
15770        assert_eq!(result1, Ok(11));
15771        assert_eq!(buf[0], b'c');
15772        assert_eq!(buf[1], b'i');
15773
15774        let result2 = pipe.server.dgram_recv(&mut buf);
15775        assert_eq!(result2, Ok(11));
15776        assert_eq!(buf[0], b'h');
15777        assert_eq!(buf[1], b'o');
15778
15779        let result3 = pipe.server.dgram_recv(&mut buf);
15780        assert_eq!(result3, Err(Error::Done));
15781    }
15782
15783    #[rstest]
15784    fn dgram_send_max_size(
15785        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15786    ) {
15787        let mut buf = [0; MAX_DGRAM_FRAME_SIZE as usize];
15788
15789        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15790        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15791        config
15792            .load_cert_chain_from_pem_file("examples/cert.crt")
15793            .unwrap();
15794        config
15795            .load_priv_key_from_pem_file("examples/cert.key")
15796            .unwrap();
15797        config
15798            .set_application_protos(&[b"proto1", b"proto2"])
15799            .unwrap();
15800        config.set_initial_max_data(30);
15801        config.set_initial_max_stream_data_bidi_local(15);
15802        config.set_initial_max_stream_data_bidi_remote(15);
15803        config.set_initial_max_stream_data_uni(10);
15804        config.set_initial_max_streams_bidi(3);
15805        config.set_initial_max_streams_uni(3);
15806        config.enable_dgram(true, 10, 10);
15807        config.set_max_recv_udp_payload_size(1452);
15808        config.verify_peer(false);
15809
15810        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15811
15812        // Before handshake (before peer settings) we don't know max dgram size
15813        assert_eq!(pipe.client.dgram_max_writable_len(), None);
15814
15815        assert_eq!(pipe.handshake(), Ok(()));
15816
15817        let max_dgram_size = pipe.client.dgram_max_writable_len().unwrap();
15818
15819        // Tests use a 16-byte connection ID, so the max datagram frame payload
15820        // size is (1200 byte-long packet - 40 bytes overhead)
15821        assert_eq!(max_dgram_size, 1160);
15822
15823        let dgram_packet: Vec<u8> = vec![42; max_dgram_size];
15824
15825        assert_eq!(pipe.client.dgram_send(&dgram_packet), Ok(()));
15826
15827        assert_eq!(pipe.advance(), Ok(()));
15828
15829        let result1 = pipe.server.dgram_recv(&mut buf);
15830        assert_eq!(result1, Ok(max_dgram_size));
15831
15832        let result2 = pipe.server.dgram_recv(&mut buf);
15833        assert_eq!(result2, Err(Error::Done));
15834    }
15835
15836    #[rstest]
15837    /// Tests is_readable check.
15838    fn is_readable(
15839        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15840    ) {
15841        let mut buf = [0; 65535];
15842
15843        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15844        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
15845        config
15846            .load_cert_chain_from_pem_file("examples/cert.crt")
15847            .unwrap();
15848        config
15849            .load_priv_key_from_pem_file("examples/cert.key")
15850            .unwrap();
15851        config
15852            .set_application_protos(&[b"proto1", b"proto2"])
15853            .unwrap();
15854        config.set_initial_max_data(30);
15855        config.set_initial_max_stream_data_bidi_local(15);
15856        config.set_initial_max_stream_data_bidi_remote(15);
15857        config.set_initial_max_stream_data_uni(10);
15858        config.set_initial_max_streams_bidi(3);
15859        config.set_initial_max_streams_uni(3);
15860        config.enable_dgram(true, 10, 10);
15861        config.set_max_recv_udp_payload_size(1452);
15862        config.verify_peer(false);
15863
15864        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15865        assert_eq!(pipe.handshake(), Ok(()));
15866
15867        // No readable data.
15868        assert!(!pipe.client.is_readable());
15869        assert!(!pipe.server.is_readable());
15870
15871        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
15872        assert_eq!(pipe.advance(), Ok(()));
15873
15874        // Server received stream.
15875        assert!(!pipe.client.is_readable());
15876        assert!(pipe.server.is_readable());
15877
15878        assert_eq!(
15879            pipe.server.stream_send(4, b"aaaaaaaaaaaaaaa", false),
15880            Ok(15)
15881        );
15882        assert_eq!(pipe.advance(), Ok(()));
15883
15884        // Client received stream.
15885        assert!(pipe.client.is_readable());
15886        assert!(pipe.server.is_readable());
15887
15888        // Client drains stream.
15889        let mut b = [0; 15];
15890        pipe.client.stream_recv(4, &mut b).unwrap();
15891        assert_eq!(pipe.advance(), Ok(()));
15892
15893        assert!(!pipe.client.is_readable());
15894        assert!(pipe.server.is_readable());
15895
15896        // Server shuts down stream.
15897        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 0), Ok(()));
15898        assert!(!pipe.server.is_readable());
15899
15900        // Server received dgram.
15901        assert_eq!(pipe.client.dgram_send(b"dddddddddddddd"), Ok(()));
15902        assert_eq!(pipe.advance(), Ok(()));
15903
15904        assert!(!pipe.client.is_readable());
15905        assert!(pipe.server.is_readable());
15906
15907        // Client received dgram.
15908        assert_eq!(pipe.server.dgram_send(b"dddddddddddddd"), Ok(()));
15909        assert_eq!(pipe.advance(), Ok(()));
15910
15911        assert!(pipe.client.is_readable());
15912        assert!(pipe.server.is_readable());
15913
15914        // Drain the dgram queues.
15915        let r = pipe.server.dgram_recv(&mut buf);
15916        assert_eq!(r, Ok(14));
15917        assert!(!pipe.server.is_readable());
15918
15919        let r = pipe.client.dgram_recv(&mut buf);
15920        assert_eq!(r, Ok(14));
15921        assert!(!pipe.client.is_readable());
15922    }
15923
15924    #[rstest]
15925    fn close(
15926        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15927    ) {
15928        let mut buf = [0; 65535];
15929
15930        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15931        assert_eq!(pipe.handshake(), Ok(()));
15932
15933        assert_eq!(pipe.client.close(false, 0x1234, b"hello?"), Ok(()));
15934
15935        assert_eq!(
15936            pipe.client.close(false, 0x4321, b"hello?"),
15937            Err(Error::Done)
15938        );
15939
15940        let (len, _) = pipe.client.send(&mut buf).unwrap();
15941
15942        let frames =
15943            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
15944
15945        assert_eq!(
15946            frames.first(),
15947            Some(&frame::Frame::ConnectionClose {
15948                error_code: 0x1234,
15949                frame_type: 0,
15950                reason: b"hello?".to_vec(),
15951            })
15952        );
15953    }
15954
15955    #[rstest]
15956    fn app_close_by_client(
15957        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15958    ) {
15959        let mut buf = [0; 65535];
15960
15961        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15962        assert_eq!(pipe.handshake(), Ok(()));
15963
15964        assert_eq!(pipe.client.close(true, 0x1234, b"hello!"), Ok(()));
15965
15966        assert_eq!(pipe.client.close(true, 0x4321, b"hello!"), Err(Error::Done));
15967
15968        let (len, _) = pipe.client.send(&mut buf).unwrap();
15969
15970        let frames =
15971            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
15972
15973        assert_eq!(
15974            frames.first(),
15975            Some(&frame::Frame::ApplicationClose {
15976                error_code: 0x1234,
15977                reason: b"hello!".to_vec(),
15978            })
15979        );
15980    }
15981
15982    // OpenSSL does not provide a straightforward interface to deal with custom
15983    // off-load key signing.
15984    #[cfg(not(feature = "openssl"))]
15985    #[rstest]
15986    fn app_close_by_server_during_handshake_private_key_failure(
15987        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
15988    ) {
15989        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
15990        pipe.server.handshake.set_failing_private_key_method();
15991
15992        // Client sends initial flight.
15993        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15994        assert_eq!(
15995            testing::process_flight(&mut pipe.server, flight),
15996            Err(Error::TlsFail)
15997        );
15998
15999        let flight = testing::emit_flight(&mut pipe.server).unwrap();
16000
16001        // Both connections are not established.
16002        assert!(!pipe.server.is_established());
16003        assert!(!pipe.client.is_established());
16004
16005        // Connection should already be closed due the failure during key signing.
16006        assert_eq!(
16007            pipe.server.close(true, 123, b"fail whale"),
16008            Err(Error::Done)
16009        );
16010
16011        testing::process_flight(&mut pipe.client, flight).unwrap();
16012
16013        // Connection should already be closed due the failure during key signing.
16014        assert_eq!(
16015            pipe.client.close(true, 123, b"fail whale"),
16016            Err(Error::Done)
16017        );
16018
16019        // Connection is not established on the server / client (and never
16020        // will be)
16021        assert!(!pipe.server.is_established());
16022        assert!(!pipe.client.is_established());
16023
16024        assert_eq!(pipe.advance(), Ok(()));
16025
16026        assert_eq!(
16027            pipe.server.local_error(),
16028            Some(&ConnectionError {
16029                is_app: false,
16030                error_code: 0x01,
16031                reason: vec![],
16032            })
16033        );
16034        assert_eq!(
16035            pipe.client.peer_error(),
16036            Some(&ConnectionError {
16037                is_app: false,
16038                error_code: 0x01,
16039                reason: vec![],
16040            })
16041        );
16042    }
16043
16044    #[rstest]
16045    fn app_close_by_server_during_handshake_not_established(
16046        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16047    ) {
16048        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16049
16050        // Client sends initial flight.
16051        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16052        testing::process_flight(&mut pipe.server, flight).unwrap();
16053
16054        let flight = testing::emit_flight(&mut pipe.server).unwrap();
16055
16056        // Both connections are not established.
16057        assert!(!pipe.client.is_established() && !pipe.server.is_established());
16058
16059        // Server closes before connection is established.
16060        pipe.server.close(true, 123, b"fail whale").unwrap();
16061
16062        testing::process_flight(&mut pipe.client, flight).unwrap();
16063
16064        // Connection is established on the client.
16065        assert!(pipe.client.is_established());
16066
16067        // Client sends after connection is established.
16068        pipe.client.stream_send(0, b"badauthtoken", true).unwrap();
16069
16070        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16071        testing::process_flight(&mut pipe.server, flight).unwrap();
16072
16073        // Connection is not established on the server (and never will be)
16074        assert!(!pipe.server.is_established());
16075
16076        assert_eq!(pipe.advance(), Ok(()));
16077
16078        assert_eq!(
16079            pipe.server.local_error(),
16080            Some(&ConnectionError {
16081                is_app: false,
16082                error_code: 0x0c,
16083                reason: vec![],
16084            })
16085        );
16086        assert_eq!(
16087            pipe.client.peer_error(),
16088            Some(&ConnectionError {
16089                is_app: false,
16090                error_code: 0x0c,
16091                reason: vec![],
16092            })
16093        );
16094    }
16095
16096    #[rstest]
16097    fn app_close_by_server_during_handshake_established(
16098        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16099    ) {
16100        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16101
16102        // Client sends initial flight.
16103        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16104        testing::process_flight(&mut pipe.server, flight).unwrap();
16105
16106        let flight = testing::emit_flight(&mut pipe.server).unwrap();
16107
16108        // Both connections are not established.
16109        assert!(!pipe.client.is_established() && !pipe.server.is_established());
16110
16111        testing::process_flight(&mut pipe.client, flight).unwrap();
16112
16113        // Connection is established on the client.
16114        assert!(pipe.client.is_established());
16115
16116        // Client sends after connection is established.
16117        pipe.client.stream_send(0, b"badauthtoken", true).unwrap();
16118
16119        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16120        testing::process_flight(&mut pipe.server, flight).unwrap();
16121
16122        // Connection is established on the server but the Handshake ACK has not
16123        // been sent yet.
16124        assert!(pipe.server.is_established());
16125
16126        // Server closes after connection is established.
16127        pipe.server
16128            .close(true, 123, b"Invalid authentication")
16129            .unwrap();
16130
16131        // Server sends Handshake ACK and then 1RTT CONNECTION_CLOSE.
16132        assert_eq!(pipe.advance(), Ok(()));
16133
16134        assert_eq!(
16135            pipe.server.local_error(),
16136            Some(&ConnectionError {
16137                is_app: true,
16138                error_code: 123,
16139                reason: b"Invalid authentication".to_vec()
16140            })
16141        );
16142        assert_eq!(
16143            pipe.client.peer_error(),
16144            Some(&ConnectionError {
16145                is_app: true,
16146                error_code: 123,
16147                reason: b"Invalid authentication".to_vec()
16148            })
16149        );
16150    }
16151
16152    #[rstest]
16153    fn transport_close_by_client_during_handshake_established(
16154        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16155    ) {
16156        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16157
16158        // Client sends initial flight.
16159        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16160        testing::process_flight(&mut pipe.server, flight).unwrap();
16161
16162        let flight = testing::emit_flight(&mut pipe.server).unwrap();
16163
16164        // Both connections are not established.
16165        assert!(!pipe.client.is_established() && !pipe.server.is_established());
16166
16167        testing::process_flight(&mut pipe.client, flight).unwrap();
16168
16169        // Connection is established on the client.
16170        assert!(pipe.client.is_established());
16171
16172        // Client sends after connection is established.
16173        pipe.client.close(false, 123, b"connection close").unwrap();
16174
16175        let flight = testing::emit_flight(&mut pipe.client).unwrap();
16176        testing::process_flight(&mut pipe.server, flight).unwrap();
16177
16178        assert_eq!(
16179            pipe.server.peer_error(),
16180            Some(&ConnectionError {
16181                is_app: false,
16182                error_code: 123,
16183                reason: b"connection close".to_vec()
16184            })
16185        );
16186        assert_eq!(
16187            pipe.client.local_error(),
16188            Some(&ConnectionError {
16189                is_app: false,
16190                error_code: 123,
16191                reason: b"connection close".to_vec()
16192            })
16193        );
16194    }
16195
16196    #[rstest]
16197    fn peer_error(
16198        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16199    ) {
16200        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16201        assert_eq!(pipe.handshake(), Ok(()));
16202
16203        assert_eq!(pipe.server.close(false, 0x1234, b"hello?"), Ok(()));
16204        assert_eq!(pipe.advance(), Ok(()));
16205
16206        assert_eq!(
16207            pipe.client.peer_error(),
16208            Some(&ConnectionError {
16209                is_app: false,
16210                error_code: 0x1234u64,
16211                reason: b"hello?".to_vec()
16212            })
16213        );
16214    }
16215
16216    #[rstest]
16217    fn app_peer_error(
16218        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16219    ) {
16220        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16221        assert_eq!(pipe.handshake(), Ok(()));
16222
16223        assert_eq!(pipe.server.close(true, 0x1234, b"hello!"), Ok(()));
16224        assert_eq!(pipe.advance(), Ok(()));
16225
16226        assert_eq!(
16227            pipe.client.peer_error(),
16228            Some(&ConnectionError {
16229                is_app: true,
16230                error_code: 0x1234u64,
16231                reason: b"hello!".to_vec()
16232            })
16233        );
16234    }
16235
16236    #[rstest]
16237    fn local_error(
16238        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16239    ) {
16240        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
16241        assert_eq!(pipe.handshake(), Ok(()));
16242
16243        assert_eq!(pipe.server.local_error(), None);
16244
16245        assert_eq!(pipe.server.close(true, 0x1234, b"hello!"), Ok(()));
16246
16247        assert_eq!(
16248            pipe.server.local_error(),
16249            Some(&ConnectionError {
16250                is_app: true,
16251                error_code: 0x1234u64,
16252                reason: b"hello!".to_vec()
16253            })
16254        );
16255    }
16256
16257    #[rstest]
16258    fn update_max_datagram_size(
16259        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16260    ) {
16261        let mut client_scid = [0; 16];
16262        rand::rand_bytes(&mut client_scid[..]);
16263        let client_scid = ConnectionId::from_ref(&client_scid);
16264        let client_addr = "127.0.0.1:1234".parse().unwrap();
16265
16266        let mut server_scid = [0; 16];
16267        rand::rand_bytes(&mut server_scid[..]);
16268        let server_scid = ConnectionId::from_ref(&server_scid);
16269        let server_addr = "127.0.0.1:4321".parse().unwrap();
16270
16271        let mut client_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16272        assert_eq!(
16273            client_config.set_cc_algorithm_name(cc_algorithm_name),
16274            Ok(())
16275        );
16276        client_config
16277            .set_application_protos(&[b"proto1", b"proto2"])
16278            .unwrap();
16279        client_config.set_max_recv_udp_payload_size(1200);
16280
16281        let mut server_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16282        assert_eq!(
16283            server_config.set_cc_algorithm_name(cc_algorithm_name),
16284            Ok(())
16285        );
16286        server_config
16287            .load_cert_chain_from_pem_file("examples/cert.crt")
16288            .unwrap();
16289        server_config
16290            .load_priv_key_from_pem_file("examples/cert.key")
16291            .unwrap();
16292        server_config
16293            .set_application_protos(&[b"proto1", b"proto2"])
16294            .unwrap();
16295        server_config.verify_peer(false);
16296        server_config
16297            .set_application_protos(&[b"proto1", b"proto2"])
16298            .unwrap();
16299        // Larger than the client
16300        server_config.set_max_send_udp_payload_size(1500);
16301
16302        let mut pipe = testing::Pipe {
16303            client: connect(
16304                Some("quic.tech"),
16305                &client_scid,
16306                client_addr,
16307                server_addr,
16308                &mut client_config,
16309            )
16310            .unwrap(),
16311            server: accept(
16312                &server_scid,
16313                None,
16314                server_addr,
16315                client_addr,
16316                &mut server_config,
16317            )
16318            .unwrap(),
16319        };
16320
16321        // Before handshake
16322        assert_eq!(
16323            pipe.server
16324                .paths
16325                .get_active()
16326                .expect("no active")
16327                .recovery
16328                .max_datagram_size(),
16329            1500,
16330        );
16331
16332        assert_eq!(pipe.handshake(), Ok(()));
16333
16334        // After handshake, max_datagram_size should match to client's
16335        // max_recv_udp_payload_size which is smaller
16336        assert_eq!(
16337            pipe.server
16338                .paths
16339                .get_active()
16340                .expect("no active")
16341                .recovery
16342                .max_datagram_size(),
16343            1200,
16344        );
16345        assert_eq!(
16346            pipe.server
16347                .paths
16348                .get_active()
16349                .expect("no active")
16350                .recovery
16351                .cwnd(),
16352            if cc_algorithm_name == "cubic" {
16353                12000
16354            } else {
16355                if cfg!(feature = "openssl") {
16356                    13437
16357                } else {
16358                    13421
16359                }
16360            },
16361        );
16362    }
16363
16364    #[rstest]
16365    /// Tests that connection-level send capacity decreases as more stream data
16366    /// is buffered.
16367    fn send_capacity(
16368        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16369    ) {
16370        let mut buf = [0; 65535];
16371
16372        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16373        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16374        config
16375            .load_cert_chain_from_pem_file("examples/cert.crt")
16376            .unwrap();
16377        config
16378            .load_priv_key_from_pem_file("examples/cert.key")
16379            .unwrap();
16380        config
16381            .set_application_protos(&[b"proto1", b"proto2"])
16382            .unwrap();
16383        config.set_initial_max_data(100000);
16384        config.set_initial_max_stream_data_bidi_local(10000);
16385        config.set_initial_max_stream_data_bidi_remote(10000);
16386        config.set_initial_max_streams_bidi(10);
16387        config.verify_peer(false);
16388
16389        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16390        assert_eq!(pipe.handshake(), Ok(()));
16391
16392        assert_eq!(pipe.client.stream_send(0, b"hello!", true), Ok(6));
16393        assert_eq!(pipe.advance(), Ok(()));
16394
16395        assert_eq!(pipe.client.stream_send(4, b"hello!", true), Ok(6));
16396        assert_eq!(pipe.advance(), Ok(()));
16397
16398        assert_eq!(pipe.client.stream_send(8, b"hello!", true), Ok(6));
16399        assert_eq!(pipe.advance(), Ok(()));
16400
16401        assert_eq!(pipe.client.stream_send(12, b"hello!", true), Ok(6));
16402        assert_eq!(pipe.advance(), Ok(()));
16403
16404        let mut r = pipe.server.readable().collect::<Vec<u64>>();
16405        assert_eq!(r.len(), 4);
16406
16407        r.sort();
16408
16409        assert_eq!(r, [0, 4, 8, 12]);
16410
16411        assert_eq!(pipe.server.stream_recv(0, &mut buf), Ok((6, true)));
16412        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((6, true)));
16413        assert_eq!(pipe.server.stream_recv(8, &mut buf), Ok((6, true)));
16414        assert_eq!(pipe.server.stream_recv(12, &mut buf), Ok((6, true)));
16415
16416        assert_eq!(
16417            pipe.server.tx_cap,
16418            if cc_algorithm_name == "cubic" {
16419                12000
16420            } else {
16421                if cfg!(feature = "openssl") {
16422                    13959
16423                } else {
16424                    13873
16425                }
16426            }
16427        );
16428
16429        assert_eq!(pipe.server.stream_send(0, &buf[..5000], false), Ok(5000));
16430        assert_eq!(pipe.server.stream_send(4, &buf[..5000], false), Ok(5000));
16431        assert_eq!(
16432            pipe.server.stream_send(8, &buf[..5000], false),
16433            if cc_algorithm_name == "cubic" {
16434                Ok(2000)
16435            } else {
16436                if cfg!(feature = "openssl") {
16437                    Ok(3959)
16438                } else {
16439                    Ok(3873)
16440                }
16441            }
16442        );
16443
16444        // No more connection send capacity.
16445        assert_eq!(
16446            pipe.server.stream_send(12, &buf[..5000], false),
16447            Err(Error::Done)
16448        );
16449        assert_eq!(pipe.server.tx_cap, 0);
16450
16451        assert_eq!(pipe.advance(), Ok(()));
16452    }
16453
16454    #[cfg(feature = "boringssl-boring-crate")]
16455    #[rstest]
16456    fn user_provided_boring_ctx(
16457        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16458    ) -> Result<()> {
16459        // Manually construct boring ssl ctx for server
16460        let mut server_tls_ctx_builder =
16461            boring::ssl::SslContextBuilder::new(boring::ssl::SslMethod::tls())
16462                .unwrap();
16463        server_tls_ctx_builder
16464            .set_certificate_chain_file("examples/cert.crt")
16465            .unwrap();
16466        server_tls_ctx_builder
16467            .set_private_key_file(
16468                "examples/cert.key",
16469                boring::ssl::SslFiletype::PEM,
16470            )
16471            .unwrap();
16472
16473        let mut server_config = Config::with_boring_ssl_ctx_builder(
16474            crate::PROTOCOL_VERSION,
16475            server_tls_ctx_builder,
16476        )?;
16477        let mut client_config = Config::new(crate::PROTOCOL_VERSION)?;
16478        assert_eq!(
16479            client_config.set_cc_algorithm_name(cc_algorithm_name),
16480            Ok(())
16481        );
16482        client_config.load_cert_chain_from_pem_file("examples/cert.crt")?;
16483        client_config.load_priv_key_from_pem_file("examples/cert.key")?;
16484
16485        for config in [&mut client_config, &mut server_config] {
16486            config.set_application_protos(&[b"proto1", b"proto2"])?;
16487            config.set_initial_max_data(30);
16488            config.set_initial_max_stream_data_bidi_local(15);
16489            config.set_initial_max_stream_data_bidi_remote(15);
16490            config.set_initial_max_stream_data_uni(10);
16491            config.set_initial_max_streams_bidi(3);
16492            config.set_initial_max_streams_uni(3);
16493            config.set_max_idle_timeout(180_000);
16494            config.verify_peer(false);
16495            config.set_ack_delay_exponent(8);
16496        }
16497
16498        let mut pipe = testing::Pipe::with_client_and_server_config(
16499            &mut client_config,
16500            &mut server_config,
16501        )?;
16502
16503        assert_eq!(pipe.handshake(), Ok(()));
16504
16505        Ok(())
16506    }
16507
16508    #[cfg(feature = "boringssl-boring-crate")]
16509    #[rstest]
16510    fn in_handshake_config(
16511        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16512    ) -> Result<()> {
16513        let mut buf = [0; 65535];
16514
16515        const CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS: usize = 30;
16516
16517        // Manually construct `SSlContextBuilder` for the server.
16518        let mut server_tls_ctx_builder =
16519            boring::ssl::SslContextBuilder::new(boring::ssl::SslMethod::tls())
16520                .unwrap();
16521        server_tls_ctx_builder
16522            .set_certificate_chain_file("examples/cert.crt")
16523            .unwrap();
16524        server_tls_ctx_builder
16525            .set_private_key_file(
16526                "examples/cert.key",
16527                boring::ssl::SslFiletype::PEM,
16528            )
16529            .unwrap();
16530        server_tls_ctx_builder.set_select_certificate_callback(|mut hello| {
16531            <Connection>::set_initial_congestion_window_packets_in_handshake(
16532                hello.ssl_mut(),
16533                CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS,
16534            )
16535            .unwrap();
16536
16537            Ok(())
16538        });
16539
16540        let mut server_config = Config::with_boring_ssl_ctx_builder(
16541            crate::PROTOCOL_VERSION,
16542            server_tls_ctx_builder,
16543        )?;
16544        assert_eq!(
16545            server_config.set_cc_algorithm_name(cc_algorithm_name),
16546            Ok(())
16547        );
16548
16549        let mut client_config = Config::new(crate::PROTOCOL_VERSION)?;
16550        client_config.load_cert_chain_from_pem_file("examples/cert.crt")?;
16551        client_config.load_priv_key_from_pem_file("examples/cert.key")?;
16552
16553        for config in [&mut client_config, &mut server_config] {
16554            config.set_application_protos(&[b"proto1", b"proto2"])?;
16555            config.set_initial_max_data(1000000);
16556            config.set_initial_max_stream_data_bidi_local(15);
16557            config.set_initial_max_stream_data_bidi_remote(15);
16558            config.set_initial_max_stream_data_uni(10);
16559            config.set_initial_max_streams_bidi(3);
16560            config.set_initial_max_streams_uni(3);
16561            config.set_max_idle_timeout(180_000);
16562            config.verify_peer(false);
16563            config.set_ack_delay_exponent(8);
16564        }
16565
16566        let mut pipe = testing::Pipe::with_client_and_server_config(
16567            &mut client_config,
16568            &mut server_config,
16569        )?;
16570
16571        // Client sends initial flight.
16572        let (len, _) = pipe.client.send(&mut buf).unwrap();
16573
16574        assert_eq!(pipe.server.tx_cap, 0);
16575
16576        // Server receives client's initial flight and updates its config.
16577        pipe.server_recv(&mut buf[..len]).unwrap();
16578
16579        assert_eq!(
16580            pipe.server.tx_cap,
16581            CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS * 1200
16582        );
16583
16584        // Server sends initial flight.
16585        let (len, _) = pipe.server.send(&mut buf).unwrap();
16586        pipe.client_recv(&mut buf[..len]).unwrap();
16587
16588        assert_eq!(pipe.handshake(), Ok(()));
16589
16590        Ok(())
16591    }
16592
16593    #[rstest]
16594    fn initial_cwnd(
16595        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16596    ) -> Result<()> {
16597        const CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS: usize = 30;
16598
16599        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
16600        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16601        config.set_initial_congestion_window_packets(
16602            CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS,
16603        );
16604        // From Pipe::new()
16605        config.load_cert_chain_from_pem_file("examples/cert.crt")?;
16606        config.load_priv_key_from_pem_file("examples/cert.key")?;
16607        config.set_application_protos(&[b"proto1", b"proto2"])?;
16608        config.set_initial_max_data(1000000);
16609        config.set_initial_max_stream_data_bidi_local(15);
16610        config.set_initial_max_stream_data_bidi_remote(15);
16611        config.set_initial_max_stream_data_uni(10);
16612        config.set_initial_max_streams_bidi(3);
16613        config.set_initial_max_streams_uni(3);
16614        config.set_max_idle_timeout(180_000);
16615        config.verify_peer(false);
16616        config.set_ack_delay_exponent(8);
16617
16618        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16619        assert_eq!(pipe.handshake(), Ok(()));
16620
16621        if cc_algorithm_name == "cubic" {
16622            assert_eq!(
16623                pipe.server.tx_cap,
16624                CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS * 1200
16625            );
16626        } else {
16627            // TODO understand where these adjustments come from and why they vary
16628            // by TLS implementation and OS target.
16629            let expected = CUSTOM_INITIAL_CONGESTION_WINDOW_PACKETS * 1200 +
16630                if cfg!(feature = "openssl") {
16631                    1463
16632                } else {
16633                    1447
16634                };
16635
16636            assert!(
16637                pipe.server.tx_cap >= expected,
16638                "{} vs {}",
16639                pipe.server.tx_cap,
16640                expected
16641            );
16642            assert!(
16643                pipe.server.tx_cap <= expected + 1,
16644                "{} vs {}",
16645                pipe.server.tx_cap,
16646                expected + 1
16647            );
16648        }
16649
16650        Ok(())
16651    }
16652
16653    #[rstest]
16654    /// Tests that resetting a stream restores flow control for unsent data.
16655    fn last_tx_data_larger_than_tx_data(
16656        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16657    ) {
16658        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
16659        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16660        config
16661            .set_application_protos(&[b"proto1", b"proto2"])
16662            .unwrap();
16663        config.set_initial_max_data(12000);
16664        config.set_initial_max_stream_data_bidi_local(20000);
16665        config.set_initial_max_stream_data_bidi_remote(20000);
16666        config.set_max_recv_udp_payload_size(1200);
16667        config.verify_peer(false);
16668
16669        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
16670        assert_eq!(pipe.handshake(), Ok(()));
16671
16672        // Client opens stream 4 and 8.
16673        assert_eq!(pipe.client.stream_send(4, b"a", true), Ok(1));
16674        assert_eq!(pipe.client.stream_send(8, b"b", true), Ok(1));
16675        assert_eq!(pipe.advance(), Ok(()));
16676
16677        // Server reads stream data.
16678        let mut b = [0; 15];
16679        pipe.server.stream_recv(4, &mut b).unwrap();
16680
16681        // Server sends stream data close to cwnd (12000).
16682        let buf = [0; 10000];
16683        assert_eq!(pipe.server.stream_send(4, &buf, false), Ok(10000));
16684
16685        testing::emit_flight(&mut pipe.server).unwrap();
16686
16687        // Server buffers some data, until send capacity limit reached.
16688        let mut buf = [0; 1200];
16689        assert_eq!(pipe.server.stream_send(4, &buf, false), Ok(1200));
16690        assert_eq!(pipe.server.stream_send(8, &buf, false), Ok(800));
16691        assert_eq!(pipe.server.stream_send(4, &buf, false), Err(Error::Done));
16692
16693        // Wait for PTO to expire.
16694        let timer = pipe.server.timeout().unwrap();
16695        std::thread::sleep(timer + time::Duration::from_millis(1));
16696
16697        pipe.server.on_timeout();
16698
16699        // Server sends PTO probe (not limited to cwnd),
16700        // to update last_tx_data.
16701        let (len, _) = pipe.server.send(&mut buf).unwrap();
16702        assert_eq!(len, 1200);
16703
16704        // Client sends STOP_SENDING to decrease tx_data
16705        // by unsent data. It will make last_tx_data > tx_data
16706        // and trigger #1232 bug.
16707        let frames = [frame::Frame::StopSending {
16708            stream_id: 4,
16709            error_code: 42,
16710        }];
16711
16712        let pkt_type = packet::Type::Short;
16713        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
16714            .unwrap();
16715    }
16716
16717    /// Tests that when the client provides a new ConnectionId, it eventually
16718    /// reaches the server and notifies the application.
16719    #[rstest]
16720    fn send_connection_ids(
16721        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16722    ) {
16723        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16724        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16725        config
16726            .load_cert_chain_from_pem_file("examples/cert.crt")
16727            .unwrap();
16728        config
16729            .load_priv_key_from_pem_file("examples/cert.key")
16730            .unwrap();
16731        config
16732            .set_application_protos(&[b"proto1", b"proto2"])
16733            .unwrap();
16734        config.verify_peer(false);
16735        config.set_active_connection_id_limit(3);
16736
16737        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16738        assert_eq!(pipe.handshake(), Ok(()));
16739
16740        // So far, there should not have any QUIC event.
16741        assert_eq!(pipe.client.path_event_next(), None);
16742        assert_eq!(pipe.server.path_event_next(), None);
16743        assert_eq!(pipe.client.scids_left(), 2);
16744
16745        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
16746        assert_eq!(pipe.client.new_scid(&scid, reset_token, false), Ok(1));
16747
16748        // Let exchange packets over the connection.
16749        assert_eq!(pipe.advance(), Ok(()));
16750
16751        // At this point, the server should be notified that it has a new CID.
16752        assert_eq!(pipe.server.available_dcids(), 1);
16753        assert_eq!(pipe.server.path_event_next(), None);
16754        assert_eq!(pipe.client.path_event_next(), None);
16755        assert_eq!(pipe.client.scids_left(), 1);
16756
16757        // Now, a second CID can be provided.
16758        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
16759        assert_eq!(pipe.client.new_scid(&scid, reset_token, false), Ok(2));
16760
16761        // Let exchange packets over the connection.
16762        assert_eq!(pipe.advance(), Ok(()));
16763
16764        // At this point, the server should be notified that it has a new CID.
16765        assert_eq!(pipe.server.available_dcids(), 2);
16766        assert_eq!(pipe.server.path_event_next(), None);
16767        assert_eq!(pipe.client.path_event_next(), None);
16768        assert_eq!(pipe.client.scids_left(), 0);
16769
16770        // If now the client tries to send another CID, it reports an error
16771        // since it exceeds the limit of active CIDs.
16772        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
16773        assert_eq!(
16774            pipe.client.new_scid(&scid, reset_token, false),
16775            Err(Error::IdLimit),
16776        );
16777    }
16778
16779    #[rstest]
16780    /// Tests that NEW_CONNECTION_ID with zero-length CID are rejected.
16781    fn connection_id_zero(
16782        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16783    ) {
16784        let mut buf = [0; 65535];
16785
16786        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16787        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16788        config
16789            .load_cert_chain_from_pem_file("examples/cert.crt")
16790            .unwrap();
16791        config
16792            .load_priv_key_from_pem_file("examples/cert.key")
16793            .unwrap();
16794        config
16795            .set_application_protos(&[b"proto1", b"proto2"])
16796            .unwrap();
16797        config.verify_peer(false);
16798        config.set_active_connection_id_limit(2);
16799
16800        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16801        assert_eq!(pipe.handshake(), Ok(()));
16802
16803        let mut frames = Vec::new();
16804
16805        // Client adds a CID that is too short.
16806        let (scid, reset_token) = testing::create_cid_and_reset_token(0);
16807
16808        frames.push(frame::Frame::NewConnectionId {
16809            seq_num: 1,
16810            retire_prior_to: 0,
16811            conn_id: scid.to_vec(),
16812            reset_token: reset_token.to_be_bytes(),
16813        });
16814
16815        let pkt_type = packet::Type::Short;
16816
16817        let written =
16818            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
16819                .unwrap();
16820
16821        let active_path = pipe.server.paths.get_active().unwrap();
16822        let info = RecvInfo {
16823            to: active_path.local_addr(),
16824            from: active_path.peer_addr(),
16825        };
16826
16827        assert_eq!(
16828            pipe.server.recv(&mut buf[..written], info),
16829            Err(Error::InvalidFrame)
16830        );
16831
16832        let written = match pipe.server.send(&mut buf) {
16833            Ok((write, _)) => write,
16834
16835            Err(_) => unreachable!(),
16836        };
16837
16838        let frames =
16839            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
16840        let mut iter = frames.iter();
16841
16842        assert_eq!(
16843            iter.next(),
16844            Some(&frame::Frame::ConnectionClose {
16845                error_code: 0x7,
16846                frame_type: 0,
16847                reason: Vec::new(),
16848            })
16849        );
16850    }
16851
16852    #[rstest]
16853    /// Tests that NEW_CONNECTION_ID with too long CID are rejected.
16854    fn connection_id_invalid_max_len(
16855        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16856    ) {
16857        let mut buf = [0; 65535];
16858
16859        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16860        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16861        config
16862            .load_cert_chain_from_pem_file("examples/cert.crt")
16863            .unwrap();
16864        config
16865            .load_priv_key_from_pem_file("examples/cert.key")
16866            .unwrap();
16867        config
16868            .set_application_protos(&[b"proto1", b"proto2"])
16869            .unwrap();
16870        config.verify_peer(false);
16871        config.set_active_connection_id_limit(2);
16872
16873        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16874        assert_eq!(pipe.handshake(), Ok(()));
16875
16876        let mut frames = Vec::new();
16877
16878        // Client adds a CID that is too long.
16879        let (scid, reset_token) =
16880            testing::create_cid_and_reset_token(MAX_CONN_ID_LEN + 1);
16881
16882        frames.push(frame::Frame::NewConnectionId {
16883            seq_num: 1,
16884            retire_prior_to: 0,
16885            conn_id: scid.to_vec(),
16886            reset_token: reset_token.to_be_bytes(),
16887        });
16888
16889        let pkt_type = packet::Type::Short;
16890
16891        let written =
16892            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
16893                .unwrap();
16894
16895        let active_path = pipe.server.paths.get_active().unwrap();
16896        let info = RecvInfo {
16897            to: active_path.local_addr(),
16898            from: active_path.peer_addr(),
16899        };
16900
16901        assert_eq!(
16902            pipe.server.recv(&mut buf[..written], info),
16903            Err(Error::InvalidFrame)
16904        );
16905
16906        let written = match pipe.server.send(&mut buf) {
16907            Ok((write, _)) => write,
16908
16909            Err(_) => unreachable!(),
16910        };
16911
16912        let frames =
16913            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
16914        let mut iter = frames.iter();
16915
16916        assert_eq!(
16917            iter.next(),
16918            Some(&frame::Frame::ConnectionClose {
16919                error_code: 0x7,
16920                frame_type: 0,
16921                reason: Vec::new(),
16922            })
16923        );
16924    }
16925
16926    #[rstest]
16927    /// Exercises the handling of NEW_CONNECTION_ID and RETIRE_CONNECTION_ID
16928    /// frames.
16929    fn connection_id_handling(
16930        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
16931    ) {
16932        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16933        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
16934        config
16935            .load_cert_chain_from_pem_file("examples/cert.crt")
16936            .unwrap();
16937        config
16938            .load_priv_key_from_pem_file("examples/cert.key")
16939            .unwrap();
16940        config
16941            .set_application_protos(&[b"proto1", b"proto2"])
16942            .unwrap();
16943        config.verify_peer(false);
16944        config.set_active_connection_id_limit(2);
16945
16946        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16947        assert_eq!(pipe.handshake(), Ok(()));
16948
16949        // So far, there should not have any QUIC event.
16950        assert_eq!(pipe.client.path_event_next(), None);
16951        assert_eq!(pipe.server.path_event_next(), None);
16952        assert_eq!(pipe.client.scids_left(), 1);
16953
16954        let scid = pipe.client.source_id().into_owned();
16955
16956        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
16957        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
16958
16959        // Let exchange packets over the connection.
16960        assert_eq!(pipe.advance(), Ok(()));
16961
16962        // At this point, the server should be notified that it has a new CID.
16963        assert_eq!(pipe.server.available_dcids(), 1);
16964        assert_eq!(pipe.server.path_event_next(), None);
16965        assert_eq!(pipe.client.path_event_next(), None);
16966        assert_eq!(pipe.client.scids_left(), 0);
16967
16968        // Now we assume that the client wants to advertise more source
16969        // Connection IDs than the advertised limit. This is valid if it
16970        // requests its peer to retire enough Connection IDs to fit within the
16971        // limits.
16972
16973        let (scid_2, reset_token_2) = testing::create_cid_and_reset_token(16);
16974        assert_eq!(pipe.client.new_scid(&scid_2, reset_token_2, true), Ok(2));
16975
16976        // Let exchange packets over the connection.
16977        assert_eq!(pipe.advance(), Ok(()));
16978
16979        // At this point, the server still have a spare DCID.
16980        assert_eq!(pipe.server.available_dcids(), 1);
16981        assert_eq!(pipe.server.path_event_next(), None);
16982
16983        // Client should have received a retired notification.
16984        assert_eq!(pipe.client.retired_scid_next(), Some(scid));
16985        assert_eq!(pipe.client.retired_scid_next(), None);
16986
16987        assert_eq!(pipe.client.path_event_next(), None);
16988        assert_eq!(pipe.client.scids_left(), 0);
16989
16990        // The active Destination Connection ID of the server should now be the
16991        // one with sequence number 1.
16992        assert_eq!(pipe.server.destination_id(), scid_1);
16993
16994        // Now tries to experience CID retirement. If the server tries to remove
16995        // non-existing DCIDs, it fails.
16996        assert_eq!(pipe.server.retire_dcid(0), Err(Error::InvalidState));
16997        assert_eq!(pipe.server.retire_dcid(3), Err(Error::InvalidState));
16998
16999        // Now it removes DCID with sequence 1.
17000        assert_eq!(pipe.server.retire_dcid(1), Ok(()));
17001
17002        // Let exchange packets over the connection.
17003        assert_eq!(pipe.advance(), Ok(()));
17004
17005        assert_eq!(pipe.server.path_event_next(), None);
17006        assert_eq!(pipe.client.retired_scid_next(), Some(scid_1));
17007        assert_eq!(pipe.client.retired_scid_next(), None);
17008
17009        assert_eq!(pipe.server.destination_id(), scid_2);
17010        assert_eq!(pipe.server.available_dcids(), 0);
17011
17012        // Trying to remove the last DCID triggers an error.
17013        assert_eq!(pipe.server.retire_dcid(2), Err(Error::OutOfIdentifiers));
17014    }
17015
17016    #[rstest]
17017    fn lost_connection_id_frames(
17018        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17019    ) {
17020        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17021        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17022        config
17023            .load_cert_chain_from_pem_file("examples/cert.crt")
17024            .unwrap();
17025        config
17026            .load_priv_key_from_pem_file("examples/cert.key")
17027            .unwrap();
17028        config
17029            .set_application_protos(&[b"proto1", b"proto2"])
17030            .unwrap();
17031        config.verify_peer(false);
17032        config.set_active_connection_id_limit(2);
17033
17034        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17035        assert_eq!(pipe.handshake(), Ok(()));
17036
17037        let scid = pipe.client.source_id().into_owned();
17038
17039        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
17040        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
17041
17042        // Packets are sent, but never received.
17043        testing::emit_flight(&mut pipe.client).unwrap();
17044
17045        // Wait until timer expires. Since the RTT is very low, wait a bit more.
17046        let timer = pipe.client.timeout().unwrap();
17047        std::thread::sleep(timer + time::Duration::from_millis(1));
17048
17049        pipe.client.on_timeout();
17050
17051        // Let exchange packets over the connection.
17052        assert_eq!(pipe.advance(), Ok(()));
17053
17054        // At this point, the server should be notified that it has a new CID.
17055        assert_eq!(pipe.server.available_dcids(), 1);
17056
17057        // Now the server retires the first Destination CID.
17058        assert_eq!(pipe.server.retire_dcid(0), Ok(()));
17059
17060        // But the packet never reaches the client.
17061        testing::emit_flight(&mut pipe.server).unwrap();
17062
17063        // Wait until timer expires. Since the RTT is very low, wait a bit more.
17064        let timer = pipe.server.timeout().unwrap();
17065        std::thread::sleep(timer + time::Duration::from_millis(1));
17066
17067        pipe.server.on_timeout();
17068
17069        // Let exchange packets over the connection.
17070        assert_eq!(pipe.advance(), Ok(()));
17071
17072        assert_eq!(pipe.client.retired_scid_next(), Some(scid));
17073        assert_eq!(pipe.client.retired_scid_next(), None);
17074    }
17075
17076    #[rstest]
17077    fn sending_duplicate_scids(
17078        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17079    ) {
17080        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17081        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17082        config
17083            .load_cert_chain_from_pem_file("examples/cert.crt")
17084            .unwrap();
17085        config
17086            .load_priv_key_from_pem_file("examples/cert.key")
17087            .unwrap();
17088        config
17089            .set_application_protos(&[b"proto1", b"proto2"])
17090            .unwrap();
17091        config.verify_peer(false);
17092        config.set_active_connection_id_limit(3);
17093
17094        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17095        assert_eq!(pipe.handshake(), Ok(()));
17096
17097        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
17098        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
17099        assert_eq!(pipe.advance(), Ok(()));
17100
17101        // Trying to send the same CID with a different reset token raises an
17102        // InvalidState error.
17103        let reset_token_2 = reset_token_1.wrapping_add(1);
17104        assert_eq!(
17105            pipe.client.new_scid(&scid_1, reset_token_2, false),
17106            Err(Error::InvalidState),
17107        );
17108
17109        // Retrying to send the exact same CID with the same token returns the
17110        // previously assigned CID seq, but without sending anything.
17111        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
17112        assert!(!pipe.client.ids.has_new_scids());
17113
17114        // Now retire this new CID.
17115        assert_eq!(pipe.server.retire_dcid(1), Ok(()));
17116        assert_eq!(pipe.advance(), Ok(()));
17117
17118        // It is up to the application to ensure that a given SCID is not reused
17119        // later.
17120        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(2));
17121    }
17122
17123    #[rstest]
17124    /// Tests the limit to retired DCID sequence numbers.
17125    fn connection_id_retire_limit(
17126        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17127    ) {
17128        let mut buf = [0; 65535];
17129
17130        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17131        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17132        config
17133            .load_cert_chain_from_pem_file("examples/cert.crt")
17134            .unwrap();
17135        config
17136            .load_priv_key_from_pem_file("examples/cert.key")
17137            .unwrap();
17138        config
17139            .set_application_protos(&[b"proto1", b"proto2"])
17140            .unwrap();
17141        config.verify_peer(false);
17142        config.set_active_connection_id_limit(2);
17143
17144        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17145        assert_eq!(pipe.handshake(), Ok(()));
17146
17147        // So far, there should not have any QUIC event.
17148        assert_eq!(pipe.client.path_event_next(), None);
17149        assert_eq!(pipe.server.path_event_next(), None);
17150        assert_eq!(pipe.client.scids_left(), 1);
17151
17152        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
17153        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
17154
17155        // Let exchange packets over the connection.
17156        assert_eq!(pipe.advance(), Ok(()));
17157
17158        // At this point, the server should be notified that it has a new CID.
17159        assert_eq!(pipe.server.available_dcids(), 1);
17160        assert_eq!(pipe.server.path_event_next(), None);
17161        assert_eq!(pipe.client.path_event_next(), None);
17162        assert_eq!(pipe.client.scids_left(), 0);
17163
17164        let mut frames = Vec::new();
17165
17166        // Client retires more than 3x the number of allowed active CIDs.
17167        for i in 2..=7 {
17168            let (scid, reset_token) = testing::create_cid_and_reset_token(16);
17169
17170            frames.push(frame::Frame::NewConnectionId {
17171                seq_num: i,
17172                retire_prior_to: i,
17173                conn_id: scid.to_vec(),
17174                reset_token: reset_token.to_be_bytes(),
17175            });
17176        }
17177
17178        let pkt_type = packet::Type::Short;
17179
17180        let written =
17181            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
17182                .unwrap();
17183
17184        let active_path = pipe.server.paths.get_active().unwrap();
17185        let info = RecvInfo {
17186            to: active_path.local_addr(),
17187            from: active_path.peer_addr(),
17188        };
17189
17190        assert_eq!(
17191            pipe.server.recv(&mut buf[..written], info),
17192            Err(Error::IdLimit)
17193        );
17194
17195        let written = match pipe.server.send(&mut buf) {
17196            Ok((write, _)) => write,
17197
17198            Err(_) => unreachable!(),
17199        };
17200
17201        let frames =
17202            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
17203        let mut iter = frames.iter();
17204
17205        assert_eq!(
17206            iter.next(),
17207            Some(&frame::Frame::ConnectionClose {
17208                error_code: 0x9,
17209                frame_type: 0,
17210                reason: Vec::new(),
17211            })
17212        );
17213    }
17214
17215    // Utility function.
17216    fn pipe_with_exchanged_cids(
17217        config: &mut Config, client_scid_len: usize, server_scid_len: usize,
17218        additional_cids: usize,
17219    ) -> testing::Pipe {
17220        let mut pipe = testing::Pipe::with_config_and_scid_lengths(
17221            config,
17222            client_scid_len,
17223            server_scid_len,
17224        )
17225        .unwrap();
17226        assert_eq!(pipe.handshake(), Ok(()));
17227
17228        let mut c_cids = Vec::new();
17229        let mut c_reset_tokens = Vec::new();
17230        let mut s_cids = Vec::new();
17231        let mut s_reset_tokens = Vec::new();
17232
17233        for i in 0..additional_cids {
17234            if client_scid_len > 0 {
17235                let (c_cid, c_reset_token) =
17236                    testing::create_cid_and_reset_token(client_scid_len);
17237                c_cids.push(c_cid);
17238                c_reset_tokens.push(c_reset_token);
17239
17240                assert_eq!(
17241                    pipe.client.new_scid(&c_cids[i], c_reset_tokens[i], true),
17242                    Ok(i as u64 + 1)
17243                );
17244            }
17245
17246            if server_scid_len > 0 {
17247                let (s_cid, s_reset_token) =
17248                    testing::create_cid_and_reset_token(server_scid_len);
17249                s_cids.push(s_cid);
17250                s_reset_tokens.push(s_reset_token);
17251                assert_eq!(
17252                    pipe.server.new_scid(&s_cids[i], s_reset_tokens[i], true),
17253                    Ok(i as u64 + 1)
17254                );
17255            }
17256        }
17257
17258        // Let exchange packets over the connection.
17259        assert_eq!(pipe.advance(), Ok(()));
17260
17261        if client_scid_len > 0 {
17262            assert_eq!(pipe.server.available_dcids(), additional_cids);
17263        }
17264
17265        if server_scid_len > 0 {
17266            assert_eq!(pipe.client.available_dcids(), additional_cids);
17267        }
17268
17269        assert_eq!(pipe.server.path_event_next(), None);
17270        assert_eq!(pipe.client.path_event_next(), None);
17271
17272        pipe
17273    }
17274
17275    #[rstest]
17276    fn path_validation(
17277        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17278    ) {
17279        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17280        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17281        config
17282            .load_cert_chain_from_pem_file("examples/cert.crt")
17283            .unwrap();
17284        config
17285            .load_priv_key_from_pem_file("examples/cert.key")
17286            .unwrap();
17287        config
17288            .set_application_protos(&[b"proto1", b"proto2"])
17289            .unwrap();
17290        config.verify_peer(false);
17291        config.set_active_connection_id_limit(2);
17292
17293        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17294        assert_eq!(pipe.handshake(), Ok(()));
17295
17296        let server_addr = testing::Pipe::server_addr();
17297        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17298
17299        // We cannot probe a new path if there are not enough identifiers.
17300        assert_eq!(
17301            pipe.client.probe_path(client_addr_2, server_addr),
17302            Err(Error::OutOfIdentifiers)
17303        );
17304
17305        let (c_cid, c_reset_token) = testing::create_cid_and_reset_token(16);
17306
17307        assert_eq!(pipe.client.new_scid(&c_cid, c_reset_token, true), Ok(1));
17308
17309        let (s_cid, s_reset_token) = testing::create_cid_and_reset_token(16);
17310        assert_eq!(pipe.server.new_scid(&s_cid, s_reset_token, true), Ok(1));
17311
17312        // We need to exchange the CIDs first.
17313        assert_eq!(
17314            pipe.client.probe_path(client_addr_2, server_addr),
17315            Err(Error::OutOfIdentifiers)
17316        );
17317
17318        // Let exchange packets over the connection.
17319        assert_eq!(pipe.advance(), Ok(()));
17320
17321        assert_eq!(pipe.server.available_dcids(), 1);
17322        assert_eq!(pipe.server.path_event_next(), None);
17323        assert_eq!(pipe.client.available_dcids(), 1);
17324        assert_eq!(pipe.client.path_event_next(), None);
17325
17326        // Now the path probing can work.
17327        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17328
17329        // But the server cannot probe a yet-unseen path.
17330        assert_eq!(
17331            pipe.server.probe_path(server_addr, client_addr_2),
17332            Err(Error::InvalidState),
17333        );
17334
17335        assert_eq!(pipe.advance(), Ok(()));
17336
17337        // The path should be validated at some point.
17338        assert_eq!(
17339            pipe.client.path_event_next(),
17340            Some(PathEvent::Validated(client_addr_2, server_addr)),
17341        );
17342        assert_eq!(pipe.client.path_event_next(), None);
17343
17344        // The server should be notified of this new path.
17345        assert_eq!(
17346            pipe.server.path_event_next(),
17347            Some(PathEvent::New(server_addr, client_addr_2)),
17348        );
17349        assert_eq!(
17350            pipe.server.path_event_next(),
17351            Some(PathEvent::Validated(server_addr, client_addr_2)),
17352        );
17353        assert_eq!(pipe.server.path_event_next(), None);
17354
17355        // The server can later probe the path again.
17356        assert_eq!(pipe.server.probe_path(server_addr, client_addr_2), Ok(1));
17357
17358        // This should not trigger any event at client side.
17359        assert_eq!(pipe.client.path_event_next(), None);
17360        assert_eq!(pipe.server.path_event_next(), None);
17361    }
17362
17363    #[rstest]
17364    fn losing_probing_packets(
17365        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17366    ) {
17367        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17368        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17369        config
17370            .load_cert_chain_from_pem_file("examples/cert.crt")
17371            .unwrap();
17372        config
17373            .load_priv_key_from_pem_file("examples/cert.key")
17374            .unwrap();
17375        config
17376            .set_application_protos(&[b"proto1", b"proto2"])
17377            .unwrap();
17378        config.verify_peer(false);
17379        config.set_active_connection_id_limit(2);
17380
17381        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17382
17383        let server_addr = testing::Pipe::server_addr();
17384        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17385        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17386
17387        // The client creates the PATH CHALLENGE, but it is lost.
17388        testing::emit_flight(&mut pipe.client).unwrap();
17389
17390        // Wait until probing timer expires. Since the RTT is very low,
17391        // wait a bit more.
17392        let probed_pid = pipe
17393            .client
17394            .paths
17395            .path_id_from_addrs(&(client_addr_2, server_addr))
17396            .unwrap();
17397        let probe_instant = pipe
17398            .client
17399            .paths
17400            .get(probed_pid)
17401            .unwrap()
17402            .recovery
17403            .loss_detection_timer()
17404            .unwrap();
17405        let timer = probe_instant.duration_since(time::Instant::now());
17406        std::thread::sleep(timer + time::Duration::from_millis(1));
17407
17408        pipe.client.on_timeout();
17409
17410        assert_eq!(pipe.advance(), Ok(()));
17411
17412        // The path should be validated at some point.
17413        assert_eq!(
17414            pipe.client.path_event_next(),
17415            Some(PathEvent::Validated(client_addr_2, server_addr))
17416        );
17417        assert_eq!(pipe.client.path_event_next(), None);
17418
17419        assert_eq!(
17420            pipe.server.path_event_next(),
17421            Some(PathEvent::New(server_addr, client_addr_2))
17422        );
17423        // The path should be validated at some point.
17424        assert_eq!(
17425            pipe.server.path_event_next(),
17426            Some(PathEvent::Validated(server_addr, client_addr_2))
17427        );
17428        assert_eq!(pipe.server.path_event_next(), None);
17429    }
17430
17431    #[rstest]
17432    fn failed_path_validation(
17433        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17434    ) {
17435        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17436        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17437        config
17438            .load_cert_chain_from_pem_file("examples/cert.crt")
17439            .unwrap();
17440        config
17441            .load_priv_key_from_pem_file("examples/cert.key")
17442            .unwrap();
17443        config
17444            .set_application_protos(&[b"proto1", b"proto2"])
17445            .unwrap();
17446        config.verify_peer(false);
17447        config.set_active_connection_id_limit(2);
17448
17449        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17450
17451        let server_addr = testing::Pipe::server_addr();
17452        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17453        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17454
17455        for _ in 0..MAX_PROBING_TIMEOUTS {
17456            // The client creates the PATH CHALLENGE, but it is always lost.
17457            testing::emit_flight(&mut pipe.client).unwrap();
17458
17459            // Wait until probing timer expires. Since the RTT is very low,
17460            // wait a bit more.
17461            let probed_pid = pipe
17462                .client
17463                .paths
17464                .path_id_from_addrs(&(client_addr_2, server_addr))
17465                .unwrap();
17466            let probe_instant = pipe
17467                .client
17468                .paths
17469                .get(probed_pid)
17470                .unwrap()
17471                .recovery
17472                .loss_detection_timer()
17473                .unwrap();
17474            let timer = probe_instant.duration_since(time::Instant::now());
17475            std::thread::sleep(timer + time::Duration::from_millis(1));
17476
17477            pipe.client.on_timeout();
17478        }
17479
17480        assert_eq!(
17481            pipe.client.path_event_next(),
17482            Some(PathEvent::FailedValidation(client_addr_2, server_addr)),
17483        );
17484    }
17485
17486    #[rstest]
17487    fn client_discard_unknown_address(
17488        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17489    ) {
17490        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17491        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17492        config
17493            .load_cert_chain_from_pem_file("examples/cert.crt")
17494            .unwrap();
17495        config
17496            .load_priv_key_from_pem_file("examples/cert.key")
17497            .unwrap();
17498        config
17499            .set_application_protos(&[b"proto1", b"proto2"])
17500            .unwrap();
17501        config.verify_peer(false);
17502        config.set_initial_max_data(30);
17503        config.set_initial_max_stream_data_uni(10);
17504        config.set_initial_max_streams_uni(3);
17505
17506        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17507        assert_eq!(pipe.handshake(), Ok(()));
17508
17509        // Server sends stream data.
17510        assert_eq!(pipe.server.stream_send(3, b"a", true), Ok(1));
17511
17512        let mut flight =
17513            testing::emit_flight(&mut pipe.server).expect("no packet");
17514        // Let's change the address info.
17515        flight
17516            .iter_mut()
17517            .for_each(|(_, si)| si.from = "127.0.0.1:9292".parse().unwrap());
17518        assert_eq!(testing::process_flight(&mut pipe.client, flight), Ok(()));
17519        assert_eq!(pipe.client.paths.len(), 1);
17520    }
17521
17522    #[rstest]
17523    fn path_validation_limited_mtu(
17524        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17525    ) {
17526        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17527        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17528        config
17529            .load_cert_chain_from_pem_file("examples/cert.crt")
17530            .unwrap();
17531        config
17532            .load_priv_key_from_pem_file("examples/cert.key")
17533            .unwrap();
17534        config
17535            .set_application_protos(&[b"proto1", b"proto2"])
17536            .unwrap();
17537        config.verify_peer(false);
17538        config.set_active_connection_id_limit(2);
17539
17540        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17541
17542        let server_addr = testing::Pipe::server_addr();
17543        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17544        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17545        // Limited MTU of 1199 bytes for some reason.
17546        testing::process_flight(
17547            &mut pipe.server,
17548            testing::emit_flight_with_max_buffer(
17549                &mut pipe.client,
17550                1199,
17551                None,
17552                None,
17553            )
17554            .expect("no packet"),
17555        )
17556        .expect("error when processing client packets");
17557        testing::process_flight(
17558            &mut pipe.client,
17559            testing::emit_flight(&mut pipe.server).expect("no packet"),
17560        )
17561        .expect("error when processing client packets");
17562        let probed_pid = pipe
17563            .client
17564            .paths
17565            .path_id_from_addrs(&(client_addr_2, server_addr))
17566            .unwrap();
17567        assert!(!pipe.client.paths.get(probed_pid).unwrap().validated(),);
17568        assert_eq!(pipe.client.path_event_next(), None);
17569        // Now let the client probe at its MTU.
17570        assert_eq!(pipe.advance(), Ok(()));
17571        assert!(pipe.client.paths.get(probed_pid).unwrap().validated());
17572        assert_eq!(
17573            pipe.client.path_event_next(),
17574            Some(PathEvent::Validated(client_addr_2, server_addr))
17575        );
17576    }
17577
17578    #[rstest]
17579    fn path_probing_dos(
17580        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17581    ) {
17582        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17583        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17584        config
17585            .load_cert_chain_from_pem_file("examples/cert.crt")
17586            .unwrap();
17587        config
17588            .load_priv_key_from_pem_file("examples/cert.key")
17589            .unwrap();
17590        config
17591            .set_application_protos(&[b"proto1", b"proto2"])
17592            .unwrap();
17593        config.verify_peer(false);
17594        config.set_active_connection_id_limit(2);
17595
17596        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17597
17598        let server_addr = testing::Pipe::server_addr();
17599        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17600        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17601
17602        assert_eq!(pipe.advance(), Ok(()));
17603
17604        // The path should be validated at some point.
17605        assert_eq!(
17606            pipe.client.path_event_next(),
17607            Some(PathEvent::Validated(client_addr_2, server_addr))
17608        );
17609        assert_eq!(pipe.client.path_event_next(), None);
17610
17611        // The server should be notified of this new path.
17612        assert_eq!(
17613            pipe.server.path_event_next(),
17614            Some(PathEvent::New(server_addr, client_addr_2))
17615        );
17616        assert_eq!(
17617            pipe.server.path_event_next(),
17618            Some(PathEvent::Validated(server_addr, client_addr_2))
17619        );
17620        assert_eq!(pipe.server.path_event_next(), None);
17621
17622        assert_eq!(pipe.server.paths.len(), 2);
17623
17624        // Now forge a packet reusing the unverified path's CID over another
17625        // 4-tuple.
17626        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17627        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
17628        let mut flight =
17629            testing::emit_flight(&mut pipe.client).expect("no generated packet");
17630        flight
17631            .iter_mut()
17632            .for_each(|(_, si)| si.from = client_addr_3);
17633        testing::process_flight(&mut pipe.server, flight)
17634            .expect("failed to process");
17635        assert_eq!(pipe.server.paths.len(), 2);
17636        assert_eq!(
17637            pipe.server.path_event_next(),
17638            Some(PathEvent::ReusedSourceConnectionId(
17639                1,
17640                (server_addr, client_addr_2),
17641                (server_addr, client_addr_3)
17642            ))
17643        );
17644        assert_eq!(pipe.server.path_event_next(), None);
17645    }
17646
17647    #[rstest]
17648    fn retiring_active_path_dcid(
17649        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17650    ) {
17651        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17652        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17653        config
17654            .load_cert_chain_from_pem_file("examples/cert.crt")
17655            .unwrap();
17656        config
17657            .load_priv_key_from_pem_file("examples/cert.key")
17658            .unwrap();
17659        config
17660            .set_application_protos(&[b"proto1", b"proto2"])
17661            .unwrap();
17662        config.verify_peer(false);
17663        config.set_active_connection_id_limit(2);
17664
17665        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17666        let server_addr = testing::Pipe::server_addr();
17667        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17668        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17669
17670        assert_eq!(pipe.client.retire_dcid(0), Err(Error::OutOfIdentifiers));
17671    }
17672
17673    #[rstest]
17674    fn send_on_path_test(
17675        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17676    ) {
17677        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17678        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17679        config
17680            .load_cert_chain_from_pem_file("examples/cert.crt")
17681            .unwrap();
17682        config
17683            .load_priv_key_from_pem_file("examples/cert.key")
17684            .unwrap();
17685        config
17686            .set_application_protos(&[b"proto1", b"proto2"])
17687            .unwrap();
17688        config.verify_peer(false);
17689        config.set_initial_max_data(100000);
17690        config.set_initial_max_stream_data_bidi_local(100000);
17691        config.set_initial_max_stream_data_bidi_remote(100000);
17692        config.set_initial_max_streams_bidi(2);
17693        config.set_active_connection_id_limit(4);
17694
17695        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 3);
17696
17697        let server_addr = testing::Pipe::server_addr();
17698        let client_addr = testing::Pipe::client_addr();
17699        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17700        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17701
17702        let mut buf = [0; 65535];
17703        // There is nothing to send on the initial path.
17704        assert_eq!(
17705            pipe.client.send_on_path(
17706                &mut buf,
17707                Some(client_addr),
17708                Some(server_addr)
17709            ),
17710            Err(Error::Done)
17711        );
17712
17713        // Client should send padded PATH_CHALLENGE.
17714        let (sent, si) = pipe
17715            .client
17716            .send_on_path(&mut buf, Some(client_addr_2), Some(server_addr))
17717            .expect("No error");
17718        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
17719        assert_eq!(si.from, client_addr_2);
17720        assert_eq!(si.to, server_addr);
17721
17722        let ri = RecvInfo {
17723            to: si.to,
17724            from: si.from,
17725        };
17726        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
17727
17728        let stats = pipe.server.stats();
17729        assert_eq!(stats.path_challenge_rx_count, 1);
17730
17731        // A non-existing 4-tuple raises an InvalidState.
17732        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
17733        let server_addr_2 = "127.0.0.1:9876".parse().unwrap();
17734        assert_eq!(
17735            pipe.client.send_on_path(
17736                &mut buf,
17737                Some(client_addr_3),
17738                Some(server_addr)
17739            ),
17740            Err(Error::InvalidState)
17741        );
17742        assert_eq!(
17743            pipe.client.send_on_path(
17744                &mut buf,
17745                Some(client_addr),
17746                Some(server_addr_2)
17747            ),
17748            Err(Error::InvalidState)
17749        );
17750
17751        // Let's introduce some additional path challenges and data exchange.
17752        assert_eq!(pipe.client.probe_path(client_addr, server_addr_2), Ok(2));
17753        assert_eq!(pipe.client.probe_path(client_addr_3, server_addr), Ok(3));
17754        // Just to fit in two packets.
17755        assert_eq!(pipe.client.stream_send(0, &buf[..1201], true), Ok(1201));
17756
17757        // PATH_CHALLENGE
17758        let (sent, si) = pipe
17759            .client
17760            .send_on_path(&mut buf, Some(client_addr), None)
17761            .expect("No error");
17762        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
17763        assert_eq!(si.from, client_addr);
17764        assert_eq!(si.to, server_addr_2);
17765
17766        let ri = RecvInfo {
17767            to: si.to,
17768            from: si.from,
17769        };
17770        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
17771
17772        let stats = pipe.server.stats();
17773        assert_eq!(stats.path_challenge_rx_count, 2);
17774
17775        // STREAM frame on active path.
17776        let (sent, si) = pipe
17777            .client
17778            .send_on_path(&mut buf, Some(client_addr), None)
17779            .expect("No error");
17780        assert_eq!(si.from, client_addr);
17781        assert_eq!(si.to, server_addr);
17782
17783        let ri = RecvInfo {
17784            to: si.to,
17785            from: si.from,
17786        };
17787        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
17788
17789        let stats = pipe.server.stats();
17790        assert_eq!(stats.path_challenge_rx_count, 2);
17791
17792        // PATH_CHALLENGE
17793        let (sent, si) = pipe
17794            .client
17795            .send_on_path(&mut buf, None, Some(server_addr))
17796            .expect("No error");
17797        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
17798        assert_eq!(si.from, client_addr_3);
17799        assert_eq!(si.to, server_addr);
17800
17801        let ri = RecvInfo {
17802            to: si.to,
17803            from: si.from,
17804        };
17805        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
17806
17807        let stats = pipe.server.stats();
17808        assert_eq!(stats.path_challenge_rx_count, 3);
17809
17810        // STREAM frame on active path.
17811        let (sent, si) = pipe
17812            .client
17813            .send_on_path(&mut buf, None, Some(server_addr))
17814            .expect("No error");
17815        assert_eq!(si.from, client_addr);
17816        assert_eq!(si.to, server_addr);
17817
17818        let ri = RecvInfo {
17819            to: si.to,
17820            from: si.from,
17821        };
17822        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
17823
17824        // No more data to exchange leads to Error::Done.
17825        assert_eq!(
17826            pipe.client.send_on_path(&mut buf, Some(client_addr), None),
17827            Err(Error::Done)
17828        );
17829        assert_eq!(
17830            pipe.client.send_on_path(&mut buf, None, Some(server_addr)),
17831            Err(Error::Done)
17832        );
17833
17834        assert_eq!(pipe.advance(), Ok(()));
17835
17836        let mut v1 = pipe.client.paths_iter(client_addr).collect::<Vec<_>>();
17837        let mut v2 = vec![server_addr, server_addr_2];
17838
17839        v1.sort();
17840        v2.sort();
17841
17842        assert_eq!(v1, v2);
17843
17844        let mut v1 = pipe.client.paths_iter(client_addr_2).collect::<Vec<_>>();
17845        let mut v2 = vec![server_addr];
17846
17847        v1.sort();
17848        v2.sort();
17849
17850        assert_eq!(v1, v2);
17851
17852        let mut v1 = pipe.client.paths_iter(client_addr_3).collect::<Vec<_>>();
17853        let mut v2 = vec![server_addr];
17854
17855        v1.sort();
17856        v2.sort();
17857
17858        assert_eq!(v1, v2);
17859
17860        let stats = pipe.server.stats();
17861        assert_eq!(stats.path_challenge_rx_count, 3);
17862    }
17863
17864    #[rstest]
17865    fn connection_migration(
17866        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
17867    ) {
17868        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17869        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
17870        config
17871            .load_cert_chain_from_pem_file("examples/cert.crt")
17872            .unwrap();
17873        config
17874            .load_priv_key_from_pem_file("examples/cert.key")
17875            .unwrap();
17876        config
17877            .set_application_protos(&[b"proto1", b"proto2"])
17878            .unwrap();
17879        config.verify_peer(false);
17880        config.set_active_connection_id_limit(3);
17881        config.set_initial_max_data(30);
17882        config.set_initial_max_stream_data_bidi_local(15);
17883        config.set_initial_max_stream_data_bidi_remote(15);
17884        config.set_initial_max_stream_data_uni(10);
17885        config.set_initial_max_streams_bidi(3);
17886
17887        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 2);
17888
17889        let server_addr = testing::Pipe::server_addr();
17890        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17891        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
17892        let client_addr_4 = "127.0.0.1:8908".parse().unwrap();
17893
17894        // Case 1: the client first probes the new address, the server too, and
17895        // then migrates.
17896        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17897        assert_eq!(pipe.advance(), Ok(()));
17898        assert_eq!(
17899            pipe.client.path_event_next(),
17900            Some(PathEvent::Validated(client_addr_2, server_addr))
17901        );
17902        assert_eq!(pipe.client.path_event_next(), None);
17903        assert_eq!(
17904            pipe.server.path_event_next(),
17905            Some(PathEvent::New(server_addr, client_addr_2))
17906        );
17907        assert_eq!(
17908            pipe.server.path_event_next(),
17909            Some(PathEvent::Validated(server_addr, client_addr_2))
17910        );
17911        assert_eq!(
17912            pipe.client.is_path_validated(client_addr_2, server_addr),
17913            Ok(true)
17914        );
17915        assert_eq!(
17916            pipe.server.is_path_validated(server_addr, client_addr_2),
17917            Ok(true)
17918        );
17919        // The server can never initiates the connection migration.
17920        assert_eq!(
17921            pipe.server.migrate(server_addr, client_addr_2),
17922            Err(Error::InvalidState)
17923        );
17924        assert_eq!(pipe.client.migrate(client_addr_2, server_addr), Ok(1));
17925        assert_eq!(pipe.client.stream_send(0, b"data", true), Ok(4));
17926        assert_eq!(pipe.advance(), Ok(()));
17927        assert_eq!(
17928            pipe.client
17929                .paths
17930                .get_active()
17931                .expect("no active")
17932                .local_addr(),
17933            client_addr_2
17934        );
17935        assert_eq!(
17936            pipe.client
17937                .paths
17938                .get_active()
17939                .expect("no active")
17940                .peer_addr(),
17941            server_addr
17942        );
17943        assert_eq!(
17944            pipe.server.path_event_next(),
17945            Some(PathEvent::PeerMigrated(server_addr, client_addr_2))
17946        );
17947        assert_eq!(pipe.server.path_event_next(), None);
17948        assert_eq!(
17949            pipe.server
17950                .paths
17951                .get_active()
17952                .expect("no active")
17953                .local_addr(),
17954            server_addr
17955        );
17956        assert_eq!(
17957            pipe.server
17958                .paths
17959                .get_active()
17960                .expect("no active")
17961                .peer_addr(),
17962            client_addr_2
17963        );
17964
17965        // Case 2: the client migrates on a path that was not previously
17966        // validated, and has spare SCIDs/DCIDs to do so.
17967        assert_eq!(pipe.client.migrate(client_addr_3, server_addr), Ok(2));
17968        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
17969        assert_eq!(pipe.advance(), Ok(()));
17970        assert_eq!(
17971            pipe.client
17972                .paths
17973                .get_active()
17974                .expect("no active")
17975                .local_addr(),
17976            client_addr_3
17977        );
17978        assert_eq!(
17979            pipe.client
17980                .paths
17981                .get_active()
17982                .expect("no active")
17983                .peer_addr(),
17984            server_addr
17985        );
17986        assert_eq!(
17987            pipe.server.path_event_next(),
17988            Some(PathEvent::New(server_addr, client_addr_3))
17989        );
17990        assert_eq!(
17991            pipe.server.path_event_next(),
17992            Some(PathEvent::Validated(server_addr, client_addr_3))
17993        );
17994        assert_eq!(
17995            pipe.server.path_event_next(),
17996            Some(PathEvent::PeerMigrated(server_addr, client_addr_3))
17997        );
17998        assert_eq!(pipe.server.path_event_next(), None);
17999        assert_eq!(
18000            pipe.server
18001                .paths
18002                .get_active()
18003                .expect("no active")
18004                .local_addr(),
18005            server_addr
18006        );
18007        assert_eq!(
18008            pipe.server
18009                .paths
18010                .get_active()
18011                .expect("no active")
18012                .peer_addr(),
18013            client_addr_3
18014        );
18015
18016        // Case 3: the client tries to migrate on the current active path.
18017        // This is not an error, but it triggers nothing.
18018        assert_eq!(pipe.client.migrate(client_addr_3, server_addr), Ok(2));
18019        assert_eq!(pipe.client.stream_send(8, b"data", true), Ok(4));
18020        assert_eq!(pipe.advance(), Ok(()));
18021        assert_eq!(pipe.client.path_event_next(), None);
18022        assert_eq!(
18023            pipe.client
18024                .paths
18025                .get_active()
18026                .expect("no active")
18027                .local_addr(),
18028            client_addr_3
18029        );
18030        assert_eq!(
18031            pipe.client
18032                .paths
18033                .get_active()
18034                .expect("no active")
18035                .peer_addr(),
18036            server_addr
18037        );
18038        assert_eq!(pipe.server.path_event_next(), None);
18039        assert_eq!(
18040            pipe.server
18041                .paths
18042                .get_active()
18043                .expect("no active")
18044                .local_addr(),
18045            server_addr
18046        );
18047        assert_eq!(
18048            pipe.server
18049                .paths
18050                .get_active()
18051                .expect("no active")
18052                .peer_addr(),
18053            client_addr_3
18054        );
18055
18056        // Case 4: the client tries to migrate on a path that was not previously
18057        // validated, and has no spare SCIDs/DCIDs. Prevent active migration.
18058        assert_eq!(
18059            pipe.client.migrate(client_addr_4, server_addr),
18060            Err(Error::OutOfIdentifiers)
18061        );
18062        assert_eq!(
18063            pipe.client
18064                .paths
18065                .get_active()
18066                .expect("no active")
18067                .local_addr(),
18068            client_addr_3
18069        );
18070        assert_eq!(
18071            pipe.client
18072                .paths
18073                .get_active()
18074                .expect("no active")
18075                .peer_addr(),
18076            server_addr
18077        );
18078    }
18079
18080    #[rstest]
18081    fn connection_migration_zero_length_cid(
18082        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18083    ) {
18084        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18085        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18086        config
18087            .load_cert_chain_from_pem_file("examples/cert.crt")
18088            .unwrap();
18089        config
18090            .load_priv_key_from_pem_file("examples/cert.key")
18091            .unwrap();
18092        config
18093            .set_application_protos(&[b"proto1", b"proto2"])
18094            .unwrap();
18095        config.verify_peer(false);
18096        config.set_active_connection_id_limit(2);
18097        config.set_initial_max_data(30);
18098        config.set_initial_max_stream_data_bidi_local(15);
18099        config.set_initial_max_stream_data_bidi_remote(15);
18100        config.set_initial_max_stream_data_uni(10);
18101        config.set_initial_max_streams_bidi(3);
18102
18103        let mut pipe = pipe_with_exchanged_cids(&mut config, 0, 16, 1);
18104
18105        let server_addr = testing::Pipe::server_addr();
18106        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
18107
18108        // The client migrates on a path that was not previously
18109        // validated, and has spare SCIDs/DCIDs to do so.
18110        assert_eq!(pipe.client.migrate(client_addr_2, server_addr), Ok(1));
18111        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
18112        assert_eq!(pipe.advance(), Ok(()));
18113        assert_eq!(
18114            pipe.client
18115                .paths
18116                .get_active()
18117                .expect("no active")
18118                .local_addr(),
18119            client_addr_2
18120        );
18121        assert_eq!(
18122            pipe.client
18123                .paths
18124                .get_active()
18125                .expect("no active")
18126                .peer_addr(),
18127            server_addr
18128        );
18129        assert_eq!(
18130            pipe.server.path_event_next(),
18131            Some(PathEvent::New(server_addr, client_addr_2))
18132        );
18133        assert_eq!(
18134            pipe.server.path_event_next(),
18135            Some(PathEvent::Validated(server_addr, client_addr_2))
18136        );
18137        assert_eq!(
18138            pipe.server.path_event_next(),
18139            Some(PathEvent::PeerMigrated(server_addr, client_addr_2))
18140        );
18141        assert_eq!(pipe.server.path_event_next(), None);
18142        assert_eq!(
18143            pipe.server
18144                .paths
18145                .get_active()
18146                .expect("no active")
18147                .local_addr(),
18148            server_addr
18149        );
18150        assert_eq!(
18151            pipe.server
18152                .paths
18153                .get_active()
18154                .expect("no active")
18155                .peer_addr(),
18156            client_addr_2
18157        );
18158    }
18159
18160    #[rstest]
18161    fn connection_migration_reordered_non_probing(
18162        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18163    ) {
18164        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18165        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18166        config
18167            .load_cert_chain_from_pem_file("examples/cert.crt")
18168            .unwrap();
18169        config
18170            .load_priv_key_from_pem_file("examples/cert.key")
18171            .unwrap();
18172        config
18173            .set_application_protos(&[b"proto1", b"proto2"])
18174            .unwrap();
18175        config.verify_peer(false);
18176        config.set_active_connection_id_limit(2);
18177        config.set_initial_max_data(30);
18178        config.set_initial_max_stream_data_bidi_local(15);
18179        config.set_initial_max_stream_data_bidi_remote(15);
18180        config.set_initial_max_stream_data_uni(10);
18181        config.set_initial_max_streams_bidi(3);
18182
18183        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
18184
18185        let client_addr = testing::Pipe::client_addr();
18186        let server_addr = testing::Pipe::server_addr();
18187        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
18188
18189        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
18190        assert_eq!(pipe.advance(), Ok(()));
18191        assert_eq!(
18192            pipe.client.path_event_next(),
18193            Some(PathEvent::Validated(client_addr_2, server_addr))
18194        );
18195        assert_eq!(pipe.client.path_event_next(), None);
18196        assert_eq!(
18197            pipe.server.path_event_next(),
18198            Some(PathEvent::New(server_addr, client_addr_2))
18199        );
18200        assert_eq!(
18201            pipe.server.path_event_next(),
18202            Some(PathEvent::Validated(server_addr, client_addr_2))
18203        );
18204        assert_eq!(pipe.server.path_event_next(), None);
18205
18206        // A first flight sent from secondary address.
18207        assert_eq!(pipe.client.stream_send(0, b"data", true), Ok(4));
18208        let mut first = testing::emit_flight(&mut pipe.client).unwrap();
18209        first.iter_mut().for_each(|(_, si)| si.from = client_addr_2);
18210        // A second one, but sent from the original one.
18211        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
18212        let second = testing::emit_flight(&mut pipe.client).unwrap();
18213        // Second flight is received before first one.
18214        assert_eq!(testing::process_flight(&mut pipe.server, second), Ok(()));
18215        assert_eq!(testing::process_flight(&mut pipe.server, first), Ok(()));
18216
18217        // Server does not perform connection migration because of packet
18218        // reordering.
18219        assert_eq!(pipe.server.path_event_next(), None);
18220        assert_eq!(
18221            pipe.server
18222                .paths
18223                .get_active()
18224                .expect("no active")
18225                .peer_addr(),
18226            client_addr
18227        );
18228    }
18229
18230    #[rstest]
18231    fn resilience_against_migration_attack(
18232        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18233    ) {
18234        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18235        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18236        config
18237            .load_cert_chain_from_pem_file("examples/cert.crt")
18238            .unwrap();
18239        config
18240            .load_priv_key_from_pem_file("examples/cert.key")
18241            .unwrap();
18242        config
18243            .set_application_protos(&[b"proto1", b"proto2"])
18244            .unwrap();
18245        config.verify_peer(false);
18246        config.set_active_connection_id_limit(3);
18247        config.set_initial_max_data(100000);
18248        config.set_initial_max_stream_data_bidi_local(100000);
18249        config.set_initial_max_stream_data_bidi_remote(100000);
18250        config.set_initial_max_streams_bidi(2);
18251
18252        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
18253
18254        let client_addr = testing::Pipe::client_addr();
18255        let server_addr = testing::Pipe::server_addr();
18256        let spoofed_client_addr = "127.0.0.1:6666".parse().unwrap();
18257
18258        const DATA_BYTES: usize = 24000;
18259        let buf = [42; DATA_BYTES];
18260        let mut recv_buf = [0; DATA_BYTES];
18261        let send1_bytes = pipe.server.stream_send(1, &buf, true).unwrap();
18262        assert_eq!(send1_bytes, match cc_algorithm_name {
18263            #[cfg(feature = "openssl")]
18264            "bbr2" => 14041,
18265            #[cfg(not(feature = "openssl"))]
18266            "bbr2" => 13955,
18267            #[cfg(feature = "openssl")]
18268            "bbr2_gcongestion" => 13966,
18269            #[cfg(not(feature = "openssl"))]
18270            "bbr2_gcongestion" => 13880,
18271            _ => 12000,
18272        });
18273        assert_eq!(
18274            testing::process_flight(
18275                &mut pipe.client,
18276                testing::emit_flight(&mut pipe.server).unwrap()
18277            ),
18278            Ok(())
18279        );
18280        let (rcv_data_1, _) = pipe.client.stream_recv(1, &mut recv_buf).unwrap();
18281
18282        // Fake the source address of client.
18283        let mut faked_addr_flight =
18284            testing::emit_flight(&mut pipe.client).unwrap();
18285        faked_addr_flight
18286            .iter_mut()
18287            .for_each(|(_, si)| si.from = spoofed_client_addr);
18288        assert_eq!(
18289            testing::process_flight(&mut pipe.server, faked_addr_flight),
18290            Ok(())
18291        );
18292        assert_eq!(
18293            pipe.server.stream_send(1, &buf[send1_bytes..], true),
18294            Ok(24000 - send1_bytes)
18295        );
18296        assert_eq!(
18297            pipe.server.path_event_next(),
18298            Some(PathEvent::ReusedSourceConnectionId(
18299                0,
18300                (server_addr, client_addr),
18301                (server_addr, spoofed_client_addr)
18302            ))
18303        );
18304        assert_eq!(
18305            pipe.server.path_event_next(),
18306            Some(PathEvent::New(server_addr, spoofed_client_addr))
18307        );
18308
18309        assert_eq!(
18310            pipe.server.is_path_validated(server_addr, client_addr),
18311            Ok(true)
18312        );
18313        assert_eq!(
18314            pipe.server
18315                .is_path_validated(server_addr, spoofed_client_addr),
18316            Ok(false)
18317        );
18318
18319        // The client creates the PATH CHALLENGE, but it is always lost.
18320        testing::emit_flight(&mut pipe.server).unwrap();
18321
18322        // Wait until probing timer expires. Since the RTT is very low,
18323        // wait a bit more.
18324        let probed_pid = pipe
18325            .server
18326            .paths
18327            .path_id_from_addrs(&(server_addr, spoofed_client_addr))
18328            .unwrap();
18329        let probe_instant = pipe
18330            .server
18331            .paths
18332            .get(probed_pid)
18333            .unwrap()
18334            .recovery
18335            .loss_detection_timer()
18336            .unwrap();
18337        let timer = probe_instant.duration_since(time::Instant::now());
18338        std::thread::sleep(timer + time::Duration::from_millis(1));
18339
18340        pipe.server.on_timeout();
18341
18342        // Because of the small ACK size, the server cannot send more to the
18343        // client. Fallback on the previous active path.
18344        assert_eq!(
18345            pipe.server.path_event_next(),
18346            Some(PathEvent::FailedValidation(
18347                server_addr,
18348                spoofed_client_addr
18349            ))
18350        );
18351
18352        assert_eq!(
18353            pipe.server.is_path_validated(server_addr, client_addr),
18354            Ok(true)
18355        );
18356        assert_eq!(
18357            pipe.server
18358                .is_path_validated(server_addr, spoofed_client_addr),
18359            Ok(false)
18360        );
18361
18362        let server_active_path = pipe.server.paths.get_active().unwrap();
18363        assert_eq!(server_active_path.local_addr(), server_addr);
18364        assert_eq!(server_active_path.peer_addr(), client_addr);
18365        assert_eq!(pipe.advance(), Ok(()));
18366        let (rcv_data_2, fin) =
18367            pipe.client.stream_recv(1, &mut recv_buf).unwrap();
18368        assert!(fin);
18369        assert_eq!(rcv_data_1 + rcv_data_2, DATA_BYTES);
18370    }
18371
18372    #[rstest]
18373    fn consecutive_non_ack_eliciting(
18374        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18375    ) {
18376        let mut buf = [0; 65535];
18377
18378        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
18379        assert_eq!(pipe.handshake(), Ok(()));
18380
18381        // Client sends a bunch of PING frames, causing server to ACK (ACKs aren't
18382        // ack-eliciting)
18383        let frames = [frame::Frame::Ping { mtu_probe: None }];
18384        let pkt_type = packet::Type::Short;
18385        for _ in 0..24 {
18386            let len = pipe
18387                .send_pkt_to_server(pkt_type, &frames, &mut buf)
18388                .unwrap();
18389            assert!(len > 0);
18390
18391            let frames =
18392                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18393            assert!(
18394                frames
18395                    .iter()
18396                    .all(|frame| matches!(frame, frame::Frame::ACK { .. })),
18397                "ACK only"
18398            );
18399        }
18400
18401        // After 24 non-ack-eliciting, an ACK is explicitly elicited with a PING
18402        let len = pipe
18403            .send_pkt_to_server(pkt_type, &frames, &mut buf)
18404            .unwrap();
18405        assert!(len > 0);
18406
18407        let frames =
18408            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18409        assert!(
18410            frames
18411                .iter()
18412                .any(|frame| matches!(frame, frame::Frame::Ping {
18413                    mtu_probe: None
18414                })),
18415            "found a PING"
18416        );
18417    }
18418
18419    #[rstest]
18420    fn send_ack_eliciting_causes_ping(
18421        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18422    ) {
18423        // First establish a connection
18424        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
18425        assert_eq!(pipe.handshake(), Ok(()));
18426
18427        // Queue a PING frame
18428        pipe.server.send_ack_eliciting().unwrap();
18429
18430        // Make sure ping is sent
18431        let mut buf = [0; 1500];
18432        let (len, _) = pipe.server.send(&mut buf).unwrap();
18433
18434        let frames =
18435            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18436        let mut iter = frames.iter();
18437
18438        assert_eq!(iter.next(), Some(&frame::Frame::Ping { mtu_probe: None }));
18439    }
18440
18441    #[rstest]
18442    fn send_ack_eliciting_no_ping(
18443        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18444    ) {
18445        // First establish a connection
18446        let mut pipe = testing::Pipe::new(cc_algorithm_name).unwrap();
18447        assert_eq!(pipe.handshake(), Ok(()));
18448
18449        // Queue a PING frame
18450        pipe.server.send_ack_eliciting().unwrap();
18451
18452        // Send a stream frame, which is ACK-eliciting to make sure the ping is
18453        // not sent
18454        assert_eq!(pipe.server.stream_send(1, b"a", false), Ok(1));
18455
18456        // Make sure ping is not sent
18457        let mut buf = [0; 1500];
18458        let (len, _) = pipe.server.send(&mut buf).unwrap();
18459
18460        let frames =
18461            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18462        let mut iter = frames.iter();
18463
18464        assert!(matches!(
18465            iter.next(),
18466            Some(&frame::Frame::Stream {
18467                stream_id: 1,
18468                data: _
18469            })
18470        ));
18471        assert!(iter.next().is_none());
18472    }
18473
18474    /// Tests that streams do not keep being "writable" after being collected
18475    /// on reset.
18476    #[rstest]
18477    fn stop_sending_stream_send_after_reset_stream_ack(
18478        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18479    ) {
18480        let mut b = [0; 15];
18481
18482        let mut buf = [0; 65535];
18483
18484        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18485        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18486        config
18487            .load_cert_chain_from_pem_file("examples/cert.crt")
18488            .unwrap();
18489        config
18490            .load_priv_key_from_pem_file("examples/cert.key")
18491            .unwrap();
18492        config
18493            .set_application_protos(&[b"proto1", b"proto2"])
18494            .unwrap();
18495        config.set_initial_max_data(999999999);
18496        config.set_initial_max_stream_data_bidi_local(30);
18497        config.set_initial_max_stream_data_bidi_remote(30);
18498        config.set_initial_max_stream_data_uni(30);
18499        config.set_initial_max_streams_bidi(1000);
18500        config.set_initial_max_streams_uni(0);
18501        config.verify_peer(false);
18502
18503        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
18504        assert_eq!(pipe.handshake(), Ok(()));
18505
18506        assert_eq!(pipe.server.streams.len(), 0);
18507        assert_eq!(pipe.server.readable().len(), 0);
18508        assert_eq!(pipe.server.writable().len(), 0);
18509
18510        // Client opens a load of streams
18511        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
18512        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
18513        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
18514        assert_eq!(pipe.client.stream_send(12, b"hello", true), Ok(5));
18515        assert_eq!(pipe.client.stream_send(16, b"hello", true), Ok(5));
18516        assert_eq!(pipe.client.stream_send(20, b"hello", true), Ok(5));
18517        assert_eq!(pipe.client.stream_send(24, b"hello", true), Ok(5));
18518        assert_eq!(pipe.client.stream_send(28, b"hello", true), Ok(5));
18519        assert_eq!(pipe.client.stream_send(32, b"hello", true), Ok(5));
18520        assert_eq!(pipe.client.stream_send(36, b"hello", true), Ok(5));
18521        assert_eq!(pipe.advance(), Ok(()));
18522
18523        // Server iterators are populated
18524        let mut r = pipe.server.readable();
18525        assert_eq!(r.len(), 10);
18526        assert_eq!(r.next(), Some(0));
18527        assert_eq!(r.next(), Some(4));
18528        assert_eq!(r.next(), Some(8));
18529        assert_eq!(r.next(), Some(12));
18530        assert_eq!(r.next(), Some(16));
18531        assert_eq!(r.next(), Some(20));
18532        assert_eq!(r.next(), Some(24));
18533        assert_eq!(r.next(), Some(28));
18534        assert_eq!(r.next(), Some(32));
18535        assert_eq!(r.next(), Some(36));
18536
18537        assert_eq!(r.next(), None);
18538
18539        let mut w = pipe.server.writable();
18540        assert_eq!(w.len(), 10);
18541        assert_eq!(w.next(), Some(0));
18542        assert_eq!(w.next(), Some(4));
18543        assert_eq!(w.next(), Some(8));
18544        assert_eq!(w.next(), Some(12));
18545        assert_eq!(w.next(), Some(16));
18546        assert_eq!(w.next(), Some(20));
18547        assert_eq!(w.next(), Some(24));
18548        assert_eq!(w.next(), Some(28));
18549        assert_eq!(w.next(), Some(32));
18550        assert_eq!(w.next(), Some(36));
18551        assert_eq!(w.next(), None);
18552
18553        // Read one stream
18554        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
18555        assert!(pipe.server.stream_finished(0));
18556
18557        assert_eq!(pipe.server.readable().len(), 9);
18558        assert_eq!(pipe.server.writable().len(), 10);
18559
18560        assert_eq!(pipe.server.stream_writable(0, 0), Ok(true));
18561
18562        // Server sends data on stream 0, until blocked.
18563        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {
18564            assert_eq!(pipe.advance(), Ok(()));
18565        }
18566
18567        assert_eq!(pipe.server.writable().len(), 9);
18568        assert_eq!(pipe.server.stream_writable(0, 0), Ok(true));
18569
18570        // Client sends STOP_SENDING.
18571        let frames = [frame::Frame::StopSending {
18572            stream_id: 0,
18573            error_code: 42,
18574        }];
18575
18576        let pkt_type = packet::Type::Short;
18577        let len = pipe
18578            .send_pkt_to_server(pkt_type, &frames, &mut buf)
18579            .unwrap();
18580
18581        // Server sent a RESET_STREAM frame in response.
18582        let frames =
18583            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18584
18585        let mut iter = frames.iter();
18586
18587        // Skip ACK frame.
18588        iter.next();
18589
18590        assert_eq!(
18591            iter.next(),
18592            Some(&frame::Frame::ResetStream {
18593                stream_id: 0,
18594                error_code: 42,
18595                final_size: 30,
18596            })
18597        );
18598
18599        // Stream 0 is now writable in order to make apps aware of STOP_SENDING
18600        // via returning an error.
18601        let mut w = pipe.server.writable();
18602        assert_eq!(w.len(), 10);
18603
18604        assert!(w.any(|s| s == 0));
18605        assert_eq!(
18606            pipe.server.stream_writable(0, 1),
18607            Err(Error::StreamStopped(42))
18608        );
18609
18610        assert_eq!(pipe.server.writable().len(), 10);
18611        assert_eq!(pipe.server.streams.len(), 10);
18612
18613        // Client acks RESET_STREAM frame.
18614        let mut ranges = ranges::RangeSet::default();
18615        ranges.insert(0..12);
18616
18617        let frames = [frame::Frame::ACK {
18618            ack_delay: 15,
18619            ranges,
18620            ecn_counts: None,
18621        }];
18622
18623        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
18624
18625        // Stream is collected on the server after RESET_STREAM is acked.
18626        assert_eq!(pipe.server.streams.len(), 9);
18627
18628        // Sending STOP_SENDING again shouldn't trigger RESET_STREAM again.
18629        let frames = [frame::Frame::StopSending {
18630            stream_id: 0,
18631            error_code: 42,
18632        }];
18633
18634        let len = pipe
18635            .send_pkt_to_server(pkt_type, &frames, &mut buf)
18636            .unwrap();
18637
18638        let frames =
18639            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
18640
18641        assert_eq!(frames.len(), 1);
18642
18643        match frames.first() {
18644            Some(frame::Frame::ACK { .. }) => (),
18645
18646            f => panic!("expected ACK frame, got {:?}", f),
18647        };
18648
18649        assert_eq!(pipe.server.streams.len(), 9);
18650
18651        // Stream 0 has been collected and must not be writable anymore.
18652        let mut w = pipe.server.writable();
18653        assert_eq!(w.len(), 9);
18654        assert!(!w.any(|s| s == 0));
18655
18656        // If we called send before the client ACK of reset stream, it would
18657        // have failed with StreamStopped.
18658        assert_eq!(pipe.server.stream_send(0, b"world", true), Err(Error::Done),);
18659
18660        // Stream 0 is still not writable.
18661        let mut w = pipe.server.writable();
18662        assert_eq!(w.len(), 9);
18663        assert!(!w.any(|s| s == 0));
18664    }
18665
18666    #[rstest]
18667    fn challenge_no_cids(
18668        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18669    ) {
18670        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18671        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18672        config
18673            .load_cert_chain_from_pem_file("examples/cert.crt")
18674            .unwrap();
18675        config
18676            .load_priv_key_from_pem_file("examples/cert.key")
18677            .unwrap();
18678        config
18679            .set_application_protos(&[b"proto1", b"proto2"])
18680            .unwrap();
18681        config.verify_peer(false);
18682        config.set_active_connection_id_limit(4);
18683        config.set_initial_max_data(30);
18684        config.set_initial_max_stream_data_bidi_local(15);
18685        config.set_initial_max_stream_data_bidi_remote(15);
18686        config.set_initial_max_stream_data_uni(10);
18687        config.set_initial_max_streams_bidi(3);
18688
18689        let mut pipe =
18690            testing::Pipe::with_config_and_scid_lengths(&mut config, 16, 16)
18691                .unwrap();
18692        assert_eq!(pipe.handshake(), Ok(()));
18693
18694        // Server send CIDs to client
18695        let mut server_cids = Vec::new();
18696        for _ in 0..2 {
18697            let (cid, reset_token) = testing::create_cid_and_reset_token(16);
18698            pipe.server
18699                .new_scid(&cid, reset_token, true)
18700                .expect("server issue cid");
18701            server_cids.push(cid);
18702        }
18703        assert_eq!(pipe.advance(), Ok(()));
18704
18705        let server_addr = testing::Pipe::server_addr();
18706        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
18707
18708        // Client probes path before sending CIDs (simulating race condition)
18709        let frames = [frame::Frame::PathChallenge {
18710            data: [0, 1, 2, 3, 4, 5, 6, 7],
18711        }];
18712        let mut pkt_buf = [0u8; 1500];
18713        let mut b = octets::OctetsMut::with_slice(&mut pkt_buf);
18714        let epoch = packet::Type::Short.to_epoch().unwrap();
18715        let crypto_ctx = &mut pipe.client.crypto_ctx[epoch];
18716        let pn = pipe.client.next_pkt_num;
18717        let pn_len = 4;
18718
18719        let hdr = Header {
18720            ty: packet::Type::Short,
18721            version: pipe.client.version,
18722            dcid: server_cids[0].clone(),
18723            scid: ConnectionId::from_ref(&[5, 4, 3, 2, 1]),
18724            pkt_num: 0,
18725            pkt_num_len: pn_len,
18726            token: pipe.client.token.clone(),
18727            versions: None,
18728            key_phase: pipe.client.key_phase,
18729        };
18730        hdr.to_bytes(&mut b).expect("encode header");
18731        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
18732        b.put_u32(pn as u32).expect("put pn");
18733
18734        let payload_offset = b.off();
18735
18736        for frame in frames {
18737            frame.to_bytes(&mut b).expect("encode frames");
18738        }
18739
18740        let aead = crypto_ctx.crypto_seal.as_ref().expect("crypto seal");
18741
18742        let written = packet::encrypt_pkt(
18743            &mut b,
18744            pn,
18745            pn_len,
18746            payload_len,
18747            payload_offset,
18748            None,
18749            aead,
18750        )
18751        .expect("packet encrypt");
18752        pipe.client.next_pkt_num += 1;
18753
18754        pipe.server
18755            .recv(&mut pkt_buf[..written], RecvInfo {
18756                to: server_addr,
18757                from: client_addr_2,
18758            })
18759            .expect("server receive path challenge");
18760
18761        // Show that the new path is not considered a destination path by quiche
18762        assert!(!pipe
18763            .server
18764            .paths_iter(server_addr)
18765            .any(|path| path == client_addr_2));
18766    }
18767
18768    #[rstest]
18769    fn successful_probe_pmtud(
18770        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18771    ) {
18772        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18773        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18774        config
18775            .load_cert_chain_from_pem_file("examples/cert.crt")
18776            .unwrap();
18777        config
18778            .load_priv_key_from_pem_file("examples/cert.key")
18779            .unwrap();
18780        config
18781            .set_application_protos(&[b"proto1", b"proto2"])
18782            .unwrap();
18783        config.verify_peer(false);
18784        config.set_initial_max_data(100000);
18785        config.set_initial_max_stream_data_bidi_local(100000);
18786        config.set_initial_max_stream_data_bidi_remote(100000);
18787        config.set_initial_max_streams_bidi(2);
18788        config.set_active_connection_id_limit(4);
18789        config.set_max_send_udp_payload_size(1350);
18790        config.set_max_recv_udp_payload_size(1350);
18791        config.discover_pmtu(true);
18792
18793        // Perform initial handshake.
18794        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
18795        assert_eq!(pipe.handshake(), Ok(()));
18796
18797        let server_addr = testing::Pipe::server_addr();
18798        let client_addr = testing::Pipe::client_addr();
18799        let pid_1 = pipe
18800            .server
18801            .paths
18802            .path_id_from_addrs(&(server_addr, client_addr))
18803            .expect("no such path");
18804
18805        // Check that PMTU params are configured correctly
18806        let pmtu_param = &mut pipe.server.paths.get_mut(pid_1).unwrap().pmtud;
18807        assert!(pmtu_param.get_probe_status());
18808        assert_eq!(pmtu_param.get_probe_size(), 1350);
18809        assert_eq!(pipe.advance(), Ok(()));
18810
18811        for (_, p) in pipe.server.paths.iter_mut() {
18812            assert_eq!(p.pmtud.get_current(), 1350);
18813            assert!(!p.pmtud.get_probe_status());
18814        }
18815    }
18816
18817    #[rstest]
18818    fn pmtud_probe_loss(
18819        #[values("cubic", "bbr2", "bbr2_gcongestion")] cc_algorithm_name: &str,
18820    ) {
18821        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
18822        assert_eq!(config.set_cc_algorithm_name(cc_algorithm_name), Ok(()));
18823        config
18824            .load_cert_chain_from_pem_file("examples/cert.crt")
18825            .unwrap();
18826        config
18827            .load_priv_key_from_pem_file("examples/cert.key")
18828            .unwrap();
18829        config
18830            .set_application_protos(&[b"proto1", b"proto2"])
18831            .unwrap();
18832        config.verify_peer(false);
18833        config.set_initial_max_data(100000);
18834        config.set_initial_max_stream_data_bidi_local(100000);
18835        config.set_initial_max_stream_data_bidi_remote(100000);
18836        config.set_initial_max_streams_bidi(2);
18837        config.set_active_connection_id_limit(4);
18838        config.set_max_send_udp_payload_size(1350);
18839        config.set_max_recv_udp_payload_size(1250);
18840        config.discover_pmtu(true);
18841
18842        // Perform initial handshake.
18843        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
18844        assert_eq!(pipe.handshake(), Ok(()));
18845
18846        let server_addr = testing::Pipe::server_addr();
18847        let client_addr = testing::Pipe::client_addr();
18848        let pid_1 = pipe
18849            .server
18850            .paths
18851            .path_id_from_addrs(&(server_addr, client_addr))
18852            .expect("no such path");
18853
18854        // Check that PMTU params are configured correctly
18855        let pmtu_param = &mut pipe.server.paths.get_mut(pid_1).unwrap().pmtud;
18856        assert!(pmtu_param.get_probe_status());
18857        assert_eq!(pmtu_param.get_probe_size(), 1350);
18858        std::thread::sleep(
18859            pipe.server.paths.get_mut(pid_1).unwrap().recovery.rtt() +
18860                time::Duration::from_millis(1),
18861        );
18862
18863        let active_server_path = pipe.server.paths.get_active_mut().unwrap();
18864        let pmtu_param = &mut active_server_path.pmtud;
18865
18866        // PMTU not updated since probe is not ACKed
18867        assert_eq!(pmtu_param.get_current(), 1200);
18868
18869        // Continue searching for PMTU
18870        assert!(pmtu_param.get_probe_status());
18871    }
18872}
18873
18874pub use crate::packet::ConnectionId;
18875pub use crate::packet::Header;
18876pub use crate::packet::Type;
18877
18878pub use crate::path::PathEvent;
18879pub use crate::path::PathStats;
18880pub use crate::path::SocketAddrIter;
18881
18882pub use crate::recovery::BbrBwLoReductionStrategy;
18883pub use crate::recovery::BbrParams;
18884pub use crate::recovery::CongestionControlAlgorithm;
18885use crate::recovery::RecoveryOps;
18886
18887pub use crate::stream::StreamIter;
18888
18889pub use crate::range_buf::BufFactory;
18890pub use crate::range_buf::BufSplit;
18891
18892mod cid;
18893mod crypto;
18894mod dgram;
18895#[cfg(feature = "ffi")]
18896mod ffi;
18897mod flowcontrol;
18898mod frame;
18899pub mod h3;
18900mod minmax;
18901mod packet;
18902mod path;
18903mod pmtud;
18904mod rand;
18905mod range_buf;
18906mod ranges;
18907mod recovery;
18908mod stream;
18909mod tls;