feat: interface can be filtered and flags #66
Conversation
- unix like os use interface `ifa_flags` to map interface types, windows use `IfType` - add filter fn:wq
- format code by running `cargo fmt` - add some comments for constants
- format code by running `cargo fmt` - add some comments for constants
There was a problem hiding this comment.
Pull request overview
This PR adds interface filtering functionality and platform-specific flag mapping to distinguish between different network interface types. It introduces a Status enum to represent interface operational status and adds a filter() method to allow users to filter network interfaces by flags (e.g., Ethernet, wireless, tunnel, loopback).
- Adds
Statusenum withUnknown,Up,Down, andUnavailablestates - Adds
filter()method toNetworkInterfaceConfigtrait for filtering interfaces by flags - Maps Unix
ifa_flagsand WindowsIfTypeto custom flag constants for cross-platform filtering - Updates
NetworkInterfacestruct to includestatusandflagsfields
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 19 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib.rs | Adds filter() method to NetworkInterfaceConfig trait with documentation |
| src/interface.rs | Defines Status enum, flag constants for both Unix and Windows, adds status and flags fields to NetworkInterface, adds is_up() helper method |
| src/target/windows.rs | Implements filter() method, adds get_adapter_operstatus() and get_adapter_flags() functions to map Windows adapter types to custom flags |
| src/target/unix/mod.rs | Implements filter() method, adds netifa_status() function to map Unix flags to Status enum, updates interface creation to include status and flags |
| src/target/linux.rs | Implements filter() method, adds netifa_status() function for Linux-specific status mapping, updates interface creation to include status and flags |
| examples/demo.rs | Updates example to demonstrate filtering by IFF_ETH | IFF_RUNNING flags |
| Cargo.toml | Removes duplicate "network" keyword and adds explicit example configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Unavailable, | ||
| } | ||
|
|
||
| /// Filter Running deivces, on Windows, it has no effect |
There was a problem hiding this comment.
Typo: "deivces" should be "devices".
| /// Filter Running deivces, on Windows, it has no effect | ||
| #[cfg(target_os = "windows")] | ||
| pub const IFF_RUNNING: i32 = 0x40; | ||
| /// Filter Ethernet interfaces, on *unix is can be `IFF_UP | IFF_BROADCAST | IFF_MULTICAST`, on windows adapter `ifType` is used |
There was a problem hiding this comment.
Typo: "is can be" should be "can be".
| /// Filter Ethernet interfaces, on *unix is can be `IFF_UP | IFF_BROADCAST | IFF_MULTICAST`, on windows adapter `ifType` is used | ||
| #[cfg(target_os = "windows")] | ||
| pub const IFF_ETH: i32 = 0x1; | ||
| /// Filter Wireless interfaces sometimes it sames as Eth, on windows adapter `ifType` is used. |
There was a problem hiding this comment.
Typo: "sames" should be "same".
| pub const IFF_WIRELESS: i32 = 0x2; | ||
| ///Filter out TUN interfaces. Note! This is only a hypothesis. on windows adapter `ifType` is used. | ||
| #[cfg(target_os = "windows")] | ||
| pub const IFF_TUN: i32 = 0x1 | 0x4; |
There was a problem hiding this comment.
The IFF_TUN constant is defined as 0x1 | 0x4 which equals 0x5. However, looking at get_adapter_flags() in windows.rs (line 350), IF_TYPE_TUNNEL maps to IFF_TUN which should be 0x4, but the constant here is 0x5. This inconsistency means filtering by IFF_TUN won't correctly match tunnel interfaces because 0x4 & 0x5 != 0x5.
| pub const IFF_TUN: i32 = 0x1 | 0x4; | |
| pub const IFF_TUN: i32 = 0x4; |
There was a problem hiding this comment.
@hanskorg have you checked on this one? I'm not a Windows expert.
I would appreciate your input here.
| /// Filter Wireless interfaces sometimes it sames as Eth, on windows adapter `ifType` is used. | ||
| #[cfg(not(target_os = "windows"))] | ||
| pub const IFF_WIRELESS: i32 = libc::IFF_UP | libc::IFF_BROADCAST | libc::IFF_MULTICAST; | ||
| ///Filter out TUN interfaces. Note! This is only a hypothesis. on windows adapter `ifType` is used. |
There was a problem hiding this comment.
Missing space after /// in the comment. Should be /// Filter out TUN interfaces... for consistency with other comments.
| Unknown, | ||
| /// device is up | ||
| Up, | ||
| /// deivce down, by default you can't get device markd `down` |
There was a problem hiding this comment.
Typo: "deivce" should be "device".
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
ifa_flagsto map interface types, on windowsIt uses
IfType