lsquic.h revision 461e84d8
1/* Copyright (c) 2017 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 <sys/types.h>
16#include <time.h>
17#else
18#include <vc_compat.h>
19#endif
20
21struct iovec;
22struct sockaddr;
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/**
29 * Engine flags:
30 */
31
32/** Server mode */
33#define LSENG_SERVER (1 << 0)
34
35/** Treat stream 3 as headers stream and, in general, behave like the
36 *  regular QUIC.
37 */
38#define LSENG_HTTP  (1 << 1)
39
40#define LSENG_HTTP_SERVER (LSENG_SERVER|LSENG_HTTP)
41
42/**
43 * This is a list of QUIC versions that we know of.  List of supported
44 * versions is in LSQUIC_SUPPORTED_VERSIONS.
45 */
46enum lsquic_version
47{
48
49    /** Q035.  This is the first version to be supported by LSQUIC. */
50    LSQVER_035,
51
52    /**
53     * Q037.  This version is like Q035, except the way packet hashes are
54     * generated is different for clients and servers.  In addition, new
55     * option NSTP (no STOP_WAITING frames) is rumored to be supported at
56     * some point in the future.
57     */
58    LSQVER_037,
59
60    /**
61     * Q038.  Based on Q037, supports PADDING frames in the middle of packet
62     * and NSTP (no STOP_WAITING frames) option.
63     */
64    LSQVER_038,
65
66    /**
67     * Q039.  Switch to big endian.  Do not ack acks.  Send connection level
68     * WINDOW_UPDATE frame every 20 sent packets which do not contain
69     * retransmittable frames.
70     */
71    LSQVER_039,
72
73    /**
74     * Q041.  RST_STREAM, ACK and STREAM frames match IETF format.
75     */
76    LSQVER_041,
77
78    N_LSQVER
79};
80
81/**
82 * We currently support versions 35, 37, 38, 39, and 41.
83 * @see lsquic_version
84 */
85#define LSQUIC_SUPPORTED_VERSIONS ((1 << LSQVER_035) | (1 << LSQVER_037) | \
86                    (1 << LSQVER_038) | (1 << LSQVER_039) | (1 << LSQVER_041))
87
88#define LSQUIC_EXPERIMENTAL_VERSIONS ((1 << LSQVER_041))
89
90/**
91 * @struct lsquic_stream_if
92 * @brief The definition of callback functions call by lsquic_stream to
93 * process events.
94 *
95 */
96struct lsquic_stream_if {
97
98    /**
99     * Use @ref lsquic_conn_get_ctx to get back the context.  It is
100     * OK for this function to return NULL.
101     */
102    lsquic_conn_ctx_t *(*on_new_conn)(void *stream_if_ctx,
103                                                        lsquic_conn_t *c);
104
105    /** This is called when our side received GOAWAY frame.  After this,
106     *  new streams should not be created.  The callback is optional.
107     */
108    void (*on_goaway_received)(lsquic_conn_t *c);
109    void (*on_conn_closed)(lsquic_conn_t *c);
110
111    /** If you need to initiate a connection, call lsquic_conn_make_stream().
112     *  This will cause `on_new_stream' callback to be called when appropriate
113     *  (this operation is delayed when maximum number of outgoing streams is
114     *  reached).
115     *
116     *  After `on_close' is called, the stream is no longer accessible.
117     */
118    lsquic_stream_ctx_t *
119         (*on_new_stream)(void *stream_if_ctx, lsquic_stream_t *s);
120
121    void (*on_read)     (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
122    void (*on_write)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
123    void (*on_close)    (lsquic_stream_t *s, lsquic_stream_ctx_t *h);
124};
125
126/**
127 * Minimum flow control window is set to 16 KB for both client and server.
128 * This means we can send up to this amount of data before handshake gets
129 * completed.
130 */
131#define      LSQUIC_MIN_FCW             (16 * 1024)
132
133/* Each LSQUIC_DF_* value corresponds to es_* entry in
134 * lsquic_engine_settings below.
135 */
136
137/**
138 * By default, experimental versions are not included.
139 */
140#define LSQUIC_DF_VERSIONS         (LSQUIC_SUPPORTED_VERSIONS & \
141                                            ~LSQUIC_EXPERIMENTAL_VERSIONS)
142
143#define LSQUIC_DF_CFCW_SERVER      (3 * 1024 * 1024 / 2)
144#define LSQUIC_DF_CFCW_CLIENT      (15 * 1024 * 1024)
145#define LSQUIC_DF_SFCW_SERVER      (1 * 1024 * 1024)
146#define LSQUIC_DF_SFCW_CLIENT      (6 * 1024 * 1024)
147#define LSQUIC_DF_MAX_STREAMS_IN   100
148
149/**
150 * Default handshake timeout in microseconds.
151 */
152#define LSQUIC_DF_HANDSHAKE_TO     (10 * 1000 * 1000)
153
154#define LSQUIC_DF_IDLE_CONN_TO     (30 * 1000 * 1000)
155#define LSQUIC_DF_SILENT_CLOSE     1
156
157/** Default value of maximum header list size.  If set to non-zero value,
158 *  SETTINGS_MAX_HEADER_LIST_SIZE will be sent to peer after handshake is
159 *  completed (assuming the peer supports this setting frame type).
160 */
161#define LSQUIC_DF_MAX_HEADER_LIST_SIZE 0
162
163/** Default value of UAID (user-agent ID). */
164#define LSQUIC_DF_UA               "LSQUIC"
165
166#define LSQUIC_DF_STTL               86400
167#define LSQUIC_DF_MAX_INCHOATE     (1 * 1000 * 1000)
168#define LSQUIC_DF_SUPPORT_SREJ_SERVER  1
169#define LSQUIC_DF_SUPPORT_SREJ_CLIENT  0       /* TODO: client support */
170/** Do not use NSTP by default */
171#define LSQUIC_DF_SUPPORT_NSTP     0
172#define LSQUIC_DF_SUPPORT_PUSH         1
173#define LSQUIC_DF_SUPPORT_TCID0    1
174/** By default, LSQUIC ignores Public Reset packets. */
175#define LSQUIC_DF_HONOR_PRST       0
176
177/** By default, infinite loop checks are turned on */
178#define LSQUIC_DF_PROGRESS_CHECK    1000
179
180/** By default, Pending RW Queue infinite loop checks are turned on: */
181#define LSQUIC_DF_PENDRW_CHECK      10
182
183/** By default, read/write events are dispatched in a loop */
184#define LSQUIC_DF_RW_ONCE           0
185
186/** By default, the threshold is not enabled */
187#define LSQUIC_DF_PROC_TIME_THRESH  0
188
189/** By default, packets are paced */
190#define LSQUIC_DF_PACE_PACKETS      1
191
192struct lsquic_engine_settings {
193    /**
194     * This is a bit mask wherein each bit corresponds to a value in
195     * enum lsquic_version.  Client starts negotiating with the highest
196     * version and goes down.  Server supports either of the versions
197     * specified here.
198     *
199     * @see lsquic_version
200     */
201    unsigned        es_versions;
202
203    /**
204     * Initial default CFCW.
205     *
206     * In server mode, per-connection values may be set lower than
207     * this if resources are scarce.
208     *
209     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
210     *
211     * @see es_max_cfcw
212     */
213    unsigned        es_cfcw;
214
215    /**
216     * Initial default SFCW.
217     *
218     * In server mode, per-connection values may be set lower than
219     * this if resources are scarce.
220     *
221     * Do not set es_cfcw and es_sfcw lower than @ref LSQUIC_MIN_FCW.
222     *
223     * @see es_max_sfcw
224     */
225    unsigned        es_sfcw;
226
227    /**
228     * This value is used to specify maximum allowed value CFCW is allowed
229     * to reach due to window auto-tuning.  By default, this value is zero,
230     * which means that CFCW is not allowed to increase from its initial
231     * value.
232     *
233     * @see es_cfcw
234     */
235    unsigned        es_max_cfcw;
236
237    unsigned        es_max_sfcw;
238
239    /** MIDS */
240    unsigned        es_max_streams_in;
241
242    /**
243     * Handshake timeout in microseconds.
244     *
245     * For client, this can be set to an arbitrary value (zero turns the
246     * timeout off).
247     *
248     */
249    unsigned long   es_handshake_to;
250
251    /** ICSL in microseconds */
252    unsigned long   es_idle_conn_to;
253
254    /** SCLS (silent close) */
255    int             es_silent_close;
256
257    /**
258     * This corresponds to SETTINGS_MAX_HEADER_LIST_SIZE
259     * (RFC 7540, Section 6.5.2).  0 means no limit.  Defaults
260     * to @ref LSQUIC_DF_MAX_HEADER_LIST_SIZE.
261     */
262    unsigned        es_max_header_list_size;
263
264    /** UAID -- User-Agent ID.  Defaults to @ref LSQUIC_DF_UA. */
265    const char     *es_ua;
266
267    uint32_t        es_pdmd; /* One fixed value X509 */
268    uint32_t        es_aead; /* One fixed value AESG */
269    uint32_t        es_kexs; /* One fixed value C255 */
270
271    /**
272     * Support SREJ: for client side, this means supporting server's SREJ
273     * responses (this does not work yet) and for server side, this means
274     * generating SREJ instead of REJ when appropriate.
275     */
276    int             es_support_srej;
277
278    /**
279     * Setting this value to 0 means that
280     *
281     * For client:
282     *  a) we send a SETTINGS frame to indicate that we do not support server
283     *     push; and
284     *  b) All incoming pushed streams get reset immediately.
285     * (For maximum effect, set es_max_streams_in to 0.)
286     *
287     */
288    int             es_support_push;
289
290    /**
291     * If set to true value, the server will not include connection ID in
292     * outgoing packets if client's CHLO specifies TCID=0.
293     *
294     * For client, this means including TCID=0 into CHLO message.  TODO:
295     * this does not work yet.
296     */
297    int             es_support_tcid0;
298
299    /**
300     * Q037 and higher support "No STOP_WAITING frame" mode.  When set, the
301     * client will send NSTP option in its Client Hello message and will not
302     * sent STOP_WAITING frames, while ignoring incoming STOP_WAITING frames,
303     * if any.  Note that if the version negotiation happens to downgrade the
304     * client below Q037, this mode will *not* be used.
305     *
306     * This option does not affect the server, as it must support NSTP mode
307     * if it was specified by the client.
308     */
309    int             es_support_nstp;
310
311    /**
312     * If set to true value, the library will drop connections when it
313     * receives corresponding Public Reset packet.  The default is to
314     * ignore these packets.
315     */
316    int             es_honor_prst;
317
318    /**
319     * A non-zero value enables internal checks that identify suspected
320     * infinite loops in user @ref on_read and @ref on_write callbacks
321     * and break them.  An infinite loop may occur if user code keeps
322     * on performing the same operation without checking status, e.g.
323     * reading from a closed stream etc.
324     *
325     * The value of this parameter is as follows: should a callback return
326     * this number of times in a row without making progress (that is,
327     * reading, writing, or changing stream state), loop break will occur.
328     *
329     * The defaut value is @ref LSQUIC_DF_PROGRESS_CHECK.
330     */
331    unsigned        es_progress_check;
332
333    /**
334     * A non-zero value enables internal checks to identify suspected
335     * infinite loops in Pending RW Queue logic.  The value of this
336     * setting is the number of times a connection on Pending RW Queue
337     * is allowed to be processed without making progress before it is
338     * banished from Pending RW Queue.
339     *
340     * Progress is considered to have happened if any of the following
341     * occurs:
342     *   - User reads data, FIN, or new error (due to a reset) from a
343     *     stream.
344     *   - A new stream-related frame is packetized.
345     *
346     * The defaut value is @ref LSQUIC_DF_PENDRW_CHECK.
347     */
348    unsigned        es_pendrw_check;
349
350    /**
351     * A non-zero value make stream dispatch its read-write events once
352     * per call.
353     *
354     * When zero, read and write events are dispatched until the stream
355     * is no longer readable or writeable, respectively, or until the
356     * user signals unwillingness to read or write using
357     * @ref lsquic_stream_wantread() or @ref lsquic_stream_wantwrite()
358     * or shuts down the stream.
359     *
360     * The default value is @ref LSQUIC_DF_RW_ONCE.
361     */
362    int             es_rw_once;
363
364    /**
365     * If set, this value specifies that number of microseconds that
366     * functions @ref lsquic_engine_proc_all(),
367     * @ref lsquic_engine_process_conns_with_incoming(),
368     * @ref lsquic_engine_process_conns_to_tick(), and
369     * @ref lsquic_engine_process_conns_with_pend_rw() are allowed
370     * to spend before returning.
371     *
372     * This is not an exact science and the connections must make
373     * progress, so the deadline is checked after all connections get
374     * a chance to tick and at least one batch of packets is sent out.
375     *
376     * When processing function runs out of its time slice, immediate
377     * calls to @ref lsquic_engine_has_pend_rw() and
378     * @ref lsquic_engine_has_unsent_packets() return false.
379     *
380     * The default value is @ref LSQUIC_DF_PROC_TIME_THRESH.
381     */
382    unsigned        es_proc_time_thresh;
383
384    /**
385     * If set to true, packet pacing is implemented per connection.
386     *
387     * The default value is @ref LSQUIC_DF_PACE_PACKETS.
388     */
389    int             es_pace_packets;
390
391};
392
393/* Initialize `settings' to default values */
394void
395lsquic_engine_init_settings (struct lsquic_engine_settings *,
396                             unsigned lsquic_engine_flags);
397
398/**
399 * Check settings for errors.
400 *
401 * @param   settings    Settings struct.
402 *
403 * @param   flags       Engine flags.
404 *
405 * @param   err_buf     Optional pointer to buffer into which error string
406 *                      is written.
407
408 * @param   err_buf_sz  Size of err_buf.  No more than this number of bytes
409 *                      will be written to err_buf, including the NUL byte.
410 *
411 * @retval  0   Settings have no errors.
412 * @retval -1   There are errors in settings.
413 */
414int
415lsquic_engine_check_settings (const struct lsquic_engine_settings *settings,
416                              unsigned lsquic_engine_flags,
417                              char *err_buf, size_t err_buf_sz);
418
419struct lsquic_out_spec
420{
421    const unsigned char   *buf;
422    size_t                 sz;
423    const struct sockaddr *local_sa;
424    const struct sockaddr *dest_sa;
425    void                  *peer_ctx;
426};
427
428/**
429 * Returns number of packets successfully sent out or -1 on error.  -1 should
430 * only be returned if no packets were sent out.
431 */
432typedef int (*lsquic_packets_out_f)(
433    void                          *packets_out_ctx,
434    const struct lsquic_out_spec  *out_spec,
435    unsigned                       n_packets_out
436);
437
438/**
439 * The packet out memory interface is used by LSQUIC to get buffers to
440 * which outgoing packets will be written before they are passed to
441 * ea_packets_out callback.  pmi_release() is called at some point,
442 * usually after the packet is sent successfully, to return the buffer
443 * to the pool.
444 *
445 * If not specified, malloc() and free() are used.
446 */
447struct lsquic_packout_mem_if
448{
449    void *  (*pmi_allocate) (void *pmi_ctx, size_t sz);
450    void    (*pmi_release)  (void *pmi_ctx, void *obj);
451};
452
453/* TODO: describe this important data structure */
454typedef struct lsquic_engine_api
455{
456    const struct lsquic_engine_settings *ea_settings;   /* Optional */
457    const struct lsquic_stream_if       *ea_stream_if;
458    void                                *ea_stream_if_ctx;
459    lsquic_packets_out_f                 ea_packets_out;
460    void                                *ea_packets_out_ctx;
461    /**
462     * Memory interface is optional.
463     */
464    const struct lsquic_packout_mem_if  *ea_pmi;
465    void                                *ea_pmi_ctx;
466} lsquic_engine_api_t;
467
468/**
469 * Create new engine.
470 *
471 * @param   lsquic_engine_flags     A bitmask of @ref LSENG_SERVER and
472 *                                  @ref LSENG_HTTP
473 */
474lsquic_engine_t *
475lsquic_engine_new (unsigned lsquic_engine_flags,
476                   const struct lsquic_engine_api *);
477
478/**
479 * Create a client connection to peer identified by `peer_ctx'.
480 * If `max_packet_size' is set to zero, it is inferred based on `peer_sa':
481 * 1350 for IPv6 and 1370 for IPv4.
482 */
483lsquic_conn_t *
484lsquic_engine_connect (lsquic_engine_t *, const struct sockaddr *peer_sa,
485                       void *peer_ctx, lsquic_conn_ctx_t *conn_ctx,
486                       const char *hostname, unsigned short max_packet_size);
487
488/**
489 * Pass incoming packet to the QUIC engine.  This function can be called
490 * more than once in a row.  After you add one or more packets, call
491 * lsquic_engine_process_conns_with_incoming() to schedule output, if any.
492 *
493 * @retval  0   Packet was processed by a real connection.
494 *
495 * @retval -1   Some error occurred.  Possible reasons are invalid packet
496 *              size or failure to allocate memory.
497 */
498int
499lsquic_engine_packet_in (lsquic_engine_t *,
500        const unsigned char *packet_in_data, size_t packet_in_size,
501        const struct sockaddr *sa_local, const struct sockaddr *sa_peer,
502        void *peer_ctx);
503
504/**
505 * Process all connections.  This function must be called often enough so
506 * that packets and connections do not expire.
507 */
508void
509lsquic_engine_proc_all (lsquic_engine_t *engine);
510
511/**
512 * Process connections that have incoming packets.  Call this after adding
513 * one or more incoming packets using lsquic_engine_packet_in().
514 */
515void
516lsquic_engine_process_conns_with_incoming (lsquic_engine_t *);
517
518/**
519 * Process connections in Advisory Tick Time queue whose tick times are in
520 * the past.
521 */
522void
523lsquic_engine_process_conns_to_tick (lsquic_engine_t *);
524
525/**
526 * Returns true if engine has connections that have pending read or write
527 * events.
528 *
529 * Connections with pending read or write events are those that have at
530 * least one stream whose state changed outside of the regular callback
531 * mechanism.  The simplest example is writing directly to the stream
532 * object when data comes in.
533 *
534 * A call to @ref lsquic_engine_proc_all,
535 * @ref lsquic_engine_process_conns_with_incoming,
536 * @ref lsquic_engine_process_conns_to_tick, or
537 * @ref lsquic_engine_process_conns_with_pend_rw removes processed connection
538 * from Pending RW queue.
539 */
540int
541lsquic_engine_has_pend_rw (lsquic_engine_t *);
542
543/**
544 * Process connections that have pending read or write events (@see
545 * lsquic_engine_has_pend_rw for description).
546 */
547void
548lsquic_engine_process_conns_with_pend_rw (lsquic_engine_t *);
549
550/**
551 * Returns true if engine has some unsent packets.  This happens if
552 * @ref ea_packets_out() could not send everything out.
553 */
554int
555lsquic_engine_has_unsent_packets (lsquic_engine_t *engine);
556
557/**
558 * Send out as many unsent packets as possibe: until we are out of unsent
559 * packets or until @ref ea_packets_out() fails.
560 */
561void
562lsquic_engine_send_unsent_packets (lsquic_engine_t *engine);
563
564void
565lsquic_engine_destroy (lsquic_engine_t *);
566
567void lsquic_conn_make_stream(lsquic_conn_t *);
568
569/** Return number of delayed streams currently pending */
570unsigned
571lsquic_conn_n_pending_streams (const lsquic_conn_t *);
572
573/** Cancel `n' pending streams.  Returns new number of pending streams. */
574unsigned
575lsquic_conn_cancel_pending_streams (lsquic_conn_t *, unsigned n);
576
577/**
578 * Mark connection as going away: send GOAWAY frame and do not accept
579 * any more incoming streams, nor generate streams of our own.
580 */
581void
582lsquic_conn_going_away(lsquic_conn_t *conn);
583
584/**
585 * This forces connection close.  on_conn_closed and on_close callbacks
586 * will be called.
587 */
588void lsquic_conn_close(lsquic_conn_t *conn);
589
590int lsquic_stream_wantread(lsquic_stream_t *s, int is_want);
591ssize_t lsquic_stream_read(lsquic_stream_t *s, void *buf, size_t len);
592ssize_t lsquic_stream_readv(lsquic_stream_t *s, const struct iovec *,
593                                                            int iovcnt);
594
595int lsquic_stream_wantwrite(lsquic_stream_t *s, int is_want);
596
597/**
598 * Write `len' bytes to the stream.  Returns number of bytes written, which
599 * may be smaller that `len'.
600 */
601ssize_t lsquic_stream_write(lsquic_stream_t *s, const void *buf, size_t len);
602
603ssize_t lsquic_stream_writev(lsquic_stream_t *s, const struct iovec *vec, int count);
604
605/**
606 * Used as argument to @ref lsquic_stream_writef()
607 */
608struct lsquic_reader
609{
610    /**
611     * Not a ssize_t because the read function is not supposed to return
612     * an error.  If an error occurs in the read function (for example, when
613     * reading from a file fails), it is supposed to deal with the error
614     * itself.
615     */
616    size_t (*lsqr_read) (void *lsqr_ctx, void *buf, size_t count);
617    /**
618     * Return number of bytes remaining in the reader.
619     */
620    size_t (*lsqr_size) (void *lsqr_ctx);
621    void    *lsqr_ctx;
622};
623
624/**
625 * Write to stream using @ref lsquic_reader.  This is the most generic of
626 * the write functions -- @ref lsquic_stream_write() and
627 * @ref lsquic_stream_writev() utilize the same mechanism.
628 *
629 * @retval Number of bytes written or -1 on error.
630 */
631ssize_t
632lsquic_stream_writef (lsquic_stream_t *, struct lsquic_reader *);
633
634/**
635 * Flush any buffered data.  This triggers packetizing even a single byte
636 * into a separate frame.  Flushing a closed stream is an error.
637 *
638 * @retval  0   Success
639 * @retval -1   Failure
640 */
641int
642lsquic_stream_flush (lsquic_stream_t *s);
643
644/**
645 * @typedef lsquic_http_header_t
646 * @brief HTTP header structure. Contains header name and value.
647 *
648 */
649typedef struct lsquic_http_header
650{
651   struct iovec name;
652   struct iovec value;
653} lsquic_http_header_t;
654
655/**
656 * @typedef lsquic_http_headers_t
657 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs.
658 * used in API functions to pass headers.
659 */
660struct lsquic_http_headers
661{
662    int                     count;
663    lsquic_http_header_t   *headers;
664};
665
666int lsquic_stream_send_headers(lsquic_stream_t *s,
667                               const lsquic_http_headers_t *h, int eos);
668
669int lsquic_conn_is_push_enabled(lsquic_conn_t *c);
670
671/** Possible values for how are 0, 1, and 2.  See shutdown(2). */
672int lsquic_stream_shutdown(lsquic_stream_t *s, int how);
673
674int lsquic_stream_close(lsquic_stream_t *s);
675
676/** Returns ID of the stream */
677uint32_t
678lsquic_stream_id (const lsquic_stream_t *s);
679
680/**
681 * Returns stream ctx associated with the stream.  (The context is what
682 * is returned by @ref on_new_stream callback).
683 */
684lsquic_stream_ctx_t *
685lsquic_stream_get_ctx (const lsquic_stream_t *s);
686
687/** Returns true if this is a pushed stream */
688int
689lsquic_stream_is_pushed (const lsquic_stream_t *s);
690
691/**
692 * Refuse pushed stream.  Call it from @ref on_new_stream.
693 *
694 * No need to call lsquic_stream_close() after this.  on_close will be called.
695 *
696 * @see lsquic_stream_is_pushed
697 */
698int
699lsquic_stream_refuse_push (lsquic_stream_t *s);
700
701/**
702 * Get information associated with pushed stream:
703 *
704 * @param ref_stream_id   Stream ID in response to which push promise was
705 *                            sent.
706 * @param headers         Uncompressed request headers.
707 * @param headers_sz      Size of uncompressed request headers, not counting
708 *                          the NUL byte.
709 *
710 * @retval   0  Success.
711 * @retval  -1  This is not a pushed stream.
712 */
713int
714lsquic_stream_push_info (const lsquic_stream_t *, uint32_t *ref_stream_id,
715                         const char **headers, size_t *headers_sz);
716
717/** Return current priority of the stream */
718unsigned lsquic_stream_priority (const lsquic_stream_t *s);
719
720/**
721 * Set stream priority.  Valid priority values are 1 through 256, inclusive.
722 *
723 * @retval   0  Success.
724 * @retval  -1  Priority value is invalid.
725 */
726int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority);
727
728/**
729 * Get a pointer to the connection object.  Use it with lsquic_conn_*
730 * functions.
731 */
732lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s);
733
734lsquic_stream_t *
735lsquic_conn_get_stream_by_id (lsquic_conn_t *c, uint32_t stream_id);
736
737/** Get connection ID */
738lsquic_cid_t
739lsquic_conn_id (const lsquic_conn_t *c);
740
741/** Get pointer to the engine */
742lsquic_engine_t *
743lsquic_conn_get_engine (lsquic_conn_t *c);
744
745int lsquic_conn_get_sockaddr(const lsquic_conn_t *c,
746                const struct sockaddr **local, const struct sockaddr **peer);
747
748struct lsquic_logger_if {
749    int     (*vprintf)(void *logger_ctx, const char *fmt, va_list args);
750};
751
752/**
753 * Enumerate timestamp styles supported by LSQUIC logger mechanism.
754 */
755enum lsquic_logger_timestamp_style {
756    /**
757     * No timestamp is generated.
758     */
759    LLTS_NONE,
760
761    /**
762     * The timestamp consists of 24 hours, minutes, seconds, and
763     * milliseconds.  Example: 13:43:46.671
764     */
765    LLTS_HHMMSSMS,
766
767    /**
768     * Like above, plus date, e.g: 2017-03-21 13:43:46.671
769     */
770    LLTS_YYYYMMDD_HHMMSSMS,
771
772    /**
773     * This is Chrome-like timestamp used by proto-quic.  The timestamp
774     * includes month, date, hours, minutes, seconds, and microseconds.
775     *
776     * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
777     *
778     * This is to facilitate reading two logs side-by-side.
779     */
780    LLTS_CHROMELIKE,
781
782    /**
783     * The timestamp consists of 24 hours, minutes, seconds, and
784     * microseconds.  Example: 13:43:46.671123
785     */
786    LLTS_HHMMSSUS,
787
788    /**
789     * Date and time using microsecond resolution,
790     * e.g: 2017-03-21 13:43:46.671123
791     */
792    LLTS_YYYYMMDD_HHMMSSUS,
793
794    N_LLTS
795};
796
797/**
798 * Call this if you want to do something with LSQUIC log messages, as they
799 * are thrown out by default.
800 */
801void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx,
802                        enum lsquic_logger_timestamp_style);
803
804/**
805 * Set log level for all LSQUIC modules.  Acceptable values are debug, info,
806 * notice, warning, error, alert, emerg, crit (case-insensitive).
807 *
808 * @retval  0   Success.
809 * @retval -1   Failure: log_level is not valid.
810 */
811int
812lsquic_set_log_level (const char *log_level);
813
814/**
815 * E.g. "event=debug"
816 */
817int
818lsquic_logger_lopt (const char *optarg);
819
820/**
821 * Return the list of QUIC versions (as bitmask) this engine instance
822 * supports.
823 */
824unsigned lsquic_engine_quic_versions (const lsquic_engine_t *);
825
826/**
827 * This is one of the flags that can be passed to @ref lsquic_global_init.
828 * Use it to initialize LSQUIC for use in client mode.
829 */
830#define LSQUIC_GLOBAL_CLIENT (1 << 0)
831
832/**
833 * This is one of the flags that can be passed to @ref lsquic_global_init.
834 * Use it to initialize LSQUIC for use in server mode.
835 */
836#define LSQUIC_GLOBAL_SERVER (1 << 1)
837
838/**
839 * Initialize LSQUIC.  This must be called before any other LSQUIC function
840 * is called.  Returns 0 on success and -1 on failure.
841 *
842 * @param flags     This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and
843 *                    @ref LSQUIC_GLOBAL_SERVER.  At least one of these
844 *                    flags should be specified.
845 *
846 * @retval  0   Success.
847 * @retval -1   Initialization failed.
848 *
849 * @see LSQUIC_GLOBAL_CLIENT
850 * @see LSQUIC_GLOBAL_SERVER
851 */
852int
853lsquic_global_init (int flags);
854
855/**
856 * Clean up global state created by @ref lsquic_global_init.  Should be
857 * called after all LSQUIC engine instances are gone.
858 */
859void
860lsquic_global_cleanup (void);
861
862/**
863 * Get QUIC version used by the connection.
864 *
865 * @see lsquic_version
866 */
867enum lsquic_version
868lsquic_conn_quic_version (const lsquic_conn_t *c);
869
870/** Translate string QUIC version to LSQUIC QUIC version representation */
871enum lsquic_version
872lsquic_str2ver (const char *str, size_t len);
873
874/**
875 * Get user-supplied context associated with the connection.
876 */
877lsquic_conn_ctx_t *
878lsquic_conn_get_ctx (const lsquic_conn_t *c);
879
880/**
881 * Set user-supplied context associated with the connection.
882 */
883void lsquic_conn_set_ctx (lsquic_conn_t *c, lsquic_conn_ctx_t *h);
884
885/**
886 * Get peer context associated with the connection.
887 */
888void *lsquic_conn_get_peer_ctx( const lsquic_conn_t *lconn);
889
890/**
891 * Abort connection.
892 */
893void
894lsquic_conn_abort (lsquic_conn_t *c);
895
896/**
897 * Returns true if there is a connection on the Advisory Tick Time queue,
898 * false otherwise.  If true, `diff' is set to the difference between
899 * the earliest advisory tick time and now.  If the former is in the past,
900 * the value of `diff' is negative.
901 */
902int
903lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff);
904
905/**
906 * Return number of connections whose advisory tick time is before current
907 * time plus `from_now' microseconds from now.  `from_now' can be negative.
908 */
909unsigned
910lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now);
911
912enum LSQUIC_CONN_STATUS
913{
914    LSCONN_ST_HSK_IN_PROGRESS,
915    LSCONN_ST_CONNECTED,
916    LSCONN_ST_HSK_FAILURE,
917    LSCONN_ST_GOING_AWAY,
918    LSCONN_ST_TIMED_OUT,
919    /* If es_honor_prst is not set, the connection will never get public
920     * reset packets and this flag will not be set.
921     */
922    LSCONN_ST_RESET,
923    LSCONN_ST_USER_ABORTED,
924    LSCONN_ST_ERROR,
925    LSCONN_ST_CLOSED,
926};
927
928enum LSQUIC_CONN_STATUS
929lsquic_conn_status (lsquic_conn_t *, char *errbuf, size_t bufsz);
930
931#ifdef __cplusplus
932}
933#endif
934
935#endif //__LSQUIC_H__
936
937