junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.annotation.NonNull; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.net.InetAddress; |
| 25 | import java.util.concurrent.Executor; |
| 26 | |
| 27 | /** @hide */ |
| 28 | public 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; |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 34 | private final int mResourceId; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 35 | |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 36 | public NattSocketKeepalive(@NonNull IConnectivityManager service, |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 37 | @NonNull Network network, |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 38 | @NonNull ParcelFileDescriptor pfd, |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 39 | int resourceId, |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 40 | @NonNull InetAddress source, |
| 41 | @NonNull InetAddress destination, |
| 42 | @NonNull Executor executor, |
| 43 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 44 | super(service, network, pfd, executor, callback); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 45 | mSource = source; |
| 46 | mDestination = destination; |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 47 | mResourceId = resourceId; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 48 | } |
| 49 | |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 50 | /** |
| 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. |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 69 | * @param underpinnedNetwork The underpinned network of this keepalive. |
| 70 | * |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 71 | * @hide |
| 72 | */ |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 73 | @Override |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 74 | protected void startImpl(int intervalSec, int flags, Network underpinnedNetwork) { |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 75 | 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); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 80 | mExecutor.execute(() -> { |
| 81 | try { |
Chiachang Wang | 04a34b6 | 2021-01-19 15:35:03 +0800 | [diff] [blame] | 82 | mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId, |
chiachangwang | 9ef4ffe | 2023-01-18 01:19:27 +0000 | [diff] [blame] | 83 | intervalSec, mCallback, mSource.getHostAddress(), |
chiachangwang | 676c84e | 2023-02-14 09:22:05 +0000 | [diff] [blame] | 84 | mDestination.getHostAddress(), automaticOnOffKeepalives, |
| 85 | underpinnedNetwork); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 86 | } catch (RemoteException e) { |
| 87 | Log.e(TAG, "Error starting socket keepalive: ", e); |
| 88 | throw e.rethrowFromSystemServer(); |
| 89 | } |
| 90 | }); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Remi NGUYEN VAN | e55a88d | 2022-04-20 15:59:16 +0900 | [diff] [blame] | 94 | protected void stopImpl() { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 95 | mExecutor.execute(() -> { |
| 96 | try { |
Chalard Jean | f0b261e | 2023-02-03 22:11:20 +0900 | [diff] [blame] | 97 | mService.stopKeepalive(mCallback); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 98 | } catch (RemoteException e) { |
| 99 | Log.e(TAG, "Error stopping socket keepalive: ", e); |
| 100 | throw e.rethrowFromSystemServer(); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 101 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 102 | }); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 103 | } |
| 104 | } |