winpcap:獲取網絡適配器信息

1.pcap_if_t結構體

typedef struct pcap_if pcap_if_t;
struct pcap_if {
	struct pcap_if *next;
	char *name;		/* 適配器名字*/
	char *description;	/* 適配器描述 */
	struct pcap_addr *addresses;/*適配器地址*/
	bpf_u_int32 flags;	/* 適配器接口標識符,值爲PCAP_IF_LOOPBACK */
};

2. pcap_findalldevs函數

int	pcap_findalldevs(pcap_if_t **, char *);

    說明:用來獲得網卡的列表
    參數: 指向pcap_if_t**類型的列表的指針的指針; char型指針,當打開列表錯誤時返回錯誤信息
    返回值: 爲int型,當顯示列表失敗時返回-1

 

3.測試例子

#include <QCoreApplication>
#include <QDebug>
#include "pcap.h"
//打印設備列表信息
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    pcap_if_t *alldevs;        //所有網絡適配器
    pcap_if_t *d;               //選中的網絡適配器

    int i = 0;
    char errbuf[PCAP_ERRBUF_SIZE];
    if(pcap_findalldevs(&alldevs, errbuf) == -1)//獲取網卡列表道中
    {
        qDebug() << errbuf;
    }
    for(d = alldevs; d; d = d->next)
    {
        qDebug() << ++i << d->name;
        if(d->description)
            qDebug() << d->description;
        else
            qDebug("(No description available)");
    }
    if(0 == i)
    {
        qDebug("No interfaces found! Make sure WinPcap is installed.");
    }
    pcap_freealldevs(alldevs);
    return a.exec();

}

4.輸出結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章