async_arp/
request.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::error::InputBuildError;
use pnet::{packet::arp::Arp, util::MacAddr};
use std::net::Ipv4Addr;

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct RequestInput {
    pub sender_ip: Ipv4Addr,
    pub sender_mac: MacAddr,
    pub target_ip: Ipv4Addr,
    pub target_mac: MacAddr,
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct RequestInputBuilder {
    sender_ip: Option<Ipv4Addr>,
    sender_mac: Option<MacAddr>,
    target_ip: Option<Ipv4Addr>,
    target_mac: Option<MacAddr>,
}

impl RequestInputBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_sender_mac(mut self, sender_mac: MacAddr) -> Self {
        self.sender_mac = Some(sender_mac);
        self
    }

    pub fn with_sender_ip(mut self, sender_ip: Ipv4Addr) -> Self {
        self.sender_ip = Some(sender_ip);
        self
    }

    pub fn with_target_mac(mut self, target_mac: MacAddr) -> Self {
        self.target_mac = Some(target_mac);
        self
    }

    pub fn with_target_ip(mut self, target_ip: Ipv4Addr) -> Self {
        self.target_ip = Some(target_ip);
        self
    }

    pub fn build(&self) -> std::result::Result<RequestInput, InputBuildError> {
        Ok(RequestInput {
            target_mac: self.target_mac.ok_or(InputBuildError::MissingTargetMac)?,
            target_ip: self.target_ip.ok_or(InputBuildError::MissingTargetIp)?,
            sender_mac: self.sender_mac.ok_or(InputBuildError::MissingSenderMac)?,
            sender_ip: self.sender_ip.ok_or(InputBuildError::MissingSenderIp)?,
        })
    }
}

#[derive(Clone, Debug)]
pub struct RequestOutcome {
    pub input: RequestInput,
    pub response: Arp,
}

impl RequestOutcome {
    pub fn new(input: RequestInput, response: Arp) -> Self {
        Self { input, response }
    }
}