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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use pnet::datalink::{interfaces, NetworkInterface};
use std::net::{IpAddr, Ipv4Addr};

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct Interface {
    network_interface: Option<NetworkInterface>,
}

impl Interface {
    pub fn new() -> Self {
        Interface {
            network_interface: Interface::get_interface_by_guess(),
        }
    }

    pub fn new_by_name(interface_name: String) -> Self {
        Interface {
            network_interface: Interface::get_interface_by_name(&interface_name),
        }
    }

    pub fn get_ip(&self) -> Ipv4Addr {
        self.network_interface
            .as_ref()
            .unwrap()
            .ips
            .iter()
            .find(|ip| ip.is_ipv4())
            .map(|ip| match ip.ip() {
                IpAddr::V4(ip) => ip,
                _ => panic!("Interface only supports IPv6, but IPv4 address was requested."),
            })
            .unwrap()
    }

    pub fn get_mac(&self) -> MacAddr {
        self.network_interface.as_ref().unwrap().mac.unwrap().into()
    }

    pub fn get_raw_interface(&self) -> &NetworkInterface {
        &self.network_interface.as_ref().unwrap()
    }

    fn get_all_interfaces() -> Vec<NetworkInterface> {
        interfaces()
    }

    fn get_interface_by_guess() -> Option<NetworkInterface> {
        let considered_ifaces = Interface::get_all_interfaces()
            .into_iter()
            .filter(|iface| !iface.is_loopback() && iface.is_up() && !iface.ips.is_empty())
            .collect::<Vec<NetworkInterface>>();

        match considered_ifaces.first() {
            Some(iface) => Some(iface.clone()),
            None => None,
        }
    }

    fn get_interface_by_name(name: &str) -> Option<NetworkInterface> {
        let considered_ifaces = Interface::get_all_interfaces()
            .into_iter()
            .filter(|iface| iface.name == *name)
            .collect::<Vec<NetworkInterface>>();

        match considered_ifaces.first() {
            Some(iface) => Some(iface.clone()),
            None => None,
        }
    }
}

// redefinition, so that as a user pnet does not need to be imported
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct MacAddr(pub u8, pub u8, pub u8, pub u8, pub u8, pub u8);

impl MacAddr {
    pub fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Self {
        MacAddr(a, b, c, d, e, f)
    }
}

impl std::fmt::Display for MacAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f,  "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", self.0, self.1, self.2, self.3, self.4, self.5)
    }
}

impl From<pnet::util::MacAddr> for MacAddr {
    fn from(pnet_mac_addr: pnet::util::MacAddr) -> Self {
        MacAddr(
            pnet_mac_addr.0,
            pnet_mac_addr.1,
            pnet_mac_addr.2,
            pnet_mac_addr.3,
            pnet_mac_addr.4,
            pnet_mac_addr.5,
        )
    }
}

impl From<MacAddr> for pnet::util::MacAddr {
    fn from(mac_addr: MacAddr) -> Self {
        pnet::util::MacAddr(
            mac_addr.0, mac_addr.1, mac_addr.2, mac_addr.3, mac_addr.4, mac_addr.5,
        )
    }
}