Crate getifaddrs

Source
Expand description

§getifaddrs

Documentation Crates.io Rust

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.

§Features

  • Retrieve network interface information (name, IP address, netmask, flags, mac address,etc.)
  • Filter interfaces based on various criteria (loopback, IPv4/IPv6, name, index)
  • Cross-platform support (Unix-like systems and Windows)
  • Provides a cross-platform implementation of if_indextoname and if_nametoindex

§License

This project is licensed under the MIT or APACHE license.

§Usage

Add this to your Cargo.toml:

[dependencies]
getifaddrs = "0.4"

§Example

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!();
}

Structs§

Addresses
A collection of addresses, keyed by AddressFamily.
AddressesIter
An iterator over the addresses in a Addresses collection.
Interface
Represents a network interface.
InterfaceFilter
A filter for network interfaces.
InterfaceFlags
Flags representing the status and capabilities of a network interface.
NetworkAddress
Represents a network address of a given type.

Enums§

Address
Represents a network address of a given type.
AddressFamily
Represents a network address family.

Functions§

getifaddrs
Returns an iterator for all network interfaces on the system.
if_indextoname
Converts a network interface index to its corresponding name.
if_nametoindex
Converts a network interface name to its corresponding index.

Type Aliases§

InterfaceIndex
This represents the index of a network interface.
Interfaces
A map of interface index to interface addresses. Note that this assumes each interface has at most one address of each family.