Skip to main content

net_wire/ipv4/
ethernet.rs

1//! IPv4 packet integration for Ethernet frames.
2
3use super::packet::{Ipv4Packet, Ipv4PacketMut};
4use crate::error::ParseError;
5use crate::ethernet::{
6    ether_type::EtherType,
7    frame::{EthernetFrame, EthernetFrameMut},
8};
9
10impl<'a> EthernetFrame<'a> {
11    /// Parses IPv4 only for the IPv4 EtherType.
12    #[inline]
13    pub fn ipv4(&self) -> Result<Option<Ipv4Packet<'a>>, ParseError> {
14        if self.ether_type() != EtherType::IPV4 {
15            return Ok(None);
16        }
17        Ipv4Packet::parse(self.payload()).map(Some)
18    }
19}
20
21impl<'a> EthernetFrameMut<'a> {
22    /// Parses mutable IPv4 only for the IPv4 EtherType.
23    #[inline]
24    pub fn ipv4_mut(&mut self) -> Result<Option<Ipv4PacketMut<'_>>, ParseError> {
25        if self.ether_type() != EtherType::IPV4 {
26            return Ok(None);
27        }
28        Ipv4PacketMut::parse(self.payload_mut()).map(Some)
29    }
30}