1. 程式人生 > >Linux 下 可以使用ioctl()函式 以及 結構體 struct ifreq 結構體struct ifconf來獲取網路介面的各種資訊。

Linux 下 可以使用ioctl()函式 以及 結構體 struct ifreq 結構體struct ifconf來獲取網路介面的各種資訊。

轉載於:windeal專欄

 

Linux 下 可以使用ioctl()函式 以及 結構體 struct ifreq  結構體struct ifconf來獲取網路介面的各種資訊。

 

ioctl

首先看ioctl()用法

ioctl()原型如下:

 

#include <sys/ioctl.h>

int ioctl(int fd, int request, ...);

引數:

    fd     : 檔案描述符

request:  表示要請求的資訊。如IP地址、網路掩碼等

     ...     :  後面的可變引數根據request而定

 

比如我們請求所有網路介面的清單:

[cpp] view plaincopyprint?

  1. struct ifconf IoCtlReq;  
  2. ...  
  3. ioctl( Sock, SIOCGIFCONF, &IoCtlReq )  

 

其中IoCtlReq 是一個

 

與介面相關的request如下表所示(來源: <http://baike.baidu.com/view/1081282.htm?fr=aladdin>):

SIOCGIFCONF

SIOCSIFADDR

SIOCGIFADDR

SIOCSIFFLAGS

SIOCGIFFLAGS

SIOCSIFDSTADDR

SIOCGIFDSTADDR

SIOCGIFBRDADDR

SIOCSIFBRDADDR

SIOCGIFNETMASK

SIOCSIFNETMASK

SIOCGIFMETRIC

SIOCSIFMETRIC

SIOCGIFMTU

SIOCxxx

獲取所有介面的清單

設定介面地址

獲取介面地址

設定介面標誌

獲取介面標誌

設定點到點地址

獲取點到點地址

獲取廣播地址

設定廣播地址

獲取子網掩碼

設定子網掩碼

獲取介面的測度

設定介面的測度

獲取介面MTU

(還有很多取決於系統的實現)

struct ifconf

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

 

 

關於ioctl的詳細解釋清查閱本博其它博文

 

struct ifreq

結構體 struct ifreq用來儲存某個介面的資訊。

[cpp] view plaincopyprint?

  1. // if.h  
  2. /* 
  3.  * Interface request structure used for socket 
  4.  * ioctl's.  All interface ioctl's must have parameter 
  5.  * definitions which begin with ifr_name.  The 
  6.  * remainder may be interface specific. 
  7.  */  
  8. struct ifreq {  
  9. #define IFHWADDRLEN 6  
  10.     union  
  11.     {  
  12.         char    ifrn_name[IFNAMSIZ];        /* if name, e.g. "en0" */  
  13.     } ifr_ifrn;  
  14.       
  15.     union {  
  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   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.         void __user *   ifru_data;  
  28.         struct  if_settings ifru_settings;  
  29.     } ifr_ifru;  
  30. };  
  31. #define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */  
  32. #define ifr_hwaddr  ifr_ifru.ifru_hwaddr    /* MAC address      */  
  33. #define ifr_addr    ifr_ifru.ifru_addr  /* address      */  
  34. #define ifr_dstaddr ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */  
  35. #define ifr_broadaddr   ifr_ifru.ifru_broadaddr /* broadcast address    */  
  36. #define ifr_netmask ifr_ifru.ifru_netmask   /* interface net mask   */  
  37. #define ifr_flags   ifr_ifru.ifru_flags /* flags        */  
  38. #define ifr_metric  ifr_ifru.ifru_ivalue    /* metric       */  
  39. #define ifr_mtu     ifr_ifru.ifru_mtu   /* mtu          */  
  40. #define ifr_map     ifr_ifru.ifru_map   /* device map       */  
  41. #define ifr_slave   ifr_ifru.ifru_slave /* slave device     */  
  42. #define ifr_data    ifr_ifru.ifru_data  /* for use by interface */  
  43. #define ifr_ifindex ifr_ifru.ifru_ivalue    /* interface index  */  
  44. #define ifr_bandwidth   ifr_ifru.ifru_ivalue    /* link bandwidth   */  
  45. #define ifr_qlen    ifr_ifru.ifru_ivalue    /* Queue length     */  
  46. #define ifr_newname ifr_ifru.ifru_newname   /* New name     */  
  47. #define ifr_settings    ifr_ifru.ifru_settings  /* Device/proto settings*/  


 

 

ifr_name 標識了某一介面。

可以通過ioctl獲取該介面的資訊。如:

 

ioctl(Sock, SIOCGIFNETMASK, &IfReq);//獲取網路介面地址掩碼

該程式碼需要先對IfReq->ifr_name賦值,然後獲取與IfReq->ifr_name向匹配的網路介面 的地址掩碼

 

 

 

struct ifconf

結構體struct ifconf通常用來儲存所有介面資訊

[cpp] view plaincopyprint?

  1. // if.h  
  2. /* 
  3.  * Structure used in SIOCGIFCONF request. 
  4.  * Used to retrieve interface configuration 
  5.  * for machine (useful for programs which 
  6.  * must know all networks accessible). 
  7.  */  
  8. struct ifconf  {  
  9.     int ifc_len;            /* size of buffer   */  
  10.     union {  
  11.         char __user *ifcu_buf;  
  12.         struct ifreq __user *ifcu_req;  
  13.     } ifc_ifcu;  
  14. };  
  15. #define ifc_buf ifc_ifcu.ifcu_buf       /* buffer address   */  
  16. #define ifc_req ifc_ifcu.ifcu_req       /* array of structures  */  


 

該結構體可以用來獲取所喲網路介面的名字和資訊(不是全部資訊,是ip地址)