Skip to content

Commit

Permalink
Cleaner function export, better doc, "fix" macos/ios by disabling them
Browse files Browse the repository at this point in the history
  • Loading branch information
mon committed Apr 4, 2024
1 parent 8bf87d6 commit 5a67b22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod windows;

use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::time::Duration;

Check warning on line 18 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable)

unused import: `std::time::Duration`

Check warning on line 18 in src/lib.rs

View workflow job for this annotation

GitHub Actions / iOS Build (x86_64-apple-ios)

unused import: `std::time::Duration`

/// Details about an interface on this host.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
Expand Down Expand Up @@ -366,10 +367,21 @@ pub fn get_if_addrs() -> io::Result<Vec<Interface>> {
getifaddrs_windows::get_if_addrs()
}

#[cfg(windows)]
pub use windows::detect_interface_changes;
#[cfg(not(windows))]
pub use posix::detect_interface_changes;
/// (Not available on iOS/macOS) Block until the OS reports that the network
/// interface list has changed, or until an optional timeout.
///
/// For example, if an ethernet connector is plugged/unplugged, or a WiFi
/// network is connected to.
///
/// Returns an [`io::ErrorKind::WouldBlock`] error on timeout, or another error
/// if the network notifier could not be set up.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub fn detect_interface_changes(timeout: Option<Duration>) -> io::Result<()> {
#[cfg(windows)]
return windows::detect_interface_changes(timeout);
#[cfg(not(windows))]
return posix::detect_interface_changes(timeout);
}

#[cfg(test)]
mod tests {
Expand Down
6 changes: 2 additions & 4 deletions src/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

use crate::sockaddr;
use libc::{
bind, close, freeifaddrs, getifaddrs, ifaddrs, sockaddr_nl, socket, AF_NETLINK, NETLINK_ROUTE, SOCK_RAW
bind, close, freeifaddrs, getifaddrs, ifaddrs, sockaddr_nl, socket, AF_NETLINK, NETLINK_ROUTE,

Check failure on line 12 in src/posix.rs

View workflow job for this annotation

GitHub Actions / Test Suite (macos-latest, stable)

unresolved imports `libc::sockaddr_nl`, `libc::AF_NETLINK`, `libc::NETLINK_ROUTE`

Check failure on line 12 in src/posix.rs

View workflow job for this annotation

GitHub Actions / iOS Build (x86_64-apple-ios)

unresolved imports `libc::sockaddr_nl`, `libc::AF_NETLINK`, `libc::NETLINK_ROUTE`
SOCK_RAW,
};
use std::net::{IpAddr, UdpSocket};
use std::os::fd::FromRawFd;
Expand Down Expand Up @@ -99,9 +100,6 @@ impl Iterator for IfAddrsIterator {
}
}

/// Block until the OS reports that the network interface list has changed, or
/// until an optional timeout. Returns an [`io::ErrorKind::WouldBlock`] error on
/// timeout, or another error if the network notifier could not be set up.
pub fn detect_interface_changes(timeout: Option<Duration>) -> io::Result<()> {
let socket = unsafe { socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) };
if socket < 0 {
Expand Down
14 changes: 8 additions & 6 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ use std::ffi::CStr;
use std::time::Duration;
use std::{io, ptr};
use windows_sys::Win32::Foundation::{
ERROR_BUFFER_OVERFLOW, ERROR_SUCCESS, HANDLE, NO_ERROR, WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT
ERROR_BUFFER_OVERFLOW, ERROR_SUCCESS, HANDLE, NO_ERROR, WAIT_ABANDONED, WAIT_OBJECT_0,
WAIT_TIMEOUT,
};
use windows_sys::Win32::NetworkManagement::IpHelper::{
CancelIPChangeNotify, GetAdaptersAddresses, NotifyAddrChange, GAA_FLAG_INCLUDE_PREFIX,
GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER, GAA_FLAG_SKIP_MULTICAST,
IP_ADAPTER_ADDRESSES_LH, IP_ADAPTER_PREFIX_XP, IP_ADAPTER_UNICAST_ADDRESS_LH,
};
use windows_sys::Win32::Networking::WinSock::{WSACloseEvent, WSACreateEvent, WSAGetLastError, WSA_IO_PENDING};
use windows_sys::Win32::Networking::WinSock::{
WSACloseEvent, WSACreateEvent, WSAGetLastError, WSA_IO_PENDING,
};
use windows_sys::Win32::System::Memory::{
GetProcessHeap, HeapAlloc, HeapFree, HEAP_NONE, HEAP_ZERO_MEMORY,
};
Expand Down Expand Up @@ -213,9 +216,6 @@ impl<'a> Iterator for UnicastAddressesIterator<'a> {
}
}

/// Block until the OS reports that the network interface list has changed, or
/// until an optional timeout. Returns an [`io::ErrorKind::WouldBlock`] error on
/// timeout, or another error if the network notifier could not be set up.
pub fn detect_interface_changes(timeout: Option<Duration>) -> io::Result<()> {
let mut overlap: OVERLAPPED = unsafe { std::mem::zeroed() };
let mut notify_event: HANDLE = Default::default();
Expand All @@ -239,7 +239,9 @@ pub fn detect_interface_changes(timeout: Option<Duration>) -> io::Result<()> {

let ret = match unsafe { WaitForSingleObject(overlap.hEvent, millis) } {
WAIT_OBJECT_0 => Ok(()),
WAIT_TIMEOUT | WAIT_ABANDONED => Err(io::Error::new(io::ErrorKind::WouldBlock, "Timed out")),
WAIT_TIMEOUT | WAIT_ABANDONED => {
Err(io::Error::new(io::ErrorKind::WouldBlock, "Timed out"))
}
_ => Err(io::Error::last_os_error()),
};
unsafe {
Expand Down

0 comments on commit 5a67b22

Please sign in to comment.