junyulai | 48eac1d4 | 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 | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
junyulai | 48eac1d4 | 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 | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 34 | private final int mResourceId; |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 35 | |
| 36 | NattSocketKeepalive(@NonNull IConnectivityManager service, |
| 37 | @NonNull Network network, |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 38 | @NonNull ParcelFileDescriptor pfd, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 39 | int resourceId, |
junyulai | 48eac1d4 | 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 | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 44 | super(service, network, pfd, executor, callback); |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 45 | mSource = source; |
| 46 | mDestination = destination; |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 47 | mResourceId = resourceId; |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | void startImpl(int intervalSec) { |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 52 | mExecutor.execute(() -> { |
| 53 | try { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 54 | mService.startNattKeepaliveWithFd(mNetwork, mPfd.getFileDescriptor(), mResourceId, |
| 55 | intervalSec, mCallback, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 56 | mSource.getHostAddress(), mDestination.getHostAddress()); |
| 57 | } catch (RemoteException e) { |
| 58 | Log.e(TAG, "Error starting socket keepalive: ", e); |
| 59 | throw e.rethrowFromSystemServer(); |
| 60 | } |
| 61 | }); |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | void stopImpl() { |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 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(); |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 74 | } |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 75 | }); |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 76 | } |
| 77 | } |