]>
code.octet-stream.net Git - netwatcher/blob - src/lib.rs
2 collections
::{HashMap
, HashSet
},
3 net
::{IpAddr
, Ipv4Addr
, Ipv6Addr
},
9 #[cfg_attr(windows, path = "list_win.rs")]
10 #[cfg_attr(unix, path = "list_unix.rs")]
13 #[cfg_attr(windows, path = "watch_win.rs")]
14 #[cfg_attr(target_vendor = "apple", path = "watch_mac.rs")]
16 any(target_os
= "linux", target_os
= "android"),
17 path
= "watch_linux.rs"
25 /// Information about one network interface at a point in time.
26 #[derive(Debug, Clone, PartialEq, Eq)]
27 pub struct Interface
{
35 /// Helper to iterate over only the IPv4 addresses on this interface.
36 pub fn ipv4_ips(&self) -> impl Iterator
<Item
= &Ipv4Addr
> {
37 self.ips
.iter
().filter
_map
(|ip
| match ip
{
38 IpAddr
::V4(v4
) => Some(v4
),
39 IpAddr
::V6(_
) => None
,
43 /// Helper to iterate over only the IPv6 addresses on this interface.
44 pub fn ipv6_ips(&self) -> impl Iterator
<Item
= &Ipv6Addr
> {
45 self.ips
.iter
().filter
_map
(|ip
| match ip
{
46 IpAddr
::V4(_
) => None
,
47 IpAddr
::V6(v6
) => Some(v6
),
52 /// Information delivered via callback when a network interface change is detected.
54 /// This contains up-to-date information about all interfaces, plus a diff which
55 /// details which interfaces and IP addresses have changed since the last callback.
56 #[derive(Debug, Clone, PartialEq, Eq)]
58 pub interfaces
: HashMap
<IfIndex
, Interface
>,
62 /// What changed between one `Update` and the next.
63 #[derive(Debug, Clone, PartialEq, Eq, Default)]
64 pub struct UpdateDiff
{
65 pub added
: Vec
<IfIndex
>,
66 pub removed
: Vec
<IfIndex
>,
67 pub modified
: HashMap
<IfIndex
, InterfaceDiff
>,
70 /// What changed within a single interface between updates, if it was present in both.
71 #[derive(Debug, Clone, PartialEq, Eq, Default)]
72 pub struct InterfaceDiff
{
73 pub hw_addr_changed
: bool
,
74 pub addrs_added
: Vec
<IpAddr
>,
75 pub addrs_removed
: Vec
<IpAddr
>,
78 #[derive(Default, PartialEq, Eq)]
79 struct List(HashMap
<IfIndex
, Interface
>);
82 fn diff_from(&self, prev
: &List
) -> UpdateDiff
{
83 let prev_index_set
: HashSet
<IfIndex
> = prev
.0.keys().cloned().collect();
84 let curr_index_set
: HashSet
<IfIndex
> = self.0.keys().cloned().collect();
85 let added
= curr_index_set
.sub(&prev_index_set
).into
_iter
().collect();
86 let removed
= prev_index_set
.sub(&curr_index_set
).into
_iter
().collect();
87 let mut modified
= HashMap
::new();
88 for index
in curr_index_set
.intersect
ion
(&prev_index_set
) {
89 if prev
.0[index
] == self.0[index
] {
92 let prev_addr_set
: HashSet
<&IpAddr
> = prev
.0[index
].ips
.iter
().collect();
93 let curr_addr_set
: HashSet
<&IpAddr
> = self.0[index
].ips
.iter
().collect();
94 let addrs_added
: Vec
<IpAddr
> = curr_addr_set
100 let addrs_removed
: Vec
<IpAddr
> = prev_addr_set
106 let hw_addr_changed
= prev
.0[index
].hw_addr
!= self.0[index
].hw_addr
;
124 /// A handle to keep alive as long as you wish to receive callbacks.
126 /// If the callback is executing at the time the handle is dropped, drop will block until
127 /// the callback is finished and it's guaranteed that it will not be called again.
129 /// Do not drop the handle from within the callback itself. It will probably deadlock.
130 pub struct WatchHandle
{
131 _inner
: watch
::WatchHandle
,
134 /// Retrieve information about all enabled network interfaces and their IP addresses.
136 /// This is a once-off operation. If you want to detect changes over time, see `watch_interfaces`.
137 pub fn list_interfaces() -> Result
<HashMap
<IfIndex
, Interface
>, Error
> {
138 list
::list_interfaces().map(|list
| list
.0)
141 /// Retrieve interface information and watch for changes, which will be delivered via callback.
143 /// If setting up the watch is successful, this returns a `WatchHandle` which must be kept for
144 /// as long as the provided callback should operate.
146 /// The callback will fire once immediately with an initial interface list, and a diff as if
147 /// there were originally no interfaces present.
149 /// This function will return an error if there is a problem configuring the watcher, or if there
150 /// is an error retrieving the initial interface list.
152 /// We assume that if listing the interfaces worked the first time, then it will continue to work
153 /// for as long as the watcher is running. If listing interfaces begins to fail later, those
154 /// failures will be swallowed and the callback will not be called for that change event.
155 pub fn watch_interfaces
<F
: FnMut(Update
) + Send
+ '
static>(
157 ) -> Result
<WatchHandle
, Error
> {
158 watch
::watch_interfaces(callback
).map(|handle
| WatchHandle
{ _inner
: handle
})