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!(f, "Expected header to the protocol prefix plus 4 bytes after the prefix (length {}).", len),
34            Self::Prefix => write!(f, "Expected header to start with a prefix of '\\r\\n\\r\\n\\0\\r\\nQUIT\\n'."),
35            Self::Version(version) => write!(f, "Expected version {:X} to be equal to 2.", version),
36            Self::Command(command) => write!(f, "Invalid command {:X}. Command must be one of: Local, Proxy.", command),
37            Self::AddressFamily(af) => write!(f, "Invalid Address Family {:X}. Address Family must be one of: Unspecified, IPv4, IPv6, Unix.", af),
38            Self::Protocol(protocol) => write!(f, "Invalid protocol {:X}. Protocol must be one of: Unspecified, Stream, or Datagram.", protocol),
39            Self::Partial(len, total) => write!(f, "Header does not contain the advertised length of the address information and TLVs (has {} out of {} bytes).", len, total),
40            Self::InvalidAddresses(len, total) => write!(f, "Header length of {} bytes cannot store the {} bytes required for the address family.", len, total),
41            Self::InvalidTLV(tlv, len) => write!(f, "Header is not long enough to contain TLV {} with length {}.", tlv, len),
42            Self::Leftovers(len) => write!(f, "Header contains leftover {} bytes not accounted for by the address family or TLVs.", len),
43        }
44    }
45}
46
47impl std::error::Error for ParseError {}