DnsResolver cannot discover system DNS servers on Android
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
### Description
`DnsResolver` (and `Dns.Resolve*`) is now implemented on all Unix-like platforms by the managed stub resolver in `DnsResolverPal.Managed.cs`, which is compiled for the `-unix` TFM and therefore also ships on Android.
When `DnsResolverOptions.Servers` is empty, the managed resolver obtains the system DNS servers from `/etc/resolv.conf` via `ResolvConf.GetNameServers()`. Android has no readable resolver configuration, so this returns an empty list and `GetServers()` falls back to `127.0.0.1:53`:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.NameResolution/src/System/Net/DnsResolverPal.Managed.cs#L851-L857
The result is that every query issued by a default-constructed `DnsResolver` times out on Android.
This is the same underlying gap that makes `IPInterfaceProperties.DnsAddresses` throw `PlatformNotSupportedException` on Android (see `AndroidIPInterfaceProperties` and the corresponding assertion in `IPInterfacePropertiesTest_Android.cs`).
Note that this only affects the *system-configured* server path. Callers that supply `DnsResolverOptions.Servers` explicitly work fine on Android.
### Suggested fix
Add an Android-specific source for the system DNS servers, e.g.:
- `ConnectivityManager.getLinkProperties(activeNetwork).getDnsServers()` through the Android interop layer, or
- the `net.dns1` / `net.dns2` system properties via `__system_property_get` (legacy, unavailable to non-privileged apps on newer API levels), or
- `android_res_nsend` / the NDK resolver API.
### Test impact
The network tests in `DnsResolverTest` are currently gated with `PlatformDetection.IsNotAndroid` for this reason. `DnsResolverLoopbackTest` is unaffected because it passes explicit servers, and does run on Android.
> [!NOTE]
> This issue was drafted by GitHub Copilot.
Contributor guide
Research direction
Start in src/libraries/System.Net.NameResolution/src/System/Net/DnsResolverPal.Managed.cs, especially the GetServers() fallback and ResolvConf.GetNameServers(), then compare AndroidIPInterfaceProperties and IPInterfacePropertiesTest_Android.cs. Review the Android-gated cases in DnsResolverTest and the explicit-server coverage in DnsResolverLoopbackTest. Done means default DnsResolver queries discover Android system DNS servers while explicit-server and loopback behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, csharp
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100