Skip to content

DnsAddresses throw PlatformNotSupportedException if /etc/resolv.conf is missing #109582

Closed

Description

@Emdot

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 run

Expected 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

  1. added
    untriaged
    on Nov 6, 2024
  2. dotnet-policy-service commented on Nov 6, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  3. added this to the Future milestone on Nov 6, 2024
  4. added
    help wanted
    and removed
    untriaged
    on Nov 6, 2024
  5. antonfirsov commented on Nov 6, 2024

    @antonfirsov
    Contributor

    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.1 vs empty list?

  6. wfurt commented on Nov 6, 2024

    @wfurt
    Member

    either way. I feel we should not throw.

  7. Emdot commented on Nov 7, 2024

    @Emdot
    Author

    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.

  8. added
    in-pr
    on Jan 15, 2025
  9. locked and limited conversation to collaborators on Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Netbughelp wantedin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions