Crate local_ip_address

source ·
Expand description

§Local IP Address

Retrieve system’s local IP address and Network Interfaces/Adapters on Linux, Windows, and macOS (and other BSD-based systems).

§Usage

Get the local IP address of your system by executing the local_ip function:

use local_ip_address::local_ip;

let my_local_ip = local_ip();

if let Ok(my_local_ip) = my_local_ip {
    println!("This is my local IP address: {:?}", my_local_ip);
} else {
    println!("Error getting local IP: {:?}", my_local_ip);
}

Retrieve all the available network interfaces from both, the AF_INET and the AF_INET6 family by executing the list_afinet_netifas function:

use local_ip_address::list_afinet_netifas;

let network_interfaces = list_afinet_netifas();

if let Ok(network_interfaces) = network_interfaces {
    for (name, ip) in network_interfaces.iter() {
        println!("{}:\t{:?}", name, ip);
    }
} else {
    println!("Error getting network interfaces: {:?}", network_interfaces);
}

Underlying approach on retrieving network interfaces or the local IP address may differ based on the running operative system.

OSApproach
LinuxEstablishes a Netlink socket interchange to retrieve network interfaces
BSD-based & AndroidUses of getifaddrs to retrieve network interfaces
WindowsConsumes Win32 API’s to retrieve the network adapters table

Supported BSD-based systems include:

  • macOS
  • FreeBSD
  • OpenBSD
  • NetBSD
  • DragonFly

Re-exports§

Modules§

Enums§

Functions§

  • Retrieves the local IPv4 address of the machine in the local network from the AF_INET family.
  • Retrieves the local IPv6 address of the machine in the local network from the AF_INET6 family.