1 /* 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 17 #pragma once 18 19 #include <sys/socket.h> 20 #include <stdint.h> 21 22 extern "C" { 23 24 struct AIBinder; 25 struct ARpcServer; 26 struct ARpcSession; 27 28 enum class ARpcSession_FileDescriptorTransportMode { 29 None, 30 Unix, 31 Trusty, 32 }; 33 34 // Starts an RPC server on a given port and a given root IBinder object. 35 // The server will only accept connections from the given CID. 36 // Set `cid` to VMADDR_CID_ANY to accept connections from any client. 37 // Set `cid` to VMADDR_CID_LOCAL to only bind to the local vsock interface. 38 // Returns an opaque handle to the running server instance, or null if the server 39 // could not be started. 40 // Set |port| to VMADDR_PORT_ANY to pick an available ephemeral port. 41 // |assignedPort| will be set to the assigned port number if it is not null. 42 // This will be the provided |port|, or the chosen available ephemeral port when 43 // |port| is VMADDR_PORT_ANY. 44 [[nodiscard]] ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, 45 unsigned int port, unsigned int* assignedPort); 46 47 // Starts a Unix domain RPC server with an open raw socket file descriptor 48 // and a given root IBinder object. 49 // The socket should be created and bound to an address. 50 // Returns an opaque handle to the running server instance, or null if the server 51 // could not be started. 52 // The socket will be closed by the server once the server goes out of scope. 53 [[nodiscard]] ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd); 54 55 // Starts an RPC server that bootstraps sessions using an existing Unix domain 56 // socket pair, with a given root IBinder object. 57 // Callers should create a pair of SOCK_STREAM Unix domain sockets, pass one to 58 // this function and the other to UnixDomainBootstrapClient(). Multiple client 59 // session can be created from the client end of the pair. 60 // Does not take ownership of `service`. 61 // Returns an opaque handle to the running server instance, or null if the server 62 // could not be started. 63 [[nodiscard]] ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd); 64 65 // Starts an RPC server on a given IP address+port and a given IBinder object. 66 // Returns an opaque handle to the running server instance, or null if the server 67 // could not be started. 68 // Does not take ownership of `service`. 69 // Returns an opaque handle to the running service instance, or null if the server 70 // could not be started. 71 [[nodiscard]] ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address, 72 unsigned int port); 73 74 // Sets the list of supported file descriptor transport modes of this RPC server. 75 void ARpcServer_setSupportedFileDescriptorTransportModes( 76 ARpcServer* handle, 77 const ARpcSession_FileDescriptorTransportMode modes[], 78 size_t modes_len); 79 80 // Sets the maximum number of threads that the Server will use for 81 // incoming client connections. 82 // 83 // This must be called before adding a client session. This corresponds 84 // to the number of incoming connections to RpcSession objects in the 85 // server, which will correspond to the number of outgoing connections 86 // in client RpcSession objects. 87 // 88 // If this is not specified, this will be a single-threaded server. 89 void ARpcServer_setMaxThreads(ARpcServer* server, size_t threads); 90 91 // Runs ARpcServer_join() in a background thread. Immediately returns. 92 void ARpcServer_start(ARpcServer* server); 93 94 // Joins the thread of a running RpcServer instance. At any given point, there 95 // can only be one thread calling ARpcServer_join(). 96 // If a client needs to actively terminate join, call ARpcServer_shutdown() in 97 // a separate thread. 98 void ARpcServer_join(ARpcServer* server); 99 100 // Shuts down any running ARpcServer_join(). 101 [[nodiscard]] bool ARpcServer_shutdown(ARpcServer* server); 102 103 // Frees the ARpcServer handle and drops the reference count on the underlying 104 // RpcServer instance. The handle must not be reused afterwards. 105 // This automatically calls ARpcServer_shutdown(). 106 void ARpcServer_free(ARpcServer* server); 107 108 // Allocates a new RpcSession object and returns an opaque handle to it. 109 [[nodiscard]] ARpcSession* ARpcSession_new(); 110 111 // Connects to an RPC server over vsock at a given CID on a given port. 112 // Returns the root Binder object of the server. 113 AIBinder* ARpcSession_setupVsockClient(ARpcSession* session, unsigned int cid, 114 unsigned int port); 115 116 // Connects to an RPC server over a Unix Domain Socket of the given name. 117 // The final Unix Domain Socket path name is /dev/socket/`name`. 118 // Returns the root Binder object of the server. 119 AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* session, const char* name); 120 121 // Connects to an RPC server over the given bootstrap Unix domain socket. 122 // Does NOT take ownership of `bootstrapFd`. 123 AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* session, 124 int bootstrapFd); 125 126 // Connects to an RPC server over an INET socket at a given IP address on a given port. 127 // Returns the root Binder object of the server. 128 AIBinder* ARpcSession_setupInet(ARpcSession* session, const char* address, unsigned int port); 129 130 // Connects to an RPC server with preconnected file descriptors. 131 // 132 // requestFd should connect to the server and return a valid file descriptor, or 133 // -1 if connection fails. 134 // 135 // param will be passed to requestFd. Callers can use param to pass contexts to 136 // the requestFd function. 137 AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* session, 138 int (*requestFd)(void* param), 139 void* param); 140 141 // Sets the file descriptor transport mode for this session. 142 void ARpcSession_setFileDescriptorTransportMode(ARpcSession* session, 143 ARpcSession_FileDescriptorTransportMode mode); 144 145 // Sets the maximum number of incoming threads, to service connections. 146 void ARpcSession_setMaxIncomingThreads(ARpcSession* session, size_t threads); 147 148 // Sets the maximum number of outgoing connections. 149 void ARpcSession_setMaxOutgoingConnections(ARpcSession* session, size_t connections); 150 151 // Decrements the refcount of the underlying RpcSession object. 152 void ARpcSession_free(ARpcSession* session); 153 } 154