eth0接口被列出了3次。

huangapple go评论58阅读模式
英文:

eth0 interface getting listed 3 times

问题

你使用的是WSL 2,并在Rust中使用了nix crate来列出网络接口,如下所示:

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

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

当我这样做时,它奇怪地将eth0列出了三次。一次是网掩码为None,一次是广播地址为None。

有人知道为什么会出现三次分开列出的情况吗?还是这是我的WSL配置有问题?我期望它只会像运行ip show link的结果中那样显示一次。

我提供了以下三个列表:

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,
}
英文:

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,
}

答案1

得分: 1

这是正常的。你没有列出接口,而是列出了使用 getif*addr* 获取的接口地址。每个接口可以有多个地址。例如,看一下它们中的一些是否有 ss_family 2,而另一些有 10?这分别代表 IPv4 和 IPv6 地址。与 ip -brief a 或类似命令的输出进行比较。

你可能想要使用 if_nameindex

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 08:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定