lsquic_parse_gquic_be.h revision 5d77f141
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2#ifndef LSQUIC_PARSE_GQUIC_BE_H
3#define LSQUIC_PARSE_GQUIC_BE_H
4
5/* Header file to make it easy to reference gQUIC parsing functions.  This
6 * is only meant to be used internally.  The alternative would be to place
7 * all gQUIC-big-endian functions -- from all versions -- in a single file,
8 * and that would be a mess.
9 */
10
11#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
12#include <sys/endian.h>
13#define bswap_16 bswap16
14#define bswap_32 bswap32
15#define bswap_64 bswap64
16#elif defined(__APPLE__)
17#include <libkern/OSByteOrder.h>
18#define bswap_16 OSSwapInt16
19#define bswap_32 OSSwapInt32
20#define bswap_64 OSSwapInt64
21#elif defined(WIN32)
22#define bswap_16 _byteswap_ushort
23#define bswap_32 _byteswap_ulong
24#define bswap_64 _byteswap_uint64
25#else
26#include <byteswap.h>
27#endif
28
29#define CHECK_SPACE(need, pstart, pend)  \
30    do { if ((intptr_t) (need) > ((pend) - (pstart))) { return -1; } } while (0)
31
32#if __BYTE_ORDER == __LITTLE_ENDIAN
33#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
34    varname = 0;                                                            \
35    memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src),     \
36                                                                (nbytes));  \
37    varname = bswap_##varwidth(varname);                                    \
38} while (0)
39#else
40#define READ_UINT(varname, varwidth, src, nbytes) do {                      \
41    varname = 0;                                                            \
42    memcpy((unsigned char *) &varname + varwidth / 8 - (nbytes), (src),     \
43                                                                (nbytes));  \
44} while (0)
45#endif
46
47uint64_t
48gquic_be_read_float_time16 (const void *mem);
49
50void
51gquic_be_write_float_time16 (lsquic_time_t time_us, void *mem);
52
53void
54gquic_be_parse_packet_in_finish (lsquic_packet_in_t *packet_in,
55                                            struct packin_parse_state *state);
56
57int
58gquic_be_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz, uint64_t conn_id,
59                  unsigned version_bitmask);
60
61int
62gquic_be_gen_reg_pkt_header (unsigned char *buf, size_t bufsz, const lsquic_cid_t *conn_id,
63                    const lsquic_ver_tag_t *ver, const unsigned char *nonce,
64                    lsquic_packno_t packno, enum lsquic_packno_bits bits);
65
66int
67gquic_be_gen_stream_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id,
68                  uint64_t offset, int fin, size_t size,
69                  gsf_read_f gsf_read, void *stream);
70
71int
72gquic_be_parse_stream_frame (const unsigned char *buf, size_t rem_packet_sz,
73                       stream_frame_t *stream_frame);
74
75lsquic_packno_t
76gquic_be_parse_ack_high (const unsigned char *buf, size_t buf_len);
77
78int
79gquic_be_parse_ack_frame (const unsigned char *buf, size_t buf_len, ack_info_t *ack);
80
81int
82gquic_be_gen_stop_waiting_frame(unsigned char *buf, size_t buf_len,
83                lsquic_packno_t cur_packno, enum lsquic_packno_bits bits,
84                lsquic_packno_t least_unacked_packno);
85
86int
87gquic_be_parse_stop_waiting_frame (const unsigned char *buf, size_t buf_len,
88                 lsquic_packno_t cur_packno, enum lsquic_packno_bits bits,
89                 lsquic_packno_t *least_unacked);
90
91int
92gquic_be_skip_stop_waiting_frame (size_t buf_len, enum lsquic_packno_bits bits);
93
94int
95gquic_be_gen_window_update_frame (unsigned char *buf, int buf_len, uint32_t stream_id,
96                         uint64_t offset);
97
98int
99gquic_be_parse_window_update_frame (const unsigned char *buf, size_t buf_len,
100                              uint32_t *stream_id, uint64_t *offset);
101
102int
103gquic_be_gen_blocked_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id);
104
105int
106gquic_be_parse_blocked_frame (const unsigned char *buf, size_t buf_len,
107                                                    uint32_t *stream_id);
108
109int
110gquic_be_gen_rst_frame (unsigned char *buf, size_t buf_len, uint32_t stream_id,
111                    uint64_t offset, uint32_t error_code);
112
113int
114gquic_be_parse_rst_frame (const unsigned char *buf, size_t buf_len, uint32_t *stream_id,
115                    uint64_t *offset, uint32_t *error_code);
116
117int
118gquic_be_gen_ping_frame (unsigned char *buf, int buf_len);
119
120int
121gquic_be_gen_connect_close_frame (unsigned char *buf, int buf_len, uint32_t error_code,
122                            const char *reason, int reason_len);
123
124int
125gquic_be_parse_connect_close_frame (const unsigned char *buf, size_t buf_len,
126        uint32_t *error_code, uint16_t *reason_len, uint8_t *reason_offset);
127
128int
129gquic_be_gen_goaway_frame(unsigned char *buf, size_t buf_len, uint32_t error_code,
130                     uint32_t last_good_stream_id, const char *reason,
131                     size_t reason_len);
132
133int
134gquic_be_parse_goaway_frame (const unsigned char *buf, size_t buf_len,
135                       uint32_t *error_code, uint32_t *last_good_stream_id,
136                       uint16_t *reason_length, const char **reason);
137
138int
139gquic_be_gen_ack_frame (unsigned char *outbuf, size_t outbuf_sz,
140        gaf_rechist_first_f rechist_first, gaf_rechist_next_f rechist_next,
141        gaf_rechist_largest_recv_f rechist_largest_recv,
142        void *rechist, lsquic_time_t now, int *has_missing, lsquic_packno_t *);
143
144#endif
145