简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM