haprox_rs/protocol/
protocol_raw.rs

1/*-
2 * haprox-rs - a HaProxy protocol parser.
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * The scram-rs crate can be redistributed and/or modified
6 * under the terms of either of the following licenses:
7 *
8 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
9 *                     
10 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
11 */
12
13/// Characters match the US-ASCII representation of "PROXY" (not-supported)
14pub const HEADER_MAGIC_V1: &[u8] = b"\x50\x52\x4F\x58\x59";
15
16/// Binary header format starts with a constant 12 bytes.
17pub const HEADER_MAGIC_V2: &[u8] = b"\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
18
19/// Binary header format starts with a constant 12 bytes.
20pub const MSG_HEADER_LOCAL_V2: &[u8] = b"\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x20\x00\x00\x00";
21
22/// Length if the `HEADER_MAGIC_V2`
23pub const HEADER_MAGINC_LEN: usize = HEADER_MAGIC_V2.len();
24
25pub const HEADER_UNIX_ADDR_LEN: usize = 108;
26
27#[repr(C, packed)]
28pub struct PP2TlvSsl
29{
30    pub client: u8,
31    pub verify: u32,
32    pub data: [u8; 0],
33}
34
35#[repr(C, packed)]
36pub struct PP2Tlv
37{
38    pub mtype: u8,
39    pub length_hi: u8,
40    pub length_lo: u8,
41    pub value: [u8; 0],
42}
43
44#[repr(C, packed)]
45#[derive(Clone, Copy, Debug)]
46pub struct Ipv4Addr
47{
48    pub src_addr: u32,
49    pub dst_addr: u32,
50    pub src_port: u16,
51    pub dst_port: u16
52}
53
54#[repr(C, packed)]
55#[derive(Clone, Copy, Debug)]
56pub struct Ipv6Addr
57{
58    pub src_addr: [u8; 16],
59    pub  dst_addr: [u8; 16],
60    pub src_port: u16,
61    pub dst_port: u16
62}
63
64#[repr(C, packed)]
65#[derive(Clone, Copy, Debug)]
66pub struct UnixAddr
67{
68    pub src_addr: [u8; HEADER_UNIX_ADDR_LEN],
69    pub dst_addr: [u8; HEADER_UNIX_ADDR_LEN],
70}
71
72#[repr(C, packed)]
73pub union ProxyAddr
74{
75    pub ipv4_addr: Ipv4Addr,
76    pub ipv6_addr: Ipv6Addr,
77    pub unix_addr: UnixAddr,
78}
79
80#[repr(C, packed)]
81pub struct ProxyHdrV2
82{
83    /// [HEADER_MAGIC] const
84    pub signature: [u8; HEADER_MAGINC_LEN],
85
86    /// protocol version and command 
87    pub ver_cmd: u8,
88
89    /// protocol family and address
90    pub fam: u8,
91
92    /// number of following bytes part of the header
93    pub len: u16,
94
95    ///   17th byte, addresses in network byte order.
96    pub address: [u8; 0]//ProxyAddr,
97}
98
99impl ProxyHdrV2
100{
101    pub const VERSION_MASK: u8 = 0xF0;
102    pub const COMMAND_MASK: u8 = 0x0F;
103    pub const TRANSPT_MASK: u8 = 0x0F;
104    pub const ADDRESS_MASK: u8 = 0xF0;
105
106    pub const VERSION_RAW: u8 = 0x20;
107
108    pub const HEADER_LEN: usize = size_of::<Self>();
109    pub const HEADER_ADDR_LEN: usize = size_of::<Self>() + size_of::<ProxyAddr>();
110}