Skip to content

Commit 87cfc70

Browse files
committed
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
1 parent a7bdace commit 87cfc70

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

core/java/android/net/LinkProperties.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,17 @@ public boolean hasIPv4Address() {
661661
return false;
662662
}
663663

664+
/**
665+
* Returns true if this link or any of its stacked interfaces has an IPv4 address.
666+
*
667+
* @return {@code true} if there is an IPv4 address, {@code false} otherwise.
668+
*/
669+
private boolean hasIPv4AddressOnInterface(String iface) {
670+
return (mIfaceName.equals(iface) && hasIPv4Address()) ||
671+
(iface != null && mStackedLinks.containsKey(iface) &&
672+
mStackedLinks.get(iface).hasIPv4Address());
673+
}
674+
664675
/**
665676
* Returns true if this link has a global preferred IPv6 address.
666677
*
@@ -792,7 +803,7 @@ public boolean isReachable(InetAddress ip) {
792803

793804
if (ip instanceof Inet4Address) {
794805
// For IPv4, it suffices for now to simply have any address.
795-
return hasIPv4Address();
806+
return hasIPv4AddressOnInterface(bestRoute.getInterface());
796807
} else if (ip instanceof Inet6Address) {
797808
if (ip.isLinkLocalAddress()) {
798809
// For now, just make sure link-local destinations have

services/core/java/com/android/server/ConnectivityService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
17721772
// Start gathering diagnostic information.
17731773
netDiags.add(new NetworkDiagnostics(
17741774
nai.network,
1775-
new LinkProperties(nai.linkProperties),
1775+
new LinkProperties(nai.linkProperties), // Must be a copy.
17761776
DIAG_TIME_MS));
17771777
}
17781778

services/core/java/com/android/server/connectivity/NetworkDiagnostics.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import android.net.LinkProperties;
2222
import android.net.Network;
23+
import android.net.NetworkUtils;
2324
import android.net.RouteInfo;
2425
import android.os.SystemClock;
2526
import android.system.ErrnoException;
@@ -79,6 +80,10 @@
7980
public class NetworkDiagnostics {
8081
private static final String TAG = "NetworkDiagnostics";
8182

83+
private static final InetAddress TEST_DNS4 = NetworkUtils.numericToInetAddress("8.8.8.8");
84+
private static final InetAddress TEST_DNS6 = NetworkUtils.numericToInetAddress(
85+
"2001:4860:4860::8888");
86+
8287
// For brevity elsewhere.
8388
private static final long now() {
8489
return SystemClock.elapsedRealtime();
@@ -156,6 +161,21 @@ public NetworkDiagnostics(Network network, LinkProperties lp, long timeoutMs) {
156161
mStartTime = now();
157162
mDeadlineTime = mStartTime + mTimeoutMs;
158163

164+
// Hardcode measurements to TEST_DNS4 and TEST_DNS6 in order to test off-link connectivity.
165+
// We are free to modify mLinkProperties with impunity because ConnectivityService passes us
166+
// a copy and not the original object. It's easier to do it this way because we don't need
167+
// to check whether the LinkProperties already contains these DNS servers because
168+
// LinkProperties#addDnsServer checks for duplicates.
169+
if (mLinkProperties.isReachable(TEST_DNS4)) {
170+
mLinkProperties.addDnsServer(TEST_DNS4);
171+
}
172+
// TODO: we could use mLinkProperties.isReachable(TEST_DNS6) here, because we won't set any
173+
// DNS servers for which isReachable() is false, but since this is diagnostic code, be extra
174+
// careful.
175+
if (mLinkProperties.hasGlobalIPv6Address() || mLinkProperties.hasIPv6DefaultRoute()) {
176+
mLinkProperties.addDnsServer(TEST_DNS6);
177+
}
178+
159179
for (RouteInfo route : mLinkProperties.getRoutes()) {
160180
if (route.hasGateway()) {
161181
prepareIcmpMeasurement(route.getGateway());

0 commit comments

Comments
 (0)