lsquic.h revision e1b8f1a8
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef __LSQUIC_H__
3#define __LSQUIC_H__
4
5/**
6 * @file
7 * public API for using liblsquic is defined in this file.
8 *
9 */
10
11#include <stdarg.h>
12#include <lsquic_types.h>
13#ifndef WIN32
14#include <sys/uio.h>
15#include <time.h>
16#else
17#include <vc_compat.h>
18#endif
19
20struct sockaddr;
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#define LSQUIC_MAJOR_VERSION 2
27#define LSQUIC_MINOR_VERSION 29
28#define LSQUIC_PATCH_VERSION 4
29
30/**
31 * Engine flags:
32 */
33
34/** Server mode */
35#define LSENG_SERVER (1 << 0)
36
37/** Use HTTP behavior */
38#define LSENG_HTTP  (1 << 1)
39
40#define LSENG_HTTP_SERVER (LSENG_SERVER|LSENG_HTTP)
41
42/**
43 * This is a list of QUIC versions that we know of.  List of supported
44 * versions is in LSQUIC_SUPPORTED_VERSIONS.
45 */
46enum lsquic_version
47{
48    /**
49     * Q043.  Support for processing PRIORITY frames.  Since this library
50     * has supported PRIORITY frames from the beginning, this version is
51     * exactly the same as LSQVER_042.
52     */
53    LSQVER_043,
54
55    /**
56     * Q046.  Use IETF Draft-17 compatible packet headers.
57     */
58    LSQVER_046,
59
60    /**
61     * Q050.  Variable-length QUIC server connection IDs.  Use CRYPTO frames
62     * for handshake.  IETF header format matching invariants-06.  Packet
63     * number encryption.  Initial packets are obfuscated.
64     */
65    LSQVER_050,
66
67#if LSQUIC_USE_Q098
68    /**
69     * Q098.  This is a made-up, experimental version used to test version
70     * negotiation.  The choice of 98 is similar to Google's choice of 99
71     * as the "IETF" version.
72     */
73    LSQVER_098,
74#define LSQUIC_EXPERIMENTAL_Q098 (1 << LSQVER_098)
75#else
76#define LSQUIC_EXPERIMENTAL_Q098 0
77#endif
78
79    /**
80     * IETF QUIC Draft-27
81     */
82    LSQVER_ID27,
83
84    /**
85     * IETF QUIC Draft-29
86     */
87    LSQVER_ID29,
88
89    /**
90     * IETF QUIC Draft-34
91     */
92    LSQVER_ID34,
93
94    /**
95     * IETF QUIC v1.  Functionally the same as Draft-34, but marked
96     * experimental for now.
97     */
98    LSQVER_I001,
99
100    /**
101     * Special version to trigger version negotiation.
102     * [draft-ietf-quic-transport-11], Section 3.
103     */
104    LSQVER_VERNEG,
105
106    N_LSQVER
107};
108
109/**
110 * We currently support versions 43, 46, 50, Draft-27, Draft-29, Draft-34,
111 * and IETF QUIC v1.
112 * @see lsquic_version
113 */
114#define LSQUIC_SUPPORTED_VERSIONS ((1 << N_LSQVER) - 1)
115
116/**
117 * List of versions in which the server never includes CID in short packets.
118 */
119#define LSQUIC_FORCED_TCID0_VERSIONS ((1 << LSQVER_046)|(1 << LSQVER_050))
120
121#define LSQUIC_EXPERIMENTAL_VERSIONS ( \
122                            (1 << LSQVER_I001) | \
123                            (1 << LSQVER_VERNEG) | LSQUIC_EXPERIMENTAL_Q098)
124
125#define LSQUIC_DEPRECATED_VERSIONS ((1 << LSQVER_ID27))
126
127#define LSQUIC_GQUIC_HEADER_VERSIONS (1 << LSQVER_043)
128
129#define LSQUIC_IETF_VERSIONS ((1 << LSQVER_ID27) \
130                          | (1 << LSQVER_ID29) \
131                          | (1 << LSQVER_ID34) \
132                          | (1 << LSQVER_I001) | (1 << LSQVER_VERNEG))
133
134#define LSQUIC_IETF_DRAFT_VERSIONS ((1 << LSQVER_ID27) \
135                                  | (1 << LSQVER_ID29) \
136              | (1 << LSQVER_ID34) | (1 << LSQVER_VERNEG))
137
138enum lsquic_hsk_status
139{
140    /**
141     * The handshake failed.
142     */
143    LSQ_HSK_FAIL,
144    /**
145     * The handshake succeeded without session resumption.
146     */
147    LSQ_HSK_OK,
148    /**
149     * The handshake succeeded with session resumption.
150     */
151    LSQ_HSK_RESUMED_OK,
152    /**
153     * Session resumption failed.  Retry the connection without session
154     * resumption.
155     */
156    LSQ_HSK_RESUMED_FAIL,
157};
158
159/**
160 * @struct lsquic_stream_if
161 * @brief The definitions of callback functions called by lsquic_stream to
162 * process events.
163 *
164 */
165struct lsquic_stream_if {
166
167    /**
168     * Use @ref lsquic_conn_get_ctx to get back the context.  It is
169     * OK for this function to return NULL.
170     */
171    lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx,
172                                                        lsquic_conn_t *c);
173
174    /** This is called when our side received GOAWAY frame.  After this,
175     *  new streams should not be created.  The callback is optional.
176     */
177    void (*on_goaway_received)(lsquic_conn_t *c);
178    void (*on_conn_closed)(lsquic_conn_t *c);
179
180    /** If you need to initiate a connection, call lsquic_conn_make_stream().
181     *  This will cause `on_new_stream' callback to be called when appropriate
182     *  (this operation is delayed when maximum number of outgoing streams is
183     *  reached).
184     *
185     *  After `on_close' is called, the stream is no longer accessible.
186     */
187    lsquic_stream_ctx_t *
188         (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *s);
189
190    void (*on_read)     (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
191    void (*on_write)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
192    void (*on_close)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
193    /* Called when datagram is ready to be written */
194    ssize_t (*on_dg_write)(lsquic_conn_t *c, void *, size_t);
195    /* Called when datagram is read from a packet.  This callback is required
196     * when es_datagrams is true.  Take care to process it quickly, as this
197     * is called during lsquic_engine_packet_in().
198     */
199    void (*on_datagram)(lsquic_conn_t *, const void *buf, size_t);
200    /* This callback in only called in client mode */
201    /**
202     * When handshake is completed, this optional callback is called.
203     */
204    void (*on_hsk_done)(lsquic_conn_t *c, enum lsquic_hsk_status s);
205    /**
206     * When client receives a token in NEW_TOKEN frame, this callback is called.
207     * The callback is optional.
208     */
209    void (*on_new_token)(lsquic_conn_t *c, const unsigned char *token,
210                                                        size_t token_size);
211    /**
212     * This optional callback lets client record information needed to
213     * perform a session resumption next time around.
214     *
215     * For IETF QUIC, this is called only if ea_get_ssl_ctx() is *not* set,
216     * in which case the library creates its own SSL_CTX.
217     *
218     * Note: this callback will be deprecated when gQUIC support is removed.
219     */
220    void (*on_sess_resume_info)(lsquic_conn_t *c, const unsigned char *, size_t);
221    /**
222     * Optional callback is called as soon as the peer resets a stream.
223     * The argument `how' is either 0, 1, or 2, meaning "read", "write", and
224     * "read and write", respectively (just like in shutdown(2)).  This
225     * signals the user to stop reading, writing, or both.
226     *
227     * Note that resets differ in gQUIC and IETF QUIC.  In gQUIC, `how' is
228     * always 2; in IETF QUIC, `how' is either 0 or 1 because one can reset
229     * just one direction in IETF QUIC.
230     */
231    void (*on_reset)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h, int how);
232    /**
233     * Optional callback is called when a CONNECTION_CLOSE frame is received.
234     * This allows the application to log low-level diagnostic information about
235     * errors received with the CONNECTION_CLOSE frame. If app_error is -1 then
236     * it is considered unknown if this is an app_error or not.
237     */
238    void (*on_conncloseframe_received)(lsquic_conn_t *c,
239                                       int app_error, uint64_t error_code,
240                                       const char *reason, int reason_len);
241};
242
243struct ssl_ctx_st;
244struct ssl_st;
245struct ssl_session_st;
246struct lsxpack_header;
247
248/**
249 * QUIC engine in server mode needs access to certificates.  This is
250 * accomplished by providing a callback and a context to the engine
251 * constructor.
252 */
253
254/* `sni' may be NULL if engine is not HTTP mode and client TLS transport
255 * parameters did not include the SNI.
256 */
257typedef struct ssl_ctx_st * (*lsquic_lookup_cert_f)(
258    void *lsquic_cert_lookup_ctx, const struct sockaddr *local, const char *sni);
259
260/**
261 * Minimum flow control window is set to 16 KB for both client and server.
262 * This means we can send up to this amount of data before handshake gets
263 * completed.
264 */
265#define      LSQUIC_MIN_FCW             (16 * 1024)
266
267/* Each LSQUIC_DF_* value corresponds to es_* entry in
268 * lsquic_engine_settings below.
269 */
270
271/**
272 * By default, deprecated and experimental versions are not included.
273 */
274#define LSQUIC_DF_VERSIONS         (LSQUIC_SUPPORTED_VERSIONS & \
275                                            ~LSQUIC_DEPRECATED_VERSIONS & \
276                                            ~LSQUIC_EXPERIMENTAL_VERSIONS)
277
278#define LSQUIC_DF_CFCW_SERVER      (3 * 1024 * 1024 / 2)
279#define LSQUIC_DF_CFCW_CLIENT      (15 * 1024 * 1024)
280#define LSQUIC_DF_SFCW_SERVER      (1 * 1024 * 1024)
281#define LSQUIC_DF_SFCW_CLIENT      (6 * 1024 * 1024)
282#define LSQUIC_DF_MAX_STREAMS_IN   100
283
284/* IQUIC uses different names for these: */
285#define LSQUIC_DF_INIT_MAX_DATA_SERVER LSQUIC_DF_CFCW_SERVER
286#define LSQUIC_DF_INIT_MAX_DATA_CLIENT LSQUIC_DF_CFCW_CLIENT
287#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER LSQUIC_DF_SFCW_SERVER
288#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER 0
289#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT 0
290#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT LSQUIC_DF_SFCW_CLIENT
291#define LSQUIC_DF_INIT_MAX_STREAMS_BIDI LSQUIC_DF_MAX_STREAMS_IN
292#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT 100
293#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_SERVER 3
294/* XXX What's a good value here? */
295#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT   (32 * 1024)
296#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER   (12 * 1024)
297
298/**
299 * Default idle connection time in seconds.
300 */
301#define LSQUIC_DF_IDLE_TIMEOUT 30
302
303/**
304 * Default ping period in seconds.
305 */
306#define LSQUIC_DF_PING_PERIOD 15
307
308/**
309 * Default handshake timeout in microseconds.
310 */
311#define LSQUIC_DF_HANDSHAKE_TO     (10 * 1000 * 1000)
312
313#define LSQUIC_DF_IDLE_CONN_TO     (LSQUIC_DF_IDLE_TIMEOUT * 1000 * 1000)
314#define LSQUIC_DF_SILENT_CLOSE     1
315
316/** Default value of maximum header list size.  If set to non-zero value,
317 *  SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is
318 *  completed (assuming the peer supports this setting frame type).
319 */
320#define LSQUIC_DF_MAX_HEADER_LIST_SIZE 0
321
322/** Default value of UAID (user-agent ID). */
323#define LSQUIC_DF_UA               "LSQUIC"
324
325#define LSQUIC_DF_STTL               86400
326#define LSQUIC_DF_MAX_INCHOATE     (1 * 1000 * 1000)
327/** Do not use NSTP by default */
328#define LSQUIC_DF_SUPPORT_NSTP     0
329/** TODO: IETF QUIC clients do not support push */
330#define LSQUIC_DF_SUPPORT_PUSH         1
331#define LSQUIC_DF_SUPPORT_TCID0    1
332/** By default, LSQUIC ignores Public Reset packets. */
333#define LSQUIC_DF_HONOR_PRST       0
334
335/**
336 * By default, LSQUIC will not send Public Reset packets in response to
337 * packets that specify unknown connections.
338 */
339#define LSQUIC_DF_SEND_PRST        0
340
341/** By default, infinite loop checks are turned on */
342#define LSQUIC_DF_PROGRESS_CHECK    1000
343
344/** By default, read/write events are dispatched in a loop */
345#define LSQUIC_DF_RW_ONCE           0
346
347/** By default, the threshold is not enabled */
348#define LSQUIC_DF_PROC_TIME_THRESH  0
349
350/** By default, packets are paced */
351#define LSQUIC_DF_PACE_PACKETS      1
352
353/** Default clock granularity is 1000 microseconds */
354#define LSQUIC_DF_CLOCK_GRANULARITY      1000
355
356/** The default value is 8 for simplicity */
357#define LSQUIC_DF_SCID_LEN 8
358
359/** The default value is 60 CIDs per minute */
360#define LSQUIC_DF_SCID_ISS_RATE   60
361
362#define LSQUIC_DF_QPACK_DEC_MAX_BLOCKED 100
363#define LSQUIC_DF_QPACK_DEC_MAX_SIZE 4096
364#define LSQUIC_DF_QPACK_ENC_MAX_BLOCKED 100
365#define LSQUIC_DF_QPACK_ENC_MAX_SIZE 4096
366
367/* By default, QPACK experiments are turned off */
368#define LSQUIC_DF_QPACK_EXPERIMENT 0
369
370/** ECN is disabled by default */
371#define LSQUIC_DF_ECN 0
372
373/** Allow migration by default */
374#define LSQUIC_DF_ALLOW_MIGRATION 1
375
376/** Use QL loss bits by default */
377#define LSQUIC_DF_QL_BITS 2
378
379/** Turn spin bit on by default */
380#define LSQUIC_DF_SPIN 1
381
382/** Turn on delayed ACKs extension by default */
383#define LSQUIC_DF_DELAYED_ACKS 1
384
385/**
386 * Defaults for the Packet Tolerance PID Controller (PTPC) used by the
387 * Delayed ACKs extension:
388 */
389#define LSQUIC_DF_PTPC_PERIODICITY 3
390#define LSQUIC_DF_PTPC_MAX_PACKTOL 150
391#define LSQUIC_DF_PTPC_DYN_TARGET 1
392#define LSQUIC_DF_PTPC_TARGET 1.0
393#define LSQUIC_DF_PTPC_PROP_GAIN 0.8
394#define LSQUIC_DF_PTPC_INT_GAIN 0.35
395#define LSQUIC_DF_PTPC_ERR_THRESH 0.05
396#define LSQUIC_DF_PTPC_ERR_DIVISOR 0.05
397
398/** Turn on timestamp extension by default */
399#define LSQUIC_DF_TIMESTAMPS 1
400
401/* Use Adaptive CC by default */
402#define LSQUIC_DF_CC_ALGO 3
403
404/* Default value of the CC RTT threshold is 1.5 ms */
405#define LSQUIC_DF_CC_RTT_THRESH 1500
406
407/** Turn off datagram extension by default */
408#define LSQUIC_DF_DATAGRAMS 0
409
410/** Assume optimistic NAT by default. */
411#define LSQUIC_DF_OPTIMISTIC_NAT 1
412
413/** Turn on Extensible HTTP Priorities by default. */
414#define LSQUIC_DF_EXT_HTTP_PRIO 1
415
416/** By default, incoming packet size is not limited. */
417#define LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX 0
418
419/**
420 * By default, greasing the QUIC bit is enabled (if peer sent
421 * the "grease_quic_bit" transport parameter).
422 */
423#define LSQUIC_DF_GREASE_QUIC_BIT 1
424
425/** By default, DPLPMTUD is enabled */
426#define LSQUIC_DF_DPLPMTUD 1
427
428/** By default, this value is left up to the engine. */
429#define LSQUIC_DF_BASE_PLPMTU 0
430
431/** By default, this value is left up to the engine. */
432#define LSQUIC_DF_MAX_PLPMTU 0
433
434/** By default, drop no-progress connections after 60 seconds on the server */
435#define LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER 60
436
437/** By default, do not use no-progress timeout on the client */
438#define LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT 0
439
440/** By default, we use the minimum timer of 1000 milliseconds */
441#define LSQUIC_DF_MTU_PROBE_TIMER 1000
442
443/** By default, calling on_close() is not delayed */
444#define LSQUIC_DF_DELAY_ONCLOSE 0
445
446/**
447 * By default, maximum batch size is not specified, leaving it up to the
448 * library.
449 */
450#define LSQUIC_DF_MAX_BATCH_SIZE 0
451
452/** Transport parameter sanity checks are performed by default. */
453#define LSQUIC_DF_CHECK_TP_SANITY 1
454
455struct lsquic_engine_settings {
456    /**
457     * This is a bit mask wherein each bit corresponds to a value in
458     * enum lsquic_version.  Client starts negotiating with the highest
459     * version and goes down.  Server supports either of the versions
460     * specified here.
461     *
462     * This setting applies to both Google and IETF QUIC.
463     *
464     * @see lsquic_version
465     */
466    unsigned        es_versions;
467
468    /**
469     * Initial default CFCW.
470     *
471     * In server mode, per-connection values may be set lower than
472     * this if resources are scarce.
473     *
474     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
475     *
476     * @see es_max_cfcw
477     */
478    unsigned        es_cfcw;
479
480    /**
481     * Initial default SFCW.
482     *
483     * In server mode, per-connection values may be set lower than
484     * this if resources are scarce.
485     *
486     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
487     *
488     * @see es_max_sfcw
489     */
490    unsigned        es_sfcw;
491
492    /**
493     * This value is used to specify maximum allowed value CFCW is allowed
494     * to reach due to window auto-tuning.  By default, this value is zero,
495     * which means that CFCW is not allowed to increase from its initial
496     * value.
497     *
498     * This setting is applicable to both gQUIC and IETF QUIC.
499     *
500     * @see es_cfcw, @see es_init_max_data.
501     */
502    unsigned        es_max_cfcw;
503
504    /**
505     * This value is used to specify the maximum value stream flow control
506     * window is allowed to reach due to auto-tuning.  By default, this
507     * value is zero, meaning that auto-tuning is turned off.
508     *
509     * This setting is applicable to both gQUIC and IETF QUIC.
510     *
511     * @see es_sfcw, @see es_init_max_stream_data_bidi_remote,
512     * @see es_init_max_stream_data_bidi_local.
513     */
514    unsigned        es_max_sfcw;
515
516    /** MIDS */
517    unsigned        es_max_streams_in;
518
519    /**
520     * Handshake timeout in microseconds.
521     *
522     * For client, this can be set to an arbitrary value (zero turns the
523     * timeout off).
524     *
525     * For server, this value is limited to about 16 seconds.  Do not set
526     * it to zero.
527     */
528    unsigned long   es_handshake_to;
529
530    /** ICSL in microseconds; GQUIC only */
531    unsigned long   es_idle_conn_to;
532
533    /**
534     * When true, CONNECTION_CLOSE is not sent when connection times out.
535     * The server will also not send a reply to client's CONNECTION_CLOSE.
536     *
537     * Corresponds to SCLS (silent close) gQUIC option.
538     */
539    int             es_silent_close;
540
541    /**
542     * This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE
543     * (RFC 7540, Section 6.5.2).  0 means no limit.  Defaults
544     * to @ref LSQUIC_DF_MAX_HEADER_LIST_SIZE.
545     */
546    unsigned        es_max_header_list_size;
547
548    /** UAID -- User-Agent ID.  Defaults to @ref LSQUIC_DF_UA. */
549    const char     *es_ua;
550
551    /**
552     * More parameters for server
553     */
554    uint64_t        es_sttl; /* SCFG TTL in seconds */
555
556    uint32_t        es_pdmd; /* One fixed value X509 */
557    uint32_t        es_aead; /* One fixed value AESG */
558    uint32_t        es_kexs; /* One fixed value C255 */
559
560    /* Maximum number of incoming connections in inchoate state.  This is
561     * only applicable in server mode.
562     */
563    unsigned        es_max_inchoate;
564
565    /**
566     * Setting this value to 0 means that
567     *
568     * For client:
569     *  a) we send a SETTINGS frame to indicate that we do not support server
570     *     push; and
571     *  b) All incoming pushed streams get reset immediately.
572     * (For maximum effect, set es_max_streams_in to 0.)
573     *
574     * For server:
575     *  lsquic_conn_push_stream() will return -1.
576     */
577    int             es_support_push;
578
579    /**
580     * If set to true value, the server will not include connection ID in
581     * outgoing packets if client's CHLO specifies TCID=0.
582     *
583     * For client, this means including TCID=0 into CHLO message.  Note that
584     * in this case, the engine tracks connections by the
585     * (source-addr, dest-addr) tuple, thereby making it necessary to create
586     * a socket for each connection.
587     *
588     * This option has no effect in Q046 and Q050, as the server never includes
589     * CIDs in the short packets.
590     *
591     * This setting is applicable to gQUIC only.
592     *
593     * The default is @ref LSQUIC_DF_SUPPORT_TCID0.
594     */
595    int             es_support_tcid0;
596
597    /**
598     * Q037 and higher support "No STOP_WAITING frame" mode.  When set, the
599     * client will send NSTP option in its Client Hello message and will not
600     * sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames,
601     * if any.  Note that if the version negotiation happens to downgrade the
602     * client below Q037, this mode will *not* be used.
603     *
604     * This option does not affect the server, as it must support NSTP mode
605     * if it was specified by the client.
606     *
607     * This setting is applicable to gQUIC only.
608     */
609    int             es_support_nstp;
610
611    /**
612     * If set to true value, the library will drop connections when it
613     * receives corresponding Public Reset packet.  The default is to
614     * ignore these packets.
615     *
616     * The default is @ref LSQUIC_DF_HONOR_PRST.
617     */
618    int             es_honor_prst;
619
620    /**
621     * If set to true value, the library will send Public Reset packets
622     * in response to incoming packets with unknown Connection IDs.
623     * The default is @ref LSQUIC_DF_SEND_PRST.
624     */
625    int             es_send_prst;
626
627    /**
628     * A non-zero value enables internal checks that identify suspected
629     * infinite loops in user @ref on_read and @ref on_write callbacks
630     * and break them.  An infinite loop may occur if user code keeps
631     * on performing the same operation without checking status, e.g.
632     * reading from a closed stream etc.
633     *
634     * The value of this parameter is as follows: should a callback return
635     * this number of times in a row without making progress (that is,
636     * reading, writing, or changing stream state), loop break will occur.
637     *
638     * The defaut value is @ref LSQUIC_DF_PROGRESS_CHECK.
639     */
640    unsigned        es_progress_check;
641
642    /**
643     * A non-zero value make stream dispatch its read-write events once
644     * per call.
645     *
646     * When zero, read and write events are dispatched until the stream
647     * is no longer readable or writeable, respectively, or until the
648     * user signals unwillingness to read or write using
649     * @ref lsquic_stream_wantread() or @ref lsquic_stream_wantwrite()
650     * or shuts down the stream.
651     *
652     * This also applies to the on_dg_write() callback.
653     *
654     * The default value is @ref LSQUIC_DF_RW_ONCE.
655     */
656    int             es_rw_once;
657
658    /**
659     * If set, this value specifies the number of microseconds that
660     * @ref lsquic_engine_process_conns() and
661     * @ref lsquic_engine_send_unsent_packets() are allowed to spend
662     * before returning.
663     *
664     * This is not an exact science and the connections must make
665     * progress, so the deadline is checked after all connections get
666     * a chance to tick (in the case of @ref lsquic_engine_process_conns())
667     * and at least one batch of packets is sent out.
668     *
669     * When processing function runs out of its time slice, immediate
670     * calls to @ref lsquic_engine_has_unsent_packets() return false.
671     *
672     * The default value is @ref LSQUIC_DF_PROC_TIME_THRESH.
673     */
674    unsigned        es_proc_time_thresh;
675
676    /**
677     * If set to true, packet pacing is implemented per connection.
678     *
679     * The default value is @ref LSQUIC_DF_PACE_PACKETS.
680     */
681    int             es_pace_packets;
682
683    /**
684     * Clock granularity information is used by the pacer.  The value
685     * is in microseconds; default is @ref LSQUIC_DF_CLOCK_GRANULARITY.
686     */
687    unsigned        es_clock_granularity;
688
689    /**
690     * Congestion control algorithm to use.
691     *
692     *  0:  Use default (@ref LSQUIC_DF_CC_ALGO)
693     *  1:  Cubic
694     *  2:  BBRv1
695     *  3:  Adaptive (Cubic or BBRv1)
696     */
697    unsigned        es_cc_algo;
698
699    /**
700     * Congestion controller RTT threshold in microseconds.
701     *
702     * Adaptive congestion control uses BBRv1 until RTT is determined.  At
703     * that point a permanent choice of congestion controller is made. If
704     * RTT is smaller than or equal to es_cc_rtt_thresh, congestion
705     * controller is switched to Cubic; otherwise, BBRv1 is picked.
706     *
707     * The default value is @ref LSQUIC_DF_CC_RTT_THRESH.
708     */
709    unsigned        es_cc_rtt_thresh;
710
711    /**
712     * No progress timeout.
713     *
714     * If connection does not make progress for this number of seconds, the
715     * connection is dropped.  Here, progress is defined as user streams
716     * being written to or read from.
717     *
718     * If this value is zero, this timeout is disabled.
719     *
720     * Default value is @ref LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER in server
721     * mode and @ref LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT in client mode.
722     */
723    unsigned        es_noprogress_timeout;
724
725    /* The following settings are specific to IETF QUIC. */
726    /* vvvvvvvvvvv */
727
728    /**
729     * Initial max data.
730     *
731     * This is a transport parameter.
732     *
733     * Depending on the engine mode, the default value is either
734     * @ref LSQUIC_DF_INIT_MAX_DATA_CLIENT or
735     * @ref LSQUIC_DF_INIT_MAX_DATA_SERVER.
736     */
737    unsigned        es_init_max_data;
738
739    /**
740     * Initial maximum amount of stream data allowed to be sent on streams
741     * created by remote end (peer).
742     *
743     * This is a transport parameter.
744     *
745     * Depending on the engine mode, the default value is either
746     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT or
747     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER.
748     */
749    unsigned        es_init_max_stream_data_bidi_remote;
750
751    /**
752     * Initial maximum amount of stream data allowed to be sent on streams
753     * created by remote end (peer).
754     *
755     * This is a transport parameter.
756     *
757     * Depending on the engine mode, the default value is either
758     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT or
759     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER.
760     */
761    unsigned        es_init_max_stream_data_bidi_local;
762
763    /**
764     * Initial max stream data for unidirectional streams initiated
765     * by remote endpoint.
766     *
767     * This is a transport parameter.
768     *
769     * Depending on the engine mode, the default value is either
770     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT or
771     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER.
772     */
773    unsigned        es_init_max_stream_data_uni;
774
775    /**
776     * Maximum initial number of bidirectional stream.
777     *
778     * This is a transport parameter.
779     *
780     * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_BIDI.
781     */
782    unsigned        es_init_max_streams_bidi;
783
784    /**
785     * Maximum initial number of unidirectional stream.
786     *
787     * This is a transport parameter.
788     *
789     * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT or
790     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER.
791     */
792    unsigned        es_init_max_streams_uni;
793
794    /**
795     * Idle connection timeout.
796     *
797     * This is a transport parameter.
798     *
799     * (Note: es_idle_conn_to is not reused because it is in microseconds,
800     * which, I now realize, was not a good choice.  Since it will be
801     * obsoleted some time after the switchover to IETF QUIC, we do not
802     * have to keep on using strange units.)
803     *
804     * Default value is @ref LSQUIC_DF_IDLE_TIMEOUT.
805     *
806     * Maximum value is 600 seconds.
807     */
808    unsigned        es_idle_timeout;
809
810    /**
811     * Ping period.  If set to non-zero value, the connection will generate and
812     * send PING frames in the absence of other activity.
813     *
814     * By default, the server does not send PINGs and the period is set to zero.
815     * The client's defaut value is @ref LSQUIC_DF_PING_PERIOD.
816     */
817    unsigned        es_ping_period;
818
819    /**
820     * Source Connection ID length.  Only applicable to the IETF QUIC
821     * versions.  Valid values are 0 through 20, inclusive.
822     *
823     * Default value is @ref LSQUIC_DF_SCID_LEN.
824     */
825    unsigned        es_scid_len;
826
827    /**
828     * Source Connection ID issuance rate.  Only applicable to the IETF QUIC
829     * versions.  This field is measured in CIDs per minute.  Using value 0
830     * indicates that there is no rate limit for CID issuance.
831     *
832     * Default value is @ref LSQUIC_DF_SCID_ISS_RATE.
833     */
834    unsigned        es_scid_iss_rate;
835
836    /**
837     * Maximum size of the QPACK dynamic table that the QPACK decoder will
838     * use.
839     *
840     * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_SIZE.
841     */
842    unsigned        es_qpack_dec_max_size;
843
844    /**
845     * Maximum number of blocked streams that the QPACK decoder is willing
846     * to tolerate.
847     *
848     * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_BLOCKED.
849     */
850    unsigned        es_qpack_dec_max_blocked;
851
852    /**
853     * Maximum size of the dynamic table that the encoder is willing to use.
854     * The actual size of the dynamic table will not exceed the minimum of
855     * this value and the value advertized by peer.
856     *
857     * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_SIZE.
858     */
859    unsigned        es_qpack_enc_max_size;
860
861    /**
862     * Maximum number of blocked streams that the QPACK encoder is willing
863     * to risk.  The actual number of blocked streams will not exceed the
864     * minimum of this value and the value advertized by peer.
865     *
866     * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_BLOCKED.
867     */
868    unsigned        es_qpack_enc_max_blocked;
869
870    /**
871     * Enable ECN support.
872     *
873     * The default is @ref LSQUIC_DF_ECN
874     */
875    int             es_ecn;
876
877    /**
878     * Allow peer to migrate connection.
879     *
880     * The default is @ref LSQUIC_DF_ALLOW_MIGRATION
881     */
882    int             es_allow_migration;
883
884    /**
885     * Use QL loss bits.  Allowed values are:
886     *  0:  Do not use loss bits
887     *  1:  Allow loss bits
888     *  2:  Allow and send loss bits
889     *
890     * Default value is @ref LSQUIC_DF_QL_BITS
891     */
892    int             es_ql_bits;
893
894    /**
895     * Enable spin bit.  Allowed values are 0 and 1.
896     *
897     * Default value is @ref LSQUIC_DF_SPIN
898     */
899    int             es_spin;
900
901    /**
902     * Enable delayed ACKs extension.  Allowed values are 0 and 1.
903     *
904     * Default value is @ref LSQUIC_DF_DELAYED_ACKS
905     */
906    int             es_delayed_acks;
907
908    /**
909     * Enable timestamps extension.  Allowed values are 0 and 1.
910     *
911     * Default value is @ref LSQUIC_DF_TIMESTAMPS
912     */
913    int             es_timestamps;
914
915    /**
916     * Maximum packet size we are willing to receive.  This is sent to
917     * peer in transport parameters: the library does not enforce this
918     * limit for incoming packets.
919     *
920     * If set to zero, limit is not set.
921     *
922     * Default value is @ref LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX
923     */
924    unsigned short  es_max_udp_payload_size_rx;
925
926    /**
927     * Enable the "QUIC bit grease" extension.  When set to a true value,
928     * lsquic will grease the QUIC bit on the outgoing QUIC packets if
929     * the peer sent the "grease_quic_bit" transport parameter.
930     *
931     * Default value is @ref LSQUIC_DF_GREASE_QUIC_BIT
932     */
933    int             es_grease_quic_bit;
934
935    /**
936     * If set to true value, enable DPLPMTUD -- Datagram Packetization
937     * Layer Path MTU Discovery.
938     *
939     * Default value is @ref LSQUIC_DF_DPLPMTUD
940     */
941    int             es_dplpmtud;
942
943    /**
944     * PLPMTU size expected to work for most paths.
945     *
946     * If set to zero, this value is calculated based on QUIC and IP versions.
947     *
948     * Default value is @ref LSQUIC_DF_BASE_PLPMTU.
949     */
950    unsigned short  es_base_plpmtu;
951
952    /**
953     * Largest PLPMTU size the engine will try.
954     *
955     * If set to zero, picking this value is left to the engine.
956     *
957     * Default value is @ref LSQUIC_DF_MAX_PLPMTU.
958     */
959    unsigned short  es_max_plpmtu;
960
961    /**
962     * This value specifies how long the DPLPMTUD probe timer is, in
963     * milliseconds.  [draft-ietf-tsvwg-datagram-plpmtud-17] says:
964     *
965     " PROBE_TIMER:  The PROBE_TIMER is configured to expire after a period
966     "    longer than the maximum time to receive an acknowledgment to a
967     "    probe packet.  This value MUST NOT be smaller than 1 second, and
968     "    SHOULD be larger than 15 seconds.  Guidance on selection of the
969     "    timer value are provided in section 3.1.1 of the UDP Usage
970     "    Guidelines [RFC8085].
971     *
972     * If set to zero, the default is used.
973     *
974     * Default value is @ref LSQUIC_DF_MTU_PROBE_TIMER.
975     */
976    unsigned        es_mtu_probe_timer;
977
978    /**
979     * Enable datagram extension.  Allowed values are 0 and 1.
980     *
981     * Default value is @ref LSQUIC_DF_DATAGRAMS
982     */
983    int             es_datagrams;
984
985    /**
986     * If set to true, changes in peer port are assumed to be due to a
987     * benign NAT rebinding and path characteristics -- MTU, RTT, and
988     * CC state -- are not reset.
989     *
990     * Default value is @ref LSQUIC_DF_OPTIMISTIC_NAT.
991     */
992    int             es_optimistic_nat;
993
994    /**
995     * If set to true, Extensible HTTP Priorities are enabled.  This
996     * is HTTP/3-only setting.
997     *
998     * Default value is @ref LSQUIC_DF_EXT_HTTP_PRIO
999     */
1000    int             es_ext_http_prio;
1001
1002    /**
1003     * If set to 1, QPACK statistics are logged per connection.
1004     *
1005     * If set to 2, QPACK experiments are run.  In this mode, encoder
1006     * and decoder setting values are randomly selected (from the range
1007     * [0, whatever is specified in es_qpack_(enc|dec)_*]) and these
1008     * values along with compression ratio and user agent are logged at
1009     * NOTICE level when connection is destroyed.  The purpose of these
1010     * experiments is to use compression performance statistics to figure
1011     * out a good set of default values.
1012     *
1013     * Default value is @ref LSQUIC_DF_QPACK_EXPERIMENT.
1014     */
1015    int             es_qpack_experiment;
1016
1017    /**
1018     * Settings for the Packet Tolerance PID Controller (PTPC) used for
1019     * the Delayed ACKs logic.  Periodicity is how often the number of
1020     * incoming ACKs is sampled.  Periodicity's units is the number of
1021     * RTTs. Target is the average number of incoming ACKs per RTT we
1022     * want to achieve.  Error threshold defines the range of error values
1023     * within which no action is taken.  For example, error threshold of
1024     * 0.03 means that adjustment actions will be taken only when the
1025     * error is outside of the [-0.03, 0.03] range.  Proportional and
1026     * integral gains have their usual meanings described here:
1027     *      https://en.wikipedia.org/wiki/PID_controller#Controller_theory
1028     *
1029     * The average is normalized as follows:
1030     *    AvgNormalized = Avg * e / Target      # Where 'e' is 2.71828...
1031     *
1032     * The error is then calculated as ln(AvgNormalized) - 1.  This gives
1033     * us a logarithmic scale that is convenient to use for adjustment
1034     * calculations.  The error divisor is used to calculate the packet
1035     * tolerance adjustment:
1036     *    Adjustment = Error / ErrorDivisor
1037     *
1038     * WARNING.  The library comes with sane defaults.  Only fiddle with
1039     * these knobs if you know what you are doing.
1040     */
1041    unsigned es_ptpc_periodicity;   /* LSQUIC_DF_PTPC_PERIODICITY */
1042    unsigned es_ptpc_max_packtol;   /* LSQUIC_DF_PTPC_MAX_PACKTOL */
1043    int      es_ptpc_dyn_target;    /* LSQUIC_DF_PTPC_DYN_TARGET */
1044    float    es_ptpc_target,        /* LSQUIC_DF_PTPC_TARGET */
1045             es_ptpc_prop_gain,     /* LSQUIC_DF_PTPC_PROP_GAIN */
1046             es_ptpc_int_gain,      /* LSQUIC_DF_PTPC_INT_GAIN */
1047             es_ptpc_err_thresh,    /* LSQUIC_DF_PTPC_ERR_THRESH */
1048             es_ptpc_err_divisor;   /* LSQUIC_DF_PTPC_ERR_DIVISOR */
1049
1050    /**
1051     * When set to true, the on_close() callback will be delayed until the
1052     * peer acknowledges all data sent on the stream.  (Or until the connection
1053     * is destroyed in some manner -- either explicitly closed by the user or
1054     * as a result of an engine shutdown.)
1055     *
1056     * Default value is @ref LSQUIC_DF_DELAY_ONCLOSE
1057     */
1058    int             es_delay_onclose;
1059
1060    /**
1061     * If set to a non-zero value, specified maximum batch size.  (The
1062     * batch of packets passed to @ref ea_packets_out() callback).  Must
1063     * be no larger than 1024.
1064     *
1065     * Default value is @ref LSQUIC_DF_MAX_BATCH_SIZE
1066     */
1067    unsigned        es_max_batch_size;
1068
1069    /**
1070     * When true, sanity checks are performed on peer's transport parameter
1071     * values.  If some limits are set suspiciously low, the connection won't
1072     * be established.
1073     *
1074     * Default value is @ref LSQUIC_DF_CHECK_TP_SANITY
1075     */
1076    int             es_check_tp_sanity;
1077};
1078
1079/* Initialize `settings' to default values */
1080void
1081lsquic_engine_init_settings (struct lsquic_engine_settings *,
1082                             unsigned lsquic_engine_flags);
1083
1084/**
1085 * Check settings for errors.
1086 *
1087 * @param   settings    Settings struct.
1088 *
1089 * @param   flags       Engine flags.
1090 *
1091 * @param   err_buf     Optional pointer to buffer into which error string
1092 *                      is written.
1093
1094 * @param   err_buf_sz  Size of err_buf.  No more than this number of bytes
1095 *                      will be written to err_buf, including the NUL byte.
1096 *
1097 * @retval  0   Settings have no errors.
1098 * @retval -1   There are errors in settings.
1099 */
1100int
1101lsquic_engine_check_settings (const struct lsquic_engine_settings *settings,
1102                              unsigned lsquic_engine_flags,
1103                              char *err_buf, size_t err_buf_sz);
1104
1105struct lsquic_out_spec
1106{
1107    struct iovec          *iov;
1108    size_t                 iovlen;
1109    const struct sockaddr *local_sa;
1110    const struct sockaddr *dest_sa;
1111    void                  *peer_ctx;
1112    lsquic_conn_ctx_t     *conn_ctx;  /* will be NULL when sending out the first batch of handshake packets */
1113    int                    ecn;       /* Valid values are 0 - 3.  See RFC 3168 */
1114};
1115
1116/**
1117 * Returns number of packets successfully sent out or -1 on error.  -1 should
1118 * only be returned if no packets were sent out.  If -1 is returned or if the
1119 * return value is smaller than `n_packets_out', this indicates that sending
1120 * of packets is not possible.
1121 *
1122 * If not all packets could be sent out, errno is examined.  If it is not
1123 * EAGAIN or EWOULDBLOCK, the connection whose packet cause the error is
1124 * closed forthwith.
1125 *
1126 * No packets will be attempted to be sent out until
1127 * @ref lsquic_engine_send_unsent_packets() is called.
1128 */
1129typedef int (*lsquic_packets_out_f)(
1130    void                          *packets_out_ctx,
1131    const struct lsquic_out_spec  *out_spec,
1132    unsigned                       n_packets_out
1133);
1134
1135/**
1136 * The shared hash interface is used to share data between multiple LSQUIC
1137 * instances.
1138 */
1139struct lsquic_shared_hash_if
1140{
1141    /**
1142     * If you want your item to never expire, set `expiry' to zero.
1143     * Returns 0 on success, -1 on failure.
1144     *
1145     * If inserted successfully, `free()' will be called on `data' and 'key'
1146     * pointer when the element is deleted, whether due to expiration
1147     * or explicit deletion.
1148     */
1149    int (*shi_insert)(void *shi_ctx, void *key, unsigned key_sz,
1150                      void *data, unsigned data_sz, time_t expiry);
1151    /**
1152     * Returns 0 on success, -1 on failure.
1153     */
1154    int (*shi_delete)(void *shi_ctx, const void *key, unsigned key_sz);
1155
1156    /**
1157     * `data' is pointed to the result and `data_sz' is set to the
1158     * object size.  The implementation may choose to copy the object
1159     * into buffer pointed to by `data', so you should have it ready.
1160     *
1161     * @retval  1   found.
1162     * @retval  0   not found.
1163     * @retval -1   error (perhaps not enough room in `data' if copy was
1164     *                attempted).
1165     */
1166    int (*shi_lookup)(void *shi_ctx, const void *key, unsigned key_sz,
1167                                     void **data, unsigned *data_sz);
1168};
1169
1170/**
1171 * The packet out memory interface is used by LSQUIC to get buffers to
1172 * which outgoing packets will be written before they are passed to
1173 * ea_packets_out callback.
1174 *
1175 * If not specified, malloc() and free() are used.
1176 */
1177struct lsquic_packout_mem_if
1178{
1179    /**
1180     * Allocate buffer for sending.
1181     */
1182    void *  (*pmi_allocate) (void *pmi_ctx, void *peer_ctx, lsquic_conn_ctx_t *, unsigned short sz,
1183                                                                char is_ipv6);
1184    /**
1185     * This function is used to release the allocated buffer after it is
1186     * sent via @ref ea_packets_out.
1187     */
1188    void    (*pmi_release)  (void *pmi_ctx, void *peer_ctx, void *buf,
1189                                                                char is_ipv6);
1190    /**
1191     * If allocated buffer is not going to be sent, return it to the caller
1192     * using this function.
1193     */
1194    void    (*pmi_return)  (void *pmi_ctx, void *peer_ctx, void *buf,
1195                                                                char is_ipv6);
1196};
1197
1198typedef void (*lsquic_cids_update_f)(void *ctx, void **peer_ctx,
1199                                const lsquic_cid_t *cids, unsigned n_cids);
1200
1201struct stack_st_X509;
1202
1203enum lsquic_hsi_flag {
1204    /**
1205     * Turn HTTP/1.x mode on or off.  In this mode, decoded name and value
1206     * pair are separated by ": " and "\r\n" is appended to the end of the
1207     * string.  By default, this mode is off.
1208     */
1209    LSQUIC_HSI_HTTP1X          = 1 << 1,
1210    /** Include name hash into lsxpack_header */
1211    LSQUIC_HSI_HASH_NAME       = 1 << 2,
1212    /** Include nameval hash into lsxpack_header */
1213    LSQUIC_HSI_HASH_NAMEVAL    = 1 << 3,
1214};
1215
1216struct lsquic_hset_if
1217{
1218    /**
1219     * Create a new header set.  This object is (and must be) fetched from a
1220     * stream by calling @ref lsquic_stream_get_hset() before the stream can
1221     * be read.
1222     *
1223     * `stream' may be set to NULL in server mode.
1224     */
1225    void * (*hsi_create_header_set)(void *hsi_ctx, lsquic_stream_t *stream,
1226                                    int is_push_promise);
1227    /**
1228     * Return a header set prepared for decoding.  If `hdr' is NULL, this
1229     * means return a new structure with at least `space' bytes available
1230     * in the decoder buffer.  On success, a newly prepared header is
1231     * returned.
1232     *
1233     * If `hdr' is not NULL, it means there was not enough decoder buffer
1234     * and it must be increased to at least `space' bytes.  `buf', `val_len',
1235     * and `name_offset' member of the `hdr' structure may change.  On
1236     * success, the return value is the same as `hdr'.
1237     *
1238     * If NULL is returned, the space cannot be allocated.
1239     */
1240    struct lsxpack_header *
1241                        (*hsi_prepare_decode)(void *hdr_set,
1242                                              struct lsxpack_header *hdr,
1243                                              size_t space);
1244    /**
1245     * Process new header.  Return 0 on success, a positive value if a header
1246     * error occured, or a negative value on any other error.
1247     *
1248     * A positive return value will result in cancellation of associated
1249     * stream.
1250     *
1251     * A negative return value will result in connection being aborted.
1252     *
1253     * `hdr_set' is the header set object returned by
1254     * @ref hsi_create_header_set().
1255     *
1256     * `hdr' is the header returned by @ref `hsi_prepare_decode'.
1257     *
1258     * If `hdr' is NULL, this means that no more header are going to be
1259     * added to the set.
1260     */
1261    int (*hsi_process_header)(void *hdr_set, struct lsxpack_header *hdr);
1262    /**
1263     * Discard header set.  This is called for unclaimed header sets and
1264     * header sets that had an error.
1265     */
1266    void                (*hsi_discard_header_set)(void *hdr_set);
1267    /**
1268     * These flags specify properties of decoded headers passed to
1269     * hsi_process_header().  This is only applicable to QPACK headers;
1270     * HPACK library header properties are based on compilation, not
1271     * run-time, options.
1272     */
1273    enum lsquic_hsi_flag hsi_flags;
1274};
1275
1276/**
1277 * This struct contains a list of all callbacks that are used by the engine
1278 * to communicate with the user code.  Most of these are optional, while
1279 * the following are mandatory:
1280 *
1281 *  @ref ea_stream_if       The stream interface.
1282 *  @ref ea_packets_out     Function to send packets.
1283 *  @ref ea_lookup_cert     Function to look up certificates by SNI (used
1284 *                            in server mode).
1285 *
1286 * A pointer to this structure is passed to engine constructor
1287 * @ref lsquic_engine_new().
1288 */
1289struct lsquic_engine_api
1290{
1291    const struct lsquic_engine_settings *ea_settings;   /* Optional */
1292    /** Stream interface is required to manage connections and streams. */
1293    const struct lsquic_stream_if       *ea_stream_if;
1294    void                                *ea_stream_if_ctx;
1295    /** Function to send packets out is required. */
1296    lsquic_packets_out_f                 ea_packets_out;
1297    void                                *ea_packets_out_ctx;
1298    /** Function to look up certificates by SNI is used in server mode. */
1299    lsquic_lookup_cert_f                 ea_lookup_cert;
1300    void                                *ea_cert_lu_ctx;
1301    /** Mandatory callback for server, optional for client. */
1302    struct ssl_ctx_st *                (*ea_get_ssl_ctx)(void *peer_ctx,
1303                                                const struct sockaddr *local);
1304    /**
1305     * Shared hash interface is optional.  If set to zero, performance of
1306     * multiple LSQUIC instances will be degraded.
1307     */
1308    const struct lsquic_shared_hash_if  *ea_shi;
1309    void                                *ea_shi_ctx;
1310    /**
1311     * Memory interface is optional.
1312     */
1313    const struct lsquic_packout_mem_if  *ea_pmi;
1314    void                                *ea_pmi_ctx;
1315    /**
1316     * Optional interface to report new and old source connection IDs.
1317     */
1318    lsquic_cids_update_f                 ea_new_scids;
1319    lsquic_cids_update_f                 ea_live_scids;
1320    lsquic_cids_update_f                 ea_old_scids;
1321    void                                *ea_cids_update_ctx;
1322    /**
1323     * Function to verify server certificate.  The chain contains at least
1324     * one element.  The first element in the chain is the server
1325     * certificate.  The chain belongs to the library.  If you want to
1326     * retain it, call sk_X509_up_ref().
1327     *
1328     * 0 is returned on success, -1 on error.
1329     *
1330     * If the function pointer is not set, no verification is performed
1331     * (the connection is allowed to proceed).
1332     */
1333    int                                (*ea_verify_cert)(void *verify_ctx,
1334                                                struct stack_st_X509 *chain);
1335    void                                *ea_verify_ctx;
1336
1337    /**
1338     * Optional header set interface.  If not specified, the incoming headers
1339     * are converted to HTTP/1.x format and are read from stream and have to
1340     * be parsed again.
1341     */
1342    const struct lsquic_hset_if         *ea_hsi_if;
1343    void                                *ea_hsi_ctx;
1344
1345    /**
1346     * If set, engine will print cumulative connection statistics to this
1347     * file just before it is destroyed.  (Must be compiled with
1348     * -DLSQUIC_CONN_STATS=1).
1349     */
1350    void /* FILE, really */             *ea_stats_fh;
1351
1352    /**
1353     * The optional ALPN string is used by the client if @ref LSENG_HTTP
1354     * is not set.
1355     */
1356    const char                          *ea_alpn;
1357
1358    /**
1359     * Optional interface to control the creation of connection IDs
1360     */
1361    void                               (*ea_generate_scid)(void *ctx,
1362                                lsquic_conn_t *, lsquic_cid_t *, unsigned);
1363    /** Passed to ea_generate_scid() */
1364    void                                *ea_gen_scid_ctx;
1365};
1366
1367/**
1368 * Create new engine.
1369 *
1370 * @param   lsquic_engine_flags     A bitmask of @ref LSENG_SERVER and
1371 *                                  @ref LSENG_HTTP
1372 *
1373 * @param   api                     Required parameter that specifies
1374 *                                    various callbacks.
1375 *
1376 * The engine can be instantiated either in server mode (when LSENG_SERVER
1377 * is set) or client mode.  If you need both server and client in your
1378 * program, create two engines (or as many as you'd like).
1379 */
1380lsquic_engine_t *
1381lsquic_engine_new (unsigned lsquic_engine_flags,
1382                   const struct lsquic_engine_api *api);
1383
1384/**
1385 * Create a client connection to peer identified by `peer_ctx'.
1386 *
1387 * To let the engine specify QUIC version, use N_LSQVER.  If session resumption
1388 * information is supplied, version is picked from there instead.
1389 *
1390 * If `base_plpmtu' is set to zero, it is selected based on the
1391 * engine settings, QUIC version, and IP version.
1392 */
1393lsquic_conn_t *
1394lsquic_engine_connect (lsquic_engine_t *, enum lsquic_version,
1395                       const struct sockaddr *local_sa,
1396                       const struct sockaddr *peer_sa,
1397                       void *peer_ctx, lsquic_conn_ctx_t *conn_ctx,
1398                       const char *hostname, unsigned short base_plpmtu,
1399                       const unsigned char *sess_resume, size_t sess_resume_len,
1400                       /** Resumption token: optional */
1401                       const unsigned char *token, size_t token_sz);
1402
1403/**
1404 * Pass incoming packet to the QUIC engine.  This function can be called
1405 * more than once in a row.  After you add one or more packets, call
1406 * lsquic_engine_process_conns() to schedule output, if any.
1407 *
1408 * @retval  0   Packet was processed by a real connection.
1409 *
1410 * @retval  1   Packet was handled successfully, but not by a connection.
1411 *              This may happen with version negotiation and public reset
1412 *              packets as well as some packets that may be ignored.
1413 *
1414 * @retval -1   An error occurred.  Possible reasons are failure to allocate
1415 *              memory and invalid @param sa_local in client mode.
1416 */
1417int
1418lsquic_engine_packet_in (lsquic_engine_t *,
1419        const unsigned char *packet_in_data, size_t packet_in_size,
1420        const struct sockaddr *sa_local, const struct sockaddr *sa_peer,
1421        void *peer_ctx, int ecn);
1422
1423/**
1424 * Process tickable connections.  This function must be called often enough so
1425 * that packets and connections do not expire.
1426 */
1427void
1428lsquic_engine_process_conns (lsquic_engine_t *engine);
1429
1430/**
1431 * Returns true if engine has some unsent packets.  This happens if
1432 * @ref ea_packets_out() could not send everything out or if processing
1433 * deadline was exceeded (see @ref es_proc_time_thresh).
1434 */
1435int
1436lsquic_engine_has_unsent_packets (lsquic_engine_t *engine);
1437
1438/**
1439 * Send out as many unsent packets as possibe: until we are out of unsent
1440 * packets or until @ref ea_packets_out() fails.
1441 *
1442 * If @ref ea_packets_out() does fail cannot send all packets, this
1443 * function must be called to signify that sending of packets is possible
1444 * again.
1445 */
1446void
1447lsquic_engine_send_unsent_packets (lsquic_engine_t *engine);
1448
1449/**
1450 * Destroy engine and all connections and streams in it and free all
1451 * memory associated with this engine.
1452 */
1453void
1454lsquic_engine_destroy (lsquic_engine_t *);
1455
1456/** Return max allowed outbound streams less current outbound streams. */
1457unsigned
1458lsquic_conn_n_avail_streams (const lsquic_conn_t *);
1459
1460/**
1461 * Create a new request stream.  This causes @ref on_new_stream() callback
1462 * to be called.  If creating more requests is not permitted at the moment
1463 * (due to number of concurrent streams limit), stream creation is registered
1464 * as "pending" and the stream is created later when number of streams dips
1465 * under the limit again.  Any number of pending streams can be created.
1466 * Use @ref lsquic_conn_n_pending_streams() and
1467 * @ref lsquic_conn_cancel_pending_streams() to manage pending streams.
1468 *
1469 * If connection is going away, @ref on_new_stream() is called with the
1470 * stream parameter set to NULL.
1471 */
1472void
1473lsquic_conn_make_stream (lsquic_conn_t *);
1474
1475/** Return number of delayed streams currently pending */
1476unsigned
1477lsquic_conn_n_pending_streams (const lsquic_conn_t *);
1478
1479/** Cancel `n' pending streams.  Returns new number of pending streams. */
1480unsigned
1481lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n);
1482
1483/**
1484 * Mark connection as going away: send GOAWAY frame and do not accept
1485 * any more incoming streams, nor generate streams of our own.
1486 *
1487 * Only applicable to HTTP/3 and GQUIC connections.  Otherwise a no-op.
1488 */
1489void
1490lsquic_conn_going_away (lsquic_conn_t *);
1491
1492/**
1493 * This forces connection close.  on_conn_closed and on_close callbacks
1494 * will be called.
1495 */
1496void
1497lsquic_conn_close (lsquic_conn_t *);
1498
1499/**
1500 * Set whether you want to read from stream.  If @param is_want is true,
1501 * @ref on_read() will be called when there is readable data in the
1502 * stream.  If @param is false, @ref on_read() will not be called.
1503 *
1504 * Returns previous value of this flag.
1505 */
1506int
1507lsquic_stream_wantread (lsquic_stream_t *s, int is_want);
1508
1509/**
1510 * Read up to @param len bytes from stream into @param buf.  Returns number
1511 * of bytes read or -1 on error, in which case errno is set.  Possible
1512 * errno values:
1513 *
1514 *  EBADF           The stream is closed.
1515 *  ECONNRESET      The stream has been reset.
1516 *  EWOULDBLOCK     There is no data to be read.
1517 *
1518 * Return value of zero indicates EOF.
1519 */
1520ssize_t
1521lsquic_stream_read (lsquic_stream_t *s, void *buf, size_t len);
1522
1523/**
1524 * Similar to @ref lsquic_stream_read(), but reads data into @param vec.
1525 */
1526ssize_t
1527lsquic_stream_readv (lsquic_stream_t *s, const struct iovec *vec, int iovcnt);
1528
1529/**
1530 * This function allows user-supplied callback to read the stream contents.
1531 * It is meant to be used for zero-copy stream processing.
1532 *
1533 * Return value and errors are same as in @ref lsquic_stream_read().
1534 */
1535ssize_t
1536lsquic_stream_readf (lsquic_stream_t *s,
1537    /**
1538     * The callback takes four parameters:
1539     *  - Pointer to user-supplied context;
1540     *  - Pointer to the data;
1541     *  - Data size (can be zero); and
1542     *  - Indicator whether the FIN follows the data.
1543     *
1544     * The callback returns number of bytes processed.  If this number is zero
1545     * or is smaller than `len', reading from stream stops.
1546     */
1547    size_t (*readf)(void *ctx, const unsigned char *buf, size_t len, int fin),
1548    void *ctx);
1549
1550/**
1551 * Set whether you want to write to stream.  If @param is_want is true,
1552 * @ref on_write() will be called when it is possible to write data to
1553 * the stream.  If @param is false, @ref on_write() will not be called.
1554 *
1555 * Returns previous value of this flag.
1556 */
1557int
1558lsquic_stream_wantwrite (lsquic_stream_t *s, int is_want);
1559
1560/**
1561 * Write `len' bytes to the stream.  Returns number of bytes written, which
1562 * may be smaller that `len'.
1563 *
1564 * A negative return value indicates a serious error (the library is likely
1565 * to have aborted the connection because of it).
1566 */
1567ssize_t
1568lsquic_stream_write (lsquic_stream_t *s, const void *buf, size_t len);
1569
1570/**
1571 * Like @ref lsquic_stream_write(), but read data from @param vec.
1572 */
1573ssize_t
1574lsquic_stream_writev (lsquic_stream_t *s, const struct iovec *vec, int count);
1575
1576/**
1577 * Write to streams using a single call to a preadv-like function.
1578 */
1579ssize_t
1580lsquic_stream_pwritev (lsquic_stream_t *s,
1581    ssize_t (*preadv)(void *user_data, const struct iovec *iov, int iovcnt),
1582    void *user_data, size_t n_to_write);
1583
1584/**
1585 * Used as argument to @ref lsquic_stream_writef()
1586 */
1587struct lsquic_reader
1588{
1589    /**
1590     * Not a ssize_t because the read function is not supposed to return
1591     * an error.  If an error occurs in the read function (for example, when
1592     * reading from a file fails), it is supposed to deal with the error
1593     * itself.
1594     */
1595    size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count);
1596    /**
1597     * Return number of bytes remaining in the reader.
1598     */
1599    size_t (*lsqr_size) (void *lsqr_ctx);
1600    void    *lsqr_ctx;
1601};
1602
1603/**
1604 * Write to stream using @ref lsquic_reader.  This is the most generic of
1605 * the write functions -- @ref lsquic_stream_write() and
1606 * @ref lsquic_stream_writev() utilize the same mechanism.
1607 *
1608 * @retval Number of bytes written or -1 on error.
1609 */
1610ssize_t
1611lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *);
1612
1613/**
1614 * Flush any buffered data.  This triggers packetizing even a single byte
1615 * into a separate frame.  Flushing a closed stream is an error.
1616 *
1617 * @retval  0   Success
1618 * @retval -1   Failure
1619 */
1620int
1621lsquic_stream_flush (lsquic_stream_t *s);
1622
1623/**
1624 * @typedef lsquic_http_headers_t
1625 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs.
1626 * used in API functions to pass headers.
1627 */
1628struct lsquic_http_headers
1629{
1630    int                     count;
1631    struct lsxpack_header  *headers;
1632};
1633
1634/**
1635 * Send headers in @param headers.  This function must be called before
1636 * writing to the stream.  The value of @param eos is ignored in IETF QUIC.
1637 */
1638int
1639lsquic_stream_send_headers (lsquic_stream_t *s,
1640                               const lsquic_http_headers_t *headers, int eos);
1641
1642/**
1643 * Get header set associated with the stream.  The header set is created by
1644 * @ref hsi_create_header_set() callback.  After this call, the ownership of
1645 * the header set is transferred to the caller.
1646 *
1647 * This call must precede calls to @ref lsquic_stream_read() and
1648 * @ref lsquic_stream_readv().
1649 *
1650 * If the optional header set interface (@ref ea_hsi_if) is not specified,
1651 * this function returns NULL.
1652 */
1653void *
1654lsquic_stream_get_hset (lsquic_stream_t *);
1655
1656/**
1657 * A server may push a stream.  This call creates a new stream in reference
1658 * to stream `s'.  It will behave as if the client made a request: it will
1659 * trigger on_new_stream() event and it can be used as a regular client-
1660 * initiated stream.
1661 *
1662 * `hdr_set' must be set.  It is passed as-is to @lsquic_stream_get_hset.
1663 *
1664 * @retval  0   Stream pushed successfully.
1665 * @retval  1   Stream push failed because it is disabled or because we hit
1666 *                stream limit or connection is going away.
1667 * @retval -1   Stream push failed because of an internal error.
1668 */
1669int
1670lsquic_conn_push_stream (lsquic_conn_t *c, void *hdr_set, lsquic_stream_t *s,
1671    const lsquic_http_headers_t *headers);
1672
1673/**
1674 * Only makes sense in server mode: the client cannot push a stream and this
1675 * function always returns false in client mode.
1676 */
1677int
1678lsquic_conn_is_push_enabled (lsquic_conn_t *);
1679
1680/** Possible values for how are 0, 1, and 2.  See shutdown(2). */
1681int lsquic_stream_shutdown(lsquic_stream_t *s, int how);
1682
1683int lsquic_stream_close(lsquic_stream_t *s);
1684
1685/**
1686 * Return true if peer has not ACKed all data written to the stream.  This
1687 * includes both packetized and buffered data.
1688 */
1689int
1690lsquic_stream_has_unacked_data (lsquic_stream_t *s);
1691
1692/**
1693 * Get certificate chain returned by the server.  This can be used for
1694 * server certificate verification.
1695 *
1696 * The caller releases the stack using sk_X509_free().
1697 */
1698struct stack_st_X509 *
1699lsquic_conn_get_server_cert_chain (lsquic_conn_t *);
1700
1701/** Returns ID of the stream */
1702lsquic_stream_id_t
1703lsquic_stream_id (const lsquic_stream_t *s);
1704
1705/**
1706 * Returns stream ctx associated with the stream.  (The context is what
1707 * is returned by @ref on_new_stream callback).
1708 */
1709lsquic_stream_ctx_t *
1710lsquic_stream_get_ctx (const lsquic_stream_t *s);
1711
1712/** Returns true if this is a pushed stream */
1713int
1714lsquic_stream_is_pushed (const lsquic_stream_t *s);
1715
1716/**
1717 * Returns true if this stream was rejected, false otherwise.  Use this as
1718 * an aid to distinguish between errors.
1719 */
1720int
1721lsquic_stream_is_rejected (const lsquic_stream_t *s);
1722
1723/**
1724 * Refuse pushed stream.  Call it from @ref on_new_stream.
1725 *
1726 * No need to call lsquic_stream_close() after this.  on_close will be called.
1727 *
1728 * @see lsquic_stream_is_pushed
1729 */
1730int
1731lsquic_stream_refuse_push (lsquic_stream_t *s);
1732
1733/**
1734 * Get information associated with pushed stream:
1735 *
1736 * @param ref_stream_id   Stream ID in response to which push promise was
1737 *                            sent.
1738 * @param hdr_set         Header set.  This object was passed to or generated
1739 *                            by @ref lsquic_conn_push_stream().
1740 *
1741 * @retval   0  Success.
1742 * @retval  -1  This is not a pushed stream.
1743 */
1744int
1745lsquic_stream_push_info (const lsquic_stream_t *,
1746                         lsquic_stream_id_t *ref_stream_id, void **hdr_set);
1747
1748/** Return current priority of the stream */
1749unsigned lsquic_stream_priority (const lsquic_stream_t *s);
1750
1751/**
1752 * Set stream priority.  Valid priority values are 1 through 256, inclusive.
1753 * Lower value means higher priority.
1754 *
1755 * @retval   0  Success.
1756 * @retval  -1  Priority value is invalid.
1757 */
1758int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority);
1759
1760/*
1761 * Definitions for Extensible HTTP Priorities:
1762 * https://tools.ietf.org/html/draft-ietf-httpbis-priority-01
1763 */
1764/* This is maximum *value* -- but it's the lowest *priority* */
1765#define LSQUIC_MAX_HTTP_URGENCY 7
1766#define LSQUIC_DEF_HTTP_URGENCY 3
1767#define LSQUIC_DEF_HTTP_INCREMENTAL 0
1768
1769struct lsquic_ext_http_prio
1770{
1771    unsigned char   urgency;
1772    signed char     incremental;
1773};
1774
1775/**
1776 * Get Extensible HTTP Priorities associated with the stream.
1777 *
1778 * Returns zero on success of a negative value on failure.  A failure occurs
1779 * if this is not an HTTP/3 stream or if Extensible HTTP Priorities haven't
1780 * been enabled.  See @ref es_ext_http_prio.
1781 */
1782int
1783lsquic_stream_get_http_prio (lsquic_stream_t *, struct lsquic_ext_http_prio *);
1784
1785/**
1786 * Set Extensible HTTP Priorities of the stream.
1787 *
1788 * Returns zero on success of a negative value on failure.  A failure occurs
1789 * if some internal error occured or if this is not an HTTP/3 stream or if
1790 * Extensible HTTP Priorities haven't been enabled.  See @ref es_ext_http_prio.
1791 */
1792int
1793lsquic_stream_set_http_prio (lsquic_stream_t *,
1794                                        const struct lsquic_ext_http_prio *);
1795
1796/**
1797 * Get a pointer to the connection object.  Use it with lsquic_conn_*
1798 * functions.
1799 */
1800lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s);
1801
1802/** Get connection ID */
1803const lsquic_cid_t *
1804lsquic_conn_id (const lsquic_conn_t *c);
1805
1806/** Get pointer to the engine */
1807lsquic_engine_t *
1808lsquic_conn_get_engine (lsquic_conn_t *c);
1809
1810int
1811lsquic_conn_get_sockaddr(lsquic_conn_t *c,
1812                const struct sockaddr **local, const struct sockaddr **peer);
1813
1814/* Returns previous value */
1815int
1816lsquic_conn_want_datagram_write (lsquic_conn_t *, int is_want);
1817
1818/* Get minimum datagram size.  By default, this value is zero. */
1819size_t
1820lsquic_conn_get_min_datagram_size (lsquic_conn_t *);
1821
1822/* Set minimum datagram size.  This is the minumum value of the buffer passed
1823 * to the on_dg_write() callback.
1824 */
1825int
1826lsquic_conn_set_min_datagram_size (lsquic_conn_t *, size_t sz);
1827
1828struct lsquic_logger_if {
1829    int     (*log_buf)(void *logger_ctx, const char *buf, size_t len);
1830};
1831
1832/**
1833 * Enumerate timestamp styles supported by LSQUIC logger mechanism.
1834 */
1835enum lsquic_logger_timestamp_style {
1836    /**
1837     * No timestamp is generated.
1838     */
1839    LLTS_NONE,
1840
1841    /**
1842     * The timestamp consists of 24 hours, minutes, seconds, and
1843     * milliseconds.  Example: 13:43:46.671
1844     */
1845    LLTS_HHMMSSMS,
1846
1847    /**
1848     * Like above, plus date, e.g: 2017-03-21 13:43:46.671
1849     */
1850    LLTS_YYYYMMDD_HHMMSSMS,
1851
1852    /**
1853     * This is Chrome-like timestamp used by proto-quic.  The timestamp
1854     * includes month, date, hours, minutes, seconds, and microseconds.
1855     *
1856     * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
1857     *
1858     * This is to facilitate reading two logs side-by-side.
1859     */
1860    LLTS_CHROMELIKE,
1861
1862    /**
1863     * The timestamp consists of 24 hours, minutes, seconds, and
1864     * microseconds.  Example: 13:43:46.671123
1865     */
1866    LLTS_HHMMSSUS,
1867
1868    /**
1869     * Date and time using microsecond resolution,
1870     * e.g: 2017-03-21 13:43:46.671123
1871     */
1872    LLTS_YYYYMMDD_HHMMSSUS,
1873
1874    N_LLTS
1875};
1876
1877/**
1878 * Call this if you want to do something with LSQUIC log messages, as they
1879 * are thrown out by default.
1880 */
1881void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx,
1882                        enum lsquic_logger_timestamp_style);
1883
1884/**
1885 * Set log level for all LSQUIC modules.  Acceptable values are debug, info,
1886 * notice, warning, error, alert, emerg, crit (case-insensitive).
1887 *
1888 * @retval  0   Success.
1889 * @retval -1   Failure: log_level is not valid.
1890 */
1891int
1892lsquic_set_log_level (const char *log_level);
1893
1894/**
1895 * E.g. "event=debug"
1896 */
1897int
1898lsquic_logger_lopt (const char *optarg);
1899
1900/**
1901 * Return the list of QUIC versions (as bitmask) this engine instance
1902 * supports.
1903 */
1904unsigned lsquic_engine_quic_versions (const lsquic_engine_t *);
1905
1906/**
1907 * This is one of the flags that can be passed to @ref lsquic_global_init.
1908 * Use it to initialize LSQUIC for use in client mode.
1909 */
1910#define LSQUIC_GLOBAL_CLIENT (1 << 0)
1911
1912/**
1913 * This is one of the flags that can be passed to @ref lsquic_global_init.
1914 * Use it to initialize LSQUIC for use in server mode.
1915 */
1916#define LSQUIC_GLOBAL_SERVER (1 << 1)
1917
1918/**
1919 * Initialize LSQUIC.  This must be called before any other LSQUIC function
1920 * is called.  Returns 0 on success and -1 on failure.
1921 *
1922 * @param flags     This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and
1923 *                    @ref LSQUIC_GLOBAL_SERVER.  At least one of these
1924 *                    flags should be specified.
1925 *
1926 * @retval  0   Success.
1927 * @retval -1   Initialization failed.
1928 *
1929 * @see LSQUIC_GLOBAL_CLIENT
1930 * @see LSQUIC_GLOBAL_SERVER
1931 */
1932int
1933lsquic_global_init (int flags);
1934
1935/**
1936 * Clean up global state created by @ref lsquic_global_init.  Should be
1937 * called after all LSQUIC engine instances are gone.
1938 */
1939void
1940lsquic_global_cleanup (void);
1941
1942/**
1943 * Get QUIC version used by the connection.
1944 *
1945 * @see lsquic_version
1946 */
1947enum lsquic_version
1948lsquic_conn_quic_version (const lsquic_conn_t *c);
1949
1950/* Return keysize or -1 on error */
1951int
1952lsquic_conn_crypto_keysize (const lsquic_conn_t *c);
1953
1954/* Return algorithm keysize or -1 on error */
1955int
1956lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *c);
1957
1958enum lsquic_crypto_ver
1959{
1960    LSQ_CRY_QUIC,
1961    LSQ_CRY_TLSv13,
1962};
1963
1964enum lsquic_crypto_ver
1965lsquic_conn_crypto_ver (const lsquic_conn_t *c);
1966
1967/* Return cipher or NULL on error */
1968const char *
1969lsquic_conn_crypto_cipher (const lsquic_conn_t *c);
1970
1971/** Translate string QUIC version to LSQUIC QUIC version representation */
1972enum lsquic_version
1973lsquic_str2ver (const char *str, size_t len);
1974
1975/** Translate ALPN (e.g. "h3", "h3-23", "h3-Q046") to LSQUIC enum */
1976enum lsquic_version
1977lsquic_alpn2ver (const char *alpn, size_t len);
1978
1979/**
1980 * This function closes all mini connections and marks all full connections
1981 * as going away.  In server mode, this also causes the engine to stop
1982 * creating new connections.
1983 */
1984void
1985lsquic_engine_cooldown (lsquic_engine_t *);
1986
1987/**
1988 * Get user-supplied context associated with the connection.
1989 */
1990lsquic_conn_ctx_t *
1991lsquic_conn_get_ctx (const lsquic_conn_t *);
1992
1993/**
1994 * Set user-supplied context associated with the connection.
1995 */
1996void
1997lsquic_conn_set_ctx (lsquic_conn_t *, lsquic_conn_ctx_t *);
1998
1999/**
2000 * Get peer context associated with the connection.
2001 */
2002void *
2003lsquic_conn_get_peer_ctx (lsquic_conn_t *, const struct sockaddr *local_sa);
2004
2005/** Get SNI sent by the client */
2006const char *
2007lsquic_conn_get_sni (lsquic_conn_t *);
2008
2009/**
2010 * Abort connection.
2011 */
2012void
2013lsquic_conn_abort (lsquic_conn_t *);
2014
2015/**
2016 * Helper function: convert list of versions as specified in the argument
2017 * bitmask to string that can be included as argument to "v=" part of the
2018 * Alt-Svc header.
2019 *
2020 * For example (1<<LSQVER_037)|(1<<LSQVER_038) => "37,38"
2021 *
2022 * This is only applicable to Google QUIC versions.
2023 */
2024const char *
2025lsquic_get_alt_svc_versions (unsigned versions);
2026
2027/**
2028 * Return a NULL-terminated list of HTTP/3 ALPNs, e.g "h3-17", "h3-18", "h3".
2029 */
2030const char *const *
2031lsquic_get_h3_alpns (unsigned versions);
2032
2033/**
2034 * Returns true if provided buffer could be a valid handshake-stage packet,
2035 * false otherwise.  Do not call this function if a connection has already
2036 * been established: it will return incorrect result.
2037 */
2038int
2039lsquic_is_valid_hs_packet (lsquic_engine_t *, const unsigned char *, size_t);
2040
2041/**
2042 * Parse cid from packet stored in `buf' and store it to `cid'.  Returns 0
2043 * on success and -1 on failure.
2044 */
2045int
2046lsquic_cid_from_packet (const unsigned char *, size_t bufsz, lsquic_cid_t *cid);
2047
2048/**
2049 * On success, offset to the CID is returned (a non-negative value).
2050 * `cid_len' is set to the length of the CID.  The server perspective
2051 * is assumed.  `server_cid_len' is set to the length of the CIDs that
2052 * server generates.
2053 *
2054 * On failure, a negative value is returned.
2055 */
2056int
2057lsquic_dcid_from_packet (const unsigned char *, size_t bufsz,
2058                                unsigned server_cid_len, unsigned *cid_len);
2059
2060/**
2061 * Returns true if there are connections to be processed, false otherwise.
2062 * If true, `diff' is set to the difference between the earliest advisory
2063 * tick time and now.  If the former is in the past, the value of `diff'
2064 * is negative.
2065 */
2066int
2067lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff);
2068
2069/**
2070 * Return number of connections whose advisory tick time is before current
2071 * time plus `from_now' microseconds from now.  `from_now' can be negative.
2072 */
2073unsigned
2074lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now);
2075
2076enum LSQUIC_CONN_STATUS
2077{
2078    LSCONN_ST_HSK_IN_PROGRESS,
2079    LSCONN_ST_CONNECTED,
2080    LSCONN_ST_HSK_FAILURE,
2081    LSCONN_ST_GOING_AWAY,
2082    LSCONN_ST_TIMED_OUT,
2083    /* If es_honor_prst is not set, the connection will never get public
2084     * reset packets and this flag will not be set.
2085     */
2086    LSCONN_ST_RESET,
2087    LSCONN_ST_USER_ABORTED,
2088    LSCONN_ST_ERROR,
2089    LSCONN_ST_CLOSED,
2090    LSCONN_ST_PEER_GOING_AWAY,
2091};
2092
2093enum LSQUIC_CONN_STATUS
2094lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz);
2095
2096extern const char *const
2097lsquic_ver2str[N_LSQVER];
2098
2099/* Return connection associated with this SSL object */
2100lsquic_conn_t *
2101lsquic_ssl_to_conn (const struct ssl_st *);
2102
2103/* Return session resumption information that can be used on subsequenct
2104 * connection as argument to lsquic_engine_connect().  Call from inside
2105 * SSL's new session callback.
2106 *
2107 * Returns 0 on success.  In this case, `buf' is made to point to newly
2108 * allocated memory containing `buf_sz' bytes.  It is the caller's
2109 * responsibility to free the memory.
2110 */
2111int
2112lsquic_ssl_sess_to_resume_info (struct ssl_st *, struct ssl_session_st *,
2113                                        unsigned char **buf, size_t *buf_sz);
2114
2115#ifdef __cplusplus
2116}
2117#endif
2118
2119#endif //__LSQUIC_H__
2120
2121