tokio_quiche/settings/mod.rs
1// Copyright (C) 2025, 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//! Configuration for QUIC connections.
28
29mod config;
30mod hooks;
31mod quic;
32mod tls;
33
34pub(crate) use self::config::*;
35
36pub use self::hooks::*;
37pub use self::quic::*;
38pub use self::tls::*;
39
40/// Combined configuration parameters required to establish a QUIC connection.
41///
42/// [`ConnectionParams`] aggregates the parameters required for all QUIC
43/// connections, regardless of whether it's a client- or server-side connection.
44/// To construct them, either `ConnectionParams::new_server` or
45/// `ConnectionParams::new_client` must be used. The parameters can be modified
46/// freely after construction.
47#[derive(Default)]
48#[non_exhaustive] // force use of constructor functions
49pub struct ConnectionParams<'a> {
50 /// QUIC connection settings.
51 pub settings: QuicSettings,
52 /// Optional TLS credentials to authenticate with.
53 pub tls_cert: Option<TlsCertificatePaths<'a>>,
54 /// Hooks to use for the connection.
55 pub hooks: Hooks,
56 /// Set the session to attempt resumption.
57 pub session: Option<Vec<u8>>,
58}
59
60impl core::fmt::Debug for ConnectionParams<'_> {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 // Avoid printing 'session' since it contains connection secrets.
63 f.debug_struct("ConnectionParams")
64 .field("settings", &self.settings)
65 .field("tls_cert", &self.tls_cert)
66 .field("hooks", &self.hooks)
67 .finish()
68 }
69}
70
71impl<'a> ConnectionParams<'a> {
72 /// Creates [`ConnectionParams`] for a QUIC server.
73 /// Servers should always specify TLS credentials.
74 #[inline]
75 pub fn new_server(
76 settings: QuicSettings, tls_cert: TlsCertificatePaths<'a>, hooks: Hooks,
77 ) -> Self {
78 Self {
79 settings,
80 tls_cert: Some(tls_cert),
81 hooks,
82 session: None,
83 }
84 }
85
86 /// Creates [`ConnectionParams`] for a QUIC client.
87 /// Clients may enable mTLS by specifying TLS credentials.
88 #[inline]
89 pub fn new_client(
90 settings: QuicSettings, tls_cert: Option<TlsCertificatePaths<'a>>,
91 hooks: Hooks,
92 ) -> Self {
93 Self {
94 settings,
95 tls_cert,
96 hooks,
97 session: None,
98 }
99 }
100}