现在的位置: 首页 > 综合 > 正文

获取网络状态

2013年10月09日 ⁄ 综合 ⁄ 共 1049字 ⁄ 字号 评论关闭

点击打开链接

Linux_stat.c

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <fcntl.h>  
  5. #include <errno.h>  
  6. #include <sys/ioctl.h>  
  7. #include <sys/types.h>  
  8. #include <sys/socket.h>  
  9. #include <linux/if.h>  
  10. #include <linux/sockios.h>  
  11. #include <linux/ethtool.h>  
  12. int get_netlink_status(const char *if_name);  
  13. int main()  
  14. {  
  15.     if(getuid() != 0)  
  16.     {  
  17.         fprintf(stderr, "Netlink Status Check Need Root Power./n");  
  18.         return 1;  
  19.     }  
  20.       
  21.     printf("Net link status: %d/n", get_netlink_status("eth0"));  
  22.     return 0;  
  23. }  
  24. // if_name like "ath0", "eth0". Notice: call this function  
  25. // need root privilege.  
  26. // return value:  
  27. // -1 -- error , details can check errno  
  28. // 1 -- interface link up  
  29. // 0 -- interface link down.  
  30. int get_netlink_status(const char *if_name)  
  31. {  
  32.     int skfd;  
  33.     struct ifreq ifr;  
  34.     struct ethtool_value edata;  
  35.     edata.cmd = ETHTOOL_GLINK;  
  36.     edata.data = 0;  
  37.     memset(&ifr, 0, sizeof(ifr));  
  38.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);  
  39.     ifr.ifr_data = (

抱歉!评论已关闭.