#winapi #windows #networking #ip-address #adapter #enumerate #associated

get_adapters_addresses

A wrapper around win32 GetAdaptersAddresses(). Enumerate network interfaces, and associated IP addresses.

2 releases

0.1.1 Jul 27, 2023
0.1.0 Jul 24, 2023

#122 in Windows APIs

MIT/Apache

38KB
919 lines

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

Dependencies

~0.3–12MB
~104K SLoC