Search code examples
clinuxsocketsnetlink

How to suppress multiple netlink events?


I have been using this code as a sample to receive interface up/down events on a Linux machine. I am able to receive the events correctly but when an interface comes up from a previous down state, I receive multiple netlink events like this:

Event received >> NETLINK::Down
Event received >> NETLINK::Down
Event received >> NETLINK::Up

edit: I have modified a line in this code from:

addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;

to:

addr.nl_groups = RTMGRP_LINK;

But still I receive multiple events. Can anybody suggest if it is possible to suppress these events to receive a unique event for each state? If so how?


Solution

  • Finally after a lot of research I was able to get this done. There is a flag called "ifi_change" which gives out the change in the Netdevice's state change. I wasn't able to think of it because the rtnetlink man page did not mention anything about it. It says it is reserved for future use and should always be set to 0xFFFFFFFF. However, its value changes upon change in the current state of the interface. If there is a state change it gives a finite value else it is zero. Using this check I was able to suppress multiple netlink messages.