haprox_rs/protocol_v2/
mod.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
15use std::{fmt, io::Cursor};
16
17use crate::error::HaProxRes;
18
19pub mod protocol;
20
21pub mod protocol_parser;
22pub mod protocol_composer;
23
24pub mod autogen;
25
26/// Should be implemented by the structure which contains UniqID in order to be passed
27/// as argument to TLV.
28pub trait PP2TlvUniqId: fmt::Display
29{
30    /// Should return the ID as an array of length [MAX_UNIQ_ID_LEN_BYTES] bytes.
31    fn into_bytes(&self) -> Vec<u8>;
32
33    fn as_slice<'a>(&'a self) -> &'a [u8];
34
35    /// Should return the length in bytes of the array.
36    fn get_len(&self) -> u16;
37}
38
39/// A V2 OP code.
40pub trait ProxyV2OpCode: fmt::Debug + Clone
41{
42    /// An operational code of the specific proto msg type identifier.
43    const OPCODE: u8;
44}
45
46pub trait PP2TlvDump: fmt::Debug + fmt::Display
47{
48    /// Returns the type number of the TLV.
49    fn get_type(&self) -> u8;
50
51    /// Writes the content of the instance to the TLV's payload. It must NOT 
52    /// write the TLV header, it is written before and after.
53    /// 
54    /// # Arguments
55    /// 
56    /// * `cur` - a [Cursor] which points to the beginning of the payload.
57    /// The packet size should not exceed the MTU of your network.
58    /// 
59    /// # Returns 
60    /// 
61    /// A [Result] should be returned without any inner data.
62    fn dump(&self, cur: &mut Cursor<Vec<u8>>) -> HaProxRes<()>;
63}
64
65
66
67/// A trait which provides functionality for `protocol_parser`. A user's program can
68/// implement this on the own set of TLV in order to parse the received data and 
69/// return the result.
70pub trait PP2TlvRestore<'zc>: fmt::Debug + fmt::Display + 'zc
71{
72    type TlvTblRes;
73
74    /// Checks if the current TLV is in range of the `tlv_parent_type` parent. If
75    /// `tlv_parent_type` is `None` then the `tlv_type` can be checked against the
76    /// main range. The `tlv_parent_type` is set when the current `tlv_type` is a 
77    /// subset of the parent.
78    /// 
79    /// It is called by the TLV iterator to determine which parser should be called next 
80    /// or if it is out of range then error.
81    /// 
82    /// # Arguments
83    /// 
84    /// * `tlv_type` - a current tlv ID.
85    /// 
86    /// * `tlv_parent_type` - a parent TLV ID if any.
87    /// 
88    /// # Returns 
89    /// 
90    /// Either true or false. If not in range then false.
91    fn is_in_range(tlv_type: u8, tlv_parent_type: Option<u8>) -> bool;
92
93
94    /// Should return `true` if the current instance contains subtypes.
95    fn contains_subtype(item: &Self::TlvTblRes) -> bool;
96
97    /// A function which is called when the parsing reaches the TLV's payload. The cursor
98    /// is poiniting to the beginning of the payload and the inner buffer is a slice 
99    /// from the main buffer of size of the TLV's payload.
100    /// 
101    /// # Arguments
102    /// 
103    /// * `tlv_type` - a current TLV type.
104    /// 
105    /// * `cur` - a cursor pointing to the beginning of the TLV's payload.
106    /// 
107    /// # Returns 
108    /// 
109    /// A [HaProxRes] should be returned with
110    /// 
111    /// * [Result::Ok] - with the `self` instance.
112    /// 
113    /// * [Result::Err] - an error description.
114    fn restore(tlv_type: u8, cur: &mut Cursor<&'zc [u8]>) -> HaProxRes<Self::TlvTblRes>;
115}
116