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 2
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};
92
94
95typedef struct lws_io_ops {
102
103 int (*want_write)(struct lws *wsi);
108 int (*want_read)(struct lws *wsi, int on);
111 void (*deadline)(struct lws_context *cx, int tsi, lws_usec_t us);
117 int (*close)(struct lws *wsi, int phase);
130 int (*path)(struct lws *wsi, int op, const union lws_sockaddr46 *peer);
140 void (*created)(struct lws *wsi);
146 int (*transfer)(struct lws *from, struct lws *to);
154
155 /*
156 * the transport: a client's, as its protocol decides about it, and
157 * what IO knows of a connection's peer
158 */
159
160 struct lws *(*transport_start)(struct lws *wsi);
164 int (*transport_connected)(struct lws *wsi);
173 void (*transport_established)(struct lws *wsi);
178 int (*transport_failed)(struct lws *wsi);
183 struct lws *(*client_connect)(const struct lws_client_connect_info *i);
188 const char *(*peer_address)(struct lws *wsi, char *buf, size_t len);
193
194 /*
195 * the tls session, which is IO's: what sansIO asks of it. NULL in a
196 * build without tls.
197 */
198
199 void *(*tls_session)(struct lws *wsi);
203 const uint8_t *(*tls_hs_ca_id)(struct lws *wsi);
208 int (*tls_peer_cert_info)(struct lws *wsi, enum lws_tls_cert_info type,
209 union lws_tls_cert_info_results *buf,
210 size_t len);
215
216 /*
217 * quic runs the tls handshake inside its own packets: these must be
218 * set in a build with quic and tls, and are NULL in one without
219 */
220
221 int (*tls_quic_session)(struct lws *wsi, lws_tls_quic_secret_cb cb);
227 int (*tls_quic_handshake)(struct lws *wsi, int level, const uint8_t *in,
228 size_t in_len, uint8_t *out, size_t *out_len);
234 int (*tls_quic_set_tp)(struct lws *wsi, const uint8_t *tp, size_t tp_len);
237 int (*tls_quic_get_tp)(struct lws *wsi, const uint8_t **tp,
238 size_t *tp_len);
241 int (*tls_confirm_peer_cert)(struct lws *wsi, char *ebuf,
242 size_t ebuf_len);
249 int (*tls_quic_aead)(struct lws *wsi);
252 int (*tls_quic_alert)(struct lws *wsi);
255 int (*tls_quic_alpn)(struct lws *wsi, char *buf, size_t len);
258
259 /*
260 * tx: the transport taking sansIO's bytes
261 */
262
263 int (*tx_push)(struct lws *wsi, unsigned char *buf, size_t len);
274 void (*tx_now)(struct lws *wsi);
279 int (*tx_choked)(struct lws *wsi);
285 int (*tx_file)(struct lws *wsi);
293
294 /*
295 * service: sansIO's handling of a connection, run now
296 */
297
298 int (*service_writable)(struct lws *wsi);
304 int (*service_now)(struct lws *wsi);
309 int (*flag_pending_rx)(struct lws *wsi);
316 void (*wake)(struct lws *wsi);
321
322 /*
323 * rx at the app's pace
324 */
325
326 int (*http_client_read)(struct lws *wsi, char **buf, int *len);
334
335 /*
336 * the vhost and the context: a vhost's creation and destruction are
337 * IO's, and so is the process the context runs in
338 */
339
340 int (*finalize_startup)(struct lws_context *cx, const char *where);
347
348 void (*vhost_destroy)(struct lws_vhost *vh);
354
355/*
356 * IO's own: the requests reach the poll set and the socket. An embedder
357 * that only wants to hear the requests starts its table from this one with
358 * lws_io_ops_init() and wraps the ops it listens to (api-test-sansio does).
359 */
361
372static LWS_INLINE void
373lws_io_ops_init(lws_io_ops_t *ops)
374{
375 *ops = lws_io_ops_default;
377}
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:360
#define LWS_IO_OPS_ABI_VERSION
Definition lws-io-ops.h:58
lws_io_path_op
Definition lws-io-ops.h:84
@ LWS_IOPATH_NEW_SOCKET
Definition lws-io-ops.h:88
@ LWS_IOPATH_COMMIT
Definition lws-io-ops.h:85
struct lws_io_ops lws_io_ops_t
lws_tls_cert_info
Definition lws-x509.h:25
int(* tls_quic_alert)(struct lws *wsi)
Definition lws-io-ops.h:252
void(* vhost_destroy)(struct lws_vhost *vh)
Definition lws-io-ops.h:348
uint32_t abi_version
Definition lws-io-ops.h:96
int(* close)(struct lws *wsi, int phase)
Definition lws-io-ops.h:117
void(* transport_established)(struct lws *wsi)
Definition lws-io-ops.h:173
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:208
void(* created)(struct lws *wsi)
Definition lws-io-ops.h:140
int(* tls_quic_alpn)(struct lws *wsi, char *buf, size_t len)
Definition lws-io-ops.h:255
int(* tls_quic_get_tp)(struct lws *wsi, const uint8_t **tp, size_t *tp_len)
Definition lws-io-ops.h:237
int(* tx_push)(struct lws *wsi, unsigned char *buf, size_t len)
Definition lws-io-ops.h:263
int(* tls_quic_set_tp)(struct lws *wsi, const uint8_t *tp, size_t tp_len)
Definition lws-io-ops.h:234
int(* service_writable)(struct lws *wsi)
Definition lws-io-ops.h:298
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:227
void(* deadline)(struct lws_context *cx, int tsi, lws_usec_t us)
Definition lws-io-ops.h:111
int(* http_client_read)(struct lws *wsi, char **buf, int *len)
Definition lws-io-ops.h:326
int(* want_read)(struct lws *wsi, int on)
Definition lws-io-ops.h:108
int(* tls_confirm_peer_cert)(struct lws *wsi, char *ebuf, size_t ebuf_len)
Definition lws-io-ops.h:241
int(* tx_choked)(struct lws *wsi)
Definition lws-io-ops.h:279
int(* transport_connected)(struct lws *wsi)
Definition lws-io-ops.h:164
int(* path)(struct lws *wsi, int op, const union lws_sockaddr46 *peer)
Definition lws-io-ops.h:130
int(* tx_file)(struct lws *wsi)
Definition lws-io-ops.h:285
void(* wake)(struct lws *wsi)
Definition lws-io-ops.h:316
int(* transport_failed)(struct lws *wsi)
Definition lws-io-ops.h:178
int(* finalize_startup)(struct lws_context *cx, const char *where)
Definition lws-io-ops.h:340
int(* tls_quic_session)(struct lws *wsi, lws_tls_quic_secret_cb cb)
Definition lws-io-ops.h:221
int(* want_write)(struct lws *wsi)
Definition lws-io-ops.h:103
int(* service_now)(struct lws *wsi)
Definition lws-io-ops.h:304
int(* flag_pending_rx)(struct lws *wsi)
Definition lws-io-ops.h:309
int(* transfer)(struct lws *from, struct lws *to)
Definition lws-io-ops.h:146
int(* tls_quic_aead)(struct lws *wsi)
Definition lws-io-ops.h:249
void(* tx_now)(struct lws *wsi)
Definition lws-io-ops.h:274