Skip to main content

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 {len}).",
36            ),
37            Self::Prefix => write!(
38                f,
39                "Expected header to start with a prefix of '\\r\\n\\r\\n\\0\\r\\nQUIT\\n'."
40            ),
41            Self::Version(version) => write!(f, "Expected version {version:X} to be equal to 2."),
42            Self::Command(command) => write!(
43                f,
44                "Invalid command {command:X}. Command must be one of: Local, Proxy.",
45            ),
46            Self::AddressFamily(af) => write!(
47                f,
48                "Invalid Address Family {af:X}. Address Family must be one of: Unspecified, IPv4, IPv6, Unix.",
49            ),
50            Self::Protocol(protocol) => write!(
51                f,
52                "Invalid protocol {protocol:X}. Protocol must be one of: Unspecified, Stream, or Datagram.",
53            ),
54            Self::Partial(len, total) => write!(
55                f,
56                "Header does not contain the advertised length of the address information and TLVs (has {len} out of {total} bytes).",
57            ),
58            Self::InvalidAddresses(len, total) => write!(
59                f,
60                "Header length of {len} bytes cannot store the {total} bytes required for the address family.",
61            ),
62            Self::InvalidTLV(tlv, len) => write!(
63                f,
64                "Header is not long enough to contain TLV {tlv} with length {len}.",
65            ),
66            Self::Leftovers(len) => write!(
67                f,
68                "Header contains leftover {len} bytes not accounted for by the address family or TLVs.",
69            ),
70        }
71    }
72}
73
74impl std::error::Error for ParseError {}