lsquic_parse.h revision c7d81ce1
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_PARSE_H
3#define LSQUIC_PARSE_H 1
4
5#include <stdint.h>
6
7#include "lsquic_packet_common.h"
8
9struct lsquic_packet_in;
10struct lsquic_packet_out;
11struct packin_parse_state;
12struct stream_frame;
13enum packet_out_flags;
14
15#define LSQUIC_PARSE_ACK_TIMESTAMPS 0
16
17typedef struct ack_info
18{
19    unsigned    n_timestamps;   /* 0 to 255 */
20    unsigned    n_ranges;       /* This is at least 1 */
21                                /* Largest acked is ack_info.ranges[0].high */
22    lsquic_time_t   lack_delta;
23    struct lsquic_packno_range ranges[256];
24#if LSQUIC_PARSE_ACK_TIMESTAMPS
25    struct {
26        /* Currently we just read these timestamps in (assuming it is
27         * compiled in, of course), but do not do anything with them.
28         * When we do, the representation of these fields should be
29         * switched to whatever is most appropriate/efficient.
30         */
31        unsigned char   packet_delta;
32        uint64_t        delta_usec;
33    }           timestamps[255];
34#endif
35} ack_info_t;
36
37struct short_ack_info
38{
39    unsigned                    sai_n_timestamps;
40    lsquic_time_t               sai_lack_delta;
41    struct lsquic_packno_range  sai_range;
42};
43
44#define largest_acked(acki) (+(acki)->ranges[0].high)
45
46#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low)
47
48/* gaf_: generate ACK frame */
49struct lsquic_packno_range;
50typedef const struct lsquic_packno_range *
51    (*gaf_rechist_first_f)          (void *rechist);
52typedef const struct lsquic_packno_range *
53    (*gaf_rechist_next_f)           (void *rechist);
54typedef lsquic_time_t
55    (*gaf_rechist_largest_recv_f)   (void *rechist);
56
57/* gsf_: generate stream frame */
58typedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin);
59
60/* This structure contains functions that parse and generate packets and
61 * frames in version-specific manner.  To begin with, there is difference
62 * between GQUIC's little-endian (Q038 and lower) and big-endian formats
63 * (Q039 and higher).  Q044 and higher uses different format for packet headers.
64 */
65struct parse_funcs
66{
67    /* Return buf length */
68    int
69    (*pf_gen_reg_pkt_header) (const struct lsquic_conn *,
70                const struct lsquic_packet_out *, unsigned char *, size_t);
71    void
72    (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in,
73                                                struct packin_parse_state *);
74    enum quic_frame_type
75    (*pf_parse_frame_type) (unsigned char);
76    /* Return used buffer length or a negative value if there was not enough
77     * room to write the stream frame.  In the latter case, the negative of
78     * the negative return value is the number of bytes required.  The
79     * exception is -1, which is a generic error code, as we always need
80     * more than 1 byte to write a STREAM frame.
81     */
82    int
83    (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz,
84                            uint32_t stream_id, uint64_t offset,
85                            int fin, size_t size, gsf_read_f, void *stream);
86    int
87    (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz,
88                                                    struct stream_frame *);
89    int
90    (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len,
91                                                    ack_info_t *ack_info);
92    int
93    (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz,
94                gaf_rechist_first_f, gaf_rechist_next_f,
95                gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now,
96                int *has_missing, lsquic_packno_t *largest_received);
97    int
98    (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len,
99                    lsquic_packno_t cur_packno, enum packno_bits,
100                    lsquic_packno_t least_unacked_packno);
101    int
102    (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len,
103                     lsquic_packno_t cur_packno, enum packno_bits,
104                     lsquic_packno_t *least_unacked);
105    int
106    (*pf_skip_stop_waiting_frame) (size_t buf_len, enum packno_bits);
107    int
108    (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len,
109                                    uint32_t stream_id, uint64_t offset);
110    int
111    (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len,
112                                      uint32_t *stream_id, uint64_t *offset);
113    int
114    (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len,
115                                                        uint32_t stream_id);
116    int
117    (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len,
118                                                        uint32_t *stream_id);
119    int
120    (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id,
121                          uint64_t offset, uint32_t error_code);
122    int
123    (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len,
124                uint32_t *stream_id, uint64_t *offset, uint32_t *error_code);
125    int
126    (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len,
127                uint32_t error_code, const char *reason, int reason_len);
128    int
129    (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len,
130                uint32_t *error_code, uint16_t *reason_length,
131                uint8_t *reason_offset);
132    int
133    (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len,
134                uint32_t error_code, uint32_t last_good_stream_id,
135                const char *reason, size_t reason_len);
136    int
137    (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len,
138                uint32_t *error_code, uint32_t *last_good_stream_id,
139                uint16_t *reason_length, const char **reason);
140    int
141    (*pf_gen_ping_frame) (unsigned char *buf, int buf_len);
142#ifndef NDEBUG
143    /* These float reading and writing functions assume `mem' has at least
144     * 2 bytes.
145     */
146    void
147    (*pf_write_float_time16) (lsquic_time_t time_us, void *mem);
148    uint64_t
149    (*pf_read_float_time16) (const void *mem);
150#endif
151    size_t
152    (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset);
153    void
154    (*pf_turn_on_fin) (unsigned char *);
155
156    size_t
157    (*pf_packout_size) (const struct lsquic_conn *,
158                                            const struct lsquic_packet_out *);
159
160    size_t
161    (*pf_packout_header_size) (const struct lsquic_conn *,
162                                            enum packet_out_flags);
163
164    enum packno_bits
165    (*pf_calc_packno_bits) (lsquic_packno_t packno,
166                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
167    unsigned
168    (*pf_packno_bits2len) (enum packno_bits);
169};
170
171extern const struct parse_funcs lsquic_parse_funcs_gquic_le;
172/* Q039 and later are big-endian: */
173extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039;
174extern const struct parse_funcs lsquic_parse_funcs_gquic_Q044;
175extern const struct parse_funcs lsquic_parse_funcs_gquic_Q046;
176
177#define select_pf_by_ver(ver) (                                         \
178    ((1 << (ver)) & (1 << LSQVER_035))                                  \
179        ? &lsquic_parse_funcs_gquic_le                                  \
180        : (ver) < LSQVER_044                                            \
181        ? &lsquic_parse_funcs_gquic_Q039                                \
182        : (ver) < LSQVER_046                                            \
183        ? &lsquic_parse_funcs_gquic_Q044                                \
184        : &lsquic_parse_funcs_gquic_Q046)
185
186int
187lsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length,
188                                int is_server, struct packin_parse_state *);
189
190int
191lsquic_iquic_parse_packet_in_long_begin (struct lsquic_packet_in *,
192            size_t length, int is_server, struct packin_parse_state *state);
193
194int
195lsquic_iquic_parse_packet_in_short_begin (struct lsquic_packet_in *,
196            size_t length, int is_server, struct packin_parse_state *state);
197
198enum quic_frame_type
199parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte);
200
201size_t
202calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset);
203
204size_t
205lsquic_gquic_packout_size (const struct lsquic_conn *,
206                                            const struct lsquic_packet_out *);
207
208size_t
209lsquic_gquic_packout_header_size (const struct lsquic_conn *conn,
210                                                enum packet_out_flags flags);
211
212size_t
213lsquic_gquic_po_header_sz (enum packet_out_flags flags);
214
215/* This maps two bits as follows:
216 *  00  ->  1
217 *  01  ->  2
218 *  10  ->  4
219 *  11  ->  6
220 *
221 * Assumes that only two low bits are set.
222 */
223#define twobit_to_1246(bits) ((bits) * 2 + !(bits))
224
225/* This maps two bits as follows:
226 *  00  ->  1
227 *  01  ->  2
228 *  10  ->  4
229 *  11  ->  8
230 *
231 * Assumes that only two low bits are set.
232 */
233#define twobit_to_1248(bits) (1 << (bits))
234
235char *
236acki2str (const struct ack_info *acki, size_t *sz);
237
238void
239lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *);
240
241enum packno_bits
242lsquic_gquic_calc_packno_bits (lsquic_packno_t packno,
243                        lsquic_packno_t least_unacked, uint64_t n_in_flight);
244
245unsigned
246lsquic_gquic_packno_bits2len (enum packno_bits);
247
248#endif
249