Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
packet.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
9
10#include <cstdint>
11#include <span>
12#include <utility>
13#include <variant>
14#include <vector>
15
17{
18
19// ============================================================================
20// QUIC Versions
21// ============================================================================
22
26namespace quic_version
27{
29 constexpr uint32_t version_1 = 0x00000001;
30
32 constexpr uint32_t version_2 = 0x6b3343cf;
33
35 constexpr uint32_t negotiation = 0x00000000;
36} // namespace quic_version
37
38// ============================================================================
39// Packet Types
40// ============================================================================
41
54enum class packet_type : uint8_t
55{
56 initial = 0x00,
57 zero_rtt = 0x01,
58 handshake = 0x02,
59 retry = 0x03,
60 one_rtt = 0xFF, // Special marker for short header packets
61};
62
66[[nodiscard]] auto packet_type_to_string(packet_type type) -> std::string;
67
68// ============================================================================
69// Header Structures
70// ============================================================================
71
95{
96 uint8_t first_byte{0};
97 uint32_t version{0};
100
101 // Type-specific fields
102 std::vector<uint8_t> token;
103 uint64_t packet_number{0};
105
107 std::array<uint8_t, 16> retry_integrity_tag{};
108
113 [[nodiscard]] auto type() const noexcept -> packet_type;
114
118 [[nodiscard]] auto is_retry() const noexcept -> bool;
119};
120
138{
139 uint8_t first_byte{0};
141 uint64_t packet_number{0};
143
148 [[nodiscard]] auto spin_bit() const noexcept -> bool;
149
154 [[nodiscard]] auto key_phase() const noexcept -> bool;
155};
156
161
162// ============================================================================
163// Packet Number Encoding
164// ============================================================================
165
174{
175public:
182 [[nodiscard]] static auto encode(uint64_t full_pn, uint64_t largest_acked)
183 -> std::pair<std::vector<uint8_t>, size_t>;
184
192 [[nodiscard]] static auto decode(uint64_t truncated_pn, size_t pn_length,
193 uint64_t largest_pn) noexcept -> uint64_t;
194
201 [[nodiscard]] static auto encoded_length(uint64_t full_pn,
202 uint64_t largest_acked) noexcept -> size_t;
203};
204
205// ============================================================================
206// Packet Parser
207// ============================================================================
208
218{
219public:
225 [[nodiscard]] static constexpr auto is_long_header(uint8_t first_byte) noexcept -> bool
226 {
227 return (first_byte & 0x80) != 0;
228 }
229
235 [[nodiscard]] static constexpr auto has_valid_fixed_bit(uint8_t first_byte) noexcept -> bool
236 {
237 return (first_byte & 0x40) != 0;
238 }
239
247 [[nodiscard]] static auto parse_header(std::span<const uint8_t> data)
249
255 [[nodiscard]] static auto parse_long_header(std::span<const uint8_t> data)
257
264 [[nodiscard]] static auto parse_short_header(std::span<const uint8_t> data,
265 size_t conn_id_length)
267
273 [[nodiscard]] static constexpr auto get_long_packet_type(uint8_t first_byte) noexcept
274 -> packet_type
275 {
276 return static_cast<packet_type>((first_byte >> 4) & 0x03);
277 }
278
284 [[nodiscard]] static auto is_version_negotiation(std::span<const uint8_t> data) noexcept
285 -> bool;
286};
287
288// ============================================================================
289// Packet Builder
290// ============================================================================
291
300{
301public:
311 [[nodiscard]] static auto build_initial(
312 const connection_id& dest_cid,
313 const connection_id& src_cid,
314 const std::vector<uint8_t>& token,
315 uint64_t packet_number,
316 uint32_t version = quic_version::version_1) -> std::vector<uint8_t>;
317
326 [[nodiscard]] static auto build_handshake(
327 const connection_id& dest_cid,
328 const connection_id& src_cid,
329 uint64_t packet_number,
330 uint32_t version = quic_version::version_1) -> std::vector<uint8_t>;
331
340 [[nodiscard]] static auto build_zero_rtt(
341 const connection_id& dest_cid,
342 const connection_id& src_cid,
343 uint64_t packet_number,
344 uint32_t version = quic_version::version_1) -> std::vector<uint8_t>;
345
355 [[nodiscard]] static auto build_retry(
356 const connection_id& dest_cid,
357 const connection_id& src_cid,
358 const std::vector<uint8_t>& token,
359 const std::array<uint8_t, 16>& integrity_tag,
360 uint32_t version = quic_version::version_1) -> std::vector<uint8_t>;
361
370 [[nodiscard]] static auto build_short(
371 const connection_id& dest_cid,
372 uint64_t packet_number,
373 bool key_phase = false,
374 bool spin_bit = false) -> std::vector<uint8_t>;
375
381 [[nodiscard]] static auto build(const long_header& header) -> std::vector<uint8_t>;
382
388 [[nodiscard]] static auto build(const short_header& header) -> std::vector<uint8_t>;
389
390private:
392 static void append_bytes(std::vector<uint8_t>& buffer,
393 std::span<const uint8_t> data);
394};
395
396} // namespace kcenon::network::protocols::quic
QUIC Connection ID (RFC 9000 Section 5.1)
Builder for QUIC packet headers (RFC 9000 Section 17)
Definition packet.h:300
QUIC packet number utilities (RFC 9000 Section 17.1)
Definition packet.h:174
Parser for QUIC packet headers (RFC 9000 Section 17)
Definition packet.h:218
static constexpr auto has_valid_fixed_bit(uint8_t first_byte) noexcept -> bool
Check if the fixed bit is set correctly.
Definition packet.h:235
static constexpr auto is_long_header(uint8_t first_byte) noexcept -> bool
Check if a packet has a long header.
Definition packet.h:225
static constexpr auto get_long_packet_type(uint8_t first_byte) noexcept -> packet_type
Get the packet type from long header's first byte.
Definition packet.h:273
QUIC connection identifier type.
constexpr uint32_t version_2
QUIC version 2 (RFC 9369)
Definition packet.h:32
constexpr uint32_t negotiation
Version Negotiation (special value)
Definition packet.h:35
constexpr uint32_t version_1
QUIC version 1 (RFC 9000)
Definition packet.h:29
auto packet_type_to_string(packet_type type) -> std::string
Convert packet type to string for debugging.
Definition packet.cpp:43
packet_type
QUIC packet types (RFC 9000 Section 17)
Definition packet.h:55
std::variant< long_header, short_header > packet_header
Variant type for packet headers.
Definition packet.h:160
constexpr const char * version() noexcept
Get the network system version string.
Definition network.cppm:111
Network-specific error and result type definitions.
QUIC Long Header format (RFC 9000 Section 17.2)
Definition packet.h:95
std::array< uint8_t, 16 > retry_integrity_tag
Retry integrity tag (Retry packets only, 16 bytes)
Definition packet.h:107
std::vector< uint8_t > token
Token (Initial and Retry only)
Definition packet.h:102
connection_id dest_conn_id
Destination Connection ID.
Definition packet.h:98
size_t packet_number_length
Packet number length (1-4 bytes)
Definition packet.h:104
auto type() const noexcept -> packet_type
Get the packet type from first byte.
Definition packet.cpp:60
uint8_t first_byte
Header form, fixed bit, type, reserved, PN length.
Definition packet.h:96
auto is_retry() const noexcept -> bool
Check if this is a Retry packet (has integrity tag, no packet number)
Definition packet.cpp:65
connection_id src_conn_id
Source Connection ID.
Definition packet.h:99
QUIC Short Header format (RFC 9000 Section 17.3)
Definition packet.h:138
connection_id dest_conn_id
Destination Connection ID.
Definition packet.h:140