diff --git a/framework/src/android/net/NetworkAgent.java b/framework/src/android/net/NetworkAgent.java index 8fe20de223..177f7e3d17 100644 --- a/framework/src/android/net/NetworkAgent.java +++ b/framework/src/android/net/NetworkAgent.java @@ -281,9 +281,8 @@ public abstract class NetworkAgent { * * arg1 = the hardware slot number of the keepalive to start * arg2 = interval in seconds - * obj = AutomaticKeepaliveInfo object + * obj = KeepalivePacketData object describing the data to be sent * - * Also used internally by ConnectivityService / KeepaliveTracker, with different semantics. * @hide */ public static final int CMD_START_SOCKET_KEEPALIVE = BASE + 11; @@ -435,6 +434,14 @@ public abstract class NetworkAgent { */ public static final int CMD_DSCP_POLICY_STATUS = BASE + 28; + /** + * Sent by the NetworkAgent to ConnectivityService to notify that this network is expected to be + * replaced within the specified time by a similar network. + * arg1 = timeout in milliseconds + * @hide + */ + public static final int EVENT_UNREGISTER_AFTER_REPLACEMENT = BASE + 29; + /** * DSCP policy was successfully added. */ @@ -477,27 +484,6 @@ public abstract class NetworkAgent { @Retention(RetentionPolicy.SOURCE) public @interface DscpPolicyStatus {} - /** - * Sent by the NetworkAgent to ConnectivityService to notify that this network is expected to be - * replaced within the specified time by a similar network. - * arg1 = timeout in milliseconds - * @hide - */ - public static final int EVENT_UNREGISTER_AFTER_REPLACEMENT = BASE + 29; - - /** - * Sent by AutomaticOnOffKeepaliveTracker periodically (when relevant) to trigger monitor - * automatic keepalive request. - * - * NATT keepalives have an automatic mode where the system only sends keepalive packets when - * TCP sockets are open over a VPN. The system will check periodically for presence of - * such open sockets, and this message is what triggers the re-evaluation. - * - * obj = A Binder object associated with the keepalive. - * @hide - */ - public static final int CMD_MONITOR_AUTOMATIC_KEEPALIVE = BASE + 30; - private static NetworkInfo getLegacyNetworkInfo(final NetworkAgentConfig config) { final NetworkInfo ni = new NetworkInfo(config.legacyType, config.legacySubType, config.legacyTypeName, config.legacySubTypeName); diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 2af30ddc4b..42984d9939 100755 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -463,7 +463,11 @@ public class ConnectivityService extends IConnectivityManager.Stub private String mCurrentTcpBufferSizes; private static final SparseArray sMagicDecoderRing = MessageUtils.findMessageNames( - new Class[] { ConnectivityService.class, NetworkAgent.class, NetworkAgentInfo.class }); + new Class[] { + ConnectivityService.class, + NetworkAgent.class, + NetworkAgentInfo.class, + AutomaticOnOffKeepaliveTracker.class }); private enum ReapUnvalidatedNetworks { // Tear down networks that have no chance (e.g. even if validated) of becoming @@ -5605,12 +5609,13 @@ public class ConnectivityService extends IConnectivityManager.Stub handleConfigureAlwaysOnNetworks(); break; } - // Sent by KeepaliveTracker to process an app request on the state machine thread. - case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: { + // Sent by AutomaticOnOffKeepaliveTracker to process an app request on the + // handler thread. + case AutomaticOnOffKeepaliveTracker.CMD_REQUEST_START_KEEPALIVE: { mKeepaliveTracker.handleStartKeepalive(msg); break; } - case NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE: { + case AutomaticOnOffKeepaliveTracker.CMD_MONITOR_AUTOMATIC_KEEPALIVE: { final AutomaticOnOffKeepalive ki = mKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj); if (null == ki) return; // The callback was unregistered before the alarm fired diff --git a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java index acce95dc77..881c92d308 100644 --- a/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java +++ b/service/src/com/android/server/connectivity/AutomaticOnOffKeepaliveTracker.java @@ -16,7 +16,6 @@ package com.android.server.connectivity; -import static android.net.NetworkAgent.CMD_START_SOCKET_KEEPALIVE; import static android.net.SocketKeepalive.ERROR_INVALID_SOCKET; import static android.net.SocketKeepalive.MIN_INTERVAL_SEC; import static android.net.SocketKeepalive.SUCCESS_PAUSED; @@ -40,7 +39,6 @@ import android.net.INetd; import android.net.ISocketKeepaliveCallback; import android.net.MarkMaskParcel; import android.net.Network; -import android.net.NetworkAgent; import android.net.SocketKeepalive.InvalidSocketException; import android.os.FileUtils; import android.os.Handler; @@ -94,6 +92,29 @@ public class AutomaticOnOffKeepaliveTracker { private static final int ADJUST_TCP_POLLING_DELAY_MS = 2000; private static final String AUTOMATIC_ON_OFF_KEEPALIVE_VERSION = "automatic_on_off_keepalive_version"; + + // ConnectivityService parses message constants from itself and AutomaticOnOffKeepaliveTracker + // with MessageUtils for debugging purposes, and crashes if some messages have the same values. + private static final int BASE = 2000; + /** + * Sent by AutomaticOnOffKeepaliveTracker periodically (when relevant) to trigger monitor + * automatic keepalive request. + * + * NATT keepalives have an automatic mode where the system only sends keepalive packets when + * TCP sockets are open over a VPN. The system will check periodically for presence of + * such open sockets, and this message is what triggers the re-evaluation. + * + * obj = A Binder object associated with the keepalive. + */ + public static final int CMD_MONITOR_AUTOMATIC_KEEPALIVE = BASE + 1; + + /** + * Sent by AutomaticOnOffKeepaliveTracker to ConnectivityService to start a keepalive. + * + * obj = AutomaticKeepaliveInfo object + */ + public static final int CMD_REQUEST_START_KEEPALIVE = BASE + 2; + /** * States for {@code #AutomaticOnOffKeepalive}. * @@ -202,7 +223,7 @@ public class AutomaticOnOffKeepaliveTracker { throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); } mAlarmListener = () -> mConnectivityServiceHandler.obtainMessage( - NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE, mCallback.asBinder()) + CMD_MONITOR_AUTOMATIC_KEEPALIVE, mCallback.asBinder()) .sendToTarget(); } else { mAutomaticOnOffState = STATE_ALWAYS_ON; @@ -482,9 +503,8 @@ public class AutomaticOnOffKeepaliveTracker { + " → " + dstAddrString + ":" + dstPort + " auto=" + autoKi + " underpinned=" + underpinnedNetwork); - mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE, - // TODO : move ConnectivityService#encodeBool to a static lib. - automaticOnOffKeepalives ? 1 : 0, 0, autoKi).sendToTarget(); + mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi) + .sendToTarget(); } catch (InvalidSocketException e) { mKeepaliveTracker.notifyErrorCallback(cb, e.error); } @@ -517,9 +537,8 @@ public class AutomaticOnOffKeepaliveTracker { + " → " + dstAddrString + ":" + dstPort + " auto=" + autoKi + " underpinned=" + underpinnedNetwork); - mConnectivityServiceHandler.obtainMessage(NetworkAgent.CMD_START_SOCKET_KEEPALIVE, - // TODO : move ConnectivityService#encodeBool to a static lib. - automaticOnOffKeepalives ? 1 : 0, 0, autoKi).sendToTarget(); + mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi) + .sendToTarget(); } catch (InvalidSocketException e) { mKeepaliveTracker.notifyErrorCallback(cb, e.error); } @@ -547,7 +566,7 @@ public class AutomaticOnOffKeepaliveTracker { final AutomaticOnOffKeepalive autoKi = new AutomaticOnOffKeepalive(ki, false /* autoOnOff, tcp keepalives are never auto on/off */, null /* underpinnedNetwork, tcp keepalives do not refer to this */); - mConnectivityServiceHandler.obtainMessage(CMD_START_SOCKET_KEEPALIVE, autoKi) + mConnectivityServiceHandler.obtainMessage(CMD_REQUEST_START_KEEPALIVE, autoKi) .sendToTarget(); } catch (InvalidSocketException e) { mKeepaliveTracker.notifyErrorCallback(cb, e.error); diff --git a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java index 696eff47a8..3eb1b26add 100644 --- a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java +++ b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java @@ -47,7 +47,6 @@ import android.net.LinkProperties; import android.net.MarkMaskParcel; import android.net.NattKeepalivePacketData; import android.net.Network; -import android.net.NetworkAgent; import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.os.Binder; @@ -268,11 +267,11 @@ public class AutomaticOnOffKeepaliveTrackerTest { @Override public void handleMessage(@NonNull final Message msg) { switch (msg.what) { - case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: - Log.d(TAG, "Test handler received CMD_START_SOCKET_KEEPALIVE : " + msg); + case AutomaticOnOffKeepaliveTracker.CMD_REQUEST_START_KEEPALIVE: + Log.d(TAG, "Test handler received CMD_REQUEST_START_KEEPALIVE : " + msg); mAOOKeepaliveTracker.handleStartKeepalive(msg); break; - case NetworkAgent.CMD_MONITOR_AUTOMATIC_KEEPALIVE: + case AutomaticOnOffKeepaliveTracker.CMD_MONITOR_AUTOMATIC_KEEPALIVE: Log.d(TAG, "Test handler received CMD_MONITOR_AUTOMATIC_KEEPALIVE : " + msg); mLastAutoKi = mAOOKeepaliveTracker.getKeepaliveForBinder((IBinder) msg.obj); break;