Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/crypto/noise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
use bytes::{Buf, Bytes, BytesMut};
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use prost::Message;
use snow::{Builder, HandshakeState, TransportState};
use snow::{params::NoiseParams, Builder, HandshakeState, TransportState};

use std::{
fmt, io,
Expand All @@ -47,7 +47,10 @@ mod handshake_schema {
}

/// Noise parameters.
const NOISE_PARAMETERS: &str = "Noise_XX_25519_ChaChaPoly_SHA256";
static NOISE_PARAMETERS: std::sync::OnceLock<NoiseParams> = std::sync::OnceLock::new();

/// Noise parameters name.
const NOISE_PARAMETERS_NAME: &str = "Noise_XX_25519_ChaChaPoly_SHA256";

/// Prefix of static key signatures for domain separation.
pub(crate) const STATIC_KEY_DOMAIN: &str = "noise-libp2p-static-key:";
Expand Down Expand Up @@ -128,7 +131,9 @@ impl NoiseContext {
tracing::trace!(target: LOG_TARGET, ?role, "create new noise configuration");

let builder: Builder<'_> = Builder::with_resolver(
NOISE_PARAMETERS.parse().expect("qed; Valid noise pattern"),
NOISE_PARAMETERS
.get_or_init(|| NOISE_PARAMETERS_NAME.parse().expect("qed; Valid noise pattern"))
.clone(),
Box::new(protocol::Resolver),
);

Expand All @@ -147,7 +152,9 @@ impl NoiseContext {
#[cfg(feature = "webrtc")]
pub fn with_prologue(id_keys: &Keypair, prologue: Vec<u8>) -> Result<Self, NegotiationError> {
let noise: Builder<'_> = Builder::with_resolver(
NOISE_PARAMETERS.parse().expect("qed; Valid noise pattern"),
NOISE_PARAMETERS
.get_or_init(|| NOISE_PARAMETERS_NAME.parse().expect("qed; Valid noise pattern"))
.clone(),
Box::new(protocol::Resolver),
);

Expand Down
35 changes: 19 additions & 16 deletions src/transport/common/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use std::{
task::{Context, Poll},
};

/// Tokio DNS resolver.
static TOKIO_DNS_RESOLVER: std::sync::OnceLock<TokioAsyncResolver> = std::sync::OnceLock::new();

/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::transport::listener";

Expand Down Expand Up @@ -84,23 +87,23 @@ impl AddressType {
} => (address, port, dns_type),
};

let lookup =
match TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default())
.lookup_ip(url.clone())
.await
{
Ok(lookup) => lookup,
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
?error,
"failed to resolve DNS address `{}`",
url
);
let resolver = TOKIO_DNS_RESOLVER.get_or_init(|| {
TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default())
});

return Err(DnsError::ResolveError(url));
}
};
let lookup = match resolver.lookup_ip(url.clone()).await {
Ok(lookup) => lookup,
Err(error) => {
tracing::debug!(
target: LOG_TARGET,
?error,
"failed to resolve DNS address `{}`",
url
);

return Err(DnsError::ResolveError(url));
}
};

let Some(ip) = lookup.iter().find(|ip| match dns_type {
DnsType::Dns => true,
Expand Down