擷取網路介面資訊——ioctl()函數與結構體struct ifreq、 struct ifconf

來源:互聯網
上載者:User

標籤:ifconf   ifreq   ioctl   

轉載請註明出處:windeal專欄


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


ioctl首先看ioctl()用法ioctl()原型如下:
       #include <sys/ioctl.h>       int ioctl(int fd, int request, ...);
參數:    fd     : 檔案描述符request:  表示要請求的資訊。如IP地址、網路遮罩等     ...     :  後面的可變參數根據request而定
比如我們請求所有網路介面的清單:
struct ifconf IoCtlReq;...ioctl( Sock, SIOCGIFCONF, &IoCtlReq )

其中IoCtlReq 是一個
與介面相關的request如下表所示(來源: <http://baike.baidu.com/view/1081282.htm?fr=aladdin>):
接口 SIOCGIFCONFSIOCSIFADDRSIOCGIFADDRSIOCSIFFLAGSSIOCGIFFLAGSSIOCSIFDSTADDRSIOCGIFDSTADDRSIOCGIFBRDADDRSIOCSIFBRDADDRSIOCGIFNETMASKSIOCSIFNETMASKSIOCGIFMETRICSIOCSIFMETRICSIOCGIFMTUSIOCxxx 擷取所有介面的清單設定介面地址擷取介面地址設定介面標誌擷取介面標誌設定點到點地址擷取點到點地址擷取廣播位址設定廣播位址擷取子網路遮罩設定子網路遮罩擷取介面的測度設定介面的測度擷取介面MTU(還有很多取決於系統的實現) struct ifconfstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreqstruct ifreq

關於ioctl的詳細解釋清查閱本博其它博文
struct ifreq結構體 struct ifreq用來儲存某個介面的資訊。
// if.h/* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have parameter * definitions which begin with ifr_name.  The * remainder may be interface specific. */struct ifreq {#define IFHWADDRLEN6union{charifrn_name[IFNAMSIZ];/* if name, e.g. "en0" */} ifr_ifrn;union {structsockaddr ifru_addr;structsockaddr ifru_dstaddr;structsockaddr ifru_broadaddr;structsockaddr ifru_netmask;struct  sockaddr ifru_hwaddr;shortifru_flags;intifru_ivalue;intifru_mtu;struct  ifmap ifru_map;charifru_slave[IFNAMSIZ];/* Just fits the size */charifru_newname[IFNAMSIZ];void __user *ifru_data;structif_settings ifru_settings;} ifr_ifru;};#define ifr_nameifr_ifrn.ifrn_name/* interface name */#define ifr_hwaddrifr_ifru.ifru_hwaddr/* MAC address */#defineifr_addrifr_ifru.ifru_addr/* address*/#defineifr_dstaddrifr_ifru.ifru_dstaddr/* other end of p-p lnk*/#defineifr_broadaddrifr_ifru.ifru_broadaddr/* broadcast address*/#defineifr_netmaskifr_ifru.ifru_netmask/* interface net mask*/#defineifr_flagsifr_ifru.ifru_flags/* flags*/#defineifr_metricifr_ifru.ifru_ivalue/* metric*/#defineifr_mtuifr_ifru.ifru_mtu/* mtu*/#define ifr_mapifr_ifru.ifru_map/* device map*/#define ifr_slaveifr_ifru.ifru_slave/* slave device*/#defineifr_dataifr_ifru.ifru_data/* for use by interface*/#define ifr_ifindexifr_ifru.ifru_ivalue/* interface index*/#define ifr_bandwidthifr_ifru.ifru_ivalue    /* link bandwidth*/#define ifr_qlenifr_ifru.ifru_ivalue/* Queue length */#define ifr_newnameifr_ifru.ifru_newname/* New name*/#define ifr_settingsifr_ifru.ifru_settings/* Device/proto settings*/



ifr_name 標識了某一介面。
可以通過ioctl擷取該介面的資訊。如:
ioctl(Sock, SIOCGIFNETMASK, &IfReq);//擷取網路介面位址遮罩
該代碼需要先對IfReq->ifr_name賦值,然後擷取與IfReq->ifr_name向匹配的網路介面 的位址遮罩


struct ifconf結構體struct ifconf通常用來儲存所有介面資訊
// if.h/* * Structure used in SIOCGIFCONF request. * Used to retrieve interface configuration * for machine (useful for programs which * must know all networks accessible). */struct ifconf  {intifc_len;/* size of buffer*/union {char __user *ifcu_buf;struct ifreq __user *ifcu_req;} ifc_ifcu;};#defineifc_bufifc_ifcu.ifcu_buf/* buffer address*/#defineifc_reqifc_ifcu.ifcu_req/* array of structures*/


該結構體可以用來擷取所喲網路介面的名字和資訊(不是全部資訊,是ip地址)(圖片來自:http://tech.sunplusedu.com/space/post-4064.aspx)


Example:
#include <sys/types.h>  #include <sys/ioctl.h>  #include <sys/socket.h>  #include <net/if.h>  #include <stdio.h>  #include <stdlib.h>  #include <unistd.h>  #include <netdb.h>  #include <string.h>  #include <fcntl.h>  #include <string.h>  #include <errno.h>typedef uint32_t uint32; #define MAX_IF 10int main(){struct ifreq ifVec[MAX_IF];//用來儲存所有介面int sock = -1;if ( (sock = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )fprintf(stderr, "Error:%d, cannot open RAM;\n");// get if vector struct ifconf ioIfConf;    ioIfConf.ifc_buf = (void *)ifVec;       ioIfConf.ifc_len = sizeof(ifVec); printf("Len:%d\n", ioIfConf.ifc_len);     if (ioctl(sock, SIOCGIFCONF, &ioIfConf) < 0 )//擷取所有網路介面資訊fprintf(stderr, "Error:%d   ioctl IFCONF\n"); printf("Len:%d\n", ioIfConf.ifc_len);// 和前面到len對比,發現ioctl修改裡len到大小     //迴圈列印每個網路介面到資訊{struct ifreq *ifPt;struct ifreq *ifEndPt;ifPt = ifVec;ifEndPt = (void *)((char *)ifVec + ioIfConf.ifc_len);for (ifPt = ifVec; ifPt < ifEndPt; ifPt++){struct ifreq ifReq;if ( ifPt->ifr_addr.sa_family != AF_INET ) {                continue;            }// Temp keepers of interface params...uint32 u32_addr, u32_mask;/*列印ip地址*/char ipDotBuf[16], subnetDotBuf[16], maskDotBuf[16]; // 儲存點分十進位到ip地址u32_addr = ((struct sockaddr_in *)&ifPt->ifr_addr)->sin_addr.s_addr;inet_ntop(AF_INET, &u32_addr, ipDotBuf, (socklen_t )sizeof(ipDotBuf)); printf("IP Address: %s\n", ipDotBuf);/*    列印位址遮罩    */bzero(&ifReq,sizeof(struct ifreq));  memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));  if (ioctl(sock, SIOCGIFNETMASK, &ifReq ) < 0){fprintf(stderr, "Error: %d, cannot get mask\n", errno);}else{u32_mask = ((struct sockaddr_in *)&ifReq.ifr_addr)->sin_addr.s_addr;inet_ntop(AF_INET, &u32_mask, maskDotBuf, (socklen_t )sizeof(maskDotBuf));printf("Mask: %s\n", maskDotBuf);}/*    列印MTU    */bzero(&ifReq,sizeof(struct ifreq));  memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));  if (ioctl(sock, SIOCGIFMTU, &ifReq ) < 0){fprintf(stderr, "Error: %d, cannot get MTU\n", errno);}else{printf("SIOCGIFMTU:%d\n", ifReq.ifr_mtu); }           /*    其他資訊的列印方式與掩碼和MTU相同    */}}}



運行結果:
[email protected]:~/Windeal/apue$ ./exe Len:320Len:64IP Address: 127.0.0.1Mask: 255.0.0.0SIOCGIFMTU:16436IP Address: 172.17.92.198Mask: 255.255.254.0SIOCGIFMTU:1500[email protected]:~/Windeal/apue$ 



















擷取網路介面資訊——ioctl()函數與結構體struct ifreq、 struct ifconf

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.