Wincap设置过滤器,该如何解决

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/06 21:43:14
Wincap设置过滤器,该如何解决

Wincap设置过滤器,该如何解决
Wincap设置过滤器,该如何解决

Wincap设置过滤器,该如何解决
我按照Wincap官方的例子,设置我的抓包程序的过滤器.
BOOL CapturePacket::SetFilter(char * packet_filter)//设置过滤器{if (pcap_compile(adhandle,&fcode,packet_filter,1,netmask) <0 )//设置过滤器{AfxMessageBox(Unable to compil e the packet filter.Check the syntax.);
TRACE(Error reading the packets:%s\n,pcap_geterr(adhandle));//语法错误,&fcode为0
/* 释放设备列表 */
pcap_freealldevs(alldevs);//return -1;}if (pcap_setfilter(adhandle,&fcode)<0)//设置过滤器{AfxMessageBox(Error setting the filter.);
/* 释放设备列表 */
pcap_freealldevs(alldevs);return -1;}// pcap_loop(adhandle,0,packet_handler,NULL);return 0;}pcap_compile函数执行返回小于0的数,就是没设置成功.于是我调试发现是&fcode为0.
就是开始定义struct bpf_program fcode;
然后pcap_compile,pcap_setfilter的参数调用啊!没其他地方调用,我问别人说,这个值要有值才行.
在pcap_compile(adhandle,&fcode,packet_filter,1,netmask)中,fcode的使用方式
基本上就是 :struct bpf_program fcode;然后传给这个接口就好了,这个参数的值是由pcap_compile来写的
我觉得之所以返回值小于0,你可能需要检查:
(1)adhandle,这个是一个网卡;
(2)packet_filter是不是符合winpcap的语法规则
(3)netmask是否正确,拿不准的话可以用0xffffff
希望对你有用
------解决方案--------------------------------------------------------
int pcap_setfilter ( pcap_t * p,
struct bpf_program * fp)Associate a filter to a capture.
pcap_setfilter() is used to specify a filter program.fp is a pointer to a bpf_program struct,usually the result of a call to pcap_compile().-1 is returned on failure,in which case pcap_geterr() may be used to display the error text; 0 is returned on success.
------解决方案--------------------------------------------------------
应该肯定不是fcode的问题你看看是不是掩码没有设置正确