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 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 @Override startImpl(int intervalSec)51 void startImpl(int intervalSec) { 52 mExecutor.execute(() -> { 53 try { 54 mService.startNattKeepaliveWithFd(mNetwork, mPfd.getFileDescriptor(), mResourceId, 55 intervalSec, mCallback, 56 mSource.getHostAddress(), mDestination.getHostAddress()); 57 } catch (RemoteException e) { 58 Log.e(TAG, "Error starting socket keepalive: ", e); 59 throw e.rethrowFromSystemServer(); 60 } 61 }); 62 } 63 64 @Override stopImpl()65 void stopImpl() { 66 mExecutor.execute(() -> { 67 try { 68 if (mSlot != null) { 69 mService.stopKeepalive(mNetwork, mSlot); 70 } 71 } catch (RemoteException e) { 72 Log.e(TAG, "Error stopping socket keepalive: ", e); 73 throw e.rethrowFromSystemServer(); 74 } 75 }); 76 } 77 } 78