Reputation: 169
I am trying to write an event-based c/c++ program which detects that how many times the network cable or the physical link (carrier signal) went down and how much packet loss is in the link. For the packet-loss, i would prefer it that it will check every second.Ideally, the pseudo code should look like;
//C program for checking how many times physical link went down
if(event==physical_link_down)
{
link_down++;
}
//separate C program for packet loss
while(true)
{
check_packet_loss;
sleep(1);
}
If I'm correct, I can get the following information by doing an ifconfig where the word "RUNNING" means we have an active physical link and packet loss can be calculated from dropped/error field.
I'm using Ubuntu OS btw.
I'm thinking of doing grep with ifconfig through my C program to get the required information. Is my approach correct? Second, how do I grep through a C program as I don't know how to do it? Similar questions have been asked but mostly those questions were asked for Windows environment. Any help will be appreciated! :)
Upvotes: 1
Views: 3258
Reputation: 2592
As long as I know getifaddrs, only allows to know the RX or TX bytes, packet loss..., but not to know the status of the physical link.
The interfaces used to know if the link is up or down, are ETHTOOL, or MII (wich are implemented with ethtool and mii-tool userland applications), one of those interfaces should be implemented in your network driver.
The best code I know for this task it's a part of the debian-installer, wich determines if a link is up. It tries with ethtool, and if it's not successfull with mii-tool. Here you can find the code.
Upvotes: 0
Reputation: 104549
You can get link status via getifaddrs. This is the C api in Linux that's roughly equivalent to calling ifconfig from the command line.
Upvotes: 3