Skip to content

Commit 9ab0e6f

Browse files
authored
feat(libp2p): add SwarmBuilder::with_dns_config
Add the missing `SwarmBuilder::with_dns_config`. The `SwarmBuilder` doesn't have any way of providing a custom DNS configuration. Also, `with_other_transport` could not be used because is using `or_transport` instead of just wrapping the inner transport. Pull-Request: #4808.
1 parent 2b12663 commit 9ab0e6f

File tree

6 files changed

+227
-1
lines changed

6 files changed

+227
-1
lines changed

libp2p/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Allow `SwarmBuilder::with_quic_config` to be called without `with_tcp` first.
44
See [PR 4821](https://github.com/libp2p/rust-libp2p/pull/4821).
5+
- Introduce `SwarmBuilder::with_dns_config`.
6+
See [PR 4808](https://github.com/libp2p/rust-libp2p/pull/4808).
57

68
## 0.53.0
79

libp2p/src/builder.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,103 @@ mod tests {
363363
.build();
364364
}
365365

366+
#[tokio::test]
367+
#[cfg(all(
368+
feature = "tokio",
369+
feature = "tcp",
370+
feature = "noise",
371+
feature = "yamux",
372+
feature = "dns"
373+
))]
374+
async fn tcp_dns_config() {
375+
SwarmBuilder::with_new_identity()
376+
.with_tokio()
377+
.with_tcp(
378+
Default::default(),
379+
(libp2p_tls::Config::new, libp2p_noise::Config::new),
380+
libp2p_yamux::Config::default,
381+
)
382+
.unwrap()
383+
.with_dns_config(
384+
libp2p_dns::ResolverConfig::default(),
385+
libp2p_dns::ResolverOpts::default(),
386+
)
387+
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
388+
.unwrap()
389+
.build();
390+
}
391+
392+
#[tokio::test]
393+
#[cfg(all(feature = "tokio", feature = "quic", feature = "dns"))]
394+
async fn quic_dns_config() {
395+
SwarmBuilder::with_new_identity()
396+
.with_tokio()
397+
.with_quic()
398+
.with_dns_config(
399+
libp2p_dns::ResolverConfig::default(),
400+
libp2p_dns::ResolverOpts::default(),
401+
)
402+
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
403+
.unwrap()
404+
.build();
405+
}
406+
407+
#[tokio::test]
408+
#[cfg(all(
409+
feature = "tokio",
410+
feature = "tcp",
411+
feature = "noise",
412+
feature = "yamux",
413+
feature = "quic",
414+
feature = "dns"
415+
))]
416+
async fn tcp_quic_dns_config() {
417+
SwarmBuilder::with_new_identity()
418+
.with_tokio()
419+
.with_tcp(
420+
Default::default(),
421+
(libp2p_tls::Config::new, libp2p_noise::Config::new),
422+
libp2p_yamux::Config::default,
423+
)
424+
.unwrap()
425+
.with_quic()
426+
.with_dns_config(
427+
libp2p_dns::ResolverConfig::default(),
428+
libp2p_dns::ResolverOpts::default(),
429+
)
430+
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
431+
.unwrap()
432+
.build();
433+
}
434+
435+
#[tokio::test]
436+
#[cfg(all(
437+
feature = "async-std",
438+
feature = "tcp",
439+
feature = "noise",
440+
feature = "yamux",
441+
feature = "quic",
442+
feature = "dns"
443+
))]
444+
async fn async_std_tcp_quic_dns_config() {
445+
SwarmBuilder::with_new_identity()
446+
.with_async_std()
447+
.with_tcp(
448+
Default::default(),
449+
(libp2p_tls::Config::new, libp2p_noise::Config::new),
450+
libp2p_yamux::Config::default,
451+
)
452+
.unwrap()
453+
.with_quic()
454+
.with_dns_config(
455+
libp2p_dns::ResolverConfig::default(),
456+
libp2p_dns::ResolverOpts::default(),
457+
)
458+
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
459+
.unwrap()
460+
.build();
461+
}
462+
366463
/// Showcases how to provide custom transports unknown to the libp2p crate, e.g. WebRTC.
367464
#[test]
368465
#[cfg(feature = "tokio")]

libp2p/src/builder/phase/dns.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct DnsPhase<T> {
88

99
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std", feature = "dns"))]
1010
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::AsyncStd, DnsPhase<T>> {
11+
// TODO: Remove `async`
1112
pub async fn with_dns(
1213
self,
1314
) -> Result<
@@ -21,7 +22,7 @@ impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::AsyncSt
2122
keypair: self.keypair,
2223
phantom: PhantomData,
2324
phase: WebsocketPhase {
24-
transport: libp2p_dns::async_std::Transport::system(self.phase.transport).await?,
25+
transport: libp2p_dns::async_std::Transport::system2(self.phase.transport)?,
2526
},
2627
})
2728
}
@@ -48,6 +49,48 @@ impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::Tokio,
4849
}
4950
}
5051

52+
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std", feature = "dns"))]
53+
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::AsyncStd, DnsPhase<T>> {
54+
pub fn with_dns_config(
55+
self,
56+
cfg: libp2p_dns::ResolverConfig,
57+
opts: libp2p_dns::ResolverOpts,
58+
) -> SwarmBuilder<
59+
super::provider::AsyncStd,
60+
WebsocketPhase<impl AuthenticatedMultiplexedTransport>,
61+
> {
62+
SwarmBuilder {
63+
keypair: self.keypair,
64+
phantom: PhantomData,
65+
phase: WebsocketPhase {
66+
transport: libp2p_dns::async_std::Transport::custom2(
67+
self.phase.transport,
68+
cfg,
69+
opts,
70+
),
71+
},
72+
}
73+
}
74+
}
75+
76+
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio", feature = "dns"))]
77+
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::Tokio, DnsPhase<T>> {
78+
pub fn with_dns_config(
79+
self,
80+
cfg: libp2p_dns::ResolverConfig,
81+
opts: libp2p_dns::ResolverOpts,
82+
) -> SwarmBuilder<super::provider::Tokio, WebsocketPhase<impl AuthenticatedMultiplexedTransport>>
83+
{
84+
SwarmBuilder {
85+
keypair: self.keypair,
86+
phantom: PhantomData,
87+
phase: WebsocketPhase {
88+
transport: libp2p_dns::tokio::Transport::custom(self.phase.transport, cfg, opts),
89+
},
90+
}
91+
}
92+
}
93+
5194
impl<Provider, T> SwarmBuilder<Provider, DnsPhase<T>> {
5295
pub(crate) fn without_dns(self) -> SwarmBuilder<Provider, WebsocketPhase<T>> {
5396
SwarmBuilder {

libp2p/src/builder/phase/other_transport.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ impl<T: AuthenticatedMultiplexedTransport>
100100
self.without_any_other_transports().with_dns()
101101
}
102102
}
103+
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std", feature = "dns"))]
104+
impl<T: AuthenticatedMultiplexedTransport>
105+
SwarmBuilder<super::provider::AsyncStd, OtherTransportPhase<T>>
106+
{
107+
pub fn with_dns_config(
108+
self,
109+
cfg: libp2p_dns::ResolverConfig,
110+
opts: libp2p_dns::ResolverOpts,
111+
) -> SwarmBuilder<
112+
super::provider::AsyncStd,
113+
WebsocketPhase<impl AuthenticatedMultiplexedTransport>,
114+
> {
115+
self.without_any_other_transports()
116+
.with_dns_config(cfg, opts)
117+
}
118+
}
119+
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio", feature = "dns"))]
120+
impl<T: AuthenticatedMultiplexedTransport>
121+
SwarmBuilder<super::provider::Tokio, OtherTransportPhase<T>>
122+
{
123+
pub fn with_dns_config(
124+
self,
125+
cfg: libp2p_dns::ResolverConfig,
126+
opts: libp2p_dns::ResolverOpts,
127+
) -> SwarmBuilder<super::provider::Tokio, WebsocketPhase<impl AuthenticatedMultiplexedTransport>>
128+
{
129+
self.without_any_other_transports()
130+
.with_dns_config(cfg, opts)
131+
}
132+
}
103133
#[cfg(feature = "relay")]
104134
impl<T: AuthenticatedMultiplexedTransport, Provider>
105135
SwarmBuilder<Provider, OtherTransportPhase<T>>

libp2p/src/builder/phase/quic.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,35 @@ impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::Tokio,
181181
.with_dns()
182182
}
183183
}
184+
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std", feature = "dns"))]
185+
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::AsyncStd, QuicPhase<T>> {
186+
pub fn with_dns_config(
187+
self,
188+
cfg: libp2p_dns::ResolverConfig,
189+
opts: libp2p_dns::ResolverOpts,
190+
) -> SwarmBuilder<
191+
super::provider::AsyncStd,
192+
WebsocketPhase<impl AuthenticatedMultiplexedTransport>,
193+
> {
194+
self.without_quic()
195+
.without_any_other_transports()
196+
.with_dns_config(cfg, opts)
197+
}
198+
}
199+
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio", feature = "dns"))]
200+
impl<T: AuthenticatedMultiplexedTransport> SwarmBuilder<super::provider::Tokio, QuicPhase<T>> {
201+
pub fn with_dns_config(
202+
self,
203+
cfg: libp2p_dns::ResolverConfig,
204+
opts: libp2p_dns::ResolverOpts,
205+
) -> SwarmBuilder<super::provider::Tokio, WebsocketPhase<impl AuthenticatedMultiplexedTransport>>
206+
{
207+
self.without_quic()
208+
.without_any_other_transports()
209+
.with_dns_config(cfg, opts)
210+
}
211+
}
212+
184213
macro_rules! impl_quic_phase_with_websocket {
185214
($providerKebabCase:literal, $providerPascalCase:ty, $websocketStream:ty) => {
186215
#[cfg(all(feature = $providerKebabCase, not(target_arch = "wasm32"), feature = "websocket"))]

transports/dns/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#[cfg(feature = "async-std")]
6161
pub mod async_std {
6262
use async_std_resolver::AsyncStdResolver;
63+
use futures::FutureExt;
6364
use hickory_resolver::{
6465
config::{ResolverConfig, ResolverOpts},
6566
system_conf,
@@ -85,6 +86,30 @@ pub mod async_std {
8586
resolver: async_std_resolver::resolver(cfg, opts).await,
8687
}
8788
}
89+
90+
// TODO: Replace `system` implementation with this
91+
#[doc(hidden)]
92+
pub fn system2(inner: T) -> Result<Transport<T>, io::Error> {
93+
Ok(Transport {
94+
inner: Arc::new(Mutex::new(inner)),
95+
resolver: async_std_resolver::resolver_from_system_conf()
96+
.now_or_never()
97+
.expect(
98+
"async_std_resolver::resolver_from_system_conf did not resolve immediately",
99+
)?,
100+
})
101+
}
102+
103+
// TODO: Replace `custom` implementation with this
104+
#[doc(hidden)]
105+
pub fn custom2(inner: T, cfg: ResolverConfig, opts: ResolverOpts) -> Transport<T> {
106+
Transport {
107+
inner: Arc::new(Mutex::new(inner)),
108+
resolver: async_std_resolver::resolver(cfg, opts)
109+
.now_or_never()
110+
.expect("async_std_resolver::resolver did not resolve immediately"),
111+
}
112+
}
88113
}
89114
}
90115

0 commit comments

Comments
 (0)