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