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