diff options
author | Xiao Ma <xiaom@google.com> | 2019-06-11 18:51:24 +0900 |
---|---|---|
committer | Xiao Ma <xiaom@google.com> | 2019-07-02 18:57:04 +0900 |
commit | f963e366ec80924fd17e4631f2243fb6b1a3670d (patch) | |
tree | 014a2690fa5883cdeb9db393ce1a75c2e8b13b2a /src/android/net/dhcp/DhcpClient.java | |
parent | 74c651f8f3fde9ebb4bd43b433759aaa40bf8f40 (diff) |
Add DHCP Rapid Commit option(RFC4039) support in client side.
Bug: 122710829
Bug: 136216415
Test: atest FrameworksNetTests NetworkStackTests
Test: manual test
Change-Id: I384282b7efd0df0c4a6de7bdc1957048303ad88b
Diffstat (limited to 'src/android/net/dhcp/DhcpClient.java')
-rw-r--r-- | src/android/net/dhcp/DhcpClient.java | 91 |
1 files changed, 68 insertions, 23 deletions
diff --git a/src/android/net/dhcp/DhcpClient.java b/src/android/net/dhcp/DhcpClient.java index 0ed81a3..a140f4d 100644 --- a/src/android/net/dhcp/DhcpClient.java +++ b/src/android/net/dhcp/DhcpClient.java @@ -179,7 +179,8 @@ public class DhcpClient extends StateMachine { private static final int EVENT_CONFIGURATION_INVALID = PRIVATE_BASE + 9; // constant to represent this DHCP lease has been expired. - private static final long EXPIRED_LEASE = 1L; + @VisibleForTesting + public static final long EXPIRED_LEASE = 1L; // For message logging. private static final Class[] sMessageClasses = { DhcpClient.class }; @@ -239,7 +240,7 @@ public class DhcpClient extends StateMachine { private String mL2Key; private Inet4Address mLastAssignedIpv4Address; private long mLastAssignedIpv4AddressExpiry; - private boolean mDhcpLeaseCacheEnabled; + private Dependencies mDependencies; @NonNull private final NetworkStackIpMemoryStore mIpMemoryStore; @@ -271,15 +272,43 @@ public class DhcpClient extends StateMachine { return new WakeupMessage(mContext, getHandler(), cmdName, cmd); } + /** + * Encapsulates DhcpClient depencencies that's used for unit testing and + * integration testing. + */ + public static class Dependencies { + private final NetworkStackIpMemoryStore mNetworkStackIpMemoryStore; + + public Dependencies(NetworkStackIpMemoryStore store) { + mNetworkStackIpMemoryStore = store; + } + + /** + * Get a IpMemoryStore instance. + */ + public NetworkStackIpMemoryStore getIpMemoryStore() { + return mNetworkStackIpMemoryStore; + } + + /** + * Get the value of DHCP related experiment flags. + */ + public boolean getBooleanDeviceConfig(final String nameSpace, final String flagName) { + return NetworkStackUtils.getDeviceConfigPropertyBoolean(nameSpace, flagName, + false /* default value */); + } + } + // TODO: Take an InterfaceParams instance instead of an interface name String. private DhcpClient(Context context, StateMachine controller, String iface, - NetworkStackIpMemoryStore ipMemoryStore) { + Dependencies deps) { super(TAG, controller.getHandler()); + mDependencies = deps; mContext = context; mController = controller; mIfaceName = iface; - mIpMemoryStore = ipMemoryStore; + mIpMemoryStore = deps.getIpMemoryStore(); // CHECKSTYLE:OFF IndentationCheck addState(mStoppedState); @@ -320,8 +349,8 @@ public class DhcpClient extends StateMachine { public static DhcpClient makeDhcpClient( Context context, StateMachine controller, InterfaceParams ifParams, - NetworkStackIpMemoryStore ipMemoryStore) { - DhcpClient client = new DhcpClient(context, controller, ifParams.name, ipMemoryStore); + Dependencies deps) { + DhcpClient client = new DhcpClient(context, controller, ifParams.name, deps); client.mIface = ifParams; client.start(); return client; @@ -332,19 +361,21 @@ public class DhcpClient extends StateMachine { * */ public boolean isDhcpLeaseCacheEnabled() { - // TODO: call DeviceConfig.getProperty(DeviceConfig.NAMESPACE_CONNECTIVITY, - // DeviceConfig.PROPERTY); - // to fetch the dynamic experiment flag value. Return false by default. - return mDhcpLeaseCacheEnabled; + return mDependencies.getBooleanDeviceConfig(NetworkStackUtils.NAMESPACE_CONNECTIVITY, + NetworkStackUtils.DHCP_INIT_REBOOT_ENABLED); } /** - * set DHCP lease cached enabled experiment flag. - * + * check whether or not to support DHCP Rapid Commit option. */ - @VisibleForTesting - public void setDhcpLeaseCacheEnabled(final boolean enabled) { - mDhcpLeaseCacheEnabled = enabled; + public boolean isDhcpRapidCommitEnabled() { + return mDependencies.getBooleanDeviceConfig(NetworkStackUtils.NAMESPACE_CONNECTIVITY, + NetworkStackUtils.DHCP_RAPID_COMMIT_ENABLED); + } + + private void confirmDhcpLease(DhcpPacket packet, DhcpResults results) { + setDhcpLeaseExpiry(packet); + acceptDhcpResults(results, "Confirmed"); } private boolean initInterface() { @@ -495,7 +526,7 @@ public class DhcpClient extends StateMachine { private boolean sendDiscoverPacket() { ByteBuffer packet = DhcpPacket.buildDiscoverPacket( DhcpPacket.ENCAP_L2, mTransactionId, getSecs(), mHwAddr, - DO_UNICAST, REQUESTED_PARAMS); + DO_UNICAST, REQUESTED_PARAMS, isDhcpRapidCommitEnabled()); return transmitPacket(packet, "DHCPDISCOVER", DhcpPacket.ENCAP_L2, INADDR_BROADCAST); } @@ -959,11 +990,26 @@ public class DhcpClient extends StateMachine { protected void receivePacket(DhcpPacket packet) { if (!isValidPacket(packet)) return; - if (!(packet instanceof DhcpOfferPacket)) return; - mOffer = packet.toDhcpResults(); - if (mOffer != null) { - Log.d(TAG, "Got pending lease: " + mOffer); - transitionTo(mDhcpRequestingState); + + // 1. received the DHCPOFFER packet, process it by following RFC2131. + // 2. received the DHCPACK packet from DHCP Servers who support Rapid + // Commit option, process it by following RFC4039. + if (packet instanceof DhcpOfferPacket) { + mOffer = packet.toDhcpResults(); + if (mOffer != null) { + Log.d(TAG, "Got pending lease: " + mOffer); + transitionTo(mDhcpRequestingState); + } + } else if (packet instanceof DhcpAckPacket) { + // If received DHCPACK packet w/o Rapid Commit option in this state, + // just drop it and wait for the next DHCPOFFER packet or DHCPACK w/ + // Rapid Commit option. + if (!isDhcpRapidCommitEnabled() || !packet.mRapidCommit) return; + final DhcpResults results = packet.toDhcpResults(); + if (results != null) { + confirmDhcpLease(packet, results); + transitionTo(mConfiguringInterfaceState); + } } } } @@ -990,8 +1036,7 @@ public class DhcpClient extends StateMachine { if ((packet instanceof DhcpAckPacket)) { DhcpResults results = packet.toDhcpResults(); if (results != null) { - setDhcpLeaseExpiry(packet); - acceptDhcpResults(results, "Confirmed"); + confirmDhcpLease(packet, results); transitionTo(mConfiguringInterfaceState); } } else if (packet instanceof DhcpNakPacket) { |