Skip to content

System.Net.Dns does not always resolve "" and the system's own hostname on Unix #36849

Open
@antonfirsov

Description

@antonfirsov

A follow-up on the discussion in #36072.

Problem

dotnet/corefx#41764 implemented a consistent solution to match Windows behavior when Dns.GetHostEntry(hostName) or related methods are invoked with "" or the systems hostname on Unix, but it only works on Unix systems which are configured to successfully resolve their own hostname. Although most of the time this is true, there are some exceptions, eg. our 10.14 CI macs don't do it. I'm not sure about the root cause on mac, but it's relatively easy to "misconfigure" Ubuntu 18.04, see last paragraph. On these systems the shell command hostname | nslookup (and the matching libc call chain) fails, which makes our current System.Net.Dns implementation throw. This does not conform our documentation:

If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

Suggestion

In pal_networking.c we are already handling hostname as a special case:

result = gethostname((char*)name, _POSIX_HOST_NAME_MAX);
bool includeIPv4Loopback = true;
bool includeIPv6Loopback = true;
if (result == 0 && strcasecmp((const char*)address, name) == 0)
{
// Get all interface addresses if the host name corresponds to the local host.
result = getifaddrs(&addrs);

We should return the same result, even if the underlying OS name resolution (getaddrinfo) fails.

Despite it's name, the System.Net.Dns class is a universal name resolver that goes behind the DNS resolution rules, implementing a .NET-specific logic for name resolution. It's better to aim for cross-platform consistency, instead of matching the behavior of nslookup / getaddrinfo (which is not being matched anyways since dotnet/corefx#41764)

Note: on Windows and "properly configured" Unixes, Dns.GetHostEntry("") and Dns.GetHostEntry("my-hostname") are essentially the same, we should not change this behavior IMO.

CC @wfurt @scalablecory @davidsh

Reproduction on Ubuntu 18.04

In /etc/nsswitch.conf:

- hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname
+ hosts:          files dns

In /etc/resolv.conf:

- nameserver 127.0.0.53
+ nameserver 8.8.8.8

In /etc/hosts:

- 127.0.1.1 <hostname>

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.NetenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions