getifaddrs() 结果发生变化时是否有通知机制?

发布于 2024-07-30 11:12:09 字数 297 浏览 6 评论 0原文

启动时,我的程序调用 getifaddrs() 来查找哪些网络接口可用于链路本地 IPv6 多播。 就目前而言,这是有效的,但它不能处理 getifaddrs() 返回后可用网络接口集发生更改的情况。

当网络接口发生更改时,操作系统是否有某种方式通知我的程序,以便我可以再次调用 getifaddrs() 并更新我的列表? 或者我注定每隔几秒就轮询一次 getifaddrs() ,永远?

(注意:在 Windows 上,我调用 GetAdaptersAddresses() 而不是 getifaddrs(),但那里存在同样的问题)

On startup, my program calls getifaddrs() to find out what network interfaces are available for link-local IPv6 multicasting. This works as far as it goes, but it doesn't handle the case where the set of available network interfaces changes after getifaddrs() has returned.

Is there some way for the OS to notify my program when the network interfaces have changed, so I can call getifaddrs() again and update my list? Or am I doomed to poll getifaddrs() every few seconds, forever?

(Note: on Windows, I call GetAdaptersAddresses() instead of getifaddrs(), but the same issue exists there)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜是你 2024-08-06 11:12:09

此外,实现此目的的 Linux 方法是打开 AF_NETLINK 系列和子类型 NETLINK_ROUTE 的套接字并读取从内核到达的消息,如“man 7 netlink”中包含的示例代码所示。 (感谢 Rob Searce 向我指出这一点!)

Also, the Linux way to implement this is by opening a socket of family AF_NETLINK and subtype NETLINK_ROUTE and reading the messages that arrive on it from the kernel, as shown in the example code included in "man 7 netlink". (Thanks to Rob Searce for pointing me to that!)

苦行僧 2024-08-06 11:12:09

如果有人感兴趣,我在苹果开发者网站上找到了以下文档,其中描述了如何在网络配置更改时收到通知。 这并不简单,但我确实掌握了对我有用的技术。 特别参见清单 8。

技术说明 TN1145 - 生活在动态 TCP/IP 环境中”

In case anyone is interested, I found the following document on Apple's developer site that describes how to get notified when the network configuration changes. It's non-trivial, but I did get the technique to work for me. See Listing 8 in particular.

Technical Note TN1145 - Living in a Dynamic TCP/IP Environment"

机场等船 2024-08-06 11:12:09

您可能想看看 NotifyAddrChange 和 NotifyIpInterfaceChange 函数。

You probably want to have a look at the NotifyAddrChange and NotifyIpInterfaceChange functions.

一人独醉 2024-08-06 11:12:09

对于 Linux:

使用 netlink 套接字。 它们可用于获取添加和删除接口的通知,以及接口地址更改的通知。

对于 macOS:

使用 PF_SYSTEM 套接字。 它们没有很好的文档记录,因此您可能需要四处寻找才能了解如何使用它们。 请参阅我在下面指出的代码中的注释。

对于 *BSD:

使用 PF_ROUTE 套接字。 除了以 RTM_IFANNOUNCE 消息形式传递的新/已删除接口指示之外,它们还可以以 RTM_NEWADDR/RTM_DELADDR 形式传递地址更改通知> 消息。

有关 Wireshark 中侦听新接口和已删除接口的代码(在主分支的提示中执行上述所有操作),请参阅 capture/iface_monitor.c。 它在 Linux 上使用 libnl 进行 netlink 套接字,并在其他平台上使用直接套接字调用。

对于其他 UN*Xes:

我不知道 Solaris、AIX、HP-UX 或各种现已失效的 UN*Xes 在这方面提供了什么。

对于 Windows:

请参阅此答案

For Linux:

Use netlink sockets. They can be used to get notifications of added and removed interfaces and, I think, changes of interface addresses.

For macOS:

Use PF_SYSTEM sockets. They're not well documented, so you may have to poke around to see how to use them. See comments in the code I point to below.

For *BSD:

Use PF_ROUTE sockets. In addition to new/removed interfaces indications, which are delivered as RTM_IFANNOUNCE message, they can deliver address-change notifications as well, as RTM_NEWADDR/RTM_DELADDR messages.

For the code in Wireshark that listens for new and removed interfaces, which, in the tip of the main branch, does all of the above, see capture/iface_monitor.c. It uses libnl for netlink sockets on Linux, and uses direct socket calls on the other platforms.

For other UN*Xes:

I don't know what Solaris, AIX, HP-UX, or various now-dead UN*Xes offer in that regard.

For Windows:

See this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文