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