local-ip-address
Retrieve the system's local IP address and network interfaces across Linux, macOS, Windows, and BSD
Repository Health
Technical Analysis
local-ip-address is a small, dependency-light Rust crate that answers a surprisingly platform-fragmented question: “what is this machine’s local IP address?” It exposes two core functions — local_ip() for the primary local IPv4 address and list_afinet_netifas() for enumerating all AF_INET/AF_INET6 network interfaces — with a consistent API regardless of whether the code runs on Linux, macOS, Windows, or a BSD variant.
Each platform uses its own native mechanism under the hood: Linux goes through netlink sockets via the neli crate, Unix/BSD/macOS use libc’s getifaddrs, and Windows uses the windows-sys crate’s IP Helper API bindings. This keeps the public API identical across platforms while letting each backend use the fastest, most correct OS-native approach rather than a lowest-common-denominator implementation.
What You Get
local_ip()returning the machine’s primary local IPv4 address as a typedIpAddrlist_afinet_netifas()enumerating all network interfaces with their AF_INET/AF_INET6 addresses- Platform-native backends: netlink sockets on Linux (via
neli),getifaddrson Unix/macOS (vialibc), and IP Helper API on Windows (viawindows-sys) - A small, typed
Errorenum (src/error.rs) instead of stringly-typed errors for interface lookup failures - Zero required dependencies beyond the platform-specific backend crate actually needed for the target OS
Common Use Cases
- Displaying a device’s local network address in a CLI tool or desktop application’s diagnostics screen
- Auto-discovering the local IP to bind a server or advertise a service on a LAN without hardcoding an interface name
- Building cross-platform network diagnostic or monitoring utilities that need consistent interface enumeration
- Filtering available network interfaces (e.g. skipping loopback or VPN-only interfaces) before establishing a connection
Under The Hood
Architecture The crate follows a clean per-platform-backend pattern: src/lib.rs defines the public local_ip()/list_afinet_netifas() functions and re-exports the appropriate platform module — src/linux.rs (netlink-based), src/unix.rs (libc getifaddrs-based, covering macOS/BSD), and src/windows.rs (IP Helper API-based) — selected via cfg target attributes, so the public API surface stays identical while internals diverge entirely per OS.
Tech Stack Rust 2021 edition with zero unconditional dependencies: libc is pulled in only for cfg(unix) targets, neli (a pure-Rust netlink socket library) only for cfg(target_os = "linux"), and windows-sys (with a narrow set of IP Helper/WinSock features) only for cfg(windows) — keeping non-target platform dependencies out of the build entirely.
Code Quality The crate has no dedicated tests/ directory; correctness for this kind of platform-syscall code is largely exercised via the example binary (examples/show_ip_and_ifs.rs) and CI build/clippy/fmt checks across target platforms, which is a real gap for a crate whose only value proposition is platform-specific correctness. Linux’s netlink implementation (src/linux.rs, 590 lines) is by far the most involved of the three backends, reflecting netlink’s inherent complexity versus the simpler libc/WinAPI calls used elsewhere.
API Design The two-function public API (local_ip(), list_afinet_netifas()) is about as low-friction as a cross-platform networking crate can be — no configuration, no builder pattern, just a function call returning a typed result. The cost of this simplicity is limited flexibility: there’s no way to filter interfaces or select a non-default one without manually inspecting the full list returned by list_afinet_netifas().
Used by 2 apps in this directory
Arroyo
Data Engineering · Analytics
A distributed stream processing engine written in Rust that lets you write SQL to run stateful, real-time computations over data streams with subsecond results.
Spacedrive
File Storage · Collaboration
One file manager for all your devices and clouds — powered by a Virtual Distributed File System built in Rust.