lsquic_parse.h revision 1b97e4af
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}; 157 158extern const struct parse_funcs lsquic_parse_funcs_gquic_le; 159/* Q039 and later are big-endian: */ 160extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039; 161extern const struct parse_funcs lsquic_parse_funcs_gquic_Q041; 162 163#define select_pf_by_ver(ver) ( \ 164 ((1 << (ver)) & ((1 << LSQVER_035) | \ 165 (1 << LSQVER_037) | (1 << LSQVER_038))) \ 166 ? &lsquic_parse_funcs_gquic_le : \ 167 ((1 << (ver)) & (1 << LSQVER_039)) \ 168 ? &lsquic_parse_funcs_gquic_Q039 \ 169 : &lsquic_parse_funcs_gquic_Q041) 170 171/* This function is QUIC-version independent */ 172int 173parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 174 int is_server, struct packin_parse_state *); 175 176enum QUIC_FRAME_TYPE 177parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte); 178 179enum QUIC_FRAME_TYPE 180parse_frame_type_gquic_Q041 (unsigned char first_byte); 181 182unsigned 183parse_stream_frame_header_sz_gquic (unsigned char type); 184 185size_t 186calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset); 187 188/* This maps two bits as follows: 189 * 00 -> 1 190 * 01 -> 2 191 * 10 -> 4 192 * 11 -> 6 193 * 194 * Assumes that only two low bits are set. 195 */ 196#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 197 198/* This maps two bits as follows: 199 * 00 -> 1 200 * 01 -> 2 201 * 10 -> 4 202 * 11 -> 8 203 * 204 * Assumes that only two low bits are set. 205 */ 206#define twobit_to_1248(bits) (1 << (bits)) 207 208char * 209acki2str (const struct ack_info *acki, size_t *sz); 210 211#endif 212