-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patherror.rs
More file actions
71 lines (58 loc) · 2.47 KB
/
error.rs
File metadata and controls
71 lines (58 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/// Error types for the IP stack.
///
/// This enum represents all possible errors that can occur when working with the IP stack.
#[derive(thiserror::Error, Debug)]
pub enum IpStackError {
/// The transport protocol is not supported.
#[error("The transport protocol is not supported")]
UnsupportedTransportProtocol,
/// The packet is invalid or malformed.
#[error("The packet is invalid")]
InvalidPacket,
/// A value is too large to fit in a u16.
#[error("ValueTooBigError<u16> {0}")]
ValueTooBigErrorU16(#[from] etherparse::err::ValueTooBigError<u16>),
/// A value is too large to fit in a usize.
#[error("ValueTooBigError<usize> {0}")]
ValueTooBigErrorUsize(#[from] etherparse::err::ValueTooBigError<usize>),
/// The TCP packet is invalid.
#[error("Invalid Tcp packet")]
InvalidTcpPacket,
/// An I/O error occurred.
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
/// Error accepting a new stream.
#[error("Accept Error")]
AcceptError,
/// Error sending data through a channel.
#[error("Send Error {0}")]
SendError(#[from] Box<tokio::sync::mpsc::error::SendError<crate::stream::IpStackStream>>),
/// Invalid MTU size. The minimum MTU is 1280 bytes to comply with IPv6 standards.
#[error("Invalid MTU size: {0} (bytes). Minimum MTU is 1280 bytes.")]
InvalidMtuSize(u16),
}
impl From<tokio::sync::mpsc::error::SendError<crate::stream::IpStackStream>> for IpStackError {
fn from(e: tokio::sync::mpsc::error::SendError<crate::stream::IpStackStream>) -> Self {
IpStackError::SendError(Box::new(e))
}
}
// Safety: All variants of IpStackError either contain no data or wrap types that are `Send`.
// This ensures that IpStackError as a whole is safe to send between threads.
unsafe impl Send for IpStackError {}
// Safety: All variants of IpStackError either contain no data or wrap types that are `Sync`.
// This ensures that IpStackError as a whole is safe to share between threads.
unsafe impl Sync for IpStackError {}
impl From<IpStackError> for std::io::Error {
fn from(e: IpStackError) -> Self {
match e {
IpStackError::IoError(e) => e,
_ => std::io::Error::other(e),
}
}
}
/// A specialized [`Result`] type for IP stack operations.
///
/// This type is used throughout the IP stack for any operation which may produce an error.
///
/// [`Result`]: std::result::Result
pub type Result<T, E = IpStackError> = std::result::Result<T, E>;