UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Networking
  • » How to get a notification from Linux when the set of network interfaces changes

#1 2009-08-18 08:51 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: 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

Offline

#2 2009-08-18 01:30 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,836
Website

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

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...

Offline

#3 2009-08-19 05:14 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

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

Thanks Rob, that's exactly what I was looking for... and implementing it was about 10 times easier than the Mac version  :)

Offline

#4 2018-02-02 02:24 PM

Rak
Guest

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

Hi jfriesne , RobSeace
I am currently facing same problem and while doing google search came across this site.
I am looking for similar solution. Needed to check if you have the code available and can share it, please let me know.

  • Index
  • » Networking
  • » How to get a notification from Linux when the set of network interfaces changes

Board footer

Powered by FluxBB