]>
code.octet-stream.net Git - netwatcher/blob - src/lib.rs
2 collections
::{HashMap
, HashSet
},
3 net
::{IpAddr
, Ipv4Addr
, Ipv6Addr
},
7 #[cfg_attr(windows, path = "list_win.rs")]
8 #[cfg_attr(unix, path = "list_unix.rs")]
11 #[cfg_attr(windows, path = "watch_win.rs")]
12 #[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")]
20 #[derive(Debug, Clone, PartialEq, Eq)]
21 pub struct Interface
{
29 pub fn ipv4_ips(&self) -> impl Iterator
<Item
= &Ipv4Addr
> {
30 self.ips
.iter
().filter
_map
(|ip
| match ip
{
31 IpAddr
::V4(v4
) => Some(v4
),
32 IpAddr
::V6(_
) => None
,
36 pub fn ipv6_ips(&self) -> impl Iterator
<Item
= &Ipv6Addr
> {
37 self.ips
.iter
().filter
_map
(|ip
| match ip
{
38 IpAddr
::V4(_
) => None
,
39 IpAddr
::V6(v6
) => Some(v6
),
44 #[derive(Debug, Clone, PartialEq, Eq)]
46 pub interfaces
: HashMap
<IfIndex
, Interface
>,
50 #[derive(Debug, Clone, PartialEq, Eq, Default)]
51 pub struct UpdateDiff
{
52 pub added
: Vec
<IfIndex
>,
53 pub removed
: Vec
<IfIndex
>,
54 pub modified
: HashMap
<IfIndex
, InterfaceDiff
>,
57 #[derive(Debug, Clone, PartialEq, Eq, Default)]
58 pub struct InterfaceDiff
{
59 pub hw_addr_changed
: bool
,
60 pub addrs_added
: Vec
<IpAddr
>,
61 pub addrs_removed
: Vec
<IpAddr
>,
64 #[derive(Debug, Clone, PartialEq, Eq)]
66 // TODO: handle all cases with proper sources
70 #[derive(Default, PartialEq, Eq)]
71 struct List(HashMap
<IfIndex
, Interface
>);
74 fn diff_from(&self, prev
: &List
) -> UpdateDiff
{
75 let prev_index_set
: HashSet
<IfIndex
> = prev
.0.keys().cloned().collect();
76 let curr_index_set
: HashSet
<IfIndex
> = self.0.keys().cloned().collect();
77 let added
= curr_index_set
.sub(&prev_index_set
).into
_iter
().collect();
78 let removed
= prev_index_set
.sub(&curr_index_set
).into
_iter
().collect();
79 let mut modified
= HashMap
::new();
80 for index
in curr_index_set
.intersect
ion
(&prev_index_set
) {
81 if prev
.0[index
] == self.0[index
] {
84 let prev_addr_set
: HashSet
<&IpAddr
> = prev
.0[index
].ips
.iter
().collect();
85 let curr_addr_set
: HashSet
<&IpAddr
> = self.0[index
].ips
.iter
().collect();
86 let addrs_added
: Vec
<IpAddr
> = curr_addr_set
92 let addrs_removed
: Vec
<IpAddr
> = prev_addr_set
98 let hw_addr_changed
= prev
.0[index
].hw_addr
!= self.0[index
].hw_addr
;
116 pub struct WatchHandle
{
117 _inner
: watch
::WatchHandle
,
120 pub fn list_interfaces() -> Result
<HashMap
<IfIndex
, Interface
>, Error
> {
121 list
::list_interfaces().map(|list
| list
.0)
124 pub fn watch_interfaces
<F
: FnMut(Update
) + '
static>(callback
: F
) -> Result
<WatchHandle
, Error
> {
125 watch
::watch_interfaces(callback
).map(|handle
| WatchHandle
{ _inner
: handle
})