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