tokio-xmpp: gate AsRawFd behind ktls feature
`AsRawFd` was breaking compilation on Windows. skip-changelog: ktls support hasn't been released yet Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
22292c86cc
commit
b046d36424
2 changed files with 21 additions and 18 deletions
|
|
@ -2,14 +2,10 @@
|
||||||
|
|
||||||
use alloc::borrow::Cow;
|
use alloc::borrow::Cow;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::os::fd::AsRawFd;
|
|
||||||
|
|
||||||
use futures::{sink::SinkExt, stream::StreamExt};
|
use futures::{sink::SinkExt, stream::StreamExt};
|
||||||
use sasl::common::ChannelBinding;
|
use sasl::common::ChannelBinding;
|
||||||
use tokio::{
|
use tokio::{io::BufStream, net::TcpStream};
|
||||||
io::{AsyncRead, AsyncWrite, BufStream},
|
|
||||||
net::TcpStream,
|
|
||||||
};
|
|
||||||
use xmpp_parsers::{
|
use xmpp_parsers::{
|
||||||
jid::Jid,
|
jid::Jid,
|
||||||
starttls::{self, Request},
|
starttls::{self, Request},
|
||||||
|
|
@ -17,7 +13,7 @@ use xmpp_parsers::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
connect::{
|
connect::{
|
||||||
tls_common::{establish_tls_connection, TlsConnectorError, TlsStream},
|
tls_common::{establish_tls_connection, TlsAsyncStream, TlsConnectorError, TlsStream},
|
||||||
DnsConfig, ServerConnector,
|
DnsConfig, ServerConnector,
|
||||||
},
|
},
|
||||||
error::{Error, ProtocolError},
|
error::{Error, ProtocolError},
|
||||||
|
|
@ -94,7 +90,7 @@ impl ServerConnector for StartTlsServerConnector {
|
||||||
|
|
||||||
/// Performs `<starttls/>` on an XmppStream and returns a binary
|
/// Performs `<starttls/>` on an XmppStream and returns a binary
|
||||||
/// TlsStream.
|
/// TlsStream.
|
||||||
pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin + AsRawFd>(
|
pub async fn starttls<S: TlsAsyncStream>(
|
||||||
mut stream: XmppStream<BufStream<S>>,
|
mut stream: XmppStream<BufStream<S>>,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
) -> Result<(TlsStream<S>, ChannelBinding), Error> {
|
) -> Result<(TlsStream<S>, ChannelBinding), Error> {
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,23 @@
|
||||||
//! Common TLS functionality shared between direct_tls and starttls modules
|
//! Common TLS functionality shared between direct_tls and starttls modules
|
||||||
|
|
||||||
use core::{error::Error as StdError, fmt};
|
use core::{error::Error as StdError, fmt};
|
||||||
#[cfg(all(feature = "rustls-any-backend", not(feature = "native-tls")))]
|
#[cfg(feature = "ktls")]
|
||||||
use std::os::fd::AsRawFd;
|
use std::os::fd::AsRawFd;
|
||||||
use tokio::io::{AsyncRead, AsyncWrite};
|
use tokio::io::{AsyncRead, AsyncWrite};
|
||||||
|
|
||||||
|
/// Trait alias for async streams that can be used with TLS.
|
||||||
|
// When the `ktls` feature is enabled, this additionally requires `AsRawFd`.
|
||||||
|
#[cfg(feature = "ktls")]
|
||||||
|
pub trait TlsAsyncStream: AsyncRead + AsyncWrite + Unpin + AsRawFd {}
|
||||||
|
#[cfg(feature = "ktls")]
|
||||||
|
impl<T: AsyncRead + AsyncWrite + Unpin + AsRawFd> TlsAsyncStream for T {}
|
||||||
|
|
||||||
|
/// Trait alias for async streams that can be used with TLS.
|
||||||
|
#[cfg(not(feature = "ktls"))]
|
||||||
|
pub trait TlsAsyncStream: AsyncRead + AsyncWrite + Unpin {}
|
||||||
|
#[cfg(not(feature = "ktls"))]
|
||||||
|
impl<T: AsyncRead + AsyncWrite + Unpin> TlsAsyncStream for T {}
|
||||||
|
|
||||||
#[cfg(feature = "native-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
use native_tls::Error as TlsError;
|
use native_tls::Error as TlsError;
|
||||||
#[cfg(feature = "rustls-any-backend")]
|
#[cfg(feature = "rustls-any-backend")]
|
||||||
|
|
@ -92,13 +105,10 @@ impl From<InvalidDnsNameError> for TlsConnectorError {
|
||||||
|
|
||||||
/// Establish TLS connection using native-tls
|
/// Establish TLS connection using native-tls
|
||||||
#[cfg(feature = "native-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
pub async fn establish_tls_connection<S>(
|
pub async fn establish_tls_connection<S: TlsAsyncStream>(
|
||||||
stream: S,
|
stream: S,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
) -> Result<(TlsStream<S>, ChannelBinding), Error>
|
) -> Result<(TlsStream<S>, ChannelBinding), Error> {
|
||||||
where
|
|
||||||
S: AsyncRead + AsyncWrite + Unpin,
|
|
||||||
{
|
|
||||||
let domain = domain.to_owned();
|
let domain = domain.to_owned();
|
||||||
let tls_stream = TlsConnector::from(NativeTlsConnector::builder().build().unwrap())
|
let tls_stream = TlsConnector::from(NativeTlsConnector::builder().build().unwrap())
|
||||||
.connect(&domain, stream)
|
.connect(&domain, stream)
|
||||||
|
|
@ -112,13 +122,10 @@ where
|
||||||
|
|
||||||
/// Establish TLS connection using rustls
|
/// Establish TLS connection using rustls
|
||||||
#[cfg(all(feature = "rustls-any-backend", not(feature = "native-tls")))]
|
#[cfg(all(feature = "rustls-any-backend", not(feature = "native-tls")))]
|
||||||
pub async fn establish_tls_connection<S>(
|
pub async fn establish_tls_connection<S: TlsAsyncStream>(
|
||||||
stream: S,
|
stream: S,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
) -> Result<(TlsStream<S>, ChannelBinding), Error>
|
) -> Result<(TlsStream<S>, ChannelBinding), Error> {
|
||||||
where
|
|
||||||
S: AsyncRead + AsyncWrite + Unpin + AsRawFd,
|
|
||||||
{
|
|
||||||
let domain =
|
let domain =
|
||||||
ServerName::try_from(domain.to_owned()).map_err(TlsConnectorError::DnsNameError)?;
|
ServerName::try_from(domain.to_owned()).map_err(TlsConnectorError::DnsNameError)?;
|
||||||
let mut root_store = RootCertStore::empty();
|
let mut root_store = RootCertStore::empty();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue