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 419 of file packet.cpp.

421{
422 buffer.insert(buffer.end(), data.begin(), data.end());
423}

◆ 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 613 of file packet.cpp.

614{
615 auto ptype = header.type();
616 switch (ptype)
617 {
619 return build_initial(header.dest_conn_id, header.src_conn_id,
620 header.token, header.packet_number, header.version);
622 return build_handshake(header.dest_conn_id, header.src_conn_id,
623 header.packet_number, header.version);
625 return build_zero_rtt(header.dest_conn_id, header.src_conn_id,
626 header.packet_number, header.version);
628 return build_retry(header.dest_conn_id, header.src_conn_id,
629 header.token, header.retry_integrity_tag, header.version);
630 default:
631 return {};
632 }
633}
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:543
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:469
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:506
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:425

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 635 of file packet.cpp.

636{
637 return build_short(header.dest_conn_id, header.packet_number,
638 header.key_phase(), header.spin_bit());
639}
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:580

◆ 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 469 of file packet.cpp.

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

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

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

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 580 of file packet.cpp.

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

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 506 of file packet.cpp.

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

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: