From bf2e4016ae904fd77883dd9841bd7aaa55c08c7e Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sat, 27 Nov 2021 14:56:05 -0800 Subject: [PATCH 1/2] impl From for io::Error --- quinn-proto/src/endpoint.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/quinn-proto/src/endpoint.rs b/quinn-proto/src/endpoint.rs index ae8b1f810..ff468005e 100644 --- a/quinn-proto/src/endpoint.rs +++ b/quinn-proto/src/endpoint.rs @@ -1,7 +1,7 @@ use std::{ collections::{HashMap, VecDeque}, convert::TryFrom, - fmt, iter, + fmt, io, iter, net::{IpAddr, SocketAddr}, ops::{Index, IndexMut}, sync::Arc, @@ -844,6 +844,19 @@ pub enum ConnectError { UnsupportedVersion, } +// For compatibility with API consumers +impl From for io::Error { + fn from(x: ConnectError) -> io::Error { + use ConnectError::*; + let kind = match x { + InvalidDnsName(_) | InvalidRemoteAddress(_) => io::ErrorKind::InvalidData, + EndpointStopping | TooManyConnections | NoDefaultClientConfig => io::ErrorKind::Other, + UnsupportedVersion => io::ErrorKind::InvalidInput, + }; + io::Error::new(kind, x) + } +} + /// Reset Tokens which are associated with peer socket addresses /// /// The standard `HashMap` is used since both `SocketAddr` and `ResetToken` are From ba74597bc34cfec1fabfca5cdb90ffc14185201b Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sat, 27 Nov 2021 15:00:01 -0800 Subject: [PATCH 2/2] Simple connect function --- quinn/src/connect.rs | 21 +++++++++++++++++++++ quinn/src/lib.rs | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 quinn/src/connect.rs diff --git a/quinn/src/connect.rs b/quinn/src/connect.rs new file mode 100644 index 000000000..1ac08703e --- /dev/null +++ b/quinn/src/connect.rs @@ -0,0 +1,21 @@ +use std::{ + io, + net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, +}; + +use crate::{ClientConfig, Connecting, Endpoint}; + +/// Connect to `server_address`, authenticating it as `server_name`, using a new endpoint +pub fn connect( + config: ClientConfig, + server_address: SocketAddr, + server_name: &str, +) -> io::Result { + let bind_addr = match server_address { + SocketAddr::V6(_) => IpAddr::from(Ipv6Addr::UNSPECIFIED), + SocketAddr::V4(_) => IpAddr::from(Ipv4Addr::UNSPECIFIED), + }; + let endpoint = Endpoint::client(SocketAddr::new(bind_addr, 0))?; + let fut = endpoint.connect_with(config, server_address, server_name)?; + Ok(fut) +} diff --git a/quinn/src/lib.rs b/quinn/src/lib.rs index 9ce8f04e5..39fe06c75 100644 --- a/quinn/src/lib.rs +++ b/quinn/src/lib.rs @@ -42,6 +42,7 @@ use std::time::Duration; mod broadcast; +mod connect; mod connection; mod endpoint; mod mutex; @@ -55,6 +56,7 @@ pub use proto::{ Transmit, TransportConfig, VarInt, }; +pub use crate::connect::connect; pub use crate::connection::{ Connecting, Connection, Datagrams, IncomingBiStreams, IncomingUniStreams, NewConnection, OpenBi, OpenUni, SendDatagramError, ZeroRttAccepted,