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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use netdev::mac::MacAddr;
use nex::net::interface::Interface;
use std::{
    collections::{HashMap, HashSet},
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

pub fn get_interface_by_ip(ip_addr: IpAddr) -> Option<Interface> {
    for iface in nex::net::interface::get_interfaces() {
        for ip in iface.ipv4.clone() {
            if ip.addr == ip_addr {
                return Some(iface);
            }
        }
        for ip in iface.ipv6.clone() {
            if ip.addr == ip_addr {
                return Some(iface);
            }
        }
    }
    return None;
}

pub fn get_interface_by_index(index: u32) -> Option<Interface> {
    for iface in nex::net::interface::get_interfaces() {
        if iface.index == index {
            return Some(iface);
        }
    }
    return None;
}

pub fn get_interface_by_name(name: String) -> Option<Interface> {
    for iface in nex::net::interface::get_interfaces() {
        if iface.name == name {
            return Some(iface);
        }
    }
    return None;
}

pub fn get_interface_ipv4(iface: &Interface) -> Option<IpAddr> {
    for ip in iface.ipv4.clone() {
        return Some(IpAddr::V4(ip.addr));
    }
    return None;
}

pub fn get_interface_global_ipv6(iface: &Interface) -> Option<IpAddr> {
    for ip in iface.ipv6.clone() {
        if nex::net::ip::is_global_ipv6(&ip.addr) {
            return Some(IpAddr::V6(ip.addr));
        }
    }
    return None;
}

pub fn get_interface_local_ipv6(iface: &Interface) -> Option<IpAddr> {
    for ip in iface.ipv6.clone() {
        if !nex::net::ip::is_global_ipv6(&ip.addr) {
            return Some(IpAddr::V6(ip.addr));
        }
    }
    return None;
}

pub fn get_interface_ips(iface: &Interface) -> Vec<String> {
    let mut ips: Vec<String> = Vec::new();
    for ip in iface.ipv4.clone() {
        ips.push(ip.addr.to_string());
    }
    for ip in iface.ipv6.clone() {
        ips.push(ip.addr.to_string());
    }
    ips
}

pub fn get_local_ips(if_index: u32) -> HashSet<IpAddr> {
    let interface = get_interface_by_index(if_index).unwrap();
    let mut ips: HashSet<IpAddr> = HashSet::new();
    for ip in interface.ipv4.clone() {
        ips.insert(IpAddr::V4(ip.addr));
    }
    for ip in interface.ipv6.clone() {
        ips.insert(IpAddr::V6(ip.addr));
    }
    // localhost IP addresses
    ips.insert(IpAddr::V4(Ipv4Addr::LOCALHOST));
    ips.insert(IpAddr::V6(Ipv6Addr::LOCALHOST));
    ips
}

pub fn get_default_local_ips() -> HashSet<IpAddr> {
    // Default interface IP addresses
    let default_interface = netdev::get_default_interface().unwrap();
    let mut ips: HashSet<IpAddr> = HashSet::new();
    for ip in default_interface.ipv4.clone() {
        ips.insert(IpAddr::V4(ip.addr));
    }
    for ip in default_interface.ipv6.clone() {
        ips.insert(IpAddr::V6(ip.addr));
    }
    // localhost IP addresses
    ips.insert(IpAddr::V4(Ipv4Addr::LOCALHOST));
    ips.insert(IpAddr::V6(Ipv6Addr::LOCALHOST));
    ips
}

pub fn get_interface_local_ips(iface: &Interface) -> HashSet<IpAddr> {
    let mut ips: HashSet<IpAddr> = HashSet::new();
    for ip in iface.ipv4.clone() {
        ips.insert(IpAddr::V4(ip.addr));
    }
    for ip in iface.ipv6.clone() {
        ips.insert(IpAddr::V6(ip.addr));
    }
    // localhost IP addresses
    ips.insert(IpAddr::V4(Ipv4Addr::LOCALHOST));
    ips.insert(IpAddr::V6(Ipv6Addr::LOCALHOST));
    ips
}

pub fn get_local_ip_map() -> HashMap<IpAddr, String> {
    let mut ip_map: HashMap<IpAddr, String> = HashMap::new();
    for iface in nex::net::interface::get_interfaces() {
        for ip in iface.ipv4.clone() {
            ip_map.insert(IpAddr::V4(ip.addr), iface.name.clone());
        }
        for ip in iface.ipv6.clone() {
            ip_map.insert(IpAddr::V6(ip.addr), iface.name.clone());
        }
    }
    ip_map
}

// get usable interface list
pub fn get_usable_interfaces() -> Vec<Interface> {
    let mut usable_interfaces: Vec<Interface> = Vec::new();
    for iface in nex::net::interface::get_interfaces() {
        if iface.is_up() && (iface.ipv4.len() > 0 || iface.ipv6.len() > 0) {
            usable_interfaces.push(iface);
        }
    }
    usable_interfaces
}

pub fn get_interface_macaddr(iface: &Interface) -> MacAddr {
    match &iface.mac_addr {
        Some(mac_addr) => mac_addr.clone(),
        None => MacAddr::zero(),
    }
}

pub fn get_gateway_macaddr(iface: &Interface) -> MacAddr {
    match &iface.gateway {
        Some(gateway) => gateway.mac_addr.clone(),
        None => MacAddr::zero(),
    }
}