lsquic.h revision 50aadb33
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     * Q040.  RST_STREAM, ACK and STREAM frames match IETF format.
71     */
72    LSQVER_040,
73
74    N_LSQVER
75};
76
77/**
78 * We currently support versions 35, 37, 38, 39, and 40.
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_040))
83
84#define LSQUIC_EXPERIMENTAL_VERSIONS ((1 << LSQVER_040))
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 */
479int
480lsquic_engine_connect (lsquic_engine_t *, const struct sockaddr *peer_sa,
481                       void *peer_ctx, const char *hostname,
482                       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 * Return maximum number of bytes lsquic_stream_write() will write.  This
595 * call is useful if you don't want to perform your own buffering.
596 */
597size_t lsquic_stream_write_avail (const lsquic_stream_t *s);
598
599/**
600 * Write `len' bytes to the stream.  Returns number of bytes written, which
601 * may be smaller that `len'.  Use lsquic_stream_write_avail() to find out
602 * maximum size of `len'.
603 */
604ssize_t lsquic_stream_write(lsquic_stream_t *s, const void *buf, size_t len);
605
606/**
607 * Returns 0 if `filename' was queued for writing, -1 on error.  This
608 * function queues the size of the file as it was when the function was
609 * called.  The stream will write at most this number of bytes to the
610 * peer.  If the file grows, appended data is not used.
611 */
612int lsquic_stream_write_file(lsquic_stream_t *s, const char *filename);
613
614ssize_t lsquic_stream_writev(lsquic_stream_t *s, const struct iovec *vec, int count);
615
616/**
617 * Returns 0 if `fdSrc' was queued for writing, -1 on error.  This
618 * function queues at most `size' bytes to be written.  If the file shrinks,
619 * fewer bytes are written.
620 */
621int lsquic_stream_sendfile(lsquic_stream_t *s, int fdSrc, off_t off, size_t size);
622
623int lsquic_stream_flush(lsquic_stream_t *s);
624
625/**
626 * @typedef lsquic_http_header_t
627 * @brief HTTP header structure. Contains header name and value.
628 *
629 */
630typedef struct lsquic_http_header
631{
632   struct iovec name;
633   struct iovec value;
634} lsquic_http_header_t;
635
636/**
637 * @typedef lsquic_http_headers_t
638 * @brief HTTP header list structure. Contains a list of HTTP headers in key/value pairs.
639 * used in API functions to pass headers.
640 */
641struct lsquic_http_headers
642{
643    int                     count;
644    lsquic_http_header_t   *headers;
645};
646
647int lsquic_stream_send_headers(lsquic_stream_t *s,
648                               const lsquic_http_headers_t *h, int eos);
649
650int lsquic_conn_is_push_enabled(lsquic_conn_t *c);
651
652/** Possible values for how are 0, 1, and 2.  See shutdown(2). */
653int lsquic_stream_shutdown(lsquic_stream_t *s, int how);
654
655int lsquic_stream_close(lsquic_stream_t *s);
656
657/** Returns ID of the stream */
658uint32_t
659lsquic_stream_id (const lsquic_stream_t *s);
660
661/**
662 * Returns stream ctx associated with the stream.  (The context is what
663 * is returned by @ref on_new_stream callback).
664 */
665lsquic_stream_ctx_t *
666lsquic_stream_get_ctx (const lsquic_stream_t *s);
667
668/** Returns true if this is a pushed stream */
669int
670lsquic_stream_is_pushed (const lsquic_stream_t *s);
671
672/**
673 * Refuse pushed stream.  Call it from @ref on_new_stream.
674 *
675 * No need to call lsquic_stream_close() after this.  on_close will be called.
676 *
677 * @see lsquic_stream_is_pushed
678 */
679int
680lsquic_stream_refuse_push (lsquic_stream_t *s);
681
682/**
683 * Get information associated with pushed stream:
684 *
685 * @param ref_stream_id   Stream ID in response to which push promise was
686 *                            sent.
687 * @param headers         Uncompressed request headers.
688 * @param headers_sz      Size of uncompressed request headers, not counting
689 *                          the NUL byte.
690 *
691 * @retval   0  Success.
692 * @retval  -1  This is not a pushed stream.
693 */
694int
695lsquic_stream_push_info (const lsquic_stream_t *, uint32_t *ref_stream_id,
696                         const char **headers, size_t *headers_sz);
697
698/** Return current priority of the stream */
699unsigned lsquic_stream_priority (const lsquic_stream_t *s);
700
701/**
702 * Set stream priority.  Valid priority values are 1 through 256, inclusive.
703 *
704 * @retval   0  Success.
705 * @retval  -1  Priority value is invalid.
706 */
707int lsquic_stream_set_priority (lsquic_stream_t *s, unsigned priority);
708
709/**
710 * Get a pointer to the connection object.  Use it with lsquic_conn_*
711 * functions.
712 */
713lsquic_conn_t * lsquic_stream_conn(const lsquic_stream_t *s);
714
715lsquic_stream_t *
716lsquic_conn_get_stream_by_id (lsquic_conn_t *c, uint32_t stream_id);
717
718/** Get connection ID */
719lsquic_cid_t
720lsquic_conn_id (const lsquic_conn_t *c);
721
722int lsquic_conn_get_sockaddr(const lsquic_conn_t *c,
723                const struct sockaddr **local, const struct sockaddr **peer);
724
725struct lsquic_logger_if {
726    int     (*vprintf)(void *logger_ctx, const char *fmt, va_list args);
727};
728
729/**
730 * Enumerate timestamp styles supported by LSQUIC logger mechanism.
731 */
732enum lsquic_logger_timestamp_style {
733    /**
734     * No timestamp is generated.
735     */
736    LLTS_NONE,
737
738    /**
739     * The timestamp consists of 24 hours, minutes, seconds, and
740     * milliseconds.  Example: 13:43:46.671
741     */
742    LLTS_HHMMSSMS,
743
744    /**
745     * Like above, plus date, e.g: 2017-03-21 13:43:46.671
746     */
747    LLTS_YYYYMMDD_HHMMSSMS,
748
749    /**
750     * This is Chrome-like timestamp used by proto-quic.  The timestamp
751     * includes month, date, hours, minutes, seconds, and microseconds.
752     *
753     * Example: 1223/104613.946956 (instead of 12/23 10:46:13.946956).
754     *
755     * This is to facilitate reading two logs side-by-side.
756     */
757    LLTS_CHROMELIKE,
758
759    /**
760     * The timestamp consists of 24 hours, minutes, seconds, and
761     * microseconds.  Example: 13:43:46.671123
762     */
763    LLTS_HHMMSSUS,
764
765    N_LLTS
766};
767
768/**
769 * Call this if you want to do something with LSQUIC log messages, as they
770 * are thrown out by default.
771 */
772void lsquic_logger_init(const struct lsquic_logger_if *, void *logger_ctx,
773                        enum lsquic_logger_timestamp_style);
774
775/**
776 * Set log level for all LSQUIC modules.  Acceptable values are debug, info,
777 * notice, warning, error, alert, emerg, crit (case-insensitive).
778 *
779 * @retval  0   Success.
780 * @retval -1   Failure: log_level is not valid.
781 */
782int
783lsquic_set_log_level (const char *log_level);
784
785/**
786 * E.g. "event=debug"
787 */
788int
789lsquic_logger_lopt (const char *optarg);
790
791/**
792 * Return the list of QUIC versions (as bitmask) this engine instance
793 * supports.
794 */
795unsigned lsquic_engine_quic_versions (const lsquic_engine_t *);
796
797/**
798 * This is one of the flags that can be passed to @ref lsquic_global_init.
799 * Use it to initialize LSQUIC for use in client mode.
800 */
801#define LSQUIC_GLOBAL_CLIENT (1 << 0)
802
803/**
804 * This is one of the flags that can be passed to @ref lsquic_global_init.
805 * Use it to initialize LSQUIC for use in server mode.
806 */
807#define LSQUIC_GLOBAL_SERVER (1 << 1)
808
809/**
810 * Initialize LSQUIC.  This must be called before any other LSQUIC function
811 * is called.  Returns 0 on success and -1 on failure.
812 *
813 * @param flags     This a bitmask of @ref LSQUIC_GLOBAL_CLIENT and
814 *                    @ref LSQUIC_GLOBAL_SERVER.  At least one of these
815 *                    flags should be specified.
816 *
817 * @retval  0   Success.
818 * @retval -1   Initialization failed.
819 *
820 * @see LSQUIC_GLOBAL_CLIENT
821 * @see LSQUIC_GLOBAL_SERVER
822 */
823int
824lsquic_global_init (int flags);
825
826/**
827 * Clean up global state created by @ref lsquic_global_init.  Should be
828 * called after all LSQUIC engine instances are gone.
829 */
830void
831lsquic_global_cleanup (void);
832
833/**
834 * Get QUIC version used by the connection.
835 *
836 * @see lsquic_version
837 */
838enum lsquic_version
839lsquic_conn_quic_version (const lsquic_conn_t *c);
840
841/** Translate string QUIC version to LSQUIC QUIC version representation */
842enum lsquic_version
843lsquic_str2ver (const char *str, size_t len);
844
845/**
846 * Get user-supplied context associated with the connection.
847 */
848lsquic_conn_ctx_t *
849lsquic_conn_get_ctx (const lsquic_conn_t *c);
850
851/**
852 * Get peer context associated with the connection. should be UdpListener *.
853 */
854void *lsquic_conn_get_peer_ctx( const lsquic_conn_t *lconn);
855
856/**
857 * Abort connection.
858 */
859void
860lsquic_conn_abort (lsquic_conn_t *c);
861
862/**
863 * Returns true if there is a connection on the Advisory Tick Time queue,
864 * false otherwise.  If true, `diff' is set to the difference between
865 * the earliest advisory tick time and now.  If the former is in the past,
866 * the value of `diff' is negative.
867 */
868int
869lsquic_engine_earliest_adv_tick (lsquic_engine_t *engine, int *diff);
870
871/**
872 * Return number of connections whose advisory tick time is before $now
873 * plus `from_now' delta.  `from_now' can be negative.
874 */
875unsigned
876lsquic_engine_count_attq (lsquic_engine_t *engine, int from_now);
877
878#ifdef __cplusplus
879}
880#endif
881
882#endif //__LSQUIC_H__
883
884