lsquic_parse_gquic_be.h revision 50aadb33
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov#ifndef LSQUIC_PARSE_GQUIC_BE_H 350aadb33SDmitri Tikhonov#define LSQUIC_PARSE_GQUIC_BE_H 450aadb33SDmitri Tikhonov 550aadb33SDmitri Tikhonov/* Header file to make it easy to reference gQUIC parsing functions. This 650aadb33SDmitri Tikhonov * is only meant to be used internally. The alternative would be to place 750aadb33SDmitri Tikhonov * all gQUIC-big-endian functions -- from all versions -- in a single file, 850aadb33SDmitri Tikhonov * and that would be a mess. 950aadb33SDmitri Tikhonov */ 1050aadb33SDmitri Tikhonov 1150aadb33SDmitri Tikhonov#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) 1250aadb33SDmitri Tikhonov#include <sys/endian.h> 1350aadb33SDmitri Tikhonov#define bswap_16 bswap16 1450aadb33SDmitri Tikhonov#define bswap_32 bswap32 1550aadb33SDmitri Tikhonov#define bswap_64 bswap64 1650aadb33SDmitri Tikhonov#else 1750aadb33SDmitri Tikhonov#include <byteswap.h> 1850aadb33SDmitri Tikhonov#endif 1950aadb33SDmitri Tikhonov 2050aadb33SDmitri Tikhonov#define CHECK_SPACE(need, pstart, pend) \ 2150aadb33SDmitri Tikhonov do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0) 2250aadb33SDmitri Tikhonov 2350aadb33SDmitri Tikhonov#if __BYTE_ORDER == __LITTLE_ENDIAN 2450aadb33SDmitri Tikhonov#define READ_UINT(varname, varwidth, src, nbytes) do { \ 2550aadb33SDmitri Tikhonov varname = 0; \ 2650aadb33SDmitri Tikhonov memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src), \ 2750aadb33SDmitri Tikhonov (nbytes)); \ 2850aadb33SDmitri Tikhonov varname = bswap_##varwidth(varname); \ 2950aadb33SDmitri Tikhonov} while (0) 3050aadb33SDmitri Tikhonov#else 3150aadb33SDmitri Tikhonov#define READ_UINT(varname, varwidth, src, nbytes) do { \ 3250aadb33SDmitri Tikhonov varname = 0; \ 3350aadb33SDmitri Tikhonov memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src), \ 3450aadb33SDmitri Tikhonov (nbytes)); \ 3550aadb33SDmitri Tikhonov} while (0) 3650aadb33SDmitri Tikhonov#endif 3750aadb33SDmitri Tikhonov 3850aadb33SDmitri Tikhonovuint64_t 3950aadb33SDmitri Tikhonovgquic_be_read_float_time16 (const void *mem); 4050aadb33SDmitri Tikhonov 4150aadb33SDmitri Tikhonovvoid 4250aadb33SDmitri Tikhonovgquic_be_write_float_time16 (lsquic_time_t time_us, void *mem); 4350aadb33SDmitri Tikhonov 4450aadb33SDmitri Tikhonovvoid 4550aadb33SDmitri Tikhonovgquic_be_parse_packet_in_finish (lsquic_packet_in_t *packet_in, 4650aadb33SDmitri Tikhonov struct packin_parse_state *state); 4750aadb33SDmitri Tikhonov 4850aadb33SDmitri Tikhonovint 4950aadb33SDmitri Tikhonovgquic_be_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz, uint64_t conn_id, 5050aadb33SDmitri Tikhonov unsigned version_bitmask); 5150aadb33SDmitri Tikhonov 5250aadb33SDmitri Tikhonovint 5350aadb33SDmitri Tikhonovgquic_be_gen_reg_pkt_header (unsigned char *buf, size_t bufsz, const lsquic_cid_t *conn_id, 5450aadb33SDmitri Tikhonov const lsquic_ver_tag_t *ver, const unsigned char *nonce, 5550aadb33SDmitri Tikhonov lsquic_packno_t packno, enum lsquic_packno_bits bits); 5650aadb33SDmitri Tikhonov 5750aadb33SDmitri Tikhonovint 5850aadb33SDmitri Tikhonovgquic_be_gen_stream_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id, 5950aadb33SDmitri Tikhonov uint64_t offset, gsf_fin_f gsf_fin, gsf_size_f gsf_size, 6050aadb33SDmitri Tikhonov gsf_read_f gsf_read, void *stream); 6150aadb33SDmitri Tikhonov 6250aadb33SDmitri Tikhonovint 6350aadb33SDmitri Tikhonovgquic_be_parse_stream_frame (const unsigned char *buf, size_t rem_packet_sz, 6450aadb33SDmitri Tikhonov stream_frame_t *stream_frame); 6550aadb33SDmitri Tikhonov 6650aadb33SDmitri Tikhonovlsquic_packno_t 6750aadb33SDmitri Tikhonovgquic_be_parse_ack_high (const unsigned char *buf, size_t buf_len); 6850aadb33SDmitri Tikhonov 6950aadb33SDmitri Tikhonovint 7050aadb33SDmitri Tikhonovgquic_be_parse_ack_frame (const unsigned char *buf, size_t buf_len, ack_info_t *ack); 7150aadb33SDmitri Tikhonov 7250aadb33SDmitri Tikhonovint 7350aadb33SDmitri Tikhonovgquic_be_gen_stop_waiting_frame(unsigned char *buf, size_t buf_len, 7450aadb33SDmitri Tikhonov lsquic_packno_t cur_packno, enum lsquic_packno_bits bits, 7550aadb33SDmitri Tikhonov lsquic_packno_t least_unacked_packno); 7650aadb33SDmitri Tikhonov 7750aadb33SDmitri Tikhonovint 7850aadb33SDmitri Tikhonovgquic_be_parse_stop_waiting_frame (const unsigned char *buf, size_t buf_len, 7950aadb33SDmitri Tikhonov lsquic_packno_t cur_packno, enum lsquic_packno_bits bits, 8050aadb33SDmitri Tikhonov lsquic_packno_t *least_unacked); 8150aadb33SDmitri Tikhonov 8250aadb33SDmitri Tikhonovint 8350aadb33SDmitri Tikhonovgquic_be_skip_stop_waiting_frame (size_t buf_len, enum lsquic_packno_bits bits); 8450aadb33SDmitri Tikhonov 8550aadb33SDmitri Tikhonovint 8650aadb33SDmitri Tikhonovgquic_be_gen_window_update_frame (unsigned char *buf, int buf_len, uint32_t stream_id, 8750aadb33SDmitri Tikhonov uint64_t offset); 8850aadb33SDmitri Tikhonov 8950aadb33SDmitri Tikhonovint 9050aadb33SDmitri Tikhonovgquic_be_parse_window_update_frame (const unsigned char *buf, size_t buf_len, 9150aadb33SDmitri Tikhonov uint32_t *stream_id, uint64_t *offset); 9250aadb33SDmitri Tikhonov 9350aadb33SDmitri Tikhonovint 9450aadb33SDmitri Tikhonovgquic_be_gen_blocked_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id); 9550aadb33SDmitri Tikhonov 9650aadb33SDmitri Tikhonovint 9750aadb33SDmitri Tikhonovgquic_be_parse_blocked_frame (const unsigned char *buf, size_t buf_len, 9850aadb33SDmitri Tikhonov uint32_t *stream_id); 9950aadb33SDmitri Tikhonov 10050aadb33SDmitri Tikhonovint 10150aadb33SDmitri Tikhonovgquic_be_gen_rst_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id, 10250aadb33SDmitri Tikhonov uint64_t offset, uint32_t error_code); 10350aadb33SDmitri Tikhonov 10450aadb33SDmitri Tikhonovint 10550aadb33SDmitri Tikhonovgquic_be_parse_rst_frame (const unsigned char *buf, size_t buf_len, uint32_t *stream_id, 10650aadb33SDmitri Tikhonov uint64_t *offset, uint32_t *error_code); 10750aadb33SDmitri Tikhonov 10850aadb33SDmitri Tikhonovint 10950aadb33SDmitri Tikhonovgquic_be_gen_ping_frame (unsigned char *buf, int buf_len); 11050aadb33SDmitri Tikhonov 11150aadb33SDmitri Tikhonovint 11250aadb33SDmitri Tikhonovgquic_be_gen_connect_close_frame (unsigned char *buf, int buf_len, uint32_t error_code, 11350aadb33SDmitri Tikhonov const char *reason, int reason_len); 11450aadb33SDmitri Tikhonov 11550aadb33SDmitri Tikhonovint 11650aadb33SDmitri Tikhonovgquic_be_parse_connect_close_frame (const unsigned char *buf, size_t buf_len, 11750aadb33SDmitri Tikhonov uint32_t *error_code, uint16_t *reason_len, uint8_t *reason_offset); 11850aadb33SDmitri Tikhonov 11950aadb33SDmitri Tikhonovint 12050aadb33SDmitri Tikhonovgquic_be_gen_goaway_frame(unsigned char *buf, size_t buf_len, uint32_t error_code, 12150aadb33SDmitri Tikhonov uint32_t last_good_stream_id, const char *reason, 12250aadb33SDmitri Tikhonov size_t reason_len); 12350aadb33SDmitri Tikhonov 12450aadb33SDmitri Tikhonovint 12550aadb33SDmitri Tikhonovgquic_be_parse_goaway_frame (const unsigned char *buf, size_t buf_len, 12650aadb33SDmitri Tikhonov uint32_t *error_code, uint32_t *last_good_stream_id, 12750aadb33SDmitri Tikhonov uint16_t *reason_length, const char **reason); 12850aadb33SDmitri Tikhonov 12950aadb33SDmitri Tikhonovint 13050aadb33SDmitri Tikhonovgquic_be_gen_ack_frame (unsigned char *outbuf, size_t outbuf_sz, 13150aadb33SDmitri Tikhonov gaf_rechist_first_f rechist_first, gaf_rechist_next_f rechist_next, 13250aadb33SDmitri Tikhonov gaf_rechist_largest_recv_f rechist_largest_recv, 13350aadb33SDmitri Tikhonov void *rechist, lsquic_time_t now, int *has_missing); 13450aadb33SDmitri Tikhonov 13550aadb33SDmitri Tikhonov#endif 136