#network-interface #cross-platform #ifaddrs #networking #interface

getifaddrs

A cross-platform library for retrieving network interface information

6 releases

0.1.5 Nov 14, 2024
0.1.4 Sep 21, 2024

#488 in Network programming

Download history 1640/week @ 2024-12-03 1163/week @ 2024-12-10 1515/week @ 2024-12-17 547/week @ 2024-12-24 857/week @ 2024-12-31 1494/week @ 2025-01-07 643/week @ 2025-01-14 668/week @ 2025-01-21 608/week @ 2025-01-28 1175/week @ 2025-02-04 1183/week @ 2025-02-11 1728/week @ 2025-02-18 1275/week @ 2025-02-25 1340/week @ 2025-03-04 1630/week @ 2025-03-11 724/week @ 2025-03-18

5,394 downloads per month
Used in 5 crates (via tun-rs)

MIT/Apache

39KB
764 lines

getifaddrs

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

Usage

Add this to your Cargo.toml:

[dependencies]
getifaddrs = "0.1"

Example

use getifaddrs::{getifaddrs, InterfaceFlags};

fn main() -> std::io::Result<()> {
    for interface in getifaddrs()? {
        println!("Interface: {}", interface.name);
        println!("  Address: {}", interface.address);
        if let Some(netmask) = interface.netmask {
            println!("  Netmask: {}", netmask);
        }
        println!("  Flags: {:?}", interface.flags);
        if interface.flags.contains(InterfaceFlags::UP) {
            println!("  Status: Up");
        } else {
            println!("  Status: Down");
        }
        println!();
    }
    Ok(())
}

License

This project is licensed under the MIT License.

Dependencies

~0–8MB
~55K SLoC