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

使用WINPCAP获得已安装网卡的详细信息

2013年08月09日 ⁄ 综合 ⁄ 共 3033字 ⁄ 字号 评论关闭
应用winpcap-SDK提供的pcap_findalldevs()函数我们可以得到包含网卡信息的一张链表,网卡的信息保存在pcap_if_t结构体中,其中name字段为网卡的名称,description字段为对网卡的一种人们可以理解的描述。Addresses为网卡列表中首元素的指针.flags为一个标志位,指示此网卡是否可回送地址。其中addresses一个pcap_addr结构体,正是由于这个结构体,我们可以获得很多的有用信息,包括应用的网络协议族,IP地址,子网掩码,广播地址,目的地址。
其中pcap_addr结构体中addr指向一个包含IP地址的sockaddr的结构,netmask指向掩码,broadaddr指向广播地址,dstaddr指向目的地址。
相关的结构体如下:
/**********************************************************************/
Struct pcap_addr
{
pcap_addr * next   // if not NULL, a pointer to the next element in the list; NULL for the last element of the list
 
sockaddr * addr    // a pointer to a struct sockaddr containing an address
 
sockaddr * netmask   // if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding //to the address pointed to by addr.
 
 
sockaddr * broadaddr   // if not NULL, a pointer to a struct sockaddr that contains the broadcast address //corre­ sponding to the address pointed to by addr; may be null if the interface //doesn't support broadcasts
 
 
sockaddr * dstaddr    // if not NULL, a pointer to a struct sockaddr that contains the destination address //corre­ sponding to the address pointed to by addr; may be null if the interface isn't //a point- to-point interface 
 
}
 
/**********************************************************************/
Struct sockaddr_in
{
Short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}
 
具体的代码如下(C++):
 
#include<winsock2.h>
#include<pcap.h>
#include<iostream.h>
#pragma comment (lib,"ws2_32.lib")
void Analyse(pcap_if_t *d)
;
int main()
{
pcap_if_t *alldevs;
pcap_if_t *temp;
int idx=0;
char errbuf[PCAP_ERRBUF_SIZE];
/*应用pcap_findalldevs()获得网卡列表*/
if(-1==pcap_findalldevs(&alldevs,errbuf))
{
cout<<"pcap_findalldevs() failed!:"<<errbuf<<endl;
return 1;
}
cout<<"/t/t/t/t<网卡详细信息>"<<endl<<endl;
for(temp=alldevs;temp;temp=temp->next)
{
Analyse(temp);
}
 
 
/*调用pcap_freealldevs()释放资源*/
pcap_freealldevs(alldevs);
return 1;
}
/*************************************************/
/*void Analyse(pcap_if_t *d)
/*输出网卡的详细信息
/*************************************************/
void Analyse(pcap_if_t *d)
{
pcap_addr_t *a;
//输出网卡名字
cout<<"名称:"<<d->name<<endl;
//输出网卡描述
if(d->description)
cout<<"描述:"<<d->description<<endl;
else
cout<<"描述:没有可用描述."<<endl;
//判断网卡是否可回送
cout<<"网卡是否可回送:"<<((d->flags)&PCAP_IF_LOOPBACK?"Yes":"No")<<endl;
for(a=d->addresses;a;a=a->next)
{
//输出所应用的协议族
       cout<<"协议族:#"<<((struct sockaddr_in*)a->addr)->sin_family<<endl;
       switch(((struct sockaddr_in*)a->addr)->sin_family)
       {
       case AF_INET:
              cout<<"网络协议族:AF_INET"<<endl;
              //输出IP地址
              if(a->addr)
                     cout<<"IP地址:"<<inet_ntoa(((struct sockaddr_in*)a->addr)->sin_addr)<<endl;
              //输出掩码
              if(a->netmask)
cout<<"掩码:"<<inet_ntoa(((struct sockaddr_in*)a->netmask)->sin_addr)<<endl;
      
       //输出广播地址
       if(a->broadaddr)
cout<<"掩码:"<<inet_ntoa(((struct sockaddr_in*)a->broadaddr)->sin_addr)<<endl;
              //目的地址
       if (a->dstaddr)
cout<<"目的地址:"<<inet_ntoa(((struct sockaddr_in*)a->dstaddr)->sin_addr)<<endl;
break;
       default:
              cout<<"网络协议族未知.";
      
       break;
      
       }//switch
}//for
cout<<"===================================================================="<<endl;
 
}//Analyse

 

抱歉!评论已关闭.