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