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

Builder for QUIC packet headers (RFC 9000 Section 17) More...

#include <packet.h>

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

Static Public Member Functions

static auto build_initial (const connection_id &dest_cid, const connection_id &src_cid, const std::vector< uint8_t > &token, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
 Build an Initial packet header.
 
static auto build_handshake (const connection_id &dest_cid, const connection_id &src_cid, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
 Build a Handshake packet header.
 
static auto build_zero_rtt (const connection_id &dest_cid, const connection_id &src_cid, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
 Build a 0-RTT packet header.
 
static auto build_retry (const connection_id &dest_cid, const connection_id &src_cid, const std::vector< uint8_t > &token, const std::array< uint8_t, 16 > &integrity_tag, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
 Build a Retry packet header.
 
static auto build_short (const connection_id &dest_cid, uint64_t packet_number, bool key_phase=false, bool spin_bit=false) -> std::vector< uint8_t >
 Build a Short Header (1-RTT) packet.
 
static auto build (const long_header &header) -> std::vector< uint8_t >
 Build a long header from a header structure.
 
static auto build (const short_header &header) -> std::vector< uint8_t >
 Build a short header from a header structure.
 

Static Private Member Functions

static void append_bytes (std::vector< uint8_t > &buffer, std::span< const uint8_t > data)
 Helper to append bytes to buffer.
 

Detailed Description

Builder for QUIC packet headers (RFC 9000 Section 17)

Builds raw bytes from QUIC packet header structures. Note that header protection must be applied after building.

Definition at line 299 of file packet.h.

Member Function Documentation

◆ append_bytes()

void kcenon::network::protocols::quic::packet_builder::append_bytes ( std::vector< uint8_t > & buffer,
std::span< const uint8_t > data )
staticprivate

Helper to append bytes to buffer.

Definition at line 417 of file packet.cpp.

419{
420 buffer.insert(buffer.end(), data.begin(), data.end());
421}

◆ build() [1/2]

auto kcenon::network::protocols::quic::packet_builder::build ( const long_header & header) -> std::vector<uint8_t>
staticnodiscard

Build a long header from a header structure.

Parameters
headerLong header structure
Returns
Serialized header bytes

Definition at line 611 of file packet.cpp.

612{
613 auto ptype = header.type();
614 switch (ptype)
615 {
617 return build_initial(header.dest_conn_id, header.src_conn_id,
618 header.token, header.packet_number, header.version);
620 return build_handshake(header.dest_conn_id, header.src_conn_id,
621 header.packet_number, header.version);
623 return build_zero_rtt(header.dest_conn_id, header.src_conn_id,
624 header.packet_number, header.version);
626 return build_retry(header.dest_conn_id, header.src_conn_id,
627 header.token, header.retry_integrity_tag, header.version);
628 default:
629 return {};
630 }
631}
static auto build_retry(const connection_id &dest_cid, const connection_id &src_cid, const std::vector< uint8_t > &token, const std::array< uint8_t, 16 > &integrity_tag, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
Build a Retry packet header.
Definition packet.cpp:541
static auto build_handshake(const connection_id &dest_cid, const connection_id &src_cid, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
Build a Handshake packet header.
Definition packet.cpp:467
static auto build_zero_rtt(const connection_id &dest_cid, const connection_id &src_cid, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
Build a 0-RTT packet header.
Definition packet.cpp:504
static auto build_initial(const connection_id &dest_cid, const connection_id &src_cid, const std::vector< uint8_t > &token, uint64_t packet_number, uint32_t version=quic_version::version_1) -> std::vector< uint8_t >
Build an Initial packet header.
Definition packet.cpp:423

References kcenon::network::protocols::quic::handshake, kcenon::network::protocols::quic::initial, kcenon::network::protocols::quic::retry, and kcenon::network::protocols::quic::zero_rtt.

◆ build() [2/2]

auto kcenon::network::protocols::quic::packet_builder::build ( const short_header & header) -> std::vector<uint8_t>
staticnodiscard

Build a short header from a header structure.

Parameters
headerShort header structure
Returns
Serialized header bytes

Definition at line 633 of file packet.cpp.

634{
635 return build_short(header.dest_conn_id, header.packet_number,
636 header.key_phase(), header.spin_bit());
637}
static auto build_short(const connection_id &dest_cid, uint64_t packet_number, bool key_phase=false, bool spin_bit=false) -> std::vector< uint8_t >
Build a Short Header (1-RTT) packet.
Definition packet.cpp:578

◆ build_handshake()

auto kcenon::network::protocols::quic::packet_builder::build_handshake ( const connection_id & dest_cid,
const connection_id & src_cid,
uint64_t packet_number,
uint32_t version = quic_version::version_1 ) -> std::vector<uint8_t>
staticnodiscard

Build a Handshake packet header.

Parameters
dest_cidDestination Connection ID
src_cidSource Connection ID
packet_numberPacket number
versionQUIC version (default: version 1)
Returns
Serialized header bytes

Definition at line 467 of file packet.cpp.

472{
473 std::vector<uint8_t> buffer;
474
475 size_t pn_len = packet_number::encoded_length(packet_num, 0);
476
477 // First byte: Long header (1) + Fixed bit (1) + Type (10) + Reserved (00) + PN Length
478 uint8_t first_byte = header_form_long | fixed_bit |
479 (static_cast<uint8_t>(packet_type::handshake) << long_packet_type_shift) |
480 static_cast<uint8_t>(pn_len - 1);
481 buffer.push_back(first_byte);
482
483 // Version
484 buffer.push_back(static_cast<uint8_t>(version >> 24));
485 buffer.push_back(static_cast<uint8_t>(version >> 16));
486 buffer.push_back(static_cast<uint8_t>(version >> 8));
487 buffer.push_back(static_cast<uint8_t>(version));
488
489 // DCID
490 buffer.push_back(static_cast<uint8_t>(dest_cid.length()));
491 append_bytes(buffer, dest_cid.data());
492
493 // SCID
494 buffer.push_back(static_cast<uint8_t>(src_cid.length()));
495 append_bytes(buffer, src_cid.data());
496
497 // Packet number
498 auto [pn_bytes, _] = packet_number::encode(packet_num, 0);
499 append_bytes(buffer, pn_bytes);
500
501 return buffer;
502}
static void append_bytes(std::vector< uint8_t > &buffer, std::span< const uint8_t > data)
Helper to append bytes to buffer.
Definition packet.cpp:417
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
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.
Definition packet.cpp:88
constexpr const char * version() noexcept
Get the network system version string.
Definition network.cppm:111

References kcenon::network::protocols::quic::packet_number::encode(), kcenon::network::protocols::quic::packet_number::encoded_length(), kcenon::network::protocols::quic::handshake, and kcenon::network::version().

Referenced by kcenon::network::protocols::quic::connection::build_packet(), and kcenon::network::internal::quic_socket::send_packet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_initial()

auto kcenon::network::protocols::quic::packet_builder::build_initial ( const connection_id & dest_cid,
const connection_id & src_cid,
const std::vector< uint8_t > & token,
uint64_t packet_number,
uint32_t version = quic_version::version_1 ) -> std::vector<uint8_t>
staticnodiscard

Build an Initial packet header.

Parameters
dest_cidDestination Connection ID
src_cidSource Connection ID
tokenToken for address validation (empty for client initial)
packet_numberPacket number
versionQUIC version (default: version 1)
Returns
Serialized header bytes

Definition at line 423 of file packet.cpp.

429{
430 std::vector<uint8_t> buffer;
431
432 // Calculate packet number length
433 size_t pn_len = packet_number::encoded_length(packet_num, 0);
434
435 // First byte: Long header (1) + Fixed bit (1) + Type (00) + Reserved (00) + PN Length
436 uint8_t first_byte = header_form_long | fixed_bit |
437 (static_cast<uint8_t>(packet_type::initial) << long_packet_type_shift) |
438 static_cast<uint8_t>(pn_len - 1);
439 buffer.push_back(first_byte);
440
441 // Version (4 bytes, big-endian)
442 buffer.push_back(static_cast<uint8_t>(version >> 24));
443 buffer.push_back(static_cast<uint8_t>(version >> 16));
444 buffer.push_back(static_cast<uint8_t>(version >> 8));
445 buffer.push_back(static_cast<uint8_t>(version));
446
447 // DCID Length + DCID
448 buffer.push_back(static_cast<uint8_t>(dest_cid.length()));
449 append_bytes(buffer, dest_cid.data());
450
451 // SCID Length + SCID
452 buffer.push_back(static_cast<uint8_t>(src_cid.length()));
453 append_bytes(buffer, src_cid.data());
454
455 // Token Length (varint) + Token
456 auto token_len_encoded = varint::encode(token.size());
457 append_bytes(buffer, token_len_encoded);
458 append_bytes(buffer, token);
459
460 // Packet number (placeholder - actual encoding done separately)
461 auto [pn_bytes, _] = packet_number::encode(packet_num, 0);
462 append_bytes(buffer, pn_bytes);
463
464 return buffer;
465}
static auto encode(uint64_t value) -> std::vector< uint8_t >
Encode a value to variable-length format.
Definition varint.cpp:10

References kcenon::network::protocols::quic::packet_number::encode(), kcenon::network::protocols::quic::varint::encode(), kcenon::network::protocols::quic::packet_number::encoded_length(), kcenon::network::protocols::quic::initial, and kcenon::network::version().

Referenced by kcenon::network::protocols::quic::connection::build_packet(), and kcenon::network::internal::quic_socket::send_packet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_retry()

auto kcenon::network::protocols::quic::packet_builder::build_retry ( const connection_id & dest_cid,
const connection_id & src_cid,
const std::vector< uint8_t > & token,
const std::array< uint8_t, 16 > & integrity_tag,
uint32_t version = quic_version::version_1 ) -> std::vector<uint8_t>
staticnodiscard

Build a Retry packet header.

Parameters
dest_cidDestination Connection ID
src_cidSource Connection ID
tokenRetry token
integrity_tagRetry Integrity Tag (16 bytes)
versionQUIC version (default: version 1)
Returns
Serialized header bytes

Definition at line 541 of file packet.cpp.

547{
548 std::vector<uint8_t> buffer;
549
550 // First byte: Long header (1) + Fixed bit (1) + Type (11) + Unused (0000)
551 uint8_t first_byte = header_form_long | fixed_bit |
552 (static_cast<uint8_t>(packet_type::retry) << long_packet_type_shift);
553 buffer.push_back(first_byte);
554
555 // Version
556 buffer.push_back(static_cast<uint8_t>(version >> 24));
557 buffer.push_back(static_cast<uint8_t>(version >> 16));
558 buffer.push_back(static_cast<uint8_t>(version >> 8));
559 buffer.push_back(static_cast<uint8_t>(version));
560
561 // DCID
562 buffer.push_back(static_cast<uint8_t>(dest_cid.length()));
563 append_bytes(buffer, dest_cid.data());
564
565 // SCID
566 buffer.push_back(static_cast<uint8_t>(src_cid.length()));
567 append_bytes(buffer, src_cid.data());
568
569 // Retry Token
570 append_bytes(buffer, token);
571
572 // Retry Integrity Tag
573 buffer.insert(buffer.end(), integrity_tag.begin(), integrity_tag.end());
574
575 return buffer;
576}

References kcenon::network::protocols::quic::retry, and kcenon::network::version().

Here is the call graph for this function:

◆ build_short()

auto kcenon::network::protocols::quic::packet_builder::build_short ( const connection_id & dest_cid,
uint64_t packet_number,
bool key_phase = false,
bool spin_bit = false ) -> std::vector<uint8_t>
staticnodiscard

Build a Short Header (1-RTT) packet.

Parameters
dest_cidDestination Connection ID
packet_numberPacket number
key_phaseKey phase bit
spin_bitSpin bit for latency measurement
Returns
Serialized header bytes

Definition at line 578 of file packet.cpp.

583{
584 std::vector<uint8_t> buffer;
585
586 size_t pn_len = packet_number::encoded_length(packet_num, 0);
587
588 // First byte: Short header (0) + Fixed bit (1) + Spin + Reserved (00) + Key Phase + PN Length
589 uint8_t first_byte = fixed_bit; // Header form = 0
590 if (spin_bit)
591 {
592 first_byte |= spin_bit_mask;
593 }
594 if (key_phase)
595 {
596 first_byte |= key_phase_mask;
597 }
598 first_byte |= static_cast<uint8_t>(pn_len - 1);
599 buffer.push_back(first_byte);
600
601 // DCID (no length field for short header)
602 append_bytes(buffer, dest_cid.data());
603
604 // Packet number
605 auto [pn_bytes, _] = packet_number::encode(packet_num, 0);
606 append_bytes(buffer, pn_bytes);
607
608 return buffer;
609}

References kcenon::network::protocols::quic::packet_number::encode(), and kcenon::network::protocols::quic::packet_number::encoded_length().

Referenced by kcenon::network::protocols::quic::connection::build_packet(), and kcenon::network::internal::quic_socket::send_packet().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ build_zero_rtt()

auto kcenon::network::protocols::quic::packet_builder::build_zero_rtt ( const connection_id & dest_cid,
const connection_id & src_cid,
uint64_t packet_number,
uint32_t version = quic_version::version_1 ) -> std::vector<uint8_t>
staticnodiscard

Build a 0-RTT packet header.

Parameters
dest_cidDestination Connection ID
src_cidSource Connection ID
packet_numberPacket number
versionQUIC version (default: version 1)
Returns
Serialized header bytes

Definition at line 504 of file packet.cpp.

509{
510 std::vector<uint8_t> buffer;
511
512 size_t pn_len = packet_number::encoded_length(packet_num, 0);
513
514 // First byte: Long header (1) + Fixed bit (1) + Type (01) + Reserved (00) + PN Length
515 uint8_t first_byte = header_form_long | fixed_bit |
516 (static_cast<uint8_t>(packet_type::zero_rtt) << long_packet_type_shift) |
517 static_cast<uint8_t>(pn_len - 1);
518 buffer.push_back(first_byte);
519
520 // Version
521 buffer.push_back(static_cast<uint8_t>(version >> 24));
522 buffer.push_back(static_cast<uint8_t>(version >> 16));
523 buffer.push_back(static_cast<uint8_t>(version >> 8));
524 buffer.push_back(static_cast<uint8_t>(version));
525
526 // DCID
527 buffer.push_back(static_cast<uint8_t>(dest_cid.length()));
528 append_bytes(buffer, dest_cid.data());
529
530 // SCID
531 buffer.push_back(static_cast<uint8_t>(src_cid.length()));
532 append_bytes(buffer, src_cid.data());
533
534 // Packet number
535 auto [pn_bytes, _] = packet_number::encode(packet_num, 0);
536 append_bytes(buffer, pn_bytes);
537
538 return buffer;
539}

References kcenon::network::protocols::quic::packet_number::encode(), kcenon::network::protocols::quic::packet_number::encoded_length(), kcenon::network::version(), and kcenon::network::protocols::quic::zero_rtt.

Here is the call graph for this function:

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