Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::quic::packet_number Class Reference

QUIC packet number utilities (RFC 9000 Section 17.1) More...

#include <packet.h>

Collaboration diagram for kcenon::network::protocols::quic::packet_number:
Collaboration graph

Static Public Member Functions

static auto encode (uint64_t full_pn, uint64_t largest_acked) -> std::pair< std::vector< uint8_t >, size_t >
 Encode a packet number for transmission.
 
static auto decode (uint64_t truncated_pn, size_t pn_length, uint64_t largest_pn) noexcept -> uint64_t
 Decode a packet number from received data.
 
static auto encoded_length (uint64_t full_pn, uint64_t largest_acked) noexcept -> size_t
 Get the minimum number of bytes needed to encode a packet number.
 

Detailed Description

QUIC packet number utilities (RFC 9000 Section 17.1)

Packet numbers are encoded using a variable-length encoding based on the difference from the largest acknowledged packet number.

Definition at line 173 of file packet.h.

Member Function Documentation

◆ decode()

auto kcenon::network::protocols::quic::packet_number::decode ( uint64_t truncated_pn,
size_t pn_length,
uint64_t largest_pn ) -> uint64_t
staticnodiscardnoexcept

Decode a packet number from received data.

Parameters
truncated_pnTruncated packet number from packet
pn_lengthLength of the truncated packet number (1-4)
largest_pnLargest packet number received so far
Returns
Full recovered packet number

Definition at line 103 of file packet.cpp.

105{
106 // RFC 9000 Appendix A
107 uint64_t expected_pn = largest_pn + 1;
108 uint64_t pn_win = 1ULL << (pn_length * 8);
109 uint64_t pn_hwin = pn_win / 2;
110 uint64_t pn_mask = pn_win - 1;
111
112 // Reconstruct the full packet number
113 uint64_t candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
114
115 // Handle wrap-around cases
116 if (candidate_pn <= expected_pn - pn_hwin && candidate_pn < (1ULL << 62) - pn_win)
117 {
118 return candidate_pn + pn_win;
119 }
120 if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win)
121 {
122 return candidate_pn - pn_win;
123 }
124 return candidate_pn;
125}

Referenced by kcenon::network::internal::quic_socket::handle_packet().

Here is the caller graph for this function:

◆ encode()

auto kcenon::network::protocols::quic::packet_number::encode ( uint64_t full_pn,
uint64_t largest_acked ) -> std::pair<std::vector<uint8_t>, size_t>
staticnodiscard

Encode a packet number for transmission.

Parameters
full_pnFull packet number to encode
largest_ackedLargest acknowledged packet number (0 if none)
Returns
Pair of (encoded bytes, number of bytes used)

Definition at line 88 of file packet.cpp.

90{
91 size_t len = encoded_length(full_pn, largest_acked);
92 std::vector<uint8_t> result(len);
93
94 // Encode in big-endian order
95 for (size_t i = 0; i < len; ++i)
96 {
97 result[len - 1 - i] = static_cast<uint8_t>(full_pn >> (i * 8));
98 }
99
100 return {result, len};
101}
static auto encoded_length(uint64_t full_pn, uint64_t largest_acked) noexcept -> size_t
Get the minimum number of bytes needed to encode a packet number.
Definition packet.cpp:127
constexpr uint8_t len
Length field present.
Definition frame_types.h:64

Referenced by kcenon::network::protocols::quic::packet_builder::build_handshake(), kcenon::network::protocols::quic::packet_builder::build_initial(), kcenon::network::protocols::quic::packet_builder::build_short(), and kcenon::network::protocols::quic::packet_builder::build_zero_rtt().

Here is the caller graph for this function:

◆ encoded_length()

auto kcenon::network::protocols::quic::packet_number::encoded_length ( uint64_t full_pn,
uint64_t largest_acked ) -> size_t
staticnodiscardnoexcept

Get the minimum number of bytes needed to encode a packet number.

Parameters
full_pnFull packet number
largest_ackedLargest acknowledged packet number
Returns
Number of bytes (1-4)

Definition at line 127 of file packet.cpp.

128{
129 uint64_t num_unacked = (full_pn > largest_acked) ? (full_pn - largest_acked) : 1;
130
131 if (num_unacked < (1ULL << 7))
132 {
133 return 1;
134 }
135 if (num_unacked < (1ULL << 15))
136 {
137 return 2;
138 }
139 if (num_unacked < (1ULL << 23))
140 {
141 return 3;
142 }
143 return 4;
144}

Referenced by kcenon::network::protocols::quic::packet_builder::build_handshake(), kcenon::network::protocols::quic::packet_builder::build_initial(), kcenon::network::protocols::quic::packet_builder::build_short(), and kcenon::network::protocols::quic::packet_builder::build_zero_rtt().

Here is the caller graph for this function:

The documentation for this class was generated from the following files: