blob: f51cd9bc996883869e3785dbbf626634a6140ed7 [file] [log] [blame]
Victor Hsiehd35d31d2021-06-03 11:24:31 -07001/*
2 * Copyright (C) 2021 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
David Brazdilefb56832022-11-16 11:47:00 +000017#include <binder_rpc_unstable.hpp>
18
Victor Hsiehd35d31d2021-06-03 11:24:31 -070019#include <android-base/logging.h>
Inseob Kime12fccc2021-09-02 17:23:33 +090020#include <android-base/unique_fd.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070021#include <android/binder_libbinder.h>
22#include <binder/RpcServer.h>
23#include <binder/RpcSession.h>
Alice Wang893a9912022-10-24 10:44:09 +000024#include <cutils/sockets.h>
Inseob Kim091050a2021-10-25 14:25:25 +000025#include <linux/vm_sockets.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070026
Steven Moreland2372f9d2021-08-05 15:42:01 -070027using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070028using android::RpcServer;
29using android::RpcSession;
David Brazdilefb56832022-11-16 11:47:00 +000030using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070031using android::status_t;
32using android::statusToString;
Inseob Kime12fccc2021-09-02 17:23:33 +090033using android::base::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070034
David Brazdilefb56832022-11-16 11:47:00 +000035// Opaque handle for RpcServer.
36struct ARpcServer {};
Victor Hsiehd35d31d2021-06-03 11:24:31 -070037
David Brazdilae3ec6a2022-12-14 13:13:32 +000038// Opaque handle for RpcSession.
39struct ARpcSession {};
Alice Wang893a9912022-10-24 10:44:09 +000040
David Brazdilae3ec6a2022-12-14 13:13:32 +000041template <typename A, typename T>
42static A* createObjectHandle(sp<T>& server) {
David Brazdilefb56832022-11-16 11:47:00 +000043 auto ref = server.get();
44 ref->incStrong(ref);
David Brazdilae3ec6a2022-12-14 13:13:32 +000045 return reinterpret_cast<A*>(ref);
David Brazdilefb56832022-11-16 11:47:00 +000046}
47
David Brazdilae3ec6a2022-12-14 13:13:32 +000048template <typename T, typename A>
49static void freeObjectHandle(A* handle) {
50 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
51 auto ref = reinterpret_cast<T*>(handle);
David Brazdilefb56832022-11-16 11:47:00 +000052 ref->decStrong(ref);
53}
54
David Brazdilae3ec6a2022-12-14 13:13:32 +000055template <typename T, typename A>
56static sp<T> handleToStrongPointer(A* handle) {
57 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
58 auto ref = reinterpret_cast<T*>(handle);
59 return sp<T>::fromExisting(ref);
60}
61
62RpcSession::FileDescriptorTransportMode toTransportMode(
63 ARpcSession_FileDescriptorTransportMode mode) {
64 switch (mode) {
65 case ARpcSession_FileDescriptorTransportMode::None:
66 return RpcSession::FileDescriptorTransportMode::NONE;
67 case ARpcSession_FileDescriptorTransportMode::Unix:
68 return RpcSession::FileDescriptorTransportMode::UNIX;
69 case ARpcSession_FileDescriptorTransportMode::Trusty:
70 return RpcSession::FileDescriptorTransportMode::TRUSTY;
71 default:
72 return RpcSession::FileDescriptorTransportMode::NONE;
73 }
74}
75
David Brazdilefb56832022-11-16 11:47:00 +000076extern "C" {
77
David Brazdila47dfda2022-11-22 22:52:19 +000078ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070079 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000080
81 unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface
82 if (cid == VMADDR_CID_LOCAL) {
83 bindCid = VMADDR_CID_LOCAL; // bind to the local interface
84 cid = VMADDR_CID_ANY; // no need for a connection filter
85 }
86
87 if (status_t status = server->setupVsockServer(bindCid, port); status != OK) {
Steven Moreland2372f9d2021-08-05 15:42:01 -070088 LOG(ERROR) << "Failed to set up vsock server with port " << port
89 << " error: " << statusToString(status).c_str();
David Brazdilefb56832022-11-16 11:47:00 +000090 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070091 }
David Brazdila47dfda2022-11-22 22:52:19 +000092 if (cid != VMADDR_CID_ANY) {
93 server->setConnectionFilter([=](const void* addr, size_t addrlen) {
David Brazdilb96737d2022-12-14 14:03:42 +000094 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
95 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
96 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
97 if (cid != vaddr->svm_cid) {
98 LOG(ERROR) << "Rejected vsock connection from CID " << vaddr->svm_cid;
David Brazdila47dfda2022-11-22 22:52:19 +000099 return false;
100 }
101 return true;
102 });
103 }
David Brazdilefb56832022-11-16 11:47:00 +0000104 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000105 return createObjectHandle<ARpcServer>(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700106}
107
Alice Wang072c3982023-05-25 09:45:13 +0000108ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd) {
David Brazdilefb56832022-11-16 11:47:00 +0000109 auto server = RpcServer::make();
Alice Wang072c3982023-05-25 09:45:13 +0000110 auto fd = unique_fd(socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000111 if (!fd.ok()) {
Alice Wang072c3982023-05-25 09:45:13 +0000112 LOG(ERROR) << "Invalid socket fd " << socketFd;
David Brazdilefb56832022-11-16 11:47:00 +0000113 return nullptr;
114 }
115 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
Alice Wang072c3982023-05-25 09:45:13 +0000116 LOG(ERROR) << "Failed to set up RPC server with fd " << socketFd
David Brazdilefb56832022-11-16 11:47:00 +0000117 << " error: " << statusToString(status).c_str();
118 return nullptr;
119 }
120 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000121 return createObjectHandle<ARpcServer>(server);
122}
123
David Brazdile1bd9832022-12-14 20:22:09 +0000124ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd) {
125 auto server = RpcServer::make();
126 auto fd = unique_fd(bootstrapFd);
127 if (!fd.ok()) {
128 LOG(ERROR) << "Invalid bootstrap fd " << bootstrapFd;
129 return nullptr;
130 }
131 if (status_t status = server->setupUnixDomainSocketBootstrapServer(std::move(fd));
132 status != OK) {
133 LOG(ERROR) << "Failed to set up Unix Domain RPC server with bootstrap fd " << bootstrapFd
134 << " error: " << statusToString(status).c_str();
135 return nullptr;
136 }
137 server->setRootObject(AIBinder_toPlatformBinder(service));
138 return createObjectHandle<ARpcServer>(server);
139}
140
Yiming Jing34637062023-02-05 14:05:32 -0800141ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address, unsigned int port) {
142 auto server = RpcServer::make();
143 if (status_t status = server->setupInetServer(address, port, nullptr); status != OK) {
144 LOG(ERROR) << "Failed to set up inet RPC server with address " << address << " and port "
145 << port << " error: " << statusToString(status).c_str();
146 return nullptr;
147 }
148 server->setRootObject(AIBinder_toPlatformBinder(service));
149 return createObjectHandle<ARpcServer>(server);
150}
151
David Brazdilae3ec6a2022-12-14 13:13:32 +0000152void ARpcServer_setSupportedFileDescriptorTransportModes(
153 ARpcServer* handle, const ARpcSession_FileDescriptorTransportMode modes[],
154 size_t modes_len) {
155 auto server = handleToStrongPointer<RpcServer>(handle);
156 std::vector<RpcSession::FileDescriptorTransportMode> modevec;
157 for (size_t i = 0; i < modes_len; i++) {
158 modevec.push_back(toTransportMode(modes[i]));
159 }
160 server->setSupportedFileDescriptorTransportModes(modevec);
David Brazdilefb56832022-11-16 11:47:00 +0000161}
162
163void ARpcServer_start(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000164 handleToStrongPointer<RpcServer>(handle)->start();
David Brazdilefb56832022-11-16 11:47:00 +0000165}
166
167void ARpcServer_join(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000168 handleToStrongPointer<RpcServer>(handle)->join();
David Brazdilefb56832022-11-16 11:47:00 +0000169}
170
David Brazdil793e8792022-12-19 23:16:30 +0000171bool ARpcServer_shutdown(ARpcServer* handle) {
172 return handleToStrongPointer<RpcServer>(handle)->shutdown();
David Brazdilefb56832022-11-16 11:47:00 +0000173}
174
175void ARpcServer_free(ARpcServer* handle) {
David Brazdil4766a1f2022-12-19 21:58:25 +0000176 // Ignore the result of ARpcServer_shutdown - either it had been called
177 // earlier, or the RpcServer destructor will panic.
178 (void)ARpcServer_shutdown(handle);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000179 freeObjectHandle<RpcServer>(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900180}
181
David Brazdilae3ec6a2022-12-14 13:13:32 +0000182ARpcSession* ARpcSession_new() {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700183 auto session = RpcSession::make();
David Brazdilae3ec6a2022-12-14 13:13:32 +0000184 return createObjectHandle<ARpcSession>(session);
185}
186
187void ARpcSession_free(ARpcSession* handle) {
188 freeObjectHandle<RpcSession>(handle);
189}
190
191AIBinder* ARpcSession_setupVsockClient(ARpcSession* handle, unsigned int cid, unsigned int port) {
192 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700193 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
194 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port
195 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700196 return nullptr;
197 }
198 return AIBinder_fromPlatformBinder(session->getRootObject());
199}
Inseob Kime12fccc2021-09-02 17:23:33 +0900200
David Brazdilae3ec6a2022-12-14 13:13:32 +0000201AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* handle, const char* name) {
Alice Wang893a9912022-10-24 10:44:09 +0000202 std::string pathname(name);
203 pathname = ANDROID_SOCKET_DIR "/" + pathname;
David Brazdilae3ec6a2022-12-14 13:13:32 +0000204 auto session = handleToStrongPointer<RpcSession>(handle);
Alice Wang893a9912022-10-24 10:44:09 +0000205 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
206 LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname
207 << " error: " << statusToString(status).c_str();
208 return nullptr;
209 }
210 return AIBinder_fromPlatformBinder(session->getRootObject());
211}
212
David Brazdile1bd9832022-12-14 20:22:09 +0000213AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* handle, int bootstrapFd) {
214 auto session = handleToStrongPointer<RpcSession>(handle);
215 auto fd = unique_fd(dup(bootstrapFd));
216 if (!fd.ok()) {
217 LOG(ERROR) << "Invalid bootstrap fd " << bootstrapFd;
218 return nullptr;
219 }
220 if (status_t status = session->setupUnixDomainSocketBootstrapClient(std::move(fd));
221 status != OK) {
222 LOG(ERROR) << "Failed to set up Unix Domain RPC client with bootstrap fd: " << bootstrapFd
223 << " error: " << statusToString(status).c_str();
224 return nullptr;
225 }
226 return AIBinder_fromPlatformBinder(session->getRootObject());
227}
228
Yiming Jing34637062023-02-05 14:05:32 -0800229AIBinder* ARpcSession_setupInet(ARpcSession* handle, const char* address, unsigned int port) {
230 auto session = handleToStrongPointer<RpcSession>(handle);
231 if (status_t status = session->setupInetClient(address, port); status != OK) {
232 LOG(ERROR) << "Failed to set up inet RPC client with address " << address << " and port "
233 << port << " error: " << statusToString(status).c_str();
234 return nullptr;
235 }
236 return AIBinder_fromPlatformBinder(session->getRootObject());
237}
238
David Brazdilae3ec6a2022-12-14 13:13:32 +0000239AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* handle, int (*requestFd)(void* param),
240 void* param) {
241 auto session = handleToStrongPointer<RpcSession>(handle);
Inseob Kime12fccc2021-09-02 17:23:33 +0900242 auto request = [=] { return unique_fd{requestFd(param)}; };
243 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
244 LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str();
245 return nullptr;
246 }
247 return AIBinder_fromPlatformBinder(session->getRootObject());
248}
David Brazdilae3ec6a2022-12-14 13:13:32 +0000249
250void ARpcSession_setFileDescriptorTransportMode(ARpcSession* handle,
251 ARpcSession_FileDescriptorTransportMode mode) {
252 auto session = handleToStrongPointer<RpcSession>(handle);
253 session->setFileDescriptorTransportMode(toTransportMode(mode));
254}
255
256void ARpcSession_setMaxIncomingThreads(ARpcSession* handle, size_t threads) {
257 auto session = handleToStrongPointer<RpcSession>(handle);
258 session->setMaxIncomingThreads(threads);
259}
260
Steven Morelande65f73c2023-03-04 01:34:50 +0000261void ARpcSession_setMaxOutgoingConnections(ARpcSession* handle, size_t connections) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000262 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Morelande65f73c2023-03-04 01:34:50 +0000263 session->setMaxOutgoingConnections(connections);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000264}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700265}