在 WinPcap SDK 中,有一个函数:pcap_findalldevs_ex,这个函数可以获得网络接口列表。
示例程序:
// SimpleSniffer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <pcap.h>
int _tmain()
{
pcap_if_t *alldevs = nullptr;
pcap_if_t *d = nullptr;
char errbuf[PCAP_ERRBUF_SIZE];
int count = 0;
// retrieve the adapters from the computer
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
_ftprintf(stderr, _T("Error in pcap_findalldevs_ex: %s\n"), errbuf);
exit(1);
}
// if there are no adapters, print an error
if (alldevs == nullptr)
{
_ftprintf(stderr, _T("\nNo adapters found! Make sure WinPcap is installed.\n"), errbuf);
exit(2);
}
// print the list of adapters along with basic information about an adapter
for (d = alldevs; d != NULL; d = d->next)
{
printf("%2d. %s\n", ++count, d->name);
if (d->description)
{
printf(" %s\n\n", d->description);
}
else
{
printf(" (No description available)\n");
}
}
if (count == 0)
{
_tprintf(_T("\nNo interfaces found! Make sure WinPcap is installed.\n"));
}
// free the network adapter list
pcap_freealldevs(alldevs);
return 0;
}
运行结果: