libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-io-ops.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2026 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * lws-io-ops.h: the requests the sansIO half of lws makes of the IO half.
25 * See READMEs/README.sans-io-split.md, "The interface" and "The headers".
26 *
27 * sansIO asks IO for exactly these, and nothing else: the four requests
28 * of the loop (call my tx when the transport can take bytes, feed or stop
29 * feeding my rx, the earliest deadline moved, release my transport), which
30 * it spells as it always has (lws_callback_on_writable(),
31 * lws_rx_flow_control(), lws_set_timeout() and lws_sul_schedule(),
32 * lws_close_free_wsi()) and which reach this struct at the bottom; the
33 * connection object's (made, moved to another object, its datagram path);
34 * and, grouped below by theme, the client transport, the tls session, tx,
35 * service, the app's rx pull and the vhost. The platform's clock and
36 * random source, lws_now_usecs() and lws_get_random(), are dependencies
37 * rather than requests. What sansIO asks of features lws' own IO provides
38 * (a cgi child process, a jit-trust vhost) is not here: no other IO
39 * supplies them, and they are private to lws. README.sans-io-split.md,
40 * "The contract", lists them.
41 *
42 * IO fills it in for the normal build (lws_io_ops_default). An embedder of
43 * the sansIO half alone supplies its own in
44 * lws_context_creation_info.io_ops, and a port that returns its requests as
45 * polled outputs implements the same.
46 *
47 * The struct is one ABI whatever the build: every member is declared in
48 * every build, and one whose feature a build lacks is NULL there. Its
49 * version is LWS_IO_OPS_ABI_VERSION, and a table handed in must carry the
50 * version the embedder was compiled with in its abi_version, or context
51 * creation fails, saying so.
52 */
53
54/*
55 * The version of lws_io_ops_t: its members, their order and what they
56 * mean. Any change to them changes it.
57 */
58#define LWS_IO_OPS_ABI_VERSION 5
59
60/*
61 * The close request reaches the transport in phases, in this order over a
62 * connection's close: not every close has every phase
63 */
65 LWS_IOCLOSE_QUIESCE, /* nothing of the transport's may act on the
66 * wsi any more: connect attempts and their
67 * timers, dns, the wait for a socket */
68 LWS_IOCLOSE_UNWATCH, /* stop watching the transport, keep it for
69 * the release (a restart follows, or a
70 * file's user hears it close first) */
71 LWS_IOCLOSE_SHUTDOWN, /* stop sending: a tls close_notify when there
72 * is a session, else the write side */
73 LWS_IOCLOSE_STAGE, /* keep the transport until the peer has
74 * finished, and say when it has */
75 LWS_IOCLOSE_RELEASE, /* release it all: tls session, fd, its place
76 * in the poll set */
77};
78
79/*
80 * A datagram connection's path: which address its datagrams go to is its
81 * protocol's decision (quic's connection migration, RFC 9000 9); the socket
82 * that reaches it is IO's
83 */
85 LWS_IOPATH_COMMIT, /* the peer is at this address now: report it
86 * as the connection's peer, and aim a
87 * connected socket there if it is not */
88 LWS_IOPATH_NEW_SOCKET, /* move onto a fresh socket (a new local
89 * port) connected to this address, closing
90 * the old; the reported peer is unchanged */
91 LWS_IOPATH_RELAYED, /* a server connection was just born, and
92 * committed to its peer, from a datagram IO
93 * delivered as carried by a relay at this
94 * address: the peer is the relay's claim, and
95 * is reached through the relay */
96 LWS_IOPATH_PROVEN, /* the protocol validated the path to the
97 * peer at this address (quic address
98 * validation, RFC 9000 8): what carried it,
99 * eg, a relay, is proven along with it */
100};
101
103
104typedef struct lws_io_ops {
111
112 int (*want_write)(struct lws *wsi);
117 int (*want_read)(struct lws *wsi, int on);
120 void (*deadline)(struct lws_context *cx, int tsi, lws_usec_t us);
126 int (*close)(struct lws *wsi, int phase);
139 int (*path)(struct lws *wsi, int op, const union lws_sockaddr46 *peer);
152 void (*created)(struct lws *wsi);
158 int (*transfer)(struct lws *from, struct lws *to);
166
167 /*
168 * the transport: a client's, as its protocol decides about it, and
169 * what IO knows of a connection's peer
170 */
171
172 struct lws *(*transport_start)(struct lws *wsi);
176 int (*transport_connected)(struct lws *wsi);
185 void (*transport_established)(struct lws *wsi);
190 int (*transport_failed)(struct lws *wsi);
195 struct lws *(*client_connect)(const struct lws_client_connect_info *i);
200 const char *(*peer_address)(struct lws *wsi, char *buf, size_t len);
205 const char *(*peer_describe)(struct lws *wsi, char *buf, size_t len,
206 int flags);
212#if defined(LWS_WITH_NETWORK)
213 /* the peer stack's types only exist with networking */
214 int (*peer_walk)(struct lws *wsi, lws_peer_entry_t *dest, int max);
221 int (*peer_raise)(struct lws *wsi, const lws_sockaddr46 *seen,
222 lws_peer_cred_t cred);
228#endif
229
230 /*
231 * the tls session, which is IO's: what sansIO asks of it. NULL in a
232 * build without tls.
233 */
234
235 void *(*tls_session)(struct lws *wsi);
239 const uint8_t *(*tls_hs_ca_id)(struct lws *wsi);
244 int (*tls_peer_cert_info)(struct lws *wsi, enum lws_tls_cert_info type,
245 union lws_tls_cert_info_results *buf,
246 size_t len);
251
252 /*
253 * quic runs the tls handshake inside its own packets: these must be
254 * set in a build with quic and tls, and are NULL in one without
255 */
256
257 int (*tls_quic_session)(struct lws *wsi, lws_tls_quic_secret_cb cb);
263 int (*tls_quic_handshake)(struct lws *wsi, int level, const uint8_t *in,
264 size_t in_len, uint8_t *out, size_t *out_len);
270 int (*tls_quic_set_tp)(struct lws *wsi, const uint8_t *tp, size_t tp_len);
273 int (*tls_quic_get_tp)(struct lws *wsi, const uint8_t **tp,
274 size_t *tp_len);
277 int (*tls_confirm_peer_cert)(struct lws *wsi, char *ebuf,
278 size_t ebuf_len);
285 int (*tls_quic_aead)(struct lws *wsi);
288 int (*tls_quic_alert)(struct lws *wsi);
291 int (*tls_quic_alpn)(struct lws *wsi, char *buf, size_t len);
294 int (*tls_quic_admit)(struct lws *wsi);
302
303 /*
304 * tx: the transport taking sansIO's bytes
305 */
306
307 int (*tx_push)(struct lws *wsi, unsigned char *buf, size_t len);
318 void (*tx_now)(struct lws *wsi);
323 int (*tx_choked)(struct lws *wsi);
329 int (*tx_file)(struct lws *wsi);
337
338 /*
339 * service: sansIO's handling of a connection, run now
340 */
341
342 int (*service_writable)(struct lws *wsi);
348 int (*service_now)(struct lws *wsi);
353 int (*flag_pending_rx)(struct lws *wsi);
360 void (*wake)(struct lws *wsi);
365
366 /*
367 * rx at the app's pace
368 */
369
370 int (*http_client_read)(struct lws *wsi, char **buf, int *len);
378
379 /*
380 * the vhost and the context: a vhost's creation and destruction are
381 * IO's, and so is the process the context runs in
382 */
383
384 int (*finalize_startup)(struct lws_context *cx, const char *where);
391
392 void (*vhost_destroy)(struct lws_vhost *vh);
398
399/*
400 * IO's own: the requests reach the poll set and the socket. An embedder
401 * that only wants to hear the requests starts its table from this one with
402 * lws_io_ops_init() and wraps the ops it listens to (api-test-sansio does).
403 */
405
416static LWS_INLINE void
417lws_io_ops_init(lws_io_ops_t *ops)
418{
419 *ops = lws_io_ops_default;
421}
int(* lws_tls_quic_secret_cb)(struct lws *wsi, enum lws_tls_quic_secret_type type, const uint8_t *secret, size_t secret_len)
Definition lws-quic.h:75
#define LWS_EXTERN_FOR_DATA
unsigned int uint32_t
#define LWS_INLINE
int64_t lws_usec_t
unsigned char uint8_t
#define LWS_VISIBLE
lws_io_close_phase
Definition lws-io-ops.h:64
@ LWS_IOCLOSE_SHUTDOWN
Definition lws-io-ops.h:71
@ LWS_IOCLOSE_QUIESCE
Definition lws-io-ops.h:65
@ LWS_IOCLOSE_UNWATCH
Definition lws-io-ops.h:68
@ LWS_IOCLOSE_RELEASE
Definition lws-io-ops.h:75
@ LWS_IOCLOSE_STAGE
Definition lws-io-ops.h:73
LWS_VISIBLE LWS_EXTERN_FOR_DATA const lws_io_ops_t lws_io_ops_default
Definition lws-io-ops.h:404
#define LWS_IO_OPS_ABI_VERSION
Definition lws-io-ops.h:58
lws_io_path_op
Definition lws-io-ops.h:84
@ LWS_IOPATH_RELAYED
Definition lws-io-ops.h:91
@ LWS_IOPATH_NEW_SOCKET
Definition lws-io-ops.h:88
@ LWS_IOPATH_PROVEN
Definition lws-io-ops.h:96
@ LWS_IOPATH_COMMIT
Definition lws-io-ops.h:85
struct lws_io_ops lws_io_ops_t
struct lws_peer_entry lws_peer_entry_t
enum lws_peer_cred lws_peer_cred_t
lws_tls_cert_info
Definition lws-x509.h:25
int(* tls_quic_alert)(struct lws *wsi)
Definition lws-io-ops.h:288
void(* vhost_destroy)(struct lws_vhost *vh)
Definition lws-io-ops.h:392
uint32_t abi_version
Definition lws-io-ops.h:105
int(* close)(struct lws *wsi, int phase)
Definition lws-io-ops.h:126
void(* transport_established)(struct lws *wsi)
Definition lws-io-ops.h:185
int(* tls_peer_cert_info)(struct lws *wsi, enum lws_tls_cert_info type, union lws_tls_cert_info_results *buf, size_t len)
Definition lws-io-ops.h:244
void(* created)(struct lws *wsi)
Definition lws-io-ops.h:152
int(* tls_quic_alpn)(struct lws *wsi, char *buf, size_t len)
Definition lws-io-ops.h:291
int(* tls_quic_get_tp)(struct lws *wsi, const uint8_t **tp, size_t *tp_len)
Definition lws-io-ops.h:273
int(* tx_push)(struct lws *wsi, unsigned char *buf, size_t len)
Definition lws-io-ops.h:307
int(* tls_quic_set_tp)(struct lws *wsi, const uint8_t *tp, size_t tp_len)
Definition lws-io-ops.h:270
int(* service_writable)(struct lws *wsi)
Definition lws-io-ops.h:342
int(* tls_quic_handshake)(struct lws *wsi, int level, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition lws-io-ops.h:263
void(* deadline)(struct lws_context *cx, int tsi, lws_usec_t us)
Definition lws-io-ops.h:120
int(* http_client_read)(struct lws *wsi, char **buf, int *len)
Definition lws-io-ops.h:370
int(* want_read)(struct lws *wsi, int on)
Definition lws-io-ops.h:117
int(* tls_confirm_peer_cert)(struct lws *wsi, char *ebuf, size_t ebuf_len)
Definition lws-io-ops.h:277
int(* tx_choked)(struct lws *wsi)
Definition lws-io-ops.h:323
int(* transport_connected)(struct lws *wsi)
Definition lws-io-ops.h:176
int(* path)(struct lws *wsi, int op, const union lws_sockaddr46 *peer)
Definition lws-io-ops.h:139
int(* tx_file)(struct lws *wsi)
Definition lws-io-ops.h:329
void(* wake)(struct lws *wsi)
Definition lws-io-ops.h:360
int(* transport_failed)(struct lws *wsi)
Definition lws-io-ops.h:190
int(* finalize_startup)(struct lws_context *cx, const char *where)
Definition lws-io-ops.h:384
int(* tls_quic_session)(struct lws *wsi, lws_tls_quic_secret_cb cb)
Definition lws-io-ops.h:257
int(* want_write)(struct lws *wsi)
Definition lws-io-ops.h:112
int(* service_now)(struct lws *wsi)
Definition lws-io-ops.h:348
int(* flag_pending_rx)(struct lws *wsi)
Definition lws-io-ops.h:353
int(* transfer)(struct lws *from, struct lws *to)
Definition lws-io-ops.h:158
int(* tls_quic_admit)(struct lws *wsi)
Definition lws-io-ops.h:294
int(* tls_quic_aead)(struct lws *wsi)
Definition lws-io-ops.h:285
void(* tx_now)(struct lws *wsi)
Definition lws-io-ops.h:318