Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 87cfc70b authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

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
parent a7bdace2
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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));
            }

+20 −0
Original line number Diff line number Diff line
@@ -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());