Skip to content

Commit e288b3a

Browse files
lcolittiAndroid (Google) Code Review
authored andcommitted
Merge changes I5c994de5,I6cb0dd84 into mnc-dev
* changes: Add a test for public bugs 2111 and 2136. Always check off-link connectivity in NetworkDiagnostics.
2 parents 965bab2 + f28e62b commit e288b3a

4 files changed

Lines changed: 100 additions & 3 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());

services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import android.net.LinkAddress;
2222
import android.system.OsConstants;
2323
import android.test.suitebuilder.annotation.SmallTest;
24-
import junit.framework.TestCase;
24+
import com.android.internal.util.HexDump;
2525

2626
import java.net.Inet4Address;
2727
import java.nio.ByteBuffer;
2828
import java.util.ArrayList;
2929

30+
import junit.framework.TestCase;
3031
import libcore.util.HexEncoding;
3132

3233
import static android.net.dhcp.DhcpPacket.*;
@@ -370,4 +371,69 @@ public void testPadAndOverloadedOptionsOffer() throws Exception {
370371
assertDhcpResults("172.17.152.118/16", "172.17.1.1", "172.17.1.1",
371372
null, "1.1.1.1", null, 43200, false, dhcpResults);
372373
}
374+
375+
@SmallTest
376+
public void testBug2111() throws Exception {
377+
final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
378+
// IP header.
379+
"4500014c00000000ff119beac3eaf3880a3f5d04" +
380+
// UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
381+
"0043004401387464" +
382+
// BOOTP header.
383+
"0201060002554812000a0000000000000a3f5d040000000000000000" +
384+
// MAC address.
385+
"00904c00000000000000000000000000" +
386+
// Server name.
387+
"0000000000000000000000000000000000000000000000000000000000000000" +
388+
"0000000000000000000000000000000000000000000000000000000000000000" +
389+
// File.
390+
"0000000000000000000000000000000000000000000000000000000000000000" +
391+
"0000000000000000000000000000000000000000000000000000000000000000" +
392+
"0000000000000000000000000000000000000000000000000000000000000000" +
393+
"0000000000000000000000000000000000000000000000000000000000000000" +
394+
// Options.
395+
"638253633501023604c00002fe33040000bfc60104fffff00003040a3f50010608c0000201c0000202" +
396+
"0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"
397+
).toCharArray(), false));
398+
399+
DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
400+
assertTrue(offerPacket instanceof DhcpOfferPacket);
401+
DhcpResults dhcpResults = offerPacket.toDhcpResults();
402+
assertDhcpResults("10.63.93.4/20", "10.63.80.1", "192.0.2.1,192.0.2.2",
403+
"domain123.co.uk", "192.0.2.254", null, 49094, false, dhcpResults);
404+
}
405+
406+
@SmallTest
407+
public void testBug2136() throws Exception {
408+
final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
409+
// Ethernet header.
410+
"bcf5ac000000d0c7890000000800" +
411+
// IP header.
412+
"4500014c00000000ff119beac3eaf3880a3f5d04" +
413+
// UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
414+
"0043004401387574" +
415+
// BOOTP header.
416+
"0201060163339a3000050000000000000a209ecd0000000000000000" +
417+
// MAC address.
418+
"bcf5ac00000000000000000000000000" +
419+
// Server name.
420+
"0000000000000000000000000000000000000000000000000000000000000000" +
421+
"0000000000000000000000000000000000000000000000000000000000000000" +
422+
// File.
423+
"0000000000000000000000000000000000000000000000000000000000000000" +
424+
"0000000000000000000000000000000000000000000000000000000000000000" +
425+
"0000000000000000000000000000000000000000000000000000000000000000" +
426+
"0000000000000000000000000000000000000000000000000000000000000000" +
427+
// Options.
428+
"6382536335010236040a20ff80330400001c200104fffff00003040a20900106089458413494584135" +
429+
"0f0b6c616e63732e61632e756b000000000000000000ff00000000"
430+
).toCharArray(), false));
431+
432+
DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
433+
assertTrue(offerPacket instanceof DhcpOfferPacket);
434+
assertEquals("BCF5AC000000", HexDump.toHexString(offerPacket.getClientMac()));
435+
DhcpResults dhcpResults = offerPacket.toDhcpResults();
436+
assertDhcpResults("10.32.158.205/20", "10.32.144.1", "148.88.65.52,148.88.65.53",
437+
"lancs.ac.uk", "10.32.255.128", null, 7200, false, dhcpResults);
438+
}
373439
}

0 commit comments

Comments
 (0)