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