EstebanBorai / network-interface

Retrieve system's Network Interfaces on Linux, macOS and Windows on a standardized manner
https://crates.io/crates/network-interface
Apache License 2.0
61 stars 28 forks source link

Incorrect broadcast address on Windows with multiple IPs per adapter #46

Closed mrtnlrsn closed 11 months ago

mrtnlrsn commented 11 months ago

Given this Windows 10 Home Edition IP configuration (ipconfig selected lines)

Ethernet adapter Ethernet:
   Description . . . . . . . . . . . : Intel(R) PRO/1000 MT Desktop Adapter
   Physical Address. . . . . . . . . : 08-00-27-1E-BA-0E
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 10.234.88.89(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.254.0
   IPv4 Address. . . . . . . . . . . : 192.168.0.191(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   IPv4 Address. . . . . . . . . . . : 192.168.1.25(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1
   DNS Servers . . . . . . . . . . . : 192.168.0.19

cargo test "test::show_network_interface" -- --nocapture reports (loopback interfaces omitted): Notice the broadcast address is identical for all IPs.

    NetworkInterface {
        name: "Ethernet",
        addr: [
            V4(
                V4IfAddr {
                    ip: 10.234.88.89,
                    broadcast: Some(
                        10.234.89.255,
                    ),
                    netmask: Some(
                        255.255.254.0,
                    ),
                },
            ),
        ],
        mac_addr: Some(
            "08:00:27:1E:BA:0E",
        ),
        index: 10,
    },
    NetworkInterface {
        name: "Ethernet",
        addr: [
            V4(
                V4IfAddr {
                    ip: 192.168.0.191,
                    broadcast: Some(
                        10.234.89.255,
                    ),
                    netmask: Some(
                        255.255.255.0,
                    ),
                },
            ),
        ],
        mac_addr: Some(
            "08:00:27:1E:BA:0E",
        ),
        index: 10,
    },
    NetworkInterface {
        name: "Ethernet",
        addr: [
            V4(
                V4IfAddr {
                    ip: 192.168.1.25,
                    broadcast: Some(
                        10.234.89.255,
                    ),
                    netmask: Some(
                        255.255.255.0,
                    ),
                },
            ),
        ],
        mac_addr: Some(
            "08:00:27:1E:BA:0E",
        ),
        index: 10,
    },

I did a little debugging and I can see, that the correct broadcasst addresses are returned from windows' GetAdaptersAddresses(). Your function windows.rs:NetworkInterface:show() has this break if prefix_index_ipv4 == 2 {. If that is removed it will iterate over the correct broadcast addresses also.

It is however non-trivial for me to see, how to correctly incorporate that information... Hoping you are more experienced in understanding Windows' network API docs..

EstebanBorai commented 11 months ago

Hi @mrtnlrsn thanks for opening this isssue!

I could invest some time on Windows debugging for this but it may take a little while due to the fact that I dont have access to a Windows PC anymore.

If you are willing to give a shot, I think you could follow your hypothesis and provide a couple tests that support it.

Most of the Windows based improvements in this crate has been introduced by more experienced Windows users to be honest, Im more on the Unix side.

Feel free to reach out anytime!

mrtnlrsn commented 11 months ago

I don't have a hypothesis as such to try out, I just confirmed, that the data is available... I don't undestand the windows API good enought to understand why the code is as it is... And it is somewhat complex...

mrtnlrsn commented 11 months ago

I have been given time at my work to fix this. So expect a pull request some time...