diff --git a/compio-tls/Cargo.toml b/compio-tls/Cargo.toml index 31ce789a..3251f555 100644 --- a/compio-tls/Cargo.toml +++ b/compio-tls/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "compio-tls" -version = "0.3.0" +version = "0.3.1" description = "TLS adaptor with compio" categories = ["asynchronous", "network-programming"] keywords = ["async", "net", "tls"] @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"] compio-buf = { workspace = true } compio-io = { workspace = true, features = ["compat"] } -native-tls = { version = "0.2.11", optional = true } +native-tls = { version = "0.2.11", optional = true, features = ["alpn"] } rustls = { workspace = true, default-features = false, optional = true, features = [ "logging", "std", diff --git a/compio-tls/src/stream/mod.rs b/compio-tls/src/stream/mod.rs index 8557f20d..1bbf4a22 100644 --- a/compio-tls/src/stream/mod.rs +++ b/compio-tls/src/stream/mod.rs @@ -1,4 +1,4 @@ -use std::{io, mem::MaybeUninit}; +use std::{borrow::Cow, io, mem::MaybeUninit}; use compio_buf::{BufResult, IoBuf, IoBufMut}; use compio_io::{compat::SyncStream, AsyncRead, AsyncWrite}; @@ -24,6 +24,15 @@ impl TlsStreamInner { Self::Rustls(s) => s.get_mut(), } } + + pub fn negotiated_alpn(&self) -> Option> { + match self { + #[cfg(feature = "native-tls")] + Self::NativeTls(s) => s.negotiated_alpn().ok().flatten().map(Cow::from), + #[cfg(feature = "rustls")] + Self::Rustls(s) => s.negotiated_alpn().map(Cow::from), + } + } } impl io::Read for TlsStreamInner { @@ -87,6 +96,11 @@ impl TlsStream { pub(crate) fn new_rustls_server(s: SyncStream, conn: rustls::ServerConnection) -> Self { Self(TlsStreamInner::Rustls(rtls::TlsStream::new_server(s, conn))) } + + /// Returns the negotiated ALPN protocol. + pub fn negotiated_alpn(&self) -> Option> { + self.0.negotiated_alpn() + } } #[cfg(feature = "native-tls")] diff --git a/compio-tls/src/stream/rtls.rs b/compio-tls/src/stream/rtls.rs index 08e01a93..ff63a8f2 100644 --- a/compio-tls/src/stream/rtls.rs +++ b/compio-tls/src/stream/rtls.rs @@ -83,6 +83,13 @@ impl TlsStream { pub fn get_mut(&mut self) -> &mut S { &mut self.inner } + + pub fn negotiated_alpn(&self) -> Option<&[u8]> { + match &self.conn { + TlsConnection::Client(client) => client.alpn_protocol(), + TlsConnection::Server(server) => server.alpn_protocol(), + } + } } impl TlsStream {