From bbbfcfabcdac52613450a183179ad05d3c173bcc Mon Sep 17 00:00:00 2001 From: Paul Loyd Date: Fri, 10 Nov 2023 18:06:34 +0400 Subject: [PATCH] feat(network): resolve TCP addresses --- CHANGELOG.md | 3 +++ elfo-network/src/config.rs | 16 +++++----------- elfo-network/src/discovery/mod.rs | 1 + elfo-network/src/socket/raw/mod.rs | 4 ++-- elfo-network/src/socket/raw/tcp.rs | 6 +++--- examples/examples/network/alice.toml | 8 ++++---- examples/examples/network/bob.toml | 8 ++++---- 7 files changed, 22 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caf8de7e..cdf492a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - ReleaseDate +### Added +- network: DNS resolve for TCP transport. + ### Fixed - network: unrouted requests don't lead to infinite waiting anymore ([#116]). - network: release routed if flow closed. diff --git a/elfo-network/src/config.rs b/elfo-network/src/config.rs index bbff1e87..0ef805e0 100644 --- a/elfo-network/src/config.rs +++ b/elfo-network/src/config.rs @@ -1,9 +1,9 @@ #[cfg(unix)] use std::path::PathBuf; -use std::{net::SocketAddr, str::FromStr, time::Duration}; +use std::{str::FromStr, time::Duration}; use derive_more::Display; -use eyre::{bail, Result, WrapErr}; +use eyre::{bail, Result}; use serde::{ de::{self, Deserializer}, Deserialize, Serialize, @@ -51,7 +51,7 @@ fn default_attempt_interval() -> Duration { #[derive(Debug, Clone, Hash, PartialEq, Eq, Display, Serialize)] pub(crate) enum Transport { #[display(fmt = "tcp://{}", _0)] - Tcp(SocketAddr), + Tcp(String), #[cfg(unix)] #[display(fmt = "uds://{}", "_0.display()")] Uds(PathBuf), @@ -70,10 +70,7 @@ impl FromStr for Transport { match protocol { "" => bail!("protocol must be specified ({PROTOCOLS})"), - "tcp" => addr - .parse() - .map(Transport::Tcp) - .wrap_err("invalid TCP address"), + "tcp" => Ok(Transport::Tcp(addr.into())), #[cfg(unix)] "uds" => { eyre::ensure!( @@ -102,8 +99,6 @@ impl<'de> Deserialize<'de> for Transport { #[cfg(test)] mod tests { - use std::net::{IpAddr, Ipv4Addr}; - use super::*; #[test] @@ -130,10 +125,9 @@ mod tests { .starts_with("unknown protocol")); // TCP - let expected = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 4242); assert_eq!( Transport::from_str("tcp://127.0.0.1:4242").unwrap(), - Transport::Tcp(expected) + Transport::Tcp("127.0.0.1:4242".into()) ); assert_eq!( Transport::from_str("tcp://foobar").unwrap_err().to_string(), diff --git a/elfo-network/src/discovery/mod.rs b/elfo-network/src/discovery/mod.rs index 90d28012..b4cc15f2 100644 --- a/elfo-network/src/discovery/mod.rs +++ b/elfo-network/src/discovery/mod.rs @@ -316,6 +316,7 @@ impl Discovery { .into_iter() .for_each(|(local_group_no, remote_group_no)| { // TODO: save stream to cancel later. + // TODO: connect without DNS resolving here. self.open_connection( &transport, ConnectionRole::Data(internode::SwitchToData { diff --git a/elfo-network/src/socket/raw/mod.rs b/elfo-network/src/socket/raw/mod.rs index c9897b89..36d4d959 100644 --- a/elfo-network/src/socket/raw/mod.rs +++ b/elfo-network/src/socket/raw/mod.rs @@ -114,7 +114,7 @@ impl From for Socket { pub(super) async fn connect(addr: &Transport) -> Result { match addr { - Transport::Tcp(addr) => tcp::connect(*addr).await.map(Into::into), + Transport::Tcp(addr) => tcp::connect(addr).await.map(Into::into), #[cfg(unix)] Transport::Uds(addr) => uds::connect(addr).await.map(Into::into), } @@ -123,7 +123,7 @@ pub(super) async fn connect(addr: &Transport) -> Result { pub(super) async fn listen(addr: &Transport) -> Result + 'static> { match addr { Transport::Tcp(addr) => { - let result = tcp::listen(*addr).await.map(|s| s.map(Into::into)); + let result = tcp::listen(addr).await.map(|s| s.map(Into::into)); #[cfg(unix)] let result = result.map(Either::Left); result diff --git a/elfo-network/src/socket/raw/tcp.rs b/elfo-network/src/socket/raw/tcp.rs index 53f50452..4c06bec8 100644 --- a/elfo-network/src/socket/raw/tcp.rs +++ b/elfo-network/src/socket/raw/tcp.rs @@ -40,11 +40,11 @@ fn prepare_stream(stream: TcpStream) -> Result { Ok(Socket { read, write, info }) } -pub(super) async fn connect(addr: SocketAddr) -> Result { +pub(super) async fn connect(addr: &str) -> Result { prepare_stream(TcpStream::connect(addr).await?) } -pub(super) async fn listen(addr: SocketAddr) -> Result + 'static> { +pub(super) async fn listen(addr: &str) -> Result + 'static> { let listener = TcpListener::bind(addr).await?; let accept = move |listener: TcpListener| async move { @@ -61,7 +61,7 @@ pub(super) async fn listen(addr: SocketAddr) -> Result