messense / if-addrs

Retrieve IP addresses for all interfaces. POSIX & Windows
BSD 3-Clause "New" or "Revised" License
22 stars 15 forks source link

Add more details about each IpAddr for interface #28

Open mortenlj opened 1 year ago

mortenlj commented 1 year ago

I have an interface with multiple IP-addresses, where I need to pick the "primary" IP assigned to the interface. As it is, I get both IP-addresses, but there is no way to tell which is which from the data returned.

Unfortunately, I don't know enough about these things to know where to start on a PR.

To show my use case more clearly:

In the below output, I need a way to differentiate between 192.168.68.125 (the one I want), and 192.168.68.91 (the secondary):

$ ip address show dev wlan0
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:e2:f1:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.68.125/24 brd 192.168.68.255 scope global dynamic wlan0
       valid_lft 5035sec preferred_lft 5035sec
    inet 192.168.68.91/24 brd 192.168.68.255 scope global secondary wlan0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:fee2:f135/64 scope link
       valid_lft forever preferred_lft forever

When using if_addrs, this is as far as I get:

Got list of interfaces
[
    Interface {
        name: "wlan0",
        addr: V4(
            Ifv4Addr {
                ip: 192.168.68.125,
                netmask: 255.255.255.0,
                broadcast: Some(
                    192.168.68.255,
                ),
            },
        ),
        index: Some(
            3,
        ),
    },
    Interface {
        name: "wlan0",
        addr: V4(
            Ifv4Addr {
                ip: 192.168.68.91,
                netmask: 255.255.255.0,
                broadcast: Some(
                    192.168.68.255,
                ),
            },
        ),
        index: Some(
            3,
        ),
    },
]