Skip to main content

haprox_rs/
protocol_raw.rs

1/*-
2 * haprox-rs - a HaProxy protocol parser.
3 * 
4 * Copyright 2025 (c) 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. The MIT License (MIT)
11 *                     
12 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
13 */
14
15/// Characters match the US-ASCII representation of "PROXY"
16pub const HEADER_MAGIC_V1: &[u8] = b"\x50\x52\x4F\x58\x59";
17
18pub const HEADER_MAGIC_V1_STR: &str = "\x50\x52\x4F\x58\x59";
19
20pub const HEADER_V1_WSPACE: &str = "\x20";
21
22pub const HEADER_V1_EOM: &str = "\r\n";
23
24/// 108-byte buffer is always enough to store all the line and a trailing zero
25/// for string processing.
26pub const HEADER_V1_MAX_LEN: usize = 108;
27
28/// Binary header format starts with a constant 12 bytes.
29pub const HEADER_MAGIC_V2: &[u8] = b"\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
30
31/// Binary header format starts with a constant 12 bytes.
32pub const MSG_HEADER_LOCAL_V2: &[u8] = b"\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x20\x00\x00\x00";
33
34/// Length if the `HEADER_MAGIC_V2`
35pub const HEADER_MAGINC_LEN: usize = HEADER_MAGIC_V2.len();
36
37pub const HEADER_UNIX_ADDR_LEN: usize = 108;
38
39/// \x54 \x43 \x50 \x34
40pub const HEADER_V1_INET_TCP4: &str = "TCP4";
41
42/// \x54 \x43 \x50 \x36
43pub const HEADER_V1_INET_TCP6: &str = "TCP6";
44
45pub const HEADER_V1_INET_UNKNWON: &str = "UNKNOWN";
46
47#[allow(unused)]
48#[repr(C, packed)]
49pub struct PP2TlvSsl
50{
51    pub client: u8,
52    pub verify: u32,
53    pub data: [u8; 0],
54}
55
56#[repr(C, packed)]
57pub struct PP2Tlv
58{
59    pub mtype: u8,
60    pub length_hi: u8,
61    pub length_lo: u8,
62    pub value: [u8; 0],
63}
64
65#[repr(C, packed)]
66#[derive(Clone, Copy, Debug)]
67pub struct Ipv4Addr
68{
69    pub src_addr: u32,
70    pub dst_addr: u32,
71    pub src_port: u16,
72    pub dst_port: u16
73}
74
75#[repr(C, packed)]
76#[derive(Clone, Copy, Debug)]
77pub struct Ipv6Addr
78{
79    pub src_addr: [u8; 16],
80    pub  dst_addr: [u8; 16],
81    pub src_port: u16,
82    pub dst_port: u16
83}
84
85#[allow(unused)]
86#[repr(C, packed)]
87#[derive(Clone, Copy, Debug)]
88pub struct UnixAddr
89{
90    pub src_addr: [u8; HEADER_UNIX_ADDR_LEN],
91    pub dst_addr: [u8; HEADER_UNIX_ADDR_LEN],
92}
93
94#[allow(unused)]
95#[repr(C, packed)]
96pub union ProxyAddr
97{
98    pub ipv4_addr: Ipv4Addr,
99    pub ipv6_addr: Ipv6Addr,
100    pub unix_addr: UnixAddr,
101}
102
103#[repr(C, packed)]
104pub struct ProxyHdrV2
105{
106    /// [HEADER_MAGIC] const
107    pub signature: [u8; HEADER_MAGINC_LEN],
108
109    /// protocol version and command 
110    pub ver_cmd: u8,
111
112    /// protocol family and address
113    pub fam: u8,
114
115    /// number of following bytes part of the header
116    pub len: u16,
117
118    ///   17th byte, addresses in network byte order.
119    pub address: [u8; 0]//ProxyAddr,
120}
121
122#[allow(unused)]
123impl ProxyHdrV2
124{
125    pub const VERSION_MASK: u8 = 0xF0;
126    pub const COMMAND_MASK: u8 = 0x0F;
127    pub const TRANSPT_MASK: u8 = 0x0F;
128    pub const ADDRESS_MASK: u8 = 0xF0;
129
130    pub const VERSION_RAW: u8 = 0x20;
131
132    pub const HEADER_LEN: usize = size_of::<Self>();
133    pub const HEADER_ADDR_LEN: usize = size_of::<Self>() + size_of::<ProxyAddr>();
134}