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; 20 import android.os.ParcelFileDescriptor; 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; 34 private final int mResourceId; 35 NattSocketKeepalive(@onNull IConnectivityManager service, @NonNull Network network, @NonNull ParcelFileDescriptor pfd, int resourceId, @NonNull InetAddress source, @NonNull InetAddress destination, @NonNull Executor executor, @NonNull Callback callback)36 public NattSocketKeepalive(@NonNull IConnectivityManager service, 37 @NonNull Network network, 38 @NonNull ParcelFileDescriptor pfd, 39 int resourceId, 40 @NonNull InetAddress source, 41 @NonNull InetAddress destination, 42 @NonNull Executor executor, 43 @NonNull Callback callback) { 44 super(service, network, pfd, executor, callback); 45 mSource = source; 46 mDestination = destination; 47 mResourceId = resourceId; 48 } 49 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. 69 * @param underpinnedNetwork The underpinned network of this keepalive. 70 * 71 * @hide 72 */ 73 @Override startImpl(int intervalSec, int flags, Network underpinnedNetwork)74 protected void startImpl(int intervalSec, int flags, Network underpinnedNetwork) { 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); 80 mExecutor.execute(() -> { 81 try { 82 mService.startNattKeepaliveWithFd(mNetwork, mPfd, mResourceId, 83 intervalSec, mCallback, mSource.getHostAddress(), 84 mDestination.getHostAddress(), automaticOnOffKeepalives, 85 underpinnedNetwork); 86 } catch (RemoteException e) { 87 Log.e(TAG, "Error starting socket keepalive: ", e); 88 throw e.rethrowFromSystemServer(); 89 } 90 }); 91 } 92 93 @Override stopImpl()94 protected void stopImpl() { 95 mExecutor.execute(() -> { 96 try { 97 mService.stopKeepalive(mCallback); 98 } catch (RemoteException e) { 99 Log.e(TAG, "Error stopping socket keepalive: ", e); 100 throw e.rethrowFromSystemServer(); 101 } 102 }); 103 } 104 } 105