Skip to main content

Crate getifs

Crate getifs 

Source
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.

github LoC Build codecov

docs.rs crates.io crates.io license

§Installation

[dependencies]
getifs = "0.6"

§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
  • Routing table - Enumerate kernel routing-table entries
  • RFC-based filtering - Filter addresses by RFC classification
  • High performance - Up to 72x 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

§Details

OSApproach
Linux (no libc)socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)
BSD-likesysctl
WindowsGetAdaptersAddresses

§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 SmallVec and SmolStr
  • Providing comprehensive interface information including MTU and multicast support
  • Achieving significantly better performance than alternatives:
    • Up to 72x faster on macOS (interface_by_index)
    • Up to 22x faster on macOS (list interfaces)
    • 2.4–2.8x faster on Linux (interface enumeration)
    • ~6.8x faster local-IP lookup on Linux
    • Comparable performance on Windows (GetAdaptersAddresses overhead dominates)

§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), measured on GitHub Actions macos-latest (ARM64) / ubuntu-latest (x64) / windows-latest (x64) runners on 2026-05-06.

PlatformBest OperationgetifsAlternativeSpeedup
macOS (ARM64)Get interface by index2.6 μs187.4 μs72x faster
macOS (ARM64)List all interfaces8.5 μs187.7 μs22x faster
Linux (x64)Local IPv4 lookup13.7 μs93.3 μs6.8x faster
Linux (x64)List all interfaces42.7 μs113.7 μs2.7x faster
Windows (x64)Gateway IPv435.5 μsN/AUnique feature

§Detailed Results

§Interface Operations

macOS (GitHub Actions ARM64)

OperationgetifsAlternativeSpeedup
List all interfaces8.5 μs187.7 μs (network-interface)22x faster
Get interface by index2.6 μs187.4 μs (network-interface)72x faster
Get interface by name11.4 μs187.8 μs (network-interface)17x faster
Get interface addresses8.5 μs--
Get multicast addresses4.6 μs--

Linux (GitHub Actions x64)

OperationgetifsAlternativeSpeedup
List all interfaces42.7 μs113.7 μs (network-interface)2.7x faster
Get interface by index40.7 μs113.4 μs (network-interface)2.8x faster
Get interface by name46.4 μs113.5 μs (network-interface)2.4x faster
Get interface addresses17.2 μs--
Get multicast addresses31.6 μs--

Windows (GitHub Actions x64)

OperationgetifsAlternativeNotes
List all interfaces1119 μs1108 μs (network-interface)Within noise
Get interface by index1101 μs1110 μs (network-interface)Within noise
Get interface by name1167 μs1109 μs (network-interface)Within noise
Get interface addresses1094 μs--
Get multicast addresses1104 μ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 (GitHub Actions ARM64)

OperationgetifsAlternativeSpeedup
Get local IPv4 address6.2 μs9.9 μs (local-ip-address)1.6x faster
Get local IPv6 address7.8 μs9.5 μs (local-ip-address)1.2x faster

Linux (GitHub Actions x64)

OperationgetifsAlternativeSpeedup
Get local IPv4 address13.7 μs93.3 μs (local-ip-address)6.8x faster
Get local IPv6 address11.3 μs-No IPv6 result from alternative

Windows (GitHub Actions x64)

OperationgetifsAlternativeNotes
Get local IPv4 address1110 μs1062 μs (local-ip-address)Win32 ~1 ms floor
Get local IPv6 address1104 μs1120 μs (local-ip-address)Win32 ~1 ms floor
§Gateway Operations
PlatformIPv4 GatewaysIPv6 GatewaysAll Gateways
macOS (ARM64, CI)24.3 μs3.4 μs26.8 μs
Linux (x64, CI)19.9 μs16.1 μs24.1 μs
Windows (x64, CI)35.5 μs21.8 μs58.4 μs

Note: No direct alternatives available for gateway discovery, so these are reported as absolute times rather than as speedups.

§Route Table Operations
PlatformIPv4 routesIPv6 routesAll routesDefault-only filter
macOS (ARM64, CI)28.1 μs12.2 μs41.0 μs40.8 μs
Linux (x64, CI)30.1 μs26.6 μs34.8 μs34.6 μs
Windows (x64, CI)65.1 μs22.5 μs81.7 μs80.8 μs

Note: The default_only filter result is essentially identical to the unfiltered route_table because the closure runs after each row is built — the dominant cost is the kernel dump itself, not the per-row filter. No direct alternatives expose a routing-table API for comparison.

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 SmallVec and SmolStr to avoid heap allocations for common cases

Platform Performance Notes:

  • macOS: Shows the largest speedups (17–72x on the ARM64 CI runners) due to the efficient sysctl-based implementation avoiding getifaddrs’s per-call overhead.
  • Linux: 2.4–2.8x faster interface enumeration via direct netlink, and ~6.8x faster local-IP lookup from avoiding the test-socket round trip that local-ip-address performs.
  • Windows: Similar performance to alternatives — GetAdaptersAddresses has an inherent ~1 ms floor that dominates every implementation. Gateway and route-table queries skip that path entirely (~36 μs gateway / ~80 μs full table); getifs only — the alternatives don’t expose them.

§Sister crates

  • iprobe: Probe if the host system supports IPv4, IPv6 and IPv4-mapped-IPv6.
  • iprfc: Known RFCs for IP addresses.

§Pedigree

§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§

probe
IP protocol probing
rfc
Known RFCs for IP addresses

Structs§

Flagslinux_like
Flags represents the interface flags.
Ifv4Addr
An interface IPv4 address.
Ifv4Net
An interface IPv4 network.
Ifv6Addr
An interface IPv6 address.
Ifv6Net
An interface IPv6 network.
Interface
The interface struct
Ipv4Route
An IPv4 entry from the kernel routing table.
Ipv6Route
An IPv6 entry from the kernel routing table.
MacAddr
Represents a physical hardware address (MAC address).
SmolStr
A SmolStr is a string type that has the following properties:

Enums§

IfAddr
An interface address.
IfNet
An interface network.
IpRoute
An entry from the kernel routing table.

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(s) 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(s) 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_addrsApple or FreeBSD or DragonFly BSD or Linux or Windows
Returns a list of the system’s multicast interface addrs.
interface_multicast_addrs_by_filterApple or FreeBSD or DragonFly BSD 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_addrsApple or FreeBSD or DragonFly BSD or Linux or Windows
Returns a list of the system’s multicast, IPv4 interface addrs.
interface_multicast_ipv4_addrs_by_filterApple or FreeBSD or DragonFly BSD 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_addrsApple or FreeBSD or DragonFly BSD or Linux or Windows
Returns a list of the system’s multicast, IPv6 interface addrs.
interface_multicast_ipv6_addrs_by_filterApple or FreeBSD or DragonFly BSD 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 non-loopback IP addresses (both IPv4 and IPv6) configured on every interface on the system.
local_addrs_by_filter
Returns all non-loopback IP addresses (both IPv4 and IPv6) configured on every interface on the system, further refined by the provided filter.
local_ipv4_addrs
Returns all non-loopback IPv4 addresses configured on every interface on the system.
local_ipv4_addrs_by_filter
Returns all non-loopback IPv4 addresses configured on every interface on the system, further refined by the provided filter.
local_ipv6_addrs
Returns all non-loopback IPv6 addresses configured on every interface on the system.
local_ipv6_addrs_by_filter
Returns all non-loopback IPv6 addresses configured on every interface on the system, further refined by the provided filter.
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).
route_ipv4_table
Returns the IPv4 unicast/local entries from the kernel routing table. See route_table for the exact set of route kinds included and the platform notes.
route_ipv4_table_by_filter
Returns IPv4 routing-table entries that match the given filter.
route_ipv6_table
Returns the IPv6 unicast/local entries from the kernel routing table. See route_table for the exact set of route kinds included and the platform notes.
route_ipv6_table_by_filter
Returns IPv6 routing-table entries that match the given filter.
route_table
Returns the unicast and local entries from the kernel routing table (both IPv4 and IPv6). Other route classes are intentionally excluded — the IpRoute type only models a single (destination, gateway, index) tuple, so route kinds without a usable next-hop interface are filtered out at the platform layer:
route_table_by_filter
Returns routing-table entries that match the given filter. Only the unicast/local route classes (see route_table) are visible to the filter; non-unicast kinds are excluded at the platform layer before f runs.

Type Aliases§

ParseMacAddrError
Represents an error that occurred while parsing MacAddr.