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
use std::fmt;
use std::net::IpAddr;

use mac_address::MacAddress;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct NetInterface {
    pub host_uuid: Uuid,
    pub uuid: Uuid,
    pub name: String,
    pub mac_addr: MacAddress,
}

impl fmt::Display for NetInterface {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let parts: Vec<String> = vec![
            self.host_uuid.to_string(),
            self.name.clone(),
            // self.ip_addr
            //     .iter()
            //     .map(|ip| ip.to_string())
            //     .collect::<Vec<String>>()
            //     .join(", "),
        ];
        write!(f, "{}", parts.join("_"))
    }
}

impl NetInterface {
    // pub fn fingerprint(&self, ip_addr: &IpAddr) -> [u8; 32] {
    //     let mut hash = Hash::new();
    //     hash.update(self.host_id);
    //     hash.update(self.name.clone());
    //     hash.update(ip_addr.to_string());
    //     hash.finalize()
    // }
    // pub fn fingerprint_set(&self) -> HashSet<[u8; 32]> {
    //     let mut set = HashSet::new();
    //     for addr in self.ip_addr.iter() {
    //         set.insert(self.fingerprint(addr));
    //     }
    //     set
    // }
}

// pub fn identify_interface(
//     known_interfaces: &[NetInterfaceId],
//     remote_socket: &SocketAddr,
//     fingerprint: [u8; 32],
// ) -> Result<NetInterfaceId, NetzworkApiError> {
//     for iface in known_interfaces.iter() {
//         if iface.ip_addr.contains(&remote_socket.ip())
//             && iface.fingerprint(&remote_socket.ip()) == fingerprint
//         {
//             return Ok(iface.to_owned());
//         };
//     }
//     Err(NetzworkApiError::UnknownNetInterfaceId(format!(
//         "{:?} with fingerprint {:?}",
//         remote_socket, fingerprint
//     )))
// }

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct NetInterfaceAddr {
    pub iface_uuid: Uuid,
    pub uuid: Uuid,
    pub ip_addr: IpAddr,
    pub active: bool,
}