Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #include "connectivity_native.h" |
| 18 | |
| 19 | #include <android/binder_manager.h> |
Remi NGUYEN VAN | 2c1b9b8 | 2023-01-06 10:57:12 +0900 | [diff] [blame^] | 20 | #include <android-modules-utils/sdk_level.h> |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 21 | #include <aidl/android/net/connectivity/aidl/ConnectivityNative.h> |
| 22 | |
| 23 | using aidl::android::net::connectivity::aidl::IConnectivityNative; |
| 24 | |
| 25 | |
| 26 | static std::shared_ptr<IConnectivityNative> getBinder() { |
| 27 | static ndk::SpAIBinder sBinder = ndk::SpAIBinder(reinterpret_cast<AIBinder*>( |
| 28 | AServiceManager_getService("connectivity_native"))); |
| 29 | return aidl::android::net::connectivity::aidl::IConnectivityNative::fromBinder(sBinder); |
| 30 | } |
| 31 | |
| 32 | static int getErrno(const ::ndk::ScopedAStatus& status) { |
| 33 | switch (status.getExceptionCode()) { |
| 34 | case EX_NONE: |
| 35 | return 0; |
| 36 | case EX_ILLEGAL_ARGUMENT: |
| 37 | return EINVAL; |
| 38 | case EX_SECURITY: |
| 39 | return EPERM; |
| 40 | case EX_SERVICE_SPECIFIC: |
| 41 | return status.getServiceSpecificError(); |
| 42 | default: |
| 43 | return EPROTO; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int AConnectivityNative_blockPortForBind(in_port_t port) { |
Remi NGUYEN VAN | 2c1b9b8 | 2023-01-06 10:57:12 +0900 | [diff] [blame^] | 48 | if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS; |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 49 | std::shared_ptr<IConnectivityNative> c = getBinder(); |
| 50 | return getErrno(c->blockPortForBind(port)); |
| 51 | } |
| 52 | |
| 53 | int AConnectivityNative_unblockPortForBind(in_port_t port) { |
Remi NGUYEN VAN | 2c1b9b8 | 2023-01-06 10:57:12 +0900 | [diff] [blame^] | 54 | if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS; |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 55 | std::shared_ptr<IConnectivityNative> c = getBinder(); |
| 56 | return getErrno(c->unblockPortForBind(port)); |
| 57 | } |
| 58 | |
| 59 | int AConnectivityNative_unblockAllPortsForBind() { |
Remi NGUYEN VAN | 2c1b9b8 | 2023-01-06 10:57:12 +0900 | [diff] [blame^] | 60 | if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS; |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 61 | std::shared_ptr<IConnectivityNative> c = getBinder(); |
| 62 | return getErrno(c->unblockAllPortsForBind()); |
| 63 | } |
| 64 | |
| 65 | int AConnectivityNative_getPortsBlockedForBind(in_port_t *ports, size_t *count) { |
Remi NGUYEN VAN | 2c1b9b8 | 2023-01-06 10:57:12 +0900 | [diff] [blame^] | 66 | if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS; |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 67 | std::shared_ptr<IConnectivityNative> c = getBinder(); |
| 68 | std::vector<int32_t> actualBlockedPorts; |
| 69 | int err = getErrno(c->getPortsBlockedForBind(&actualBlockedPorts)); |
| 70 | if (err) { |
| 71 | return err; |
| 72 | } |
| 73 | |
| 74 | for (int i = 0; i < *count && i < actualBlockedPorts.size(); i++) { |
| 75 | ports[i] = actualBlockedPorts[i]; |
| 76 | } |
| 77 | *count = actualBlockedPorts.size(); |
| 78 | return 0; |
| 79 | } |