Use AI_ADDRCONFIG for most DNS requests

Not using it doubles the number of DNS lookups for single-stack
networks.
Private DNS hostname resolution still does not use this flag so that we
get all address families in advance, in case of LinkProperties changes.
Bug: b/79811321
Test: manual: Captive portal, 464xlat, networking in apps working
Merged-In: I7037342a93dc48b8e0988e719b9a9a2d5055bcf2
Merged-In: I8ca15fa079cd5ff94e4d9f7e0476504769f9708f
(Clean cherry-pick of pi-dev Ib46756e5e5f8d8d2698c90c5183c368d2d44be7a)

Change-Id: Iedb7a9c5f9caeb66de4505189fca8db91013d844
This commit is contained in:
Remi NGUYEN VAN
2018-05-22 18:20:51 +09:00
parent 6fa8d06c78
commit a6085d17ca
2 changed files with 13 additions and 5 deletions

View File

@@ -822,8 +822,9 @@ public class NetworkMonitor extends StateMachine {
private void resolveStrictModeHostname() {
try {
// Do a blocking DNS resolution using the network-assigned nameservers.
// Do not set AI_ADDRCONFIG in ai_flags so we get all address families in advance.
final InetAddress[] ips = ResolvUtil.blockingResolveAllLocally(
mNetwork, mPrivateDnsProviderHostname);
mNetwork, mPrivateDnsProviderHostname, 0 /* aiFlags */);
mPrivateDnsConfig = new PrivateDnsConfig(mPrivateDnsProviderHostname, ips);
} catch (UnknownHostException uhe) {
mPrivateDnsConfig = null;

View File

@@ -16,6 +16,8 @@
package android.net.dns;
import static android.system.OsConstants.AI_ADDRCONFIG;
import android.net.Network;
import android.net.NetworkUtils;
import android.system.GaiException;
@@ -41,12 +43,17 @@ public class ResolvUtil {
public static InetAddress[] blockingResolveAllLocally(Network network, String name)
throws UnknownHostException {
// Use AI_ADDRCONFIG by default
return blockingResolveAllLocally(network, name, AI_ADDRCONFIG);
}
public static InetAddress[] blockingResolveAllLocally(
Network network, String name, int aiFlags) throws UnknownHostException {
final StructAddrinfo hints = new StructAddrinfo();
// Unnecessary, but expressly no AI_ADDRCONFIG.
hints.ai_flags = 0;
// Fetch all IP addresses at once to minimize re-resolution.
hints.ai_flags = aiFlags;
// Other hints identical to the default Inet6AddressImpl implementation
hints.ai_family = OsConstants.AF_UNSPEC;
hints.ai_socktype = OsConstants.SOCK_DGRAM;
hints.ai_socktype = OsConstants.SOCK_STREAM;
final Network networkForResolv = getNetworkWithUseLocalNameserversFlag(network);