Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: improve error message for hickory-dns and warn in docs #2389

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
30 changes: 8 additions & 22 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,14 +1720,7 @@ impl ClientBuilder {
self
}

/// Enables the [hickory-dns](hickory_resolver) async resolver instead of a default threadpool
/// using `getaddrinfo`.
///
/// If the `hickory-dns` feature is turned on, the default option is enabled.
///
/// # Optional
///
/// This requires the optional `hickory-dns` feature to be enabled
#[doc(hidden)]
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
#[deprecated(note = "use `hickory_dns` instead")]
Expand All @@ -1744,29 +1737,22 @@ impl ClientBuilder {
/// # Optional
///
/// This requires the optional `hickory-dns` feature to be enabled
///
/// # Warning
///
/// The hickory resolver does not work exactly the same, or on all the platforms
/// that the default resolver does
#[cfg(feature = "hickory-dns")]
#[cfg_attr(docsrs, doc(cfg(feature = "hickory-dns")))]
pub fn hickory_dns(mut self, enable: bool) -> ClientBuilder {
self.config.hickory_dns = enable;
self
}

/// Disables the hickory-dns async resolver.
///
/// This method exists even if the optional `hickory-dns` feature is not enabled.
/// This can be used to ensure a `Client` doesn't use the hickory-dns async resolver
/// even if another dependency were to enable the optional `hickory-dns` feature.
#[doc(hidden)]
#[deprecated(note = "use `no_hickory_dns` instead")]
pub fn no_trust_dns(self) -> ClientBuilder {
#[cfg(feature = "hickory-dns")]
{
self.hickory_dns(false)
}

#[cfg(not(feature = "hickory-dns"))]
{
self
}
self.no_hickory_dns()
}

/// Disables the hickory-dns async resolver.
Expand Down
29 changes: 20 additions & 9 deletions src/dns/hickory.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! DNS resolution via the [hickory-resolver](https://github.com/hickory-dns/hickory-dns) crate

use hickory_resolver::{
config::LookupIpStrategy, lookup_ip::LookupIpIntoIter, system_conf, TokioAsyncResolver,
config::LookupIpStrategy, error::ResolveError, lookup_ip::LookupIpIntoIter, system_conf,
TokioAsyncResolver,
};
use once_cell::sync::OnceCell;

use std::io;
use std::fmt;
use std::net::SocketAddr;
use std::sync::Arc;

Expand All @@ -24,6 +25,9 @@ struct SocketAddrs {
iter: LookupIpIntoIter,
}

#[derive(Debug)]
struct HickoryDnsSystemConfError(ResolveError);

impl Resolve for HickoryDnsResolver {
fn resolve(&self, name: Name) -> Resolving {
let resolver = self.clone();
Expand Down Expand Up @@ -51,13 +55,20 @@ impl Iterator for SocketAddrs {
/// which reads from `/etc/resolve.conf`. The options are
/// overridden to look up for both IPv4 and IPv6 addresses
/// to work with "happy eyeballs" algorithm.
fn new_resolver() -> io::Result<TokioAsyncResolver> {
let (config, mut opts) = system_conf::read_system_conf().map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("error reading DNS system conf: {e}"),
)
})?;
fn new_resolver() -> Result<TokioAsyncResolver, HickoryDnsSystemConfError> {
let (config, mut opts) = system_conf::read_system_conf().map_err(HickoryDnsSystemConfError)?;
opts.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
Ok(TokioAsyncResolver::tokio(config, opts))
}

impl fmt::Display for HickoryDnsSystemConfError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("error reading DNS system conf for hickory-dns")
}
}

impl std::error::Error for HickoryDnsSystemConfError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}