diff --git a/src/wire/ethernet.rs b/src/wire/ethernet.rs index aeca882..17bb78c 100644 --- a/src/wire/ethernet.rs +++ b/src/wire/ethernet.rs @@ -241,6 +241,37 @@ impl> PrettyPrint for Frame { } } +/// A high-level representation of an Internet Protocol version 4 packet header. +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub struct Repr { + pub src_addr: Address, + pub dst_addr: Address, + pub ethertype: EtherType, +} + +impl Repr { + /// Parse an Ethernet II frame and return a high-level representation. + pub fn parse + ?Sized>(frame: &Frame<&T>) -> Result { + frame.check_len()?; + Ok(Repr { + src_addr: frame.src_addr(), + dst_addr: frame.dst_addr(), + ethertype: frame.ethertype(), + }) + } + + /// Return the length of a header that will be emitted from this high-level representation. + pub fn buffer_len(&self) -> usize { + field::PAYLOAD.start + } + + /// Emit a high-level representation into an Ethernet II frame. + pub fn emit + AsMut<[u8]>>(&self, frame: &mut Frame) { + frame.set_src_addr(self.src_addr); + frame.set_dst_addr(self.dst_addr); + frame.set_ethertype(self.ethertype); + } +} #[cfg(test)] mod test { diff --git a/src/wire/mod.rs b/src/wire/mod.rs index 372237d..344aad3 100644 --- a/src/wire/mod.rs +++ b/src/wire/mod.rs @@ -104,7 +104,8 @@ pub use self::pretty_print::PrettyPrinter; pub use self::ethernet::{EtherType as EthernetProtocol, Address as EthernetAddress, - Frame as EthernetFrame}; + Frame as EthernetFrame, + Repr as EthernetRepr}; #[cfg(feature = "proto-ipv4")] pub use self::arp::{Hardware as ArpHardware,