Last active
May 17, 2016 06:17
-
-
Save ajithbh/cd626ab2fc95cff0ff9b to your computer and use it in GitHub Desktop.
Network link detection in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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