1use std::{io, num::NonZeroUsize, sync::Arc};
4
5use bornera::{TransportLimits, TransportPressure};
6use calandria::RetainedBytes;
7use rustls::{ClientConfig, pki_types::ServerName};
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub struct RustlsTransportLimits {
12 application_write_buffer_bytes: NonZeroUsize,
13 max_tls_egress_bytes: NonZeroUsize,
14 max_plaintext_bytes: NonZeroUsize,
15 pressure: TransportPressure,
16}
17
18impl RustlsTransportLimits {
19 pub fn new(
29 application_write_buffer_bytes: NonZeroUsize,
30 max_tls_egress_bytes: NonZeroUsize,
31 max_plaintext_bytes: NonZeroUsize,
32 pressure: TransportPressure,
33 ) -> Result<Self, RustlsTransportLimitsError> {
34 if pressure.inbound() == RetainedBytes::ZERO {
35 return Err(RustlsTransportLimitsError::MissingInboundCharge);
36 }
37 if pressure.protocol() == RetainedBytes::ZERO {
38 return Err(RustlsTransportLimitsError::MissingProtocolCharge);
39 }
40 let application_write = u64::try_from(application_write_buffer_bytes.get())
41 .map(RetainedBytes::new)
42 .map_err(|_| RustlsTransportLimitsError::AddressSpace)?;
43 let readable_plaintext = u64::try_from(max_plaintext_bytes.get())
44 .map(RetainedBytes::new)
45 .map_err(|_| RustlsTransportLimitsError::AddressSpace)?;
46 let Some(minimum_plaintext) = application_write.checked_add(readable_plaintext) else {
47 return Err(RustlsTransportLimitsError::AddressSpace);
48 };
49 if pressure.plaintext() < minimum_plaintext {
50 return Err(RustlsTransportLimitsError::PlaintextCharge {
51 minimum: minimum_plaintext,
52 supplied: pressure.plaintext(),
53 });
54 }
55 let max_tls_egress = u64::try_from(max_tls_egress_bytes.get())
56 .map(RetainedBytes::new)
57 .map_err(|_| RustlsTransportLimitsError::AddressSpace)?;
58 if pressure.outbound() < max_tls_egress {
59 return Err(RustlsTransportLimitsError::EgressCharge {
60 minimum: max_tls_egress,
61 supplied: pressure.outbound(),
62 });
63 }
64 Ok(Self {
65 application_write_buffer_bytes,
66 max_tls_egress_bytes,
67 max_plaintext_bytes,
68 pressure,
69 })
70 }
71
72 pub const fn application_write_buffer_bytes(self) -> NonZeroUsize {
74 self.application_write_buffer_bytes
75 }
76
77 pub const fn max_tls_egress_bytes(self) -> NonZeroUsize {
79 self.max_tls_egress_bytes
80 }
81
82 pub const fn max_plaintext_bytes(self) -> NonZeroUsize {
84 self.max_plaintext_bytes
85 }
86
87 pub const fn pressure(self) -> TransportPressure {
89 self.pressure
90 }
91
92 pub const fn transport_limits(self) -> TransportLimits {
94 TransportLimits::new(self.pressure.total())
95 }
96}
97
98#[derive(Clone, Copy, Debug, Eq, PartialEq)]
100#[non_exhaustive]
101pub enum RustlsTransportLimitsError {
102 AddressSpace,
104 MissingInboundCharge,
106 MissingProtocolCharge,
108 EgressCharge {
110 minimum: RetainedBytes,
112 supplied: RetainedBytes,
114 },
115 PlaintextCharge {
117 minimum: RetainedBytes,
119 supplied: RetainedBytes,
121 },
122}
123
124#[derive(Clone, Debug)]
126pub struct RustlsTransportConfig {
127 client: Arc<ClientConfig>,
128 server_name: ServerName<'static>,
129 limits: RustlsTransportLimits,
130}
131
132impl RustlsTransportConfig {
133 pub fn new(
135 client: Arc<ClientConfig>,
136 server_name: ServerName<'static>,
137 limits: RustlsTransportLimits,
138 ) -> Self {
139 Self {
140 client,
141 server_name,
142 limits,
143 }
144 }
145
146 pub fn for_server_name(
148 client: Arc<ClientConfig>,
149 server_name: &str,
150 limits: RustlsTransportLimits,
151 ) -> Result<Self, RustlsConfigError> {
152 let server_name = ServerName::try_from(server_name.to_owned())
153 .map_err(|_| RustlsConfigError::InvalidServerName)?;
154 Ok(Self::new(client, server_name, limits))
155 }
156
157 pub fn client_config(&self) -> &Arc<ClientConfig> {
159 &self.client
160 }
161
162 pub const fn server_name(&self) -> &ServerName<'static> {
164 &self.server_name
165 }
166
167 pub const fn limits(&self) -> RustlsTransportLimits {
169 self.limits
170 }
171}
172
173#[derive(Clone, Copy, Debug, Eq, PartialEq)]
175#[non_exhaustive]
176pub enum RustlsConfigError {
177 InvalidServerName,
179}
180
181#[derive(Debug)]
183#[non_exhaustive]
184pub enum RustlsConnectError {
185 Capacity {
187 required: RetainedBytes,
189 supplied: RetainedBytes,
191 },
192 Transport(bornera::TransportDiagnostic),
194 Tls(rustls::Error),
196 Io(io::Error),
198}