lsquic_parse.h revision 1b97e4af
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 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;
1050aadb33SDmitri Tikhonovstruct stream_frame;
1150aadb33SDmitri Tikhonov
1250aadb33SDmitri Tikhonov#define LSQUIC_PARSE_ACK_TIMESTAMPS 0
1350aadb33SDmitri Tikhonov
1450aadb33SDmitri Tikhonovtypedef struct ack_info
1550aadb33SDmitri Tikhonov{
1650aadb33SDmitri Tikhonov    unsigned    n_timestamps;   /* 0 to 255 */
1750aadb33SDmitri Tikhonov    unsigned    n_ranges;       /* This is at least 1 */
1850aadb33SDmitri Tikhonov                                /* Largest acked is ack_info.ranges[0].high */
1950aadb33SDmitri Tikhonov    lsquic_time_t   lack_delta;
2050aadb33SDmitri Tikhonov    struct {
2150aadb33SDmitri Tikhonov        lsquic_packno_t high, low;
2250aadb33SDmitri Tikhonov    }           ranges[256];
2350aadb33SDmitri Tikhonov#if LSQUIC_PARSE_ACK_TIMESTAMPS
2450aadb33SDmitri Tikhonov    struct {
2550aadb33SDmitri Tikhonov        /* Currently we just read these timestamps in (assuming it is
2650aadb33SDmitri Tikhonov         * compiled in, of course), but do not do anything with them.
2750aadb33SDmitri Tikhonov         * When we do, the representation of these fields should be
2850aadb33SDmitri Tikhonov         * switched to whatever is most appropriate/efficient.
2950aadb33SDmitri Tikhonov         */
3050aadb33SDmitri Tikhonov        unsigned char   packet_delta;
3150aadb33SDmitri Tikhonov        uint64_t        delta_usec;
3250aadb33SDmitri Tikhonov    }           timestamps[255];
3350aadb33SDmitri Tikhonov#endif
3450aadb33SDmitri Tikhonov} ack_info_t;
3550aadb33SDmitri Tikhonov
3650aadb33SDmitri Tikhonov#define largest_acked(acki) (+(acki)->ranges[0].high)
3750aadb33SDmitri Tikhonov
3850aadb33SDmitri Tikhonov#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low)
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov/* gaf_: generate ACK frame */
4150aadb33SDmitri Tikhonovstruct lsquic_packno_range;
4250aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
4350aadb33SDmitri Tikhonov    (*gaf_rechist_first_f)          (void *rechist);
4450aadb33SDmitri Tikhonovtypedef const struct lsquic_packno_range *
4550aadb33SDmitri Tikhonov    (*gaf_rechist_next_f)           (void *rechist);
4650aadb33SDmitri Tikhonovtypedef lsquic_time_t
4750aadb33SDmitri Tikhonov    (*gaf_rechist_largest_recv_f)   (void *rechist);
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonov/* gsf_: generate stream frame */
5050aadb33SDmitri Tikhonovtypedef int    (*gsf_fin_f)  (void *stream);
5150aadb33SDmitri Tikhonovtypedef size_t (*gsf_size_f) (void *stream);
5250aadb33SDmitri Tikhonovtypedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin);
5350aadb33SDmitri Tikhonov
5450aadb33SDmitri Tikhonovstruct packin_parse_state {
5550aadb33SDmitri Tikhonov    const unsigned char     *pps_p;      /* Pointer to packet number */
5650aadb33SDmitri Tikhonov    unsigned                 pps_nbytes; /* Number of bytes in packet number */
5750aadb33SDmitri Tikhonov};
5850aadb33SDmitri Tikhonov
5950aadb33SDmitri Tikhonov/* This structure contains functions that parse and generate packets and
6050aadb33SDmitri Tikhonov * frames in version-specific manner.  To begin with, there is difference
6150aadb33SDmitri Tikhonov * between GQUIC's little-endian (Q038 and lower) and big-endian formats
6250aadb33SDmitri Tikhonov * (Q039 and higher).
6350aadb33SDmitri Tikhonov */
6450aadb33SDmitri Tikhonovstruct parse_funcs
6550aadb33SDmitri Tikhonov{
6650aadb33SDmitri Tikhonov    int
6750aadb33SDmitri Tikhonov    (*pf_gen_ver_nego_pkt) (unsigned char *buf, size_t bufsz, uint64_t conn_id,
6850aadb33SDmitri Tikhonov                            unsigned version_bitmask);
6950aadb33SDmitri Tikhonov    /* Return buf length */
7050aadb33SDmitri Tikhonov    int
7150aadb33SDmitri Tikhonov    (*pf_gen_reg_pkt_header) (unsigned char *buf, size_t bufsz,
7250aadb33SDmitri Tikhonov                            const lsquic_cid_t *, const lsquic_ver_tag_t *,
7350aadb33SDmitri Tikhonov                            const unsigned char *nonce, lsquic_packno_t,
7450aadb33SDmitri Tikhonov                            enum lsquic_packno_bits);
7550aadb33SDmitri Tikhonov    void
7650aadb33SDmitri Tikhonov    (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in,
7750aadb33SDmitri Tikhonov                                                struct packin_parse_state *);
7850aadb33SDmitri Tikhonov    enum QUIC_FRAME_TYPE
7950aadb33SDmitri Tikhonov    (*pf_parse_frame_type) (unsigned char);
8050aadb33SDmitri Tikhonov    /* Return used buffer length */
8150aadb33SDmitri Tikhonov    int
8250aadb33SDmitri Tikhonov    (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz,
8350aadb33SDmitri Tikhonov                            uint32_t stream_id, uint64_t offset,
8450aadb33SDmitri Tikhonov                            gsf_fin_f, gsf_size_f, gsf_read_f, void *stream);
8550aadb33SDmitri Tikhonov    unsigned
8650aadb33SDmitri Tikhonov    (*pf_parse_stream_frame_header_sz) (unsigned char type);
8750aadb33SDmitri Tikhonov    int
8850aadb33SDmitri Tikhonov    (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz,
8950aadb33SDmitri Tikhonov                                                    struct stream_frame *);
9050aadb33SDmitri Tikhonov    int
9150aadb33SDmitri Tikhonov    (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len,
9250aadb33SDmitri Tikhonov                                                    ack_info_t *ack_info);
9350aadb33SDmitri Tikhonov    lsquic_packno_t
9450aadb33SDmitri Tikhonov    (*pf_parse_ack_high) (const unsigned char *buf, size_t buf_len);
9550aadb33SDmitri Tikhonov    int
9650aadb33SDmitri Tikhonov    (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz,
9750aadb33SDmitri Tikhonov                gaf_rechist_first_f, gaf_rechist_next_f,
9850aadb33SDmitri Tikhonov                gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now,
9950aadb33SDmitri Tikhonov                int *has_missing);
10050aadb33SDmitri Tikhonov    int
10150aadb33SDmitri Tikhonov    (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len,
10250aadb33SDmitri Tikhonov                    lsquic_packno_t cur_packno, enum lsquic_packno_bits,
10350aadb33SDmitri Tikhonov                    lsquic_packno_t least_unacked_packno);
10450aadb33SDmitri Tikhonov    int
10550aadb33SDmitri Tikhonov    (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len,
10650aadb33SDmitri Tikhonov                     lsquic_packno_t cur_packno, enum lsquic_packno_bits,
10750aadb33SDmitri Tikhonov                     lsquic_packno_t *least_unacked);
10850aadb33SDmitri Tikhonov    int
10950aadb33SDmitri Tikhonov    (*pf_skip_stop_waiting_frame) (size_t buf_len, enum lsquic_packno_bits);
11050aadb33SDmitri Tikhonov    int
11150aadb33SDmitri Tikhonov    (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len,
11250aadb33SDmitri Tikhonov                                    uint32_t stream_id, uint64_t offset);
11350aadb33SDmitri Tikhonov    int
11450aadb33SDmitri Tikhonov    (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len,
11550aadb33SDmitri Tikhonov                                      uint32_t *stream_id, uint64_t *offset);
11650aadb33SDmitri Tikhonov    int
11750aadb33SDmitri Tikhonov    (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len,
11850aadb33SDmitri Tikhonov                                                        uint32_t stream_id);
11950aadb33SDmitri Tikhonov    int
12050aadb33SDmitri Tikhonov    (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len,
12150aadb33SDmitri Tikhonov                                                        uint32_t *stream_id);
12250aadb33SDmitri Tikhonov    int
12350aadb33SDmitri Tikhonov    (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id,
12450aadb33SDmitri Tikhonov                          uint64_t offset, uint32_t error_code);
12550aadb33SDmitri Tikhonov    int
12650aadb33SDmitri Tikhonov    (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len,
12750aadb33SDmitri Tikhonov                uint32_t *stream_id, uint64_t *offset, uint32_t *error_code);
12850aadb33SDmitri Tikhonov    int
12950aadb33SDmitri Tikhonov    (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len,
13050aadb33SDmitri Tikhonov                uint32_t error_code, const char *reason, int reason_len);
13150aadb33SDmitri Tikhonov    int
13250aadb33SDmitri Tikhonov    (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len,
13350aadb33SDmitri Tikhonov                uint32_t *error_code, uint16_t *reason_length,
13450aadb33SDmitri Tikhonov                uint8_t *reason_offset);
13550aadb33SDmitri Tikhonov    int
13650aadb33SDmitri Tikhonov    (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len,
13750aadb33SDmitri Tikhonov                uint32_t error_code, uint32_t last_good_stream_id,
13850aadb33SDmitri Tikhonov                const char *reason, size_t reason_len);
13950aadb33SDmitri Tikhonov    int
14050aadb33SDmitri Tikhonov    (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len,
14150aadb33SDmitri Tikhonov                uint32_t *error_code, uint32_t *last_good_stream_id,
14250aadb33SDmitri Tikhonov                uint16_t *reason_length, const char **reason);
14350aadb33SDmitri Tikhonov    int
14450aadb33SDmitri Tikhonov    (*pf_gen_ping_frame) (unsigned char *buf, int buf_len);
14550aadb33SDmitri Tikhonov#ifndef NDEBUG
14650aadb33SDmitri Tikhonov    /* These float reading and writing functions assume `mem' has at least
14750aadb33SDmitri Tikhonov     * 2 bytes.
14850aadb33SDmitri Tikhonov     */
14950aadb33SDmitri Tikhonov    void
15050aadb33SDmitri Tikhonov    (*pf_write_float_time16) (lsquic_time_t time_us, void *mem);
15150aadb33SDmitri Tikhonov    uint64_t
15250aadb33SDmitri Tikhonov    (*pf_read_float_time16) (const void *mem);
15350aadb33SDmitri Tikhonov#endif
15450aadb33SDmitri Tikhonov    size_t
15550aadb33SDmitri Tikhonov    (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset);
15650aadb33SDmitri Tikhonov};
15750aadb33SDmitri Tikhonov
15850aadb33SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_le;
15950aadb33SDmitri Tikhonov/* Q039 and later are big-endian: */
16050aadb33SDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q039;
1611b97e4afSDmitri Tikhonovextern const struct parse_funcs lsquic_parse_funcs_gquic_Q041;
16250aadb33SDmitri Tikhonov
16350aadb33SDmitri Tikhonov#define select_pf_by_ver(ver) (                                         \
16450aadb33SDmitri Tikhonov    ((1 << (ver)) & ((1 << LSQVER_035) |                                \
16550aadb33SDmitri Tikhonov                     (1 << LSQVER_037) | (1 << LSQVER_038)))            \
16650aadb33SDmitri Tikhonov        ? &lsquic_parse_funcs_gquic_le :                                \
16750aadb33SDmitri Tikhonov    ((1 << (ver)) & (1 << LSQVER_039))                                  \
16850aadb33SDmitri Tikhonov        ? &lsquic_parse_funcs_gquic_Q039                                \
1691b97e4afSDmitri Tikhonov        : &lsquic_parse_funcs_gquic_Q041)
17050aadb33SDmitri Tikhonov
17150aadb33SDmitri Tikhonov/* This function is QUIC-version independent */
17250aadb33SDmitri Tikhonovint
17350aadb33SDmitri Tikhonovparse_packet_in_begin (struct lsquic_packet_in *, size_t length,
17450aadb33SDmitri Tikhonov                                int is_server, struct packin_parse_state *);
17550aadb33SDmitri Tikhonov
17650aadb33SDmitri Tikhonovenum QUIC_FRAME_TYPE
17750aadb33SDmitri Tikhonovparse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte);
17850aadb33SDmitri Tikhonov
17950aadb33SDmitri Tikhonovenum QUIC_FRAME_TYPE
1801b97e4afSDmitri Tikhonovparse_frame_type_gquic_Q041 (unsigned char first_byte);
18150aadb33SDmitri Tikhonov
18250aadb33SDmitri Tikhonovunsigned
18350aadb33SDmitri Tikhonovparse_stream_frame_header_sz_gquic (unsigned char type);
18450aadb33SDmitri Tikhonov
18550aadb33SDmitri Tikhonovsize_t
18650aadb33SDmitri Tikhonovcalc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset);
18750aadb33SDmitri Tikhonov
18850aadb33SDmitri Tikhonov/* This maps two bits as follows:
18950aadb33SDmitri Tikhonov *  00  ->  1
19050aadb33SDmitri Tikhonov *  01  ->  2
19150aadb33SDmitri Tikhonov *  10  ->  4
19250aadb33SDmitri Tikhonov *  11  ->  6
19350aadb33SDmitri Tikhonov *
19450aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
19550aadb33SDmitri Tikhonov */
19650aadb33SDmitri Tikhonov#define twobit_to_1246(bits) ((bits) * 2 + !(bits))
19750aadb33SDmitri Tikhonov
19850aadb33SDmitri Tikhonov/* This maps two bits as follows:
19950aadb33SDmitri Tikhonov *  00  ->  1
20050aadb33SDmitri Tikhonov *  01  ->  2
20150aadb33SDmitri Tikhonov *  10  ->  4
20250aadb33SDmitri Tikhonov *  11  ->  8
20350aadb33SDmitri Tikhonov *
20450aadb33SDmitri Tikhonov * Assumes that only two low bits are set.
20550aadb33SDmitri Tikhonov */
20650aadb33SDmitri Tikhonov#define twobit_to_1248(bits) (1 << (bits))
20750aadb33SDmitri Tikhonov
20850aadb33SDmitri Tikhonovchar *
20950aadb33SDmitri Tikhonovacki2str (const struct ack_info *acki, size_t *sz);
21050aadb33SDmitri Tikhonov
21150aadb33SDmitri Tikhonov#endif
212