apiref.rst revision 692a9102
1API Reference
2=============
3
4.. highlight:: c
5
6Preliminaries
7-------------
8
9All declarations are in :file:`lsquic.h`, so it is enough to
10
11::
12
13    #incluide <lsquic.h>
14
15in each source file.
16
17
18Library Version
19---------------
20
21LSQUIC follows the following versioning model.  The version number
22has the form MAJOR.MINOR.PATCH, where
23
24- MAJOR changes when a large redesign occurs;
25- MINOR changes when an API change or another significant change occurs; and
26- PATCH changes when a bug is fixed or another small, API-compatible change occurs.
27
28QUIC Versions
29-------------
30
31LSQUIC supports two types of QUIC protocol: Google QUIC and IETF QUIC.  The
32former will at some point become obsolete, while the latter is still being
33developed by the IETF.  Both types are included in a single enum:
34
35.. type:: enum lsquic_version
36
37    .. member:: LSQVER_043
38
39        Google QUIC version Q043
40
41    .. member:: LSQVER_046
42
43        Google QUIC version Q046
44
45    .. member:: LSQVER_050
46
47        Google QUIC version Q050
48
49    .. member:: LSQVER_ID27
50
51        IETF QUIC version ID (Internet-Draft) 27
52
53    .. member:: LSQVER_ID28
54
55        IETF QUIC version ID 28
56
57    .. member:: LSQVER_ID29
58
59        IETF QUIC version ID 29
60
61    .. member:: N_LSQVER
62
63        Special value indicating the number of versions in the enum.  It
64        may be used as argument to :func:`lsquic_engine_connect()`.
65
66Several version lists (as bitmasks) are defined in :file:`lsquic.h`:
67
68.. macro:: LSQUIC_SUPPORTED_VERSIONS
69
70List of all supported versions.
71
72.. macro:: LSQUIC_FORCED_TCID0_VERSIONS
73
74List of versions in which the server never includes CID in short packets.
75
76.. macro:: LSQUIC_EXPERIMENTAL_VERSIONS
77
78Experimental versions.
79
80.. macro:: LSQUIC_DEPRECATED_VERSIONS
81
82Deprecated versions.
83
84.. macro:: LSQUIC_GQUIC_HEADER_VERSIONS
85
86Versions that have Google QUIC-like headers.  Only Q043 remains in this
87list.
88
89.. macro:: LSQUIC_IETF_VERSIONS
90
91IETF QUIC versions.
92
93.. macro:: LSQUIC_IETF_DRAFT_VERSIONS
94
95IETF QUIC *draft* versions.  When IETF QUIC v1 is released, it will not
96be included in this list.
97
98LSQUIC Types
99------------
100
101LSQUIC declares several types used by many of its public functions.  They are:
102
103.. type:: lsquic_engine_t
104
105    Instance of LSQUIC engine.
106
107.. type:: lsquic_conn_t
108
109    QUIC connection.
110
111.. type:: lsquic_stream_t
112
113    QUIC stream.
114
115.. type:: lsquic_stream_id_t
116
117    Stream ID.
118
119.. type:: lsquic_conn_ctx_t
120
121    Connection context.  This is the return value of :member:`lsquic_stream_if.on_new_conn`.
122    To LSQUIC, this is just an opaque pointer.  User code is expected to
123    use it for its own purposes.
124
125.. type:: lsquic_stream_ctx_t
126
127    Stream context.  This is the return value of :func:`on_new_stream()`.
128    To LSQUIC, this is just an opaque pointer.  User code is expected to
129    use it for its own purposes.
130
131.. type:: lsquic_http_headers_t
132
133    HTTP headers
134
135Library Initialization
136----------------------
137
138Before using the library, internal structures must be initialized using
139the global initialization function:
140
141::
142
143    if (0 == lsquic_global_init(LSQUIC_GLOBAL_CLIENT|LSQUIC_GLOBAL_SERVER))
144        /* OK, do something useful */
145        ;
146
147This call only needs to be made once.  Afterwards, any number of LSQUIC
148engines may be instantiated.
149
150After a process is done using LSQUIC, it should clean up:
151
152::
153
154    lsquic_global_cleanup();
155
156Logging
157-------
158
159.. type:: struct lsquic_logger_if
160
161    .. member:: int     (*log_buf)(void *logger_ctx, const char *buf, size_t len)
162
163.. function:: void lsquic_logger_init (const struct lsquic_logger_if *logger_if, void *logger_ctx, enum lsquic_logger_timestamp_style)
164
165    Call this if you want to do something with LSQUIC log messages, as they are thrown out by default.
166
167.. function:: int lsquic_set_log_level (const char *log_level)
168
169    Set log level for all LSQUIC modules.
170
171    :param log_level: Acceptable values are debug, info, notice, warning, error, alert, emerg, crit (case-insensitive).
172    :return: 0 on success or -1 on failure (invalid log level).
173
174.. function:: int lsquic_logger_lopt (const char *log_specs)
175
176    Set log level for a particular module or several modules.
177
178    :param log_specs:
179
180        One or more "module=level" specifications serapated by comma.
181        For example, "event=debug,engine=info".  See `List of Log Modules`_
182
183Engine Instantiation and Destruction
184------------------------------------
185
186To use the library, an instance of the ``struct lsquic_engine`` needs to be
187created:
188
189.. function:: lsquic_engine_t *lsquic_engine_new (unsigned flags, const struct lsquic_engine_api *api)
190
191    Create a new engine.
192
193    :param flags: This is is a bitmask of :macro:`LSENG_SERVER` and
194                :macro:`LSENG_HTTP`.
195    :param api: Pointer to an initialized :type:`lsquic_engine_api`.
196
197    The engine can be instantiated either in server mode (when ``LSENG_SERVER``
198    is set) or client mode.  If you need both server and client in your program,
199    create two engines (or as many as you'd like).
200
201    Specifying ``LSENG_HTTP`` flag enables the HTTP functionality: HTTP/2-like
202    for Google QUIC connections and HTTP/3 functionality for IETF QUIC
203    connections.
204
205.. macro:: LSENG_SERVER
206
207    One of possible bitmask values passed as first argument to
208    :type:`lsquic_engine_new`.  When set, the engine instance
209    will be in the server mode.
210
211.. macro:: LSENG_HTTP
212
213    One of possible bitmask values passed as first argument to
214    :type:`lsquic_engine_new`.  When set, the engine instance
215    will enable HTTP functionality.
216
217.. function:: void lsquic_engine_cooldown (lsquic_engine_t *engine)
218
219    This function closes all mini connections and marks all full connections
220    as going away.  In server mode, this also causes the engine to stop
221    creating new connections.
222
223.. function:: void lsquic_engine_destroy (lsquic_engine_t *engine)
224
225    Destroy engine and all its resources.
226
227Engine Callbacks
228----------------
229
230``struct lsquic_engine_api`` contains a few mandatory members and several
231optional members.
232
233.. type:: struct lsquic_engine_api
234
235    .. member:: const struct lsquic_stream_if       *ea_stream_if
236    .. member:: void                                *ea_stream_if_ctx
237
238        ``ea_stream_if`` is mandatory.  This structure contains pointers
239        to callbacks that handle connections and stream events.
240
241    .. member:: lsquic_packets_out_f                 ea_packets_out
242    .. member:: void                                *ea_packets_out_ctx
243
244        ``ea_packets_out`` is used by the engine to send packets.
245
246    .. member:: const struct lsquic_engine_settings *ea_settings
247
248        If ``ea_settings`` is set to NULL, the engine uses default settings
249        (see :func:`lsquic_engine_init_settings()`)
250
251    .. member:: lsquic_lookup_cert_f                 ea_lookup_cert
252    .. member:: void                                *ea_cert_lu_ctx
253
254        Look up certificate.  Mandatory in server mode.
255
256    .. member:: struct ssl_ctx_st *                (*ea_get_ssl_ctx)(void *peer_ctx)
257
258        Get SSL_CTX associated with a peer context.  Mandatory in server
259        mode.  This is use for default values for SSL instantiation.
260
261    .. member:: const struct lsquic_hset_if         *ea_hsi_if
262    .. member:: void                                *ea_hsi_ctx
263
264        Optional header set interface.  If not specified, the incoming headers
265        are converted to HTTP/1.x format and are read from stream and have to
266        be parsed again.
267
268    .. member:: const struct lsquic_shared_hash_if  *ea_shi
269    .. member:: void                                *ea_shi_ctx
270
271        Shared hash interface can be used to share state between several
272        processes of a single QUIC server.
273
274    .. member:: const struct lsquic_packout_mem_if  *ea_pmi
275    .. member:: void                                *ea_pmi_ctx
276
277        Optional set of functions to manage memory allocation for outgoing
278        packets.
279
280    .. member:: lsquic_cids_update_f                 ea_new_scids
281    .. member:: lsquic_cids_update_f                 ea_live_scids
282    .. member:: lsquic_cids_update_f                 ea_old_scids
283    .. member:: void                                *ea_cids_update_ctx
284
285        In a multi-process setup, it may be useful to observe the CID
286        lifecycle.  This optional set of callbacks makes it possible.
287
288    .. member:: const char                          *ea_alpn
289
290        The optional ALPN string is used by the client if :macro:`LSENG_HTTP`
291        is not set.
292
293.. _apiref-engine-settings:
294
295Engine Settings
296---------------
297
298Engine behavior can be controlled by several settings specified in the
299settings structure:
300
301.. type:: struct lsquic_engine_settings
302
303    .. member:: unsigned        es_versions
304
305        This is a bit mask wherein each bit corresponds to a value in
306        :type:`lsquic_version`.  Client starts negotiating with the highest
307        version and goes down.  Server supports either of the versions
308        specified here.  This setting applies to both Google and IETF QUIC.
309
310        The default value is :macro:`LSQUIC_DF_VERSIONS`.
311
312    .. member:: unsigned        es_cfcw
313
314       Initial default connection flow control window.
315
316       In server mode, per-connection values may be set lower than
317       this if resources are scarce.
318
319       Do not set es_cfcw and es_sfcw lower than :macro:`LSQUIC_MIN_FCW`.
320
321    .. member:: unsigned        es_sfcw
322
323       Initial default stream flow control window.
324
325       In server mode, per-connection values may be set lower than
326       this if resources are scarce.
327
328       Do not set es_cfcw and es_sfcw lower than :macro:`LSQUIC_MIN_FCW`.
329
330    .. member:: unsigned        es_max_cfcw
331
332       This value is used to specify maximum allowed value CFCW is allowed
333       to reach due to window auto-tuning.  By default, this value is zero,
334       which means that CFCW is not allowed to increase from its initial
335       value.
336
337       This setting is applicable to both gQUIC and IETF QUIC.
338
339       See :member:`lsquic_engine_settings.es_cfcw`,
340       :member:`lsquic_engine_settings.es_init_max_data`.
341
342    .. member:: unsigned        es_max_sfcw
343
344       This value is used to specify the maximum value stream flow control
345       window is allowed to reach due to auto-tuning.  By default, this
346       value is zero, meaning that auto-tuning is turned off.
347
348       This setting is applicable to both gQUIC and IETF QUIC.
349
350       See :member:`lsquic_engine_settings.es_sfcw`,
351       :member:`lsquic_engine_settings.es_init_max_stream_data_bidi_local`,
352       :member:`lsquic_engine_settings.es_init_max_stream_data_bidi_remote`.
353
354    .. member:: unsigned        es_max_streams_in
355
356        Maximum incoming streams, a.k.a. MIDS.
357
358        Google QUIC only.
359
360    .. member:: unsigned long   es_handshake_to
361
362       Handshake timeout in microseconds.
363
364       For client, this can be set to an arbitrary value (zero turns the
365       timeout off).
366
367       For server, this value is limited to about 16 seconds.  Do not set
368       it to zero.
369
370       Defaults to :macro:`LSQUIC_DF_HANDSHAKE_TO`.
371
372    .. member:: unsigned long   es_idle_conn_to
373
374        Idle connection timeout, a.k.a ICSL, in microseconds; GQUIC only.
375
376        Defaults to :macro:`LSQUIC_DF_IDLE_CONN_TO`
377
378    .. member:: int             es_silent_close
379
380        SCLS (silent close)
381
382    .. member:: unsigned        es_max_header_list_size
383
384       This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE
385       (:rfc:`7540#section-6.5.2`).  0 means no limit.  Defaults
386       to :func:`LSQUIC_DF_MAX_HEADER_LIST_SIZE`.
387
388    .. member:: const char     *es_ua
389
390        UAID -- User-Agent ID.  Defaults to :macro:`LSQUIC_DF_UA`.
391
392        Google QUIC only.
393
394
395       More parameters for server
396
397    .. member:: unsigned        es_max_inchoate
398
399        Maximum number of incoming connections in inchoate state.  (In
400        other words, maximum number of mini connections.)
401
402        This is only applicable in server mode.
403
404        Defaults to :macro:`LSQUIC_DF_MAX_INCHOATE`.
405
406    .. member:: int             es_support_push
407
408       Setting this value to 0 means that
409
410       For client:
411
412       1. we send a SETTINGS frame to indicate that we do not support server
413          push; and
414       2. all incoming pushed streams get reset immediately.
415
416       (For maximum effect, set es_max_streams_in to 0.)
417
418       For server:
419
420       1. :func:`lsquic_conn_push_stream()` will return -1.
421
422    .. member:: int             es_support_tcid0
423
424       If set to true value, the server will not include connection ID in
425       outgoing packets if client's CHLO specifies TCID=0.
426
427       For client, this means including TCID=0 into CHLO message.  Note that
428       in this case, the engine tracks connections by the
429       (source-addr, dest-addr) tuple, thereby making it necessary to create
430       a socket for each connection.
431
432       This option has no effect in Q046 and Q050, as the server never includes
433       CIDs in the short packets.
434
435       This setting is applicable to gQUIC only.
436
437       The default is :func:`LSQUIC_DF_SUPPORT_TCID0`.
438
439    .. member:: int             es_support_nstp
440
441       Q037 and higher support "No STOP_WAITING frame" mode.  When set, the
442       client will send NSTP option in its Client Hello message and will not
443       sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames,
444       if any.  Note that if the version negotiation happens to downgrade the
445       client below Q037, this mode will *not* be used.
446
447       This option does not affect the server, as it must support NSTP mode
448       if it was specified by the client.
449
450        Defaults to :macro:`LSQUIC_DF_SUPPORT_NSTP`.
451
452    .. member:: int             es_honor_prst
453
454       If set to true value, the library will drop connections when it
455       receives corresponding Public Reset packet.  The default is to
456       ignore these packets.
457
458       The default is :macro:`LSQUIC_DF_HONOR_PRST`.
459
460    .. member:: int             es_send_prst
461
462       If set to true value, the library will send Public Reset packets
463       in response to incoming packets with unknown Connection IDs.
464
465       The default is :macro:`LSQUIC_DF_SEND_PRST`.
466
467    .. member:: unsigned        es_progress_check
468
469       A non-zero value enables internal checks that identify suspected
470       infinite loops in user `on_read` and `on_write` callbacks
471       and break them.  An infinite loop may occur if user code keeps
472       on performing the same operation without checking status, e.g.
473       reading from a closed stream etc.
474
475       The value of this parameter is as follows: should a callback return
476       this number of times in a row without making progress (that is,
477       reading, writing, or changing stream state), loop break will occur.
478
479       The defaut value is :macro:`LSQUIC_DF_PROGRESS_CHECK`.
480
481    .. member:: int             es_rw_once
482
483       A non-zero value make stream dispatch its read-write events once
484       per call.
485
486       When zero, read and write events are dispatched until the stream
487       is no longer readable or writeable, respectively, or until the
488       user signals unwillingness to read or write using
489       :func:`lsquic_stream_wantread()` or :func:`lsquic_stream_wantwrite()`
490       or shuts down the stream.
491
492       The default value is :macro:`LSQUIC_DF_RW_ONCE`.
493
494    .. member:: unsigned        es_proc_time_thresh
495
496       If set, this value specifies the number of microseconds that
497       :func:`lsquic_engine_process_conns()` and
498       :func:`lsquic_engine_send_unsent_packets()` are allowed to spend
499       before returning.
500
501       This is not an exact science and the connections must make
502       progress, so the deadline is checked after all connections get
503       a chance to tick (in the case of :func:`lsquic_engine_process_conns())`
504       and at least one batch of packets is sent out.
505
506       When processing function runs out of its time slice, immediate
507       calls to :func:`lsquic_engine_has_unsent_packets()` return false.
508
509       The default value is :func:`LSQUIC_DF_PROC_TIME_THRESH`.
510
511    .. member:: int             es_pace_packets
512
513       If set to true, packet pacing is implemented per connection.
514
515       The default value is :func:`LSQUIC_DF_PACE_PACKETS`.
516
517    .. member:: unsigned        es_clock_granularity
518
519       Clock granularity information is used by the pacer.  The value
520       is in microseconds; default is :func:`LSQUIC_DF_CLOCK_GRANULARITY`.
521
522    .. member:: unsigned        es_init_max_data
523
524       Initial max data.
525
526       This is a transport parameter.
527
528       Depending on the engine mode, the default value is either
529       :macro:`LSQUIC_DF_INIT_MAX_DATA_CLIENT` or
530       :macro:`LSQUIC_DF_INIT_MAX_DATA_SERVER`.
531
532       IETF QUIC only.
533
534    .. member:: unsigned        es_init_max_stream_data_bidi_remote
535
536       Initial max stream data.
537
538       This is a transport parameter.
539
540       Depending on the engine mode, the default value is either
541       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT` or
542       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER`.
543
544       IETF QUIC only.
545
546    .. member:: unsigned        es_init_max_stream_data_bidi_local
547
548       Initial max stream data.
549
550       This is a transport parameter.
551
552       Depending on the engine mode, the default value is either
553       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT` or
554       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER`.
555
556       IETF QUIC only.
557
558    .. member:: unsigned        es_init_max_stream_data_uni
559
560       Initial max stream data for unidirectional streams initiated
561       by remote endpoint.
562
563       This is a transport parameter.
564
565       Depending on the engine mode, the default value is either
566       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT` or
567       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER`.
568
569       IETF QUIC only.
570
571    .. member:: unsigned        es_init_max_streams_bidi
572
573       Maximum initial number of bidirectional stream.
574
575       This is a transport parameter.
576
577       Default value is :macro:`LSQUIC_DF_INIT_MAX_STREAMS_BIDI`.
578
579       IETF QUIC only.
580
581    .. member:: unsigned        es_init_max_streams_uni
582
583       Maximum initial number of unidirectional stream.
584
585       This is a transport parameter.
586
587       Default value is :macro:`LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT` or
588       :macro:`LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER`.
589
590       IETF QUIC only.
591
592    .. member:: unsigned        es_idle_timeout
593
594       Idle connection timeout.
595
596       This is a transport parameter.
597
598       (Note: `es_idle_conn_to` is not reused because it is in microseconds,
599       which, I now realize, was not a good choice.  Since it will be
600       obsoleted some time after the switchover to IETF QUIC, we do not
601       have to keep on using strange units.)
602
603       Default value is :macro:`LSQUIC_DF_IDLE_TIMEOUT`.
604
605       Maximum value is 600 seconds.
606
607       IETF QUIC only.
608
609    .. member:: unsigned        es_ping_period
610
611       Ping period.  If set to non-zero value, the connection will generate and
612       send PING frames in the absence of other activity.
613
614       By default, the server does not send PINGs and the period is set to zero.
615       The client's defaut value is :macro:`LSQUIC_DF_PING_PERIOD`.
616
617       IETF QUIC only.
618
619    .. member:: unsigned        es_scid_len
620
621       Source Connection ID length.  Valid values are 0 through 20, inclusive.
622
623       Default value is :macro:`LSQUIC_DF_SCID_LEN`.
624
625       IETF QUIC only.
626
627    .. member:: unsigned        es_scid_iss_rate
628
629       Source Connection ID issuance rate.  This field is measured in CIDs
630       per minute.  Using value 0 indicates that there is no rate limit for
631       CID issuance.
632
633       Default value is :macro:`LSQUIC_DF_SCID_ISS_RATE`.
634
635       IETF QUIC only.
636
637    .. member:: unsigned        es_qpack_dec_max_size
638
639       Maximum size of the QPACK dynamic table that the QPACK decoder will
640       use.
641
642       The default is :macro:`LSQUIC_DF_QPACK_DEC_MAX_SIZE`.
643
644       IETF QUIC only.
645
646    .. member:: unsigned        es_qpack_dec_max_blocked
647
648       Maximum number of blocked streams that the QPACK decoder is willing
649       to tolerate.
650
651       The default is :macro:`LSQUIC_DF_QPACK_DEC_MAX_BLOCKED`.
652
653       IETF QUIC only.
654
655    .. member:: unsigned        es_qpack_enc_max_size
656
657       Maximum size of the dynamic table that the encoder is willing to use.
658       The actual size of the dynamic table will not exceed the minimum of
659       this value and the value advertized by peer.
660
661       The default is :macro:`LSQUIC_DF_QPACK_ENC_MAX_SIZE`.
662
663       IETF QUIC only.
664
665    .. member:: unsigned        es_qpack_enc_max_blocked
666
667       Maximum number of blocked streams that the QPACK encoder is willing
668       to risk.  The actual number of blocked streams will not exceed the
669       minimum of this value and the value advertized by peer.
670
671       The default is :macro:`LSQUIC_DF_QPACK_ENC_MAX_BLOCKED`.
672
673       IETF QUIC only.
674
675    .. member:: int             es_ecn
676
677       Enable ECN support.
678
679       The default is :macro:`LSQUIC_DF_ECN`
680
681       IETF QUIC only.
682
683    .. member:: int             es_allow_migration
684
685       Allow peer to migrate connection.
686
687       The default is :macro:`LSQUIC_DF_ALLOW_MIGRATION`
688
689       IETF QUIC only.
690
691    .. member:: unsigned        es_cc_algo
692
693       Congestion control algorithm to use.
694
695       - 0:  Use default (:macro:`LSQUIC_DF_CC_ALGO)`
696       - 1:  Cubic
697       - 2:  BBR
698
699       IETF QUIC only.
700
701    .. member:: int             es_ql_bits
702
703       Use QL loss bits.  Allowed values are:
704
705       - 0:  Do not use loss bits
706       - 1:  Allow loss bits
707       - 2:  Allow and send loss bits
708
709       Default value is :macro:`LSQUIC_DF_QL_BITS`
710
711    .. member:: int             es_spin
712
713       Enable spin bit.  Allowed values are 0 and 1.
714
715       Default value is :macro:`LSQUIC_DF_SPIN`
716
717    .. member:: int             es_delayed_acks
718
719       Enable delayed ACKs extension.  Allowed values are 0 and 1.
720
721       **Warning**: this is an experimental feature.  Using it will most likely
722       lead to degraded performance.
723
724       Default value is :macro:`LSQUIC_DF_DELAYED_ACKS`
725
726    .. member:: int             es_timestamps
727
728       Enable timestamps extension.  Allowed values are 0 and 1.
729
730       Default value is @ref LSQUIC_DF_TIMESTAMPS
731
732    .. member:: unsigned short  es_max_udp_payload_size_rx
733
734       Maximum packet size we are willing to receive.  This is sent to
735       peer in transport parameters: the library does not enforce this
736       limit for incoming packets.
737
738       If set to zero, limit is not set.
739
740       Default value is :macro:`LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX`
741
742    .. member:: unsigned        es_noprogress_timeout
743
744       No progress timeout.
745
746       If connection does not make progress for this number of seconds, the
747       connection is dropped.  Here, progress is defined as user streams
748       being written to or read from.
749
750       If this value is zero, this timeout is disabled.
751
752       Default value is :macro:`LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER` in server
753       mode and :macro:`LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT` in client mode.
754
755    .. member:: int             es_grease_quic_bit
756
757       Enable the "QUIC bit grease" extension.  When set to a true value,
758       lsquic will grease the QUIC bit on the outgoing QUIC packets if
759       the peer sent the "grease_quic_bit" transport parameter.
760
761       Default value is :macro:`LSQUIC_DF_GREASE_QUIC_BIT`
762
763To initialize the settings structure to library defaults, use the following
764convenience function:
765
766.. function:: lsquic_engine_init_settings (struct lsquic_engine_settings *, unsigned flags)
767
768    ``flags`` is a bitmask of ``LSENG_SERVER`` and ``LSENG_HTTP``
769
770After doing this, change just the settings you'd like.  To check whether
771the values are correct, another convenience function is provided:
772
773.. function:: lsquic_engine_check_settings (const struct lsquic_engine_settings *, unsigned flags, char *err_buf, size_t err_buf_sz)
774
775    Check settings for errors.  Return 0 if settings are OK, -1 otherwise.
776
777    If `err_buf` and `err_buf_sz` are set, an error string is written to the
778    buffers.
779
780The following macros in :file:`lsquic.h` specify default values:
781
782*Note that, despite our best efforts, documentation may accidentally get
783out of date.  Please check your :file:`lsquic.h` for actual values.*
784
785.. macro::      LSQUIC_MIN_FCW
786
787    Minimum flow control window is set to 16 KB for both client and server.
788    This means we can send up to this amount of data before handshake gets
789    completed.
790
791.. macro:: LSQUIC_DF_VERSIONS
792
793    By default, deprecated and experimental versions are not included.
794
795.. macro:: LSQUIC_DF_CFCW_SERVER
796.. macro:: LSQUIC_DF_CFCW_CLIENT
797.. macro:: LSQUIC_DF_SFCW_SERVER
798.. macro:: LSQUIC_DF_SFCW_CLIENT
799.. macro:: LSQUIC_DF_MAX_STREAMS_IN
800
801.. macro:: LSQUIC_DF_INIT_MAX_DATA_SERVER
802.. macro:: LSQUIC_DF_INIT_MAX_DATA_CLIENT
803.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER
804.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER
805.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT
806.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT
807.. macro:: LSQUIC_DF_INIT_MAX_STREAMS_BIDI
808.. macro:: LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT
809.. macro:: LSQUIC_DF_INIT_MAX_STREAMS_UNI_SERVER
810.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT
811.. macro:: LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER
812
813.. macro:: LSQUIC_DF_IDLE_TIMEOUT
814
815    Default idle connection timeout is 30 seconds.
816
817.. macro:: LSQUIC_DF_PING_PERIOD
818
819    Default ping period is 15 seconds.
820
821.. macro:: LSQUIC_DF_HANDSHAKE_TO
822
823    Default handshake timeout is 10,000,000 microseconds (10 seconds).
824
825.. macro:: LSQUIC_DF_IDLE_CONN_TO
826
827    Default idle connection timeout is 30,000,000 microseconds.
828
829.. macro:: LSQUIC_DF_SILENT_CLOSE
830
831    By default, connections are closed silenty when they time out (no
832    CONNECTION_CLOSE frame is sent).
833
834.. macro:: LSQUIC_DF_MAX_HEADER_LIST_SIZE
835
836    Default value of maximum header list size.  If set to non-zero value,
837    SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is
838    completed (assuming the peer supports this setting frame type).
839
840.. macro:: LSQUIC_DF_UA
841
842    Default value of UAID (user-agent ID).
843
844.. macro:: LSQUIC_DF_MAX_INCHOATE
845
846    Default is 1,000,000.
847
848.. macro:: LSQUIC_DF_SUPPORT_NSTP
849
850    NSTP is not used by default.
851
852.. macro:: LSQUIC_DF_SUPPORT_PUSH
853
854    Push promises are supported by default.
855
856.. macro:: LSQUIC_DF_SUPPORT_TCID0
857
858    Support for TCID=0 is enabled by default.
859
860.. macro:: LSQUIC_DF_HONOR_PRST
861
862    By default, LSQUIC ignores Public Reset packets.
863
864.. macro:: LSQUIC_DF_SEND_PRST
865
866    By default, LSQUIC will not send Public Reset packets in response to
867    packets that specify unknown connections.
868
869.. macro:: LSQUIC_DF_PROGRESS_CHECK
870
871    By default, infinite loop checks are turned on.
872
873.. macro:: LSQUIC_DF_RW_ONCE
874
875    By default, read/write events are dispatched in a loop.
876
877.. macro:: LSQUIC_DF_PROC_TIME_THRESH
878
879    By default, the threshold is not enabled.
880
881.. macro:: LSQUIC_DF_PACE_PACKETS
882
883    By default, packets are paced
884
885.. macro:: LSQUIC_DF_CLOCK_GRANULARITY
886
887    Default clock granularity is 1000 microseconds.
888
889.. macro:: LSQUIC_DF_SCID_LEN
890
891    The default value is 8 for simplicity and speed.
892
893.. macro:: LSQUIC_DF_SCID_ISS_RATE
894
895    The default value is 60 CIDs per minute.
896
897.. macro:: LSQUIC_DF_QPACK_DEC_MAX_BLOCKED
898
899    Default value is 100.
900
901.. macro:: LSQUIC_DF_QPACK_DEC_MAX_SIZE
902
903    Default value is 4,096 bytes.
904
905.. macro:: LSQUIC_DF_QPACK_ENC_MAX_BLOCKED
906
907    Default value is 100.
908
909.. macro:: LSQUIC_DF_QPACK_ENC_MAX_SIZE
910
911    Default value is 4,096 bytes.
912
913.. macro:: LSQUIC_DF_ECN
914
915    ECN is disabled by default.
916
917.. macro:: LSQUIC_DF_ALLOW_MIGRATION
918
919    Allow migration by default.
920
921.. macro:: LSQUIC_DF_QL_BITS
922
923    Use QL loss bits by default.
924
925.. macro:: LSQUIC_DF_SPIN
926
927    Turn spin bit on by default.
928
929.. macro:: LSQUIC_DF_CC_ALGO
930
931    Use Cubic by default.
932
933.. macro:: LSQUIC_DF_DELAYED_ACKS
934
935    Delayed ACKs are off by default.
936
937.. macro:: LSQUIC_DF_MAX_UDP_PAYLOAD_SIZE_RX
938
939    By default, incoming packet size is not limited.
940
941.. macro:: LSQUIC_DF_NOPROGRESS_TIMEOUT_SERVER
942
943    By default, drop no-progress connections after 60 seconds on the server.
944
945.. macro:: LSQUIC_DF_NOPROGRESS_TIMEOUT_CLIENT
946
947    By default, do not use no-progress timeout on the client.
948
949.. macro:: LSQUIC_DF_GREASE_QUIC_BIT
950
951    By default, greasing the QUIC bit is enabled (if peer sent
952    the "grease_quic_bit" transport parameter).
953
954Receiving Packets
955-----------------
956
957Incoming packets are supplied to the engine using :func:`lsquic_engine_packet_in()`.
958It is up to the engine to decide what do to with the packet.  It can find an existing
959connection and dispatch the packet there, create a new connection (in server mode), or
960schedule a version negotiation or stateless reset packet.
961
962.. function:: int lsquic_engine_packet_in (lsquic_engine_t *engine, const unsigned char *data, size_t size, const struct sockaddr *local, const struct sockaddr *peer, void *peer_ctx, int ecn)
963
964    Pass incoming packet to the QUIC engine.  This function can be called
965    more than once in a row.  After you add one or more packets, call
966    :func:`lsquic_engine_process_conns()` to schedule outgoing packets, if any.
967
968    :param engine: Engine instance.
969    :param data: Pointer to UDP datagram payload.
970    :param size: Size of UDP datagram.
971    :param local: Local address.
972    :param peer: Peer address.
973    :param peer_ctx: Peer context.
974    :param ecn: ECN marking associated with this UDP datagram.
975
976    :return:
977
978        - ``0``: Packet was processed by a real connection.
979        - ``1``: Packet was handled successfully, but not by a connection.
980          This may happen with version negotiation and public reset
981          packets as well as some packets that may be ignored.
982        - ``-1``: Some error occurred.  Possible reasons are invalid packet
983          size or failure to allocate memory.
984
985.. function:: int lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff)
986
987    Returns true if there are connections to be processed, false otherwise.
988
989    :param engine:
990
991        Engine instance.
992
993    :param diff:
994
995        If the function returns a true value, the pointed to integer is set to the
996        difference between the earliest advisory tick time and now.
997        If the former is in the past, this difference is negative.
998
999    :return:
1000
1001        True if there are connections to be processed, false otherwise.
1002
1003Sending Packets
1004---------------
1005
1006User specifies a callback :type:`lsquic_packets_out_f` in :type:`lsquic_engine_api`
1007that the library uses to send packets.
1008
1009.. type:: struct lsquic_out_spec
1010
1011    This structure describes an outgoing packet.
1012
1013    .. member:: struct iovec          *iov
1014
1015        A vector with payload.
1016
1017    .. member:: size_t                 iovlen
1018
1019        Vector length.
1020
1021    .. member:: const struct sockaddr *local_sa
1022
1023        Local address.
1024
1025    .. member:: const struct sockaddr *dest_sa
1026
1027        Destination address.
1028
1029    .. member:: void                  *peer_ctx
1030
1031        Peer context associated with the local address.
1032
1033    .. member:: int                    ecn
1034
1035        ECN: Valid values are 0 - 3. See :rfc:`3168`.
1036
1037        ECN may be set by IETF QUIC connections if ``es_ecn`` is set.
1038
1039.. type:: typedef int (*lsquic_packets_out_f)(void *packets_out_ctx, const struct lsquic_out_spec  *out_spec, unsigned n_packets_out)
1040
1041    Returns number of packets successfully sent out or -1 on error.  -1 should
1042    only be returned if no packets were sent out.  If -1 is returned or if the
1043    return value is smaller than ``n_packets_out``, this indicates that sending
1044    of packets is not possible.
1045
1046    If not all packets could be sent out, then:
1047
1048        - errno is examined.  If it is not EAGAIN or EWOULDBLOCK, the connection
1049          whose packet caused the error is closed forthwith.
1050        - No packets are attempted to be sent out until :func:`lsquic_engine_send_unsent_packets()`
1051          is called.
1052
1053.. function:: void lsquic_engine_process_conns (lsquic_engine_t *engine)
1054
1055    Process tickable connections.  This function must be called often enough so
1056    that packets and connections do not expire.  The preferred method of doing
1057    so is by using :func:`lsquic_engine_earliest_adv_tick()`.
1058
1059.. function:: int lsquic_engine_has_unsent_packets (lsquic_engine_t *engine)
1060
1061    Returns true if engine has some unsent packets.  This happens if
1062    :member:`lsquic_engine_api.ea_packets_out` could not send everything out
1063    or if processing deadline was exceeded (see
1064    :member:`lsquic_engine_settings.es_proc_time_thresh`).
1065
1066.. function:: void lsquic_engine_send_unsent_packets (lsquic_engine_t *engine)
1067
1068    Send out as many unsent packets as possibe: until we are out of unsent
1069    packets or until ``ea_packets_out()`` fails.
1070
1071    If ``ea_packets_out()`` cannot send all packets, this function must be
1072    called to signify that sending of packets is possible again.
1073
1074Stream Callback Interface
1075-------------------------
1076
1077The stream callback interface structure lists the callbacks used by
1078the engine to communicate with the user code:
1079
1080.. type:: struct lsquic_stream_if
1081
1082    .. member:: lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx, lsquic_conn_t *)
1083
1084        Called when a new connection has been created.  In server mode,
1085        this means that the handshake has been successful.  In client mode,
1086        on the other hand, this callback is called as soon as connection
1087        object is created inside the engine, but before the handshake is
1088        done.
1089
1090        The return value is the connection context associated with this
1091        connection.  Use :func:`lsquic_conn_get_ctx()` to get back this
1092        context.  It is OK for this function to return NULL.
1093
1094        This callback is mandatory.
1095
1096    .. member:: void (*on_conn_closed)(lsquic_conn_t *)
1097
1098        Connection is closed.
1099
1100        This callback is mandatory.
1101
1102    .. member:: lsquic_stream_ctx_t * (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *)
1103
1104        If you need to initiate a connection, call lsquic_conn_make_stream().
1105        This will cause `on_new_stream` callback to be called when appropriate
1106        (this operation is delayed when maximum number of outgoing streams is
1107        reached).
1108
1109        If connection is going away, this callback may be called with the
1110        second parameter set to NULL.
1111
1112        The return value is the stream context associated with the stream.
1113        A pointer to it is passed to `on_read()`, `on_write()`, and `on_close()`
1114        callbacks.  It is OK for this function to return NULL.
1115
1116        This callback is mandatory.
1117
1118    .. member:: void (*on_read)     (lsquic_stream_t *s, lsquic_stream_ctx_t *h)
1119
1120        Stream is readable: either there are bytes to be read or an error
1121        is ready to be collected.
1122
1123        This callback is mandatory.
1124
1125    .. member:: void (*on_write)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h)
1126
1127        Stream is writeable.
1128
1129        This callback is mandatory.
1130
1131    .. member:: void (*on_close)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h)
1132
1133        After this callback returns, the stream is no longer accessible.  This is
1134        a good time to clean up the stream context.
1135
1136        This callback is mandatory.
1137
1138    .. member:: void (*on_hsk_done)(lsquic_conn_t *c, enum lsquic_hsk_status s)
1139
1140        When handshake is completed, this callback is called.
1141
1142        This callback is optional.
1143
1144    .. member:: void (*on_goaway_received)(lsquic_conn_t *)
1145
1146        This is called when our side received GOAWAY frame.  After this,
1147        new streams should not be created.
1148
1149        This callback is optional.
1150
1151    .. member:: void (*on_new_token)(lsquic_conn_t *c, const unsigned char *token, size_t token_size)
1152
1153        When client receives a token in NEW_TOKEN frame, this callback is called.
1154
1155        This callback is optional.
1156
1157    .. member:: void (*on_sess_resume_info)(lsquic_conn_t *c, const unsigned char *, size_t)
1158
1159        This callback lets client record information needed to
1160        perform session resumption next time around.
1161
1162        This callback is optional.
1163
1164Creating Connections
1165--------------------
1166
1167In server mode, the connections are created by the library based on incoming
1168packets.  After handshake is completed, the library calls :member:`lsquic_stream_if.on_new_conn`
1169callback.
1170
1171In client mode, a new connection is created by
1172
1173.. function:: lsquic_conn_t * lsquic_engine_connect (lsquic_engine_t *engine, enum lsquic_version version, const struct sockaddr *local_sa, const struct sockaddr *peer_sa, void *peer_ctx, lsquic_conn_ctx_t *conn_ctx, const char *sni, unsigned short max_udp_payload_size, const unsigned char *sess_resume, size_t sess_resume_len, const unsigned char *token, size_t token_sz)
1174
1175    :param engine: Engine to use.
1176
1177    :param version:
1178
1179        To let the engine specify QUIC version, use N_LSQVER.  If session resumption
1180        information is supplied, version is picked from there instead.
1181
1182    :param local_sa:
1183
1184        Local address.
1185
1186    :param peer_sa:
1187
1188        Address of the server.
1189
1190    :param peer_ctx:
1191
1192        Context associated with the peer.  This is what gets passed to TODO.
1193
1194    :param conn_ctx:
1195
1196        Connection context can be set early using this parameter.  Useful if
1197        you need the connection context to be available in `on_conn_new()`.
1198        Note that that callback's return value replaces the connection
1199        context set here.
1200
1201    :param sni:
1202
1203        The SNI is required for Google QUIC connections; it is optional for
1204        IETF QUIC and may be set to NULL.
1205
1206    :param max_udp_payload_size:
1207
1208        Maximum packet size.  If set to zero, it is inferred based on `peer_sa`
1209        and `version`.
1210
1211    :param sess_resume:
1212
1213        Pointer to previously saved session resumption data needed for
1214        TLS resumption.  May be NULL.
1215
1216    :param sess_resume_len:
1217
1218        Size of session resumption data.
1219
1220    :param token:
1221
1222        Pointer to previously received token to include in the Initial
1223        packet.  Tokens are used by IETF QUIC to pre-validate client
1224        connections, potentially avoiding a retry.
1225
1226        See :member:`lsquic_stream_if.on_new_token` callback.
1227
1228        May be NULL.
1229
1230    :param token_sz:
1231
1232        Size of data pointed to by ``token``.
1233
1234Closing Connections
1235-------------------
1236
1237.. function:: void lsquic_conn_going_away (lsquic_conn_t *conn)
1238
1239    Mark connection as going away: send GOAWAY frame and do not accept
1240    any more incoming streams, nor generate streams of our own.
1241
1242    Only applicable to HTTP/3 and GQUIC connections.  Otherwise a no-op.
1243
1244.. function:: void lsquic_conn_close (lsquic_conn_t *conn)
1245
1246    This closes the connection.  :member:`lsquic_stream_if.on_conn_closed`
1247    and :member:`lsquic_stream_if.on_close` callbacks will be called.
1248
1249Creating Streams
1250----------------
1251
1252Similar to connections, streams are created by the library in server mode; they
1253correspond to requests.  In client mode, a new stream is created by
1254
1255.. function:: void lsquic_conn_make_stream (lsquic_conn_t *)
1256
1257    Create a new request stream.  This causes :member:`on_new_stream()` callback
1258    to be called.  If creating more requests is not permitted at the moment
1259    (due to number of concurrent streams limit), stream creation is registered
1260    as "pending" and the stream is created later when number of streams dips
1261    under the limit again.  Any number of pending streams can be created.
1262    Use :func:`lsquic_conn_n_pending_streams()` and
1263    :func:`lsquic_conn_cancel_pending_streams()` to manage pending streams.
1264
1265    If connection is going away, :func:`on_new_stream()` is called with the
1266    stream parameter set to NULL.
1267
1268Stream Events
1269-------------
1270
1271To register or unregister an interest in a read or write event, use the
1272following functions:
1273
1274.. function:: int lsquic_stream_wantread (lsquic_stream_t *stream, int want)
1275
1276    :param stream: Stream to read from.
1277    :param want: Boolean value indicating whether the caller wants to read
1278                 from stream.
1279    :return: Previous value of ``want`` or ``-1`` if the stream has already
1280             been closed for reading.
1281
1282    A stream becomes readable if there is was an error: for example, the
1283    peer may have reset the stream.  In this case, reading from the stream
1284    will return an error.
1285
1286.. function:: int lsquic_stream_wantwrite (lsquic_stream_t *stream, int want)
1287
1288    :param stream: Stream to write to.
1289    :param want: Boolean value indicating whether the caller wants to write
1290                 to stream.
1291    :return: Previous value of ``want`` or ``-1`` if the stream has already
1292             been closed for writing.
1293
1294Reading From Streams
1295--------------------
1296
1297.. function:: ssize_t lsquic_stream_read (lsquic_stream_t *stream, unsigned char *buf, size_t sz)
1298
1299    :param stream: Stream to read from.
1300    :param buf: Buffer to copy data to.
1301    :param sz: Size of the buffer.
1302    :return: Number of bytes read, zero if EOS has been reached, or -1 on error.
1303
1304    Read up to ``sz`` bytes from ``stream`` into buffer ``buf``.
1305
1306    ``-1`` is returned on error, in which case ``errno`` is set:
1307
1308    - ``EBADF``: The stream is closed.
1309    - ``ECONNRESET``: The stream has been reset.
1310    - ``EWOULDBLOCK``: There is no data to be read.
1311
1312.. function:: ssize_t lsquic_stream_readv (lsquic_stream_t *stream, const struct iovec *vec, int iovcnt)
1313
1314    :param stream: Stream to read from.
1315    :param vec: Array of ``iovec`` structures.
1316    :param iovcnt: Number of elements in ``vec``.
1317    :return: Number of bytes read, zero if EOS has been reached, or -1 on error.
1318
1319    Similar to :func:`lsquic_stream_read()`, but reads data into a vector.
1320
1321.. function:: ssize_t lsquic_stream_readf (lsquic_stream_t *stream, size_t (*readf)(void *ctx, const unsigned char *buf, size_t len, int fin), void *ctx)
1322
1323    :param stream: Stream to read from.
1324
1325    :param readf:
1326
1327        The callback takes four parameters:
1328
1329        - Pointer to user-supplied context;
1330        - Pointer to the data;
1331        - Data size (can be zero); and
1332        - Indicator whether the FIN follows the data.
1333
1334        The callback returns number of bytes processed.  If this number is zero
1335        or is smaller than ``len``, reading from stream stops.
1336
1337    :param ctx: Context pointer passed to ``readf``.
1338
1339    This function allows user-supplied callback to read the stream contents.
1340    It is meant to be used for zero-copy stream processing.
1341
1342    Return value and errors are same as in :func:`lsquic_stream_read()`.
1343
1344Writing To Streams
1345------------------
1346
1347.. function:: ssize_t lsquic_stream_write (lsquic_stream_t *stream, const void *buf, size_t len)
1348
1349    :param stream: Stream to write to.
1350    :param buf: Buffer to copy data from.
1351    :param len: Number of bytes to copy.
1352    :return: Number of bytes written -- which may be smaller than ``len`` -- or a negative
1353             value when an error occurs.
1354
1355    Write ``len`` bytes to the stream.  Returns number of bytes written, which
1356    may be smaller that ``len``.
1357
1358    A negative return value indicates a serious error (the library is likely
1359    to have aborted the connection because of it).
1360
1361.. function:: ssize_t lsquic_stream_writev (lsquic_stream_t *s, const struct iovec *vec, int count)
1362
1363    Like :func:`lsquic_stream_write()`, but read data from a vector.
1364
1365.. type:: struct lsquic_reader
1366
1367    Used as argument to :func:`lsquic_stream_writef()`.
1368
1369    .. member:: size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count)
1370
1371        :param lsqr_ctx: Pointer to user-specified context.
1372        :param buf: Memory location to write to.
1373        :param count: Size of available memory pointed to by ``buf``.
1374        :return:
1375
1376            Number of bytes written.  This is not a ``ssize_t`` because
1377            the read function is not supposed to return an error.  If an error
1378            occurs in the read function (for example, when reading from a file
1379            fails), it is supposed to deal with the error itself.
1380
1381    .. member:: size_t (*lsqr_size) (void *lsqr_ctx)
1382
1383        Return number of bytes remaining in the reader.
1384
1385    .. member:: void    *lsqr_ctx
1386
1387        Context pointer passed both to ``lsqr_read()`` and to ``lsqr_size()``.
1388
1389.. function:: ssize_t lsquic_stream_writef (lsquic_stream_t *stream, struct lsquic_reader *reader)
1390
1391    :param stream: Stream to write to.
1392    :param reader: Reader to read from.
1393    :return: Number of bytes written or -1 on error.
1394
1395    Write to stream using :type:`lsquic_reader`.  This is the most generic of
1396    the write functions -- :func:`lsquic_stream_write()` and
1397    :func:`lsquic_stream_writev()` utilize the same mechanism.
1398
1399.. function:: int lsquic_stream_flush (lsquic_stream_t *stream)
1400
1401    :param stream: Stream to flush.
1402    :return: 0 on success and -1 on failure.
1403
1404    Flush any buffered data.  This triggers packetizing even a single byte
1405    into a separate frame.  Flushing a closed stream is an error.
1406
1407Closing Streams
1408---------------
1409
1410Streams can be closed for reading, writing, or both.
1411``on_close()`` callback is called at some point after a stream is closed
1412for both reading and writing,
1413
1414.. function:: int lsquic_stream_shutdown (lsquic_stream_t *stream, int how)
1415
1416    :param stream: Stream to shut down.
1417    :param how:
1418
1419        This parameter specifies what do to.  Allowed values are:
1420
1421        - 0: Stop reading.
1422        - 1: Stop writing.
1423        - 2: Stop both reading and writing.
1424
1425    :return: 0 on success or -1 on failure.
1426
1427.. function:: int lsquic_stream_close (lsquic_stream_t *stream)
1428
1429    :param stream: Stream to close.
1430    :return: 0 on success or -1 on failure.
1431
1432Sending HTTP Headers
1433--------------------
1434
1435.. type:: struct lsxpack_header
1436
1437This type is defined in _lsxpack_header.h_.  See that header file for
1438more information.
1439
1440    .. member:: char             *buf
1441
1442        the buffer for headers
1443
1444    .. member:: uint32_t          name_hash
1445
1446        hash value for name
1447
1448    .. member:: uint32_t          nameval_hash
1449
1450        hash value for name + value
1451
1452    .. member:: lsxpack_strlen_t  name_offset
1453
1454        the offset for name in the buffer
1455
1456    .. member:: lsxpack_strlen_t  name_len
1457
1458        the length of name
1459
1460    .. member:: lsxpack_strlen_t  val_offset
1461
1462        the offset for value in the buffer
1463
1464    .. member:: lsxpack_strlen_t  val_len
1465
1466        the length of value
1467
1468    .. member:: uint16_t          chain_next_idx
1469
1470        mainly for cookie value chain
1471
1472    .. member:: uint8_t           hpack_index
1473
1474        HPACK static table index
1475
1476    .. member:: uint8_t           qpack_index
1477
1478        QPACK static table index
1479
1480    .. member:: uint8_t           app_index
1481
1482        APP header index
1483
1484    .. member:: enum lsxpack_flag flags:8
1485
1486        combination of lsxpack_flag
1487
1488    .. member:: uint8_t           indexed_type
1489
1490        control to disable index or not
1491
1492    .. member:: uint8_t           dec_overhead
1493
1494        num of extra bytes written to decoded buffer
1495
1496.. type:: lsquic_http_headers_t
1497
1498    .. member::     int   count
1499
1500        Number of headers in ``headers``.
1501
1502    .. member::     struct lsxpack_header   *headers
1503
1504        Pointer to an array of HTTP headers.
1505
1506    HTTP header list structure.  Contains a list of HTTP headers.
1507
1508.. function:: int lsquic_stream_send_headers (lsquic_stream_t *stream, const lsquic_http_headers_t *headers, int eos)
1509
1510    :param stream:
1511
1512        Stream to send headers on.
1513
1514    :param headers:
1515
1516        Headers to send.
1517
1518    :param eos:
1519
1520        Boolean value to indicate whether these headers constitute the whole
1521        HTTP message.
1522
1523    :return:
1524
1525        0 on success or -1 on error.
1526
1527Receiving HTTP Headers
1528----------------------
1529
1530If ``ea_hsi_if`` is not set in :type:`lsquic_engine_api`, the library will translate
1531HPACK- and QPACK-encoded headers into HTTP/1.x-like headers and prepend them to the
1532stream.  To the stream-reading function, it will look as if a standard HTTP/1.x
1533message.
1534
1535Alternatively, you can specify header-processing set of functions and manage header
1536fields yourself.  In that case, the header set must be "read" from the stream via
1537:func:`lsquic_stream_get_hset()`.
1538
1539.. type:: struct lsquic_hset_if
1540
1541    .. member::  void * (*hsi_create_header_set)(void *hsi_ctx, lsquic_stream_t *stream, int is_push_promise)
1542
1543        :param hsi_ctx: User context.  This is the pointer specifed in ``ea_hsi_ctx``.
1544        :param stream: Stream with which the header set is associated.  May be set
1545                       to NULL in server mode.
1546        :param is_push_promise: Boolean value indicating whether this header set is
1547                                for a push promise.
1548        :return: Pointer to user-defined header set object.
1549
1550        Create a new header set.  This object is (and must be) fetched from a
1551        stream by calling :func:`lsquic_stream_get_hset()` before the stream can
1552        be read.
1553
1554    .. member:: struct lsxpack_header * (*hsi_prepare_decode)(void *hdr_set, struct lsxpack_header *hdr, size_t space)
1555
1556        Return a header set prepared for decoding.  If ``hdr`` is NULL, this
1557        means return a new structure with at least ``space`` bytes available
1558        in the decoder buffer.  On success, a newly prepared header is
1559        returned.
1560
1561        If ``hdr`` is not NULL, it means there was not enough decoder buffer
1562        and it must be increased to at least ``space`` bytes.  ``buf``, ``val_len``,
1563        and ``name_offset`` member of the ``hdr`` structure may change.  On
1564        success, the return value is the same as ``hdr``.
1565
1566        If NULL is returned, the space cannot be allocated.
1567
1568    .. member:: int (*hsi_process_header)(void *hdr_set, struct lsxpack_header *hdr)
1569
1570        Process new header.
1571
1572        :param hdr_set:
1573
1574            Header set to add the new header field to.  This is the object
1575            returned by ``hsi_create_header_set()``.
1576
1577        :param hdr:
1578
1579            The header returned by @ref ``hsi_prepare_decode()``.
1580
1581        :return:
1582
1583            Return 0 on success, a positive value if a header error occured,
1584            or a negative value on any other error.  A positive return value
1585            will result in cancellation of associated stream. A negative return
1586            value will result in connection being aborted.
1587
1588    .. member:: void                (*hsi_discard_header_set)(void *hdr_set)
1589
1590        :param hdr_set: Header set to discard.
1591
1592        Discard header set.  This is called for unclaimed header sets and
1593        header sets that had an error.
1594
1595    .. member:: enum lsquic_hsi_flag hsi_flags
1596
1597        These flags specify properties of decoded headers passed to
1598        ``hsi_process_header()``.  This is only applicable to QPACK headers;
1599        HPACK library header properties are based on compilation, not
1600        run-time, options.
1601
1602.. function:: void * lsquic_stream_get_hset (lsquic_stream_t *stream)
1603
1604    :param stream: Stream to fetch header set from.
1605
1606    :return: Header set associated with the stream.
1607
1608    Get header set associated with the stream.  The header set is created by
1609    ``hsi_create_header_set()`` callback.  After this call, the ownership of
1610    the header set is transferred to the caller.
1611
1612    This call must precede calls to :func:`lsquic_stream_read()`,
1613    :func:`lsquic_stream_readv()`, and :func:`lsquic_stream_readf()`.
1614
1615    If the optional header set interface is not specified,
1616    this function returns NULL.
1617
1618Push Promises
1619-------------
1620
1621.. function:: int lsquic_conn_push_stream (lsquic_conn_t *conn, void *hdr_set, lsquic_stream_t *stream, const lsquic_http_headers_t *headers)
1622
1623    :return:
1624
1625        - 0: Stream pushed successfully.
1626        - 1: Stream push failed because it is disabled or because we hit
1627             stream limit or connection is going away.
1628        - -1: Stream push failed because of an internal error.
1629
1630    A server may push a stream.  This call creates a new stream in reference
1631    to stream ``stream``.  It will behave as if the client made a request: it will
1632    trigger ``on_new_stream()`` event and it can be used as a regular client-initiated stream.
1633
1634    ``hdr_set`` must be set.  It is passed as-is to :func:`lsquic_stream_get_hset()`.
1635
1636.. function:: int lsquic_conn_is_push_enabled (lsquic_conn_t *conn)
1637
1638    :return: Boolean value indicating whether push promises are enabled.
1639
1640    Only makes sense in server mode: the client cannot push a stream and this
1641    function always returns false in client mode.
1642
1643.. function:: int lsquic_stream_is_pushed (const lsquic_stream_t *stream)
1644
1645    :return: Boolean value indicating whether this is a pushed stream.
1646
1647.. function:: int lsquic_stream_refuse_push (lsquic_stream_t *stream)
1648
1649    Refuse pushed stream.  Call it from ``on_new_stream()``.  No need to
1650    call :func:`lsquic_stream_close()` after this.  ``on_close()`` will be called.
1651
1652.. function:: int lsquic_stream_push_info (const lsquic_stream_t *stream, lsquic_stream_id_t *ref_stream_id, void **hdr_set)
1653
1654    Get information associated with pushed stream
1655
1656    :param ref_stream_id: Stream ID in response to which push promise was sent.
1657    :param hdr_set: Header set. This object was passed to or generated by :func:`lsquic_conn_push_stream()`.
1658
1659    :return: 0 on success and -1 if this is not a pushed stream.
1660
1661Stream Priorities
1662-----------------
1663
1664.. function:: unsigned lsquic_stream_priority (const lsquic_stream_t *stream)
1665
1666    Return current priority of the stream.
1667
1668.. function:: int lsquic_stream_set_priority (lsquic_stream_t *stream, unsigned priority)
1669
1670    Set stream priority.  Valid priority values are 1 through 256, inclusive.
1671    Lower value means higher priority.
1672
1673    :return: 0 on success of -1 on failure (this happens if priority value is invalid).
1674
1675Miscellaneous Engine Functions
1676------------------------------
1677
1678.. function:: unsigned lsquic_engine_quic_versions (const lsquic_engine_t *engine)
1679
1680    Return the list of QUIC versions (as bitmask) this engine instance supports.
1681
1682.. function:: unsigned lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now)
1683
1684    Return number of connections whose advisory tick time is before current
1685    time plus ``from_now`` microseconds from now.  ``from_now`` can be negative.
1686
1687Miscellaneous Connection Functions
1688----------------------------------
1689
1690.. function:: enum lsquic_version lsquic_conn_quic_version (const lsquic_conn_t *conn)
1691
1692    Get QUIC version used by the connection.
1693
1694    If version has not yet been negotiated (can happen in client mode), ``-1`` is
1695    returned.
1696
1697.. function:: const lsquic_cid_t * lsquic_conn_id (const lsquic_conn_t *conn)
1698
1699    Get connection ID.
1700
1701.. function:: lsquic_engine_t * lsquic_conn_get_engine (lsquic_conn_t *conn)
1702
1703    Get pointer to the engine.
1704
1705.. function:: int lsquic_conn_get_sockaddr (lsquic_conn_t *conn, const struct sockaddr **local, const struct sockaddr **peer)
1706
1707    Get current (last used) addresses associated with the current path
1708    used by the connection.
1709
1710.. function:: struct stack_st_X509 * lsquic_conn_get_server_cert_chain (lsquic_conn_t *conn)
1711
1712    Get certificate chain returned by the server.  This can be used for
1713    server certificate verification.
1714
1715    The caller releases the stack using sk_X509_free().
1716
1717.. function:: lsquic_conn_ctx_t * lsquic_conn_get_ctx (const lsquic_conn_t *conn)
1718
1719    Get user-supplied context associated with the connection.
1720
1721.. function:: void lsquic_conn_set_ctx (lsquic_conn_t *conn, lsquic_conn_ctx_t *ctx)
1722
1723    Set user-supplied context associated with the connection.
1724
1725.. function:: void * lsquic_conn_get_peer_ctx (lsquic_conn_t *conn, const struct sockaddr *local_sa)
1726
1727    Get peer context associated with the connection and local address.
1728
1729.. function:: enum LSQUIC_CONN_STATUS lsquic_conn_status (lsquic_conn_t *conn, char *errbuf, size_t bufsz)
1730
1731    Get connection status.
1732
1733Miscellaneous Stream Functions
1734------------------------------
1735
1736.. function:: unsigned lsquic_conn_n_avail_streams (const lsquic_conn_t *conn)
1737
1738    Return max allowed outbound streams less current outbound streams.
1739
1740.. function:: unsigned lsquic_conn_n_pending_streams (const lsquic_conn_t *conn)
1741
1742    Return number of delayed streams currently pending.
1743
1744.. function:: unsigned lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n)
1745
1746    Cancel ``n`` pending streams.  Returns new number of pending streams.
1747
1748.. function:: lsquic_conn_t * lsquic_stream_conn (const lsquic_stream_t *stream)
1749
1750    Get a pointer to the connection object.  Use it with connection functions.
1751
1752.. function:: int lsquic_stream_is_rejected (const lsquic_stream_t *stream)
1753
1754    Returns true if this stream was rejected, false otherwise.  Use this as
1755    an aid to distinguish between errors.
1756
1757Other Functions
1758---------------
1759
1760.. function:: enum lsquic_version lsquic_str2ver (const char *str, size_t len)
1761
1762    Translate string QUIC version to LSQUIC QUIC version representation.
1763
1764.. function:: enum lsquic_version lsquic_alpn2ver (const char *alpn, size_t len)
1765
1766    Translate ALPN (e.g. "h3", "h3-23", "h3-Q046") to LSQUIC enum.
1767
1768Miscellaneous Types
1769-------------------
1770
1771.. type:: struct lsquic_shared_hash_if
1772
1773    The shared hash interface is used to share data between multiple LSQUIC instances.
1774
1775    .. member:: int (*shi_insert)(void *shi_ctx, void *key, unsigned key_sz, void *data, unsigned data_sz, time_t expiry)
1776
1777        :param shi_ctx:
1778
1779            Shared memory context pointer
1780
1781        :param key:
1782
1783            Key data.
1784
1785        :param key_sz:
1786
1787            Key size.
1788
1789        :param data:
1790
1791            Pointer to the data to store.
1792
1793        :param data_sz:
1794
1795            Data size.
1796
1797        :param expiry: When this item expires.  If you want your item to never expire, set this to zero.
1798
1799        :return: 0 on success, -1 on failure.
1800
1801        If inserted successfully, ``free()`` will be called on ``data`` and ``key``
1802        pointer when the element is deleted, whether due to expiration
1803        or explicit deletion.
1804
1805    .. member:: int (*shi_delete)(void *shi_ctx, const void *key, unsigned key_sz)
1806
1807        Delete item from shared hash
1808
1809        :return: 0 on success, -1 on failure.
1810
1811    .. member:: int (*shi_lookup)(void *shi_ctx, const void *key, unsigned key_sz, void **data, unsigned *data_sz)
1812
1813        :param shi_ctx:
1814
1815            Shared memory context pointer
1816
1817        :param key:
1818
1819            Key data.
1820
1821        :param key_sz:
1822
1823            Key size.
1824
1825        :param data:
1826
1827            Pointer to set to the result.
1828
1829        :param data_sz:
1830
1831            Pointer to the data size.
1832
1833        :return:
1834
1835            - ``1``: found.
1836            - ``0``: not found.
1837            - ``-1``:  error (perhaps not enough room in ``data`` if copy was attempted).
1838
1839         The implementation may choose to copy the object into buffer pointed
1840         to by ``data``, so you should have it ready.
1841
1842.. type:: struct lsquic_packout_mem_if
1843
1844    The packet out memory interface is used by LSQUIC to get buffers to
1845    which outgoing packets will be written before they are passed to
1846    :member:`lsquic_engine_api.ea_packets_out` callback.
1847
1848    If not specified, malloc() and free() are used.
1849
1850    .. member:: void *  (*pmi_allocate) (void *pmi_ctx, void *peer_ctx, unsigned short sz, char is_ipv6)
1851
1852        Allocate buffer for sending.
1853
1854    .. member:: void    (*pmi_release)  (void *pmi_ctx, void *peer_ctx, void *buf, char is_ipv6)
1855
1856        This function is used to release the allocated buffer after it is
1857        sent via ``ea_packets_out()``.
1858
1859    .. member:: void    (*pmi_return)  (void *pmi_ctx, void *peer_ctx, void *buf, char is_ipv6)
1860
1861        If allocated buffer is not going to be sent, return it to the
1862        caller using this function.
1863
1864.. type:: typedef void (*lsquic_cids_update_f)(void *ctx, void **peer_ctx, const lsquic_cid_t *cids, unsigned n_cids)
1865
1866    :param ctx:
1867
1868        Context associated with the CID lifecycle callbacks (ea_cids_update_ctx).
1869
1870    :param peer_ctx:
1871
1872        Array of peer context pointers.
1873
1874    :param cids:
1875
1876        Array of connection IDs.
1877
1878    :param n_cids:
1879
1880        Number of elements in the peer context pointer and connection ID arrays.
1881
1882.. type:: struct lsquic_keylog_if
1883
1884    SSL keylog interface.
1885
1886    .. member:: void *    (*kli_open) (void *keylog_ctx, lsquic_conn_t *conn)
1887
1888        Return keylog handle or NULL if no key logging is desired.
1889
1890    .. member:: void      (*kli_log_line) (void *handle, const char *line)
1891
1892        Log line.  The first argument is the pointer returned by ``kli_open()``.
1893
1894    .. member:: void      (*kli_close) (void *handle)
1895
1896        Close handle.
1897
1898.. type:: enum lsquic_logger_timestamp_style
1899
1900    Enumerate timestamp styles supported by LSQUIC logger mechanism.
1901
1902    .. member:: LLTS_NONE
1903
1904        No timestamp is generated.
1905
1906    .. member:: LLTS_HHMMSSMS
1907
1908        The timestamp consists of 24 hours, minutes, seconds, and milliseconds.  Example: 13:43:46.671
1909
1910    .. member:: LLTS_YYYYMMDD_HHMMSSMS
1911
1912        Like above, plus date, e.g: 2017-03-21 13:43:46.671
1913
1914    .. member:: LLTS_CHROMELIKE
1915
1916        This is Chrome-like timestamp used by proto-quic.  The timestamp
1917        includes month, date, hours, minutes, seconds, and microseconds.
1918
1919        Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
1920
1921        This is to facilitate reading two logs side-by-side.
1922
1923    .. member:: LLTS_HHMMSSUS
1924
1925        The timestamp consists of 24 hours, minutes, seconds, and microseconds.  Example: 13:43:46.671123
1926
1927    .. member:: LLTS_YYYYMMDD_HHMMSSUS
1928
1929        Date and time using microsecond resolution, e.g: 2017-03-21 13:43:46.671123
1930
1931.. type:: enum LSQUIC_CONN_STATUS
1932
1933    .. member:: LSCONN_ST_HSK_IN_PROGRESS
1934    .. member:: LSCONN_ST_CONNECTED
1935    .. member:: LSCONN_ST_HSK_FAILURE
1936    .. member:: LSCONN_ST_GOING_AWAY
1937    .. member:: LSCONN_ST_TIMED_OUT
1938    .. member:: LSCONN_ST_RESET
1939
1940        If es_honor_prst is not set, the connection will never get public
1941        reset packets and this flag will not be set.
1942
1943    .. member:: LSCONN_ST_USER_ABORTED
1944    .. member:: LSCONN_ST_ERROR
1945    .. member:: LSCONN_ST_CLOSED
1946    .. member:: LSCONN_ST_PEER_GOING_AWAY
1947
1948.. type:: enum lsquic_hsi_flag
1949
1950    These flags are ORed together to specify properties of
1951    :type:`lsxpack_header` passed to :member:`lsquic_hset_if.hsi_process_header`.
1952
1953    .. member:: LSQUIC_HSI_HTTP1X
1954
1955        Turn HTTP/1.x mode on or off.  In this mode, decoded name and value
1956        pair are separated by ``": "`` and ``"\r\n"`` is appended to the end
1957        of the string.  By default, this mode is off.
1958
1959    .. member:: LSQUIC_HSI_HASH_NAME
1960
1961        Include name hash into lsxpack_header.
1962
1963    .. member:: LSQUIC_HSI_HASH_NAMEVAL
1964
1965        Include nameval hash into lsxpack_header.
1966
1967Global Variables
1968----------------
1969
1970.. var:: const char *const lsquic_ver2str[N_LSQVER]
1971
1972    Convert LSQUIC version to human-readable string
1973
1974List of Log Modules
1975-------------------
1976
1977The following log modules are defined:
1978
1979- *alarmset*: Alarm processing.
1980- *bbr*: BBR congestion controller.
1981- *bw-sampler*: Bandwidth sampler (used by BBR).
1982- *cfcw*: Connection flow control window.
1983- *conn*: Connection.
1984- *crypto*: Low-level Google QUIC cryptography tracing.
1985- *cubic*: Cubic congestion controller.
1986- *di*: "Data In" handler (storing incoming data before it is read).
1987- *eng-hist*: Engine history.
1988- *engine*: Engine.
1989- *event*: Cross-module significant events.
1990- *frame-reader*: Reader of the HEADERS stream in Google QUIC.
1991- *frame-writer*: Writer of the HEADERS stream in Google QUIC.
1992- *handshake*: Handshake and packet encryption and decryption.
1993- *hcsi-reader*: Reader of the HTTP/3 control stream.
1994- *hcso-writer*: Writer of the HTTP/3 control stream.
1995- *headers*: HEADERS stream (Google QUIC).
1996- *hsk-adapter*: 
1997- *http1x*: Header conversion to HTTP/1.x.
1998- *logger*: Logger.
1999- *mini-conn*: Mini connection.
2000- *pacer*: Pacer.
2001- *parse*: Parsing.
2002- *prq*: PRQ stands for Packet Request Queue.  This logs scheduling
2003  and sending packets not associated with a connection: version
2004  negotiation and stateless resets.
2005- *purga*: CID purgatory.
2006- *qdec-hdl*: QPACK decoder stream handler.
2007- *qenc-hdl*: QPACK encoder stream handler.
2008- *qlog*: QLOG output.  At the moment, it is out of date.
2009- *qpack-dec*: QPACK decoder.
2010- *qpack-enc*: QPACK encoder.
2011- *rechist*: Receive history.
2012- *sendctl*: Send controller.
2013- *sfcw*: Stream flow control window.
2014- *spi*: Stream priority iterator.
2015- *stream*: Stream operation.
2016- *tokgen*: Token generation and validation.
2017- *trapa*: Transport parameter processing.
2018