Debian Codegraph @

crt/aws-c-io/include/aws/io/channel_bootstrap.h

raw symbols SCIP
1 #ifndef AWS_IO_CHANNEL_BOOTSTRAP_H
2 #define AWS_IO_CHANNEL_BOOTSTRAP_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8 #include <aws/common/ref_count.h>
9 #include <aws/io/channel.h>
10 #include <aws/io/host_resolver.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13
14 struct aws_client_bootstrap;
15 struct aws_socket;
16 struct aws_socket_options;
17 struct aws_socket_endpoint;
18
19 /**
20 * Generic event function for channel lifecycle events.
21 *
22 * Callbacks are provided for:
23 * (1) Channel creation
24 * (2) Channel setup - If TLS is being used, this function is called once the socket has connected, the channel has
25 * been initialized, and TLS has been successfully negotiated. A TLS handler has already been added to the channel. If
26 * TLS negotiation fails, this function will be called with the corresponding error code. If TLS is not being used, this
27 * function is called once the socket has connected and the channel has been initialized.
28 * (3) Channel shutdown
29 *
30 * These callbacks are always invoked within the thread of the event-loop that the channel is assigned to.
31 *
32 * This function does NOT always imply "success" -- if error_code is AWS_OP_SUCCESS then everything was successful,
33 * otherwise an error condition occurred.
34 */
35 typedef void(aws_client_bootstrap_on_channel_event_fn)(
36 struct aws_client_bootstrap *bootstrap,
37 int error_code,
38 struct aws_channel *channel,
39 void *user_data);
40
41 /**
42 * If ALPN is being used this function will be invoked by the channel once an ALPN message is received. The returned
43 * channel_handler will be added to, and managed by, the channel.
44 */
45 typedef struct aws_channel_handler *(aws_channel_on_protocol_negotiated_fn)(struct aws_channel_slot *new_slot,
46 struct aws_byte_buf *protocol,
47 void *user_data);
48
49 struct aws_tls_connection_options;
50
51 struct aws_event_loop_group;
52
53 /**
54 * Called after client bootstrap has been completely cleaned up, after its last refcount is released.
55 */
56 typedef void aws_client_bootstrap_shutdown_complete_fn(void *user_data);
57
58 /**
59 * aws_client_bootstrap handles creation and setup of channels that communicate via socket with a specific endpoint.
60 */
61 struct aws_client_bootstrap {
62 struct aws_allocator *allocator;
63 struct aws_event_loop_group *event_loop_group;
64 struct aws_host_resolver *host_resolver;
65 struct aws_host_resolution_config host_resolver_config;
66 aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated;
67 struct aws_ref_count ref_count;
68 aws_client_bootstrap_shutdown_complete_fn *on_shutdown_complete;
69 void *user_data;
70 };
71
72 /**
73 * aws_client_bootstrap creation options.
74 */
75 struct aws_client_bootstrap_options {
76
77 /* Required. Must outlive the client bootstrap. */
78 struct aws_event_loop_group *event_loop_group;
79
80 /* Required. Must outlive the client bootstrap. */
81 struct aws_host_resolver *host_resolver;
82
83 /* Optional. If none is provided then default settings are used.
84 * This object is deep-copied by bootstrap.
85 * */
86 const struct aws_host_resolution_config *host_resolution_config;
87
88 /* Optional. If provided, callback is invoked when client bootstrap has completely shut down. */
89 aws_client_bootstrap_shutdown_complete_fn *on_shutdown_complete;
90
91 /* Optional. Passed to callbacks */
92 void *user_data;
93 };
94
95 struct aws_server_bootstrap;
96
97 /**
98 * If TLS is being used, this function is called once the socket has received an incoming connection, the channel has
99 * been initialized, and TLS has been successfully negotiated. A TLS handler has already been added to the channel. If
100 * TLS negotiation fails, this function will be called with the corresponding error code.
101 *
102 * If TLS is not being used, this function is called once the socket has received an incoming connection and the channel
103 * has been initialized.
104 *
105 * This function is always called within the thread of the event-loop that the new channel is assigned to upon success.
106 *
107 * On failure, the channel might not be assigned to an event loop yet, and will thus be invoked on the listener's
108 * event-loop thread.
109 *
110 * This function does NOT mean "success", if error_code is AWS_OP_SUCCESS then everything was successful, otherwise an
111 * error condition occurred.
112 *
113 * If an error occurred, you do not need to shutdown the channel. The `aws_channel_client_shutdown_callback` will be
114 * invoked once the channel has finished shutting down.
115 */
116 typedef void(aws_server_bootstrap_on_accept_channel_setup_fn)(
117 struct aws_server_bootstrap *bootstrap,
118 int error_code,
119 struct aws_channel *channel,
120 void *user_data);
121
122 /**
123 * Once the channel shuts down, this function will be invoked within the thread of
124 * the event-loop that the channel is assigned to.
125 *
126 * Note: this function is only invoked if the channel was successfully setup,
127 * e.g. aws_server_bootstrap_on_accept_channel_setup_fn() was invoked without an error code.
128 */
129 typedef void(aws_server_bootstrap_on_accept_channel_shutdown_fn)(
130 struct aws_server_bootstrap *bootstrap,
131 int error_code,
132 struct aws_channel *channel,
133 void *user_data);
134
135 /**
136 * This function is only used for async listener (Apple Network Framework in this case).
137 * Once the server listener socket is finished setup and starting listening, this fuction
138 * will be invoked.
139 */
140 typedef void(
141 aws_server_bootstrap_on_listener_setup_fn)(struct aws_server_bootstrap *bootstrap, int error_code, void *user_data);
142
143 /**
144 * Once the server listener socket is finished destroying, and all the existing connections are closed, this fuction
145 * will be invoked.
146 */
147 typedef void(
148 aws_server_bootstrap_on_server_listener_destroy_fn)(struct aws_server_bootstrap *bootstrap, void *user_data);
149
150 /**
151 * aws_server_bootstrap manages listening sockets, creating and setting up channels to handle each incoming connection.
152 */
153 struct aws_server_bootstrap {
154 struct aws_allocator *allocator;
155 struct aws_event_loop_group *event_loop_group;
156 aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated;
157 struct aws_ref_count ref_count;
158 };
159
160 /**
161 * Socket-based channel creation options.
162 *
163 * bootstrap - configs name resolution and which event loop group the connection will be seated into
164 * host_name - host to connect to; if a dns address, will be resolved prior to connecting
165 * port - port to connect to
166 * socket_options - socket properties, including type (tcp vs. udp vs. unix domain) and connect timeout. TLS
167 * connections are currently restricted to tcp (AWS_SOCKET_STREAM) only.
168 * tls_options - (optional) tls context to apply after connection establishment. If NULL, the connection will
169 * not be protected by TLS.
170 * creation_callback - (optional) callback invoked when the channel is first created. This is always right after
171 * the connection was successfully established. *Does NOT* get called if the initial connect failed.
172 * setup_callback - callback invoked once the channel is ready for use and TLS has been negotiated or if an error
173 * is encountered
174 * shutdown_callback - callback invoked once the channel has shutdown.
175 * enable_read_back_pressure - controls whether or not back pressure will be applied in the channel
176 * user_data - arbitrary data to pass back to the various callbacks
177 * requested_event_loop - if set, the connection will be placed on the requested event loop rather than one
178 * chosen internally from the bootstrap's associated event loop group. It is an error to pass in an event loop
179 * that is not associated with the bootstrap's event loop group.
180 *
181 * Immediately after the `shutdown_callback` returns, the channel is cleaned up automatically. All callbacks are invoked
182 * in the thread of the event-loop that the new channel is assigned to.
183 *
184 */
185 struct aws_socket_channel_bootstrap_options {
186 struct aws_client_bootstrap *bootstrap;
187 const char *host_name;
188 uint32_t port;
189 const struct aws_socket_options *socket_options;
190 const struct aws_tls_connection_options *tls_options;
191 aws_client_bootstrap_on_channel_event_fn *creation_callback;
192 aws_client_bootstrap_on_channel_event_fn *setup_callback;
193 aws_client_bootstrap_on_channel_event_fn *shutdown_callback;
194 bool enable_read_back_pressure;
195 void *user_data;
196 struct aws_event_loop *requested_event_loop;
197 const struct aws_host_resolution_config *host_resolution_override_config;
198 };
199
200 /**
201 * Arguments to setup a server socket listener which will also negotiate and configure TLS.
202 * This creates a socket listener bound to `host` and 'port' using socket options `options`, and TLS options
203 * `tls_options`. `incoming_callback` will be invoked once an incoming channel is ready for use and TLS is
204 * finished negotiating, or if an error is encountered. `shutdown_callback` will be invoked once the channel has
205 * shutdown. `destroy_callback` will be invoked after the server socket listener is destroyed, and all associated
206 * connections and channels have finished shutting down. Immediately after the `shutdown_callback` returns, the channel
207 * is cleaned up automatically. All callbacks are invoked in the thread of the event-loop that listener is assigned to.
208 *
209 * Upon shutdown of your application, you'll want to call `aws_server_bootstrap_destroy_socket_listener` with the return
210 * value from this function.
211 *
212 * The socket type in `options` must be AWS_SOCKET_STREAM if tls_options is set.
213 * DTLS is not currently supported for tls.
214 */
215 struct aws_server_socket_channel_bootstrap_options {
216 struct aws_server_bootstrap *bootstrap;
217 const char *host_name;
218 uint32_t port;
219 const struct aws_socket_options *socket_options;
220 const struct aws_tls_connection_options *tls_options;
221 aws_server_bootstrap_on_listener_setup_fn *setup_callback;
222 aws_server_bootstrap_on_accept_channel_setup_fn *incoming_callback;
223 aws_server_bootstrap_on_accept_channel_shutdown_fn *shutdown_callback;
224 aws_server_bootstrap_on_server_listener_destroy_fn *destroy_callback;
225 bool enable_read_back_pressure;
226 void *user_data;
227 };
228
229 AWS_EXTERN_C_BEGIN
230
231 /**
232 * Create the client bootstrap.
233 */
234 AWS_IO_API struct aws_client_bootstrap *aws_client_bootstrap_new(
235 struct aws_allocator *allocator,
236 const struct aws_client_bootstrap_options *options);
237
238 /**
239 * Increments a client bootstrap's ref count, allowing the caller to take a reference to it.
240 *
241 * Returns the same client bootstrap passed in.
242 */
243 AWS_IO_API struct aws_client_bootstrap *aws_client_bootstrap_acquire(struct aws_client_bootstrap *bootstrap);
244
245 /**
246 * Decrements a client bootstrap's ref count. When the ref count drops to zero, the bootstrap will be destroyed.
247 */
248 AWS_IO_API void aws_client_bootstrap_release(struct aws_client_bootstrap *bootstrap);
249
250 /**
251 * When using TLS, if ALPN is used, this callback will be invoked from the channel. The returned handler will be added
252 * to the channel.
253 */
254 AWS_IO_API int aws_client_bootstrap_set_alpn_callback(
255 struct aws_client_bootstrap *bootstrap,
256 aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated);
257
258 /**
259 * Sets up a client socket channel.
260 */
261 AWS_IO_API int aws_client_bootstrap_new_socket_channel(struct aws_socket_channel_bootstrap_options *options);
262
263 /**
264 * Initializes the server bootstrap with `allocator` and `el_group`. This object manages listeners, server connections,
265 * and channels.
266 */
267 AWS_IO_API struct aws_server_bootstrap *aws_server_bootstrap_new(
268 struct aws_allocator *allocator,
269 struct aws_event_loop_group *el_group);
270
271 /**
272 * Increments a server bootstrap's ref count, allowing the caller to take a reference to it.
273 *
274 * Returns the same server bootstrap passed in.
275 */
276 AWS_IO_API struct aws_server_bootstrap *aws_server_bootstrap_acquire(struct aws_server_bootstrap *bootstrap);
277
278 /**
279 * Decrements a server bootstrap's ref count. When the ref count drops to zero, the bootstrap will be destroyed.
280 */
281 AWS_IO_API void aws_server_bootstrap_release(struct aws_server_bootstrap *bootstrap);
282
283 /**
284 * When using TLS, if ALPN is used, this callback will be invoked from the channel. The returned handler will be added
285 * to the channel.
286 */
287 AWS_IO_API int aws_server_bootstrap_set_alpn_callback(
288 struct aws_server_bootstrap *bootstrap,
289 aws_channel_on_protocol_negotiated_fn *on_protocol_negotiated);
290
291 /**
292 * Sets up a server socket listener. If you are planning on using TLS, use
293 * `aws_server_bootstrap_new_tls_socket_listener` instead. This creates a socket listener bound to `local_endpoint`
294 * using socket options `options`. `incoming_callback` will be invoked once an incoming channel is ready for use or if
295 * an error is encountered. `shutdown_callback` will be invoked once the channel has shutdown. `destroy_callback` will
296 * be invoked after the server socket listener is destroyed, and all associated connections and channels have finished
297 * shutting down. Immediately after the `shutdown_callback` returns, the channel is cleaned up automatically. All
298 * callbacks are invoked the thread of the event-loop that the listening socket is assigned to
299 *
300 * `setup_callback`. If set, the callback will be asynchronously invoked when the listener is ready for use. For Apple
301 * Network Framework, the listener is not usable until the callback is invoked. If the listener creation failed
302 * (return NULL), the `setup_callback` will not be invoked.
303 *
304 * Upon shutdown of your application, you'll want to call `aws_server_bootstrap_destroy_socket_listener` with the return
305 * value from this function.
306 *
307 * bootstrap_options is copied.
308 */
309 AWS_IO_API struct aws_socket *aws_server_bootstrap_new_socket_listener(
310 const struct aws_server_socket_channel_bootstrap_options *bootstrap_options);
311
312 /**
313 * Shuts down 'listener' and cleans up any resources associated with it. Any incoming channels on `listener` will still
314 * be active. `destroy_callback` will be invoked after the server socket listener is destroyed, and all associated
315 * connections and channels have finished shutting down.
316 */
317 AWS_IO_API void aws_server_bootstrap_destroy_socket_listener(
318 struct aws_server_bootstrap *bootstrap,
319 struct aws_socket *listener);
320
321 AWS_EXTERN_C_END
322 AWS_POP_SANE_WARNING_LEVEL
323
324 #endif /* AWS_IO_CHANNEL_BOOTSTRAP_H */