| Crates.io | getifaddrs |
| lib.rs | getifaddrs |
| version | 0.5.0 |
| created_at | 2024-09-04 22:11:19.425867+00 |
| updated_at | 2025-08-19 03:17:18.500332+00 |
| description | A cross-platform library for retrieving network interface addresses and indices (getifaddrs, if_nametoindex, if_indextoname). |
| homepage | |
| repository | https://github.com/mmastrac/getifaddrs |
| max_upload_size | |
| id | 1363881 |
| size | 80,281 |
A cross-platform library for retrieving network interface information.
This crate provides a simple and consistent API for querying network interface details across different operating systems. It supports Unix-like systems (Linux, macOS, *BSD) and Windows.
if_indextoname
and
if_nametoindexThis project is licensed under the MIT or APACHE license.
Add this to your Cargo.toml:
[dependencies]
getifaddrs = "0.4"
Iterate over all network interfaces:
use getifaddrs::{getifaddrs, InterfaceFlags};
fn main() -> std::io::Result<()> {
for interface in getifaddrs()? {
println!("Interface: {}", interface.name);
if let Some(ip_addr) = interface.address.ip_addr() {
println!(" IP Address: {}", ip_addr);
}
if let Some(mac_addr) = interface.address.mac_addr() {
println!(" MAC Address: {:?}", mac_addr);
}
if let Some(netmask) = interface.address.netmask() {
println!(" Netmask: {}", netmask);
}
if let Some(associated_address) = interface.address.associated_address() {
println!(" Associated Address: {}", associated_address);
}
println!(" Flags: {:?}", interface.flags);
if interface.flags.contains(InterfaceFlags::UP) {
println!(" Status: Up");
} else {
println!(" Status: Down");
}
println!();
}
Ok(())
}
Collect all network interfaces and print the associated items in the rough style
of the ifconfig command:
use getifaddrs::{getifaddrs, Address, Interfaces};
let interfaces = getifaddrs().unwrap().collect::<Interfaces>();
for (index, interface) in interfaces {
println!("{}", interface.name);
println!(" Flags: {:?}", interface.flags);
for address in interface.address.iter().flatten() {
match address {
Address::V4(..) | Address::V6(..) => {
println!(" IP{:?}: {:?}", address.family(), address.ip_addr().unwrap());
if let Some(netmask) = address.netmask() {
println!(" Netmask: {}", netmask);
}
#[cfg(not(windows))]
if let Some(associated_address) = address.associated_address() {
println!(" Associated: {}", associated_address);
}
}
Address::Mac(addr) => {
println!(
" Ether: {}",
addr.iter().map(|b| format!("{:02x}", b)).collect::<Vec<_>>().join(":")
);
}
}
}
println!(" Index: {}", index);
println!();
}