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