blob: 49622ae3a5789b950231b6711c26106a1555f1d9 [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() {
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
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();
50 return getErrno(c->blockPortForBind(port));
51}
52
53int AConnectivityNative_unblockPortForBind(in_port_t port) {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090054 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090055 std::shared_ptr<IConnectivityNative> c = getBinder();
56 return getErrno(c->unblockPortForBind(port));
57}
58
59int AConnectivityNative_unblockAllPortsForBind() {
Remi NGUYEN VAN2c1b9b82023-01-06 10:57:12 +090060 if (!android::modules::sdklevel::IsAtLeastU()) return ENOSYS;
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090061 std::shared_ptr<IConnectivityNative> c = getBinder();
62 return getErrno(c->unblockAllPortsForBind());
63}
64
65int AConnectivityNative_getPortsBlockedForBind(in_port_t *ports, size_t *count) {
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();
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}