blob: 184d3f36eec42523cdf84c265e7ee4d05d771c44 [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 Brazdilefb56832022-11-16 11:47:00 +000038static sp<RpcServer> toRpcServer(ARpcServer* handle) {
39 auto ref = reinterpret_cast<RpcServer*>(handle);
40 return sp<RpcServer>::fromExisting(ref);
Alice Wang893a9912022-10-24 10:44:09 +000041}
42
David Brazdilefb56832022-11-16 11:47:00 +000043static ARpcServer* createRpcServerHandle(sp<RpcServer>& server) {
44 auto ref = server.get();
45 ref->incStrong(ref);
46 return reinterpret_cast<ARpcServer*>(ref);
47}
48
49static void freeRpcServerHandle(ARpcServer* handle) {
50 auto ref = reinterpret_cast<RpcServer*>(handle);
51 ref->decStrong(ref);
52}
53
54extern "C" {
55
David Brazdila47dfda2022-11-22 22:52:19 +000056ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070057 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000058
59 unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface
60 if (cid == VMADDR_CID_LOCAL) {
61 bindCid = VMADDR_CID_LOCAL; // bind to the local interface
62 cid = VMADDR_CID_ANY; // no need for a connection filter
63 }
64
65 if (status_t status = server->setupVsockServer(bindCid, port); status != OK) {
Steven Moreland2372f9d2021-08-05 15:42:01 -070066 LOG(ERROR) << "Failed to set up vsock server with port " << port
67 << " error: " << statusToString(status).c_str();
David Brazdilefb56832022-11-16 11:47:00 +000068 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070069 }
David Brazdila47dfda2022-11-22 22:52:19 +000070 if (cid != VMADDR_CID_ANY) {
71 server->setConnectionFilter([=](const void* addr, size_t addrlen) {
David Brazdilb96737d2022-12-14 14:03:42 +000072 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
73 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
74 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
75 if (cid != vaddr->svm_cid) {
76 LOG(ERROR) << "Rejected vsock connection from CID " << vaddr->svm_cid;
David Brazdila47dfda2022-11-22 22:52:19 +000077 return false;
78 }
79 return true;
80 });
81 }
David Brazdilefb56832022-11-16 11:47:00 +000082 server->setRootObject(AIBinder_toPlatformBinder(service));
83 return createRpcServerHandle(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -070084}
85
David Brazdilefb56832022-11-16 11:47:00 +000086ARpcServer* ARpcServer_newInitUnixDomain(AIBinder* service, const char* name) {
87 auto server = RpcServer::make();
88 auto fd = unique_fd(android_get_control_socket(name));
89 if (!fd.ok()) {
90 LOG(ERROR) << "Failed to get fd for the socket:" << name;
91 return nullptr;
92 }
93 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
94 LOG(ERROR) << "Failed to set up Unix Domain RPC server with name " << name
95 << " error: " << statusToString(status).c_str();
96 return nullptr;
97 }
98 server->setRootObject(AIBinder_toPlatformBinder(service));
99 return createRpcServerHandle(server);
100}
101
102void ARpcServer_start(ARpcServer* handle) {
103 toRpcServer(handle)->start();
104}
105
106void ARpcServer_join(ARpcServer* handle) {
107 toRpcServer(handle)->join();
108}
109
110void ARpcServer_shutdown(ARpcServer* handle) {
111 toRpcServer(handle)->shutdown();
112}
113
114void ARpcServer_free(ARpcServer* handle) {
115 freeRpcServerHandle(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900116}
117
Alice Wang07466682022-10-19 15:28:29 +0000118AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700119 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -0700120 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
121 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port
122 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700123 return nullptr;
124 }
125 return AIBinder_fromPlatformBinder(session->getRootObject());
126}
Inseob Kime12fccc2021-09-02 17:23:33 +0900127
Alice Wang893a9912022-10-24 10:44:09 +0000128AIBinder* UnixDomainRpcClient(const char* name) {
129 std::string pathname(name);
130 pathname = ANDROID_SOCKET_DIR "/" + pathname;
131 auto session = RpcSession::make();
132 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
133 LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname
134 << " error: " << statusToString(status).c_str();
135 return nullptr;
136 }
137 return AIBinder_fromPlatformBinder(session->getRootObject());
138}
139
Inseob Kime12fccc2021-09-02 17:23:33 +0900140AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) {
141 auto session = RpcSession::make();
142 auto request = [=] { return unique_fd{requestFd(param)}; };
143 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
144 LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str();
145 return nullptr;
146 }
147 return AIBinder_fromPlatformBinder(session->getRootObject());
148}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700149}