DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1.   
[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /********************************** (C) COPYRIGHT *******************************  
  2. * File Name          : linkstatus_check.c  
  3. * Author             : skdkjzz  
  4. * Date               : 2014/08/07  
  5. * Description        : 网线是否插上  
  6. *********************************************************************************/  
  7.   
  8.   
  9. #include <sys/types.h>    
  10. #include <string.h>    
  11. #include <stdlib.h>    
  12. #include <sys/types.h>    
  13. #include <sys/ioctl.h>    
  14. #include <sys/stat.h>    
  15. #include <stdio.h>    
  16. #include <string.h>    
  17. #include <errno.h>    
  18. #include <net/if.h>    
  19. #include <sys/utsname.h>    
  20. #include <limits.h>    
  21. #include <ctype.h>       
  22. #include <sys/socket.h>    
  23. #include <netinet/in.h>    
  24. #include <arpa/inet.h>       
  25. #include <linux/sockios.h>    
  26.     
  27. #define ETHTOOL_GLINK   0x0000000a   /* Get link status (ethtool_value) */    
  28. struct ethtool_value {    
  29.                       unsigned int   cmd;    
  30.                       unsigned int   data;     
  31.                      };      
  32.     
  33. int get_netlink_status(const char *if_name);    
  34.      
  35.   
  36.   
  37. /****************************************************************   
  38.    return value:    
  39.    -1 -- error , details can check errno    
  40.    1  -- interface link up    
  41.    0  -- interface link down.    
  42. ****************************************************************/  
  43. int get_netlink_status(const char *if_name)    
  44. {    
  45.     int skfd;    
  46.     struct ifreq ifr;    
  47.     struct ethtool_value edata;    
  48.     edata.cmd = ETHTOOL_GLINK;    
  49.     edata.data = 0;    
  50.     memset(&ifr, 0, sizeof(ifr));    
  51.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);    
  52.     ifr.ifr_data = (char *)&edata;    
  53.     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)    
  54.         return -1;    
  55.     if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)    
  56.     {    
  57.        close(skfd);         
  58.        return -1;    
  59.     }    
  60.     close(skfd);    
  61.     return edata.data;    
  62. }    
  63.   
  64.   
  65. int main()    
  66. {    
  67.     char net_buf[10]="eth0";   
  68.     printf("Net link status: %s\n", get_netlink_status(net_buf) == 1 ? "up" : "down");    
  69.     return 0;    
  70. }    
  71.    
  72.   
  73.   
  74.    </span>  
posted on 2016-04-12 19:36  DoubleLi  阅读(502)  评论(0编辑  收藏  举报