Is there a notification mechanism for when getifad

2019-04-10 02:34发布

问题:

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)

回答1:

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



回答2:

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!)



回答3:

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"