]> code.octet-stream.net Git - netwatcher/blob - src/lib.rs
Split out list and watch implementations
[netwatcher] / src / lib.rs
1 use std::{
2 collections::HashMap,
3 net::{IpAddr, Ipv4Addr, Ipv6Addr},
4 };
5
6 #[cfg_attr(windows, path = "list_win.rs")]
7 #[cfg_attr(target_vendor = "apple", path = "list_mac.rs")]
8 mod list;
9
10 #[cfg_attr(windows, path = "watch_win.rs")]
11 #[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")]
12 mod watch;
13
14 mod util;
15
16 type IfIndex = u32;
17
18 #[derive(Debug, Clone, PartialEq, Eq)]
19 pub struct Interface {
20 pub index: u32,
21 pub name: String,
22 pub hw_addr: String,
23 pub ips: Vec<IpAddr>,
24 }
25
26 impl Interface {
27 pub fn ipv4_ips(&self) -> impl Iterator<Item = &Ipv4Addr> {
28 self.ips.iter().filter_map(|ip| match ip {
29 IpAddr::V4(v4) => Some(v4),
30 IpAddr::V6(_) => None,
31 })
32 }
33
34 pub fn ipv6_ips(&self) -> impl Iterator<Item = &Ipv6Addr> {
35 self.ips.iter().filter_map(|ip| match ip {
36 IpAddr::V4(_) => None,
37 IpAddr::V6(v6) => Some(v6),
38 })
39 }
40 }
41
42 #[derive(Debug, Clone, PartialEq, Eq)]
43 pub struct Update {
44 pub interfaces: HashMap<IfIndex, Interface>,
45 pub diff: UpdateDiff,
46 }
47
48 impl Update {
49 pub fn diff_from_previous(_prev: &Update) -> UpdateDiff {
50 // TODO: real calculation
51 UpdateDiff::default()
52 }
53 }
54
55 #[derive(Debug, Clone, PartialEq, Eq, Default)]
56 pub struct UpdateDiff {
57 pub added: Vec<IfIndex>,
58 pub removed: Vec<IfIndex>,
59 pub modified: HashMap<IfIndex, InterfaceDiff>,
60 }
61
62 #[derive(Debug, Clone, PartialEq, Eq, Default)]
63 pub struct InterfaceDiff {
64 pub hw_addr_changed: bool,
65 pub addrs_added: Vec<IpAddr>,
66 pub addrs_removed: Vec<IpAddr>,
67 }
68
69 #[derive(Debug, Clone, PartialEq, Eq)]
70 pub enum Error {
71 // TODO: handle all cases with proper sources
72 Internal,
73 }
74
75 pub use list::list_interfaces;
76 pub use watch::{watch_interfaces, WatchHandle};
77