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