Search code examples
unixrustwindows-subsystem-for-linux

eth0 interface getting listed 3 times


I am on WSL 2 and using the nix crate in Rust and listing the network interfaces as shown below:

let ifaces = nix::ifaddrs::getifaddrs().unwrap();

for iface in ifaces {
    println!("{:#?}", iface);
}

When I do this, it strangely lists eth0 three times. Once with netmask as None, and once with broadcast as None.

Anyone know why it's being listed 3 separate times? Or is it an issue with my WSL config? I'd expect it to only show up once, like in the result of running ip show link.

I've provided the 3 listing below.

InterfaceAddress {
    interface_name: "eth0",
    flags: IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_MULTICAST | IFF_LOWER_UP | IFF_NO_PI | IFF_TUN | IFF_TAP,
    address: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 17,
                __ss_align: 140736544452160,
            },
        },
    ),
    netmask: None,
    broadcast: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 17,
                __ss_align: 140736544451856,
            },
        },
    ),
    destination: None,
}
InterfaceAddress {
    interface_name: "eth0",
    flags: IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_MULTICAST | IFF_LOWER_UP | IFF_NO_PI | IFF_TUN | IFF_TAP,
    address: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 2,
                __ss_align: 94107083991267,
            },
        },
    ),
    netmask: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 2,
                __ss_align: 94107084128296,
            },
        },
    ),
    broadcast: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 2,
                __ss_align: 94107084192256,
            },
        },
    ),
    destination: None,
}
InterfaceAddress {
    interface_name: "eth0",
    flags: IFF_UP | IFF_BROADCAST | IFF_RUNNING | IFF_MULTICAST | IFF_LOWER_UP | IFF_NO_PI | IFF_TUN | IFF_TAP,
    address: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 10,
                __ss_align: 94107083991267,
            },
        },
    ),
    netmask: Some(
        SockaddrStorage {
            ss: sockaddr_storage {
                ss_family: 10,
                __ss_align: 94107084128296,
            },
        },
    ),
    broadcast: None,
    destination: None,
}


Solution

  • This is normal. You're not listing interfaces, you're listing interface addresses with getifaddr. You can have multiple addresses per interface. See, e.g., how some of them have ss_family 2, others 10? That's IPv4 and v6 addresses. Compare with the output of ip -brief a or similar.

    You probably want if_nameindex instead.