blob: 9545ed1ec0692a98c090088f68f4fadc67a56350 [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>
20#include <aidl/android/net/connectivity/aidl/ConnectivityNative.h>
21
22using aidl::android::net::connectivity::aidl::IConnectivityNative;
23
24
25static std::shared_ptr<IConnectivityNative> getBinder() {
26 static ndk::SpAIBinder sBinder = ndk::SpAIBinder(reinterpret_cast<AIBinder*>(
27 AServiceManager_getService("connectivity_native")));
28 return aidl::android::net::connectivity::aidl::IConnectivityNative::fromBinder(sBinder);
29}
30
31static int getErrno(const ::ndk::ScopedAStatus& status) {
32 switch (status.getExceptionCode()) {
33 case EX_NONE:
34 return 0;
35 case EX_ILLEGAL_ARGUMENT:
36 return EINVAL;
37 case EX_SECURITY:
38 return EPERM;
39 case EX_SERVICE_SPECIFIC:
40 return status.getServiceSpecificError();
41 default:
42 return EPROTO;
43 }
44}
45
46int AConnectivityNative_blockPortForBind(in_port_t port) {
47 std::shared_ptr<IConnectivityNative> c = getBinder();
48 return getErrno(c->blockPortForBind(port));
49}
50
51int AConnectivityNative_unblockPortForBind(in_port_t port) {
52 std::shared_ptr<IConnectivityNative> c = getBinder();
53 return getErrno(c->unblockPortForBind(port));
54}
55
56int AConnectivityNative_unblockAllPortsForBind() {
57 std::shared_ptr<IConnectivityNative> c = getBinder();
58 return getErrno(c->unblockAllPortsForBind());
59}
60
61int AConnectivityNative_getPortsBlockedForBind(in_port_t *ports, size_t *count) {
62 std::shared_ptr<IConnectivityNative> c = getBinder();
63 std::vector<int32_t> actualBlockedPorts;
64 int err = getErrno(c->getPortsBlockedForBind(&actualBlockedPorts));
65 if (err) {
66 return err;
67 }
68
69 for (int i = 0; i < *count && i < actualBlockedPorts.size(); i++) {
70 ports[i] = actualBlockedPorts[i];
71 }
72 *count = actualBlockedPorts.size();
73 return 0;
74}