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