Skip to content

Commit 21b8cf0

Browse files
committed
Add packet::ChecksumError
1 parent 0972c42 commit 21b8cf0

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
/// Error if a checksum check in a packet fails.
3+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
4+
pub enum ChecksumError {
5+
/// IPv4 header checksum invalid.
6+
Ipv4HeaderChecksum,
7+
8+
/// UDP checksum invalid.
9+
UdpChecksum,
10+
11+
/// ICMPv4 checksum invalid.
12+
Icmpv4Checksum,
13+
14+
/// ICMPv6 checksum invalid.
15+
Icmpv6Checksum,
16+
17+
/// Error if the ICMPv6 checksum could not be verified
18+
/// as there was no IPv6 header present needed to
19+
/// calculate the checksum.
20+
Icmpv6MissingIpv6,
21+
}
22+
23+
24+
impl core::fmt::Display for ChecksumError {
25+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26+
use ChecksumError::*;
27+
match self {
28+
Ipv4HeaderChecksum => write!(f, "IPv4Header checksum invalid"),
29+
UdpChecksum => write!(f, "UDP checksum invalid"),
30+
Icmpv4Checksum => write!(f, "ICMPv4 checksum invalid"),
31+
Icmpv6Checksum => write!(f, "ICMPv6 checksum invalid"),
32+
Icmpv6MissingIpv6 => write!(f, "ICMPv6 checksum can not be validated (IPv6 header missing)"),
33+
}
34+
}
35+
}
36+
37+
#[cfg(feature = "std")]
38+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
39+
impl std::error::Error for ChecksumError {
40+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41+
None
42+
}
43+
}
44+
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::ChecksumError::*;
49+
use alloc::format;
50+
use std::{
51+
collections::hash_map::DefaultHasher,
52+
error::Error,
53+
hash::{Hash, Hasher},
54+
};
55+
56+
#[test]
57+
fn debug() {
58+
assert_eq!(
59+
"Ipv4HeaderChecksum",
60+
format!("{:?}", Ipv4HeaderChecksum)
61+
);
62+
}
63+
64+
#[test]
65+
fn clone_eq_hash() {
66+
let err = Ipv4HeaderChecksum;
67+
assert_eq!(err, err.clone());
68+
let hash_a = {
69+
let mut hasher = DefaultHasher::new();
70+
err.hash(&mut hasher);
71+
hasher.finish()
72+
};
73+
let hash_b = {
74+
let mut hasher = DefaultHasher::new();
75+
err.clone().hash(&mut hasher);
76+
hasher.finish()
77+
};
78+
assert_eq!(hash_a, hash_b);
79+
}
80+
81+
#[test]
82+
fn fmt() {
83+
let values = [
84+
(Ipv4HeaderChecksum, "IPv4Header checksum invalid"),
85+
(UdpChecksum, "UDP checksum invalid"),
86+
(Icmpv4Checksum, "ICMPv4 checksum invalid"),
87+
(Icmpv6Checksum, "ICMPv6 checksum invalid"),
88+
(Icmpv6MissingIpv6, "ICMPv6 checksum can not be validated (IPv6 header missing)"),
89+
];
90+
for (v, expected) in values {
91+
assert_eq!(format!("{}", v), expected);
92+
}
93+
}
94+
95+
#[cfg(feature = "std")]
96+
#[test]
97+
fn source() {
98+
let values = [
99+
Ipv4HeaderChecksum,
100+
UdpChecksum,
101+
Icmpv4Checksum,
102+
Icmpv6Checksum,
103+
Icmpv6MissingIpv6,
104+
];
105+
for v in values {
106+
assert!(v.source().is_none());
107+
}
108+
}
109+
}

etherparse/src/err/packet/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod build_write_error;
22
pub use build_write_error::*;
33

4+
mod checksum_error;
5+
pub use checksum_error::*;
6+
47
mod slice_error;
58
pub use slice_error::*;
69

0 commit comments

Comments
 (0)