1 unstable release

Uses old Rust 2015

0.1.0 Sep 8, 2017

#33 in #safe-network

Download history 68/week @ 2024-03-14 65/week @ 2024-03-21 13/week @ 2024-03-28 30/week @ 2024-04-04 25/week @ 2024-04-11 72/week @ 2024-04-18 26/week @ 2024-04-25 2/week @ 2024-05-02 41/week @ 2024-05-09 1/week @ 2024-05-16 5/week @ 2024-05-30 39/week @ 2024-06-06 7/week @ 2024-06-13 44/week @ 2024-06-20 19/week @ 2024-06-27

110 downloads per month

MIT license

16KB
159 lines

getaddrs provides a safe interface for the system's network interface data.

The InterfaceAddrs struct provides access to the system's network interface data. You can either iterate over it or consume it and convert it into an InterfaceMap (a HashMap<String, Vec<InterfaceAddr>>) for more convenient access by interface name.

Examples

You can access the basic information of the system in a few lines. The following program prints all the known addresses.

use getaddrs::InterfaceAddrs;

let addrs = InterfaceAddrs::query_system()
    .expect("System has no network interfaces.");

for addr in addrs {
    println!("{}: {:?}", addr.name, addr.address);
}

The InterfaceFlags struct provides access to info about the state of an interface. This program prints the addresses of only interfaces which are up.

use getaddrs::{InterfaceAddrs, if_flags};

let addrs = InterfaceAddrs::query_system()
    .expect("System has no network interfaces.");

for addr in addrs {
    if addr.flags.contains(if_flags::IFF_UP) {
        println!("{}: {:?}", addr.name, addr.address);
    }
}

You can convert the InterfaceAddrs struct into a HashMap easily. InterfaceMap is an alias for HashMap<String, Vec> for easier reference.

use getaddrs::{InterfaceAddrs, InterfaceAddr, InterfaceMap};
use std::collections::HashMap;

let interfaces: InterfaceMap = 
    InterfaceAddrs::query_system()
    .expect("System has no network interfaces.")
    .into(); // Convert to a hash map

// Print all the addresses of the loopback interface
if let Some(addrs) = interfaces.get("lo") {
   println!("Loopback addresses:");
   for addr in addrs {
        println!("\t{:?}", addr);
   }
}

Dependencies

~185KB