blob: a83b5b4ac6e62f189096447baf6377e4225e7c13 [file] [log] [blame]
junyulai4c95b082018-12-27 17:25:29 +08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import android.annotation.NonNull;
junyulai7e06ad42019-03-04 22:45:36 +080020import android.os.ParcelFileDescriptor;
junyulai4c95b082018-12-27 17:25:29 +080021import android.os.RemoteException;
22import android.util.Log;
23
24import java.net.InetAddress;
25import java.util.concurrent.Executor;
26
27/** @hide */
28public final class NattSocketKeepalive extends SocketKeepalive {
29 /** The NAT-T destination port for IPsec */
30 public static final int NATT_PORT = 4500;
31
32 @NonNull private final InetAddress mSource;
33 @NonNull private final InetAddress mDestination;
junyulaid05a1922019-01-15 11:32:44 +080034 private final int mResourceId;
junyulai4c95b082018-12-27 17:25:29 +080035
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090036 public NattSocketKeepalive(@NonNull IConnectivityManager service,
junyulai4c95b082018-12-27 17:25:29 +080037 @NonNull Network network,
junyulai7e06ad42019-03-04 22:45:36 +080038 @NonNull ParcelFileDescriptor pfd,
junyulaid05a1922019-01-15 11:32:44 +080039 int resourceId,
junyulai4c95b082018-12-27 17:25:29 +080040 @NonNull InetAddress source,
41 @NonNull InetAddress destination,
42 @NonNull Executor executor,
43 @NonNull Callback callback) {
junyulai7e06ad42019-03-04 22:45:36 +080044 super(service, network, pfd, executor, callback);
junyulai4c95b082018-12-27 17:25:29 +080045 mSource = source;
46 mDestination = destination;
junyulaid05a1922019-01-15 11:32:44 +080047 mResourceId = resourceId;
junyulai4c95b082018-12-27 17:25:29 +080048 }
49
chiachangwang9ef4ffe2023-01-18 01:19:27 +000050 /**
51 * Request that keepalive be started with the given {@code intervalSec}.
52 *
53 * When a VPN is running with the network for this keepalive as its underlying network, the
54 * system can monitor the TCP connections on that VPN to determine whether this keepalive is
55 * necessary. To enable this behavior, pass {@link SocketKeepalive#FLAG_AUTOMATIC_ON_OFF} into
56 * the flags. When this is enabled, the system will disable sending keepalive packets when
57 * there are no TCP connections over the VPN(s) running over this network to save battery, and
58 * restart sending them as soon as any TCP connection is opened over one of the VPN networks.
59 * When no VPN is running on top of this network, this flag has no effect, i.e. the keepalives
60 * are always sent with the specified interval.
61 *
62 * Also {@see SocketKeepalive}.
63 *
64 * @param intervalSec The target interval in seconds between keepalive packet transmissions.
65 * The interval should be between 10 seconds and 3600 seconds. Otherwise,
66 * the supplied {@link Callback} will see a call to
67 * {@link Callback#onError(int)} with {@link #ERROR_INVALID_INTERVAL}.
68 * @param flags Flags to enable/disable available options on this keepalive.
chiachangwang676c84e2023-02-14 09:22:05 +000069 * @param underpinnedNetwork The underpinned network of this keepalive.
70 *
chiachangwang9ef4ffe2023-01-18 01:19:27 +000071 * @hide
72 */
junyulai4c95b082018-12-27 17:25:29 +080073 @Override
chiachangwang676c84e2023-02-14 09:22:05 +000074 protected void startImpl(int intervalSec, int flags, Network underpinnedNetwork) {
chiachangwang9ef4ffe2023-01-18 01:19:27 +000075 if (0 != (flags & ~FLAG_AUTOMATIC_ON_OFF)) {
76 throw new IllegalArgumentException("Illegal flag value for "
77 + this.getClass().getSimpleName() + " : " + flags);
78 }
79 final boolean automaticOnOffKeepalives = 0 != (flags & FLAG_AUTOMATIC_ON_OFF);
junyulai070f9ff2019-01-16 20:23:34 +080080 mExecutor.execute(() -> {
81 try {
Chiachang Wang04a34b62021-01-19 15:35:03 +080082 mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId,
chiachangwang9ef4ffe2023-01-18 01:19:27 +000083 intervalSec, mCallback, mSource.getHostAddress(),
chiachangwang676c84e2023-02-14 09:22:05 +000084 mDestination.getHostAddress(), automaticOnOffKeepalives,
85 underpinnedNetwork);
junyulai070f9ff2019-01-16 20:23:34 +080086 } catch (RemoteException e) {
87 Log.e(TAG, "Error starting socket keepalive: ", e);
88 throw e.rethrowFromSystemServer();
89 }
90 });
junyulai4c95b082018-12-27 17:25:29 +080091 }
92
93 @Override
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090094 protected void stopImpl() {
junyulai070f9ff2019-01-16 20:23:34 +080095 mExecutor.execute(() -> {
96 try {
Chalard Jeanf0b261e2023-02-03 22:11:20 +090097 mService.stopKeepalive(mCallback);
junyulai070f9ff2019-01-16 20:23:34 +080098 } catch (RemoteException e) {
99 Log.e(TAG, "Error stopping socket keepalive: ", e);
100 throw e.rethrowFromSystemServer();
junyulai4c95b082018-12-27 17:25:29 +0800101 }
junyulai070f9ff2019-01-16 20:23:34 +0800102 });
junyulai4c95b082018-12-27 17:25:29 +0800103 }
104}