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;
420
421use smallvec::SmallVec;
422
423/// The current QUIC wire version.
424pub const PROTOCOL_VERSION: u32 = PROTOCOL_VERSION_V1;
425
426/// Supported QUIC versions.
427const PROTOCOL_VERSION_V1: u32 = 0x0000_0001;
428
429/// The maximum length of a connection ID.
430pub const MAX_CONN_ID_LEN: usize = crate::packet::MAX_CID_LEN as usize;
431
432/// The minimum length of Initial packets sent by a client.
433pub const MIN_CLIENT_INITIAL_LEN: usize = 1200;
434
435#[cfg(not(feature = "fuzzing"))]
436const PAYLOAD_MIN_LEN: usize = 4;
437
438#[cfg(feature = "fuzzing")]
439// Due to the fact that in fuzzing mode we use a zero-length AEAD tag (which
440// would normally be 16 bytes), we need to adjust the minimum payload size to
441// account for that.
442const PAYLOAD_MIN_LEN: usize = 20;
443
444// PATH_CHALLENGE (9 bytes) + AEAD tag (16 bytes).
445const MIN_PROBING_SIZE: usize = 25;
446
447const MAX_AMPLIFICATION_FACTOR: usize = 3;
448
449// The maximum number of tracked packet number ranges that need to be acked.
450//
451// This represents more or less how many ack blocks can fit in a typical packet.
452const MAX_ACK_RANGES: usize = 68;
453
454// The highest possible stream ID allowed.
455const MAX_STREAM_ID: u64 = 1 << 60;
456
457// The default max_datagram_size used in congestion control.
458const MAX_SEND_UDP_PAYLOAD_SIZE: usize = 1200;
459
460// The default length of DATAGRAM queues.
461const DEFAULT_MAX_DGRAM_QUEUE_LEN: usize = 0;
462
463// The default length of PATH_CHALLENGE receive queue.
464const DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN: usize = 3;
465
466// The DATAGRAM standard recommends either none or 65536 as maximum DATAGRAM
467// frames size. We enforce the recommendation for forward compatibility.
468const MAX_DGRAM_FRAME_SIZE: u64 = 65536;
469
470// The length of the payload length field.
471const PAYLOAD_LENGTH_LEN: usize = 2;
472
473// The number of undecryptable that can be buffered.
474const MAX_UNDECRYPTABLE_PACKETS: usize = 10;
475
476const RESERVED_VERSION_MASK: u32 = 0xfafafafa;
477
478// The default size of the receiver connection flow control window.
479const DEFAULT_CONNECTION_WINDOW: u64 = 48 * 1024;
480
481// The maximum size of the receiver connection flow control window.
482const MAX_CONNECTION_WINDOW: u64 = 24 * 1024 * 1024;
483
484// How much larger the connection flow control window need to be larger than
485// the stream flow control window.
486const CONNECTION_WINDOW_FACTOR: f64 = 1.5;
487
488// How many probing packet timeouts do we tolerate before considering the path
489// validation as failed.
490const MAX_PROBING_TIMEOUTS: usize = 3;
491
492// The default initial congestion window size in terms of packet count.
493const DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS: usize = 10;
494
495// The maximum data offset that can be stored in a crypto stream.
496const MAX_CRYPTO_STREAM_OFFSET: u64 = 1 << 16;
497
498/// A specialized [`Result`] type for quiche operations.
499///
500/// This type is used throughout quiche's public API for any operation that
501/// can produce an error.
502///
503/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
504pub type Result<T> = std::result::Result<T, Error>;
505
506/// A QUIC error.
507#[derive(Clone, Copy, Debug, PartialEq, Eq)]
508pub enum Error {
509    /// There is no more work to do.
510    Done,
511
512    /// The provided buffer is too short.
513    BufferTooShort,
514
515    /// The provided packet cannot be parsed because its version is unknown.
516    UnknownVersion,
517
518    /// The provided packet cannot be parsed because it contains an invalid
519    /// frame.
520    InvalidFrame,
521
522    /// The provided packet cannot be parsed.
523    InvalidPacket,
524
525    /// The operation cannot be completed because the connection is in an
526    /// invalid state.
527    InvalidState,
528
529    /// The operation cannot be completed because the stream is in an
530    /// invalid state.
531    ///
532    /// The stream ID is provided as associated data.
533    InvalidStreamState(u64),
534
535    /// The peer's transport params cannot be parsed.
536    InvalidTransportParam,
537
538    /// A cryptographic operation failed.
539    CryptoFail,
540
541    /// The TLS handshake failed.
542    TlsFail,
543
544    /// The peer violated the local flow control limits.
545    FlowControl,
546
547    /// The peer violated the local stream limits.
548    StreamLimit,
549
550    /// The specified stream was stopped by the peer.
551    ///
552    /// The error code sent as part of the `STOP_SENDING` frame is provided as
553    /// associated data.
554    StreamStopped(u64),
555
556    /// The specified stream was reset by the peer.
557    ///
558    /// The error code sent as part of the `RESET_STREAM` frame is provided as
559    /// associated data.
560    StreamReset(u64),
561
562    /// The received data exceeds the stream's final size.
563    FinalSize,
564
565    /// Error in congestion control.
566    CongestionControl,
567
568    /// Too many identifiers were provided.
569    IdLimit,
570
571    /// Not enough available identifiers.
572    OutOfIdentifiers,
573
574    /// Error in key update.
575    KeyUpdate,
576
577    /// The peer sent more data in CRYPTO frames than we can buffer.
578    CryptoBufferExceeded,
579}
580
581/// QUIC error codes sent on the wire.
582///
583/// As defined in [RFC9000](https://www.rfc-editor.org/rfc/rfc9000.html#name-error-codes).
584#[derive(Copy, Clone, Debug, Eq, PartialEq)]
585pub enum WireErrorCode {
586    /// An endpoint uses this with CONNECTION_CLOSE to signal that the
587    /// connection is being closed abruptly in the absence of any error.
588    NoError              = 0x0,
589    /// The endpoint encountered an internal error and cannot continue with the
590    /// connection.
591    InternalError        = 0x1,
592    /// The server refused to accept a new connection.
593    ConnectionRefused    = 0x2,
594    /// An endpoint received more data than it permitted in its advertised data
595    /// limits; see Section 4.
596    FlowControlError     = 0x3,
597    /// An endpoint received a frame for a stream identifier that exceeded its
598    /// advertised stream limit for the corresponding stream type.
599    StreamLimitError     = 0x4,
600    /// An endpoint received a frame for a stream that was not in a state that
601    /// permitted that frame.
602    StreamStateError     = 0x5,
603    /// (1) An endpoint received a STREAM frame containing data that exceeded
604    /// the previously established final size, (2) an endpoint received a
605    /// STREAM frame or a RESET_STREAM frame containing a final size that
606    /// was lower than the size of stream data that was already received, or
607    /// (3) an endpoint received a STREAM frame or a RESET_STREAM frame
608    /// containing a different final size to the one already established.
609    FinalSizeError       = 0x6,
610    /// An endpoint received a frame that was badly formatted -- for instance, a
611    /// frame of an unknown type or an ACK frame that has more
612    /// acknowledgment ranges than the remainder of the packet could carry.
613    FrameEncodingError   = 0x7,
614    /// An endpoint received transport parameters that were badly formatted,
615    /// included an invalid value, omitted a mandatory transport parameter,
616    /// included a forbidden transport parameter, or were otherwise in
617    /// error.
618    TransportParameterError = 0x8,
619    /// An endpoint received transport parameters that were badly formatted,
620    /// included an invalid value, omitted a mandatory transport parameter,
621    /// included a forbidden transport parameter, or were otherwise in
622    /// error.
623    ConnectionIdLimitError = 0x9,
624    /// An endpoint detected an error with protocol compliance that was not
625    /// covered by more specific error codes.
626    ProtocolViolation    = 0xa,
627    /// A server received a client Initial that contained an invalid Token
628    /// field.
629    InvalidToken         = 0xb,
630    /// The application or application protocol caused the connection to be
631    /// closed.
632    ApplicationError     = 0xc,
633    /// An endpoint has received more data in CRYPTO frames than it can buffer.
634    CryptoBufferExceeded = 0xd,
635    /// An endpoint detected errors in performing key updates.
636    KeyUpdateError       = 0xe,
637    /// An endpoint has reached the confidentiality or integrity limit for the
638    /// AEAD algorithm used by the given connection.
639    AeadLimitReached     = 0xf,
640    /// An endpoint has determined that the network path is incapable of
641    /// supporting QUIC. An endpoint is unlikely to receive a
642    /// CONNECTION_CLOSE frame carrying this code except when the path does
643    /// not support a large enough MTU.
644    NoViablePath         = 0x10,
645}
646
647impl Error {
648    fn to_wire(self) -> u64 {
649        match self {
650            Error::Done => WireErrorCode::NoError as u64,
651            Error::InvalidFrame => WireErrorCode::FrameEncodingError as u64,
652            Error::InvalidStreamState(..) =>
653                WireErrorCode::StreamStateError as u64,
654            Error::InvalidTransportParam =>
655                WireErrorCode::TransportParameterError as u64,
656            Error::FlowControl => WireErrorCode::FlowControlError as u64,
657            Error::StreamLimit => WireErrorCode::StreamLimitError as u64,
658            Error::IdLimit => WireErrorCode::ConnectionIdLimitError as u64,
659            Error::FinalSize => WireErrorCode::FinalSizeError as u64,
660            Error::CryptoBufferExceeded =>
661                WireErrorCode::CryptoBufferExceeded as u64,
662            Error::KeyUpdate => WireErrorCode::KeyUpdateError as u64,
663            _ => WireErrorCode::ProtocolViolation as u64,
664        }
665    }
666
667    #[cfg(feature = "ffi")]
668    fn to_c(self) -> libc::ssize_t {
669        match self {
670            Error::Done => -1,
671            Error::BufferTooShort => -2,
672            Error::UnknownVersion => -3,
673            Error::InvalidFrame => -4,
674            Error::InvalidPacket => -5,
675            Error::InvalidState => -6,
676            Error::InvalidStreamState(_) => -7,
677            Error::InvalidTransportParam => -8,
678            Error::CryptoFail => -9,
679            Error::TlsFail => -10,
680            Error::FlowControl => -11,
681            Error::StreamLimit => -12,
682            Error::FinalSize => -13,
683            Error::CongestionControl => -14,
684            Error::StreamStopped { .. } => -15,
685            Error::StreamReset { .. } => -16,
686            Error::IdLimit => -17,
687            Error::OutOfIdentifiers => -18,
688            Error::KeyUpdate => -19,
689            Error::CryptoBufferExceeded => -20,
690        }
691    }
692}
693
694impl std::fmt::Display for Error {
695    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
696        write!(f, "{self:?}")
697    }
698}
699
700impl std::error::Error for Error {
701    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
702        None
703    }
704}
705
706impl std::convert::From<octets::BufferTooShortError> for Error {
707    fn from(_err: octets::BufferTooShortError) -> Self {
708        Error::BufferTooShort
709    }
710}
711
712/// Ancillary information about incoming packets.
713#[derive(Clone, Copy, Debug, PartialEq, Eq)]
714pub struct RecvInfo {
715    /// The remote address the packet was received from.
716    pub from: SocketAddr,
717
718    /// The local address the packet was received on.
719    pub to: SocketAddr,
720}
721
722/// Ancillary information about outgoing packets.
723#[derive(Clone, Copy, Debug, PartialEq, Eq)]
724pub struct SendInfo {
725    /// The local address the packet should be sent from.
726    pub from: SocketAddr,
727
728    /// The remote address the packet should be sent to.
729    pub to: SocketAddr,
730
731    /// The time to send the packet out.
732    ///
733    /// See [Pacing] for more details.
734    ///
735    /// [Pacing]: index.html#pacing
736    pub at: time::Instant,
737}
738
739/// Represents information carried by `CONNECTION_CLOSE` frames.
740#[derive(Clone, Debug, PartialEq, Eq)]
741pub struct ConnectionError {
742    /// Whether the error came from the application or the transport layer.
743    pub is_app: bool,
744
745    /// The error code carried by the `CONNECTION_CLOSE` frame.
746    pub error_code: u64,
747
748    /// The reason carried by the `CONNECTION_CLOSE` frame.
749    pub reason: Vec<u8>,
750}
751
752/// The side of the stream to be shut down.
753///
754/// This should be used when calling [`stream_shutdown()`].
755///
756/// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
757#[repr(C)]
758#[derive(PartialEq, Eq)]
759pub enum Shutdown {
760    /// Stop receiving stream data.
761    Read  = 0,
762
763    /// Stop sending stream data.
764    Write = 1,
765}
766
767/// Qlog logging level.
768#[repr(C)]
769#[cfg(feature = "qlog")]
770#[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
771pub enum QlogLevel {
772    /// Logs any events of Core importance.
773    Core  = 0,
774
775    /// Logs any events of Core and Base importance.
776    Base  = 1,
777
778    /// Logs any events of Core, Base and Extra importance
779    Extra = 2,
780}
781
782/// Stores configuration shared between multiple connections.
783pub struct Config {
784    local_transport_params: TransportParams,
785
786    version: u32,
787
788    tls_ctx: tls::Context,
789
790    application_protos: Vec<Vec<u8>>,
791
792    grease: bool,
793
794    cc_algorithm: CongestionControlAlgorithm,
795    initial_congestion_window_packets: usize,
796
797    pmtud: bool,
798
799    hystart: bool,
800
801    pacing: bool,
802    max_pacing_rate: Option<u64>,
803
804    dgram_recv_max_queue_len: usize,
805    dgram_send_max_queue_len: usize,
806
807    path_challenge_recv_max_queue_len: usize,
808
809    max_send_udp_payload_size: usize,
810
811    max_connection_window: u64,
812    max_stream_window: u64,
813
814    max_amplification_factor: usize,
815
816    disable_dcid_reuse: bool,
817
818    track_unknown_transport_params: Option<usize>,
819}
820
821// See https://quicwg.org/base-drafts/rfc9000.html#section-15
822fn is_reserved_version(version: u32) -> bool {
823    version & RESERVED_VERSION_MASK == version
824}
825
826impl Config {
827    /// Creates a config object with the given version.
828    ///
829    /// ## Examples:
830    ///
831    /// ```
832    /// let config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
833    /// # Ok::<(), quiche::Error>(())
834    /// ```
835    pub fn new(version: u32) -> Result<Config> {
836        Self::with_tls_ctx(version, tls::Context::new()?)
837    }
838
839    /// Creates a config object with the given version and
840    /// [`SslContextBuilder`].
841    ///
842    /// This is useful for applications that wish to manually configure
843    /// [`SslContextBuilder`].
844    ///
845    /// [`SslContextBuilder`]: https://docs.rs/boring/latest/boring/ssl/struct.SslContextBuilder.html
846    #[cfg(feature = "boringssl-boring-crate")]
847    #[cfg_attr(docsrs, doc(cfg(feature = "boringssl-boring-crate")))]
848    pub fn with_boring_ssl_ctx_builder(
849        version: u32, tls_ctx_builder: boring::ssl::SslContextBuilder,
850    ) -> Result<Config> {
851        Self::with_tls_ctx(version, tls::Context::from_boring(tls_ctx_builder))
852    }
853
854    fn with_tls_ctx(version: u32, tls_ctx: tls::Context) -> Result<Config> {
855        if !is_reserved_version(version) && !version_is_supported(version) {
856            return Err(Error::UnknownVersion);
857        }
858
859        Ok(Config {
860            local_transport_params: TransportParams::default(),
861            version,
862            tls_ctx,
863            application_protos: Vec::new(),
864            grease: true,
865            cc_algorithm: CongestionControlAlgorithm::CUBIC,
866            initial_congestion_window_packets:
867                DEFAULT_INITIAL_CONGESTION_WINDOW_PACKETS,
868            pmtud: false,
869            hystart: true,
870            pacing: true,
871            max_pacing_rate: None,
872
873            dgram_recv_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
874            dgram_send_max_queue_len: DEFAULT_MAX_DGRAM_QUEUE_LEN,
875
876            path_challenge_recv_max_queue_len:
877                DEFAULT_MAX_PATH_CHALLENGE_RX_QUEUE_LEN,
878
879            max_send_udp_payload_size: MAX_SEND_UDP_PAYLOAD_SIZE,
880
881            max_connection_window: MAX_CONNECTION_WINDOW,
882            max_stream_window: stream::MAX_STREAM_WINDOW,
883
884            max_amplification_factor: MAX_AMPLIFICATION_FACTOR,
885
886            disable_dcid_reuse: false,
887
888            track_unknown_transport_params: None,
889        })
890    }
891
892    /// Configures the given certificate chain.
893    ///
894    /// The content of `file` is parsed as a PEM-encoded leaf certificate,
895    /// followed by optional intermediate certificates.
896    ///
897    /// ## Examples:
898    ///
899    /// ```no_run
900    /// # let mut config = quiche::Config::new(0xbabababa)?;
901    /// config.load_cert_chain_from_pem_file("/path/to/cert.pem")?;
902    /// # Ok::<(), quiche::Error>(())
903    /// ```
904    pub fn load_cert_chain_from_pem_file(&mut self, file: &str) -> Result<()> {
905        self.tls_ctx.use_certificate_chain_file(file)
906    }
907
908    /// Configures the given private key.
909    ///
910    /// The content of `file` is parsed as a PEM-encoded private key.
911    ///
912    /// ## Examples:
913    ///
914    /// ```no_run
915    /// # let mut config = quiche::Config::new(0xbabababa)?;
916    /// config.load_priv_key_from_pem_file("/path/to/key.pem")?;
917    /// # Ok::<(), quiche::Error>(())
918    /// ```
919    pub fn load_priv_key_from_pem_file(&mut self, file: &str) -> Result<()> {
920        self.tls_ctx.use_privkey_file(file)
921    }
922
923    /// Specifies a file where trusted CA certificates are stored for the
924    /// purposes of certificate verification.
925    ///
926    /// The content of `file` is parsed as a PEM-encoded certificate chain.
927    ///
928    /// ## Examples:
929    ///
930    /// ```no_run
931    /// # let mut config = quiche::Config::new(0xbabababa)?;
932    /// config.load_verify_locations_from_file("/path/to/cert.pem")?;
933    /// # Ok::<(), quiche::Error>(())
934    /// ```
935    pub fn load_verify_locations_from_file(&mut self, file: &str) -> Result<()> {
936        self.tls_ctx.load_verify_locations_from_file(file)
937    }
938
939    /// Specifies a directory where trusted CA certificates are stored for the
940    /// purposes of certificate verification.
941    ///
942    /// The content of `dir` a set of PEM-encoded certificate chains.
943    ///
944    /// ## Examples:
945    ///
946    /// ```no_run
947    /// # let mut config = quiche::Config::new(0xbabababa)?;
948    /// config.load_verify_locations_from_directory("/path/to/certs")?;
949    /// # Ok::<(), quiche::Error>(())
950    /// ```
951    pub fn load_verify_locations_from_directory(
952        &mut self, dir: &str,
953    ) -> Result<()> {
954        self.tls_ctx.load_verify_locations_from_directory(dir)
955    }
956
957    /// Configures whether to verify the peer's certificate.
958    ///
959    /// The default value is `true` for client connections, and `false` for
960    /// server ones.
961    ///
962    /// Note that on the server-side, enabling verification of the peer will
963    /// trigger a certificate request and make authentication errors fatal, but
964    /// will still allow anonymous clients (i.e. clients that don't present a
965    /// certificate at all). Servers can check whether a client presented a
966    /// certificate by calling [`peer_cert()`] if they need to.
967    ///
968    /// [`peer_cert()`]: struct.Connection.html#method.peer_cert
969    pub fn verify_peer(&mut self, verify: bool) {
970        self.tls_ctx.set_verify(verify);
971    }
972
973    /// Configures whether to do path MTU discovery.
974    ///
975    /// The default value is `false`.
976    pub fn discover_pmtu(&mut self, discover: bool) {
977        self.pmtud = discover;
978    }
979
980    /// Configures whether to send GREASE values.
981    ///
982    /// The default value is `true`.
983    pub fn grease(&mut self, grease: bool) {
984        self.grease = grease;
985    }
986
987    /// Enables logging of secrets.
988    ///
989    /// When logging is enabled, the [`set_keylog()`] method must be called on
990    /// the connection for its cryptographic secrets to be logged in the
991    /// [keylog] format to the specified writer.
992    ///
993    /// [`set_keylog()`]: struct.Connection.html#method.set_keylog
994    /// [keylog]: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
995    pub fn log_keys(&mut self) {
996        self.tls_ctx.enable_keylog();
997    }
998
999    /// Configures the session ticket key material.
1000    ///
1001    /// On the server this key will be used to encrypt and decrypt session
1002    /// tickets, used to perform session resumption without server-side state.
1003    ///
1004    /// By default a key is generated internally, and rotated regularly, so
1005    /// applications don't need to call this unless they need to use a
1006    /// specific key (e.g. in order to support resumption across multiple
1007    /// servers), in which case the application is also responsible for
1008    /// rotating the key to provide forward secrecy.
1009    pub fn set_ticket_key(&mut self, key: &[u8]) -> Result<()> {
1010        self.tls_ctx.set_ticket_key(key)
1011    }
1012
1013    /// Enables sending or receiving early data.
1014    pub fn enable_early_data(&mut self) {
1015        self.tls_ctx.set_early_data_enabled(true);
1016    }
1017
1018    /// Configures the list of supported application protocols.
1019    ///
1020    /// On the client this configures the list of protocols to send to the
1021    /// server as part of the ALPN extension.
1022    ///
1023    /// On the server this configures the list of supported protocols to match
1024    /// against the client-supplied list.
1025    ///
1026    /// Applications must set a value, but no default is provided.
1027    ///
1028    /// ## Examples:
1029    ///
1030    /// ```
1031    /// # let mut config = quiche::Config::new(0xbabababa)?;
1032    /// config.set_application_protos(&[b"http/1.1", b"http/0.9"]);
1033    /// # Ok::<(), quiche::Error>(())
1034    /// ```
1035    pub fn set_application_protos(
1036        &mut self, protos_list: &[&[u8]],
1037    ) -> Result<()> {
1038        self.application_protos =
1039            protos_list.iter().map(|s| s.to_vec()).collect();
1040
1041        self.tls_ctx.set_alpn(protos_list)
1042    }
1043
1044    /// Configures the list of supported application protocols using wire
1045    /// format.
1046    ///
1047    /// The list of protocols `protos` must be a series of non-empty, 8-bit
1048    /// length-prefixed strings.
1049    ///
1050    /// See [`set_application_protos`](Self::set_application_protos) for more
1051    /// background about application protocols.
1052    ///
1053    /// ## Examples:
1054    ///
1055    /// ```
1056    /// # let mut config = quiche::Config::new(0xbabababa)?;
1057    /// config.set_application_protos_wire_format(b"\x08http/1.1\x08http/0.9")?;
1058    /// # Ok::<(), quiche::Error>(())
1059    /// ```
1060    pub fn set_application_protos_wire_format(
1061        &mut self, protos: &[u8],
1062    ) -> Result<()> {
1063        let mut b = octets::Octets::with_slice(protos);
1064
1065        let mut protos_list = Vec::new();
1066
1067        while let Ok(proto) = b.get_bytes_with_u8_length() {
1068            protos_list.push(proto.buf());
1069        }
1070
1071        self.set_application_protos(&protos_list)
1072    }
1073
1074    /// Sets the anti-amplification limit factor.
1075    ///
1076    /// The default value is `3`.
1077    pub fn set_max_amplification_factor(&mut self, v: usize) {
1078        self.max_amplification_factor = v;
1079    }
1080
1081    /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
1082    ///
1083    /// The default value is infinite, that is, no timeout is used.
1084    pub fn set_max_idle_timeout(&mut self, v: u64) {
1085        self.local_transport_params.max_idle_timeout = v;
1086    }
1087
1088    /// Sets the `max_udp_payload_size transport` parameter.
1089    ///
1090    /// The default value is `65527`.
1091    pub fn set_max_recv_udp_payload_size(&mut self, v: usize) {
1092        self.local_transport_params.max_udp_payload_size = v as u64;
1093    }
1094
1095    /// Sets the maximum outgoing UDP payload size.
1096    ///
1097    /// The default and minimum value is `1200`.
1098    pub fn set_max_send_udp_payload_size(&mut self, v: usize) {
1099        self.max_send_udp_payload_size = cmp::max(v, MAX_SEND_UDP_PAYLOAD_SIZE);
1100    }
1101
1102    /// Sets the `initial_max_data` transport parameter.
1103    ///
1104    /// When set to a non-zero value quiche will only allow at most `v` bytes of
1105    /// incoming stream data to be buffered for the whole connection (that is,
1106    /// data that is not yet read by the application) and will allow more data
1107    /// to be received as the buffer is consumed by the application.
1108    ///
1109    /// When set to zero, either explicitly or via the default, quiche will not
1110    /// give any flow control to the peer, preventing it from sending any stream
1111    /// data.
1112    ///
1113    /// The default value is `0`.
1114    pub fn set_initial_max_data(&mut self, v: u64) {
1115        self.local_transport_params.initial_max_data = v;
1116    }
1117
1118    /// Sets the `initial_max_stream_data_bidi_local` transport parameter.
1119    ///
1120    /// When set to a non-zero value quiche will only allow at most `v` bytes
1121    /// of incoming stream data to be buffered for each locally-initiated
1122    /// bidirectional stream (that is, data that is not yet read by the
1123    /// application) and will allow more data to be received as the buffer is
1124    /// consumed by the application.
1125    ///
1126    /// When set to zero, either explicitly or via the default, quiche will not
1127    /// give any flow control to the peer, preventing it from sending any stream
1128    /// data.
1129    ///
1130    /// The default value is `0`.
1131    pub fn set_initial_max_stream_data_bidi_local(&mut self, v: u64) {
1132        self.local_transport_params
1133            .initial_max_stream_data_bidi_local = v;
1134    }
1135
1136    /// Sets the `initial_max_stream_data_bidi_remote` transport parameter.
1137    ///
1138    /// When set to a non-zero value quiche will only allow at most `v` bytes
1139    /// of incoming stream data to be buffered for each remotely-initiated
1140    /// bidirectional stream (that is, data that is not yet read by the
1141    /// application) and will allow more data to be received as the buffer is
1142    /// consumed by the application.
1143    ///
1144    /// When set to zero, either explicitly or via the default, quiche will not
1145    /// give any flow control to the peer, preventing it from sending any stream
1146    /// data.
1147    ///
1148    /// The default value is `0`.
1149    pub fn set_initial_max_stream_data_bidi_remote(&mut self, v: u64) {
1150        self.local_transport_params
1151            .initial_max_stream_data_bidi_remote = v;
1152    }
1153
1154    /// Sets the `initial_max_stream_data_uni` transport parameter.
1155    ///
1156    /// When set to a non-zero value quiche will only allow at most `v` bytes
1157    /// of incoming stream data to be buffered for each unidirectional stream
1158    /// (that is, data that is not yet read by the application) and will allow
1159    /// more data to be received as the buffer is consumed by the application.
1160    ///
1161    /// When set to zero, either explicitly or via the default, quiche will not
1162    /// give any flow control to the peer, preventing it from sending any stream
1163    /// data.
1164    ///
1165    /// The default value is `0`.
1166    pub fn set_initial_max_stream_data_uni(&mut self, v: u64) {
1167        self.local_transport_params.initial_max_stream_data_uni = v;
1168    }
1169
1170    /// Sets the `initial_max_streams_bidi` transport parameter.
1171    ///
1172    /// When set to a non-zero value quiche will only allow `v` number of
1173    /// concurrent remotely-initiated bidirectional streams to be open at any
1174    /// given time and will increase the limit automatically as streams are
1175    /// completed.
1176    ///
1177    /// When set to zero, either explicitly or via the default, quiche will not
1178    /// not allow the peer to open any bidirectional streams.
1179    ///
1180    /// A bidirectional stream is considered completed when all incoming data
1181    /// has been read by the application (up to the `fin` offset) or the
1182    /// stream's read direction has been shutdown, and all outgoing data has
1183    /// been acked by the peer (up to the `fin` offset) or the stream's write
1184    /// direction has been shutdown.
1185    ///
1186    /// The default value is `0`.
1187    pub fn set_initial_max_streams_bidi(&mut self, v: u64) {
1188        self.local_transport_params.initial_max_streams_bidi = v;
1189    }
1190
1191    /// Sets the `initial_max_streams_uni` transport parameter.
1192    ///
1193    /// When set to a non-zero value quiche will only allow `v` number of
1194    /// concurrent remotely-initiated unidirectional streams to be open at any
1195    /// given time and will increase the limit automatically as streams are
1196    /// completed.
1197    ///
1198    /// When set to zero, either explicitly or via the default, quiche will not
1199    /// not allow the peer to open any unidirectional streams.
1200    ///
1201    /// A unidirectional stream is considered completed when all incoming data
1202    /// has been read by the application (up to the `fin` offset) or the
1203    /// stream's read direction has been shutdown.
1204    ///
1205    /// The default value is `0`.
1206    pub fn set_initial_max_streams_uni(&mut self, v: u64) {
1207        self.local_transport_params.initial_max_streams_uni = v;
1208    }
1209
1210    /// Sets the `ack_delay_exponent` transport parameter.
1211    ///
1212    /// The default value is `3`.
1213    pub fn set_ack_delay_exponent(&mut self, v: u64) {
1214        self.local_transport_params.ack_delay_exponent = v;
1215    }
1216
1217    /// Sets the `max_ack_delay` transport parameter.
1218    ///
1219    /// The default value is `25`.
1220    pub fn set_max_ack_delay(&mut self, v: u64) {
1221        self.local_transport_params.max_ack_delay = v;
1222    }
1223
1224    /// Sets the `active_connection_id_limit` transport parameter.
1225    ///
1226    /// The default value is `2`. Lower values will be ignored.
1227    pub fn set_active_connection_id_limit(&mut self, v: u64) {
1228        if v >= 2 {
1229            self.local_transport_params.active_conn_id_limit = v;
1230        }
1231    }
1232
1233    /// Sets the `disable_active_migration` transport parameter.
1234    ///
1235    /// The default value is `false`.
1236    pub fn set_disable_active_migration(&mut self, v: bool) {
1237        self.local_transport_params.disable_active_migration = v;
1238    }
1239
1240    /// Sets the congestion control algorithm used by string.
1241    ///
1242    /// The default value is `cubic`. On error `Error::CongestionControl`
1243    /// will be returned.
1244    ///
1245    /// ## Examples:
1246    ///
1247    /// ```
1248    /// # let mut config = quiche::Config::new(0xbabababa)?;
1249    /// config.set_cc_algorithm_name("reno");
1250    /// # Ok::<(), quiche::Error>(())
1251    /// ```
1252    pub fn set_cc_algorithm_name(&mut self, name: &str) -> Result<()> {
1253        self.cc_algorithm = CongestionControlAlgorithm::from_str(name)?;
1254
1255        Ok(())
1256    }
1257
1258    /// Sets initial congestion window size in terms of packet count.
1259    ///
1260    /// The default value is 10.
1261    pub fn set_initial_congestion_window_packets(&mut self, packets: usize) {
1262        self.initial_congestion_window_packets = packets;
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    /// Configures whether to enable HyStart++.
1273    ///
1274    /// The default value is `true`.
1275    pub fn enable_hystart(&mut self, v: bool) {
1276        self.hystart = v;
1277    }
1278
1279    /// Configures whether to enable pacing.
1280    ///
1281    /// The default value is `true`.
1282    pub fn enable_pacing(&mut self, v: bool) {
1283        self.pacing = v;
1284    }
1285
1286    /// Sets the max value for pacing rate.
1287    ///
1288    /// By default pacing rate is not limited.
1289    pub fn set_max_pacing_rate(&mut self, v: u64) {
1290        self.max_pacing_rate = Some(v);
1291    }
1292
1293    /// Configures whether to enable receiving DATAGRAM frames.
1294    ///
1295    /// When enabled, the `max_datagram_frame_size` transport parameter is set
1296    /// to 65536 as recommended by draft-ietf-quic-datagram-01.
1297    ///
1298    /// The default is `false`.
1299    pub fn enable_dgram(
1300        &mut self, enabled: bool, recv_queue_len: usize, send_queue_len: usize,
1301    ) {
1302        self.local_transport_params.max_datagram_frame_size = if enabled {
1303            Some(MAX_DGRAM_FRAME_SIZE)
1304        } else {
1305            None
1306        };
1307        self.dgram_recv_max_queue_len = recv_queue_len;
1308        self.dgram_send_max_queue_len = send_queue_len;
1309    }
1310
1311    /// Configures the max number of queued received PATH_CHALLENGE frames.
1312    ///
1313    /// When an endpoint receives a PATH_CHALLENGE frame and the queue is full,
1314    /// the frame is discarded.
1315    ///
1316    /// The default is 3.
1317    pub fn set_path_challenge_recv_max_queue_len(&mut self, queue_len: usize) {
1318        self.path_challenge_recv_max_queue_len = queue_len;
1319    }
1320
1321    /// Sets the maximum size of the connection window.
1322    ///
1323    /// The default value is MAX_CONNECTION_WINDOW (24MBytes).
1324    pub fn set_max_connection_window(&mut self, v: u64) {
1325        self.max_connection_window = v;
1326    }
1327
1328    /// Sets the maximum size of the stream window.
1329    ///
1330    /// The default value is MAX_STREAM_WINDOW (16MBytes).
1331    pub fn set_max_stream_window(&mut self, v: u64) {
1332        self.max_stream_window = v;
1333    }
1334
1335    /// Sets the initial stateless reset token.
1336    ///
1337    /// This value is only advertised by servers. Setting a stateless retry
1338    /// token as a client has no effect on the connection.
1339    ///
1340    /// The default value is `None`.
1341    pub fn set_stateless_reset_token(&mut self, v: Option<u128>) {
1342        self.local_transport_params.stateless_reset_token = v;
1343    }
1344
1345    /// Sets whether the QUIC connection should avoid reusing DCIDs over
1346    /// different paths.
1347    ///
1348    /// When set to `true`, it ensures that a destination Connection ID is never
1349    /// reused on different paths. Such behaviour may lead to connection stall
1350    /// if the peer performs a non-voluntary migration (e.g., NAT rebinding) and
1351    /// does not provide additional destination Connection IDs to handle such
1352    /// event.
1353    ///
1354    /// The default value is `false`.
1355    pub fn set_disable_dcid_reuse(&mut self, v: bool) {
1356        self.disable_dcid_reuse = v;
1357    }
1358
1359    /// Enables tracking unknown transport parameters.
1360    ///
1361    /// Specify the maximum number of bytes used to track unknown transport
1362    /// parameters. The size includes the identifier and its value. If storing a
1363    /// transport parameter would cause the limit to be exceeded, it is quietly
1364    /// dropped.
1365    ///
1366    /// The default is that the feature is disabled.
1367    pub fn enable_track_unknown_transport_parameters(&mut self, size: usize) {
1368        self.track_unknown_transport_params = Some(size);
1369    }
1370}
1371
1372/// A QUIC connection.
1373pub struct Connection {
1374    /// QUIC wire version used for the connection.
1375    version: u32,
1376
1377    /// Connection Identifiers.
1378    ids: cid::ConnectionIdentifiers,
1379
1380    /// Unique opaque ID for the connection that can be used for logging.
1381    trace_id: String,
1382
1383    /// Packet number spaces.
1384    pkt_num_spaces: [packet::PktNumSpace; packet::Epoch::count()],
1385
1386    /// Next packet number.
1387    next_pkt_num: u64,
1388
1389    /// Peer's transport parameters.
1390    peer_transport_params: TransportParams,
1391
1392    /// If tracking unknown transport parameters from a peer, how much space to
1393    /// use in bytes.
1394    peer_transport_params_track_unknown: Option<usize>,
1395
1396    /// Local transport parameters.
1397    local_transport_params: TransportParams,
1398
1399    /// TLS handshake state.
1400    handshake: tls::Handshake,
1401
1402    /// Serialized TLS session buffer.
1403    ///
1404    /// This field is populated when a new session ticket is processed on the
1405    /// client. On the server this is empty.
1406    session: Option<Vec<u8>>,
1407
1408    /// The configuration for recovery.
1409    recovery_config: recovery::RecoveryConfig,
1410
1411    /// The path manager.
1412    paths: path::PathMap,
1413
1414    /// PATH_CHALLENGE receive queue max length.
1415    path_challenge_recv_max_queue_len: usize,
1416
1417    /// Total number of received PATH_CHALLENGE frames.
1418    path_challenge_rx_count: u64,
1419
1420    /// List of supported application protocols.
1421    application_protos: Vec<Vec<u8>>,
1422
1423    /// Total number of received packets.
1424    recv_count: usize,
1425
1426    /// Total number of sent packets.
1427    sent_count: usize,
1428
1429    /// Total number of lost packets.
1430    lost_count: usize,
1431
1432    /// Total number of packets sent with data retransmitted.
1433    retrans_count: usize,
1434
1435    /// Total number of sent DATAGRAM frames.
1436    dgram_sent_count: usize,
1437
1438    /// Total number of received DATAGRAM frames.
1439    dgram_recv_count: usize,
1440
1441    /// Total number of bytes received from the peer.
1442    rx_data: u64,
1443
1444    /// Receiver flow controller.
1445    flow_control: flowcontrol::FlowControl,
1446
1447    /// Whether we send MAX_DATA frame.
1448    almost_full: bool,
1449
1450    /// Number of stream data bytes that can be buffered.
1451    tx_cap: usize,
1452
1453    // Number of bytes buffered in the send buffer.
1454    tx_buffered: usize,
1455
1456    /// Total number of bytes sent to the peer.
1457    tx_data: u64,
1458
1459    /// Peer's flow control limit for the connection.
1460    max_tx_data: u64,
1461
1462    /// Last tx_data before running a full send() loop.
1463    last_tx_data: u64,
1464
1465    /// Total number of bytes retransmitted over the connection.
1466    /// This counts only STREAM and CRYPTO data.
1467    stream_retrans_bytes: u64,
1468
1469    /// Total number of bytes sent over the connection.
1470    sent_bytes: u64,
1471
1472    /// Total number of bytes received over the connection.
1473    recv_bytes: u64,
1474
1475    /// Total number of bytes sent acked over the connection.
1476    acked_bytes: u64,
1477
1478    /// Total number of bytes sent lost over the connection.
1479    lost_bytes: u64,
1480
1481    /// Streams map, indexed by stream ID.
1482    streams: stream::StreamMap,
1483
1484    /// Peer's original destination connection ID. Used by the client to
1485    /// validate the server's transport parameter.
1486    odcid: Option<ConnectionId<'static>>,
1487
1488    /// Peer's retry source connection ID. Used by the client during stateless
1489    /// retry to validate the server's transport parameter.
1490    rscid: Option<ConnectionId<'static>>,
1491
1492    /// Received address verification token.
1493    token: Option<Vec<u8>>,
1494
1495    /// Error code and reason to be sent to the peer in a CONNECTION_CLOSE
1496    /// frame.
1497    local_error: Option<ConnectionError>,
1498
1499    /// Error code and reason received from the peer in a CONNECTION_CLOSE
1500    /// frame.
1501    peer_error: Option<ConnectionError>,
1502
1503    /// The connection-level limit at which send blocking occurred.
1504    blocked_limit: Option<u64>,
1505
1506    /// Idle timeout expiration time.
1507    idle_timer: Option<time::Instant>,
1508
1509    /// Draining timeout expiration time.
1510    draining_timer: Option<time::Instant>,
1511
1512    /// List of raw packets that were received before they could be decrypted.
1513    undecryptable_pkts: VecDeque<(Vec<u8>, RecvInfo)>,
1514
1515    /// The negotiated ALPN protocol.
1516    alpn: Vec<u8>,
1517
1518    /// Whether this is a server-side connection.
1519    is_server: bool,
1520
1521    /// Whether the initial secrets have been derived.
1522    derived_initial_secrets: bool,
1523
1524    /// Whether a version negotiation packet has already been received. Only
1525    /// relevant for client connections.
1526    did_version_negotiation: bool,
1527
1528    /// Whether stateless retry has been performed.
1529    did_retry: bool,
1530
1531    /// Whether the peer already updated its connection ID.
1532    got_peer_conn_id: bool,
1533
1534    /// Whether the peer verified our initial address.
1535    peer_verified_initial_address: bool,
1536
1537    /// Whether the peer's transport parameters were parsed.
1538    parsed_peer_transport_params: bool,
1539
1540    /// Whether the connection handshake has been completed.
1541    handshake_completed: bool,
1542
1543    /// Whether the HANDSHAKE_DONE frame has been sent.
1544    handshake_done_sent: bool,
1545
1546    /// Whether the HANDSHAKE_DONE frame has been acked.
1547    handshake_done_acked: bool,
1548
1549    /// Whether the connection handshake has been confirmed.
1550    handshake_confirmed: bool,
1551
1552    /// Key phase bit used for outgoing protected packets.
1553    key_phase: bool,
1554
1555    /// Whether an ack-eliciting packet has been sent since last receiving a
1556    /// packet.
1557    ack_eliciting_sent: bool,
1558
1559    /// Whether the connection is closed.
1560    closed: bool,
1561
1562    /// Whether the connection was timed out.
1563    timed_out: bool,
1564
1565    /// Whether to send GREASE.
1566    grease: bool,
1567
1568    /// TLS keylog writer.
1569    keylog: Option<Box<dyn std::io::Write + Send + Sync>>,
1570
1571    #[cfg(feature = "qlog")]
1572    qlog: QlogInfo,
1573
1574    /// DATAGRAM queues.
1575    dgram_recv_queue: dgram::DatagramQueue,
1576    dgram_send_queue: dgram::DatagramQueue,
1577
1578    /// Whether to emit DATAGRAM frames in the next packet.
1579    emit_dgram: bool,
1580
1581    /// Whether the connection should prevent from reusing destination
1582    /// Connection IDs when the peer migrates.
1583    disable_dcid_reuse: bool,
1584
1585    /// The number of streams reset by local.
1586    reset_stream_local_count: u64,
1587
1588    /// The number of streams stopped by local.
1589    stopped_stream_local_count: u64,
1590
1591    /// The number of streams reset by remote.
1592    reset_stream_remote_count: u64,
1593
1594    /// The number of streams stopped by remote.
1595    stopped_stream_remote_count: u64,
1596
1597    /// The anti-amplification limit factor.
1598    max_amplification_factor: usize,
1599}
1600
1601/// Creates a new server-side connection.
1602///
1603/// The `scid` parameter represents the server's source connection ID, while
1604/// the optional `odcid` parameter represents the original destination ID the
1605/// client sent before a stateless retry (this is only required when using
1606/// the [`retry()`] function).
1607///
1608/// [`retry()`]: fn.retry.html
1609///
1610/// ## Examples:
1611///
1612/// ```no_run
1613/// # let mut config = quiche::Config::new(0xbabababa)?;
1614/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1615/// # let local = "127.0.0.1:0".parse().unwrap();
1616/// # let peer = "127.0.0.1:1234".parse().unwrap();
1617/// let conn = quiche::accept(&scid, None, local, peer, &mut config)?;
1618/// # Ok::<(), quiche::Error>(())
1619/// ```
1620#[inline]
1621pub fn accept(
1622    scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1623    peer: SocketAddr, config: &mut Config,
1624) -> Result<Connection> {
1625    let conn = Connection::new(scid, odcid, local, peer, config, true)?;
1626
1627    Ok(conn)
1628}
1629
1630/// Creates a new client-side connection.
1631///
1632/// The `scid` parameter is used as the connection's source connection ID,
1633/// while the optional `server_name` parameter is used to verify the peer's
1634/// certificate.
1635///
1636/// ## Examples:
1637///
1638/// ```no_run
1639/// # let mut config = quiche::Config::new(0xbabababa)?;
1640/// # let server_name = "quic.tech";
1641/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1642/// # let local = "127.0.0.1:4321".parse().unwrap();
1643/// # let peer = "127.0.0.1:1234".parse().unwrap();
1644/// let conn =
1645///     quiche::connect(Some(&server_name), &scid, local, peer, &mut config)?;
1646/// # Ok::<(), quiche::Error>(())
1647/// ```
1648#[inline]
1649pub fn connect(
1650    server_name: Option<&str>, scid: &ConnectionId, local: SocketAddr,
1651    peer: SocketAddr, config: &mut Config,
1652) -> Result<Connection> {
1653    let mut conn = Connection::new(scid, None, local, peer, config, false)?;
1654
1655    if let Some(server_name) = server_name {
1656        conn.handshake.set_host_name(server_name)?;
1657    }
1658
1659    Ok(conn)
1660}
1661
1662/// Writes a version negotiation packet.
1663///
1664/// The `scid` and `dcid` parameters are the source connection ID and the
1665/// destination connection ID extracted from the received client's Initial
1666/// packet that advertises an unsupported version.
1667///
1668/// ## Examples:
1669///
1670/// ```no_run
1671/// # let mut buf = [0; 512];
1672/// # let mut out = [0; 512];
1673/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1674/// let (len, src) = socket.recv_from(&mut buf).unwrap();
1675///
1676/// let hdr =
1677///     quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1678///
1679/// if hdr.version != quiche::PROTOCOL_VERSION {
1680///     let len = quiche::negotiate_version(&hdr.scid, &hdr.dcid, &mut out)?;
1681///     socket.send_to(&out[..len], &src).unwrap();
1682/// }
1683/// # Ok::<(), quiche::Error>(())
1684/// ```
1685#[inline]
1686pub fn negotiate_version(
1687    scid: &ConnectionId, dcid: &ConnectionId, out: &mut [u8],
1688) -> Result<usize> {
1689    packet::negotiate_version(scid, dcid, out)
1690}
1691
1692/// Writes a stateless retry packet.
1693///
1694/// The `scid` and `dcid` parameters are the source connection ID and the
1695/// destination connection ID extracted from the received client's Initial
1696/// packet, while `new_scid` is the server's new source connection ID and
1697/// `token` is the address validation token the client needs to echo back.
1698///
1699/// The application is responsible for generating the address validation
1700/// token to be sent to the client, and verifying tokens sent back by the
1701/// client. The generated token should include the `dcid` parameter, such
1702/// that it can be later extracted from the token and passed to the
1703/// [`accept()`] function as its `odcid` parameter.
1704///
1705/// [`accept()`]: fn.accept.html
1706///
1707/// ## Examples:
1708///
1709/// ```no_run
1710/// # let mut config = quiche::Config::new(0xbabababa)?;
1711/// # let mut buf = [0; 512];
1712/// # let mut out = [0; 512];
1713/// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
1714/// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
1715/// # let local = socket.local_addr().unwrap();
1716/// # fn mint_token(hdr: &quiche::Header, src: &std::net::SocketAddr) -> Vec<u8> {
1717/// #     vec![]
1718/// # }
1719/// # fn validate_token<'a>(src: &std::net::SocketAddr, token: &'a [u8]) -> Option<quiche::ConnectionId<'a>> {
1720/// #     None
1721/// # }
1722/// let (len, peer) = socket.recv_from(&mut buf).unwrap();
1723///
1724/// let hdr = quiche::Header::from_slice(&mut buf[..len], quiche::MAX_CONN_ID_LEN)?;
1725///
1726/// let token = hdr.token.as_ref().unwrap();
1727///
1728/// // No token sent by client, create a new one.
1729/// if token.is_empty() {
1730///     let new_token = mint_token(&hdr, &peer);
1731///
1732///     let len = quiche::retry(
1733///         &hdr.scid, &hdr.dcid, &scid, &new_token, hdr.version, &mut out,
1734///     )?;
1735///
1736///     socket.send_to(&out[..len], &peer).unwrap();
1737///     return Ok(());
1738/// }
1739///
1740/// // Client sent token, validate it.
1741/// let odcid = validate_token(&peer, token);
1742///
1743/// if odcid.is_none() {
1744///     // Invalid address validation token.
1745///     return Ok(());
1746/// }
1747///
1748/// let conn = quiche::accept(&scid, odcid.as_ref(), local, peer, &mut config)?;
1749/// # Ok::<(), quiche::Error>(())
1750/// ```
1751#[inline]
1752pub fn retry(
1753    scid: &ConnectionId, dcid: &ConnectionId, new_scid: &ConnectionId,
1754    token: &[u8], version: u32, out: &mut [u8],
1755) -> Result<usize> {
1756    packet::retry(scid, dcid, new_scid, token, version, out)
1757}
1758
1759/// Returns true if the given protocol version is supported.
1760#[inline]
1761pub fn version_is_supported(version: u32) -> bool {
1762    matches!(version, PROTOCOL_VERSION_V1)
1763}
1764
1765/// Pushes a frame to the output packet if there is enough space.
1766///
1767/// Returns `true` on success, `false` otherwise. In case of failure it means
1768/// there is no room to add the frame in the packet. You may retry to add the
1769/// frame later.
1770macro_rules! push_frame_to_pkt {
1771    ($out:expr, $frames:expr, $frame:expr, $left:expr) => {{
1772        if $frame.wire_len() <= $left {
1773            $left -= $frame.wire_len();
1774
1775            $frame.to_bytes(&mut $out)?;
1776
1777            $frames.push($frame);
1778
1779            true
1780        } else {
1781            false
1782        }
1783    }};
1784}
1785
1786/// Executes the provided body if the qlog feature is enabled, quiche has been
1787/// configured with a log writer, the event's importance is within the
1788/// configured level.
1789macro_rules! qlog_with_type {
1790    ($ty:expr, $qlog:expr, $qlog_streamer_ref:ident, $body:block) => {{
1791        #[cfg(feature = "qlog")]
1792        {
1793            if EventImportance::from($ty).is_contained_in(&$qlog.level) {
1794                if let Some($qlog_streamer_ref) = &mut $qlog.streamer {
1795                    $body
1796                }
1797            }
1798        }
1799    }};
1800}
1801
1802#[cfg(feature = "qlog")]
1803const QLOG_PARAMS_SET: EventType =
1804    EventType::TransportEventType(TransportEventType::ParametersSet);
1805
1806#[cfg(feature = "qlog")]
1807const QLOG_PACKET_RX: EventType =
1808    EventType::TransportEventType(TransportEventType::PacketReceived);
1809
1810#[cfg(feature = "qlog")]
1811const QLOG_PACKET_TX: EventType =
1812    EventType::TransportEventType(TransportEventType::PacketSent);
1813
1814#[cfg(feature = "qlog")]
1815const QLOG_DATA_MV: EventType =
1816    EventType::TransportEventType(TransportEventType::DataMoved);
1817
1818#[cfg(feature = "qlog")]
1819const QLOG_METRICS: EventType =
1820    EventType::RecoveryEventType(RecoveryEventType::MetricsUpdated);
1821
1822#[cfg(feature = "qlog")]
1823const QLOG_CONNECTION_CLOSED: EventType =
1824    EventType::ConnectivityEventType(ConnectivityEventType::ConnectionClosed);
1825
1826#[cfg(feature = "qlog")]
1827struct QlogInfo {
1828    streamer: Option<qlog::streamer::QlogStreamer>,
1829    logged_peer_params: bool,
1830    level: EventImportance,
1831}
1832
1833#[cfg(feature = "qlog")]
1834impl Default for QlogInfo {
1835    fn default() -> Self {
1836        QlogInfo {
1837            streamer: None,
1838            logged_peer_params: false,
1839            level: EventImportance::Base,
1840        }
1841    }
1842}
1843
1844impl Connection {
1845    fn new(
1846        scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1847        peer: SocketAddr, config: &mut Config, is_server: bool,
1848    ) -> Result<Connection> {
1849        let tls = config.tls_ctx.new_handshake()?;
1850        Connection::with_tls(scid, odcid, local, peer, config, tls, is_server)
1851    }
1852
1853    fn with_tls(
1854        scid: &ConnectionId, odcid: Option<&ConnectionId>, local: SocketAddr,
1855        peer: SocketAddr, config: &Config, tls: tls::Handshake, is_server: bool,
1856    ) -> Result<Connection> {
1857        let max_rx_data = config.local_transport_params.initial_max_data;
1858
1859        let scid_as_hex: Vec<String> =
1860            scid.iter().map(|b| format!("{b:02x}")).collect();
1861
1862        let reset_token = if is_server {
1863            config.local_transport_params.stateless_reset_token
1864        } else {
1865            None
1866        };
1867
1868        let recovery_config = recovery::RecoveryConfig::from_config(config);
1869
1870        let mut path = path::Path::new(
1871            local,
1872            peer,
1873            &recovery_config,
1874            config.path_challenge_recv_max_queue_len,
1875            MIN_CLIENT_INITIAL_LEN,
1876            true,
1877        );
1878
1879        // If we did stateless retry assume the peer's address is verified.
1880        path.verified_peer_address = odcid.is_some();
1881        // Assume clients validate the server's address implicitly.
1882        path.peer_verified_local_address = is_server;
1883
1884        // Do not allocate more than the number of active CIDs.
1885        let paths = path::PathMap::new(
1886            path,
1887            config.local_transport_params.active_conn_id_limit as usize,
1888            is_server,
1889            config.pmtud,
1890            config.max_send_udp_payload_size,
1891        );
1892
1893        let active_path_id = paths.get_active_path_id()?;
1894
1895        let ids = cid::ConnectionIdentifiers::new(
1896            config.local_transport_params.active_conn_id_limit as usize,
1897            scid,
1898            active_path_id,
1899            reset_token,
1900        );
1901
1902        let mut conn = Connection {
1903            version: config.version,
1904
1905            ids,
1906
1907            trace_id: scid_as_hex.join(""),
1908
1909            pkt_num_spaces: [
1910                packet::PktNumSpace::new(),
1911                packet::PktNumSpace::new(),
1912                packet::PktNumSpace::new(),
1913            ],
1914
1915            next_pkt_num: 0,
1916
1917            peer_transport_params: TransportParams::default(),
1918
1919            peer_transport_params_track_unknown: config
1920                .track_unknown_transport_params,
1921
1922            local_transport_params: config.local_transport_params.clone(),
1923
1924            handshake: tls,
1925
1926            session: None,
1927
1928            recovery_config,
1929
1930            paths,
1931            path_challenge_recv_max_queue_len: config
1932                .path_challenge_recv_max_queue_len,
1933            path_challenge_rx_count: 0,
1934
1935            application_protos: config.application_protos.clone(),
1936
1937            recv_count: 0,
1938            sent_count: 0,
1939            lost_count: 0,
1940            retrans_count: 0,
1941            dgram_sent_count: 0,
1942            dgram_recv_count: 0,
1943            sent_bytes: 0,
1944            recv_bytes: 0,
1945            acked_bytes: 0,
1946            lost_bytes: 0,
1947
1948            rx_data: 0,
1949            flow_control: flowcontrol::FlowControl::new(
1950                max_rx_data,
1951                cmp::min(max_rx_data / 2 * 3, DEFAULT_CONNECTION_WINDOW),
1952                config.max_connection_window,
1953            ),
1954            almost_full: false,
1955
1956            tx_cap: 0,
1957
1958            tx_buffered: 0,
1959
1960            tx_data: 0,
1961            max_tx_data: 0,
1962            last_tx_data: 0,
1963
1964            stream_retrans_bytes: 0,
1965
1966            streams: stream::StreamMap::new(
1967                config.local_transport_params.initial_max_streams_bidi,
1968                config.local_transport_params.initial_max_streams_uni,
1969                config.max_stream_window,
1970            ),
1971
1972            odcid: None,
1973
1974            rscid: None,
1975
1976            token: None,
1977
1978            local_error: None,
1979
1980            peer_error: None,
1981
1982            blocked_limit: None,
1983
1984            idle_timer: None,
1985
1986            draining_timer: None,
1987
1988            undecryptable_pkts: VecDeque::new(),
1989
1990            alpn: Vec::new(),
1991
1992            is_server,
1993
1994            derived_initial_secrets: false,
1995
1996            did_version_negotiation: false,
1997
1998            did_retry: false,
1999
2000            got_peer_conn_id: false,
2001
2002            // Assume clients validate the server's address implicitly.
2003            peer_verified_initial_address: is_server,
2004
2005            parsed_peer_transport_params: false,
2006
2007            handshake_completed: false,
2008
2009            handshake_done_sent: false,
2010            handshake_done_acked: false,
2011
2012            handshake_confirmed: false,
2013
2014            key_phase: false,
2015
2016            ack_eliciting_sent: false,
2017
2018            closed: false,
2019
2020            timed_out: false,
2021
2022            grease: config.grease,
2023
2024            keylog: None,
2025
2026            #[cfg(feature = "qlog")]
2027            qlog: Default::default(),
2028
2029            dgram_recv_queue: dgram::DatagramQueue::new(
2030                config.dgram_recv_max_queue_len,
2031            ),
2032
2033            dgram_send_queue: dgram::DatagramQueue::new(
2034                config.dgram_send_max_queue_len,
2035            ),
2036
2037            emit_dgram: true,
2038
2039            disable_dcid_reuse: config.disable_dcid_reuse,
2040
2041            reset_stream_local_count: 0,
2042            stopped_stream_local_count: 0,
2043            reset_stream_remote_count: 0,
2044            stopped_stream_remote_count: 0,
2045
2046            max_amplification_factor: config.max_amplification_factor,
2047        };
2048
2049        if let Some(odcid) = odcid {
2050            conn.local_transport_params
2051                .original_destination_connection_id = Some(odcid.to_vec().into());
2052
2053            conn.local_transport_params.retry_source_connection_id =
2054                Some(conn.ids.get_scid(0)?.cid.to_vec().into());
2055
2056            conn.did_retry = true;
2057        }
2058
2059        conn.local_transport_params.initial_source_connection_id =
2060            Some(conn.ids.get_scid(0)?.cid.to_vec().into());
2061
2062        conn.handshake.init(is_server)?;
2063
2064        conn.handshake
2065            .use_legacy_codepoint(config.version != PROTOCOL_VERSION_V1);
2066
2067        conn.encode_transport_params()?;
2068
2069        // Derive initial secrets for the client. We can do this here because
2070        // we already generated the random destination connection ID.
2071        if !is_server {
2072            let mut dcid = [0; 16];
2073            rand::rand_bytes(&mut dcid[..]);
2074
2075            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2076                &dcid,
2077                conn.version,
2078                conn.is_server,
2079            )?;
2080
2081            let reset_token = conn.peer_transport_params.stateless_reset_token;
2082            conn.set_initial_dcid(
2083                dcid.to_vec().into(),
2084                reset_token,
2085                active_path_id,
2086            )?;
2087
2088            conn.pkt_num_spaces[packet::Epoch::Initial].crypto_open =
2089                Some(aead_open);
2090            conn.pkt_num_spaces[packet::Epoch::Initial].crypto_seal =
2091                Some(aead_seal);
2092
2093            conn.derived_initial_secrets = true;
2094        }
2095
2096        Ok(conn)
2097    }
2098
2099    /// Sets keylog output to the designated [`Writer`].
2100    ///
2101    /// This needs to be called as soon as the connection is created, to avoid
2102    /// missing some early logs.
2103    ///
2104    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2105    #[inline]
2106    pub fn set_keylog(&mut self, writer: Box<dyn std::io::Write + Send + Sync>) {
2107        self.keylog = Some(writer);
2108    }
2109
2110    /// Sets qlog output to the designated [`Writer`].
2111    ///
2112    /// Only events included in `QlogLevel::Base` are written. The serialization
2113    /// format is JSON-SEQ.
2114    ///
2115    /// This needs to be called as soon as the connection is created, to avoid
2116    /// missing some early logs.
2117    ///
2118    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2119    #[cfg(feature = "qlog")]
2120    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2121    pub fn set_qlog(
2122        &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2123        description: String,
2124    ) {
2125        self.set_qlog_with_level(writer, title, description, QlogLevel::Base)
2126    }
2127
2128    /// Sets qlog output to the designated [`Writer`].
2129    ///
2130    /// Only qlog events included in the specified `QlogLevel` are written. The
2131    /// serialization format is JSON-SEQ.
2132    ///
2133    /// This needs to be called as soon as the connection is created, to avoid
2134    /// missing some early logs.
2135    ///
2136    /// [`Writer`]: https://doc.rust-lang.org/std/io/trait.Write.html
2137    #[cfg(feature = "qlog")]
2138    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2139    pub fn set_qlog_with_level(
2140        &mut self, writer: Box<dyn std::io::Write + Send + Sync>, title: String,
2141        description: String, qlog_level: QlogLevel,
2142    ) {
2143        let vp = if self.is_server {
2144            qlog::VantagePointType::Server
2145        } else {
2146            qlog::VantagePointType::Client
2147        };
2148
2149        let level = match qlog_level {
2150            QlogLevel::Core => EventImportance::Core,
2151
2152            QlogLevel::Base => EventImportance::Base,
2153
2154            QlogLevel::Extra => EventImportance::Extra,
2155        };
2156
2157        self.qlog.level = level;
2158
2159        let trace = qlog::TraceSeq::new(
2160            qlog::VantagePoint {
2161                name: None,
2162                ty: vp,
2163                flow: None,
2164            },
2165            Some(title.to_string()),
2166            Some(description.to_string()),
2167            Some(qlog::Configuration {
2168                time_offset: Some(0.0),
2169                original_uris: None,
2170            }),
2171            None,
2172        );
2173
2174        let mut streamer = qlog::streamer::QlogStreamer::new(
2175            qlog::QLOG_VERSION.to_string(),
2176            Some(title),
2177            Some(description),
2178            None,
2179            time::Instant::now(),
2180            trace,
2181            self.qlog.level,
2182            writer,
2183        );
2184
2185        streamer.start_log().ok();
2186
2187        let ev_data = self
2188            .local_transport_params
2189            .to_qlog(TransportOwner::Local, self.handshake.cipher());
2190
2191        // This event occurs very early, so just mark the relative time as 0.0.
2192        streamer.add_event(Event::with_time(0.0, ev_data)).ok();
2193
2194        self.qlog.streamer = Some(streamer);
2195    }
2196
2197    /// Returns a mutable reference to the QlogStreamer, if it exists.
2198    #[cfg(feature = "qlog")]
2199    #[cfg_attr(docsrs, doc(cfg(feature = "qlog")))]
2200    pub fn qlog_streamer(&mut self) -> Option<&mut qlog::streamer::QlogStreamer> {
2201        self.qlog.streamer.as_mut()
2202    }
2203
2204    /// Configures the given session for resumption.
2205    ///
2206    /// On the client, this can be used to offer the given serialized session,
2207    /// as returned by [`session()`], for resumption.
2208    ///
2209    /// This must only be called immediately after creating a connection, that
2210    /// is, before any packet is sent or received.
2211    ///
2212    /// [`session()`]: struct.Connection.html#method.session
2213    #[inline]
2214    pub fn set_session(&mut self, session: &[u8]) -> Result<()> {
2215        let mut b = octets::Octets::with_slice(session);
2216
2217        let session_len = b.get_u64()? as usize;
2218        let session_bytes = b.get_bytes(session_len)?;
2219
2220        self.handshake.set_session(session_bytes.as_ref())?;
2221
2222        let raw_params_len = b.get_u64()? as usize;
2223        let raw_params_bytes = b.get_bytes(raw_params_len)?;
2224
2225        let peer_params = TransportParams::decode(
2226            raw_params_bytes.as_ref(),
2227            self.is_server,
2228            self.peer_transport_params_track_unknown,
2229        )?;
2230
2231        self.process_peer_transport_params(peer_params)?;
2232
2233        Ok(())
2234    }
2235
2236    /// Sets the `max_idle_timeout` transport parameter, in milliseconds.
2237    ///
2238    /// This must only be called immediately after creating a connection, that
2239    /// is, before any packet is sent or received.
2240    ///
2241    /// The default value is infinite, that is, no timeout is used unless
2242    /// already configured when creating the connection.
2243    pub fn set_max_idle_timeout(&mut self, v: u64) -> Result<()> {
2244        self.local_transport_params.max_idle_timeout = v;
2245
2246        self.encode_transport_params()
2247    }
2248
2249    /// Processes QUIC packets received from the peer.
2250    ///
2251    /// On success the number of bytes processed from the input buffer is
2252    /// returned. On error the connection will be closed by calling [`close()`]
2253    /// with the appropriate error code.
2254    ///
2255    /// Coalesced packets will be processed as necessary.
2256    ///
2257    /// Note that the contents of the input buffer `buf` might be modified by
2258    /// this function due to, for example, in-place decryption.
2259    ///
2260    /// [`close()`]: struct.Connection.html#method.close
2261    ///
2262    /// ## Examples:
2263    ///
2264    /// ```no_run
2265    /// # let mut buf = [0; 512];
2266    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
2267    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
2268    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
2269    /// # let peer = "127.0.0.1:1234".parse().unwrap();
2270    /// # let local = socket.local_addr().unwrap();
2271    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
2272    /// loop {
2273    ///     let (read, from) = socket.recv_from(&mut buf).unwrap();
2274    ///
2275    ///     let recv_info = quiche::RecvInfo {
2276    ///         from,
2277    ///         to: local,
2278    ///     };
2279    ///
2280    ///     let read = match conn.recv(&mut buf[..read], recv_info) {
2281    ///         Ok(v) => v,
2282    ///
2283    ///         Err(e) => {
2284    ///             // An error occurred, handle it.
2285    ///             break;
2286    ///         },
2287    ///     };
2288    /// }
2289    /// # Ok::<(), quiche::Error>(())
2290    /// ```
2291    pub fn recv(&mut self, buf: &mut [u8], info: RecvInfo) -> Result<usize> {
2292        let len = buf.len();
2293
2294        if len == 0 {
2295            return Err(Error::BufferTooShort);
2296        }
2297
2298        let recv_pid = self.paths.path_id_from_addrs(&(info.to, info.from));
2299
2300        if let Some(recv_pid) = recv_pid {
2301            let recv_path = self.paths.get_mut(recv_pid)?;
2302
2303            // Keep track of how many bytes we received from the client, so we
2304            // can limit bytes sent back before address validation, to a
2305            // multiple of this. The limit needs to be increased early on, so
2306            // that if there is an error there is enough credit to send a
2307            // CONNECTION_CLOSE.
2308            //
2309            // It doesn't matter if the packets received were valid or not, we
2310            // only need to track the total amount of bytes received.
2311            //
2312            // Note that we also need to limit the number of bytes we sent on a
2313            // path if we are not the host that initiated its usage.
2314            if self.is_server && !recv_path.verified_peer_address {
2315                recv_path.max_send_bytes += len * self.max_amplification_factor;
2316            }
2317        } else if !self.is_server {
2318            // If a client receives packets from an unknown server address,
2319            // the client MUST discard these packets.
2320            trace!(
2321                "{} client received packet from unknown address {:?}, dropping",
2322                self.trace_id,
2323                info,
2324            );
2325
2326            return Ok(len);
2327        }
2328
2329        let mut done = 0;
2330        let mut left = len;
2331
2332        // Process coalesced packets.
2333        while left > 0 {
2334            let read = match self.recv_single(
2335                &mut buf[len - left..len],
2336                &info,
2337                recv_pid,
2338            ) {
2339                Ok(v) => v,
2340
2341                Err(Error::Done) => {
2342                    // If the packet can't be processed or decrypted, check if
2343                    // it's a stateless reset.
2344                    if self.is_stateless_reset(&buf[len - left..len]) {
2345                        trace!("{} packet is a stateless reset", self.trace_id);
2346
2347                        self.mark_closed();
2348                    }
2349
2350                    left
2351                },
2352
2353                Err(e) => {
2354                    // In case of error processing the incoming packet, close
2355                    // the connection.
2356                    self.close(false, e.to_wire(), b"").ok();
2357                    return Err(e);
2358                },
2359            };
2360
2361            done += read;
2362            left -= read;
2363        }
2364
2365        // Even though the packet was previously "accepted", it
2366        // should be safe to forward the error, as it also comes
2367        // from the `recv()` method.
2368        self.process_undecrypted_0rtt_packets()?;
2369
2370        Ok(done)
2371    }
2372
2373    fn process_undecrypted_0rtt_packets(&mut self) -> Result<()> {
2374        // Process previously undecryptable 0-RTT packets if the decryption key
2375        // is now available.
2376        if self.pkt_num_spaces[packet::Epoch::Application]
2377            .crypto_0rtt_open
2378            .is_some()
2379        {
2380            while let Some((mut pkt, info)) = self.undecryptable_pkts.pop_front()
2381            {
2382                if let Err(e) = self.recv(&mut pkt, info) {
2383                    self.undecryptable_pkts.clear();
2384
2385                    return Err(e);
2386                }
2387            }
2388        }
2389        Ok(())
2390    }
2391
2392    /// Returns true if a QUIC packet is a stateless reset.
2393    fn is_stateless_reset(&self, buf: &[u8]) -> bool {
2394        // If the packet is too small, then we just throw it away.
2395        let buf_len = buf.len();
2396        if buf_len < 21 {
2397            return false;
2398        }
2399
2400        // TODO: we should iterate over all active destination connection IDs
2401        // and check against their reset token.
2402        match self.peer_transport_params.stateless_reset_token {
2403            Some(token) => {
2404                let token_len = 16;
2405
2406                crypto::verify_slices_are_equal(
2407                    &token.to_be_bytes(),
2408                    &buf[buf_len - token_len..buf_len],
2409                )
2410                .is_ok()
2411            },
2412
2413            None => false,
2414        }
2415    }
2416
2417    /// Processes a single QUIC packet received from the peer.
2418    ///
2419    /// On success the number of bytes processed from the input buffer is
2420    /// returned. When the [`Done`] error is returned, processing of the
2421    /// remainder of the incoming UDP datagram should be interrupted.
2422    ///
2423    /// Note that a server might observe a new 4-tuple, preventing to
2424    /// know in advance to which path the incoming packet belongs to (`recv_pid`
2425    /// is `None`). As a client, packets from unknown 4-tuple are dropped
2426    /// beforehand (see `recv()`).
2427    ///
2428    /// On error, an error other than [`Done`] is returned.
2429    ///
2430    /// [`Done`]: enum.Error.html#variant.Done
2431    fn recv_single(
2432        &mut self, buf: &mut [u8], info: &RecvInfo, recv_pid: Option<usize>,
2433    ) -> Result<usize> {
2434        let now = time::Instant::now();
2435
2436        if buf.is_empty() {
2437            return Err(Error::Done);
2438        }
2439
2440        if self.is_closed() || self.is_draining() {
2441            return Err(Error::Done);
2442        }
2443
2444        let is_closing = self.local_error.is_some();
2445
2446        if is_closing {
2447            return Err(Error::Done);
2448        }
2449
2450        let buf_len = buf.len();
2451
2452        let mut b = octets::OctetsMut::with_slice(buf);
2453
2454        let mut hdr = Header::from_bytes(&mut b, self.source_id().len())
2455            .map_err(|e| {
2456                drop_pkt_on_err(
2457                    e,
2458                    self.recv_count,
2459                    self.is_server,
2460                    &self.trace_id,
2461                )
2462            })?;
2463
2464        if hdr.ty == packet::Type::VersionNegotiation {
2465            // Version negotiation packets can only be sent by the server.
2466            if self.is_server {
2467                return Err(Error::Done);
2468            }
2469
2470            // Ignore duplicate version negotiation.
2471            if self.did_version_negotiation {
2472                return Err(Error::Done);
2473            }
2474
2475            // Ignore version negotiation if any other packet has already been
2476            // successfully processed.
2477            if self.recv_count > 0 {
2478                return Err(Error::Done);
2479            }
2480
2481            if hdr.dcid != self.source_id() {
2482                return Err(Error::Done);
2483            }
2484
2485            if hdr.scid != self.destination_id() {
2486                return Err(Error::Done);
2487            }
2488
2489            trace!("{} rx pkt {:?}", self.trace_id, hdr);
2490
2491            let versions = hdr.versions.ok_or(Error::Done)?;
2492
2493            // Ignore version negotiation if the version already selected is
2494            // listed.
2495            if versions.iter().any(|&v| v == self.version) {
2496                return Err(Error::Done);
2497            }
2498
2499            let supported_versions =
2500                versions.iter().filter(|&&v| version_is_supported(v));
2501
2502            let mut found_version = false;
2503
2504            for &v in supported_versions {
2505                found_version = true;
2506
2507                // The final version takes precedence over draft ones.
2508                if v == PROTOCOL_VERSION_V1 {
2509                    self.version = v;
2510                    break;
2511                }
2512
2513                self.version = cmp::max(self.version, v);
2514            }
2515
2516            if !found_version {
2517                // We don't support any of the versions offered.
2518                //
2519                // While a man-in-the-middle attacker might be able to
2520                // inject a version negotiation packet that triggers this
2521                // failure, the window of opportunity is very small and
2522                // this error is quite useful for debugging, so don't just
2523                // ignore the packet.
2524                return Err(Error::UnknownVersion);
2525            }
2526
2527            self.did_version_negotiation = true;
2528
2529            // Derive Initial secrets based on the new version.
2530            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2531                &self.destination_id(),
2532                self.version,
2533                self.is_server,
2534            )?;
2535
2536            // Reset connection state to force sending another Initial packet.
2537            self.drop_epoch_state(packet::Epoch::Initial, now);
2538            self.got_peer_conn_id = false;
2539            self.handshake.clear()?;
2540
2541            self.pkt_num_spaces[packet::Epoch::Initial].crypto_open =
2542                Some(aead_open);
2543            self.pkt_num_spaces[packet::Epoch::Initial].crypto_seal =
2544                Some(aead_seal);
2545
2546            self.handshake
2547                .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
2548
2549            // Encode transport parameters again, as the new version might be
2550            // using a different format.
2551            self.encode_transport_params()?;
2552
2553            return Err(Error::Done);
2554        }
2555
2556        if hdr.ty == packet::Type::Retry {
2557            // Retry packets can only be sent by the server.
2558            if self.is_server {
2559                return Err(Error::Done);
2560            }
2561
2562            // Ignore duplicate retry.
2563            if self.did_retry {
2564                return Err(Error::Done);
2565            }
2566
2567            // Check if Retry packet is valid.
2568            if packet::verify_retry_integrity(
2569                &b,
2570                &self.destination_id(),
2571                self.version,
2572            )
2573            .is_err()
2574            {
2575                return Err(Error::Done);
2576            }
2577
2578            trace!("{} rx pkt {:?}", self.trace_id, hdr);
2579
2580            self.token = hdr.token;
2581            self.did_retry = true;
2582
2583            // Remember peer's new connection ID.
2584            self.odcid = Some(self.destination_id().into_owned());
2585
2586            self.set_initial_dcid(
2587                hdr.scid.clone(),
2588                None,
2589                self.paths.get_active_path_id()?,
2590            )?;
2591
2592            self.rscid = Some(self.destination_id().into_owned());
2593
2594            // Derive Initial secrets using the new connection ID.
2595            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2596                &hdr.scid,
2597                self.version,
2598                self.is_server,
2599            )?;
2600
2601            // Reset connection state to force sending another Initial packet.
2602            self.drop_epoch_state(packet::Epoch::Initial, now);
2603            self.got_peer_conn_id = false;
2604            self.handshake.clear()?;
2605
2606            self.pkt_num_spaces[packet::Epoch::Initial].crypto_open =
2607                Some(aead_open);
2608            self.pkt_num_spaces[packet::Epoch::Initial].crypto_seal =
2609                Some(aead_seal);
2610
2611            return Err(Error::Done);
2612        }
2613
2614        if self.is_server && !self.did_version_negotiation {
2615            if !version_is_supported(hdr.version) {
2616                return Err(Error::UnknownVersion);
2617            }
2618
2619            self.version = hdr.version;
2620            self.did_version_negotiation = true;
2621
2622            self.handshake
2623                .use_legacy_codepoint(self.version != PROTOCOL_VERSION_V1);
2624
2625            // Encode transport parameters again, as the new version might be
2626            // using a different format.
2627            self.encode_transport_params()?;
2628        }
2629
2630        if hdr.ty != packet::Type::Short && hdr.version != self.version {
2631            // At this point version negotiation was already performed, so
2632            // ignore packets that don't match the connection's version.
2633            return Err(Error::Done);
2634        }
2635
2636        // Long header packets have an explicit payload length, but short
2637        // packets don't so just use the remaining capacity in the buffer.
2638        let payload_len = if hdr.ty == packet::Type::Short {
2639            b.cap()
2640        } else {
2641            b.get_varint().map_err(|e| {
2642                drop_pkt_on_err(
2643                    e.into(),
2644                    self.recv_count,
2645                    self.is_server,
2646                    &self.trace_id,
2647                )
2648            })? as usize
2649        };
2650
2651        // Make sure the buffer is same or larger than an explicit
2652        // payload length.
2653        if payload_len > b.cap() {
2654            return Err(drop_pkt_on_err(
2655                Error::InvalidPacket,
2656                self.recv_count,
2657                self.is_server,
2658                &self.trace_id,
2659            ));
2660        }
2661
2662        // Derive initial secrets on the server.
2663        if !self.derived_initial_secrets {
2664            let (aead_open, aead_seal) = crypto::derive_initial_key_material(
2665                &hdr.dcid,
2666                self.version,
2667                self.is_server,
2668            )?;
2669
2670            self.pkt_num_spaces[packet::Epoch::Initial].crypto_open =
2671                Some(aead_open);
2672            self.pkt_num_spaces[packet::Epoch::Initial].crypto_seal =
2673                Some(aead_seal);
2674
2675            self.derived_initial_secrets = true;
2676        }
2677
2678        // Select packet number space epoch based on the received packet's type.
2679        let epoch = hdr.ty.to_epoch()?;
2680
2681        // Select AEAD context used to open incoming packet.
2682        let aead = if hdr.ty == packet::Type::ZeroRTT {
2683            // Only use 0-RTT key if incoming packet is 0-RTT.
2684            self.pkt_num_spaces[epoch].crypto_0rtt_open.as_ref()
2685        } else {
2686            // Otherwise use the packet number space's main key.
2687            self.pkt_num_spaces[epoch].crypto_open.as_ref()
2688        };
2689
2690        // Finally, discard packet if no usable key is available.
2691        let mut aead = match aead {
2692            Some(v) => v,
2693
2694            None => {
2695                if hdr.ty == packet::Type::ZeroRTT &&
2696                    self.undecryptable_pkts.len() < MAX_UNDECRYPTABLE_PACKETS &&
2697                    !self.is_established()
2698                {
2699                    // Buffer 0-RTT packets when the required read key is not
2700                    // available yet, and process them later.
2701                    //
2702                    // TODO: in the future we might want to buffer other types
2703                    // of undecryptable packets as well.
2704                    let pkt_len = b.off() + payload_len;
2705                    let pkt = (b.buf()[..pkt_len]).to_vec();
2706
2707                    self.undecryptable_pkts.push_back((pkt, *info));
2708                    return Ok(pkt_len);
2709                }
2710
2711                let e = drop_pkt_on_err(
2712                    Error::CryptoFail,
2713                    self.recv_count,
2714                    self.is_server,
2715                    &self.trace_id,
2716                );
2717
2718                return Err(e);
2719            },
2720        };
2721
2722        let aead_tag_len = aead.alg().tag_len();
2723
2724        packet::decrypt_hdr(&mut b, &mut hdr, aead).map_err(|e| {
2725            drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
2726        })?;
2727
2728        let pn = packet::decode_pkt_num(
2729            self.pkt_num_spaces[epoch].largest_rx_pkt_num,
2730            hdr.pkt_num,
2731            hdr.pkt_num_len,
2732        );
2733
2734        let pn_len = hdr.pkt_num_len;
2735
2736        trace!(
2737            "{} rx pkt {:?} len={} pn={} {}",
2738            self.trace_id,
2739            hdr,
2740            payload_len,
2741            pn,
2742            AddrTupleFmt(info.from, info.to)
2743        );
2744
2745        #[cfg(feature = "qlog")]
2746        let mut qlog_frames = vec![];
2747
2748        // Check for key update.
2749        let mut aead_next = None;
2750
2751        if self.handshake_confirmed &&
2752            hdr.ty != Type::ZeroRTT &&
2753            hdr.key_phase != self.key_phase
2754        {
2755            // Check if this packet arrived before key update.
2756            if let Some(key_update) = self.pkt_num_spaces[epoch]
2757                .key_update
2758                .as_ref()
2759                .and_then(|key_update| {
2760                    (pn < key_update.pn_on_update).then_some(key_update)
2761                })
2762            {
2763                aead = &key_update.crypto_open;
2764            } else {
2765                trace!("{} peer-initiated key update", self.trace_id);
2766
2767                aead_next = Some((
2768                    self.pkt_num_spaces[epoch]
2769                        .crypto_open
2770                        .as_ref()
2771                        .unwrap()
2772                        .derive_next_packet_key()?,
2773                    self.pkt_num_spaces[epoch]
2774                        .crypto_seal
2775                        .as_ref()
2776                        .unwrap()
2777                        .derive_next_packet_key()?,
2778                ));
2779
2780                // `aead_next` is always `Some()` at this point, so the `unwrap()`
2781                // will never fail.
2782                aead = &aead_next.as_ref().unwrap().0;
2783            }
2784        }
2785
2786        let mut payload = packet::decrypt_pkt(
2787            &mut b,
2788            pn,
2789            pn_len,
2790            payload_len,
2791            aead,
2792        )
2793        .map_err(|e| {
2794            drop_pkt_on_err(e, self.recv_count, self.is_server, &self.trace_id)
2795        })?;
2796
2797        if self.pkt_num_spaces[epoch].recv_pkt_num.contains(pn) {
2798            trace!("{} ignored duplicate packet {}", self.trace_id, pn);
2799            return Err(Error::Done);
2800        }
2801
2802        // Packets with no frames are invalid.
2803        if payload.cap() == 0 {
2804            return Err(Error::InvalidPacket);
2805        }
2806
2807        // Now that we decrypted the packet, let's see if we can map it to an
2808        // existing path.
2809        let recv_pid = if hdr.ty == packet::Type::Short && self.got_peer_conn_id {
2810            let pkt_dcid = ConnectionId::from_ref(&hdr.dcid);
2811            self.get_or_create_recv_path_id(recv_pid, &pkt_dcid, buf_len, info)?
2812        } else {
2813            // During handshake, we are on the initial path.
2814            self.paths.get_active_path_id()?
2815        };
2816
2817        // The key update is verified once a packet is successfully decrypted
2818        // using the new keys.
2819        if let Some((open_next, seal_next)) = aead_next {
2820            if !self.pkt_num_spaces[epoch]
2821                .key_update
2822                .as_ref()
2823                .map_or(true, |prev| prev.update_acked)
2824            {
2825                // Peer has updated keys twice without awaiting confirmation.
2826                return Err(Error::KeyUpdate);
2827            }
2828
2829            trace!("{} key update verified", self.trace_id);
2830
2831            let _ = self.pkt_num_spaces[epoch].crypto_seal.replace(seal_next);
2832
2833            let open_prev = self.pkt_num_spaces[epoch]
2834                .crypto_open
2835                .replace(open_next)
2836                .unwrap();
2837
2838            let recv_path = self.paths.get_mut(recv_pid)?;
2839
2840            self.pkt_num_spaces[epoch].key_update = Some(packet::KeyUpdate {
2841                crypto_open: open_prev,
2842                pn_on_update: pn,
2843                update_acked: false,
2844                timer: now + (recv_path.recovery.pto() * 3),
2845            });
2846
2847            self.key_phase = !self.key_phase;
2848
2849            qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
2850                let trigger = Some(
2851                    qlog::events::security::KeyUpdateOrRetiredTrigger::RemoteUpdate,
2852                );
2853
2854                let ev_data_client =
2855                    EventData::KeyUpdated(qlog::events::security::KeyUpdated {
2856                        key_type:
2857                            qlog::events::security::KeyType::Client1RttSecret,
2858                        trigger: trigger.clone(),
2859                        ..Default::default()
2860                    });
2861
2862                q.add_event_data_with_instant(ev_data_client, now).ok();
2863
2864                let ev_data_server =
2865                    EventData::KeyUpdated(qlog::events::security::KeyUpdated {
2866                        key_type:
2867                            qlog::events::security::KeyType::Server1RttSecret,
2868                        trigger,
2869                        ..Default::default()
2870                    });
2871
2872                q.add_event_data_with_instant(ev_data_server, now).ok();
2873            });
2874        }
2875
2876        if !self.is_server && !self.got_peer_conn_id {
2877            if self.odcid.is_none() {
2878                self.odcid = Some(self.destination_id().into_owned());
2879            }
2880
2881            // Replace the randomly generated destination connection ID with
2882            // the one supplied by the server.
2883            self.set_initial_dcid(
2884                hdr.scid.clone(),
2885                self.peer_transport_params.stateless_reset_token,
2886                recv_pid,
2887            )?;
2888
2889            self.got_peer_conn_id = true;
2890        }
2891
2892        if self.is_server && !self.got_peer_conn_id {
2893            self.set_initial_dcid(hdr.scid.clone(), None, recv_pid)?;
2894
2895            if !self.did_retry {
2896                self.local_transport_params
2897                    .original_destination_connection_id =
2898                    Some(hdr.dcid.to_vec().into());
2899
2900                self.encode_transport_params()?;
2901            }
2902
2903            self.got_peer_conn_id = true;
2904        }
2905
2906        // To avoid sending an ACK in response to an ACK-only packet, we need
2907        // to keep track of whether this packet contains any frame other than
2908        // ACK and PADDING.
2909        let mut ack_elicited = false;
2910
2911        // Process packet payload. If a frame cannot be processed, store the
2912        // error and stop further packet processing.
2913        let mut frame_processing_err = None;
2914
2915        // To know if the peer migrated the connection, we need to keep track
2916        // whether this is a non-probing packet.
2917        let mut probing = true;
2918
2919        // Process packet payload.
2920        while payload.cap() > 0 {
2921            let frame = frame::Frame::from_bytes(&mut payload, hdr.ty)?;
2922
2923            qlog_with_type!(QLOG_PACKET_RX, self.qlog, _q, {
2924                qlog_frames.push(frame.to_qlog());
2925            });
2926
2927            if frame.ack_eliciting() {
2928                ack_elicited = true;
2929            }
2930
2931            if !frame.probing() {
2932                probing = false;
2933            }
2934
2935            if let Err(e) = self.process_frame(frame, &hdr, recv_pid, epoch, now)
2936            {
2937                frame_processing_err = Some(e);
2938                break;
2939            }
2940        }
2941
2942        qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
2943            let packet_size = b.len();
2944
2945            let qlog_pkt_hdr = qlog::events::quic::PacketHeader::with_type(
2946                hdr.ty.to_qlog(),
2947                Some(pn),
2948                Some(hdr.version),
2949                Some(&hdr.scid),
2950                Some(&hdr.dcid),
2951            );
2952
2953            let qlog_raw_info = RawInfo {
2954                length: Some(packet_size as u64),
2955                payload_length: Some(payload_len as u64),
2956                data: None,
2957            };
2958
2959            let ev_data =
2960                EventData::PacketReceived(qlog::events::quic::PacketReceived {
2961                    header: qlog_pkt_hdr,
2962                    frames: Some(qlog_frames),
2963                    raw: Some(qlog_raw_info),
2964                    ..Default::default()
2965                });
2966
2967            q.add_event_data_with_instant(ev_data, now).ok();
2968        });
2969
2970        qlog_with_type!(QLOG_PACKET_RX, self.qlog, q, {
2971            let recv_path = self.paths.get_mut(recv_pid)?;
2972            if let Some(ev_data) = recv_path.recovery.maybe_qlog() {
2973                q.add_event_data_with_instant(ev_data, now).ok();
2974            }
2975        });
2976
2977        if let Some(e) = frame_processing_err {
2978            // Any frame error is terminal, so now just return.
2979            return Err(e);
2980        }
2981
2982        // Only log the remote transport parameters once the connection is
2983        // established (i.e. after frames have been fully parsed) and only
2984        // once per connection.
2985        if self.is_established() {
2986            qlog_with_type!(QLOG_PARAMS_SET, self.qlog, q, {
2987                if !self.qlog.logged_peer_params {
2988                    let ev_data = self
2989                        .peer_transport_params
2990                        .to_qlog(TransportOwner::Remote, self.handshake.cipher());
2991
2992                    q.add_event_data_with_instant(ev_data, now).ok();
2993
2994                    self.qlog.logged_peer_params = true;
2995                }
2996            });
2997        }
2998
2999        // Following flag used to upgrade datagram size, if probe is successful.
3000        let mut pmtud_probe = false;
3001
3002        // Process acked frames. Note that several packets from several paths
3003        // might have been acked by the received packet.
3004        for (_, p) in self.paths.iter_mut() {
3005            for acked in p.recovery.get_acked_frames(epoch) {
3006                match acked {
3007                    frame::Frame::Ping {
3008                        mtu_probe: Some(mtu_probe),
3009                    } => {
3010                        let pmtud_next = p.pmtud.get_current();
3011                        p.pmtud.set_current(cmp::max(pmtud_next, mtu_probe));
3012
3013                        // Stop sending path MTU probes after successful probe.
3014                        p.pmtud.should_probe(false);
3015                        pmtud_probe = true;
3016
3017                        trace!(
3018                            "{} pmtud acked; pmtu size {:?}",
3019                            self.trace_id,
3020                            p.pmtud.get_current()
3021                        );
3022                    },
3023
3024                    frame::Frame::ACK { ranges, .. } => {
3025                        // Stop acknowledging packets less than or equal to the
3026                        // largest acknowledged in the sent ACK frame that, in
3027                        // turn, got acked.
3028                        if let Some(largest_acked) = ranges.last() {
3029                            self.pkt_num_spaces[epoch]
3030                                .recv_pkt_need_ack
3031                                .remove_until(largest_acked);
3032                        }
3033                    },
3034
3035                    frame::Frame::CryptoHeader { offset, length } => {
3036                        self.pkt_num_spaces[epoch]
3037                            .crypto_stream
3038                            .send
3039                            .ack_and_drop(offset, length);
3040                    },
3041
3042                    frame::Frame::StreamHeader {
3043                        stream_id,
3044                        offset,
3045                        length,
3046                        ..
3047                    } => {
3048                        let stream = match self.streams.get_mut(stream_id) {
3049                            Some(v) => v,
3050
3051                            None => continue,
3052                        };
3053
3054                        stream.send.ack_and_drop(offset, length);
3055
3056                        self.tx_buffered =
3057                            self.tx_buffered.saturating_sub(length);
3058
3059                        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
3060                            let ev_data = EventData::DataMoved(
3061                                qlog::events::quic::DataMoved {
3062                                    stream_id: Some(stream_id),
3063                                    offset: Some(offset),
3064                                    length: Some(length as u64),
3065                                    from: Some(DataRecipient::Transport),
3066                                    to: Some(DataRecipient::Dropped),
3067                                    ..Default::default()
3068                                },
3069                            );
3070
3071                            q.add_event_data_with_instant(ev_data, now).ok();
3072                        });
3073
3074                        // Only collect the stream if it is complete and not
3075                        // readable. If it is readable, it will get collected when
3076                        // stream_recv() is used.
3077                        if stream.is_complete() && !stream.is_readable() {
3078                            let local = stream.local;
3079                            self.streams.collect(stream_id, local);
3080                        }
3081                    },
3082
3083                    frame::Frame::HandshakeDone => {
3084                        // Explicitly set this to true, so that if the frame was
3085                        // already scheduled for retransmission, it is aborted.
3086                        self.handshake_done_sent = true;
3087
3088                        self.handshake_done_acked = true;
3089                    },
3090
3091                    frame::Frame::ResetStream { stream_id, .. } => {
3092                        let stream = match self.streams.get_mut(stream_id) {
3093                            Some(v) => v,
3094
3095                            None => continue,
3096                        };
3097
3098                        // Only collect the stream if it is complete and not
3099                        // readable. If it is readable, it will get collected when
3100                        // stream_recv() is used.
3101                        if stream.is_complete() && !stream.is_readable() {
3102                            let local = stream.local;
3103                            self.streams.collect(stream_id, local);
3104                        }
3105                    },
3106
3107                    _ => (),
3108                }
3109            }
3110
3111            // Update max datagram send size with newly acked probe size.
3112            if pmtud_probe {
3113                trace!(
3114                    "{} updating pmtu {:?}",
3115                    p.pmtud.get_current(),
3116                    self.trace_id
3117                );
3118
3119                qlog_with_type!(
3120                    EventType::ConnectivityEventType(
3121                        ConnectivityEventType::MtuUpdated
3122                    ),
3123                    self.qlog,
3124                    q,
3125                    {
3126                        let pmtu_data = EventData::MtuUpdated(
3127                            qlog::events::connectivity::MtuUpdated {
3128                                old: Some(p.recovery.max_datagram_size() as u16),
3129                                new: p.pmtud.get_current() as u16,
3130                                done: Some(pmtud_probe),
3131                            },
3132                        );
3133
3134                        q.add_event_data_with_instant(pmtu_data, now).ok();
3135                    }
3136                );
3137
3138                p.recovery
3139                    .pmtud_update_max_datagram_size(p.pmtud.get_current());
3140            }
3141        }
3142
3143        // Now that we processed all the frames, if there is a path that has no
3144        // Destination CID, try to allocate one.
3145        let no_dcid = self
3146            .paths
3147            .iter_mut()
3148            .filter(|(_, p)| p.active_dcid_seq.is_none());
3149
3150        for (pid, p) in no_dcid {
3151            if self.ids.zero_length_dcid() {
3152                p.active_dcid_seq = Some(0);
3153                continue;
3154            }
3155
3156            let dcid_seq = match self.ids.lowest_available_dcid_seq() {
3157                Some(seq) => seq,
3158                None => break,
3159            };
3160
3161            self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
3162
3163            p.active_dcid_seq = Some(dcid_seq);
3164        }
3165
3166        // We only record the time of arrival of the largest packet number
3167        // that still needs to be acked, to be used for ACK delay calculation.
3168        if self.pkt_num_spaces[epoch].recv_pkt_need_ack.last() < Some(pn) {
3169            self.pkt_num_spaces[epoch].largest_rx_pkt_time = now;
3170        }
3171
3172        self.pkt_num_spaces[epoch].recv_pkt_num.insert(pn);
3173
3174        self.pkt_num_spaces[epoch].recv_pkt_need_ack.push_item(pn);
3175
3176        self.pkt_num_spaces[epoch].ack_elicited =
3177            cmp::max(self.pkt_num_spaces[epoch].ack_elicited, ack_elicited);
3178
3179        self.pkt_num_spaces[epoch].largest_rx_pkt_num =
3180            cmp::max(self.pkt_num_spaces[epoch].largest_rx_pkt_num, pn);
3181
3182        if !probing {
3183            self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num = cmp::max(
3184                self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num,
3185                pn,
3186            );
3187
3188            // Did the peer migrated to another path?
3189            let active_path_id = self.paths.get_active_path_id()?;
3190
3191            if self.is_server &&
3192                recv_pid != active_path_id &&
3193                self.pkt_num_spaces[epoch].largest_rx_non_probing_pkt_num == pn
3194            {
3195                self.on_peer_migrated(recv_pid, self.disable_dcid_reuse, now)?;
3196            }
3197        }
3198
3199        if let Some(idle_timeout) = self.idle_timeout() {
3200            self.idle_timer = Some(now + idle_timeout);
3201        }
3202
3203        // Update send capacity.
3204        self.update_tx_cap();
3205
3206        self.recv_count += 1;
3207        self.paths.get_mut(recv_pid)?.recv_count += 1;
3208
3209        let read = b.off() + aead_tag_len;
3210
3211        self.recv_bytes += read as u64;
3212        self.paths.get_mut(recv_pid)?.recv_bytes += read as u64;
3213
3214        // An Handshake packet has been received from the client and has been
3215        // successfully processed, so we can drop the initial state and consider
3216        // the client's address to be verified.
3217        if self.is_server && hdr.ty == packet::Type::Handshake {
3218            self.drop_epoch_state(packet::Epoch::Initial, now);
3219
3220            self.paths.get_mut(recv_pid)?.verified_peer_address = true;
3221        }
3222
3223        self.ack_eliciting_sent = false;
3224
3225        Ok(read)
3226    }
3227
3228    /// Writes a single QUIC packet to be sent to the peer.
3229    ///
3230    /// On success the number of bytes written to the output buffer is
3231    /// returned, or [`Done`] if there was nothing to write.
3232    ///
3233    /// The application should call `send()` multiple times until [`Done`] is
3234    /// returned, indicating that there are no more packets to send. It is
3235    /// recommended that `send()` be called in the following cases:
3236    ///
3237    ///  * When the application receives QUIC packets from the peer (that is,
3238    ///    any time [`recv()`] is also called).
3239    ///
3240    ///  * When the connection timer expires (that is, any time [`on_timeout()`]
3241    ///    is also called).
3242    ///
3243    ///  * When the application sends data to the peer (for example, any time
3244    ///    [`stream_send()`] or [`stream_shutdown()`] are called).
3245    ///
3246    ///  * When the application receives data from the peer (for example any
3247    ///    time [`stream_recv()`] is called).
3248    ///
3249    /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3250    /// `send()` and all calls will return [`Done`].
3251    ///
3252    /// [`Done`]: enum.Error.html#variant.Done
3253    /// [`recv()`]: struct.Connection.html#method.recv
3254    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3255    /// [`stream_send()`]: struct.Connection.html#method.stream_send
3256    /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3257    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3258    /// [`is_draining()`]: struct.Connection.html#method.is_draining
3259    ///
3260    /// ## Examples:
3261    ///
3262    /// ```no_run
3263    /// # let mut out = [0; 512];
3264    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3265    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3266    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3267    /// # let peer = "127.0.0.1:1234".parse().unwrap();
3268    /// # let local = socket.local_addr().unwrap();
3269    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3270    /// loop {
3271    ///     let (write, send_info) = match conn.send(&mut out) {
3272    ///         Ok(v) => v,
3273    ///
3274    ///         Err(quiche::Error::Done) => {
3275    ///             // Done writing.
3276    ///             break;
3277    ///         },
3278    ///
3279    ///         Err(e) => {
3280    ///             // An error occurred, handle it.
3281    ///             break;
3282    ///         },
3283    ///     };
3284    ///
3285    ///     socket.send_to(&out[..write], &send_info.to).unwrap();
3286    /// }
3287    /// # Ok::<(), quiche::Error>(())
3288    /// ```
3289    pub fn send(&mut self, out: &mut [u8]) -> Result<(usize, SendInfo)> {
3290        self.send_on_path(out, None, None)
3291    }
3292
3293    /// Writes a single QUIC packet to be sent to the peer from the specified
3294    /// local address `from` to the destination address `to`.
3295    ///
3296    /// The behavior of this method differs depending on the value of the `from`
3297    /// and `to` parameters:
3298    ///
3299    ///  * If both are `Some`, then the method only consider the 4-tuple
3300    ///    (`from`, `to`). Application can monitor the 4-tuple availability,
3301    ///    either by monitoring [`path_event_next()`] events or by relying on
3302    ///    the [`paths_iter()`] method. If the provided 4-tuple does not exist
3303    ///    on the connection (anymore), it returns an [`InvalidState`].
3304    ///
3305    ///  * If `from` is `Some` and `to` is `None`, then the method only
3306    ///    considers sending packets on paths having `from` as local address.
3307    ///
3308    ///  * If `to` is `Some` and `from` is `None`, then the method only
3309    ///    considers sending packets on paths having `to` as peer address.
3310    ///
3311    ///  * If both are `None`, all available paths are considered.
3312    ///
3313    /// On success the number of bytes written to the output buffer is
3314    /// returned, or [`Done`] if there was nothing to write.
3315    ///
3316    /// The application should call `send_on_path()` multiple times until
3317    /// [`Done`] is returned, indicating that there are no more packets to
3318    /// send. It is recommended that `send_on_path()` be called in the
3319    /// following cases:
3320    ///
3321    ///  * When the application receives QUIC packets from the peer (that is,
3322    ///    any time [`recv()`] is also called).
3323    ///
3324    ///  * When the connection timer expires (that is, any time [`on_timeout()`]
3325    ///    is also called).
3326    ///
3327    ///  * When the application sends data to the peer (for examples, any time
3328    ///    [`stream_send()`] or [`stream_shutdown()`] are called).
3329    ///
3330    ///  * When the application receives data from the peer (for example any
3331    ///    time [`stream_recv()`] is called).
3332    ///
3333    /// Once [`is_draining()`] returns `true`, it is no longer necessary to call
3334    /// `send_on_path()` and all calls will return [`Done`].
3335    ///
3336    /// [`Done`]: enum.Error.html#variant.Done
3337    /// [`InvalidState`]: enum.Error.html#InvalidState
3338    /// [`recv()`]: struct.Connection.html#method.recv
3339    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
3340    /// [`stream_send()`]: struct.Connection.html#method.stream_send
3341    /// [`stream_shutdown()`]: struct.Connection.html#method.stream_shutdown
3342    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
3343    /// [`path_event_next()`]: struct.Connection.html#method.path_event_next
3344    /// [`paths_iter()`]: struct.Connection.html#method.paths_iter
3345    /// [`is_draining()`]: struct.Connection.html#method.is_draining
3346    ///
3347    /// ## Examples:
3348    ///
3349    /// ```no_run
3350    /// # let mut out = [0; 512];
3351    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
3352    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
3353    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
3354    /// # let peer = "127.0.0.1:1234".parse().unwrap();
3355    /// # let local = socket.local_addr().unwrap();
3356    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
3357    /// loop {
3358    ///     let (write, send_info) = match conn.send_on_path(&mut out, Some(local), Some(peer)) {
3359    ///         Ok(v) => v,
3360    ///
3361    ///         Err(quiche::Error::Done) => {
3362    ///             // Done writing.
3363    ///             break;
3364    ///         },
3365    ///
3366    ///         Err(e) => {
3367    ///             // An error occurred, handle it.
3368    ///             break;
3369    ///         },
3370    ///     };
3371    ///
3372    ///     socket.send_to(&out[..write], &send_info.to).unwrap();
3373    /// }
3374    /// # Ok::<(), quiche::Error>(())
3375    /// ```
3376    pub fn send_on_path(
3377        &mut self, out: &mut [u8], from: Option<SocketAddr>,
3378        to: Option<SocketAddr>,
3379    ) -> Result<(usize, SendInfo)> {
3380        if out.is_empty() {
3381            return Err(Error::BufferTooShort);
3382        }
3383
3384        if self.is_closed() || self.is_draining() {
3385            return Err(Error::Done);
3386        }
3387
3388        let now = time::Instant::now();
3389
3390        if self.local_error.is_none() {
3391            self.do_handshake(now)?;
3392        }
3393
3394        // Forwarding the error value here could confuse
3395        // applications, as they may not expect getting a `recv()`
3396        // error when calling `send()`.
3397        //
3398        // We simply fall-through to sending packets, which should
3399        // take care of terminating the connection as needed.
3400        let _ = self.process_undecrypted_0rtt_packets();
3401
3402        // There's no point in trying to send a packet if the Initial secrets
3403        // have not been derived yet, so return early.
3404        if !self.derived_initial_secrets {
3405            return Err(Error::Done);
3406        }
3407
3408        let mut has_initial = false;
3409
3410        let mut done = 0;
3411
3412        // Limit output packet size to respect the sender and receiver's
3413        // maximum UDP payload size limit.
3414        let mut left = cmp::min(out.len(), self.max_send_udp_payload_size());
3415
3416        let send_pid = match (from, to) {
3417            (Some(f), Some(t)) => self
3418                .paths
3419                .path_id_from_addrs(&(f, t))
3420                .ok_or(Error::InvalidState)?,
3421
3422            _ => self.get_send_path_id(from, to)?,
3423        };
3424
3425        let send_path = self.paths.get_mut(send_pid)?;
3426
3427        // Update max datagram size to allow path MTU discovery probe to be sent.
3428        if send_path.pmtud.get_probe_status() {
3429            let size = if self.handshake_confirmed || self.handshake_done_sent {
3430                send_path.pmtud.get_probe_size()
3431            } else {
3432                send_path.pmtud.get_current()
3433            };
3434
3435            send_path.recovery.pmtud_update_max_datagram_size(size);
3436
3437            left = cmp::min(out.len(), send_path.recovery.max_datagram_size());
3438        }
3439
3440        // Limit data sent by the server based on the amount of data received
3441        // from the client before its address is validated.
3442        if !send_path.verified_peer_address && self.is_server {
3443            left = cmp::min(left, send_path.max_send_bytes);
3444        }
3445
3446        // Generate coalesced packets.
3447        while left > 0 {
3448            let (ty, written) = match self.send_single(
3449                &mut out[done..done + left],
3450                send_pid,
3451                has_initial,
3452                now,
3453            ) {
3454                Ok(v) => v,
3455
3456                Err(Error::BufferTooShort) | Err(Error::Done) => break,
3457
3458                Err(e) => return Err(e),
3459            };
3460
3461            done += written;
3462            left -= written;
3463
3464            match ty {
3465                packet::Type::Initial => has_initial = true,
3466
3467                // No more packets can be coalesced after a 1-RTT.
3468                packet::Type::Short => break,
3469
3470                _ => (),
3471            };
3472
3473            // When sending multiple PTO probes, don't coalesce them together,
3474            // so they are sent on separate UDP datagrams.
3475            if let Ok(epoch) = ty.to_epoch() {
3476                if self.paths.get_mut(send_pid)?.recovery.loss_probes(epoch) > 0 {
3477                    break;
3478                }
3479            }
3480
3481            // Don't coalesce packets that must go on different paths.
3482            if !(from.is_some() && to.is_some()) &&
3483                self.get_send_path_id(from, to)? != send_pid
3484            {
3485                break;
3486            }
3487        }
3488
3489        if done == 0 {
3490            self.last_tx_data = self.tx_data;
3491
3492            return Err(Error::Done);
3493        }
3494
3495        // Pad UDP datagram if it contains a QUIC Initial packet.
3496        #[cfg(not(feature = "fuzzing"))]
3497        if has_initial && left > 0 && done < MIN_CLIENT_INITIAL_LEN {
3498            let pad_len = cmp::min(left, MIN_CLIENT_INITIAL_LEN - done);
3499
3500            // Fill padding area with null bytes, to avoid leaking information
3501            // in case the application reuses the packet buffer.
3502            out[done..done + pad_len].fill(0);
3503
3504            done += pad_len;
3505        }
3506
3507        let send_path = self.paths.get(send_pid)?;
3508
3509        let info = SendInfo {
3510            from: send_path.local_addr(),
3511            to: send_path.peer_addr(),
3512
3513            at: send_path.recovery.get_packet_send_time(),
3514        };
3515
3516        Ok((done, info))
3517    }
3518
3519    fn send_single(
3520        &mut self, out: &mut [u8], send_pid: usize, has_initial: bool,
3521        now: time::Instant,
3522    ) -> Result<(packet::Type, usize)> {
3523        if out.is_empty() {
3524            return Err(Error::BufferTooShort);
3525        }
3526
3527        if self.is_draining() {
3528            return Err(Error::Done);
3529        }
3530
3531        let is_closing = self.local_error.is_some();
3532
3533        let out_len = out.len();
3534
3535        let mut b = octets::OctetsMut::with_slice(out);
3536
3537        let pkt_type = self.write_pkt_type(send_pid)?;
3538
3539        let max_dgram_len = if !self.dgram_send_queue.is_empty() {
3540            self.dgram_max_writable_len()
3541        } else {
3542            None
3543        };
3544
3545        let epoch = pkt_type.to_epoch()?;
3546        let pkt_space = &mut self.pkt_num_spaces[epoch];
3547
3548        // Process lost frames. There might be several paths having lost frames.
3549        for (_, p) in self.paths.iter_mut() {
3550            for lost in p.recovery.get_lost_frames(epoch) {
3551                match lost {
3552                    frame::Frame::CryptoHeader { offset, length } => {
3553                        pkt_space.crypto_stream.send.retransmit(offset, length);
3554
3555                        self.stream_retrans_bytes += length as u64;
3556                        p.stream_retrans_bytes += length as u64;
3557
3558                        self.retrans_count += 1;
3559                        p.retrans_count += 1;
3560                    },
3561
3562                    frame::Frame::StreamHeader {
3563                        stream_id,
3564                        offset,
3565                        length,
3566                        fin,
3567                    } => {
3568                        let stream = match self.streams.get_mut(stream_id) {
3569                            Some(v) => v,
3570
3571                            None => continue,
3572                        };
3573
3574                        let was_flushable = stream.is_flushable();
3575
3576                        let empty_fin = length == 0 && fin;
3577
3578                        stream.send.retransmit(offset, length);
3579
3580                        // If the stream is now flushable push it to the
3581                        // flushable queue, but only if it wasn't already
3582                        // queued.
3583                        //
3584                        // Consider the stream flushable also when we are
3585                        // sending a zero-length frame that has the fin flag
3586                        // set.
3587                        if (stream.is_flushable() || empty_fin) && !was_flushable
3588                        {
3589                            let priority_key = Arc::clone(&stream.priority_key);
3590                            self.streams.insert_flushable(&priority_key);
3591                        }
3592
3593                        self.stream_retrans_bytes += length as u64;
3594                        p.stream_retrans_bytes += length as u64;
3595
3596                        self.retrans_count += 1;
3597                        p.retrans_count += 1;
3598                    },
3599
3600                    frame::Frame::ACK { .. } => {
3601                        pkt_space.ack_elicited = true;
3602                    },
3603
3604                    frame::Frame::ResetStream {
3605                        stream_id,
3606                        error_code,
3607                        final_size,
3608                    } =>
3609                        if self.streams.get(stream_id).is_some() {
3610                            self.streams
3611                                .insert_reset(stream_id, error_code, final_size);
3612                        },
3613
3614                    // Retransmit HANDSHAKE_DONE only if it hasn't been acked at
3615                    // least once already.
3616                    frame::Frame::HandshakeDone if !self.handshake_done_acked => {
3617                        self.handshake_done_sent = false;
3618                    },
3619
3620                    frame::Frame::MaxStreamData { stream_id, .. } => {
3621                        if self.streams.get(stream_id).is_some() {
3622                            self.streams.insert_almost_full(stream_id);
3623                        }
3624                    },
3625
3626                    frame::Frame::MaxData { .. } => {
3627                        self.almost_full = true;
3628                    },
3629
3630                    frame::Frame::NewConnectionId { seq_num, .. } => {
3631                        self.ids.mark_advertise_new_scid_seq(seq_num, true);
3632                    },
3633
3634                    frame::Frame::RetireConnectionId { seq_num } => {
3635                        self.ids.mark_retire_dcid_seq(seq_num, true)?;
3636                    },
3637
3638                    frame::Frame::Ping { mtu_probe } if mtu_probe.is_some() => {
3639                        p.pmtud.pmtu_probe_lost();
3640                    },
3641
3642                    _ => (),
3643                }
3644            }
3645        }
3646
3647        let is_app_limited = self.delivery_rate_check_if_app_limited();
3648        let n_paths = self.paths.len();
3649        let path = self.paths.get_mut(send_pid)?;
3650        let flow_control = &mut self.flow_control;
3651        let pkt_space = &mut self.pkt_num_spaces[epoch];
3652
3653        let mut left = if path.pmtud.is_enabled() {
3654            // Limit output buffer size by estimated path MTU.
3655            cmp::min(path.pmtud.get_current(), b.cap())
3656        } else {
3657            b.cap()
3658        };
3659
3660        let pn = self.next_pkt_num;
3661        let largest_acked_pkt =
3662            path.recovery.get_largest_acked_on_epoch(epoch).unwrap_or(0);
3663        let pn_len = packet::pkt_num_len(pn, largest_acked_pkt);
3664
3665        // The AEAD overhead at the current encryption level.
3666        let crypto_overhead = pkt_space.crypto_overhead().ok_or(Error::Done)?;
3667
3668        let dcid_seq = path.active_dcid_seq.ok_or(Error::OutOfIdentifiers)?;
3669
3670        let dcid =
3671            ConnectionId::from_ref(self.ids.get_dcid(dcid_seq)?.cid.as_ref());
3672
3673        let scid = if let Some(scid_seq) = path.active_scid_seq {
3674            ConnectionId::from_ref(self.ids.get_scid(scid_seq)?.cid.as_ref())
3675        } else if pkt_type == packet::Type::Short {
3676            ConnectionId::default()
3677        } else {
3678            return Err(Error::InvalidState);
3679        };
3680
3681        let hdr = Header {
3682            ty: pkt_type,
3683
3684            version: self.version,
3685
3686            dcid,
3687            scid,
3688
3689            pkt_num: 0,
3690            pkt_num_len: pn_len,
3691
3692            // Only clone token for Initial packets, as other packets don't have
3693            // this field (Retry doesn't count, as it's not encoded as part of
3694            // this code path).
3695            token: if pkt_type == packet::Type::Initial {
3696                self.token.clone()
3697            } else {
3698                None
3699            },
3700
3701            versions: None,
3702            key_phase: self.key_phase,
3703        };
3704
3705        hdr.to_bytes(&mut b)?;
3706
3707        let hdr_trace = if log::max_level() == log::LevelFilter::Trace {
3708            Some(format!("{hdr:?}"))
3709        } else {
3710            None
3711        };
3712
3713        let hdr_ty = hdr.ty;
3714
3715        #[cfg(feature = "qlog")]
3716        let qlog_pkt_hdr = self.qlog.streamer.as_ref().map(|_q| {
3717            qlog::events::quic::PacketHeader::with_type(
3718                hdr.ty.to_qlog(),
3719                Some(pn),
3720                Some(hdr.version),
3721                Some(&hdr.scid),
3722                Some(&hdr.dcid),
3723            )
3724        });
3725
3726        // Calculate the space required for the packet, including the header
3727        // the payload length, the packet number and the AEAD overhead.
3728        let mut overhead = b.off() + pn_len + crypto_overhead;
3729
3730        // We assume that the payload length, which is only present in long
3731        // header packets, can always be encoded with a 2-byte varint.
3732        if pkt_type != packet::Type::Short {
3733            overhead += PAYLOAD_LENGTH_LEN;
3734        }
3735
3736        // Make sure we have enough space left for the packet overhead.
3737        match left.checked_sub(overhead) {
3738            Some(v) => left = v,
3739
3740            None => {
3741                // We can't send more because there isn't enough space available
3742                // in the output buffer.
3743                //
3744                // This usually happens when we try to send a new packet but
3745                // failed because cwnd is almost full. In such case app_limited
3746                // is set to false here to make cwnd grow when ACK is received.
3747                path.recovery.update_app_limited(false);
3748                return Err(Error::Done);
3749            },
3750        }
3751
3752        // Make sure there is enough space for the minimum payload length.
3753        if left < PAYLOAD_MIN_LEN {
3754            path.recovery.update_app_limited(false);
3755            return Err(Error::Done);
3756        }
3757
3758        let mut frames: SmallVec<[frame::Frame; 1]> = SmallVec::new();
3759
3760        let mut ack_eliciting = false;
3761        let mut in_flight = false;
3762        // Foll. flag used to upgrade datagram size, if probe successful
3763        let mut pmtud_probe = false;
3764        let mut has_data = false;
3765
3766        // Whether or not we should explicitly elicit an ACK via PING frame if we
3767        // implicitly elicit one otherwise.
3768        let ack_elicit_required = path.recovery.should_elicit_ack(epoch);
3769
3770        let header_offset = b.off();
3771
3772        // Reserve space for payload length in advance. Since we don't yet know
3773        // what the final length will be, we reserve 2 bytes in all cases.
3774        //
3775        // Only long header packets have an explicit length field.
3776        if pkt_type != packet::Type::Short {
3777            b.skip(PAYLOAD_LENGTH_LEN)?;
3778        }
3779
3780        packet::encode_pkt_num(pn, pn_len, &mut b)?;
3781
3782        let payload_offset = b.off();
3783
3784        let cwnd_available =
3785            path.recovery.cwnd_available().saturating_sub(overhead);
3786
3787        let left_before_packing_ack_frame = left;
3788
3789        // Create ACK frame.
3790        //
3791        // When we need to explicitly elicit an ACK via PING later, go ahead and
3792        // generate an ACK (if there's anything to ACK) since we're going to
3793        // send a packet with PING anyways, even if we haven't received anything
3794        // ACK eliciting.
3795        if pkt_space.recv_pkt_need_ack.len() > 0 &&
3796            (pkt_space.ack_elicited || ack_elicit_required) &&
3797            (!is_closing ||
3798                (pkt_type == Type::Handshake &&
3799                    self.local_error
3800                        .as_ref()
3801                        .is_some_and(|le| le.is_app))) &&
3802            path.active()
3803        {
3804            let ack_delay = pkt_space.largest_rx_pkt_time.elapsed();
3805
3806            let ack_delay = ack_delay.as_micros() as u64 /
3807                2_u64
3808                    .pow(self.local_transport_params.ack_delay_exponent as u32);
3809
3810            let frame = frame::Frame::ACK {
3811                ack_delay,
3812                ranges: pkt_space.recv_pkt_need_ack.clone(),
3813                ecn_counts: None, // sending ECN is not supported at this time
3814            };
3815
3816            // When a PING frame needs to be sent, avoid sending the ACK if
3817            // there is not enough cwnd available for both (note that PING
3818            // frames are always 1 byte, so we just need to check that the
3819            // ACK's length is lower than cwnd).
3820            if pkt_space.ack_elicited || frame.wire_len() < cwnd_available {
3821                // ACK-only packets are not congestion controlled so ACKs must
3822                // be bundled considering the buffer capacity only, and not the
3823                // available cwnd.
3824                if push_frame_to_pkt!(b, frames, frame, left) {
3825                    pkt_space.ack_elicited = false;
3826                }
3827            }
3828        }
3829
3830        // Limit output packet size by congestion window size.
3831        left = cmp::min(
3832            left,
3833            // Bytes consumed by ACK frames.
3834            cwnd_available.saturating_sub(left_before_packing_ack_frame - left),
3835        );
3836
3837        let mut challenge_data = None;
3838
3839        let active_path = self.paths.get_active_mut()?;
3840
3841        if pkt_type == packet::Type::Short {
3842            // Create PMTUD probe.
3843            //
3844            // In order to send a PMTUD probe the current `left` value, which was
3845            // already limited by the current PMTU measure, needs to be ignored,
3846            // but the outgoing packet still needs to be limited by
3847            // the output buffer size, as well as the congestion
3848            // window.
3849            //
3850            // In addition, the PMTUD probe is only generated when the handshake
3851            // is confirmed, to avoid interfering with the handshake
3852            // (e.g. due to the anti-amplification limits).
3853
3854            let pmtu_probe = active_path.should_send_pmtu_probe(
3855                self.handshake_confirmed,
3856                self.handshake_done_sent,
3857                out_len,
3858                is_closing,
3859                frames.is_empty(),
3860            );
3861
3862            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,
3863                    active_path.recovery.cwnd_available(), out_len, left);
3864
3865            if pmtu_probe {
3866                trace!(
3867                    "{} sending pmtud probe pmtu_probe={} next_size={} pmtu={}",
3868                    self.trace_id,
3869                    active_path.pmtud.get_probe_size(),
3870                    active_path.pmtud.get_probe_status(),
3871                    active_path.pmtud.get_current(),
3872                );
3873
3874                left = active_path.pmtud.get_probe_size();
3875
3876                match left.checked_sub(overhead) {
3877                    Some(v) => left = v,
3878
3879                    None => {
3880                        // We can't send more because there isn't enough space
3881                        // available in the output buffer.
3882                        //
3883                        // This usually happens when we try to send a new packet
3884                        // but failed because cwnd is almost full.
3885                        //
3886                        // In such case app_limited is set to false here to make
3887                        // cwnd grow when ACK is received.
3888                        active_path.recovery.update_app_limited(false);
3889                        return Err(Error::Done);
3890                    },
3891                }
3892
3893                let frame = frame::Frame::Padding {
3894                    len: active_path.pmtud.get_probe_size() - overhead - 1,
3895                };
3896
3897                if push_frame_to_pkt!(b, frames, frame, left) {
3898                    let frame = frame::Frame::Ping {
3899                        mtu_probe: Some(active_path.pmtud.get_probe_size()),
3900                    };
3901
3902                    if push_frame_to_pkt!(b, frames, frame, left) {
3903                        ack_eliciting = true;
3904                        in_flight = true;
3905                    }
3906                }
3907
3908                pmtud_probe = true;
3909            }
3910
3911            let path = self.paths.get_mut(send_pid)?;
3912            // Create PATH_RESPONSE frame if needed.
3913            // We do not try to ensure that these are really sent.
3914            while let Some(challenge) = path.pop_received_challenge() {
3915                let frame = frame::Frame::PathResponse { data: challenge };
3916
3917                if push_frame_to_pkt!(b, frames, frame, left) {
3918                    ack_eliciting = true;
3919                    in_flight = true;
3920                } else {
3921                    // If there are other pending PATH_RESPONSE, don't lose them
3922                    // now.
3923                    break;
3924                }
3925            }
3926
3927            // Create PATH_CHALLENGE frame if needed.
3928            if path.validation_requested() {
3929                // TODO: ensure that data is unique over paths.
3930                let data = rand::rand_u64().to_be_bytes();
3931
3932                let frame = frame::Frame::PathChallenge { data };
3933
3934                if push_frame_to_pkt!(b, frames, frame, left) {
3935                    // Let's notify the path once we know the packet size.
3936                    challenge_data = Some(data);
3937
3938                    ack_eliciting = true;
3939                    in_flight = true;
3940                }
3941            }
3942
3943            if let Some(key_update) = pkt_space.key_update.as_mut() {
3944                key_update.update_acked = true;
3945            }
3946        }
3947
3948        let path = self.paths.get_mut(send_pid)?;
3949
3950        if pkt_type == packet::Type::Short && !is_closing {
3951            // Create NEW_CONNECTION_ID frames as needed.
3952            while let Some(seq_num) = self.ids.next_advertise_new_scid_seq() {
3953                let frame = self.ids.get_new_connection_id_frame_for(seq_num)?;
3954
3955                if push_frame_to_pkt!(b, frames, frame, left) {
3956                    self.ids.mark_advertise_new_scid_seq(seq_num, false);
3957
3958                    ack_eliciting = true;
3959                    in_flight = true;
3960                } else {
3961                    break;
3962                }
3963            }
3964        }
3965
3966        if pkt_type == packet::Type::Short && !is_closing && path.active() {
3967            // Create HANDSHAKE_DONE frame.
3968            // self.should_send_handshake_done() but without the need to borrow
3969            if self.handshake_completed &&
3970                !self.handshake_done_sent &&
3971                self.is_server
3972            {
3973                let frame = frame::Frame::HandshakeDone;
3974
3975                if push_frame_to_pkt!(b, frames, frame, left) {
3976                    self.handshake_done_sent = true;
3977
3978                    ack_eliciting = true;
3979                    in_flight = true;
3980                }
3981            }
3982
3983            // Create MAX_STREAMS_BIDI frame.
3984            if self.streams.should_update_max_streams_bidi() {
3985                let frame = frame::Frame::MaxStreamsBidi {
3986                    max: self.streams.max_streams_bidi_next(),
3987                };
3988
3989                if push_frame_to_pkt!(b, frames, frame, left) {
3990                    self.streams.update_max_streams_bidi();
3991
3992                    ack_eliciting = true;
3993                    in_flight = true;
3994                }
3995            }
3996
3997            // Create MAX_STREAMS_UNI frame.
3998            if self.streams.should_update_max_streams_uni() {
3999                let frame = frame::Frame::MaxStreamsUni {
4000                    max: self.streams.max_streams_uni_next(),
4001                };
4002
4003                if push_frame_to_pkt!(b, frames, frame, left) {
4004                    self.streams.update_max_streams_uni();
4005
4006                    ack_eliciting = true;
4007                    in_flight = true;
4008                }
4009            }
4010
4011            // Create DATA_BLOCKED frame.
4012            if let Some(limit) = self.blocked_limit {
4013                let frame = frame::Frame::DataBlocked { limit };
4014
4015                if push_frame_to_pkt!(b, frames, frame, left) {
4016                    self.blocked_limit = None;
4017
4018                    ack_eliciting = true;
4019                    in_flight = true;
4020                }
4021            }
4022
4023            // Create MAX_STREAM_DATA frames as needed.
4024            for stream_id in self.streams.almost_full() {
4025                let stream = match self.streams.get_mut(stream_id) {
4026                    Some(v) => v,
4027
4028                    None => {
4029                        // The stream doesn't exist anymore, so remove it from
4030                        // the almost full set.
4031                        self.streams.remove_almost_full(stream_id);
4032                        continue;
4033                    },
4034                };
4035
4036                // Autotune the stream window size.
4037                stream.recv.autotune_window(now, path.recovery.rtt());
4038
4039                let frame = frame::Frame::MaxStreamData {
4040                    stream_id,
4041                    max: stream.recv.max_data_next(),
4042                };
4043
4044                if push_frame_to_pkt!(b, frames, frame, left) {
4045                    let recv_win = stream.recv.window();
4046
4047                    stream.recv.update_max_data(now);
4048
4049                    self.streams.remove_almost_full(stream_id);
4050
4051                    ack_eliciting = true;
4052                    in_flight = true;
4053
4054                    // Make sure the connection window always has some
4055                    // room compared to the stream window.
4056                    flow_control.ensure_window_lower_bound(
4057                        (recv_win as f64 * CONNECTION_WINDOW_FACTOR) as u64,
4058                    );
4059
4060                    // Also send MAX_DATA when MAX_STREAM_DATA is sent, to avoid a
4061                    // potential race condition.
4062                    self.almost_full = true;
4063                }
4064            }
4065
4066            // Create MAX_DATA frame as needed.
4067            if self.almost_full &&
4068                flow_control.max_data() < flow_control.max_data_next()
4069            {
4070                // Autotune the connection window size.
4071                flow_control.autotune_window(now, path.recovery.rtt());
4072
4073                let frame = frame::Frame::MaxData {
4074                    max: flow_control.max_data_next(),
4075                };
4076
4077                if push_frame_to_pkt!(b, frames, frame, left) {
4078                    self.almost_full = false;
4079
4080                    // Commits the new max_rx_data limit.
4081                    flow_control.update_max_data(now);
4082
4083                    ack_eliciting = true;
4084                    in_flight = true;
4085                }
4086            }
4087
4088            // Create STOP_SENDING frames as needed.
4089            for (stream_id, error_code) in self
4090                .streams
4091                .stopped()
4092                .map(|(&k, &v)| (k, v))
4093                .collect::<Vec<(u64, u64)>>()
4094            {
4095                let frame = frame::Frame::StopSending {
4096                    stream_id,
4097                    error_code,
4098                };
4099
4100                if push_frame_to_pkt!(b, frames, frame, left) {
4101                    self.streams.remove_stopped(stream_id);
4102
4103                    ack_eliciting = true;
4104                    in_flight = true;
4105                }
4106            }
4107
4108            // Create RESET_STREAM frames as needed.
4109            for (stream_id, (error_code, final_size)) in self
4110                .streams
4111                .reset()
4112                .map(|(&k, &v)| (k, v))
4113                .collect::<Vec<(u64, (u64, u64))>>()
4114            {
4115                let frame = frame::Frame::ResetStream {
4116                    stream_id,
4117                    error_code,
4118                    final_size,
4119                };
4120
4121                if push_frame_to_pkt!(b, frames, frame, left) {
4122                    self.streams.remove_reset(stream_id);
4123
4124                    ack_eliciting = true;
4125                    in_flight = true;
4126                }
4127            }
4128
4129            // Create STREAM_DATA_BLOCKED frames as needed.
4130            for (stream_id, limit) in self
4131                .streams
4132                .blocked()
4133                .map(|(&k, &v)| (k, v))
4134                .collect::<Vec<(u64, u64)>>()
4135            {
4136                let frame = frame::Frame::StreamDataBlocked { stream_id, limit };
4137
4138                if push_frame_to_pkt!(b, frames, frame, left) {
4139                    self.streams.remove_blocked(stream_id);
4140
4141                    ack_eliciting = true;
4142                    in_flight = true;
4143                }
4144            }
4145
4146            // Create RETIRE_CONNECTION_ID frames as needed.
4147            while let Some(seq_num) = self.ids.next_retire_dcid_seq() {
4148                // The sequence number specified in a RETIRE_CONNECTION_ID frame
4149                // MUST NOT refer to the Destination Connection ID field of the
4150                // packet in which the frame is contained.
4151                let dcid_seq = path.active_dcid_seq.ok_or(Error::InvalidState)?;
4152
4153                if seq_num == dcid_seq {
4154                    continue;
4155                }
4156
4157                let frame = frame::Frame::RetireConnectionId { seq_num };
4158
4159                if push_frame_to_pkt!(b, frames, frame, left) {
4160                    self.ids.mark_retire_dcid_seq(seq_num, false)?;
4161
4162                    ack_eliciting = true;
4163                    in_flight = true;
4164                } else {
4165                    break;
4166                }
4167            }
4168        }
4169
4170        // Create CONNECTION_CLOSE frame. Try to send this only on the active
4171        // path, unless it is the last one available.
4172        if path.active() || n_paths == 1 {
4173            if let Some(conn_err) = self.local_error.as_ref() {
4174                if conn_err.is_app {
4175                    // Create ApplicationClose frame.
4176                    if pkt_type == packet::Type::Short {
4177                        let frame = frame::Frame::ApplicationClose {
4178                            error_code: conn_err.error_code,
4179                            reason: conn_err.reason.clone(),
4180                        };
4181
4182                        if push_frame_to_pkt!(b, frames, frame, left) {
4183                            let pto = path.recovery.pto();
4184                            self.draining_timer = Some(now + (pto * 3));
4185
4186                            ack_eliciting = true;
4187                            in_flight = true;
4188                        }
4189                    }
4190                } else {
4191                    // Create ConnectionClose frame.
4192                    let frame = frame::Frame::ConnectionClose {
4193                        error_code: conn_err.error_code,
4194                        frame_type: 0,
4195                        reason: conn_err.reason.clone(),
4196                    };
4197
4198                    if push_frame_to_pkt!(b, frames, frame, left) {
4199                        let pto = path.recovery.pto();
4200                        self.draining_timer = Some(now + (pto * 3));
4201
4202                        ack_eliciting = true;
4203                        in_flight = true;
4204                    }
4205                }
4206            }
4207        }
4208
4209        // Create CRYPTO frame.
4210        if pkt_space.crypto_stream.is_flushable() &&
4211            left > frame::MAX_CRYPTO_OVERHEAD &&
4212            !is_closing &&
4213            path.active()
4214        {
4215            let crypto_off = pkt_space.crypto_stream.send.off_front();
4216
4217            // Encode the frame.
4218            //
4219            // Instead of creating a `frame::Frame` object, encode the frame
4220            // directly into the packet buffer.
4221            //
4222            // First we reserve some space in the output buffer for writing the
4223            // frame header (we assume the length field is always a 2-byte
4224            // varint as we don't know the value yet).
4225            //
4226            // Then we emit the data from the crypto stream's send buffer.
4227            //
4228            // Finally we go back and encode the frame header with the now
4229            // available information.
4230            let hdr_off = b.off();
4231            let hdr_len = 1 + // frame type
4232                octets::varint_len(crypto_off) + // offset
4233                2; // length, always encode as 2-byte varint
4234
4235            if let Some(max_len) = left.checked_sub(hdr_len) {
4236                let (mut crypto_hdr, mut crypto_payload) =
4237                    b.split_at(hdr_off + hdr_len)?;
4238
4239                // Write stream data into the packet buffer.
4240                let (len, _) = pkt_space
4241                    .crypto_stream
4242                    .send
4243                    .emit(&mut crypto_payload.as_mut()[..max_len])?;
4244
4245                // Encode the frame's header.
4246                //
4247                // Due to how `OctetsMut::split_at()` works, `crypto_hdr` starts
4248                // from the initial offset of `b` (rather than the current
4249                // offset), so it needs to be advanced to the
4250                // initial frame offset.
4251                crypto_hdr.skip(hdr_off)?;
4252
4253                frame::encode_crypto_header(
4254                    crypto_off,
4255                    len as u64,
4256                    &mut crypto_hdr,
4257                )?;
4258
4259                // Advance the packet buffer's offset.
4260                b.skip(hdr_len + len)?;
4261
4262                let frame = frame::Frame::CryptoHeader {
4263                    offset: crypto_off,
4264                    length: len,
4265                };
4266
4267                if push_frame_to_pkt!(b, frames, frame, left) {
4268                    ack_eliciting = true;
4269                    in_flight = true;
4270                    has_data = true;
4271                }
4272            }
4273        }
4274
4275        // The preference of data-bearing frame to include in a packet
4276        // is managed by `self.emit_dgram`. However, whether any frames
4277        // can be sent depends on the state of their buffers. In the case
4278        // where one type is preferred but its buffer is empty, fall back
4279        // to the other type in order not to waste this function call.
4280        let mut dgram_emitted = false;
4281        let dgrams_to_emit = max_dgram_len.is_some();
4282        let stream_to_emit = self.streams.has_flushable();
4283
4284        let mut do_dgram = self.emit_dgram && dgrams_to_emit;
4285        let do_stream = !self.emit_dgram && stream_to_emit;
4286
4287        if !do_stream && dgrams_to_emit {
4288            do_dgram = true;
4289        }
4290
4291        // Create DATAGRAM frame.
4292        if (pkt_type == packet::Type::Short || pkt_type == packet::Type::ZeroRTT) &&
4293            left > frame::MAX_DGRAM_OVERHEAD &&
4294            !is_closing &&
4295            path.active() &&
4296            do_dgram
4297        {
4298            if let Some(max_dgram_payload) = max_dgram_len {
4299                while let Some(len) = self.dgram_send_queue.peek_front_len() {
4300                    let hdr_off = b.off();
4301                    let hdr_len = 1 + // frame type
4302                        2; // length, always encode as 2-byte varint
4303
4304                    if (hdr_len + len) <= left {
4305                        // Front of the queue fits this packet, send it.
4306                        match self.dgram_send_queue.pop() {
4307                            Some(data) => {
4308                                // Encode the frame.
4309                                //
4310                                // Instead of creating a `frame::Frame` object,
4311                                // encode the frame directly into the packet
4312                                // buffer.
4313                                //
4314                                // First we reserve some space in the output
4315                                // buffer for writing the frame header (we
4316                                // assume the length field is always a 2-byte
4317                                // varint as we don't know the value yet).
4318                                //
4319                                // Then we emit the data from the DATAGRAM's
4320                                // buffer.
4321                                //
4322                                // Finally we go back and encode the frame
4323                                // header with the now available information.
4324                                let (mut dgram_hdr, mut dgram_payload) =
4325                                    b.split_at(hdr_off + hdr_len)?;
4326
4327                                dgram_payload.as_mut()[..len]
4328                                    .copy_from_slice(&data);
4329
4330                                // Encode the frame's header.
4331                                //
4332                                // Due to how `OctetsMut::split_at()` works,
4333                                // `dgram_hdr` starts from the initial offset
4334                                // of `b` (rather than the current offset), so
4335                                // it needs to be advanced to the initial frame
4336                                // offset.
4337                                dgram_hdr.skip(hdr_off)?;
4338
4339                                frame::encode_dgram_header(
4340                                    len as u64,
4341                                    &mut dgram_hdr,
4342                                )?;
4343
4344                                // Advance the packet buffer's offset.
4345                                b.skip(hdr_len + len)?;
4346
4347                                let frame =
4348                                    frame::Frame::DatagramHeader { length: len };
4349
4350                                if push_frame_to_pkt!(b, frames, frame, left) {
4351                                    ack_eliciting = true;
4352                                    in_flight = true;
4353                                    dgram_emitted = true;
4354                                    let _ =
4355                                        self.dgram_sent_count.saturating_add(1);
4356                                    let _ =
4357                                        path.dgram_sent_count.saturating_add(1);
4358                                }
4359                            },
4360
4361                            None => continue,
4362                        };
4363                    } else if len > max_dgram_payload {
4364                        // This dgram frame will never fit. Let's purge it.
4365                        self.dgram_send_queue.pop();
4366                    } else {
4367                        break;
4368                    }
4369                }
4370            }
4371        }
4372
4373        // Create a single STREAM frame for the first stream that is flushable.
4374        if (pkt_type == packet::Type::Short || pkt_type == packet::Type::ZeroRTT) &&
4375            left > frame::MAX_STREAM_OVERHEAD &&
4376            !is_closing &&
4377            path.active() &&
4378            !dgram_emitted
4379        {
4380            while let Some(priority_key) = self.streams.peek_flushable() {
4381                let stream_id = priority_key.id;
4382                let stream = match self.streams.get_mut(stream_id) {
4383                    // Avoid sending frames for streams that were already stopped.
4384                    //
4385                    // This might happen if stream data was buffered but not yet
4386                    // flushed on the wire when a STOP_SENDING frame is received.
4387                    Some(v) if !v.send.is_stopped() => v,
4388                    _ => {
4389                        self.streams.remove_flushable(&priority_key);
4390                        continue;
4391                    },
4392                };
4393
4394                let stream_off = stream.send.off_front();
4395
4396                // Encode the frame.
4397                //
4398                // Instead of creating a `frame::Frame` object, encode the frame
4399                // directly into the packet buffer.
4400                //
4401                // First we reserve some space in the output buffer for writing
4402                // the frame header (we assume the length field is always a
4403                // 2-byte varint as we don't know the value yet).
4404                //
4405                // Then we emit the data from the stream's send buffer.
4406                //
4407                // Finally we go back and encode the frame header with the now
4408                // available information.
4409                let hdr_off = b.off();
4410                let hdr_len = 1 + // frame type
4411                    octets::varint_len(stream_id) + // stream_id
4412                    octets::varint_len(stream_off) + // offset
4413                    2; // length, always encode as 2-byte varint
4414
4415                let max_len = match left.checked_sub(hdr_len) {
4416                    Some(v) => v,
4417                    None => {
4418                        let priority_key = Arc::clone(&stream.priority_key);
4419                        self.streams.remove_flushable(&priority_key);
4420
4421                        continue;
4422                    },
4423                };
4424
4425                let (mut stream_hdr, mut stream_payload) =
4426                    b.split_at(hdr_off + hdr_len)?;
4427
4428                // Write stream data into the packet buffer.
4429                let (len, fin) =
4430                    stream.send.emit(&mut stream_payload.as_mut()[..max_len])?;
4431
4432                // Encode the frame's header.
4433                //
4434                // Due to how `OctetsMut::split_at()` works, `stream_hdr` starts
4435                // from the initial offset of `b` (rather than the current
4436                // offset), so it needs to be advanced to the initial frame
4437                // offset.
4438                stream_hdr.skip(hdr_off)?;
4439
4440                frame::encode_stream_header(
4441                    stream_id,
4442                    stream_off,
4443                    len as u64,
4444                    fin,
4445                    &mut stream_hdr,
4446                )?;
4447
4448                // Advance the packet buffer's offset.
4449                b.skip(hdr_len + len)?;
4450
4451                let frame = frame::Frame::StreamHeader {
4452                    stream_id,
4453                    offset: stream_off,
4454                    length: len,
4455                    fin,
4456                };
4457
4458                if push_frame_to_pkt!(b, frames, frame, left) {
4459                    ack_eliciting = true;
4460                    in_flight = true;
4461                    has_data = true;
4462                }
4463
4464                let priority_key = Arc::clone(&stream.priority_key);
4465                // If the stream is no longer flushable, remove it from the queue
4466                if !stream.is_flushable() {
4467                    self.streams.remove_flushable(&priority_key);
4468                } else if stream.incremental {
4469                    // Shuffle the incremental stream to the back of the
4470                    // queue.
4471                    self.streams.remove_flushable(&priority_key);
4472                    self.streams.insert_flushable(&priority_key);
4473                }
4474
4475                #[cfg(feature = "fuzzing")]
4476                // Coalesce STREAM frames when fuzzing.
4477                if left > frame::MAX_STREAM_OVERHEAD {
4478                    continue;
4479                }
4480
4481                break;
4482            }
4483        }
4484
4485        // Alternate trying to send DATAGRAMs next time.
4486        self.emit_dgram = !dgram_emitted;
4487
4488        // If no other ack-eliciting frame is sent, include a PING frame
4489        // - if PTO probe needed; OR
4490        // - if we've sent too many non ack-eliciting packets without having
4491        // sent an ACK eliciting one; OR
4492        // - the application requested an ack-eliciting frame be sent.
4493        if (ack_elicit_required || path.needs_ack_eliciting) &&
4494            !ack_eliciting &&
4495            left >= 1 &&
4496            !is_closing
4497        {
4498            let frame = frame::Frame::Ping { mtu_probe: None };
4499
4500            if push_frame_to_pkt!(b, frames, frame, left) {
4501                ack_eliciting = true;
4502                in_flight = true;
4503            }
4504        }
4505
4506        if ack_eliciting && !pmtud_probe {
4507            path.needs_ack_eliciting = false;
4508            path.recovery.ping_sent(epoch);
4509        }
4510
4511        if frames.is_empty() {
4512            // When we reach this point we are not able to write more, so set
4513            // app_limited to false.
4514            path.recovery.update_app_limited(false);
4515            return Err(Error::Done);
4516        }
4517
4518        // When coalescing a 1-RTT packet, we can't add padding in the UDP
4519        // datagram, so use PADDING frames instead.
4520        //
4521        // This is only needed if
4522        // 1) an Initial packet has already been written to the UDP datagram,
4523        // as Initial always requires padding.
4524        //
4525        // 2) this is a probing packet towards an unvalidated peer address.
4526        if (has_initial || !path.validated()) &&
4527            pkt_type == packet::Type::Short &&
4528            left >= 1
4529        {
4530            let frame = frame::Frame::Padding { len: left };
4531
4532            if push_frame_to_pkt!(b, frames, frame, left) {
4533                in_flight = true;
4534            }
4535        }
4536
4537        // Pad payload so that it's always at least 4 bytes.
4538        if b.off() - payload_offset < PAYLOAD_MIN_LEN {
4539            let payload_len = b.off() - payload_offset;
4540
4541            let frame = frame::Frame::Padding {
4542                len: PAYLOAD_MIN_LEN - payload_len,
4543            };
4544
4545            #[allow(unused_assignments)]
4546            if push_frame_to_pkt!(b, frames, frame, left) {
4547                in_flight = true;
4548            }
4549        }
4550
4551        let payload_len = b.off() - payload_offset;
4552
4553        // Fill in payload length.
4554        if pkt_type != packet::Type::Short {
4555            let len = pn_len + payload_len + crypto_overhead;
4556
4557            let (_, mut payload_with_len) = b.split_at(header_offset)?;
4558            payload_with_len
4559                .put_varint_with_len(len as u64, PAYLOAD_LENGTH_LEN)?;
4560        }
4561
4562        trace!(
4563            "{} tx pkt {} len={} pn={} {}",
4564            self.trace_id,
4565            hdr_trace.unwrap_or_default(),
4566            payload_len,
4567            pn,
4568            AddrTupleFmt(path.local_addr(), path.peer_addr())
4569        );
4570
4571        #[cfg(feature = "qlog")]
4572        let mut qlog_frames: SmallVec<
4573            [qlog::events::quic::QuicFrame; 1],
4574        > = SmallVec::with_capacity(frames.len());
4575
4576        for frame in &mut frames {
4577            trace!("{} tx frm {:?}", self.trace_id, frame);
4578
4579            qlog_with_type!(QLOG_PACKET_TX, self.qlog, _q, {
4580                qlog_frames.push(frame.to_qlog());
4581            });
4582        }
4583
4584        qlog_with_type!(QLOG_PACKET_TX, self.qlog, q, {
4585            if let Some(header) = qlog_pkt_hdr {
4586                // Qlog packet raw info described at
4587                // https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema-00#section-5.1
4588                //
4589                // `length` includes packet headers and trailers (AEAD tag).
4590                let length = payload_len + payload_offset + crypto_overhead;
4591                let qlog_raw_info = RawInfo {
4592                    length: Some(length as u64),
4593                    payload_length: Some(payload_len as u64),
4594                    data: None,
4595                };
4596
4597                let send_at_time =
4598                    now.duration_since(q.start_time()).as_secs_f32() * 1000.0;
4599
4600                let ev_data =
4601                    EventData::PacketSent(qlog::events::quic::PacketSent {
4602                        header,
4603                        frames: Some(qlog_frames),
4604                        raw: Some(qlog_raw_info),
4605                        send_at_time: Some(send_at_time),
4606                        ..Default::default()
4607                    });
4608
4609                q.add_event_data_with_instant(ev_data, now).ok();
4610            }
4611        });
4612
4613        let aead = match pkt_space.crypto_seal {
4614            Some(ref v) => v,
4615            None => return Err(Error::InvalidState),
4616        };
4617
4618        let written = packet::encrypt_pkt(
4619            &mut b,
4620            pn,
4621            pn_len,
4622            payload_len,
4623            payload_offset,
4624            None,
4625            aead,
4626        )?;
4627
4628        let sent_pkt = recovery::Sent {
4629            pkt_num: pn,
4630            frames,
4631            time_sent: now,
4632            time_acked: None,
4633            time_lost: None,
4634            size: if ack_eliciting { written } else { 0 },
4635            ack_eliciting,
4636            in_flight,
4637            delivered: 0,
4638            delivered_time: now,
4639            first_sent_time: now,
4640            is_app_limited: false,
4641            tx_in_flight: 0,
4642            lost: 0,
4643            has_data,
4644            pmtud: pmtud_probe,
4645        };
4646
4647        if in_flight && is_app_limited {
4648            path.recovery.delivery_rate_update_app_limited(true);
4649        }
4650
4651        self.next_pkt_num += 1;
4652
4653        let handshake_status = recovery::HandshakeStatus {
4654            has_handshake_keys: self.pkt_num_spaces[packet::Epoch::Handshake]
4655                .has_keys(),
4656            peer_verified_address: self.peer_verified_initial_address,
4657            completed: self.handshake_completed,
4658        };
4659
4660        path.recovery.on_packet_sent(
4661            sent_pkt,
4662            epoch,
4663            handshake_status,
4664            now,
4665            &self.trace_id,
4666        );
4667
4668        qlog_with_type!(QLOG_METRICS, self.qlog, q, {
4669            if let Some(ev_data) = path.recovery.maybe_qlog() {
4670                q.add_event_data_with_instant(ev_data, now).ok();
4671            }
4672        });
4673
4674        // Record sent packet size if we probe the path.
4675        if let Some(data) = challenge_data {
4676            path.add_challenge_sent(data, written, now);
4677        }
4678
4679        self.sent_count += 1;
4680        self.sent_bytes += written as u64;
4681        path.sent_count += 1;
4682        path.sent_bytes += written as u64;
4683
4684        if self.dgram_send_queue.byte_size() > path.recovery.cwnd_available() {
4685            path.recovery.update_app_limited(false);
4686        }
4687
4688        path.max_send_bytes = path.max_send_bytes.saturating_sub(written);
4689
4690        // On the client, drop initial state after sending an Handshake packet.
4691        if !self.is_server && hdr_ty == packet::Type::Handshake {
4692            self.drop_epoch_state(packet::Epoch::Initial, now);
4693        }
4694
4695        // (Re)start the idle timer if we are sending the first ack-eliciting
4696        // packet since last receiving a packet.
4697        if ack_eliciting && !self.ack_eliciting_sent {
4698            if let Some(idle_timeout) = self.idle_timeout() {
4699                self.idle_timer = Some(now + idle_timeout);
4700            }
4701        }
4702
4703        if ack_eliciting {
4704            self.ack_eliciting_sent = true;
4705        }
4706
4707        let active_path = self.paths.get_active_mut()?;
4708        if active_path.pmtud.is_enabled() {
4709            active_path
4710                .recovery
4711                .pmtud_update_max_datagram_size(active_path.pmtud.get_current());
4712        }
4713
4714        Ok((pkt_type, written))
4715    }
4716
4717    /// Returns the size of the send quantum, in bytes.
4718    ///
4719    /// This represents the maximum size of a packet burst as determined by the
4720    /// congestion control algorithm in use.
4721    ///
4722    /// Applications can, for example, use it in conjunction with segmentation
4723    /// offloading mechanisms as the maximum limit for outgoing aggregates of
4724    /// multiple packets.
4725    #[inline]
4726    pub fn send_quantum(&self) -> usize {
4727        match self.paths.get_active() {
4728            Ok(p) => p.recovery.send_quantum(),
4729            _ => 0,
4730        }
4731    }
4732
4733    /// Returns the size of the send quantum over the given 4-tuple, in bytes.
4734    ///
4735    /// This represents the maximum size of a packet burst as determined by the
4736    /// congestion control algorithm in use.
4737    ///
4738    /// Applications can, for example, use it in conjunction with segmentation
4739    /// offloading mechanisms as the maximum limit for outgoing aggregates of
4740    /// multiple packets.
4741    ///
4742    /// If the (`local_addr`, peer_addr`) 4-tuple relates to a non-existing
4743    /// path, this method returns 0.
4744    pub fn send_quantum_on_path(
4745        &self, local_addr: SocketAddr, peer_addr: SocketAddr,
4746    ) -> usize {
4747        self.paths
4748            .path_id_from_addrs(&(local_addr, peer_addr))
4749            .and_then(|pid| self.paths.get(pid).ok())
4750            .map(|path| path.recovery.send_quantum())
4751            .unwrap_or(0)
4752    }
4753
4754    /// Reads contiguous data from a stream into the provided slice.
4755    ///
4756    /// The slice must be sized by the caller and will be populated up to its
4757    /// capacity.
4758    ///
4759    /// On success the amount of bytes read and a flag indicating the fin state
4760    /// is returned as a tuple, or [`Done`] if there is no data to read.
4761    ///
4762    /// Reading data from a stream may trigger queueing of control messages
4763    /// (e.g. MAX_STREAM_DATA). [`send()`] should be called after reading.
4764    ///
4765    /// [`Done`]: enum.Error.html#variant.Done
4766    /// [`send()`]: struct.Connection.html#method.send
4767    ///
4768    /// ## Examples:
4769    ///
4770    /// ```no_run
4771    /// # let mut buf = [0; 512];
4772    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
4773    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
4774    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
4775    /// # let peer = "127.0.0.1:1234".parse().unwrap();
4776    /// # let local = socket.local_addr().unwrap();
4777    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
4778    /// # let stream_id = 0;
4779    /// while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
4780    ///     println!("Got {} bytes on stream {}", read, stream_id);
4781    /// }
4782    /// # Ok::<(), quiche::Error>(())
4783    /// ```
4784    pub fn stream_recv(
4785        &mut self, stream_id: u64, out: &mut [u8],
4786    ) -> Result<(usize, bool)> {
4787        // We can't read on our own unidirectional streams.
4788        if !stream::is_bidi(stream_id) &&
4789            stream::is_local(stream_id, self.is_server)
4790        {
4791            return Err(Error::InvalidStreamState(stream_id));
4792        }
4793
4794        let stream = self
4795            .streams
4796            .get_mut(stream_id)
4797            .ok_or(Error::InvalidStreamState(stream_id))?;
4798
4799        if !stream.is_readable() {
4800            return Err(Error::Done);
4801        }
4802
4803        let local = stream.local;
4804        let priority_key = Arc::clone(&stream.priority_key);
4805
4806        #[cfg(feature = "qlog")]
4807        let offset = stream.recv.off_front();
4808
4809        let (read, fin) = match stream.recv.emit(out) {
4810            Ok(v) => v,
4811
4812            Err(e) => {
4813                // Collect the stream if it is now complete. This can happen if
4814                // we got a `StreamReset` error which will now be propagated to
4815                // the application, so we don't need to keep the stream's state
4816                // anymore.
4817                if stream.is_complete() {
4818                    self.streams.collect(stream_id, local);
4819                }
4820
4821                self.streams.remove_readable(&priority_key);
4822                return Err(e);
4823            },
4824        };
4825
4826        self.flow_control.add_consumed(read as u64);
4827
4828        let readable = stream.is_readable();
4829
4830        let complete = stream.is_complete();
4831
4832        if stream.recv.almost_full() {
4833            self.streams.insert_almost_full(stream_id);
4834        }
4835
4836        if !readable {
4837            self.streams.remove_readable(&priority_key);
4838        }
4839
4840        if complete {
4841            self.streams.collect(stream_id, local);
4842        }
4843
4844        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
4845            let ev_data = EventData::DataMoved(qlog::events::quic::DataMoved {
4846                stream_id: Some(stream_id),
4847                offset: Some(offset),
4848                length: Some(read as u64),
4849                from: Some(DataRecipient::Transport),
4850                to: Some(DataRecipient::Application),
4851                ..Default::default()
4852            });
4853
4854            let now = time::Instant::now();
4855            q.add_event_data_with_instant(ev_data, now).ok();
4856        });
4857
4858        if self.should_update_max_data() {
4859            self.almost_full = true;
4860        }
4861
4862        if priority_key.incremental && readable {
4863            // Shuffle the incremental stream to the back of the queue.
4864            self.streams.remove_readable(&priority_key);
4865            self.streams.insert_readable(&priority_key);
4866        }
4867
4868        Ok((read, fin))
4869    }
4870
4871    /// Writes data to a stream.
4872    ///
4873    /// On success the number of bytes written is returned, or [`Done`] if no
4874    /// data was written (e.g. because the stream has no capacity).
4875    ///
4876    /// Applications can provide a 0-length buffer with the fin flag set to
4877    /// true. This will lead to a 0-length FIN STREAM frame being sent at the
4878    /// latest offset. The `Ok(0)` value is only returned when the application
4879    /// provided a 0-length buffer.
4880    ///
4881    /// In addition, if the peer has signalled that it doesn't want to receive
4882    /// any more data from this stream by sending the `STOP_SENDING` frame, the
4883    /// [`StreamStopped`] error will be returned instead of any data.
4884    ///
4885    /// Note that in order to avoid buffering an infinite amount of data in the
4886    /// stream's send buffer, streams are only allowed to buffer outgoing data
4887    /// up to the amount that the peer allows it to send (that is, up to the
4888    /// stream's outgoing flow control capacity).
4889    ///
4890    /// This means that the number of written bytes returned can be lower than
4891    /// the length of the input buffer when the stream doesn't have enough
4892    /// capacity for the operation to complete. The application should retry the
4893    /// operation once the stream is reported as writable again.
4894    ///
4895    /// Applications should call this method only after the handshake is
4896    /// completed (whenever [`is_established()`] returns `true`) or during
4897    /// early data if enabled (whenever [`is_in_early_data()`] returns `true`).
4898    ///
4899    /// [`Done`]: enum.Error.html#variant.Done
4900    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
4901    /// [`is_established()`]: struct.Connection.html#method.is_established
4902    /// [`is_in_early_data()`]: struct.Connection.html#method.is_in_early_data
4903    ///
4904    /// ## Examples:
4905    ///
4906    /// ```no_run
4907    /// # let mut buf = [0; 512];
4908    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
4909    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
4910    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
4911    /// # let peer = "127.0.0.1:1234".parse().unwrap();
4912    /// # let local = "127.0.0.1:4321".parse().unwrap();
4913    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
4914    /// # let stream_id = 0;
4915    /// conn.stream_send(stream_id, b"hello", true)?;
4916    /// # Ok::<(), quiche::Error>(())
4917    /// ```
4918    pub fn stream_send(
4919        &mut self, stream_id: u64, buf: &[u8], fin: bool,
4920    ) -> Result<usize> {
4921        // We can't write on the peer's unidirectional streams.
4922        if !stream::is_bidi(stream_id) &&
4923            !stream::is_local(stream_id, self.is_server)
4924        {
4925            return Err(Error::InvalidStreamState(stream_id));
4926        }
4927
4928        // Mark the connection as blocked if the connection-level flow control
4929        // limit doesn't let us buffer all the data.
4930        //
4931        // Note that this is separate from "send capacity" as that also takes
4932        // congestion control into consideration.
4933        if self.max_tx_data - self.tx_data < buf.len() as u64 {
4934            self.blocked_limit = Some(self.max_tx_data);
4935        }
4936
4937        let cap = self.tx_cap;
4938
4939        // Get existing stream or create a new one.
4940        let stream = self.get_or_create_stream(stream_id, true)?;
4941
4942        #[cfg(feature = "qlog")]
4943        let offset = stream.send.off_back();
4944
4945        let was_writable = stream.is_writable();
4946
4947        let was_flushable = stream.is_flushable();
4948
4949        let priority_key = Arc::clone(&stream.priority_key);
4950
4951        // Truncate the input buffer based on the connection's send capacity if
4952        // necessary.
4953        //
4954        // When the cap is zero, the method returns Ok(0) *only* when the passed
4955        // buffer is empty. We return Error::Done otherwise.
4956        if cap == 0 && !buf.is_empty() {
4957            if was_writable {
4958                // When `stream_writable_next()` returns a stream, the writable
4959                // mark is removed, but because the stream is blocked by the
4960                // connection-level send capacity it won't be marked as writable
4961                // again once the capacity increases.
4962                //
4963                // Since the stream is writable already, mark it here instead.
4964                self.streams.insert_writable(&priority_key);
4965            }
4966
4967            return Err(Error::Done);
4968        }
4969
4970        let (buf, fin, blocked_by_cap) = if cap < buf.len() {
4971            (&buf[..cap], false, true)
4972        } else {
4973            (buf, fin, false)
4974        };
4975
4976        let sent = match stream.send.write(buf, fin) {
4977            Ok(v) => v,
4978
4979            Err(e) => {
4980                self.streams.remove_writable(&priority_key);
4981                return Err(e);
4982            },
4983        };
4984
4985        let incremental = stream.incremental;
4986        let priority_key = Arc::clone(&stream.priority_key);
4987
4988        let flushable = stream.is_flushable();
4989
4990        let writable = stream.is_writable();
4991
4992        let empty_fin = buf.is_empty() && fin;
4993
4994        if sent < buf.len() {
4995            let max_off = stream.send.max_off();
4996
4997            if stream.send.blocked_at() != Some(max_off) {
4998                stream.send.update_blocked_at(Some(max_off));
4999                self.streams.insert_blocked(stream_id, max_off);
5000            }
5001        } else {
5002            stream.send.update_blocked_at(None);
5003            self.streams.remove_blocked(stream_id);
5004        }
5005
5006        // If the stream is now flushable push it to the flushable queue, but
5007        // only if it wasn't already queued.
5008        //
5009        // Consider the stream flushable also when we are sending a zero-length
5010        // frame that has the fin flag set.
5011        if (flushable || empty_fin) && !was_flushable {
5012            self.streams.insert_flushable(&priority_key);
5013        }
5014
5015        if !writable {
5016            self.streams.remove_writable(&priority_key);
5017        } else if was_writable && blocked_by_cap {
5018            // When `stream_writable_next()` returns a stream, the writable
5019            // mark is removed, but because the stream is blocked by the
5020            // connection-level send capacity it won't be marked as writable
5021            // again once the capacity increases.
5022            //
5023            // Since the stream is writable already, mark it here instead.
5024            self.streams.insert_writable(&priority_key);
5025        }
5026
5027        self.tx_cap -= sent;
5028
5029        self.tx_data += sent as u64;
5030
5031        self.tx_buffered += sent;
5032
5033        qlog_with_type!(QLOG_DATA_MV, self.qlog, q, {
5034            let ev_data = EventData::DataMoved(qlog::events::quic::DataMoved {
5035                stream_id: Some(stream_id),
5036                offset: Some(offset),
5037                length: Some(sent as u64),
5038                from: Some(DataRecipient::Application),
5039                to: Some(DataRecipient::Transport),
5040                ..Default::default()
5041            });
5042
5043            let now = time::Instant::now();
5044            q.add_event_data_with_instant(ev_data, now).ok();
5045        });
5046
5047        if sent == 0 && !buf.is_empty() {
5048            return Err(Error::Done);
5049        }
5050
5051        if incremental && writable {
5052            // Shuffle the incremental stream to the back of the queue.
5053            self.streams.remove_writable(&priority_key);
5054            self.streams.insert_writable(&priority_key);
5055        }
5056
5057        Ok(sent)
5058    }
5059
5060    /// Sets the priority for a stream.
5061    ///
5062    /// A stream's priority determines the order in which stream data is sent
5063    /// on the wire (streams with lower priority are sent first). Streams are
5064    /// created with a default priority of `127`.
5065    ///
5066    /// The target stream is created if it did not exist before calling this
5067    /// method.
5068    pub fn stream_priority(
5069        &mut self, stream_id: u64, urgency: u8, incremental: bool,
5070    ) -> Result<()> {
5071        // Get existing stream or create a new one, but if the stream
5072        // has already been closed and collected, ignore the prioritization.
5073        let stream = match self.get_or_create_stream(stream_id, true) {
5074            Ok(v) => v,
5075
5076            Err(Error::Done) => return Ok(()),
5077
5078            Err(e) => return Err(e),
5079        };
5080
5081        if stream.urgency == urgency && stream.incremental == incremental {
5082            return Ok(());
5083        }
5084
5085        stream.urgency = urgency;
5086        stream.incremental = incremental;
5087
5088        let new_priority_key = Arc::new(StreamPriorityKey {
5089            urgency: stream.urgency,
5090            incremental: stream.incremental,
5091            id: stream_id,
5092            ..Default::default()
5093        });
5094
5095        let old_priority_key =
5096            std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
5097
5098        self.streams
5099            .update_priority(&old_priority_key, &new_priority_key);
5100
5101        Ok(())
5102    }
5103
5104    /// Shuts down reading or writing from/to the specified stream.
5105    ///
5106    /// When the `direction` argument is set to [`Shutdown::Read`], outstanding
5107    /// data in the stream's receive buffer is dropped, and no additional data
5108    /// is added to it. Data received after calling this method is still
5109    /// validated and acked but not stored, and [`stream_recv()`] will not
5110    /// return it to the application. In addition, a `STOP_SENDING` frame will
5111    /// be sent to the peer to signal it to stop sending data.
5112    ///
5113    /// When the `direction` argument is set to [`Shutdown::Write`], outstanding
5114    /// data in the stream's send buffer is dropped, and no additional data is
5115    /// added to it. Data passed to [`stream_send()`] after calling this method
5116    /// will be ignored. In addition, a `RESET_STREAM` frame will be sent to the
5117    /// peer to signal the reset.
5118    ///
5119    /// Locally-initiated unidirectional streams can only be closed in the
5120    /// [`Shutdown::Write`] direction. Remotely-initiated unidirectional streams
5121    /// can only be closed in the [`Shutdown::Read`] direction. Using an
5122    /// incorrect direction will return [`InvalidStreamState`].
5123    ///
5124    /// [`Shutdown::Read`]: enum.Shutdown.html#variant.Read
5125    /// [`Shutdown::Write`]: enum.Shutdown.html#variant.Write
5126    /// [`stream_recv()`]: struct.Connection.html#method.stream_recv
5127    /// [`stream_send()`]: struct.Connection.html#method.stream_send
5128    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5129    pub fn stream_shutdown(
5130        &mut self, stream_id: u64, direction: Shutdown, err: u64,
5131    ) -> Result<()> {
5132        // Don't try to stop a local unidirectional stream.
5133        if direction == Shutdown::Read &&
5134            stream::is_local(stream_id, self.is_server) &&
5135            !stream::is_bidi(stream_id)
5136        {
5137            return Err(Error::InvalidStreamState(stream_id));
5138        }
5139
5140        // Don't try to reset a remote unidirectional stream.
5141        if direction == Shutdown::Write &&
5142            !stream::is_local(stream_id, self.is_server) &&
5143            !stream::is_bidi(stream_id)
5144        {
5145            return Err(Error::InvalidStreamState(stream_id));
5146        }
5147
5148        // Get existing stream.
5149        let stream = self.streams.get_mut(stream_id).ok_or(Error::Done)?;
5150
5151        let priority_key = Arc::clone(&stream.priority_key);
5152
5153        match direction {
5154            Shutdown::Read => {
5155                stream.recv.shutdown()?;
5156
5157                if !stream.recv.is_fin() {
5158                    self.streams.insert_stopped(stream_id, err);
5159                }
5160
5161                // Once shutdown, the stream is guaranteed to be non-readable.
5162                self.streams.remove_readable(&priority_key);
5163
5164                self.stopped_stream_local_count =
5165                    self.stopped_stream_local_count.saturating_add(1);
5166            },
5167
5168            Shutdown::Write => {
5169                let (final_size, unsent) = stream.send.shutdown()?;
5170
5171                // Claw back some flow control allowance from data that was
5172                // buffered but not actually sent before the stream was reset.
5173                self.tx_data = self.tx_data.saturating_sub(unsent);
5174
5175                self.tx_buffered =
5176                    self.tx_buffered.saturating_sub(unsent as usize);
5177
5178                // Update send capacity.
5179                self.update_tx_cap();
5180
5181                self.streams.insert_reset(stream_id, err, final_size);
5182
5183                // Once shutdown, the stream is guaranteed to be non-writable.
5184                self.streams.remove_writable(&priority_key);
5185
5186                self.reset_stream_local_count =
5187                    self.reset_stream_local_count.saturating_add(1);
5188            },
5189        }
5190
5191        Ok(())
5192    }
5193
5194    /// Returns the stream's send capacity in bytes.
5195    ///
5196    /// If the specified stream doesn't exist (including when it has already
5197    /// been completed and closed), the [`InvalidStreamState`] error will be
5198    /// returned.
5199    ///
5200    /// In addition, if the peer has signalled that it doesn't want to receive
5201    /// any more data from this stream by sending the `STOP_SENDING` frame, the
5202    /// [`StreamStopped`] error will be returned.
5203    ///
5204    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5205    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
5206    #[inline]
5207    pub fn stream_capacity(&self, stream_id: u64) -> Result<usize> {
5208        if let Some(stream) = self.streams.get(stream_id) {
5209            let cap = cmp::min(self.tx_cap, stream.send.cap()?);
5210            return Ok(cap);
5211        };
5212
5213        Err(Error::InvalidStreamState(stream_id))
5214    }
5215
5216    /// Returns the next stream that has data to read.
5217    ///
5218    /// Note that once returned by this method, a stream ID will not be returned
5219    /// again until it is "re-armed".
5220    ///
5221    /// The application will need to read all of the pending data on the stream,
5222    /// and new data has to be received before the stream is reported again.
5223    ///
5224    /// This is unlike the [`readable()`] method, that returns the same list of
5225    /// readable streams when called multiple times in succession.
5226    ///
5227    /// [`readable()`]: struct.Connection.html#method.readable
5228    pub fn stream_readable_next(&mut self) -> Option<u64> {
5229        let priority_key = self.streams.readable.front().clone_pointer()?;
5230
5231        self.streams.remove_readable(&priority_key);
5232
5233        Some(priority_key.id)
5234    }
5235
5236    /// Returns true if the stream has data that can be read.
5237    pub fn stream_readable(&self, stream_id: u64) -> bool {
5238        let stream = match self.streams.get(stream_id) {
5239            Some(v) => v,
5240
5241            None => return false,
5242        };
5243
5244        stream.is_readable()
5245    }
5246
5247    /// Returns the next stream that can be written to.
5248    ///
5249    /// Note that once returned by this method, a stream ID will not be returned
5250    /// again until it is "re-armed".
5251    ///
5252    /// This is unlike the [`writable()`] method, that returns the same list of
5253    /// writable streams when called multiple times in succession. It is not
5254    /// advised to use both `stream_writable_next()` and [`writable()`] on the
5255    /// same connection, as it may lead to unexpected results.
5256    ///
5257    /// The [`stream_writable()`] method can also be used to fine-tune when a
5258    /// stream is reported as writable again.
5259    ///
5260    /// [`stream_writable()`]: struct.Connection.html#method.stream_writable
5261    /// [`writable()`]: struct.Connection.html#method.writable
5262    pub fn stream_writable_next(&mut self) -> Option<u64> {
5263        // If there is not enough connection-level send capacity, none of the
5264        // streams are writable.
5265        if self.tx_cap == 0 {
5266            return None;
5267        }
5268
5269        let mut cursor = self.streams.writable.front();
5270
5271        while let Some(priority_key) = cursor.clone_pointer() {
5272            if let Some(stream) = self.streams.get(priority_key.id) {
5273                let cap = match stream.send.cap() {
5274                    Ok(v) => v,
5275
5276                    // Return the stream to the application immediately if it's
5277                    // stopped.
5278                    Err(_) =>
5279                        return {
5280                            self.streams.remove_writable(&priority_key);
5281
5282                            Some(priority_key.id)
5283                        },
5284                };
5285
5286                if cmp::min(self.tx_cap, cap) >= stream.send_lowat {
5287                    self.streams.remove_writable(&priority_key);
5288                    return Some(priority_key.id);
5289                }
5290            }
5291
5292            cursor.move_next();
5293        }
5294
5295        None
5296    }
5297
5298    /// Returns true if the stream has enough send capacity.
5299    ///
5300    /// When `len` more bytes can be buffered into the given stream's send
5301    /// buffer, `true` will be returned, `false` otherwise.
5302    ///
5303    /// In the latter case, if the additional data can't be buffered due to
5304    /// flow control limits, the peer will also be notified, and a "low send
5305    /// watermark" will be set for the stream, such that it is not going to be
5306    /// reported as writable again by [`stream_writable_next()`] until its send
5307    /// capacity reaches `len`.
5308    ///
5309    /// If the specified stream doesn't exist (including when it has already
5310    /// been completed and closed), the [`InvalidStreamState`] error will be
5311    /// returned.
5312    ///
5313    /// In addition, if the peer has signalled that it doesn't want to receive
5314    /// any more data from this stream by sending the `STOP_SENDING` frame, the
5315    /// [`StreamStopped`] error will be returned.
5316    ///
5317    /// [`stream_writable_next()`]: struct.Connection.html#method.stream_writable_next
5318    /// [`InvalidStreamState`]: enum.Error.html#variant.InvalidStreamState
5319    /// [`StreamStopped`]: enum.Error.html#variant.StreamStopped
5320    #[inline]
5321    pub fn stream_writable(
5322        &mut self, stream_id: u64, len: usize,
5323    ) -> Result<bool> {
5324        if self.stream_capacity(stream_id)? >= len {
5325            return Ok(true);
5326        }
5327
5328        let stream = match self.streams.get_mut(stream_id) {
5329            Some(v) => v,
5330
5331            None => return Err(Error::InvalidStreamState(stream_id)),
5332        };
5333
5334        stream.send_lowat = cmp::max(1, len);
5335
5336        let is_writable = stream.is_writable();
5337
5338        let priority_key = Arc::clone(&stream.priority_key);
5339
5340        if self.max_tx_data - self.tx_data < len as u64 {
5341            self.blocked_limit = Some(self.max_tx_data);
5342        }
5343
5344        if stream.send.cap()? < len {
5345            let max_off = stream.send.max_off();
5346            if stream.send.blocked_at() != Some(max_off) {
5347                stream.send.update_blocked_at(Some(max_off));
5348                self.streams.insert_blocked(stream_id, max_off);
5349            }
5350        } else if is_writable {
5351            // When `stream_writable_next()` returns a stream, the writable
5352            // mark is removed, but because the stream is blocked by the
5353            // connection-level send capacity it won't be marked as writable
5354            // again once the capacity increases.
5355            //
5356            // Since the stream is writable already, mark it here instead.
5357            self.streams.insert_writable(&priority_key);
5358        }
5359
5360        Ok(false)
5361    }
5362
5363    /// Returns true if all the data has been read from the specified stream.
5364    ///
5365    /// This instructs the application that all the data received from the
5366    /// peer on the stream has been read, and there won't be anymore in the
5367    /// future.
5368    ///
5369    /// Basically this returns true when the peer either set the `fin` flag
5370    /// for the stream, or sent `RESET_STREAM`.
5371    #[inline]
5372    pub fn stream_finished(&self, stream_id: u64) -> bool {
5373        let stream = match self.streams.get(stream_id) {
5374            Some(v) => v,
5375
5376            None => return true,
5377        };
5378
5379        stream.recv.is_fin()
5380    }
5381
5382    /// Returns the number of bidirectional streams that can be created
5383    /// before the peer's stream count limit is reached.
5384    ///
5385    /// This can be useful to know if it's possible to create a bidirectional
5386    /// stream without trying it first.
5387    #[inline]
5388    pub fn peer_streams_left_bidi(&self) -> u64 {
5389        self.streams.peer_streams_left_bidi()
5390    }
5391
5392    /// Returns the number of unidirectional streams that can be created
5393    /// before the peer's stream count limit is reached.
5394    ///
5395    /// This can be useful to know if it's possible to create a unidirectional
5396    /// stream without trying it first.
5397    #[inline]
5398    pub fn peer_streams_left_uni(&self) -> u64 {
5399        self.streams.peer_streams_left_uni()
5400    }
5401
5402    /// Returns an iterator over streams that have outstanding data to read.
5403    ///
5404    /// Note that the iterator will only include streams that were readable at
5405    /// the time the iterator itself was created (i.e. when `readable()` was
5406    /// called). To account for newly readable streams, the iterator needs to
5407    /// be created again.
5408    ///
5409    /// ## Examples:
5410    ///
5411    /// ```no_run
5412    /// # let mut buf = [0; 512];
5413    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5414    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5415    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5416    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5417    /// # let local = socket.local_addr().unwrap();
5418    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5419    /// // Iterate over readable streams.
5420    /// for stream_id in conn.readable() {
5421    ///     // Stream is readable, read until there's no more data.
5422    ///     while let Ok((read, fin)) = conn.stream_recv(stream_id, &mut buf) {
5423    ///         println!("Got {} bytes on stream {}", read, stream_id);
5424    ///     }
5425    /// }
5426    /// # Ok::<(), quiche::Error>(())
5427    /// ```
5428    #[inline]
5429    pub fn readable(&self) -> StreamIter {
5430        self.streams.readable()
5431    }
5432
5433    /// Returns an iterator over streams that can be written in priority order.
5434    ///
5435    /// The priority order is based on RFC 9218 scheduling recommendations.
5436    /// Stream priority can be controlled using [`stream_priority()`]. In order
5437    /// to support fairness requirements, each time this method is called,
5438    /// internal state is updated. Therefore the iterator ordering can change
5439    /// between calls, even if no streams were added or removed.
5440    ///
5441    /// A "writable" stream is a stream that has enough flow control capacity to
5442    /// send data to the peer. To avoid buffering an infinite amount of data,
5443    /// streams are only allowed to buffer outgoing data up to the amount that
5444    /// the peer allows to send.
5445    ///
5446    /// Note that the iterator will only include streams that were writable at
5447    /// the time the iterator itself was created (i.e. when `writable()` was
5448    /// called). To account for newly writable streams, the iterator needs to be
5449    /// created again.
5450    ///
5451    /// ## Examples:
5452    ///
5453    /// ```no_run
5454    /// # let mut buf = [0; 512];
5455    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5456    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5457    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5458    /// # let local = socket.local_addr().unwrap();
5459    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5460    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5461    /// // Iterate over writable streams.
5462    /// for stream_id in conn.writable() {
5463    ///     // Stream is writable, write some data.
5464    ///     if let Ok(written) = conn.stream_send(stream_id, &buf, false) {
5465    ///         println!("Written {} bytes on stream {}", written, stream_id);
5466    ///     }
5467    /// }
5468    /// # Ok::<(), quiche::Error>(())
5469    /// ```
5470    /// [`stream_priority()`]: struct.Connection.html#method.stream_priority
5471    #[inline]
5472    pub fn writable(&self) -> StreamIter {
5473        // If there is not enough connection-level send capacity, none of the
5474        // streams are writable, so return an empty iterator.
5475        if self.tx_cap == 0 {
5476            return StreamIter::default();
5477        }
5478
5479        self.streams.writable()
5480    }
5481
5482    /// Returns the maximum possible size of egress UDP payloads.
5483    ///
5484    /// This is the maximum size of UDP payloads that can be sent, and depends
5485    /// on both the configured maximum send payload size of the local endpoint
5486    /// (as configured with [`set_max_send_udp_payload_size()`]), as well as
5487    /// the transport parameter advertised by the remote peer.
5488    ///
5489    /// Note that this value can change during the lifetime of the connection,
5490    /// but should remain stable across consecutive calls to [`send()`].
5491    ///
5492    /// [`set_max_send_udp_payload_size()`]:
5493    ///     struct.Config.html#method.set_max_send_udp_payload_size
5494    /// [`send()`]: struct.Connection.html#method.send
5495    pub fn max_send_udp_payload_size(&self) -> usize {
5496        let max_datagram_size = self
5497            .paths
5498            .get_active()
5499            .ok()
5500            .map(|p| p.recovery.max_datagram_size());
5501
5502        if let Some(max_datagram_size) = max_datagram_size {
5503            if self.is_established() {
5504                // We cap the maximum packet size to 16KB or so, so that it can be
5505                // always encoded with a 2-byte varint.
5506                return cmp::min(16383, max_datagram_size);
5507            }
5508        }
5509
5510        // Allow for 1200 bytes (minimum QUIC packet size) during the
5511        // handshake.
5512        MIN_CLIENT_INITIAL_LEN
5513    }
5514
5515    /// Schedule an ack-eliciting packet on the active path.
5516    ///
5517    /// QUIC packets might not contain ack-eliciting frames during normal
5518    /// operating conditions. If the packet would already contain
5519    /// ack-eliciting frames, this method does not change any behavior.
5520    /// However, if the packet would not ordinarily contain ack-eliciting
5521    /// frames, this method ensures that a PING frame sent.
5522    ///
5523    /// Calling this method multiple times before [`send()`] has no effect.
5524    ///
5525    /// [`send()`]: struct.Connection.html#method.send
5526    pub fn send_ack_eliciting(&mut self) -> Result<()> {
5527        if self.is_closed() || self.is_draining() {
5528            return Ok(());
5529        }
5530        self.paths.get_active_mut()?.needs_ack_eliciting = true;
5531        Ok(())
5532    }
5533
5534    /// Schedule an ack-eliciting packet on the specified path.
5535    ///
5536    /// See [`send_ack_eliciting()`] for more detail. [`InvalidState`] is
5537    /// returned if there is no record of the path.
5538    ///
5539    /// [`send_ack_eliciting()`]: struct.Connection.html#method.send_ack_eliciting
5540    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
5541    pub fn send_ack_eliciting_on_path(
5542        &mut self, local: SocketAddr, peer: SocketAddr,
5543    ) -> Result<()> {
5544        if self.is_closed() || self.is_draining() {
5545            return Ok(());
5546        }
5547        let path_id = self
5548            .paths
5549            .path_id_from_addrs(&(local, peer))
5550            .ok_or(Error::InvalidState)?;
5551        self.paths.get_mut(path_id)?.needs_ack_eliciting = true;
5552        Ok(())
5553    }
5554
5555    /// Reads the first received DATAGRAM.
5556    ///
5557    /// On success the DATAGRAM's data is returned along with its size.
5558    ///
5559    /// [`Done`] is returned if there is no data to read.
5560    ///
5561    /// [`BufferTooShort`] is returned if the provided buffer is too small for
5562    /// the DATAGRAM.
5563    ///
5564    /// [`Done`]: enum.Error.html#variant.Done
5565    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
5566    ///
5567    /// ## Examples:
5568    ///
5569    /// ```no_run
5570    /// # let mut buf = [0; 512];
5571    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5572    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5573    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5574    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5575    /// # let local = socket.local_addr().unwrap();
5576    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5577    /// let mut dgram_buf = [0; 512];
5578    /// while let Ok((len)) = conn.dgram_recv(&mut dgram_buf) {
5579    ///     println!("Got {} bytes of DATAGRAM", len);
5580    /// }
5581    /// # Ok::<(), quiche::Error>(())
5582    /// ```
5583    #[inline]
5584    pub fn dgram_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
5585        match self.dgram_recv_queue.pop() {
5586            Some(d) => {
5587                if d.len() > buf.len() {
5588                    return Err(Error::BufferTooShort);
5589                }
5590
5591                buf[..d.len()].copy_from_slice(&d);
5592                Ok(d.len())
5593            },
5594
5595            None => Err(Error::Done),
5596        }
5597    }
5598
5599    /// Reads the first received DATAGRAM.
5600    ///
5601    /// This is the same as [`dgram_recv()`] but returns the DATAGRAM as a
5602    /// `Vec<u8>` instead of copying into the provided buffer.
5603    ///
5604    /// [`dgram_recv()`]: struct.Connection.html#method.dgram_recv
5605    #[inline]
5606    pub fn dgram_recv_vec(&mut self) -> Result<Vec<u8>> {
5607        match self.dgram_recv_queue.pop() {
5608            Some(d) => Ok(d),
5609
5610            None => Err(Error::Done),
5611        }
5612    }
5613
5614    /// Reads the first received DATAGRAM without removing it from the queue.
5615    ///
5616    /// On success the DATAGRAM's data is returned along with the actual number
5617    /// of bytes peeked. The requested length cannot exceed the DATAGRAM's
5618    /// actual length.
5619    ///
5620    /// [`Done`] is returned if there is no data to read.
5621    ///
5622    /// [`BufferTooShort`] is returned if the provided buffer is smaller the
5623    /// number of bytes to peek.
5624    ///
5625    /// [`Done`]: enum.Error.html#variant.Done
5626    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
5627    #[inline]
5628    pub fn dgram_recv_peek(&self, buf: &mut [u8], len: usize) -> Result<usize> {
5629        self.dgram_recv_queue.peek_front_bytes(buf, len)
5630    }
5631
5632    /// Returns the length of the first stored DATAGRAM.
5633    #[inline]
5634    pub fn dgram_recv_front_len(&self) -> Option<usize> {
5635        self.dgram_recv_queue.peek_front_len()
5636    }
5637
5638    /// Returns the number of items in the DATAGRAM receive queue.
5639    #[inline]
5640    pub fn dgram_recv_queue_len(&self) -> usize {
5641        self.dgram_recv_queue.len()
5642    }
5643
5644    /// Returns the total size of all items in the DATAGRAM receive queue.
5645    #[inline]
5646    pub fn dgram_recv_queue_byte_size(&self) -> usize {
5647        self.dgram_recv_queue.byte_size()
5648    }
5649
5650    /// Returns the number of items in the DATAGRAM send queue.
5651    #[inline]
5652    pub fn dgram_send_queue_len(&self) -> usize {
5653        self.dgram_send_queue.len()
5654    }
5655
5656    /// Returns the total size of all items in the DATAGRAM send queue.
5657    #[inline]
5658    pub fn dgram_send_queue_byte_size(&self) -> usize {
5659        self.dgram_send_queue.byte_size()
5660    }
5661
5662    /// Returns whether or not the DATAGRAM send queue is full.
5663    #[inline]
5664    pub fn is_dgram_send_queue_full(&self) -> bool {
5665        self.dgram_send_queue.is_full()
5666    }
5667
5668    /// Returns whether or not the DATAGRAM recv queue is full.
5669    #[inline]
5670    pub fn is_dgram_recv_queue_full(&self) -> bool {
5671        self.dgram_recv_queue.is_full()
5672    }
5673
5674    /// Sends data in a DATAGRAM frame.
5675    ///
5676    /// [`Done`] is returned if no data was written.
5677    /// [`InvalidState`] is returned if the peer does not support DATAGRAM.
5678    /// [`BufferTooShort`] is returned if the DATAGRAM frame length is larger
5679    /// than peer's supported DATAGRAM frame length. Use
5680    /// [`dgram_max_writable_len()`] to get the largest supported DATAGRAM
5681    /// frame length.
5682    ///
5683    /// Note that there is no flow control of DATAGRAM frames, so in order to
5684    /// avoid buffering an infinite amount of frames we apply an internal
5685    /// limit.
5686    ///
5687    /// [`Done`]: enum.Error.html#variant.Done
5688    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
5689    /// [`BufferTooShort`]: enum.Error.html#variant.BufferTooShort
5690    /// [`dgram_max_writable_len()`]:
5691    /// struct.Connection.html#method.dgram_max_writable_len
5692    ///
5693    /// ## Examples:
5694    ///
5695    /// ```no_run
5696    /// # let mut buf = [0; 512];
5697    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5698    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5699    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5700    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5701    /// # let local = socket.local_addr().unwrap();
5702    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5703    /// conn.dgram_send(b"hello")?;
5704    /// # Ok::<(), quiche::Error>(())
5705    /// ```
5706    pub fn dgram_send(&mut self, buf: &[u8]) -> Result<()> {
5707        let max_payload_len = match self.dgram_max_writable_len() {
5708            Some(v) => v,
5709
5710            None => return Err(Error::InvalidState),
5711        };
5712
5713        if buf.len() > max_payload_len {
5714            return Err(Error::BufferTooShort);
5715        }
5716
5717        self.dgram_send_queue.push(buf.to_vec())?;
5718
5719        let active_path = self.paths.get_active_mut()?;
5720
5721        if self.dgram_send_queue.byte_size() >
5722            active_path.recovery.cwnd_available()
5723        {
5724            active_path.recovery.update_app_limited(false);
5725        }
5726
5727        Ok(())
5728    }
5729
5730    /// Sends data in a DATAGRAM frame.
5731    ///
5732    /// This is the same as [`dgram_send()`] but takes a `Vec<u8>` instead of
5733    /// a slice.
5734    ///
5735    /// [`dgram_send()`]: struct.Connection.html#method.dgram_send
5736    pub fn dgram_send_vec(&mut self, buf: Vec<u8>) -> Result<()> {
5737        let max_payload_len = match self.dgram_max_writable_len() {
5738            Some(v) => v,
5739
5740            None => return Err(Error::InvalidState),
5741        };
5742
5743        if buf.len() > max_payload_len {
5744            return Err(Error::BufferTooShort);
5745        }
5746
5747        self.dgram_send_queue.push(buf)?;
5748
5749        let active_path = self.paths.get_active_mut()?;
5750
5751        if self.dgram_send_queue.byte_size() >
5752            active_path.recovery.cwnd_available()
5753        {
5754            active_path.recovery.update_app_limited(false);
5755        }
5756
5757        Ok(())
5758    }
5759
5760    /// Purges queued outgoing DATAGRAMs matching the predicate.
5761    ///
5762    /// In other words, remove all elements `e` such that `f(&e)` returns true.
5763    ///
5764    /// ## Examples:
5765    /// ```no_run
5766    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5767    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5768    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5769    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5770    /// # let local = socket.local_addr().unwrap();
5771    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5772    /// conn.dgram_send(b"hello")?;
5773    /// conn.dgram_purge_outgoing(&|d: &[u8]| -> bool { d[0] == 0 });
5774    /// # Ok::<(), quiche::Error>(())
5775    /// ```
5776    #[inline]
5777    pub fn dgram_purge_outgoing<F: Fn(&[u8]) -> bool>(&mut self, f: F) {
5778        self.dgram_send_queue.purge(f);
5779    }
5780
5781    /// Returns the maximum DATAGRAM payload that can be sent.
5782    ///
5783    /// [`None`] is returned if the peer hasn't advertised a maximum DATAGRAM
5784    /// frame size.
5785    ///
5786    /// ## Examples:
5787    ///
5788    /// ```no_run
5789    /// # let mut buf = [0; 512];
5790    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
5791    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
5792    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
5793    /// # let peer = "127.0.0.1:1234".parse().unwrap();
5794    /// # let local = socket.local_addr().unwrap();
5795    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
5796    /// if let Some(payload_size) = conn.dgram_max_writable_len() {
5797    ///     if payload_size > 5 {
5798    ///         conn.dgram_send(b"hello")?;
5799    ///     }
5800    /// }
5801    /// # Ok::<(), quiche::Error>(())
5802    /// ```
5803    #[inline]
5804    pub fn dgram_max_writable_len(&self) -> Option<usize> {
5805        match self.peer_transport_params.max_datagram_frame_size {
5806            None => None,
5807            Some(peer_frame_len) => {
5808                let dcid = self.destination_id();
5809                // Start from the maximum packet size...
5810                let mut max_len = self.max_send_udp_payload_size();
5811                // ...subtract the Short packet header overhead...
5812                // (1 byte of pkt_len + len of dcid)
5813                max_len = max_len.saturating_sub(1 + dcid.len());
5814                // ...subtract the packet number (max len)...
5815                max_len = max_len.saturating_sub(packet::MAX_PKT_NUM_LEN);
5816                // ...subtract the crypto overhead...
5817                max_len = max_len.saturating_sub(
5818                    self.pkt_num_spaces[packet::Epoch::Application]
5819                        .crypto_overhead()?,
5820                );
5821                // ...clamp to what peer can support...
5822                max_len = cmp::min(peer_frame_len as usize, max_len);
5823                // ...subtract frame overhead, checked for underflow.
5824                // (1 byte of frame type + len of length )
5825                max_len.checked_sub(1 + frame::MAX_DGRAM_OVERHEAD)
5826            },
5827        }
5828    }
5829
5830    fn dgram_enabled(&self) -> bool {
5831        self.local_transport_params
5832            .max_datagram_frame_size
5833            .is_some()
5834    }
5835
5836    /// Returns when the next timeout event will occur.
5837    ///
5838    /// Once the timeout Instant has been reached, the [`on_timeout()`] method
5839    /// should be called. A timeout of `None` means that the timer should be
5840    /// disarmed.
5841    ///
5842    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
5843    pub fn timeout_instant(&self) -> Option<time::Instant> {
5844        if self.is_closed() {
5845            return None;
5846        }
5847
5848        if self.is_draining() {
5849            // Draining timer takes precedence over all other timers. If it is
5850            // set it means the connection is closing so there's no point in
5851            // processing the other timers.
5852            self.draining_timer
5853        } else {
5854            // Use the lowest timer value (i.e. "sooner") among idle and loss
5855            // detection timers. If they are both unset (i.e. `None`) then the
5856            // result is `None`, but if at least one of them is set then a
5857            // `Some(...)` value is returned.
5858            let path_timer = self
5859                .paths
5860                .iter()
5861                .filter_map(|(_, p)| p.recovery.loss_detection_timer())
5862                .min();
5863
5864            let key_update_timer = self.pkt_num_spaces
5865                [packet::Epoch::Application]
5866                .key_update
5867                .as_ref()
5868                .map(|key_update| key_update.timer);
5869
5870            let timers = [self.idle_timer, path_timer, key_update_timer];
5871
5872            timers.iter().filter_map(|&x| x).min()
5873        }
5874    }
5875
5876    /// Returns the amount of time until the next timeout event.
5877    ///
5878    /// Once the given duration has elapsed, the [`on_timeout()`] method should
5879    /// be called. A timeout of `None` means that the timer should be disarmed.
5880    ///
5881    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
5882    pub fn timeout(&self) -> Option<time::Duration> {
5883        self.timeout_instant().map(|timeout| {
5884            let now = time::Instant::now();
5885
5886            if timeout <= now {
5887                time::Duration::ZERO
5888            } else {
5889                timeout.duration_since(now)
5890            }
5891        })
5892    }
5893
5894    /// Processes a timeout event.
5895    ///
5896    /// If no timeout has occurred it does nothing.
5897    pub fn on_timeout(&mut self) {
5898        let now = time::Instant::now();
5899
5900        if let Some(draining_timer) = self.draining_timer {
5901            if draining_timer <= now {
5902                trace!("{} draining timeout expired", self.trace_id);
5903
5904                self.mark_closed();
5905            }
5906
5907            // Draining timer takes precedence over all other timers. If it is
5908            // set it means the connection is closing so there's no point in
5909            // processing the other timers.
5910            return;
5911        }
5912
5913        if let Some(timer) = self.idle_timer {
5914            if timer <= now {
5915                trace!("{} idle timeout expired", self.trace_id);
5916
5917                self.mark_closed();
5918                self.timed_out = true;
5919                return;
5920            }
5921        }
5922
5923        if let Some(timer) = self.pkt_num_spaces[packet::Epoch::Application]
5924            .key_update
5925            .as_ref()
5926            .map(|key_update| key_update.timer)
5927        {
5928            if timer <= now {
5929                // Discard previous key once key update timer expired.
5930                let _ = self.pkt_num_spaces[packet::Epoch::Application]
5931                    .key_update
5932                    .take();
5933            }
5934        }
5935
5936        let handshake_status = self.handshake_status();
5937
5938        for (_, p) in self.paths.iter_mut() {
5939            if let Some(timer) = p.recovery.loss_detection_timer() {
5940                if timer <= now {
5941                    trace!("{} loss detection timeout expired", self.trace_id);
5942
5943                    let (lost_packets, lost_bytes) = p.on_loss_detection_timeout(
5944                        handshake_status,
5945                        now,
5946                        self.is_server,
5947                        &self.trace_id,
5948                    );
5949
5950                    self.lost_count += lost_packets;
5951                    self.lost_bytes += lost_bytes as u64;
5952
5953                    qlog_with_type!(QLOG_METRICS, self.qlog, q, {
5954                        if let Some(ev_data) = p.recovery.maybe_qlog() {
5955                            q.add_event_data_with_instant(ev_data, now).ok();
5956                        }
5957                    });
5958                }
5959            }
5960        }
5961
5962        // Notify timeout events to the application.
5963        self.paths.notify_failed_validations();
5964
5965        // If the active path failed, try to find a new candidate.
5966        if self.paths.get_active_path_id().is_err() {
5967            match self.paths.find_candidate_path() {
5968                Some(pid) => {
5969                    if self.set_active_path(pid, now).is_err() {
5970                        // The connection cannot continue.
5971                        self.mark_closed();
5972                    }
5973                },
5974
5975                // The connection cannot continue.
5976                None => {
5977                    self.mark_closed();
5978                },
5979            }
5980        }
5981    }
5982
5983    /// Requests the stack to perform path validation of the proposed 4-tuple.
5984    ///
5985    /// Probing new paths requires spare Connection IDs at both the host and the
5986    /// peer sides. If it is not the case, it raises an [`OutOfIdentifiers`].
5987    ///
5988    /// The probing of new addresses can only be done by the client. The server
5989    /// can only probe network paths that were previously advertised by
5990    /// [`PathEvent::New`]. If the server tries to probe such an unseen network
5991    /// path, this call raises an [`InvalidState`].
5992    ///
5993    /// The caller might also want to probe an existing path. In such case, it
5994    /// triggers a PATH_CHALLENGE frame, but it does not require spare CIDs.
5995    ///
5996    /// A server always probes a new path it observes. Calling this method is
5997    /// hence not required to validate a new path. However, a server can still
5998    /// request an additional path validation of the proposed 4-tuple.
5999    ///
6000    /// Calling this method several times before calling [`send()`] or
6001    /// [`send_on_path()`] results in a single probe being generated. An
6002    /// application wanting to send multiple in-flight probes must call this
6003    /// method again after having sent packets.
6004    ///
6005    /// Returns the Destination Connection ID sequence number associated to that
6006    /// path.
6007    ///
6008    /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
6009    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6010    /// [`InvalidState`]: enum.Error.html#InvalidState
6011    /// [`send()`]: struct.Connection.html#method.send
6012    /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
6013    pub fn probe_path(
6014        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
6015    ) -> Result<u64> {
6016        // We may want to probe an existing path.
6017        let pid = match self.paths.path_id_from_addrs(&(local_addr, peer_addr)) {
6018            Some(pid) => pid,
6019            None => self.create_path_on_client(local_addr, peer_addr)?,
6020        };
6021
6022        let path = self.paths.get_mut(pid)?;
6023        path.request_validation();
6024
6025        path.active_dcid_seq.ok_or(Error::InvalidState)
6026    }
6027
6028    /// Migrates the connection to a new local address `local_addr`.
6029    ///
6030    /// The behavior is similar to [`migrate()`], with the nuance that the
6031    /// connection only changes the local address, but not the peer one.
6032    ///
6033    /// See [`migrate()`] for the full specification of this method.
6034    ///
6035    /// [`migrate()`]: struct.Connection.html#method.migrate
6036    pub fn migrate_source(&mut self, local_addr: SocketAddr) -> Result<u64> {
6037        let peer_addr = self.paths.get_active()?.peer_addr();
6038        self.migrate(local_addr, peer_addr)
6039    }
6040
6041    /// Migrates the connection over the given network path between `local_addr`
6042    /// and `peer_addr`.
6043    ///
6044    /// Connection migration can only be initiated by the client. Calling this
6045    /// method as a server returns [`InvalidState`].
6046    ///
6047    /// To initiate voluntary migration, there should be enough Connection IDs
6048    /// at both sides. If this requirement is not satisfied, this call returns
6049    /// [`OutOfIdentifiers`].
6050    ///
6051    /// Returns the Destination Connection ID associated to that migrated path.
6052    ///
6053    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6054    /// [`InvalidState`]: enum.Error.html#InvalidState
6055    pub fn migrate(
6056        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
6057    ) -> Result<u64> {
6058        if self.is_server {
6059            return Err(Error::InvalidState);
6060        }
6061
6062        // If the path already exists, mark it as the active one.
6063        let (pid, dcid_seq) = if let Some(pid) =
6064            self.paths.path_id_from_addrs(&(local_addr, peer_addr))
6065        {
6066            let path = self.paths.get_mut(pid)?;
6067
6068            // If it is already active, do nothing.
6069            if path.active() {
6070                return path.active_dcid_seq.ok_or(Error::OutOfIdentifiers);
6071            }
6072
6073            // Ensures that a Source Connection ID has been dedicated to this
6074            // path, or a free one is available. This is only required if the
6075            // host uses non-zero length Source Connection IDs.
6076            if !self.ids.zero_length_scid() &&
6077                path.active_scid_seq.is_none() &&
6078                self.ids.available_scids() == 0
6079            {
6080                return Err(Error::OutOfIdentifiers);
6081            }
6082
6083            // Ensures that the migrated path has a Destination Connection ID.
6084            let dcid_seq = if let Some(dcid_seq) = path.active_dcid_seq {
6085                dcid_seq
6086            } else {
6087                let dcid_seq = self
6088                    .ids
6089                    .lowest_available_dcid_seq()
6090                    .ok_or(Error::OutOfIdentifiers)?;
6091
6092                self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
6093                path.active_dcid_seq = Some(dcid_seq);
6094
6095                dcid_seq
6096            };
6097
6098            (pid, dcid_seq)
6099        } else {
6100            let pid = self.create_path_on_client(local_addr, peer_addr)?;
6101
6102            let dcid_seq = self
6103                .paths
6104                .get(pid)?
6105                .active_dcid_seq
6106                .ok_or(Error::InvalidState)?;
6107
6108            (pid, dcid_seq)
6109        };
6110
6111        // Change the active path.
6112        self.set_active_path(pid, time::Instant::now())?;
6113
6114        Ok(dcid_seq)
6115    }
6116
6117    /// Provides additional source Connection IDs that the peer can use to reach
6118    /// this host.
6119    ///
6120    /// This triggers sending NEW_CONNECTION_ID frames if the provided Source
6121    /// Connection ID is not already present. In the case the caller tries to
6122    /// reuse a Connection ID with a different reset token, this raises an
6123    /// `InvalidState`.
6124    ///
6125    /// At any time, the peer cannot have more Destination Connection IDs than
6126    /// the maximum number of active Connection IDs it negotiated. In such case
6127    /// (i.e., when [`scids_left()`] returns 0), if the host agrees to
6128    /// request the removal of previous connection IDs, it sets the
6129    /// `retire_if_needed` parameter. Otherwise, an [`IdLimit`] is returned.
6130    ///
6131    /// Note that setting `retire_if_needed` does not prevent this function from
6132    /// returning an [`IdLimit`] in the case the caller wants to retire still
6133    /// unannounced Connection IDs.
6134    ///
6135    /// The caller is responsible for ensuring that the provided `scid` is not
6136    /// repeated several times over the connection. quiche ensures that as long
6137    /// as the provided Connection ID is still in use (i.e., not retired), it
6138    /// does not assign a different sequence number.
6139    ///
6140    /// Note that if the host uses zero-length Source Connection IDs, it cannot
6141    /// advertise Source Connection IDs and calling this method returns an
6142    /// [`InvalidState`].
6143    ///
6144    /// Returns the sequence number associated to the provided Connection ID.
6145    ///
6146    /// [`scids_left()`]: struct.Connection.html#method.scids_left
6147    /// [`IdLimit`]: enum.Error.html#IdLimit
6148    /// [`InvalidState`]: enum.Error.html#InvalidState
6149    pub fn new_scid(
6150        &mut self, scid: &ConnectionId, reset_token: u128, retire_if_needed: bool,
6151    ) -> Result<u64> {
6152        self.ids.new_scid(
6153            scid.to_vec().into(),
6154            Some(reset_token),
6155            true,
6156            None,
6157            retire_if_needed,
6158        )
6159    }
6160
6161    /// Returns the number of source Connection IDs that are active. This is
6162    /// only meaningful if the host uses non-zero length Source Connection IDs.
6163    pub fn active_scids(&self) -> usize {
6164        self.ids.active_source_cids()
6165    }
6166
6167    /// Returns the number of source Connection IDs that should be provided
6168    /// to the peer without exceeding the limit it advertised.
6169    ///
6170    /// This will automatically limit the number of Connection IDs to the
6171    /// minimum between the locally configured active connection ID limit,
6172    /// and the one sent by the peer.
6173    ///
6174    /// To obtain the maximum possible value allowed by the peer an application
6175    /// can instead inspect the [`peer_active_conn_id_limit`] value.
6176    ///
6177    /// [`peer_active_conn_id_limit`]: struct.Stats.html#structfield.peer_active_conn_id_limit
6178    #[inline]
6179    pub fn scids_left(&self) -> usize {
6180        let max_active_source_cids = cmp::min(
6181            self.peer_transport_params.active_conn_id_limit,
6182            self.local_transport_params.active_conn_id_limit,
6183        ) as usize;
6184
6185        max_active_source_cids - self.active_scids()
6186    }
6187
6188    /// Requests the retirement of the destination Connection ID used by the
6189    /// host to reach its peer.
6190    ///
6191    /// This triggers sending RETIRE_CONNECTION_ID frames.
6192    ///
6193    /// If the application tries to retire a non-existing Destination Connection
6194    /// ID sequence number, or if it uses zero-length Destination Connection ID,
6195    /// this method returns an [`InvalidState`].
6196    ///
6197    /// At any time, the host must have at least one Destination ID. If the
6198    /// application tries to retire the last one, or if the caller tries to
6199    /// retire the destination Connection ID used by the current active path
6200    /// while having neither spare Destination Connection IDs nor validated
6201    /// network paths, this method returns an [`OutOfIdentifiers`]. This
6202    /// behavior prevents the caller from stalling the connection due to the
6203    /// lack of validated path to send non-probing packets.
6204    ///
6205    /// [`InvalidState`]: enum.Error.html#InvalidState
6206    /// [`OutOfIdentifiers`]: enum.Error.html#OutOfIdentifiers
6207    pub fn retire_dcid(&mut self, dcid_seq: u64) -> Result<()> {
6208        if self.ids.zero_length_dcid() {
6209            return Err(Error::InvalidState);
6210        }
6211
6212        let active_path_dcid_seq = self
6213            .paths
6214            .get_active()?
6215            .active_dcid_seq
6216            .ok_or(Error::InvalidState)?;
6217
6218        let active_path_id = self.paths.get_active_path_id()?;
6219
6220        if active_path_dcid_seq == dcid_seq &&
6221            self.ids.lowest_available_dcid_seq().is_none() &&
6222            !self
6223                .paths
6224                .iter()
6225                .any(|(pid, p)| pid != active_path_id && p.usable())
6226        {
6227            return Err(Error::OutOfIdentifiers);
6228        }
6229
6230        if let Some(pid) = self.ids.retire_dcid(dcid_seq)? {
6231            // The retired Destination CID was associated to a given path. Let's
6232            // find an available DCID to associate to that path.
6233            let path = self.paths.get_mut(pid)?;
6234            let dcid_seq = self.ids.lowest_available_dcid_seq();
6235
6236            if let Some(dcid_seq) = dcid_seq {
6237                self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
6238            }
6239
6240            path.active_dcid_seq = dcid_seq;
6241        }
6242
6243        Ok(())
6244    }
6245
6246    /// Processes path-specific events.
6247    ///
6248    /// On success it returns a [`PathEvent`], or `None` when there are no
6249    /// events to report. Please refer to [`PathEvent`] for the exhaustive event
6250    /// list.
6251    ///
6252    /// Note that all events are edge-triggered, meaning that once reported they
6253    /// will not be reported again by calling this method again, until the event
6254    /// is re-armed.
6255    ///
6256    /// [`PathEvent`]: enum.PathEvent.html
6257    pub fn path_event_next(&mut self) -> Option<PathEvent> {
6258        self.paths.pop_event()
6259    }
6260
6261    /// Returns the number of source Connection IDs that are retired.
6262    pub fn retired_scids(&self) -> usize {
6263        self.ids.retired_source_cids()
6264    }
6265
6266    /// Returns a source `ConnectionId` that has been retired.
6267    ///
6268    /// On success it returns a [`ConnectionId`], or `None` when there are no
6269    /// more retired connection IDs.
6270    ///
6271    /// [`ConnectionId`]: struct.ConnectionId.html
6272    pub fn retired_scid_next(&mut self) -> Option<ConnectionId<'static>> {
6273        self.ids.pop_retired_scid()
6274    }
6275
6276    /// Returns the number of spare Destination Connection IDs, i.e.,
6277    /// Destination Connection IDs that are still unused.
6278    ///
6279    /// Note that this function returns 0 if the host uses zero length
6280    /// Destination Connection IDs.
6281    pub fn available_dcids(&self) -> usize {
6282        self.ids.available_dcids()
6283    }
6284
6285    /// Returns an iterator over destination `SockAddr`s whose association
6286    /// with `from` forms a known QUIC path on which packets can be sent to.
6287    ///
6288    /// This function is typically used in combination with [`send_on_path()`].
6289    ///
6290    /// Note that the iterator includes all the possible combination of
6291    /// destination `SockAddr`s, even those whose sending is not required now.
6292    /// In other words, this is another way for the application to recall from
6293    /// past [`PathEvent::New`] events.
6294    ///
6295    /// [`PathEvent::New`]: enum.PathEvent.html#variant.New
6296    /// [`send_on_path()`]: struct.Connection.html#method.send_on_path
6297    ///
6298    /// ## Examples:
6299    ///
6300    /// ```no_run
6301    /// # let mut out = [0; 512];
6302    /// # let socket = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
6303    /// # let mut config = quiche::Config::new(quiche::PROTOCOL_VERSION)?;
6304    /// # let scid = quiche::ConnectionId::from_ref(&[0xba; 16]);
6305    /// # let local = socket.local_addr().unwrap();
6306    /// # let peer = "127.0.0.1:1234".parse().unwrap();
6307    /// # let mut conn = quiche::accept(&scid, None, local, peer, &mut config)?;
6308    /// // Iterate over possible destinations for the given local `SockAddr`.
6309    /// for dest in conn.paths_iter(local) {
6310    ///     loop {
6311    ///         let (write, send_info) =
6312    ///             match conn.send_on_path(&mut out, Some(local), Some(dest)) {
6313    ///                 Ok(v) => v,
6314    ///
6315    ///                 Err(quiche::Error::Done) => {
6316    ///                     // Done writing for this destination.
6317    ///                     break;
6318    ///                 },
6319    ///
6320    ///                 Err(e) => {
6321    ///                     // An error occurred, handle it.
6322    ///                     break;
6323    ///                 },
6324    ///             };
6325    ///
6326    ///         socket.send_to(&out[..write], &send_info.to).unwrap();
6327    ///     }
6328    /// }
6329    /// # Ok::<(), quiche::Error>(())
6330    /// ```
6331    #[inline]
6332    pub fn paths_iter(&self, from: SocketAddr) -> SocketAddrIter {
6333        // Instead of trying to identify whether packets will be sent on the
6334        // given 4-tuple, simply filter paths that cannot be used.
6335        SocketAddrIter {
6336            sockaddrs: self
6337                .paths
6338                .iter()
6339                .filter(|(_, p)| p.active_dcid_seq.is_some())
6340                .filter(|(_, p)| p.usable() || p.probing_required())
6341                .filter(|(_, p)| p.local_addr() == from)
6342                .map(|(_, p)| p.peer_addr())
6343                .collect(),
6344
6345            index: 0,
6346        }
6347    }
6348
6349    /// Closes the connection with the given error and reason.
6350    ///
6351    /// The `app` parameter specifies whether an application close should be
6352    /// sent to the peer. Otherwise a normal connection close is sent.
6353    ///
6354    /// If `app` is true but the connection is not in a state that is safe to
6355    /// send an application error (not established nor in early data), in
6356    /// accordance with [RFC
6357    /// 9000](https://www.rfc-editor.org/rfc/rfc9000.html#section-10.2.3-3), the
6358    /// error code is changed to APPLICATION_ERROR and the reason phrase is
6359    /// cleared.
6360    ///
6361    /// Returns [`Done`] if the connection had already been closed.
6362    ///
6363    /// Note that the connection will not be closed immediately. An application
6364    /// should continue calling the [`recv()`], [`send()`], [`timeout()`] and
6365    /// [`on_timeout()`] methods as normal, until the [`is_closed()`] method
6366    /// returns `true`.
6367    ///
6368    /// [`Done`]: enum.Error.html#variant.Done
6369    /// [`recv()`]: struct.Connection.html#method.recv
6370    /// [`send()`]: struct.Connection.html#method.send
6371    /// [`timeout()`]: struct.Connection.html#method.timeout
6372    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6373    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6374    pub fn close(&mut self, app: bool, err: u64, reason: &[u8]) -> Result<()> {
6375        if self.is_closed() || self.is_draining() {
6376            return Err(Error::Done);
6377        }
6378
6379        if self.local_error.is_some() {
6380            return Err(Error::Done);
6381        }
6382
6383        let is_safe_to_send_app_data =
6384            self.is_established() || self.is_in_early_data();
6385
6386        if app && !is_safe_to_send_app_data {
6387            // Clear error information.
6388            self.local_error = Some(ConnectionError {
6389                is_app: false,
6390                error_code: 0x0c,
6391                reason: vec![],
6392            });
6393        } else {
6394            self.local_error = Some(ConnectionError {
6395                is_app: app,
6396                error_code: err,
6397                reason: reason.to_vec(),
6398            });
6399        }
6400
6401        // When no packet was successfully processed close connection immediately.
6402        if self.recv_count == 0 {
6403            self.mark_closed();
6404        }
6405
6406        Ok(())
6407    }
6408
6409    /// Returns a string uniquely representing the connection.
6410    ///
6411    /// This can be used for logging purposes to differentiate between multiple
6412    /// connections.
6413    #[inline]
6414    pub fn trace_id(&self) -> &str {
6415        &self.trace_id
6416    }
6417
6418    /// Returns the negotiated ALPN protocol.
6419    ///
6420    /// If no protocol has been negotiated, the returned value is empty.
6421    #[inline]
6422    pub fn application_proto(&self) -> &[u8] {
6423        self.alpn.as_ref()
6424    }
6425
6426    /// Returns the server name requested by the client.
6427    #[inline]
6428    pub fn server_name(&self) -> Option<&str> {
6429        self.handshake.server_name()
6430    }
6431
6432    /// Returns the peer's leaf certificate (if any) as a DER-encoded buffer.
6433    #[inline]
6434    pub fn peer_cert(&self) -> Option<&[u8]> {
6435        self.handshake.peer_cert()
6436    }
6437
6438    /// Returns the peer's certificate chain (if any) as a vector of DER-encoded
6439    /// buffers.
6440    ///
6441    /// The certificate at index 0 is the peer's leaf certificate, the other
6442    /// certificates (if any) are the chain certificate authorities used to
6443    /// sign the leaf certificate.
6444    #[inline]
6445    pub fn peer_cert_chain(&self) -> Option<Vec<&[u8]>> {
6446        self.handshake.peer_cert_chain()
6447    }
6448
6449    /// Returns the serialized cryptographic session for the connection.
6450    ///
6451    /// This can be used by a client to cache a connection's session, and resume
6452    /// it later using the [`set_session()`] method.
6453    ///
6454    /// [`set_session()`]: struct.Connection.html#method.set_session
6455    #[inline]
6456    pub fn session(&self) -> Option<&[u8]> {
6457        self.session.as_deref()
6458    }
6459
6460    /// Returns the source connection ID.
6461    ///
6462    /// When there are multiple IDs, and if there is an active path, the ID used
6463    /// on that path is returned. Otherwise the oldest ID is returned.
6464    ///
6465    /// Note that the value returned can change throughout the connection's
6466    /// lifetime.
6467    #[inline]
6468    pub fn source_id(&self) -> ConnectionId {
6469        if let Ok(path) = self.paths.get_active() {
6470            if let Some(active_scid_seq) = path.active_scid_seq {
6471                if let Ok(e) = self.ids.get_scid(active_scid_seq) {
6472                    return ConnectionId::from_ref(e.cid.as_ref());
6473                }
6474            }
6475        }
6476
6477        let e = self.ids.oldest_scid();
6478        ConnectionId::from_ref(e.cid.as_ref())
6479    }
6480
6481    /// Returns all active source connection IDs.
6482    ///
6483    /// An iterator is returned for all active IDs (i.e. ones that have not
6484    /// been explicitly retired yet).
6485    #[inline]
6486    pub fn source_ids(&self) -> impl Iterator<Item = &ConnectionId> {
6487        self.ids.scids_iter()
6488    }
6489
6490    /// Returns the destination connection ID.
6491    ///
6492    /// Note that the value returned can change throughout the connection's
6493    /// lifetime.
6494    #[inline]
6495    pub fn destination_id(&self) -> ConnectionId {
6496        if let Ok(path) = self.paths.get_active() {
6497            if let Some(active_dcid_seq) = path.active_dcid_seq {
6498                if let Ok(e) = self.ids.get_dcid(active_dcid_seq) {
6499                    return ConnectionId::from_ref(e.cid.as_ref());
6500                }
6501            }
6502        }
6503
6504        let e = self.ids.oldest_dcid();
6505        ConnectionId::from_ref(e.cid.as_ref())
6506    }
6507
6508    /// Returns true if the connection handshake is complete.
6509    #[inline]
6510    pub fn is_established(&self) -> bool {
6511        self.handshake_completed
6512    }
6513
6514    /// Returns true if the connection is resumed.
6515    #[inline]
6516    pub fn is_resumed(&self) -> bool {
6517        self.handshake.is_resumed()
6518    }
6519
6520    /// Returns true if the connection has a pending handshake that has
6521    /// progressed enough to send or receive early data.
6522    #[inline]
6523    pub fn is_in_early_data(&self) -> bool {
6524        self.handshake.is_in_early_data()
6525    }
6526
6527    /// Returns whether there is stream or DATAGRAM data available to read.
6528    #[inline]
6529    pub fn is_readable(&self) -> bool {
6530        self.streams.has_readable() || self.dgram_recv_front_len().is_some()
6531    }
6532
6533    /// Returns whether the network path with local address `from` and remote
6534    /// address `peer` has been validated.
6535    ///
6536    /// If the 4-tuple does not exist over the connection, returns an
6537    /// [`InvalidState`].
6538    ///
6539    /// [`InvalidState`]: enum.Error.html#variant.InvalidState
6540    pub fn is_path_validated(
6541        &self, from: SocketAddr, to: SocketAddr,
6542    ) -> Result<bool> {
6543        let pid = self
6544            .paths
6545            .path_id_from_addrs(&(from, to))
6546            .ok_or(Error::InvalidState)?;
6547
6548        Ok(self.paths.get(pid)?.validated())
6549    }
6550
6551    /// Returns true if the connection is draining.
6552    ///
6553    /// If this returns `true`, the connection object cannot yet be dropped, but
6554    /// no new application data can be sent or received. An application should
6555    /// continue calling the [`recv()`], [`timeout()`], and [`on_timeout()`]
6556    /// methods as normal, until the [`is_closed()`] method returns `true`.
6557    ///
6558    /// In contrast, once `is_draining()` returns `true`, calling [`send()`]
6559    /// is not required because no new outgoing packets will be generated.
6560    ///
6561    /// [`recv()`]: struct.Connection.html#method.recv
6562    /// [`send()`]: struct.Connection.html#method.send
6563    /// [`timeout()`]: struct.Connection.html#method.timeout
6564    /// [`on_timeout()`]: struct.Connection.html#method.on_timeout
6565    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6566    #[inline]
6567    pub fn is_draining(&self) -> bool {
6568        self.draining_timer.is_some()
6569    }
6570
6571    /// Returns true if the connection is closed.
6572    ///
6573    /// If this returns true, the connection object can be dropped.
6574    #[inline]
6575    pub fn is_closed(&self) -> bool {
6576        self.closed
6577    }
6578
6579    /// Returns true if the connection was closed due to the idle timeout.
6580    #[inline]
6581    pub fn is_timed_out(&self) -> bool {
6582        self.timed_out
6583    }
6584
6585    /// Returns the error received from the peer, if any.
6586    ///
6587    /// Note that a `Some` return value does not necessarily imply
6588    /// [`is_closed()`] or any other connection state.
6589    ///
6590    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6591    #[inline]
6592    pub fn peer_error(&self) -> Option<&ConnectionError> {
6593        self.peer_error.as_ref()
6594    }
6595
6596    /// Returns the error [`close()`] was called with, or internally
6597    /// created quiche errors, if any.
6598    ///
6599    /// Note that a `Some` return value does not necessarily imply
6600    /// [`is_closed()`] or any other connection state.
6601    /// `Some` also does not guarantee that the error has been sent to
6602    /// or received by the peer.
6603    ///
6604    /// [`close()`]: struct.Connection.html#method.close
6605    /// [`is_closed()`]: struct.Connection.html#method.is_closed
6606    #[inline]
6607    pub fn local_error(&self) -> Option<&ConnectionError> {
6608        self.local_error.as_ref()
6609    }
6610
6611    /// Collects and returns statistics about the connection.
6612    #[inline]
6613    pub fn stats(&self) -> Stats {
6614        Stats {
6615            recv: self.recv_count,
6616            sent: self.sent_count,
6617            lost: self.lost_count,
6618            retrans: self.retrans_count,
6619            sent_bytes: self.sent_bytes,
6620            recv_bytes: self.recv_bytes,
6621            acked_bytes: self.acked_bytes,
6622            lost_bytes: self.lost_bytes,
6623            stream_retrans_bytes: self.stream_retrans_bytes,
6624            dgram_recv: self.dgram_recv_count,
6625            dgram_sent: self.dgram_sent_count,
6626            paths_count: self.paths.len(),
6627            reset_stream_count_local: self.reset_stream_local_count,
6628            stopped_stream_count_local: self.stopped_stream_local_count,
6629            reset_stream_count_remote: self.reset_stream_remote_count,
6630            stopped_stream_count_remote: self.stopped_stream_remote_count,
6631            path_challenge_rx_count: self.path_challenge_rx_count,
6632        }
6633    }
6634
6635    /// Returns reference to peer's transport parameters. Returns `None` if we
6636    /// have not yet processed the peer's transport parameters.
6637    pub fn peer_transport_params(&self) -> Option<&TransportParams> {
6638        if !self.parsed_peer_transport_params {
6639            return None;
6640        }
6641
6642        Some(&self.peer_transport_params)
6643    }
6644
6645    /// Collects and returns statistics about each known path for the
6646    /// connection.
6647    pub fn path_stats(&self) -> impl Iterator<Item = PathStats> + '_ {
6648        self.paths.iter().map(|(_, p)| p.stats())
6649    }
6650
6651    /// Returns whether or not this is a server-side connection.
6652    pub fn is_server(&self) -> bool {
6653        self.is_server
6654    }
6655
6656    fn encode_transport_params(&mut self) -> Result<()> {
6657        let mut raw_params = [0; 128];
6658
6659        let raw_params = TransportParams::encode(
6660            &self.local_transport_params,
6661            self.is_server,
6662            &mut raw_params,
6663        )?;
6664
6665        self.handshake.set_quic_transport_params(raw_params)?;
6666
6667        Ok(())
6668    }
6669
6670    fn parse_peer_transport_params(
6671        &mut self, peer_params: TransportParams,
6672    ) -> Result<()> {
6673        // Validate initial_source_connection_id.
6674        match &peer_params.initial_source_connection_id {
6675            Some(v) if v != &self.destination_id() =>
6676                return Err(Error::InvalidTransportParam),
6677
6678            Some(_) => (),
6679
6680            // initial_source_connection_id must be sent by
6681            // both endpoints.
6682            None => return Err(Error::InvalidTransportParam),
6683        }
6684
6685        // Validate original_destination_connection_id.
6686        if let Some(odcid) = &self.odcid {
6687            match &peer_params.original_destination_connection_id {
6688                Some(v) if v != odcid =>
6689                    return Err(Error::InvalidTransportParam),
6690
6691                Some(_) => (),
6692
6693                // original_destination_connection_id must be
6694                // sent by the server.
6695                None if !self.is_server =>
6696                    return Err(Error::InvalidTransportParam),
6697
6698                None => (),
6699            }
6700        }
6701
6702        // Validate retry_source_connection_id.
6703        if let Some(rscid) = &self.rscid {
6704            match &peer_params.retry_source_connection_id {
6705                Some(v) if v != rscid =>
6706                    return Err(Error::InvalidTransportParam),
6707
6708                Some(_) => (),
6709
6710                // retry_source_connection_id must be sent by
6711                // the server.
6712                None => return Err(Error::InvalidTransportParam),
6713            }
6714        }
6715
6716        self.process_peer_transport_params(peer_params)?;
6717
6718        self.parsed_peer_transport_params = true;
6719
6720        Ok(())
6721    }
6722
6723    fn process_peer_transport_params(
6724        &mut self, peer_params: TransportParams,
6725    ) -> Result<()> {
6726        self.max_tx_data = peer_params.initial_max_data;
6727
6728        // Update send capacity.
6729        self.update_tx_cap();
6730
6731        self.streams
6732            .update_peer_max_streams_bidi(peer_params.initial_max_streams_bidi);
6733        self.streams
6734            .update_peer_max_streams_uni(peer_params.initial_max_streams_uni);
6735
6736        let max_ack_delay =
6737            time::Duration::from_millis(peer_params.max_ack_delay);
6738
6739        self.recovery_config.max_ack_delay = max_ack_delay;
6740
6741        let active_path = self.paths.get_active_mut()?;
6742
6743        active_path.recovery.update_max_ack_delay(max_ack_delay);
6744
6745        if active_path.pmtud.get_probe_status() {
6746            active_path.recovery.pmtud_update_max_datagram_size(
6747                active_path
6748                    .pmtud
6749                    .get_probe_size()
6750                    .min(peer_params.max_udp_payload_size as usize),
6751            );
6752        } else {
6753            active_path.recovery.update_max_datagram_size(
6754                peer_params.max_udp_payload_size as usize,
6755            );
6756        }
6757
6758        // Record the max_active_conn_id parameter advertised by the peer.
6759        self.ids
6760            .set_source_conn_id_limit(peer_params.active_conn_id_limit);
6761
6762        self.peer_transport_params = peer_params;
6763
6764        Ok(())
6765    }
6766
6767    /// Continues the handshake.
6768    ///
6769    /// If the connection is already established, it does nothing.
6770    fn do_handshake(&mut self, now: time::Instant) -> Result<()> {
6771        let mut ex_data = tls::ExData {
6772            application_protos: &self.application_protos,
6773
6774            pkt_num_spaces: &mut self.pkt_num_spaces,
6775
6776            session: &mut self.session,
6777
6778            local_error: &mut self.local_error,
6779
6780            keylog: self.keylog.as_mut(),
6781
6782            trace_id: &self.trace_id,
6783
6784            is_server: self.is_server,
6785        };
6786
6787        if self.handshake_completed {
6788            return self.handshake.process_post_handshake(&mut ex_data);
6789        }
6790
6791        match self.handshake.do_handshake(&mut ex_data) {
6792            Ok(_) => (),
6793
6794            Err(Error::Done) => {
6795                // Try to parse transport parameters as soon as the first flight
6796                // of handshake data is processed.
6797                //
6798                // This is potentially dangerous as the handshake hasn't been
6799                // completed yet, though it's required to be able to send data
6800                // in 0.5 RTT.
6801                let raw_params = self.handshake.quic_transport_params();
6802
6803                if !self.parsed_peer_transport_params && !raw_params.is_empty() {
6804                    let peer_params = TransportParams::decode(
6805                        raw_params,
6806                        self.is_server,
6807                        self.peer_transport_params_track_unknown,
6808                    )?;
6809
6810                    self.parse_peer_transport_params(peer_params)?;
6811                }
6812
6813                return Ok(());
6814            },
6815
6816            Err(e) => return Err(e),
6817        };
6818
6819        self.handshake_completed = self.handshake.is_completed();
6820
6821        self.alpn = self.handshake.alpn_protocol().to_vec();
6822
6823        let raw_params = self.handshake.quic_transport_params();
6824
6825        if !self.parsed_peer_transport_params && !raw_params.is_empty() {
6826            let peer_params = TransportParams::decode(
6827                raw_params,
6828                self.is_server,
6829                self.peer_transport_params_track_unknown,
6830            )?;
6831
6832            self.parse_peer_transport_params(peer_params)?;
6833        }
6834
6835        if self.handshake_completed {
6836            // The handshake is considered confirmed at the server when the
6837            // handshake completes, at which point we can also drop the
6838            // handshake epoch.
6839            if self.is_server {
6840                self.handshake_confirmed = true;
6841
6842                self.drop_epoch_state(packet::Epoch::Handshake, now);
6843            }
6844
6845            // Once the handshake is completed there's no point in processing
6846            // 0-RTT packets anymore, so clear the buffer now.
6847            self.undecryptable_pkts.clear();
6848
6849            trace!("{} connection established: proto={:?} cipher={:?} curve={:?} sigalg={:?} resumed={} {:?}",
6850                   &self.trace_id,
6851                   std::str::from_utf8(self.application_proto()),
6852                   self.handshake.cipher(),
6853                   self.handshake.curve(),
6854                   self.handshake.sigalg(),
6855                   self.handshake.is_resumed(),
6856                   self.peer_transport_params);
6857        }
6858
6859        Ok(())
6860    }
6861
6862    /// Selects the packet type for the next outgoing packet.
6863    fn write_pkt_type(&self, send_pid: usize) -> Result<packet::Type> {
6864        // On error send packet in the latest epoch available, but only send
6865        // 1-RTT ones when the handshake is completed.
6866        if self
6867            .local_error
6868            .as_ref()
6869            .is_some_and(|conn_err| !conn_err.is_app)
6870        {
6871            let epoch = match self.handshake.write_level() {
6872                crypto::Level::Initial => packet::Epoch::Initial,
6873                crypto::Level::ZeroRTT => unreachable!(),
6874                crypto::Level::Handshake => packet::Epoch::Handshake,
6875                crypto::Level::OneRTT => packet::Epoch::Application,
6876            };
6877
6878            if !self.handshake_confirmed {
6879                match epoch {
6880                    // Downgrade the epoch to Handshake as the handshake is not
6881                    // completed yet.
6882                    packet::Epoch::Application =>
6883                        return Ok(packet::Type::Handshake),
6884
6885                    // Downgrade the epoch to Initial as the remote peer might
6886                    // not be able to decrypt handshake packets yet.
6887                    packet::Epoch::Handshake
6888                        if self.pkt_num_spaces[packet::Epoch::Initial]
6889                            .has_keys() =>
6890                        return Ok(packet::Type::Initial),
6891
6892                    _ => (),
6893                };
6894            }
6895
6896            return Ok(packet::Type::from_epoch(epoch));
6897        }
6898
6899        for &epoch in packet::Epoch::epochs(
6900            packet::Epoch::Initial..=packet::Epoch::Application,
6901        ) {
6902            // Only send packets in a space when we have the send keys for it.
6903            if self.pkt_num_spaces[epoch].crypto_seal.is_none() {
6904                continue;
6905            }
6906
6907            // We are ready to send data for this packet number space.
6908            if self.pkt_num_spaces[epoch].ready() {
6909                return Ok(packet::Type::from_epoch(epoch));
6910            }
6911
6912            // There are lost frames in this packet number space.
6913            for (_, p) in self.paths.iter() {
6914                if p.recovery.has_lost_frames(epoch) {
6915                    return Ok(packet::Type::from_epoch(epoch));
6916                }
6917
6918                // We need to send PTO probe packets.
6919                if p.recovery.loss_probes(epoch) > 0 {
6920                    return Ok(packet::Type::from_epoch(epoch));
6921                }
6922            }
6923        }
6924
6925        // If there are flushable, almost full or blocked streams, use the
6926        // Application epoch.
6927        let send_path = self.paths.get(send_pid)?;
6928        if (self.is_established() || self.is_in_early_data()) &&
6929            (self.should_send_handshake_done() ||
6930                self.almost_full ||
6931                self.blocked_limit.is_some() ||
6932                self.dgram_send_queue.has_pending() ||
6933                self.local_error
6934                    .as_ref()
6935                    .is_some_and(|conn_err| conn_err.is_app) ||
6936                self.streams.should_update_max_streams_bidi() ||
6937                self.streams.should_update_max_streams_uni() ||
6938                self.streams.has_flushable() ||
6939                self.streams.has_almost_full() ||
6940                self.streams.has_blocked() ||
6941                self.streams.has_reset() ||
6942                self.streams.has_stopped() ||
6943                self.ids.has_new_scids() ||
6944                self.ids.has_retire_dcids() ||
6945                send_path.pmtud.get_probe_status() ||
6946                send_path.needs_ack_eliciting ||
6947                send_path.probing_required())
6948        {
6949            // Only clients can send 0-RTT packets.
6950            if !self.is_server && self.is_in_early_data() {
6951                return Ok(packet::Type::ZeroRTT);
6952            }
6953
6954            return Ok(packet::Type::Short);
6955        }
6956
6957        Err(Error::Done)
6958    }
6959
6960    /// Returns the mutable stream with the given ID if it exists, or creates
6961    /// a new one otherwise.
6962    fn get_or_create_stream(
6963        &mut self, id: u64, local: bool,
6964    ) -> Result<&mut stream::Stream> {
6965        self.streams.get_or_create(
6966            id,
6967            &self.local_transport_params,
6968            &self.peer_transport_params,
6969            local,
6970            self.is_server,
6971        )
6972    }
6973
6974    /// Processes an incoming frame.
6975    fn process_frame(
6976        &mut self, frame: frame::Frame, hdr: &packet::Header,
6977        recv_path_id: usize, epoch: packet::Epoch, now: time::Instant,
6978    ) -> Result<()> {
6979        trace!("{} rx frm {:?}", self.trace_id, frame);
6980
6981        match frame {
6982            frame::Frame::Padding { .. } => (),
6983
6984            frame::Frame::Ping { .. } => (),
6985
6986            frame::Frame::ACK {
6987                ranges, ack_delay, ..
6988            } => {
6989                let ack_delay = ack_delay
6990                    .checked_mul(2_u64.pow(
6991                        self.peer_transport_params.ack_delay_exponent as u32,
6992                    ))
6993                    .ok_or(Error::InvalidFrame)?;
6994
6995                if epoch == packet::Epoch::Handshake ||
6996                    (epoch == packet::Epoch::Application &&
6997                        self.is_established())
6998                {
6999                    self.peer_verified_initial_address = true;
7000                }
7001
7002                let handshake_status = self.handshake_status();
7003
7004                let is_app_limited = self.delivery_rate_check_if_app_limited();
7005
7006                for (_, p) in self.paths.iter_mut() {
7007                    if is_app_limited {
7008                        p.recovery.delivery_rate_update_app_limited(true);
7009                    }
7010
7011                    let (lost_packets, lost_bytes, acked_bytes) =
7012                        p.recovery.on_ack_received(
7013                            &ranges,
7014                            ack_delay,
7015                            epoch,
7016                            handshake_status,
7017                            now,
7018                            &self.trace_id,
7019                        )?;
7020
7021                    self.lost_count += lost_packets;
7022                    self.lost_bytes += lost_bytes as u64;
7023                    self.acked_bytes += acked_bytes as u64;
7024                }
7025            },
7026
7027            frame::Frame::ResetStream {
7028                stream_id,
7029                error_code,
7030                final_size,
7031            } => {
7032                // Peer can't send on our unidirectional streams.
7033                if !stream::is_bidi(stream_id) &&
7034                    stream::is_local(stream_id, self.is_server)
7035                {
7036                    return Err(Error::InvalidStreamState(stream_id));
7037                }
7038
7039                let max_rx_data_left = self.max_rx_data() - self.rx_data;
7040
7041                // Get existing stream or create a new one, but if the stream
7042                // has already been closed and collected, ignore the frame.
7043                //
7044                // This can happen if e.g. an ACK frame is lost, and the peer
7045                // retransmits another frame before it realizes that the stream
7046                // is gone.
7047                //
7048                // Note that it makes it impossible to check if the frame is
7049                // illegal, since we have no state, but since we ignore the
7050                // frame, it should be fine.
7051                let stream = match self.get_or_create_stream(stream_id, false) {
7052                    Ok(v) => v,
7053
7054                    Err(Error::Done) => return Ok(()),
7055
7056                    Err(e) => return Err(e),
7057                };
7058
7059                let was_readable = stream.is_readable();
7060                let priority_key = Arc::clone(&stream.priority_key);
7061
7062                let max_off_delta =
7063                    stream.recv.reset(error_code, final_size)? as u64;
7064
7065                if max_off_delta > max_rx_data_left {
7066                    return Err(Error::FlowControl);
7067                }
7068
7069                if !was_readable && stream.is_readable() {
7070                    self.streams.insert_readable(&priority_key);
7071                }
7072
7073                self.rx_data += max_off_delta;
7074
7075                self.reset_stream_remote_count =
7076                    self.reset_stream_remote_count.saturating_add(1);
7077            },
7078
7079            frame::Frame::StopSending {
7080                stream_id,
7081                error_code,
7082            } => {
7083                // STOP_SENDING on a receive-only stream is a fatal error.
7084                if !stream::is_local(stream_id, self.is_server) &&
7085                    !stream::is_bidi(stream_id)
7086                {
7087                    return Err(Error::InvalidStreamState(stream_id));
7088                }
7089
7090                // Get existing stream or create a new one, but if the stream
7091                // has already been closed and collected, ignore the frame.
7092                //
7093                // This can happen if e.g. an ACK frame is lost, and the peer
7094                // retransmits another frame before it realizes that the stream
7095                // is gone.
7096                //
7097                // Note that it makes it impossible to check if the frame is
7098                // illegal, since we have no state, but since we ignore the
7099                // frame, it should be fine.
7100                let stream = match self.get_or_create_stream(stream_id, false) {
7101                    Ok(v) => v,
7102
7103                    Err(Error::Done) => return Ok(()),
7104
7105                    Err(e) => return Err(e),
7106                };
7107
7108                let was_writable = stream.is_writable();
7109
7110                let priority_key = Arc::clone(&stream.priority_key);
7111
7112                // Try stopping the stream.
7113                if let Ok((final_size, unsent)) = stream.send.stop(error_code) {
7114                    // Claw back some flow control allowance from data that was
7115                    // buffered but not actually sent before the stream was
7116                    // reset.
7117                    //
7118                    // Note that `tx_cap` will be updated later on, so no need
7119                    // to touch it here.
7120                    self.tx_data = self.tx_data.saturating_sub(unsent);
7121
7122                    self.tx_buffered =
7123                        self.tx_buffered.saturating_sub(unsent as usize);
7124
7125                    self.streams.insert_reset(stream_id, error_code, final_size);
7126
7127                    if !was_writable {
7128                        self.streams.insert_writable(&priority_key);
7129                    }
7130
7131                    self.stopped_stream_remote_count =
7132                        self.stopped_stream_remote_count.saturating_add(1);
7133                    self.reset_stream_local_count =
7134                        self.reset_stream_local_count.saturating_add(1);
7135                }
7136            },
7137
7138            frame::Frame::Crypto { data } => {
7139                if data.max_off() >= MAX_CRYPTO_STREAM_OFFSET {
7140                    return Err(Error::CryptoBufferExceeded);
7141                }
7142
7143                // Push the data to the stream so it can be re-ordered.
7144                self.pkt_num_spaces[epoch].crypto_stream.recv.write(data)?;
7145
7146                // Feed crypto data to the TLS state, if there's data
7147                // available at the expected offset.
7148                let mut crypto_buf = [0; 512];
7149
7150                let level = crypto::Level::from_epoch(epoch);
7151
7152                let stream = &mut self.pkt_num_spaces[epoch].crypto_stream;
7153
7154                while let Ok((read, _)) = stream.recv.emit(&mut crypto_buf) {
7155                    let recv_buf = &crypto_buf[..read];
7156                    self.handshake.provide_data(level, recv_buf)?;
7157                }
7158
7159                self.do_handshake(now)?;
7160            },
7161
7162            frame::Frame::CryptoHeader { .. } => unreachable!(),
7163
7164            // TODO: implement stateless retry
7165            frame::Frame::NewToken { .. } =>
7166                if self.is_server {
7167                    return Err(Error::InvalidPacket);
7168                },
7169
7170            frame::Frame::Stream { stream_id, data } => {
7171                // Peer can't send on our unidirectional streams.
7172                if !stream::is_bidi(stream_id) &&
7173                    stream::is_local(stream_id, self.is_server)
7174                {
7175                    return Err(Error::InvalidStreamState(stream_id));
7176                }
7177
7178                let max_rx_data_left = self.max_rx_data() - self.rx_data;
7179
7180                // Get existing stream or create a new one, but if the stream
7181                // has already been closed and collected, ignore the frame.
7182                //
7183                // This can happen if e.g. an ACK frame is lost, and the peer
7184                // retransmits another frame before it realizes that the stream
7185                // is gone.
7186                //
7187                // Note that it makes it impossible to check if the frame is
7188                // illegal, since we have no state, but since we ignore the
7189                // frame, it should be fine.
7190                let stream = match self.get_or_create_stream(stream_id, false) {
7191                    Ok(v) => v,
7192
7193                    Err(Error::Done) => return Ok(()),
7194
7195                    Err(e) => return Err(e),
7196                };
7197
7198                // Check for the connection-level flow control limit.
7199                let max_off_delta =
7200                    data.max_off().saturating_sub(stream.recv.max_off());
7201
7202                if max_off_delta > max_rx_data_left {
7203                    return Err(Error::FlowControl);
7204                }
7205
7206                let was_readable = stream.is_readable();
7207                let priority_key = Arc::clone(&stream.priority_key);
7208
7209                let was_draining = stream.recv.is_draining();
7210
7211                stream.recv.write(data)?;
7212
7213                if !was_readable && stream.is_readable() {
7214                    self.streams.insert_readable(&priority_key);
7215                }
7216
7217                self.rx_data += max_off_delta;
7218
7219                if was_draining {
7220                    // When a stream is in draining state it will not queue
7221                    // incoming data for the application to read, so consider
7222                    // the received data as consumed, which might trigger a flow
7223                    // control update.
7224                    self.flow_control.add_consumed(max_off_delta);
7225
7226                    if self.should_update_max_data() {
7227                        self.almost_full = true;
7228                    }
7229                }
7230            },
7231
7232            frame::Frame::StreamHeader { .. } => unreachable!(),
7233
7234            frame::Frame::MaxData { max } => {
7235                self.max_tx_data = cmp::max(self.max_tx_data, max);
7236            },
7237
7238            frame::Frame::MaxStreamData { stream_id, max } => {
7239                // Peer can't receive on its own unidirectional streams.
7240                if !stream::is_bidi(stream_id) &&
7241                    !stream::is_local(stream_id, self.is_server)
7242                {
7243                    return Err(Error::InvalidStreamState(stream_id));
7244                }
7245
7246                // Get existing stream or create a new one, but if the stream
7247                // has already been closed and collected, ignore the frame.
7248                //
7249                // This can happen if e.g. an ACK frame is lost, and the peer
7250                // retransmits another frame before it realizes that the stream
7251                // is gone.
7252                //
7253                // Note that it makes it impossible to check if the frame is
7254                // illegal, since we have no state, but since we ignore the
7255                // frame, it should be fine.
7256                let stream = match self.get_or_create_stream(stream_id, false) {
7257                    Ok(v) => v,
7258
7259                    Err(Error::Done) => return Ok(()),
7260
7261                    Err(e) => return Err(e),
7262                };
7263
7264                let was_flushable = stream.is_flushable();
7265
7266                stream.send.update_max_data(max);
7267
7268                let writable = stream.is_writable();
7269
7270                let priority_key = Arc::clone(&stream.priority_key);
7271
7272                // If the stream is now flushable push it to the flushable queue,
7273                // but only if it wasn't already queued.
7274                if stream.is_flushable() && !was_flushable {
7275                    let priority_key = Arc::clone(&stream.priority_key);
7276                    self.streams.insert_flushable(&priority_key);
7277                }
7278
7279                if writable {
7280                    self.streams.insert_writable(&priority_key);
7281                }
7282            },
7283
7284            frame::Frame::MaxStreamsBidi { max } => {
7285                if max > MAX_STREAM_ID {
7286                    return Err(Error::InvalidFrame);
7287                }
7288
7289                self.streams.update_peer_max_streams_bidi(max);
7290            },
7291
7292            frame::Frame::MaxStreamsUni { max } => {
7293                if max > MAX_STREAM_ID {
7294                    return Err(Error::InvalidFrame);
7295                }
7296
7297                self.streams.update_peer_max_streams_uni(max);
7298            },
7299
7300            frame::Frame::DataBlocked { .. } => (),
7301
7302            frame::Frame::StreamDataBlocked { .. } => (),
7303
7304            frame::Frame::StreamsBlockedBidi { limit } => {
7305                if limit > MAX_STREAM_ID {
7306                    return Err(Error::InvalidFrame);
7307                }
7308            },
7309
7310            frame::Frame::StreamsBlockedUni { limit } => {
7311                if limit > MAX_STREAM_ID {
7312                    return Err(Error::InvalidFrame);
7313                }
7314            },
7315
7316            frame::Frame::NewConnectionId {
7317                seq_num,
7318                retire_prior_to,
7319                conn_id,
7320                reset_token,
7321            } => {
7322                if self.ids.zero_length_dcid() {
7323                    return Err(Error::InvalidState);
7324                }
7325
7326                let mut retired_path_ids = SmallVec::new();
7327
7328                // Retire pending path IDs before propagating the error code to
7329                // make sure retired connection IDs are not in use anymore.
7330                let new_dcid_res = self.ids.new_dcid(
7331                    conn_id.into(),
7332                    seq_num,
7333                    u128::from_be_bytes(reset_token),
7334                    retire_prior_to,
7335                    &mut retired_path_ids,
7336                );
7337
7338                for (dcid_seq, pid) in retired_path_ids {
7339                    let path = self.paths.get_mut(pid)?;
7340
7341                    // Maybe the path already switched to another DCID.
7342                    if path.active_dcid_seq != Some(dcid_seq) {
7343                        continue;
7344                    }
7345
7346                    if let Some(new_dcid_seq) =
7347                        self.ids.lowest_available_dcid_seq()
7348                    {
7349                        path.active_dcid_seq = Some(new_dcid_seq);
7350
7351                        self.ids.link_dcid_to_path_id(new_dcid_seq, pid)?;
7352
7353                        trace!(
7354                            "{} path ID {} changed DCID: old seq num {} new seq num {}",
7355                            self.trace_id, pid, dcid_seq, new_dcid_seq,
7356                        );
7357                    } else {
7358                        // We cannot use this path anymore for now.
7359                        path.active_dcid_seq = None;
7360
7361                        trace!(
7362                            "{} path ID {} cannot be used; DCID seq num {} has been retired",
7363                            self.trace_id, pid, dcid_seq,
7364                        );
7365                    }
7366                }
7367
7368                // Propagate error (if any) now...
7369                new_dcid_res?;
7370            },
7371
7372            frame::Frame::RetireConnectionId { seq_num } => {
7373                if self.ids.zero_length_scid() {
7374                    return Err(Error::InvalidState);
7375                }
7376
7377                if let Some(pid) = self.ids.retire_scid(seq_num, &hdr.dcid)? {
7378                    let path = self.paths.get_mut(pid)?;
7379
7380                    // Maybe we already linked a new SCID to that path.
7381                    if path.active_scid_seq == Some(seq_num) {
7382                        // XXX: We do not remove unused paths now, we instead
7383                        // wait until we need to maintain more paths than the
7384                        // host is willing to.
7385                        path.active_scid_seq = None;
7386                    }
7387                }
7388            },
7389
7390            frame::Frame::PathChallenge { data } => {
7391                self.path_challenge_rx_count += 1;
7392
7393                self.paths
7394                    .get_mut(recv_path_id)?
7395                    .on_challenge_received(data);
7396            },
7397
7398            frame::Frame::PathResponse { data } => {
7399                self.paths.on_response_received(data)?;
7400            },
7401
7402            frame::Frame::ConnectionClose {
7403                error_code, reason, ..
7404            } => {
7405                self.peer_error = Some(ConnectionError {
7406                    is_app: false,
7407                    error_code,
7408                    reason,
7409                });
7410
7411                let path = self.paths.get_active()?;
7412                self.draining_timer = Some(now + (path.recovery.pto() * 3));
7413            },
7414
7415            frame::Frame::ApplicationClose { error_code, reason } => {
7416                self.peer_error = Some(ConnectionError {
7417                    is_app: true,
7418                    error_code,
7419                    reason,
7420                });
7421
7422                let path = self.paths.get_active()?;
7423                self.draining_timer = Some(now + (path.recovery.pto() * 3));
7424            },
7425
7426            frame::Frame::HandshakeDone => {
7427                if self.is_server {
7428                    return Err(Error::InvalidPacket);
7429                }
7430
7431                self.peer_verified_initial_address = true;
7432
7433                self.handshake_confirmed = true;
7434
7435                // Once the handshake is confirmed, we can drop Handshake keys.
7436                self.drop_epoch_state(packet::Epoch::Handshake, now);
7437            },
7438
7439            frame::Frame::Datagram { data } => {
7440                // Close the connection if DATAGRAMs are not enabled.
7441                // quiche always advertises support for 64K sized DATAGRAM
7442                // frames, as recommended by the standard, so we don't need a
7443                // size check.
7444                if !self.dgram_enabled() {
7445                    return Err(Error::InvalidState);
7446                }
7447
7448                // If recv queue is full, discard oldest
7449                if self.dgram_recv_queue.is_full() {
7450                    self.dgram_recv_queue.pop();
7451                }
7452
7453                self.dgram_recv_queue.push(data)?;
7454
7455                let _ = self.dgram_recv_count.saturating_add(1);
7456                let _ = self
7457                    .paths
7458                    .get_mut(recv_path_id)?
7459                    .dgram_recv_count
7460                    .saturating_add(1);
7461            },
7462
7463            frame::Frame::DatagramHeader { .. } => unreachable!(),
7464        }
7465
7466        Ok(())
7467    }
7468
7469    /// Drops the keys and recovery state for the given epoch.
7470    fn drop_epoch_state(&mut self, epoch: packet::Epoch, now: time::Instant) {
7471        if self.pkt_num_spaces[epoch].crypto_open.is_none() {
7472            return;
7473        }
7474
7475        self.pkt_num_spaces[epoch].crypto_open = None;
7476        self.pkt_num_spaces[epoch].crypto_seal = None;
7477        self.pkt_num_spaces[epoch].clear();
7478
7479        let handshake_status = self.handshake_status();
7480        for (_, p) in self.paths.iter_mut() {
7481            p.recovery
7482                .on_pkt_num_space_discarded(epoch, handshake_status, now);
7483        }
7484
7485        trace!("{} dropped epoch {} state", self.trace_id, epoch);
7486    }
7487
7488    /// Returns true if the connection-level flow control needs to be updated.
7489    ///
7490    /// This happens when the new max data limit is at least double the amount
7491    /// of data that can be received before blocking.
7492    fn should_update_max_data(&self) -> bool {
7493        self.flow_control.should_update_max_data()
7494    }
7495
7496    /// Returns the connection level flow control limit.
7497    fn max_rx_data(&self) -> u64 {
7498        self.flow_control.max_data()
7499    }
7500
7501    /// Returns true if the HANDSHAKE_DONE frame needs to be sent.
7502    fn should_send_handshake_done(&self) -> bool {
7503        self.is_established() && !self.handshake_done_sent && self.is_server
7504    }
7505
7506    /// Returns the idle timeout value.
7507    ///
7508    /// `None` is returned if both end-points disabled the idle timeout.
7509    fn idle_timeout(&mut self) -> Option<time::Duration> {
7510        // If the transport parameter is set to 0, then the respective endpoint
7511        // decided to disable the idle timeout. If both are disabled we should
7512        // not set any timeout.
7513        if self.local_transport_params.max_idle_timeout == 0 &&
7514            self.peer_transport_params.max_idle_timeout == 0
7515        {
7516            return None;
7517        }
7518
7519        // If the local endpoint or the peer disabled the idle timeout, use the
7520        // other peer's value, otherwise use the minimum of the two values.
7521        let idle_timeout = if self.local_transport_params.max_idle_timeout == 0 {
7522            self.peer_transport_params.max_idle_timeout
7523        } else if self.peer_transport_params.max_idle_timeout == 0 {
7524            self.local_transport_params.max_idle_timeout
7525        } else {
7526            cmp::min(
7527                self.local_transport_params.max_idle_timeout,
7528                self.peer_transport_params.max_idle_timeout,
7529            )
7530        };
7531
7532        let path_pto = match self.paths.get_active() {
7533            Ok(p) => p.recovery.pto(),
7534            Err(_) => time::Duration::ZERO,
7535        };
7536
7537        let idle_timeout = time::Duration::from_millis(idle_timeout);
7538        let idle_timeout = cmp::max(idle_timeout, 3 * path_pto);
7539
7540        Some(idle_timeout)
7541    }
7542
7543    /// Returns the connection's handshake status for use in loss recovery.
7544    fn handshake_status(&self) -> recovery::HandshakeStatus {
7545        recovery::HandshakeStatus {
7546            has_handshake_keys: self.pkt_num_spaces[packet::Epoch::Handshake]
7547                .has_keys(),
7548
7549            peer_verified_address: self.peer_verified_initial_address,
7550
7551            completed: self.is_established(),
7552        }
7553    }
7554
7555    /// Updates send capacity.
7556    fn update_tx_cap(&mut self) {
7557        let cwin_available = match self.paths.get_active() {
7558            Ok(p) => p.recovery.cwnd_available() as u64,
7559            Err(_) => 0,
7560        };
7561
7562        self.tx_cap =
7563            cmp::min(cwin_available, self.max_tx_data - self.tx_data) as usize;
7564    }
7565
7566    fn delivery_rate_check_if_app_limited(&self) -> bool {
7567        // Enter the app-limited phase of delivery rate when these conditions
7568        // are met:
7569        //
7570        // - The remaining capacity is higher than available bytes in cwnd (there
7571        //   is more room to send).
7572        // - New data since the last send() is smaller than available bytes in
7573        //   cwnd (we queued less than what we can send).
7574        // - There is room to send more data in cwnd.
7575        //
7576        // In application-limited phases the transmission rate is limited by the
7577        // application rather than the congestion control algorithm.
7578        //
7579        // Note that this is equivalent to CheckIfApplicationLimited() from the
7580        // delivery rate draft. This is also separate from `recovery.app_limited`
7581        // and only applies to delivery rate calculation.
7582        let cwin_available = self
7583            .paths
7584            .iter()
7585            .filter(|&(_, p)| p.active())
7586            .map(|(_, p)| p.recovery.cwnd_available())
7587            .sum();
7588
7589        ((self.tx_buffered + self.dgram_send_queue_byte_size()) < cwin_available) &&
7590            (self.tx_data.saturating_sub(self.last_tx_data)) <
7591                cwin_available as u64 &&
7592            cwin_available > 0
7593    }
7594
7595    fn set_initial_dcid(
7596        &mut self, cid: ConnectionId<'static>, reset_token: Option<u128>,
7597        path_id: usize,
7598    ) -> Result<()> {
7599        self.ids.set_initial_dcid(cid, reset_token, Some(path_id));
7600        self.paths.get_mut(path_id)?.active_dcid_seq = Some(0);
7601
7602        Ok(())
7603    }
7604
7605    /// Selects the path that the incoming packet belongs to, or creates a new
7606    /// one if no existing path matches.
7607    fn get_or_create_recv_path_id(
7608        &mut self, recv_pid: Option<usize>, dcid: &ConnectionId, buf_len: usize,
7609        info: &RecvInfo,
7610    ) -> Result<usize> {
7611        let ids = &mut self.ids;
7612
7613        let (in_scid_seq, mut in_scid_pid) =
7614            ids.find_scid_seq(dcid).ok_or(Error::InvalidState)?;
7615
7616        if let Some(recv_pid) = recv_pid {
7617            // If the path observes a change of SCID used, note it.
7618            let recv_path = self.paths.get_mut(recv_pid)?;
7619
7620            let cid_entry =
7621                recv_path.active_scid_seq.and_then(|v| ids.get_scid(v).ok());
7622
7623            if cid_entry.map(|e| &e.cid) != Some(dcid) {
7624                let incoming_cid_entry = ids.get_scid(in_scid_seq)?;
7625
7626                let prev_recv_pid =
7627                    incoming_cid_entry.path_id.unwrap_or(recv_pid);
7628
7629                if prev_recv_pid != recv_pid {
7630                    trace!(
7631                        "{} peer reused CID {:?} from path {} on path {}",
7632                        self.trace_id,
7633                        dcid,
7634                        prev_recv_pid,
7635                        recv_pid
7636                    );
7637
7638                    // TODO: reset congestion control.
7639                }
7640
7641                trace!(
7642                    "{} path ID {} now see SCID with seq num {}",
7643                    self.trace_id,
7644                    recv_pid,
7645                    in_scid_seq
7646                );
7647
7648                recv_path.active_scid_seq = Some(in_scid_seq);
7649                ids.link_scid_to_path_id(in_scid_seq, recv_pid)?;
7650            }
7651
7652            return Ok(recv_pid);
7653        }
7654
7655        // This is a new 4-tuple. See if the CID has not been assigned on
7656        // another path.
7657
7658        // Ignore this step if are using zero-length SCID.
7659        if ids.zero_length_scid() {
7660            in_scid_pid = None;
7661        }
7662
7663        if let Some(in_scid_pid) = in_scid_pid {
7664            // This CID has been used by another path. If we have the
7665            // room to do so, create a new `Path` structure holding this
7666            // new 4-tuple. Otherwise, drop the packet.
7667            let old_path = self.paths.get_mut(in_scid_pid)?;
7668            let old_local_addr = old_path.local_addr();
7669            let old_peer_addr = old_path.peer_addr();
7670
7671            trace!(
7672                "{} reused CID seq {} of ({},{}) (path {}) on ({},{})",
7673                self.trace_id,
7674                in_scid_seq,
7675                old_local_addr,
7676                old_peer_addr,
7677                in_scid_pid,
7678                info.to,
7679                info.from
7680            );
7681
7682            // Notify the application.
7683            self.paths
7684                .notify_event(path::PathEvent::ReusedSourceConnectionId(
7685                    in_scid_seq,
7686                    (old_local_addr, old_peer_addr),
7687                    (info.to, info.from),
7688                ));
7689        }
7690
7691        // This is a new path using an unassigned CID; create it!
7692        let mut path = path::Path::new(
7693            info.to,
7694            info.from,
7695            &self.recovery_config,
7696            self.path_challenge_recv_max_queue_len,
7697            MIN_CLIENT_INITIAL_LEN,
7698            false,
7699        );
7700
7701        path.max_send_bytes = buf_len * self.max_amplification_factor;
7702        path.active_scid_seq = Some(in_scid_seq);
7703
7704        // Automatically probes the new path.
7705        path.request_validation();
7706
7707        let pid = self.paths.insert_path(path, self.is_server)?;
7708
7709        // Do not record path reuse.
7710        if in_scid_pid.is_none() {
7711            ids.link_scid_to_path_id(in_scid_seq, pid)?;
7712        }
7713
7714        Ok(pid)
7715    }
7716
7717    /// Selects the path on which the next packet must be sent.
7718    fn get_send_path_id(
7719        &self, from: Option<SocketAddr>, to: Option<SocketAddr>,
7720    ) -> Result<usize> {
7721        // A probing packet must be sent, but only if the connection is fully
7722        // established.
7723        if self.is_established() {
7724            let mut probing = self
7725                .paths
7726                .iter()
7727                .filter(|(_, p)| from.is_none() || Some(p.local_addr()) == from)
7728                .filter(|(_, p)| to.is_none() || Some(p.peer_addr()) == to)
7729                .filter(|(_, p)| p.active_dcid_seq.is_some())
7730                .filter(|(_, p)| p.probing_required())
7731                .map(|(pid, _)| pid);
7732
7733            if let Some(pid) = probing.next() {
7734                return Ok(pid);
7735            }
7736        }
7737
7738        if let Some((pid, p)) = self.paths.get_active_with_pid() {
7739            if from.is_some() && Some(p.local_addr()) != from {
7740                return Err(Error::Done);
7741            }
7742
7743            if to.is_some() && Some(p.peer_addr()) != to {
7744                return Err(Error::Done);
7745            }
7746
7747            return Ok(pid);
7748        };
7749
7750        Err(Error::InvalidState)
7751    }
7752
7753    /// Sets the path with identifier 'path_id' to be active.
7754    fn set_active_path(
7755        &mut self, path_id: usize, now: time::Instant,
7756    ) -> Result<()> {
7757        if let Ok(old_active_path) = self.paths.get_active_mut() {
7758            for &e in packet::Epoch::epochs(
7759                packet::Epoch::Initial..=packet::Epoch::Application,
7760            ) {
7761                let (lost_packets, lost_bytes) = old_active_path
7762                    .recovery
7763                    .on_path_change(e, now, &self.trace_id);
7764
7765                self.lost_count += lost_packets;
7766                self.lost_bytes += lost_bytes as u64;
7767            }
7768        }
7769
7770        self.paths.set_active_path(path_id)
7771    }
7772
7773    /// Handles potential connection migration.
7774    fn on_peer_migrated(
7775        &mut self, new_pid: usize, disable_dcid_reuse: bool, now: time::Instant,
7776    ) -> Result<()> {
7777        let active_path_id = self.paths.get_active_path_id()?;
7778
7779        if active_path_id == new_pid {
7780            return Ok(());
7781        }
7782
7783        self.set_active_path(new_pid, now)?;
7784
7785        let no_spare_dcid =
7786            self.paths.get_mut(new_pid)?.active_dcid_seq.is_none();
7787
7788        if no_spare_dcid && !disable_dcid_reuse {
7789            self.paths.get_mut(new_pid)?.active_dcid_seq =
7790                self.paths.get_mut(active_path_id)?.active_dcid_seq;
7791        }
7792
7793        Ok(())
7794    }
7795
7796    /// Creates a new client-side path.
7797    fn create_path_on_client(
7798        &mut self, local_addr: SocketAddr, peer_addr: SocketAddr,
7799    ) -> Result<usize> {
7800        if self.is_server {
7801            return Err(Error::InvalidState);
7802        }
7803
7804        // If we use zero-length SCID and go over our local active CID limit,
7805        // the `insert_path()` call will raise an error.
7806        if !self.ids.zero_length_scid() && self.ids.available_scids() == 0 {
7807            return Err(Error::OutOfIdentifiers);
7808        }
7809
7810        // Do we have a spare DCID? If we are using zero-length DCID, just use
7811        // the default having sequence 0 (note that if we exceed our local CID
7812        // limit, the `insert_path()` call will raise an error.
7813        let dcid_seq = if self.ids.zero_length_dcid() {
7814            0
7815        } else {
7816            self.ids
7817                .lowest_available_dcid_seq()
7818                .ok_or(Error::OutOfIdentifiers)?
7819        };
7820
7821        let mut path = path::Path::new(
7822            local_addr,
7823            peer_addr,
7824            &self.recovery_config,
7825            self.path_challenge_recv_max_queue_len,
7826            MIN_CLIENT_INITIAL_LEN,
7827            false,
7828        );
7829        path.active_dcid_seq = Some(dcid_seq);
7830
7831        let pid = self
7832            .paths
7833            .insert_path(path, false)
7834            .map_err(|_| Error::OutOfIdentifiers)?;
7835        self.ids.link_dcid_to_path_id(dcid_seq, pid)?;
7836
7837        Ok(pid)
7838    }
7839
7840    // Marks the connection as closed and does any related tidyup.
7841    fn mark_closed(&mut self) {
7842        #[cfg(feature = "qlog")]
7843        {
7844            let cc = match (self.is_established(), self.timed_out, &self.peer_error, &self.local_error) {
7845                (false, _, _, _) => qlog::events::connectivity::ConnectionClosed {
7846                    owner: Some(TransportOwner::Local),
7847                    connection_code: None,
7848                    application_code: None,
7849                    internal_code: None,
7850                    reason: Some("Failed to establish connection".to_string()),
7851                    trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::HandshakeTimeout)
7852                },
7853
7854                (true, true, _, _) => qlog::events::connectivity::ConnectionClosed {
7855                    owner: Some(TransportOwner::Local),
7856                    connection_code: None,
7857                    application_code: None,
7858                    internal_code: None,
7859                    reason: Some("Idle timeout".to_string()),
7860                    trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::IdleTimeout)
7861                },
7862
7863                (true, false, Some(peer_error), None) => {
7864                    let (connection_code, application_code) = if peer_error.is_app {
7865                        (None, Some(qlog::events::ApplicationErrorCode::Value(peer_error.error_code)))
7866                    } else {
7867                        (Some(qlog::events::ConnectionErrorCode::Value(peer_error.error_code)), None)
7868                    };
7869
7870                    qlog::events::connectivity::ConnectionClosed {
7871                        owner: Some(TransportOwner::Remote),
7872                        connection_code,
7873                        application_code,
7874                        internal_code: None,
7875                        reason: Some(String::from_utf8_lossy(&peer_error.reason).to_string()),
7876                        trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::Error),
7877                    }
7878                },
7879
7880                (true, false, None, Some(local_error)) => {
7881                    let (connection_code, application_code) = if local_error.is_app {
7882                        (None, Some(qlog::events::ApplicationErrorCode::Value(local_error.error_code)))
7883                    } else {
7884                        (Some(qlog::events::ConnectionErrorCode::Value(local_error.error_code)), None)
7885                    };
7886
7887                    qlog::events::connectivity::ConnectionClosed {
7888                        owner: Some(TransportOwner::Local),
7889                        connection_code,
7890                        application_code,
7891                        internal_code: None,
7892                        reason: Some(String::from_utf8_lossy(&local_error.reason).to_string()),
7893                        trigger: Some(qlog::events::connectivity::ConnectionClosedTrigger::Error),
7894                    }
7895                },
7896
7897                _ => qlog::events::connectivity::ConnectionClosed {
7898                    owner: None,
7899                    connection_code: None,
7900                    application_code: None,
7901                    internal_code: None,
7902                    reason: None,
7903                    trigger: None,
7904                },
7905            };
7906
7907            qlog_with_type!(QLOG_CONNECTION_CLOSED, self.qlog, q, {
7908                let ev_data = qlog::events::EventData::ConnectionClosed(cc);
7909
7910                q.add_event_data_now(ev_data).ok();
7911            });
7912            self.qlog.streamer = None;
7913        }
7914        self.closed = true;
7915    }
7916}
7917
7918#[cfg(feature = "boringssl-boring-crate")]
7919impl AsMut<boring::ssl::SslRef> for Connection {
7920    fn as_mut(&mut self) -> &mut boring::ssl::SslRef {
7921        self.handshake.ssl_mut()
7922    }
7923}
7924
7925/// Maps an `Error` to `Error::Done`, or itself.
7926///
7927/// When a received packet that hasn't yet been authenticated triggers a failure
7928/// it should, in most cases, be ignored, instead of raising a connection error,
7929/// to avoid potential man-in-the-middle and man-on-the-side attacks.
7930///
7931/// However, if no other packet was previously received, the connection should
7932/// indeed be closed as the received packet might just be network background
7933/// noise, and it shouldn't keep resources occupied indefinitely.
7934///
7935/// This function maps an error to `Error::Done` to ignore a packet failure
7936/// without aborting the connection, except when no other packet was previously
7937/// received, in which case the error itself is returned, but only on the
7938/// server-side as the client will already have armed the idle timer.
7939///
7940/// This must only be used for errors preceding packet authentication. Failures
7941/// happening after a packet has been authenticated should still cause the
7942/// connection to be aborted.
7943fn drop_pkt_on_err(
7944    e: Error, recv_count: usize, is_server: bool, trace_id: &str,
7945) -> Error {
7946    // On the server, if no other packet has been successfully processed, abort
7947    // the connection to avoid keeping the connection open when only junk is
7948    // received.
7949    if is_server && recv_count == 0 {
7950        return e;
7951    }
7952
7953    trace!("{} dropped invalid packet", trace_id);
7954
7955    // Ignore other invalid packets that haven't been authenticated to prevent
7956    // man-in-the-middle and man-on-the-side attacks.
7957    Error::Done
7958}
7959
7960struct AddrTupleFmt(SocketAddr, SocketAddr);
7961
7962impl std::fmt::Display for AddrTupleFmt {
7963    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7964        let AddrTupleFmt(src, dst) = &self;
7965
7966        if src.ip().is_unspecified() || dst.ip().is_unspecified() {
7967            return Ok(());
7968        }
7969
7970        f.write_fmt(format_args!("src:{src} dst:{dst}"))
7971    }
7972}
7973
7974/// Statistics about the connection.
7975///
7976/// A connection's statistics can be collected using the [`stats()`] method.
7977///
7978/// [`stats()`]: struct.Connection.html#method.stats
7979#[derive(Clone, Default)]
7980pub struct Stats {
7981    /// The number of QUIC packets received.
7982    pub recv: usize,
7983
7984    /// The number of QUIC packets sent.
7985    pub sent: usize,
7986
7987    /// The number of QUIC packets that were lost.
7988    pub lost: usize,
7989
7990    /// The number of sent QUIC packets with retransmitted data.
7991    pub retrans: usize,
7992
7993    /// The number of sent bytes.
7994    pub sent_bytes: u64,
7995
7996    /// The number of received bytes.
7997    pub recv_bytes: u64,
7998
7999    /// The number of bytes sent acked.
8000    pub acked_bytes: u64,
8001
8002    /// The number of bytes sent lost.
8003    pub lost_bytes: u64,
8004
8005    /// The number of stream bytes retransmitted.
8006    pub stream_retrans_bytes: u64,
8007
8008    /// The number of DATAGRAM frames received.
8009    pub dgram_recv: usize,
8010
8011    /// The number of DATAGRAM frames sent.
8012    pub dgram_sent: usize,
8013
8014    /// The number of known paths for the connection.
8015    pub paths_count: usize,
8016
8017    /// The number of streams reset by local.
8018    pub reset_stream_count_local: u64,
8019
8020    /// The number of streams stopped by local.
8021    pub stopped_stream_count_local: u64,
8022
8023    /// The number of streams reset by remote.
8024    pub reset_stream_count_remote: u64,
8025
8026    /// The number of streams stopped by remote.
8027    pub stopped_stream_count_remote: u64,
8028
8029    /// The total number of PATH_CHALLENGE frames that were received.
8030    pub path_challenge_rx_count: u64,
8031}
8032
8033impl std::fmt::Debug for Stats {
8034    #[inline]
8035    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8036        write!(
8037            f,
8038            "recv={} sent={} lost={} retrans={}",
8039            self.recv, self.sent, self.lost, self.retrans,
8040        )?;
8041
8042        write!(
8043            f,
8044            " sent_bytes={} recv_bytes={} lost_bytes={}",
8045            self.sent_bytes, self.recv_bytes, self.lost_bytes,
8046        )?;
8047
8048        Ok(())
8049    }
8050}
8051
8052/// QUIC Unknown Transport Parameter.
8053///
8054/// A QUIC transport parameter that is not specifically recognized
8055/// by this implementation.
8056#[derive(Clone, Debug, PartialEq)]
8057pub struct UnknownTransportParameter<T> {
8058    /// The ID of the unknown transport parameter.
8059    pub id: u64,
8060
8061    /// Original data representing the value of the unknown transport parameter.
8062    pub value: T,
8063}
8064
8065impl<T> UnknownTransportParameter<T> {
8066    /// Checks whether an unknown Transport Parameter's ID is in the reserved
8067    /// space.
8068    ///
8069    /// See Section 18.1 in [RFC9000](https://datatracker.ietf.org/doc/html/rfc9000#name-reserved-transport-paramete).
8070    pub fn is_reserved(&self) -> bool {
8071        let n = (self.id - 27) / 31;
8072        self.id == 31 * n + 27
8073    }
8074}
8075
8076#[cfg(feature = "qlog")]
8077impl From<UnknownTransportParameter<Vec<u8>>>
8078    for qlog::events::quic::UnknownTransportParameter
8079{
8080    fn from(value: UnknownTransportParameter<Vec<u8>>) -> Self {
8081        Self {
8082            id: value.id,
8083            value: qlog::HexSlice::maybe_string(Some(value.value.as_slice()))
8084                .unwrap_or_default(),
8085        }
8086    }
8087}
8088
8089impl From<UnknownTransportParameter<&[u8]>>
8090    for UnknownTransportParameter<Vec<u8>>
8091{
8092    // When an instance of an UnknownTransportParameter is actually
8093    // stored in UnknownTransportParameters, then we make a copy
8094    // of the bytes if the source is an instance of an UnknownTransportParameter
8095    // whose value is not owned.
8096    fn from(value: UnknownTransportParameter<&[u8]>) -> Self {
8097        Self {
8098            id: value.id,
8099            value: value.value.to_vec(),
8100        }
8101    }
8102}
8103
8104/// Track unknown transport parameters, up to a limit.
8105#[derive(Clone, Debug, PartialEq, Default)]
8106pub struct UnknownTransportParameters {
8107    /// The space remaining for storing unknown transport parameters.
8108    pub capacity: usize,
8109    /// The unknown transport parameters.
8110    pub parameters: Vec<UnknownTransportParameter<Vec<u8>>>,
8111}
8112
8113impl UnknownTransportParameters {
8114    /// Pushes an unknown transport parameter into storage if there is space
8115    /// remaining.
8116    pub fn push(&mut self, new: UnknownTransportParameter<&[u8]>) -> Result<()> {
8117        let new_unknown_tp_size = new.value.len() + std::mem::size_of::<u64>();
8118        if new_unknown_tp_size < self.capacity {
8119            self.capacity -= new_unknown_tp_size;
8120            self.parameters.push(new.into());
8121            Ok(())
8122        } else {
8123            Err(BufferTooShortError.into())
8124        }
8125    }
8126}
8127
8128/// An Iterator over unknown transport parameters.
8129pub struct UnknownTransportParameterIterator<'a> {
8130    index: usize,
8131    parameters: &'a Vec<UnknownTransportParameter<Vec<u8>>>,
8132}
8133
8134impl<'a> IntoIterator for &'a UnknownTransportParameters {
8135    type IntoIter = UnknownTransportParameterIterator<'a>;
8136    type Item = &'a UnknownTransportParameter<Vec<u8>>;
8137
8138    fn into_iter(self) -> Self::IntoIter {
8139        UnknownTransportParameterIterator {
8140            index: 0,
8141            parameters: &self.parameters,
8142        }
8143    }
8144}
8145
8146impl<'a> Iterator for UnknownTransportParameterIterator<'a> {
8147    type Item = &'a UnknownTransportParameter<Vec<u8>>;
8148
8149    fn next(&mut self) -> Option<Self::Item> {
8150        let result = self.parameters.get(self.index);
8151        self.index += 1;
8152        result
8153    }
8154}
8155
8156/// QUIC Transport Parameters
8157#[derive(Clone, Debug, PartialEq)]
8158pub struct TransportParams {
8159    /// Value of Destination CID field from first Initial packet sent by client
8160    pub original_destination_connection_id: Option<ConnectionId<'static>>,
8161    /// The maximum idle timeout.
8162    pub max_idle_timeout: u64,
8163    /// Token used for verifying stateless resets
8164    pub stateless_reset_token: Option<u128>,
8165    /// The maximum UDP payload size.
8166    pub max_udp_payload_size: u64,
8167    /// The initial flow control maximum data for the connection.
8168    pub initial_max_data: u64,
8169    /// The initial flow control maximum data for local bidirectional streams.
8170    pub initial_max_stream_data_bidi_local: u64,
8171    /// The initial flow control maximum data for remote bidirectional streams.
8172    pub initial_max_stream_data_bidi_remote: u64,
8173    /// The initial flow control maximum data for unidirectional streams.
8174    pub initial_max_stream_data_uni: u64,
8175    /// The initial maximum bidirectional streams.
8176    pub initial_max_streams_bidi: u64,
8177    /// The initial maximum unidirectional streams.
8178    pub initial_max_streams_uni: u64,
8179    /// The ACK delay exponent.
8180    pub ack_delay_exponent: u64,
8181    /// The max ACK delay.
8182    pub max_ack_delay: u64,
8183    /// Whether active migration is disabled.
8184    pub disable_active_migration: bool,
8185    /// The active connection ID limit.
8186    pub active_conn_id_limit: u64,
8187    /// The value that the endpoint included in the Source CID field of a Retry
8188    /// Packet.
8189    pub initial_source_connection_id: Option<ConnectionId<'static>>,
8190    /// The value that the server included in the Source CID field of a Retry
8191    /// Packet.
8192    pub retry_source_connection_id: Option<ConnectionId<'static>>,
8193    /// DATAGRAM frame extension parameter, if any.
8194    pub max_datagram_frame_size: Option<u64>,
8195    /// Unknown peer transport parameters and values, if any.
8196    pub unknown_params: Option<UnknownTransportParameters>,
8197    // pub preferred_address: ...,
8198}
8199
8200impl Default for TransportParams {
8201    fn default() -> TransportParams {
8202        TransportParams {
8203            original_destination_connection_id: None,
8204            max_idle_timeout: 0,
8205            stateless_reset_token: None,
8206            max_udp_payload_size: 65527,
8207            initial_max_data: 0,
8208            initial_max_stream_data_bidi_local: 0,
8209            initial_max_stream_data_bidi_remote: 0,
8210            initial_max_stream_data_uni: 0,
8211            initial_max_streams_bidi: 0,
8212            initial_max_streams_uni: 0,
8213            ack_delay_exponent: 3,
8214            max_ack_delay: 25,
8215            disable_active_migration: false,
8216            active_conn_id_limit: 2,
8217            initial_source_connection_id: None,
8218            retry_source_connection_id: None,
8219            max_datagram_frame_size: None,
8220            unknown_params: Default::default(),
8221        }
8222    }
8223}
8224
8225impl TransportParams {
8226    fn decode(
8227        buf: &[u8], is_server: bool, unknown_size: Option<usize>,
8228    ) -> Result<TransportParams> {
8229        let mut params = octets::Octets::with_slice(buf);
8230        let mut seen_params = HashSet::new();
8231
8232        let mut tp = TransportParams::default();
8233
8234        if let Some(unknown_transport_param_tracking_size) = unknown_size {
8235            tp.unknown_params = Some(UnknownTransportParameters {
8236                capacity: unknown_transport_param_tracking_size,
8237                parameters: vec![],
8238            });
8239        }
8240
8241        while params.cap() > 0 {
8242            let id = params.get_varint()?;
8243
8244            if seen_params.contains(&id) {
8245                return Err(Error::InvalidTransportParam);
8246            }
8247            seen_params.insert(id);
8248
8249            let mut val = params.get_bytes_with_varint_length()?;
8250
8251            match id {
8252                0x0000 => {
8253                    if is_server {
8254                        return Err(Error::InvalidTransportParam);
8255                    }
8256
8257                    tp.original_destination_connection_id =
8258                        Some(val.to_vec().into());
8259                },
8260
8261                0x0001 => {
8262                    tp.max_idle_timeout = val.get_varint()?;
8263                },
8264
8265                0x0002 => {
8266                    if is_server {
8267                        return Err(Error::InvalidTransportParam);
8268                    }
8269
8270                    tp.stateless_reset_token = Some(u128::from_be_bytes(
8271                        val.get_bytes(16)?
8272                            .to_vec()
8273                            .try_into()
8274                            .map_err(|_| Error::BufferTooShort)?,
8275                    ));
8276                },
8277
8278                0x0003 => {
8279                    tp.max_udp_payload_size = val.get_varint()?;
8280
8281                    if tp.max_udp_payload_size < 1200 {
8282                        return Err(Error::InvalidTransportParam);
8283                    }
8284                },
8285
8286                0x0004 => {
8287                    tp.initial_max_data = val.get_varint()?;
8288                },
8289
8290                0x0005 => {
8291                    tp.initial_max_stream_data_bidi_local = val.get_varint()?;
8292                },
8293
8294                0x0006 => {
8295                    tp.initial_max_stream_data_bidi_remote = val.get_varint()?;
8296                },
8297
8298                0x0007 => {
8299                    tp.initial_max_stream_data_uni = val.get_varint()?;
8300                },
8301
8302                0x0008 => {
8303                    let max = val.get_varint()?;
8304
8305                    if max > MAX_STREAM_ID {
8306                        return Err(Error::InvalidTransportParam);
8307                    }
8308
8309                    tp.initial_max_streams_bidi = max;
8310                },
8311
8312                0x0009 => {
8313                    let max = val.get_varint()?;
8314
8315                    if max > MAX_STREAM_ID {
8316                        return Err(Error::InvalidTransportParam);
8317                    }
8318
8319                    tp.initial_max_streams_uni = max;
8320                },
8321
8322                0x000a => {
8323                    let ack_delay_exponent = val.get_varint()?;
8324
8325                    if ack_delay_exponent > 20 {
8326                        return Err(Error::InvalidTransportParam);
8327                    }
8328
8329                    tp.ack_delay_exponent = ack_delay_exponent;
8330                },
8331
8332                0x000b => {
8333                    let max_ack_delay = val.get_varint()?;
8334
8335                    if max_ack_delay >= 2_u64.pow(14) {
8336                        return Err(Error::InvalidTransportParam);
8337                    }
8338
8339                    tp.max_ack_delay = max_ack_delay;
8340                },
8341
8342                0x000c => {
8343                    tp.disable_active_migration = true;
8344                },
8345
8346                0x000d => {
8347                    if is_server {
8348                        return Err(Error::InvalidTransportParam);
8349                    }
8350
8351                    // TODO: decode preferred_address
8352                },
8353
8354                0x000e => {
8355                    let limit = val.get_varint()?;
8356
8357                    if limit < 2 {
8358                        return Err(Error::InvalidTransportParam);
8359                    }
8360
8361                    tp.active_conn_id_limit = limit;
8362                },
8363
8364                0x000f => {
8365                    tp.initial_source_connection_id = Some(val.to_vec().into());
8366                },
8367
8368                0x00010 => {
8369                    if is_server {
8370                        return Err(Error::InvalidTransportParam);
8371                    }
8372
8373                    tp.retry_source_connection_id = Some(val.to_vec().into());
8374                },
8375
8376                0x0020 => {
8377                    tp.max_datagram_frame_size = Some(val.get_varint()?);
8378                },
8379
8380                // Track unknown transport parameters specially.
8381                unknown_tp_id => {
8382                    if let Some(unknown_params) = &mut tp.unknown_params {
8383                        // It is _not_ an error not to have space enough to track
8384                        // an unknown parameter.
8385                        let _ = unknown_params.push(UnknownTransportParameter {
8386                            id: unknown_tp_id,
8387                            value: val.buf(),
8388                        });
8389                    }
8390                },
8391            }
8392        }
8393
8394        Ok(tp)
8395    }
8396
8397    fn encode_param(
8398        b: &mut octets::OctetsMut, ty: u64, len: usize,
8399    ) -> Result<()> {
8400        b.put_varint(ty)?;
8401        b.put_varint(len as u64)?;
8402
8403        Ok(())
8404    }
8405
8406    fn encode<'a>(
8407        tp: &TransportParams, is_server: bool, out: &'a mut [u8],
8408    ) -> Result<&'a mut [u8]> {
8409        let mut b = octets::OctetsMut::with_slice(out);
8410
8411        if is_server {
8412            if let Some(ref odcid) = tp.original_destination_connection_id {
8413                TransportParams::encode_param(&mut b, 0x0000, odcid.len())?;
8414                b.put_bytes(odcid)?;
8415            }
8416        };
8417
8418        if tp.max_idle_timeout != 0 {
8419            TransportParams::encode_param(
8420                &mut b,
8421                0x0001,
8422                octets::varint_len(tp.max_idle_timeout),
8423            )?;
8424            b.put_varint(tp.max_idle_timeout)?;
8425        }
8426
8427        if is_server {
8428            if let Some(ref token) = tp.stateless_reset_token {
8429                TransportParams::encode_param(&mut b, 0x0002, 16)?;
8430                b.put_bytes(&token.to_be_bytes())?;
8431            }
8432        }
8433
8434        if tp.max_udp_payload_size != 0 {
8435            TransportParams::encode_param(
8436                &mut b,
8437                0x0003,
8438                octets::varint_len(tp.max_udp_payload_size),
8439            )?;
8440            b.put_varint(tp.max_udp_payload_size)?;
8441        }
8442
8443        if tp.initial_max_data != 0 {
8444            TransportParams::encode_param(
8445                &mut b,
8446                0x0004,
8447                octets::varint_len(tp.initial_max_data),
8448            )?;
8449            b.put_varint(tp.initial_max_data)?;
8450        }
8451
8452        if tp.initial_max_stream_data_bidi_local != 0 {
8453            TransportParams::encode_param(
8454                &mut b,
8455                0x0005,
8456                octets::varint_len(tp.initial_max_stream_data_bidi_local),
8457            )?;
8458            b.put_varint(tp.initial_max_stream_data_bidi_local)?;
8459        }
8460
8461        if tp.initial_max_stream_data_bidi_remote != 0 {
8462            TransportParams::encode_param(
8463                &mut b,
8464                0x0006,
8465                octets::varint_len(tp.initial_max_stream_data_bidi_remote),
8466            )?;
8467            b.put_varint(tp.initial_max_stream_data_bidi_remote)?;
8468        }
8469
8470        if tp.initial_max_stream_data_uni != 0 {
8471            TransportParams::encode_param(
8472                &mut b,
8473                0x0007,
8474                octets::varint_len(tp.initial_max_stream_data_uni),
8475            )?;
8476            b.put_varint(tp.initial_max_stream_data_uni)?;
8477        }
8478
8479        if tp.initial_max_streams_bidi != 0 {
8480            TransportParams::encode_param(
8481                &mut b,
8482                0x0008,
8483                octets::varint_len(tp.initial_max_streams_bidi),
8484            )?;
8485            b.put_varint(tp.initial_max_streams_bidi)?;
8486        }
8487
8488        if tp.initial_max_streams_uni != 0 {
8489            TransportParams::encode_param(
8490                &mut b,
8491                0x0009,
8492                octets::varint_len(tp.initial_max_streams_uni),
8493            )?;
8494            b.put_varint(tp.initial_max_streams_uni)?;
8495        }
8496
8497        if tp.ack_delay_exponent != 0 {
8498            TransportParams::encode_param(
8499                &mut b,
8500                0x000a,
8501                octets::varint_len(tp.ack_delay_exponent),
8502            )?;
8503            b.put_varint(tp.ack_delay_exponent)?;
8504        }
8505
8506        if tp.max_ack_delay != 0 {
8507            TransportParams::encode_param(
8508                &mut b,
8509                0x000b,
8510                octets::varint_len(tp.max_ack_delay),
8511            )?;
8512            b.put_varint(tp.max_ack_delay)?;
8513        }
8514
8515        if tp.disable_active_migration {
8516            TransportParams::encode_param(&mut b, 0x000c, 0)?;
8517        }
8518
8519        // TODO: encode preferred_address
8520
8521        if tp.active_conn_id_limit != 2 {
8522            TransportParams::encode_param(
8523                &mut b,
8524                0x000e,
8525                octets::varint_len(tp.active_conn_id_limit),
8526            )?;
8527            b.put_varint(tp.active_conn_id_limit)?;
8528        }
8529
8530        if let Some(scid) = &tp.initial_source_connection_id {
8531            TransportParams::encode_param(&mut b, 0x000f, scid.len())?;
8532            b.put_bytes(scid)?;
8533        }
8534
8535        if is_server {
8536            if let Some(scid) = &tp.retry_source_connection_id {
8537                TransportParams::encode_param(&mut b, 0x0010, scid.len())?;
8538                b.put_bytes(scid)?;
8539            }
8540        }
8541
8542        if let Some(max_datagram_frame_size) = tp.max_datagram_frame_size {
8543            TransportParams::encode_param(
8544                &mut b,
8545                0x0020,
8546                octets::varint_len(max_datagram_frame_size),
8547            )?;
8548            b.put_varint(max_datagram_frame_size)?;
8549        }
8550
8551        let out_len = b.off();
8552
8553        Ok(&mut out[..out_len])
8554    }
8555
8556    /// Creates a qlog event for connection transport parameters and TLS fields
8557    #[cfg(feature = "qlog")]
8558    pub fn to_qlog(
8559        &self, owner: TransportOwner, cipher: Option<crypto::Algorithm>,
8560    ) -> EventData {
8561        let original_destination_connection_id = qlog::HexSlice::maybe_string(
8562            self.original_destination_connection_id.as_ref(),
8563        );
8564
8565        let stateless_reset_token = qlog::HexSlice::maybe_string(
8566            self.stateless_reset_token.map(|s| s.to_be_bytes()).as_ref(),
8567        );
8568
8569        EventData::TransportParametersSet(
8570            qlog::events::quic::TransportParametersSet {
8571                owner: Some(owner),
8572                tls_cipher: Some(format!("{cipher:?}")),
8573                original_destination_connection_id,
8574                stateless_reset_token,
8575                disable_active_migration: Some(self.disable_active_migration),
8576                max_idle_timeout: Some(self.max_idle_timeout),
8577                max_udp_payload_size: Some(self.max_udp_payload_size as u32),
8578                ack_delay_exponent: Some(self.ack_delay_exponent as u16),
8579                max_ack_delay: Some(self.max_ack_delay as u16),
8580                active_connection_id_limit: Some(
8581                    self.active_conn_id_limit as u32,
8582                ),
8583
8584                initial_max_data: Some(self.initial_max_data),
8585                initial_max_stream_data_bidi_local: Some(
8586                    self.initial_max_stream_data_bidi_local,
8587                ),
8588                initial_max_stream_data_bidi_remote: Some(
8589                    self.initial_max_stream_data_bidi_remote,
8590                ),
8591                initial_max_stream_data_uni: Some(
8592                    self.initial_max_stream_data_uni,
8593                ),
8594                initial_max_streams_bidi: Some(self.initial_max_streams_bidi),
8595                initial_max_streams_uni: Some(self.initial_max_streams_uni),
8596
8597                unknown_parameters: self
8598                    .unknown_params
8599                    .as_ref()
8600                    .map(|unknown_params| {
8601                        unknown_params
8602                            .into_iter()
8603                            .cloned()
8604                            .map(
8605                                Into::<
8606                                    qlog::events::quic::UnknownTransportParameter,
8607                                >::into,
8608                            )
8609                            .collect()
8610                    })
8611                    .unwrap_or_default(),
8612
8613                ..Default::default()
8614            },
8615        )
8616    }
8617}
8618
8619#[doc(hidden)]
8620pub mod testing {
8621    use super::*;
8622
8623    pub struct Pipe {
8624        pub client: Connection,
8625        pub server: Connection,
8626    }
8627
8628    impl Pipe {
8629        pub fn new() -> Result<Pipe> {
8630            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
8631            config.load_cert_chain_from_pem_file("examples/cert.crt")?;
8632            config.load_priv_key_from_pem_file("examples/cert.key")?;
8633            config.set_application_protos(&[b"proto1", b"proto2"])?;
8634            config.set_initial_max_data(30);
8635            config.set_initial_max_stream_data_bidi_local(15);
8636            config.set_initial_max_stream_data_bidi_remote(15);
8637            config.set_initial_max_stream_data_uni(10);
8638            config.set_initial_max_streams_bidi(3);
8639            config.set_initial_max_streams_uni(3);
8640            config.set_max_idle_timeout(180_000);
8641            config.verify_peer(false);
8642            config.set_ack_delay_exponent(8);
8643
8644            Pipe::with_config(&mut config)
8645        }
8646
8647        pub fn client_addr() -> SocketAddr {
8648            "127.0.0.1:1234".parse().unwrap()
8649        }
8650
8651        pub fn server_addr() -> SocketAddr {
8652            "127.0.0.1:4321".parse().unwrap()
8653        }
8654
8655        pub fn with_config(config: &mut Config) -> Result<Pipe> {
8656            let mut client_scid = [0; 16];
8657            rand::rand_bytes(&mut client_scid[..]);
8658            let client_scid = ConnectionId::from_ref(&client_scid);
8659            let client_addr = Pipe::client_addr();
8660
8661            let mut server_scid = [0; 16];
8662            rand::rand_bytes(&mut server_scid[..]);
8663            let server_scid = ConnectionId::from_ref(&server_scid);
8664            let server_addr = Pipe::server_addr();
8665
8666            Ok(Pipe {
8667                client: connect(
8668                    Some("quic.tech"),
8669                    &client_scid,
8670                    client_addr,
8671                    server_addr,
8672                    config,
8673                )?,
8674                server: accept(
8675                    &server_scid,
8676                    None,
8677                    server_addr,
8678                    client_addr,
8679                    config,
8680                )?,
8681            })
8682        }
8683
8684        pub fn with_config_and_scid_lengths(
8685            config: &mut Config, client_scid_len: usize, server_scid_len: usize,
8686        ) -> Result<Pipe> {
8687            let mut client_scid = vec![0; client_scid_len];
8688            rand::rand_bytes(&mut client_scid[..]);
8689            let client_scid = ConnectionId::from_ref(&client_scid);
8690            let client_addr = Pipe::client_addr();
8691
8692            let mut server_scid = vec![0; server_scid_len];
8693            rand::rand_bytes(&mut server_scid[..]);
8694            let server_scid = ConnectionId::from_ref(&server_scid);
8695            let server_addr = Pipe::server_addr();
8696
8697            Ok(Pipe {
8698                client: connect(
8699                    Some("quic.tech"),
8700                    &client_scid,
8701                    client_addr,
8702                    server_addr,
8703                    config,
8704                )?,
8705                server: accept(
8706                    &server_scid,
8707                    None,
8708                    server_addr,
8709                    client_addr,
8710                    config,
8711                )?,
8712            })
8713        }
8714
8715        pub fn with_client_config(client_config: &mut Config) -> Result<Pipe> {
8716            let mut client_scid = [0; 16];
8717            rand::rand_bytes(&mut client_scid[..]);
8718            let client_scid = ConnectionId::from_ref(&client_scid);
8719            let client_addr = Pipe::client_addr();
8720
8721            let mut server_scid = [0; 16];
8722            rand::rand_bytes(&mut server_scid[..]);
8723            let server_scid = ConnectionId::from_ref(&server_scid);
8724            let server_addr = Pipe::server_addr();
8725
8726            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
8727            config.load_cert_chain_from_pem_file("examples/cert.crt")?;
8728            config.load_priv_key_from_pem_file("examples/cert.key")?;
8729            config.set_application_protos(&[b"proto1", b"proto2"])?;
8730            config.set_initial_max_data(30);
8731            config.set_initial_max_stream_data_bidi_local(15);
8732            config.set_initial_max_stream_data_bidi_remote(15);
8733            config.set_initial_max_streams_bidi(3);
8734            config.set_initial_max_streams_uni(3);
8735            config.set_ack_delay_exponent(8);
8736
8737            Ok(Pipe {
8738                client: connect(
8739                    Some("quic.tech"),
8740                    &client_scid,
8741                    client_addr,
8742                    server_addr,
8743                    client_config,
8744                )?,
8745                server: accept(
8746                    &server_scid,
8747                    None,
8748                    server_addr,
8749                    client_addr,
8750                    &mut config,
8751                )?,
8752            })
8753        }
8754
8755        pub fn with_server_config(server_config: &mut Config) -> Result<Pipe> {
8756            let mut client_scid = [0; 16];
8757            rand::rand_bytes(&mut client_scid[..]);
8758            let client_scid = ConnectionId::from_ref(&client_scid);
8759            let client_addr = Pipe::client_addr();
8760
8761            let mut server_scid = [0; 16];
8762            rand::rand_bytes(&mut server_scid[..]);
8763            let server_scid = ConnectionId::from_ref(&server_scid);
8764            let server_addr = Pipe::server_addr();
8765
8766            let mut config = Config::new(crate::PROTOCOL_VERSION)?;
8767            config.set_application_protos(&[b"proto1", b"proto2"])?;
8768            config.set_initial_max_data(30);
8769            config.set_initial_max_stream_data_bidi_local(15);
8770            config.set_initial_max_stream_data_bidi_remote(15);
8771            config.set_initial_max_streams_bidi(3);
8772            config.set_initial_max_streams_uni(3);
8773            config.set_ack_delay_exponent(8);
8774
8775            Ok(Pipe {
8776                client: connect(
8777                    Some("quic.tech"),
8778                    &client_scid,
8779                    client_addr,
8780                    server_addr,
8781                    &mut config,
8782                )?,
8783                server: accept(
8784                    &server_scid,
8785                    None,
8786                    server_addr,
8787                    client_addr,
8788                    server_config,
8789                )?,
8790            })
8791        }
8792
8793        pub fn with_client_and_server_config(
8794            client_config: &mut Config, server_config: &mut Config,
8795        ) -> Result<Pipe> {
8796            let mut client_scid = [0; 16];
8797            rand::rand_bytes(&mut client_scid[..]);
8798            let client_scid = ConnectionId::from_ref(&client_scid);
8799            let client_addr = Pipe::client_addr();
8800
8801            let mut server_scid = [0; 16];
8802            rand::rand_bytes(&mut server_scid[..]);
8803            let server_scid = ConnectionId::from_ref(&server_scid);
8804            let server_addr = Pipe::server_addr();
8805
8806            Ok(Pipe {
8807                client: connect(
8808                    Some("quic.tech"),
8809                    &client_scid,
8810                    client_addr,
8811                    server_addr,
8812                    client_config,
8813                )?,
8814                server: accept(
8815                    &server_scid,
8816                    None,
8817                    server_addr,
8818                    client_addr,
8819                    server_config,
8820                )?,
8821            })
8822        }
8823
8824        pub fn handshake(&mut self) -> Result<()> {
8825            while !self.client.is_established() || !self.server.is_established() {
8826                let flight = emit_flight(&mut self.client)?;
8827                process_flight(&mut self.server, flight)?;
8828
8829                let flight = emit_flight(&mut self.server)?;
8830                process_flight(&mut self.client, flight)?;
8831            }
8832
8833            Ok(())
8834        }
8835
8836        pub fn advance(&mut self) -> Result<()> {
8837            let mut client_done = false;
8838            let mut server_done = false;
8839
8840            while !client_done || !server_done {
8841                match emit_flight(&mut self.client) {
8842                    Ok(flight) => process_flight(&mut self.server, flight)?,
8843
8844                    Err(Error::Done) => client_done = true,
8845
8846                    Err(e) => return Err(e),
8847                };
8848
8849                match emit_flight(&mut self.server) {
8850                    Ok(flight) => process_flight(&mut self.client, flight)?,
8851
8852                    Err(Error::Done) => server_done = true,
8853
8854                    Err(e) => return Err(e),
8855                };
8856            }
8857
8858            Ok(())
8859        }
8860
8861        pub fn client_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
8862            let server_path = &self.server.paths.get_active().unwrap();
8863            let info = RecvInfo {
8864                to: server_path.peer_addr(),
8865                from: server_path.local_addr(),
8866            };
8867
8868            self.client.recv(buf, info)
8869        }
8870
8871        pub fn server_recv(&mut self, buf: &mut [u8]) -> Result<usize> {
8872            let client_path = &self.client.paths.get_active().unwrap();
8873            let info = RecvInfo {
8874                to: client_path.peer_addr(),
8875                from: client_path.local_addr(),
8876            };
8877
8878            self.server.recv(buf, info)
8879        }
8880
8881        pub fn send_pkt_to_server(
8882            &mut self, pkt_type: packet::Type, frames: &[frame::Frame],
8883            buf: &mut [u8],
8884        ) -> Result<usize> {
8885            let written = encode_pkt(&mut self.client, pkt_type, frames, buf)?;
8886            recv_send(&mut self.server, buf, written)
8887        }
8888
8889        pub fn client_update_key(&mut self) -> Result<()> {
8890            let space =
8891                &mut self.client.pkt_num_spaces[packet::Epoch::Application];
8892
8893            let open_next = space
8894                .crypto_open
8895                .as_ref()
8896                .unwrap()
8897                .derive_next_packet_key()
8898                .unwrap();
8899
8900            let seal_next = space
8901                .crypto_seal
8902                .as_ref()
8903                .unwrap()
8904                .derive_next_packet_key()?;
8905
8906            let open_prev = space.crypto_open.replace(open_next);
8907            space.crypto_seal.replace(seal_next);
8908
8909            space.key_update = Some(packet::KeyUpdate {
8910                crypto_open: open_prev.unwrap(),
8911                pn_on_update: self.client.next_pkt_num,
8912                update_acked: true,
8913                timer: time::Instant::now(),
8914            });
8915
8916            self.client.key_phase = !self.client.key_phase;
8917
8918            Ok(())
8919        }
8920    }
8921
8922    pub fn recv_send(
8923        conn: &mut Connection, buf: &mut [u8], len: usize,
8924    ) -> Result<usize> {
8925        let active_path = conn.paths.get_active()?;
8926        let info = RecvInfo {
8927            to: active_path.local_addr(),
8928            from: active_path.peer_addr(),
8929        };
8930
8931        conn.recv(&mut buf[..len], info)?;
8932
8933        let mut off = 0;
8934
8935        match conn.send(&mut buf[off..]) {
8936            Ok((write, _)) => off += write,
8937
8938            Err(Error::Done) => (),
8939
8940            Err(e) => return Err(e),
8941        }
8942
8943        Ok(off)
8944    }
8945
8946    pub fn process_flight(
8947        conn: &mut Connection, flight: Vec<(Vec<u8>, SendInfo)>,
8948    ) -> Result<()> {
8949        for (mut pkt, si) in flight {
8950            let info = RecvInfo {
8951                to: si.to,
8952                from: si.from,
8953            };
8954
8955            conn.recv(&mut pkt, info)?;
8956        }
8957
8958        Ok(())
8959    }
8960
8961    pub fn emit_flight_with_max_buffer(
8962        conn: &mut Connection, out_size: usize, from: Option<SocketAddr>,
8963        to: Option<SocketAddr>,
8964    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
8965        let mut flight = Vec::new();
8966
8967        loop {
8968            let mut out = vec![0u8; out_size];
8969
8970            let info = match conn.send_on_path(&mut out, from, to) {
8971                Ok((written, info)) => {
8972                    out.truncate(written);
8973                    info
8974                },
8975
8976                Err(Error::Done) => break,
8977
8978                Err(e) => return Err(e),
8979            };
8980
8981            flight.push((out, info));
8982        }
8983
8984        if flight.is_empty() {
8985            return Err(Error::Done);
8986        }
8987
8988        Ok(flight)
8989    }
8990
8991    pub fn emit_flight_on_path(
8992        conn: &mut Connection, from: Option<SocketAddr>, to: Option<SocketAddr>,
8993    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
8994        emit_flight_with_max_buffer(conn, 65535, from, to)
8995    }
8996
8997    pub fn emit_flight(
8998        conn: &mut Connection,
8999    ) -> Result<Vec<(Vec<u8>, SendInfo)>> {
9000        emit_flight_on_path(conn, None, None)
9001    }
9002
9003    pub fn encode_pkt(
9004        conn: &mut Connection, pkt_type: packet::Type, frames: &[frame::Frame],
9005        buf: &mut [u8],
9006    ) -> Result<usize> {
9007        let mut b = octets::OctetsMut::with_slice(buf);
9008
9009        let epoch = pkt_type.to_epoch()?;
9010
9011        let space = &mut conn.pkt_num_spaces[epoch];
9012
9013        let pn = conn.next_pkt_num;
9014        let pn_len = 4;
9015
9016        let send_path = conn.paths.get_active()?;
9017        let active_dcid_seq = send_path
9018            .active_dcid_seq
9019            .as_ref()
9020            .ok_or(Error::InvalidState)?;
9021        let active_scid_seq = send_path
9022            .active_scid_seq
9023            .as_ref()
9024            .ok_or(Error::InvalidState)?;
9025
9026        let hdr = Header {
9027            ty: pkt_type,
9028            version: conn.version,
9029            dcid: ConnectionId::from_ref(
9030                conn.ids.get_dcid(*active_dcid_seq)?.cid.as_ref(),
9031            ),
9032            scid: ConnectionId::from_ref(
9033                conn.ids.get_scid(*active_scid_seq)?.cid.as_ref(),
9034            ),
9035            pkt_num: pn,
9036            pkt_num_len: pn_len,
9037            token: conn.token.clone(),
9038            versions: None,
9039            key_phase: conn.key_phase,
9040        };
9041
9042        hdr.to_bytes(&mut b)?;
9043
9044        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
9045
9046        if pkt_type != packet::Type::Short {
9047            let len = pn_len + payload_len + space.crypto_overhead().unwrap();
9048            b.put_varint(len as u64)?;
9049        }
9050
9051        // Always encode packet number in 4 bytes, to allow encoding packets
9052        // with empty payloads.
9053        b.put_u32(pn as u32)?;
9054
9055        let payload_offset = b.off();
9056
9057        for frame in frames {
9058            frame.to_bytes(&mut b)?;
9059        }
9060
9061        let aead = match space.crypto_seal {
9062            Some(ref v) => v,
9063            None => return Err(Error::InvalidState),
9064        };
9065
9066        let written = packet::encrypt_pkt(
9067            &mut b,
9068            pn,
9069            pn_len,
9070            payload_len,
9071            payload_offset,
9072            None,
9073            aead,
9074        )?;
9075
9076        conn.next_pkt_num += 1;
9077
9078        Ok(written)
9079    }
9080
9081    pub fn decode_pkt(
9082        conn: &mut Connection, buf: &mut [u8],
9083    ) -> Result<Vec<frame::Frame>> {
9084        let mut b = octets::OctetsMut::with_slice(buf);
9085
9086        let mut hdr = Header::from_bytes(&mut b, conn.source_id().len()).unwrap();
9087
9088        let epoch = hdr.ty.to_epoch()?;
9089
9090        let aead = conn.pkt_num_spaces[epoch].crypto_open.as_ref().unwrap();
9091
9092        let payload_len = b.cap();
9093
9094        packet::decrypt_hdr(&mut b, &mut hdr, aead).unwrap();
9095
9096        let pn = packet::decode_pkt_num(
9097            conn.pkt_num_spaces[epoch].largest_rx_pkt_num,
9098            hdr.pkt_num,
9099            hdr.pkt_num_len,
9100        );
9101
9102        let mut payload =
9103            packet::decrypt_pkt(&mut b, pn, hdr.pkt_num_len, payload_len, aead)
9104                .unwrap();
9105
9106        let mut frames = Vec::new();
9107
9108        while payload.cap() > 0 {
9109            let frame = frame::Frame::from_bytes(&mut payload, hdr.ty)?;
9110            frames.push(frame);
9111        }
9112
9113        Ok(frames)
9114    }
9115
9116    pub fn create_cid_and_reset_token(
9117        cid_len: usize,
9118    ) -> (ConnectionId<'static>, u128) {
9119        let mut cid = vec![0; cid_len];
9120        rand::rand_bytes(&mut cid[..]);
9121        let cid = ConnectionId::from_ref(&cid).into_owned();
9122
9123        let mut reset_token = [0; 16];
9124        rand::rand_bytes(&mut reset_token);
9125        let reset_token = u128::from_be_bytes(reset_token);
9126
9127        (cid, reset_token)
9128    }
9129}
9130
9131#[cfg(test)]
9132mod tests {
9133    use super::*;
9134
9135    #[test]
9136    fn transport_params() {
9137        // Server encodes, client decodes.
9138        let tp = TransportParams {
9139            original_destination_connection_id: None,
9140            max_idle_timeout: 30,
9141            stateless_reset_token: Some(u128::from_be_bytes([0xba; 16])),
9142            max_udp_payload_size: 23_421,
9143            initial_max_data: 424_645_563,
9144            initial_max_stream_data_bidi_local: 154_323_123,
9145            initial_max_stream_data_bidi_remote: 6_587_456,
9146            initial_max_stream_data_uni: 2_461_234,
9147            initial_max_streams_bidi: 12_231,
9148            initial_max_streams_uni: 18_473,
9149            ack_delay_exponent: 20,
9150            max_ack_delay: 2_u64.pow(14) - 1,
9151            disable_active_migration: true,
9152            active_conn_id_limit: 8,
9153            initial_source_connection_id: Some(b"woot woot".to_vec().into()),
9154            retry_source_connection_id: Some(b"retry".to_vec().into()),
9155            max_datagram_frame_size: Some(32),
9156            unknown_params: Default::default(),
9157        };
9158
9159        let mut raw_params = [42; 256];
9160        let raw_params =
9161            TransportParams::encode(&tp, true, &mut raw_params).unwrap();
9162        assert_eq!(raw_params.len(), 94);
9163
9164        let new_tp = TransportParams::decode(raw_params, false, None).unwrap();
9165
9166        assert_eq!(new_tp, tp);
9167
9168        // Client encodes, server decodes.
9169        let tp = TransportParams {
9170            original_destination_connection_id: None,
9171            max_idle_timeout: 30,
9172            stateless_reset_token: None,
9173            max_udp_payload_size: 23_421,
9174            initial_max_data: 424_645_563,
9175            initial_max_stream_data_bidi_local: 154_323_123,
9176            initial_max_stream_data_bidi_remote: 6_587_456,
9177            initial_max_stream_data_uni: 2_461_234,
9178            initial_max_streams_bidi: 12_231,
9179            initial_max_streams_uni: 18_473,
9180            ack_delay_exponent: 20,
9181            max_ack_delay: 2_u64.pow(14) - 1,
9182            disable_active_migration: true,
9183            active_conn_id_limit: 8,
9184            initial_source_connection_id: Some(b"woot woot".to_vec().into()),
9185            retry_source_connection_id: None,
9186            max_datagram_frame_size: Some(32),
9187            unknown_params: Default::default(),
9188        };
9189
9190        let mut raw_params = [42; 256];
9191        let raw_params =
9192            TransportParams::encode(&tp, false, &mut raw_params).unwrap();
9193        assert_eq!(raw_params.len(), 69);
9194
9195        let new_tp = TransportParams::decode(raw_params, true, None).unwrap();
9196
9197        assert_eq!(new_tp, tp);
9198    }
9199
9200    #[test]
9201    fn transport_params_forbid_duplicates() {
9202        // Given an encoded param.
9203        let initial_source_connection_id = b"id";
9204        let initial_source_connection_id_raw = [
9205            15,
9206            initial_source_connection_id.len() as u8,
9207            initial_source_connection_id[0],
9208            initial_source_connection_id[1],
9209        ];
9210
9211        // No error when decoding the param.
9212        let tp = TransportParams::decode(
9213            initial_source_connection_id_raw.as_slice(),
9214            true,
9215            None,
9216        )
9217        .unwrap();
9218
9219        assert_eq!(
9220            tp.initial_source_connection_id,
9221            Some(initial_source_connection_id.to_vec().into())
9222        );
9223
9224        // Duplicate the param.
9225        let mut raw_params = Vec::new();
9226        raw_params.append(&mut initial_source_connection_id_raw.to_vec());
9227        raw_params.append(&mut initial_source_connection_id_raw.to_vec());
9228
9229        // Decoding fails.
9230        assert_eq!(
9231            TransportParams::decode(raw_params.as_slice(), true, None),
9232            Err(Error::InvalidTransportParam)
9233        );
9234    }
9235
9236    #[test]
9237    fn transport_params_unknown_zero_space() {
9238        let mut unknown_params: UnknownTransportParameters =
9239            UnknownTransportParameters {
9240                capacity: 0,
9241                parameters: vec![],
9242            };
9243        let massive_unknown_param = UnknownTransportParameter::<&[u8]> {
9244            id: 5,
9245            value: &[0xau8; 280],
9246        };
9247        assert!(unknown_params.push(massive_unknown_param).is_err());
9248        assert!(unknown_params.capacity == 0);
9249        assert!(unknown_params.parameters.is_empty());
9250    }
9251
9252    #[test]
9253    fn transport_params_unknown_max_space_respected() {
9254        let mut unknown_params: UnknownTransportParameters =
9255            UnknownTransportParameters {
9256                capacity: 256,
9257                parameters: vec![],
9258            };
9259
9260        let massive_unknown_param = UnknownTransportParameter::<&[u8]> {
9261            id: 5,
9262            value: &[0xau8; 280],
9263        };
9264        let big_unknown_param = UnknownTransportParameter::<&[u8]> {
9265            id: 5,
9266            value: &[0xau8; 232],
9267        };
9268        let little_unknown_param = UnknownTransportParameter::<&[u8]> {
9269            id: 6,
9270            value: &[0xau8; 7],
9271        };
9272
9273        assert!(unknown_params.push(massive_unknown_param).is_err());
9274        assert!(unknown_params.capacity == 256);
9275        assert!(unknown_params.parameters.is_empty());
9276
9277        unknown_params.push(big_unknown_param).unwrap();
9278        assert!(unknown_params.capacity == 16);
9279        assert!(unknown_params.parameters.len() == 1);
9280
9281        unknown_params.push(little_unknown_param.clone()).unwrap();
9282        assert!(unknown_params.capacity == 1);
9283        assert!(unknown_params.parameters.len() == 2);
9284
9285        assert!(unknown_params.push(little_unknown_param).is_err());
9286
9287        let mut unknown_params_iter = unknown_params.into_iter();
9288
9289        let unknown_params_first = unknown_params_iter
9290            .next()
9291            .expect("Should have a 0th element.");
9292        assert!(
9293            unknown_params_first.id == 5 &&
9294                unknown_params_first.value == vec![0xau8; 232]
9295        );
9296
9297        let unknown_params_second = unknown_params_iter
9298            .next()
9299            .expect("Should have a 1th element.");
9300        assert!(
9301            unknown_params_second.id == 6 &&
9302                unknown_params_second.value == vec![0xau8; 7]
9303        );
9304    }
9305
9306    #[test]
9307    fn transport_params_unknown_is_reserved() {
9308        let reserved_unknown_param = UnknownTransportParameter::<&[u8]> {
9309            id: 31 * 17 + 27,
9310            value: &[0xau8; 280],
9311        };
9312        let not_reserved_unknown_param = UnknownTransportParameter::<&[u8]> {
9313            id: 32 * 17 + 27,
9314            value: &[0xau8; 280],
9315        };
9316
9317        assert!(reserved_unknown_param.is_reserved());
9318        assert!(!not_reserved_unknown_param.is_reserved());
9319    }
9320    #[test]
9321    fn unknown_version() {
9322        let mut config = Config::new(0xbabababa).unwrap();
9323        config
9324            .set_application_protos(&[b"proto1", b"proto2"])
9325            .unwrap();
9326        config.verify_peer(false);
9327
9328        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9329        assert_eq!(pipe.handshake(), Err(Error::UnknownVersion));
9330    }
9331
9332    #[test]
9333    fn config_version_reserved() {
9334        Config::new(0xbabababa).unwrap();
9335        Config::new(0x1a2a3a4a).unwrap();
9336    }
9337
9338    #[test]
9339    fn config_version_invalid() {
9340        assert_eq!(
9341            Config::new(0xb1bababa).err().unwrap(),
9342            Error::UnknownVersion
9343        );
9344    }
9345
9346    #[test]
9347    fn version_negotiation() {
9348        let mut buf = [0; 65535];
9349
9350        let mut config = Config::new(0xbabababa).unwrap();
9351        config
9352            .set_application_protos(&[b"proto1", b"proto2"])
9353            .unwrap();
9354        config.verify_peer(false);
9355
9356        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9357
9358        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
9359
9360        let hdr = packet::Header::from_slice(&mut buf[..len], 0).unwrap();
9361        len = crate::negotiate_version(&hdr.scid, &hdr.dcid, &mut buf).unwrap();
9362
9363        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
9364
9365        assert_eq!(pipe.handshake(), Ok(()));
9366
9367        assert_eq!(pipe.client.version, PROTOCOL_VERSION);
9368        assert_eq!(pipe.server.version, PROTOCOL_VERSION);
9369    }
9370
9371    #[test]
9372    fn verify_custom_root() {
9373        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
9374        config.verify_peer(true);
9375        config
9376            .load_verify_locations_from_file("examples/rootca.crt")
9377            .unwrap();
9378        config
9379            .set_application_protos(&[b"proto1", b"proto2"])
9380            .unwrap();
9381
9382        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9383        assert_eq!(pipe.handshake(), Ok(()));
9384    }
9385
9386    // Disable this for openssl as it seems to fail for some reason. It could be
9387    // because of the way the get_certs API differs from bssl.
9388    #[cfg(not(feature = "openssl"))]
9389    #[test]
9390    fn verify_client_invalid() {
9391        let mut server_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9392        server_config
9393            .load_cert_chain_from_pem_file("examples/cert.crt")
9394            .unwrap();
9395        server_config
9396            .load_priv_key_from_pem_file("examples/cert.key")
9397            .unwrap();
9398        server_config
9399            .set_application_protos(&[b"proto1", b"proto2"])
9400            .unwrap();
9401        server_config.set_initial_max_data(30);
9402        server_config.set_initial_max_stream_data_bidi_local(15);
9403        server_config.set_initial_max_stream_data_bidi_remote(15);
9404        server_config.set_initial_max_streams_bidi(3);
9405
9406        // The server shouldn't be able to verify the client's certificate due
9407        // to missing CA.
9408        server_config.verify_peer(true);
9409
9410        let mut client_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9411        client_config
9412            .load_cert_chain_from_pem_file("examples/cert.crt")
9413            .unwrap();
9414        client_config
9415            .load_priv_key_from_pem_file("examples/cert.key")
9416            .unwrap();
9417        client_config
9418            .set_application_protos(&[b"proto1", b"proto2"])
9419            .unwrap();
9420        client_config.set_initial_max_data(30);
9421        client_config.set_initial_max_stream_data_bidi_local(15);
9422        client_config.set_initial_max_stream_data_bidi_remote(15);
9423        client_config.set_initial_max_streams_bidi(3);
9424
9425        // The client is able to verify the server's certificate with the
9426        // appropriate CA.
9427        client_config
9428            .load_verify_locations_from_file("examples/rootca.crt")
9429            .unwrap();
9430        client_config.verify_peer(true);
9431
9432        let mut pipe = testing::Pipe::with_client_and_server_config(
9433            &mut client_config,
9434            &mut server_config,
9435        )
9436        .unwrap();
9437        assert_eq!(pipe.handshake(), Err(Error::TlsFail));
9438
9439        // Client did send a certificate.
9440        assert!(pipe.server.peer_cert().is_some());
9441    }
9442
9443    #[test]
9444    fn verify_client_anonymous() {
9445        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9446        config
9447            .load_cert_chain_from_pem_file("examples/cert.crt")
9448            .unwrap();
9449        config
9450            .load_priv_key_from_pem_file("examples/cert.key")
9451            .unwrap();
9452        config
9453            .set_application_protos(&[b"proto1", b"proto2"])
9454            .unwrap();
9455        config.set_initial_max_data(30);
9456        config.set_initial_max_stream_data_bidi_local(15);
9457        config.set_initial_max_stream_data_bidi_remote(15);
9458        config.set_initial_max_streams_bidi(3);
9459
9460        // Try to validate client certificate.
9461        config.verify_peer(true);
9462
9463        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
9464        assert_eq!(pipe.handshake(), Ok(()));
9465
9466        // Client didn't send a certificate.
9467        assert!(pipe.server.peer_cert().is_none());
9468    }
9469
9470    #[test]
9471    fn missing_initial_source_connection_id() {
9472        let mut buf = [0; 65535];
9473
9474        let mut pipe = testing::Pipe::new().unwrap();
9475
9476        // Reset initial_source_connection_id.
9477        pipe.client
9478            .local_transport_params
9479            .initial_source_connection_id = None;
9480        assert_eq!(pipe.client.encode_transport_params(), Ok(()));
9481
9482        // Client sends initial flight.
9483        let (len, _) = pipe.client.send(&mut buf).unwrap();
9484
9485        // Server rejects transport parameters.
9486        assert_eq!(
9487            pipe.server_recv(&mut buf[..len]),
9488            Err(Error::InvalidTransportParam)
9489        );
9490    }
9491
9492    #[test]
9493    fn invalid_initial_source_connection_id() {
9494        let mut buf = [0; 65535];
9495
9496        let mut pipe = testing::Pipe::new().unwrap();
9497
9498        // Scramble initial_source_connection_id.
9499        pipe.client
9500            .local_transport_params
9501            .initial_source_connection_id = Some(b"bogus value".to_vec().into());
9502        assert_eq!(pipe.client.encode_transport_params(), Ok(()));
9503
9504        // Client sends initial flight.
9505        let (len, _) = pipe.client.send(&mut buf).unwrap();
9506
9507        // Server rejects transport parameters.
9508        assert_eq!(
9509            pipe.server_recv(&mut buf[..len]),
9510            Err(Error::InvalidTransportParam)
9511        );
9512    }
9513
9514    #[test]
9515    fn change_idle_timeout() {
9516        let mut config = Config::new(0x1).unwrap();
9517        config
9518            .set_application_protos(&[b"proto1", b"proto2"])
9519            .unwrap();
9520        config.set_max_idle_timeout(999999);
9521        config.verify_peer(false);
9522
9523        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9524        assert_eq!(pipe.client.local_transport_params.max_idle_timeout, 999999);
9525        assert_eq!(pipe.client.peer_transport_params.max_idle_timeout, 0);
9526        assert_eq!(pipe.server.local_transport_params.max_idle_timeout, 0);
9527        assert_eq!(pipe.server.peer_transport_params.max_idle_timeout, 0);
9528
9529        pipe.client.set_max_idle_timeout(456000).unwrap();
9530        pipe.server.set_max_idle_timeout(234000).unwrap();
9531        assert_eq!(pipe.client.local_transport_params.max_idle_timeout, 456000);
9532        assert_eq!(pipe.client.peer_transport_params.max_idle_timeout, 0);
9533        assert_eq!(pipe.server.local_transport_params.max_idle_timeout, 234000);
9534        assert_eq!(pipe.server.peer_transport_params.max_idle_timeout, 0);
9535
9536        assert_eq!(pipe.handshake(), Ok(()));
9537
9538        assert_eq!(
9539            pipe.client.idle_timeout(),
9540            Some(time::Duration::from_millis(234000))
9541        );
9542        assert_eq!(
9543            pipe.server.idle_timeout(),
9544            Some(time::Duration::from_millis(234000))
9545        );
9546    }
9547
9548    #[test]
9549    fn handshake() {
9550        let mut pipe = testing::Pipe::new().unwrap();
9551        assert_eq!(pipe.handshake(), Ok(()));
9552
9553        assert_eq!(
9554            pipe.client.application_proto(),
9555            pipe.server.application_proto()
9556        );
9557
9558        assert_eq!(pipe.server.server_name(), Some("quic.tech"));
9559    }
9560
9561    #[test]
9562    fn handshake_done() {
9563        let mut pipe = testing::Pipe::new().unwrap();
9564
9565        // Disable session tickets on the server (SSL_OP_NO_TICKET) to avoid
9566        // triggering 1-RTT packet send with a CRYPTO frame.
9567        pipe.server.handshake.set_options(0x0000_4000);
9568
9569        assert_eq!(pipe.handshake(), Ok(()));
9570
9571        assert!(pipe.server.handshake_done_sent);
9572    }
9573
9574    #[test]
9575    fn handshake_confirmation() {
9576        let mut pipe = testing::Pipe::new().unwrap();
9577
9578        // Client sends initial flight.
9579        let flight = testing::emit_flight(&mut pipe.client).unwrap();
9580        testing::process_flight(&mut pipe.server, flight).unwrap();
9581
9582        // Server sends initial flight.
9583        let flight = testing::emit_flight(&mut pipe.server).unwrap();
9584
9585        assert!(!pipe.client.is_established());
9586        assert!(!pipe.client.handshake_confirmed);
9587
9588        assert!(!pipe.server.is_established());
9589        assert!(!pipe.server.handshake_confirmed);
9590
9591        testing::process_flight(&mut pipe.client, flight).unwrap();
9592
9593        // Client sends Handshake packet and completes handshake.
9594        let flight = testing::emit_flight(&mut pipe.client).unwrap();
9595
9596        assert!(pipe.client.is_established());
9597        assert!(!pipe.client.handshake_confirmed);
9598
9599        assert!(!pipe.server.is_established());
9600        assert!(!pipe.server.handshake_confirmed);
9601
9602        testing::process_flight(&mut pipe.server, flight).unwrap();
9603
9604        // Server completes and confirms handshake, and sends HANDSHAKE_DONE.
9605        let flight = testing::emit_flight(&mut pipe.server).unwrap();
9606
9607        assert!(pipe.client.is_established());
9608        assert!(!pipe.client.handshake_confirmed);
9609
9610        assert!(pipe.server.is_established());
9611        assert!(pipe.server.handshake_confirmed);
9612
9613        testing::process_flight(&mut pipe.client, flight).unwrap();
9614
9615        // Client acks 1-RTT packet, and confirms handshake.
9616        let flight = testing::emit_flight(&mut pipe.client).unwrap();
9617
9618        assert!(pipe.client.is_established());
9619        assert!(pipe.client.handshake_confirmed);
9620
9621        assert!(pipe.server.is_established());
9622        assert!(pipe.server.handshake_confirmed);
9623
9624        testing::process_flight(&mut pipe.server, flight).unwrap();
9625
9626        assert!(pipe.client.is_established());
9627        assert!(pipe.client.handshake_confirmed);
9628
9629        assert!(pipe.server.is_established());
9630        assert!(pipe.server.handshake_confirmed);
9631    }
9632
9633    #[test]
9634    fn handshake_resumption() {
9635        #[cfg(not(feature = "openssl"))]
9636        const SESSION_TICKET_KEY: [u8; 48] = [0xa; 48];
9637
9638        // 80-byte key(AES 256)
9639        // TODO: We can set the default? or query the ticket size by calling
9640        // the same API(SSL_CTX_set_tlsext_ticket_keys) twice to fetch the size.
9641        #[cfg(feature = "openssl")]
9642        const SESSION_TICKET_KEY: [u8; 80] = [0xa; 80];
9643
9644        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9645
9646        config
9647            .load_cert_chain_from_pem_file("examples/cert.crt")
9648            .unwrap();
9649        config
9650            .load_priv_key_from_pem_file("examples/cert.key")
9651            .unwrap();
9652        config
9653            .set_application_protos(&[b"proto1", b"proto2"])
9654            .unwrap();
9655        config.set_initial_max_data(30);
9656        config.set_initial_max_stream_data_bidi_local(15);
9657        config.set_initial_max_stream_data_bidi_remote(15);
9658        config.set_initial_max_streams_bidi(3);
9659        config.set_ticket_key(&SESSION_TICKET_KEY).unwrap();
9660
9661        // Perform initial handshake.
9662        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
9663        assert_eq!(pipe.handshake(), Ok(()));
9664
9665        assert!(pipe.client.is_established());
9666        assert!(pipe.server.is_established());
9667
9668        assert!(!pipe.client.is_resumed());
9669        assert!(!pipe.server.is_resumed());
9670
9671        // Extract session,
9672        let session = pipe.client.session().unwrap();
9673
9674        // Configure session on new connection and perform handshake.
9675        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9676        config
9677            .load_cert_chain_from_pem_file("examples/cert.crt")
9678            .unwrap();
9679        config
9680            .load_priv_key_from_pem_file("examples/cert.key")
9681            .unwrap();
9682        config
9683            .set_application_protos(&[b"proto1", b"proto2"])
9684            .unwrap();
9685        config.set_initial_max_data(30);
9686        config.set_initial_max_stream_data_bidi_local(15);
9687        config.set_initial_max_stream_data_bidi_remote(15);
9688        config.set_initial_max_streams_bidi(3);
9689        config.set_ticket_key(&SESSION_TICKET_KEY).unwrap();
9690
9691        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
9692
9693        assert_eq!(pipe.client.set_session(session), Ok(()));
9694        assert_eq!(pipe.handshake(), Ok(()));
9695
9696        assert!(pipe.client.is_established());
9697        assert!(pipe.server.is_established());
9698
9699        assert!(pipe.client.is_resumed());
9700        assert!(pipe.server.is_resumed());
9701    }
9702
9703    #[test]
9704    fn handshake_alpn_mismatch() {
9705        let mut buf = [0; 65535];
9706
9707        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
9708        config
9709            .set_application_protos(&[b"proto3\x06proto4"])
9710            .unwrap();
9711        config.verify_peer(false);
9712
9713        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
9714        assert_eq!(pipe.handshake(), Err(Error::TlsFail));
9715
9716        assert_eq!(pipe.client.application_proto(), b"");
9717        assert_eq!(pipe.server.application_proto(), b"");
9718
9719        // Server should only send one packet in response to ALPN mismatch.
9720        let (len, _) = pipe.server.send(&mut buf).unwrap();
9721        assert_eq!(len, 1200);
9722
9723        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
9724        assert_eq!(pipe.server.sent_count, 1);
9725    }
9726
9727    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
9728    #[test]
9729    fn handshake_0rtt() {
9730        let mut buf = [0; 65535];
9731
9732        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9733        config
9734            .load_cert_chain_from_pem_file("examples/cert.crt")
9735            .unwrap();
9736        config
9737            .load_priv_key_from_pem_file("examples/cert.key")
9738            .unwrap();
9739        config
9740            .set_application_protos(&[b"proto1", b"proto2"])
9741            .unwrap();
9742        config.set_initial_max_data(30);
9743        config.set_initial_max_stream_data_bidi_local(15);
9744        config.set_initial_max_stream_data_bidi_remote(15);
9745        config.set_initial_max_streams_bidi(3);
9746        config.enable_early_data();
9747        config.verify_peer(false);
9748
9749        // Perform initial handshake.
9750        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9751        assert_eq!(pipe.handshake(), Ok(()));
9752
9753        // Extract session,
9754        let session = pipe.client.session().unwrap();
9755
9756        // Configure session on new connection.
9757        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9758        assert_eq!(pipe.client.set_session(session), Ok(()));
9759
9760        // Client sends initial flight.
9761        let (len, _) = pipe.client.send(&mut buf).unwrap();
9762        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
9763
9764        // Client sends 0-RTT packet.
9765        let pkt_type = packet::Type::ZeroRTT;
9766
9767        let frames = [frame::Frame::Stream {
9768            stream_id: 4,
9769            data: stream::RangeBuf::from(b"aaaaa", 0, true),
9770        }];
9771
9772        assert_eq!(
9773            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
9774            Ok(1200)
9775        );
9776
9777        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
9778
9779        // 0-RTT stream data is readable.
9780        let mut r = pipe.server.readable();
9781        assert_eq!(r.next(), Some(4));
9782        assert_eq!(r.next(), None);
9783
9784        let mut b = [0; 15];
9785        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
9786        assert_eq!(&b[..5], b"aaaaa");
9787    }
9788
9789    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
9790    #[test]
9791    fn handshake_0rtt_reordered() {
9792        let mut buf = [0; 65535];
9793
9794        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9795        config
9796            .load_cert_chain_from_pem_file("examples/cert.crt")
9797            .unwrap();
9798        config
9799            .load_priv_key_from_pem_file("examples/cert.key")
9800            .unwrap();
9801        config
9802            .set_application_protos(&[b"proto1", b"proto2"])
9803            .unwrap();
9804        config.set_initial_max_data(30);
9805        config.set_initial_max_stream_data_bidi_local(15);
9806        config.set_initial_max_stream_data_bidi_remote(15);
9807        config.set_initial_max_streams_bidi(3);
9808        config.enable_early_data();
9809        config.verify_peer(false);
9810
9811        // Perform initial handshake.
9812        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9813        assert_eq!(pipe.handshake(), Ok(()));
9814
9815        // Extract session,
9816        let session = pipe.client.session().unwrap();
9817
9818        // Configure session on new connection.
9819        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9820        assert_eq!(pipe.client.set_session(session), Ok(()));
9821
9822        // Client sends initial flight.
9823        let (len, _) = pipe.client.send(&mut buf).unwrap();
9824        let mut initial = buf[..len].to_vec();
9825
9826        // Client sends 0-RTT packet.
9827        let pkt_type = packet::Type::ZeroRTT;
9828
9829        let frames = [frame::Frame::Stream {
9830            stream_id: 4,
9831            data: stream::RangeBuf::from(b"aaaaa", 0, true),
9832        }];
9833
9834        let len =
9835            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
9836                .unwrap();
9837        let mut zrtt = buf[..len].to_vec();
9838
9839        // 0-RTT packet is received before the Initial one.
9840        assert_eq!(pipe.server_recv(&mut zrtt), Ok(zrtt.len()));
9841
9842        assert_eq!(pipe.server.undecryptable_pkts.len(), 1);
9843        assert_eq!(pipe.server.undecryptable_pkts[0].0.len(), zrtt.len());
9844
9845        let mut r = pipe.server.readable();
9846        assert_eq!(r.next(), None);
9847
9848        // Initial packet is also received.
9849        assert_eq!(pipe.server_recv(&mut initial), Ok(initial.len()));
9850
9851        // 0-RTT stream data is readable.
9852        let mut r = pipe.server.readable();
9853        assert_eq!(r.next(), Some(4));
9854        assert_eq!(r.next(), None);
9855
9856        let mut b = [0; 15];
9857        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
9858        assert_eq!(&b[..5], b"aaaaa");
9859    }
9860
9861    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
9862    #[test]
9863    fn handshake_0rtt_truncated() {
9864        let mut buf = [0; 65535];
9865
9866        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9867        config
9868            .load_cert_chain_from_pem_file("examples/cert.crt")
9869            .unwrap();
9870        config
9871            .load_priv_key_from_pem_file("examples/cert.key")
9872            .unwrap();
9873        config
9874            .set_application_protos(&[b"proto1", b"proto2"])
9875            .unwrap();
9876        config.set_initial_max_data(30);
9877        config.set_initial_max_stream_data_bidi_local(15);
9878        config.set_initial_max_stream_data_bidi_remote(15);
9879        config.set_initial_max_streams_bidi(3);
9880        config.enable_early_data();
9881        config.verify_peer(false);
9882
9883        // Perform initial handshake.
9884        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9885        assert_eq!(pipe.handshake(), Ok(()));
9886
9887        // Extract session,
9888        let session = pipe.client.session().unwrap();
9889
9890        // Configure session on new connection.
9891        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9892        assert_eq!(pipe.client.set_session(session), Ok(()));
9893
9894        // Client sends initial flight.
9895        pipe.client.send(&mut buf).unwrap();
9896
9897        // Client sends 0-RTT packet.
9898        let pkt_type = packet::Type::ZeroRTT;
9899
9900        let frames = [frame::Frame::Stream {
9901            stream_id: 4,
9902            data: stream::RangeBuf::from(b"aaaaa", 0, true),
9903        }];
9904
9905        let len =
9906            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
9907                .unwrap();
9908
9909        // Simulate a truncated packet by sending one byte less.
9910        let mut zrtt = buf[..len - 1].to_vec();
9911
9912        // 0-RTT packet is received before the Initial one.
9913        assert_eq!(pipe.server_recv(&mut zrtt), Err(Error::InvalidPacket));
9914
9915        assert_eq!(pipe.server.undecryptable_pkts.len(), 0);
9916
9917        assert!(pipe.server.is_closed());
9918    }
9919
9920    #[test]
9921    fn crypto_limit() {
9922        let mut buf = [0; 65535];
9923
9924        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
9925        config
9926            .load_cert_chain_from_pem_file("examples/cert.crt")
9927            .unwrap();
9928        config
9929            .load_priv_key_from_pem_file("examples/cert.key")
9930            .unwrap();
9931        config
9932            .set_application_protos(&[b"proto1", b"proto2"])
9933            .unwrap();
9934        config.set_initial_max_data(30);
9935        config.set_initial_max_stream_data_bidi_local(15);
9936        config.set_initial_max_stream_data_bidi_remote(15);
9937        config.set_initial_max_streams_bidi(3);
9938        config.enable_early_data();
9939        config.verify_peer(false);
9940
9941        // Perform initial handshake.
9942        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
9943        assert_eq!(pipe.handshake(), Ok(()));
9944
9945        // Client send a 1-byte frame that starts from the crypto stream offset
9946        // limit.
9947        let frames = [frame::Frame::Crypto {
9948            data: stream::RangeBuf::from(b"a", MAX_CRYPTO_STREAM_OFFSET, false),
9949        }];
9950
9951        let pkt_type = packet::Type::Short;
9952
9953        let written =
9954            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
9955                .unwrap();
9956
9957        let active_path = pipe.server.paths.get_active().unwrap();
9958        let info = RecvInfo {
9959            to: active_path.local_addr(),
9960            from: active_path.peer_addr(),
9961        };
9962
9963        assert_eq!(
9964            pipe.server.recv(&mut buf[..written], info),
9965            Err(Error::CryptoBufferExceeded)
9966        );
9967
9968        let written = match pipe.server.send(&mut buf) {
9969            Ok((write, _)) => write,
9970
9971            Err(_) => unreachable!(),
9972        };
9973
9974        let frames =
9975            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
9976        let mut iter = frames.iter();
9977
9978        assert_eq!(
9979            iter.next(),
9980            Some(&frame::Frame::ConnectionClose {
9981                error_code: 0x0d,
9982                frame_type: 0,
9983                reason: Vec::new(),
9984            })
9985        );
9986    }
9987
9988    #[test]
9989    fn limit_handshake_data() {
9990        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
9991        config
9992            .load_cert_chain_from_pem_file("examples/cert-big.crt")
9993            .unwrap();
9994        config
9995            .load_priv_key_from_pem_file("examples/cert.key")
9996            .unwrap();
9997        config
9998            .set_application_protos(&[b"proto1", b"proto2"])
9999            .unwrap();
10000
10001        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10002
10003        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10004        let client_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10005        testing::process_flight(&mut pipe.server, flight).unwrap();
10006
10007        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10008        let server_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10009
10010        assert_eq!(server_sent, client_sent * MAX_AMPLIFICATION_FACTOR);
10011    }
10012
10013    #[test]
10014    fn custom_limit_handshake_data() {
10015        const CUSTOM_AMPLIFICATION_FACTOR: usize = 2;
10016
10017        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
10018        config
10019            .load_cert_chain_from_pem_file("examples/cert-big.crt")
10020            .unwrap();
10021        config
10022            .load_priv_key_from_pem_file("examples/cert.key")
10023            .unwrap();
10024        config
10025            .set_application_protos(&[b"proto1", b"proto2"])
10026            .unwrap();
10027        config.set_max_amplification_factor(CUSTOM_AMPLIFICATION_FACTOR);
10028
10029        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
10030
10031        let flight = testing::emit_flight(&mut pipe.client).unwrap();
10032        let client_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10033        testing::process_flight(&mut pipe.server, flight).unwrap();
10034
10035        let flight = testing::emit_flight(&mut pipe.server).unwrap();
10036        let server_sent = flight.iter().fold(0, |out, p| out + p.0.len());
10037
10038        assert_eq!(server_sent, client_sent * CUSTOM_AMPLIFICATION_FACTOR);
10039    }
10040
10041    #[test]
10042    fn stream() {
10043        let mut pipe = testing::Pipe::new().unwrap();
10044        assert_eq!(pipe.handshake(), Ok(()));
10045
10046        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10047        assert_eq!(pipe.advance(), Ok(()));
10048
10049        assert!(!pipe.server.stream_finished(4));
10050
10051        let mut r = pipe.server.readable();
10052        assert_eq!(r.next(), Some(4));
10053        assert_eq!(r.next(), None);
10054
10055        let mut b = [0; 15];
10056        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((12, true)));
10057        assert_eq!(&b[..12], b"hello, world");
10058
10059        assert!(pipe.server.stream_finished(4));
10060    }
10061
10062    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
10063    #[test]
10064    fn zero_rtt() {
10065        let mut buf = [0; 65535];
10066
10067        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10068        config
10069            .load_cert_chain_from_pem_file("examples/cert.crt")
10070            .unwrap();
10071        config
10072            .load_priv_key_from_pem_file("examples/cert.key")
10073            .unwrap();
10074        config
10075            .set_application_protos(&[b"proto1", b"proto2"])
10076            .unwrap();
10077        config.set_initial_max_data(30);
10078        config.set_initial_max_stream_data_bidi_local(15);
10079        config.set_initial_max_stream_data_bidi_remote(15);
10080        config.set_initial_max_streams_bidi(3);
10081        config.enable_early_data();
10082        config.verify_peer(false);
10083
10084        // Perform initial handshake.
10085        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10086        assert_eq!(pipe.handshake(), Ok(()));
10087
10088        // Extract session,
10089        let session = pipe.client.session().unwrap();
10090
10091        // Configure session on new connection.
10092        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10093        assert_eq!(pipe.client.set_session(session), Ok(()));
10094
10095        // Client sends initial flight.
10096        let (len, _) = pipe.client.send(&mut buf).unwrap();
10097        let mut initial = buf[..len].to_vec();
10098
10099        assert!(pipe.client.is_in_early_data());
10100
10101        // Client sends 0-RTT data.
10102        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10103
10104        let (len, _) = pipe.client.send(&mut buf).unwrap();
10105        let mut zrtt = buf[..len].to_vec();
10106
10107        // Server receives packets.
10108        assert_eq!(pipe.server_recv(&mut initial), Ok(initial.len()));
10109        assert!(pipe.server.is_in_early_data());
10110
10111        assert_eq!(pipe.server_recv(&mut zrtt), Ok(zrtt.len()));
10112
10113        // 0-RTT stream data is readable.
10114        let mut r = pipe.server.readable();
10115        assert_eq!(r.next(), Some(4));
10116        assert_eq!(r.next(), None);
10117
10118        let mut b = [0; 15];
10119        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((12, true)));
10120        assert_eq!(&b[..12], b"hello, world");
10121    }
10122
10123    #[test]
10124    fn stream_send_on_32bit_arch() {
10125        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
10126        config
10127            .load_cert_chain_from_pem_file("examples/cert.crt")
10128            .unwrap();
10129        config
10130            .load_priv_key_from_pem_file("examples/cert.key")
10131            .unwrap();
10132        config
10133            .set_application_protos(&[b"proto1", b"proto2"])
10134            .unwrap();
10135        config.set_initial_max_data(2_u64.pow(32) + 5);
10136        config.set_initial_max_stream_data_bidi_local(15);
10137        config.set_initial_max_stream_data_bidi_remote(15);
10138        config.set_initial_max_stream_data_uni(10);
10139        config.set_initial_max_streams_bidi(3);
10140        config.set_initial_max_streams_uni(0);
10141        config.verify_peer(false);
10142
10143        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
10144        assert_eq!(pipe.handshake(), Ok(()));
10145
10146        // In 32bit arch, send_capacity() should be min(2^32+5, cwnd),
10147        // not min(5, cwnd)
10148        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
10149
10150        assert_eq!(pipe.advance(), Ok(()));
10151
10152        assert!(!pipe.server.stream_finished(4));
10153    }
10154
10155    #[test]
10156    fn empty_stream_frame() {
10157        let mut buf = [0; 65535];
10158
10159        let mut pipe = testing::Pipe::new().unwrap();
10160        assert_eq!(pipe.handshake(), Ok(()));
10161
10162        let frames = [frame::Frame::Stream {
10163            stream_id: 4,
10164            data: stream::RangeBuf::from(b"aaaaa", 0, false),
10165        }];
10166
10167        let pkt_type = packet::Type::Short;
10168        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
10169
10170        let mut readable = pipe.server.readable();
10171        assert_eq!(readable.next(), Some(4));
10172
10173        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((5, false)));
10174
10175        let frames = [frame::Frame::Stream {
10176            stream_id: 4,
10177            data: stream::RangeBuf::from(b"", 5, true),
10178        }];
10179
10180        let pkt_type = packet::Type::Short;
10181        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
10182
10183        let mut readable = pipe.server.readable();
10184        assert_eq!(readable.next(), Some(4));
10185
10186        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((0, true)));
10187
10188        let frames = [frame::Frame::Stream {
10189            stream_id: 4,
10190            data: stream::RangeBuf::from(b"", 15, true),
10191        }];
10192
10193        let pkt_type = packet::Type::Short;
10194        assert_eq!(
10195            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10196            Err(Error::FinalSize)
10197        );
10198    }
10199
10200    #[test]
10201    fn update_key_request() {
10202        let mut b = [0; 15];
10203
10204        let mut pipe = testing::Pipe::new().unwrap();
10205        assert_eq!(pipe.handshake(), Ok(()));
10206        assert_eq!(pipe.advance(), Ok(()));
10207
10208        // Client sends message with key update request.
10209        assert_eq!(pipe.client_update_key(), Ok(()));
10210        assert_eq!(pipe.client.stream_send(4, b"hello", false), Ok(5));
10211        assert_eq!(pipe.advance(), Ok(()));
10212
10213        // Ensure server updates key and it correctly decrypts the message.
10214        let mut r = pipe.server.readable();
10215        assert_eq!(r.next(), Some(4));
10216        assert_eq!(r.next(), None);
10217        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, false)));
10218        assert_eq!(&b[..5], b"hello");
10219
10220        // Ensure ACK for key update.
10221        assert!(
10222            pipe.server.pkt_num_spaces[packet::Epoch::Application]
10223                .key_update
10224                .as_ref()
10225                .unwrap()
10226                .update_acked
10227        );
10228
10229        // Server sends message with the new key.
10230        assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
10231        assert_eq!(pipe.advance(), Ok(()));
10232
10233        // Ensure update key is completed and client can decrypt packet.
10234        let mut r = pipe.client.readable();
10235        assert_eq!(r.next(), Some(4));
10236        assert_eq!(r.next(), None);
10237        assert_eq!(pipe.client.stream_recv(4, &mut b), Ok((5, false)));
10238        assert_eq!(&b[..5], b"world");
10239
10240        // Server keeps sending packets to ensure encryption still works.
10241        for _ in 0..10 {
10242            assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
10243            assert_eq!(pipe.advance(), Ok(()));
10244
10245            let mut r = pipe.client.readable();
10246            assert_eq!(r.next(), Some(4));
10247            assert_eq!(r.next(), None);
10248            assert_eq!(pipe.client.stream_recv(4, &mut b), Ok((5, false)));
10249            assert_eq!(&b[..5], b"world");
10250        }
10251    }
10252
10253    #[test]
10254    fn update_key_request_twice_error() {
10255        let mut buf = [0; 65535];
10256
10257        let mut pipe = testing::Pipe::new().unwrap();
10258        assert_eq!(pipe.handshake(), Ok(()));
10259        assert_eq!(pipe.advance(), Ok(()));
10260
10261        let frames = [frame::Frame::Stream {
10262            stream_id: 4,
10263            data: stream::RangeBuf::from(b"hello", 0, false),
10264        }];
10265
10266        // Client sends stream frame with key update request.
10267        assert_eq!(pipe.client_update_key(), Ok(()));
10268        let written = testing::encode_pkt(
10269            &mut pipe.client,
10270            packet::Type::Short,
10271            &frames,
10272            &mut buf,
10273        )
10274        .unwrap();
10275
10276        // Server correctly decode with new key.
10277        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
10278
10279        // Client sends stream frame with another key update request before server
10280        // ACK.
10281        assert_eq!(pipe.client_update_key(), Ok(()));
10282        let written = testing::encode_pkt(
10283            &mut pipe.client,
10284            packet::Type::Short,
10285            &frames,
10286            &mut buf,
10287        )
10288        .unwrap();
10289
10290        // Check server correctly closes the connection with a key update error
10291        // for the peer.
10292        assert_eq!(pipe.server_recv(&mut buf[..written]), Err(Error::KeyUpdate));
10293    }
10294
10295    #[test]
10296    /// Tests that receiving a MAX_STREAM_DATA frame for a receive-only
10297    /// unidirectional stream is forbidden.
10298    fn max_stream_data_receive_uni() {
10299        let mut buf = [0; 65535];
10300
10301        let mut pipe = testing::Pipe::new().unwrap();
10302        assert_eq!(pipe.handshake(), Ok(()));
10303
10304        // Client opens unidirectional stream.
10305        assert_eq!(pipe.client.stream_send(2, b"hello", false), Ok(5));
10306        assert_eq!(pipe.advance(), Ok(()));
10307
10308        // Client sends MAX_STREAM_DATA on local unidirectional stream.
10309        let frames = [frame::Frame::MaxStreamData {
10310            stream_id: 2,
10311            max: 1024,
10312        }];
10313
10314        let pkt_type = packet::Type::Short;
10315        assert_eq!(
10316            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10317            Err(Error::InvalidStreamState(2)),
10318        );
10319    }
10320
10321    #[test]
10322    fn empty_payload() {
10323        let mut buf = [0; 65535];
10324
10325        let mut pipe = testing::Pipe::new().unwrap();
10326        assert_eq!(pipe.handshake(), Ok(()));
10327
10328        // Send a packet with no frames.
10329        let pkt_type = packet::Type::Short;
10330        assert_eq!(
10331            pipe.send_pkt_to_server(pkt_type, &[], &mut buf),
10332            Err(Error::InvalidPacket)
10333        );
10334    }
10335
10336    #[test]
10337    fn min_payload() {
10338        let mut buf = [0; 65535];
10339
10340        let mut pipe = testing::Pipe::new().unwrap();
10341
10342        // Send a non-ack-eliciting packet.
10343        let frames = [frame::Frame::Padding { len: 4 }];
10344
10345        let pkt_type = packet::Type::Initial;
10346        let written =
10347            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
10348                .unwrap();
10349        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
10350
10351        let initial_path = pipe
10352            .server
10353            .paths
10354            .get_active()
10355            .expect("initial path not found");
10356
10357        assert_eq!(initial_path.max_send_bytes, 195);
10358
10359        // Force server to send a single PING frame.
10360        pipe.server
10361            .paths
10362            .get_active_mut()
10363            .expect("no active path")
10364            .recovery
10365            .inc_loss_probes(packet::Epoch::Initial);
10366
10367        let initial_path = pipe
10368            .server
10369            .paths
10370            .get_active_mut()
10371            .expect("initial path not found");
10372
10373        // Artificially limit the amount of bytes the server can send.
10374        initial_path.max_send_bytes = 60;
10375
10376        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
10377    }
10378
10379    #[test]
10380    fn flow_control_limit() {
10381        let mut buf = [0; 65535];
10382
10383        let mut pipe = testing::Pipe::new().unwrap();
10384        assert_eq!(pipe.handshake(), Ok(()));
10385
10386        let frames = [
10387            frame::Frame::Stream {
10388                stream_id: 0,
10389                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
10390            },
10391            frame::Frame::Stream {
10392                stream_id: 4,
10393                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
10394            },
10395            frame::Frame::Stream {
10396                stream_id: 8,
10397                data: stream::RangeBuf::from(b"a", 0, false),
10398            },
10399        ];
10400
10401        let pkt_type = packet::Type::Short;
10402        assert_eq!(
10403            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10404            Err(Error::FlowControl),
10405        );
10406    }
10407
10408    #[test]
10409    fn flow_control_limit_dup() {
10410        let mut buf = [0; 65535];
10411
10412        let mut pipe = testing::Pipe::new().unwrap();
10413        assert_eq!(pipe.handshake(), Ok(()));
10414
10415        let frames = [
10416            // One byte less than stream limit.
10417            frame::Frame::Stream {
10418                stream_id: 0,
10419                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaa", 0, false),
10420            },
10421            // Same stream, but one byte more.
10422            frame::Frame::Stream {
10423                stream_id: 0,
10424                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
10425            },
10426            frame::Frame::Stream {
10427                stream_id: 8,
10428                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
10429            },
10430        ];
10431
10432        let pkt_type = packet::Type::Short;
10433        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10434    }
10435
10436    #[test]
10437    fn flow_control_update() {
10438        let mut buf = [0; 65535];
10439
10440        let mut pipe = testing::Pipe::new().unwrap();
10441        assert_eq!(pipe.handshake(), Ok(()));
10442
10443        let frames = [
10444            frame::Frame::Stream {
10445                stream_id: 0,
10446                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
10447            },
10448            frame::Frame::Stream {
10449                stream_id: 4,
10450                data: stream::RangeBuf::from(b"a", 0, false),
10451            },
10452        ];
10453
10454        let pkt_type = packet::Type::Short;
10455
10456        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10457
10458        pipe.server.stream_recv(0, &mut buf).unwrap();
10459        pipe.server.stream_recv(4, &mut buf).unwrap();
10460
10461        let frames = [frame::Frame::Stream {
10462            stream_id: 4,
10463            data: stream::RangeBuf::from(b"a", 1, false),
10464        }];
10465
10466        let len = pipe
10467            .send_pkt_to_server(pkt_type, &frames, &mut buf)
10468            .unwrap();
10469
10470        assert!(len > 0);
10471
10472        let frames =
10473            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
10474        let mut iter = frames.iter();
10475
10476        // Ignore ACK.
10477        iter.next().unwrap();
10478
10479        assert_eq!(
10480            iter.next(),
10481            Some(&frame::Frame::MaxStreamData {
10482                stream_id: 0,
10483                max: 30
10484            })
10485        );
10486        assert_eq!(iter.next(), Some(&frame::Frame::MaxData { max: 61 }));
10487    }
10488
10489    #[test]
10490    /// Tests that flow control is properly updated even when a stream is shut
10491    /// down.
10492    fn flow_control_drain() {
10493        let mut pipe = testing::Pipe::new().unwrap();
10494        assert_eq!(pipe.handshake(), Ok(()));
10495
10496        // Client opens a stream and sends some data.
10497        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
10498        assert_eq!(pipe.advance(), Ok(()));
10499
10500        // Server receives data, without reading it.
10501        let mut r = pipe.server.readable();
10502        assert_eq!(r.next(), Some(4));
10503        assert_eq!(r.next(), None);
10504
10505        // In the meantime, client sends more data.
10506        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
10507        assert_eq!(pipe.client.stream_send(4, b"aaaaa", true), Ok(5));
10508
10509        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
10510        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
10511        assert_eq!(pipe.client.stream_send(8, b"aaaaa", true), Ok(5));
10512
10513        // Server shuts down one stream.
10514        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
10515
10516        let mut r = pipe.server.readable();
10517        assert_eq!(r.next(), None);
10518
10519        // Flush connection.
10520        assert_eq!(pipe.advance(), Ok(()));
10521    }
10522
10523    #[test]
10524    fn stream_flow_control_limit_bidi() {
10525        let mut buf = [0; 65535];
10526
10527        let mut pipe = testing::Pipe::new().unwrap();
10528        assert_eq!(pipe.handshake(), Ok(()));
10529
10530        let frames = [frame::Frame::Stream {
10531            stream_id: 4,
10532            data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaaa", 0, true),
10533        }];
10534
10535        let pkt_type = packet::Type::Short;
10536        assert_eq!(
10537            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10538            Err(Error::FlowControl),
10539        );
10540    }
10541
10542    #[test]
10543    fn stream_flow_control_limit_uni() {
10544        let mut buf = [0; 65535];
10545
10546        let mut pipe = testing::Pipe::new().unwrap();
10547        assert_eq!(pipe.handshake(), Ok(()));
10548
10549        let frames = [frame::Frame::Stream {
10550            stream_id: 2,
10551            data: stream::RangeBuf::from(b"aaaaaaaaaaa", 0, true),
10552        }];
10553
10554        let pkt_type = packet::Type::Short;
10555        assert_eq!(
10556            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10557            Err(Error::FlowControl),
10558        );
10559    }
10560
10561    #[test]
10562    fn stream_flow_control_update() {
10563        let mut buf = [0; 65535];
10564
10565        let mut pipe = testing::Pipe::new().unwrap();
10566        assert_eq!(pipe.handshake(), Ok(()));
10567
10568        let frames = [frame::Frame::Stream {
10569            stream_id: 4,
10570            data: stream::RangeBuf::from(b"aaaaaaaaa", 0, false),
10571        }];
10572
10573        let pkt_type = packet::Type::Short;
10574
10575        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10576
10577        pipe.server.stream_recv(4, &mut buf).unwrap();
10578
10579        let frames = [frame::Frame::Stream {
10580            stream_id: 4,
10581            data: stream::RangeBuf::from(b"a", 9, false),
10582        }];
10583
10584        let len = pipe
10585            .send_pkt_to_server(pkt_type, &frames, &mut buf)
10586            .unwrap();
10587
10588        assert!(len > 0);
10589
10590        let frames =
10591            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
10592        let mut iter = frames.iter();
10593
10594        // Ignore ACK.
10595        iter.next().unwrap();
10596
10597        assert_eq!(
10598            iter.next(),
10599            Some(&frame::Frame::MaxStreamData {
10600                stream_id: 4,
10601                max: 24,
10602            })
10603        );
10604    }
10605
10606    #[test]
10607    fn stream_left_bidi() {
10608        let mut buf = [0; 65535];
10609
10610        let mut pipe = testing::Pipe::new().unwrap();
10611        assert_eq!(pipe.handshake(), Ok(()));
10612
10613        assert_eq!(3, pipe.client.peer_streams_left_bidi());
10614        assert_eq!(3, pipe.server.peer_streams_left_bidi());
10615
10616        pipe.server.stream_send(1, b"a", false).ok();
10617        assert_eq!(2, pipe.server.peer_streams_left_bidi());
10618        pipe.server.stream_send(5, b"a", false).ok();
10619        assert_eq!(1, pipe.server.peer_streams_left_bidi());
10620
10621        pipe.server.stream_send(9, b"a", false).ok();
10622        assert_eq!(0, pipe.server.peer_streams_left_bidi());
10623
10624        let frames = [frame::Frame::MaxStreamsBidi { max: MAX_STREAM_ID }];
10625
10626        let pkt_type = packet::Type::Short;
10627        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10628
10629        assert_eq!(MAX_STREAM_ID - 3, pipe.server.peer_streams_left_bidi());
10630    }
10631
10632    #[test]
10633    fn stream_left_uni() {
10634        let mut buf = [0; 65535];
10635
10636        let mut pipe = testing::Pipe::new().unwrap();
10637        assert_eq!(pipe.handshake(), Ok(()));
10638
10639        assert_eq!(3, pipe.client.peer_streams_left_uni());
10640        assert_eq!(3, pipe.server.peer_streams_left_uni());
10641
10642        pipe.server.stream_send(3, b"a", false).ok();
10643        assert_eq!(2, pipe.server.peer_streams_left_uni());
10644        pipe.server.stream_send(7, b"a", false).ok();
10645        assert_eq!(1, pipe.server.peer_streams_left_uni());
10646
10647        pipe.server.stream_send(11, b"a", false).ok();
10648        assert_eq!(0, pipe.server.peer_streams_left_uni());
10649
10650        let frames = [frame::Frame::MaxStreamsUni { max: MAX_STREAM_ID }];
10651
10652        let pkt_type = packet::Type::Short;
10653        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10654
10655        assert_eq!(MAX_STREAM_ID - 3, pipe.server.peer_streams_left_uni());
10656    }
10657
10658    #[test]
10659    fn stream_limit_bidi() {
10660        let mut buf = [0; 65535];
10661
10662        let mut pipe = testing::Pipe::new().unwrap();
10663        assert_eq!(pipe.handshake(), Ok(()));
10664
10665        let frames = [
10666            frame::Frame::Stream {
10667                stream_id: 4,
10668                data: stream::RangeBuf::from(b"a", 0, false),
10669            },
10670            frame::Frame::Stream {
10671                stream_id: 8,
10672                data: stream::RangeBuf::from(b"a", 0, false),
10673            },
10674            frame::Frame::Stream {
10675                stream_id: 12,
10676                data: stream::RangeBuf::from(b"a", 0, false),
10677            },
10678            frame::Frame::Stream {
10679                stream_id: 16,
10680                data: stream::RangeBuf::from(b"a", 0, false),
10681            },
10682            frame::Frame::Stream {
10683                stream_id: 20,
10684                data: stream::RangeBuf::from(b"a", 0, false),
10685            },
10686            frame::Frame::Stream {
10687                stream_id: 24,
10688                data: stream::RangeBuf::from(b"a", 0, false),
10689            },
10690            frame::Frame::Stream {
10691                stream_id: 28,
10692                data: stream::RangeBuf::from(b"a", 0, false),
10693            },
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::StreamLimit),
10700        );
10701    }
10702
10703    #[test]
10704    fn stream_limit_max_bidi() {
10705        let mut buf = [0; 65535];
10706
10707        let mut pipe = testing::Pipe::new().unwrap();
10708        assert_eq!(pipe.handshake(), Ok(()));
10709
10710        let frames = [frame::Frame::MaxStreamsBidi { max: MAX_STREAM_ID }];
10711
10712        let pkt_type = packet::Type::Short;
10713        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10714
10715        let frames = [frame::Frame::MaxStreamsBidi {
10716            max: MAX_STREAM_ID + 1,
10717        }];
10718
10719        let pkt_type = packet::Type::Short;
10720        assert_eq!(
10721            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10722            Err(Error::InvalidFrame),
10723        );
10724    }
10725
10726    #[test]
10727    fn stream_limit_uni() {
10728        let mut buf = [0; 65535];
10729
10730        let mut pipe = testing::Pipe::new().unwrap();
10731        assert_eq!(pipe.handshake(), Ok(()));
10732
10733        let frames = [
10734            frame::Frame::Stream {
10735                stream_id: 2,
10736                data: stream::RangeBuf::from(b"a", 0, false),
10737            },
10738            frame::Frame::Stream {
10739                stream_id: 6,
10740                data: stream::RangeBuf::from(b"a", 0, false),
10741            },
10742            frame::Frame::Stream {
10743                stream_id: 10,
10744                data: stream::RangeBuf::from(b"a", 0, false),
10745            },
10746            frame::Frame::Stream {
10747                stream_id: 14,
10748                data: stream::RangeBuf::from(b"a", 0, false),
10749            },
10750            frame::Frame::Stream {
10751                stream_id: 18,
10752                data: stream::RangeBuf::from(b"a", 0, false),
10753            },
10754            frame::Frame::Stream {
10755                stream_id: 22,
10756                data: stream::RangeBuf::from(b"a", 0, false),
10757            },
10758            frame::Frame::Stream {
10759                stream_id: 26,
10760                data: stream::RangeBuf::from(b"a", 0, false),
10761            },
10762        ];
10763
10764        let pkt_type = packet::Type::Short;
10765        assert_eq!(
10766            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10767            Err(Error::StreamLimit),
10768        );
10769    }
10770
10771    #[test]
10772    fn stream_limit_max_uni() {
10773        let mut buf = [0; 65535];
10774
10775        let mut pipe = testing::Pipe::new().unwrap();
10776        assert_eq!(pipe.handshake(), Ok(()));
10777
10778        let frames = [frame::Frame::MaxStreamsUni { max: MAX_STREAM_ID }];
10779
10780        let pkt_type = packet::Type::Short;
10781        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
10782
10783        let frames = [frame::Frame::MaxStreamsUni {
10784            max: MAX_STREAM_ID + 1,
10785        }];
10786
10787        let pkt_type = packet::Type::Short;
10788        assert_eq!(
10789            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
10790            Err(Error::InvalidFrame),
10791        );
10792    }
10793
10794    #[test]
10795    fn stream_left_reset_bidi() {
10796        let mut buf = [0; 65535];
10797
10798        let mut pipe = testing::Pipe::new().unwrap();
10799        assert_eq!(pipe.handshake(), Ok(()));
10800
10801        assert_eq!(3, pipe.client.peer_streams_left_bidi());
10802        assert_eq!(3, pipe.server.peer_streams_left_bidi());
10803
10804        pipe.client.stream_send(0, b"a", false).ok();
10805        assert_eq!(2, pipe.client.peer_streams_left_bidi());
10806        pipe.client.stream_send(4, b"a", false).ok();
10807        assert_eq!(1, pipe.client.peer_streams_left_bidi());
10808        pipe.client.stream_send(8, b"a", false).ok();
10809        assert_eq!(0, pipe.client.peer_streams_left_bidi());
10810
10811        // Client resets the stream.
10812        pipe.client
10813            .stream_shutdown(0, Shutdown::Write, 1001)
10814            .unwrap();
10815        pipe.advance().unwrap();
10816
10817        assert_eq!(0, pipe.client.peer_streams_left_bidi());
10818        let mut r = pipe.server.readable();
10819        assert_eq!(Some(0), r.next());
10820        assert_eq!(Some(4), r.next());
10821        assert_eq!(Some(8), r.next());
10822        assert_eq!(None, r.next());
10823
10824        assert_eq!(
10825            pipe.server.stream_recv(0, &mut buf),
10826            Err(Error::StreamReset(1001))
10827        );
10828
10829        let mut r = pipe.server.readable();
10830        assert_eq!(Some(4), r.next());
10831        assert_eq!(Some(8), r.next());
10832        assert_eq!(None, r.next());
10833
10834        // Server resets the stream in reaction.
10835        pipe.server
10836            .stream_shutdown(0, Shutdown::Write, 1001)
10837            .unwrap();
10838        pipe.advance().unwrap();
10839
10840        assert_eq!(1, pipe.client.peer_streams_left_bidi());
10841
10842        // Repeat for the other 2 streams
10843        pipe.client
10844            .stream_shutdown(4, Shutdown::Write, 1001)
10845            .unwrap();
10846        pipe.client
10847            .stream_shutdown(8, Shutdown::Write, 1001)
10848            .unwrap();
10849        pipe.advance().unwrap();
10850
10851        let mut r = pipe.server.readable();
10852        assert_eq!(Some(4), r.next());
10853        assert_eq!(Some(8), r.next());
10854        assert_eq!(None, r.next());
10855
10856        assert_eq!(
10857            pipe.server.stream_recv(4, &mut buf),
10858            Err(Error::StreamReset(1001))
10859        );
10860
10861        assert_eq!(
10862            pipe.server.stream_recv(8, &mut buf),
10863            Err(Error::StreamReset(1001))
10864        );
10865
10866        let mut r = pipe.server.readable();
10867        assert_eq!(None, r.next());
10868
10869        pipe.server
10870            .stream_shutdown(4, Shutdown::Write, 1001)
10871            .unwrap();
10872        pipe.server
10873            .stream_shutdown(8, Shutdown::Write, 1001)
10874            .unwrap();
10875        pipe.advance().unwrap();
10876
10877        assert_eq!(3, pipe.client.peer_streams_left_bidi());
10878    }
10879
10880    #[test]
10881    fn stream_reset_counts() {
10882        let mut pipe = testing::Pipe::new().unwrap();
10883        assert_eq!(pipe.handshake(), Ok(()));
10884
10885        pipe.client.stream_send(0, b"a", false).ok();
10886        pipe.client.stream_send(2, b"a", false).ok();
10887        pipe.client.stream_send(4, b"a", false).ok();
10888        pipe.client.stream_send(8, b"a", false).ok();
10889        pipe.advance().unwrap();
10890
10891        let stats = pipe.client.stats();
10892        assert_eq!(stats.reset_stream_count_local, 0);
10893
10894        // Client resets the stream.
10895        pipe.client
10896            .stream_shutdown(0, Shutdown::Write, 1001)
10897            .unwrap();
10898        pipe.advance().unwrap();
10899
10900        let stats = pipe.client.stats();
10901        assert_eq!(stats.reset_stream_count_local, 1);
10902        assert_eq!(stats.reset_stream_count_remote, 0);
10903        let stats = pipe.server.stats();
10904        assert_eq!(stats.reset_stream_count_local, 0);
10905        assert_eq!(stats.reset_stream_count_remote, 1);
10906
10907        // Server resets the stream in reaction.
10908        pipe.server
10909            .stream_shutdown(0, Shutdown::Write, 1001)
10910            .unwrap();
10911        pipe.advance().unwrap();
10912
10913        let stats = pipe.client.stats();
10914        assert_eq!(stats.reset_stream_count_local, 1);
10915        assert_eq!(stats.reset_stream_count_remote, 1);
10916        let stats = pipe.server.stats();
10917        assert_eq!(stats.reset_stream_count_local, 1);
10918        assert_eq!(stats.reset_stream_count_remote, 1);
10919
10920        // Repeat for the other streams
10921        pipe.client
10922            .stream_shutdown(2, Shutdown::Write, 1001)
10923            .unwrap();
10924        pipe.client
10925            .stream_shutdown(4, Shutdown::Write, 1001)
10926            .unwrap();
10927        pipe.client
10928            .stream_shutdown(8, Shutdown::Write, 1001)
10929            .unwrap();
10930        pipe.advance().unwrap();
10931
10932        pipe.server
10933            .stream_shutdown(4, Shutdown::Write, 1001)
10934            .unwrap();
10935        pipe.server
10936            .stream_shutdown(8, Shutdown::Write, 1001)
10937            .unwrap();
10938        pipe.advance().unwrap();
10939
10940        let stats = pipe.client.stats();
10941        assert_eq!(stats.reset_stream_count_local, 4);
10942        assert_eq!(stats.reset_stream_count_remote, 3);
10943        let stats = pipe.server.stats();
10944        assert_eq!(stats.reset_stream_count_local, 3);
10945        assert_eq!(stats.reset_stream_count_remote, 4);
10946    }
10947
10948    #[test]
10949    fn stream_stop_counts() {
10950        let mut pipe = testing::Pipe::new().unwrap();
10951        assert_eq!(pipe.handshake(), Ok(()));
10952
10953        pipe.client.stream_send(0, b"a", false).ok();
10954        pipe.client.stream_send(2, b"a", false).ok();
10955        pipe.client.stream_send(4, b"a", false).ok();
10956        pipe.client.stream_send(8, b"a", false).ok();
10957        pipe.advance().unwrap();
10958
10959        let stats = pipe.client.stats();
10960        assert_eq!(stats.reset_stream_count_local, 0);
10961
10962        // Server stops the stream and client automatically resets.
10963        pipe.server
10964            .stream_shutdown(0, Shutdown::Read, 1001)
10965            .unwrap();
10966        pipe.advance().unwrap();
10967
10968        let stats = pipe.client.stats();
10969        assert_eq!(stats.stopped_stream_count_local, 0);
10970        assert_eq!(stats.stopped_stream_count_remote, 1);
10971        assert_eq!(stats.reset_stream_count_local, 1);
10972        assert_eq!(stats.reset_stream_count_remote, 0);
10973
10974        let stats = pipe.server.stats();
10975        assert_eq!(stats.stopped_stream_count_local, 1);
10976        assert_eq!(stats.stopped_stream_count_remote, 0);
10977        assert_eq!(stats.reset_stream_count_local, 0);
10978        assert_eq!(stats.reset_stream_count_remote, 1);
10979
10980        // Repeat for the other streams
10981        pipe.server
10982            .stream_shutdown(2, Shutdown::Read, 1001)
10983            .unwrap();
10984        pipe.server
10985            .stream_shutdown(4, Shutdown::Read, 1001)
10986            .unwrap();
10987        pipe.server
10988            .stream_shutdown(8, Shutdown::Read, 1001)
10989            .unwrap();
10990        pipe.advance().unwrap();
10991
10992        let stats = pipe.client.stats();
10993        assert_eq!(stats.stopped_stream_count_local, 0);
10994        assert_eq!(stats.stopped_stream_count_remote, 4);
10995        assert_eq!(stats.reset_stream_count_local, 4);
10996        assert_eq!(stats.reset_stream_count_remote, 0);
10997
10998        let stats = pipe.server.stats();
10999        assert_eq!(stats.stopped_stream_count_local, 4);
11000        assert_eq!(stats.stopped_stream_count_remote, 0);
11001        assert_eq!(stats.reset_stream_count_local, 0);
11002        assert_eq!(stats.reset_stream_count_remote, 4);
11003    }
11004
11005    #[test]
11006    fn streams_blocked_max_bidi() {
11007        let mut buf = [0; 65535];
11008
11009        let mut pipe = testing::Pipe::new().unwrap();
11010        assert_eq!(pipe.handshake(), Ok(()));
11011
11012        let frames = [frame::Frame::StreamsBlockedBidi {
11013            limit: MAX_STREAM_ID,
11014        }];
11015
11016        let pkt_type = packet::Type::Short;
11017        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11018
11019        let frames = [frame::Frame::StreamsBlockedBidi {
11020            limit: MAX_STREAM_ID + 1,
11021        }];
11022
11023        let pkt_type = packet::Type::Short;
11024        assert_eq!(
11025            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11026            Err(Error::InvalidFrame),
11027        );
11028    }
11029
11030    #[test]
11031    fn streams_blocked_max_uni() {
11032        let mut buf = [0; 65535];
11033
11034        let mut pipe = testing::Pipe::new().unwrap();
11035        assert_eq!(pipe.handshake(), Ok(()));
11036
11037        let frames = [frame::Frame::StreamsBlockedUni {
11038            limit: MAX_STREAM_ID,
11039        }];
11040
11041        let pkt_type = packet::Type::Short;
11042        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11043
11044        let frames = [frame::Frame::StreamsBlockedUni {
11045            limit: MAX_STREAM_ID + 1,
11046        }];
11047
11048        let pkt_type = packet::Type::Short;
11049        assert_eq!(
11050            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11051            Err(Error::InvalidFrame),
11052        );
11053    }
11054
11055    #[test]
11056    fn stream_data_overlap() {
11057        let mut buf = [0; 65535];
11058
11059        let mut pipe = testing::Pipe::new().unwrap();
11060        assert_eq!(pipe.handshake(), Ok(()));
11061
11062        let frames = [
11063            frame::Frame::Stream {
11064                stream_id: 0,
11065                data: stream::RangeBuf::from(b"aaaaa", 0, false),
11066            },
11067            frame::Frame::Stream {
11068                stream_id: 0,
11069                data: stream::RangeBuf::from(b"bbbbb", 3, false),
11070            },
11071            frame::Frame::Stream {
11072                stream_id: 0,
11073                data: stream::RangeBuf::from(b"ccccc", 6, false),
11074            },
11075        ];
11076
11077        let pkt_type = packet::Type::Short;
11078        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11079
11080        let mut b = [0; 15];
11081        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((11, false)));
11082        assert_eq!(&b[..11], b"aaaaabbbccc");
11083    }
11084
11085    #[test]
11086    fn stream_data_overlap_with_reordering() {
11087        let mut buf = [0; 65535];
11088
11089        let mut pipe = testing::Pipe::new().unwrap();
11090        assert_eq!(pipe.handshake(), Ok(()));
11091
11092        let frames = [
11093            frame::Frame::Stream {
11094                stream_id: 0,
11095                data: stream::RangeBuf::from(b"aaaaa", 0, false),
11096            },
11097            frame::Frame::Stream {
11098                stream_id: 0,
11099                data: stream::RangeBuf::from(b"ccccc", 6, false),
11100            },
11101            frame::Frame::Stream {
11102                stream_id: 0,
11103                data: stream::RangeBuf::from(b"bbbbb", 3, false),
11104            },
11105        ];
11106
11107        let pkt_type = packet::Type::Short;
11108        assert!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf).is_ok());
11109
11110        let mut b = [0; 15];
11111        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((11, false)));
11112        assert_eq!(&b[..11], b"aaaaabccccc");
11113    }
11114
11115    #[test]
11116    /// Tests that receiving a valid RESET_STREAM frame when all data has
11117    /// already been read, notifies the application.
11118    fn reset_stream_data_recvd() {
11119        let mut b = [0; 15];
11120        let mut buf = [0; 65535];
11121
11122        let mut pipe = testing::Pipe::new().unwrap();
11123        assert_eq!(pipe.handshake(), Ok(()));
11124
11125        // Client sends some data.
11126        assert_eq!(pipe.client.stream_send(0, b"hello", false), Ok(5));
11127        assert_eq!(pipe.advance(), Ok(()));
11128
11129        // Server gets data and sends data back, closing stream.
11130        let mut r = pipe.server.readable();
11131        assert_eq!(r.next(), Some(0));
11132        assert_eq!(r.next(), None);
11133
11134        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, false)));
11135        assert!(!pipe.server.stream_finished(0));
11136
11137        let mut r = pipe.server.readable();
11138        assert_eq!(r.next(), None);
11139
11140        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
11141        assert_eq!(pipe.advance(), Ok(()));
11142
11143        let mut r = pipe.client.readable();
11144        assert_eq!(r.next(), Some(0));
11145        assert_eq!(r.next(), None);
11146
11147        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((0, true)));
11148        assert!(pipe.client.stream_finished(0));
11149
11150        // Client sends RESET_STREAM, closing stream.
11151        let frames = [frame::Frame::ResetStream {
11152            stream_id: 0,
11153            error_code: 42,
11154            final_size: 5,
11155        }];
11156
11157        let pkt_type = packet::Type::Short;
11158        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11159
11160        // Server is notified of stream readability, due to reset.
11161        let mut r = pipe.server.readable();
11162        assert_eq!(r.next(), Some(0));
11163        assert_eq!(r.next(), None);
11164
11165        assert_eq!(
11166            pipe.server.stream_recv(0, &mut b),
11167            Err(Error::StreamReset(42))
11168        );
11169
11170        assert!(pipe.server.stream_finished(0));
11171
11172        // Sending RESET_STREAM again shouldn't make stream readable again.
11173        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
11174            .unwrap();
11175
11176        let mut r = pipe.server.readable();
11177        assert_eq!(r.next(), None);
11178    }
11179
11180    #[test]
11181    /// Tests that receiving a valid RESET_STREAM frame when all data has _not_
11182    /// been read, discards all buffered data and notifies the application.
11183    fn reset_stream_data_not_recvd() {
11184        let mut b = [0; 15];
11185        let mut buf = [0; 65535];
11186
11187        let mut pipe = testing::Pipe::new().unwrap();
11188        assert_eq!(pipe.handshake(), Ok(()));
11189
11190        // Client sends some data.
11191        assert_eq!(pipe.client.stream_send(0, b"h", false), Ok(1));
11192        assert_eq!(pipe.advance(), Ok(()));
11193
11194        // Server gets data and sends data back, closing stream.
11195        let mut r = pipe.server.readable();
11196        assert_eq!(r.next(), Some(0));
11197        assert_eq!(r.next(), None);
11198
11199        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((1, false)));
11200        assert!(!pipe.server.stream_finished(0));
11201
11202        let mut r = pipe.server.readable();
11203        assert_eq!(r.next(), None);
11204
11205        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
11206        assert_eq!(pipe.advance(), Ok(()));
11207
11208        let mut r = pipe.client.readable();
11209        assert_eq!(r.next(), Some(0));
11210        assert_eq!(r.next(), None);
11211
11212        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((0, true)));
11213        assert!(pipe.client.stream_finished(0));
11214
11215        // Client sends RESET_STREAM, closing stream.
11216        let frames = [frame::Frame::ResetStream {
11217            stream_id: 0,
11218            error_code: 42,
11219            final_size: 5,
11220        }];
11221
11222        let pkt_type = packet::Type::Short;
11223        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11224
11225        // Server is notified of stream readability, due to reset.
11226        let mut r = pipe.server.readable();
11227        assert_eq!(r.next(), Some(0));
11228        assert_eq!(r.next(), None);
11229
11230        assert_eq!(
11231            pipe.server.stream_recv(0, &mut b),
11232            Err(Error::StreamReset(42))
11233        );
11234
11235        assert!(pipe.server.stream_finished(0));
11236
11237        // Sending RESET_STREAM again shouldn't make stream readable again.
11238        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
11239
11240        let mut r = pipe.server.readable();
11241        assert_eq!(r.next(), None);
11242    }
11243
11244    #[test]
11245    /// Tests that RESET_STREAM frames exceeding the connection-level flow
11246    /// control limit cause an error.
11247    fn reset_stream_flow_control() {
11248        let mut buf = [0; 65535];
11249
11250        let mut pipe = testing::Pipe::new().unwrap();
11251        assert_eq!(pipe.handshake(), Ok(()));
11252
11253        let frames = [
11254            frame::Frame::Stream {
11255                stream_id: 0,
11256                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
11257            },
11258            frame::Frame::Stream {
11259                stream_id: 4,
11260                data: stream::RangeBuf::from(b"a", 0, false),
11261            },
11262            frame::Frame::ResetStream {
11263                stream_id: 4,
11264                error_code: 0,
11265                final_size: 15,
11266            },
11267            frame::Frame::Stream {
11268                stream_id: 8,
11269                data: stream::RangeBuf::from(b"a", 0, false),
11270            },
11271        ];
11272
11273        let pkt_type = packet::Type::Short;
11274        assert_eq!(
11275            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf),
11276            Err(Error::FlowControl),
11277        );
11278    }
11279
11280    #[test]
11281    /// Tests that RESET_STREAM frames exceeding the stream-level flow control
11282    /// limit cause an error.
11283    fn reset_stream_flow_control_stream() {
11284        let mut buf = [0; 65535];
11285
11286        let mut pipe = testing::Pipe::new().unwrap();
11287        assert_eq!(pipe.handshake(), Ok(()));
11288
11289        let frames = [
11290            frame::Frame::Stream {
11291                stream_id: 4,
11292                data: stream::RangeBuf::from(b"a", 0, false),
11293            },
11294            frame::Frame::ResetStream {
11295                stream_id: 4,
11296                error_code: 0,
11297                final_size: 16, // Past stream's flow control limit.
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::FlowControl),
11305        );
11306    }
11307
11308    #[test]
11309    fn path_challenge() {
11310        let mut buf = [0; 65535];
11311
11312        let mut pipe = testing::Pipe::new().unwrap();
11313        assert_eq!(pipe.handshake(), Ok(()));
11314
11315        let frames = [frame::Frame::PathChallenge { data: [0xba; 8] }];
11316
11317        let pkt_type = packet::Type::Short;
11318
11319        let len = pipe
11320            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11321            .unwrap();
11322
11323        assert!(len > 0);
11324
11325        let frames =
11326            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11327        let mut iter = frames.iter();
11328
11329        // Ignore ACK.
11330        iter.next().unwrap();
11331
11332        assert_eq!(
11333            iter.next(),
11334            Some(&frame::Frame::PathResponse { data: [0xba; 8] })
11335        );
11336    }
11337
11338    #[cfg(not(feature = "openssl"))] // 0-RTT not supported when using openssl/quictls
11339    #[test]
11340    /// Simulates reception of an early 1-RTT packet on the server, by
11341    /// delaying the client's Handshake packet that completes the handshake.
11342    fn early_1rtt_packet() {
11343        let mut buf = [0; 65535];
11344
11345        let mut pipe = testing::Pipe::new().unwrap();
11346
11347        // Client sends initial flight
11348        let flight = testing::emit_flight(&mut pipe.client).unwrap();
11349        testing::process_flight(&mut pipe.server, flight).unwrap();
11350
11351        // Server sends initial flight.
11352        let flight = testing::emit_flight(&mut pipe.server).unwrap();
11353        testing::process_flight(&mut pipe.client, flight).unwrap();
11354
11355        // Client sends Handshake packet.
11356        let flight = testing::emit_flight(&mut pipe.client).unwrap();
11357
11358        // Emulate handshake packet delay by not making server process client
11359        // packet.
11360        let delayed = flight;
11361
11362        testing::emit_flight(&mut pipe.server).ok();
11363
11364        assert!(pipe.client.is_established());
11365
11366        // Send 1-RTT packet #0.
11367        let frames = [frame::Frame::Stream {
11368            stream_id: 0,
11369            data: stream::RangeBuf::from(b"hello, world", 0, true),
11370        }];
11371
11372        let pkt_type = packet::Type::Short;
11373        let written =
11374            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
11375                .unwrap();
11376
11377        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
11378
11379        // Send 1-RTT packet #1.
11380        let frames = [frame::Frame::Stream {
11381            stream_id: 4,
11382            data: stream::RangeBuf::from(b"hello, world", 0, true),
11383        }];
11384
11385        let written =
11386            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
11387                .unwrap();
11388
11389        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
11390
11391        assert!(!pipe.server.is_established());
11392
11393        // Client sent 1-RTT packets 0 and 1, but server hasn't received them.
11394        //
11395        // Note that `largest_rx_pkt_num` is initialized to 0, so we need to
11396        // send another 1-RTT packet to make this check meaningful.
11397        assert_eq!(
11398            pipe.server.pkt_num_spaces[packet::Epoch::Application]
11399                .largest_rx_pkt_num,
11400            0
11401        );
11402
11403        // Process delayed packet.
11404        testing::process_flight(&mut pipe.server, delayed).unwrap();
11405
11406        assert!(pipe.server.is_established());
11407
11408        assert_eq!(
11409            pipe.server.pkt_num_spaces[packet::Epoch::Application]
11410                .largest_rx_pkt_num,
11411            0
11412        );
11413    }
11414
11415    #[test]
11416    fn stop_sending() {
11417        let mut b = [0; 15];
11418
11419        let mut buf = [0; 65535];
11420
11421        let mut pipe = testing::Pipe::new().unwrap();
11422        assert_eq!(pipe.handshake(), Ok(()));
11423
11424        // Client sends some data, and closes stream.
11425        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
11426        assert_eq!(pipe.advance(), Ok(()));
11427
11428        // Server gets data.
11429        let mut r = pipe.server.readable();
11430        assert_eq!(r.next(), Some(0));
11431        assert_eq!(r.next(), None);
11432
11433        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
11434        assert!(pipe.server.stream_finished(0));
11435
11436        let mut r = pipe.server.readable();
11437        assert_eq!(r.next(), None);
11438
11439        // Server sends data, until blocked.
11440        let mut r = pipe.server.writable();
11441        assert_eq!(r.next(), Some(0));
11442        assert_eq!(r.next(), None);
11443
11444        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {
11445            assert_eq!(pipe.advance(), Ok(()));
11446        }
11447
11448        let mut r = pipe.server.writable();
11449        assert_eq!(r.next(), None);
11450
11451        // Client sends STOP_SENDING.
11452        let frames = [frame::Frame::StopSending {
11453            stream_id: 0,
11454            error_code: 42,
11455        }];
11456
11457        let pkt_type = packet::Type::Short;
11458        let len = pipe
11459            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11460            .unwrap();
11461
11462        // Server sent a RESET_STREAM frame in response.
11463        let frames =
11464            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11465
11466        let mut iter = frames.iter();
11467
11468        // Skip ACK frame.
11469        iter.next();
11470
11471        assert_eq!(
11472            iter.next(),
11473            Some(&frame::Frame::ResetStream {
11474                stream_id: 0,
11475                error_code: 42,
11476                final_size: 15,
11477            })
11478        );
11479
11480        // Stream is writable, but writing returns an error.
11481        let mut r = pipe.server.writable();
11482        assert_eq!(r.next(), Some(0));
11483        assert_eq!(r.next(), None);
11484
11485        assert_eq!(
11486            pipe.server.stream_send(0, b"world", true),
11487            Err(Error::StreamStopped(42)),
11488        );
11489
11490        assert_eq!(pipe.server.streams.len(), 1);
11491
11492        // Client acks RESET_STREAM frame.
11493        let mut ranges = ranges::RangeSet::default();
11494        ranges.insert(pipe.server.next_pkt_num - 5..pipe.server.next_pkt_num);
11495
11496        let frames = [frame::Frame::ACK {
11497            ack_delay: 15,
11498            ranges,
11499            ecn_counts: None,
11500        }];
11501
11502        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
11503
11504        // Stream is collected on the server after RESET_STREAM is acked.
11505        assert_eq!(pipe.server.streams.len(), 0);
11506
11507        // Sending STOP_SENDING again shouldn't trigger RESET_STREAM again.
11508        let frames = [frame::Frame::StopSending {
11509            stream_id: 0,
11510            error_code: 42,
11511        }];
11512
11513        let len = pipe
11514            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11515            .unwrap();
11516
11517        let frames =
11518            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11519
11520        assert_eq!(frames.len(), 1);
11521
11522        match frames.first() {
11523            Some(frame::Frame::ACK { .. }) => (),
11524
11525            f => panic!("expected ACK frame, got {:?}", f),
11526        };
11527
11528        let mut r = pipe.server.writable();
11529        assert_eq!(r.next(), None);
11530    }
11531
11532    #[test]
11533    fn stop_sending_fin() {
11534        let mut b = [0; 15];
11535
11536        let mut buf = [0; 65535];
11537
11538        let mut pipe = testing::Pipe::new().unwrap();
11539        assert_eq!(pipe.handshake(), Ok(()));
11540
11541        // Client sends some data, and closes stream.
11542        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
11543        assert_eq!(pipe.advance(), Ok(()));
11544
11545        // Server gets data.
11546        let mut r = pipe.server.readable();
11547        assert_eq!(r.next(), Some(4));
11548        assert_eq!(r.next(), None);
11549
11550        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
11551        assert!(pipe.server.stream_finished(4));
11552
11553        let mut r = pipe.server.readable();
11554        assert_eq!(r.next(), None);
11555
11556        // Server sends data...
11557        let mut r = pipe.server.writable();
11558        assert_eq!(r.next(), Some(4));
11559        assert_eq!(r.next(), None);
11560
11561        assert_eq!(pipe.server.stream_send(4, b"world", false), Ok(5));
11562        assert_eq!(pipe.advance(), Ok(()));
11563
11564        // ...and buffers more, and closes stream.
11565        assert_eq!(pipe.server.stream_send(4, b"world", true), Ok(5));
11566
11567        // Client sends STOP_SENDING before server flushes stream.
11568        let frames = [frame::Frame::StopSending {
11569            stream_id: 4,
11570            error_code: 42,
11571        }];
11572
11573        let pkt_type = packet::Type::Short;
11574        let len = pipe
11575            .send_pkt_to_server(pkt_type, &frames, &mut buf)
11576            .unwrap();
11577
11578        // Server sent a RESET_STREAM frame in response.
11579        let frames =
11580            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
11581
11582        let mut iter = frames.iter();
11583
11584        // Skip ACK frame.
11585        iter.next();
11586
11587        assert_eq!(
11588            iter.next(),
11589            Some(&frame::Frame::ResetStream {
11590                stream_id: 4,
11591                error_code: 42,
11592                final_size: 5,
11593            })
11594        );
11595
11596        // No more frames are sent by the server.
11597        assert_eq!(iter.next(), None);
11598    }
11599
11600    #[test]
11601    /// Tests that resetting a stream restores flow control for unsent data.
11602    fn stop_sending_unsent_tx_cap() {
11603        let mut buf = [0; 65535];
11604
11605        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
11606        config
11607            .load_cert_chain_from_pem_file("examples/cert.crt")
11608            .unwrap();
11609        config
11610            .load_priv_key_from_pem_file("examples/cert.key")
11611            .unwrap();
11612        config
11613            .set_application_protos(&[b"proto1", b"proto2"])
11614            .unwrap();
11615        config.set_initial_max_data(15);
11616        config.set_initial_max_stream_data_bidi_local(30);
11617        config.set_initial_max_stream_data_bidi_remote(30);
11618        config.set_initial_max_stream_data_uni(30);
11619        config.set_initial_max_streams_bidi(3);
11620        config.set_initial_max_streams_uni(0);
11621        config.verify_peer(false);
11622
11623        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
11624        assert_eq!(pipe.handshake(), Ok(()));
11625
11626        // Client sends some data.
11627        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
11628        assert_eq!(pipe.advance(), Ok(()));
11629
11630        let mut r = pipe.server.readable();
11631        assert_eq!(r.next(), Some(4));
11632        assert_eq!(r.next(), None);
11633
11634        let mut b = [0; 15];
11635        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
11636
11637        // Server sends some data.
11638        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
11639        assert_eq!(pipe.advance(), Ok(()));
11640
11641        // Server buffers some data, until send capacity limit reached.
11642        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
11643        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
11644        assert_eq!(
11645            pipe.server.stream_send(4, b"hello", false),
11646            Err(Error::Done)
11647        );
11648
11649        // Client sends STOP_SENDING.
11650        let frames = [frame::Frame::StopSending {
11651            stream_id: 4,
11652            error_code: 42,
11653        }];
11654
11655        let pkt_type = packet::Type::Short;
11656        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
11657            .unwrap();
11658
11659        // Server can now send more data (on a different stream).
11660        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
11661        assert_eq!(pipe.advance(), Ok(()));
11662
11663        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
11664        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
11665        assert_eq!(
11666            pipe.server.stream_send(8, b"hello", false),
11667            Err(Error::Done)
11668        );
11669        assert_eq!(pipe.advance(), Ok(()));
11670    }
11671
11672    #[test]
11673    fn stream_shutdown_read() {
11674        let mut buf = [0; 65535];
11675
11676        let mut pipe = testing::Pipe::new().unwrap();
11677        assert_eq!(pipe.handshake(), Ok(()));
11678
11679        // Client sends some data.
11680        assert_eq!(pipe.client.stream_send(4, b"hello, world", false), Ok(12));
11681        assert_eq!(pipe.advance(), Ok(()));
11682
11683        let mut r = pipe.server.readable();
11684        assert_eq!(r.next(), Some(4));
11685        assert_eq!(r.next(), None);
11686
11687        assert_eq!(pipe.client.streams.len(), 1);
11688        assert_eq!(pipe.server.streams.len(), 1);
11689
11690        // Server shuts down stream.
11691        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
11692
11693        let mut r = pipe.server.readable();
11694        assert_eq!(r.next(), None);
11695
11696        let (len, _) = pipe.server.send(&mut buf).unwrap();
11697
11698        let mut dummy = buf[..len].to_vec();
11699
11700        let frames =
11701            testing::decode_pkt(&mut pipe.client, &mut dummy[..len]).unwrap();
11702        let mut iter = frames.iter();
11703
11704        assert_eq!(
11705            iter.next(),
11706            Some(&frame::Frame::StopSending {
11707                stream_id: 4,
11708                error_code: 42,
11709            })
11710        );
11711
11712        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
11713
11714        assert_eq!(pipe.advance(), Ok(()));
11715
11716        // Sending more data is forbidden.
11717        let mut r = pipe.client.writable();
11718        assert_eq!(r.next(), Some(4));
11719        assert_eq!(r.next(), None);
11720
11721        assert_eq!(
11722            pipe.client.stream_send(4, b"bye", false),
11723            Err(Error::StreamStopped(42))
11724        );
11725
11726        // Server sends some data, without reading the incoming data, and closes
11727        // the stream.
11728        assert_eq!(pipe.server.stream_send(4, b"hello, world", true), Ok(12));
11729        assert_eq!(pipe.advance(), Ok(()));
11730
11731        // Client reads the data.
11732        let mut r = pipe.client.readable();
11733        assert_eq!(r.next(), Some(4));
11734        assert_eq!(r.next(), None);
11735
11736        assert_eq!(pipe.client.stream_recv(4, &mut buf), Ok((12, true)));
11737
11738        // Stream is collected on both sides.
11739        assert_eq!(pipe.client.streams.len(), 0);
11740        assert_eq!(pipe.server.streams.len(), 0);
11741
11742        assert_eq!(
11743            pipe.server.stream_shutdown(4, Shutdown::Read, 0),
11744            Err(Error::Done)
11745        );
11746    }
11747
11748    #[test]
11749    fn stream_shutdown_read_after_fin() {
11750        let mut buf = [0; 65535];
11751
11752        let mut pipe = testing::Pipe::new().unwrap();
11753        assert_eq!(pipe.handshake(), Ok(()));
11754
11755        // Client sends some data.
11756        assert_eq!(pipe.client.stream_send(4, b"hello, world", true), Ok(12));
11757        assert_eq!(pipe.advance(), Ok(()));
11758
11759        let mut r = pipe.server.readable();
11760        assert_eq!(r.next(), Some(4));
11761        assert_eq!(r.next(), None);
11762
11763        assert_eq!(pipe.client.streams.len(), 1);
11764        assert_eq!(pipe.server.streams.len(), 1);
11765
11766        // Server shuts down stream.
11767        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 42), Ok(()));
11768
11769        let mut r = pipe.server.readable();
11770        assert_eq!(r.next(), None);
11771
11772        // Server has nothing to send.
11773        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
11774
11775        assert_eq!(pipe.advance(), Ok(()));
11776
11777        // Server sends some data, without reading the incoming data, and closes
11778        // the stream.
11779        assert_eq!(pipe.server.stream_send(4, b"hello, world", true), Ok(12));
11780        assert_eq!(pipe.advance(), Ok(()));
11781
11782        // Client reads the data.
11783        let mut r = pipe.client.readable();
11784        assert_eq!(r.next(), Some(4));
11785        assert_eq!(r.next(), None);
11786
11787        assert_eq!(pipe.client.stream_recv(4, &mut buf), Ok((12, true)));
11788
11789        // Stream is collected on both sides.
11790        assert_eq!(pipe.client.streams.len(), 0);
11791        assert_eq!(pipe.server.streams.len(), 0);
11792
11793        assert_eq!(
11794            pipe.server.stream_shutdown(4, Shutdown::Read, 0),
11795            Err(Error::Done)
11796        );
11797    }
11798
11799    #[test]
11800    fn stream_shutdown_read_update_max_data() {
11801        let mut buf = [0; 65535];
11802
11803        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
11804        config
11805            .load_cert_chain_from_pem_file("examples/cert.crt")
11806            .unwrap();
11807        config
11808            .load_priv_key_from_pem_file("examples/cert.key")
11809            .unwrap();
11810        config
11811            .set_application_protos(&[b"proto1", b"proto2"])
11812            .unwrap();
11813        config.set_initial_max_data(30);
11814        config.set_initial_max_stream_data_bidi_local(10000);
11815        config.set_initial_max_stream_data_bidi_remote(10000);
11816        config.set_initial_max_streams_bidi(10);
11817        config.verify_peer(false);
11818
11819        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
11820        assert_eq!(pipe.handshake(), Ok(()));
11821
11822        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
11823        assert_eq!(pipe.advance(), Ok(()));
11824
11825        assert_eq!(pipe.server.stream_recv(0, &mut buf), Ok((1, false)));
11826        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Read, 123), Ok(()));
11827
11828        assert_eq!(pipe.server.rx_data, 1);
11829        assert_eq!(pipe.client.tx_data, 1);
11830        assert_eq!(pipe.client.max_tx_data, 30);
11831
11832        assert_eq!(
11833            pipe.client
11834                .stream_send(0, &buf[..pipe.client.tx_cap], false),
11835            Ok(29)
11836        );
11837        assert_eq!(pipe.advance(), Ok(()));
11838
11839        assert!(!pipe.server.stream_readable(0)); // nothing can be consumed
11840
11841        // The client has increased its tx_data, and server has received it, so
11842        // it increases flow control accordingly.
11843        assert_eq!(pipe.client.tx_data, 30);
11844        assert_eq!(pipe.server.rx_data, 30);
11845        assert_eq!(pipe.client.tx_cap, 45);
11846    }
11847
11848    #[test]
11849    fn stream_shutdown_uni() {
11850        let mut pipe = testing::Pipe::new().unwrap();
11851        assert_eq!(pipe.handshake(), Ok(()));
11852
11853        // Exchange some data on uni streams.
11854        assert_eq!(pipe.client.stream_send(2, b"hello, world", false), Ok(10));
11855        assert_eq!(pipe.server.stream_send(3, b"hello, world", false), Ok(10));
11856        assert_eq!(pipe.advance(), Ok(()));
11857
11858        // Test local and remote shutdown.
11859        assert_eq!(pipe.client.stream_shutdown(2, Shutdown::Write, 42), Ok(()));
11860        assert_eq!(
11861            pipe.client.stream_shutdown(2, Shutdown::Read, 42),
11862            Err(Error::InvalidStreamState(2))
11863        );
11864
11865        assert_eq!(
11866            pipe.client.stream_shutdown(3, Shutdown::Write, 42),
11867            Err(Error::InvalidStreamState(3))
11868        );
11869        assert_eq!(pipe.client.stream_shutdown(3, Shutdown::Read, 42), Ok(()));
11870    }
11871
11872    #[test]
11873    fn stream_shutdown_write() {
11874        let mut buf = [0; 65535];
11875
11876        let mut pipe = testing::Pipe::new().unwrap();
11877        assert_eq!(pipe.handshake(), Ok(()));
11878
11879        // Client sends some data.
11880        assert_eq!(pipe.client.stream_send(4, b"hello, world", false), Ok(12));
11881        assert_eq!(pipe.advance(), Ok(()));
11882
11883        let mut r = pipe.server.readable();
11884        assert_eq!(r.next(), Some(4));
11885        assert_eq!(r.next(), None);
11886
11887        let mut r = pipe.server.writable();
11888        assert_eq!(r.next(), Some(4));
11889        assert_eq!(r.next(), None);
11890
11891        assert_eq!(pipe.client.streams.len(), 1);
11892        assert_eq!(pipe.server.streams.len(), 1);
11893
11894        // Server sends some data.
11895        assert_eq!(pipe.server.stream_send(4, b"goodbye, world", false), Ok(14));
11896        assert_eq!(pipe.advance(), Ok(()));
11897
11898        // Server shuts down stream.
11899        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Write, 42), Ok(()));
11900
11901        let mut r = pipe.server.writable();
11902        assert_eq!(r.next(), None);
11903
11904        let (len, _) = pipe.server.send(&mut buf).unwrap();
11905
11906        let mut dummy = buf[..len].to_vec();
11907
11908        let frames =
11909            testing::decode_pkt(&mut pipe.client, &mut dummy[..len]).unwrap();
11910        let mut iter = frames.iter();
11911
11912        assert_eq!(
11913            iter.next(),
11914            Some(&frame::Frame::ResetStream {
11915                stream_id: 4,
11916                error_code: 42,
11917                final_size: 14,
11918            })
11919        );
11920
11921        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
11922
11923        assert_eq!(pipe.advance(), Ok(()));
11924
11925        // Sending more data is forbidden.
11926        assert_eq!(
11927            pipe.server.stream_send(4, b"bye", false),
11928            Err(Error::FinalSize)
11929        );
11930
11931        // Client sends some data and closes the stream.
11932        assert_eq!(pipe.client.stream_send(4, b"bye", true), Ok(3));
11933        assert_eq!(pipe.advance(), Ok(()));
11934
11935        // Server reads the data.
11936        let mut r = pipe.server.readable();
11937        assert_eq!(r.next(), Some(4));
11938        assert_eq!(r.next(), None);
11939
11940        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((15, true)));
11941
11942        // Client processes readable streams.
11943        let mut r = pipe.client.readable();
11944        assert_eq!(r.next(), Some(4));
11945        assert_eq!(r.next(), None);
11946
11947        assert_eq!(
11948            pipe.client.stream_recv(4, &mut buf),
11949            Err(Error::StreamReset(42))
11950        );
11951
11952        // Stream is collected on both sides.
11953        assert_eq!(pipe.client.streams.len(), 0);
11954        assert_eq!(pipe.server.streams.len(), 0);
11955
11956        assert_eq!(
11957            pipe.server.stream_shutdown(4, Shutdown::Write, 0),
11958            Err(Error::Done)
11959        );
11960    }
11961
11962    #[test]
11963    /// Tests that shutting down a stream restores flow control for unsent data.
11964    fn stream_shutdown_write_unsent_tx_cap() {
11965        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
11966        config
11967            .load_cert_chain_from_pem_file("examples/cert.crt")
11968            .unwrap();
11969        config
11970            .load_priv_key_from_pem_file("examples/cert.key")
11971            .unwrap();
11972        config
11973            .set_application_protos(&[b"proto1", b"proto2"])
11974            .unwrap();
11975        config.set_initial_max_data(15);
11976        config.set_initial_max_stream_data_bidi_local(30);
11977        config.set_initial_max_stream_data_bidi_remote(30);
11978        config.set_initial_max_stream_data_uni(30);
11979        config.set_initial_max_streams_bidi(3);
11980        config.set_initial_max_streams_uni(0);
11981        config.verify_peer(false);
11982
11983        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
11984        assert_eq!(pipe.handshake(), Ok(()));
11985
11986        // Client sends some data.
11987        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
11988        assert_eq!(pipe.advance(), Ok(()));
11989
11990        let mut r = pipe.server.readable();
11991        assert_eq!(r.next(), Some(4));
11992        assert_eq!(r.next(), None);
11993
11994        let mut b = [0; 15];
11995        assert_eq!(pipe.server.stream_recv(4, &mut b), Ok((5, true)));
11996
11997        // Server sends some data.
11998        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
11999        assert_eq!(pipe.advance(), Ok(()));
12000
12001        // Server buffers some data, until send capacity limit reached.
12002        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12003        assert_eq!(pipe.server.stream_send(4, b"hello", false), Ok(5));
12004        assert_eq!(
12005            pipe.server.stream_send(4, b"hello", false),
12006            Err(Error::Done)
12007        );
12008
12009        // Client shouldn't update flow control.
12010        assert!(!pipe.client.should_update_max_data());
12011
12012        // Server shuts down stream.
12013        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Write, 42), Ok(()));
12014        assert_eq!(pipe.advance(), Ok(()));
12015
12016        // Server can now send more data (on a different stream).
12017        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
12018        assert_eq!(pipe.advance(), Ok(()));
12019
12020        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12021        assert_eq!(pipe.server.stream_send(8, b"hello", false), Ok(5));
12022        assert_eq!(
12023            pipe.server.stream_send(8, b"hello", false),
12024            Err(Error::Done)
12025        );
12026        assert_eq!(pipe.advance(), Ok(()));
12027    }
12028
12029    #[test]
12030    /// Tests that the order of flushable streams scheduled on the wire is the
12031    /// same as the order of `stream_send()` calls done by the application.
12032    fn stream_round_robin() {
12033        let mut buf = [0; 65535];
12034
12035        let mut pipe = testing::Pipe::new().unwrap();
12036        assert_eq!(pipe.handshake(), Ok(()));
12037
12038        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12039        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12040        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12041
12042        let (len, _) = pipe.client.send(&mut buf).unwrap();
12043
12044        let frames =
12045            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12046
12047        let mut iter = frames.iter();
12048
12049        // Skip ACK frame.
12050        iter.next();
12051
12052        assert_eq!(
12053            iter.next(),
12054            Some(&frame::Frame::Stream {
12055                stream_id: 8,
12056                data: stream::RangeBuf::from(b"aaaaa", 0, false),
12057            })
12058        );
12059
12060        let (len, _) = pipe.client.send(&mut buf).unwrap();
12061
12062        let frames =
12063            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12064
12065        assert_eq!(
12066            frames.first(),
12067            Some(&frame::Frame::Stream {
12068                stream_id: 0,
12069                data: stream::RangeBuf::from(b"aaaaa", 0, false),
12070            })
12071        );
12072
12073        let (len, _) = pipe.client.send(&mut buf).unwrap();
12074
12075        let frames =
12076            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
12077
12078        assert_eq!(
12079            frames.first(),
12080            Some(&frame::Frame::Stream {
12081                stream_id: 4,
12082                data: stream::RangeBuf::from(b"aaaaa", 0, false),
12083            })
12084        );
12085    }
12086
12087    #[test]
12088    /// Tests the readable iterator.
12089    fn stream_readable() {
12090        let mut pipe = testing::Pipe::new().unwrap();
12091        assert_eq!(pipe.handshake(), Ok(()));
12092
12093        // No readable streams.
12094        let mut r = pipe.client.readable();
12095        assert_eq!(r.next(), None);
12096
12097        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12098
12099        let mut r = pipe.client.readable();
12100        assert_eq!(r.next(), None);
12101
12102        let mut r = pipe.server.readable();
12103        assert_eq!(r.next(), None);
12104
12105        assert_eq!(pipe.advance(), Ok(()));
12106
12107        // Server received stream.
12108        let mut r = pipe.server.readable();
12109        assert_eq!(r.next(), Some(0));
12110        assert_eq!(r.next(), None);
12111
12112        assert_eq!(
12113            pipe.server.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12114            Ok(15)
12115        );
12116        assert_eq!(pipe.advance(), Ok(()));
12117
12118        let mut r = pipe.client.readable();
12119        assert_eq!(r.next(), Some(0));
12120        assert_eq!(r.next(), None);
12121
12122        // Client drains stream.
12123        let mut b = [0; 15];
12124        pipe.client.stream_recv(0, &mut b).unwrap();
12125        assert_eq!(pipe.advance(), Ok(()));
12126
12127        let mut r = pipe.client.readable();
12128        assert_eq!(r.next(), None);
12129
12130        // Server shuts down stream.
12131        let mut r = pipe.server.readable();
12132        assert_eq!(r.next(), Some(0));
12133        assert_eq!(r.next(), None);
12134
12135        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Read, 0), Ok(()));
12136
12137        let mut r = pipe.server.readable();
12138        assert_eq!(r.next(), None);
12139
12140        // Client creates multiple streams.
12141        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12142        assert_eq!(pipe.advance(), Ok(()));
12143
12144        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12145        assert_eq!(pipe.advance(), Ok(()));
12146
12147        let mut r = pipe.server.readable();
12148        assert_eq!(r.len(), 2);
12149
12150        assert!(r.next().is_some());
12151        assert!(r.next().is_some());
12152        assert!(r.next().is_none());
12153
12154        assert_eq!(r.len(), 0);
12155    }
12156
12157    #[test]
12158    /// Tests the writable iterator.
12159    fn stream_writable() {
12160        let mut pipe = testing::Pipe::new().unwrap();
12161        assert_eq!(pipe.handshake(), Ok(()));
12162
12163        // No writable streams.
12164        let mut w = pipe.client.writable();
12165        assert_eq!(w.next(), None);
12166
12167        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
12168
12169        // Client created stream.
12170        let mut w = pipe.client.writable();
12171        assert_eq!(w.next(), Some(0));
12172        assert_eq!(w.next(), None);
12173
12174        assert_eq!(pipe.advance(), Ok(()));
12175
12176        // Server created stream.
12177        let mut w = pipe.server.writable();
12178        assert_eq!(w.next(), Some(0));
12179        assert_eq!(w.next(), None);
12180
12181        assert_eq!(
12182            pipe.server.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12183            Ok(15)
12184        );
12185
12186        // Server stream is full.
12187        let mut w = pipe.server.writable();
12188        assert_eq!(w.next(), None);
12189
12190        assert_eq!(pipe.advance(), Ok(()));
12191
12192        // Client drains stream.
12193        let mut b = [0; 15];
12194        pipe.client.stream_recv(0, &mut b).unwrap();
12195        assert_eq!(pipe.advance(), Ok(()));
12196
12197        // Server stream is writable again.
12198        let mut w = pipe.server.writable();
12199        assert_eq!(w.next(), Some(0));
12200        assert_eq!(w.next(), None);
12201
12202        // Server shuts down stream.
12203        assert_eq!(pipe.server.stream_shutdown(0, Shutdown::Write, 0), Ok(()));
12204
12205        let mut w = pipe.server.writable();
12206        assert_eq!(w.next(), None);
12207
12208        // Client creates multiple streams.
12209        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
12210        assert_eq!(pipe.advance(), Ok(()));
12211
12212        assert_eq!(pipe.client.stream_send(8, b"aaaaa", false), Ok(5));
12213        assert_eq!(pipe.advance(), Ok(()));
12214
12215        let mut w = pipe.server.writable();
12216        assert_eq!(w.len(), 2);
12217
12218        assert!(w.next().is_some());
12219        assert!(w.next().is_some());
12220        assert!(w.next().is_none());
12221
12222        assert_eq!(w.len(), 0);
12223
12224        // Server finishes stream.
12225        assert_eq!(pipe.server.stream_send(8, b"aaaaa", true), Ok(5));
12226
12227        let mut w = pipe.server.writable();
12228        assert_eq!(w.next(), Some(4));
12229        assert_eq!(w.next(), None);
12230    }
12231
12232    #[test]
12233    fn stream_writable_blocked() {
12234        let mut config = crate::Config::new(crate::PROTOCOL_VERSION).unwrap();
12235        config
12236            .load_cert_chain_from_pem_file("examples/cert.crt")
12237            .unwrap();
12238        config
12239            .load_priv_key_from_pem_file("examples/cert.key")
12240            .unwrap();
12241        config.set_application_protos(&[b"h3"]).unwrap();
12242        config.set_initial_max_data(70);
12243        config.set_initial_max_stream_data_bidi_local(150000);
12244        config.set_initial_max_stream_data_bidi_remote(150000);
12245        config.set_initial_max_stream_data_uni(150000);
12246        config.set_initial_max_streams_bidi(100);
12247        config.set_initial_max_streams_uni(5);
12248        config.verify_peer(false);
12249
12250        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12251        assert_eq!(pipe.handshake(), Ok(()));
12252
12253        // Client creates stream and sends some data.
12254        let send_buf = [0; 35];
12255        assert_eq!(pipe.client.stream_send(0, &send_buf, false), Ok(35));
12256
12257        // Stream is still writable as it still has capacity.
12258        assert_eq!(pipe.client.stream_writable_next(), Some(0));
12259        assert_eq!(pipe.client.stream_writable_next(), None);
12260
12261        // Client fills stream, which becomes unwritable due to connection
12262        // capacity.
12263        let send_buf = [0; 36];
12264        assert_eq!(pipe.client.stream_send(0, &send_buf, false), Ok(35));
12265
12266        assert_eq!(pipe.client.stream_writable_next(), None);
12267
12268        assert_eq!(pipe.client.tx_cap, 0);
12269
12270        assert_eq!(pipe.advance(), Ok(()));
12271
12272        let mut b = [0; 70];
12273        pipe.server.stream_recv(0, &mut b).unwrap();
12274
12275        assert_eq!(pipe.advance(), Ok(()));
12276
12277        // The connection capacity has increased and the stream is now writable
12278        // again.
12279        assert_ne!(pipe.client.tx_cap, 0);
12280
12281        assert_eq!(pipe.client.stream_writable_next(), Some(0));
12282        assert_eq!(pipe.client.stream_writable_next(), None);
12283    }
12284
12285    #[test]
12286    /// Tests that we don't exceed the per-connection flow control limit set by
12287    /// the peer.
12288    fn flow_control_limit_send() {
12289        let mut pipe = testing::Pipe::new().unwrap();
12290        assert_eq!(pipe.handshake(), Ok(()));
12291
12292        assert_eq!(
12293            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12294            Ok(15)
12295        );
12296        assert_eq!(pipe.advance(), Ok(()));
12297        assert_eq!(
12298            pipe.client.stream_send(4, b"aaaaaaaaaaaaaaa", false),
12299            Ok(15)
12300        );
12301        assert_eq!(pipe.advance(), Ok(()));
12302        assert_eq!(pipe.client.stream_send(8, b"a", false), Err(Error::Done));
12303        assert_eq!(pipe.advance(), Ok(()));
12304
12305        let mut r = pipe.server.readable();
12306        assert!(r.next().is_some());
12307        assert!(r.next().is_some());
12308        assert!(r.next().is_none());
12309    }
12310
12311    #[test]
12312    /// Tests that invalid packets received before any other valid ones cause
12313    /// the server to close the connection immediately.
12314    fn invalid_initial_server() {
12315        let mut buf = [0; 65535];
12316        let mut pipe = testing::Pipe::new().unwrap();
12317
12318        let frames = [frame::Frame::Padding { len: 10 }];
12319
12320        let written = testing::encode_pkt(
12321            &mut pipe.client,
12322            packet::Type::Initial,
12323            &frames,
12324            &mut buf,
12325        )
12326        .unwrap();
12327
12328        // Corrupt the packets's last byte to make decryption fail (the last
12329        // byte is part of the AEAD tag, so changing it means that the packet
12330        // cannot be authenticated during decryption).
12331        buf[written - 1] = !buf[written - 1];
12332
12333        assert_eq!(pipe.server.timeout(), None);
12334
12335        assert_eq!(
12336            pipe.server_recv(&mut buf[..written]),
12337            Err(Error::CryptoFail)
12338        );
12339
12340        assert!(pipe.server.is_closed());
12341    }
12342
12343    #[test]
12344    /// Tests that invalid Initial packets received to cause
12345    /// the client to close the connection immediately.
12346    fn invalid_initial_client() {
12347        let mut buf = [0; 65535];
12348        let mut pipe = testing::Pipe::new().unwrap();
12349
12350        // Client sends initial flight.
12351        let (len, _) = pipe.client.send(&mut buf).unwrap();
12352
12353        // Server sends initial flight.
12354        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(1200));
12355
12356        let frames = [frame::Frame::Padding { len: 10 }];
12357
12358        let written = testing::encode_pkt(
12359            &mut pipe.server,
12360            packet::Type::Initial,
12361            &frames,
12362            &mut buf,
12363        )
12364        .unwrap();
12365
12366        // Corrupt the packets's last byte to make decryption fail (the last
12367        // byte is part of the AEAD tag, so changing it means that the packet
12368        // cannot be authenticated during decryption).
12369        buf[written - 1] = !buf[written - 1];
12370
12371        // Client will ignore invalid packet.
12372        assert_eq!(pipe.client_recv(&mut buf[..written]), Ok(71));
12373
12374        // The connection should be alive...
12375        assert!(!pipe.client.is_closed());
12376
12377        // ...and the idle timeout should be armed.
12378        assert!(pipe.client.idle_timer.is_some());
12379    }
12380
12381    #[test]
12382    /// Tests that packets with invalid payload length received before any other
12383    /// valid packet cause the server to close the connection immediately.
12384    fn invalid_initial_payload() {
12385        let mut buf = [0; 65535];
12386        let mut pipe = testing::Pipe::new().unwrap();
12387
12388        let mut b = octets::OctetsMut::with_slice(&mut buf);
12389
12390        let epoch = packet::Type::Initial.to_epoch().unwrap();
12391
12392        let pn = 0;
12393        let pn_len = packet::pkt_num_len(pn, 0);
12394
12395        let dcid = pipe.client.destination_id();
12396        let scid = pipe.client.source_id();
12397
12398        let hdr = Header {
12399            ty: packet::Type::Initial,
12400            version: pipe.client.version,
12401            dcid: ConnectionId::from_ref(&dcid),
12402            scid: ConnectionId::from_ref(&scid),
12403            pkt_num: 0,
12404            pkt_num_len: pn_len,
12405            token: pipe.client.token.clone(),
12406            versions: None,
12407            key_phase: false,
12408        };
12409
12410        hdr.to_bytes(&mut b).unwrap();
12411
12412        // Payload length is invalid!!!
12413        let payload_len = 4096;
12414
12415        let len = pn_len + payload_len;
12416        b.put_varint(len as u64).unwrap();
12417
12418        packet::encode_pkt_num(pn, pn_len, &mut b).unwrap();
12419
12420        let payload_offset = b.off();
12421
12422        let frames = [frame::Frame::Padding { len: 10 }];
12423
12424        for frame in &frames {
12425            frame.to_bytes(&mut b).unwrap();
12426        }
12427
12428        let space = &mut pipe.client.pkt_num_spaces[epoch];
12429
12430        // Use correct payload length when encrypting the packet.
12431        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
12432
12433        let aead = space.crypto_seal.as_ref().unwrap();
12434
12435        let written = packet::encrypt_pkt(
12436            &mut b,
12437            pn,
12438            pn_len,
12439            payload_len,
12440            payload_offset,
12441            None,
12442            aead,
12443        )
12444        .unwrap();
12445
12446        assert_eq!(pipe.server.timeout(), None);
12447
12448        assert_eq!(
12449            pipe.server_recv(&mut buf[..written]),
12450            Err(Error::InvalidPacket)
12451        );
12452
12453        assert!(pipe.server.is_closed());
12454    }
12455
12456    #[test]
12457    /// Tests that invalid packets don't cause the connection to be closed.
12458    fn invalid_packet() {
12459        let mut buf = [0; 65535];
12460
12461        let mut pipe = testing::Pipe::new().unwrap();
12462        assert_eq!(pipe.handshake(), Ok(()));
12463
12464        let frames = [frame::Frame::Padding { len: 10 }];
12465
12466        let written = testing::encode_pkt(
12467            &mut pipe.client,
12468            packet::Type::Short,
12469            &frames,
12470            &mut buf,
12471        )
12472        .unwrap();
12473
12474        // Corrupt the packets's last byte to make decryption fail (the last
12475        // byte is part of the AEAD tag, so changing it means that the packet
12476        // cannot be authenticated during decryption).
12477        buf[written - 1] = !buf[written - 1];
12478
12479        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
12480
12481        // Corrupt the packets's first byte to make the header fail decoding.
12482        buf[0] = 255;
12483
12484        assert_eq!(pipe.server_recv(&mut buf[..written]), Ok(written));
12485    }
12486
12487    #[test]
12488    fn recv_empty_buffer() {
12489        let mut buf = [0; 65535];
12490
12491        let mut pipe = testing::Pipe::new().unwrap();
12492        assert_eq!(pipe.handshake(), Ok(()));
12493
12494        assert_eq!(pipe.server_recv(&mut buf[..0]), Err(Error::BufferTooShort));
12495    }
12496
12497    #[test]
12498    fn stop_sending_before_flushed_packets() {
12499        let mut b = [0; 15];
12500
12501        let mut buf = [0; 65535];
12502
12503        let mut pipe = testing::Pipe::new().unwrap();
12504        assert_eq!(pipe.handshake(), Ok(()));
12505
12506        // Client sends some data, and closes stream.
12507        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
12508        assert_eq!(pipe.advance(), Ok(()));
12509
12510        // Server gets data.
12511        let mut r = pipe.server.readable();
12512        assert_eq!(r.next(), Some(0));
12513        assert_eq!(r.next(), None);
12514
12515        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
12516        assert!(pipe.server.stream_finished(0));
12517
12518        let mut r = pipe.server.readable();
12519        assert_eq!(r.next(), None);
12520
12521        // Server sends data, until blocked.
12522        let mut r = pipe.server.writable();
12523        assert_eq!(r.next(), Some(0));
12524        assert_eq!(r.next(), None);
12525
12526        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {}
12527
12528        let mut r = pipe.server.writable();
12529        assert_eq!(r.next(), None);
12530
12531        // Client sends STOP_SENDING.
12532        let frames = [frame::Frame::StopSending {
12533            stream_id: 0,
12534            error_code: 42,
12535        }];
12536
12537        let pkt_type = packet::Type::Short;
12538        let len = pipe
12539            .send_pkt_to_server(pkt_type, &frames, &mut buf)
12540            .unwrap();
12541
12542        // Server sent a RESET_STREAM frame in response.
12543        let frames =
12544            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
12545
12546        let mut iter = frames.iter();
12547
12548        // Skip ACK frame.
12549        iter.next();
12550
12551        assert_eq!(
12552            iter.next(),
12553            Some(&frame::Frame::ResetStream {
12554                stream_id: 0,
12555                error_code: 42,
12556                final_size: 0,
12557            })
12558        );
12559
12560        // Stream is writable, but writing returns an error.
12561        let mut r = pipe.server.writable();
12562        assert_eq!(r.next(), Some(0));
12563        assert_eq!(r.next(), None);
12564
12565        assert_eq!(
12566            pipe.server.stream_send(0, b"world", true),
12567            Err(Error::StreamStopped(42)),
12568        );
12569
12570        assert_eq!(pipe.server.streams.len(), 1);
12571
12572        // Client acks RESET_STREAM frame.
12573        let mut ranges = ranges::RangeSet::default();
12574        ranges.insert(0..6);
12575
12576        let frames = [frame::Frame::ACK {
12577            ack_delay: 15,
12578            ranges,
12579            ecn_counts: None,
12580        }];
12581
12582        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
12583
12584        // Client has ACK'd the RESET_STREAM so the stream is collected.
12585        assert_eq!(pipe.server.streams.len(), 0);
12586    }
12587
12588    #[test]
12589    fn reset_before_flushed_packets() {
12590        let mut b = [0; 15];
12591
12592        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12593        config
12594            .load_cert_chain_from_pem_file("examples/cert.crt")
12595            .unwrap();
12596        config
12597            .load_priv_key_from_pem_file("examples/cert.key")
12598            .unwrap();
12599        config
12600            .set_application_protos(&[b"proto1", b"proto2"])
12601            .unwrap();
12602        config.set_initial_max_data(30);
12603        config.set_initial_max_stream_data_bidi_local(5);
12604        config.set_initial_max_stream_data_bidi_remote(15);
12605        config.set_initial_max_streams_bidi(3);
12606        config.verify_peer(false);
12607
12608        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12609        assert_eq!(pipe.handshake(), Ok(()));
12610
12611        // Client sends some data, and closes stream.
12612        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
12613        assert_eq!(pipe.advance(), Ok(()));
12614
12615        // Server gets data.
12616        let mut r = pipe.server.readable();
12617        assert_eq!(r.next(), Some(0));
12618        assert_eq!(r.next(), None);
12619
12620        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
12621        assert!(pipe.server.stream_finished(0));
12622
12623        let mut r = pipe.server.readable();
12624        assert_eq!(r.next(), None);
12625
12626        // Server sends data and is blocked by small stream flow control.
12627        let mut r = pipe.server.writable();
12628        assert_eq!(r.next(), Some(0));
12629        assert_eq!(r.next(), None);
12630
12631        assert_eq!(pipe.server.stream_send(0, b"helloworld", false), Ok(5));
12632        assert_eq!(pipe.advance(), Ok(()));
12633
12634        // Client reads to give flow control back.
12635        assert_eq!(pipe.client.stream_recv(0, &mut b), Ok((5, false)));
12636        assert_eq!(pipe.advance(), Ok(()));
12637
12638        // Server writes stream data and resets the stream before sending a
12639        // packet.
12640        assert_eq!(pipe.server.stream_send(0, b"world", false), Ok(5));
12641        pipe.server.stream_shutdown(0, Shutdown::Write, 42).unwrap();
12642        assert_eq!(pipe.advance(), Ok(()));
12643
12644        // Client has ACK'd the RESET_STREAM so the stream is collected.
12645        assert_eq!(pipe.server.streams.len(), 0);
12646    }
12647
12648    #[test]
12649    /// Tests that the MAX_STREAMS frame is sent for bidirectional streams.
12650    fn stream_limit_update_bidi() {
12651        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12652        config
12653            .load_cert_chain_from_pem_file("examples/cert.crt")
12654            .unwrap();
12655        config
12656            .load_priv_key_from_pem_file("examples/cert.key")
12657            .unwrap();
12658        config
12659            .set_application_protos(&[b"proto1", b"proto2"])
12660            .unwrap();
12661        config.set_initial_max_data(30);
12662        config.set_initial_max_stream_data_bidi_local(15);
12663        config.set_initial_max_stream_data_bidi_remote(15);
12664        config.set_initial_max_stream_data_uni(10);
12665        config.set_initial_max_streams_bidi(3);
12666        config.set_initial_max_streams_uni(0);
12667        config.verify_peer(false);
12668
12669        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12670        assert_eq!(pipe.handshake(), Ok(()));
12671
12672        // Client sends stream data.
12673        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
12674        assert_eq!(pipe.advance(), Ok(()));
12675
12676        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
12677        assert_eq!(pipe.advance(), Ok(()));
12678
12679        assert_eq!(pipe.client.stream_send(4, b"b", true), Ok(1));
12680        assert_eq!(pipe.advance(), Ok(()));
12681
12682        assert_eq!(pipe.client.stream_send(0, b"b", true), Ok(1));
12683        assert_eq!(pipe.advance(), Ok(()));
12684
12685        // Server reads stream data.
12686        let mut b = [0; 15];
12687        pipe.server.stream_recv(0, &mut b).unwrap();
12688        pipe.server.stream_recv(4, &mut b).unwrap();
12689        assert_eq!(pipe.advance(), Ok(()));
12690
12691        // Server sends stream data, with fin.
12692        assert_eq!(pipe.server.stream_send(0, b"a", false), Ok(1));
12693        assert_eq!(pipe.advance(), Ok(()));
12694
12695        assert_eq!(pipe.server.stream_send(4, b"a", false), Ok(1));
12696        assert_eq!(pipe.advance(), Ok(()));
12697
12698        assert_eq!(pipe.server.stream_send(4, b"b", true), Ok(1));
12699        assert_eq!(pipe.advance(), Ok(()));
12700
12701        assert_eq!(pipe.server.stream_send(0, b"b", true), Ok(1));
12702
12703        // Server sends MAX_STREAMS.
12704        assert_eq!(pipe.advance(), Ok(()));
12705
12706        // Client tries to create new streams.
12707        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
12708        assert_eq!(pipe.advance(), Ok(()));
12709
12710        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
12711        assert_eq!(pipe.advance(), Ok(()));
12712
12713        assert_eq!(pipe.client.stream_send(16, b"a", false), Ok(1));
12714        assert_eq!(pipe.advance(), Ok(()));
12715
12716        assert_eq!(
12717            pipe.client.stream_send(20, b"a", false),
12718            Err(Error::StreamLimit)
12719        );
12720
12721        assert_eq!(pipe.server.readable().len(), 3);
12722    }
12723
12724    #[test]
12725    /// Tests that the MAX_STREAMS frame is sent for unidirectional streams.
12726    fn stream_limit_update_uni() {
12727        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
12728        config
12729            .load_cert_chain_from_pem_file("examples/cert.crt")
12730            .unwrap();
12731        config
12732            .load_priv_key_from_pem_file("examples/cert.key")
12733            .unwrap();
12734        config
12735            .set_application_protos(&[b"proto1", b"proto2"])
12736            .unwrap();
12737        config.set_initial_max_data(30);
12738        config.set_initial_max_stream_data_bidi_local(15);
12739        config.set_initial_max_stream_data_bidi_remote(15);
12740        config.set_initial_max_stream_data_uni(10);
12741        config.set_initial_max_streams_bidi(0);
12742        config.set_initial_max_streams_uni(3);
12743        config.verify_peer(false);
12744
12745        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
12746        assert_eq!(pipe.handshake(), Ok(()));
12747
12748        // Client sends stream data.
12749        assert_eq!(pipe.client.stream_send(2, b"a", false), Ok(1));
12750        assert_eq!(pipe.advance(), Ok(()));
12751
12752        assert_eq!(pipe.client.stream_send(6, b"a", false), Ok(1));
12753        assert_eq!(pipe.advance(), Ok(()));
12754
12755        assert_eq!(pipe.client.stream_send(6, b"b", true), Ok(1));
12756        assert_eq!(pipe.advance(), Ok(()));
12757
12758        assert_eq!(pipe.client.stream_send(2, b"b", true), Ok(1));
12759        assert_eq!(pipe.advance(), Ok(()));
12760
12761        // Server reads stream data.
12762        let mut b = [0; 15];
12763        pipe.server.stream_recv(2, &mut b).unwrap();
12764        pipe.server.stream_recv(6, &mut b).unwrap();
12765
12766        // Server sends MAX_STREAMS.
12767        assert_eq!(pipe.advance(), Ok(()));
12768
12769        // Client tries to create new streams.
12770        assert_eq!(pipe.client.stream_send(10, b"a", false), Ok(1));
12771        assert_eq!(pipe.advance(), Ok(()));
12772
12773        assert_eq!(pipe.client.stream_send(14, b"a", false), Ok(1));
12774        assert_eq!(pipe.advance(), Ok(()));
12775
12776        assert_eq!(pipe.client.stream_send(18, b"a", false), Ok(1));
12777        assert_eq!(pipe.advance(), Ok(()));
12778
12779        assert_eq!(
12780            pipe.client.stream_send(22, b"a", false),
12781            Err(Error::StreamLimit)
12782        );
12783
12784        assert_eq!(pipe.server.readable().len(), 3);
12785    }
12786
12787    #[test]
12788    /// Tests that the stream's fin flag is properly flushed even if there's no
12789    /// data in the buffer, and that the buffer becomes readable on the other
12790    /// side.
12791    fn stream_zero_length_fin() {
12792        let mut pipe = testing::Pipe::new().unwrap();
12793        assert_eq!(pipe.handshake(), Ok(()));
12794
12795        assert_eq!(
12796            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12797            Ok(15)
12798        );
12799        assert_eq!(pipe.advance(), Ok(()));
12800
12801        let mut r = pipe.server.readable();
12802        assert_eq!(r.next(), Some(0));
12803        assert!(r.next().is_none());
12804
12805        let mut b = [0; 15];
12806        pipe.server.stream_recv(0, &mut b).unwrap();
12807        assert_eq!(pipe.advance(), Ok(()));
12808
12809        // Client sends zero-length frame.
12810        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
12811        assert_eq!(pipe.advance(), Ok(()));
12812
12813        // Stream should be readable on the server after receiving empty fin.
12814        let mut r = pipe.server.readable();
12815        assert_eq!(r.next(), Some(0));
12816        assert!(r.next().is_none());
12817
12818        let mut b = [0; 15];
12819        pipe.server.stream_recv(0, &mut b).unwrap();
12820        assert_eq!(pipe.advance(), Ok(()));
12821
12822        // Client sends zero-length frame (again).
12823        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
12824        assert_eq!(pipe.advance(), Ok(()));
12825
12826        // Stream should _not_ be readable on the server after receiving empty
12827        // fin, because it was already finished.
12828        let mut r = pipe.server.readable();
12829        assert_eq!(r.next(), None);
12830    }
12831
12832    #[test]
12833    /// Tests that the stream's fin flag is properly flushed even if there's no
12834    /// data in the buffer, that the buffer becomes readable on the other
12835    /// side and stays readable even if the stream is fin'd locally.
12836    fn stream_zero_length_fin_deferred_collection() {
12837        let mut pipe = testing::Pipe::new().unwrap();
12838        assert_eq!(pipe.handshake(), Ok(()));
12839
12840        assert_eq!(
12841            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaa", false),
12842            Ok(15)
12843        );
12844        assert_eq!(pipe.advance(), Ok(()));
12845
12846        let mut r = pipe.server.readable();
12847        assert_eq!(r.next(), Some(0));
12848        assert!(r.next().is_none());
12849
12850        let mut b = [0; 15];
12851        pipe.server.stream_recv(0, &mut b).unwrap();
12852        assert_eq!(pipe.advance(), Ok(()));
12853
12854        // Client sends zero-length frame.
12855        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
12856        assert_eq!(pipe.advance(), Ok(()));
12857
12858        // Server sends zero-length frame.
12859        assert_eq!(pipe.server.stream_send(0, b"", true), Ok(0));
12860        assert_eq!(pipe.advance(), Ok(()));
12861
12862        // Stream should be readable on the server after receiving empty fin.
12863        let mut r = pipe.server.readable();
12864        assert_eq!(r.next(), Some(0));
12865        assert!(r.next().is_none());
12866
12867        let mut b = [0; 15];
12868        pipe.server.stream_recv(0, &mut b).unwrap();
12869        assert_eq!(pipe.advance(), Ok(()));
12870
12871        // Client sends zero-length frame (again).
12872        assert_eq!(pipe.client.stream_send(0, b"", true), Ok(0));
12873        assert_eq!(pipe.advance(), Ok(()));
12874
12875        // Stream should _not_ be readable on the server after receiving empty
12876        // fin, because it was already finished.
12877        let mut r = pipe.server.readable();
12878        assert_eq!(r.next(), None);
12879
12880        // Stream _is_readable on the client side.
12881        let mut r = pipe.client.readable();
12882        assert_eq!(r.next(), Some(0));
12883
12884        pipe.client.stream_recv(0, &mut b).unwrap();
12885        assert_eq!(pipe.advance(), Ok(()));
12886
12887        // Stream is completed and _is not_ readable.
12888        let mut r = pipe.client.readable();
12889        assert_eq!(r.next(), None);
12890    }
12891
12892    #[test]
12893    /// Tests that the stream gets created with stream_send() even if there's
12894    /// no data in the buffer and the fin flag is not set.
12895    fn stream_zero_length_non_fin() {
12896        let mut pipe = testing::Pipe::new().unwrap();
12897        assert_eq!(pipe.handshake(), Ok(()));
12898
12899        assert_eq!(pipe.client.stream_send(0, b"", false), Ok(0));
12900
12901        // The stream now should have been created.
12902        assert_eq!(pipe.client.streams.len(), 1);
12903        assert_eq!(pipe.advance(), Ok(()));
12904
12905        // Sending an empty non-fin should not change any stream state on the
12906        // other side.
12907        let mut r = pipe.server.readable();
12908        assert!(r.next().is_none());
12909    }
12910
12911    #[test]
12912    /// Tests that completed streams are garbage collected.
12913    fn collect_streams() {
12914        let mut buf = [0; 65535];
12915
12916        let mut pipe = testing::Pipe::new().unwrap();
12917        assert_eq!(pipe.handshake(), Ok(()));
12918
12919        assert_eq!(pipe.client.streams.len(), 0);
12920        assert_eq!(pipe.server.streams.len(), 0);
12921
12922        assert_eq!(pipe.client.stream_send(0, b"aaaaa", true), Ok(5));
12923        assert_eq!(pipe.advance(), Ok(()));
12924
12925        assert!(!pipe.client.stream_finished(0));
12926        assert!(!pipe.server.stream_finished(0));
12927
12928        assert_eq!(pipe.client.streams.len(), 1);
12929        assert_eq!(pipe.server.streams.len(), 1);
12930
12931        let mut b = [0; 5];
12932        pipe.server.stream_recv(0, &mut b).unwrap();
12933        assert_eq!(pipe.advance(), Ok(()));
12934
12935        assert_eq!(pipe.server.stream_send(0, b"aaaaa", true), Ok(5));
12936        assert_eq!(pipe.advance(), Ok(()));
12937
12938        assert!(!pipe.client.stream_finished(0));
12939        assert!(pipe.server.stream_finished(0));
12940
12941        assert_eq!(pipe.client.streams.len(), 1);
12942        assert_eq!(pipe.server.streams.len(), 0);
12943
12944        let mut b = [0; 5];
12945        pipe.client.stream_recv(0, &mut b).unwrap();
12946        assert_eq!(pipe.advance(), Ok(()));
12947
12948        assert_eq!(pipe.client.streams.len(), 0);
12949        assert_eq!(pipe.server.streams.len(), 0);
12950
12951        assert!(pipe.client.stream_finished(0));
12952        assert!(pipe.server.stream_finished(0));
12953
12954        assert_eq!(pipe.client.stream_send(0, b"", true), Err(Error::Done));
12955
12956        let frames = [frame::Frame::Stream {
12957            stream_id: 0,
12958            data: stream::RangeBuf::from(b"aa", 0, false),
12959        }];
12960
12961        let pkt_type = packet::Type::Short;
12962        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(39));
12963    }
12964
12965    #[test]
12966    fn config_set_cc_algorithm_name() {
12967        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
12968
12969        assert_eq!(config.set_cc_algorithm_name("reno"), Ok(()));
12970
12971        // Unknown name.
12972        assert_eq!(
12973            config.set_cc_algorithm_name("???"),
12974            Err(Error::CongestionControl)
12975        );
12976    }
12977
12978    #[test]
12979    fn peer_cert() {
12980        let mut pipe = testing::Pipe::new().unwrap();
12981        assert_eq!(pipe.handshake(), Ok(()));
12982
12983        match pipe.client.peer_cert() {
12984            Some(c) => assert_eq!(c.len(), 753),
12985
12986            None => panic!("missing server certificate"),
12987        }
12988    }
12989
12990    #[test]
12991    fn peer_cert_chain() {
12992        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
12993        config
12994            .load_cert_chain_from_pem_file("examples/cert-big.crt")
12995            .unwrap();
12996        config
12997            .load_priv_key_from_pem_file("examples/cert.key")
12998            .unwrap();
12999        config
13000            .set_application_protos(&[b"proto1", b"proto2"])
13001            .unwrap();
13002
13003        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13004        assert_eq!(pipe.handshake(), Ok(()));
13005
13006        match pipe.client.peer_cert_chain() {
13007            Some(c) => assert_eq!(c.len(), 5),
13008
13009            None => panic!("missing server certificate chain"),
13010        }
13011    }
13012
13013    #[test]
13014    fn retry() {
13015        let mut buf = [0; 65535];
13016
13017        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13018        config
13019            .load_cert_chain_from_pem_file("examples/cert.crt")
13020            .unwrap();
13021        config
13022            .load_priv_key_from_pem_file("examples/cert.key")
13023            .unwrap();
13024        config
13025            .set_application_protos(&[b"proto1", b"proto2"])
13026            .unwrap();
13027
13028        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13029
13030        // Client sends initial flight.
13031        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13032
13033        // Server sends Retry packet.
13034        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13035
13036        let odcid = hdr.dcid.clone();
13037
13038        let mut scid = [0; MAX_CONN_ID_LEN];
13039        rand::rand_bytes(&mut scid[..]);
13040        let scid = ConnectionId::from_ref(&scid);
13041
13042        let token = b"quiche test retry token";
13043
13044        len = packet::retry(
13045            &hdr.scid,
13046            &hdr.dcid,
13047            &scid,
13048            token,
13049            hdr.version,
13050            &mut buf,
13051        )
13052        .unwrap();
13053
13054        // Client receives Retry and sends new Initial.
13055        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13056
13057        let (len, _) = pipe.client.send(&mut buf).unwrap();
13058
13059        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13060        assert_eq!(&hdr.token.unwrap(), token);
13061
13062        // Server accepts connection.
13063        let from = "127.0.0.1:1234".parse().unwrap();
13064        pipe.server = accept(
13065            &scid,
13066            Some(&odcid),
13067            testing::Pipe::server_addr(),
13068            from,
13069            &mut config,
13070        )
13071        .unwrap();
13072        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13073
13074        assert_eq!(pipe.advance(), Ok(()));
13075
13076        assert!(pipe.client.is_established());
13077        assert!(pipe.server.is_established());
13078    }
13079
13080    #[test]
13081    fn missing_retry_source_connection_id() {
13082        let mut buf = [0; 65535];
13083
13084        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13085        config
13086            .load_cert_chain_from_pem_file("examples/cert.crt")
13087            .unwrap();
13088        config
13089            .load_priv_key_from_pem_file("examples/cert.key")
13090            .unwrap();
13091        config
13092            .set_application_protos(&[b"proto1", b"proto2"])
13093            .unwrap();
13094
13095        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13096
13097        // Client sends initial flight.
13098        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13099
13100        // Server sends Retry packet.
13101        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13102
13103        let mut scid = [0; MAX_CONN_ID_LEN];
13104        rand::rand_bytes(&mut scid[..]);
13105        let scid = ConnectionId::from_ref(&scid);
13106
13107        let token = b"quiche test retry token";
13108
13109        len = packet::retry(
13110            &hdr.scid,
13111            &hdr.dcid,
13112            &scid,
13113            token,
13114            hdr.version,
13115            &mut buf,
13116        )
13117        .unwrap();
13118
13119        // Client receives Retry and sends new Initial.
13120        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13121
13122        let (len, _) = pipe.client.send(&mut buf).unwrap();
13123
13124        // Server accepts connection and send first flight. But original
13125        // destination connection ID is ignored.
13126        let from = "127.0.0.1:1234".parse().unwrap();
13127        pipe.server =
13128            accept(&scid, None, testing::Pipe::server_addr(), from, &mut config)
13129                .unwrap();
13130        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13131
13132        let flight = testing::emit_flight(&mut pipe.server).unwrap();
13133
13134        assert_eq!(
13135            testing::process_flight(&mut pipe.client, flight),
13136            Err(Error::InvalidTransportParam)
13137        );
13138    }
13139
13140    #[test]
13141    fn invalid_retry_source_connection_id() {
13142        let mut buf = [0; 65535];
13143
13144        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13145        config
13146            .load_cert_chain_from_pem_file("examples/cert.crt")
13147            .unwrap();
13148        config
13149            .load_priv_key_from_pem_file("examples/cert.key")
13150            .unwrap();
13151        config
13152            .set_application_protos(&[b"proto1", b"proto2"])
13153            .unwrap();
13154
13155        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
13156
13157        // Client sends initial flight.
13158        let (mut len, _) = pipe.client.send(&mut buf).unwrap();
13159
13160        // Server sends Retry packet.
13161        let hdr = Header::from_slice(&mut buf[..len], MAX_CONN_ID_LEN).unwrap();
13162
13163        let mut scid = [0; MAX_CONN_ID_LEN];
13164        rand::rand_bytes(&mut scid[..]);
13165        let scid = ConnectionId::from_ref(&scid);
13166
13167        let token = b"quiche test retry token";
13168
13169        len = packet::retry(
13170            &hdr.scid,
13171            &hdr.dcid,
13172            &scid,
13173            token,
13174            hdr.version,
13175            &mut buf,
13176        )
13177        .unwrap();
13178
13179        // Client receives Retry and sends new Initial.
13180        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
13181
13182        let (len, _) = pipe.client.send(&mut buf).unwrap();
13183
13184        // Server accepts connection and send first flight. But original
13185        // destination connection ID is invalid.
13186        let from = "127.0.0.1:1234".parse().unwrap();
13187        let odcid = ConnectionId::from_ref(b"bogus value");
13188        pipe.server = accept(
13189            &scid,
13190            Some(&odcid),
13191            testing::Pipe::server_addr(),
13192            from,
13193            &mut config,
13194        )
13195        .unwrap();
13196        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
13197
13198        let flight = testing::emit_flight(&mut pipe.server).unwrap();
13199
13200        assert_eq!(
13201            testing::process_flight(&mut pipe.client, flight),
13202            Err(Error::InvalidTransportParam)
13203        );
13204    }
13205
13206    #[test]
13207    /// Tests that a zero-length NEW_TOKEN frame is detected as an error.
13208    fn zero_length_new_token() {
13209        let mut buf = [0; 65535];
13210
13211        let mut pipe = testing::Pipe::new().unwrap();
13212        assert_eq!(pipe.handshake(), Ok(()));
13213
13214        let frames = vec![frame::Frame::NewToken { token: vec![] }];
13215
13216        let pkt_type = packet::Type::Short;
13217
13218        let written =
13219            testing::encode_pkt(&mut pipe.server, pkt_type, &frames, &mut buf)
13220                .unwrap();
13221
13222        assert_eq!(
13223            pipe.client_recv(&mut buf[..written]),
13224            Err(Error::InvalidFrame)
13225        );
13226    }
13227
13228    #[test]
13229    /// Tests that a NEW_TOKEN frame sent by client is detected as an error.
13230    fn client_sent_new_token() {
13231        let mut buf = [0; 65535];
13232
13233        let mut pipe = testing::Pipe::new().unwrap();
13234        assert_eq!(pipe.handshake(), Ok(()));
13235
13236        let frames = vec![frame::Frame::NewToken {
13237            token: vec![1, 2, 3],
13238        }];
13239
13240        let pkt_type = packet::Type::Short;
13241
13242        let written =
13243            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
13244                .unwrap();
13245
13246        assert_eq!(
13247            pipe.server_recv(&mut buf[..written]),
13248            Err(Error::InvalidPacket)
13249        );
13250    }
13251
13252    fn check_send(_: &mut impl Send) {}
13253
13254    #[test]
13255    fn config_must_be_send() {
13256        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13257        check_send(&mut config);
13258    }
13259
13260    #[test]
13261    fn connection_must_be_send() {
13262        let mut pipe = testing::Pipe::new().unwrap();
13263        check_send(&mut pipe.client);
13264    }
13265
13266    fn check_sync(_: &mut impl Sync) {}
13267
13268    #[test]
13269    fn config_must_be_sync() {
13270        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13271        check_sync(&mut config);
13272    }
13273
13274    #[test]
13275    fn connection_must_be_sync() {
13276        let mut pipe = testing::Pipe::new().unwrap();
13277        check_sync(&mut pipe.client);
13278    }
13279
13280    #[test]
13281    fn data_blocked() {
13282        let mut buf = [0; 65535];
13283
13284        let mut pipe = testing::Pipe::new().unwrap();
13285        assert_eq!(pipe.handshake(), Ok(()));
13286
13287        assert_eq!(pipe.client.stream_send(0, b"aaaaaaaaaa", false), Ok(10));
13288        assert_eq!(pipe.client.blocked_limit, None);
13289        assert_eq!(pipe.advance(), Ok(()));
13290
13291        assert_eq!(pipe.client.stream_send(4, b"aaaaaaaaaa", false), Ok(10));
13292        assert_eq!(pipe.client.blocked_limit, None);
13293        assert_eq!(pipe.advance(), Ok(()));
13294
13295        assert_eq!(pipe.client.stream_send(8, b"aaaaaaaaaaa", false), Ok(10));
13296        assert_eq!(pipe.client.blocked_limit, Some(30));
13297
13298        let (len, _) = pipe.client.send(&mut buf).unwrap();
13299        assert_eq!(pipe.client.blocked_limit, None);
13300
13301        let frames =
13302            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
13303
13304        let mut iter = frames.iter();
13305
13306        assert_eq!(iter.next(), Some(&frame::Frame::DataBlocked { limit: 30 }));
13307
13308        assert_eq!(
13309            iter.next(),
13310            Some(&frame::Frame::Stream {
13311                stream_id: 8,
13312                data: stream::RangeBuf::from(b"aaaaaaaaaa", 0, false),
13313            })
13314        );
13315
13316        assert_eq!(iter.next(), None);
13317    }
13318
13319    #[test]
13320    fn stream_data_blocked() {
13321        let mut buf = [0; 65535];
13322
13323        let mut pipe = testing::Pipe::new().unwrap();
13324        assert_eq!(pipe.handshake(), Ok(()));
13325
13326        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
13327        assert_eq!(pipe.client.streams.blocked().len(), 0);
13328
13329        assert_eq!(pipe.client.stream_send(0, b"aaaaa", false), Ok(5));
13330        assert_eq!(pipe.client.streams.blocked().len(), 0);
13331
13332        assert_eq!(pipe.client.stream_send(0, b"aaaaaa", false), Ok(5));
13333        assert_eq!(pipe.client.streams.blocked().len(), 1);
13334
13335        let (len, _) = pipe.client.send(&mut buf).unwrap();
13336        assert_eq!(pipe.client.streams.blocked().len(), 0);
13337
13338        let frames =
13339            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
13340
13341        let mut iter = frames.iter();
13342
13343        // Skip ACK frame.
13344        iter.next();
13345
13346        assert_eq!(
13347            iter.next(),
13348            Some(&frame::Frame::StreamDataBlocked {
13349                stream_id: 0,
13350                limit: 15,
13351            })
13352        );
13353
13354        assert_eq!(
13355            iter.next(),
13356            Some(&frame::Frame::Stream {
13357                stream_id: 0,
13358                data: stream::RangeBuf::from(b"aaaaaaaaaaaaaaa", 0, false),
13359            })
13360        );
13361
13362        assert_eq!(iter.next(), None);
13363
13364        // Send from another stream, make sure we don't send STREAM_DATA_BLOCKED
13365        // again.
13366        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
13367
13368        let (len, _) = pipe.client.send(&mut buf).unwrap();
13369        assert_eq!(pipe.client.streams.blocked().len(), 0);
13370
13371        let frames =
13372            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
13373
13374        let mut iter = frames.iter();
13375
13376        assert_eq!(
13377            iter.next(),
13378            Some(&frame::Frame::Stream {
13379                stream_id: 4,
13380                data: stream::RangeBuf::from(b"a", 0, false),
13381            })
13382        );
13383
13384        assert_eq!(iter.next(), None);
13385
13386        // Send again from blocked stream and make sure it is not marked as
13387        // blocked again.
13388        assert_eq!(
13389            pipe.client.stream_send(0, b"aaaaaa", false),
13390            Err(Error::Done)
13391        );
13392        assert_eq!(pipe.client.streams.blocked().len(), 0);
13393        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
13394    }
13395
13396    #[test]
13397    fn stream_data_blocked_unblocked_flow_control() {
13398        let mut buf = [0; 65535];
13399        let mut pipe = testing::Pipe::new().unwrap();
13400        assert_eq!(pipe.handshake(), Ok(()));
13401
13402        assert_eq!(
13403            pipe.client.stream_send(0, b"aaaaaaaaaaaaaaah", false),
13404            Ok(15)
13405        );
13406        assert_eq!(pipe.client.streams.blocked().len(), 1);
13407        assert_eq!(pipe.advance(), Ok(()));
13408        assert_eq!(pipe.client.streams.blocked().len(), 0);
13409
13410        // Send again on blocked stream. It's blocked at the same offset as
13411        // previously, so it should not be marked as blocked again.
13412        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
13413        assert_eq!(pipe.client.streams.blocked().len(), 0);
13414
13415        // No matter how many times we try to write stream data tried, no
13416        // packets containing STREAM_BLOCKED should be emitted.
13417        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
13418        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
13419
13420        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
13421        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
13422
13423        assert_eq!(pipe.client.stream_send(0, b"h", false), Err(Error::Done));
13424        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
13425
13426        // Now read some data at the server to release flow control.
13427        let mut r = pipe.server.readable();
13428        assert_eq!(r.next(), Some(0));
13429        assert_eq!(r.next(), None);
13430
13431        let mut b = [0; 10];
13432        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((10, false)));
13433        assert_eq!(&b[..10], b"aaaaaaaaaa");
13434        assert_eq!(pipe.advance(), Ok(()));
13435
13436        assert_eq!(pipe.client.stream_send(0, b"hhhhhhhhhh!", false), Ok(10));
13437        assert_eq!(pipe.client.streams.blocked().len(), 1);
13438
13439        let (len, _) = pipe.client.send(&mut buf).unwrap();
13440        assert_eq!(pipe.client.streams.blocked().len(), 0);
13441
13442        let frames =
13443            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
13444
13445        let mut iter = frames.iter();
13446
13447        assert_eq!(
13448            iter.next(),
13449            Some(&frame::Frame::StreamDataBlocked {
13450                stream_id: 0,
13451                limit: 25,
13452            })
13453        );
13454
13455        // don't care about remaining received frames
13456
13457        assert_eq!(pipe.client.stream_send(0, b"!", false), Err(Error::Done));
13458        assert_eq!(pipe.client.streams.blocked().len(), 0);
13459        assert_eq!(pipe.client.send(&mut buf), Err(Error::Done));
13460    }
13461
13462    #[test]
13463    fn app_limited_true() {
13464        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13465        config
13466            .set_application_protos(&[b"proto1", b"proto2"])
13467            .unwrap();
13468        config.set_initial_max_data(50000);
13469        config.set_initial_max_stream_data_bidi_local(50000);
13470        config.set_initial_max_stream_data_bidi_remote(50000);
13471        config.set_max_recv_udp_payload_size(1200);
13472        config.verify_peer(false);
13473
13474        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
13475        assert_eq!(pipe.handshake(), Ok(()));
13476
13477        // Client sends stream data.
13478        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
13479        assert_eq!(pipe.advance(), Ok(()));
13480
13481        // Server reads stream data.
13482        let mut b = [0; 15];
13483        pipe.server.stream_recv(0, &mut b).unwrap();
13484        assert_eq!(pipe.advance(), Ok(()));
13485
13486        // Server sends stream data smaller than cwnd.
13487        let send_buf = [0; 10000];
13488        assert_eq!(pipe.server.stream_send(0, &send_buf, false), Ok(10000));
13489        assert_eq!(pipe.advance(), Ok(()));
13490
13491        // app_limited should be true because we send less than cwnd.
13492        assert!(pipe
13493            .server
13494            .paths
13495            .get_active()
13496            .expect("no active")
13497            .recovery
13498            .app_limited());
13499    }
13500
13501    #[test]
13502    fn app_limited_false() {
13503        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13504        config
13505            .set_application_protos(&[b"proto1", b"proto2"])
13506            .unwrap();
13507        config.set_initial_max_data(50000);
13508        config.set_initial_max_stream_data_bidi_local(50000);
13509        config.set_initial_max_stream_data_bidi_remote(50000);
13510        config.set_max_recv_udp_payload_size(1200);
13511        config.verify_peer(false);
13512
13513        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
13514        assert_eq!(pipe.handshake(), Ok(()));
13515
13516        // Client sends stream data.
13517        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
13518        assert_eq!(pipe.advance(), Ok(()));
13519
13520        // Server reads stream data.
13521        let mut b = [0; 15];
13522        pipe.server.stream_recv(0, &mut b).unwrap();
13523        assert_eq!(pipe.advance(), Ok(()));
13524
13525        // Server sends stream data bigger than cwnd.
13526        let send_buf1 = [0; 20000];
13527        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
13528
13529        testing::emit_flight(&mut pipe.server).ok();
13530
13531        // We can't create a new packet header because there is no room by cwnd.
13532        // app_limited should be false because we can't send more by cwnd.
13533        assert!(!pipe
13534            .server
13535            .paths
13536            .get_active()
13537            .expect("no active")
13538            .recovery
13539            .app_limited());
13540    }
13541
13542    #[test]
13543    fn sends_ack_only_pkt_when_full_cwnd_and_ack_elicited() {
13544        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13545        config
13546            .load_cert_chain_from_pem_file("examples/cert.crt")
13547            .unwrap();
13548        config
13549            .load_priv_key_from_pem_file("examples/cert.key")
13550            .unwrap();
13551        config
13552            .set_application_protos(&[b"proto1", b"proto2"])
13553            .unwrap();
13554        config.set_initial_max_data(50000);
13555        config.set_initial_max_stream_data_bidi_local(50000);
13556        config.set_initial_max_stream_data_bidi_remote(50000);
13557        config.set_initial_max_streams_bidi(3);
13558        config.set_initial_max_streams_uni(3);
13559        config.set_max_recv_udp_payload_size(1200);
13560        config.verify_peer(false);
13561
13562        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13563        assert_eq!(pipe.handshake(), Ok(()));
13564
13565        // Client sends stream data bigger than cwnd (it will never arrive to the
13566        // server).
13567        let send_buf1 = [0; 20000];
13568        assert_eq!(pipe.client.stream_send(0, &send_buf1, false), Ok(12000));
13569
13570        testing::emit_flight(&mut pipe.client).ok();
13571
13572        // Server sends some stream data that will need ACKs.
13573        assert_eq!(
13574            pipe.server.stream_send(1, &send_buf1[..500], false),
13575            Ok(500)
13576        );
13577
13578        testing::process_flight(
13579            &mut pipe.client,
13580            testing::emit_flight(&mut pipe.server).unwrap(),
13581        )
13582        .unwrap();
13583
13584        let mut buf = [0; 2000];
13585
13586        let ret = pipe.client.send(&mut buf);
13587
13588        assert_eq!(pipe.client.tx_cap, 0);
13589
13590        assert!(matches!(ret, Ok((_, _))), "the client should at least send one packet to acknowledge the newly received data");
13591
13592        let (sent, _) = ret.unwrap();
13593
13594        assert_ne!(sent, 0, "the client should at least send a pure ACK packet");
13595
13596        let frames =
13597            testing::decode_pkt(&mut pipe.server, &mut buf[..sent]).unwrap();
13598        assert_eq!(1, frames.len());
13599        assert!(
13600            matches!(frames[0], frame::Frame::ACK { .. }),
13601            "the packet sent by the client must be an ACK only packet"
13602        );
13603    }
13604
13605    /// Like sends_ack_only_pkt_when_full_cwnd_and_ack_elicited, but when
13606    /// ack_eliciting is explicitly requested.
13607    #[test]
13608    fn sends_ack_only_pkt_when_full_cwnd_and_ack_elicited_despite_max_unacknowledging(
13609    ) {
13610        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13611        config
13612            .load_cert_chain_from_pem_file("examples/cert.crt")
13613            .unwrap();
13614        config
13615            .load_priv_key_from_pem_file("examples/cert.key")
13616            .unwrap();
13617        config
13618            .set_application_protos(&[b"proto1", b"proto2"])
13619            .unwrap();
13620        config.set_initial_max_data(50000);
13621        config.set_initial_max_stream_data_bidi_local(50000);
13622        config.set_initial_max_stream_data_bidi_remote(50000);
13623        config.set_initial_max_streams_bidi(3);
13624        config.set_initial_max_streams_uni(3);
13625        config.set_max_recv_udp_payload_size(1200);
13626        config.verify_peer(false);
13627
13628        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13629        assert_eq!(pipe.handshake(), Ok(()));
13630
13631        // Client sends stream data bigger than cwnd (it will never arrive to the
13632        // server). This exhausts the congestion window.
13633        let send_buf1 = [0; 20000];
13634        assert_eq!(pipe.client.stream_send(0, &send_buf1, false), Ok(12000));
13635
13636        testing::emit_flight(&mut pipe.client).ok();
13637
13638        // Client gets PING frames from server, which elicit ACK
13639        let mut buf = [0; 2000];
13640        for _ in 0..recovery::MAX_OUTSTANDING_NON_ACK_ELICITING {
13641            let written = testing::encode_pkt(
13642                &mut pipe.server,
13643                packet::Type::Short,
13644                &[frame::Frame::Ping { mtu_probe: None }],
13645                &mut buf,
13646            )
13647            .unwrap();
13648
13649            pipe.client_recv(&mut buf[..written])
13650                .expect("client recv ping");
13651
13652            // Client acknowledges despite a full congestion window
13653            let ret = pipe.client.send(&mut buf);
13654
13655            assert!(matches!(ret, Ok((_, _))), "the client should at least send one packet to acknowledge the newly received data");
13656
13657            let (sent, _) = ret.unwrap();
13658
13659            assert_ne!(
13660                sent, 0,
13661                "the client should at least send a pure ACK packet"
13662            );
13663
13664            let frames =
13665                testing::decode_pkt(&mut pipe.server, &mut buf[..sent]).unwrap();
13666
13667            assert_eq!(1, frames.len());
13668
13669            assert!(
13670                matches!(frames[0], frame::Frame::ACK { .. }),
13671                "the packet sent by the client must be an ACK only packet"
13672            );
13673        }
13674
13675        // The client shouldn't need to send any more packets after the ACK only
13676        // packet it just sent.
13677        assert_eq!(
13678            pipe.client.send(&mut buf),
13679            Err(Error::Done),
13680            "nothing for client to send after ACK-only packet"
13681        );
13682    }
13683
13684    #[test]
13685    fn app_limited_false_no_frame() {
13686        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13687        config
13688            .set_application_protos(&[b"proto1", b"proto2"])
13689            .unwrap();
13690        config.set_initial_max_data(50000);
13691        config.set_initial_max_stream_data_bidi_local(50000);
13692        config.set_initial_max_stream_data_bidi_remote(50000);
13693        config.set_max_recv_udp_payload_size(1405);
13694        config.verify_peer(false);
13695
13696        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
13697        assert_eq!(pipe.handshake(), Ok(()));
13698
13699        // Client sends stream data.
13700        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
13701        assert_eq!(pipe.advance(), Ok(()));
13702
13703        // Server reads stream data.
13704        let mut b = [0; 15];
13705        pipe.server.stream_recv(0, &mut b).unwrap();
13706        assert_eq!(pipe.advance(), Ok(()));
13707
13708        // Server sends stream data bigger than cwnd.
13709        let send_buf1 = [0; 20000];
13710        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
13711
13712        testing::emit_flight(&mut pipe.server).ok();
13713
13714        // We can't create a new packet header because there is no room by cwnd.
13715        // app_limited should be false because we can't send more by cwnd.
13716        assert!(!pipe
13717            .server
13718            .paths
13719            .get_active()
13720            .expect("no active")
13721            .recovery
13722            .app_limited());
13723    }
13724
13725    #[test]
13726    fn app_limited_false_no_header() {
13727        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13728        config
13729            .set_application_protos(&[b"proto1", b"proto2"])
13730            .unwrap();
13731        config.set_initial_max_data(50000);
13732        config.set_initial_max_stream_data_bidi_local(50000);
13733        config.set_initial_max_stream_data_bidi_remote(50000);
13734        config.set_max_recv_udp_payload_size(1406);
13735        config.verify_peer(false);
13736
13737        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
13738        assert_eq!(pipe.handshake(), Ok(()));
13739
13740        // Client sends stream data.
13741        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
13742        assert_eq!(pipe.advance(), Ok(()));
13743
13744        // Server reads stream data.
13745        let mut b = [0; 15];
13746        pipe.server.stream_recv(0, &mut b).unwrap();
13747        assert_eq!(pipe.advance(), Ok(()));
13748
13749        // Server sends stream data bigger than cwnd.
13750        let send_buf1 = [0; 20000];
13751        assert_eq!(pipe.server.stream_send(0, &send_buf1, false), Ok(12000));
13752
13753        testing::emit_flight(&mut pipe.server).ok();
13754
13755        // We can't create a new frame because there is no room by cwnd.
13756        // app_limited should be false because we can't send more by cwnd.
13757        assert!(!pipe
13758            .server
13759            .paths
13760            .get_active()
13761            .expect("no active")
13762            .recovery
13763            .app_limited());
13764    }
13765
13766    #[test]
13767    fn app_limited_not_changed_on_no_new_frames() {
13768        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
13769        config
13770            .set_application_protos(&[b"proto1", b"proto2"])
13771            .unwrap();
13772        config.set_initial_max_data(50000);
13773        config.set_initial_max_stream_data_bidi_local(50000);
13774        config.set_initial_max_stream_data_bidi_remote(50000);
13775        config.set_max_recv_udp_payload_size(1200);
13776        config.verify_peer(false);
13777
13778        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
13779        assert_eq!(pipe.handshake(), Ok(()));
13780
13781        // Client sends stream data.
13782        assert_eq!(pipe.client.stream_send(0, b"a", true), Ok(1));
13783        assert_eq!(pipe.advance(), Ok(()));
13784
13785        // Server reads stream data.
13786        let mut b = [0; 15];
13787        pipe.server.stream_recv(0, &mut b).unwrap();
13788        assert_eq!(pipe.advance(), Ok(()));
13789
13790        // Client's app_limited is true because its bytes-in-flight
13791        // is much smaller than the current cwnd.
13792        assert!(pipe
13793            .client
13794            .paths
13795            .get_active()
13796            .expect("no active")
13797            .recovery
13798            .app_limited());
13799
13800        // Client has no new frames to send - returns Done.
13801        assert_eq!(testing::emit_flight(&mut pipe.client), Err(Error::Done));
13802
13803        // Client's app_limited should remain the same.
13804        assert!(pipe
13805            .client
13806            .paths
13807            .get_active()
13808            .expect("no active")
13809            .recovery
13810            .app_limited());
13811    }
13812
13813    #[test]
13814    fn limit_ack_ranges() {
13815        let mut buf = [0; 65535];
13816
13817        let mut pipe = testing::Pipe::new().unwrap();
13818        assert_eq!(pipe.handshake(), Ok(()));
13819
13820        let epoch = packet::Epoch::Application;
13821
13822        assert_eq!(pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.len(), 0);
13823
13824        let frames = [
13825            frame::Frame::Ping { mtu_probe: None },
13826            frame::Frame::Padding { len: 3 },
13827        ];
13828
13829        let pkt_type = packet::Type::Short;
13830
13831        let mut last_packet_sent = 0;
13832
13833        for _ in 0..512 {
13834            let recv_count = pipe.server.recv_count;
13835
13836            last_packet_sent = pipe.client.next_pkt_num;
13837
13838            pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
13839                .unwrap();
13840
13841            assert_eq!(pipe.server.recv_count, recv_count + 1);
13842
13843            // Skip packet number.
13844            pipe.client.next_pkt_num += 1;
13845        }
13846
13847        assert_eq!(
13848            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.len(),
13849            MAX_ACK_RANGES
13850        );
13851
13852        assert_eq!(
13853            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.first(),
13854            Some(last_packet_sent - ((MAX_ACK_RANGES as u64) - 1) * 2)
13855        );
13856
13857        assert_eq!(
13858            pipe.server.pkt_num_spaces[epoch].recv_pkt_need_ack.last(),
13859            Some(last_packet_sent)
13860        );
13861    }
13862
13863    #[test]
13864    /// Tests that streams are correctly scheduled based on their priority.
13865    fn stream_priority() {
13866        // Limit 1-RTT packet size to avoid congestion control interference.
13867        const MAX_TEST_PACKET_SIZE: usize = 540;
13868
13869        let mut buf = [0; 65535];
13870
13871        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
13872        config
13873            .load_cert_chain_from_pem_file("examples/cert.crt")
13874            .unwrap();
13875        config
13876            .load_priv_key_from_pem_file("examples/cert.key")
13877            .unwrap();
13878        config
13879            .set_application_protos(&[b"proto1", b"proto2"])
13880            .unwrap();
13881        config.set_initial_max_data(1_000_000);
13882        config.set_initial_max_stream_data_bidi_local(1_000_000);
13883        config.set_initial_max_stream_data_bidi_remote(1_000_000);
13884        config.set_initial_max_stream_data_uni(0);
13885        config.set_initial_max_streams_bidi(100);
13886        config.set_initial_max_streams_uni(0);
13887        config.verify_peer(false);
13888
13889        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
13890        assert_eq!(pipe.handshake(), Ok(()));
13891
13892        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
13893        assert_eq!(pipe.advance(), Ok(()));
13894
13895        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
13896        assert_eq!(pipe.advance(), Ok(()));
13897
13898        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
13899        assert_eq!(pipe.advance(), Ok(()));
13900
13901        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
13902        assert_eq!(pipe.advance(), Ok(()));
13903
13904        assert_eq!(pipe.client.stream_send(16, b"a", false), Ok(1));
13905        assert_eq!(pipe.advance(), Ok(()));
13906
13907        assert_eq!(pipe.client.stream_send(20, b"a", false), Ok(1));
13908        assert_eq!(pipe.advance(), Ok(()));
13909
13910        let mut b = [0; 1];
13911
13912        let out = [b'b'; 500];
13913
13914        // Server prioritizes streams as follows:
13915        //  * Stream 8 and 16 have the same priority but are non-incremental.
13916        //  * Stream 4, 12 and 20 have the same priority but 20 is non-incremental
13917        //    and 4 and 12 are incremental.
13918        //  * Stream 0 is on its own.
13919
13920        pipe.server.stream_recv(0, &mut b).unwrap();
13921        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
13922        pipe.server.stream_send(0, &out, false).unwrap();
13923        pipe.server.stream_send(0, &out, false).unwrap();
13924        pipe.server.stream_send(0, &out, false).unwrap();
13925
13926        pipe.server.stream_recv(12, &mut b).unwrap();
13927        assert_eq!(pipe.server.stream_priority(12, 42, true), Ok(()));
13928        pipe.server.stream_send(12, &out, false).unwrap();
13929        pipe.server.stream_send(12, &out, false).unwrap();
13930        pipe.server.stream_send(12, &out, false).unwrap();
13931
13932        pipe.server.stream_recv(16, &mut b).unwrap();
13933        assert_eq!(pipe.server.stream_priority(16, 10, false), Ok(()));
13934        pipe.server.stream_send(16, &out, false).unwrap();
13935        pipe.server.stream_send(16, &out, false).unwrap();
13936        pipe.server.stream_send(16, &out, false).unwrap();
13937
13938        pipe.server.stream_recv(4, &mut b).unwrap();
13939        assert_eq!(pipe.server.stream_priority(4, 42, true), Ok(()));
13940        pipe.server.stream_send(4, &out, false).unwrap();
13941        pipe.server.stream_send(4, &out, false).unwrap();
13942        pipe.server.stream_send(4, &out, false).unwrap();
13943
13944        pipe.server.stream_recv(8, &mut b).unwrap();
13945        assert_eq!(pipe.server.stream_priority(8, 10, false), Ok(()));
13946        pipe.server.stream_send(8, &out, false).unwrap();
13947        pipe.server.stream_send(8, &out, false).unwrap();
13948        pipe.server.stream_send(8, &out, false).unwrap();
13949
13950        pipe.server.stream_recv(20, &mut b).unwrap();
13951        assert_eq!(pipe.server.stream_priority(20, 42, false), Ok(()));
13952        pipe.server.stream_send(20, &out, false).unwrap();
13953        pipe.server.stream_send(20, &out, false).unwrap();
13954        pipe.server.stream_send(20, &out, false).unwrap();
13955
13956        // First is stream 8.
13957        let mut off = 0;
13958
13959        for _ in 1..=3 {
13960            let (len, _) =
13961                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
13962
13963            let frames =
13964                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
13965            let stream = frames.first().unwrap();
13966
13967            assert_eq!(stream, &frame::Frame::Stream {
13968                stream_id: 8,
13969                data: stream::RangeBuf::from(&out, off, false),
13970            });
13971
13972            off = match stream {
13973                frame::Frame::Stream { data, .. } => data.max_off(),
13974
13975                _ => unreachable!(),
13976            };
13977        }
13978
13979        // Then is stream 16.
13980        let mut off = 0;
13981
13982        for _ in 1..=3 {
13983            let (len, _) =
13984                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
13985
13986            let frames =
13987                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
13988            let stream = frames.first().unwrap();
13989
13990            assert_eq!(stream, &frame::Frame::Stream {
13991                stream_id: 16,
13992                data: stream::RangeBuf::from(&out, off, false),
13993            });
13994
13995            off = match stream {
13996                frame::Frame::Stream { data, .. } => data.max_off(),
13997
13998                _ => unreachable!(),
13999            };
14000        }
14001
14002        // Then is stream 20.
14003        let mut off = 0;
14004
14005        for _ in 1..=3 {
14006            let (len, _) =
14007                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14008
14009            let frames =
14010                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14011            let stream = frames.first().unwrap();
14012
14013            assert_eq!(stream, &frame::Frame::Stream {
14014                stream_id: 20,
14015                data: stream::RangeBuf::from(&out, off, false),
14016            });
14017
14018            off = match stream {
14019                frame::Frame::Stream { data, .. } => data.max_off(),
14020
14021                _ => unreachable!(),
14022            };
14023        }
14024
14025        // Then are stream 12 and 4, with the same priority, incrementally.
14026        let mut off = 0;
14027
14028        for _ in 1..=3 {
14029            let (len, _) =
14030                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14031
14032            let frames =
14033                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14034
14035            assert_eq!(
14036                frames.first(),
14037                Some(&frame::Frame::Stream {
14038                    stream_id: 12,
14039                    data: stream::RangeBuf::from(&out, off, false),
14040                })
14041            );
14042
14043            let (len, _) =
14044                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14045
14046            let frames =
14047                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14048
14049            let stream = frames.first().unwrap();
14050
14051            assert_eq!(stream, &frame::Frame::Stream {
14052                stream_id: 4,
14053                data: stream::RangeBuf::from(&out, off, false),
14054            });
14055
14056            off = match stream {
14057                frame::Frame::Stream { data, .. } => data.max_off(),
14058
14059                _ => unreachable!(),
14060            };
14061        }
14062
14063        // Final is stream 0.
14064        let mut off = 0;
14065
14066        for _ in 1..=3 {
14067            let (len, _) =
14068                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14069
14070            let frames =
14071                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14072            let stream = frames.first().unwrap();
14073
14074            assert_eq!(stream, &frame::Frame::Stream {
14075                stream_id: 0,
14076                data: stream::RangeBuf::from(&out, off, false),
14077            });
14078
14079            off = match stream {
14080                frame::Frame::Stream { data, .. } => data.max_off(),
14081
14082                _ => unreachable!(),
14083            };
14084        }
14085
14086        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
14087    }
14088
14089    #[test]
14090    /// Tests that changing a stream's priority is correctly propagated.
14091    fn stream_reprioritize() {
14092        let mut buf = [0; 65535];
14093
14094        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14095        config
14096            .load_cert_chain_from_pem_file("examples/cert.crt")
14097            .unwrap();
14098        config
14099            .load_priv_key_from_pem_file("examples/cert.key")
14100            .unwrap();
14101        config
14102            .set_application_protos(&[b"proto1", b"proto2"])
14103            .unwrap();
14104        config.set_initial_max_data(30);
14105        config.set_initial_max_stream_data_bidi_local(15);
14106        config.set_initial_max_stream_data_bidi_remote(15);
14107        config.set_initial_max_stream_data_uni(0);
14108        config.set_initial_max_streams_bidi(5);
14109        config.set_initial_max_streams_uni(0);
14110        config.verify_peer(false);
14111
14112        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14113        assert_eq!(pipe.handshake(), Ok(()));
14114
14115        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
14116        assert_eq!(pipe.advance(), Ok(()));
14117
14118        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
14119        assert_eq!(pipe.advance(), Ok(()));
14120
14121        assert_eq!(pipe.client.stream_send(8, b"a", false), Ok(1));
14122        assert_eq!(pipe.advance(), Ok(()));
14123
14124        assert_eq!(pipe.client.stream_send(12, b"a", false), Ok(1));
14125        assert_eq!(pipe.advance(), Ok(()));
14126
14127        let mut b = [0; 1];
14128
14129        pipe.server.stream_recv(0, &mut b).unwrap();
14130        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
14131        pipe.server.stream_send(0, b"b", false).unwrap();
14132
14133        pipe.server.stream_recv(12, &mut b).unwrap();
14134        assert_eq!(pipe.server.stream_priority(12, 42, true), Ok(()));
14135        pipe.server.stream_send(12, b"b", false).unwrap();
14136
14137        pipe.server.stream_recv(8, &mut b).unwrap();
14138        assert_eq!(pipe.server.stream_priority(8, 10, true), Ok(()));
14139        pipe.server.stream_send(8, b"b", false).unwrap();
14140
14141        pipe.server.stream_recv(4, &mut b).unwrap();
14142        assert_eq!(pipe.server.stream_priority(4, 42, true), Ok(()));
14143        pipe.server.stream_send(4, b"b", false).unwrap();
14144
14145        // Stream 0 is re-prioritized!!!
14146        assert_eq!(pipe.server.stream_priority(0, 20, true), Ok(()));
14147
14148        // First is stream 8.
14149        let (len, _) = pipe.server.send(&mut buf).unwrap();
14150
14151        let frames =
14152            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14153
14154        assert_eq!(
14155            frames.first(),
14156            Some(&frame::Frame::Stream {
14157                stream_id: 8,
14158                data: stream::RangeBuf::from(b"b", 0, false),
14159            })
14160        );
14161
14162        // Then is stream 0.
14163        let (len, _) = pipe.server.send(&mut buf).unwrap();
14164
14165        let frames =
14166            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14167
14168        assert_eq!(
14169            frames.first(),
14170            Some(&frame::Frame::Stream {
14171                stream_id: 0,
14172                data: stream::RangeBuf::from(b"b", 0, false),
14173            })
14174        );
14175
14176        // Then are stream 12 and 4, with the same priority.
14177        let (len, _) = pipe.server.send(&mut buf).unwrap();
14178
14179        let frames =
14180            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14181
14182        assert_eq!(
14183            frames.first(),
14184            Some(&frame::Frame::Stream {
14185                stream_id: 12,
14186                data: stream::RangeBuf::from(b"b", 0, false),
14187            })
14188        );
14189
14190        let (len, _) = pipe.server.send(&mut buf).unwrap();
14191
14192        let frames =
14193            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14194
14195        assert_eq!(
14196            frames.first(),
14197            Some(&frame::Frame::Stream {
14198                stream_id: 4,
14199                data: stream::RangeBuf::from(b"b", 0, false),
14200            })
14201        );
14202
14203        assert_eq!(pipe.server.send(&mut buf), Err(Error::Done));
14204    }
14205
14206    #[test]
14207    /// Tests that streams and datagrams are correctly scheduled.
14208    fn stream_datagram_priority() {
14209        // Limit 1-RTT packet size to avoid congestion control interference.
14210        const MAX_TEST_PACKET_SIZE: usize = 540;
14211
14212        let mut buf = [0; 65535];
14213
14214        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14215        config
14216            .load_cert_chain_from_pem_file("examples/cert.crt")
14217            .unwrap();
14218        config
14219            .load_priv_key_from_pem_file("examples/cert.key")
14220            .unwrap();
14221        config
14222            .set_application_protos(&[b"proto1", b"proto2"])
14223            .unwrap();
14224        config.set_initial_max_data(1_000_000);
14225        config.set_initial_max_stream_data_bidi_local(1_000_000);
14226        config.set_initial_max_stream_data_bidi_remote(1_000_000);
14227        config.set_initial_max_stream_data_uni(0);
14228        config.set_initial_max_streams_bidi(100);
14229        config.set_initial_max_streams_uni(0);
14230        config.enable_dgram(true, 10, 10);
14231        config.verify_peer(false);
14232
14233        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14234        assert_eq!(pipe.handshake(), Ok(()));
14235
14236        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
14237        assert_eq!(pipe.advance(), Ok(()));
14238
14239        assert_eq!(pipe.client.stream_send(4, b"a", false), Ok(1));
14240        assert_eq!(pipe.advance(), Ok(()));
14241
14242        let mut b = [0; 1];
14243
14244        let out = [b'b'; 500];
14245
14246        // Server prioritizes Stream 0 and 4 with the same urgency with
14247        // incremental, meaning the frames should be sent in round-robin
14248        // fashion. It also sends DATAGRAMS which are always interleaved with
14249        // STREAM frames. So we'll expect a mix of frame types regardless
14250        // of the order that the application writes things in.
14251
14252        pipe.server.stream_recv(0, &mut b).unwrap();
14253        assert_eq!(pipe.server.stream_priority(0, 255, true), Ok(()));
14254        pipe.server.stream_send(0, &out, false).unwrap();
14255        pipe.server.stream_send(0, &out, false).unwrap();
14256        pipe.server.stream_send(0, &out, false).unwrap();
14257
14258        assert_eq!(pipe.server.stream_priority(4, 255, true), Ok(()));
14259        pipe.server.stream_send(4, &out, false).unwrap();
14260        pipe.server.stream_send(4, &out, false).unwrap();
14261        pipe.server.stream_send(4, &out, false).unwrap();
14262
14263        for _ in 1..=6 {
14264            assert_eq!(pipe.server.dgram_send(&out), Ok(()));
14265        }
14266
14267        let mut off_0 = 0;
14268        let mut off_4 = 0;
14269
14270        for _ in 1..=3 {
14271            // DATAGRAM
14272            let (len, _) =
14273                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14274
14275            let frames =
14276                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14277            let mut frame_iter = frames.iter();
14278
14279            assert_eq!(frame_iter.next().unwrap(), &frame::Frame::Datagram {
14280                data: out.into()
14281            });
14282            assert_eq!(frame_iter.next(), None);
14283
14284            // STREAM 0
14285            let (len, _) =
14286                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14287
14288            let frames =
14289                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14290            let mut frame_iter = frames.iter();
14291            let stream = frame_iter.next().unwrap();
14292
14293            assert_eq!(stream, &frame::Frame::Stream {
14294                stream_id: 0,
14295                data: stream::RangeBuf::from(&out, off_0, false),
14296            });
14297
14298            off_0 = match stream {
14299                frame::Frame::Stream { data, .. } => data.max_off(),
14300
14301                _ => unreachable!(),
14302            };
14303            assert_eq!(frame_iter.next(), None);
14304
14305            // DATAGRAM
14306            let (len, _) =
14307                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14308
14309            let frames =
14310                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14311            let mut frame_iter = frames.iter();
14312
14313            assert_eq!(frame_iter.next().unwrap(), &frame::Frame::Datagram {
14314                data: out.into()
14315            });
14316            assert_eq!(frame_iter.next(), None);
14317
14318            // STREAM 4
14319            let (len, _) =
14320                pipe.server.send(&mut buf[..MAX_TEST_PACKET_SIZE]).unwrap();
14321
14322            let frames =
14323                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
14324            let mut frame_iter = frames.iter();
14325            let stream = frame_iter.next().unwrap();
14326
14327            assert_eq!(stream, &frame::Frame::Stream {
14328                stream_id: 4,
14329                data: stream::RangeBuf::from(&out, off_4, false),
14330            });
14331
14332            off_4 = match stream {
14333                frame::Frame::Stream { data, .. } => data.max_off(),
14334
14335                _ => unreachable!(),
14336            };
14337            assert_eq!(frame_iter.next(), None);
14338        }
14339    }
14340
14341    #[test]
14342    /// Tests that old data is retransmitted on PTO.
14343    fn early_retransmit() {
14344        let mut buf = [0; 65535];
14345
14346        let mut pipe = testing::Pipe::new().unwrap();
14347        assert_eq!(pipe.handshake(), Ok(()));
14348
14349        // Client sends stream data.
14350        assert_eq!(pipe.client.stream_send(0, b"a", false), Ok(1));
14351        assert_eq!(pipe.advance(), Ok(()));
14352
14353        // Client sends more stream data, but packet is lost
14354        assert_eq!(pipe.client.stream_send(4, b"b", false), Ok(1));
14355        assert!(pipe.client.send(&mut buf).is_ok());
14356
14357        // Wait until PTO expires. Since the RTT is very low, wait a bit more.
14358        let timer = pipe.client.timeout().unwrap();
14359        std::thread::sleep(timer + time::Duration::from_millis(1));
14360
14361        pipe.client.on_timeout();
14362
14363        let epoch = packet::Epoch::Application;
14364        assert_eq!(
14365            pipe.client
14366                .paths
14367                .get_active()
14368                .expect("no active")
14369                .recovery
14370                .loss_probes(epoch),
14371            1,
14372        );
14373
14374        // Client retransmits stream data in PTO probe.
14375        let (len, _) = pipe.client.send(&mut buf).unwrap();
14376        assert_eq!(
14377            pipe.client
14378                .paths
14379                .get_active()
14380                .expect("no active")
14381                .recovery
14382                .loss_probes(epoch),
14383            0,
14384        );
14385
14386        let frames =
14387            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
14388
14389        let mut iter = frames.iter();
14390
14391        // Skip ACK frame.
14392        iter.next();
14393
14394        assert_eq!(
14395            iter.next(),
14396            Some(&frame::Frame::Stream {
14397                stream_id: 4,
14398                data: stream::RangeBuf::from(b"b", 0, false),
14399            })
14400        );
14401        assert_eq!(pipe.client.stats().retrans, 1);
14402    }
14403
14404    #[test]
14405    /// Tests that PTO probe packets are not coalesced together.
14406    fn dont_coalesce_probes() {
14407        let mut buf = [0; 65535];
14408
14409        let mut pipe = testing::Pipe::new().unwrap();
14410
14411        // Client sends Initial packet.
14412        let (len, _) = pipe.client.send(&mut buf).unwrap();
14413        assert_eq!(len, 1200);
14414
14415        // Wait for PTO to expire.
14416        let timer = pipe.client.timeout().unwrap();
14417        std::thread::sleep(timer + time::Duration::from_millis(1));
14418
14419        pipe.client.on_timeout();
14420
14421        let epoch = packet::Epoch::Initial;
14422        assert_eq!(
14423            pipe.client
14424                .paths
14425                .get_active()
14426                .expect("no active")
14427                .recovery
14428                .loss_probes(epoch),
14429            1,
14430        );
14431
14432        // Client sends PTO probe.
14433        let (len, _) = pipe.client.send(&mut buf).unwrap();
14434        assert_eq!(len, 1200);
14435        assert_eq!(
14436            pipe.client
14437                .paths
14438                .get_active()
14439                .expect("no active")
14440                .recovery
14441                .loss_probes(epoch),
14442            0,
14443        );
14444
14445        // Wait for PTO to expire.
14446        let timer = pipe.client.timeout().unwrap();
14447        std::thread::sleep(timer + time::Duration::from_millis(1));
14448
14449        pipe.client.on_timeout();
14450
14451        assert_eq!(
14452            pipe.client
14453                .paths
14454                .get_active()
14455                .expect("no active")
14456                .recovery
14457                .loss_probes(epoch),
14458            2,
14459        );
14460
14461        // Client sends first PTO probe.
14462        let (len, _) = pipe.client.send(&mut buf).unwrap();
14463        assert_eq!(len, 1200);
14464        assert_eq!(
14465            pipe.client
14466                .paths
14467                .get_active()
14468                .expect("no active")
14469                .recovery
14470                .loss_probes(epoch),
14471            1,
14472        );
14473
14474        // Client sends second PTO probe.
14475        let (len, _) = pipe.client.send(&mut buf).unwrap();
14476        assert_eq!(len, 1200);
14477        assert_eq!(
14478            pipe.client
14479                .paths
14480                .get_active()
14481                .expect("no active")
14482                .recovery
14483                .loss_probes(epoch),
14484            0,
14485        );
14486    }
14487
14488    #[test]
14489    fn coalesce_padding_short() {
14490        let mut buf = [0; 65535];
14491
14492        let mut pipe = testing::Pipe::new().unwrap();
14493
14494        // Client sends first flight.
14495        let (len, _) = pipe.client.send(&mut buf).unwrap();
14496        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
14497        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14498
14499        // Server sends first flight.
14500        let (len, _) = pipe.server.send(&mut buf).unwrap();
14501        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
14502        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
14503
14504        let (len, _) = pipe.server.send(&mut buf).unwrap();
14505        assert_eq!(pipe.client_recv(&mut buf[..len]), Ok(len));
14506
14507        // Client sends stream data.
14508        assert!(pipe.client.is_established());
14509        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
14510
14511        // Client sends second flight.
14512        let (len, _) = pipe.client.send(&mut buf).unwrap();
14513        assert_eq!(len, MIN_CLIENT_INITIAL_LEN);
14514        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14515
14516        // None of the sent packets should have been dropped.
14517        assert_eq!(pipe.client.sent_count, pipe.server.recv_count);
14518        assert_eq!(pipe.server.sent_count, pipe.client.recv_count);
14519    }
14520
14521    #[test]
14522    /// Tests that client avoids handshake deadlock by arming PTO.
14523    fn handshake_anti_deadlock() {
14524        let mut buf = [0; 65535];
14525
14526        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
14527        config
14528            .load_cert_chain_from_pem_file("examples/cert-big.crt")
14529            .unwrap();
14530        config
14531            .load_priv_key_from_pem_file("examples/cert.key")
14532            .unwrap();
14533        config
14534            .set_application_protos(&[b"proto1", b"proto2"])
14535            .unwrap();
14536
14537        let mut pipe = testing::Pipe::with_server_config(&mut config).unwrap();
14538
14539        assert!(!pipe.client.handshake_status().has_handshake_keys);
14540        assert!(!pipe.client.handshake_status().peer_verified_address);
14541        assert!(!pipe.server.handshake_status().has_handshake_keys);
14542        assert!(pipe.server.handshake_status().peer_verified_address);
14543
14544        // Client sends padded Initial.
14545        let (len, _) = pipe.client.send(&mut buf).unwrap();
14546        assert_eq!(len, 1200);
14547
14548        // Server receives client's Initial and sends own Initial and Handshake
14549        // until it's blocked by the anti-amplification limit.
14550        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14551        let flight = testing::emit_flight(&mut pipe.server).unwrap();
14552
14553        assert!(!pipe.client.handshake_status().has_handshake_keys);
14554        assert!(!pipe.client.handshake_status().peer_verified_address);
14555        assert!(pipe.server.handshake_status().has_handshake_keys);
14556        assert!(pipe.server.handshake_status().peer_verified_address);
14557
14558        // Client receives the server flight and sends Handshake ACK, but it is
14559        // lost.
14560        testing::process_flight(&mut pipe.client, flight).unwrap();
14561        testing::emit_flight(&mut pipe.client).unwrap();
14562
14563        assert!(pipe.client.handshake_status().has_handshake_keys);
14564        assert!(!pipe.client.handshake_status().peer_verified_address);
14565        assert!(pipe.server.handshake_status().has_handshake_keys);
14566        assert!(pipe.server.handshake_status().peer_verified_address);
14567
14568        // Make sure client's PTO timer is armed.
14569        assert!(pipe.client.timeout().is_some());
14570    }
14571
14572    #[test]
14573    /// Tests that packets with corrupted type (from Handshake to Initial) are
14574    /// properly ignored.
14575    fn handshake_packet_type_corruption() {
14576        let mut buf = [0; 65535];
14577
14578        let mut pipe = testing::Pipe::new().unwrap();
14579
14580        // Client sends padded Initial.
14581        let (len, _) = pipe.client.send(&mut buf).unwrap();
14582        assert_eq!(len, 1200);
14583
14584        // Server receives client's Initial and sends own Initial and Handshake.
14585        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14586
14587        let flight = testing::emit_flight(&mut pipe.server).unwrap();
14588        testing::process_flight(&mut pipe.client, flight).unwrap();
14589
14590        // Client sends Initial packet with ACK.
14591        let active_pid =
14592            pipe.client.paths.get_active_path_id().expect("no active");
14593        let (ty, len) = pipe
14594            .client
14595            .send_single(&mut buf, active_pid, false, time::Instant::now())
14596            .unwrap();
14597        assert_eq!(ty, Type::Initial);
14598
14599        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14600
14601        // Client sends Handshake packet.
14602        let (ty, len) = pipe
14603            .client
14604            .send_single(&mut buf, active_pid, false, time::Instant::now())
14605            .unwrap();
14606        assert_eq!(ty, Type::Handshake);
14607
14608        // Packet type is corrupted to Initial.
14609        buf[0] &= !(0x20);
14610
14611        let hdr = Header::from_slice(&mut buf[..len], 0).unwrap();
14612        assert_eq!(hdr.ty, Type::Initial);
14613
14614        // Server receives corrupted packet without returning an error.
14615        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14616    }
14617
14618    #[test]
14619    fn dgram_send_fails_invalidstate() {
14620        let mut pipe = testing::Pipe::new().unwrap();
14621        assert_eq!(pipe.handshake(), Ok(()));
14622
14623        assert_eq!(
14624            pipe.client.dgram_send(b"hello, world"),
14625            Err(Error::InvalidState)
14626        );
14627    }
14628
14629    #[test]
14630    fn dgram_send_app_limited() {
14631        let mut buf = [0; 65535];
14632        let send_buf = [0xcf; 1000];
14633
14634        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14635        config
14636            .load_cert_chain_from_pem_file("examples/cert.crt")
14637            .unwrap();
14638        config
14639            .load_priv_key_from_pem_file("examples/cert.key")
14640            .unwrap();
14641        config
14642            .set_application_protos(&[b"proto1", b"proto2"])
14643            .unwrap();
14644        config.set_initial_max_data(30);
14645        config.set_initial_max_stream_data_bidi_local(15);
14646        config.set_initial_max_stream_data_bidi_remote(15);
14647        config.set_initial_max_stream_data_uni(10);
14648        config.set_initial_max_streams_bidi(3);
14649        config.set_initial_max_streams_uni(3);
14650        config.enable_dgram(true, 1000, 1000);
14651        config.set_max_recv_udp_payload_size(1200);
14652        config.verify_peer(false);
14653
14654        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14655        assert_eq!(pipe.handshake(), Ok(()));
14656
14657        for _ in 0..1000 {
14658            assert_eq!(pipe.client.dgram_send(&send_buf), Ok(()));
14659        }
14660
14661        assert!(!pipe
14662            .client
14663            .paths
14664            .get_active()
14665            .expect("no active")
14666            .recovery
14667            .app_limited());
14668        assert_eq!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
14669
14670        let (len, _) = pipe.client.send(&mut buf).unwrap();
14671
14672        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 0);
14673        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
14674        assert!(!pipe
14675            .client
14676            .paths
14677            .get_active()
14678            .expect("no active")
14679            .recovery
14680            .app_limited());
14681
14682        assert_eq!(pipe.server_recv(&mut buf[..len]), Ok(len));
14683
14684        let flight = testing::emit_flight(&mut pipe.client).unwrap();
14685        testing::process_flight(&mut pipe.server, flight).unwrap();
14686
14687        let flight = testing::emit_flight(&mut pipe.server).unwrap();
14688        testing::process_flight(&mut pipe.client, flight).unwrap();
14689
14690        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 0);
14691        assert_ne!(pipe.client.dgram_send_queue.byte_size(), 1_000_000);
14692
14693        assert!(!pipe
14694            .client
14695            .paths
14696            .get_active()
14697            .expect("no active")
14698            .recovery
14699            .app_limited());
14700    }
14701
14702    #[test]
14703    fn dgram_single_datagram() {
14704        let mut buf = [0; 65535];
14705
14706        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14707        config
14708            .load_cert_chain_from_pem_file("examples/cert.crt")
14709            .unwrap();
14710        config
14711            .load_priv_key_from_pem_file("examples/cert.key")
14712            .unwrap();
14713        config
14714            .set_application_protos(&[b"proto1", b"proto2"])
14715            .unwrap();
14716        config.set_initial_max_data(30);
14717        config.set_initial_max_stream_data_bidi_local(15);
14718        config.set_initial_max_stream_data_bidi_remote(15);
14719        config.set_initial_max_stream_data_uni(10);
14720        config.set_initial_max_streams_bidi(3);
14721        config.set_initial_max_streams_uni(3);
14722        config.enable_dgram(true, 10, 10);
14723        config.verify_peer(false);
14724
14725        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14726        assert_eq!(pipe.handshake(), Ok(()));
14727
14728        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
14729
14730        assert_eq!(pipe.advance(), Ok(()));
14731
14732        let result1 = pipe.server.dgram_recv(&mut buf);
14733        assert_eq!(result1, Ok(12));
14734
14735        let result2 = pipe.server.dgram_recv(&mut buf);
14736        assert_eq!(result2, Err(Error::Done));
14737    }
14738
14739    #[test]
14740    fn dgram_multiple_datagrams() {
14741        let mut buf = [0; 65535];
14742
14743        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14744        config
14745            .load_cert_chain_from_pem_file("examples/cert.crt")
14746            .unwrap();
14747        config
14748            .load_priv_key_from_pem_file("examples/cert.key")
14749            .unwrap();
14750        config
14751            .set_application_protos(&[b"proto1", b"proto2"])
14752            .unwrap();
14753        config.set_initial_max_data(30);
14754        config.set_initial_max_stream_data_bidi_local(15);
14755        config.set_initial_max_stream_data_bidi_remote(15);
14756        config.set_initial_max_stream_data_uni(10);
14757        config.set_initial_max_streams_bidi(3);
14758        config.set_initial_max_streams_uni(3);
14759        config.enable_dgram(true, 2, 3);
14760        config.verify_peer(false);
14761
14762        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14763        assert_eq!(pipe.handshake(), Ok(()));
14764
14765        assert_eq!(pipe.client.dgram_send_queue_len(), 0);
14766        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 0);
14767
14768        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
14769        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
14770        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Ok(()));
14771        assert!(pipe.client.is_dgram_send_queue_full());
14772
14773        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 34);
14774
14775        pipe.client
14776            .dgram_purge_outgoing(|d: &[u8]| -> bool { d[0] == b'c' });
14777
14778        assert_eq!(pipe.client.dgram_send_queue_len(), 2);
14779        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 23);
14780        assert!(!pipe.client.is_dgram_send_queue_full());
14781
14782        // Before packets exchanged, no dgrams on server receive side.
14783        assert_eq!(pipe.server.dgram_recv_queue_len(), 0);
14784
14785        assert_eq!(pipe.advance(), Ok(()));
14786
14787        // After packets exchanged, no dgrams on client send side.
14788        assert_eq!(pipe.client.dgram_send_queue_len(), 0);
14789        assert_eq!(pipe.client.dgram_send_queue_byte_size(), 0);
14790
14791        assert_eq!(pipe.server.dgram_recv_queue_len(), 2);
14792        assert_eq!(pipe.server.dgram_recv_queue_byte_size(), 23);
14793        assert!(pipe.server.is_dgram_recv_queue_full());
14794
14795        let result1 = pipe.server.dgram_recv(&mut buf);
14796        assert_eq!(result1, Ok(12));
14797        assert_eq!(buf[0], b'h');
14798        assert_eq!(buf[1], b'e');
14799        assert!(!pipe.server.is_dgram_recv_queue_full());
14800
14801        let result2 = pipe.server.dgram_recv(&mut buf);
14802        assert_eq!(result2, Ok(11));
14803        assert_eq!(buf[0], b'h');
14804        assert_eq!(buf[1], b'o');
14805
14806        let result3 = pipe.server.dgram_recv(&mut buf);
14807        assert_eq!(result3, Err(Error::Done));
14808
14809        assert_eq!(pipe.server.dgram_recv_queue_len(), 0);
14810        assert_eq!(pipe.server.dgram_recv_queue_byte_size(), 0);
14811    }
14812
14813    #[test]
14814    fn dgram_send_queue_overflow() {
14815        let mut buf = [0; 65535];
14816
14817        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14818        config
14819            .load_cert_chain_from_pem_file("examples/cert.crt")
14820            .unwrap();
14821        config
14822            .load_priv_key_from_pem_file("examples/cert.key")
14823            .unwrap();
14824        config
14825            .set_application_protos(&[b"proto1", b"proto2"])
14826            .unwrap();
14827        config.set_initial_max_data(30);
14828        config.set_initial_max_stream_data_bidi_local(15);
14829        config.set_initial_max_stream_data_bidi_remote(15);
14830        config.set_initial_max_stream_data_uni(10);
14831        config.set_initial_max_streams_bidi(3);
14832        config.set_initial_max_streams_uni(3);
14833        config.enable_dgram(true, 10, 2);
14834        config.verify_peer(false);
14835
14836        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14837        assert_eq!(pipe.handshake(), Ok(()));
14838
14839        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
14840        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
14841        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Err(Error::Done));
14842
14843        assert_eq!(pipe.advance(), Ok(()));
14844
14845        let result1 = pipe.server.dgram_recv(&mut buf);
14846        assert_eq!(result1, Ok(12));
14847        assert_eq!(buf[0], b'h');
14848        assert_eq!(buf[1], b'e');
14849
14850        let result2 = pipe.server.dgram_recv(&mut buf);
14851        assert_eq!(result2, Ok(11));
14852        assert_eq!(buf[0], b'c');
14853        assert_eq!(buf[1], b'i');
14854
14855        let result3 = pipe.server.dgram_recv(&mut buf);
14856        assert_eq!(result3, Err(Error::Done));
14857    }
14858
14859    #[test]
14860    fn dgram_recv_queue_overflow() {
14861        let mut buf = [0; 65535];
14862
14863        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14864        config
14865            .load_cert_chain_from_pem_file("examples/cert.crt")
14866            .unwrap();
14867        config
14868            .load_priv_key_from_pem_file("examples/cert.key")
14869            .unwrap();
14870        config
14871            .set_application_protos(&[b"proto1", b"proto2"])
14872            .unwrap();
14873        config.set_initial_max_data(30);
14874        config.set_initial_max_stream_data_bidi_local(15);
14875        config.set_initial_max_stream_data_bidi_remote(15);
14876        config.set_initial_max_stream_data_uni(10);
14877        config.set_initial_max_streams_bidi(3);
14878        config.set_initial_max_streams_uni(3);
14879        config.enable_dgram(true, 2, 10);
14880        config.set_max_recv_udp_payload_size(1200);
14881        config.verify_peer(false);
14882
14883        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14884        assert_eq!(pipe.handshake(), Ok(()));
14885
14886        assert_eq!(pipe.client.dgram_send(b"hello, world"), Ok(()));
14887        assert_eq!(pipe.client.dgram_send(b"ciao, mondo"), Ok(()));
14888        assert_eq!(pipe.client.dgram_send(b"hola, mundo"), Ok(()));
14889
14890        assert_eq!(pipe.advance(), Ok(()));
14891
14892        let result1 = pipe.server.dgram_recv(&mut buf);
14893        assert_eq!(result1, Ok(11));
14894        assert_eq!(buf[0], b'c');
14895        assert_eq!(buf[1], b'i');
14896
14897        let result2 = pipe.server.dgram_recv(&mut buf);
14898        assert_eq!(result2, Ok(11));
14899        assert_eq!(buf[0], b'h');
14900        assert_eq!(buf[1], b'o');
14901
14902        let result3 = pipe.server.dgram_recv(&mut buf);
14903        assert_eq!(result3, Err(Error::Done));
14904    }
14905
14906    #[test]
14907    fn dgram_send_max_size() {
14908        let mut buf = [0; MAX_DGRAM_FRAME_SIZE as usize];
14909
14910        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14911        config
14912            .load_cert_chain_from_pem_file("examples/cert.crt")
14913            .unwrap();
14914        config
14915            .load_priv_key_from_pem_file("examples/cert.key")
14916            .unwrap();
14917        config
14918            .set_application_protos(&[b"proto1", b"proto2"])
14919            .unwrap();
14920        config.set_initial_max_data(30);
14921        config.set_initial_max_stream_data_bidi_local(15);
14922        config.set_initial_max_stream_data_bidi_remote(15);
14923        config.set_initial_max_stream_data_uni(10);
14924        config.set_initial_max_streams_bidi(3);
14925        config.set_initial_max_streams_uni(3);
14926        config.enable_dgram(true, 10, 10);
14927        config.set_max_recv_udp_payload_size(1452);
14928        config.verify_peer(false);
14929
14930        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14931
14932        // Before handshake (before peer settings) we don't know max dgram size
14933        assert_eq!(pipe.client.dgram_max_writable_len(), None);
14934
14935        assert_eq!(pipe.handshake(), Ok(()));
14936
14937        let max_dgram_size = pipe.client.dgram_max_writable_len().unwrap();
14938
14939        // Tests use a 16-byte connection ID, so the max datagram frame payload
14940        // size is (1200 byte-long packet - 40 bytes overhead)
14941        assert_eq!(max_dgram_size, 1160);
14942
14943        let dgram_packet: Vec<u8> = vec![42; max_dgram_size];
14944
14945        assert_eq!(pipe.client.dgram_send(&dgram_packet), Ok(()));
14946
14947        assert_eq!(pipe.advance(), Ok(()));
14948
14949        let result1 = pipe.server.dgram_recv(&mut buf);
14950        assert_eq!(result1, Ok(max_dgram_size));
14951
14952        let result2 = pipe.server.dgram_recv(&mut buf);
14953        assert_eq!(result2, Err(Error::Done));
14954    }
14955
14956    #[test]
14957    /// Tests is_readable check.
14958    fn is_readable() {
14959        let mut buf = [0; 65535];
14960
14961        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
14962        config
14963            .load_cert_chain_from_pem_file("examples/cert.crt")
14964            .unwrap();
14965        config
14966            .load_priv_key_from_pem_file("examples/cert.key")
14967            .unwrap();
14968        config
14969            .set_application_protos(&[b"proto1", b"proto2"])
14970            .unwrap();
14971        config.set_initial_max_data(30);
14972        config.set_initial_max_stream_data_bidi_local(15);
14973        config.set_initial_max_stream_data_bidi_remote(15);
14974        config.set_initial_max_stream_data_uni(10);
14975        config.set_initial_max_streams_bidi(3);
14976        config.set_initial_max_streams_uni(3);
14977        config.enable_dgram(true, 10, 10);
14978        config.set_max_recv_udp_payload_size(1452);
14979        config.verify_peer(false);
14980
14981        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
14982        assert_eq!(pipe.handshake(), Ok(()));
14983
14984        // No readable data.
14985        assert!(!pipe.client.is_readable());
14986        assert!(!pipe.server.is_readable());
14987
14988        assert_eq!(pipe.client.stream_send(4, b"aaaaa", false), Ok(5));
14989        assert_eq!(pipe.advance(), Ok(()));
14990
14991        // Server received stream.
14992        assert!(!pipe.client.is_readable());
14993        assert!(pipe.server.is_readable());
14994
14995        assert_eq!(
14996            pipe.server.stream_send(4, b"aaaaaaaaaaaaaaa", false),
14997            Ok(15)
14998        );
14999        assert_eq!(pipe.advance(), Ok(()));
15000
15001        // Client received stream.
15002        assert!(pipe.client.is_readable());
15003        assert!(pipe.server.is_readable());
15004
15005        // Client drains stream.
15006        let mut b = [0; 15];
15007        pipe.client.stream_recv(4, &mut b).unwrap();
15008        assert_eq!(pipe.advance(), Ok(()));
15009
15010        assert!(!pipe.client.is_readable());
15011        assert!(pipe.server.is_readable());
15012
15013        // Server shuts down stream.
15014        assert_eq!(pipe.server.stream_shutdown(4, Shutdown::Read, 0), Ok(()));
15015        assert!(!pipe.server.is_readable());
15016
15017        // Server received dgram.
15018        assert_eq!(pipe.client.dgram_send(b"dddddddddddddd"), Ok(()));
15019        assert_eq!(pipe.advance(), Ok(()));
15020
15021        assert!(!pipe.client.is_readable());
15022        assert!(pipe.server.is_readable());
15023
15024        // Client received dgram.
15025        assert_eq!(pipe.server.dgram_send(b"dddddddddddddd"), Ok(()));
15026        assert_eq!(pipe.advance(), Ok(()));
15027
15028        assert!(pipe.client.is_readable());
15029        assert!(pipe.server.is_readable());
15030
15031        // Drain the dgram queues.
15032        let r = pipe.server.dgram_recv(&mut buf);
15033        assert_eq!(r, Ok(14));
15034        assert!(!pipe.server.is_readable());
15035
15036        let r = pipe.client.dgram_recv(&mut buf);
15037        assert_eq!(r, Ok(14));
15038        assert!(!pipe.client.is_readable());
15039    }
15040
15041    #[test]
15042    fn close() {
15043        let mut buf = [0; 65535];
15044
15045        let mut pipe = testing::Pipe::new().unwrap();
15046        assert_eq!(pipe.handshake(), Ok(()));
15047
15048        assert_eq!(pipe.client.close(false, 0x1234, b"hello?"), Ok(()));
15049
15050        assert_eq!(
15051            pipe.client.close(false, 0x4321, b"hello?"),
15052            Err(Error::Done)
15053        );
15054
15055        let (len, _) = pipe.client.send(&mut buf).unwrap();
15056
15057        let frames =
15058            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
15059
15060        assert_eq!(
15061            frames.first(),
15062            Some(&frame::Frame::ConnectionClose {
15063                error_code: 0x1234,
15064                frame_type: 0,
15065                reason: b"hello?".to_vec(),
15066            })
15067        );
15068    }
15069
15070    #[test]
15071    fn app_close_by_client() {
15072        let mut buf = [0; 65535];
15073
15074        let mut pipe = testing::Pipe::new().unwrap();
15075        assert_eq!(pipe.handshake(), Ok(()));
15076
15077        assert_eq!(pipe.client.close(true, 0x1234, b"hello!"), Ok(()));
15078
15079        assert_eq!(pipe.client.close(true, 0x4321, b"hello!"), Err(Error::Done));
15080
15081        let (len, _) = pipe.client.send(&mut buf).unwrap();
15082
15083        let frames =
15084            testing::decode_pkt(&mut pipe.server, &mut buf[..len]).unwrap();
15085
15086        assert_eq!(
15087            frames.first(),
15088            Some(&frame::Frame::ApplicationClose {
15089                error_code: 0x1234,
15090                reason: b"hello!".to_vec(),
15091            })
15092        );
15093    }
15094
15095    // OpenSSL does not provide a straightforward interface to deal with custom
15096    // off-load key signing.
15097    #[cfg(not(feature = "openssl"))]
15098    #[test]
15099    fn app_close_by_server_during_handshake_private_key_failure() {
15100        let mut pipe = testing::Pipe::new().unwrap();
15101        pipe.server.handshake.set_failing_private_key_method();
15102
15103        // Client sends initial flight.
15104        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15105        assert_eq!(
15106            testing::process_flight(&mut pipe.server, flight),
15107            Err(Error::TlsFail)
15108        );
15109
15110        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15111
15112        // Both connections are not established.
15113        assert!(!pipe.server.is_established());
15114        assert!(!pipe.client.is_established());
15115
15116        // Connection should already be closed due the failure during key signing.
15117        assert_eq!(
15118            pipe.server.close(true, 123, b"fail whale"),
15119            Err(Error::Done)
15120        );
15121
15122        testing::process_flight(&mut pipe.client, flight).unwrap();
15123
15124        // Connection should already be closed due the failure during key signing.
15125        assert_eq!(
15126            pipe.client.close(true, 123, b"fail whale"),
15127            Err(Error::Done)
15128        );
15129
15130        // Connection is not established on the server / client (and never
15131        // will be)
15132        assert!(!pipe.server.is_established());
15133        assert!(!pipe.client.is_established());
15134
15135        assert_eq!(pipe.advance(), Ok(()));
15136
15137        assert_eq!(
15138            pipe.server.local_error(),
15139            Some(&ConnectionError {
15140                is_app: false,
15141                error_code: 0x01,
15142                reason: vec![],
15143            })
15144        );
15145        assert_eq!(
15146            pipe.client.peer_error(),
15147            Some(&ConnectionError {
15148                is_app: false,
15149                error_code: 0x01,
15150                reason: vec![],
15151            })
15152        );
15153    }
15154
15155    #[test]
15156    fn app_close_by_server_during_handshake_not_established() {
15157        let mut pipe = testing::Pipe::new().unwrap();
15158
15159        // Client sends initial flight.
15160        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15161        testing::process_flight(&mut pipe.server, flight).unwrap();
15162
15163        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15164
15165        // Both connections are not established.
15166        assert!(!pipe.client.is_established() && !pipe.server.is_established());
15167
15168        // Server closes before connection is established.
15169        pipe.server.close(true, 123, b"fail whale").unwrap();
15170
15171        testing::process_flight(&mut pipe.client, flight).unwrap();
15172
15173        // Connection is established on the client.
15174        assert!(pipe.client.is_established());
15175
15176        // Client sends after connection is established.
15177        pipe.client.stream_send(0, b"badauthtoken", true).unwrap();
15178
15179        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15180        testing::process_flight(&mut pipe.server, flight).unwrap();
15181
15182        // Connection is not established on the server (and never will be)
15183        assert!(!pipe.server.is_established());
15184
15185        assert_eq!(pipe.advance(), Ok(()));
15186
15187        assert_eq!(
15188            pipe.server.local_error(),
15189            Some(&ConnectionError {
15190                is_app: false,
15191                error_code: 0x0c,
15192                reason: vec![],
15193            })
15194        );
15195        assert_eq!(
15196            pipe.client.peer_error(),
15197            Some(&ConnectionError {
15198                is_app: false,
15199                error_code: 0x0c,
15200                reason: vec![],
15201            })
15202        );
15203    }
15204
15205    #[test]
15206    fn app_close_by_server_during_handshake_established() {
15207        let mut pipe = testing::Pipe::new().unwrap();
15208
15209        // Client sends initial flight.
15210        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15211        testing::process_flight(&mut pipe.server, flight).unwrap();
15212
15213        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15214
15215        // Both connections are not established.
15216        assert!(!pipe.client.is_established() && !pipe.server.is_established());
15217
15218        testing::process_flight(&mut pipe.client, flight).unwrap();
15219
15220        // Connection is established on the client.
15221        assert!(pipe.client.is_established());
15222
15223        // Client sends after connection is established.
15224        pipe.client.stream_send(0, b"badauthtoken", true).unwrap();
15225
15226        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15227        testing::process_flight(&mut pipe.server, flight).unwrap();
15228
15229        // Connection is established on the server but the Handshake ACK has not
15230        // been sent yet.
15231        assert!(pipe.server.is_established());
15232
15233        // Server closes after connection is established.
15234        pipe.server
15235            .close(true, 123, b"Invalid authentication")
15236            .unwrap();
15237
15238        // Server sends Handshake ACK and then 1RTT CONNECTION_CLOSE.
15239        assert_eq!(pipe.advance(), Ok(()));
15240
15241        assert_eq!(
15242            pipe.server.local_error(),
15243            Some(&ConnectionError {
15244                is_app: true,
15245                error_code: 123,
15246                reason: b"Invalid authentication".to_vec()
15247            })
15248        );
15249        assert_eq!(
15250            pipe.client.peer_error(),
15251            Some(&ConnectionError {
15252                is_app: true,
15253                error_code: 123,
15254                reason: b"Invalid authentication".to_vec()
15255            })
15256        );
15257    }
15258
15259    #[test]
15260    fn transport_close_by_client_during_handshake_established() {
15261        let mut pipe = testing::Pipe::new().unwrap();
15262
15263        // Client sends initial flight.
15264        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15265        testing::process_flight(&mut pipe.server, flight).unwrap();
15266
15267        let flight = testing::emit_flight(&mut pipe.server).unwrap();
15268
15269        // Both connections are not established.
15270        assert!(!pipe.client.is_established() && !pipe.server.is_established());
15271
15272        testing::process_flight(&mut pipe.client, flight).unwrap();
15273
15274        // Connection is established on the client.
15275        assert!(pipe.client.is_established());
15276
15277        // Client sends after connection is established.
15278        pipe.client.close(false, 123, b"connection close").unwrap();
15279
15280        let flight = testing::emit_flight(&mut pipe.client).unwrap();
15281        testing::process_flight(&mut pipe.server, flight).unwrap();
15282
15283        assert_eq!(
15284            pipe.server.peer_error(),
15285            Some(&ConnectionError {
15286                is_app: false,
15287                error_code: 123,
15288                reason: b"connection close".to_vec()
15289            })
15290        );
15291        assert_eq!(
15292            pipe.client.local_error(),
15293            Some(&ConnectionError {
15294                is_app: false,
15295                error_code: 123,
15296                reason: b"connection close".to_vec()
15297            })
15298        );
15299    }
15300
15301    #[test]
15302    fn peer_error() {
15303        let mut pipe = testing::Pipe::new().unwrap();
15304        assert_eq!(pipe.handshake(), Ok(()));
15305
15306        assert_eq!(pipe.server.close(false, 0x1234, b"hello?"), Ok(()));
15307        assert_eq!(pipe.advance(), Ok(()));
15308
15309        assert_eq!(
15310            pipe.client.peer_error(),
15311            Some(&ConnectionError {
15312                is_app: false,
15313                error_code: 0x1234u64,
15314                reason: b"hello?".to_vec()
15315            })
15316        );
15317    }
15318
15319    #[test]
15320    fn app_peer_error() {
15321        let mut pipe = testing::Pipe::new().unwrap();
15322        assert_eq!(pipe.handshake(), Ok(()));
15323
15324        assert_eq!(pipe.server.close(true, 0x1234, b"hello!"), Ok(()));
15325        assert_eq!(pipe.advance(), Ok(()));
15326
15327        assert_eq!(
15328            pipe.client.peer_error(),
15329            Some(&ConnectionError {
15330                is_app: true,
15331                error_code: 0x1234u64,
15332                reason: b"hello!".to_vec()
15333            })
15334        );
15335    }
15336
15337    #[test]
15338    fn local_error() {
15339        let mut pipe = testing::Pipe::new().unwrap();
15340        assert_eq!(pipe.handshake(), Ok(()));
15341
15342        assert_eq!(pipe.server.local_error(), None);
15343
15344        assert_eq!(pipe.server.close(true, 0x1234, b"hello!"), Ok(()));
15345
15346        assert_eq!(
15347            pipe.server.local_error(),
15348            Some(&ConnectionError {
15349                is_app: true,
15350                error_code: 0x1234u64,
15351                reason: b"hello!".to_vec()
15352            })
15353        );
15354    }
15355
15356    #[test]
15357    fn update_max_datagram_size() {
15358        let mut client_scid = [0; 16];
15359        rand::rand_bytes(&mut client_scid[..]);
15360        let client_scid = ConnectionId::from_ref(&client_scid);
15361        let client_addr = "127.0.0.1:1234".parse().unwrap();
15362
15363        let mut server_scid = [0; 16];
15364        rand::rand_bytes(&mut server_scid[..]);
15365        let server_scid = ConnectionId::from_ref(&server_scid);
15366        let server_addr = "127.0.0.1:4321".parse().unwrap();
15367
15368        let mut client_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15369        client_config
15370            .set_application_protos(&[b"proto1", b"proto2"])
15371            .unwrap();
15372        client_config.set_max_recv_udp_payload_size(1200);
15373
15374        let mut server_config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15375        server_config
15376            .load_cert_chain_from_pem_file("examples/cert.crt")
15377            .unwrap();
15378        server_config
15379            .load_priv_key_from_pem_file("examples/cert.key")
15380            .unwrap();
15381        server_config
15382            .set_application_protos(&[b"proto1", b"proto2"])
15383            .unwrap();
15384        server_config.verify_peer(false);
15385        server_config
15386            .set_application_protos(&[b"proto1", b"proto2"])
15387            .unwrap();
15388        // Larger than the client
15389        server_config.set_max_send_udp_payload_size(1500);
15390
15391        let mut pipe = testing::Pipe {
15392            client: connect(
15393                Some("quic.tech"),
15394                &client_scid,
15395                client_addr,
15396                server_addr,
15397                &mut client_config,
15398            )
15399            .unwrap(),
15400            server: accept(
15401                &server_scid,
15402                None,
15403                server_addr,
15404                client_addr,
15405                &mut server_config,
15406            )
15407            .unwrap(),
15408        };
15409
15410        // Before handshake
15411        assert_eq!(
15412            pipe.server
15413                .paths
15414                .get_active()
15415                .expect("no active")
15416                .recovery
15417                .max_datagram_size(),
15418            1500,
15419        );
15420
15421        assert_eq!(pipe.handshake(), Ok(()));
15422
15423        // After handshake, max_datagram_size should match to client's
15424        // max_recv_udp_payload_size which is smaller
15425        assert_eq!(
15426            pipe.server
15427                .paths
15428                .get_active()
15429                .expect("no active")
15430                .recovery
15431                .max_datagram_size(),
15432            1200,
15433        );
15434        assert_eq!(
15435            pipe.server
15436                .paths
15437                .get_active()
15438                .expect("no active")
15439                .recovery
15440                .cwnd(),
15441            12000,
15442        );
15443    }
15444
15445    #[test]
15446    /// Tests that connection-level send capacity decreases as more stream data
15447    /// is buffered.
15448    fn send_capacity() {
15449        let mut buf = [0; 65535];
15450
15451        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15452        config
15453            .load_cert_chain_from_pem_file("examples/cert.crt")
15454            .unwrap();
15455        config
15456            .load_priv_key_from_pem_file("examples/cert.key")
15457            .unwrap();
15458        config
15459            .set_application_protos(&[b"proto1", b"proto2"])
15460            .unwrap();
15461        config.set_initial_max_data(100000);
15462        config.set_initial_max_stream_data_bidi_local(10000);
15463        config.set_initial_max_stream_data_bidi_remote(10000);
15464        config.set_initial_max_streams_bidi(10);
15465        config.verify_peer(false);
15466
15467        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15468        assert_eq!(pipe.handshake(), Ok(()));
15469
15470        assert_eq!(pipe.client.stream_send(0, b"hello!", true), Ok(6));
15471        assert_eq!(pipe.advance(), Ok(()));
15472
15473        assert_eq!(pipe.client.stream_send(4, b"hello!", true), Ok(6));
15474        assert_eq!(pipe.advance(), Ok(()));
15475
15476        assert_eq!(pipe.client.stream_send(8, b"hello!", true), Ok(6));
15477        assert_eq!(pipe.advance(), Ok(()));
15478
15479        assert_eq!(pipe.client.stream_send(12, b"hello!", true), Ok(6));
15480        assert_eq!(pipe.advance(), Ok(()));
15481
15482        let mut r = pipe.server.readable().collect::<Vec<u64>>();
15483        assert_eq!(r.len(), 4);
15484
15485        r.sort();
15486
15487        assert_eq!(r, [0, 4, 8, 12]);
15488
15489        assert_eq!(pipe.server.stream_recv(0, &mut buf), Ok((6, true)));
15490        assert_eq!(pipe.server.stream_recv(4, &mut buf), Ok((6, true)));
15491        assert_eq!(pipe.server.stream_recv(8, &mut buf), Ok((6, true)));
15492        assert_eq!(pipe.server.stream_recv(12, &mut buf), Ok((6, true)));
15493
15494        assert_eq!(pipe.server.tx_cap, 12000);
15495
15496        assert_eq!(pipe.server.stream_send(0, &buf[..5000], false), Ok(5000));
15497        assert_eq!(pipe.server.stream_send(4, &buf[..5000], false), Ok(5000));
15498        assert_eq!(pipe.server.stream_send(8, &buf[..5000], false), Ok(2000));
15499
15500        // No more connection send capacity.
15501        assert_eq!(
15502            pipe.server.stream_send(12, &buf[..5000], false),
15503            Err(Error::Done)
15504        );
15505        assert_eq!(pipe.server.tx_cap, 0);
15506
15507        assert_eq!(pipe.advance(), Ok(()));
15508    }
15509
15510    #[cfg(feature = "boringssl-boring-crate")]
15511    #[test]
15512    fn user_provided_boring_ctx() -> Result<()> {
15513        // Manually construct boring ssl ctx for server
15514        let mut server_tls_ctx_builder =
15515            boring::ssl::SslContextBuilder::new(boring::ssl::SslMethod::tls())
15516                .unwrap();
15517        server_tls_ctx_builder
15518            .set_certificate_chain_file("examples/cert.crt")
15519            .unwrap();
15520        server_tls_ctx_builder
15521            .set_private_key_file(
15522                "examples/cert.key",
15523                boring::ssl::SslFiletype::PEM,
15524            )
15525            .unwrap();
15526
15527        let mut server_config = Config::with_boring_ssl_ctx_builder(
15528            crate::PROTOCOL_VERSION,
15529            server_tls_ctx_builder,
15530        )?;
15531        let mut client_config = Config::new(crate::PROTOCOL_VERSION)?;
15532        client_config.load_cert_chain_from_pem_file("examples/cert.crt")?;
15533        client_config.load_priv_key_from_pem_file("examples/cert.key")?;
15534
15535        for config in [&mut client_config, &mut server_config] {
15536            config.set_application_protos(&[b"proto1", b"proto2"])?;
15537            config.set_initial_max_data(30);
15538            config.set_initial_max_stream_data_bidi_local(15);
15539            config.set_initial_max_stream_data_bidi_remote(15);
15540            config.set_initial_max_stream_data_uni(10);
15541            config.set_initial_max_streams_bidi(3);
15542            config.set_initial_max_streams_uni(3);
15543            config.set_max_idle_timeout(180_000);
15544            config.verify_peer(false);
15545            config.set_ack_delay_exponent(8);
15546        }
15547
15548        let mut client_scid = [0; 16];
15549        rand::rand_bytes(&mut client_scid[..]);
15550        let client_scid = ConnectionId::from_ref(&client_scid);
15551        let client_addr = "127.0.0.1:1234".parse().unwrap();
15552
15553        let mut server_scid = [0; 16];
15554        rand::rand_bytes(&mut server_scid[..]);
15555        let server_scid = ConnectionId::from_ref(&server_scid);
15556        let server_addr = "127.0.0.1:4321".parse().unwrap();
15557
15558        let mut pipe = testing::Pipe {
15559            client: connect(
15560                Some("quic.tech"),
15561                &client_scid,
15562                client_addr,
15563                server_addr,
15564                &mut client_config,
15565            )?,
15566            server: accept(
15567                &server_scid,
15568                None,
15569                server_addr,
15570                client_addr,
15571                &mut server_config,
15572            )?,
15573        };
15574
15575        assert_eq!(pipe.handshake(), Ok(()));
15576
15577        Ok(())
15578    }
15579
15580    #[test]
15581    /// Tests that resetting a stream restores flow control for unsent data.
15582    fn last_tx_data_larger_than_tx_data() {
15583        let mut config = Config::new(PROTOCOL_VERSION).unwrap();
15584        config
15585            .set_application_protos(&[b"proto1", b"proto2"])
15586            .unwrap();
15587        config.set_initial_max_data(12000);
15588        config.set_initial_max_stream_data_bidi_local(20000);
15589        config.set_initial_max_stream_data_bidi_remote(20000);
15590        config.set_max_recv_udp_payload_size(1200);
15591        config.verify_peer(false);
15592
15593        let mut pipe = testing::Pipe::with_client_config(&mut config).unwrap();
15594        assert_eq!(pipe.handshake(), Ok(()));
15595
15596        // Client opens stream 4 and 8.
15597        assert_eq!(pipe.client.stream_send(4, b"a", true), Ok(1));
15598        assert_eq!(pipe.client.stream_send(8, b"b", true), Ok(1));
15599        assert_eq!(pipe.advance(), Ok(()));
15600
15601        // Server reads stream data.
15602        let mut b = [0; 15];
15603        pipe.server.stream_recv(4, &mut b).unwrap();
15604
15605        // Server sends stream data close to cwnd (12000).
15606        let buf = [0; 10000];
15607        assert_eq!(pipe.server.stream_send(4, &buf, false), Ok(10000));
15608
15609        testing::emit_flight(&mut pipe.server).unwrap();
15610
15611        // Server buffers some data, until send capacity limit reached.
15612        let mut buf = [0; 1200];
15613        assert_eq!(pipe.server.stream_send(4, &buf, false), Ok(1200));
15614        assert_eq!(pipe.server.stream_send(8, &buf, false), Ok(800));
15615        assert_eq!(pipe.server.stream_send(4, &buf, false), Err(Error::Done));
15616
15617        // Wait for PTO to expire.
15618        let timer = pipe.server.timeout().unwrap();
15619        std::thread::sleep(timer + time::Duration::from_millis(1));
15620
15621        pipe.server.on_timeout();
15622
15623        // Server sends PTO probe (not limited to cwnd),
15624        // to update last_tx_data.
15625        let (len, _) = pipe.server.send(&mut buf).unwrap();
15626        assert_eq!(len, 1200);
15627
15628        // Client sends STOP_SENDING to decrease tx_data
15629        // by unsent data. It will make last_tx_data > tx_data
15630        // and trigger #1232 bug.
15631        let frames = [frame::Frame::StopSending {
15632            stream_id: 4,
15633            error_code: 42,
15634        }];
15635
15636        let pkt_type = packet::Type::Short;
15637        pipe.send_pkt_to_server(pkt_type, &frames, &mut buf)
15638            .unwrap();
15639    }
15640
15641    /// Tests that when the client provides a new ConnectionId, it eventually
15642    /// reaches the server and notifies the application.
15643    #[test]
15644    fn send_connection_ids() {
15645        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15646        config
15647            .load_cert_chain_from_pem_file("examples/cert.crt")
15648            .unwrap();
15649        config
15650            .load_priv_key_from_pem_file("examples/cert.key")
15651            .unwrap();
15652        config
15653            .set_application_protos(&[b"proto1", b"proto2"])
15654            .unwrap();
15655        config.verify_peer(false);
15656        config.set_active_connection_id_limit(3);
15657
15658        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15659        assert_eq!(pipe.handshake(), Ok(()));
15660
15661        // So far, there should not have any QUIC event.
15662        assert_eq!(pipe.client.path_event_next(), None);
15663        assert_eq!(pipe.server.path_event_next(), None);
15664        assert_eq!(pipe.client.scids_left(), 2);
15665
15666        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
15667        assert_eq!(pipe.client.new_scid(&scid, reset_token, false), Ok(1));
15668
15669        // Let exchange packets over the connection.
15670        assert_eq!(pipe.advance(), Ok(()));
15671
15672        // At this point, the server should be notified that it has a new CID.
15673        assert_eq!(pipe.server.available_dcids(), 1);
15674        assert_eq!(pipe.server.path_event_next(), None);
15675        assert_eq!(pipe.client.path_event_next(), None);
15676        assert_eq!(pipe.client.scids_left(), 1);
15677
15678        // Now, a second CID can be provided.
15679        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
15680        assert_eq!(pipe.client.new_scid(&scid, reset_token, false), Ok(2));
15681
15682        // Let exchange packets over the connection.
15683        assert_eq!(pipe.advance(), Ok(()));
15684
15685        // At this point, the server should be notified that it has a new CID.
15686        assert_eq!(pipe.server.available_dcids(), 2);
15687        assert_eq!(pipe.server.path_event_next(), None);
15688        assert_eq!(pipe.client.path_event_next(), None);
15689        assert_eq!(pipe.client.scids_left(), 0);
15690
15691        // If now the client tries to send another CID, it reports an error
15692        // since it exceeds the limit of active CIDs.
15693        let (scid, reset_token) = testing::create_cid_and_reset_token(16);
15694        assert_eq!(
15695            pipe.client.new_scid(&scid, reset_token, false),
15696            Err(Error::IdLimit),
15697        );
15698    }
15699
15700    #[test]
15701    /// Tests that NEW_CONNECTION_ID with zero-length CID are rejected.
15702    fn connection_id_zero() {
15703        let mut buf = [0; 65535];
15704
15705        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15706        config
15707            .load_cert_chain_from_pem_file("examples/cert.crt")
15708            .unwrap();
15709        config
15710            .load_priv_key_from_pem_file("examples/cert.key")
15711            .unwrap();
15712        config
15713            .set_application_protos(&[b"proto1", b"proto2"])
15714            .unwrap();
15715        config.verify_peer(false);
15716        config.set_active_connection_id_limit(2);
15717
15718        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15719        assert_eq!(pipe.handshake(), Ok(()));
15720
15721        let mut frames = Vec::new();
15722
15723        // Client adds a CID that is too short.
15724        let (scid, reset_token) = testing::create_cid_and_reset_token(0);
15725
15726        frames.push(frame::Frame::NewConnectionId {
15727            seq_num: 1,
15728            retire_prior_to: 0,
15729            conn_id: scid.to_vec(),
15730            reset_token: reset_token.to_be_bytes(),
15731        });
15732
15733        let pkt_type = packet::Type::Short;
15734
15735        let written =
15736            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
15737                .unwrap();
15738
15739        let active_path = pipe.server.paths.get_active().unwrap();
15740        let info = RecvInfo {
15741            to: active_path.local_addr(),
15742            from: active_path.peer_addr(),
15743        };
15744
15745        assert_eq!(
15746            pipe.server.recv(&mut buf[..written], info),
15747            Err(Error::InvalidFrame)
15748        );
15749
15750        let written = match pipe.server.send(&mut buf) {
15751            Ok((write, _)) => write,
15752
15753            Err(_) => unreachable!(),
15754        };
15755
15756        let frames =
15757            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
15758        let mut iter = frames.iter();
15759
15760        assert_eq!(
15761            iter.next(),
15762            Some(&frame::Frame::ConnectionClose {
15763                error_code: 0x7,
15764                frame_type: 0,
15765                reason: Vec::new(),
15766            })
15767        );
15768    }
15769
15770    #[test]
15771    /// Tests that NEW_CONNECTION_ID with too long CID are rejected.
15772    fn connection_id_invalid_max_len() {
15773        let mut buf = [0; 65535];
15774
15775        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15776        config
15777            .load_cert_chain_from_pem_file("examples/cert.crt")
15778            .unwrap();
15779        config
15780            .load_priv_key_from_pem_file("examples/cert.key")
15781            .unwrap();
15782        config
15783            .set_application_protos(&[b"proto1", b"proto2"])
15784            .unwrap();
15785        config.verify_peer(false);
15786        config.set_active_connection_id_limit(2);
15787
15788        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15789        assert_eq!(pipe.handshake(), Ok(()));
15790
15791        let mut frames = Vec::new();
15792
15793        // Client adds a CID that is too long.
15794        let (scid, reset_token) =
15795            testing::create_cid_and_reset_token(MAX_CONN_ID_LEN + 1);
15796
15797        frames.push(frame::Frame::NewConnectionId {
15798            seq_num: 1,
15799            retire_prior_to: 0,
15800            conn_id: scid.to_vec(),
15801            reset_token: reset_token.to_be_bytes(),
15802        });
15803
15804        let pkt_type = packet::Type::Short;
15805
15806        let written =
15807            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
15808                .unwrap();
15809
15810        let active_path = pipe.server.paths.get_active().unwrap();
15811        let info = RecvInfo {
15812            to: active_path.local_addr(),
15813            from: active_path.peer_addr(),
15814        };
15815
15816        assert_eq!(
15817            pipe.server.recv(&mut buf[..written], info),
15818            Err(Error::InvalidFrame)
15819        );
15820
15821        let written = match pipe.server.send(&mut buf) {
15822            Ok((write, _)) => write,
15823
15824            Err(_) => unreachable!(),
15825        };
15826
15827        let frames =
15828            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
15829        let mut iter = frames.iter();
15830
15831        assert_eq!(
15832            iter.next(),
15833            Some(&frame::Frame::ConnectionClose {
15834                error_code: 0x7,
15835                frame_type: 0,
15836                reason: Vec::new(),
15837            })
15838        );
15839    }
15840
15841    #[test]
15842    /// Exercises the handling of NEW_CONNECTION_ID and RETIRE_CONNECTION_ID
15843    /// frames.
15844    fn connection_id_handling() {
15845        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15846        config
15847            .load_cert_chain_from_pem_file("examples/cert.crt")
15848            .unwrap();
15849        config
15850            .load_priv_key_from_pem_file("examples/cert.key")
15851            .unwrap();
15852        config
15853            .set_application_protos(&[b"proto1", b"proto2"])
15854            .unwrap();
15855        config.verify_peer(false);
15856        config.set_active_connection_id_limit(2);
15857
15858        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15859        assert_eq!(pipe.handshake(), Ok(()));
15860
15861        // So far, there should not have any QUIC event.
15862        assert_eq!(pipe.client.path_event_next(), None);
15863        assert_eq!(pipe.server.path_event_next(), None);
15864        assert_eq!(pipe.client.scids_left(), 1);
15865
15866        let scid = pipe.client.source_id().into_owned();
15867
15868        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
15869        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
15870
15871        // Let exchange packets over the connection.
15872        assert_eq!(pipe.advance(), Ok(()));
15873
15874        // At this point, the server should be notified that it has a new CID.
15875        assert_eq!(pipe.server.available_dcids(), 1);
15876        assert_eq!(pipe.server.path_event_next(), None);
15877        assert_eq!(pipe.client.path_event_next(), None);
15878        assert_eq!(pipe.client.scids_left(), 0);
15879
15880        // Now we assume that the client wants to advertise more source
15881        // Connection IDs than the advertised limit. This is valid if it
15882        // requests its peer to retire enough Connection IDs to fit within the
15883        // limits.
15884
15885        let (scid_2, reset_token_2) = testing::create_cid_and_reset_token(16);
15886        assert_eq!(pipe.client.new_scid(&scid_2, reset_token_2, true), Ok(2));
15887
15888        // Let exchange packets over the connection.
15889        assert_eq!(pipe.advance(), Ok(()));
15890
15891        // At this point, the server still have a spare DCID.
15892        assert_eq!(pipe.server.available_dcids(), 1);
15893        assert_eq!(pipe.server.path_event_next(), None);
15894
15895        // Client should have received a retired notification.
15896        assert_eq!(pipe.client.retired_scid_next(), Some(scid));
15897        assert_eq!(pipe.client.retired_scid_next(), None);
15898
15899        assert_eq!(pipe.client.path_event_next(), None);
15900        assert_eq!(pipe.client.scids_left(), 0);
15901
15902        // The active Destination Connection ID of the server should now be the
15903        // one with sequence number 1.
15904        assert_eq!(pipe.server.destination_id(), scid_1);
15905
15906        // Now tries to experience CID retirement. If the server tries to remove
15907        // non-existing DCIDs, it fails.
15908        assert_eq!(pipe.server.retire_dcid(0), Err(Error::InvalidState));
15909        assert_eq!(pipe.server.retire_dcid(3), Err(Error::InvalidState));
15910
15911        // Now it removes DCID with sequence 1.
15912        assert_eq!(pipe.server.retire_dcid(1), Ok(()));
15913
15914        // Let exchange packets over the connection.
15915        assert_eq!(pipe.advance(), Ok(()));
15916
15917        assert_eq!(pipe.server.path_event_next(), None);
15918        assert_eq!(pipe.client.retired_scid_next(), Some(scid_1));
15919        assert_eq!(pipe.client.retired_scid_next(), None);
15920
15921        assert_eq!(pipe.server.destination_id(), scid_2);
15922        assert_eq!(pipe.server.available_dcids(), 0);
15923
15924        // Trying to remove the last DCID triggers an error.
15925        assert_eq!(pipe.server.retire_dcid(2), Err(Error::OutOfIdentifiers));
15926    }
15927
15928    #[test]
15929    fn lost_connection_id_frames() {
15930        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15931        config
15932            .load_cert_chain_from_pem_file("examples/cert.crt")
15933            .unwrap();
15934        config
15935            .load_priv_key_from_pem_file("examples/cert.key")
15936            .unwrap();
15937        config
15938            .set_application_protos(&[b"proto1", b"proto2"])
15939            .unwrap();
15940        config.verify_peer(false);
15941        config.set_active_connection_id_limit(2);
15942
15943        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
15944        assert_eq!(pipe.handshake(), Ok(()));
15945
15946        let scid = pipe.client.source_id().into_owned();
15947
15948        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
15949        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
15950
15951        // Packets are sent, but never received.
15952        testing::emit_flight(&mut pipe.client).unwrap();
15953
15954        // Wait until timer expires. Since the RTT is very low, wait a bit more.
15955        let timer = pipe.client.timeout().unwrap();
15956        std::thread::sleep(timer + time::Duration::from_millis(1));
15957
15958        pipe.client.on_timeout();
15959
15960        // Let exchange packets over the connection.
15961        assert_eq!(pipe.advance(), Ok(()));
15962
15963        // At this point, the server should be notified that it has a new CID.
15964        assert_eq!(pipe.server.available_dcids(), 1);
15965
15966        // Now the server retires the first Destination CID.
15967        assert_eq!(pipe.server.retire_dcid(0), Ok(()));
15968
15969        // But the packet never reaches the client.
15970        testing::emit_flight(&mut pipe.server).unwrap();
15971
15972        // Wait until timer expires. Since the RTT is very low, wait a bit more.
15973        let timer = pipe.server.timeout().unwrap();
15974        std::thread::sleep(timer + time::Duration::from_millis(1));
15975
15976        pipe.server.on_timeout();
15977
15978        // Let exchange packets over the connection.
15979        assert_eq!(pipe.advance(), Ok(()));
15980
15981        assert_eq!(pipe.client.retired_scid_next(), Some(scid));
15982        assert_eq!(pipe.client.retired_scid_next(), None);
15983    }
15984
15985    #[test]
15986    fn sending_duplicate_scids() {
15987        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
15988        config
15989            .load_cert_chain_from_pem_file("examples/cert.crt")
15990            .unwrap();
15991        config
15992            .load_priv_key_from_pem_file("examples/cert.key")
15993            .unwrap();
15994        config
15995            .set_application_protos(&[b"proto1", b"proto2"])
15996            .unwrap();
15997        config.verify_peer(false);
15998        config.set_active_connection_id_limit(3);
15999
16000        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16001        assert_eq!(pipe.handshake(), Ok(()));
16002
16003        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
16004        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
16005        assert_eq!(pipe.advance(), Ok(()));
16006
16007        // Trying to send the same CID with a different reset token raises an
16008        // InvalidState error.
16009        let reset_token_2 = reset_token_1.wrapping_add(1);
16010        assert_eq!(
16011            pipe.client.new_scid(&scid_1, reset_token_2, false),
16012            Err(Error::InvalidState),
16013        );
16014
16015        // Retrying to send the exact same CID with the same token returns the
16016        // previously assigned CID seq, but without sending anything.
16017        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
16018        assert!(!pipe.client.ids.has_new_scids());
16019
16020        // Now retire this new CID.
16021        assert_eq!(pipe.server.retire_dcid(1), Ok(()));
16022        assert_eq!(pipe.advance(), Ok(()));
16023
16024        // It is up to the application to ensure that a given SCID is not reused
16025        // later.
16026        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(2));
16027    }
16028
16029    #[test]
16030    /// Tests the limit to retired DCID sequence numbers.
16031    fn connection_id_retire_limit() {
16032        let mut buf = [0; 65535];
16033
16034        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16035        config
16036            .load_cert_chain_from_pem_file("examples/cert.crt")
16037            .unwrap();
16038        config
16039            .load_priv_key_from_pem_file("examples/cert.key")
16040            .unwrap();
16041        config
16042            .set_application_protos(&[b"proto1", b"proto2"])
16043            .unwrap();
16044        config.verify_peer(false);
16045        config.set_active_connection_id_limit(2);
16046
16047        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16048        assert_eq!(pipe.handshake(), Ok(()));
16049
16050        // So far, there should not have any QUIC event.
16051        assert_eq!(pipe.client.path_event_next(), None);
16052        assert_eq!(pipe.server.path_event_next(), None);
16053        assert_eq!(pipe.client.scids_left(), 1);
16054
16055        let (scid_1, reset_token_1) = testing::create_cid_and_reset_token(16);
16056        assert_eq!(pipe.client.new_scid(&scid_1, reset_token_1, false), Ok(1));
16057
16058        // Let exchange packets over the connection.
16059        assert_eq!(pipe.advance(), Ok(()));
16060
16061        // At this point, the server should be notified that it has a new CID.
16062        assert_eq!(pipe.server.available_dcids(), 1);
16063        assert_eq!(pipe.server.path_event_next(), None);
16064        assert_eq!(pipe.client.path_event_next(), None);
16065        assert_eq!(pipe.client.scids_left(), 0);
16066
16067        let mut frames = Vec::new();
16068
16069        // Client retires more than 3x the number of allowed active CIDs.
16070        for i in 2..=7 {
16071            let (scid, reset_token) = testing::create_cid_and_reset_token(16);
16072
16073            frames.push(frame::Frame::NewConnectionId {
16074                seq_num: i,
16075                retire_prior_to: i,
16076                conn_id: scid.to_vec(),
16077                reset_token: reset_token.to_be_bytes(),
16078            });
16079        }
16080
16081        let pkt_type = packet::Type::Short;
16082
16083        let written =
16084            testing::encode_pkt(&mut pipe.client, pkt_type, &frames, &mut buf)
16085                .unwrap();
16086
16087        let active_path = pipe.server.paths.get_active().unwrap();
16088        let info = RecvInfo {
16089            to: active_path.local_addr(),
16090            from: active_path.peer_addr(),
16091        };
16092
16093        assert_eq!(
16094            pipe.server.recv(&mut buf[..written], info),
16095            Err(Error::IdLimit)
16096        );
16097
16098        let written = match pipe.server.send(&mut buf) {
16099            Ok((write, _)) => write,
16100
16101            Err(_) => unreachable!(),
16102        };
16103
16104        let frames =
16105            testing::decode_pkt(&mut pipe.client, &mut buf[..written]).unwrap();
16106        let mut iter = frames.iter();
16107
16108        assert_eq!(
16109            iter.next(),
16110            Some(&frame::Frame::ConnectionClose {
16111                error_code: 0x9,
16112                frame_type: 0,
16113                reason: Vec::new(),
16114            })
16115        );
16116    }
16117
16118    // Utility function.
16119    fn pipe_with_exchanged_cids(
16120        config: &mut Config, client_scid_len: usize, server_scid_len: usize,
16121        additional_cids: usize,
16122    ) -> testing::Pipe {
16123        let mut pipe = testing::Pipe::with_config_and_scid_lengths(
16124            config,
16125            client_scid_len,
16126            server_scid_len,
16127        )
16128        .unwrap();
16129        assert_eq!(pipe.handshake(), Ok(()));
16130
16131        let mut c_cids = Vec::new();
16132        let mut c_reset_tokens = Vec::new();
16133        let mut s_cids = Vec::new();
16134        let mut s_reset_tokens = Vec::new();
16135
16136        for i in 0..additional_cids {
16137            if client_scid_len > 0 {
16138                let (c_cid, c_reset_token) =
16139                    testing::create_cid_and_reset_token(client_scid_len);
16140                c_cids.push(c_cid);
16141                c_reset_tokens.push(c_reset_token);
16142
16143                assert_eq!(
16144                    pipe.client.new_scid(&c_cids[i], c_reset_tokens[i], true),
16145                    Ok(i as u64 + 1)
16146                );
16147            }
16148
16149            if server_scid_len > 0 {
16150                let (s_cid, s_reset_token) =
16151                    testing::create_cid_and_reset_token(server_scid_len);
16152                s_cids.push(s_cid);
16153                s_reset_tokens.push(s_reset_token);
16154                assert_eq!(
16155                    pipe.server.new_scid(&s_cids[i], s_reset_tokens[i], true),
16156                    Ok(i as u64 + 1)
16157                );
16158            }
16159        }
16160
16161        // Let exchange packets over the connection.
16162        assert_eq!(pipe.advance(), Ok(()));
16163
16164        if client_scid_len > 0 {
16165            assert_eq!(pipe.server.available_dcids(), additional_cids);
16166        }
16167
16168        if server_scid_len > 0 {
16169            assert_eq!(pipe.client.available_dcids(), additional_cids);
16170        }
16171
16172        assert_eq!(pipe.server.path_event_next(), None);
16173        assert_eq!(pipe.client.path_event_next(), None);
16174
16175        pipe
16176    }
16177
16178    #[test]
16179    fn path_validation() {
16180        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16181        config
16182            .load_cert_chain_from_pem_file("examples/cert.crt")
16183            .unwrap();
16184        config
16185            .load_priv_key_from_pem_file("examples/cert.key")
16186            .unwrap();
16187        config
16188            .set_application_protos(&[b"proto1", b"proto2"])
16189            .unwrap();
16190        config.verify_peer(false);
16191        config.set_active_connection_id_limit(2);
16192
16193        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16194        assert_eq!(pipe.handshake(), Ok(()));
16195
16196        let server_addr = testing::Pipe::server_addr();
16197        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16198
16199        // We cannot probe a new path if there are not enough identifiers.
16200        assert_eq!(
16201            pipe.client.probe_path(client_addr_2, server_addr),
16202            Err(Error::OutOfIdentifiers)
16203        );
16204
16205        let (c_cid, c_reset_token) = testing::create_cid_and_reset_token(16);
16206
16207        assert_eq!(pipe.client.new_scid(&c_cid, c_reset_token, true), Ok(1));
16208
16209        let (s_cid, s_reset_token) = testing::create_cid_and_reset_token(16);
16210        assert_eq!(pipe.server.new_scid(&s_cid, s_reset_token, true), Ok(1));
16211
16212        // We need to exchange the CIDs first.
16213        assert_eq!(
16214            pipe.client.probe_path(client_addr_2, server_addr),
16215            Err(Error::OutOfIdentifiers)
16216        );
16217
16218        // Let exchange packets over the connection.
16219        assert_eq!(pipe.advance(), Ok(()));
16220
16221        assert_eq!(pipe.server.available_dcids(), 1);
16222        assert_eq!(pipe.server.path_event_next(), None);
16223        assert_eq!(pipe.client.available_dcids(), 1);
16224        assert_eq!(pipe.client.path_event_next(), None);
16225
16226        // Now the path probing can work.
16227        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16228
16229        // But the server cannot probe a yet-unseen path.
16230        assert_eq!(
16231            pipe.server.probe_path(server_addr, client_addr_2),
16232            Err(Error::InvalidState),
16233        );
16234
16235        assert_eq!(pipe.advance(), Ok(()));
16236
16237        // The path should be validated at some point.
16238        assert_eq!(
16239            pipe.client.path_event_next(),
16240            Some(PathEvent::Validated(client_addr_2, server_addr)),
16241        );
16242        assert_eq!(pipe.client.path_event_next(), None);
16243
16244        // The server should be notified of this new path.
16245        assert_eq!(
16246            pipe.server.path_event_next(),
16247            Some(PathEvent::New(server_addr, client_addr_2)),
16248        );
16249        assert_eq!(
16250            pipe.server.path_event_next(),
16251            Some(PathEvent::Validated(server_addr, client_addr_2)),
16252        );
16253        assert_eq!(pipe.server.path_event_next(), None);
16254
16255        // The server can later probe the path again.
16256        assert_eq!(pipe.server.probe_path(server_addr, client_addr_2), Ok(1));
16257
16258        // This should not trigger any event at client side.
16259        assert_eq!(pipe.client.path_event_next(), None);
16260        assert_eq!(pipe.server.path_event_next(), None);
16261    }
16262
16263    #[test]
16264    fn losing_probing_packets() {
16265        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16266        config
16267            .load_cert_chain_from_pem_file("examples/cert.crt")
16268            .unwrap();
16269        config
16270            .load_priv_key_from_pem_file("examples/cert.key")
16271            .unwrap();
16272        config
16273            .set_application_protos(&[b"proto1", b"proto2"])
16274            .unwrap();
16275        config.verify_peer(false);
16276        config.set_active_connection_id_limit(2);
16277
16278        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
16279
16280        let server_addr = testing::Pipe::server_addr();
16281        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16282        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16283
16284        // The client creates the PATH CHALLENGE, but it is lost.
16285        testing::emit_flight(&mut pipe.client).unwrap();
16286
16287        // Wait until probing timer expires. Since the RTT is very low,
16288        // wait a bit more.
16289        let probed_pid = pipe
16290            .client
16291            .paths
16292            .path_id_from_addrs(&(client_addr_2, server_addr))
16293            .unwrap();
16294        let probe_instant = pipe
16295            .client
16296            .paths
16297            .get(probed_pid)
16298            .unwrap()
16299            .recovery
16300            .loss_detection_timer()
16301            .unwrap();
16302        let timer = probe_instant.duration_since(time::Instant::now());
16303        std::thread::sleep(timer + time::Duration::from_millis(1));
16304
16305        pipe.client.on_timeout();
16306
16307        assert_eq!(pipe.advance(), Ok(()));
16308
16309        // The path should be validated at some point.
16310        assert_eq!(
16311            pipe.client.path_event_next(),
16312            Some(PathEvent::Validated(client_addr_2, server_addr))
16313        );
16314        assert_eq!(pipe.client.path_event_next(), None);
16315
16316        assert_eq!(
16317            pipe.server.path_event_next(),
16318            Some(PathEvent::New(server_addr, client_addr_2))
16319        );
16320        // The path should be validated at some point.
16321        assert_eq!(
16322            pipe.server.path_event_next(),
16323            Some(PathEvent::Validated(server_addr, client_addr_2))
16324        );
16325        assert_eq!(pipe.server.path_event_next(), None);
16326    }
16327
16328    #[test]
16329    fn failed_path_validation() {
16330        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16331        config
16332            .load_cert_chain_from_pem_file("examples/cert.crt")
16333            .unwrap();
16334        config
16335            .load_priv_key_from_pem_file("examples/cert.key")
16336            .unwrap();
16337        config
16338            .set_application_protos(&[b"proto1", b"proto2"])
16339            .unwrap();
16340        config.verify_peer(false);
16341        config.set_active_connection_id_limit(2);
16342
16343        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
16344
16345        let server_addr = testing::Pipe::server_addr();
16346        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16347        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16348
16349        for _ in 0..MAX_PROBING_TIMEOUTS {
16350            // The client creates the PATH CHALLENGE, but it is always lost.
16351            testing::emit_flight(&mut pipe.client).unwrap();
16352
16353            // Wait until probing timer expires. Since the RTT is very low,
16354            // wait a bit more.
16355            let probed_pid = pipe
16356                .client
16357                .paths
16358                .path_id_from_addrs(&(client_addr_2, server_addr))
16359                .unwrap();
16360            let probe_instant = pipe
16361                .client
16362                .paths
16363                .get(probed_pid)
16364                .unwrap()
16365                .recovery
16366                .loss_detection_timer()
16367                .unwrap();
16368            let timer = probe_instant.duration_since(time::Instant::now());
16369            std::thread::sleep(timer + time::Duration::from_millis(1));
16370
16371            pipe.client.on_timeout();
16372        }
16373
16374        assert_eq!(
16375            pipe.client.path_event_next(),
16376            Some(PathEvent::FailedValidation(client_addr_2, server_addr)),
16377        );
16378    }
16379
16380    #[test]
16381    fn client_discard_unknown_address() {
16382        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16383        config
16384            .load_cert_chain_from_pem_file("examples/cert.crt")
16385            .unwrap();
16386        config
16387            .load_priv_key_from_pem_file("examples/cert.key")
16388            .unwrap();
16389        config
16390            .set_application_protos(&[b"proto1", b"proto2"])
16391            .unwrap();
16392        config.verify_peer(false);
16393        config.set_initial_max_data(30);
16394        config.set_initial_max_stream_data_uni(10);
16395        config.set_initial_max_streams_uni(3);
16396
16397        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
16398        assert_eq!(pipe.handshake(), Ok(()));
16399
16400        // Server sends stream data.
16401        assert_eq!(pipe.server.stream_send(3, b"a", true), Ok(1));
16402
16403        let mut flight =
16404            testing::emit_flight(&mut pipe.server).expect("no packet");
16405        // Let's change the address info.
16406        flight
16407            .iter_mut()
16408            .for_each(|(_, si)| si.from = "127.0.0.1:9292".parse().unwrap());
16409        assert_eq!(testing::process_flight(&mut pipe.client, flight), Ok(()));
16410        assert_eq!(pipe.client.paths.len(), 1);
16411    }
16412
16413    #[test]
16414    fn path_validation_limited_mtu() {
16415        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16416        config
16417            .load_cert_chain_from_pem_file("examples/cert.crt")
16418            .unwrap();
16419        config
16420            .load_priv_key_from_pem_file("examples/cert.key")
16421            .unwrap();
16422        config
16423            .set_application_protos(&[b"proto1", b"proto2"])
16424            .unwrap();
16425        config.verify_peer(false);
16426        config.set_active_connection_id_limit(2);
16427
16428        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
16429
16430        let server_addr = testing::Pipe::server_addr();
16431        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16432        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16433        // Limited MTU of 1199 bytes for some reason.
16434        testing::process_flight(
16435            &mut pipe.server,
16436            testing::emit_flight_with_max_buffer(
16437                &mut pipe.client,
16438                1199,
16439                None,
16440                None,
16441            )
16442            .expect("no packet"),
16443        )
16444        .expect("error when processing client packets");
16445        testing::process_flight(
16446            &mut pipe.client,
16447            testing::emit_flight(&mut pipe.server).expect("no packet"),
16448        )
16449        .expect("error when processing client packets");
16450        let probed_pid = pipe
16451            .client
16452            .paths
16453            .path_id_from_addrs(&(client_addr_2, server_addr))
16454            .unwrap();
16455        assert!(!pipe.client.paths.get(probed_pid).unwrap().validated(),);
16456        assert_eq!(pipe.client.path_event_next(), None);
16457        // Now let the client probe at its MTU.
16458        assert_eq!(pipe.advance(), Ok(()));
16459        assert!(pipe.client.paths.get(probed_pid).unwrap().validated());
16460        assert_eq!(
16461            pipe.client.path_event_next(),
16462            Some(PathEvent::Validated(client_addr_2, server_addr))
16463        );
16464    }
16465
16466    #[test]
16467    fn path_probing_dos() {
16468        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16469        config
16470            .load_cert_chain_from_pem_file("examples/cert.crt")
16471            .unwrap();
16472        config
16473            .load_priv_key_from_pem_file("examples/cert.key")
16474            .unwrap();
16475        config
16476            .set_application_protos(&[b"proto1", b"proto2"])
16477            .unwrap();
16478        config.verify_peer(false);
16479        config.set_active_connection_id_limit(2);
16480
16481        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
16482
16483        let server_addr = testing::Pipe::server_addr();
16484        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16485        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16486
16487        assert_eq!(pipe.advance(), Ok(()));
16488
16489        // The path should be validated at some point.
16490        assert_eq!(
16491            pipe.client.path_event_next(),
16492            Some(PathEvent::Validated(client_addr_2, server_addr))
16493        );
16494        assert_eq!(pipe.client.path_event_next(), None);
16495
16496        // The server should be notified of this new path.
16497        assert_eq!(
16498            pipe.server.path_event_next(),
16499            Some(PathEvent::New(server_addr, client_addr_2))
16500        );
16501        assert_eq!(
16502            pipe.server.path_event_next(),
16503            Some(PathEvent::Validated(server_addr, client_addr_2))
16504        );
16505        assert_eq!(pipe.server.path_event_next(), None);
16506
16507        assert_eq!(pipe.server.paths.len(), 2);
16508
16509        // Now forge a packet reusing the unverified path's CID over another
16510        // 4-tuple.
16511        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16512        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
16513        let mut flight =
16514            testing::emit_flight(&mut pipe.client).expect("no generated packet");
16515        flight
16516            .iter_mut()
16517            .for_each(|(_, si)| si.from = client_addr_3);
16518        testing::process_flight(&mut pipe.server, flight)
16519            .expect("failed to process");
16520        assert_eq!(pipe.server.paths.len(), 2);
16521        assert_eq!(
16522            pipe.server.path_event_next(),
16523            Some(PathEvent::ReusedSourceConnectionId(
16524                1,
16525                (server_addr, client_addr_2),
16526                (server_addr, client_addr_3)
16527            ))
16528        );
16529        assert_eq!(pipe.server.path_event_next(), None);
16530    }
16531
16532    #[test]
16533    fn retiring_active_path_dcid() {
16534        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16535        config
16536            .load_cert_chain_from_pem_file("examples/cert.crt")
16537            .unwrap();
16538        config
16539            .load_priv_key_from_pem_file("examples/cert.key")
16540            .unwrap();
16541        config
16542            .set_application_protos(&[b"proto1", b"proto2"])
16543            .unwrap();
16544        config.verify_peer(false);
16545        config.set_active_connection_id_limit(2);
16546
16547        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
16548        let server_addr = testing::Pipe::server_addr();
16549        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16550        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16551
16552        assert_eq!(pipe.client.retire_dcid(0), Err(Error::OutOfIdentifiers));
16553    }
16554
16555    #[test]
16556    fn send_on_path_test() {
16557        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16558        config
16559            .load_cert_chain_from_pem_file("examples/cert.crt")
16560            .unwrap();
16561        config
16562            .load_priv_key_from_pem_file("examples/cert.key")
16563            .unwrap();
16564        config
16565            .set_application_protos(&[b"proto1", b"proto2"])
16566            .unwrap();
16567        config.verify_peer(false);
16568        config.set_initial_max_data(100000);
16569        config.set_initial_max_stream_data_bidi_local(100000);
16570        config.set_initial_max_stream_data_bidi_remote(100000);
16571        config.set_initial_max_streams_bidi(2);
16572        config.set_active_connection_id_limit(4);
16573
16574        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 3);
16575
16576        let server_addr = testing::Pipe::server_addr();
16577        let client_addr = testing::Pipe::client_addr();
16578        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16579        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16580
16581        let mut buf = [0; 65535];
16582        // There is nothing to send on the initial path.
16583        assert_eq!(
16584            pipe.client.send_on_path(
16585                &mut buf,
16586                Some(client_addr),
16587                Some(server_addr)
16588            ),
16589            Err(Error::Done)
16590        );
16591
16592        // Client should send padded PATH_CHALLENGE.
16593        let (sent, si) = pipe
16594            .client
16595            .send_on_path(&mut buf, Some(client_addr_2), Some(server_addr))
16596            .expect("No error");
16597        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
16598        assert_eq!(si.from, client_addr_2);
16599        assert_eq!(si.to, server_addr);
16600
16601        let ri = RecvInfo {
16602            to: si.to,
16603            from: si.from,
16604        };
16605        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
16606
16607        let stats = pipe.server.stats();
16608        assert_eq!(stats.path_challenge_rx_count, 1);
16609
16610        // A non-existing 4-tuple raises an InvalidState.
16611        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
16612        let server_addr_2 = "127.0.0.1:9876".parse().unwrap();
16613        assert_eq!(
16614            pipe.client.send_on_path(
16615                &mut buf,
16616                Some(client_addr_3),
16617                Some(server_addr)
16618            ),
16619            Err(Error::InvalidState)
16620        );
16621        assert_eq!(
16622            pipe.client.send_on_path(
16623                &mut buf,
16624                Some(client_addr),
16625                Some(server_addr_2)
16626            ),
16627            Err(Error::InvalidState)
16628        );
16629
16630        // Let's introduce some additional path challenges and data exchange.
16631        assert_eq!(pipe.client.probe_path(client_addr, server_addr_2), Ok(2));
16632        assert_eq!(pipe.client.probe_path(client_addr_3, server_addr), Ok(3));
16633        // Just to fit in two packets.
16634        assert_eq!(pipe.client.stream_send(0, &buf[..1201], true), Ok(1201));
16635
16636        // PATH_CHALLENGE
16637        let (sent, si) = pipe
16638            .client
16639            .send_on_path(&mut buf, Some(client_addr), None)
16640            .expect("No error");
16641        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
16642        assert_eq!(si.from, client_addr);
16643        assert_eq!(si.to, server_addr_2);
16644
16645        let ri = RecvInfo {
16646            to: si.to,
16647            from: si.from,
16648        };
16649        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
16650
16651        let stats = pipe.server.stats();
16652        assert_eq!(stats.path_challenge_rx_count, 2);
16653
16654        // STREAM frame on active path.
16655        let (sent, si) = pipe
16656            .client
16657            .send_on_path(&mut buf, Some(client_addr), None)
16658            .expect("No error");
16659        assert_eq!(si.from, client_addr);
16660        assert_eq!(si.to, server_addr);
16661
16662        let ri = RecvInfo {
16663            to: si.to,
16664            from: si.from,
16665        };
16666        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
16667
16668        let stats = pipe.server.stats();
16669        assert_eq!(stats.path_challenge_rx_count, 2);
16670
16671        // PATH_CHALLENGE
16672        let (sent, si) = pipe
16673            .client
16674            .send_on_path(&mut buf, None, Some(server_addr))
16675            .expect("No error");
16676        assert_eq!(sent, MIN_CLIENT_INITIAL_LEN);
16677        assert_eq!(si.from, client_addr_3);
16678        assert_eq!(si.to, server_addr);
16679
16680        let ri = RecvInfo {
16681            to: si.to,
16682            from: si.from,
16683        };
16684        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
16685
16686        let stats = pipe.server.stats();
16687        assert_eq!(stats.path_challenge_rx_count, 3);
16688
16689        // STREAM frame on active path.
16690        let (sent, si) = pipe
16691            .client
16692            .send_on_path(&mut buf, None, Some(server_addr))
16693            .expect("No error");
16694        assert_eq!(si.from, client_addr);
16695        assert_eq!(si.to, server_addr);
16696
16697        let ri = RecvInfo {
16698            to: si.to,
16699            from: si.from,
16700        };
16701        assert_eq!(pipe.server.recv(&mut buf[..sent], ri), Ok(sent));
16702
16703        // No more data to exchange leads to Error::Done.
16704        assert_eq!(
16705            pipe.client.send_on_path(&mut buf, Some(client_addr), None),
16706            Err(Error::Done)
16707        );
16708        assert_eq!(
16709            pipe.client.send_on_path(&mut buf, None, Some(server_addr)),
16710            Err(Error::Done)
16711        );
16712
16713        assert_eq!(pipe.advance(), Ok(()));
16714
16715        let mut v1 = pipe.client.paths_iter(client_addr).collect::<Vec<_>>();
16716        let mut v2 = vec![server_addr, server_addr_2];
16717
16718        v1.sort();
16719        v2.sort();
16720
16721        assert_eq!(v1, v2);
16722
16723        let mut v1 = pipe.client.paths_iter(client_addr_2).collect::<Vec<_>>();
16724        let mut v2 = vec![server_addr];
16725
16726        v1.sort();
16727        v2.sort();
16728
16729        assert_eq!(v1, v2);
16730
16731        let mut v1 = pipe.client.paths_iter(client_addr_3).collect::<Vec<_>>();
16732        let mut v2 = vec![server_addr];
16733
16734        v1.sort();
16735        v2.sort();
16736
16737        assert_eq!(v1, v2);
16738
16739        let stats = pipe.server.stats();
16740        assert_eq!(stats.path_challenge_rx_count, 3);
16741    }
16742
16743    #[test]
16744    fn connection_migration() {
16745        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16746        config
16747            .load_cert_chain_from_pem_file("examples/cert.crt")
16748            .unwrap();
16749        config
16750            .load_priv_key_from_pem_file("examples/cert.key")
16751            .unwrap();
16752        config
16753            .set_application_protos(&[b"proto1", b"proto2"])
16754            .unwrap();
16755        config.verify_peer(false);
16756        config.set_active_connection_id_limit(3);
16757        config.set_initial_max_data(30);
16758        config.set_initial_max_stream_data_bidi_local(15);
16759        config.set_initial_max_stream_data_bidi_remote(15);
16760        config.set_initial_max_stream_data_uni(10);
16761        config.set_initial_max_streams_bidi(3);
16762
16763        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 2);
16764
16765        let server_addr = testing::Pipe::server_addr();
16766        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16767        let client_addr_3 = "127.0.0.1:9012".parse().unwrap();
16768        let client_addr_4 = "127.0.0.1:8908".parse().unwrap();
16769
16770        // Case 1: the client first probes the new address, the server too, and
16771        // then migrates.
16772        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
16773        assert_eq!(pipe.advance(), Ok(()));
16774        assert_eq!(
16775            pipe.client.path_event_next(),
16776            Some(PathEvent::Validated(client_addr_2, server_addr))
16777        );
16778        assert_eq!(pipe.client.path_event_next(), None);
16779        assert_eq!(
16780            pipe.server.path_event_next(),
16781            Some(PathEvent::New(server_addr, client_addr_2))
16782        );
16783        assert_eq!(
16784            pipe.server.path_event_next(),
16785            Some(PathEvent::Validated(server_addr, client_addr_2))
16786        );
16787        assert_eq!(
16788            pipe.client.is_path_validated(client_addr_2, server_addr),
16789            Ok(true)
16790        );
16791        assert_eq!(
16792            pipe.server.is_path_validated(server_addr, client_addr_2),
16793            Ok(true)
16794        );
16795        // The server can never initiates the connection migration.
16796        assert_eq!(
16797            pipe.server.migrate(server_addr, client_addr_2),
16798            Err(Error::InvalidState)
16799        );
16800        assert_eq!(pipe.client.migrate(client_addr_2, server_addr), Ok(1));
16801        assert_eq!(pipe.client.stream_send(0, b"data", true), Ok(4));
16802        assert_eq!(pipe.advance(), Ok(()));
16803        assert_eq!(
16804            pipe.client
16805                .paths
16806                .get_active()
16807                .expect("no active")
16808                .local_addr(),
16809            client_addr_2
16810        );
16811        assert_eq!(
16812            pipe.client
16813                .paths
16814                .get_active()
16815                .expect("no active")
16816                .peer_addr(),
16817            server_addr
16818        );
16819        assert_eq!(
16820            pipe.server.path_event_next(),
16821            Some(PathEvent::PeerMigrated(server_addr, client_addr_2))
16822        );
16823        assert_eq!(pipe.server.path_event_next(), None);
16824        assert_eq!(
16825            pipe.server
16826                .paths
16827                .get_active()
16828                .expect("no active")
16829                .local_addr(),
16830            server_addr
16831        );
16832        assert_eq!(
16833            pipe.server
16834                .paths
16835                .get_active()
16836                .expect("no active")
16837                .peer_addr(),
16838            client_addr_2
16839        );
16840
16841        // Case 2: the client migrates on a path that was not previously
16842        // validated, and has spare SCIDs/DCIDs to do so.
16843        assert_eq!(pipe.client.migrate(client_addr_3, server_addr), Ok(2));
16844        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
16845        assert_eq!(pipe.advance(), Ok(()));
16846        assert_eq!(
16847            pipe.client
16848                .paths
16849                .get_active()
16850                .expect("no active")
16851                .local_addr(),
16852            client_addr_3
16853        );
16854        assert_eq!(
16855            pipe.client
16856                .paths
16857                .get_active()
16858                .expect("no active")
16859                .peer_addr(),
16860            server_addr
16861        );
16862        assert_eq!(
16863            pipe.server.path_event_next(),
16864            Some(PathEvent::New(server_addr, client_addr_3))
16865        );
16866        assert_eq!(
16867            pipe.server.path_event_next(),
16868            Some(PathEvent::Validated(server_addr, client_addr_3))
16869        );
16870        assert_eq!(
16871            pipe.server.path_event_next(),
16872            Some(PathEvent::PeerMigrated(server_addr, client_addr_3))
16873        );
16874        assert_eq!(pipe.server.path_event_next(), None);
16875        assert_eq!(
16876            pipe.server
16877                .paths
16878                .get_active()
16879                .expect("no active")
16880                .local_addr(),
16881            server_addr
16882        );
16883        assert_eq!(
16884            pipe.server
16885                .paths
16886                .get_active()
16887                .expect("no active")
16888                .peer_addr(),
16889            client_addr_3
16890        );
16891
16892        // Case 3: the client tries to migrate on the current active path.
16893        // This is not an error, but it triggers nothing.
16894        assert_eq!(pipe.client.migrate(client_addr_3, server_addr), Ok(2));
16895        assert_eq!(pipe.client.stream_send(8, b"data", true), Ok(4));
16896        assert_eq!(pipe.advance(), Ok(()));
16897        assert_eq!(pipe.client.path_event_next(), None);
16898        assert_eq!(
16899            pipe.client
16900                .paths
16901                .get_active()
16902                .expect("no active")
16903                .local_addr(),
16904            client_addr_3
16905        );
16906        assert_eq!(
16907            pipe.client
16908                .paths
16909                .get_active()
16910                .expect("no active")
16911                .peer_addr(),
16912            server_addr
16913        );
16914        assert_eq!(pipe.server.path_event_next(), None);
16915        assert_eq!(
16916            pipe.server
16917                .paths
16918                .get_active()
16919                .expect("no active")
16920                .local_addr(),
16921            server_addr
16922        );
16923        assert_eq!(
16924            pipe.server
16925                .paths
16926                .get_active()
16927                .expect("no active")
16928                .peer_addr(),
16929            client_addr_3
16930        );
16931
16932        // Case 4: the client tries to migrate on a path that was not previously
16933        // validated, and has no spare SCIDs/DCIDs. Prevent active migration.
16934        assert_eq!(
16935            pipe.client.migrate(client_addr_4, server_addr),
16936            Err(Error::OutOfIdentifiers)
16937        );
16938        assert_eq!(
16939            pipe.client
16940                .paths
16941                .get_active()
16942                .expect("no active")
16943                .local_addr(),
16944            client_addr_3
16945        );
16946        assert_eq!(
16947            pipe.client
16948                .paths
16949                .get_active()
16950                .expect("no active")
16951                .peer_addr(),
16952            server_addr
16953        );
16954    }
16955
16956    #[test]
16957    fn connection_migration_zero_length_cid() {
16958        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
16959        config
16960            .load_cert_chain_from_pem_file("examples/cert.crt")
16961            .unwrap();
16962        config
16963            .load_priv_key_from_pem_file("examples/cert.key")
16964            .unwrap();
16965        config
16966            .set_application_protos(&[b"proto1", b"proto2"])
16967            .unwrap();
16968        config.verify_peer(false);
16969        config.set_active_connection_id_limit(2);
16970        config.set_initial_max_data(30);
16971        config.set_initial_max_stream_data_bidi_local(15);
16972        config.set_initial_max_stream_data_bidi_remote(15);
16973        config.set_initial_max_stream_data_uni(10);
16974        config.set_initial_max_streams_bidi(3);
16975
16976        let mut pipe = pipe_with_exchanged_cids(&mut config, 0, 16, 1);
16977
16978        let server_addr = testing::Pipe::server_addr();
16979        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
16980
16981        // The client migrates on a path that was not previously
16982        // validated, and has spare SCIDs/DCIDs to do so.
16983        assert_eq!(pipe.client.migrate(client_addr_2, server_addr), Ok(1));
16984        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
16985        assert_eq!(pipe.advance(), Ok(()));
16986        assert_eq!(
16987            pipe.client
16988                .paths
16989                .get_active()
16990                .expect("no active")
16991                .local_addr(),
16992            client_addr_2
16993        );
16994        assert_eq!(
16995            pipe.client
16996                .paths
16997                .get_active()
16998                .expect("no active")
16999                .peer_addr(),
17000            server_addr
17001        );
17002        assert_eq!(
17003            pipe.server.path_event_next(),
17004            Some(PathEvent::New(server_addr, client_addr_2))
17005        );
17006        assert_eq!(
17007            pipe.server.path_event_next(),
17008            Some(PathEvent::Validated(server_addr, client_addr_2))
17009        );
17010        assert_eq!(
17011            pipe.server.path_event_next(),
17012            Some(PathEvent::PeerMigrated(server_addr, client_addr_2))
17013        );
17014        assert_eq!(pipe.server.path_event_next(), None);
17015        assert_eq!(
17016            pipe.server
17017                .paths
17018                .get_active()
17019                .expect("no active")
17020                .local_addr(),
17021            server_addr
17022        );
17023        assert_eq!(
17024            pipe.server
17025                .paths
17026                .get_active()
17027                .expect("no active")
17028                .peer_addr(),
17029            client_addr_2
17030        );
17031    }
17032
17033    #[test]
17034    fn connection_migration_reordered_non_probing() {
17035        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17036        config
17037            .load_cert_chain_from_pem_file("examples/cert.crt")
17038            .unwrap();
17039        config
17040            .load_priv_key_from_pem_file("examples/cert.key")
17041            .unwrap();
17042        config
17043            .set_application_protos(&[b"proto1", b"proto2"])
17044            .unwrap();
17045        config.verify_peer(false);
17046        config.set_active_connection_id_limit(2);
17047        config.set_initial_max_data(30);
17048        config.set_initial_max_stream_data_bidi_local(15);
17049        config.set_initial_max_stream_data_bidi_remote(15);
17050        config.set_initial_max_stream_data_uni(10);
17051        config.set_initial_max_streams_bidi(3);
17052
17053        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17054
17055        let client_addr = testing::Pipe::client_addr();
17056        let server_addr = testing::Pipe::server_addr();
17057        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17058
17059        assert_eq!(pipe.client.probe_path(client_addr_2, server_addr), Ok(1));
17060        assert_eq!(pipe.advance(), Ok(()));
17061        assert_eq!(
17062            pipe.client.path_event_next(),
17063            Some(PathEvent::Validated(client_addr_2, server_addr))
17064        );
17065        assert_eq!(pipe.client.path_event_next(), None);
17066        assert_eq!(
17067            pipe.server.path_event_next(),
17068            Some(PathEvent::New(server_addr, client_addr_2))
17069        );
17070        assert_eq!(
17071            pipe.server.path_event_next(),
17072            Some(PathEvent::Validated(server_addr, client_addr_2))
17073        );
17074        assert_eq!(pipe.server.path_event_next(), None);
17075
17076        // A first flight sent from secondary address.
17077        assert_eq!(pipe.client.stream_send(0, b"data", true), Ok(4));
17078        let mut first = testing::emit_flight(&mut pipe.client).unwrap();
17079        first.iter_mut().for_each(|(_, si)| si.from = client_addr_2);
17080        // A second one, but sent from the original one.
17081        assert_eq!(pipe.client.stream_send(4, b"data", true), Ok(4));
17082        let second = testing::emit_flight(&mut pipe.client).unwrap();
17083        // Second flight is received before first one.
17084        assert_eq!(testing::process_flight(&mut pipe.server, second), Ok(()));
17085        assert_eq!(testing::process_flight(&mut pipe.server, first), Ok(()));
17086
17087        // Server does not perform connection migration because of packet
17088        // reordering.
17089        assert_eq!(pipe.server.path_event_next(), None);
17090        assert_eq!(
17091            pipe.server
17092                .paths
17093                .get_active()
17094                .expect("no active")
17095                .peer_addr(),
17096            client_addr
17097        );
17098    }
17099
17100    #[test]
17101    fn resilience_against_migration_attack() {
17102        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17103        config
17104            .load_cert_chain_from_pem_file("examples/cert.crt")
17105            .unwrap();
17106        config
17107            .load_priv_key_from_pem_file("examples/cert.key")
17108            .unwrap();
17109        config
17110            .set_application_protos(&[b"proto1", b"proto2"])
17111            .unwrap();
17112        config.verify_peer(false);
17113        config.set_active_connection_id_limit(3);
17114        config.set_initial_max_data(100000);
17115        config.set_initial_max_stream_data_bidi_local(100000);
17116        config.set_initial_max_stream_data_bidi_remote(100000);
17117        config.set_initial_max_streams_bidi(2);
17118
17119        let mut pipe = pipe_with_exchanged_cids(&mut config, 16, 16, 1);
17120
17121        let client_addr = testing::Pipe::client_addr();
17122        let server_addr = testing::Pipe::server_addr();
17123        let spoofed_client_addr = "127.0.0.1:6666".parse().unwrap();
17124
17125        const DATA_BYTES: usize = 24000;
17126        let buf = [42; DATA_BYTES];
17127        let mut recv_buf = [0; DATA_BYTES];
17128        assert_eq!(pipe.server.stream_send(1, &buf, true), Ok(12000));
17129        assert_eq!(
17130            testing::process_flight(
17131                &mut pipe.client,
17132                testing::emit_flight(&mut pipe.server).unwrap()
17133            ),
17134            Ok(())
17135        );
17136        let (rcv_data_1, _) = pipe.client.stream_recv(1, &mut recv_buf).unwrap();
17137
17138        // Fake the source address of client.
17139        let mut faked_addr_flight =
17140            testing::emit_flight(&mut pipe.client).unwrap();
17141        faked_addr_flight
17142            .iter_mut()
17143            .for_each(|(_, si)| si.from = spoofed_client_addr);
17144        assert_eq!(
17145            testing::process_flight(&mut pipe.server, faked_addr_flight),
17146            Ok(())
17147        );
17148        assert_eq!(pipe.server.stream_send(1, &buf[12000..], true), Ok(12000));
17149        assert_eq!(
17150            pipe.server.path_event_next(),
17151            Some(PathEvent::ReusedSourceConnectionId(
17152                0,
17153                (server_addr, client_addr),
17154                (server_addr, spoofed_client_addr)
17155            ))
17156        );
17157        assert_eq!(
17158            pipe.server.path_event_next(),
17159            Some(PathEvent::New(server_addr, spoofed_client_addr))
17160        );
17161
17162        assert_eq!(
17163            pipe.server.is_path_validated(server_addr, client_addr),
17164            Ok(true)
17165        );
17166        assert_eq!(
17167            pipe.server
17168                .is_path_validated(server_addr, spoofed_client_addr),
17169            Ok(false)
17170        );
17171
17172        // The client creates the PATH CHALLENGE, but it is always lost.
17173        testing::emit_flight(&mut pipe.server).unwrap();
17174
17175        // Wait until probing timer expires. Since the RTT is very low,
17176        // wait a bit more.
17177        let probed_pid = pipe
17178            .server
17179            .paths
17180            .path_id_from_addrs(&(server_addr, spoofed_client_addr))
17181            .unwrap();
17182        let probe_instant = pipe
17183            .server
17184            .paths
17185            .get(probed_pid)
17186            .unwrap()
17187            .recovery
17188            .loss_detection_timer()
17189            .unwrap();
17190        let timer = probe_instant.duration_since(time::Instant::now());
17191        std::thread::sleep(timer + time::Duration::from_millis(1));
17192
17193        pipe.server.on_timeout();
17194
17195        // Because of the small ACK size, the server cannot send more to the
17196        // client. Fallback on the previous active path.
17197        assert_eq!(
17198            pipe.server.path_event_next(),
17199            Some(PathEvent::FailedValidation(
17200                server_addr,
17201                spoofed_client_addr
17202            ))
17203        );
17204
17205        assert_eq!(
17206            pipe.server.is_path_validated(server_addr, client_addr),
17207            Ok(true)
17208        );
17209        assert_eq!(
17210            pipe.server
17211                .is_path_validated(server_addr, spoofed_client_addr),
17212            Ok(false)
17213        );
17214
17215        let server_active_path = pipe.server.paths.get_active().unwrap();
17216        assert_eq!(server_active_path.local_addr(), server_addr);
17217        assert_eq!(server_active_path.peer_addr(), client_addr);
17218        assert_eq!(pipe.advance(), Ok(()));
17219        let (rcv_data_2, fin) =
17220            pipe.client.stream_recv(1, &mut recv_buf).unwrap();
17221        assert!(fin);
17222        assert_eq!(rcv_data_1 + rcv_data_2, DATA_BYTES);
17223    }
17224
17225    #[test]
17226    fn consecutive_non_ack_eliciting() {
17227        let mut buf = [0; 65535];
17228
17229        let mut pipe = testing::Pipe::new().unwrap();
17230        assert_eq!(pipe.handshake(), Ok(()));
17231
17232        // Client sends a bunch of PING frames, causing server to ACK (ACKs aren't
17233        // ack-eliciting)
17234        let frames = [frame::Frame::Ping { mtu_probe: None }];
17235        let pkt_type = packet::Type::Short;
17236        for _ in 0..24 {
17237            let len = pipe
17238                .send_pkt_to_server(pkt_type, &frames, &mut buf)
17239                .unwrap();
17240            assert!(len > 0);
17241
17242            let frames =
17243                testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17244            assert!(
17245                frames
17246                    .iter()
17247                    .all(|frame| matches!(frame, frame::Frame::ACK { .. })),
17248                "ACK only"
17249            );
17250        }
17251
17252        // After 24 non-ack-eliciting, an ACK is explicitly elicited with a PING
17253        let len = pipe
17254            .send_pkt_to_server(pkt_type, &frames, &mut buf)
17255            .unwrap();
17256        assert!(len > 0);
17257
17258        let frames =
17259            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17260        assert!(
17261            frames
17262                .iter()
17263                .any(|frame| matches!(frame, frame::Frame::Ping {
17264                    mtu_probe: None
17265                })),
17266            "found a PING"
17267        );
17268    }
17269
17270    #[test]
17271    fn send_ack_eliciting_causes_ping() {
17272        // First establish a connection
17273        let mut pipe = testing::Pipe::new().unwrap();
17274        assert_eq!(pipe.handshake(), Ok(()));
17275
17276        // Queue a PING frame
17277        pipe.server.send_ack_eliciting().unwrap();
17278
17279        // Make sure ping is sent
17280        let mut buf = [0; 1500];
17281        let (len, _) = pipe.server.send(&mut buf).unwrap();
17282
17283        let frames =
17284            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17285        let mut iter = frames.iter();
17286
17287        assert_eq!(iter.next(), Some(&frame::Frame::Ping { mtu_probe: None }));
17288    }
17289
17290    #[test]
17291    fn send_ack_eliciting_no_ping() {
17292        // First establish a connection
17293        let mut pipe = testing::Pipe::new().unwrap();
17294        assert_eq!(pipe.handshake(), Ok(()));
17295
17296        // Queue a PING frame
17297        pipe.server.send_ack_eliciting().unwrap();
17298
17299        // Send a stream frame, which is ACK-eliciting to make sure the ping is
17300        // not sent
17301        assert_eq!(pipe.server.stream_send(1, b"a", false), Ok(1));
17302
17303        // Make sure ping is not sent
17304        let mut buf = [0; 1500];
17305        let (len, _) = pipe.server.send(&mut buf).unwrap();
17306
17307        let frames =
17308            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17309        let mut iter = frames.iter();
17310
17311        assert!(matches!(
17312            iter.next(),
17313            Some(&frame::Frame::Stream {
17314                stream_id: 1,
17315                data: _
17316            })
17317        ));
17318        assert!(iter.next().is_none());
17319    }
17320
17321    /// Tests that streams do not keep being "writable" after being collected
17322    /// on reset.
17323    #[test]
17324    fn stop_sending_stream_send_after_reset_stream_ack() {
17325        let mut b = [0; 15];
17326
17327        let mut buf = [0; 65535];
17328
17329        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17330        config
17331            .load_cert_chain_from_pem_file("examples/cert.crt")
17332            .unwrap();
17333        config
17334            .load_priv_key_from_pem_file("examples/cert.key")
17335            .unwrap();
17336        config
17337            .set_application_protos(&[b"proto1", b"proto2"])
17338            .unwrap();
17339        config.set_initial_max_data(999999999);
17340        config.set_initial_max_stream_data_bidi_local(30);
17341        config.set_initial_max_stream_data_bidi_remote(30);
17342        config.set_initial_max_stream_data_uni(30);
17343        config.set_initial_max_streams_bidi(1000);
17344        config.set_initial_max_streams_uni(0);
17345        config.verify_peer(false);
17346
17347        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17348        assert_eq!(pipe.handshake(), Ok(()));
17349
17350        assert_eq!(pipe.server.streams.len(), 0);
17351        assert_eq!(pipe.server.readable().len(), 0);
17352        assert_eq!(pipe.server.writable().len(), 0);
17353
17354        // Client opens a load of streams
17355        assert_eq!(pipe.client.stream_send(0, b"hello", true), Ok(5));
17356        assert_eq!(pipe.client.stream_send(4, b"hello", true), Ok(5));
17357        assert_eq!(pipe.client.stream_send(8, b"hello", true), Ok(5));
17358        assert_eq!(pipe.client.stream_send(12, b"hello", true), Ok(5));
17359        assert_eq!(pipe.client.stream_send(16, b"hello", true), Ok(5));
17360        assert_eq!(pipe.client.stream_send(20, b"hello", true), Ok(5));
17361        assert_eq!(pipe.client.stream_send(24, b"hello", true), Ok(5));
17362        assert_eq!(pipe.client.stream_send(28, b"hello", true), Ok(5));
17363        assert_eq!(pipe.client.stream_send(32, b"hello", true), Ok(5));
17364        assert_eq!(pipe.client.stream_send(36, b"hello", true), Ok(5));
17365        assert_eq!(pipe.advance(), Ok(()));
17366
17367        // Server iterators are populated
17368        let mut r = pipe.server.readable();
17369        assert_eq!(r.len(), 10);
17370        assert_eq!(r.next(), Some(0));
17371        assert_eq!(r.next(), Some(4));
17372        assert_eq!(r.next(), Some(8));
17373        assert_eq!(r.next(), Some(12));
17374        assert_eq!(r.next(), Some(16));
17375        assert_eq!(r.next(), Some(20));
17376        assert_eq!(r.next(), Some(24));
17377        assert_eq!(r.next(), Some(28));
17378        assert_eq!(r.next(), Some(32));
17379        assert_eq!(r.next(), Some(36));
17380
17381        assert_eq!(r.next(), None);
17382
17383        let mut w = pipe.server.writable();
17384        assert_eq!(w.len(), 10);
17385        assert_eq!(w.next(), Some(0));
17386        assert_eq!(w.next(), Some(4));
17387        assert_eq!(w.next(), Some(8));
17388        assert_eq!(w.next(), Some(12));
17389        assert_eq!(w.next(), Some(16));
17390        assert_eq!(w.next(), Some(20));
17391        assert_eq!(w.next(), Some(24));
17392        assert_eq!(w.next(), Some(28));
17393        assert_eq!(w.next(), Some(32));
17394        assert_eq!(w.next(), Some(36));
17395        assert_eq!(w.next(), None);
17396
17397        // Read one stream
17398        assert_eq!(pipe.server.stream_recv(0, &mut b), Ok((5, true)));
17399        assert!(pipe.server.stream_finished(0));
17400
17401        assert_eq!(pipe.server.readable().len(), 9);
17402        assert_eq!(pipe.server.writable().len(), 10);
17403
17404        assert_eq!(pipe.server.stream_writable(0, 0), Ok(true));
17405
17406        // Server sends data on stream 0, until blocked.
17407        while pipe.server.stream_send(0, b"world", false) != Err(Error::Done) {
17408            assert_eq!(pipe.advance(), Ok(()));
17409        }
17410
17411        assert_eq!(pipe.server.writable().len(), 9);
17412        assert_eq!(pipe.server.stream_writable(0, 0), Ok(true));
17413
17414        // Client sends STOP_SENDING.
17415        let frames = [frame::Frame::StopSending {
17416            stream_id: 0,
17417            error_code: 42,
17418        }];
17419
17420        let pkt_type = packet::Type::Short;
17421        let len = pipe
17422            .send_pkt_to_server(pkt_type, &frames, &mut buf)
17423            .unwrap();
17424
17425        // Server sent a RESET_STREAM frame in response.
17426        let frames =
17427            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17428
17429        let mut iter = frames.iter();
17430
17431        // Skip ACK frame.
17432        iter.next();
17433
17434        assert_eq!(
17435            iter.next(),
17436            Some(&frame::Frame::ResetStream {
17437                stream_id: 0,
17438                error_code: 42,
17439                final_size: 30,
17440            })
17441        );
17442
17443        // Stream 0 is now writable in order to make apps aware of STOP_SENDING
17444        // via returning an error.
17445        let mut w = pipe.server.writable();
17446        assert_eq!(w.len(), 10);
17447
17448        assert!(w.any(|s| s == 0));
17449        assert_eq!(
17450            pipe.server.stream_writable(0, 1),
17451            Err(Error::StreamStopped(42))
17452        );
17453
17454        assert_eq!(pipe.server.writable().len(), 10);
17455        assert_eq!(pipe.server.streams.len(), 10);
17456
17457        // Client acks RESET_STREAM frame.
17458        let mut ranges = ranges::RangeSet::default();
17459        ranges.insert(0..12);
17460
17461        let frames = [frame::Frame::ACK {
17462            ack_delay: 15,
17463            ranges,
17464            ecn_counts: None,
17465        }];
17466
17467        assert_eq!(pipe.send_pkt_to_server(pkt_type, &frames, &mut buf), Ok(0));
17468
17469        // Stream is collected on the server after RESET_STREAM is acked.
17470        assert_eq!(pipe.server.streams.len(), 9);
17471
17472        // Sending STOP_SENDING again shouldn't trigger RESET_STREAM again.
17473        let frames = [frame::Frame::StopSending {
17474            stream_id: 0,
17475            error_code: 42,
17476        }];
17477
17478        let len = pipe
17479            .send_pkt_to_server(pkt_type, &frames, &mut buf)
17480            .unwrap();
17481
17482        let frames =
17483            testing::decode_pkt(&mut pipe.client, &mut buf[..len]).unwrap();
17484
17485        assert_eq!(frames.len(), 1);
17486
17487        match frames.first() {
17488            Some(frame::Frame::ACK { .. }) => (),
17489
17490            f => panic!("expected ACK frame, got {:?}", f),
17491        };
17492
17493        assert_eq!(pipe.server.streams.len(), 9);
17494
17495        // Stream 0 has been collected and must not be writable anymore.
17496        let mut w = pipe.server.writable();
17497        assert_eq!(w.len(), 9);
17498        assert!(!w.any(|s| s == 0));
17499
17500        // If we called send before the client ACK of reset stream, it would
17501        // have failed with StreamStopped.
17502        assert_eq!(pipe.server.stream_send(0, b"world", true), Err(Error::Done),);
17503
17504        // Stream 0 is still not writable.
17505        let mut w = pipe.server.writable();
17506        assert_eq!(w.len(), 9);
17507        assert!(!w.any(|s| s == 0));
17508    }
17509
17510    #[test]
17511    fn challenge_no_cids() {
17512        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17513        config
17514            .load_cert_chain_from_pem_file("examples/cert.crt")
17515            .unwrap();
17516        config
17517            .load_priv_key_from_pem_file("examples/cert.key")
17518            .unwrap();
17519        config
17520            .set_application_protos(&[b"proto1", b"proto2"])
17521            .unwrap();
17522        config.verify_peer(false);
17523        config.set_active_connection_id_limit(4);
17524        config.set_initial_max_data(30);
17525        config.set_initial_max_stream_data_bidi_local(15);
17526        config.set_initial_max_stream_data_bidi_remote(15);
17527        config.set_initial_max_stream_data_uni(10);
17528        config.set_initial_max_streams_bidi(3);
17529
17530        let mut pipe =
17531            testing::Pipe::with_config_and_scid_lengths(&mut config, 16, 16)
17532                .unwrap();
17533        assert_eq!(pipe.handshake(), Ok(()));
17534
17535        // Server send CIDs to client
17536        let mut server_cids = Vec::new();
17537        for _ in 0..2 {
17538            let (cid, reset_token) = testing::create_cid_and_reset_token(16);
17539            pipe.server
17540                .new_scid(&cid, reset_token, true)
17541                .expect("server issue cid");
17542            server_cids.push(cid);
17543        }
17544        assert_eq!(pipe.advance(), Ok(()));
17545
17546        let server_addr = testing::Pipe::server_addr();
17547        let client_addr_2 = "127.0.0.1:5678".parse().unwrap();
17548
17549        // Client probes path before sending CIDs (simulating race condition)
17550        let frames = [frame::Frame::PathChallenge {
17551            data: [0, 1, 2, 3, 4, 5, 6, 7],
17552        }];
17553        let mut pkt_buf = [0u8; 1500];
17554        let mut b = octets::OctetsMut::with_slice(&mut pkt_buf);
17555        let epoch = packet::Type::Short.to_epoch().unwrap();
17556        let space = &mut pipe.client.pkt_num_spaces[epoch];
17557        let pn = pipe.client.next_pkt_num;
17558        let pn_len = 4;
17559
17560        let hdr = Header {
17561            ty: packet::Type::Short,
17562            version: pipe.client.version,
17563            dcid: server_cids[0].clone(),
17564            scid: ConnectionId::from_ref(&[5, 4, 3, 2, 1]),
17565            pkt_num: 0,
17566            pkt_num_len: pn_len,
17567            token: pipe.client.token.clone(),
17568            versions: None,
17569            key_phase: pipe.client.key_phase,
17570        };
17571        hdr.to_bytes(&mut b).expect("encode header");
17572        let payload_len = frames.iter().fold(0, |acc, x| acc + x.wire_len());
17573        b.put_u32(pn as u32).expect("put pn");
17574
17575        let payload_offset = b.off();
17576
17577        for frame in frames {
17578            frame.to_bytes(&mut b).expect("encode frames");
17579        }
17580
17581        let aead = space.crypto_seal.as_ref().expect("crypto seal");
17582
17583        let written = packet::encrypt_pkt(
17584            &mut b,
17585            pn,
17586            pn_len,
17587            payload_len,
17588            payload_offset,
17589            None,
17590            aead,
17591        )
17592        .expect("packet encrypt");
17593        pipe.client.next_pkt_num += 1;
17594
17595        pipe.server
17596            .recv(&mut pkt_buf[..written], RecvInfo {
17597                to: server_addr,
17598                from: client_addr_2,
17599            })
17600            .expect("server receive path challenge");
17601
17602        // Show that the new path is not considered a destination path by quiche
17603        assert!(!pipe
17604            .server
17605            .paths_iter(server_addr)
17606            .any(|path| path == client_addr_2));
17607    }
17608
17609    #[test]
17610    fn successful_probe_pmtud() {
17611        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17612        config
17613            .load_cert_chain_from_pem_file("examples/cert.crt")
17614            .unwrap();
17615        config
17616            .load_priv_key_from_pem_file("examples/cert.key")
17617            .unwrap();
17618        config
17619            .set_application_protos(&[b"proto1", b"proto2"])
17620            .unwrap();
17621        config.verify_peer(false);
17622        config.set_initial_max_data(100000);
17623        config.set_initial_max_stream_data_bidi_local(100000);
17624        config.set_initial_max_stream_data_bidi_remote(100000);
17625        config.set_initial_max_streams_bidi(2);
17626        config.set_active_connection_id_limit(4);
17627        config.set_max_send_udp_payload_size(1350);
17628        config.set_max_recv_udp_payload_size(1350);
17629        config.discover_pmtu(true);
17630
17631        // Perform initial handshake.
17632        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17633        assert_eq!(pipe.handshake(), Ok(()));
17634
17635        let server_addr = testing::Pipe::server_addr();
17636        let client_addr = testing::Pipe::client_addr();
17637        let pid_1 = pipe
17638            .server
17639            .paths
17640            .path_id_from_addrs(&(server_addr, client_addr))
17641            .expect("no such path");
17642
17643        // Check that PMTU params are configured correctly
17644        let pmtu_param = &mut pipe.server.paths.get_mut(pid_1).unwrap().pmtud;
17645        assert!(pmtu_param.get_probe_status());
17646        assert_eq!(pmtu_param.get_probe_size(), 1350);
17647        assert_eq!(pipe.advance(), Ok(()));
17648
17649        for (_, p) in pipe.server.paths.iter_mut() {
17650            assert_eq!(p.pmtud.get_current(), 1350);
17651            assert!(!p.pmtud.get_probe_status());
17652        }
17653    }
17654
17655    #[test]
17656    fn pmtud_probe_loss() {
17657        let mut config = Config::new(crate::PROTOCOL_VERSION).unwrap();
17658        config
17659            .load_cert_chain_from_pem_file("examples/cert.crt")
17660            .unwrap();
17661        config
17662            .load_priv_key_from_pem_file("examples/cert.key")
17663            .unwrap();
17664        config
17665            .set_application_protos(&[b"proto1", b"proto2"])
17666            .unwrap();
17667        config.verify_peer(false);
17668        config.set_initial_max_data(100000);
17669        config.set_initial_max_stream_data_bidi_local(100000);
17670        config.set_initial_max_stream_data_bidi_remote(100000);
17671        config.set_initial_max_streams_bidi(2);
17672        config.set_active_connection_id_limit(4);
17673        config.set_max_send_udp_payload_size(1350);
17674        config.set_max_recv_udp_payload_size(1250);
17675        config.discover_pmtu(true);
17676
17677        // Perform initial handshake.
17678        let mut pipe = testing::Pipe::with_config(&mut config).unwrap();
17679        assert_eq!(pipe.handshake(), Ok(()));
17680
17681        let server_addr = testing::Pipe::server_addr();
17682        let client_addr = testing::Pipe::client_addr();
17683        let pid_1 = pipe
17684            .server
17685            .paths
17686            .path_id_from_addrs(&(server_addr, client_addr))
17687            .expect("no such path");
17688
17689        // Check that PMTU params are configured correctly
17690        let pmtu_param = &mut pipe.server.paths.get_mut(pid_1).unwrap().pmtud;
17691        assert!(pmtu_param.get_probe_status());
17692        assert_eq!(pmtu_param.get_probe_size(), 1350);
17693        std::thread::sleep(
17694            pipe.server.paths.get_mut(pid_1).unwrap().recovery.rtt() +
17695                time::Duration::from_millis(1),
17696        );
17697
17698        let active_server_path = pipe.server.paths.get_active_mut().unwrap();
17699        let pmtu_param = &mut active_server_path.pmtud;
17700
17701        // PMTU not updated since probe is not ACKed
17702        assert_eq!(pmtu_param.get_current(), 1200);
17703
17704        // Continue searching for PMTU
17705        assert!(pmtu_param.get_probe_status());
17706    }
17707}
17708
17709pub use crate::packet::ConnectionId;
17710pub use crate::packet::Header;
17711pub use crate::packet::Type;
17712
17713pub use crate::path::PathEvent;
17714pub use crate::path::PathStats;
17715pub use crate::path::SocketAddrIter;
17716
17717pub use crate::recovery::congestion::CongestionControlAlgorithm;
17718
17719pub use crate::stream::StreamIter;
17720
17721mod cid;
17722mod crypto;
17723mod dgram;
17724#[cfg(feature = "ffi")]
17725mod ffi;
17726mod flowcontrol;
17727mod frame;
17728pub mod h3;
17729mod minmax;
17730mod packet;
17731mod path;
17732mod pmtud;
17733mod rand;
17734mod ranges;
17735mod recovery;
17736mod stream;
17737mod tls;