lsquic.h revision 7a8b2ece
1/* Copyright (c) 2017 - 2019 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 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    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 1
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 4 through 18, 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.
752     *
753     * Default value is @ref LSQUIC_DF_QL_BITS
754     */
755    int             es_ql_bits;
756};
757
758/* Initialize `settings' to default values */
759void
760lsquic_engine_init_settings (struct lsquic_engine_settings *,
761                             unsigned lsquic_engine_flags);
762
763/**
764 * Check settings for errors.
765 *
766 * @param   settings    Settings struct.
767 *
768 * @param   flags       Engine flags.
769 *
770 * @param   err_buf     Optional pointer to buffer into which error string
771 *                      is written.
772
773 * @param   err_buf_sz  Size of err_buf.  No more than this number of bytes
774 *                      will be written to err_buf, including the NUL byte.
775 *
776 * @retval  0   Settings have no errors.
777 * @retval -1   There are errors in settings.
778 */
779int
780lsquic_engine_check_settings (const struct lsquic_engine_settings *settings,
781                              unsigned lsquic_engine_flags,
782                              char *err_buf, size_t err_buf_sz);
783
784struct lsquic_out_spec
785{
786    struct iovec          *iov;
787    size_t                 iovlen;
788    const struct sockaddr *local_sa;
789    const struct sockaddr *dest_sa;
790    void                  *peer_ctx;
791    int                    ecn; /* Valid values are 0 - 3.  See RFC 3168 */
792};
793
794/**
795 * Returns number of packets successfully sent out or -1 on error.  -1 should
796 * only be returned if no packets were sent out.  If -1 is returned or if the
797 * return value is smaller than `n_packets_out', this indicates that sending
798 * of packets is not possible.
799 *
800 * If not all packets could be sent out, errno is examined.  If it is not
801 * EAGAIN or EWOULDBLOCK, the connection whose packet cause the error is
802 * closed forthwith.
803 *
804 * No packets will be attempted to be sent out until
805 * @ref lsquic_engine_send_unsent_packets() is called.
806 */
807typedef int (*lsquic_packets_out_f)(
808    void                          *packets_out_ctx,
809    const struct lsquic_out_spec  *out_spec,
810    unsigned                       n_packets_out
811);
812
813/**
814 * The shared hash interface is used to share data between multiple LSQUIC
815 * instances.
816 */
817struct lsquic_shared_hash_if
818{
819    /**
820     * If you want your item to never expire, set `expiry' to zero.
821     * Returns 0 on success, -1 on failure.
822     *
823     * If inserted successfully, `free()' will be called on `data' and 'key'
824     * pointer when the element is deleted, whether due to expiration
825     * or explicit deletion.
826     */
827    int (*shi_insert)(void *shi_ctx, void *key, unsigned key_sz,
828                      void *data, unsigned data_sz, time_t expiry);
829    /**
830     * Returns 0 on success, -1 on failure.
831     */
832    int (*shi_delete)(void *shi_ctx, const void *key, unsigned key_sz);
833
834    /**
835     * `data' is pointed to the result and `data_sz' is set to the
836     * object size.  The implementation may choose to copy the object
837     * into buffer pointed to by `data', so you should have it ready.
838     *
839     * @retval  1   found.
840     * @retval  0   not found.
841     * @retval -1   error (perhaps not enough room in `data' if copy was
842     *                attempted).
843     */
844    int (*shi_lookup)(void *shi_ctx, const void *key, unsigned key_sz,
845                                     void **data, unsigned *data_sz);
846};
847
848/**
849 * The packet out memory interface is used by LSQUIC to get buffers to
850 * which outgoing packets will be written before they are passed to
851 * ea_packets_out callback.
852 *
853 * If not specified, malloc() and free() are used.
854 */
855struct lsquic_packout_mem_if
856{
857    /**
858     * Allocate buffer for sending.
859     */
860    void *  (*pmi_allocate) (void *pmi_ctx, void *conn_ctx, unsigned short sz,
861                                                                char is_ipv6);
862    /**
863     * This function is used to release the allocated buffer after it is
864     * sent via @ref ea_packets_out.
865     */
866    void    (*pmi_release)  (void *pmi_ctx, void *conn_ctx, void *buf,
867                                                                char is_ipv6);
868    /**
869     * If allocated buffer is not going to be sent, return it to the caller
870     * using this function.
871     */
872    void    (*pmi_return)  (void *pmi_ctx, void *conn_ctx, void *buf,
873                                                                char is_ipv6);
874};
875
876typedef void (*lsquic_cids_update_f)(void *ctx, void **peer_ctx,
877                                const lsquic_cid_t *cids, unsigned n_cids);
878
879struct stack_st_X509;
880
881/**
882 * When headers are processed, various errors may occur.  They are listed
883 * in this enum.
884 */
885enum lsquic_header_status
886{
887    LSQUIC_HDR_OK,
888    /** Duplicate pseudo-header */
889    LSQUIC_HDR_ERR_DUPLICATE_PSDO_HDR,
890    /** Not all request pseudo-headers are present */
891    LSQUIC_HDR_ERR_INCOMPL_REQ_PSDO_HDR,
892    /** Unnecessary request pseudo-header present in the response */
893    LSQUIC_HDR_ERR_UNNEC_REQ_PSDO_HDR,
894    /** Prohibited header in request */
895    LSQUIC_HDR_ERR_BAD_REQ_HEADER,
896    /** Not all response pseudo-headers are present */
897    LSQUIC_HDR_ERR_INCOMPL_RESP_PSDO_HDR,
898    /** Unnecessary response pseudo-header present in the response. */
899    LSQUIC_HDR_ERR_UNNEC_RESP_PSDO_HDR,
900    /** Unknown pseudo-header */
901    LSQUIC_HDR_ERR_UNKNOWN_PSDO_HDR,
902    /** Uppercase letter in header */
903    LSQUIC_HDR_ERR_UPPERCASE_HEADER,
904    /** Misplaced pseudo-header */
905    LSQUIC_HDR_ERR_MISPLACED_PSDO_HDR,
906    /** Missing pseudo-header */
907    LSQUIC_HDR_ERR_MISSING_PSDO_HDR,
908    /** Header or headers are too large */
909    LSQUIC_HDR_ERR_HEADERS_TOO_LARGE,
910    /** Cannot allocate any more memory. */
911    LSQUIC_HDR_ERR_NOMEM,
912};
913
914struct lsquic_hset_if
915{
916    /**
917     * Create a new header set.  This object is (and must be) fetched from a
918     * stream by calling @ref lsquic_stream_get_hset() before the stream can
919     * be read.
920     */
921    void *              (*hsi_create_header_set)(void *hsi_ctx,
922                                                        int is_push_promise);
923    /**
924     * Process new header.  Return 0 on success, -1 if there is a problem with
925     * the header.  -1 is treated as a stream error: the associated stream is
926     * reset.
927     *
928     * `hdr_set' is the header set object returned by
929     * @ref hsi_create_header_set().
930     *
931     * `name_idx' is set to the index in either the HPACK or QPACK static table
932     * whose entry's name element matches `name'.  The values are as follows:
933     *      - if there is no such match, `name_idx' is set to zero;
934     *      - if HPACK is used, the value is between 1 and 61; and
935     *      - if QPACK is used, the value is 62+ (subtract 62 to get the QPACK
936     *        static table index).
937     *
938     * If `name' is NULL, this means that no more header are going to be
939     * added to the set.
940     */
941    enum lsquic_header_status (*hsi_process_header)(void *hdr_set,
942                                    unsigned name_idx,
943                                    const char *name, unsigned name_len,
944                                    const char *value, unsigned value_len);
945    /**
946     * Discard header set.  This is called for unclaimed header sets and
947     * header sets that had an error.
948     */
949    void                (*hsi_discard_header_set)(void *hdr_set);
950};
951
952/**
953 * SSL keylog interface.
954 */
955struct lsquic_keylog_if
956{
957    /** Return keylog handle or NULL if no key logging is desired */
958    void *    (*kli_open) (void *keylog_ctx, lsquic_conn_t *);
959
960    /**
961     * Log line.  The first argument is the pointer returned by
962     * @ref kli_open.
963     */
964    void      (*kli_log_line) (void *handle, const char *line);
965
966    /**
967     * Close handle.
968     */
969    void      (*kli_close) (void *handle);
970};
971
972/* TODO: describe this important data structure */
973typedef struct lsquic_engine_api
974{
975    const struct lsquic_engine_settings *ea_settings;   /* Optional */
976    const struct lsquic_stream_if       *ea_stream_if;
977    void                                *ea_stream_if_ctx;
978    lsquic_packets_out_f                 ea_packets_out;
979    void                                *ea_packets_out_ctx;
980    lsquic_lookup_cert_f                 ea_lookup_cert;
981    void                                *ea_cert_lu_ctx;
982    struct ssl_ctx_st *                (*ea_get_ssl_ctx)(void *peer_ctx);
983    /**
984     * Shared hash interface is optional.  If set to zero, performance of
985     * multiple LSQUIC instances will be degraded.
986     */
987    const struct lsquic_shared_hash_if  *ea_shi;
988    void                                *ea_shi_ctx;
989    /**
990     * Memory interface is optional.
991     */
992    const struct lsquic_packout_mem_if  *ea_pmi;
993    void                                *ea_pmi_ctx;
994    /**
995     * Optional interface to report new and old source connection IDs.
996     */
997    lsquic_cids_update_f                 ea_new_scids;
998    lsquic_cids_update_f                 ea_live_scids;
999    lsquic_cids_update_f                 ea_old_scids;
1000    void                                *ea_cids_update_ctx;
1001    /**
1002     * Function to verify server certificate.  The chain contains at least
1003     * one element.  The first element in the chain is the server
1004     * certificate.  The chain belongs to the library.  If you want to
1005     * retain it, call sk_X509_up_ref().
1006     *
1007     * 0 is returned on success, -1 on error.
1008     *
1009     * If the function pointer is not set, no verification is performed
1010     * (the connection is allowed to proceed).
1011     */
1012    int                                (*ea_verify_cert)(void *verify_ctx,
1013                                                struct stack_st_X509 *chain);
1014    void                                *ea_verify_ctx;
1015
1016    /**
1017     * Optional header set interface.  If not specified, the incoming headers
1018     * are converted to HTTP/1.x format and are read from stream and have to
1019     * be parsed again.
1020     */
1021    const struct lsquic_hset_if         *ea_hsi_if;
1022    void                                *ea_hsi_ctx;
1023#if LSQUIC_CONN_STATS
1024    /**
1025     * If set, engine will print cumulative connection statistics to this
1026     * file just before it is destroyed.
1027     */
1028    void /* FILE, really */             *ea_stats_fh;
1029#endif
1030
1031    /**
1032     * Optional SSL key logging interface.
1033     */
1034    const struct lsquic_keylog_if       *ea_keylog_if;
1035    void                                *ea_keylog_ctx;
1036} lsquic_engine_api_t;
1037
1038/**
1039 * Create new engine.
1040 *
1041 * @param   lsquic_engine_flags     A bitmask of @ref LSENG_SERVER and
1042 *                                  @ref LSENG_HTTP
1043 */
1044lsquic_engine_t *
1045lsquic_engine_new (unsigned lsquic_engine_flags,
1046                   const struct lsquic_engine_api *);
1047
1048/**
1049 * Create a client connection to peer identified by `peer_ctx'.
1050 *
1051 * To let the engine specify QUIC version, use N_LSQVER.  If zero-rtt info
1052 * is supplied, version is picked from there instead.
1053 *
1054 * If `max_packet_size' is set to zero, it is inferred based on `peer_sa':
1055 * 1350 for IPv6 and 1370 for IPv4.
1056 */
1057lsquic_conn_t *
1058lsquic_engine_connect (lsquic_engine_t *, enum lsquic_version,
1059                       const struct sockaddr *local_sa,
1060                       const struct sockaddr *peer_sa,
1061                       void *peer_ctx, lsquic_conn_ctx_t *conn_ctx,
1062                       const char *hostname, unsigned short max_packet_size,
1063                       const unsigned char *zero_rtt, size_t zero_rtt_len,
1064                       /** Resumption token: optional */
1065                       const unsigned char *token, size_t token_sz);
1066
1067/**
1068 * Pass incoming packet to the QUIC engine.  This function can be called
1069 * more than once in a row.  After you add one or more packets, call
1070 * lsquic_engine_process_conns() to schedule output, if any.
1071 *
1072 * @retval  0   Packet was processed by a real connection.
1073 *
1074 * @retval  1   Packet was handled successfully, but not by a connection.
1075 *              This may happen with version negotiation and public reset
1076 *              packets as well as some packets that may be ignored.
1077 *
1078 * @retval -1   Some error occurred.  Possible reasons are invalid packet
1079 *              size or failure to allocate memory.
1080 */
1081int
1082lsquic_engine_packet_in (lsquic_engine_t *,
1083        const unsigned char *packet_in_data, size_t packet_in_size,
1084        const struct sockaddr *sa_local, const struct sockaddr *sa_peer,
1085        void *peer_ctx, int ecn);
1086
1087/**
1088 * Process tickable connections.  This function must be called often enough so
1089 * that packets and connections do not expire.
1090 */
1091void
1092lsquic_engine_process_conns (lsquic_engine_t *engine);
1093
1094/**
1095 * Returns true if engine has some unsent packets.  This happens if
1096 * @ref ea_packets_out() could not send everything out.
1097 */
1098int
1099lsquic_engine_has_unsent_packets (lsquic_engine_t *engine);
1100
1101/**
1102 * Send out as many unsent packets as possibe: until we are out of unsent
1103 * packets or until @ref ea_packets_out() fails.
1104 *
1105 * If @ref ea_packets_out() does fail (that is, it returns an error), this
1106 * function must be called to signify that sending of packets is possible
1107 * again.
1108 */
1109void
1110lsquic_engine_send_unsent_packets (lsquic_engine_t *engine);
1111
1112void
1113lsquic_engine_destroy (lsquic_engine_t *);
1114
1115/** Return max allowed outbound streams less current outbound streams. */
1116unsigned
1117lsquic_conn_n_avail_streams (const lsquic_conn_t *);
1118
1119void
1120lsquic_conn_make_stream (lsquic_conn_t *);
1121
1122/** Return number of delayed streams currently pending */
1123unsigned
1124lsquic_conn_n_pending_streams (const lsquic_conn_t *);
1125
1126/** Cancel `n' pending streams.  Returns new number of pending streams. */
1127unsigned
1128lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n);
1129
1130/**
1131 * Mark connection as going away: send GOAWAY frame and do not accept
1132 * any more incoming streams, nor generate streams of our own.
1133 *
1134 * In the server mode, of course, we can call this function just fine in both
1135 * Google and IETF QUIC.
1136 *
1137 * In client mode, calling this function in for an IETF QUIC connection does
1138 * not do anything, as the client MUST NOT send GOAWAY frames.
1139 * See [draft-ietf-quic-http-17] Section 4.2.7.
1140 */
1141void
1142lsquic_conn_going_away (lsquic_conn_t *);
1143
1144/**
1145 * This forces connection close.  on_conn_closed and on_close callbacks
1146 * will be called.
1147 */
1148void
1149lsquic_conn_close (lsquic_conn_t *);
1150
1151int lsquic_stream_wantread(lsquic_stream_t *s, int is_want);
1152ssize_t lsquic_stream_read(lsquic_stream_t *s, void *buf, size_t len);
1153ssize_t lsquic_stream_readv(lsquic_stream_t *s, const struct iovec *,
1154                                                            int iovcnt);
1155
1156/**
1157 * This function allows user-supplied callback to read the stream contents.
1158 * It is meant to be used for zero-copy stream processing.
1159 */
1160ssize_t
1161lsquic_stream_readf (lsquic_stream_t *s,
1162    /**
1163     * The callback takes four parameters:
1164     *  - Pointer to user-supplied context;
1165     *  - Pointer to the data;
1166     *  - Data size (can be zero); and
1167     *  - Indicator whether the FIN follows the data.
1168     *
1169     * The callback returns number of bytes processed.  If this number is zero
1170     * or is smaller than `len', reading from stream stops.
1171     */
1172    size_t (*readf)(void *ctx, const unsigned char *buf, size_t len, int fin),
1173    void *ctx);
1174
1175int lsquic_stream_wantwrite(lsquic_stream_t *s, int is_want);
1176
1177/**
1178 * Write `len' bytes to the stream.  Returns number of bytes written, which
1179 * may be smaller that `len'.
1180 */
1181ssize_t lsquic_stream_write(lsquic_stream_t *s, const void *buf, size_t len);
1182
1183ssize_t lsquic_stream_writev(lsquic_stream_t *s, const struct iovec *vec, int count);
1184
1185/**
1186 * Used as argument to @ref lsquic_stream_writef()
1187 */
1188struct lsquic_reader
1189{
1190    /**
1191     * Not a ssize_t because the read function is not supposed to return
1192     * an error.  If an error occurs in the read function (for example, when
1193     * reading from a file fails), it is supposed to deal with the error
1194     * itself.
1195     */
1196    size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count);
1197    /**
1198     * Return number of bytes remaining in the reader.
1199     */
1200    size_t (*lsqr_size) (void *lsqr_ctx);
1201    void    *lsqr_ctx;
1202};
1203
1204/**
1205 * Write to stream using @ref lsquic_reader.  This is the most generic of
1206 * the write functions -- @ref lsquic_stream_write() and
1207 * @ref lsquic_stream_writev() utilize the same mechanism.
1208 *
1209 * @retval Number of bytes written or -1 on error.
1210 */
1211ssize_t
1212lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *);
1213
1214/**
1215 * Flush any buffered data.  This triggers packetizing even a single byte
1216 * into a separate frame.  Flushing a closed stream is an error.
1217 *
1218 * @retval  0   Success
1219 * @retval -1   Failure
1220 */
1221int
1222lsquic_stream_flush (lsquic_stream_t *s);
1223
1224/**
1225 * @typedef lsquic_http_header_t
1226 * @brief HTTP header structure. Contains header name and value.
1227 *
1228 */
1229typedef struct lsquic_http_header
1230{
1231   struct iovec name;
1232   struct iovec value;
1233} lsquic_http_header_t;
1234
1235/**
1236 * @typedef lsquic_http_headers_t
1237 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs.
1238 * used in API functions to pass headers.
1239 */
1240struct lsquic_http_headers
1241{
1242    int                     count;
1243    lsquic_http_header_t   *headers;
1244};
1245
1246int lsquic_stream_send_headers(lsquic_stream_t *s,
1247                               const lsquic_http_headers_t *h, int eos);
1248
1249/**
1250 * Get header set associated with the stream.  The header set is created by
1251 * @ref hsi_create_header_set() callback.  After this call, the ownership of
1252 * the header set is trasnferred to the caller.
1253 *
1254 * This call must precede calls to @ref lsquic_stream_read() and
1255 * @ref lsquic_stream_readv().
1256 *
1257 * If the optional header set interface (@ref ea_hsi_if) is not specified,
1258 * this function returns NULL.
1259 */
1260void *
1261lsquic_stream_get_hset (lsquic_stream_t *);
1262
1263/**
1264 * A server may push a stream.  This call creates a new stream in reference
1265 * to stream `s'.  It will behave as if the client made a request: it will
1266 * trigger on_new_stream() event and it can be used as a regular client-
1267 * initiated stream.
1268 *
1269 * If `hdr_set' is not set, it is generated by using `ea_hsi_if' callbacks.
1270 * In either case, the header set object belongs to the connection.  The
1271 * user is not to free this object until (@ref hsi_discard_header_set) is
1272 * called.
1273 *
1274 * @retval  0   Stream pushed successfully.
1275 * @retval  1   Stream push failed because it is disabled or because we hit
1276 *                stream limit or connection is going away.
1277 * @retval -1   Stream push failed because of an internal error.
1278 */
1279int
1280lsquic_conn_push_stream (lsquic_conn_t *c, void *hdr_set, lsquic_stream_t *s,
1281    const  struct iovec* url, const struct iovec* authority,
1282    const lsquic_http_headers_t *headers);
1283
1284/**
1285 * Only makes sense in server mode: the client cannot push a stream and this
1286 * function always returns false in client mode.
1287 */
1288int
1289lsquic_conn_is_push_enabled (lsquic_conn_t *);
1290
1291/** Possible values for how are 0, 1, and 2.  See shutdown(2). */
1292int lsquic_stream_shutdown(lsquic_stream_t *s, int how);
1293
1294int lsquic_stream_close(lsquic_stream_t *s);
1295
1296/**
1297 * Get certificate chain returned by the server.  This can be used for
1298 * server certificate verification.
1299 *
1300 * If server certificate cannot be verified, the connection can be closed
1301 * using lsquic_conn_cert_verification_failed().
1302 *
1303 * The caller releases the stack using sk_X509_free().
1304 */
1305struct stack_st_X509 *
1306lsquic_conn_get_server_cert_chain (lsquic_conn_t *);
1307
1308/** Returns ID of the stream */
1309lsquic_stream_id_t
1310lsquic_stream_id (const lsquic_stream_t *s);
1311
1312/**
1313 * Returns stream ctx associated with the stream.  (The context is what
1314 * is returned by @ref on_new_stream callback).
1315 */
1316lsquic_stream_ctx_t *
1317lsquic_stream_get_ctx (const lsquic_stream_t *s);
1318
1319/** Returns true if this is a pushed stream */
1320int
1321lsquic_stream_is_pushed (const lsquic_stream_t *s);
1322
1323/**
1324 * Returns true if this stream was rejected, false otherwise.  Use this as
1325 * an aid to distinguish between errors.
1326 */
1327int
1328lsquic_stream_is_rejected (const lsquic_stream_t *s);
1329
1330/**
1331 * Refuse pushed stream.  Call it from @ref on_new_stream.
1332 *
1333 * No need to call lsquic_stream_close() after this.  on_close will be called.
1334 *
1335 * @see lsquic_stream_is_pushed
1336 */
1337int
1338lsquic_stream_refuse_push (lsquic_stream_t *s);
1339
1340/**
1341 * Get information associated with pushed stream:
1342 *
1343 * @param ref_stream_id   Stream ID in response to which push promise was
1344 *                            sent.
1345 * @param hdr_set         Header set.  This object was passed to or generated
1346 *                            by @ref lsquic_conn_push_stream().
1347 *
1348 * @retval   0  Success.
1349 * @retval  -1  This is not a pushed stream.
1350 */
1351int
1352lsquic_stream_push_info (const lsquic_stream_t *,
1353                         lsquic_stream_id_t *ref_stream_id, void **hdr_set);
1354
1355/** Return current priority of the stream */
1356unsigned lsquic_stream_priority (const lsquic_stream_t *s);
1357
1358/**
1359 * Set stream priority.  Valid priority values are 1 through 256, inclusive.
1360 *
1361 * @retval   0  Success.
1362 * @retval  -1  Priority value is invalid.
1363 */
1364int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority);
1365
1366/**
1367 * Get a pointer to the connection object.  Use it with lsquic_conn_*
1368 * functions.
1369 */
1370lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s);
1371
1372lsquic_stream_t *
1373lsquic_conn_get_stream_by_id (lsquic_conn_t *c, lsquic_stream_id_t stream_id);
1374
1375/** Get connection ID */
1376const lsquic_cid_t *
1377lsquic_conn_id (const lsquic_conn_t *c);
1378
1379/** Get pointer to the engine */
1380lsquic_engine_t *
1381lsquic_conn_get_engine (lsquic_conn_t *c);
1382
1383int
1384lsquic_conn_get_sockaddr(lsquic_conn_t *c,
1385                const struct sockaddr **local, const struct sockaddr **peer);
1386
1387struct lsquic_logger_if {
1388    int     (*log_buf)(void *logger_ctx, const char *buf, size_t len);
1389};
1390
1391/**
1392 * Enumerate timestamp styles supported by LSQUIC logger mechanism.
1393 */
1394enum lsquic_logger_timestamp_style {
1395    /**
1396     * No timestamp is generated.
1397     */
1398    LLTS_NONE,
1399
1400    /**
1401     * The timestamp consists of 24 hours, minutes, seconds, and
1402     * milliseconds.  Example: 13:43:46.671
1403     */
1404    LLTS_HHMMSSMS,
1405
1406    /**
1407     * Like above, plus date, e.g: 2017-03-21 13:43:46.671
1408     */
1409    LLTS_YYYYMMDD_HHMMSSMS,
1410
1411    /**
1412     * This is Chrome-like timestamp used by proto-quic.  The timestamp
1413     * includes month, date, hours, minutes, seconds, and microseconds.
1414     *
1415     * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
1416     *
1417     * This is to facilitate reading two logs side-by-side.
1418     */
1419    LLTS_CHROMELIKE,
1420
1421    /**
1422     * The timestamp consists of 24 hours, minutes, seconds, and
1423     * microseconds.  Example: 13:43:46.671123
1424     */
1425    LLTS_HHMMSSUS,
1426
1427    /**
1428     * Date and time using microsecond resolution,
1429     * e.g: 2017-03-21 13:43:46.671123
1430     */
1431    LLTS_YYYYMMDD_HHMMSSUS,
1432
1433    N_LLTS
1434};
1435
1436/**
1437 * Call this if you want to do something with LSQUIC log messages, as they
1438 * are thrown out by default.
1439 */
1440void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx,
1441                        enum lsquic_logger_timestamp_style);
1442
1443/**
1444 * Set log level for all LSQUIC modules.  Acceptable values are debug, info,
1445 * notice, warning, error, alert, emerg, crit (case-insensitive).
1446 *
1447 * @retval  0   Success.
1448 * @retval -1   Failure: log_level is not valid.
1449 */
1450int
1451lsquic_set_log_level (const char *log_level);
1452
1453/**
1454 * E.g. "event=debug"
1455 */
1456int
1457lsquic_logger_lopt (const char *optarg);
1458
1459/**
1460 * Return the list of QUIC versions (as bitmask) this engine instance
1461 * supports.
1462 */
1463unsigned lsquic_engine_quic_versions (const lsquic_engine_t *);
1464
1465/**
1466 * This is one of the flags that can be passed to @ref lsquic_global_init.
1467 * Use it to initialize LSQUIC for use in client mode.
1468 */
1469#define LSQUIC_GLOBAL_CLIENT (1 << 0)
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 server mode.
1474 */
1475#define LSQUIC_GLOBAL_SERVER (1 << 1)
1476
1477/**
1478 * Initialize LSQUIC.  This must be called before any other LSQUIC function
1479 * is called.  Returns 0 on success and -1 on failure.
1480 *
1481 * @param flags     This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and
1482 *                    @ref LSQUIC_GLOBAL_SERVER.  At least one of these
1483 *                    flags should be specified.
1484 *
1485 * @retval  0   Success.
1486 * @retval -1   Initialization failed.
1487 *
1488 * @see LSQUIC_GLOBAL_CLIENT
1489 * @see LSQUIC_GLOBAL_SERVER
1490 */
1491int
1492lsquic_global_init (int flags);
1493
1494/**
1495 * Clean up global state created by @ref lsquic_global_init.  Should be
1496 * called after all LSQUIC engine instances are gone.
1497 */
1498void
1499lsquic_global_cleanup (void);
1500
1501/**
1502 * Get QUIC version used by the connection.
1503 *
1504 * @see lsquic_version
1505 */
1506enum lsquic_version
1507lsquic_conn_quic_version (const lsquic_conn_t *c);
1508
1509/* Return keysize or -1 on error */
1510int
1511lsquic_conn_crypto_keysize (const lsquic_conn_t *c);
1512
1513/* Return algorithm keysize or -1 on error */
1514int
1515lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *c);
1516
1517enum lsquic_crypto_ver
1518{
1519    LSQ_CRY_QUIC,
1520    LSQ_CRY_TLSv13,
1521};
1522
1523enum lsquic_crypto_ver
1524lsquic_conn_crypto_ver (const lsquic_conn_t *c);
1525
1526/* Return cipher or NULL on error */
1527const char *
1528lsquic_conn_crypto_cipher (const lsquic_conn_t *c);
1529
1530/** Translate string QUIC version to LSQUIC QUIC version representation */
1531enum lsquic_version
1532lsquic_str2ver (const char *str, size_t len);
1533
1534/** Translate ALPN (e.g. "h3", "h3-23", "h3-Q046") to LSQUIC enum */
1535enum lsquic_version
1536lsquic_alpn2ver (const char *alpn, size_t len);
1537
1538/**
1539 * This function closes all mini connections and marks all full connections
1540 * as going away.  In server mode, this also causes the engine to stop
1541 * creating new connections.
1542 */
1543void
1544lsquic_engine_cooldown (lsquic_engine_t *);
1545
1546struct ssl_st *
1547lsquic_hsk_getssl(lsquic_conn_t *conn);
1548
1549/**
1550 * Get user-supplied context associated with the connection.
1551 */
1552lsquic_conn_ctx_t *
1553lsquic_conn_get_ctx (const lsquic_conn_t *);
1554
1555/**
1556 * Set user-supplied context associated with the connection.
1557 */
1558void
1559lsquic_conn_set_ctx (lsquic_conn_t *, lsquic_conn_ctx_t *);
1560
1561/**
1562 * Get peer context associated with the connection.
1563 */
1564void *
1565lsquic_conn_get_peer_ctx (lsquic_conn_t *, const struct sockaddr *local_sa);
1566
1567/**
1568 * Abort connection.
1569 */
1570void
1571lsquic_conn_abort (lsquic_conn_t *);
1572
1573/**
1574 * Helper function: convert list of versions as specified in the argument
1575 * bitmask to string that can be included as argument to "v=" part of the
1576 * Alt-Svc header.
1577 *
1578 * For example (1<<LSQVER_037)|(1<<LSQVER_038) => "37,38"
1579 *
1580 * This is only applicable to Google QUIC versions.
1581 */
1582const char *
1583lsquic_get_alt_svc_versions (unsigned versions);
1584
1585/**
1586 * Return a NULL-terminated list of HTTP/3 ALPNs, e.g "h3-17", "h3-18", "h3".
1587 */
1588const char *const *
1589lsquic_get_h3_alpns (unsigned versions);
1590
1591/**
1592 * Returns true if provided buffer could be a valid handshake-stage packet,
1593 * false otherwise.  Do not call this function if a connection has already
1594 * been established: it will return incorrect result.
1595 */
1596int
1597lsquic_is_valid_hs_packet (lsquic_engine_t *, const unsigned char *, size_t);
1598
1599/**
1600 * Parse cid from packet stored in `buf' and store it to `cid'.  Returns 0
1601 * on success and -1 on failure.
1602 */
1603int
1604lsquic_cid_from_packet (const unsigned char *, size_t bufsz, lsquic_cid_t *cid);
1605
1606/**
1607 * Returns true if there are connections to be processed, false otherwise.
1608 * If true, `diff' is set to the difference between the earliest advisory
1609 * tick time and now.  If the former is in the past, the value of `diff'
1610 * is negative.
1611 */
1612int
1613lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff);
1614
1615/**
1616 * Return number of connections whose advisory tick time is before current
1617 * time plus `from_now' microseconds from now.  `from_now' can be negative.
1618 */
1619unsigned
1620lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now);
1621
1622enum LSQUIC_CONN_STATUS
1623{
1624    LSCONN_ST_HSK_IN_PROGRESS,
1625    LSCONN_ST_CONNECTED,
1626    LSCONN_ST_HSK_FAILURE,
1627    LSCONN_ST_GOING_AWAY,
1628    LSCONN_ST_TIMED_OUT,
1629    /* If es_honor_prst is not set, the connection will never get public
1630     * reset packets and this flag will not be set.
1631     */
1632    LSCONN_ST_RESET,
1633    LSCONN_ST_USER_ABORTED,
1634    LSCONN_ST_ERROR,
1635    LSCONN_ST_CLOSED,
1636    LSCONN_ST_PEER_GOING_AWAY,
1637};
1638
1639enum LSQUIC_CONN_STATUS
1640lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz);
1641
1642extern const char *const
1643lsquic_ver2str[N_LSQVER];
1644
1645#ifdef __cplusplus
1646}
1647#endif
1648
1649#endif //__LSQUIC_H__
1650
1651