[GH-ISSUE #2836] 'hosts' doesn't seem to work #1070

Closed
opened 2026-03-16 01:32:13 +03:00 by kerem · 4 comments
Owner

Originally created by @ryoii on GitHub (Mar 10, 2025).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2836

I want to use the built-in 'hosts' to handle some DNS requests. I try like this

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut root_cert_store = rustls::RootCertStore::empty();
    root_cert_store.roots = webpki_roots::TLS_SERVER_ROOTS.into();

    let dns_name = "1.1.1.1";
    // let dns_ips = vec![dns_name.parse()?];
    let dns_ips = vec![];
    let ns = NameServerConfigGroup::from_ips_https(
        &dns_ips,
        443,
        "".to_string(),
        true
    );

    let mut opt = ResolverOpts::default();
    opt.use_hosts_file = ResolveHosts::Always;
    opt.tls_config = ClientConfig::builder()
        .with_root_certificates(root_cert_store)
        .with_no_client_auth();

    let config = ResolverConfig::from_parts(None, vec![], ns);
    let mut hosts = Hosts::new();
    hosts.read_hosts_conf("0.0.0.0 www.google.com".as_bytes())?;

    // with hosts
    {
        let query = Query::query("www.google.com".into_name()?, RecordType::A);
        let lookup = hosts.lookup_static_host(&query);
        println!("Lookup for {:?}: {:?}", query, lookup);
    }

    // with resolver
    {
        let mut resolver = TokioResolver::tokio(config, opt);
        resolver.set_hosts(Some(hosts));

        let lookup = resolver.lookup_ip("www.google.com").await?;
        println!("Lookup result: {:?}", lookup);
    }

    Ok(())
}

It returns an error proto error: no connections available while calling lookup_ip.
Once I set a doh server let dns_ips = vec![dns_name.parse()?];, It works but doesn't return 0.0.0.0 as I expected.

Library version
hickory-resolver = { version = "0.25.0-alpha.5", features = ["dns-over-rustls", "dns-over-https-rustls"]}

Originally created by @ryoii on GitHub (Mar 10, 2025). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2836 I want to use the built-in 'hosts' to handle some DNS requests. I try like this ```rust #[tokio::main] async fn main() -> anyhow::Result<()> { let mut root_cert_store = rustls::RootCertStore::empty(); root_cert_store.roots = webpki_roots::TLS_SERVER_ROOTS.into(); let dns_name = "1.1.1.1"; // let dns_ips = vec![dns_name.parse()?]; let dns_ips = vec![]; let ns = NameServerConfigGroup::from_ips_https( &dns_ips, 443, "".to_string(), true ); let mut opt = ResolverOpts::default(); opt.use_hosts_file = ResolveHosts::Always; opt.tls_config = ClientConfig::builder() .with_root_certificates(root_cert_store) .with_no_client_auth(); let config = ResolverConfig::from_parts(None, vec![], ns); let mut hosts = Hosts::new(); hosts.read_hosts_conf("0.0.0.0 www.google.com".as_bytes())?; // with hosts { let query = Query::query("www.google.com".into_name()?, RecordType::A); let lookup = hosts.lookup_static_host(&query); println!("Lookup for {:?}: {:?}", query, lookup); } // with resolver { let mut resolver = TokioResolver::tokio(config, opt); resolver.set_hosts(Some(hosts)); let lookup = resolver.lookup_ip("www.google.com").await?; println!("Lookup result: {:?}", lookup); } Ok(()) } ``` It returns an error `proto error: no connections available` while calling `lookup_ip`. Once I set a doh server `let dns_ips = vec![dns_name.parse()?];`, It works but doesn't return `0.0.0.0` as I expected. Library version `hickory-resolver = { version = "0.25.0-alpha.5", features = ["dns-over-rustls", "dns-over-https-rustls"]}`
kerem closed this issue 2026-03-16 01:32:18 +03:00
Author
Owner

@djc commented on GitHub (Mar 10, 2025):

Your example configures 0 IP addresses for the NameServerConfigGroup so it's not surprising that the resolver is unable to make a connection. Do you want to argue that an empty config group should still be able to resolve names that are configured in the hosts file, without even trying DNS queries?

<!-- gh-comment-id:2710597992 --> @djc commented on GitHub (Mar 10, 2025): Your example configures 0 IP addresses for the `NameServerConfigGroup` so it's not surprising that the resolver is unable to make a connection. Do you want to argue that an empty config group should still be able to resolve names that are configured in the hosts file, without even trying DNS queries?
Author
Owner

@ryoii commented on GitHub (Mar 10, 2025):

Do you want to argue that an empty config group should still be able to resolve names that are configured in the hosts file, without even trying DNS queries?

Sure, if the hosts file can provide query results, shouldn't the request for the dns service be send?

But, as I said, even if I provide the IP address for the NameServerConfigGroup

    let dns_name = "1.1.1.1";
    let dns_ips = vec![dns_name.parse()?];
    // let dns_ips = vec![];
    let ns = NameServerConfigGroup::from_ips_https(
        &dns_ips,
        443,
        "".to_string(),
        true
    );

It also won't return 0.0.0.0 as I was expected.

<!-- gh-comment-id:2710661125 --> @ryoii commented on GitHub (Mar 10, 2025): > Do you want to argue that an empty config group should still be able to resolve names that are configured in the hosts file, without even trying DNS queries? Sure, if the hosts file can provide query results, shouldn't the request for the dns service be send? But, as I said, even if I provide the IP address for the `NameServerConfigGroup` ```rust let dns_name = "1.1.1.1"; let dns_ips = vec![dns_name.parse()?]; // let dns_ips = vec![]; let ns = NameServerConfigGroup::from_ips_https( &dns_ips, 443, "".to_string(), true ); ``` It also won't return `0.0.0.0` as I was expected.
Author
Owner

@djc commented on GitHub (Mar 10, 2025):

This appears to be a regression introduced with #2560 -- should be fixed in #2839. Thanks for the clear reproduction!

<!-- gh-comment-id:2711914799 --> @djc commented on GitHub (Mar 10, 2025): This appears to be a regression introduced with #2560 -- should be fixed in #2839. Thanks for the clear reproduction!
Author
Owner

@ryoii commented on GitHub (Mar 11, 2025):

According to #2839
Using root domain can work around.

hosts.read_hosts_conf("0.0.0.0 www.google.com.".as_bytes())?;
<!-- gh-comment-id:2712378268 --> @ryoii commented on GitHub (Mar 11, 2025): According to #2839 Using root domain can work around. ```rust hosts.read_hosts_conf("0.0.0.0 www.google.com.".as_bytes())?; ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hickory-dns#1070
No description provided.