lsquic_parse.h revision 4429f8ea
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#ifndef LSQUIC_PARSE_H
350aadb33SDmitri Tikhonov#define LSQUIC_PARSE_H 1
450aadb33SDmitri Tikhonov
550aadb33SDmitri Tikhonov#include <stdint.h>
650aadb33SDmitri Tikhonov
750aadb33SDmitri Tikhonov#include "lsquic_packet_common.h"
85392f7a3SLiteSpeed Tech#include "lsquic_packet_gquic.h"
950aadb33SDmitri Tikhonov
105392f7a3SLiteSpeed Techstruct lsquic_conn;
1150aadb33SDmitri Tikhonovstruct lsquic_packet_in;
129626cfc2SDmitri Tikhonovstruct lsquic_packet_out;
139626cfc2SDmitri Tikhonovstruct packin_parse_state;
1450aadb33SDmitri Tikhonovstruct stream_frame;
155392f7a3SLiteSpeed Techstruct lsquic_cid;
169626cfc2SDmitri Tikhonovenum packet_out_flags;
175392f7a3SLiteSpeed Techenum lsquic_version;
185392f7a3SLiteSpeed Techenum stream_dir;
1950aadb33SDmitri Tikhonov
2050aadb33SDmitri Tikhonov
215392f7a3SLiteSpeed Techstruct ack_info
2250aadb33SDmitri Tikhonov{
235392f7a3SLiteSpeed Tech    enum packnum_space pns;
245392f7a3SLiteSpeed Tech    enum {
255392f7a3SLiteSpeed Tech        AI_ECN        = 1 << 0, /* ecn_counts[1,2,3] contain ECN counts */
265392f7a3SLiteSpeed Tech        AI_TRUNCATED  = 1 << 1, /* There were more ranges to parse, but we
275392f7a3SLiteSpeed Tech                                 * ran out of elements in `ranges'.
285392f7a3SLiteSpeed Tech                                 */
295392f7a3SLiteSpeed Tech    }               flags;
3050aadb33SDmitri Tikhonov    unsigned    n_ranges;       /* This is at least 1 */
3150aadb33SDmitri Tikhonov                                /* Largest acked is ack_info.ranges[0].high */
3250aadb33SDmitri Tikhonov    lsquic_time_t   lack_delta;
335392f7a3SLiteSpeed Tech    uint64_t        ecn_counts[4];
34bfc7bfd8SDmitri Tikhonov    struct lsquic_packno_range ranges[256];
3516a9b66aSDmitri Tikhonov};
3616a9b66aSDmitri Tikhonov
3750aadb33SDmitri Tikhonov#define largest_acked(acki) (+(acki)->ranges[0].high)
3850aadb33SDmitri Tikhonov
3950aadb33SDmitri Tikhonov#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low)
4050aadb33SDmitri Tikhonov
415392f7a3SLiteSpeed Tech/* Chrome may send an empty ACK frame when it closes a connection.
425392f7a3SLiteSpeed Tech * We do not know why it occurs -- perhaps a bug in Chrome.
435392f7a3SLiteSpeed Tech */
445392f7a3SLiteSpeed Tech/* This macro cannot be used in IETF QUIC as zero is a valid packet number.
455392f7a3SLiteSpeed Tech * Hopefully the Chrome bug will have been fixed by then.
465392f7a3SLiteSpeed Tech */
475392f7a3SLiteSpeed Tech#define empty_ack_frame(acki) (largest_acked(acki) == 0)
485392f7a3SLiteSpeed Tech
4950aadb33SDmitri Tikhonov/* gaf_: generate ACK frame */
5050aadb33SDmitri Tikhonovstruct lsquic_packno_range;
5150aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
5250aadb33SDmitri Tikhonov    (*gaf_rechist_first_f)          (void *rechist);
5350aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
5450aadb33SDmitri Tikhonov    (*gaf_rechist_next_f)           (void *rechist);
5550aadb33SDmitri Tikhonovtypedef lsquic_time_t
5650aadb33SDmitri Tikhonov    (*gaf_rechist_largest_recv_f)   (void *rechist);
5750aadb33SDmitri Tikhonov
5850aadb33SDmitri Tikhonov/* gsf_: generate stream frame */
5950aadb33SDmitri Tikhonovtypedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin);
6050aadb33SDmitri Tikhonov
6150aadb33SDmitri Tikhonov/* This structure contains functions that parse and generate packets and
6250aadb33SDmitri Tikhonov * frames in version-specific manner.  To begin with, there is difference
6350aadb33SDmitri Tikhonov * between GQUIC's little-endian (Q038 and lower) and big-endian formats
645392f7a3SLiteSpeed Tech * (Q039 and higher).  Q046 and higher uses different format for packet headers.
6550aadb33SDmitri Tikhonov */
6650aadb33SDmitri Tikhonovstruct parse_funcs
6750aadb33SDmitri Tikhonov{
6850aadb33SDmitri Tikhonov    /* Return buf length */
6950aadb33SDmitri Tikhonov    int
709626cfc2SDmitri Tikhonov    (*pf_gen_reg_pkt_header) (const struct lsquic_conn *,
7149f1f4f6SDmitri Tikhonov                const struct lsquic_packet_out *, unsigned char *, size_t,
7249f1f4f6SDmitri Tikhonov                /* In Q050 and IETF QUIC, these are set: */
7349f1f4f6SDmitri Tikhonov                unsigned *packno_off, unsigned *packno_len);
7450aadb33SDmitri Tikhonov    void
7550aadb33SDmitri Tikhonov    (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in,
7650aadb33SDmitri Tikhonov                                                struct packin_parse_state *);
776aba801dSDmitri Tikhonov    enum quic_frame_type
78feca77f5SDmitri Tikhonov    (*pf_parse_frame_type) (const unsigned char *, size_t);
7914e3680dSDmitri Tikhonov    /* Return used buffer length or a negative value if there was not enough
8014e3680dSDmitri Tikhonov     * room to write the stream frame.  In the latter case, the negative of
8114e3680dSDmitri Tikhonov     * the negative return value is the number of bytes required.  The
8214e3680dSDmitri Tikhonov     * exception is -1, which is a generic error code, as we always need
8314e3680dSDmitri Tikhonov     * more than 1 byte to write a STREAM frame.
8414e3680dSDmitri Tikhonov     */
85b8fa6195SDmitri Tikhonov    /* pf_gen_stream_frame and pf_gen_crypto_frame must be adjacent so that
86b8fa6195SDmitri Tikhonov     * they can be cast to an array.
87b8fa6195SDmitri Tikhonov     */
8850aadb33SDmitri Tikhonov    int
8950aadb33SDmitri Tikhonov    (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz,
905392f7a3SLiteSpeed Tech                            lsquic_stream_id_t stream_id, uint64_t offset,
91bfc7bfd8SDmitri Tikhonov                            int fin, size_t size, gsf_read_f, void *stream);
92b8fa6195SDmitri Tikhonov    /* The two "UNUSED" parameters are here so that it matches
93b8fa6195SDmitri Tikhonov     * pf_gen_stream_frame.
94b8fa6195SDmitri Tikhonov     */
95b8fa6195SDmitri Tikhonov    int
96b8fa6195SDmitri Tikhonov    (*pf_gen_crypto_frame) (unsigned char *buf, size_t bufsz,
97b8fa6195SDmitri Tikhonov                            lsquic_stream_id_t UNUSED_1, uint64_t offset,
98b8fa6195SDmitri Tikhonov                            int UNUSED_2, size_t size, gsf_read_f, void *stream);
99b8fa6195SDmitri Tikhonov    /* pf_parse_stream_frame and pf_parse_crypto_frame must be adjacent so that
100b8fa6195SDmitri Tikhonov     * they can be cast to an array.
101b8fa6195SDmitri Tikhonov     */
10250aadb33SDmitri Tikhonov    int
10350aadb33SDmitri Tikhonov    (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz,
10450aadb33SDmitri Tikhonov                                                    struct stream_frame *);
10550aadb33SDmitri Tikhonov    int
1065392f7a3SLiteSpeed Tech    (*pf_parse_crypto_frame) (const unsigned char *buf, size_t rem_packet_sz,
1075392f7a3SLiteSpeed Tech                                                    struct stream_frame *);
1082f2f4363SDmitri Tikhonov    /* Return true if STREAM frame extends to the end of the packet and thus
1092f2f4363SDmitri Tikhonov     * does not contain a Length field (no update).
1102f2f4363SDmitri Tikhonov     */
1112f2f4363SDmitri Tikhonov    int
1122f2f4363SDmitri Tikhonov    (*pf_dec_stream_frame_size) (unsigned char *buf, size_t new_size);
1135392f7a3SLiteSpeed Tech    int
11450aadb33SDmitri Tikhonov    (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len,
1155392f7a3SLiteSpeed Tech                                    struct ack_info *ack_info, uint8_t exp);
11650aadb33SDmitri Tikhonov    int
11750aadb33SDmitri Tikhonov    (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz,
11850aadb33SDmitri Tikhonov                gaf_rechist_first_f, gaf_rechist_next_f,
11950aadb33SDmitri Tikhonov                gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now,
1205392f7a3SLiteSpeed Tech                int *has_missing, lsquic_packno_t *largest_received,
1215392f7a3SLiteSpeed Tech                const uint64_t *ecn_counts);
12250aadb33SDmitri Tikhonov    int
12350aadb33SDmitri Tikhonov    (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len,
124c7d81ce1SDmitri Tikhonov                    lsquic_packno_t cur_packno, enum packno_bits,
12550aadb33SDmitri Tikhonov                    lsquic_packno_t least_unacked_packno);
12650aadb33SDmitri Tikhonov    int
12750aadb33SDmitri Tikhonov    (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len,
128c7d81ce1SDmitri Tikhonov                     lsquic_packno_t cur_packno, enum packno_bits,
12950aadb33SDmitri Tikhonov                     lsquic_packno_t *least_unacked);
13050aadb33SDmitri Tikhonov    int
131c7d81ce1SDmitri Tikhonov    (*pf_skip_stop_waiting_frame) (size_t buf_len, enum packno_bits);
13250aadb33SDmitri Tikhonov    int
13350aadb33SDmitri Tikhonov    (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len,
1345392f7a3SLiteSpeed Tech                                lsquic_stream_id_t stream_id, uint64_t offset);
13550aadb33SDmitri Tikhonov    int
13650aadb33SDmitri Tikhonov    (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len,
1375392f7a3SLiteSpeed Tech                              lsquic_stream_id_t *stream_id, uint64_t *offset);
1385392f7a3SLiteSpeed Tech    /* The third argument for pf_gen_blocked_frame() and pf_parse_blocked_frame()
1395392f7a3SLiteSpeed Tech     * is Stream ID for GQUIC and offset for IETF QUIC.  Since both of these are
1405392f7a3SLiteSpeed Tech     * uint64_t, we'll use the same function pointer.  Just have to be a little
1415392f7a3SLiteSpeed Tech     * careful here.
1425392f7a3SLiteSpeed Tech     */
14350aadb33SDmitri Tikhonov    int
14450aadb33SDmitri Tikhonov    (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len,
1455392f7a3SLiteSpeed Tech                                                lsquic_stream_id_t stream_id);
14650aadb33SDmitri Tikhonov    int
14750aadb33SDmitri Tikhonov    (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len,
1485392f7a3SLiteSpeed Tech        /* TODO: rename third argument when dropping GQUIC */
1495392f7a3SLiteSpeed Tech                                                lsquic_stream_id_t *stream_id);
1505392f7a3SLiteSpeed Tech    unsigned
1515392f7a3SLiteSpeed Tech    (*pf_blocked_frame_size) (uint64_t);
1525392f7a3SLiteSpeed Tech    unsigned
1535392f7a3SLiteSpeed Tech    (*pf_rst_frame_size) (lsquic_stream_id_t stream_id, uint64_t final_size,
1545392f7a3SLiteSpeed Tech                                                        uint64_t error_code);
15550aadb33SDmitri Tikhonov    int
1565392f7a3SLiteSpeed Tech    (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len,
1575392f7a3SLiteSpeed Tech        lsquic_stream_id_t stream_id, uint64_t offset, uint64_t error_code);
15850aadb33SDmitri Tikhonov    int
15950aadb33SDmitri Tikhonov    (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len,
1605392f7a3SLiteSpeed Tech        lsquic_stream_id_t *stream_id, uint64_t *offset, uint64_t *error_code);
1615392f7a3SLiteSpeed Tech    int
1625392f7a3SLiteSpeed Tech    (*pf_parse_stop_sending_frame) (const unsigned char *buf, size_t buf_len,
1635392f7a3SLiteSpeed Tech        lsquic_stream_id_t *stream_id, uint64_t *error_code);
1645392f7a3SLiteSpeed Tech    unsigned
1655392f7a3SLiteSpeed Tech    (*pf_stop_sending_frame_size) (lsquic_stream_id_t, uint64_t);
1665392f7a3SLiteSpeed Tech    int
1675392f7a3SLiteSpeed Tech    (*pf_gen_stop_sending_frame) (unsigned char *buf, size_t buf_len,
1685392f7a3SLiteSpeed Tech                                    lsquic_stream_id_t, uint64_t error_code);
1697d09751dSDmitri Tikhonov    size_t
1707d09751dSDmitri Tikhonov    (*pf_connect_close_frame_size) (int app_error, unsigned error_code,
1717d09751dSDmitri Tikhonov                                unsigned frame_type, size_t reason_len);
17250aadb33SDmitri Tikhonov    int
1735392f7a3SLiteSpeed Tech    (*pf_gen_connect_close_frame) (unsigned char *buf, size_t buf_len,
1745392f7a3SLiteSpeed Tech        int app_error, unsigned error_code, const char *reason, int reason_len);
17550aadb33SDmitri Tikhonov    int
17650aadb33SDmitri Tikhonov    (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len,
1775392f7a3SLiteSpeed Tech                int *app_error, uint64_t *error_code, uint16_t *reason_length,
17850aadb33SDmitri Tikhonov                uint8_t *reason_offset);
17950aadb33SDmitri Tikhonov    int
18050aadb33SDmitri Tikhonov    (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len,
1815392f7a3SLiteSpeed Tech                uint32_t error_code, lsquic_stream_id_t last_good_stream_id,
18250aadb33SDmitri Tikhonov                const char *reason, size_t reason_len);
18350aadb33SDmitri Tikhonov    int
18450aadb33SDmitri Tikhonov    (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len,
1855392f7a3SLiteSpeed Tech                uint32_t *error_code, lsquic_stream_id_t *last_good_stream_id,
18650aadb33SDmitri Tikhonov                uint16_t *reason_length, const char **reason);
18750aadb33SDmitri Tikhonov    int
18850aadb33SDmitri Tikhonov    (*pf_gen_ping_frame) (unsigned char *buf, int buf_len);
1895392f7a3SLiteSpeed Tech    int
1905392f7a3SLiteSpeed Tech    (*pf_parse_path_chal_frame) (const unsigned char *buf, size_t,
1915392f7a3SLiteSpeed Tech                                                            uint64_t *chal);
1925392f7a3SLiteSpeed Tech    int
1935392f7a3SLiteSpeed Tech    (*pf_parse_path_resp_frame) (const unsigned char *buf, size_t,
1945392f7a3SLiteSpeed Tech                                                            uint64_t *resp);
195a4e24689SBob Perper#ifndef NDEBUG
19650aadb33SDmitri Tikhonov    /* These float reading and writing functions assume `mem' has at least
19750aadb33SDmitri Tikhonov     * 2 bytes.
19850aadb33SDmitri Tikhonov     */
19950aadb33SDmitri Tikhonov    void
20050aadb33SDmitri Tikhonov    (*pf_write_float_time16) (lsquic_time_t time_us, void *mem);
20150aadb33SDmitri Tikhonov    uint64_t
20250aadb33SDmitri Tikhonov    (*pf_read_float_time16) (const void *mem);
203a4e24689SBob Perper#endif
2045392f7a3SLiteSpeed Tech    ssize_t
2055392f7a3SLiteSpeed Tech    (*pf_generate_simple_prst) (const lsquic_cid_t *cid,
2065392f7a3SLiteSpeed Tech                                                    unsigned char *, size_t);
20750aadb33SDmitri Tikhonov    size_t
2085392f7a3SLiteSpeed Tech    (*pf_calc_stream_frame_header_sz) (lsquic_stream_id_t stream_id,
2095392f7a3SLiteSpeed Tech                                            uint64_t offset, unsigned data_sz);
2105392f7a3SLiteSpeed Tech    size_t
2117a8b2eceSDmitri Tikhonov    (*pf_calc_crypto_frame_header_sz) (uint64_t offset, unsigned data_sz);
212c51ce338SDmitri Tikhonov    void
213c51ce338SDmitri Tikhonov    (*pf_turn_on_fin) (unsigned char *);
2149626cfc2SDmitri Tikhonov
2159626cfc2SDmitri Tikhonov    size_t
2169626cfc2SDmitri Tikhonov    (*pf_packout_size) (const struct lsquic_conn *,
2179626cfc2SDmitri Tikhonov                                            const struct lsquic_packet_out *);
2189626cfc2SDmitri Tikhonov
2195392f7a3SLiteSpeed Tech    /* This returns the high estimate of the header size.  Note that it
2205392f7a3SLiteSpeed Tech     * cannot account for the size of the token in the IETF QUIC Initial
2215392f7a3SLiteSpeed Tech     * packets as it does not know it.
2225392f7a3SLiteSpeed Tech     */
2239626cfc2SDmitri Tikhonov    size_t
2245392f7a3SLiteSpeed Tech    (*pf_packout_max_header_size) (const struct lsquic_conn *,
2254429f8eaSDmitri Tikhonov                    enum packet_out_flags, size_t dcid_len, enum header_type);
226c7d81ce1SDmitri Tikhonov
227c7d81ce1SDmitri Tikhonov    enum packno_bits
228c7d81ce1SDmitri Tikhonov    (*pf_calc_packno_bits) (lsquic_packno_t packno,
229c7d81ce1SDmitri Tikhonov                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
230c7d81ce1SDmitri Tikhonov    unsigned
231c7d81ce1SDmitri Tikhonov    (*pf_packno_bits2len) (enum packno_bits);
2325392f7a3SLiteSpeed Tech
2335392f7a3SLiteSpeed Tech    int
2345392f7a3SLiteSpeed Tech    (*pf_parse_max_data) (const unsigned char *, size_t, uint64_t *);
2355392f7a3SLiteSpeed Tech    int
2365392f7a3SLiteSpeed Tech    (*pf_gen_max_data_frame) (unsigned char *, size_t, uint64_t);
2375392f7a3SLiteSpeed Tech    unsigned
2385392f7a3SLiteSpeed Tech    (*pf_max_data_frame_size) (uint64_t);
23903e6b668SDmitri Tikhonov    /*
24003e6b668SDmitri Tikhonov     * Returns number of bytes parsed on success or negative value on error:
24103e6b668SDmitri Tikhonov     *  -1  Out of input buffer
24203e6b668SDmitri Tikhonov     *  -2  Invalid CID length value
24303e6b668SDmitri Tikhonov     */
2445392f7a3SLiteSpeed Tech    int
2455392f7a3SLiteSpeed Tech    (*pf_parse_new_conn_id) (const unsigned char *, size_t, uint64_t *,
2465392f7a3SLiteSpeed Tech                        uint64_t *, lsquic_cid_t *, const unsigned char **);
2475392f7a3SLiteSpeed Tech    unsigned
2485392f7a3SLiteSpeed Tech    (*pf_stream_blocked_frame_size) (lsquic_stream_id_t, uint64_t);
2495392f7a3SLiteSpeed Tech    int
2505392f7a3SLiteSpeed Tech    (*pf_gen_stream_blocked_frame) (unsigned char *buf, size_t,
2515392f7a3SLiteSpeed Tech                                                lsquic_stream_id_t, uint64_t);
2525392f7a3SLiteSpeed Tech    int
2535392f7a3SLiteSpeed Tech    (*pf_parse_stream_blocked_frame) (const unsigned char *buf, size_t,
2545392f7a3SLiteSpeed Tech                                            lsquic_stream_id_t *, uint64_t *);
2555392f7a3SLiteSpeed Tech    unsigned
2565392f7a3SLiteSpeed Tech    (*pf_max_stream_data_frame_size) (lsquic_stream_id_t, uint64_t);
2575392f7a3SLiteSpeed Tech    int
2585392f7a3SLiteSpeed Tech    (*pf_gen_max_stream_data_frame) (unsigned char *buf, size_t,
2595392f7a3SLiteSpeed Tech                                                lsquic_stream_id_t, uint64_t);
2605392f7a3SLiteSpeed Tech    int
2615392f7a3SLiteSpeed Tech    (*pf_parse_max_stream_data_frame) (const unsigned char *buf, size_t,
2625392f7a3SLiteSpeed Tech                                            lsquic_stream_id_t *, uint64_t *);
2635392f7a3SLiteSpeed Tech    int
2645392f7a3SLiteSpeed Tech    (*pf_parse_new_token_frame) (const unsigned char *buf, size_t,
2655392f7a3SLiteSpeed Tech                            const unsigned char **token, size_t *token_size);
2665392f7a3SLiteSpeed Tech    size_t
2675392f7a3SLiteSpeed Tech    (*pf_new_connection_id_frame_size) (unsigned seqno, unsigned cid_len);
2685392f7a3SLiteSpeed Tech    int
2695392f7a3SLiteSpeed Tech    (*pf_gen_new_connection_id_frame) (unsigned char *buf, size_t,
2705392f7a3SLiteSpeed Tech                unsigned seqno, const struct lsquic_cid *,
2715392f7a3SLiteSpeed Tech                const unsigned char *token, size_t);
2725392f7a3SLiteSpeed Tech    size_t
2735392f7a3SLiteSpeed Tech    (*pf_retire_cid_frame_size) (uint64_t);
2745392f7a3SLiteSpeed Tech    int
2755392f7a3SLiteSpeed Tech    (*pf_gen_retire_cid_frame) (unsigned char *buf, size_t, uint64_t);
2765392f7a3SLiteSpeed Tech    int
2775392f7a3SLiteSpeed Tech    (*pf_parse_retire_cid_frame) (const unsigned char *buf, size_t, uint64_t *);
2785392f7a3SLiteSpeed Tech    size_t
2795392f7a3SLiteSpeed Tech    (*pf_new_token_frame_size) (size_t);
2805392f7a3SLiteSpeed Tech    int
2815392f7a3SLiteSpeed Tech    (*pf_gen_new_token_frame) (unsigned char *buf, size_t,
2825392f7a3SLiteSpeed Tech                                        const unsigned char *token, size_t);
2835392f7a3SLiteSpeed Tech    int
2845392f7a3SLiteSpeed Tech    (*pf_gen_streams_blocked_frame) (unsigned char *buf, size_t buf_len,
2855392f7a3SLiteSpeed Tech                                        enum stream_dir, uint64_t);
2865392f7a3SLiteSpeed Tech    int
2875392f7a3SLiteSpeed Tech    (*pf_parse_streams_blocked_frame) (const unsigned char *buf, size_t buf_len,
2885392f7a3SLiteSpeed Tech                                        enum stream_dir *, uint64_t *);
2895392f7a3SLiteSpeed Tech    unsigned
2905392f7a3SLiteSpeed Tech    (*pf_streams_blocked_frame_size) (uint64_t);
2915392f7a3SLiteSpeed Tech    int
2925392f7a3SLiteSpeed Tech    (*pf_gen_max_streams_frame) (unsigned char *buf, size_t buf_len,
2935392f7a3SLiteSpeed Tech                                        enum stream_dir, uint64_t);
2945392f7a3SLiteSpeed Tech    int
2955392f7a3SLiteSpeed Tech    (*pf_parse_max_streams_frame) (const unsigned char *buf, size_t buf_len,
2965392f7a3SLiteSpeed Tech                                        enum stream_dir *, uint64_t *);
2975392f7a3SLiteSpeed Tech    unsigned
2985392f7a3SLiteSpeed Tech    (*pf_max_streams_frame_size) (uint64_t);
2995392f7a3SLiteSpeed Tech    unsigned
3005392f7a3SLiteSpeed Tech    (*pf_path_chal_frame_size) (void);
3015392f7a3SLiteSpeed Tech    int
3025392f7a3SLiteSpeed Tech    (*pf_gen_path_chal_frame) (unsigned char *, size_t, uint64_t chal);
3035392f7a3SLiteSpeed Tech    unsigned
3045392f7a3SLiteSpeed Tech    (*pf_path_resp_frame_size) (void);
3055392f7a3SLiteSpeed Tech    int
3065392f7a3SLiteSpeed Tech    (*pf_gen_path_resp_frame) (unsigned char *, size_t, uint64_t resp);
3079fc12041SDmitri Tikhonov    int
3089fc12041SDmitri Tikhonov    (*pf_gen_handshake_done_frame) (unsigned char *buf, size_t buf_len);
3099fc12041SDmitri Tikhonov    int
3109fc12041SDmitri Tikhonov    (*pf_parse_handshake_done_frame) (const unsigned char *buf, size_t buf_len);
3119fc12041SDmitri Tikhonov    unsigned
3129fc12041SDmitri Tikhonov    (*pf_handshake_done_frame_size) (void);
313feca77f5SDmitri Tikhonov    int
314feca77f5SDmitri Tikhonov    (*pf_gen_ack_frequency_frame) (unsigned char *buf, size_t buf_len,
3153a537672SDmitri Tikhonov        uint64_t seqno, uint64_t pack_tol, uint64_t upd_mad, int ignore);
316feca77f5SDmitri Tikhonov    int
317feca77f5SDmitri Tikhonov    (*pf_parse_ack_frequency_frame) (const unsigned char *buf, size_t buf_len,
3183a537672SDmitri Tikhonov        uint64_t *seqno, uint64_t *pack_tol, uint64_t *upd_mad, int *ignore);
319feca77f5SDmitri Tikhonov    unsigned
320feca77f5SDmitri Tikhonov    (*pf_ack_frequency_frame_size) (uint64_t seqno, uint64_t pack_tol,
3213a537672SDmitri Tikhonov        uint64_t upd_mad /* Don't need to pass `ignore' */);
322afe3d363SDmitri Tikhonov    int
323afe3d363SDmitri Tikhonov    (*pf_gen_timestamp_frame) (unsigned char *buf, size_t buf_len, uint64_t);
324afe3d363SDmitri Tikhonov    int
325afe3d363SDmitri Tikhonov    (*pf_parse_timestamp_frame) (const unsigned char *buf, size_t, uint64_t *);
326b1a7c3f9SDmitri Tikhonov    int
327b1a7c3f9SDmitri Tikhonov    (*pf_parse_datagram_frame) (const unsigned char *buf, size_t, const void **,
328b1a7c3f9SDmitri Tikhonov                                                                    size_t *);
329b1a7c3f9SDmitri Tikhonov    int
330b1a7c3f9SDmitri Tikhonov    (*pf_gen_datagram_frame) (unsigned char *, size_t bufsz, size_t min_sz,
331b1a7c3f9SDmitri Tikhonov        size_t max_sz, ssize_t (*)(struct lsquic_conn *, void *, size_t),
332b1a7c3f9SDmitri Tikhonov        struct lsquic_conn *);
333b1a7c3f9SDmitri Tikhonov    unsigned
334b1a7c3f9SDmitri Tikhonov    (*pf_datagram_frame_size) (size_t);
33550aadb33SDmitri Tikhonov};
33650aadb33SDmitri Tikhonov
3375392f7a3SLiteSpeed Tech
338fb96f4ddSDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q043;
339c7d81ce1SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q046;
3407a8b2eceSDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q050;
3415392f7a3SLiteSpeed Techextern const struct parse_funcs lsquic_parse_funcs_ietf_v1;
34250aadb33SDmitri Tikhonov
3435392f7a3SLiteSpeed Tech#define select_pf_by_ver(ver) (                                             \
344fb96f4ddSDmitri Tikhonov    (1 << (ver)) & (1 << LSQVER_043)                     ?                  \
345fb96f4ddSDmitri Tikhonov                                         &lsquic_parse_funcs_gquic_Q043 :   \
3467a8b2eceSDmitri Tikhonov    (1 << (ver)) & (1 << LSQVER_046)                            ?           \
3475392f7a3SLiteSpeed Tech                                         &lsquic_parse_funcs_gquic_Q046 :   \
3487a8b2eceSDmitri Tikhonov    (1 << (ver)) & ((1 << LSQVER_050)|LSQUIC_EXPERIMENTAL_Q098) ?           \
3497a8b2eceSDmitri Tikhonov                                         &lsquic_parse_funcs_gquic_Q050 :   \
3505392f7a3SLiteSpeed Tech    &lsquic_parse_funcs_ietf_v1)
35150aadb33SDmitri Tikhonov
3525392f7a3SLiteSpeed Tech/* This function is gQUIC-version independent */
35350aadb33SDmitri Tikhonovint
3549626cfc2SDmitri Tikhonovlsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length,
3555392f7a3SLiteSpeed Tech                int is_server, unsigned cid_len, struct packin_parse_state *);
35650aadb33SDmitri Tikhonov
3579626cfc2SDmitri Tikhonovint
3585392f7a3SLiteSpeed Techlsquic_Q046_parse_packet_in_short_begin (struct lsquic_packet_in *, size_t length,
3595392f7a3SLiteSpeed Tech                int is_server, unsigned, struct packin_parse_state *);
3609626cfc2SDmitri Tikhonov
3619626cfc2SDmitri Tikhonovint
3625392f7a3SLiteSpeed Techlsquic_Q046_parse_packet_in_long_begin (struct lsquic_packet_in *, size_t length,
3635392f7a3SLiteSpeed Tech                int is_server, unsigned, struct packin_parse_state *);
3649626cfc2SDmitri Tikhonov
3657a8b2eceSDmitri Tikhonovint
3667a8b2eceSDmitri Tikhonovlsquic_Q050_parse_packet_in_long_begin (struct lsquic_packet_in *, size_t length,
3677a8b2eceSDmitri Tikhonov                int is_server, unsigned, struct packin_parse_state *);
3687a8b2eceSDmitri Tikhonov
3696aba801dSDmitri Tikhonovenum quic_frame_type
370feca77f5SDmitri Tikhonovlsquic_parse_frame_type_gquic_Q035_thru_Q046 (const unsigned char *, size_t);
37150aadb33SDmitri Tikhonov
372feca77f5SDmitri Tikhonovextern const enum quic_frame_type lsquic_iquic_byte2type[0x40];
3735392f7a3SLiteSpeed Tech
37450aadb33SDmitri Tikhonovsize_t
375a5fa05f9SDmitri Tikhonovlsquic_calc_stream_frame_header_sz_gquic (lsquic_stream_id_t stream_id,
3765392f7a3SLiteSpeed Tech                                                    uint64_t offset, unsigned);
37750aadb33SDmitri Tikhonov
3789626cfc2SDmitri Tikhonovsize_t
3799626cfc2SDmitri Tikhonovlsquic_gquic_packout_size (const struct lsquic_conn *,
3809626cfc2SDmitri Tikhonov                                            const struct lsquic_packet_out *);
3819626cfc2SDmitri Tikhonov
3829626cfc2SDmitri Tikhonovsize_t
3839626cfc2SDmitri Tikhonovlsquic_gquic_packout_header_size (const struct lsquic_conn *conn,
3844429f8eaSDmitri Tikhonov                enum packet_out_flags flags, size_t unused, enum header_type);
3855392f7a3SLiteSpeed Tech
3865392f7a3SLiteSpeed Techsize_t
3875392f7a3SLiteSpeed Techlsquic_gquic_po_header_sz (enum packet_out_flags flags);
3885392f7a3SLiteSpeed Tech
3895392f7a3SLiteSpeed Techsize_t
3905392f7a3SLiteSpeed Techlsquic_gquic_packout_size (const struct lsquic_conn *,
3915392f7a3SLiteSpeed Tech                                            const struct lsquic_packet_out *);
3929626cfc2SDmitri Tikhonov
3939626cfc2SDmitri Tikhonovsize_t
3949626cfc2SDmitri Tikhonovlsquic_gquic_po_header_sz (enum packet_out_flags flags);
3959626cfc2SDmitri Tikhonov
39650aadb33SDmitri Tikhonov/* This maps two bits as follows:
39750aadb33SDmitri Tikhonov *  00  ->  1
39850aadb33SDmitri Tikhonov *  01  ->  2
39950aadb33SDmitri Tikhonov *  10  ->  4
40050aadb33SDmitri Tikhonov *  11  ->  6
40150aadb33SDmitri Tikhonov *
40250aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
40350aadb33SDmitri Tikhonov */
40450aadb33SDmitri Tikhonov#define twobit_to_1246(bits) ((bits) * 2 + !(bits))
40550aadb33SDmitri Tikhonov
40650aadb33SDmitri Tikhonov/* This maps two bits as follows:
40750aadb33SDmitri Tikhonov *  00  ->  1
40850aadb33SDmitri Tikhonov *  01  ->  2
40950aadb33SDmitri Tikhonov *  10  ->  4
41050aadb33SDmitri Tikhonov *  11  ->  8
41150aadb33SDmitri Tikhonov *
41250aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
41350aadb33SDmitri Tikhonov */
41450aadb33SDmitri Tikhonov#define twobit_to_1248(bits) (1 << (bits))
41550aadb33SDmitri Tikhonov
416de46bf2fSDmitri Tikhonov#define ECN_COUNTS_STR  " ECT(0): 01234567879012345678790;" \
417de46bf2fSDmitri Tikhonov                        " ECT(1): 01234567879012345678790;" \
418de46bf2fSDmitri Tikhonov                        " CE: 01234567879012345678790"
419de46bf2fSDmitri Tikhonov#define RANGES_TRUNCATED_STR " ranges truncated! "
420de46bf2fSDmitri Tikhonov
421de46bf2fSDmitri Tikhonov#define MAX_ACKI_STR_SZ (256 * (3 /* [-] */ + 20 /* ~0ULL */ * 2) \
422de46bf2fSDmitri Tikhonov                    + sizeof(ECN_COUNTS_STR) + sizeof(RANGES_TRUNCATED_STR))
423de46bf2fSDmitri Tikhonov
424de46bf2fSDmitri Tikhonovvoid
425de46bf2fSDmitri Tikhonovlsquic_acki2str (const struct ack_info *acki, char *, size_t);
42650aadb33SDmitri Tikhonov
427c51ce338SDmitri Tikhonovvoid
4287a8b2eceSDmitri Tikhonovlsquic_turn_on_fin_Q035_thru_Q046 (unsigned char *);
429c51ce338SDmitri Tikhonov
430c7d81ce1SDmitri Tikhonovenum packno_bits
431c7d81ce1SDmitri Tikhonovlsquic_gquic_calc_packno_bits (lsquic_packno_t packno,
432c7d81ce1SDmitri Tikhonov                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
433c7d81ce1SDmitri Tikhonov
434c7d81ce1SDmitri Tikhonovunsigned
435c7d81ce1SDmitri Tikhonovlsquic_gquic_packno_bits2len (enum packno_bits);
436c7d81ce1SDmitri Tikhonov
437de46bf2fSDmitri Tikhonovint
438de46bf2fSDmitri Tikhonovlsquic_merge_acks (struct ack_info *dst, const struct ack_info *src);
439de46bf2fSDmitri Tikhonov
44050aadb33SDmitri Tikhonov#endif
441