<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    struct ifconf和struct ifreq,獲取網線插入狀態

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    這兩天看用C獲取當前網口的插入網線狀態的程序,遇見了這兩個不熟悉的結構體,看了頭文件中的說明和詳細。

    struct ifreq

    這個結構定義在include/net/if.h,用來配置ip地址,激活接口,配置MTU等接口信息的

     

    [cpp] view plaincopy  
    1. /* Interface request structure used for socket ioctl's.  All interface 
    2.    ioctl's must have parameter definitions which begin with ifr_name. 
    3.    The remainder may be interface specific.  */  
    4.   
    5. struct ifreq  
    6.   {  
    7. # define IFHWADDRLEN    6  
    8. # define IFNAMSIZ   IF_NAMESIZE  
    9.     union  
    10.       {     
    11.     char ifrn_name[IFNAMSIZ];   /* Interface name, e.g. "en0".  */  
    12.       } ifr_ifrn;  
    13.   
    14.     union  
    15.       {     
    16.     struct sockaddr ifru_addr;  
    17.     struct sockaddr ifru_dstaddr;  
    18.     struct sockaddr ifru_broadaddr;  
    19.     struct sockaddr ifru_netmask;  
    20.     struct sockaddr ifru_hwaddr;  
    21.     short int ifru_flags;  
    22.     int ifru_ivalue;  
    23.     int ifru_mtu;  
    24.     struct ifmap ifru_map;  
    25.     char ifru_slave[IFNAMSIZ];  /* Just fits the size */  
    26.     char ifru_newname[IFNAMSIZ];  
    27.     __caddr_t ifru_data;  
    28.       } ifr_ifru;  
    29.   };    
    30. # define ifr_name   ifr_ifrn.ifrn_name  /* interface name   */  
    31. # define ifr_hwaddr ifr_ifru.ifru_hwaddr    /* MAC address      */  
    32. # define ifr_addr   ifr_ifru.ifru_addr  /* address      */  
    33. # define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */  
    34. # define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */  
    35. # define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */  
    36. # define ifr_flags  ifr_ifru.ifru_flags /* flags        */  
    37. # define ifr_metric ifr_ifru.ifru_ivalue    /* metric       */  
    38. # define ifr_mtu    ifr_ifru.ifru_mtu   /* mtu          */  
    39. # define ifr_map    ifr_ifru.ifru_map   /* device map       */  
    40. # define ifr_slave  ifr_ifru.ifru_slave /* slave device     */  
    41. # define ifr_data   ifr_ifru.ifru_data  /* for use by interface */  
    42. # define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */  
    43. # define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth   */  
    44. # define ifr_qlen   ifr_ifru.ifru_ivalue    /* queue length     */  
    45. # define ifr_newname    ifr_ifru.ifru_newname   /* New name     */  
    46. # define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)  
    47. # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)  
    48. # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)  

     

     

    struct  ifconf

    通常是用來保存所有接口信息的

     

    [cpp] view plaincopy  
    1. /* Structure used in SIOCGIFCONF request.  Used to retrieve interface 
    2.    configuration for machine (useful for programs which must know all 
    3.    networks accessible).  */  
    4.   
    5. struct ifconf  
    6.   {  
    7.     int ifc_len;            /* Size of buffer.  */  
    8.     union  
    9.       {  
    10.     __caddr_t ifcu_buf;  
    11.     struct ifreq *ifcu_req;  
    12.       } ifc_ifcu;  
    13.   };  
    14. # define ifc_buf    ifc_ifcu.ifcu_buf   /* Buffer address.  */  
    15. # define ifc_req    ifc_ifcu.ifcu_req   /* Array of structures.  */  
    16. # define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0) /* not right */  

     

     

    應用

    想要獲取當前網口網線插入狀態,需要用到ifreq結構體,獲取網卡的信息,然后socket結合網卡驅動的ioctl,就可以得到與網線插入狀態相關的數據。
    另外推薦一個百度文庫的文章,包含代碼

    http://wenku.baidu.com/view/59f4508d680203d8ce2f2412.html

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全