1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Quic header types

use serde::Serialize;

use crate::protocols::stream::quic::QuicError;

/// Quic Long Header
#[derive(Debug, Serialize, Clone)]
pub struct QuicLongHeader {
    pub packet_type: LongHeaderPacketType,
    pub type_specific: u8,
    pub version: u32,
    pub dcid_len: u8,              // length of dcid in bytes
    pub dcid: String,              // hex string
    pub scid_len: u8,              // length of scid in bytes
    pub scid: String,              // hex string
    pub token_len: Option<u64>,    // length of token in bytes, if packet is of type Init or Retry
    pub token: Option<String>,     // hex string, if packet is of type Init or Retry
    pub retry_tag: Option<String>, // hex string, if packet is of type Retry
}

/// Quic Short Header
#[derive(Debug, Serialize, Clone)]
pub struct QuicShortHeader {
    pub dcid: Option<String>, // optional. If not pre-existing cid then none.
}

// Long Header Packet Types from RFC 9000 Table 5
#[derive(Debug, Clone, Serialize, Copy)]
pub enum LongHeaderPacketType {
    Initial,
    ZeroRTT,
    Handshake,
    Retry,
}

impl LongHeaderPacketType {
    pub fn from_u8(value: u8) -> Result<LongHeaderPacketType, QuicError> {
        match value {
            0x00 => Ok(LongHeaderPacketType::Initial),
            0x01 => Ok(LongHeaderPacketType::ZeroRTT),
            0x02 => Ok(LongHeaderPacketType::Handshake),
            0x03 => Ok(LongHeaderPacketType::Retry),
            _ => Err(QuicError::UnknowLongHeaderPacketType),
        }
    }
}