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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::fmt;
use std::io;
use crate::wire::backend::pcap::PcapError;
use crate::CrafterError;
use super::send::{SendMode, SendTarget};
pub type Result<T> = std::result::Result<T, NetError>;
/// Errors returned by packet send helpers.
#[derive(Debug)]
pub enum NetError {
/// Packet compilation failed before a send could be planned.
Packet(CrafterError),
/// A send plan or live send was requested without selecting an interface.
InterfaceRequired,
/// The selected interface name is structurally invalid.
InvalidInterfaceName {
/// Interface name supplied by the caller.
name: String,
/// Stable diagnostic reason.
reason: &'static str,
},
/// The selected interface was not present in the local interface table.
InterfaceNotFound {
/// Interface name supplied by the caller.
name: String,
},
/// No non-loopback, up interface with an address was available.
NoDefaultInterface,
/// The selected interface has no MAC address in the local interface table.
InterfaceMacNotFound {
/// Interface name supplied by the caller or selected by default.
name: String,
},
/// The selected interface has no address for the requested family.
InterfaceAddressNotFound {
/// Interface name supplied by the caller or selected by default.
name: String,
/// Requested address family.
family: &'static str,
},
/// A target address, wildcard, CIDR, or number range could not be parsed safely.
InvalidIpRange {
/// User supplied range expression.
input: String,
/// Stable diagnostic reason.
reason: &'static str,
},
/// The packet stack cannot be sent with the requested mode.
UnsupportedPacketShape {
/// Requested send mode.
mode: SendMode,
/// One-line packet summary.
summary: String,
/// Stable diagnostic reason.
reason: &'static str,
},
/// The live backend cannot transmit the planned packet target.
UnsupportedSendTarget {
/// Planned send target.
target: SendTarget,
/// Stable diagnostic reason.
reason: &'static str,
},
/// The datalink backend opened a channel type this crate does not send on.
UnsupportedDatalinkChannel {
/// Interface used for the channel.
interface: String,
},
/// The datalink backend could not allocate a write buffer.
SendBufferUnavailable {
/// Interface selected for sending.
interface: String,
/// Packet byte length.
len: usize,
},
/// A platform or permission error occurred while opening or writing a raw socket.
PermissionDenied {
/// Stable operation name.
operation: &'static str,
/// Underlying operating-system error.
source: io::Error,
},
/// A platform error occurred while opening or writing a raw socket.
Io {
/// Stable operation name.
operation: &'static str,
/// Underlying operating-system error.
source: io::Error,
},
/// Packet capture failed while waiting for a reply.
Capture(PcapError),
/// A packet wire capture pipeline failed while waiting for a reply.
WireCapture {
/// Stable diagnostic reason.
reason: String,
},
}
impl fmt::Display for NetError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Packet(err) => write!(f, "{err}"),
Self::InterfaceRequired => write!(f, "send interface is required"),
Self::InvalidInterfaceName { name, reason } => {
write!(f, "invalid interface name '{name}': {reason}")
}
Self::InterfaceNotFound { name } => write!(f, "interface '{name}' was not found"),
Self::NoDefaultInterface => write!(
f,
"no usable default interface was found in the local interface table"
),
Self::InterfaceMacNotFound { name } => {
write!(f, "interface '{name}' does not have a MAC address")
}
Self::InterfaceAddressNotFound { name, family } => {
write!(f, "interface '{name}' does not have a {family} address")
}
Self::InvalidIpRange { input, reason } => {
write!(f, "invalid IP range '{input}': {reason}")
}
Self::UnsupportedPacketShape {
mode,
summary,
reason,
} => write!(
f,
"cannot build {mode:?} send plan for packet '{summary}': {reason}"
),
Self::UnsupportedSendTarget { target, reason } => {
write!(f, "cannot transmit {target:?}: {reason}")
}
Self::UnsupportedDatalinkChannel { interface } => {
write!(
f,
"unsupported datalink channel for interface '{interface}'"
)
}
Self::SendBufferUnavailable { interface, len } => write!(
f,
"send buffer for interface '{interface}' could not accept {len} bytes"
),
Self::PermissionDenied { operation, source } => {
write!(
f,
"{operation} failed due to missing platform permission: {source}"
)
}
Self::Io { operation, source } => write!(f, "{operation} failed: {source}"),
Self::Capture(err) => write!(f, "{err}"),
Self::WireCapture { reason } => write!(f, "packet capture failed: {reason}"),
}
}
}
impl std::error::Error for NetError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Packet(err) => Some(err),
Self::PermissionDenied { source, .. } => Some(source),
Self::Io { source, .. } => Some(source),
Self::Capture(err) => Some(err),
_ => None,
}
}
}
impl From<CrafterError> for NetError {
fn from(value: CrafterError) -> Self {
Self::Packet(value)
}
}
impl From<PcapError> for NetError {
fn from(value: PcapError) -> Self {
Self::Capture(value)
}
}