Skip to content

Commit

Permalink
Return ip's instead of routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Nov 18, 2020
1 parent b4d5898 commit 0b5d380
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 433 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository = "https://github.com/dvc94ch/if-watch"
futures-lite = "1.11.2"
ipnet = "2.3.0"
libc = "0.2.66"
log = "0.4.11"

[target.'cfg(unix)'.dependencies]
async-io = "1.2.0"
Expand All @@ -20,3 +21,6 @@ async-io = "1.2.0"
futures = { version = "0.3.8", default-features = false }
if-addrs = "0.6.5"
winapi = { version = "0.3.8", features = ["netioapi", "ntdef", "winerror", "ws2def"] }

[dev-dependencies]
env_logger = "0.8.1"
1 change: 1 addition & 0 deletions examples/if_watch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use if_watch::IfWatcher;

fn main() {
env_logger::init();
futures_lite::future::block_on(async {
let mut set = IfWatcher::new().await.unwrap();
loop {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! IP address watching.
#![deny(missing_docs)]
#![deny(warnings)]
//#![deny(warnings)]

pub use ipnet::IpNet;
use std::io::Result;
Expand Down
69 changes: 29 additions & 40 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ macro_rules! align_of {
};
}

macro_rules! errno {
($t:expr) => {{
let res = $t;
if res < 0 {
Err(Error::last_os_error())
} else {
Ok(res)
}
}};
}

use crate::IfEvent;
use async_io::Async;
use ipnet::IpNet;
Expand Down Expand Up @@ -49,73 +60,51 @@ impl Drop for Fd {
}
}

#[derive(Debug)]
#[must_use]
enum Status<T> {
IO(std::io::Error),
Desync,
Data(T),
}

/// An address set/watcher
#[derive(Debug)]
pub struct IfWatcher {
hash: HashSet<IpNet>,
addrs: HashSet<IpNet>,
watcher: Watcher,
buf: Vec<u64>,
queue: VecDeque<IfEvent>,
}

impl IfWatcher {
/// Create a watcher
pub async fn new() -> Result<Self> {
let mut hash = HashSet::new();
let addrs = HashSet::new();
let queue = VecDeque::new();
let mut watcher = Watcher::new()?;
let mut buf = Vec::with_capacity(1 << 16);
let mut queue = VecDeque::new();
watcher.resync(&mut buf, &mut queue, &mut hash).await?;
watcher.send_getaddr().await?;
Ok(Self {
hash,
addrs,
watcher,
buf,
queue,
})
}

/// Returns an iterator of ip's.
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
self.hash.iter().filter(move |inet| {
self.queue.iter().find(|ev| **ev == IfEvent::Up(**inet)).is_none()
})
self.addrs.iter()
}

/// Returns a future for the next event.
pub async fn next(&mut self) -> Result<IfEvent> {
let Self {
watcher,
buf,
hash,
queue,
} = self;
if let Some(event) = queue.pop_front() {
return Ok(event);
}
loop {
match watcher.next(buf, queue, hash).await {
Status::IO(e) => return Err(e),
Status::Desync => {
if buf.capacity() < 1 << 19 {
buf.reserve(buf.capacity() * 2);
}
if watcher.resync(buf, queue, hash).await.is_err() {
continue;
while let Some(event) = self.queue.pop_front() {
match event {
IfEvent::Up(inet) => {
if self.addrs.insert(inet) {
return Ok(event);
}
}
}
Status::Data(()) => {
if let Some(event) = queue.pop_front() {
return Ok(event);
IfEvent::Down(inet) => {
if self.addrs.remove(&inet) {
return Ok(event);
}
}
}
}
self.watcher.recv_event(&mut self.queue).await?;
}
}
}
124 changes: 0 additions & 124 deletions src/unix/bsd.rs

This file was deleted.

Loading

0 comments on commit 0b5d380

Please sign in to comment.