rama_haproxy/protocol/v2/
error.rs

1//! Errors for the binary proxy protocol.
2
3use std::fmt;
4
5/// An error in parsing a binary PROXY protocol header.
6#[derive(Debug, Clone, PartialEq, Eq, Hash)]
7pub enum ParseError {
8    /// Expected header to the protocol prefix plus 4 bytes after the prefix.
9    Incomplete(usize),
10    /// Expected header to start with a prefix of '\r\n\r\n\0\r\nQUIT\n'.
11    Prefix,
12    /// Expected version to be equal to 2.
13    Version(u8),
14    /// Invalid command. Command must be one of: Local, Proxy.
15    Command(u8),
16    /// Invalid Address Family. Address Family must be one of: Unspecified, IPv4, IPv6, Unix.
17    AddressFamily(u8),
18    /// Invalid protocol. Protocol must be one of: Unspecified, Stream, or Datagram.
19    Protocol(u8),
20    /// Header does not contain the advertised length of the address information and TLVs.
21    Partial(usize, usize),
22    /// Header length of {0} bytes cannot store the {1} bytes required for the address family.
23    InvalidAddresses(usize, usize),
24    /// Header is not long enough to contain TLV {0} with length {1}.
25    InvalidTLV(u8, u16),
26    /// Header contains leftover {0} bytes not accounted for by the address family or TLVs.
27    Leftovers(usize),
28}
29
30impl fmt::Display for ParseError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::Incomplete(len) => write!(
34                f,
35                "Expected header to the protocol prefix plus 4 bytes after the prefix (length {}).",
36                len
37            ),
38            Self::Prefix => write!(
39                f,
40                "Expected header to start with a prefix of '\\r\\n\\r\\n\\0\\r\\nQUIT\\n'."
41            ),
42            Self::Version(version) => write!(f, "Expected version {:X} to be equal to 2.", version),
43            Self::Command(command) => write!(
44                f,
45                "Invalid command {:X}. Command must be one of: Local, Proxy.",
46                command
47            ),
48            Self::AddressFamily(af) => write!(
49                f,
50                "Invalid Address Family {:X}. Address Family must be one of: Unspecified, IPv4, IPv6, Unix.",
51                af
52            ),
53            Self::Protocol(protocol) => write!(
54                f,
55                "Invalid protocol {:X}. Protocol must be one of: Unspecified, Stream, or Datagram.",
56                protocol
57            ),
58            Self::Partial(len, total) => write!(
59                f,
60                "Header does not contain the advertised length of the address information and TLVs (has {} out of {} bytes).",
61                len, total
62            ),
63            Self::InvalidAddresses(len, total) => write!(
64                f,
65                "Header length of {} bytes cannot store the {} bytes required for the address family.",
66                len, total
67            ),
68            Self::InvalidTLV(tlv, len) => write!(
69                f,
70                "Header is not long enough to contain TLV {} with length {}.",
71                tlv, len
72            ),
73            Self::Leftovers(len) => write!(
74                f,
75                "Header contains leftover {} bytes not accounted for by the address family or TLVs.",
76                len
77            ),
78        }
79    }
80}
81
82impl std::error::Error for ParseError {}