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 /// Custom destination connection ID to use for client connections.
59 ///
60 /// Be aware that [RFC 9000] places requirements for unpredictability and
61 /// length on the client DCID field. Setting this field is dangerous if
62 /// these requirements are not satisfied.
63 ///
64 /// Has no effect on server-side [`ConnectionParams`].
65 ///
66 /// [RFC 9000]: <https://datatracker.ietf.org/doc/html/rfc9000#section-7.2-3>
67 #[cfg(feature = "custom-client-dcid")]
68 pub dcid: Option<quiche::ConnectionId<'static>>,
69}
70
71impl core::fmt::Debug for ConnectionParams<'_> {
72 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 // Avoid printing 'session' since it contains connection secrets.
74 let mut s = f.debug_struct("ConnectionParams");
75 s.field("settings", &self.settings)
76 .field("tls_cert", &self.tls_cert)
77 .field("hooks", &self.hooks);
78
79 #[cfg(feature = "custom-client-dcid")]
80 s.field("dcid", &self.dcid);
81
82 s.finish()
83 }
84}
85
86impl<'a> ConnectionParams<'a> {
87 /// Creates [`ConnectionParams`] for a QUIC server.
88 /// Servers should always specify TLS credentials.
89 #[inline]
90 pub fn new_server(
91 settings: QuicSettings, tls_cert: TlsCertificatePaths<'a>, hooks: Hooks,
92 ) -> Self {
93 Self {
94 settings,
95 tls_cert: Some(tls_cert),
96 hooks,
97 session: None,
98 #[cfg(feature = "custom-client-dcid")]
99 dcid: None,
100 }
101 }
102
103 /// Creates [`ConnectionParams`] for a QUIC client.
104 /// Clients may enable mTLS by specifying TLS credentials.
105 #[inline]
106 pub fn new_client(
107 settings: QuicSettings, tls_cert: Option<TlsCertificatePaths<'a>>,
108 hooks: Hooks,
109 ) -> Self {
110 Self {
111 settings,
112 tls_cert,
113 hooks,
114 session: None,
115 #[cfg(feature = "custom-client-dcid")]
116 dcid: None,
117 }
118 }
119}