]> code.octet-stream.net Git - netwatcher/blob - src/list_mac.rs
4ae0fc7c8b6d1053b2f77bc69bc9dc4ec9089db7
[netwatcher] / src / list_mac.rs
1 use std::{collections::HashMap, net::IpAddr};
2
3 use block2::Block;
4 use nix::libc::c_long;
5 use nix::{ifaddrs::getifaddrs, net::if_::if_nametoindex};
6
7 use crate::util::format_mac;
8 use crate::{Error, IfIndex, Interface};
9
10 struct CandidateInterface {
11 name: String,
12 index: u32,
13 hw_addr: Option<String>,
14 ips: Vec<IpAddr>,
15 }
16
17 pub(crate) fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
18 let addrs = getifaddrs().map_err(|_| Error::Internal)?;
19 let mut candidates = HashMap::new();
20
21 for addr in addrs {
22 let index = if_nametoindex(addr.interface_name.as_str()).map_err(|_| Error::Internal)?;
23 let candidate = candidates
24 .entry(addr.interface_name.clone())
25 .or_insert_with(|| CandidateInterface {
26 name: addr.interface_name.clone(),
27 index,
28 hw_addr: None,
29 ips: vec![],
30 });
31 if let Some(a) = addr.address {
32 if let Some(a) = a.as_link_addr() {
33 if let Some(raw_addr) = a.addr() {
34 candidate.hw_addr = Some(format_mac(&raw_addr)?);
35 }
36 }
37 if let Some(a) = a.as_sockaddr_in() {
38 candidate.ips.push(IpAddr::V4(a.ip()));
39 }
40 if let Some(a) = a.as_sockaddr_in6() {
41 candidate.ips.push(IpAddr::V6(a.ip()));
42 }
43 }
44 }
45
46 let ifs = candidates
47 .drain()
48 .flat_map(|(_, c)| {
49 c.hw_addr.map(|hw_addr| {
50 (
51 c.index,
52 Interface {
53 index: c.index,
54 hw_addr,
55 name: c.name,
56 ips: c.ips,
57 },
58 )
59 })
60 })
61 .collect();
62 Ok(ifs)
63 }
64
65 // The "objc2" project aims to provide bindings for all frameworks but Network.framework
66 // isn't ready yet so let's kick it old-school
67
68 struct nw_path_monitor;
69 type nw_path_monitor_t = *mut nw_path_monitor;
70 struct nw_path;
71 type nw_path_t = *mut nw_path;
72 struct dispatch_queue;
73 type dispatch_queue_t = *mut dispatch_queue;
74 const QOS_CLASS_BACKGROUND: usize = 0x09;
75
76 #[link(name = "Network", kind = "framework")]
77 extern "C" {
78 fn nw_path_monitor_create() -> nw_path_monitor_t;
79 fn nw_path_monitor_set_update_handler(
80 monitor: nw_path_monitor_t,
81 update_handler: &Block<dyn Fn(nw_path_t)>,
82 );
83 fn nw_path_monitor_set_queue(monitor: nw_path_monitor_t, queue: dispatch_queue_t);
84 fn nw_path_monitor_start(monitor: nw_path_monitor_t);
85 fn nw_path_monitor_cancel(monitor: nw_path_monitor_t);
86
87 fn dispatch_get_global_queue(identifier: usize, flag: usize) -> dispatch_queue_t;
88 }
89
90 #[cfg(test)]
91 mod test {
92 use super::list_interfaces;
93
94 #[test]
95 fn list() {
96 let ifaces = list_interfaces().unwrap();
97 println!("{:?}", ifaces);
98 }
99 }