Skip to content

Commit a09fe43

Browse files
committed
docs: adjust docs, misc
1 parent 2863bc8 commit a09fe43

File tree

14 files changed

+174
-175
lines changed

14 files changed

+174
-175
lines changed

ktls-util/src/client.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,32 @@ impl KtlsConnector {
3636
///
3737
/// [`SetupError`]. This may contain the original socket if the setup failed
3838
/// and the caller can fallback to normal TLS connector implementation.
39-
pub async fn try_connect<IO>(
39+
pub async fn try_connect<S>(
4040
&self,
41-
socket: IO,
41+
socket: S,
4242
server_name: ServerName<'static>,
43-
) -> Result<KtlsStream<IO>, SetupError<IO>>
43+
) -> Result<KtlsStream<S>, SetupError<S>>
4444
where
45-
IO: AsyncRead + AsyncWrite + AsFd + Unpin,
45+
S: AsyncRead + AsyncWrite + AsFd + Unpin,
4646
{
4747
let socket = setup_ulp(socket)?;
4848

4949
self.internal_try_connect(socket, server_name)
5050
.await
5151
.map_err(|error| SetupError {
5252
error: io::Error::other(error),
53-
stream: None,
53+
socket: None,
5454
})
5555
}
5656

5757
// `rustls` has poor support for async/await...
58-
async fn internal_try_connect<IO>(
58+
async fn internal_try_connect<S>(
5959
&self,
60-
mut socket: IO,
60+
mut socket: S,
6161
server_name: ServerName<'static>,
62-
) -> Result<KtlsStream<IO>, ktls::Error>
62+
) -> Result<KtlsStream<S>, ktls::Error>
6363
where
64-
IO: AsyncRead + AsyncWrite + AsFd + Unpin,
64+
S: AsyncRead + AsyncWrite + AsFd + Unpin,
6565
{
6666
let mut conn = UnbufferedClientConnection::new(self.config.clone(), server_name)
6767
.map_err(ktls::Error::Config)?;

ktls-util/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ pub mod client;
2828
pub mod server;
2929
pub mod suites;
3030

31-
pub(crate) async fn read_record<IO>(socket: &mut IO, incoming: &mut Vec<u8>) -> io::Result<()>
31+
pub(crate) async fn read_record<S>(socket: &mut S, incoming: &mut Vec<u8>) -> io::Result<()>
3232
where
33-
IO: AsyncRead + Unpin,
33+
S: AsyncRead + Unpin,
3434
{
3535
const RECORD_HDR_SIZE: usize = 5;
3636

ktls-util/src/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ impl KtlsAcceptor {
3535
///
3636
/// [`SetupError`]. This may contain the original socket if the setup failed
3737
/// and the caller can fallback to normal TLS acceptor implementation.
38-
pub async fn try_accept<IO>(&self, socket: IO) -> Result<KtlsStream<IO>, SetupError<IO>>
38+
pub async fn try_accept<S>(&self, socket: S) -> Result<KtlsStream<S>, SetupError<S>>
3939
where
40-
IO: AsyncRead + AsyncWrite + AsFd + Unpin,
40+
S: AsyncRead + AsyncWrite + AsFd + Unpin,
4141
{
4242
let socket = setup_ulp(socket)?;
4343

4444
self.internal_try_accept(socket)
4545
.await
4646
.map_err(|error| SetupError {
4747
error: io::Error::other(error),
48-
stream: None,
48+
socket: None,
4949
})
5050
}
5151

52-
async fn internal_try_accept<IO>(&self, mut socket: IO) -> Result<KtlsStream<IO>, ktls::Error>
52+
async fn internal_try_accept<S>(&self, mut socket: S) -> Result<KtlsStream<S>, ktls::Error>
5353
where
54-
IO: AsyncWrite + AsyncRead + AsFd + Unpin,
54+
S: AsyncWrite + AsyncRead + AsFd + Unpin,
5555
{
5656
let mut conn =
5757
UnbufferedServerConnection::new(self.config.clone()).map_err(ktls::Error::Config)?;

ktls-util/src/suites.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashSet;
44
use std::io;
55
use std::net::{TcpListener, TcpStream};
66

7-
use ktls::setup::{SetupError, TlsCryptoInfoTx};
7+
use ktls::setup::{setup_ulp, SetupError, TlsCryptoInfoTx};
88
use rustls::{CipherSuite, SupportedCipherSuite, SupportedProtocolVersion};
99

1010
#[derive(Debug, Clone)]
@@ -17,17 +17,22 @@ pub struct CompatibleCipherSuites {
1717
}
1818

1919
impl CompatibleCipherSuites {
20-
/// Probes the current kernel for kTLS cipher suite compatibility.
20+
/// Probes the current Linux kernel for kTLS cipher suite compatibility.
2121
///
22-
/// - If the current kernel does not support kTLS at all, returns None.
23-
/// - Otherwise, returns a `CompatibleCipherSuites` instance containing the
24-
/// supported cipher suites and protocol versions.
22+
/// Returns `None` if the kernel does not support kTLS, otherwise returns
23+
/// a `CompatibleCipherSuites` containing supported cipher suites and
24+
/// protocol versions.
2525
///
26-
/// The caller may cache the result of this function.
26+
/// # Notes
27+
///
28+
/// - The caller may enable feature `rustls/tls12` to include TLS 1.2
29+
/// support, or the protocol versions may be empty if only TLS 1.2 is
30+
/// supported by current Linux kernel.
31+
/// - The caller may cache the result, as probing is expensive.
2732
///
2833
/// ## Errors
2934
///
30-
/// If an I/O error occurs while probing, an `io::Error` is returned.
35+
/// [`io::Error`].
3136
pub fn probe() -> io::Result<Option<Self>> {
3237
let listener = TcpListener::bind("127.0.0.0:0")?;
3338

@@ -40,10 +45,10 @@ impl CompatibleCipherSuites {
4045

4146
macro_rules! test_param {
4247
($method:ident, $data:ident, $version:expr, $cipher_type:expr) => {{
43-
let stream = match ktls::setup::setup_ulp(TcpStream::connect(local_addr)?) {
48+
let stream = match setup_ulp(TcpStream::connect(local_addr)?) {
4449
Ok(stream) => stream,
4550
Err(SetupError {
46-
stream: Some(_), ..
51+
socket: Some(_), ..
4752
}) => {
4853
// kTLS is not supported
4954
return Ok(None);
@@ -147,7 +152,8 @@ impl CompatibleCipherSuites {
147152
(true, true) => rustls::DEFAULT_VERSIONS,
148153
// The first element is TLS 1.3
149154
(false, true) => &rustls::DEFAULT_VERSIONS[..1],
150-
// The first element is TLS 1.2 (maybe, but empty slice is OK)
155+
// The first element is TLS 1.2 (maybe, but empty slice is OK, let the caller handle
156+
// it)
151157
(true, false) => &rustls::DEFAULT_VERSIONS[1..],
152158
// No supported versions
153159
(false, false) => return Ok(None),
@@ -156,9 +162,9 @@ impl CompatibleCipherSuites {
156162
}
157163

158164
/// Filters the provided cipher suites list in place, removing suites
159-
/// incompatible with kTLS on the current kernel.
165+
/// which is incompatible.
160166
///
161-
/// # Examples
167+
/// ## Examples
162168
///
163169
/// ```no_run
164170
/// use std::sync::Arc;

ktls/examples/common/client.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use std::sync::Arc;
24

35
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};

ktls/examples/common/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
#![allow(dead_code)]
2-
31
pub mod client;
42
pub mod server;

ktls/examples/common/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Example: TLS server using `ktls`.
22
3+
#![allow(dead_code)]
4+
35
use std::io;
46
use std::sync::Arc;
57

ktls/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ pub mod setup;
3030
pub mod stream;
3131

3232
pub use error::Error;
33+
pub use stream::KtlsStream;

ktls/src/setup/tls.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::error::{Error, InvalidCryptoInfo};
2020
///
2121
/// * Invalid crypto materials.
2222
/// * Syscall error.
23-
pub fn setup_tls_params<IO: AsFd>(
24-
socket: &IO,
23+
pub fn setup_tls_params<S: AsFd>(
24+
socket: &S,
2525
cipher_suite: SupportedCipherSuite,
2626
secrets: ExtractedSecrets,
2727
) -> Result<(), Error> {
@@ -41,8 +41,8 @@ pub fn setup_tls_params<IO: AsFd>(
4141
/// ## Errors
4242
///
4343
/// See [`setup_tls_params`].
44-
pub fn setup_tls_params_tx<IO: AsFd>(
45-
socket: &IO,
44+
pub fn setup_tls_params_tx<S: AsFd>(
45+
socket: &S,
4646
cipher_suite: SupportedCipherSuite,
4747
(seq, secrets): (u64, ConnectionTrafficSecrets),
4848
) -> Result<(), Error> {
@@ -61,8 +61,8 @@ pub fn setup_tls_params_tx<IO: AsFd>(
6161
/// ## Errors
6262
///
6363
/// See [`setup_tls_params`].
64-
pub fn setup_tls_params_rx<IO: AsFd>(
65-
socket: &IO,
64+
pub fn setup_tls_params_rx<S: AsFd>(
65+
socket: &S,
6666
cipher_suite: SupportedCipherSuite,
6767
(seq, secrets): (u64, ConnectionTrafficSecrets),
6868
) -> Result<(), Error> {

ktls/src/setup/ulp.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ use nix::sys::socket::{setsockopt, sockopt};
1818
/// If the error is caused by the system not supporting kTLS, such as kernel
1919
/// module `tls` not being enabled or the kernel version being too old, will
2020
/// have the original socket returned, see [`SetupError::stream`].
21-
pub fn setup_ulp<IO: AsFd>(socket: IO) -> Result<IO, SetupError<IO>> {
21+
pub fn setup_ulp<S: AsFd>(socket: S) -> Result<S, SetupError<S>> {
2222
match setsockopt(&socket, sockopt::TcpUlp::default(), b"tls") {
2323
Ok(()) => Ok(socket),
2424
Err(err) if err == Errno::ENOENT => Err(SetupError {
2525
error: io::Error::from(err),
26-
stream: Some(socket),
26+
socket: Some(socket),
2727
}),
2828
Err(err) => Err(SetupError {
2929
error: io::Error::from(err),
30-
stream: None,
30+
socket: None,
3131
}),
3232
}
3333
}
@@ -36,16 +36,16 @@ pub fn setup_ulp<IO: AsFd>(socket: IO) -> Result<IO, SetupError<IO>> {
3636
#[derive(thiserror::Error)]
3737
#[error("{error}")]
3838
/// An error that occurred while configuring the ULP.
39-
pub struct SetupError<IO> {
39+
pub struct SetupError<S> {
4040
#[source]
4141
/// The I/O error that occurred while configuring the ULP.
4242
pub error: io::Error,
4343

44-
/// The original I/O stream.
45-
pub stream: Option<IO>,
44+
/// The original I/O socket.
45+
pub socket: Option<S>,
4646
}
4747

48-
impl<IO> fmt::Debug for SetupError<IO> {
48+
impl<S> fmt::Debug for SetupError<S> {
4949
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5050
self.error.fmt(f)
5151
}

0 commit comments

Comments
 (0)