Skip to content

Instantly share code, notes, and snippets.

@ajithbh
Last active May 17, 2016 06:17
Show Gist options
  • Save ajithbh/cd626ab2fc95cff0ff9b to your computer and use it in GitHub Desktop.
Save ajithbh/cd626ab2fc95cff0ff9b to your computer and use it in GitHub Desktop.
Network link detection in Linux
#include <linux/mii.h>
#include <linux/sockios.h>
int check_iface_connected(int *psockfd)
{
struct ifreq ifr;
if (*psockfd == -1) {
*psockfd = socket(AF_INET, SOCK_DGRAM, 0);
}
if (*psockfd != -1) {
memset(&ifr, 0, sizeof(struct ifreq));
strcpy(ifr.ifr_name, "eth0");
if (ioctl(*psockfd, SIOCGMIIPHY, &ifr) != -1) {
struct mii_ioctl_data *p_mii_data = (struct mii_ioctl_data *) &(ifr.ifr_data);
p_mii_data->reg_num = MII_BMSR;
if (ioctl(*psockfd, SIOCGMIIREG, &ifr) != -1) {
if (p_mii_data->val_out & BMSR_LSTATUS) {
return TRUE;
} else {
return FALSE;
}
}
}
}
return FAIL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment