From a79ca366abc013fc5049c11add1495be1743ec97 Mon Sep 17 00:00:00 2001 From: whtsht Date: Fri, 15 Sep 2023 16:44:41 +0900 Subject: [PATCH 01/17] refactor(dns): integrate `TokioDnsConfig` and `DnsConfig` into `Config` --- examples/dcutr/src/main.rs | 4 +- libp2p/src/lib.rs | 8 +-- misc/server/src/main.rs | 2 +- protocols/perf/src/bin/perf.rs | 2 +- transports/dns/Cargo.toml | 2 +- transports/dns/src/config.rs | 5 ++ transports/dns/src/config/async_std.rs | 30 +++++++++ transports/dns/src/config/tokio.rs | 28 +++++++++ transports/dns/src/lib.rs | 85 ++++++-------------------- transports/tcp/Cargo.toml | 1 + transports/websocket/src/lib.rs | 2 +- 11 files changed, 93 insertions(+), 76 deletions(-) create mode 100644 transports/dns/src/config.rs create mode 100644 transports/dns/src/config/async_std.rs create mode 100644 transports/dns/src/config/tokio.rs diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index e9e731b011b..7e286560207 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -34,7 +34,7 @@ use libp2p::{ upgrade, }, dcutr, - dns::DnsConfig, + dns::async_std::Config, identify, identity, noise, ping, quic, relay, swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}, tcp, yamux, PeerId, @@ -102,7 +102,7 @@ fn main() -> Result<(), Box> { &local_key, ))); - block_on(DnsConfig::system(relay_tcp_quic_transport)) + block_on(Config::system(relay_tcp_quic_transport)) .unwrap() .map(|either_output, _| match either_output { Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), diff --git a/libp2p/src/lib.rs b/libp2p/src/lib.rs index 624ff897c17..5906579f376 100644 --- a/libp2p/src/lib.rs +++ b/libp2p/src/lib.rs @@ -189,12 +189,12 @@ pub async fn development_transport( keypair: identity::Keypair, ) -> std::io::Result> { let transport = { - let dns_tcp = dns::DnsConfig::system(tcp::async_io::Transport::new( + let dns_tcp = dns::async_std::Config::system(tcp::async_io::Transport::new( tcp::Config::new().nodelay(true), )) .await?; let ws_dns_tcp = websocket::WsConfig::new( - dns::DnsConfig::system(tcp::async_io::Transport::new( + dns::async_std::Config::system(tcp::async_io::Transport::new( tcp::Config::new().nodelay(true), )) .await?, @@ -234,10 +234,10 @@ pub fn tokio_development_transport( keypair: identity::Keypair, ) -> std::io::Result> { let transport = { - let dns_tcp = dns::TokioDnsConfig::system(tcp::tokio::Transport::new( + let dns_tcp = dns::tokio::Config::system(tcp::tokio::Transport::new( tcp::Config::new().nodelay(true), ))?; - let ws_dns_tcp = websocket::WsConfig::new(dns::TokioDnsConfig::system( + let ws_dns_tcp = websocket::WsConfig::new(dns::tokio::Config::system( tcp::tokio::Transport::new(tcp::Config::new().nodelay(true)), )?); dns_tcp.or_transport(ws_dns_tcp) diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index 26737085d91..6e856934ae9 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -88,7 +88,7 @@ async fn main() -> Result<(), Box> { let quic_transport = quic::tokio::Transport::new(quic::Config::new(&local_keypair)); - dns::TokioDnsConfig::system(libp2p::core::transport::OrTransport::new( + dns::tokio::Config::system(libp2p::core::transport::OrTransport::new( quic_transport, tcp_transport, ))? diff --git a/protocols/perf/src/bin/perf.rs b/protocols/perf/src/bin/perf.rs index b6b090608f5..0a34c0963bd 100644 --- a/protocols/perf/src/bin/perf.rs +++ b/protocols/perf/src/bin/perf.rs @@ -405,7 +405,7 @@ async fn swarm() -> Result> { libp2p_quic::tokio::Transport::new(config) }; - let dns = libp2p_dns::TokioDnsConfig::system(OrTransport::new(quic, tcp))?; + let dns = libp2p_dns::tokio::Config::system(OrTransport::new(quic, tcp))?; dns.map(|either_output, _| match either_output { Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index 77c8e57e3ff..bd002c35e25 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -35,7 +35,7 @@ tokio = ["trust-dns-resolver/tokio-runtime"] tokio-dns-over-rustls = ["tokio", "trust-dns-resolver/dns-over-rustls"] tokio-dns-over-https-rustls = ["tokio", "trust-dns-resolver/dns-over-https-rustls"] -# Passing arguments to the docsrs builder in order to properly document cfg's. +# Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true diff --git a/transports/dns/src/config.rs b/transports/dns/src/config.rs new file mode 100644 index 00000000000..006f7c359db --- /dev/null +++ b/transports/dns/src/config.rs @@ -0,0 +1,5 @@ +#[cfg(feature = "async-std")] +pub mod async_std; + +#[cfg(feature = "tokio")] +pub mod tokio; diff --git a/transports/dns/src/config/async_std.rs b/transports/dns/src/config/async_std.rs new file mode 100644 index 00000000000..780ee4ed38f --- /dev/null +++ b/transports/dns/src/config/async_std.rs @@ -0,0 +1,30 @@ +use std::{io, sync::Arc}; + +use async_std_resolver::AsyncStdResolver; +use parking_lot::Mutex; +use trust_dns_resolver::{ + config::{ResolverConfig, ResolverOpts}, + system_conf, +}; + +pub type Config = crate::Config; + +impl Config { + /// Creates a new [`DnsConfig`] from the OS's DNS configuration and defaults. + pub async fn system(inner: T) -> Result, io::Error> { + let (cfg, opts) = system_conf::read_system_conf()?; + Self::custom(inner, cfg, opts).await + } + + /// Creates a [`DnsConfig`] with a custom resolver configuration and options. + pub async fn custom( + inner: T, + cfg: ResolverConfig, + opts: ResolverOpts, + ) -> Result, io::Error> { + Ok(Config { + inner: Arc::new(Mutex::new(inner)), + resolver: async_std_resolver::resolver(cfg, opts).await, + }) + } +} diff --git a/transports/dns/src/config/tokio.rs b/transports/dns/src/config/tokio.rs new file mode 100644 index 00000000000..0ac39eea549 --- /dev/null +++ b/transports/dns/src/config/tokio.rs @@ -0,0 +1,28 @@ +use std::sync::Arc; + +use parking_lot::Mutex; +use trust_dns_resolver::{system_conf, TokioAsyncResolver}; + +pub type Config = crate::Config; + +impl Config { + /// Creates a new [`TokioDnsConfig`] from the OS's DNS configuration and defaults. + pub fn system(inner: T) -> Result, std::io::Error> { + let (cfg, opts) = system_conf::read_system_conf()?; + Self::custom(inner, cfg, opts) + } + + /// Creates a [`TokioDnsConfig`] with a custom resolver configuration + /// and options. + pub fn custom( + inner: T, + cfg: trust_dns_resolver::config::ResolverConfig, + opts: trust_dns_resolver::config::ResolverOpts, + ) -> Result, std::io::Error> { + // TODO: Make infallible in next breaking release. Or deprecation? + Ok(Config { + inner: Arc::new(Mutex::new(inner)), + resolver: TokioAsyncResolver::tokio(cfg, opts), + }) + } +} diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 61d1f4fed91..13e22438fc2 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -57,8 +57,14 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +mod config; + #[cfg(feature = "async-std")] -use async_std_resolver::AsyncStdResolver; +pub use config::async_std; + +#[cfg(feature = "tokio")] +pub use config::tokio; + use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{ @@ -80,10 +86,6 @@ use std::{ sync::Arc, task::{Context, Poll}, }; -#[cfg(any(feature = "async-std", feature = "tokio"))] -use trust_dns_resolver::system_conf; -#[cfg(feature = "tokio")] -use trust_dns_resolver::TokioAsyncResolver; pub use trust_dns_resolver::config::{ResolverConfig, ResolverOpts}; pub use trust_dns_resolver::error::{ResolveError, ResolveErrorKind}; @@ -112,75 +114,24 @@ const MAX_TXT_RECORDS: usize = 16; /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses /// using `async-std` for all async I/O. -#[cfg(feature = "async-std")] -pub type DnsConfig = GenDnsConfig; +// #[cfg(feature = "async-std")] +// pub type DnsConfig = Config; /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses /// using `tokio` for all async I/O. -#[cfg(feature = "tokio")] -pub type TokioDnsConfig = GenDnsConfig; +// #[cfg(feature = "tokio")] +// pub type TokioDnsConfig = Config; /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses. #[derive(Debug)] -pub struct GenDnsConfig { +pub struct Config { /// The underlying transport. inner: Arc>, /// The DNS resolver used when dialing addresses with DNS components. resolver: R, } -#[cfg(feature = "async-std")] -impl DnsConfig -where - T: Send, -{ - /// Creates a new [`DnsConfig`] from the OS's DNS configuration and defaults. - pub async fn system(inner: T) -> Result, io::Error> { - let (cfg, opts) = system_conf::read_system_conf()?; - Self::custom(inner, cfg, opts).await - } - - /// Creates a [`DnsConfig`] with a custom resolver configuration and options. - pub async fn custom( - inner: T, - cfg: ResolverConfig, - opts: ResolverOpts, - ) -> Result, io::Error> { - // TODO: Make infallible in next breaking release. Or deprecation? - Ok(DnsConfig { - inner: Arc::new(Mutex::new(inner)), - resolver: async_std_resolver::resolver(cfg, opts).await, - }) - } -} - -#[cfg(feature = "tokio")] -impl TokioDnsConfig -where - T: Send, -{ - /// Creates a new [`TokioDnsConfig`] from the OS's DNS configuration and defaults. - pub fn system(inner: T) -> Result, io::Error> { - let (cfg, opts) = system_conf::read_system_conf()?; - Self::custom(inner, cfg, opts) - } - - /// Creates a [`TokioDnsConfig`] with a custom resolver configuration - /// and options. - pub fn custom( - inner: T, - cfg: ResolverConfig, - opts: ResolverOpts, - ) -> Result, io::Error> { - // TODO: Make infallible in next breaking release. Or deprecation? - Ok(TokioDnsConfig { - inner: Arc::new(Mutex::new(inner)), - resolver: TokioAsyncResolver::tokio(cfg, opts), - }) - } -} - -impl Transport for GenDnsConfig +impl Transport for Config where T: Transport + Send + Unpin + 'static, T::Error: Send, @@ -238,7 +189,7 @@ where } } -impl GenDnsConfig +impl Config where T: Transport + Send + Unpin + 'static, T::Error: Send, @@ -664,7 +615,7 @@ mod tests { } } - async fn run(mut transport: GenDnsConfig) + async fn run(mut transport: Config) where T: Transport + Clone + Send + Unpin + 'static, T::Error: Send, @@ -746,7 +697,8 @@ mod tests { let config = ResolverConfig::quad9(); let opts = ResolverOpts::default(); async_std_crate::task::block_on( - DnsConfig::custom(CustomTransport, config, opts).then(|dns| run(dns.unwrap())), + async_std::Config::custom(CustomTransport, config, opts) + .then(|dns| run(dns.unwrap())), ); } @@ -761,8 +713,9 @@ mod tests { .enable_time() .build() .unwrap(); + rt.block_on(run( - TokioDnsConfig::custom(CustomTransport, config, opts).unwrap() + tokio::Config::custom(CustomTransport, config, opts).unwrap() )); } } diff --git a/transports/tcp/Cargo.toml b/transports/tcp/Cargo.toml index 1714debada5..ffee29c54c6 100644 --- a/transports/tcp/Cargo.toml +++ b/transports/tcp/Cargo.toml @@ -35,5 +35,6 @@ env_logger = "0.10.0" # More information: https://docs.rs/about/builds#cross-compiling [package.metadata.docs.rs] all-features = true + rustdoc-args = ["--cfg", "docsrs"] rustc-args = ["--cfg", "docsrs"] diff --git a/transports/websocket/src/lib.rs b/transports/websocket/src/lib.rs index 01c02b15320..eae5f007583 100644 --- a/transports/websocket/src/lib.rs +++ b/transports/websocket/src/lib.rs @@ -74,7 +74,7 @@ use std::{ /// # #[async_std::main] /// # async fn main() { /// -/// let mut transport = websocket::WsConfig::new(dns::DnsConfig::system( +/// let mut transport = websocket::WsConfig::new(dns::async_std::Config::system( /// tcp::async_io::Transport::new(tcp::Config::default()), /// ).await.unwrap()); /// From cb691fddc95f12403c7a0c8ab7a9fe1a83e92fbc Mon Sep 17 00:00:00 2001 From: whtsht Date: Fri, 15 Sep 2023 20:01:42 +0900 Subject: [PATCH 02/17] docs(dns): rename `TokioDnsConfig` and `DnsConfig` into `Config` --- transports/dns/src/config/async_std.rs | 6 ++++-- transports/dns/src/config/tokio.rs | 6 ++++-- transports/dns/src/lib.rs | 23 +++++++---------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/transports/dns/src/config/async_std.rs b/transports/dns/src/config/async_std.rs index 780ee4ed38f..184223fbe73 100644 --- a/transports/dns/src/config/async_std.rs +++ b/transports/dns/src/config/async_std.rs @@ -7,16 +7,18 @@ use trust_dns_resolver::{ system_conf, }; +/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses +/// using `async-std` for all async I/O. pub type Config = crate::Config; impl Config { - /// Creates a new [`DnsConfig`] from the OS's DNS configuration and defaults. + /// Creates a new [`Config`] from the OS's DNS configuration and defaults. pub async fn system(inner: T) -> Result, io::Error> { let (cfg, opts) = system_conf::read_system_conf()?; Self::custom(inner, cfg, opts).await } - /// Creates a [`DnsConfig`] with a custom resolver configuration and options. + /// Creates a [`Config`] with a custom resolver configuration and options. pub async fn custom( inner: T, cfg: ResolverConfig, diff --git a/transports/dns/src/config/tokio.rs b/transports/dns/src/config/tokio.rs index 0ac39eea549..9d4e5f2828a 100644 --- a/transports/dns/src/config/tokio.rs +++ b/transports/dns/src/config/tokio.rs @@ -3,16 +3,18 @@ use std::sync::Arc; use parking_lot::Mutex; use trust_dns_resolver::{system_conf, TokioAsyncResolver}; +/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses +/// using `tokio` for all async I/O. pub type Config = crate::Config; impl Config { - /// Creates a new [`TokioDnsConfig`] from the OS's DNS configuration and defaults. + /// Creates a new [`Config`] from the OS's DNS configuration and defaults. pub fn system(inner: T) -> Result, std::io::Error> { let (cfg, opts) = system_conf::read_system_conf()?; Self::custom(inner, cfg, opts) } - /// Creates a [`TokioDnsConfig`] with a custom resolver configuration + /// Creates a [`Config`] with a custom resolver configuration /// and options. pub fn custom( inner: T, diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 13e22438fc2..e12eca5eab2 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -21,17 +21,17 @@ //! # [DNS name resolution](https://github.com/libp2p/specs/blob/master/addressing/README.md#ip-and-name-resolution) //! [`Transport`] for libp2p. //! -//! This crate provides the type [`GenDnsConfig`] with its instantiations -//! [`DnsConfig`] and `TokioDnsConfig` for use with `async-std` and `tokio`, +//! This crate provides the type [`async_std::Config`] and [`tokio::Config`] +//! for use with `async-std` and `tokio`, //! respectively. //! -//! A [`GenDnsConfig`] is an address-rewriting [`Transport`] wrapper around +//! A [`Config`] is an address-rewriting [`Transport`] wrapper around //! an inner `Transport`. The composed transport behaves like the inner //! transport, except that [`Transport::dial`] resolves `/dns/...`, `/dns4/...`, //! `/dns6/...` and `/dnsaddr/...` components of the given `Multiaddr` through //! a DNS, replacing them with the resolved protocols (typically TCP/IP). //! -//! The `async-std` feature and hence the `DnsConfig` are +//! The `async-std` feature and hence the `async_std::Config` are //! enabled by default. Tokio users can furthermore opt-in //! to the `tokio-dns-over-rustls` and `tokio-dns-over-https-rustls` //! features. For more information about these features, please @@ -49,7 +49,7 @@ //! problematic on platforms like Android, where there's a lot of //! complexity hidden behind the system APIs. //! If the implementation requires different characteristics, one should -//! consider providing their own implementation of [`GenDnsConfig`] or use +//! consider providing their own implementation of [`Config`] or use //! platform specific APIs to extract the host's DNS configuration (if possible) //! and provide a custom [`ResolverConfig`]. //! @@ -112,17 +112,8 @@ const MAX_DNS_LOOKUPS: usize = 32; /// result of a single `/dnsaddr` lookup. const MAX_TXT_RECORDS: usize = 16; -/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses -/// using `async-std` for all async I/O. -// #[cfg(feature = "async-std")] -// pub type DnsConfig = Config; - -/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses -/// using `tokio` for all async I/O. -// #[cfg(feature = "tokio")] -// pub type TokioDnsConfig = Config; - /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses. +/// You shouldn't need to use this type directly. Use [`tokio::Config`] or [`async_std::Config`] instead. #[derive(Debug)] pub struct Config { /// The underlying transport. @@ -336,7 +327,7 @@ where } } -/// The possible errors of a [`GenDnsConfig`] wrapped transport. +/// The possible errors of a [`Config`] wrapped transport. #[derive(Debug)] #[allow(clippy::large_enum_variant)] pub enum DnsErr { From bac10bf2042f5fd90daf4b9453a94a07e39f60c2 Mon Sep 17 00:00:00 2001 From: whtsht Date: Fri, 15 Sep 2023 20:24:29 +0900 Subject: [PATCH 03/17] docs(dns): increment version number --- Cargo.toml | 2 +- transports/dns/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9b703d9c4ee..23f601cfcde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ libp2p-connection-limits = { version = "0.2.1", path = "misc/connection-limits" libp2p-core = { version = "0.40.1", path = "core" } libp2p-dcutr = { version = "0.10.0", path = "protocols/dcutr" } libp2p-deflate = { version = "0.40.0", path = "transports/deflate" } -libp2p-dns = { version = "0.40.0", path = "transports/dns" } +libp2p-dns = { version = "0.41.0", path = "transports/dns" } libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" } libp2p-identify = { version = "0.43.0", path = "protocols/identify" } diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index bd002c35e25..d5544ba52f9 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-dns" edition = "2021" rust-version = { workspace = true } description = "DNS transport implementation for libp2p" -version = "0.40.0" +version = "0.41.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From f8468c551252350ee8f23480e8e55e0f348aa113 Mon Sep 17 00:00:00 2001 From: whtsht Date: Fri, 15 Sep 2023 22:07:57 +0900 Subject: [PATCH 04/17] docs(dns): update CHANGELOG.md --- transports/dns/CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index a51e4165c18..8add7f7a9b5 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.41.0 + +- Rename types with `Dns` prefix (`TokioDnsConfig`, and `DnsConfig`) +- Integrate `GenDnsConfig` into `Config` + See [PR 4505]. + +[PR 4505]: https://github.com/libp2p/rust-libp2p/pull/4505 + ## 0.40.0 - Raise MSRV to 1.65. From defa4fba55c1dc90925c3ff06bf2f965337999a8 Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 17 Sep 2023 10:37:01 +0900 Subject: [PATCH 05/17] docs(dns): update Cargo.lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 342f17d6a37..c990d011d0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2535,7 +2535,7 @@ dependencies = [ [[package]] name = "libp2p-dns" -version = "0.40.0" +version = "0.41.0" dependencies = [ "async-std", "async-std-resolver", From 9e515abeb22d60ef743b7d298d1b99080408f875 Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 17 Sep 2023 10:37:15 +0900 Subject: [PATCH 06/17] refactor(dns): rename `DnsErr` to `Error` --- transports/dns/src/lib.rs | 54 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index e12eca5eab2..28f3c3dcbfd 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -130,7 +130,7 @@ where R: Clone + Send + Sync + Resolver + 'static, { type Output = T::Output; - type Error = DnsErr; + type Error = Error; type ListenerUpgrade = future::MapErr Self::Error>; type Dial = future::Either< future::MapErr Self::Error>, @@ -145,7 +145,7 @@ where self.inner .lock() .listen_on(id, addr) - .map_err(|e| e.map(DnsErr::Transport)) + .map_err(|e| e.map(Error::Transport)) } fn remove_listener(&mut self, id: ListenerId) -> bool { @@ -174,8 +174,8 @@ where let mut inner = self.inner.lock(); Transport::poll(Pin::new(inner.deref_mut()), cx).map(|event| { event - .map_upgrade(|upgr| upgr.map_err::<_, fn(_) -> _>(DnsErr::Transport)) - .map_err(DnsErr::Transport) + .map_upgrade(|upgr| upgr.map_err::<_, fn(_) -> _>(Error::Transport)) + .map_err(Error::Transport) }) } } @@ -221,7 +221,7 @@ where }) { if dns_lookups == MAX_DNS_LOOKUPS { log::debug!("Too many DNS lookups. Dropping unresolved {}.", addr); - last_err = Some(DnsErr::TooManyLookups); + last_err = Some(Error::TooManyLookups); // There may still be fully resolved addresses in `unresolved`, // so keep going until `unresolved` is empty. continue; @@ -286,12 +286,12 @@ where // actually accepted, i.e. for which it produced // a dialing future. dial_attempts += 1; - out.await.map_err(DnsErr::Transport) + out.await.map_err(Error::Transport) } Err(TransportError::MultiaddrNotSupported(a)) => { - Err(DnsErr::MultiaddrNotSupported(a)) + Err(Error::MultiaddrNotSupported(a)) } - Err(TransportError::Other(err)) => Err(DnsErr::Transport(err)), + Err(TransportError::Other(err)) => Err(Error::Transport(err)), }; match result { @@ -319,7 +319,7 @@ where // for the given address to begin with (i.e. DNS lookups succeeded but // produced no records relevant for the given `addr`). Err(last_err.unwrap_or_else(|| { - DnsErr::ResolveError(ResolveErrorKind::Message("No matching records found.").into()) + Error::ResolveError(ResolveErrorKind::Message("No matching records found.").into()) })) } .boxed() @@ -330,7 +330,7 @@ where /// The possible errors of a [`Config`] wrapped transport. #[derive(Debug)] #[allow(clippy::large_enum_variant)] -pub enum DnsErr { +pub enum Error { /// The underlying transport encountered an error. Transport(TErr), /// DNS resolution failed. @@ -346,30 +346,30 @@ pub enum DnsErr { TooManyLookups, } -impl fmt::Display for DnsErr +impl fmt::Display for Error where TErr: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - DnsErr::Transport(err) => write!(f, "{err}"), - DnsErr::ResolveError(err) => write!(f, "{err}"), - DnsErr::MultiaddrNotSupported(a) => write!(f, "Unsupported resolved address: {a}"), - DnsErr::TooManyLookups => write!(f, "Too many DNS lookups"), + Error::Transport(err) => write!(f, "{err}"), + Error::ResolveError(err) => write!(f, "{err}"), + Error::MultiaddrNotSupported(a) => write!(f, "Unsupported resolved address: {a}"), + Error::TooManyLookups => write!(f, "Too many DNS lookups"), } } } -impl error::Error for DnsErr +impl error::Error for Error where TErr: error::Error + 'static, { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { - DnsErr::Transport(err) => Some(err), - DnsErr::ResolveError(err) => Some(err), - DnsErr::MultiaddrNotSupported(_) => None, - DnsErr::TooManyLookups => None, + Error::Transport(err) => Some(err), + Error::ResolveError(err) => Some(err), + Error::MultiaddrNotSupported(_) => None, + Error::TooManyLookups => None, } } } @@ -395,7 +395,7 @@ enum Resolved<'a> { fn resolve<'a, E: 'a + Send, R: Resolver>( proto: &Protocol<'a>, resolver: &'a R, -) -> BoxFuture<'a, Result, DnsErr>> { +) -> BoxFuture<'a, Result, Error>> { match proto { Protocol::Dns(ref name) => resolver .lookup_ip(name.clone().into_owned()) @@ -417,7 +417,7 @@ fn resolve<'a, E: 'a + Send, R: Resolver>( Ok(Resolved::One(Protocol::from(one))) } } - Err(e) => Err(DnsErr::ResolveError(e)), + Err(e) => Err(Error::ResolveError(e)), }) .boxed(), Protocol::Dns4(ref name) => resolver @@ -441,7 +441,7 @@ fn resolve<'a, E: 'a + Send, R: Resolver>( Ok(Resolved::One(Protocol::from(Ipv4Addr::from(one)))) } } - Err(e) => Err(DnsErr::ResolveError(e)), + Err(e) => Err(Error::ResolveError(e)), }) .boxed(), Protocol::Dns6(ref name) => resolver @@ -465,7 +465,7 @@ fn resolve<'a, E: 'a + Send, R: Resolver>( Ok(Resolved::One(Protocol::from(Ipv6Addr::from(one)))) } } - Err(e) => Err(DnsErr::ResolveError(e)), + Err(e) => Err(Error::ResolveError(e)), }) .boxed(), Protocol::Dnsaddr(ref name) => { @@ -490,7 +490,7 @@ fn resolve<'a, E: 'a + Send, R: Resolver>( } Ok(Resolved::Addrs(addrs)) } - Err(e) => Err(DnsErr::ResolveError(e)), + Err(e) => Err(Error::ResolveError(e)), }) .boxed() } @@ -661,7 +661,7 @@ mod tests { .unwrap() .await { - Err(DnsErr::ResolveError(_)) => {} + Err(Error::ResolveError(_)) => {} Err(e) => panic!("Unexpected error: {e:?}"), Ok(_) => panic!("Unexpected success."), } @@ -672,7 +672,7 @@ mod tests { .unwrap() .await { - Err(DnsErr::ResolveError(e)) => match e.kind() { + Err(Error::ResolveError(e)) => match e.kind() { ResolveErrorKind::NoRecordsFound { .. } => {} _ => panic!("Unexpected DNS error: {e:?}"), }, From a9ba9a2fb042091d2ded6d9ce0756c97bd68cc57 Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 17 Sep 2023 11:13:02 +0900 Subject: [PATCH 07/17] refactor(dns): add type alias for non-breaking changes --- transports/dns/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 28f3c3dcbfd..ee02230998b 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -62,9 +62,17 @@ mod config; #[cfg(feature = "async-std")] pub use config::async_std; +#[cfg(feature = "async-std")] +#[deprecated(since = "0.40.0", note = "Use `async_std::Config` instead.")] +pub type DnsConfig = async_std::Config; + #[cfg(feature = "tokio")] pub use config::tokio; +#[cfg(feature = "tokio")] +#[deprecated(since = "0.40.0", note = "Use `tokio::Config` instead.")] +pub type TokioDnsConfig = tokio::Config; + use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; use libp2p_core::{ @@ -122,6 +130,12 @@ pub struct Config { resolver: R, } +#[deprecated( + since = "0.40.0", + note = "Use `async_std::Config` or `tokio::Config` instead." +)] +pub type GenDnsConfig = Config; + impl Transport for Config where T: Transport + Send + Unpin + 'static, @@ -346,6 +360,9 @@ pub enum Error { TooManyLookups, } +#[deprecated(since = "0.40.0", note = "Use `Error` instead.")] +pub type DnsError = Error; + impl fmt::Display for Error where TErr: fmt::Display, From b22f1d1be7b8e83254b82183360b80cce5674e53 Mon Sep 17 00:00:00 2001 From: whtsht <85547207+whtsht@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:52:39 +0900 Subject: [PATCH 08/17] docs(dns): update CHANGELOG.md Co-authored-by: Thomas Eizinger --- transports/dns/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index 8add7f7a9b5..9c57b2bf35b 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.41.0 +## 0.40.1 - unreleased - Rename types with `Dns` prefix (`TokioDnsConfig`, and `DnsConfig`) - Integrate `GenDnsConfig` into `Config` From c79558d9cd04a994b65a9427b2df9c155af7f8f7 Mon Sep 17 00:00:00 2001 From: whtsht Date: Thu, 21 Sep 2023 10:56:47 +0900 Subject: [PATCH 09/17] docs(dns): changed version from `0.41.0` to `0.40.1` --- Cargo.lock | 2 +- Cargo.toml | 2 +- transports/dns/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c990d011d0b..405e2db2f7b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2535,7 +2535,7 @@ dependencies = [ [[package]] name = "libp2p-dns" -version = "0.41.0" +version = "0.40.1" dependencies = [ "async-std", "async-std-resolver", diff --git a/Cargo.toml b/Cargo.toml index 23f601cfcde..9685bd42d5f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,7 +78,7 @@ libp2p-connection-limits = { version = "0.2.1", path = "misc/connection-limits" libp2p-core = { version = "0.40.1", path = "core" } libp2p-dcutr = { version = "0.10.0", path = "protocols/dcutr" } libp2p-deflate = { version = "0.40.0", path = "transports/deflate" } -libp2p-dns = { version = "0.41.0", path = "transports/dns" } +libp2p-dns = { version = "0.40.1", path = "transports/dns" } libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.45.1", path = "protocols/gossipsub" } libp2p-identify = { version = "0.43.0", path = "protocols/identify" } diff --git a/transports/dns/Cargo.toml b/transports/dns/Cargo.toml index d5544ba52f9..7c7bd439f41 100644 --- a/transports/dns/Cargo.toml +++ b/transports/dns/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-dns" edition = "2021" rust-version = { workspace = true } description = "DNS transport implementation for libp2p" -version = "0.41.0" +version = "0.40.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 3e24659afabfb914dd6bcbc5546ca22094c405f5 Mon Sep 17 00:00:00 2001 From: whtsht Date: Thu, 21 Sep 2023 11:00:10 +0900 Subject: [PATCH 10/17] docs(dns): remove since attribute --- transports/dns/src/lib.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index ee02230998b..ae7975b01c8 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -63,14 +63,14 @@ mod config; pub use config::async_std; #[cfg(feature = "async-std")] -#[deprecated(since = "0.40.0", note = "Use `async_std::Config` instead.")] +#[deprecated(note = "Use `async_std::Config` instead.")] pub type DnsConfig = async_std::Config; #[cfg(feature = "tokio")] pub use config::tokio; #[cfg(feature = "tokio")] -#[deprecated(since = "0.40.0", note = "Use `tokio::Config` instead.")] +#[deprecated(note = "Use `tokio::Config` instead.")] pub type TokioDnsConfig = tokio::Config; use async_trait::async_trait; @@ -130,10 +130,7 @@ pub struct Config { resolver: R, } -#[deprecated( - since = "0.40.0", - note = "Use `async_std::Config` or `tokio::Config` instead." -)] +#[deprecated(note = "Use `async_std::Config` or `tokio::Config` instead.")] pub type GenDnsConfig = Config; impl Transport for Config @@ -360,7 +357,7 @@ pub enum Error { TooManyLookups, } -#[deprecated(since = "0.40.0", note = "Use `Error` instead.")] +#[deprecated(note = "Use `Error` instead.")] pub type DnsError = Error; impl fmt::Display for Error From d348833f18ded46a486469465fa8efd255e181d9 Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 24 Sep 2023 09:34:30 +0900 Subject: [PATCH 11/17] refactor(dns): make inline modules --- transports/dns/src/config.rs | 5 -- transports/dns/src/config/async_std.rs | 32 ------------ transports/dns/src/config/tokio.rs | 30 ------------ transports/dns/src/lib.rs | 68 ++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 71 deletions(-) delete mode 100644 transports/dns/src/config.rs delete mode 100644 transports/dns/src/config/async_std.rs delete mode 100644 transports/dns/src/config/tokio.rs diff --git a/transports/dns/src/config.rs b/transports/dns/src/config.rs deleted file mode 100644 index 006f7c359db..00000000000 --- a/transports/dns/src/config.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[cfg(feature = "async-std")] -pub mod async_std; - -#[cfg(feature = "tokio")] -pub mod tokio; diff --git a/transports/dns/src/config/async_std.rs b/transports/dns/src/config/async_std.rs deleted file mode 100644 index 184223fbe73..00000000000 --- a/transports/dns/src/config/async_std.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::{io, sync::Arc}; - -use async_std_resolver::AsyncStdResolver; -use parking_lot::Mutex; -use trust_dns_resolver::{ - config::{ResolverConfig, ResolverOpts}, - system_conf, -}; - -/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses -/// using `async-std` for all async I/O. -pub type Config = crate::Config; - -impl Config { - /// Creates a new [`Config`] from the OS's DNS configuration and defaults. - pub async fn system(inner: T) -> Result, io::Error> { - let (cfg, opts) = system_conf::read_system_conf()?; - Self::custom(inner, cfg, opts).await - } - - /// Creates a [`Config`] with a custom resolver configuration and options. - pub async fn custom( - inner: T, - cfg: ResolverConfig, - opts: ResolverOpts, - ) -> Result, io::Error> { - Ok(Config { - inner: Arc::new(Mutex::new(inner)), - resolver: async_std_resolver::resolver(cfg, opts).await, - }) - } -} diff --git a/transports/dns/src/config/tokio.rs b/transports/dns/src/config/tokio.rs deleted file mode 100644 index 9d4e5f2828a..00000000000 --- a/transports/dns/src/config/tokio.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::sync::Arc; - -use parking_lot::Mutex; -use trust_dns_resolver::{system_conf, TokioAsyncResolver}; - -/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses -/// using `tokio` for all async I/O. -pub type Config = crate::Config; - -impl Config { - /// Creates a new [`Config`] from the OS's DNS configuration and defaults. - pub fn system(inner: T) -> Result, std::io::Error> { - let (cfg, opts) = system_conf::read_system_conf()?; - Self::custom(inner, cfg, opts) - } - - /// Creates a [`Config`] with a custom resolver configuration - /// and options. - pub fn custom( - inner: T, - cfg: trust_dns_resolver::config::ResolverConfig, - opts: trust_dns_resolver::config::ResolverOpts, - ) -> Result, std::io::Error> { - // TODO: Make infallible in next breaking release. Or deprecation? - Ok(Config { - inner: Arc::new(Mutex::new(inner)), - resolver: TokioAsyncResolver::tokio(cfg, opts), - }) - } -} diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index ae7975b01c8..14ccc29ad2d 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -57,17 +57,77 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -mod config; - #[cfg(feature = "async-std")] -pub use config::async_std; +pub mod async_std { + use async_std_resolver::AsyncStdResolver; + use parking_lot::Mutex; + use std::{io, sync::Arc}; + use trust_dns_resolver::{ + config::{ResolverConfig, ResolverOpts}, + system_conf, + }; + + /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses + /// using `async-std` for all async I/O. + pub type Config = crate::Config; + + impl Config { + /// Creates a new [`Config`] from the OS's DNS configuration and defaults. + pub async fn system(inner: T) -> Result, io::Error> { + let (cfg, opts) = system_conf::read_system_conf()?; + Self::custom(inner, cfg, opts).await + } + + /// Creates a [`Config`] with a custom resolver configuration and options. + pub async fn custom( + inner: T, + cfg: ResolverConfig, + opts: ResolverOpts, + ) -> Result, io::Error> { + Ok(Config { + inner: Arc::new(Mutex::new(inner)), + resolver: async_std_resolver::resolver(cfg, opts).await, + }) + } + } +} #[cfg(feature = "async-std")] #[deprecated(note = "Use `async_std::Config` instead.")] pub type DnsConfig = async_std::Config; #[cfg(feature = "tokio")] -pub use config::tokio; +pub mod tokio { + use parking_lot::Mutex; + use std::sync::Arc; + use trust_dns_resolver::{system_conf, TokioAsyncResolver}; + + /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses + /// using `tokio` for all async I/O. + pub type Config = crate::Config; + + impl Config { + /// Creates a new [`Config`] from the OS's DNS configuration and defaults. + pub fn system(inner: T) -> Result, std::io::Error> { + let (cfg, opts) = system_conf::read_system_conf()?; + Self::custom(inner, cfg, opts) + } + + /// Creates a [`Config`] with a custom resolver configuration + /// and options. + pub fn custom( + inner: T, + cfg: trust_dns_resolver::config::ResolverConfig, + opts: trust_dns_resolver::config::ResolverOpts, + ) -> Result, std::io::Error> { + // TODO: Make infallible in next breaking release. Or deprecation? + Ok(Config { + inner: Arc::new(Mutex::new(inner)), + resolver: TokioAsyncResolver::tokio(cfg, opts), + }) + } + } +} #[cfg(feature = "tokio")] #[deprecated(note = "Use `tokio::Config` instead.")] From f8c17e1c68ef29936ade166429a6aa60681c7a45 Mon Sep 17 00:00:00 2001 From: whtsht <85547207+whtsht@users.noreply.github.com> Date: Sun, 24 Sep 2023 09:40:33 +0900 Subject: [PATCH 12/17] refactor(dns): rename `Config` to `Transport` refactor(dns): rename `Config` to `Transport` Co-authored-by: Thomas Eizinger update --- examples/dcutr/src/main.rs | 6 ++-- libp2p/src/lib.rs | 8 ++--- misc/server/src/main.rs | 2 +- protocols/perf/src/bin/perf.rs | 2 +- transports/dns/src/lib.rs | 56 +++++++++++++++++---------------- transports/websocket/src/lib.rs | 2 +- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 7e286560207..ec308521874 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -33,9 +33,7 @@ use libp2p::{ transport::Transport, upgrade, }, - dcutr, - dns::async_std::Config, - identify, identity, noise, ping, quic, relay, + dcutr, dns, identify, identity, noise, ping, quic, relay, swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}, tcp, yamux, PeerId, }; @@ -102,7 +100,7 @@ fn main() -> Result<(), Box> { &local_key, ))); - block_on(Config::system(relay_tcp_quic_transport)) + block_on(dns::async_std::Transport::system(relay_tcp_quic_transport)) .unwrap() .map(|either_output, _| match either_output { Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), diff --git a/libp2p/src/lib.rs b/libp2p/src/lib.rs index 5906579f376..0d4ea730dc0 100644 --- a/libp2p/src/lib.rs +++ b/libp2p/src/lib.rs @@ -189,12 +189,12 @@ pub async fn development_transport( keypair: identity::Keypair, ) -> std::io::Result> { let transport = { - let dns_tcp = dns::async_std::Config::system(tcp::async_io::Transport::new( + let dns_tcp = dns::async_std::Transport::system(tcp::async_io::Transport::new( tcp::Config::new().nodelay(true), )) .await?; let ws_dns_tcp = websocket::WsConfig::new( - dns::async_std::Config::system(tcp::async_io::Transport::new( + dns::async_std::Transport::system(tcp::async_io::Transport::new( tcp::Config::new().nodelay(true), )) .await?, @@ -234,10 +234,10 @@ pub fn tokio_development_transport( keypair: identity::Keypair, ) -> std::io::Result> { let transport = { - let dns_tcp = dns::tokio::Config::system(tcp::tokio::Transport::new( + let dns_tcp = dns::tokio::Transport::system(tcp::tokio::Transport::new( tcp::Config::new().nodelay(true), ))?; - let ws_dns_tcp = websocket::WsConfig::new(dns::tokio::Config::system( + let ws_dns_tcp = websocket::WsConfig::new(dns::tokio::Transport::system( tcp::tokio::Transport::new(tcp::Config::new().nodelay(true)), )?); dns_tcp.or_transport(ws_dns_tcp) diff --git a/misc/server/src/main.rs b/misc/server/src/main.rs index 6e856934ae9..e885301d590 100644 --- a/misc/server/src/main.rs +++ b/misc/server/src/main.rs @@ -88,7 +88,7 @@ async fn main() -> Result<(), Box> { let quic_transport = quic::tokio::Transport::new(quic::Config::new(&local_keypair)); - dns::tokio::Config::system(libp2p::core::transport::OrTransport::new( + dns::tokio::Transport::system(libp2p::core::transport::OrTransport::new( quic_transport, tcp_transport, ))? diff --git a/protocols/perf/src/bin/perf.rs b/protocols/perf/src/bin/perf.rs index 0a34c0963bd..31644324571 100644 --- a/protocols/perf/src/bin/perf.rs +++ b/protocols/perf/src/bin/perf.rs @@ -405,7 +405,7 @@ async fn swarm() -> Result> { libp2p_quic::tokio::Transport::new(config) }; - let dns = libp2p_dns::tokio::Config::system(OrTransport::new(quic, tcp))?; + let dns = libp2p_dns::tokio::Transport::system(OrTransport::new(quic, tcp))?; dns.map(|either_output, _| match either_output { Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)), diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 14ccc29ad2d..7e4c1000d2b 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -69,11 +69,11 @@ pub mod async_std { /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses /// using `async-std` for all async I/O. - pub type Config = crate::Config; + pub type Transport = crate::Transport; - impl Config { + impl Transport { /// Creates a new [`Config`] from the OS's DNS configuration and defaults. - pub async fn system(inner: T) -> Result, io::Error> { + pub async fn system(inner: T) -> Result, io::Error> { let (cfg, opts) = system_conf::read_system_conf()?; Self::custom(inner, cfg, opts).await } @@ -83,8 +83,8 @@ pub mod async_std { inner: T, cfg: ResolverConfig, opts: ResolverOpts, - ) -> Result, io::Error> { - Ok(Config { + ) -> Result, io::Error> { + Ok(Transport { inner: Arc::new(Mutex::new(inner)), resolver: async_std_resolver::resolver(cfg, opts).await, }) @@ -94,7 +94,7 @@ pub mod async_std { #[cfg(feature = "async-std")] #[deprecated(note = "Use `async_std::Config` instead.")] -pub type DnsConfig = async_std::Config; +pub type DnsConfig = async_std::Transport; #[cfg(feature = "tokio")] pub mod tokio { @@ -104,24 +104,24 @@ pub mod tokio { /// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses /// using `tokio` for all async I/O. - pub type Config = crate::Config; + pub type Transport = crate::Transport; - impl Config { - /// Creates a new [`Config`] from the OS's DNS configuration and defaults. - pub fn system(inner: T) -> Result, std::io::Error> { + impl Transport { + /// Creates a new [`Transport`] from the OS's DNS configuration and defaults. + pub fn system(inner: T) -> Result, std::io::Error> { let (cfg, opts) = system_conf::read_system_conf()?; Self::custom(inner, cfg, opts) } - /// Creates a [`Config`] with a custom resolver configuration + /// Creates a [`Transport`] with a custom resolver configuration /// and options. pub fn custom( inner: T, cfg: trust_dns_resolver::config::ResolverConfig, opts: trust_dns_resolver::config::ResolverOpts, - ) -> Result, std::io::Error> { + ) -> Result, std::io::Error> { // TODO: Make infallible in next breaking release. Or deprecation? - Ok(Config { + Ok(Transport { inner: Arc::new(Mutex::new(inner)), resolver: TokioAsyncResolver::tokio(cfg, opts), }) @@ -131,7 +131,7 @@ pub mod tokio { #[cfg(feature = "tokio")] #[deprecated(note = "Use `tokio::Config` instead.")] -pub type TokioDnsConfig = tokio::Config; +pub type TokioDnsConfig = tokio::Transport; use async_trait::async_trait; use futures::{future::BoxFuture, prelude::*}; @@ -139,7 +139,6 @@ use libp2p_core::{ connection::Endpoint, multiaddr::{Multiaddr, Protocol}, transport::{ListenerId, TransportError, TransportEvent}, - Transport, }; use parking_lot::Mutex; use smallvec::SmallVec; @@ -180,10 +179,10 @@ const MAX_DNS_LOOKUPS: usize = 32; /// result of a single `/dnsaddr` lookup. const MAX_TXT_RECORDS: usize = 16; -/// A `Transport` wrapper for performing DNS lookups when dialing `Multiaddr`esses. +/// A [`Transport`] for performing DNS lookups when dialing `Multiaddr`esses. /// You shouldn't need to use this type directly. Use [`tokio::Config`] or [`async_std::Config`] instead. #[derive(Debug)] -pub struct Config { +pub struct Transport { /// The underlying transport. inner: Arc>, /// The DNS resolver used when dialing addresses with DNS components. @@ -191,11 +190,11 @@ pub struct Config { } #[deprecated(note = "Use `async_std::Config` or `tokio::Config` instead.")] -pub type GenDnsConfig = Config; +pub type GenDnsConfig = Transport; -impl Transport for Config +impl libp2p_core::Transport for Transport where - T: Transport + Send + Unpin + 'static, + T: libp2p_core::Transport + Send + Unpin + 'static, T::Error: Send, T::Dial: Send, R: Clone + Send + Sync + Resolver + 'static, @@ -243,7 +242,7 @@ where cx: &mut Context<'_>, ) -> Poll> { let mut inner = self.inner.lock(); - Transport::poll(Pin::new(inner.deref_mut()), cx).map(|event| { + libp2p_core::Transport::poll(Pin::new(inner.deref_mut()), cx).map(|event| { event .map_upgrade(|upgr| upgr.map_err::<_, fn(_) -> _>(Error::Transport)) .map_err(Error::Transport) @@ -251,9 +250,9 @@ where } } -impl Config +impl Transport where - T: Transport + Send + Unpin + 'static, + T: libp2p_core::Transport + Send + Unpin + 'static, T::Error: Send, T::Dial: Send, R: Clone + Send + Sync + Resolver + 'static, @@ -262,7 +261,10 @@ where &mut self, addr: Multiaddr, role_override: Endpoint, - ) -> Result<::Dial, TransportError<::Error>> { + ) -> Result< + ::Dial, + TransportError<::Error>, + > { let resolver = self.resolver.clone(); let inner = self.inner.clone(); @@ -680,7 +682,7 @@ mod tests { } } - async fn run(mut transport: Config) + async fn run(mut transport: super::Transport) where T: Transport + Clone + Send + Unpin + 'static, T::Error: Send, @@ -762,7 +764,7 @@ mod tests { let config = ResolverConfig::quad9(); let opts = ResolverOpts::default(); async_std_crate::task::block_on( - async_std::Config::custom(CustomTransport, config, opts) + async_std::Transport::custom(CustomTransport, config, opts) .then(|dns| run(dns.unwrap())), ); } @@ -780,7 +782,7 @@ mod tests { .unwrap(); rt.block_on(run( - tokio::Config::custom(CustomTransport, config, opts).unwrap() + tokio::Transport::custom(CustomTransport, config, opts).unwrap() )); } } diff --git a/transports/websocket/src/lib.rs b/transports/websocket/src/lib.rs index eae5f007583..d7dd7628888 100644 --- a/transports/websocket/src/lib.rs +++ b/transports/websocket/src/lib.rs @@ -74,7 +74,7 @@ use std::{ /// # #[async_std::main] /// # async fn main() { /// -/// let mut transport = websocket::WsConfig::new(dns::async_std::Config::system( +/// let mut transport = websocket::WsConfig::new(dns::async_std::Transport::system( /// tcp::async_io::Transport::new(tcp::Config::default()), /// ).await.unwrap()); /// From fa86e0c653ad1eafdb3899b561415c5a21773955 Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 24 Sep 2023 10:44:14 +0900 Subject: [PATCH 13/17] docs(dns): rename `Config` to `Transport` --- transports/dns/src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 7e4c1000d2b..d254f2d247a 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -21,17 +21,17 @@ //! # [DNS name resolution](https://github.com/libp2p/specs/blob/master/addressing/README.md#ip-and-name-resolution) //! [`Transport`] for libp2p. //! -//! This crate provides the type [`async_std::Config`] and [`tokio::Config`] +//! This crate provides the type [`async_std::Transport`] and [`tokio::Transport`] //! for use with `async-std` and `tokio`, //! respectively. //! -//! A [`Config`] is an address-rewriting [`Transport`] wrapper around +//! A [`Transport`] is an address-rewriting [`libp2p::Transport`] wrapper around //! an inner `Transport`. The composed transport behaves like the inner //! transport, except that [`Transport::dial`] resolves `/dns/...`, `/dns4/...`, //! `/dns6/...` and `/dnsaddr/...` components of the given `Multiaddr` through //! a DNS, replacing them with the resolved protocols (typically TCP/IP). //! -//! The `async-std` feature and hence the `async_std::Config` are +//! The `async-std` feature and hence the `async_std::Transport` are //! enabled by default. Tokio users can furthermore opt-in //! to the `tokio-dns-over-rustls` and `tokio-dns-over-https-rustls` //! features. For more information about these features, please @@ -49,7 +49,7 @@ //! problematic on platforms like Android, where there's a lot of //! complexity hidden behind the system APIs. //! If the implementation requires different characteristics, one should -//! consider providing their own implementation of [`Config`] or use +//! consider providing their own implementation of [`Transport`] or use //! platform specific APIs to extract the host's DNS configuration (if possible) //! and provide a custom [`ResolverConfig`]. //! @@ -72,13 +72,13 @@ pub mod async_std { pub type Transport = crate::Transport; impl Transport { - /// Creates a new [`Config`] from the OS's DNS configuration and defaults. + /// Creates a new [`Transport`] from the OS's DNS configuration and defaults. pub async fn system(inner: T) -> Result, io::Error> { let (cfg, opts) = system_conf::read_system_conf()?; Self::custom(inner, cfg, opts).await } - /// Creates a [`Config`] with a custom resolver configuration and options. + /// Creates a [`Transport`] with a custom resolver configuration and options. pub async fn custom( inner: T, cfg: ResolverConfig, @@ -93,7 +93,7 @@ pub mod async_std { } #[cfg(feature = "async-std")] -#[deprecated(note = "Use `async_std::Config` instead.")] +#[deprecated(note = "Use `async_std::Transport` instead.")] pub type DnsConfig = async_std::Transport; #[cfg(feature = "tokio")] @@ -130,7 +130,7 @@ pub mod tokio { } #[cfg(feature = "tokio")] -#[deprecated(note = "Use `tokio::Config` instead.")] +#[deprecated(note = "Use `tokio::Transport` instead.")] pub type TokioDnsConfig = tokio::Transport; use async_trait::async_trait; @@ -180,7 +180,7 @@ const MAX_DNS_LOOKUPS: usize = 32; const MAX_TXT_RECORDS: usize = 16; /// A [`Transport`] for performing DNS lookups when dialing `Multiaddr`esses. -/// You shouldn't need to use this type directly. Use [`tokio::Config`] or [`async_std::Config`] instead. +/// You shouldn't need to use this type directly. Use [`tokio::Transport`] or [`async_std::Transport`] instead. #[derive(Debug)] pub struct Transport { /// The underlying transport. @@ -189,7 +189,7 @@ pub struct Transport { resolver: R, } -#[deprecated(note = "Use `async_std::Config` or `tokio::Config` instead.")] +#[deprecated(note = "Use `async_std::Transport` or `tokio::Transport` instead.")] pub type GenDnsConfig = Transport; impl libp2p_core::Transport for Transport @@ -400,7 +400,7 @@ where } } -/// The possible errors of a [`Config`] wrapped transport. +/// The possible errors of a [`Transport`] wrapped transport. #[derive(Debug)] #[allow(clippy::large_enum_variant)] pub enum Error { From 95624554a91875cefc082180818fa5671cd9ebda Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 24 Sep 2023 10:55:24 +0900 Subject: [PATCH 14/17] fix(dns): correct the type alias of Error to `DnsErr` --- transports/dns/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index d254f2d247a..c98545251d0 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -420,7 +420,7 @@ pub enum Error { } #[deprecated(note = "Use `Error` instead.")] -pub type DnsError = Error; +pub type DnsErr = Error; impl fmt::Display for Error where From 0b87c0390dbf9696c6d9b47d81d6108bdd89e0f3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 24 Sep 2023 13:42:01 +1000 Subject: [PATCH 15/17] Apply suggestions from code review --- transports/dns/CHANGELOG.md | 5 ++--- transports/dns/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index 9c57b2bf35b..e4b13a94598 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,8 +1,7 @@ ## 0.40.1 - unreleased -- Rename types with `Dns` prefix (`TokioDnsConfig`, and `DnsConfig`) -- Integrate `GenDnsConfig` into `Config` - See [PR 4505]. +- Remove `Dns` prefix from types like `TokioDnsConfig` and `DnsConfig` in favor of modules that describe the different variants. Users are encouraged to import the `libp2p::dns` module and refer to types as `dns::tokio::Transport` and `dns::async_std::Transport`. + See [PR 4505]. [PR 4505]: https://github.com/libp2p/rust-libp2p/pull/4505 diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index c98545251d0..28367fca1cd 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -25,13 +25,13 @@ //! for use with `async-std` and `tokio`, //! respectively. //! -//! A [`Transport`] is an address-rewriting [`libp2p::Transport`] wrapper around +//! A [`Transport`] is an address-rewriting [`libp2p_core::Transport`] wrapper around //! an inner `Transport`. The composed transport behaves like the inner //! transport, except that [`Transport::dial`] resolves `/dns/...`, `/dns4/...`, //! `/dns6/...` and `/dnsaddr/...` components of the given `Multiaddr` through //! a DNS, replacing them with the resolved protocols (typically TCP/IP). //! -//! The `async-std` feature and hence the `async_std::Transport` are +//! The `async-std` feature and hence the [`async_std::Transport`] are //! enabled by default. Tokio users can furthermore opt-in //! to the `tokio-dns-over-rustls` and `tokio-dns-over-https-rustls` //! features. For more information about these features, please From d27d7a6a1be1afeed61dd5fee9a48d00f7061760 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 24 Sep 2023 13:42:30 +1000 Subject: [PATCH 16/17] Update transports/dns/CHANGELOG.md --- transports/dns/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/transports/dns/CHANGELOG.md b/transports/dns/CHANGELOG.md index e4b13a94598..29b5ac4403a 100644 --- a/transports/dns/CHANGELOG.md +++ b/transports/dns/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.40.1 - unreleased -- Remove `Dns` prefix from types like `TokioDnsConfig` and `DnsConfig` in favor of modules that describe the different variants. Users are encouraged to import the `libp2p::dns` module and refer to types as `dns::tokio::Transport` and `dns::async_std::Transport`. +- Remove `Dns` prefix from types like `TokioDnsConfig` and `DnsConfig` in favor of modules that describe the different variants. + Users are encouraged to import the `libp2p::dns` module and refer to types as `dns::tokio::Transport` and `dns::async_std::Transport`. See [PR 4505]. [PR 4505]: https://github.com/libp2p/rust-libp2p/pull/4505 From 3ce22219e887dbe85e41634ff5e2121900a28ecf Mon Sep 17 00:00:00 2001 From: whtsht Date: Sun, 24 Sep 2023 13:14:14 +0900 Subject: [PATCH 17/17] docs(dns): fix wrong item link --- transports/dns/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/dns/src/lib.rs b/transports/dns/src/lib.rs index 28367fca1cd..b27b14a77c0 100644 --- a/transports/dns/src/lib.rs +++ b/transports/dns/src/lib.rs @@ -27,7 +27,7 @@ //! //! A [`Transport`] is an address-rewriting [`libp2p_core::Transport`] wrapper around //! an inner `Transport`. The composed transport behaves like the inner -//! transport, except that [`Transport::dial`] resolves `/dns/...`, `/dns4/...`, +//! transport, except that [`libp2p_core::Transport::dial`] resolves `/dns/...`, `/dns4/...`, //! `/dns6/...` and `/dnsaddr/...` components of the given `Multiaddr` through //! a DNS, replacing them with the resolved protocols (typically TCP/IP). //!