lsquic_parse.h revision c7d81ce1
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 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"
850aadb33SDmitri Tikhonov
950aadb33SDmitri Tikhonovstruct lsquic_packet_in;
109626cfc2SDmitri Tikhonovstruct lsquic_packet_out;
119626cfc2SDmitri Tikhonovstruct packin_parse_state;
1250aadb33SDmitri Tikhonovstruct stream_frame;
139626cfc2SDmitri Tikhonovenum packet_out_flags;
1450aadb33SDmitri Tikhonov
1550aadb33SDmitri Tikhonov#define LSQUIC_PARSE_ACK_TIMESTAMPS 0
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonovtypedef struct ack_info
1850aadb33SDmitri Tikhonov{
1950aadb33SDmitri Tikhonov    unsigned    n_timestamps;   /* 0 to 255 */
2050aadb33SDmitri Tikhonov    unsigned    n_ranges;       /* This is at least 1 */
2150aadb33SDmitri Tikhonov                                /* Largest acked is ack_info.ranges[0].high */
2250aadb33SDmitri Tikhonov    lsquic_time_t   lack_delta;
23bfc7bfd8SDmitri Tikhonov    struct lsquic_packno_range ranges[256];
2450aadb33SDmitri Tikhonov#if LSQUIC_PARSE_ACK_TIMESTAMPS
2550aadb33SDmitri Tikhonov    struct {
2650aadb33SDmitri Tikhonov        /* Currently we just read these timestamps in (assuming it is
2750aadb33SDmitri Tikhonov         * compiled in, of course), but do not do anything with them.
2850aadb33SDmitri Tikhonov         * When we do, the representation of these fields should be
2950aadb33SDmitri Tikhonov         * switched to whatever is most appropriate/efficient.
3050aadb33SDmitri Tikhonov         */
3150aadb33SDmitri Tikhonov        unsigned char   packet_delta;
3250aadb33SDmitri Tikhonov        uint64_t        delta_usec;
3350aadb33SDmitri Tikhonov    }           timestamps[255];
3450aadb33SDmitri Tikhonov#endif
3550aadb33SDmitri Tikhonov} ack_info_t;
3650aadb33SDmitri Tikhonov
3716a9b66aSDmitri Tikhonovstruct short_ack_info
3816a9b66aSDmitri Tikhonov{
3916a9b66aSDmitri Tikhonov    unsigned                    sai_n_timestamps;
4016a9b66aSDmitri Tikhonov    lsquic_time_t               sai_lack_delta;
4116a9b66aSDmitri Tikhonov    struct lsquic_packno_range  sai_range;
4216a9b66aSDmitri Tikhonov};
4316a9b66aSDmitri Tikhonov
4450aadb33SDmitri Tikhonov#define largest_acked(acki) (+(acki)->ranges[0].high)
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonov#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low)
4750aadb33SDmitri Tikhonov
4850aadb33SDmitri Tikhonov/* gaf_: generate ACK frame */
4950aadb33SDmitri Tikhonovstruct lsquic_packno_range;
5050aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
5150aadb33SDmitri Tikhonov    (*gaf_rechist_first_f)          (void *rechist);
5250aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
5350aadb33SDmitri Tikhonov    (*gaf_rechist_next_f)           (void *rechist);
5450aadb33SDmitri Tikhonovtypedef lsquic_time_t
5550aadb33SDmitri Tikhonov    (*gaf_rechist_largest_recv_f)   (void *rechist);
5650aadb33SDmitri Tikhonov
5750aadb33SDmitri Tikhonov/* gsf_: generate stream frame */
5850aadb33SDmitri Tikhonovtypedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin);
5950aadb33SDmitri Tikhonov
6050aadb33SDmitri Tikhonov/* This structure contains functions that parse and generate packets and
6150aadb33SDmitri Tikhonov * frames in version-specific manner.  To begin with, there is difference
6250aadb33SDmitri Tikhonov * between GQUIC's little-endian (Q038 and lower) and big-endian formats
63c7d81ce1SDmitri Tikhonov * (Q039 and higher).  Q044 and higher uses different format for packet headers.
6450aadb33SDmitri Tikhonov */
6550aadb33SDmitri Tikhonovstruct parse_funcs
6650aadb33SDmitri Tikhonov{
6750aadb33SDmitri Tikhonov    /* Return buf length */
6850aadb33SDmitri Tikhonov    int
699626cfc2SDmitri Tikhonov    (*pf_gen_reg_pkt_header) (const struct lsquic_conn *,
709626cfc2SDmitri Tikhonov                const struct lsquic_packet_out *, unsigned char *, size_t);
7150aadb33SDmitri Tikhonov    void
7250aadb33SDmitri Tikhonov    (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in,
7350aadb33SDmitri Tikhonov                                                struct packin_parse_state *);
746aba801dSDmitri Tikhonov    enum quic_frame_type
7550aadb33SDmitri Tikhonov    (*pf_parse_frame_type) (unsigned char);
7614e3680dSDmitri Tikhonov    /* Return used buffer length or a negative value if there was not enough
7714e3680dSDmitri Tikhonov     * room to write the stream frame.  In the latter case, the negative of
7814e3680dSDmitri Tikhonov     * the negative return value is the number of bytes required.  The
7914e3680dSDmitri Tikhonov     * exception is -1, which is a generic error code, as we always need
8014e3680dSDmitri Tikhonov     * more than 1 byte to write a STREAM frame.
8114e3680dSDmitri Tikhonov     */
8250aadb33SDmitri Tikhonov    int
8350aadb33SDmitri Tikhonov    (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz,
8450aadb33SDmitri Tikhonov                            uint32_t stream_id, uint64_t offset,
85bfc7bfd8SDmitri Tikhonov                            int fin, size_t size, gsf_read_f, void *stream);
8650aadb33SDmitri Tikhonov    int
8750aadb33SDmitri Tikhonov    (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz,
8850aadb33SDmitri Tikhonov                                                    struct stream_frame *);
8950aadb33SDmitri Tikhonov    int
9050aadb33SDmitri Tikhonov    (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len,
9150aadb33SDmitri Tikhonov                                                    ack_info_t *ack_info);
9250aadb33SDmitri Tikhonov    int
9350aadb33SDmitri Tikhonov    (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz,
9450aadb33SDmitri Tikhonov                gaf_rechist_first_f, gaf_rechist_next_f,
9550aadb33SDmitri Tikhonov                gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now,
9616a9b66aSDmitri Tikhonov                int *has_missing, lsquic_packno_t *largest_received);
9750aadb33SDmitri Tikhonov    int
9850aadb33SDmitri Tikhonov    (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len,
99c7d81ce1SDmitri Tikhonov                    lsquic_packno_t cur_packno, enum packno_bits,
10050aadb33SDmitri Tikhonov                    lsquic_packno_t least_unacked_packno);
10150aadb33SDmitri Tikhonov    int
10250aadb33SDmitri Tikhonov    (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len,
103c7d81ce1SDmitri Tikhonov                     lsquic_packno_t cur_packno, enum packno_bits,
10450aadb33SDmitri Tikhonov                     lsquic_packno_t *least_unacked);
10550aadb33SDmitri Tikhonov    int
106c7d81ce1SDmitri Tikhonov    (*pf_skip_stop_waiting_frame) (size_t buf_len, enum packno_bits);
10750aadb33SDmitri Tikhonov    int
10850aadb33SDmitri Tikhonov    (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len,
10950aadb33SDmitri Tikhonov                                    uint32_t stream_id, uint64_t offset);
11050aadb33SDmitri Tikhonov    int
11150aadb33SDmitri Tikhonov    (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len,
11250aadb33SDmitri Tikhonov                                      uint32_t *stream_id, uint64_t *offset);
11350aadb33SDmitri Tikhonov    int
11450aadb33SDmitri Tikhonov    (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len,
11550aadb33SDmitri Tikhonov                                                        uint32_t stream_id);
11650aadb33SDmitri Tikhonov    int
11750aadb33SDmitri Tikhonov    (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len,
11850aadb33SDmitri Tikhonov                                                        uint32_t *stream_id);
11950aadb33SDmitri Tikhonov    int
12050aadb33SDmitri Tikhonov    (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id,
12150aadb33SDmitri Tikhonov                          uint64_t offset, uint32_t error_code);
12250aadb33SDmitri Tikhonov    int
12350aadb33SDmitri Tikhonov    (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len,
12450aadb33SDmitri Tikhonov                uint32_t *stream_id, uint64_t *offset, uint32_t *error_code);
12550aadb33SDmitri Tikhonov    int
12650aadb33SDmitri Tikhonov    (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len,
12750aadb33SDmitri Tikhonov                uint32_t error_code, const char *reason, int reason_len);
12850aadb33SDmitri Tikhonov    int
12950aadb33SDmitri Tikhonov    (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len,
13050aadb33SDmitri Tikhonov                uint32_t *error_code, uint16_t *reason_length,
13150aadb33SDmitri Tikhonov                uint8_t *reason_offset);
13250aadb33SDmitri Tikhonov    int
13350aadb33SDmitri Tikhonov    (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len,
13450aadb33SDmitri Tikhonov                uint32_t error_code, uint32_t last_good_stream_id,
13550aadb33SDmitri Tikhonov                const char *reason, size_t reason_len);
13650aadb33SDmitri Tikhonov    int
13750aadb33SDmitri Tikhonov    (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len,
13850aadb33SDmitri Tikhonov                uint32_t *error_code, uint32_t *last_good_stream_id,
13950aadb33SDmitri Tikhonov                uint16_t *reason_length, const char **reason);
14050aadb33SDmitri Tikhonov    int
14150aadb33SDmitri Tikhonov    (*pf_gen_ping_frame) (unsigned char *buf, int buf_len);
142a4e24689SBob Perper#ifndef NDEBUG
14350aadb33SDmitri Tikhonov    /* These float reading and writing functions assume `mem' has at least
14450aadb33SDmitri Tikhonov     * 2 bytes.
14550aadb33SDmitri Tikhonov     */
14650aadb33SDmitri Tikhonov    void
14750aadb33SDmitri Tikhonov    (*pf_write_float_time16) (lsquic_time_t time_us, void *mem);
14850aadb33SDmitri Tikhonov    uint64_t
14950aadb33SDmitri Tikhonov    (*pf_read_float_time16) (const void *mem);
150a4e24689SBob Perper#endif
15150aadb33SDmitri Tikhonov    size_t
15250aadb33SDmitri Tikhonov    (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset);
153c51ce338SDmitri Tikhonov    void
154c51ce338SDmitri Tikhonov    (*pf_turn_on_fin) (unsigned char *);
1559626cfc2SDmitri Tikhonov
1569626cfc2SDmitri Tikhonov    size_t
1579626cfc2SDmitri Tikhonov    (*pf_packout_size) (const struct lsquic_conn *,
1589626cfc2SDmitri Tikhonov                                            const struct lsquic_packet_out *);
1599626cfc2SDmitri Tikhonov
1609626cfc2SDmitri Tikhonov    size_t
1619626cfc2SDmitri Tikhonov    (*pf_packout_header_size) (const struct lsquic_conn *,
1629626cfc2SDmitri Tikhonov                                            enum packet_out_flags);
163c7d81ce1SDmitri Tikhonov
164c7d81ce1SDmitri Tikhonov    enum packno_bits
165c7d81ce1SDmitri Tikhonov    (*pf_calc_packno_bits) (lsquic_packno_t packno,
166c7d81ce1SDmitri Tikhonov                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
167c7d81ce1SDmitri Tikhonov    unsigned
168c7d81ce1SDmitri Tikhonov    (*pf_packno_bits2len) (enum packno_bits);
16950aadb33SDmitri Tikhonov};
17050aadb33SDmitri Tikhonov
17150aadb33SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_le;
17250aadb33SDmitri Tikhonov/* Q039 and later are big-endian: */
17350aadb33SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q039;
1749626cfc2SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q044;
175c7d81ce1SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q046;
17650aadb33SDmitri Tikhonov
17750aadb33SDmitri Tikhonov#define select_pf_by_ver(ver) (                                         \
178052a1c28SDmitri Tikhonov    ((1 << (ver)) & (1 << LSQVER_035))                                  \
17997028223SDmitri Tikhonov        ? &lsquic_parse_funcs_gquic_le                                  \
1809626cfc2SDmitri Tikhonov        : (ver) < LSQVER_044                                            \
1819626cfc2SDmitri Tikhonov        ? &lsquic_parse_funcs_gquic_Q039                                \
182c7d81ce1SDmitri Tikhonov        : (ver) < LSQVER_046                                            \
183c7d81ce1SDmitri Tikhonov        ? &lsquic_parse_funcs_gquic_Q044                                \
184c7d81ce1SDmitri Tikhonov        : &lsquic_parse_funcs_gquic_Q046)
18550aadb33SDmitri Tikhonov
18650aadb33SDmitri Tikhonovint
1879626cfc2SDmitri Tikhonovlsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length,
18850aadb33SDmitri Tikhonov                                int is_server, struct packin_parse_state *);
18950aadb33SDmitri Tikhonov
1909626cfc2SDmitri Tikhonovint
1919626cfc2SDmitri Tikhonovlsquic_iquic_parse_packet_in_long_begin (struct lsquic_packet_in *,
1929626cfc2SDmitri Tikhonov            size_t length, int is_server, struct packin_parse_state *state);
1939626cfc2SDmitri Tikhonov
1949626cfc2SDmitri Tikhonovint
1959626cfc2SDmitri Tikhonovlsquic_iquic_parse_packet_in_short_begin (struct lsquic_packet_in *,
1969626cfc2SDmitri Tikhonov            size_t length, int is_server, struct packin_parse_state *state);
1979626cfc2SDmitri Tikhonov
1986aba801dSDmitri Tikhonovenum quic_frame_type
19950aadb33SDmitri Tikhonovparse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte);
20050aadb33SDmitri Tikhonov
20150aadb33SDmitri Tikhonovsize_t
20250aadb33SDmitri Tikhonovcalc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset);
20350aadb33SDmitri Tikhonov
2049626cfc2SDmitri Tikhonovsize_t
2059626cfc2SDmitri Tikhonovlsquic_gquic_packout_size (const struct lsquic_conn *,
2069626cfc2SDmitri Tikhonov                                            const struct lsquic_packet_out *);
2079626cfc2SDmitri Tikhonov
2089626cfc2SDmitri Tikhonovsize_t
2099626cfc2SDmitri Tikhonovlsquic_gquic_packout_header_size (const struct lsquic_conn *conn,
2109626cfc2SDmitri Tikhonov                                                enum packet_out_flags flags);
2119626cfc2SDmitri Tikhonov
2129626cfc2SDmitri Tikhonovsize_t
2139626cfc2SDmitri Tikhonovlsquic_gquic_po_header_sz (enum packet_out_flags flags);
2149626cfc2SDmitri Tikhonov
21550aadb33SDmitri Tikhonov/* This maps two bits as follows:
21650aadb33SDmitri Tikhonov *  00  ->  1
21750aadb33SDmitri Tikhonov *  01  ->  2
21850aadb33SDmitri Tikhonov *  10  ->  4
21950aadb33SDmitri Tikhonov *  11  ->  6
22050aadb33SDmitri Tikhonov *
22150aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
22250aadb33SDmitri Tikhonov */
22350aadb33SDmitri Tikhonov#define twobit_to_1246(bits) ((bits) * 2 + !(bits))
22450aadb33SDmitri Tikhonov
22550aadb33SDmitri Tikhonov/* This maps two bits as follows:
22650aadb33SDmitri Tikhonov *  00  ->  1
22750aadb33SDmitri Tikhonov *  01  ->  2
22850aadb33SDmitri Tikhonov *  10  ->  4
22950aadb33SDmitri Tikhonov *  11  ->  8
23050aadb33SDmitri Tikhonov *
23150aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
23250aadb33SDmitri Tikhonov */
23350aadb33SDmitri Tikhonov#define twobit_to_1248(bits) (1 << (bits))
23450aadb33SDmitri Tikhonov
23550aadb33SDmitri Tikhonovchar *
23650aadb33SDmitri Tikhonovacki2str (const struct ack_info *acki, size_t *sz);
23750aadb33SDmitri Tikhonov
238c51ce338SDmitri Tikhonovvoid
239c51ce338SDmitri Tikhonovlsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *);
240c51ce338SDmitri Tikhonov
241c7d81ce1SDmitri Tikhonovenum packno_bits
242c7d81ce1SDmitri Tikhonovlsquic_gquic_calc_packno_bits (lsquic_packno_t packno,
243c7d81ce1SDmitri Tikhonov                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
244c7d81ce1SDmitri Tikhonov
245c7d81ce1SDmitri Tikhonovunsigned
246c7d81ce1SDmitri Tikhonovlsquic_gquic_packno_bits2len (enum packno_bits);
247c7d81ce1SDmitri Tikhonov
24850aadb33SDmitri Tikhonov#endif
249