Merge "Use AI_ADDRCONFIG for most DNS requests"

This commit is contained in:
Remi NGUYEN VAN
2018-05-25 03:28:05 +00:00
committed by Gerrit Code Review
2 changed files with 13 additions and 5 deletions

View File

@@ -795,8 +795,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);