Skip to content

feat: interface can be filtered and flags #66

Open
hanskorg wants to merge 24 commits into
LeoBorai:mainfrom
hanskorg:main
Open

feat: interface can be filtered and flags #66
hanskorg wants to merge 24 commits into
LeoBorai:mainfrom
hanskorg:main

Conversation

@hanskorg

@hanskorg hanskorg commented Dec 2, 2025

Copy link
Copy Markdown
  • unix like os uses interface ifa_flags to map interface types, on windows
    It uses IfType
  • add filter fn
///  This helps people filter interfaces that look like`Ethernet` and are running
NetworkInterface::filter(NetworkInterface::show().unwarp(), IFF_ETH   | IFF_RUNNING)

Compared to the previous failed pull request, more testing was done on Windows systems. Several magic numbers were replaced with Windows API constants.

infra-bot and others added 17 commits November 27, 2025 15:37
- unix like os use interface `ifa_flags` to map interface types, windows
  use `IfType`
- add filter fn:wq
- windows not set flags
- filter  `netif.flags` empty remove
- format code by running `cargo fmt`
- add some comments for constants
- format code by running `cargo fmt`
- add some comments for constants
- windows interface verify
- demo lint

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Status enum with Unknown, Up, Down, and Unavailable states
  • Adds filter() method to NetworkInterfaceConfig trait for filtering interfaces by flags
  • Maps Unix ifa_flags and Windows IfType to custom flag constants for cross-platform filtering
  • Updates NetworkInterface struct to include status and flags fields

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.

Comment thread src/interface.rs Outdated
Comment thread src/interface.rs Outdated
Comment thread src/interface.rs
Unavailable,
}

/// Filter Running deivces, on Windows, it has no effect

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "deivces" should be "devices".

Copilot uses AI. Check for mistakes.
Comment thread src/interface.rs
/// 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

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "is can be" should be "can be".

Copilot uses AI. Check for mistakes.
Comment thread src/interface.rs
/// 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.

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "sames" should be "same".

Copilot uses AI. Check for mistakes.
Comment thread src/interface.rs
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;

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
pub const IFF_TUN: i32 = 0x1 | 0x4;
pub const IFF_TUN: i32 = 0x4;

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanskorg have you checked on this one? I'm not a Windows expert.

I would appreciate your input here.

Comment thread src/interface.rs
/// 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.

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after /// in the comment. Should be /// Filter out TUN interfaces... for consistency with other comments.

Copilot uses AI. Check for mistakes.
Comment thread src/interface.rs
Comment thread src/interface.rs Outdated
Unknown,
/// device is up
Up,
/// deivce down, by default you can't get device markd `down`

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "deivce" should be "device".

Copilot uses AI. Check for mistakes.
Comment thread src/interface.rs Outdated
LeoBorai and others added 7 commits December 4, 2025 01:11
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>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants