blob: 31eb5dadf13dc81760bdeac431dbb76edde37e9a [file] [log] [blame]
Andrei Homescu2a298012022-06-15 01:08:54 +00001/*
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 "binderRpcTestCommon.h"
18
19using namespace android;
20
21int main(int argc, const char* argv[]) {
22 LOG_ALWAYS_FATAL_IF(argc != 3, "Invalid number of arguments: %d", argc);
23 base::unique_fd writeEnd(atoi(argv[1]));
24 base::unique_fd readEnd(atoi(argv[2]));
25
26 auto serverConfig = readFromFd<BinderRpcTestServerConfig>(readEnd);
27 auto socketType = static_cast<SocketType>(serverConfig.socketType);
28 auto rpcSecurity = static_cast<RpcSecurity>(serverConfig.rpcSecurity);
29
30 std::vector<RpcSession::FileDescriptorTransportMode>
31 serverSupportedFileDescriptorTransportModes;
32 for (auto mode : serverConfig.serverSupportedFileDescriptorTransportModes) {
33 serverSupportedFileDescriptorTransportModes.push_back(
34 static_cast<RpcSession::FileDescriptorTransportMode>(mode));
35 }
36
37 auto certVerifier = std::make_shared<RpcCertificateVerifierSimple>();
38 sp<RpcServer> server = RpcServer::make(newFactory(rpcSecurity, certVerifier));
39
40 server->setProtocolVersion(serverConfig.serverVersion);
41 server->setMaxThreads(serverConfig.numThreads);
42 server->setSupportedFileDescriptorTransportModes(serverSupportedFileDescriptorTransportModes);
43
44 unsigned int outPort = 0;
45
46 switch (socketType) {
47 case SocketType::PRECONNECTED:
48 [[fallthrough]];
49 case SocketType::UNIX:
50 CHECK_EQ(OK, server->setupUnixDomainServer(serverConfig.addr.c_str()))
51 << serverConfig.addr;
52 break;
53 case SocketType::VSOCK:
54 CHECK_EQ(OK, server->setupVsockServer(serverConfig.vsockPort));
55 break;
56 case SocketType::INET: {
57 CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort));
58 CHECK_NE(0, outPort);
59 break;
60 }
61 default:
62 LOG_ALWAYS_FATAL("Unknown socket type");
63 }
64
65 BinderRpcTestServerInfo serverInfo;
66 serverInfo.port = static_cast<int64_t>(outPort);
67 serverInfo.cert.data = server->getCertificate(RpcCertificateFormat::PEM);
68 writeToFd(writeEnd, serverInfo);
69 auto clientInfo = readFromFd<BinderRpcTestClientInfo>(readEnd);
70
71 if (rpcSecurity == RpcSecurity::TLS) {
72 for (const auto& clientCert : clientInfo.certs) {
73 CHECK_EQ(OK,
74 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
75 clientCert.data));
76 }
77 }
78
79 server->setPerSessionRootObject([&](const void* addrPtr, size_t len) {
80 // UNIX sockets with abstract addresses return
81 // sizeof(sa_family_t)==2 in addrlen
82 CHECK_GE(len, sizeof(sa_family_t));
83 const sockaddr* addr = reinterpret_cast<const sockaddr*>(addrPtr);
84 sp<MyBinderRpcTest> service = sp<MyBinderRpcTest>::make();
85 switch (addr->sa_family) {
86 case AF_UNIX:
87 // nothing to save
88 break;
89 case AF_VSOCK:
90 CHECK_EQ(len, sizeof(sockaddr_vm));
91 service->port = reinterpret_cast<const sockaddr_vm*>(addr)->svm_port;
92 break;
93 case AF_INET:
94 CHECK_EQ(len, sizeof(sockaddr_in));
95 service->port = ntohs(reinterpret_cast<const sockaddr_in*>(addr)->sin_port);
96 break;
97 case AF_INET6:
98 CHECK_EQ(len, sizeof(sockaddr_in));
99 service->port = ntohs(reinterpret_cast<const sockaddr_in6*>(addr)->sin6_port);
100 break;
101 default:
102 LOG_ALWAYS_FATAL("Unrecognized address family %d", addr->sa_family);
103 }
104 service->server = server;
105 return service;
106 });
107
108 server->join();
109
110 // Another thread calls shutdown. Wait for it to complete.
111 (void)server->shutdown();
112
113 return 0;
114}