lsquic_parse.h revision 9626cfc2
1/* Copyright (c) 2017 - 2018 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 */ 77 int 78 (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz, 79 uint32_t stream_id, uint64_t offset, 80 int fin, size_t size, gsf_read_f, void *stream); 81 int 82 (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz, 83 struct stream_frame *); 84 int 85 (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len, 86 ack_info_t *ack_info); 87 int 88 (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz, 89 gaf_rechist_first_f, gaf_rechist_next_f, 90 gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now, 91 int *has_missing, lsquic_packno_t *largest_received); 92 int 93 (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len, 94 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 95 lsquic_packno_t least_unacked_packno); 96 int 97 (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len, 98 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 99 lsquic_packno_t *least_unacked); 100 int 101 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum lsquic_packno_bits); 102 int 103 (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len, 104 uint32_t stream_id, uint64_t offset); 105 int 106 (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len, 107 uint32_t *stream_id, uint64_t *offset); 108 int 109 (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len, 110 uint32_t stream_id); 111 int 112 (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len, 113 uint32_t *stream_id); 114 int 115 (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id, 116 uint64_t offset, uint32_t error_code); 117 int 118 (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len, 119 uint32_t *stream_id, uint64_t *offset, uint32_t *error_code); 120 int 121 (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len, 122 uint32_t error_code, const char *reason, int reason_len); 123 int 124 (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len, 125 uint32_t *error_code, uint16_t *reason_length, 126 uint8_t *reason_offset); 127 int 128 (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len, 129 uint32_t error_code, uint32_t last_good_stream_id, 130 const char *reason, size_t reason_len); 131 int 132 (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len, 133 uint32_t *error_code, uint32_t *last_good_stream_id, 134 uint16_t *reason_length, const char **reason); 135 int 136 (*pf_gen_ping_frame) (unsigned char *buf, int buf_len); 137#ifndef NDEBUG 138 /* These float reading and writing functions assume `mem' has at least 139 * 2 bytes. 140 */ 141 void 142 (*pf_write_float_time16) (lsquic_time_t time_us, void *mem); 143 uint64_t 144 (*pf_read_float_time16) (const void *mem); 145#endif 146 size_t 147 (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset); 148 void 149 (*pf_turn_on_fin) (unsigned char *); 150 151 size_t 152 (*pf_packout_size) (const struct lsquic_conn *, 153 const struct lsquic_packet_out *); 154 155 size_t 156 (*pf_packout_header_size) (const struct lsquic_conn *, 157 enum packet_out_flags); 158}; 159 160extern const struct parse_funcs lsquic_parse_funcs_gquic_le; 161/* Q039 and later are big-endian: */ 162extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039; 163extern const struct parse_funcs lsquic_parse_funcs_gquic_Q044; 164 165#define select_pf_by_ver(ver) ( \ 166 ((1 << (ver)) & (1 << LSQVER_035)) \ 167 ? &lsquic_parse_funcs_gquic_le \ 168 : (ver) < LSQVER_044 \ 169 ? &lsquic_parse_funcs_gquic_Q039 \ 170 : &lsquic_parse_funcs_gquic_Q044) 171 172int 173lsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 174 int is_server, struct packin_parse_state *); 175 176int 177lsquic_iquic_parse_packet_in_long_begin (struct lsquic_packet_in *, 178 size_t length, int is_server, struct packin_parse_state *state); 179 180int 181lsquic_iquic_parse_packet_in_short_begin (struct lsquic_packet_in *, 182 size_t length, int is_server, struct packin_parse_state *state); 183 184enum QUIC_FRAME_TYPE 185parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte); 186 187size_t 188calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset); 189 190size_t 191lsquic_gquic_packout_size (const struct lsquic_conn *, 192 const struct lsquic_packet_out *); 193 194size_t 195lsquic_gquic_packout_header_size (const struct lsquic_conn *conn, 196 enum packet_out_flags flags); 197 198size_t 199lsquic_gquic_po_header_sz (enum packet_out_flags flags); 200 201/* This maps two bits as follows: 202 * 00 -> 1 203 * 01 -> 2 204 * 10 -> 4 205 * 11 -> 6 206 * 207 * Assumes that only two low bits are set. 208 */ 209#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 210 211/* This maps two bits as follows: 212 * 00 -> 1 213 * 01 -> 2 214 * 10 -> 4 215 * 11 -> 8 216 * 217 * Assumes that only two low bits are set. 218 */ 219#define twobit_to_1248(bits) (1 << (bits)) 220 221char * 222acki2str (const struct ack_info *acki, size_t *sz); 223 224void 225lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *); 226 227#endif 228