【发布时间】:2015-06-28 03:03:59
【问题描述】:
我正在尝试使用 pcap 进行一些简单的数据包捕获,因此我创建了一个句柄来通过 eth0 进行侦听。我的问题是代码末尾附近的pcap_loop(handle, 10, myCallback, NULL); 行。我正在尝试使用pcap_loop。
预期的输出应该是:
eth0
Activated!
1
2
3
...
10
Done processing packets!
当前输出缺少增量:
eth0
Activated!
Done processing packets!
目前它只是直接跳到“完成处理数据包!”我不知道为什么。即使它没有进入回调,它仍应等待数据包,因为 ;count' 参数(请参阅 pcap_loop 的文档)设置为 10。
#include <iostream>
#include <pcap.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void myCallback(u_char *useless, const struct pcap_pkthdr* hdr, const u_char*packet){
static int count = 1;
std::cout <<count<<std::endl;
count ++;
}
int main(){
char errbuf[PCAP_ERRBUF_SIZE];
char * devName;
char* net;
char* mask;
const u_char*packet;
struct in_addr addr;
struct pcap_pkthdr hdr;
bpf_u_int32 netp;
bpf_u_int32 maskp;
pcap_if_t *devs;
pcap_findalldevs(&devs, errbuf);
devName = pcap_lookupdev(errbuf);
std::cout <<devName<<std::endl;
int success = pcap_lookupnet(devName, &netp, &maskp, errbuf);
if(success<0){
exit(EXIT_FAILURE);
}
pcap_freealldevs(devs);
//Create a handle
pcap_t *handle = pcap_create(devName, errbuf);
pcap_set_promisc(handle, 1);
pcap_can_set_rfmon(handle);
//Activate the handle
if(pcap_activate(handle)){
std::cout <<"Activated!"<<std::endl;
}
else{
exit(EXIT_FAILURE);
}
pcap_loop(handle, 10, myCallback, NULL);
std::cout <<"Done processing packets!"<<std::endl;
//close handle
pcap_close(handle);
}
【问题讨论】:
标签: c++ pcap packet-sniffers winpcap sniffing