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