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

QUIC Connection ID (RFC 9000 Section 5.1) More...

#include <connection_id.h>

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

Public Member Functions

 connection_id ()=default
 Default constructor creates an empty connection ID.
 
 connection_id (std::span< const uint8_t > data)
 Construct from raw bytes.
 
auto data () const -> std::span< const uint8_t >
 Get the raw bytes of the connection ID.
 
auto length () const noexcept -> size_t
 Get the length of the connection ID.
 
auto empty () const noexcept -> bool
 Check if the connection ID is empty.
 
auto operator== (const connection_id &other) const noexcept -> bool
 Equality comparison.
 
auto operator!= (const connection_id &other) const noexcept -> bool
 Inequality comparison.
 
auto operator< (const connection_id &other) const noexcept -> bool
 Less-than comparison for use in ordered containers.
 
auto to_string () const -> std::string
 Convert to hexadecimal string for debugging.
 

Static Public Member Functions

static auto generate (size_t length=8) -> connection_id
 Generate a random connection ID.
 

Static Public Attributes

static constexpr size_t max_length = 20
 Maximum length of a connection ID (RFC 9000)
 

Private Attributes

std::array< uint8_t, max_lengthdata_ {}
 
uint8_t length_ {0}
 

Detailed Description

QUIC Connection ID (RFC 9000 Section 5.1)

A connection ID is used to identify a QUIC connection. Connection IDs are used to route packets to the correct connection and to allow endpoints to change network addresses without breaking the connection.

Key properties (RFC 9000):

  • Length: 0 to 20 bytes (max_length = 20)
  • An endpoint generates connection IDs for its peer to use
  • Zero-length connection IDs are valid but limit connection migration
  • Connection IDs should be unpredictable to avoid linkability

Definition at line 43 of file connection_id.h.

Constructor & Destructor Documentation

◆ connection_id() [1/2]

kcenon::network::protocols::quic::connection_id::connection_id ( )
default

Default constructor creates an empty connection ID.

Referenced by kcenon::network::protocols::quic::preferred_address_info::operator==().

Here is the caller graph for this function:

◆ connection_id() [2/2]

kcenon::network::protocols::quic::connection_id::connection_id ( std::span< const uint8_t > data)
explicit

Construct from raw bytes.

Parameters
dataSpan of bytes (max 20)
Note
If data is longer than max_length, only the first max_length bytes are used

Definition at line 15 of file connection_id.cpp.

16{
17 size_t copy_len = std::min(data.size(), max_length);
18 std::copy_n(data.begin(), copy_len, data_.begin());
19 length_ = static_cast<uint8_t>(copy_len);
20}
std::array< uint8_t, max_length > data_
static constexpr size_t max_length
Maximum length of a connection ID (RFC 9000)
auto data() const -> std::span< const uint8_t >
Get the raw bytes of the connection ID.

References data(), data_, length_, and max_length.

Here is the call graph for this function:

Member Function Documentation

◆ data()

auto kcenon::network::protocols::quic::connection_id::data ( ) const -> std::span<const uint8_t>
nodiscard

Get the raw bytes of the connection ID.

Returns
Span of connection ID bytes

Definition at line 47 of file connection_id.cpp.

48{
49 return std::span<const uint8_t>(data_.data(), length_);
50}

References data_, and length_.

Referenced by connection_id().

Here is the caller graph for this function:

◆ empty()

auto kcenon::network::protocols::quic::connection_id::empty ( ) const -> bool
nodiscardnoexcept

Check if the connection ID is empty.

Returns
true if length is 0

Definition at line 57 of file connection_id.cpp.

58{
59 return length_ == 0;
60}

References length_.

◆ generate()

auto kcenon::network::protocols::quic::connection_id::generate ( size_t length = 8) -> connection_id
staticnodiscard

Generate a random connection ID.

Parameters
lengthDesired length (1-20, clamped to max_length)
Returns
Randomly generated connection ID

Definition at line 22 of file connection_id.cpp.

23{
24 connection_id cid;
25
26 // Clamp length to valid range
27 size_t actual_length = std::min(length, max_length);
28 if (actual_length == 0)
29 {
30 actual_length = 1; // Minimum 1 byte for non-empty CID
31 }
32
33 // Use random_device for cryptographically secure random bytes
34 std::random_device rd;
35 std::mt19937_64 gen(rd());
36 std::uniform_int_distribution<uint16_t> dist(0, 255);
37
38 for (size_t i = 0; i < actual_length; ++i)
39 {
40 cid.data_[i] = static_cast<uint8_t>(dist(gen));
41 }
42 cid.length_ = static_cast<uint8_t>(actual_length);
43
44 return cid;
45}
auto length() const noexcept -> size_t
Get the length of the connection ID.
connection_id()=default
Default constructor creates an empty connection ID.

References data_, and length_.

Referenced by kcenon::network::protocols::quic::connection::connection().

Here is the caller graph for this function:

◆ length()

auto kcenon::network::protocols::quic::connection_id::length ( ) const -> size_t
nodiscardnoexcept

Get the length of the connection ID.

Returns
Length in bytes (0-20)

Definition at line 52 of file connection_id.cpp.

53{
54 return length_;
55}

References length_.

◆ operator!=()

auto kcenon::network::protocols::quic::connection_id::operator!= ( const connection_id & other) const -> bool
nodiscardnoexcept

Inequality comparison.

Parameters
otherConnection ID to compare with
Returns
true if IDs are not equal

Definition at line 72 of file connection_id.cpp.

73{
74 return !(*this == other);
75}

◆ operator<()

auto kcenon::network::protocols::quic::connection_id::operator< ( const connection_id & other) const -> bool
nodiscardnoexcept

Less-than comparison for use in ordered containers.

Parameters
otherConnection ID to compare with
Returns
true if this ID is less than other

Definition at line 77 of file connection_id.cpp.

78{
79 // Compare by length first, then by content
80 if (length_ != other.length_)
81 {
82 return length_ < other.length_;
83 }
84 return std::lexicographical_compare(
85 data_.begin(), data_.begin() + length_,
86 other.data_.begin(), other.data_.begin() + other.length_);
87}

◆ operator==()

auto kcenon::network::protocols::quic::connection_id::operator== ( const connection_id & other) const -> bool
nodiscardnoexcept

Equality comparison.

Parameters
otherConnection ID to compare with
Returns
true if both IDs are equal

Definition at line 62 of file connection_id.cpp.

63{
64 if (length_ != other.length_)
65 {
66 return false;
67 }
68 return std::equal(data_.begin(), data_.begin() + length_,
69 other.data_.begin());
70}

◆ to_string()

auto kcenon::network::protocols::quic::connection_id::to_string ( ) const -> std::string
nodiscard

Convert to hexadecimal string for debugging.

Returns
Hexadecimal representation of the connection ID

Definition at line 89 of file connection_id.cpp.

90{
91 if (length_ == 0)
92 {
93 return "<empty>";
94 }
95
96 std::ostringstream oss;
97 oss << std::hex << std::setfill('0');
98 for (size_t i = 0; i < length_; ++i)
99 {
100 oss << std::setw(2) << static_cast<unsigned>(data_[i]);
101 }
102 return oss.str();
103}

References data_, and length_.

Member Data Documentation

◆ data_

std::array<uint8_t, max_length> kcenon::network::protocols::quic::connection_id::data_ {}
private

Definition at line 115 of file connection_id.h.

115{};

Referenced by connection_id(), data(), generate(), and to_string().

◆ length_

uint8_t kcenon::network::protocols::quic::connection_id::length_ {0}
private

Definition at line 116 of file connection_id.h.

116{0};

Referenced by connection_id(), data(), empty(), generate(), length(), and to_string().

◆ max_length

size_t kcenon::network::protocols::quic::connection_id::max_length = 20
staticconstexpr

Maximum length of a connection ID (RFC 9000)

Definition at line 47 of file connection_id.h.

Referenced by connection_id(), and kcenon::network::protocols::quic::packet_parser::parse_long_header().


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