tokio-xmpp: Remove an unwrap() in DNS name parsing

This commit is contained in:
Emmanuel Gil Peyrot 2021-12-01 17:30:20 +01:00 committed by Link Mauve
parent 4a8bda1287
commit edba9c9f5d
2 changed files with 15 additions and 1 deletions

View file

@ -7,6 +7,8 @@ use std::fmt;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::str::Utf8Error; use std::str::Utf8Error;
#[cfg(feature = "tls-rust")] #[cfg(feature = "tls-rust")]
use tokio_rustls::rustls::client::InvalidDnsNameError;
#[cfg(feature = "tls-rust")]
use tokio_rustls::rustls::Error as TlsError; use tokio_rustls::rustls::Error as TlsError;
use trust_dns_proto::error::ProtoError; use trust_dns_proto::error::ProtoError;
use trust_dns_resolver::error::ResolveError; use trust_dns_resolver::error::ResolveError;
@ -32,6 +34,9 @@ pub enum Error {
Auth(AuthError), Auth(AuthError),
/// TLS error /// TLS error
Tls(TlsError), Tls(TlsError),
#[cfg(feature = "tls-rust")]
/// DNS name parsing error
DnsNameError(InvalidDnsNameError),
/// Connection closed /// Connection closed
Disconnected, Disconnected,
/// Shoud never happen /// Shoud never happen
@ -48,6 +53,8 @@ impl fmt::Display for Error {
Error::Protocol(e) => write!(fmt, "protocol error: {}", e), Error::Protocol(e) => write!(fmt, "protocol error: {}", e),
Error::Auth(e) => write!(fmt, "authentication error: {}", e), Error::Auth(e) => write!(fmt, "authentication error: {}", e),
Error::Tls(e) => write!(fmt, "TLS error: {}", e), Error::Tls(e) => write!(fmt, "TLS error: {}", e),
#[cfg(feature = "tls-rust")]
Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
Error::Disconnected => write!(fmt, "disconnected"), Error::Disconnected => write!(fmt, "disconnected"),
Error::InvalidState => write!(fmt, "invalid state"), Error::InvalidState => write!(fmt, "invalid state"),
} }
@ -90,6 +97,13 @@ impl From<TlsError> for Error {
} }
} }
#[cfg(feature = "tls-rust")]
impl From<InvalidDnsNameError> for Error {
fn from(e: InvalidDnsNameError) -> Self {
Error::DnsNameError(e)
}
}
/// Causes for stream parsing errors /// Causes for stream parsing errors
#[derive(Debug)] #[derive(Debug)]
pub enum ParserError { pub enum ParserError {

View file

@ -42,7 +42,7 @@ async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>, xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> { ) -> Result<TlsStream<S>, Error> {
let domain = &xmpp_stream.jid.clone().domain(); let domain = &xmpp_stream.jid.clone().domain();
let domain = ServerName::try_from(domain.as_str()).unwrap(); let domain = ServerName::try_from(domain.as_str())?;
let stream = xmpp_stream.into_inner(); let stream = xmpp_stream.into_inner();
let mut root_store = RootCertStore::empty(); let mut root_store = RootCertStore::empty();
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| { root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {