Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
frame_types.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <array>
8#include <cstdint>
9#include <optional>
10#include <string>
11#include <tuple>
12#include <variant>
13#include <vector>
14
16{
17
24enum class frame_type : uint64_t
25{
26 padding = 0x00,
27 ping = 0x01,
28 ack = 0x02,
29 ack_ecn = 0x03,
30 reset_stream = 0x04,
31 stop_sending = 0x05,
32 crypto = 0x06,
33 new_token = 0x07,
34 // STREAM frames: 0x08-0x0f (use stream_base and flags)
35 stream_base = 0x08,
36 max_data = 0x10,
37 max_stream_data = 0x11,
38 max_streams_bidi = 0x12,
39 max_streams_uni = 0x13,
40 data_blocked = 0x14,
44 new_connection_id = 0x18,
46 path_challenge = 0x1a,
47 path_response = 0x1b,
48 connection_close = 0x1c,
50 handshake_done = 0x1e,
51};
52
61namespace stream_flags
62{
63 constexpr uint8_t fin = 0x01;
64 constexpr uint8_t len = 0x02;
65 constexpr uint8_t off = 0x04;
66 constexpr uint8_t mask = 0x07;
67 constexpr uint8_t base = 0x08;
68} // namespace stream_flags
69
73[[nodiscard]] constexpr auto is_stream_frame(uint64_t type) noexcept -> bool
74{
75 return (type >= 0x08 && type <= 0x0f);
76}
77
81[[nodiscard]] constexpr auto get_stream_flags(uint64_t type) noexcept -> uint8_t
82{
83 return static_cast<uint8_t>(type & stream_flags::mask);
84}
85
89[[nodiscard]] constexpr auto make_stream_type(bool has_fin, bool has_length,
90 bool has_offset) noexcept -> uint8_t
91{
92 uint8_t type = stream_flags::base;
93 if (has_fin) type |= stream_flags::fin;
94 if (has_length) type |= stream_flags::len;
95 if (has_offset) type |= stream_flags::off;
96 return type;
97}
98
99// ============================================================================
100// Frame Structures (RFC 9000 Section 19)
101// ============================================================================
102
110{
111 size_t count{1};
112};
113
121{
122 // PING frames have no content
123};
124
129{
130 uint64_t gap{0};
131 uint64_t length{0};
132};
133
138{
139 uint64_t ect0{0};
140 uint64_t ect1{0};
141 uint64_t ecn_ce{0};
142};
143
151{
153 uint64_t ack_delay{0};
154 std::vector<ack_range> ranges;
155 std::optional<ecn_counts> ecn;
156};
157
165{
166 uint64_t stream_id{0};
168 uint64_t final_size{0};
169};
170
178{
179 uint64_t stream_id{0};
181};
182
189{
190 uint64_t offset{0};
191 std::vector<uint8_t> data;
192};
193
201{
202 std::vector<uint8_t> token;
203};
204
211{
212 uint64_t stream_id{0};
213 uint64_t offset{0};
214 std::vector<uint8_t> data;
215 bool fin{false};
216};
217
225{
226 uint64_t maximum_data{0};
227};
228
236{
237 uint64_t stream_id{0};
239};
240
248{
249 uint64_t maximum_streams{0};
250 bool bidirectional{true};
251};
252
260{
261 uint64_t maximum_data{0};
262};
263
271{
272 uint64_t stream_id{0};
274};
275
283{
284 uint64_t maximum_streams{0};
285 bool bidirectional{true};
286};
287
295{
296 uint64_t sequence_number{0};
297 uint64_t retire_prior_to{0};
298 std::vector<uint8_t> connection_id;
299 std::array<uint8_t, 16> stateless_reset_token{};
300};
301
309{
310 uint64_t sequence_number{0};
311};
312
320{
321 std::array<uint8_t, 8> data{};
322};
323
330{
331 std::array<uint8_t, 8> data{};
332};
333
341{
342 uint64_t error_code{0};
343 uint64_t frame_type{0};
344 std::string reason_phrase;
346};
347
355{
356 // HANDSHAKE_DONE frames have no content
357};
358
359// ============================================================================
360// Frame Variant
361// ============================================================================
362
366using frame = std::variant<
369 ack_frame,
387>;
388
392[[nodiscard]] auto get_frame_type(const frame& f) -> frame_type;
393
397[[nodiscard]] auto frame_type_to_string(frame_type type) -> std::string;
398
399} // namespace kcenon::network::protocols::quic
constexpr uint8_t fin
Stream is finished.
Definition frame_types.h:63
constexpr uint8_t len
Length field present.
Definition frame_types.h:64
constexpr uint8_t mask
Mask for all flags.
Definition frame_types.h:66
constexpr uint8_t base
Base type for STREAM frames.
Definition frame_types.h:67
constexpr uint8_t off
Offset field present.
Definition frame_types.h:65
constexpr auto is_stream_frame(uint64_t type) noexcept -> bool
Check if a frame type value represents a STREAM frame.
Definition frame_types.h:73
auto frame_type_to_string(frame_type type) -> std::string
Get string name for a frame type.
Definition frame.cpp:80
constexpr auto make_stream_type(bool has_fin, bool has_length, bool has_offset) noexcept -> uint8_t
Build STREAM frame type from flags.
Definition frame_types.h:89
frame_type
QUIC frame types as defined in RFC 9000 Section 12.4.
Definition frame_types.h:25
std::variant< padding_frame, ping_frame, ack_frame, reset_stream_frame, stop_sending_frame, crypto_frame, new_token_frame, stream_frame, max_data_frame, max_stream_data_frame, max_streams_frame, data_blocked_frame, stream_data_blocked_frame, streams_blocked_frame, new_connection_id_frame, retire_connection_id_frame, path_challenge_frame, path_response_frame, connection_close_frame, handshake_done_frame > frame
Variant type holding any QUIC frame.
auto get_frame_type(const frame &f) -> frame_type
Get the frame type for a frame variant.
Definition frame.cpp:28
constexpr auto get_stream_flags(uint64_t type) noexcept -> uint8_t
Extract STREAM flags from frame type.
Definition frame_types.h:81
ACK frame (RFC 9000 Section 19.3)
uint64_t ack_delay
Time since receiving largest_acknowledged (encoded)
std::optional< ecn_counts > ecn
ECN counts (for ACK_ECN frames)
uint64_t largest_acknowledged
Largest packet number acknowledged.
std::vector< ack_range > ranges
Additional ACK ranges.
uint64_t gap
Number of contiguous unacknowledged packets.
uint64_t length
Number of contiguous acknowledged packets.
CONNECTION_CLOSE frame (RFC 9000 Section 19.19)
uint64_t error_code
Error code indicating reason.
bool is_application_error
True if application-level error.
CRYPTO frame (RFC 9000 Section 19.6)
uint64_t offset
Byte offset in crypto stream.
std::vector< uint8_t > data
Cryptographic handshake data.
DATA_BLOCKED frame (RFC 9000 Section 19.12)
uint64_t maximum_data
Connection-level limit at which blocking occurred.
ECN counts for ACK_ECN frames.
HANDSHAKE_DONE frame (RFC 9000 Section 19.20)
MAX_DATA frame (RFC 9000 Section 19.9)
uint64_t maximum_data
Maximum data that can be sent.
MAX_STREAM_DATA frame (RFC 9000 Section 19.10)
MAX_STREAMS frame (RFC 9000 Section 19.11)
uint64_t maximum_streams
Maximum number of streams.
bool bidirectional
True for bidi, false for uni.
NEW_CONNECTION_ID frame (RFC 9000 Section 19.15)
std::vector< uint8_t > connection_id
Connection ID (1-20 bytes)
uint64_t sequence_number
Sequence number for this CID.
std::array< uint8_t, 16 > stateless_reset_token
Stateless reset token.
uint64_t retire_prior_to
CIDs below this should be retired.
NEW_TOKEN frame (RFC 9000 Section 19.7)
std::vector< uint8_t > token
Opaque token.
PADDING frame (RFC 9000 Section 19.1)
PATH_CHALLENGE frame (RFC 9000 Section 19.17)
std::array< uint8_t, 8 > data
Arbitrary 8-byte data.
PATH_RESPONSE frame (RFC 9000 Section 19.18)
std::array< uint8_t, 8 > data
Data from PATH_CHALLENGE.
PING frame (RFC 9000 Section 19.2)
RESET_STREAM frame (RFC 9000 Section 19.4)
uint64_t application_error_code
Application error code.
RETIRE_CONNECTION_ID frame (RFC 9000 Section 19.16)
uint64_t sequence_number
Sequence number of CID to retire.
STOP_SENDING frame (RFC 9000 Section 19.5)
uint64_t application_error_code
Application error code.
STREAM_DATA_BLOCKED frame (RFC 9000 Section 19.13)
uint64_t maximum_stream_data
Stream-level limit at which blocking occurred.
STREAM frame (RFC 9000 Section 19.8)
std::vector< uint8_t > data
Stream data.
uint64_t offset
Byte offset in stream (0 if not present)
bool fin
True if this is the final data.
STREAMS_BLOCKED frame (RFC 9000 Section 19.14)
uint64_t maximum_streams
Stream limit at which blocking occurred.
bool bidirectional
True for bidi, false for uni.