I write a program which needs to get notified if the network interfaces have changed, in particular new one appeared or existed one gone. My research brought to the netlink and its RTMGRP_LINK
signal. This manpage gives an example which is not clear for me yet.
It has this code:
memset(&sa, 0, sizeof(sa));
snl.nl_family = AF_NETLINK;
snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
bind(fd, (struct sockaddr*)&sa, sizeof(sa));
My understading is this is an initialization part to enable some signals. I guess there must be a handler subroutine which processes the event.
My questions are:
Is this a correct code snippet for enabling the event?
How to process the event: do I need to have some handler?
What are the data structures that contains a relevant information (about network interfaces and their changes)?
Thanks for any help.
The following code has been taken from here and I have added the logic to get the name of the concerned interface.