blob: a476498ecc6bc38048f5f80564228e750d769533 [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() {
Tyler Weardf6cf302023-03-08 11:22:10 -080026 ndk::SpAIBinder sBinder = ndk::SpAIBinder(reinterpret_cast<AIBinder*>(
27 AServiceManager_checkService("connectivity_native")));
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090028 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();
Tyler Weardf6cf302023-03-08 11:22:10 -080048 if (!c) {
49 return EAGAIN;
50 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090051 return getErrno(c->blockPortForBind(port));
52}
53
54int AConnectivityNative_unblockPortForBind(in_port_t port) {
55 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080056 if (!c) {
57 return EAGAIN;
58 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090059 return getErrno(c->unblockPortForBind(port));
60}
61
62int AConnectivityNative_unblockAllPortsForBind() {
63 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080064 if (!c) {
65 return EAGAIN;
66 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090067 return getErrno(c->unblockAllPortsForBind());
68}
69
70int AConnectivityNative_getPortsBlockedForBind(in_port_t *ports, size_t *count) {
71 std::shared_ptr<IConnectivityNative> c = getBinder();
Tyler Weardf6cf302023-03-08 11:22:10 -080072 if (!c) {
73 return EAGAIN;
74 }
Remi NGUYEN VANfb70eba2022-04-04 20:26:16 +090075 std::vector<int32_t> actualBlockedPorts;
76 int err = getErrno(c->getPortsBlockedForBind(&actualBlockedPorts));
77 if (err) {
78 return err;
79 }
80
81 for (int i = 0; i < *count && i < actualBlockedPorts.size(); i++) {
82 ports[i] = actualBlockedPorts[i];
83 }
84 *count = actualBlockedPorts.size();
85 return 0;
86}