33,311
社区成员
发帖
与我相关
我的任务
分享
#include<pcap.h>
/******* 主函数 *********/
void main()
{
pcap_t* pcap_handle; //WinPcap句柄
char error_content[PCAP_ERRBUF_SIZE]; //存储错误信息
char *net_interface; //网络接口
struct bpf_program bpf_filter; //BPF过滤规则
char bpf_filter_string[] = ""; //活驴规则字符串
bpf_int32 net_mask; //掩码
bpf_int32 net_ip; //网络地址
net_interface = pcap_lookupdev(error_content);//获得可用网络接口
pcap_lookupnet(net_interface,&net_ip,&net_mask,error_content); //获得网络地址和掩码地址
pcap_handle = pcap_open_live(net_interface,BUFSIZ,1,1,error_content); //打开网络接口
pcap_compile(pcap_handle,&bpf_filter,bpf_filter_string,0,net_ip); //编译BPF过滤规则
if(pcap_datalink(pcap_handle)!=DLT_EN10MB)
return;
pcap_loop(pcap_handle,-1,(pcap_handler)ethernet_protocol_packet_callback,NULL);
//注册回调函数,循环捕获网络数据包,一用回调函数来处理每个数据包
pcap_close(pcap_handle);
//关闭WinPcap操作
}