最近在做基于wincap的网络嗅探器,开始对wincap的嗅探过程有了了解
1.先获取当前主机上的所有网卡设备(如果之前对设备名了如指掌,那可以直接从 第二步 开始)
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}
其中 alldevs 是个 pcap_if 结构体类型的指针,pcap_if结构体具体如下:
struct pcap_if {
struct pcap_if *next;
char *name; /* name to hand to "pcap_open_live()" */
char *description; /* textual description of interface, or NULL */
struct pcap_addr *addresses;
bpf_u_int32 flags; /* PCAP_IF_ interface flags */
};
2.从alldevs所指向的设备链表中找到想要监听的设备名也就是结构体中 pcap_if 中的name。然后使用下面这个
pcap_t* pcap_open_live (
const char * device,
int snaplen,
int promisc,
int to_ms,
char * ebuf)
函数打开设备文件,其中