mutnet/tcp/
error.rs

1//! TCP specific errors.
2
3use core::error;
4use core::fmt::{Debug, Display, Formatter};
5
6use crate::error::{InvalidChecksumError, NotEnoughHeadroomError, UnexpectedBufferEndError};
7
8/// Error returned when parsing a TCP header.
9#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
10pub enum ParseTcpError {
11    /// The data buffer ended unexpectedly.
12    UnexpectedBufferEnd(UnexpectedBufferEndError),
13    /// Invalid checksum.
14    InvalidChecksum(InvalidChecksumError),
15    /// Data offset header smaller than minimum (5).
16    DataOffsetHeaderValueTooSmall {
17        /// Found data offset header value.
18        data_offset_header: usize,
19    },
20}
21
22impl From<UnexpectedBufferEndError> for ParseTcpError {
23    #[inline]
24    fn from(value: UnexpectedBufferEndError) -> Self {
25        Self::UnexpectedBufferEnd(value)
26    }
27}
28
29impl From<InvalidChecksumError> for ParseTcpError {
30    #[inline]
31    fn from(value: InvalidChecksumError) -> Self {
32        Self::InvalidChecksum(value)
33    }
34}
35
36impl Display for ParseTcpError {
37    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
38        match self {
39            Self::UnexpectedBufferEnd(err) => {
40                write!(f, "{err}")
41            }
42            Self::InvalidChecksum(err) => {
43                write!(f, "{err}")
44            }
45            Self::DataOffsetHeaderValueTooSmall { data_offset_header } => {
46                write!(
47                    f,
48                    "Data offset header value too small, minimum 5 expected, was: {data_offset_header}"
49                )
50            }
51        }
52    }
53}
54
55impl error::Error for ParseTcpError {}
56
57/// Error returned by methods manipulating the data offset.
58#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
59pub enum SetDataOffsetError {
60    /// Data offset is not within required bounds (5..=15).
61    InvalidDataOffset {
62        /// Invalid data offset.
63        data_offset: usize,
64    },
65    /// Not enough headroom available.
66    NotEnoughHeadroom(NotEnoughHeadroomError),
67}
68
69impl Display for SetDataOffsetError {
70    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
71        match self {
72            Self::InvalidDataOffset { data_offset } => {
73                write!(
74                    f,
75                    "Data offset header value invalid, expected to be between 5 and 15 (inclusive): {data_offset}"
76                )
77            }
78            Self::NotEnoughHeadroom(err) => {
79                write!(f, "{err}")
80            }
81        }
82    }
83}
84
85impl From<NotEnoughHeadroomError> for SetDataOffsetError {
86    #[inline]
87    fn from(value: NotEnoughHeadroomError) -> Self {
88        Self::NotEnoughHeadroom(value)
89    }
90}
91
92impl error::Error for SetDataOffsetError {}