我正在尝试将 Boost 用于某些 IPv6 和多播网络通信。我需要构建一个使用特定网络接口(interface)索引的 IPv6 多播套接字。
我能够在 boost/asio/ip/detail/socket_option.hpp 中找到正确的多播选项来设置网络接口(interface)索引:
显式多播请求(const boost::asio::ip::address_v6& 多播地址,无符号长网络接口(interface) = 0)
问题是,我不知道如何找到“network_interface”参数的正确值。有没有办法使用我可以提供的本地 IPv6 地址获取 network_interface 值?我查看了文档和示例,但找不到任何东西。
——迪伦
最佳答案
每个平台都提供 API 来枚举网络接口(interface),例如 getifaddrs
对于许多 Unix 和 GetAdaptersAddresses
对于 Windows。注意在 Windows 上,IPv4 和 IPv6 适配器有一个单独的数字空间,这使得 API 调用 if_nametoindex
相当困惑。
考虑到 Windows 并没有真正有用的适配器名称,您可能希望检查我在 OpenPGM 中使用的方法的可移植性:
http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/getifaddrs.c
http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/nametoindex.c
http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/indextoaddr.c
http://code.google.com/p/openpgm/source/browse/trunk/openpgm/pgm/indextoname.c
关于networking - 为 IPv6 找到正确的 "network interface"号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942073/