get_adapters_addresses

Crates.ioget_adapters_addresses
lib.rsget_adapters_addresses
version0.1.1
sourcesrc
created_at2023-07-24 12:38:19.891641
updated_at2023-07-27 13:44:16.177562
descriptionA wrapper around win32 GetAdaptersAddresses(). Enumerate network interfaces, and associated IP addresses.
homepagehttps://gitlab.com/cratesio/get_adapters_addresses
repositoryhttps://gitlab.com/cratesio/get_adapters_addresses
max_upload_size
id924537
size37,795
(43687269732053746F6E6573)

documentation

README

What !?

This crate provides a rust interface to the win32 function GetAdaptersAddresses.

Why !?

There is a windows crate. But the function GetAdaptersAddresses is not fun to use.

  • The user is expected to manage memory for the output with HeapAlloc/HeapFree.
  • The user is expected to traverse the linked-list of adapters and addresses.
  • The user is expected to cast the *SOCKADDR to *SOCKADDR_IN / *SOCKADDR_IN6 to interpret it.
  • its all unsafe.

How does this work then!?

The struct AdaptersAddresses allocates and owns the buffer that GetAdaptersAddresses() wrote to.
Iterating over this produces Adapter's.
Adapters are queries for their various attributes, often iterable.

Example !

use get_adapters_addresses::*;
fn main() -> Result<()> {
    let adapter_addresses =
        AdaptersAddresses::try_new(Family::Unspec, *Flags::default().include_gateways())?;

    for adapter in &adapter_addresses {
        println!("============================================================");
        println!("adapter name: {:?}", adapter.adapter_name());
        println!("friendly name: {:?}", adapter.friendly_name());
        println!("description: {:?}", adapter.description());
        println!("interface_type {:?}", adapter.interface_type());
        if let Some(physical_address) = adapter.physical_address() {
            println!(
                "physical_address: {} {{{:X}}}",
                physical_address,
                physical_address.as_u64()
            );
        }
        println!("status: {:?}", adapter.operational_status());
        for addr in adapter.unicast_addresses() {
            println!("\tunicast: {:?}", addr);
        }
        for addr in adapter.multicast_addresses() {
            println!("\tmulticast: {:?}", addr);
        }
        for addr in adapter.anycast_addresses() {
            println!("\tanycast: {:?}", addr);
        }
        for addr in adapter.dns_addresses() {
            println!("\tdns: {:?}", addr);
        }
        for addr in adapter.gateway_addresses() {
            println!("\tgateway: {:?}", addr);
        }
        println!("\tdhcpv4: {:?}", adapter.dhcpv4_address());
        println!("\tdhcpv6: {:?}", adapter.dhcpv6_address());
    }
    Ok(())
}
Commit count: 4

cargo fmt