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