Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
connection_id.cpp
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
6
7#include <algorithm>
8#include <random>
9#include <sstream>
10#include <iomanip>
11
13{
14
15connection_id::connection_id(std::span<const uint8_t> data)
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}
21
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}
46
47auto connection_id::data() const -> std::span<const uint8_t>
48{
49 return std::span<const uint8_t>(data_.data(), length_);
50}
51
52auto connection_id::length() const noexcept -> size_t
53{
54 return length_;
55}
56
57auto connection_id::empty() const noexcept -> bool
58{
59 return length_ == 0;
60}
61
62auto connection_id::operator==(const connection_id& other) const noexcept -> bool
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}
71
72auto connection_id::operator!=(const connection_id& other) const noexcept -> bool
73{
74 return !(*this == other);
75}
76
77auto connection_id::operator<(const connection_id& other) const noexcept -> bool
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}
88
89auto connection_id::to_string() const -> std::string
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}
104
105} // namespace kcenon::network::protocols::quic
QUIC Connection ID (RFC 9000 Section 5.1)
std::array< uint8_t, max_length > data_
auto length() const noexcept -> size_t
Get the length of the connection ID.
auto to_string() const -> std::string
Convert to hexadecimal string for debugging.
auto empty() const noexcept -> bool
Check if the connection ID is empty.
connection_id()=default
Default constructor creates an empty connection ID.
static auto generate(size_t length=8) -> connection_id
Generate a random connection ID.
static constexpr size_t max_length
Maximum length of a connection ID (RFC 9000)
auto operator==(const connection_id &other) const noexcept -> bool
Equality comparison.
auto operator!=(const connection_id &other) const noexcept -> bool
Inequality comparison.
auto data() const -> std::span< const uint8_t >
Get the raw bytes of the connection ID.
auto operator<(const connection_id &other) const noexcept -> bool
Less-than comparison for use in ordered containers.
QUIC connection identifier type.