Merge "Add more tests in NetworkCapabilitiesTest.java"
diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java
index fe90a84..e9bcefe 100644
--- a/core/java/android/net/NetworkAgent.java
+++ b/core/java/android/net/NetworkAgent.java
@@ -556,12 +556,12 @@
@NonNull
public Network register() {
if (VDBG) log("Registering NetworkAgent");
- final ConnectivityManager cm = (ConnectivityManager) mInitialConfiguration.context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
synchronized (mRegisterLock) {
if (mNetwork != null) {
throw new IllegalStateException("Agent already registered");
}
+ final ConnectivityManager cm = (ConnectivityManager) mInitialConfiguration.context
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
mNetwork = cm.registerNetworkAgent(new Messenger(mHandler),
new NetworkInfo(mInitialConfiguration.info),
mInitialConfiguration.properties, mInitialConfiguration.capabilities,
@@ -624,7 +624,9 @@
throw new UnsupportedOperationException(
"Legacy agents can't call markConnected.");
}
- mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null);
+ // |reason| cannot be used by the non-legacy agents
+ mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null /* reason */,
+ mNetworkInfo.getExtraInfo());
queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, mNetworkInfo);
}
@@ -638,7 +640,9 @@
if (mIsLegacy) {
throw new UnsupportedOperationException("Legacy agents can't call unregister.");
}
- mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null);
+ // When unregistering an agent nobody should use the extrainfo (or reason) any more.
+ mNetworkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null /* reason */,
+ null /* extraInfo */);
queueOrSendMessage(EVENT_NETWORK_INFO_CHANGED, mNetworkInfo);
}
diff --git a/services/core/java/com/android/server/connectivity/Nat464Xlat.java b/services/core/java/com/android/server/connectivity/Nat464Xlat.java
index 3b3c520..741cb5b 100644
--- a/services/core/java/com/android/server/connectivity/Nat464Xlat.java
+++ b/services/core/java/com/android/server/connectivity/Nat464Xlat.java
@@ -213,12 +213,10 @@
}
mIface = null;
mBaseIface = null;
- mState = State.IDLE;
if (requiresClat(mNetwork)) {
mState = State.DISCOVERING;
} else {
stopPrefixDiscovery();
- mState = State.IDLE;
}
}
@@ -285,6 +283,7 @@
private void stopPrefixDiscovery() {
try {
mDnsResolver.stopPrefix64Discovery(getNetId());
+ mState = State.IDLE;
} catch (RemoteException | ServiceSpecificException e) {
Slog.e(TAG, "Error stopping prefix discovery on netId " + getNetId() + ": " + e);
}
@@ -294,27 +293,43 @@
* Starts/stops NAT64 prefix discovery and clatd as necessary.
*/
public void update() {
- // TODO: turn this class into a proper StateMachine. // http://b/126113090
- if (requiresClat(mNetwork)) {
- if (!isPrefixDiscoveryStarted()) {
- startPrefixDiscovery();
- } else if (shouldStartClat(mNetwork)) {
- // NAT64 prefix detected. Start clatd.
- // TODO: support the NAT64 prefix changing after it's been discovered. There is no
- // need to support this at the moment because it cannot happen without changes to
- // the Dns64Configuration code in netd.
- start();
- } else {
- // NAT64 prefix removed. Stop clatd and go back into DISCOVERING state.
- stop();
- }
- } else {
- // Network no longer requires clat. Stop clat and prefix discovery.
- if (isStarted()) {
- stop();
- } else if (isPrefixDiscoveryStarted()) {
- leaveStartedState();
- }
+ // TODO: turn this class into a proper StateMachine. http://b/126113090
+ switch (mState) {
+ case IDLE:
+ if (requiresClat(mNetwork)) {
+ // Network is detected to be IPv6-only.
+ // TODO: consider going to STARTING directly if the NAT64 prefix is already
+ // known. This would however result in clatd running without prefix discovery
+ // running, which might be a surprising combination.
+ startPrefixDiscovery(); // Enters DISCOVERING state.
+ return;
+ }
+ break;
+
+ case DISCOVERING:
+ if (shouldStartClat(mNetwork)) {
+ // NAT64 prefix detected. Start clatd.
+ start(); // Enters STARTING state.
+ return;
+ }
+ if (!requiresClat(mNetwork)) {
+ // IPv4 address added. Go back to IDLE state.
+ stopPrefixDiscovery();
+ return;
+ }
+ break;
+
+ case STARTING:
+ case RUNNING:
+ // NAT64 prefix removed, or IPv4 address added.
+ // Stop clatd and go back into DISCOVERING or idle.
+ if (!shouldStartClat(mNetwork)) {
+ stop();
+ }
+ break;
+ // TODO: support the NAT64 prefix changing after it's been discovered. There is
+ // no need to support this at the moment because it cannot happen without
+ // changes to the Dns64Configuration code in netd.
}
}