lsquic_parse.h revision 8d029038
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 stream_frame; 11 12#define LSQUIC_PARSE_ACK_TIMESTAMPS 0 13 14typedef struct ack_info 15{ 16 unsigned n_timestamps; /* 0 to 255 */ 17 unsigned n_ranges; /* This is at least 1 */ 18 /* Largest acked is ack_info.ranges[0].high */ 19 lsquic_time_t lack_delta; 20 struct lsquic_packno_range ranges[256]; 21#if LSQUIC_PARSE_ACK_TIMESTAMPS 22 struct { 23 /* Currently we just read these timestamps in (assuming it is 24 * compiled in, of course), but do not do anything with them. 25 * When we do, the representation of these fields should be 26 * switched to whatever is most appropriate/efficient. 27 */ 28 unsigned char packet_delta; 29 uint64_t delta_usec; 30 } timestamps[255]; 31#endif 32} ack_info_t; 33 34struct short_ack_info 35{ 36 unsigned sai_n_timestamps; 37 lsquic_time_t sai_lack_delta; 38 struct lsquic_packno_range sai_range; 39}; 40 41#define largest_acked(acki) (+(acki)->ranges[0].high) 42 43#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low) 44 45/* gaf_: generate ACK frame */ 46struct lsquic_packno_range; 47typedef const struct lsquic_packno_range * 48 (*gaf_rechist_first_f) (void *rechist); 49typedef const struct lsquic_packno_range * 50 (*gaf_rechist_next_f) (void *rechist); 51typedef lsquic_time_t 52 (*gaf_rechist_largest_recv_f) (void *rechist); 53 54/* gsf_: generate stream frame */ 55typedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin); 56 57struct packin_parse_state { 58 const unsigned char *pps_p; /* Pointer to packet number */ 59 unsigned pps_nbytes; /* Number of bytes in packet number */ 60}; 61 62/* This structure contains functions that parse and generate packets and 63 * frames in version-specific manner. To begin with, there is difference 64 * between GQUIC's little-endian (Q038 and lower) and big-endian formats 65 * (Q039 and higher). 66 */ 67struct parse_funcs 68{ 69 int 70 (*pf_gen_ver_nego_pkt) (unsigned char *buf, size_t bufsz, uint64_t conn_id, 71 unsigned version_bitmask); 72 /* Return buf length */ 73 int 74 (*pf_gen_reg_pkt_header) (unsigned char *buf, size_t bufsz, 75 const lsquic_cid_t *, const lsquic_ver_tag_t *, 76 const unsigned char *nonce, lsquic_packno_t, 77 enum lsquic_packno_bits); 78 void 79 (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in, 80 struct packin_parse_state *); 81 enum QUIC_FRAME_TYPE 82 (*pf_parse_frame_type) (unsigned char); 83 /* Return used buffer length */ 84 int 85 (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz, 86 uint32_t stream_id, uint64_t offset, 87 int fin, size_t size, gsf_read_f, void *stream); 88 unsigned 89 (*pf_parse_stream_frame_header_sz) (unsigned char type); 90 int 91 (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz, 92 struct stream_frame *); 93 int 94 (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len, 95 ack_info_t *ack_info); 96 int 97 (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz, 98 gaf_rechist_first_f, gaf_rechist_next_f, 99 gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now, 100 int *has_missing, lsquic_packno_t *largest_received); 101 int 102 (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len, 103 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 104 lsquic_packno_t least_unacked_packno); 105 int 106 (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len, 107 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 108 lsquic_packno_t *least_unacked); 109 int 110 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum lsquic_packno_bits); 111 int 112 (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len, 113 uint32_t stream_id, uint64_t offset); 114 int 115 (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len, 116 uint32_t *stream_id, uint64_t *offset); 117 int 118 (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len, 119 uint32_t stream_id); 120 int 121 (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len, 122 uint32_t *stream_id); 123 int 124 (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id, 125 uint64_t offset, uint32_t error_code); 126 int 127 (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len, 128 uint32_t *stream_id, uint64_t *offset, uint32_t *error_code); 129 int 130 (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len, 131 uint32_t error_code, const char *reason, int reason_len); 132 int 133 (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len, 134 uint32_t *error_code, uint16_t *reason_length, 135 uint8_t *reason_offset); 136 int 137 (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len, 138 uint32_t error_code, uint32_t last_good_stream_id, 139 const char *reason, size_t reason_len); 140 int 141 (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len, 142 uint32_t *error_code, uint32_t *last_good_stream_id, 143 uint16_t *reason_length, const char **reason); 144 int 145 (*pf_gen_ping_frame) (unsigned char *buf, int buf_len); 146#ifndef NDEBUG 147 /* These float reading and writing functions assume `mem' has at least 148 * 2 bytes. 149 */ 150 void 151 (*pf_write_float_time16) (lsquic_time_t time_us, void *mem); 152 uint64_t 153 (*pf_read_float_time16) (const void *mem); 154#endif 155 size_t 156 (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset); 157 void 158 (*pf_turn_on_fin) (unsigned char *); 159}; 160 161extern const struct parse_funcs lsquic_parse_funcs_gquic_le; 162/* Q039 and later are big-endian: */ 163extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039; 164 165#define select_pf_by_ver(ver) ( \ 166 ((1 << (ver)) & (1 << LSQVER_035)) \ 167 ? &lsquic_parse_funcs_gquic_le \ 168 : &lsquic_parse_funcs_gquic_Q039) 169 170/* This function is QUIC-version independent */ 171int 172parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 173 int is_server, struct packin_parse_state *); 174 175enum QUIC_FRAME_TYPE 176parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte); 177 178unsigned 179parse_stream_frame_header_sz_gquic (unsigned char type); 180 181size_t 182calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset); 183 184/* This maps two bits as follows: 185 * 00 -> 1 186 * 01 -> 2 187 * 10 -> 4 188 * 11 -> 6 189 * 190 * Assumes that only two low bits are set. 191 */ 192#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 193 194/* This maps two bits as follows: 195 * 00 -> 1 196 * 01 -> 2 197 * 10 -> 4 198 * 11 -> 8 199 * 200 * Assumes that only two low bits are set. 201 */ 202#define twobit_to_1248(bits) (1 << (bits)) 203 204char * 205acki2str (const struct ack_info *acki, size_t *sz); 206 207void 208lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *); 209 210#endif 211