4

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)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Why would you expect the network interfaces to change so often that you'd have to poll every few seconds? Are you talking about virtual interfaces? – Michael Foukarakis Aug 13 '09 at 05:56
  • So far we've mainly seen the problem on MacBooks, where the user has our program running and is turning AirPort on and off, connecting/disconnecting from the wired LAN, enabling/disabling HSPA high-speed wireless, etc. When this happens, the program starts malfunctioning since it is still trying to use the now-inactive interfaces, and not trying to use the newly active ones. – Jeremy Friesner Aug 13 '09 at 15:46
  • Dupe: http://stackoverflow.com/questions/579783/how-to-detect-ip-address-change-programmatically-in-linux – Steve-o Aug 09 '10 at 14:28

3 Answers3

4

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

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
2

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"

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • I think the above link is now dead - I found it on this one http://developer.apple.com/library/mac/#technotes/tn1145/_index.html – Steg Oct 27 '11 at 09:30
1

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

steve
  • 5,870
  • 1
  • 21
  • 22