blob: a922b21dd72207dad7972d85780c99205b6936c5 [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;
David Brazdil21c887c2022-09-23 12:25:18 +010045 base::unique_fd unixBootstrapFd(serverConfig.unixBootstrapFd);
Andrei Homescu2a298012022-06-15 01:08:54 +000046
47 switch (socketType) {
48 case SocketType::PRECONNECTED:
49 [[fallthrough]];
50 case SocketType::UNIX:
51 CHECK_EQ(OK, server->setupUnixDomainServer(serverConfig.addr.c_str()))
52 << serverConfig.addr;
53 break;
David Brazdil21c887c2022-09-23 12:25:18 +010054 case SocketType::UNIX_BOOTSTRAP:
55 CHECK_EQ(OK, server->setupUnixDomainSocketBootstrapServer(std::move(unixBootstrapFd)));
56 break;
Andrei Homescu2a298012022-06-15 01:08:54 +000057 case SocketType::VSOCK:
58 CHECK_EQ(OK, server->setupVsockServer(serverConfig.vsockPort));
59 break;
60 case SocketType::INET: {
61 CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort));
62 CHECK_NE(0, outPort);
63 break;
64 }
65 default:
66 LOG_ALWAYS_FATAL("Unknown socket type");
67 }
68
69 BinderRpcTestServerInfo serverInfo;
70 serverInfo.port = static_cast<int64_t>(outPort);
71 serverInfo.cert.data = server->getCertificate(RpcCertificateFormat::PEM);
72 writeToFd(writeEnd, serverInfo);
73 auto clientInfo = readFromFd<BinderRpcTestClientInfo>(readEnd);
74
75 if (rpcSecurity == RpcSecurity::TLS) {
76 for (const auto& clientCert : clientInfo.certs) {
77 CHECK_EQ(OK,
78 certVerifier->addTrustedPeerCertificate(RpcCertificateFormat::PEM,
79 clientCert.data));
80 }
81 }
82
83 server->setPerSessionRootObject([&](const void* addrPtr, size_t len) {
84 // UNIX sockets with abstract addresses return
85 // sizeof(sa_family_t)==2 in addrlen
86 CHECK_GE(len, sizeof(sa_family_t));
87 const sockaddr* addr = reinterpret_cast<const sockaddr*>(addrPtr);
88 sp<MyBinderRpcTest> service = sp<MyBinderRpcTest>::make();
89 switch (addr->sa_family) {
90 case AF_UNIX:
91 // nothing to save
92 break;
93 case AF_VSOCK:
94 CHECK_EQ(len, sizeof(sockaddr_vm));
95 service->port = reinterpret_cast<const sockaddr_vm*>(addr)->svm_port;
96 break;
97 case AF_INET:
98 CHECK_EQ(len, sizeof(sockaddr_in));
99 service->port = ntohs(reinterpret_cast<const sockaddr_in*>(addr)->sin_port);
100 break;
101 case AF_INET6:
102 CHECK_EQ(len, sizeof(sockaddr_in));
103 service->port = ntohs(reinterpret_cast<const sockaddr_in6*>(addr)->sin6_port);
104 break;
105 default:
106 LOG_ALWAYS_FATAL("Unrecognized address family %d", addr->sa_family);
107 }
108 service->server = server;
109 return service;
110 });
111
112 server->join();
113
114 // Another thread calls shutdown. Wait for it to complete.
115 (void)server->shutdown();
116
117 return 0;
118}