lsquic_parse.h revision bdf79b05
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 int 89 (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz, 90 struct stream_frame *); 91 int 92 (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len, 93 ack_info_t *ack_info); 94 int 95 (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz, 96 gaf_rechist_first_f, gaf_rechist_next_f, 97 gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now, 98 int *has_missing, lsquic_packno_t *largest_received); 99 int 100 (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len, 101 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 102 lsquic_packno_t least_unacked_packno); 103 int 104 (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len, 105 lsquic_packno_t cur_packno, enum lsquic_packno_bits, 106 lsquic_packno_t *least_unacked); 107 int 108 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum lsquic_packno_bits); 109 int 110 (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len, 111 uint32_t stream_id, uint64_t offset); 112 int 113 (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len, 114 uint32_t *stream_id, uint64_t *offset); 115 int 116 (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len, 117 uint32_t stream_id); 118 int 119 (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len, 120 uint32_t *stream_id); 121 int 122 (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, uint32_t stream_id, 123 uint64_t offset, uint32_t error_code); 124 int 125 (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len, 126 uint32_t *stream_id, uint64_t *offset, uint32_t *error_code); 127 int 128 (*pf_gen_connect_close_frame) (unsigned char *buf, int buf_len, 129 uint32_t error_code, const char *reason, int reason_len); 130 int 131 (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len, 132 uint32_t *error_code, uint16_t *reason_length, 133 uint8_t *reason_offset); 134 int 135 (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len, 136 uint32_t error_code, uint32_t last_good_stream_id, 137 const char *reason, size_t reason_len); 138 int 139 (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len, 140 uint32_t *error_code, uint32_t *last_good_stream_id, 141 uint16_t *reason_length, const char **reason); 142 int 143 (*pf_gen_ping_frame) (unsigned char *buf, int buf_len); 144#ifndef NDEBUG 145 /* These float reading and writing functions assume `mem' has at least 146 * 2 bytes. 147 */ 148 void 149 (*pf_write_float_time16) (lsquic_time_t time_us, void *mem); 150 uint64_t 151 (*pf_read_float_time16) (const void *mem); 152#endif 153 size_t 154 (*pf_calc_stream_frame_header_sz) (uint32_t stream_id, uint64_t offset); 155 void 156 (*pf_turn_on_fin) (unsigned char *); 157}; 158 159extern const struct parse_funcs lsquic_parse_funcs_gquic_le; 160/* Q039 and later are big-endian: */ 161extern const struct parse_funcs lsquic_parse_funcs_gquic_Q039; 162 163#define select_pf_by_ver(ver) ( \ 164 ((1 << (ver)) & (1 << LSQVER_035)) \ 165 ? &lsquic_parse_funcs_gquic_le \ 166 : &lsquic_parse_funcs_gquic_Q039) 167 168/* This function is QUIC-version independent */ 169int 170parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 171 int is_server, struct packin_parse_state *); 172 173enum QUIC_FRAME_TYPE 174parse_frame_type_gquic_Q035_thru_Q039 (unsigned char first_byte); 175 176size_t 177calc_stream_frame_header_sz_gquic (uint32_t stream_id, uint64_t offset); 178 179/* This maps two bits as follows: 180 * 00 -> 1 181 * 01 -> 2 182 * 10 -> 4 183 * 11 -> 6 184 * 185 * Assumes that only two low bits are set. 186 */ 187#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 188 189/* This maps two bits as follows: 190 * 00 -> 1 191 * 01 -> 2 192 * 10 -> 4 193 * 11 -> 8 194 * 195 * Assumes that only two low bits are set. 196 */ 197#define twobit_to_1248(bits) (1 << (bits)) 198 199char * 200acki2str (const struct ack_info *acki, size_t *sz); 201 202void 203lsquic_turn_on_fin_Q035_thru_Q039 (unsigned char *); 204 205#endif 206