• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [[nodiscard]] ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid,
41                                               unsigned int port);
42 
43 // Starts a Unix domain RPC server with a given init-managed Unix domain `name`
44 // and a given root IBinder object.
45 // The socket should be created in init.rc with the same `name`.
46 // Returns an opaque handle to the running server instance, or null if the server
47 // could not be started.
48 [[nodiscard]] ARpcServer* ARpcServer_newInitUnixDomain(AIBinder* service, const char* name);
49 
50 // Starts an RPC server that bootstraps sessions using an existing Unix domain
51 // socket pair, with a given root IBinder object.
52 // Callers should create a pair of SOCK_STREAM Unix domain sockets, pass one to
53 // this function and the other to UnixDomainBootstrapClient(). Multiple client
54 // session can be created from the client end of the pair.
55 // Does not take ownership of `service`.
56 // Returns an opaque handle to the running server instance, or null if the server
57 // could not be started.
58 [[nodiscard]] ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd);
59 
60 // Starts an RPC server on a given IP address+port and a given IBinder object.
61 // Returns an opaque handle to the running server instance, or null if the server
62 // could not be started.
63 // Does not take ownership of `service`.
64 // Returns an opaque handle to the running service instance, or null if the server
65 // could not be started.
66 [[nodiscard]] ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address,
67                                              unsigned int port);
68 
69 // Sets the list of supported file descriptor transport modes of this RPC server.
70 void ARpcServer_setSupportedFileDescriptorTransportModes(
71         ARpcServer* handle,
72         const ARpcSession_FileDescriptorTransportMode modes[],
73         size_t modes_len);
74 
75 // Runs ARpcServer_join() in a background thread. Immediately returns.
76 void ARpcServer_start(ARpcServer* server);
77 
78 // Joins the thread of a running RpcServer instance. At any given point, there
79 // can only be one thread calling ARpcServer_join().
80 // If a client needs to actively terminate join, call ARpcServer_shutdown() in
81 // a separate thread.
82 void ARpcServer_join(ARpcServer* server);
83 
84 // Shuts down any running ARpcServer_join().
85 [[nodiscard]] bool ARpcServer_shutdown(ARpcServer* server);
86 
87 // Frees the ARpcServer handle and drops the reference count on the underlying
88 // RpcServer instance. The handle must not be reused afterwards.
89 // This automatically calls ARpcServer_shutdown().
90 void ARpcServer_free(ARpcServer* server);
91 
92 // Allocates a new RpcSession object and returns an opaque handle to it.
93 [[nodiscard]] ARpcSession* ARpcSession_new();
94 
95 // Connects to an RPC server over vsock at a given CID on a given port.
96 // Returns the root Binder object of the server.
97 AIBinder* ARpcSession_setupVsockClient(ARpcSession* session, unsigned int cid,
98                                        unsigned int port);
99 
100 // Connects to an RPC server over a Unix Domain Socket of the given name.
101 // The final Unix Domain Socket path name is /dev/socket/`name`.
102 // Returns the root Binder object of the server.
103 AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* session, const char* name);
104 
105 // Connects to an RPC server over the given bootstrap Unix domain socket.
106 // Does NOT take ownership of `bootstrapFd`.
107 AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* session,
108                                                      int bootstrapFd);
109 
110 // Connects to an RPC server over an INET socket at a given IP address on a given port.
111 // Returns the root Binder object of the server.
112 AIBinder* ARpcSession_setupInet(ARpcSession* session, const char* address, unsigned int port);
113 
114 // Connects to an RPC server with preconnected file descriptors.
115 //
116 // requestFd should connect to the server and return a valid file descriptor, or
117 // -1 if connection fails.
118 //
119 // param will be passed to requestFd. Callers can use param to pass contexts to
120 // the requestFd function.
121 AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* session,
122                                               int (*requestFd)(void* param),
123                                               void* param);
124 
125 // Sets the file descriptor transport mode for this session.
126 void ARpcSession_setFileDescriptorTransportMode(ARpcSession* session,
127                                                 ARpcSession_FileDescriptorTransportMode mode);
128 
129 // Sets the maximum number of incoming threads, to service connections.
130 void ARpcSession_setMaxIncomingThreads(ARpcSession* session, size_t threads);
131 
132 // Sets the maximum number of outgoing connections.
133 void ARpcSession_setMaxOutgoingConnections(ARpcSession* session, size_t connections);
134 
135 // Decrements the refcount of the underlying RpcSession object.
136 void ARpcSession_free(ARpcSession* session);
137 }
138