lsquic.h revision 9fc12041
1/* Copyright (c) 2017 - 2020 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 10
28#define LSQUIC_PATCH_VERSION 0
29
30/**
31 * Engine flags:
32 */
33
34/** Server mode */
35#define LSENG_SERVER (1 << 0)
36
37/** Treat stream 3 as headers stream and, in general, behave like the
38 *  regular QUIC.
39 */
40#define LSENG_HTTP  (1 << 1)
41
42#define LSENG_HTTP_SERVER (LSENG_SERVER|LSENG_HTTP)
43
44/**
45 * This is a list of QUIC versions that we know of.  List of supported
46 * versions is in LSQUIC_SUPPORTED_VERSIONS.
47 */
48enum lsquic_version
49{
50
51    /** Q035.  This is the first version to be supported by LSQUIC. */
52    /* Support for this version has been removed.  The comment remains to
53     * document the changes.
54     */
55
56    /*
57     * Q037.  This version is like Q035, except the way packet hashes are
58     * generated is different for clients and servers.  In addition, new
59     * option NSTP (no STOP_WAITING frames) is rumored to be supported at
60     * some point in the future.
61     */
62    /* Support for this version has been removed.  The comment remains to
63     * document the changes.
64     */
65
66    /*
67     * Q038.  Based on Q037, supports PADDING frames in the middle of packet
68     * and NSTP (no STOP_WAITING frames) option.
69     */
70    /* Support for this version has been removed.  The comment remains to
71     * document the changes.
72     */
73
74    /**
75     * Q039.  Switch to big endian.  Do not ack acks.  Send connection level
76     * WINDOW_UPDATE frame every 20 sent packets which do not contain
77     * retransmittable frames.
78     */
79    /* Support for this version has been removed.  The comment remains to
80     * document the changes.
81     */
82
83    /*
84     * Q041.  RST_STREAM, ACK and STREAM frames match IETF format.
85     */
86    /* Support for this version has been removed.  The comment remains to
87     * document the changes.
88     */
89
90    /*
91     * Q042.  Receiving overlapping stream data is allowed.
92     */
93    /* Support for this version has been removed.  The comment remains to
94     * document the changes.
95     */
96
97    /**
98     * Q043.  Support for processing PRIORITY frames.  Since this library
99     * has supported PRIORITY frames from the beginning, this version is
100     * exactly the same as LSQVER_042.
101     */
102    LSQVER_043,
103
104    /**
105     * Q044.  IETF-like packet headers are used.  Frames are the same as
106     * in Q043.  Server never includes CIDs in short packets.
107     */
108    /* Support for this version has been removed.  The comment remains to
109     * document the changes.
110     */
111
112    /**
113     * Q046.  Use IETF Draft-17 compatible packet headers.
114     */
115    LSQVER_046,
116
117    /**
118     * Q050.  Variable-length QUIC server connection IDs.  Use CRYPTO frames
119     * for handshake.  IETF header format matching invariants-06.  Packet
120     * number encryption.  Initial packets are obfuscated.
121     */
122    LSQVER_050,
123
124#if LSQUIC_USE_Q098
125    /**
126     * Q098.  This is a made-up, experimental version used to test version
127     * negotiation.  The choice of 98 is similar to Google's choice of 99
128     * as the "IETF" version.
129     */
130    LSQVER_098,
131#define LSQUIC_EXPERIMENTAL_Q098 (1 << LSQVER_098)
132#else
133#define LSQUIC_EXPERIMENTAL_Q098 0
134#endif
135
136    /**
137     * IETF QUIC Draft-24
138     */
139    LSQVER_ID24,
140
141    /**
142     * IETF QUIC Draft-25
143     */
144    LSQVER_ID25,
145
146    /**
147     * Special version to trigger version negotiation.
148     * [draft-ietf-quic-transport-11], Section 3.
149     */
150    LSQVER_VERNEG,
151
152    N_LSQVER
153};
154
155/**
156 * We currently support versions 43, 46, 50, and Draft-24
157 * @see lsquic_version
158 */
159#define LSQUIC_SUPPORTED_VERSIONS ((1 << N_LSQVER) - 1)
160
161/**
162 * List of versions in which the server never includes CID in short packets.
163 */
164#define LSQUIC_FORCED_TCID0_VERSIONS ((1 << LSQVER_046)|(1 << LSQVER_050))
165
166#define LSQUIC_EXPERIMENTAL_VERSIONS ( \
167                            (1 << LSQVER_VERNEG) | LSQUIC_EXPERIMENTAL_Q098)
168
169#define LSQUIC_DEPRECATED_VERSIONS 0
170
171#define LSQUIC_GQUIC_HEADER_VERSIONS (1 << LSQVER_043)
172
173#define LSQUIC_IETF_VERSIONS ((1 << LSQVER_ID24) | (1 << LSQVER_ID25) \
174                                                    | (1 << LSQVER_VERNEG))
175
176#define LSQUIC_IETF_DRAFT_VERSIONS ((1 << LSQVER_ID24) | (1 << LSQVER_ID25) \
177                                                    | (1 << LSQVER_VERNEG))
178
179enum lsquic_hsk_status
180{
181    /**
182     * The handshake failed.
183     */
184    LSQ_HSK_FAIL,
185    /**
186     * The handshake succeeded without 0-RTT.
187     */
188    LSQ_HSK_OK,
189    /**
190     * The handshake succeeded with 0-RTT.
191     */
192    LSQ_HSK_0RTT_OK,
193    /**
194     * The handshake failed because of 0-RTT (early data rejected).  Retry
195     * the connection without 0-RTT.
196     */
197    LSQ_HSK_0RTT_FAIL,
198};
199
200/**
201 * @struct lsquic_stream_if
202 * @brief The definition of callback functions call by lsquic_stream to
203 * process events.
204 *
205 */
206struct lsquic_stream_if {
207
208    /**
209     * Use @ref lsquic_conn_get_ctx to get back the context.  It is
210     * OK for this function to return NULL.
211     */
212    lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx,
213                                                        lsquic_conn_t *c);
214
215    /** This is called when our side received GOAWAY frame.  After this,
216     *  new streams should not be created.  The callback is optional.
217     */
218    void (*on_goaway_received)(lsquic_conn_t *c);
219    void (*on_conn_closed)(lsquic_conn_t *c);
220
221    /** If you need to initiate a connection, call lsquic_conn_make_stream().
222     *  This will cause `on_new_stream' callback to be called when appropriate
223     *  (this operation is delayed when maximum number of outgoing streams is
224     *  reached).
225     *
226     *  After `on_close' is called, the stream is no longer accessible.
227     */
228    lsquic_stream_ctx_t *
229         (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *s);
230
231    void (*on_read)     (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
232    void (*on_write)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
233    void (*on_close)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
234    /* This callback in only called in client mode */
235    /**
236     * When handshake is completed, this callback is called.  `ok' is set
237     * to true if handshake was successful; otherwise, `ok' is set to
238     * false.
239     *
240     * This callback is optional.
241     */
242    void (*on_hsk_done)(lsquic_conn_t *c, enum lsquic_hsk_status s);
243    /**
244     * When server sends a token in NEW_TOKEN frame, this callback is called.
245     * The callback is optional.
246     */
247    void (*on_new_token)(lsquic_conn_t *c, const unsigned char *token,
248                                                        size_t token_size);
249    /**
250     * This optional callback lets client record information needed to
251     * perform a zero-RTT handshake next time around.
252     */
253    void (*on_zero_rtt_info)(lsquic_conn_t *c, const unsigned char *, size_t);
254};
255
256struct ssl_ctx_st;
257struct ssl_st;
258
259/**
260 * QUIC engine in server role needs access to certificates.  This is
261 * accomplished by providing a callback and a context to the engine
262 * constructor.
263 */
264
265typedef struct ssl_ctx_st * (*lsquic_lookup_cert_f)(
266    void *lsquic_cert_lookup_ctx, const struct sockaddr *local, const char *sni);
267
268/**
269 * Minimum flow control window is set to 16 KB for both client and server.
270 * This means we can send up to this amount of data before handshake gets
271 * completed.
272 */
273#define      LSQUIC_MIN_FCW             (16 * 1024)
274
275/* Each LSQUIC_DF_* value corresponds to es_* entry in
276 * lsquic_engine_settings below.
277 */
278
279/**
280 * By default, deprecated and experimental versions are not included.
281 */
282#define LSQUIC_DF_VERSIONS         (LSQUIC_SUPPORTED_VERSIONS & \
283                                            ~LSQUIC_DEPRECATED_VERSIONS & \
284                                            ~LSQUIC_EXPERIMENTAL_VERSIONS)
285
286#define LSQUIC_DF_CFCW_SERVER      (3 * 1024 * 1024 / 2)
287#define LSQUIC_DF_CFCW_CLIENT      (15 * 1024 * 1024)
288#define LSQUIC_DF_SFCW_SERVER      (1 * 1024 * 1024)
289#define LSQUIC_DF_SFCW_CLIENT      (6 * 1024 * 1024)
290#define LSQUIC_DF_MAX_STREAMS_IN   100
291
292/* IQUIC uses different names for these: */
293#define LSQUIC_DF_INIT_MAX_DATA_SERVER LSQUIC_DF_CFCW_SERVER
294#define LSQUIC_DF_INIT_MAX_DATA_CLIENT LSQUIC_DF_CFCW_CLIENT
295#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER LSQUIC_DF_SFCW_SERVER
296#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_SERVER 0
297#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_CLIENT 0
298#define LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_LOCAL_CLIENT LSQUIC_DF_SFCW_CLIENT
299#define LSQUIC_DF_INIT_MAX_STREAMS_BIDI LSQUIC_DF_MAX_STREAMS_IN
300#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT 100
301#define LSQUIC_DF_INIT_MAX_STREAMS_UNI_SERVER 3
302/* XXX What's a good value here? */
303#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT   (32 * 1024)
304#define LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER   (12 * 1024)
305
306/**
307 * Default idle connection time in seconds.
308 */
309#define LSQUIC_DF_IDLE_TIMEOUT 30
310
311/**
312 * Default ping period in seconds.
313 */
314#define LSQUIC_DF_PING_PERIOD 15
315
316/**
317 * Default handshake timeout in microseconds.
318 */
319#define LSQUIC_DF_HANDSHAKE_TO     (10 * 1000 * 1000)
320
321#define LSQUIC_DF_IDLE_CONN_TO     (LSQUIC_DF_IDLE_TIMEOUT * 1000 * 1000)
322#define LSQUIC_DF_SILENT_CLOSE     1
323
324/** Default value of maximum header list size.  If set to non-zero value,
325 *  SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is
326 *  completed (assuming the peer supports this setting frame type).
327 */
328#define LSQUIC_DF_MAX_HEADER_LIST_SIZE 0
329
330/** Default value of UAID (user-agent ID). */
331#define LSQUIC_DF_UA               "LSQUIC"
332
333#define LSQUIC_DF_STTL               86400
334#define LSQUIC_DF_MAX_INCHOATE     (1 * 1000 * 1000)
335/** Do not use NSTP by default */
336#define LSQUIC_DF_SUPPORT_NSTP     0
337/** TODO: IETF QUIC clients do not support push */
338#define LSQUIC_DF_SUPPORT_PUSH         1
339#define LSQUIC_DF_SUPPORT_TCID0    1
340/** By default, LSQUIC ignores Public Reset packets. */
341#define LSQUIC_DF_HONOR_PRST       0
342
343/**
344 * By default, LSQUIC will not send Public Reset packets in response to
345 * packets that specify unknown connections.
346 */
347#define LSQUIC_DF_SEND_PRST        0
348
349/** By default, infinite loop checks are turned on */
350#define LSQUIC_DF_PROGRESS_CHECK    1000
351
352/** By default, read/write events are dispatched in a loop */
353#define LSQUIC_DF_RW_ONCE           0
354
355/** By default, the threshold is not enabled */
356#define LSQUIC_DF_PROC_TIME_THRESH  0
357
358/** By default, packets are paced */
359#define LSQUIC_DF_PACE_PACKETS      1
360
361/** Default clock granularity is 1000 microseconds */
362#define LSQUIC_DF_CLOCK_GRANULARITY      1000
363
364/** The default value is 8 for simplicity */
365#define LSQUIC_DF_SCID_LEN 8
366
367/** The default value is 60 CIDs per minute */
368#define LSQUIC_DF_SCID_ISS_RATE   60
369
370#define LSQUIC_DF_QPACK_DEC_MAX_BLOCKED 100
371#define LSQUIC_DF_QPACK_DEC_MAX_SIZE 4096
372#define LSQUIC_DF_QPACK_ENC_MAX_BLOCKED 100
373#define LSQUIC_DF_QPACK_ENC_MAX_SIZE 4096
374
375/** ECN is disabled by default */
376#define LSQUIC_DF_ECN 0
377
378/** Allow migration by default */
379#define LSQUIC_DF_ALLOW_MIGRATION 1
380
381/** Use QL loss bits by default */
382#define LSQUIC_DF_QL_BITS 2
383
384/* 1: Cubic; 2: BBR */
385#define LSQUIC_DF_CC_ALGO 1
386
387struct lsquic_engine_settings {
388    /**
389     * This is a bit mask wherein each bit corresponds to a value in
390     * enum lsquic_version.  Client starts negotiating with the highest
391     * version and goes down.  Server supports either of the versions
392     * specified here.
393     *
394     * This setting applies to both Google and IETF QUIC.
395     *
396     * @see lsquic_version
397     */
398    unsigned        es_versions;
399
400    /**
401     * Initial default CFCW.
402     *
403     * In server mode, per-connection values may be set lower than
404     * this if resources are scarce.
405     *
406     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
407     *
408     * @see es_max_cfcw
409     */
410    unsigned        es_cfcw;
411
412    /**
413     * Initial default SFCW.
414     *
415     * In server mode, per-connection values may be set lower than
416     * this if resources are scarce.
417     *
418     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
419     *
420     * @see es_max_sfcw
421     */
422    unsigned        es_sfcw;
423
424    /**
425     * This value is used to specify maximum allowed value CFCW is allowed
426     * to reach due to window auto-tuning.  By default, this value is zero,
427     * which means that CFCW is not allowed to increase from its initial
428     * value.
429     *
430     * @see es_cfcw
431     */
432    unsigned        es_max_cfcw;
433
434    unsigned        es_max_sfcw;
435
436    /** MIDS */
437    unsigned        es_max_streams_in;
438
439    /**
440     * Handshake timeout in microseconds.
441     *
442     * For client, this can be set to an arbitrary value (zero turns the
443     * timeout off).
444     *
445     * For server, this value is limited to about 16 seconds.  Do not set
446     * it to zero.
447     */
448    unsigned long   es_handshake_to;
449
450    /** ICSL in microseconds; GQUIC only */
451    unsigned long   es_idle_conn_to;
452
453    /** SCLS (silent close) */
454    int             es_silent_close;
455
456    /**
457     * This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE
458     * (RFC 7540, Section 6.5.2).  0 means no limit.  Defaults
459     * to @ref LSQUIC_DF_MAX_HEADER_LIST_SIZE.
460     */
461    unsigned        es_max_header_list_size;
462
463    /** UAID -- User-Agent ID.  Defaults to @ref LSQUIC_DF_UA. */
464    const char     *es_ua;
465
466    /**
467     * More parameters for server
468     */
469    uint64_t        es_sttl; /* SCFG TTL in seconds */
470
471    uint32_t        es_pdmd; /* One fixed value X509 */
472    uint32_t        es_aead; /* One fixed value AESG */
473    uint32_t        es_kexs; /* One fixed value C255 */
474
475    /* Maximum number of incoming connections in inchoate state.  This is
476     * only applicable in server mode.
477     */
478    unsigned        es_max_inchoate;
479
480    /**
481     * Setting this value to 0 means that
482     *
483     * For client:
484     *  a) we send a SETTINGS frame to indicate that we do not support server
485     *     push; and
486     *  b) All incoming pushed streams get reset immediately.
487     * (For maximum effect, set es_max_streams_in to 0.)
488     *
489     * For server:
490     *  lsquic_conn_push_stream() will return -1.
491     */
492    int             es_support_push;
493
494    /**
495     * If set to true value, the server will not include connection ID in
496     * outgoing packets if client's CHLO specifies TCID=0.
497     *
498     * For client, this means including TCID=0 into CHLO message.  Note that
499     * in this case, the engine tracks connections by the
500     * (source-addr, dest-addr) tuple, thereby making it necessary to create
501     * a socket for each connection.
502     *
503     * This option has no effect in Q046, as the server never includes
504     * CIDs in the short packets.
505     *
506     * The default is @ref LSQUIC_DF_SUPPORT_TCID0.
507     */
508    int             es_support_tcid0;
509
510    /**
511     * Q037 and higher support "No STOP_WAITING frame" mode.  When set, the
512     * client will send NSTP option in its Client Hello message and will not
513     * sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames,
514     * if any.  Note that if the version negotiation happens to downgrade the
515     * client below Q037, this mode will *not* be used.
516     *
517     * This option does not affect the server, as it must support NSTP mode
518     * if it was specified by the client.
519     */
520    int             es_support_nstp;
521
522    /**
523     * If set to true value, the library will drop connections when it
524     * receives corresponding Public Reset packet.  The default is to
525     * ignore these packets.
526     */
527    int             es_honor_prst;
528
529    /**
530     * If set to true value, the library will send Public Reset packets
531     * in response to incoming packets with unknown Connection IDs.
532     * The default is @ref LSQUIC_DF_SEND_PRST.
533     */
534    int             es_send_prst;
535
536    /**
537     * A non-zero value enables internal checks that identify suspected
538     * infinite loops in user @ref on_read and @ref on_write callbacks
539     * and break them.  An infinite loop may occur if user code keeps
540     * on performing the same operation without checking status, e.g.
541     * reading from a closed stream etc.
542     *
543     * The value of this parameter is as follows: should a callback return
544     * this number of times in a row without making progress (that is,
545     * reading, writing, or changing stream state), loop break will occur.
546     *
547     * The defaut value is @ref LSQUIC_DF_PROGRESS_CHECK.
548     */
549    unsigned        es_progress_check;
550
551    /**
552     * A non-zero value make stream dispatch its read-write events once
553     * per call.
554     *
555     * When zero, read and write events are dispatched until the stream
556     * is no longer readable or writeable, respectively, or until the
557     * user signals unwillingness to read or write using
558     * @ref lsquic_stream_wantread() or @ref lsquic_stream_wantwrite()
559     * or shuts down the stream.
560     *
561     * The default value is @ref LSQUIC_DF_RW_ONCE.
562     */
563    int             es_rw_once;
564
565    /**
566     * If set, this value specifies that number of microseconds that
567     * @ref lsquic_engine_process_conns() and
568     * @ref lsquic_engine_send_unsent_packets() are allowed to spend
569     * before returning.
570     *
571     * This is not an exact science and the connections must make
572     * progress, so the deadline is checked after all connections get
573     * a chance to tick (in the case of @ref lsquic_engine_process_conns())
574     * and at least one batch of packets is sent out.
575     *
576     * When processing function runs out of its time slice, immediate
577     * calls to @ref lsquic_engine_has_unsent_packets() return false.
578     *
579     * The default value is @ref LSQUIC_DF_PROC_TIME_THRESH.
580     */
581    unsigned        es_proc_time_thresh;
582
583    /**
584     * If set to true, packet pacing is implemented per connection.
585     *
586     * The default value is @ref LSQUIC_DF_PACE_PACKETS.
587     */
588    int             es_pace_packets;
589
590    /**
591     * Clock granularity information is used by the pacer.  The value
592     * is in microseconds; default is @ref LSQUIC_DF_CLOCK_GRANULARITY.
593     */
594    unsigned        es_clock_granularity;
595
596    /* The following settings are specific to IETF QUIC. */
597    /* vvvvvvvvvvv */
598
599    /**
600     * Initial max data.
601     *
602     * This is a transport parameter.
603     *
604     * Depending on the engine mode, the default value is either
605     * @ref LSQUIC_DF_INIT_MAX_DATA_CLIENT or
606     * @ref LSQUIC_DF_INIT_MAX_DATA_SERVER.
607     */
608    unsigned        es_init_max_data;
609
610    /**
611     * Initial max stream data.
612     *
613     * This is a transport parameter.
614     *
615     * Depending on the engine mode, the default value is either
616     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_CLIENT or
617     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_BIDI_REMOTE_SERVER.
618     */
619    unsigned        es_init_max_stream_data_bidi_remote;
620    unsigned        es_init_max_stream_data_bidi_local;
621
622    /**
623     * Initial max stream data for unidirectional streams initiated
624     * by remote endpoint.
625     *
626     * This is a transport parameter.
627     *
628     * Depending on the engine mode, the default value is either
629     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_CLIENT or
630     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER.
631     */
632    unsigned        es_init_max_stream_data_uni;
633
634    /**
635     * Maximum initial number of bidirectional stream.
636     *
637     * This is a transport parameter.
638     *
639     * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_BIDI.
640     */
641    unsigned        es_init_max_streams_bidi;
642
643    /**
644     * Maximum initial number of unidirectional stream.
645     *
646     * This is a transport parameter.
647     *
648     * Default value is @ref LSQUIC_DF_INIT_MAX_STREAMS_UNI_CLIENT or
649     * @ref LSQUIC_DF_INIT_MAX_STREAM_DATA_UNI_SERVER.
650     */
651    unsigned        es_init_max_streams_uni;
652
653    /**
654     * Idle connection timeout.
655     *
656     * This is a transport parameter.
657     *
658     * (Note: es_idle_conn_to is not reused because it is in microseconds,
659     * which, I now realize, was not a good choice.  Since it will be
660     * obsoleted some time after the switchover to IETF QUIC, we do not
661     * have to keep on using strange units.)
662     *
663     * Default value is @ref LSQUIC_DF_IDLE_TIMEOUT.
664     *
665     * Maximum value is 600 seconds.
666     */
667    unsigned        es_idle_timeout;
668
669    /**
670     * Ping period.  If set to non-zero value, the connection will generate and
671     * send PING frames in the absence of other activity.
672     *
673     * By default, the server does not send PINGs and the period is set to zero.
674     * The client's defaut value is @ref LSQUIC_DF_PING_PERIOD.
675     */
676    unsigned        es_ping_period;
677
678    /**
679     * Source Connection ID length.  Only applicable to the IETF QUIC
680     * versions.  Valid values are 0 through 20, inclusive.
681     *
682     * Default value is @ref LSQUIC_DF_SCID_LEN.
683     */
684    unsigned        es_scid_len;
685
686    /**
687     * Source Connection ID issuance rate.  Only applicable to the IETF QUIC
688     * versions.  This field is measured in CIDs per minute.  Using value 0
689     * indicates that there is no rate limit for CID issuance.
690     *
691     * Default value is @ref LSQUIC_DF_SCID_ISS_RATE.
692     */
693    unsigned        es_scid_iss_rate;
694
695    /**
696     * Maximum size of the QPACK dynamic table that the QPACK decoder will
697     * use.
698     *
699     * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_SIZE.
700     */
701    unsigned        es_qpack_dec_max_size;
702
703    /**
704     * Maximum number of blocked streams that the QPACK decoder is willing
705     * to tolerate.
706     *
707     * The default is @ref LSQUIC_DF_QPACK_DEC_MAX_BLOCKED.
708     */
709    unsigned        es_qpack_dec_max_blocked;
710
711    /**
712     * Maximum size of the dynamic table that the encoder is willing to use.
713     * The actual size of the dynamic table will not exceed the minimum of
714     * this value and the value advertized by peer.
715     *
716     * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_SIZE.
717     */
718    unsigned        es_qpack_enc_max_size;
719
720    /**
721     * Maximum number of blocked streams that the QPACK encoder is willing
722     * to risk.  The actual number of blocked streams will not exceed the
723     * minimum of this value and the value advertized by peer.
724     *
725     * The default is @ref LSQUIC_DF_QPACK_ENC_MAX_BLOCKED.
726     */
727    unsigned        es_qpack_enc_max_blocked;
728
729    /**
730     * Enable ECN support.
731     *
732     * The default is @ref LSQUIC_DF_ECN
733     */
734    int             es_ecn;
735
736    /**
737     * Allow peer to migrate connection.
738     *
739     * The default is @ref LSQUIC_DF_ALLOW_MIGRATION
740     */
741    int             es_allow_migration;
742
743    /**
744     * Congestion control algorithm to use.
745     *
746     *  0:  Use default (@ref LSQUIC_DF_CC_ALGO)
747     *  1:  Cubic
748     *  2:  BBR
749     */
750    unsigned        es_cc_algo;
751
752    /**
753     * Use QL loss bits.  Allowed values are:
754     *  0:  Do not use loss bits
755     *  1:  Allow loss bits
756     *  2:  Allow and send loss bits
757     * -1:  Allow and send loss bits, sending old-style boolean loss_bits TP
758     *
759     * Default value is @ref LSQUIC_DF_QL_BITS
760     */
761    int             es_ql_bits;
762};
763
764/* Initialize `settings' to default values */
765void
766lsquic_engine_init_settings (struct lsquic_engine_settings *,
767                             unsigned lsquic_engine_flags);
768
769/**
770 * Check settings for errors.
771 *
772 * @param   settings    Settings struct.
773 *
774 * @param   flags       Engine flags.
775 *
776 * @param   err_buf     Optional pointer to buffer into which error string
777 *                      is written.
778
779 * @param   err_buf_sz  Size of err_buf.  No more than this number of bytes
780 *                      will be written to err_buf, including the NUL byte.
781 *
782 * @retval  0   Settings have no errors.
783 * @retval -1   There are errors in settings.
784 */
785int
786lsquic_engine_check_settings (const struct lsquic_engine_settings *settings,
787                              unsigned lsquic_engine_flags,
788                              char *err_buf, size_t err_buf_sz);
789
790struct lsquic_out_spec
791{
792    struct iovec          *iov;
793    size_t                 iovlen;
794    const struct sockaddr *local_sa;
795    const struct sockaddr *dest_sa;
796    void                  *peer_ctx;
797    int                    ecn; /* Valid values are 0 - 3.  See RFC 3168 */
798};
799
800/**
801 * Returns number of packets successfully sent out or -1 on error.  -1 should
802 * only be returned if no packets were sent out.  If -1 is returned or if the
803 * return value is smaller than `n_packets_out', this indicates that sending
804 * of packets is not possible.
805 *
806 * If not all packets could be sent out, errno is examined.  If it is not
807 * EAGAIN or EWOULDBLOCK, the connection whose packet cause the error is
808 * closed forthwith.
809 *
810 * No packets will be attempted to be sent out until
811 * @ref lsquic_engine_send_unsent_packets() is called.
812 */
813typedef int (*lsquic_packets_out_f)(
814    void                          *packets_out_ctx,
815    const struct lsquic_out_spec  *out_spec,
816    unsigned                       n_packets_out
817);
818
819/**
820 * The shared hash interface is used to share data between multiple LSQUIC
821 * instances.
822 */
823struct lsquic_shared_hash_if
824{
825    /**
826     * If you want your item to never expire, set `expiry' to zero.
827     * Returns 0 on success, -1 on failure.
828     *
829     * If inserted successfully, `free()' will be called on `data' and 'key'
830     * pointer when the element is deleted, whether due to expiration
831     * or explicit deletion.
832     */
833    int (*shi_insert)(void *shi_ctx, void *key, unsigned key_sz,
834                      void *data, unsigned data_sz, time_t expiry);
835    /**
836     * Returns 0 on success, -1 on failure.
837     */
838    int (*shi_delete)(void *shi_ctx, const void *key, unsigned key_sz);
839
840    /**
841     * `data' is pointed to the result and `data_sz' is set to the
842     * object size.  The implementation may choose to copy the object
843     * into buffer pointed to by `data', so you should have it ready.
844     *
845     * @retval  1   found.
846     * @retval  0   not found.
847     * @retval -1   error (perhaps not enough room in `data' if copy was
848     *                attempted).
849     */
850    int (*shi_lookup)(void *shi_ctx, const void *key, unsigned key_sz,
851                                     void **data, unsigned *data_sz);
852};
853
854/**
855 * The packet out memory interface is used by LSQUIC to get buffers to
856 * which outgoing packets will be written before they are passed to
857 * ea_packets_out callback.
858 *
859 * If not specified, malloc() and free() are used.
860 */
861struct lsquic_packout_mem_if
862{
863    /**
864     * Allocate buffer for sending.
865     */
866    void *  (*pmi_allocate) (void *pmi_ctx, void *conn_ctx, unsigned short sz,
867                                                                char is_ipv6);
868    /**
869     * This function is used to release the allocated buffer after it is
870     * sent via @ref ea_packets_out.
871     */
872    void    (*pmi_release)  (void *pmi_ctx, void *conn_ctx, void *buf,
873                                                                char is_ipv6);
874    /**
875     * If allocated buffer is not going to be sent, return it to the caller
876     * using this function.
877     */
878    void    (*pmi_return)  (void *pmi_ctx, void *conn_ctx, void *buf,
879                                                                char is_ipv6);
880};
881
882typedef void (*lsquic_cids_update_f)(void *ctx, void **peer_ctx,
883                                const lsquic_cid_t *cids, unsigned n_cids);
884
885struct stack_st_X509;
886
887/**
888 * When headers are processed, various errors may occur.  They are listed
889 * in this enum.
890 */
891enum lsquic_header_status
892{
893    LSQUIC_HDR_OK,
894    /** Duplicate pseudo-header */
895    LSQUIC_HDR_ERR_DUPLICATE_PSDO_HDR,
896    /** Not all request pseudo-headers are present */
897    LSQUIC_HDR_ERR_INCOMPL_REQ_PSDO_HDR,
898    /** Unnecessary request pseudo-header present in the response */
899    LSQUIC_HDR_ERR_UNNEC_REQ_PSDO_HDR,
900    /** Prohibited header in request */
901    LSQUIC_HDR_ERR_BAD_REQ_HEADER,
902    /** Not all response pseudo-headers are present */
903    LSQUIC_HDR_ERR_INCOMPL_RESP_PSDO_HDR,
904    /** Unnecessary response pseudo-header present in the response. */
905    LSQUIC_HDR_ERR_UNNEC_RESP_PSDO_HDR,
906    /** Unknown pseudo-header */
907    LSQUIC_HDR_ERR_UNKNOWN_PSDO_HDR,
908    /** Uppercase letter in header */
909    LSQUIC_HDR_ERR_UPPERCASE_HEADER,
910    /** Misplaced pseudo-header */
911    LSQUIC_HDR_ERR_MISPLACED_PSDO_HDR,
912    /** Missing pseudo-header */
913    LSQUIC_HDR_ERR_MISSING_PSDO_HDR,
914    /** Header or headers are too large */
915    LSQUIC_HDR_ERR_HEADERS_TOO_LARGE,
916    /** Cannot allocate any more memory. */
917    LSQUIC_HDR_ERR_NOMEM,
918};
919
920struct lsquic_hset_if
921{
922    /**
923     * Create a new header set.  This object is (and must be) fetched from a
924     * stream by calling @ref lsquic_stream_get_hset() before the stream can
925     * be read.
926     */
927    void *              (*hsi_create_header_set)(void *hsi_ctx,
928                                                        int is_push_promise);
929    /**
930     * Process new header.  Return 0 on success, -1 if there is a problem with
931     * the header.  -1 is treated as a stream error: the associated stream is
932     * reset.
933     *
934     * `hdr_set' is the header set object returned by
935     * @ref hsi_create_header_set().
936     *
937     * `name_idx' is set to the index in either the HPACK or QPACK static table
938     * whose entry's name element matches `name'.  The values are as follows:
939     *      - if there is no such match, `name_idx' is set to zero;
940     *      - if HPACK is used, the value is between 1 and 61; and
941     *      - if QPACK is used, the value is 62+ (subtract 62 to get the QPACK
942     *        static table index).
943     *
944     * If `name' is NULL, this means that no more header are going to be
945     * added to the set.
946     */
947    enum lsquic_header_status (*hsi_process_header)(void *hdr_set,
948                                    unsigned name_idx,
949                                    const char *name, unsigned name_len,
950                                    const char *value, unsigned value_len);
951    /**
952     * Discard header set.  This is called for unclaimed header sets and
953     * header sets that had an error.
954     */
955    void                (*hsi_discard_header_set)(void *hdr_set);
956};
957
958/**
959 * SSL keylog interface.
960 */
961struct lsquic_keylog_if
962{
963    /** Return keylog handle or NULL if no key logging is desired */
964    void *    (*kli_open) (void *keylog_ctx, lsquic_conn_t *);
965
966    /**
967     * Log line.  The first argument is the pointer returned by
968     * @ref kli_open.
969     */
970    void      (*kli_log_line) (void *handle, const char *line);
971
972    /**
973     * Close handle.
974     */
975    void      (*kli_close) (void *handle);
976};
977
978/* TODO: describe this important data structure */
979typedef struct lsquic_engine_api
980{
981    const struct lsquic_engine_settings *ea_settings;   /* Optional */
982    const struct lsquic_stream_if       *ea_stream_if;
983    void                                *ea_stream_if_ctx;
984    lsquic_packets_out_f                 ea_packets_out;
985    void                                *ea_packets_out_ctx;
986    lsquic_lookup_cert_f                 ea_lookup_cert;
987    void                                *ea_cert_lu_ctx;
988    struct ssl_ctx_st *                (*ea_get_ssl_ctx)(void *peer_ctx);
989    /**
990     * Shared hash interface is optional.  If set to zero, performance of
991     * multiple LSQUIC instances will be degraded.
992     */
993    const struct lsquic_shared_hash_if  *ea_shi;
994    void                                *ea_shi_ctx;
995    /**
996     * Memory interface is optional.
997     */
998    const struct lsquic_packout_mem_if  *ea_pmi;
999    void                                *ea_pmi_ctx;
1000    /**
1001     * Optional interface to report new and old source connection IDs.
1002     */
1003    lsquic_cids_update_f                 ea_new_scids;
1004    lsquic_cids_update_f                 ea_live_scids;
1005    lsquic_cids_update_f                 ea_old_scids;
1006    void                                *ea_cids_update_ctx;
1007    /**
1008     * Function to verify server certificate.  The chain contains at least
1009     * one element.  The first element in the chain is the server
1010     * certificate.  The chain belongs to the library.  If you want to
1011     * retain it, call sk_X509_up_ref().
1012     *
1013     * 0 is returned on success, -1 on error.
1014     *
1015     * If the function pointer is not set, no verification is performed
1016     * (the connection is allowed to proceed).
1017     */
1018    int                                (*ea_verify_cert)(void *verify_ctx,
1019                                                struct stack_st_X509 *chain);
1020    void                                *ea_verify_ctx;
1021
1022    /**
1023     * Optional header set interface.  If not specified, the incoming headers
1024     * are converted to HTTP/1.x format and are read from stream and have to
1025     * be parsed again.
1026     */
1027    const struct lsquic_hset_if         *ea_hsi_if;
1028    void                                *ea_hsi_ctx;
1029#if LSQUIC_CONN_STATS
1030    /**
1031     * If set, engine will print cumulative connection statistics to this
1032     * file just before it is destroyed.
1033     */
1034    void /* FILE, really */             *ea_stats_fh;
1035#endif
1036
1037    /**
1038     * Optional SSL key logging interface.
1039     */
1040    const struct lsquic_keylog_if       *ea_keylog_if;
1041    void                                *ea_keylog_ctx;
1042} lsquic_engine_api_t;
1043
1044/**
1045 * Create new engine.
1046 *
1047 * @param   lsquic_engine_flags     A bitmask of @ref LSENG_SERVER and
1048 *                                  @ref LSENG_HTTP
1049 */
1050lsquic_engine_t *
1051lsquic_engine_new (unsigned lsquic_engine_flags,
1052                   const struct lsquic_engine_api *);
1053
1054/**
1055 * Create a client connection to peer identified by `peer_ctx'.
1056 *
1057 * To let the engine specify QUIC version, use N_LSQVER.  If zero-rtt info
1058 * is supplied, version is picked from there instead.
1059 *
1060 * If `max_packet_size' is set to zero, it is inferred based on `peer_sa':
1061 * 1350 for IPv6 and 1370 for IPv4.
1062 */
1063lsquic_conn_t *
1064lsquic_engine_connect (lsquic_engine_t *, enum lsquic_version,
1065                       const struct sockaddr *local_sa,
1066                       const struct sockaddr *peer_sa,
1067                       void *peer_ctx, lsquic_conn_ctx_t *conn_ctx,
1068                       const char *hostname, unsigned short max_packet_size,
1069                       const unsigned char *zero_rtt, size_t zero_rtt_len,
1070                       /** Resumption token: optional */
1071                       const unsigned char *token, size_t token_sz);
1072
1073/**
1074 * Pass incoming packet to the QUIC engine.  This function can be called
1075 * more than once in a row.  After you add one or more packets, call
1076 * lsquic_engine_process_conns() to schedule output, if any.
1077 *
1078 * @retval  0   Packet was processed by a real connection.
1079 *
1080 * @retval  1   Packet was handled successfully, but not by a connection.
1081 *              This may happen with version negotiation and public reset
1082 *              packets as well as some packets that may be ignored.
1083 *
1084 * @retval -1   Some error occurred.  Possible reasons are invalid packet
1085 *              size or failure to allocate memory.
1086 */
1087int
1088lsquic_engine_packet_in (lsquic_engine_t *,
1089        const unsigned char *packet_in_data, size_t packet_in_size,
1090        const struct sockaddr *sa_local, const struct sockaddr *sa_peer,
1091        void *peer_ctx, int ecn);
1092
1093/**
1094 * Process tickable connections.  This function must be called often enough so
1095 * that packets and connections do not expire.
1096 */
1097void
1098lsquic_engine_process_conns (lsquic_engine_t *engine);
1099
1100/**
1101 * Returns true if engine has some unsent packets.  This happens if
1102 * @ref ea_packets_out() could not send everything out.
1103 */
1104int
1105lsquic_engine_has_unsent_packets (lsquic_engine_t *engine);
1106
1107/**
1108 * Send out as many unsent packets as possibe: until we are out of unsent
1109 * packets or until @ref ea_packets_out() fails.
1110 *
1111 * If @ref ea_packets_out() does fail (that is, it returns an error), this
1112 * function must be called to signify that sending of packets is possible
1113 * again.
1114 */
1115void
1116lsquic_engine_send_unsent_packets (lsquic_engine_t *engine);
1117
1118void
1119lsquic_engine_destroy (lsquic_engine_t *);
1120
1121/** Return max allowed outbound streams less current outbound streams. */
1122unsigned
1123lsquic_conn_n_avail_streams (const lsquic_conn_t *);
1124
1125void
1126lsquic_conn_make_stream (lsquic_conn_t *);
1127
1128/** Return number of delayed streams currently pending */
1129unsigned
1130lsquic_conn_n_pending_streams (const lsquic_conn_t *);
1131
1132/** Cancel `n' pending streams.  Returns new number of pending streams. */
1133unsigned
1134lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n);
1135
1136/**
1137 * Mark connection as going away: send GOAWAY frame and do not accept
1138 * any more incoming streams, nor generate streams of our own.
1139 *
1140 * In the server mode, of course, we can call this function just fine in both
1141 * Google and IETF QUIC.
1142 *
1143 * In client mode, calling this function in for an IETF QUIC connection does
1144 * not do anything, as the client MUST NOT send GOAWAY frames.
1145 * See [draft-ietf-quic-http-17] Section 4.2.7.
1146 */
1147void
1148lsquic_conn_going_away (lsquic_conn_t *);
1149
1150/**
1151 * This forces connection close.  on_conn_closed and on_close callbacks
1152 * will be called.
1153 */
1154void
1155lsquic_conn_close (lsquic_conn_t *);
1156
1157int lsquic_stream_wantread(lsquic_stream_t *s, int is_want);
1158ssize_t lsquic_stream_read(lsquic_stream_t *s, void *buf, size_t len);
1159ssize_t lsquic_stream_readv(lsquic_stream_t *s, const struct iovec *,
1160                                                            int iovcnt);
1161
1162/**
1163 * This function allows user-supplied callback to read the stream contents.
1164 * It is meant to be used for zero-copy stream processing.
1165 */
1166ssize_t
1167lsquic_stream_readf (lsquic_stream_t *s,
1168    /**
1169     * The callback takes four parameters:
1170     *  - Pointer to user-supplied context;
1171     *  - Pointer to the data;
1172     *  - Data size (can be zero); and
1173     *  - Indicator whether the FIN follows the data.
1174     *
1175     * The callback returns number of bytes processed.  If this number is zero
1176     * or is smaller than `len', reading from stream stops.
1177     */
1178    size_t (*readf)(void *ctx, const unsigned char *buf, size_t len, int fin),
1179    void *ctx);
1180
1181int lsquic_stream_wantwrite(lsquic_stream_t *s, int is_want);
1182
1183/**
1184 * Write `len' bytes to the stream.  Returns number of bytes written, which
1185 * may be smaller that `len'.
1186 */
1187ssize_t lsquic_stream_write(lsquic_stream_t *s, const void *buf, size_t len);
1188
1189ssize_t lsquic_stream_writev(lsquic_stream_t *s, const struct iovec *vec, int count);
1190
1191/**
1192 * Used as argument to @ref lsquic_stream_writef()
1193 */
1194struct lsquic_reader
1195{
1196    /**
1197     * Not a ssize_t because the read function is not supposed to return
1198     * an error.  If an error occurs in the read function (for example, when
1199     * reading from a file fails), it is supposed to deal with the error
1200     * itself.
1201     */
1202    size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count);
1203    /**
1204     * Return number of bytes remaining in the reader.
1205     */
1206    size_t (*lsqr_size) (void *lsqr_ctx);
1207    void    *lsqr_ctx;
1208};
1209
1210/**
1211 * Write to stream using @ref lsquic_reader.  This is the most generic of
1212 * the write functions -- @ref lsquic_stream_write() and
1213 * @ref lsquic_stream_writev() utilize the same mechanism.
1214 *
1215 * @retval Number of bytes written or -1 on error.
1216 */
1217ssize_t
1218lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *);
1219
1220/**
1221 * Flush any buffered data.  This triggers packetizing even a single byte
1222 * into a separate frame.  Flushing a closed stream is an error.
1223 *
1224 * @retval  0   Success
1225 * @retval -1   Failure
1226 */
1227int
1228lsquic_stream_flush (lsquic_stream_t *s);
1229
1230/**
1231 * @typedef lsquic_http_header_t
1232 * @brief HTTP header structure. Contains header name and value.
1233 *
1234 */
1235typedef struct lsquic_http_header
1236{
1237   struct iovec name;
1238   struct iovec value;
1239} lsquic_http_header_t;
1240
1241/**
1242 * @typedef lsquic_http_headers_t
1243 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs.
1244 * used in API functions to pass headers.
1245 */
1246struct lsquic_http_headers
1247{
1248    int                     count;
1249    lsquic_http_header_t   *headers;
1250};
1251
1252int lsquic_stream_send_headers(lsquic_stream_t *s,
1253                               const lsquic_http_headers_t *h, int eos);
1254
1255/**
1256 * Get header set associated with the stream.  The header set is created by
1257 * @ref hsi_create_header_set() callback.  After this call, the ownership of
1258 * the header set is trasnferred to the caller.
1259 *
1260 * This call must precede calls to @ref lsquic_stream_read() and
1261 * @ref lsquic_stream_readv().
1262 *
1263 * If the optional header set interface (@ref ea_hsi_if) is not specified,
1264 * this function returns NULL.
1265 */
1266void *
1267lsquic_stream_get_hset (lsquic_stream_t *);
1268
1269/**
1270 * A server may push a stream.  This call creates a new stream in reference
1271 * to stream `s'.  It will behave as if the client made a request: it will
1272 * trigger on_new_stream() event and it can be used as a regular client-
1273 * initiated stream.
1274 *
1275 * If `hdr_set' is not set, it is generated by using `ea_hsi_if' callbacks.
1276 * In either case, the header set object belongs to the connection.  The
1277 * user is not to free this object until (@ref hsi_discard_header_set) is
1278 * called.
1279 *
1280 * @retval  0   Stream pushed successfully.
1281 * @retval  1   Stream push failed because it is disabled or because we hit
1282 *                stream limit or connection is going away.
1283 * @retval -1   Stream push failed because of an internal error.
1284 */
1285int
1286lsquic_conn_push_stream (lsquic_conn_t *c, void *hdr_set, lsquic_stream_t *s,
1287    const  struct iovec* url, const struct iovec* authority,
1288    const lsquic_http_headers_t *headers);
1289
1290/**
1291 * Only makes sense in server mode: the client cannot push a stream and this
1292 * function always returns false in client mode.
1293 */
1294int
1295lsquic_conn_is_push_enabled (lsquic_conn_t *);
1296
1297/** Possible values for how are 0, 1, and 2.  See shutdown(2). */
1298int lsquic_stream_shutdown(lsquic_stream_t *s, int how);
1299
1300int lsquic_stream_close(lsquic_stream_t *s);
1301
1302/**
1303 * Get certificate chain returned by the server.  This can be used for
1304 * server certificate verification.
1305 *
1306 * If server certificate cannot be verified, the connection can be closed
1307 * using lsquic_conn_cert_verification_failed().
1308 *
1309 * The caller releases the stack using sk_X509_free().
1310 */
1311struct stack_st_X509 *
1312lsquic_conn_get_server_cert_chain (lsquic_conn_t *);
1313
1314/** Returns ID of the stream */
1315lsquic_stream_id_t
1316lsquic_stream_id (const lsquic_stream_t *s);
1317
1318/**
1319 * Returns stream ctx associated with the stream.  (The context is what
1320 * is returned by @ref on_new_stream callback).
1321 */
1322lsquic_stream_ctx_t *
1323lsquic_stream_get_ctx (const lsquic_stream_t *s);
1324
1325/** Returns true if this is a pushed stream */
1326int
1327lsquic_stream_is_pushed (const lsquic_stream_t *s);
1328
1329/**
1330 * Returns true if this stream was rejected, false otherwise.  Use this as
1331 * an aid to distinguish between errors.
1332 */
1333int
1334lsquic_stream_is_rejected (const lsquic_stream_t *s);
1335
1336/**
1337 * Refuse pushed stream.  Call it from @ref on_new_stream.
1338 *
1339 * No need to call lsquic_stream_close() after this.  on_close will be called.
1340 *
1341 * @see lsquic_stream_is_pushed
1342 */
1343int
1344lsquic_stream_refuse_push (lsquic_stream_t *s);
1345
1346/**
1347 * Get information associated with pushed stream:
1348 *
1349 * @param ref_stream_id   Stream ID in response to which push promise was
1350 *                            sent.
1351 * @param hdr_set         Header set.  This object was passed to or generated
1352 *                            by @ref lsquic_conn_push_stream().
1353 *
1354 * @retval   0  Success.
1355 * @retval  -1  This is not a pushed stream.
1356 */
1357int
1358lsquic_stream_push_info (const lsquic_stream_t *,
1359                         lsquic_stream_id_t *ref_stream_id, void **hdr_set);
1360
1361/** Return current priority of the stream */
1362unsigned lsquic_stream_priority (const lsquic_stream_t *s);
1363
1364/**
1365 * Set stream priority.  Valid priority values are 1 through 256, inclusive.
1366 *
1367 * @retval   0  Success.
1368 * @retval  -1  Priority value is invalid.
1369 */
1370int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority);
1371
1372/**
1373 * Get a pointer to the connection object.  Use it with lsquic_conn_*
1374 * functions.
1375 */
1376lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s);
1377
1378lsquic_stream_t *
1379lsquic_conn_get_stream_by_id (lsquic_conn_t *c, lsquic_stream_id_t stream_id);
1380
1381/** Get connection ID */
1382const lsquic_cid_t *
1383lsquic_conn_id (const lsquic_conn_t *c);
1384
1385/** Get pointer to the engine */
1386lsquic_engine_t *
1387lsquic_conn_get_engine (lsquic_conn_t *c);
1388
1389int
1390lsquic_conn_get_sockaddr(lsquic_conn_t *c,
1391                const struct sockaddr **local, const struct sockaddr **peer);
1392
1393struct lsquic_logger_if {
1394    int     (*log_buf)(void *logger_ctx, const char *buf, size_t len);
1395};
1396
1397/**
1398 * Enumerate timestamp styles supported by LSQUIC logger mechanism.
1399 */
1400enum lsquic_logger_timestamp_style {
1401    /**
1402     * No timestamp is generated.
1403     */
1404    LLTS_NONE,
1405
1406    /**
1407     * The timestamp consists of 24 hours, minutes, seconds, and
1408     * milliseconds.  Example: 13:43:46.671
1409     */
1410    LLTS_HHMMSSMS,
1411
1412    /**
1413     * Like above, plus date, e.g: 2017-03-21 13:43:46.671
1414     */
1415    LLTS_YYYYMMDD_HHMMSSMS,
1416
1417    /**
1418     * This is Chrome-like timestamp used by proto-quic.  The timestamp
1419     * includes month, date, hours, minutes, seconds, and microseconds.
1420     *
1421     * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
1422     *
1423     * This is to facilitate reading two logs side-by-side.
1424     */
1425    LLTS_CHROMELIKE,
1426
1427    /**
1428     * The timestamp consists of 24 hours, minutes, seconds, and
1429     * microseconds.  Example: 13:43:46.671123
1430     */
1431    LLTS_HHMMSSUS,
1432
1433    /**
1434     * Date and time using microsecond resolution,
1435     * e.g: 2017-03-21 13:43:46.671123
1436     */
1437    LLTS_YYYYMMDD_HHMMSSUS,
1438
1439    N_LLTS
1440};
1441
1442/**
1443 * Call this if you want to do something with LSQUIC log messages, as they
1444 * are thrown out by default.
1445 */
1446void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx,
1447                        enum lsquic_logger_timestamp_style);
1448
1449/**
1450 * Set log level for all LSQUIC modules.  Acceptable values are debug, info,
1451 * notice, warning, error, alert, emerg, crit (case-insensitive).
1452 *
1453 * @retval  0   Success.
1454 * @retval -1   Failure: log_level is not valid.
1455 */
1456int
1457lsquic_set_log_level (const char *log_level);
1458
1459/**
1460 * E.g. "event=debug"
1461 */
1462int
1463lsquic_logger_lopt (const char *optarg);
1464
1465/**
1466 * Return the list of QUIC versions (as bitmask) this engine instance
1467 * supports.
1468 */
1469unsigned lsquic_engine_quic_versions (const lsquic_engine_t *);
1470
1471/**
1472 * This is one of the flags that can be passed to @ref lsquic_global_init.
1473 * Use it to initialize LSQUIC for use in client mode.
1474 */
1475#define LSQUIC_GLOBAL_CLIENT (1 << 0)
1476
1477/**
1478 * This is one of the flags that can be passed to @ref lsquic_global_init.
1479 * Use it to initialize LSQUIC for use in server mode.
1480 */
1481#define LSQUIC_GLOBAL_SERVER (1 << 1)
1482
1483/**
1484 * Initialize LSQUIC.  This must be called before any other LSQUIC function
1485 * is called.  Returns 0 on success and -1 on failure.
1486 *
1487 * @param flags     This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and
1488 *                    @ref LSQUIC_GLOBAL_SERVER.  At least one of these
1489 *                    flags should be specified.
1490 *
1491 * @retval  0   Success.
1492 * @retval -1   Initialization failed.
1493 *
1494 * @see LSQUIC_GLOBAL_CLIENT
1495 * @see LSQUIC_GLOBAL_SERVER
1496 */
1497int
1498lsquic_global_init (int flags);
1499
1500/**
1501 * Clean up global state created by @ref lsquic_global_init.  Should be
1502 * called after all LSQUIC engine instances are gone.
1503 */
1504void
1505lsquic_global_cleanup (void);
1506
1507/**
1508 * Get QUIC version used by the connection.
1509 *
1510 * @see lsquic_version
1511 */
1512enum lsquic_version
1513lsquic_conn_quic_version (const lsquic_conn_t *c);
1514
1515/* Return keysize or -1 on error */
1516int
1517lsquic_conn_crypto_keysize (const lsquic_conn_t *c);
1518
1519/* Return algorithm keysize or -1 on error */
1520int
1521lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *c);
1522
1523enum lsquic_crypto_ver
1524{
1525    LSQ_CRY_QUIC,
1526    LSQ_CRY_TLSv13,
1527};
1528
1529enum lsquic_crypto_ver
1530lsquic_conn_crypto_ver (const lsquic_conn_t *c);
1531
1532/* Return cipher or NULL on error */
1533const char *
1534lsquic_conn_crypto_cipher (const lsquic_conn_t *c);
1535
1536/** Translate string QUIC version to LSQUIC QUIC version representation */
1537enum lsquic_version
1538lsquic_str2ver (const char *str, size_t len);
1539
1540/** Translate ALPN (e.g. "h3", "h3-23", "h3-Q046") to LSQUIC enum */
1541enum lsquic_version
1542lsquic_alpn2ver (const char *alpn, size_t len);
1543
1544/**
1545 * This function closes all mini connections and marks all full connections
1546 * as going away.  In server mode, this also causes the engine to stop
1547 * creating new connections.
1548 */
1549void
1550lsquic_engine_cooldown (lsquic_engine_t *);
1551
1552struct ssl_st *
1553lsquic_hsk_getssl(lsquic_conn_t *conn);
1554
1555/**
1556 * Get user-supplied context associated with the connection.
1557 */
1558lsquic_conn_ctx_t *
1559lsquic_conn_get_ctx (const lsquic_conn_t *);
1560
1561/**
1562 * Set user-supplied context associated with the connection.
1563 */
1564void
1565lsquic_conn_set_ctx (lsquic_conn_t *, lsquic_conn_ctx_t *);
1566
1567/**
1568 * Get peer context associated with the connection.
1569 */
1570void *
1571lsquic_conn_get_peer_ctx (lsquic_conn_t *, const struct sockaddr *local_sa);
1572
1573/**
1574 * Abort connection.
1575 */
1576void
1577lsquic_conn_abort (lsquic_conn_t *);
1578
1579/**
1580 * Helper function: convert list of versions as specified in the argument
1581 * bitmask to string that can be included as argument to "v=" part of the
1582 * Alt-Svc header.
1583 *
1584 * For example (1<<LSQVER_037)|(1<<LSQVER_038) => "37,38"
1585 *
1586 * This is only applicable to Google QUIC versions.
1587 */
1588const char *
1589lsquic_get_alt_svc_versions (unsigned versions);
1590
1591/**
1592 * Return a NULL-terminated list of HTTP/3 ALPNs, e.g "h3-17", "h3-18", "h3".
1593 */
1594const char *const *
1595lsquic_get_h3_alpns (unsigned versions);
1596
1597/**
1598 * Returns true if provided buffer could be a valid handshake-stage packet,
1599 * false otherwise.  Do not call this function if a connection has already
1600 * been established: it will return incorrect result.
1601 */
1602int
1603lsquic_is_valid_hs_packet (lsquic_engine_t *, const unsigned char *, size_t);
1604
1605/**
1606 * Parse cid from packet stored in `buf' and store it to `cid'.  Returns 0
1607 * on success and -1 on failure.
1608 */
1609int
1610lsquic_cid_from_packet (const unsigned char *, size_t bufsz, lsquic_cid_t *cid);
1611
1612/**
1613 * Returns true if there are connections to be processed, false otherwise.
1614 * If true, `diff' is set to the difference between the earliest advisory
1615 * tick time and now.  If the former is in the past, the value of `diff'
1616 * is negative.
1617 */
1618int
1619lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff);
1620
1621/**
1622 * Return number of connections whose advisory tick time is before current
1623 * time plus `from_now' microseconds from now.  `from_now' can be negative.
1624 */
1625unsigned
1626lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now);
1627
1628enum LSQUIC_CONN_STATUS
1629{
1630    LSCONN_ST_HSK_IN_PROGRESS,
1631    LSCONN_ST_CONNECTED,
1632    LSCONN_ST_HSK_FAILURE,
1633    LSCONN_ST_GOING_AWAY,
1634    LSCONN_ST_TIMED_OUT,
1635    /* If es_honor_prst is not set, the connection will never get public
1636     * reset packets and this flag will not be set.
1637     */
1638    LSCONN_ST_RESET,
1639    LSCONN_ST_USER_ABORTED,
1640    LSCONN_ST_ERROR,
1641    LSCONN_ST_CLOSED,
1642    LSCONN_ST_PEER_GOING_AWAY,
1643};
1644
1645enum LSQUIC_CONN_STATUS
1646lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz);
1647
1648extern const char *const
1649lsquic_ver2str[N_LSQVER];
1650
1651#ifdef __cplusplus
1652}
1653#endif
1654
1655#endif //__LSQUIC_H__
1656
1657