messense / if-addrs

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

Byte-order not correctly handled (?) #22

Closed mortenlj closed 1 year ago

mortenlj commented 1 year ago

I'm trying to get the IP of all interfaces, and when running on x86_64 it works fine, but when I compile for a more esoteric target (powerpc-unknown-linux-gnuspe), the returned IP addresses are reversed.

I imagine this is some sort of byte-ordering problem, but I'm not really sure where the problem lies.

Output from the example code in https://github.com/messense/if-addrs/blob/master/examples/list_interfaces.rs when executed on a Synology DS213+, which has a FREESCALE QorIQ P1022 (PowerPC) CPU:

Got list of interfaces
[
    Interface {
        name: "lo",
        addr: V4(
            Ifv4Addr {
                ip: 1.0.0.127,
                netmask: 0.0.0.255,
                broadcast: None,
            },
        ),
    },
    Interface {
        name: "eth0",
        addr: V4(
            Ifv4Addr {
                ip: 114.68.168.192,
                netmask: 0.255.255.255,
                broadcast: Some(
                    255.68.168.192,
                ),
            },
        ),
    },
    Interface {
        name: "eth50",
        addr: V4(
            Ifv4Addr {
                ip: 219.255.255.10,
                netmask: 0.0.255.255,
                broadcast: Some(
                    255.255.255.10,
                ),
            },
        ),
    },
    Interface {
        name: "lo",
        addr: V6(
            Ifv6Addr {
                ip: ::1,
                netmask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff,
                broadcast: None,
            },
        ),
    },
]
messense commented 1 year ago

I don't have a ppc64 machine to test, pull requests are welcome.

mortenlj commented 1 year ago

Even worse, this is a plain ppc (32 bit). :slightly_smiling_face: (Should be possible to emulate with QEMU though, if anyone is inclined).

I'm a beginner in Rust, and I couldn't figure out where or how this happens, so I'm at a loss as to where I would start.

Got any pointers for me, I can see what I manage to cobble together if I have a sense of where to start ...

mortenlj commented 1 year ago

I have been looking more at this, and have a fair idea of how it can be done. One thing I'm wondering is which would you prefer:

mortenlj commented 1 year ago

Turned out to be even simpler than I thought. :smile: