Skip to main content

net_wire/ipv4/
arp.rs

1//! IPv4 address integration for ARP packets.
2
3use super::address::Ipv4Address;
4use crate::arp::{packet::ArpPacket, types::ArpProtocolType};
5
6impl ArpPacket<'_> {
7    /// Returns the sender IPv4 address when the protocol type is IPv4 and its size is four.
8    #[inline]
9    pub fn sender_ipv4_address(&self) -> Option<Ipv4Address> {
10        if self.protocol_type() != ArpProtocolType::IPV4
11            || self.sender_protocol_address().len() != 4
12        {
13            return None;
14        }
15        Some(Ipv4Address::new(
16            self.sender_protocol_address()
17                .try_into()
18                .expect("checked address length"),
19        ))
20    }
21
22    /// Returns the target IPv4 address when the protocol type is IPv4 and its size is four.
23    #[inline]
24    pub fn target_ipv4_address(&self) -> Option<Ipv4Address> {
25        if self.protocol_type() != ArpProtocolType::IPV4
26            || self.target_protocol_address().len() != 4
27        {
28            return None;
29        }
30        Some(Ipv4Address::new(
31            self.target_protocol_address()
32                .try_into()
33                .expect("checked address length"),
34        ))
35    }
36}