1 /// Errors that can be encountered while decoding a TCP header. 2 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 3 pub enum HeaderError { 4 /// Error when the data_offset is so small that the data would 5 /// start within the TCP header itself. 6 DataOffsetTooSmall { data_offset: u8 }, 7 } 8 9 impl core::fmt::Display for HeaderError { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result10 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 11 use HeaderError::*; 12 match self { 13 DataOffsetTooSmall{ data_offset } => write!( 14 f, 15 "TCP Header Error: 'data offset' too small ({}). The 'data offset' must be at least 5 so the data is not overlapping with the TCP header itself.", 16 data_offset 17 ), 18 } 19 } 20 } 21 22 #[cfg(feature = "std")] 23 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 24 impl std::error::Error for HeaderError { source(&self) -> Option<&(dyn std::error::Error + 'static)>25 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 26 None 27 } 28 } 29 30 #[cfg(test)] 31 mod tests { 32 use super::HeaderError::*; 33 use alloc::format; 34 use std::{ 35 collections::hash_map::DefaultHasher, 36 error::Error, 37 hash::{Hash, Hasher}, 38 }; 39 40 #[test] debug()41 fn debug() { 42 assert_eq!( 43 "DataOffsetTooSmall { data_offset: 1 }", 44 format!("{:?}", DataOffsetTooSmall { data_offset: 1 }) 45 ); 46 } 47 48 #[test] clone_eq_hash()49 fn clone_eq_hash() { 50 let err = DataOffsetTooSmall { data_offset: 1 }; 51 assert_eq!(err, err.clone()); 52 let hash_a = { 53 let mut hasher = DefaultHasher::new(); 54 err.hash(&mut hasher); 55 hasher.finish() 56 }; 57 let hash_b = { 58 let mut hasher = DefaultHasher::new(); 59 err.clone().hash(&mut hasher); 60 hasher.finish() 61 }; 62 assert_eq!(hash_a, hash_b); 63 } 64 65 #[test] fmt()66 fn fmt() { 67 assert_eq!( 68 "TCP Header Error: 'data offset' too small (1). The 'data offset' must be at least 5 so the data is not overlapping with the TCP header itself.", 69 format!("{}", DataOffsetTooSmall{ data_offset: 1 }) 70 ); 71 } 72 73 #[cfg(feature = "std")] 74 #[test] source()75 fn source() { 76 assert!(DataOffsetTooSmall { data_offset: 0 }.source().is_none()); 77 } 78 } 79