fork of https://github.com/shellrow/default-net/tree/v0.16.2 fixes an unsafe bug in sockaddr_to_network_addr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shellrow 7ad6493c88 Refactoring for unix-like OS 3 years ago
examples Refactoring for unix-like OS 3 years ago
src Refactoring for unix-like OS 3 years ago
.gitignore Impl interface for windows 3 years ago
Cargo.toml Refactoring for unix-like OS 3 years ago
LICENSE Initial commit 4 years ago
README.md Update README.md 3 years ago

README.md

default-net Crates.io License

Get default network information
default-net provides a cross-platform API for network interface and gateway.

Supported platform

  • Linux
  • macOS
  • Windows

Usage

Add default-net to your dependencies

[dependencies]
default-net = "0.6.0"

Example

use default_net;

fn main(){
    match default_net::get_default_interface() {
        Ok(default_interface) => {
            println!("Default Interface");
            println!("\tIndex: {}", default_interface.index);
            println!("\tName: {}", default_interface.name);
            println!("\tDescription: {:?}", default_interface.description);
            if let Some(mac_addr) = default_interface.mac_addr {
                println!("\tMAC: {}", mac_addr);
            }else{
                println!("\tMAC: (Failed to get mac address)");
            }
            println!("\tIPv4: {:?}", default_interface.ipv4);
            println!("\tIPv6: {:?}", default_interface.ipv6);
            if let Some(gateway) = default_interface.gateway {
                println!("Default Gateway");
                println!("\tMAC: {}", gateway.mac_addr);
                println!("\tIP: {}", gateway.ip_addr);
            }else {
                println!("Default Gateway: (Not found)");
            }
        },
        Err(e) => {
            println!("{}", e);
        },
    }
}

Tested on

  • Linux
    • Ubuntu 20.04, 18.04
    • Kali 2021.1 (VM)
  • macOS 11.6
  • Windows 10 20H2

For more details, see examples or doc.

v0.6.0

Remove libpnet from Windows build dependencies and changed to windows-rs only.

For Windows users using v0.5.0 or lower

To build libpnet on Windows, follow the instructions below.

Windows

  • You must use a version of Rust which uses the MSVC toolchain
  • You must have WinPcap or npcap installed (tested with version WinPcap 4.1.3) (If using npcap, make sure to install with the "Install Npcap in WinPcap API-compatible Mode")
  • You must place Packet.lib from the WinPcap Developers pack in a directory named lib, in the root of this repository. Alternatively, you can use any of the locations listed in the %LIB%/$Env:LIB environment variables. For the 64 bit toolchain it is in WpdPack/Lib/x64/Packet.lib, for the 32 bit toolchain, it is in WpdPack/Lib/Packet.lib.

Source