]> code.octet-stream.net Git - netwatcher/blob - src/lib.rs
e7bf15234aaaf92a217e90a7d56c57598fdf93b5
[netwatcher] / src / lib.rs
1 use std::{
2 collections::{HashMap, HashSet},
3 net::{IpAddr, Ipv4Addr, Ipv6Addr},
4 ops::Sub,
5 };
6
7 #[cfg_attr(windows, path = "list_win.rs")]
8 #[cfg_attr(unix, path = "list_unix.rs")]
9 mod list;
10
11 #[cfg_attr(windows, path = "watch_win.rs")]
12 #[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")]
13 #[cfg_attr(target_os = "linux", path = "watch_linux.rs")]
14 mod watch;
15
16 #[cfg(unix)]
17 mod util;
18
19 type IfIndex = u32;
20
21 #[derive(Debug, Clone, PartialEq, Eq)]
22 pub struct Interface {
23 pub index: u32,
24 pub name: String,
25 pub hw_addr: String,
26 pub ips: Vec<IpAddr>,
27 }
28
29 impl Interface {
30 pub fn ipv4_ips(&self) -> impl Iterator<Item = &Ipv4Addr> {
31 self.ips.iter().filter_map(|ip| match ip {
32 IpAddr::V4(v4) => Some(v4),
33 IpAddr::V6(_) => None,
34 })
35 }
36
37 pub fn ipv6_ips(&self) -> impl Iterator<Item = &Ipv6Addr> {
38 self.ips.iter().filter_map(|ip| match ip {
39 IpAddr::V4(_) => None,
40 IpAddr::V6(v6) => Some(v6),
41 })
42 }
43 }
44
45 #[derive(Debug, Clone, PartialEq, Eq)]
46 pub struct Update {
47 pub interfaces: HashMap<IfIndex, Interface>,
48 pub diff: UpdateDiff,
49 }
50
51 #[derive(Debug, Clone, PartialEq, Eq, Default)]
52 pub struct UpdateDiff {
53 pub added: Vec<IfIndex>,
54 pub removed: Vec<IfIndex>,
55 pub modified: HashMap<IfIndex, InterfaceDiff>,
56 }
57
58 #[derive(Debug, Clone, PartialEq, Eq, Default)]
59 pub struct InterfaceDiff {
60 pub hw_addr_changed: bool,
61 pub addrs_added: Vec<IpAddr>,
62 pub addrs_removed: Vec<IpAddr>,
63 }
64
65 #[derive(Debug, Clone, PartialEq, Eq)]
66 pub enum Error {
67 // TODO: handle all cases with proper sources
68 Internal,
69 }
70
71 #[derive(Default, PartialEq, Eq)]
72 struct List(HashMap<IfIndex, Interface>);
73
74 impl List {
75 fn diff_from(&self, prev: &List) -> UpdateDiff {
76 let prev_index_set: HashSet<IfIndex> = prev.0.keys().cloned().collect();
77 let curr_index_set: HashSet<IfIndex> = self.0.keys().cloned().collect();
78 let added = curr_index_set.sub(&prev_index_set).into_iter().collect();
79 let removed = prev_index_set.sub(&curr_index_set).into_iter().collect();
80 let mut modified = HashMap::new();
81 for index in curr_index_set.intersection(&prev_index_set) {
82 if prev.0[index] == self.0[index] {
83 continue;
84 }
85 let prev_addr_set: HashSet<&IpAddr> = prev.0[index].ips.iter().collect();
86 let curr_addr_set: HashSet<&IpAddr> = self.0[index].ips.iter().collect();
87 let addrs_added: Vec<IpAddr> = curr_addr_set
88 .sub(&prev_addr_set)
89 .iter()
90 .cloned()
91 .cloned()
92 .collect();
93 let addrs_removed: Vec<IpAddr> = prev_addr_set
94 .sub(&curr_addr_set)
95 .iter()
96 .cloned()
97 .cloned()
98 .collect();
99 let hw_addr_changed = prev.0[index].hw_addr != self.0[index].hw_addr;
100 modified.insert(
101 *index,
102 InterfaceDiff {
103 hw_addr_changed,
104 addrs_added,
105 addrs_removed,
106 },
107 );
108 }
109 UpdateDiff {
110 added,
111 removed,
112 modified,
113 }
114 }
115 }
116
117 pub struct WatchHandle {
118 _inner: watch::WatchHandle,
119 }
120
121 pub fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
122 list::list_interfaces().map(|list| list.0)
123 }
124
125 pub fn watch_interfaces<F: FnMut(Update) + 'static>(callback: F) -> Result<WatchHandle, Error> {
126 watch::watch_interfaces(callback).map(|handle| WatchHandle { _inner: handle })
127 }