lsquic_parse_gquic_be.h revision e0197994
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
16e0197994SDmitri Tikhonov#elif defined(__APPLE__)
17e0197994SDmitri Tikhonov#include <libkern/OSByteOrder.h>
18e0197994SDmitri Tikhonov#define bswap_16 OSSwapInt16
19e0197994SDmitri Tikhonov#define bswap_32 OSSwapInt32
20e0197994SDmitri Tikhonov#define bswap_64 OSSwapInt64
2150aadb33SDmitri Tikhonov#else
2250aadb33SDmitri Tikhonov#include <byteswap.h>
2350aadb33SDmitri Tikhonov#endif
2450aadb33SDmitri Tikhonov
2550aadb33SDmitri Tikhonov#define CHECK_SPACE(need, pstart, pend)  \
2650aadb33SDmitri Tikhonov    do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0)
2750aadb33SDmitri Tikhonov
2850aadb33SDmitri Tikhonov#if __BYTE_ORDER == __LITTLE_ENDIAN
2950aadb33SDmitri Tikhonov#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
3050aadb33SDmitri Tikhonov    varname = 0;                                                            \
3150aadb33SDmitri Tikhonov    memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src),     \
3250aadb33SDmitri Tikhonov                                                                (nbytes));  \
3350aadb33SDmitri Tikhonov    varname = bswap_##varwidth(varname);                                    \
3450aadb33SDmitri Tikhonov} while (0)
3550aadb33SDmitri Tikhonov#else
3650aadb33SDmitri Tikhonov#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
3750aadb33SDmitri Tikhonov    varname = 0;                                                            \
3850aadb33SDmitri Tikhonov    memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src),     \
3950aadb33SDmitri Tikhonov                                                                (nbytes));  \
4050aadb33SDmitri Tikhonov} while (0)
4150aadb33SDmitri Tikhonov#endif
4250aadb33SDmitri Tikhonov
4350aadb33SDmitri Tikhonovuint64_t
4450aadb33SDmitri Tikhonovgquic_be_read_float_time16 (const void *mem);
4550aadb33SDmitri Tikhonov
4650aadb33SDmitri Tikhonovvoid
4750aadb33SDmitri Tikhonovgquic_be_write_float_time16 (lsquic_time_t time_us, void *mem);
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonovvoid
5050aadb33SDmitri Tikhonovgquic_be_parse_packet_in_finish (lsquic_packet_in_t *packet_in,
5150aadb33SDmitri Tikhonov                                            struct packin_parse_state *state);
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonovint
5450aadb33SDmitri Tikhonovgquic_be_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz, uint64_t conn_id,
5550aadb33SDmitri Tikhonov                  unsigned version_bitmask);
5650aadb33SDmitri Tikhonov
5750aadb33SDmitri Tikhonovint
5850aadb33SDmitri Tikhonovgquic_be_gen_reg_pkt_header (unsigned char *buf, size_t bufsz, const lsquic_cid_t *conn_id,
5950aadb33SDmitri Tikhonov                    const lsquic_ver_tag_t *ver, const unsigned char *nonce,
6050aadb33SDmitri Tikhonov                    lsquic_packno_t packno, enum lsquic_packno_bits bits);
6150aadb33SDmitri Tikhonov
6250aadb33SDmitri Tikhonovint
6350aadb33SDmitri Tikhonovgquic_be_gen_stream_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id,
6450aadb33SDmitri Tikhonov                  uint64_t offset, gsf_fin_f gsf_fin, gsf_size_f gsf_size,
6550aadb33SDmitri Tikhonov                  gsf_read_f gsf_read, void *stream);
6650aadb33SDmitri Tikhonov
6750aadb33SDmitri Tikhonovint
6850aadb33SDmitri Tikhonovgquic_be_parse_stream_frame (const unsigned char *buf, size_t rem_packet_sz,
6950aadb33SDmitri Tikhonov                       stream_frame_t *stream_frame);
7050aadb33SDmitri Tikhonov
7150aadb33SDmitri Tikhonovlsquic_packno_t
7250aadb33SDmitri Tikhonovgquic_be_parse_ack_high (const unsigned char *buf, size_t buf_len);
7350aadb33SDmitri Tikhonov
7450aadb33SDmitri Tikhonovint
7550aadb33SDmitri Tikhonovgquic_be_parse_ack_frame (const unsigned char *buf, size_t buf_len, ack_info_t *ack);
7650aadb33SDmitri Tikhonov
7750aadb33SDmitri Tikhonovint
7850aadb33SDmitri Tikhonovgquic_be_gen_stop_waiting_frame(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_packno);
8150aadb33SDmitri Tikhonov
8250aadb33SDmitri Tikhonovint
8350aadb33SDmitri Tikhonovgquic_be_parse_stop_waiting_frame (const unsigned char *buf, size_t buf_len,
8450aadb33SDmitri Tikhonov                 lsquic_packno_t cur_packno, enum lsquic_packno_bits bits,
8550aadb33SDmitri Tikhonov                 lsquic_packno_t *least_unacked);
8650aadb33SDmitri Tikhonov
8750aadb33SDmitri Tikhonovint
8850aadb33SDmitri Tikhonovgquic_be_skip_stop_waiting_frame (size_t buf_len, enum lsquic_packno_bits bits);
8950aadb33SDmitri Tikhonov
9050aadb33SDmitri Tikhonovint
9150aadb33SDmitri Tikhonovgquic_be_gen_window_update_frame (unsigned char *buf, int buf_len, uint32_t stream_id,
9250aadb33SDmitri Tikhonov                         uint64_t offset);
9350aadb33SDmitri Tikhonov
9450aadb33SDmitri Tikhonovint
9550aadb33SDmitri Tikhonovgquic_be_parse_window_update_frame (const unsigned char *buf, size_t buf_len,
9650aadb33SDmitri Tikhonov                              uint32_t *stream_id, uint64_t *offset);
9750aadb33SDmitri Tikhonov
9850aadb33SDmitri Tikhonovint
9950aadb33SDmitri Tikhonovgquic_be_gen_blocked_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id);
10050aadb33SDmitri Tikhonov
10150aadb33SDmitri Tikhonovint
10250aadb33SDmitri Tikhonovgquic_be_parse_blocked_frame (const unsigned char *buf, size_t buf_len,
10350aadb33SDmitri Tikhonov                                                    uint32_t *stream_id);
10450aadb33SDmitri Tikhonov
10550aadb33SDmitri Tikhonovint
10650aadb33SDmitri Tikhonovgquic_be_gen_rst_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id,
10750aadb33SDmitri Tikhonov                    uint64_t offset, uint32_t error_code);
10850aadb33SDmitri Tikhonov
10950aadb33SDmitri Tikhonovint
11050aadb33SDmitri Tikhonovgquic_be_parse_rst_frame (const unsigned char *buf, size_t buf_len, uint32_t *stream_id,
11150aadb33SDmitri Tikhonov                    uint64_t *offset, uint32_t *error_code);
11250aadb33SDmitri Tikhonov
11350aadb33SDmitri Tikhonovint
11450aadb33SDmitri Tikhonovgquic_be_gen_ping_frame (unsigned char *buf, int buf_len);
11550aadb33SDmitri Tikhonov
11650aadb33SDmitri Tikhonovint
11750aadb33SDmitri Tikhonovgquic_be_gen_connect_close_frame (unsigned char *buf, int buf_len, uint32_t error_code,
11850aadb33SDmitri Tikhonov                            const char *reason, int reason_len);
11950aadb33SDmitri Tikhonov
12050aadb33SDmitri Tikhonovint
12150aadb33SDmitri Tikhonovgquic_be_parse_connect_close_frame (const unsigned char *buf, size_t buf_len,
12250aadb33SDmitri Tikhonov        uint32_t *error_code, uint16_t *reason_len, uint8_t *reason_offset);
12350aadb33SDmitri Tikhonov
12450aadb33SDmitri Tikhonovint
12550aadb33SDmitri Tikhonovgquic_be_gen_goaway_frame(unsigned char *buf, size_t buf_len, uint32_t error_code,
12650aadb33SDmitri Tikhonov                     uint32_t last_good_stream_id, const char *reason,
12750aadb33SDmitri Tikhonov                     size_t reason_len);
12850aadb33SDmitri Tikhonov
12950aadb33SDmitri Tikhonovint
13050aadb33SDmitri Tikhonovgquic_be_parse_goaway_frame (const unsigned char *buf, size_t buf_len,
13150aadb33SDmitri Tikhonov                       uint32_t *error_code, uint32_t *last_good_stream_id,
13250aadb33SDmitri Tikhonov                       uint16_t *reason_length, const char **reason);
13350aadb33SDmitri Tikhonov
13450aadb33SDmitri Tikhonovint
13550aadb33SDmitri Tikhonovgquic_be_gen_ack_frame (unsigned char *outbuf, size_t outbuf_sz,
13650aadb33SDmitri Tikhonov        gaf_rechist_first_f rechist_first, gaf_rechist_next_f rechist_next,
13750aadb33SDmitri Tikhonov        gaf_rechist_largest_recv_f rechist_largest_recv,
13850aadb33SDmitri Tikhonov        void *rechist, lsquic_time_t now, int *has_missing);
13950aadb33SDmitri Tikhonov
14050aadb33SDmitri Tikhonov#endif
141