1 use crate::{EtherPayloadSlice, EtherType, LinuxSllProtocolType}; 2 3 /// Payload of Linux Cooked Capture v1 (SLL) packet 4 #[derive(Clone, Debug, Eq, PartialEq)] 5 pub struct LinuxSllPayloadSlice<'a> { 6 /// Identifying content of the payload. 7 pub protocol_type: LinuxSllProtocolType, 8 9 /// Payload 10 pub payload: &'a [u8], 11 } 12 13 impl<'a> From<EtherPayloadSlice<'a>> for LinuxSllPayloadSlice<'a> { from(value: EtherPayloadSlice<'a>) -> LinuxSllPayloadSlice<'a>14 fn from(value: EtherPayloadSlice<'a>) -> LinuxSllPayloadSlice<'a> { 15 LinuxSllPayloadSlice { 16 protocol_type: LinuxSllProtocolType::EtherType(value.ether_type), 17 payload: value.payload, 18 } 19 } 20 } 21 22 impl<'a> TryFrom<LinuxSllPayloadSlice<'a>> for EtherPayloadSlice<'a> { 23 type Error = (); 24 try_from(value: LinuxSllPayloadSlice<'a>) -> Result<EtherPayloadSlice<'a>, Self::Error>25 fn try_from(value: LinuxSllPayloadSlice<'a>) -> Result<EtherPayloadSlice<'a>, Self::Error> { 26 match value.protocol_type { 27 LinuxSllProtocolType::LinuxNonstandardEtherType(nonstandard_ether_type) => { 28 Ok(EtherPayloadSlice { 29 ether_type: EtherType(nonstandard_ether_type.into()), 30 payload: value.payload, 31 }) 32 } 33 LinuxSllProtocolType::EtherType(ether_type) => Ok(EtherPayloadSlice { 34 ether_type, 35 payload: value.payload, 36 }), 37 _ => Err(()), 38 } 39 } 40 } 41 42 #[cfg(test)] 43 mod test { 44 use super::*; 45 use alloc::format; 46 47 #[test] debug()48 fn debug() { 49 let s = LinuxSllPayloadSlice { 50 protocol_type: LinuxSllProtocolType::EtherType(EtherType::IPV4), 51 payload: &[], 52 }; 53 assert_eq!( 54 format!( 55 "LinuxSllPayloadSlice {{ protocol_type: {:?}, payload: {:?} }}", 56 s.protocol_type, s.payload 57 ), 58 format!("{:?}", s) 59 ); 60 } 61 62 #[test] clone_eq()63 fn clone_eq() { 64 let s = LinuxSllPayloadSlice { 65 protocol_type: LinuxSllProtocolType::EtherType(EtherType::IPV4), 66 payload: &[], 67 }; 68 assert_eq!(s.clone(), s); 69 } 70 } 71