Coder Social home page Coder Social logo

ifcfg-rs's Introduction

IFCFG

Crates.io Documentation Build Status

IFCFG (ifconfig) is a Rust library to get network interfaces information for Windows/Linux/Mac

Requirements

  • Rust

Include in project

[dependencies]
ifcfg = "0.1.2"

Example

use ifcfg;

fn main() -> ifcfg::Result<()> {
    let ifaces = ifcfg::IfCfg::get().expect("could not get interfaces");
    println!("{:#?}", &ifaces);
    Ok(())
}

Compile

cargo b

Tests

cargo t

Run the included example

cargo run --example interfaces
   Compiling ifcfg v0.1.0 (/root/ifcfg)
    Finished dev [optimized + debuginfo] target(s) in 0.63s
     Running `target/debug/examples/interfaces`
[
    IfCfg {
        name: "lo",
        mac: "00:00:00:00:00:00",
        addresses: [
            InterfaceAddress {
                address_family: IPv6,
                address: Some(
                    [::1]:0,
                ),
                mask: Some(
                    [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:0,
                ),
                hop: None,
            },
            InterfaceAddress {
                address_family: IPv4,
                address: Some(
                    127.0.0.1:0,
                ),
                mask: Some(
                    255.0.0.0:0,
                ),
                hop: Some(
                    Destination(
                        127.0.0.1:0,
                    ),
                ),
            },
            InterfaceAddress {
                address_family: Packet,
                address: None,
                mask: None,
                hop: None,
            },
        ],
        description: "",
    },
    IfCfg {
        name: "ens33",
        mac: "00:0c:29:0a:e0:b4",
        addresses: [
            InterfaceAddress {
                address_family: IPv6,
                address: Some(
                    [fe80::20c:29ff:fe0a:e0b4%2]:0,
                ),
                mask: Some(
                    [ffff:ffff:ffff:ffff::]:0,
                ),
                hop: None,
            },
            InterfaceAddress {
                address_family: IPv6,
                address: Some(
                    [fd15:4ba5:5a2b:1008:20c:29ff:fe0a:e0b4]:0,
                ),
                mask: Some(
                    [ffff:ffff:ffff:ffff::]:0,
                ),
                hop: None,
            },
            InterfaceAddress {
                address_family: IPv4,
                address: Some(
                    192.168.2.3:0,
                ),
                mask: Some(
                    255.255.255.0:0,
                ),
                hop: Some(
                    Broadcast(
                        192.168.2.255:0,
                    ),
                ),
            },
            InterfaceAddress {
                address_family: Packet,
                address: None,
                mask: None,
                hop: None,
            },
        ],
        description: "",
    },
]

License: MIT/Apache

ifcfg-rs's People

Contributors

marirs avatar postrequest avatar tuxu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ifcfg-rs's Issues

Request: Add IPv6 scope IDs for link-local addresses.

IPv6 link-local addresses are not necessarily globally unique. To use them, you need to also add a "scope ID" when addressing them.

I saw that ifcfg returns SocketAddrs, which contain a scope_id for the V6 variant, but it looks like they're not being populated:

fn main() -> Result<(), Box<dyn Error>> {

    let interfaces = ifcfg::IfCfg::get().expect("ifcfg");

    // for interface in &interfaces {
    //     println!("{:#?}", interface);
    // }

    for interface in &interfaces {
        for address in &interface.addresses {
            if let Some(ref sock_addr) = address.address {
                if let SocketAddr::V6(v6_sock_addr) = sock_addr {
                    println!("scope_id: {} for {}", v6_sock_addr.scope_id(), v6_sock_addr)
                }
                
            }
        }
    }

    Ok(())
}

Results: All of theIPv6 addresses across multiple interfaces have scope_id 0.


I'm just trying to write my first IPv6 networking code, so I don't know how to actually get the scope ID. (Though I was playing around with Deno's new Deno.networkInterfaces() API yesterday and noticed that the do provide that. Maybe there's an example to follow somewhere in their code?)

also: Thanks for writing this library! ๐Ÿ˜„

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.