blob: f39bb0a4a88acdc4cc94a85e79887ddba1999b8f [file] [log] [blame]
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +09001/*
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 VAN2c1b9b82023-01-06 10:57:12 +090020#include <android-modules-utils/sdk_level.h>
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090021#include <aidl/android/net/connectivity/aidl/ConnectivityNative.h>
22
23using aidl::android::net::connectivity::aidl::IConnectivityNative;
24
25
26static std::shared_ptr<IConnectivityNative> getBinder() {
Tyler Weardf6cf302023-03-08 11:22:10 -080027 ndk::SpAIBinder sBinder = ndk::SpAIBinder(reinterpret_cast<AIBinder*>(
28 AServiceManager_checkService("connectivity_native")));
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090029 return aidl::android::net::connectivity::aidl::IConnectivityNative::fromBinder(sBinder);
30}
31
32static 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
47int AConnectivityNative_blockPortForBind(in_port_t port) {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090048 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090049 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080050 if (!c) {
51 return EAGAIN;
52 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090053 return getErrno(c->blockPortForBind(port));
54}
55
56int AConnectivityNative_unblockPortForBind(in_port_t port) {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090057 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090058 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080059 if (!c) {
60 return EAGAIN;
61 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090062 return getErrno(c->unblockPortForBind(port));
63}
64
65int AConnectivityNative_unblockAllPortsForBind() {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090066 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090067 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080068 if (!c) {
69 return EAGAIN;
70 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090071 return getErrno(c->unblockAllPortsForBind());
72}
73
74int AConnectivityNative_getPortsBlockedForBind(in_port_t *ports, size_t *count) {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090075 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090076 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080077 if (!c) {
78 return EAGAIN;
79 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090080 std::vector<int32_t> actualBlockedPorts;
81 int err = getErrno(c->getPortsBlockedForBind(&actualBlockedPorts));
82 if (err) {
83 return err;
84 }
85
86 for (int i = 0; i < *count && i < actualBlockedPorts.size(); i++) {
87 ports[i] = actualBlockedPorts[i];
88 }
89 *count = actualBlockedPorts.size();
90 return 0;
91}