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