Description
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:
runtime/src/libraries/Native/Unix/System.Native/pal_networking.c
Lines 318 to 326 in 3fda6ef
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>