4

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?

Community
  • 1
  • 1
iqstatic
  • 2,322
  • 3
  • 21
  • 39

2 Answers2

4

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.

iqstatic
  • 2,322
  • 3
  • 21
  • 39
  • 1
    how did you learn about ifi_change? by browsing and understanding the kernel code ? or is there any other documentation which i'm not able to find yet. – dhanlin Sep 06 '19 at 03:52
1

Looking at the code, I suspect you're getting a notification for each of the following types of events:

addr.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;

I'd suggest narrowing it down to

addr.nl_groups = RTMGRP_LINK;

(or to IPv4/IPv6 as appropriate.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Oops!! Sorry I forgot to mention that I have already tried that but still I receive multiple up/down events. – iqstatic Dec 30 '14 at 05:55
  • @iqstatic: If the code you're running isn't identical to the code you've linked to, I think you should include your code in the question (reduced to the absolute minimum needed to reproduce the problem). – NPE Dec 30 '14 at 05:58
  • The change you suggested is the only change I have made. – iqstatic Dec 30 '14 at 06:02