lsquic_parse.h revision 6aba801d
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 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 lsquic_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 lsquic_packno_bits, 104 lsquic_packno_t *least_unacked); 105 int 106 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum lsquic_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 165extern const struct parse_funcs lsquic_parse_funcs_gquic_le; 166/* Q039 and later are big-endian: */ 167extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039; 168extern const struct parse_funcs lsquic_parse_funcs_gquic_Q044; 169 170#define select_pf_by_ver(ver) ( \ 171 ((1 << (ver)) & (1 << LSQVER_035)) \ 172 ? &lsquic_parse_funcs_gquic_le \ 173 : (ver) < LSQVER_044 \ 174 ? &lsquic_parse_funcs_gquic_Q039 \ 175 : &lsquic_parse_funcs_gquic_Q044) 176 177int 178lsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 179 int is_server, struct packin_parse_state *); 180 181int 182lsquic_iquic_parse_packet_in_long_begin (struct lsquic_packet_in *, 183 size_t length, int is_server, struct packin_parse_state *state); 184 185int 186lsquic_iquic_parse_packet_in_short_begin (struct lsquic_packet_in *, 187 size_t length, int is_server, struct packin_parse_state *state); 188 189enum quic_frame_type 190parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte); 191 192size_t 193calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset); 194 195size_t 196lsquic_gquic_packout_size (const struct lsquic_conn *, 197 const struct lsquic_packet_out *); 198 199size_t 200lsquic_gquic_packout_header_size (const struct lsquic_conn *conn, 201 enum packet_out_flags flags); 202 203size_t 204lsquic_gquic_po_header_sz (enum packet_out_flags flags); 205 206/* This maps two bits as follows: 207 * 00 -> 1 208 * 01 -> 2 209 * 10 -> 4 210 * 11 -> 6 211 * 212 * Assumes that only two low bits are set. 213 */ 214#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 215 216/* This maps two bits as follows: 217 * 00 -> 1 218 * 01 -> 2 219 * 10 -> 4 220 * 11 -> 8 221 * 222 * Assumes that only two low bits are set. 223 */ 224#define twobit_to_1248(bits) (1 << (bits)) 225 226char * 227acki2str (const struct ack_info *acki, size_t *sz); 228 229void 230lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *); 231 232#endif 233