Skip to main content

bornera_rustls/
server_status.rs

1//! Data-only observations of a socket-free server session.
2
3use bornera::TransportDiagnostic;
4
5pub(crate) const STATUS_HANDSHAKING: u8 = 1 << 0;
6pub(crate) const STATUS_OPEN: u8 = 1 << 1;
7pub(crate) const STATUS_WANTS_READ: u8 = 1 << 2;
8pub(crate) const STATUS_WANTS_WRITE: u8 = 1 << 3;
9pub(crate) const STATUS_CLOSE_NOTIFY_SENT: u8 = 1 << 4;
10
11/// Authenticated peer-input termination observed by a TLS server session.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum RustlsPeerClosure {
15    /// Further encrypted peer input remains possible.
16    Open,
17    /// The peer sent an authenticated TLS `close_notify` alert.
18    Clean,
19    /// Raw input ended without an authenticated TLS `close_notify` alert.
20    Truncated,
21}
22
23/// Exact bounded state needed by an external socket and lifecycle owner.
24#[derive(Clone, Copy, Debug, Eq, PartialEq)]
25pub struct RustlsServerStatus {
26    pub(crate) flags: u8,
27    pub(crate) readable_plaintext_bytes: usize,
28    pub(crate) tls_egress_bytes: usize,
29    pub(crate) peer_closure: RustlsPeerClosure,
30    pub(crate) failure: Option<TransportDiagnostic>,
31}
32
33impl RustlsServerStatus {
34    /// Returns whether rustls is still negotiating the initial handshake.
35    pub const fn is_handshaking(self) -> bool {
36        self.flags & STATUS_HANDSHAKING != 0
37    }
38
39    /// Returns whether rustls has authenticated and negotiated the initial handshake.
40    pub const fn is_handshake_complete(self) -> bool {
41        !self.is_handshaking()
42    }
43
44    /// Returns whether handshake completion and its required egress are externally visible.
45    pub const fn is_open(self) -> bool {
46        self.flags & STATUS_OPEN != 0
47    }
48
49    /// Returns whether further encrypted peer input can progress the session.
50    pub const fn wants_read(self) -> bool {
51        self.flags & STATUS_WANTS_READ != 0
52    }
53
54    /// Returns whether [`crate::RustlsServerSession::drain_tls`] can produce output.
55    pub const fn wants_write(self) -> bool {
56        self.flags & STATUS_WANTS_WRITE != 0
57    }
58
59    /// Returns authenticated application bytes immediately available to read.
60    pub const fn readable_plaintext_bytes(self) -> usize {
61        self.readable_plaintext_bytes
62    }
63
64    /// Returns encrypted output observed by the last successful rustls transition.
65    pub const fn tls_egress_bytes(self) -> usize {
66        self.tls_egress_bytes
67    }
68
69    /// Returns the authenticated or raw peer-input termination state.
70    pub const fn peer_closure(self) -> RustlsPeerClosure {
71        self.peer_closure
72    }
73
74    /// Returns whether local graceful TLS shutdown has started.
75    pub const fn close_notify_sent(self) -> bool {
76        self.flags & STATUS_CLOSE_NOTIFY_SENT != 0
77    }
78
79    /// Returns the first fatal bounded diagnostic, if progression has stopped.
80    pub const fn failure(self) -> Option<TransportDiagnostic> {
81        self.failure
82    }
83}