diff options
author | Xiao Ma <xiaom@google.com> | 2019-07-05 07:46:34 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-07-05 07:46:34 -0700 |
commit | f093022d6c8186c9bb3e02cba10f76ca3f848d1e (patch) | |
tree | 6da997056111b477a3a564d93a6d8862acdfbbd0 /src/android/net/dhcp/DhcpClient.java | |
parent | fff7b07d00fbe0cacaf61dec80f5e28cec982c81 (diff) | |
parent | d53f5498850e2371843dfbba2bcaad7dd5cd74a3 (diff) |
Merge "Add DHCP Rapid Commit option(RFC4039) support in client side."
am: d53f549885
Change-Id: I4a868d673f6263a5ad2d5acb475e99aac80b750c
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) { |