]> code.octet-stream.net Git - netwatcher/blob - src/lib.rs
Groundwork for watching iface changes on Apple via Network.framework
[netwatcher] / src / lib.rs
1 use std::{
2 collections::HashMap,
3 net::{IpAddr, Ipv4Addr, Ipv6Addr},
4 };
5
6 #[cfg_attr(windows, path = "imp_win.rs")]
7 #[cfg_attr(target_vendor = "apple", path = "imp_mac.rs")]
8 mod imp;
9 mod util;
10
11 type IfIndex = u32;
12
13 #[derive(Debug, Clone, PartialEq, Eq)]
14 pub struct Interface {
15 pub index: u32,
16 pub name: String,
17 pub hw_addr: String,
18 pub ips: Vec<IpAddr>,
19 }
20
21 impl Interface {
22 pub fn ipv4_ips(&self) -> impl Iterator<Item = &Ipv4Addr> {
23 self.ips.iter().filter_map(|ip| match ip {
24 IpAddr::V4(v4) => Some(v4),
25 IpAddr::V6(_) => None,
26 })
27 }
28
29 pub fn ipv6_ips(&self) -> impl Iterator<Item = &Ipv6Addr> {
30 self.ips.iter().filter_map(|ip| match ip {
31 IpAddr::V4(_) => None,
32 IpAddr::V6(v6) => Some(v6),
33 })
34 }
35 }
36
37 #[derive(Debug, Clone, PartialEq, Eq)]
38 pub struct Update {
39 pub interfaces: HashMap<IfIndex, Interface>,
40 pub diff: UpdateDiff,
41 }
42
43 #[derive(Debug, Clone, PartialEq, Eq)]
44 pub struct UpdateDiff {
45 pub added: Vec<IfIndex>,
46 pub removed: Vec<IfIndex>,
47 pub modified: HashMap<IfIndex, InterfaceDiff>,
48 }
49
50 #[derive(Debug, Clone, PartialEq, Eq)]
51 pub struct InterfaceDiff {
52 pub hw_addr_changed: bool,
53 pub addrs_added: Vec<IpAddr>,
54 pub addrs_removed: Vec<IpAddr>,
55 }
56
57 #[derive(Debug, Clone, PartialEq, Eq)]
58 pub enum Error {
59 Internal,
60 }
61
62 pub fn list_interfaces() -> Result<HashMap<IfIndex, Interface>, Error> {
63 imp::list_interfaces()
64 }
65
66 pub struct WatchHandle;
67
68 pub fn watch_interfaces<F: FnMut(Update)>(callback: F) -> WatchHandle {
69 // stop current worker thread
70 // post this into a thread that will use it
71 drop(callback);
72 WatchHandle
73 }