Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
connection_id_manager.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
8#include "frame_types.h"
10
11#include <array>
12#include <cstdint>
13#include <vector>
14
16{
17
18// ============================================================================
19// Connection ID Manager Error Codes
20// ============================================================================
21
22namespace cid_manager_error
23{
24 constexpr int duplicate_sequence = -740;
25 constexpr int sequence_too_low = -741;
26 constexpr int no_available_cid = -742;
27 constexpr int cid_not_found = -743;
28 constexpr int invalid_retire_prior_to = -744;
29 constexpr int active_cid_limit_exceeded = -745;
30} // namespace cid_manager_error
31
32// ============================================================================
33// Connection ID Entry
34// ============================================================================
35
47{
49 uint64_t sequence_number{0};
50 std::array<uint8_t, 16> stateless_reset_token{};
51 bool retired{false};
52};
53
54// ============================================================================
55// Connection ID Manager
56// ============================================================================
57
76{
77public:
82 explicit connection_id_manager(uint64_t active_cid_limit = 8);
83
85
86 // Non-copyable but movable
91
92 // ========================================================================
93 // Peer Connection ID Management
94 // ========================================================================
95
103 void set_initial_peer_cid(const connection_id& cid);
104
113 [[nodiscard]] auto add_peer_cid(const connection_id& cid,
114 uint64_t sequence,
115 uint64_t retire_prior_to,
116 const std::array<uint8_t, 16>& reset_token) -> VoidResult;
117
122 [[nodiscard]] auto get_active_peer_cid() const -> const connection_id&;
123
131 [[nodiscard]] auto rotate_peer_cid() -> VoidResult;
132
136 [[nodiscard]] auto available_peer_cids() const -> size_t;
137
143 [[nodiscard]] auto is_stateless_reset_token(
144 const std::array<uint8_t, 16>& token) const -> bool;
145
146 // ========================================================================
147 // Retirement Management
148 // ========================================================================
149
156 void retire_cids_prior_to(uint64_t prior_to);
157
162 [[nodiscard]] auto get_pending_retire_frames() -> std::vector<retire_connection_id_frame>;
163
170
176 [[nodiscard]] auto retire_peer_cid(uint64_t sequence) -> VoidResult;
177
178 // ========================================================================
179 // State Queries
180 // ========================================================================
181
185 [[nodiscard]] auto largest_retire_prior_to() const -> uint64_t
186 {
188 }
189
193 [[nodiscard]] auto peer_cid_count() const -> size_t { return peer_cids_.size(); }
194
198 [[nodiscard]] auto active_cid_limit() const -> uint64_t { return active_cid_limit_; }
199
204 void set_active_cid_limit(uint64_t limit) { active_cid_limit_ = limit; }
205
211 [[nodiscard]] auto has_peer_cid(const connection_id& cid) const -> bool;
212
213private:
216
218 std::vector<connection_id_entry> peer_cids_;
219
221 size_t active_index_{0};
222
225
227 std::vector<retire_connection_id_frame> pending_retire_frames_;
228
231
235 [[nodiscard]] auto find_by_sequence(uint64_t sequence)
236 -> std::vector<connection_id_entry>::iterator;
237
241 [[nodiscard]] auto count_active_cids() const -> size_t;
242};
243
244} // namespace kcenon::network::protocols::quic
Manages peer connection IDs for QUIC connections (RFC 9000 Section 5.1)
auto get_pending_retire_frames() -> std::vector< retire_connection_id_frame >
Get pending RETIRE_CONNECTION_ID frames to send.
auto add_peer_cid(const connection_id &cid, uint64_t sequence, uint64_t retire_prior_to, const std::array< uint8_t, 16 > &reset_token) -> VoidResult
Add a new peer connection ID from NEW_CONNECTION_ID frame.
connection_id_manager(const connection_id_manager &)=delete
auto retire_peer_cid(uint64_t sequence) -> VoidResult
Mark a specific CID sequence as retired.
auto has_peer_cid(const connection_id &cid) const -> bool
Check if a given CID matches any stored peer CID.
auto peer_cid_count() const -> size_t
Get the current number of peer CIDs (including retired)
auto rotate_peer_cid() -> VoidResult
Rotate to a new peer connection ID.
auto operator=(const connection_id_manager &) -> connection_id_manager &=delete
auto active_cid_limit() const -> uint64_t
Get the active connection ID limit.
auto count_active_cids() const -> size_t
Count non-retired CIDs.
connection_id_manager(connection_id_manager &&)=default
std::vector< connection_id_entry > peer_cids_
Peer connection IDs.
void set_active_cid_limit(uint64_t limit)
Set the active connection ID limit.
auto operator=(connection_id_manager &&) -> connection_id_manager &=default
auto is_stateless_reset_token(const std::array< uint8_t, 16 > &token) const -> bool
Check if a stateless reset token matches any stored CID.
auto get_active_peer_cid() const -> const connection_id &
Get the currently active peer connection ID.
std::vector< retire_connection_id_frame > pending_retire_frames_
Pending RETIRE_CONNECTION_ID frames to send.
static const connection_id empty_cid_
Empty CID for error cases.
void set_initial_peer_cid(const connection_id &cid)
Set the initial peer connection ID (from Initial packet)
auto find_by_sequence(uint64_t sequence) -> std::vector< connection_id_entry >::iterator
Find an entry by sequence number.
auto available_peer_cids() const -> size_t
Get the number of available (non-retired, non-active) peer CIDs.
uint64_t largest_retire_prior_to_
Largest retire_prior_to value received.
connection_id_manager(uint64_t active_cid_limit=8)
Construct a connection ID manager.
void retire_cids_prior_to(uint64_t prior_to)
Retire CIDs with sequence numbers less than the given value.
void clear_pending_retire_frames()
Clear the pending retire frames queue.
auto largest_retire_prior_to() const -> uint64_t
Get the largest retire_prior_to value received.
size_t active_index_
Index of the currently active peer CID.
QUIC Connection ID (RFC 9000 Section 5.1)
QUIC connection identifier type.
Result< std::monostate > VoidResult
Network-specific error and result type definitions.
Entry for storing a peer's connection ID with metadata.
bool retired
uint64_t sequence_number
std::array< uint8_t, 16 > stateless_reset_token
connection_id cid
RETIRE_CONNECTION_ID frame (RFC 9000 Section 19.16)