default-net
Cross-platform Rust library for enumerating network interfaces, their addresses, and the system's default gateway.
Repository Health
Technical Analysis
default-net gives Rust programs a single, unified API for discovering local network interfaces on Linux, macOS, Windows, Android, iOS, and the BSDs, instead of hand-rolling platform-specific syscalls (netlink on Linux, IP Helper on Windows, SystemConfiguration on Apple platforms). Each Interface struct carries MAC address, IPv4/IPv6 addresses with prefixes, MTU, operational state, flags, and — when the gateway feature is enabled — the resolved default interface, default gateway, and DNS servers.
The crate was originally published as default-net and has since been rebranded to netdev by its author for continued development; the default-net name on crates.io remains as the earlier, still-installable release line built on the same underlying platform code.
What You Get
- A single
get_interfaces()call that returns every visible interface with MAC address, IPv4/IPv6 addresses, MTU, flags, and operational state populated from the OS - Default-route resolution via
get_default_interface()andget_default_gateway()gated behind thegatewayfeature, including the gateway’s MAC and IP addresses - Native traffic statistics (RX/TX byte counters) surfaced per interface where the platform exposes them
- Cross-platform coverage for Linux, macOS, Windows, Android, iOS, and the BSDs behind a single API rather than one crate per OS
- Optional
serdefeature to serialize the returnedInterfacestructs directly for logging, diagnostics tooling, or IPC
Common Use Cases
- Network diagnostics and troubleshooting CLIs that need to print a machine’s interfaces, addresses, and active gateway
- VPN and tunneling tools that must detect the current default interface before rerouting traffic
- System monitoring agents that poll interface state and traffic counters for dashboards or alerting
- Cross-platform desktop or CLI applications that need consistent network topology info without writing per-OS code paths
Under The Hood
Architecture
The crate is organized as a thin, feature-gated dispatch layer over per-OS backends: src/interface/ and src/net/ define the public, platform-neutral Interface, MacAddr, and NetworkDevice types, while src/os/ contains one module per target (linux, windows, darwin/macos/ios, android, bsd, unix) selected entirely at compile time via #[cfg(...)] attributes in src/os/mod.rs. lib.rs re-exports a small, curated public surface (get_interfaces, Interface, and the gateway/route helpers gated behind the gateway feature) so callers never touch the OS-specific modules directly. On Linux, interface discovery goes through netlink (netlink-packet-route/netlink-sys) plus /proc and /sys reads (procfs.rs, sysfs.rs); on Windows through the IP Helper API via windows-sys; on Apple platforms through SystemConfiguration and, optionally, CoreWLAN for Wi-Fi metadata. Swapping the core Interface representation would ripple through every OS backend, since each one is responsible for populating the same struct from a different native API.
Tech Stack
The crate targets Rust 2024 edition with a deliberately narrow default dependency set: ipnet for CIDR-aware address types, mac-addr for MAC address handling, and optional serde for serialization. Platform-specific dependencies are pulled in only for the relevant target: netlink-packet-core/netlink-packet-route/netlink-sys and libc on Linux/Android, windows-sys (IP Helper, WinSock, NDIS bindings) on Windows, and objc2/objc2-system-configuration/objc2-core-wlan/plist on macOS/iOS, with dlopen2/jni/ndk-context for Android’s JNI-based enrichment. A build.rs links SystemConfiguration.framework automatically on Apple targets. CI (GitHub Actions) runs cargo fmt --check, cargo build/cargo test across Ubuntu/macOS/Windows, cargo check --all-features, and cross-compilation checks for additional targets like musl and Android ABIs.
Code Quality
The crate carries unit tests alongside its modules (16 files containing #[test] blocks) rather than a separate top-level test suite, and CI runs cargo test plus cargo check --all-features on every push across all three major OS runners, giving reasonable confidence that each platform backend actually compiles and links. unsafe blocks appear in 28 files, expected given the crate talks directly to netlink sockets, Win32 APIs, and Apple system frameworks through FFI; each is scoped to the specific OS module that needs it rather than leaking into the public API. Public types are documented with doc comments explaining platform caveats (e.g., which fields are typically None on which OS), and rustfmt is enforced in CI.
What Makes It Unique
Most Rust crates for interface enumeration either wrap a single OS API (Windows-only or Linux-only) or expose raw, unsafe bindings the caller has to interpret themselves. default-net’s contribution is normalizing genuinely different OS concepts — Windows adapter GUIDs and friendly names, Linux netlink attributes, Apple SystemConfiguration dictionaries — into one Interface struct with explicit doc comments about which fields are meaningful on which platform, plus opt-in feature flags (gateway, apple-wifi-extra, android-extra) so callers only pay for the platform-specific enrichment they actually need (e.g., synchronous CoreWLAN IPC for Wi-Fi speed is off by default because of its latency cost).