How to get a notification from Linux when the set of network interfaces changes

Hi all,

My program sends (and receives) IPv6 multicast packets (ff02::blah) over all of the local Ethernet links; at startup it calls getifaddrs() to find out what Ethernet devices are available to be multicast over.

This all works fine; my problem arises when the user changes the set of active Ethernet devices (e.g. by plugging or unplugging an Ethernet cable, or by enabling/disabling WiFi). When this happens, the list of Ethernet devices I cached at program startup is out-of-date; my program is likely now trying to send multicast packets over a network device that is no longer available; or conversely it may be failing to send multicast packets over a new network device that just became available.

In any case, what I need is some way for my program to be notified when the set of available network interfaces changes; when I get that notification I can then call getifaddrs() again and assemble a new list of network interfaces to use. With a bit of googling I was able to discover how to get this notification under MacOS/X (via the System Configuration framework) and under Windows XP (via NotifyAddrChange() and/or NotifyIpInterfaceChange()). I still haven't discovered the Linux way to do this, however, short of brute-forcing it by running a polling loop that calls getifaddrs() every few seconds and compares the results to the previous set. Perhaps someone knows how this is done?

Thanks,
Jeremy

 

 

I think you should be able to do that with an AF_NETLINK socket... See "man 7 netlink";
they show example code that uses a NETLINK_ROUTE socket to listen for interface
create/delete/up/down events... (You might also want to read RFC 3549 and
this old LJ article ...)

I wonder if you might also able to do it by setting up inotify/dnotify monitoring on
"/proc/net/dev"? I don't know if that works on proc files, though...

 

http://www.developerweb.net/forum/showthread.php?t=6035

你可能感兴趣的:(windows,linux,list,socket,user,NetWork)