Skip to content

Commit

Permalink
fix: use tcp backend for peer seed DNS resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Nov 7, 2021
1 parent 800686e commit 8f3e7b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
35 changes: 27 additions & 8 deletions base_layer/p2p/src/dns/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,23 @@ use crate::dns::mock::{DefaultOnSend, MockClientHandle};

use super::DnsClientError;
use futures::{future, FutureExt};
use std::{net::SocketAddr, sync::Arc};
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tari_shutdown::Shutdown;
use tokio::{net::UdpSocket, task};
use tokio::task;
use trust_dns_client::{
client::{AsyncClient, AsyncDnssecClient, ClientHandle},
op::Query,
proto::{error::ProtoError, rr::dnssec::TrustAnchor, udp::UdpClientStream, xfer::DnsResponse, DnsHandle},
rr::{DNSClass, IntoName, RecordType},
proto::{
error::ProtoError,
iocompat::AsyncIoTokioAsStd,
rr::dnssec::TrustAnchor,
xfer::DnsResponse,
DnsHandle,
DnsMultiplexer,
},
rr::{dnssec::SigSigner, DNSClass, IntoName, RecordType},
serialize::binary::BinEncoder,
tcp::TcpClientStream,
};

#[derive(Clone)]
Expand Down Expand Up @@ -112,11 +120,16 @@ pub struct Client<C> {
impl Client<AsyncDnssecClient> {
pub async fn connect_secure(name_server: SocketAddr, trust_anchor: TrustAnchor) -> Result<Self, DnsClientError> {
let shutdown = Shutdown::new();
let stream = UdpClientStream::<UdpSocket>::new(name_server);
let (client, background) = AsyncDnssecClient::builder(stream)

// TODO: make configurable
let timeout = Duration::from_secs(5);
let (connect_fut, handle) = TcpClientStream::<AsyncIoTokioAsStd<tokio::net::TcpStream>>::new(name_server);
let dns_muxer = DnsMultiplexer::<_, SigSigner>::with_timeout(connect_fut, handle, timeout, None);
let (client, background) = AsyncDnssecClient::builder(dns_muxer)
.trust_anchor(trust_anchor)
.build()
.await?;

task::spawn(future::select(shutdown.to_signal(), background.fuse()));

Ok(Self {
Expand All @@ -129,8 +142,13 @@ impl Client<AsyncDnssecClient> {
impl Client<AsyncClient> {
pub async fn connect(name_server: SocketAddr) -> Result<Self, DnsClientError> {
let shutdown = Shutdown::new();
let stream = UdpClientStream::<UdpSocket>::new(name_server);
let (client, background) = AsyncClient::connect(stream).await?;

// TODO: make configurable
let timeout = Duration::from_secs(5);
let (stream, handle) =
TcpClientStream::<AsyncIoTokioAsStd<tokio::net::TcpStream>>::with_timeout(name_server, timeout);

let (client, background) = AsyncClient::with_timeout(stream, handle, timeout, None).await?;
task::spawn(future::select(shutdown.to_signal(), background.fuse()));

Ok(Self {
Expand All @@ -148,6 +166,7 @@ where C: DnsHandle<Error = ProtoError>
.inner
.query(query.name().clone(), query.query_class(), query.query_type())
.await?;

Ok(client_resp)
}
}
Expand Down
2 changes: 2 additions & 0 deletions base_layer/p2p/src/dns/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ pub enum DnsClientError {
ClientError(#[from] ClientError),
#[error("DNS Protocol error: {0}")]
ProtoError(#[from] ProtoError),
#[error("DNS timeout error")]
Timeout,
}
1 change: 1 addition & 0 deletions base_layer/p2p/src/peer_seeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ mod test {
client: DnsClient::connect("1.1.1.1:53".parse().unwrap()).await.unwrap(),
};
let seeds = resolver.resolve("seeds.weatherwax.tari.com").await.unwrap();
println!("{:?}", seeds);
assert!(!seeds.is_empty());
}

Expand Down

0 comments on commit 8f3e7b2

Please sign in to comment.