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