kumo_tls_helper/
traits.rs

1//! Async stream traits for TLS and plain TCP connections.
2
3use std::fmt::Debug;
4use std::os::fd::{AsRawFd, FromRawFd};
5use tokio::io::{AsyncRead, AsyncWrite};
6use tokio::net::TcpStream;
7use tokio_openssl::SslStream;
8use tokio_rustls::client::TlsStream as TlsClientStream;
9use tokio_rustls::server::TlsStream as TlsServerStream;
10
11pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Debug + Unpin + Send + Sync {
12    /// Optionally clone a TcpStream that represents the same underlying
13    /// stream as this one.
14    /// This only has an impl that returns Some for TcpStream.
15    /// It is present to facilitate a workaround for some awkwardness
16    /// in the SslStream implementation for the failed-handshake case.
17    fn try_dup(&self) -> Option<TcpStream> {
18        None
19    }
20
21    /// Returns Ok() if the type can be converted without loss to a TcpStream,
22    /// or Err(self) otherwise.  This is used by the proxy server to decide
23    /// whether we can use splice(2).
24    fn try_into_tcp_stream(self) -> Result<TcpStream, Self>
25    where
26        Self: Sized,
27    {
28        Err(self)
29    }
30}
31impl AsyncReadAndWrite for TlsClientStream<TcpStream> {}
32impl AsyncReadAndWrite for TlsClientStream<BoxedAsyncReadAndWrite> {}
33impl AsyncReadAndWrite for TlsServerStream<TcpStream> {}
34impl AsyncReadAndWrite for TlsServerStream<BoxedAsyncReadAndWrite> {}
35
36impl AsyncReadAndWrite for TcpStream {
37    fn try_dup(&self) -> Option<TcpStream> {
38        let fd = self.as_raw_fd();
39        // SAFETY: dup creates a new fd without affecting the state
40        // of other descriptors
41        let duplicate = unsafe { libc::dup(fd) };
42        if duplicate == -1 {
43            None
44        } else {
45            // SAFETY: we're wrapping the new duplicate from above,
46            // which is fine, and provides a destructor for that fd
47            // when the TcpStream is dropped
48            let duplicate_stream = unsafe { std::net::TcpStream::from_raw_fd(duplicate) };
49            TcpStream::from_std(duplicate_stream).ok()
50        }
51    }
52
53    // Yes, a TcpStream can be converted to a TcpStream
54    fn try_into_tcp_stream(self) -> Result<TcpStream, Self> {
55        Ok(self)
56    }
57}
58impl AsyncReadAndWrite for SslStream<TcpStream> {}
59impl AsyncReadAndWrite for SslStream<BoxedAsyncReadAndWrite> {}
60impl AsyncReadAndWrite for tokio::net::UnixStream {}
61
62pub type BoxedAsyncReadAndWrite = Box<dyn AsyncReadAndWrite>;
63
64impl AsyncReadAndWrite for BoxedAsyncReadAndWrite {}