Repository navigation
Description
Description
On Linux, IPInterfaceProperties.DnsAddresses throws PlatformNotSupportedException if /etc/resolv.conf is missing. This is incorrect. Per the man page, if the file does not exist (or has no nameserver lines) then the host acts as its own name server.
Reproduction Steps
On a Linux machine:
mv /etc/resolv.conf /etc/resolv.conf.backup
dotnet new console -o dnstest
cd dnstest
echo "using System.Net.NetworkInformation; Console.WriteLine(string.Join(' ', NetworkInterface.GetAllNetworkInterfaces().SelectMany(nic => nic.GetIPProperties().DnsAddresses)));" > Program.cs
dotnet runExpected behavior
127.0.0.1 or 127.0.0.1 ::1
Alternately, blank might be considered correct depending on how you define the DnsAddresses property. That would be consistent with the case where /etc/resolv.conf has no nameserver lines, which presently produces an empty list. Or perhaps it should depend on whether the host actually has a DNS service running.
Alternately, document that PlatformNotSupportedException can be thrown if the DNS configuration is missing. Although I think InvalidOperationException is a better choice, since it's not really a platform-specific problem.
Regardless, the behavior when the file is missing should match the behavior when the file is empty.
Actual behavior
Unhandled exception. System.PlatformNotSupportedException: The information requested is unavailable on the current platform.
at System.Net.NetworkInformation.UnixIPInterfaceProperties.get_DnsAddresses()
at Program.<>c.<<Main>$>b__0_0(NetworkInterface nic) in /home/mmorrison/dnstest/Program.cs:line 1
at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
at System.String.JoinCore[T](ReadOnlySpan`1 separator, IEnumerable`1 values)
at System.String.Join[T](Char separator, IEnumerable`1 values)
at Program.<Main>$(String[] args) in /home/mmorrison/dnstest/Program.cs:line 1
Regression?
No response
Known Workarounds
Catch the exception and treat it as an empty list.
Configuration
dotnet 6.0.135
Red Hat Enterprise Linux 8.10
x64
based on source code, the issue should apply to all Linux hosts
Other information
No response
Activity
dotnet-policy-service commentedon Nov 6, 2024
Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.
antonfirsov commentedon Nov 6, 2024
I think this is worth to fix, and also looks like a good candidate for community contribution. @wfurt do you agree? Any thoughts re returning
127.0.0.1vs empty list?wfurt commentedon Nov 6, 2024
either way. I feel we should not throw.
Emdot commentedon Nov 7, 2024
Looks like checking /etc/resolv.conf directly might not be a solid approach anyway, since (if I'm reading https://www.freedesktop.org/software/systemd/man/latest/systemd-resolved.service.html#/etc/resolv.conf correctly), name resolution doesn't necessarily actually go through that file.