找到正确的“网络接口” IPv6 号码
我正在尝试使用 Boost 进行某些 IPv6 和多播网络通信。我需要构建一个使用特定网络接口索引的 IPv6 多播套接字。
我能够在 boost/asio/ip/detail/socket_option.hpp 中找到正确的多播选项来设置网络接口索引: 显式的multicast_request(const boost::asio::ip::address_v6&multicast_address, unsigned long network_interface = 0)
问题是,我不知道如何找到“network_interface”参数的正确值。有没有办法使用我可以提供的本地 IPv6 地址获取 network_interface 值?我查看了文档和示例,但找不到任何内容。
——迪伦
I am trying to use Boost for some IPv6 and multicast network communication. I need to construct an IPv6 multicast socket that uses a specific network interface index.
I was able to find the correct multicast option to set the network interface index in boost/asio/ip/detail/socket_option.hpp:
explicit multicast_request(const boost::asio::ip::address_v6& multicast_address, unsigned long network_interface = 0)
The problem is, I don't know how to find the correct value for the "network_interface" parameter. Is there a way to get the network_interface value using a local IPv6 address that I can provide? I looked in the documentation and examples, but couldn't find anything.
-- Dylan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个平台都提供 API 来枚举网络接口,例如
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/ browser/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。
Each platform provides APIs to enumerate the network interfaces, e.g.
getifaddrs
for many Unixes andGetAdaptersAddresses
for Windows. Note on Windows there is a separate numerical space for IPv4 and IPv6 adapters which makes the API callif_nametoindex
quite confusing.You may wish to inspect the methods I employed in OpenPGM for portability, considering Windows doesn't really have useful adapter names:
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
我认为没有一种独立于平台的方法可以解决这个问题,就像有 没有可移植的解决方案来枚举本地地址。
在 Linux 上,您可以在
/proc/net/if_inet6
的第二列中找到所需内容,也可以通过rtnetlink(7)
接口更可靠地使用该内容。I don't think there's a platform-independent way to figure this out, just as there is no portable solution to enumerating the local addresses.
On Linux, you can find what you want in the second column of
/proc/net/if_inet6
, which is also available more robustly through thertnetlink(7)
interface.