0

I have written a program using Netlink sockets. I am able to successfully detect the link status change (i.e., when the network cable is plugged/unplugged). But in both the cases I am getting a RTM_NEWLINK event.

I thought I could query the SIOCGIFFLAGS and get the IFF_UP status to know the status using getifaddrs system call. To do that I would like to know what is the interface that caused the link event. Is there a way to know that? Currently I am not aware of any members in the struct nlmsghdr or sockaddr_nl that would give the interface that caused a link change.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Thomas
  • 489
  • 1
  • 8
  • 13

2 Answers2

0

Using SIOCIFCONF ioctl call you could iterate over all the available interfaces. And then using SIOCGIFFLAGS and IFF_UP and IFF_RUNNING you could check the status of that interface. But SIOCGIFCONF is not of much help in case you do the bridging and add one or two of interfaces to that bridge.

These days i am also dealing with netlink. and my problem is almost similar to yours "Knowing the interface that caused a link status change". Please refer to link which i created:https://superuser.com/questions/596296/how-to-identify-that-one-of-the-bridge-interfacessw-bridge-in-linux-is-down

Could you please post your netlink socket code.

Community
  • 1
  • 1
user2311046
  • 309
  • 1
  • 3
  • 10
0

you can just get the name from the ifi_index:

struct ifinfomsg *ifi   = NLMSG_DATA(h);
char name[IF_NAMESIZE];
printf("%s is %s\n", if_indextoname(ifi->ifi_index, name), ifi->ifi_flags & IFF_UP ? "up":"down");

where h is your nlmsghdr structure.

I'm clearly not en expert but I found that funny you get RTM_NEWLINK when you plug and unplug the cable. I would expect RTM_DELLINK when cable gets unplugged... or maybe I am missing something...

yes, I was missing something :) you can get the status simply by looking at the ifi->ifi_flags. Depending on your needs, I guess you will be interested in IFF_UP, IFF_RUNNING and IFF_LOWER_UP.

Ayman Khamouma
  • 976
  • 10
  • 24