Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
[Android] Fix DNS localhost subdomain resolution with IPv6 on Android#125478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
simonrozsival
merged 3 commits into
dotnet:main
from
simonrozsival:fix/124751-dns-localhost-androidMay 13, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
56 changes: 48 additions & 8 deletions
56 src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -419,6 +419,10 @@ private static bool ValidateAddressFamily(ref AddressFamily addressFamily, strin | ||
| private const string Localhost = "localhost"; | ||
| private const string InvalidDomain = "invalid"; | ||
| // Some systems (e.g. Android, some Linux distros) map ::1 to "ip6-localhost" instead of | ||
| // "localhost" in /etc/hosts, which causes getaddrinfo("localhost", AF_INET6) to fail with EAI_NONAME. | ||
| private const string IPv6Localhost = "ip6-localhost"; | ||
| /// <summary> | ||
| /// Checks if the given host name matches a reserved name or is a subdomain of it. | ||
| /// For example, IsReservedName("foo.localhost", "localhost") returns true. | ||
| @@ -550,7 +554,16 @@ private static object GetHostEntryOrAddressesCore(string hostName, bool justAddr | ||
| if (fallbackToLocalhost) | ||
| { | ||
| return GetHostEntryOrAddressesCore(Localhost, justAddresses, addressFamily); | ||
| try | ||
| { | ||
| return GetHostEntryOrAddressesCore(Localhost, justAddresses, addressFamily); | ||
| } | ||
| catch (SocketException ex) when (addressFamily == AddressFamily.InterNetworkV6 && ex.SocketErrorCode == SocketError.HostNotFound) | ||
| { | ||
| // Some systems map ::1 to "ip6-localhost" instead of "localhost" in /etc/hosts. | ||
| if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(Localhost, $"localhost IPv6 resolution failed, retrying with '{IPv6Localhost}'"); | ||
| return GetHostEntryOrAddressesCore(IPv6Localhost, justAddresses, addressFamily); | ||
| } | ||
| } | ||
| Debug.Assert(result is not null); | ||
| @@ -795,8 +808,7 @@ static async Task<T> CompleteAsync(Task task, string hostName, bool justAddresse | ||
| NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: result, exception: null); | ||
| fallbackOccurred = true; | ||
| // result is IPAddress[] so justAddresses is guaranteed true here. | ||
| return await ((Task<T>)(Task)Dns.GetHostAddressesAsync(Localhost, addressFamily, cancellationToken)).ConfigureAwait(false); | ||
| return await GetLocalhostAddressesAsync(addressFamily, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| if (isLocalhostSubdomain && result is IPHostEntry entry && entry.AddressList.Length == 0) | ||
| @@ -805,8 +817,7 @@ static async Task<T> CompleteAsync(Task task, string hostName, bool justAddresse | ||
| NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: result, exception: null); | ||
| fallbackOccurred = true; | ||
| // result is IPHostEntry so justAddresses is guaranteed false here. | ||
| return await ((Task<T>)(Task)Dns.GetHostEntryAsync(Localhost, addressFamily, cancellationToken)).ConfigureAwait(false); | ||
| return await GetLocalhostEntryAsync(addressFamily, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| return result; | ||
| @@ -818,9 +829,9 @@ static async Task<T> CompleteAsync(Task task, string hostName, bool justAddresse | ||
| NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: null, exception: ex); | ||
| fallbackOccurred = true; | ||
| return await ((Task<T>)(justAddresses | ||
| ? (Task)Dns.GetHostAddressesAsync(Localhost, addressFamily, cancellationToken) | ||
| : Dns.GetHostEntryAsync(Localhost, addressFamily, cancellationToken))).ConfigureAwait(false); | ||
| return justAddresses | ||
| ? await GetLocalhostAddressesAsync(addressFamily, cancellationToken).ConfigureAwait(false) | ||
| : await GetLocalhostEntryAsync(addressFamily, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| @@ -834,6 +845,35 @@ static async Task<T> CompleteAsync(Task task, string hostName, bool justAddresse | ||
| NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: result, exception: exception); | ||
| } | ||
| } | ||
| // Resolves "localhost" with the given address family, returning addresses. | ||
| // If IPv6 resolution fails with HostNotFound, retries with "ip6-localhost" | ||
| // because some systems map ::1 to "ip6-localhost" instead of "localhost" in /etc/hosts. | ||
| static async Task<T> GetLocalhostAddressesAsync(AddressFamily family, CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| return await ((Task<T>)(Task)Dns.GetHostAddressesAsync(Localhost, family, cancellationToken)).ConfigureAwait(false); | ||
| } | ||
| catch (SocketException ex) when (family == AddressFamily.InterNetworkV6 && ex.SocketErrorCode == SocketError.HostNotFound) | ||
| { | ||
| if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(Localhost, $"localhost IPv6 resolution failed, retrying with '{IPv6Localhost}'"); | ||
| return await ((Task<T>)(Task)Dns.GetHostAddressesAsync(IPv6Localhost, family, cancellationToken)).ConfigureAwait(false); | ||
| } | ||
simonrozsival marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| static async Task<T> GetLocalhostEntryAsync(AddressFamily family, CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| return await ((Task<T>)(Task)Dns.GetHostEntryAsync(Localhost, family, cancellationToken)).ConfigureAwait(false); | ||
| } | ||
| catch (SocketException ex) when (family == AddressFamily.InterNetworkV6 && ex.SocketErrorCode == SocketError.HostNotFound) | ||
| { | ||
| if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(Localhost, $"localhost IPv6 resolution failed, retrying with '{IPv6Localhost}'"); | ||
| return await ((Task<T>)(Task)Dns.GetHostEntryAsync(IPv6Localhost, family, cancellationToken)).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| } | ||
1 change: 0 additions & 1 deletion
1 src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1 src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.