aboutsummaryrefslogtreecommitdiff
path: root/net/src/transports
diff options
context:
space:
mode:
Diffstat (limited to 'net/src/transports')
-rw-r--r--net/src/transports/tcp.rs17
-rw-r--r--net/src/transports/tls.rs39
-rw-r--r--net/src/transports/udp.rs14
3 files changed, 26 insertions, 44 deletions
diff --git a/net/src/transports/tcp.rs b/net/src/transports/tcp.rs
index 99243b5..af50c10 100644
--- a/net/src/transports/tcp.rs
+++ b/net/src/transports/tcp.rs
@@ -1,5 +1,6 @@
-use async_trait::async_trait;
+use std::net::SocketAddr;
+use async_trait::async_trait;
use smol::{
io::{split, AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf},
lock::Mutex,
@@ -8,7 +9,7 @@ use smol::{
use crate::{
connection::{Connection, ToConn},
- endpoint::{Addr, Endpoint, Port},
+ endpoint::Endpoint,
listener::{ConnListener, ToListener},
Error, Result,
};
@@ -70,17 +71,17 @@ impl ConnListener for TcpListener {
}
/// Connects to the given TCP address and port.
-pub async fn dial_tcp(addr: &Addr, port: &Port) -> Result<TcpConn> {
- let address = format!("{}:{}", addr, port);
- let conn = TcpStream::connect(address).await?;
+pub async fn dial_tcp(endpoint: &Endpoint) -> Result<TcpConn> {
+ let addr = SocketAddr::try_from(endpoint.clone())?;
+ let conn = TcpStream::connect(addr).await?;
conn.set_nodelay(true)?;
Ok(TcpConn::new(conn))
}
/// Listens on the given TCP address and port.
-pub async fn listen_tcp(addr: &Addr, port: &Port) -> Result<TcpListener> {
- let address = format!("{}:{}", addr, port);
- let listener = TcpListener::bind(address).await?;
+pub async fn listen_tcp(endpoint: &Endpoint) -> Result<TcpListener> {
+ let addr = SocketAddr::try_from(endpoint.clone())?;
+ let listener = TcpListener::bind(addr).await?;
Ok(listener)
}
diff --git a/net/src/transports/tls.rs b/net/src/transports/tls.rs
index 83f7a11..53b4566 100644
--- a/net/src/transports/tls.rs
+++ b/net/src/transports/tls.rs
@@ -1,4 +1,4 @@
-use std::sync::Arc;
+use std::{net::SocketAddr, sync::Arc};
use async_trait::async_trait;
use futures_rustls::{pki_types, rustls, TlsAcceptor, TlsConnector, TlsStream};
@@ -10,7 +10,7 @@ use smol::{
use crate::{
connection::{Connection, ToConn},
- endpoint::{Addr, Endpoint, Port},
+ endpoint::Endpoint,
listener::{ConnListener, ToListener},
Error, Result,
};
@@ -60,16 +60,15 @@ impl Connection for TlsConn {
/// Connects to the given TLS address and port.
pub async fn dial_tls(
- addr: &Addr,
- port: &Port,
+ endpoint: &Endpoint,
config: rustls::ClientConfig,
dns_name: &'static str,
) -> Result<TlsConn> {
- let address = format!("{}:{}", addr, port);
+ let addr = SocketAddr::try_from(endpoint.clone())?;
let connector = TlsConnector::from(Arc::new(config));
- let sock = TcpStream::connect(&address).await?;
+ let sock = TcpStream::connect(addr).await?;
sock.set_nodelay(true)?;
let altname = pki_types::ServerName::try_from(dns_name)?;
@@ -88,10 +87,11 @@ pub async fn dial(
_ => return Err(Error::InvalidEndpoint(endpoint.to_string())),
}
- dial_tls(endpoint.addr()?, endpoint.port()?, config, dns_name)
+ dial_tls(endpoint, config, dns_name)
.await
.map(|c| Box::new(c) as Box<dyn Connection>)
}
+
/// Tls network listener implementation of the `Listener` [`ConnListener`] trait.
pub struct TlsListener {
acceptor: TlsAcceptor,
@@ -113,32 +113,13 @@ impl ConnListener for TlsListener {
}
/// Listens on the given TLS address and port.
-pub async fn listen_tls(
- addr: &Addr,
- port: &Port,
- config: rustls::ServerConfig,
-) -> Result<TlsListener> {
- let address = format!("{}:{}", addr, port);
+pub async fn listen_tls(endpoint: &Endpoint, config: rustls::ServerConfig) -> Result<TlsListener> {
+ let addr = SocketAddr::try_from(endpoint.clone())?;
let acceptor = TlsAcceptor::from(Arc::new(config));
- let listener = TcpListener::bind(&address).await?;
+ let listener = TcpListener::bind(addr).await?;
Ok(TlsListener { acceptor, listener })
}
-/// Listens on the given TLS endpoint, returns `Listener` [`ConnListener`].
-pub async fn listen(
- endpoint: &Endpoint,
- config: rustls::ServerConfig,
-) -> Result<Box<dyn ConnListener>> {
- match endpoint {
- Endpoint::Tcp(..) | Endpoint::Tls(..) => {}
- _ => return Err(Error::InvalidEndpoint(endpoint.to_string())),
- }
-
- listen_tls(endpoint.addr()?, endpoint.port()?, config)
- .await
- .map(|l| Box::new(l) as Box<dyn ConnListener>)
-}
-
impl From<TlsStream<TcpStream>> for Box<dyn Connection> {
fn from(conn: TlsStream<TcpStream>) -> Self {
Box::new(TlsConn::new(conn.get_ref().0.clone(), conn))
diff --git a/net/src/transports/udp.rs b/net/src/transports/udp.rs
index a8b505c..991b1fd 100644
--- a/net/src/transports/udp.rs
+++ b/net/src/transports/udp.rs
@@ -5,7 +5,7 @@ use smol::net::UdpSocket;
use crate::{
connection::{Connection, ToConn},
- endpoint::{Addr, Endpoint, Port},
+ endpoint::Endpoint,
Error, Result,
};
@@ -57,19 +57,19 @@ impl Connection for UdpConn {
}
/// Connects to the given UDP address and port.
-pub async fn dial_udp(addr: &Addr, port: &Port) -> Result<UdpConn> {
- let address = format!("{}:{}", addr, port);
+pub async fn dial_udp(endpoint: &Endpoint) -> Result<UdpConn> {
+ let addr = SocketAddr::try_from(endpoint.clone())?;
// Let the operating system assign an available port to this socket
let conn = UdpSocket::bind("[::]:0").await?;
- conn.connect(address).await?;
+ conn.connect(addr).await?;
Ok(UdpConn::new(conn))
}
/// Listens on the given UDP address and port.
-pub async fn listen_udp(addr: &Addr, port: &Port) -> Result<UdpConn> {
- let address = format!("{}:{}", addr, port);
- let conn = UdpSocket::bind(address).await?;
+pub async fn listen_udp(endpoint: &Endpoint) -> Result<UdpConn> {
+ let addr = SocketAddr::try_from(endpoint.clone())?;
+ let conn = UdpSocket::bind(addr).await?;
let udp_conn = UdpConn::new(conn);
Ok(udp_conn)
}