Expand description
GetIfs
Cross-platform enumeration of network interfaces and their MTU, gateway, multicast, and local/private/public IP addresses — with a libc-free netlink backend on Linux and no getifaddrs dependency.
§Installation
[dependencies]
getifs = "0.5"§Features
- Zero libc dependency on Linux (uses netlink directly)
- MTU information - Get interface MTU values
- Multicast addresses - Fetch multicast group memberships
- Gateway discovery - Find IPv4 and IPv6 gateway addresses
- RFC-based filtering - Filter addresses by RFC classification
- High performance - Up to 189x faster than alternatives on macOS (see benchmarks)
- Cross-platform - Linux, macOS, BSD, Windows support
§Quick Start
use getifs::{interfaces, local_addrs, gateway_addrs};
// Get all network interfaces
let interfaces = interfaces().unwrap();
for interface in interfaces {
println!("Interface: {} (index: {})", interface.name(), interface.index());
println!(" MTU: {}", interface.mtu());
println!(" Flags: {:?}", interface.flags());
}
// Get local IP addresses
let local_ips = local_addrs().unwrap();
for ip in local_ips {
println!("Local IP: {}", ip);
}
// Get gateway addresses
let gateways = gateway_addrs().unwrap();
for gateway in gateways {
println!("Gateway: {}", gateway);
}§Examples
- Fetching all interfaces: examples/interfaces.rs
- Fetching all interface addresses (excluding multicast addrs): examples/addrs.rs
- Fetching all interface multicast addresses: examples/multicast_addrs.rs
- Fetching gateway addresses: examples/gateway.rs
- Fetching local ip addresses: examples/local_ip_addrs.rs
- Fetching ip addresses by RFC: examples/filter_by_rfc.rs
§Details
| OS | Approach |
|---|---|
Linux (no libc) | socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE) |
| BSD-like | sysctl |
| Windows | GetAdaptersAddresses |
§Why getifs?
Existing network interface crates have limitations:
- Missing features: Most don’t support MTU or multicast addresses
- Performance overhead: Nearly all use
libc::getifaddrs, which is slower - Unnecessary allocations: Heavy use of heap allocations for simple queries
getifs addresses these by:
- Using platform-native APIs directly (netlink, sysctl, GetAdaptersAddresses)
- Minimizing allocations with
SmallVecandSmolStr - Providing comprehensive interface information including MTU and multicast support
- Achieving significantly better performance than alternatives:
- Up to 189x faster on macOS (
interface_by_index) - Up to 37x faster on macOS (list interfaces)
- 2.4–2.8x faster on Linux (interface enumeration)
- ~7x faster local-IP lookup on Linux
- Comparable performance on Windows (
GetAdaptersAddressesoverhead dominates)
- Up to 189x faster on macOS (
§Roadmap
- Support fetching routing tables
§Benchmarks
All benchmarks are run with Criterion.rs on GitHub Actions. Lower is better.
Note: Automated benchmarks run on Linux, macOS, and Windows via GitHub Actions. View the latest results in the Actions tab or see benchmark documentation for more details.
§Performance Summary
Numbers below compare getifs against network-interface 2 and
local-ip-address 0.6. Each row reports median cargo bench time
(Criterion, 100 samples).
- macOS (Apple M4 Pro) — measured on a bare-metal laptop, not CI. The GitHub Actions macOS runners are shared virtual machines with highly variable latency (run-to-run wobble of ±30% on these ops), which obscures the real cost difference.
- Linux (x64) / Windows (x64) — measured on GitHub Actions
ubuntu-latest/windows-latestrunners on 2026-04-15 03:34 UTC.
| Platform | Best Operation | getifs | Alternative | Speedup |
|---|---|---|---|---|
| macOS (Apple M4 Pro) | Get interface by index | 1.7 μs | 314.2 μs | 189x faster |
| macOS (Apple M4 Pro) | List all interfaces | 8.4 μs | 313.0 μs | 37x faster |
| Linux (x64) | Local IPv4 lookup | 13.7 μs | 94.3 μs | 6.9x faster |
| Linux (x64) | List all interfaces | 41.5 μs | 111.7 μs | 2.7x faster |
| Windows (x64) | Gateway IPv4 | 30.2 μs | N/A | Unique feature |
§Detailed Results
§Interface Operations
macOS (Apple M4 Pro, 24 GB, local bare-metal)
| Operation | getifs | Alternative | Speedup |
|---|---|---|---|
| List all interfaces | 8.4 μs | 313.0 μs (network-interface) | 37x faster |
| Get interface by index | 1.7 μs | 314.2 μs (network-interface) | 189x faster |
| Get interface by name | 10.4 μs | 314.1 μs (network-interface) | 30x faster |
| Get interface addresses | 8.4 μs | - | - |
| Get multicast addresses | 2.9 μs | - | - |
Linux (GitHub Actions x64)
| Operation | getifs | Alternative | Speedup |
|---|---|---|---|
| List all interfaces | 41.5 μs | 111.7 μs (network-interface) | 2.7x faster |
| Get interface by index | 40.3 μs | 111.9 μs (network-interface) | 2.8x faster |
| Get interface by name | 46.1 μs | 111.9 μs (network-interface) | 2.4x faster |
| Get interface addresses | 17.3 μs | - | - |
| Get multicast addresses | 31.4 μs | - | - |
Windows (GitHub Actions x64)
| Operation | getifs | Alternative | Notes |
|---|---|---|---|
| List all interfaces | 974 μs | 967 μs (network-interface) | Within noise |
| Get interface by index | 954 μs | 976 μs (network-interface) | Within noise |
| Get interface by name | 1011 μs | 969 μs (network-interface) | Within noise |
| Get interface addresses | 958 μs | - | - |
| Get multicast addresses | 966 μs | - | - |
Note: the Win32 GetAdaptersAddresses API has an inherent ~1 ms floor
that dominates every implementation — getifs and network-interface
end up within measurement noise of each other on Windows.
§Local IP Address Operations
macOS (Apple M4 Pro, 24 GB, local bare-metal)
| Operation | getifs | Alternative | Speedup |
|---|---|---|---|
| Get local IPv4 address | 6.5 μs | 9.8 μs (local-ip-address) | 1.5x faster |
| Get local IPv6 address | 7.9 μs | 9.8 μs (local-ip-address) | 1.2x faster |
Linux (GitHub Actions x64)
| Operation | getifs | Alternative | Speedup |
|---|---|---|---|
| Get local IPv4 address | 13.7 μs | 94.3 μs (local-ip-address) | 6.9x faster |
| Get local IPv6 address | 11.2 μs | - | No IPv6 result from alternative |
Windows (GitHub Actions x64)
| Operation | getifs | Alternative | Notes |
|---|---|---|---|
| Get local IPv4 address | 963 μs | 919 μs (local-ip-address) | Win32 ~1 ms floor |
| Get local IPv6 address | 992 μs | 972 μs (local-ip-address) | Win32 ~1 ms floor |
§Gateway Operations
| Platform | IPv4 Gateways | IPv6 Gateways | All Gateways |
|---|---|---|---|
| macOS (M4 Pro, local) | 17.5 μs | 2.4 μs | 19.8 μs |
| Linux (x64, CI) | 18.4 μs | 14.3 μs | 22.4 μs |
| Windows (x64, CI) | 30.2 μs | 18.0 μs | 48.5 μs |
Note: No direct alternatives available for gateway discovery, so these are reported as absolute times rather than as speedups.
Why is getifs faster?
- Direct system calls: Uses platform-native APIs (netlink on Linux, sysctl on BSD/macOS, GetAdaptersAddresses on Windows)
- Zero-copy parsing: Minimal allocations and efficient buffer reuse
- No libc dependency on Linux: Direct netlink socket communication
- Optimized data structures: Uses
SmallVecandSmolStrto avoid heap allocations for common cases
Platform Performance Notes:
- macOS: Shows the largest speedups (30–189x on bare metal) due to
the efficient sysctl-based implementation avoiding
getifaddrs’s per-call overhead. On shared CI runners the absolute numbers are noisier but the ordering holds. - Linux: 2.4–2.8x faster interface enumeration via direct netlink,
and ~7x faster local-IP lookup from avoiding the test-socket round
trip that
local-ip-addressperforms. - Windows: Similar performance to alternatives —
GetAdaptersAddresseshas an inherent ~1 ms floor that dominates every implementation.
§Sister crates
iprobe: Probe if the host system supports IPv4, IPv6 and IPv4-mapped-IPv6.iprfc: Known RFCs for IP addresses.
§Pedigree
- The code in this crate is inspired by Golang’s
interface.goand HashiCorp’s go-sockaddr.
§License
getifs is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2026 Al Liu.
Re-exports§
pub use ipnet;
Modules§
Structs§
- Flags
- Flags represents the interface flags.
- Ifv4
Addr - An interface IPv4 address.
- Ifv4Net
- An interface IPv4 network.
- Ifv6
Addr - An interface IPv6 address.
- Ifv6Net
- An interface IPv6 network.
- Interface
- The interface struct
- MacAddr
- Represents a physical hardware address (MAC address).
- SmolStr
- A
SmolStris a string type that has the following properties:
Enums§
Functions§
- best_
local_ addrs - Returns both IPv4 and IPv6 addresses from the interfaces with the best default routes. The “best” interfaces are determined by the routing metrics of default routes.
- best_
local_ ipv4_ addrs - Returns the IPv4 addresses from the interface with the best default route.
The “best” interface is determined by the routing metrics of default routes (
0.0.0.0). - best_
local_ ipv6_ addrs - Returns the IPv6 addresses from the interface with the best default route.
The “best” interface is determined by the routing metrics of default routes (
::). - gateway_
addrs - Returns all gateway IP addresses (both IPv4 and IPv6) configured on the system. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- gateway_
addrs_ by_ filter - Returns all gateway IP addresses (both IPv4 and IPv6) configured on the system that match the given filter. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- gateway_
ipv4_ addrs - Returns all IPv4 gateway addresses configured on the system. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- gateway_
ipv4_ addrs_ by_ filter - Returns all IPv4 gateway addresses configured on the system that match the given filter. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- gateway_
ipv6_ addrs - Returns all IPv6 gateway addresses configured on the system. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- gateway_
ipv6_ addrs_ by_ filter - Returns all IPv6 gateway addresses configured on the system that match the given filter. Only returns addresses from interfaces that have valid routes and excludes any addresses that are not configured as gateways.
- get_
ip_ mtu - Get the MTU of the given
IpAddr. - get_
ipv4_ mtu - Get the MTU of the given
Ipv4Addr. - get_
ipv6_ mtu - Get the MTU of the given
Ipv6Addr. - ifindex_
to_ name - Returns the name of the interface by the given index.
- ifname_
to_ iface - Returns the IPv6 interface of by the given name.
- ifname_
to_ index - Returns the index of the interface by the given name.
- ifname_
to_ v4_ iface - Returns the IPv4 interface of by the given name.
- ifname_
to_ v6_ iface - Returns the IPv6 interface of by the given name.
- interface_
addrs - Returns a list of the system’s unicast interface addrs.
- interface_
addrs_ by_ filter - Returns a list of the system’s unicast interface addrs.
- interface_
by_ index - Returns the interface specified by index.
- interface_
by_ name - Returns the interface specified by name.
- interface_
ipv4_ addrs - Returns a list of the system’s unicast, IPv4 interface addrs.
- interface_
ipv4_ addrs_ by_ filter - Returns a list of the system’s unicast, IPv4 interface addrs.
- interface_
ipv6_ addrs - Returns a list of the system’s unicast, IPv6 interface addrs.
- interface_
ipv6_ addrs_ by_ filter - Returns a list of the system’s unicast, IPv6 interface addrs.
- interface_
multicast_ addrs Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast interface addrs.
- interface_
multicast_ addrs_ by_ filter Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast interface addrs. The filter is used to determine which multicast addresses to include.
- interface_
multicast_ ipv4_ addrs Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast, IPv4 interface addrs.
- interface_
multicast_ ipv4_ addrs_ by_ filter Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast, IPv4 interface addrs. The filter is used to determine which multicast addresses to include.
- interface_
multicast_ ipv6_ addrs Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast, IPv6 interface addrs.
- interface_
multicast_ ipv6_ addrs_ by_ filter Apple or FreeBSD or Linux or Windows - Returns a list of the system’s multicast, IPv6 interface addrs. The filter is used to determine which multicast addresses to include.
- interfaces
- Returns a list of the system’s network interfaces.
- local_
addrs - Returns all IP addresses (both IPv4 and IPv6) from interfaces that have valid routes (excluding loopback). This ensures we only return addresses that can be used for communication.
- local_
addrs_ by_ filter - Returns all IP addresses (both IPv4 and IPv6) from interfaces that have valid routes.
- local_
ipv4_ addrs - Returns all IPv4 addresses from interfaces that have valid routes (excluding loopback). This ensures we only return addresses that can be used for communication.
- local_
ipv4_ addrs_ by_ filter - Returns all IPv4 addresses from interfaces that have valid routes.
- local_
ipv6_ addrs - Returns all IPv6 addresses from interfaces that have valid routes (excluding loopback). This ensures we only return addresses that can be used for communication.
- local_
ipv6_ addrs_ by_ filter - Returns all IPv6 addresses from interfaces that have valid routes.
- private_
addrs - Returns all IP addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_addrs). - private_
addrs_ by_ filter - Returns all IP addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_addrs_by_filter). - private_
ipv4_ addrs - Returns all IPv4 addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_ipv4_addrs). - private_
ipv4_ addrs_ by_ filter - Returns all IPv4 addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_ipv4_addrs_by_filter). - private_
ipv6_ addrs - Returns all IPv6 addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_ipv6_addrs). - private_
ipv6_ addrs_ by_ filter - Returns all IPv6 addresses that are part of RFC
6890 (regardless of whether or not there is a default route, unlike
public_ipv6_addrs_by_filter). - public_
addrs - Returns all IP addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_addrs). - public_
addrs_ by_ filter - Returns all IP addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_addrs_by_filter). - public_
ipv4_ addrs - Returns all IPv4 addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_ipv4_addrs). - public_
ipv4_ addrs_ by_ filter - Returns all IP addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_ipv4_addrs_by_filter). - public_
ipv6_ addrs - Returns all IPv6 addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_ipv6_addrs). - public_
ipv6_ addrs_ by_ filter - Returns all IPv6 addresses that are NOT part of RFC
6890 (regardless of whether or not there is a default route, unlike
private_ipv6_addrs_by_filter).
Type Aliases§
- Parse
MacAddr Error - Represents an error that occurred while parsing
MacAddr.