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