From 05a505c94394d9849efc56c26aec2d8cb7aeb2bf Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Mon, 27 Jul 2015 16:35:33 +0900 Subject: [PATCH] Always check off-link connectivity in NetworkDiagnostics. Currently, NetworkDiagnostics only checks off-link connectivity if one of the DNS servers is off-link. Make it check off-link connectivity in all cases by sending probes to Google Public DNS if off-link DNS servers are not specified. Bug: 22569331 Bug: 22641669 Bug: 22748900 Change-Id: I6cb0dd8491bc0c1a488631deca56722b9c1d2b3f --- core/java/android/net/LinkProperties.java | 13 +++++++++++- .../android/server/ConnectivityService.java | 2 +- .../connectivity/NetworkDiagnostics.java | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java index cfd5bf1596..c4de4a21e0 100644 --- a/core/java/android/net/LinkProperties.java +++ b/core/java/android/net/LinkProperties.java @@ -661,6 +661,17 @@ public final class LinkProperties implements Parcelable { return false; } + /** + * Returns true if this link or any of its stacked interfaces has an IPv4 address. + * + * @return {@code true} if there is an IPv4 address, {@code false} otherwise. + */ + private boolean hasIPv4AddressOnInterface(String iface) { + return (mIfaceName.equals(iface) && hasIPv4Address()) || + (iface != null && mStackedLinks.containsKey(iface) && + mStackedLinks.get(iface).hasIPv4Address()); + } + /** * Returns true if this link has a global preferred IPv6 address. * @@ -792,7 +803,7 @@ public final class LinkProperties implements Parcelable { if (ip instanceof Inet4Address) { // For IPv4, it suffices for now to simply have any address. - return hasIPv4Address(); + return hasIPv4AddressOnInterface(bestRoute.getInterface()); } else if (ip instanceof Inet6Address) { if (ip.isLinkLocalAddress()) { // For now, just make sure link-local destinations have diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index eb74ab065d..76d2258e0b 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -1772,7 +1772,7 @@ public class ConnectivityService extends IConnectivityManager.Stub // Start gathering diagnostic information. netDiags.add(new NetworkDiagnostics( nai.network, - new LinkProperties(nai.linkProperties), + new LinkProperties(nai.linkProperties), // Must be a copy. DIAG_TIME_MS)); } diff --git a/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java b/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java index 74ba404eb0..aca699152e 100644 --- a/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java +++ b/services/core/java/com/android/server/connectivity/NetworkDiagnostics.java @@ -20,6 +20,7 @@ import static android.system.OsConstants.*; import android.net.LinkProperties; import android.net.Network; +import android.net.NetworkUtils; import android.net.RouteInfo; import android.os.SystemClock; import android.system.ErrnoException; @@ -79,6 +80,10 @@ import libcore.io.IoUtils; public class NetworkDiagnostics { private static final String TAG = "NetworkDiagnostics"; + private static final InetAddress TEST_DNS4 = NetworkUtils.numericToInetAddress("8.8.8.8"); + private static final InetAddress TEST_DNS6 = NetworkUtils.numericToInetAddress( + "2001:4860:4860::8888"); + // For brevity elsewhere. private static final long now() { return SystemClock.elapsedRealtime(); @@ -156,6 +161,21 @@ public class NetworkDiagnostics { mStartTime = now(); mDeadlineTime = mStartTime + mTimeoutMs; + // Hardcode measurements to TEST_DNS4 and TEST_DNS6 in order to test off-link connectivity. + // We are free to modify mLinkProperties with impunity because ConnectivityService passes us + // a copy and not the original object. It's easier to do it this way because we don't need + // to check whether the LinkProperties already contains these DNS servers because + // LinkProperties#addDnsServer checks for duplicates. + if (mLinkProperties.isReachable(TEST_DNS4)) { + mLinkProperties.addDnsServer(TEST_DNS4); + } + // TODO: we could use mLinkProperties.isReachable(TEST_DNS6) here, because we won't set any + // DNS servers for which isReachable() is false, but since this is diagnostic code, be extra + // careful. + if (mLinkProperties.hasGlobalIPv6Address() || mLinkProperties.hasIPv6DefaultRoute()) { + mLinkProperties.addDnsServer(TEST_DNS6); + } + for (RouteInfo route : mLinkProperties.getRoutes()) { if (route.hasGateway()) { prepareIcmpMeasurement(route.getGateway());