我有filter="ip或vlan“,我像下面这样将它传递给libpcap,它在我的应用程序中工作了很长时间,没有任何问题。
pcap_set_filter(pdesc->pd, filter, 0, netp);现在我也被要求解析arp贸易。所以我设置了我的过滤器
ip或vlan或arp或rarp
。但是我的回调函数并不是对arp数据包调用,即使对于ip数据包,我的函数仍然被调用。
总之,我的问题是如何正确设置libpcap来从系统中获取arp数据包?
发布于 2014-01-07 12:42:18
我检查了BPF指令在WireShark,以了解发生了什么。以下是各种过滤器的BPF筛选程序:
1.简单案例:
过滤器:vlan
if the frame is VLAN-tagged then
return true
else
return false过滤器:ip
if the frame is not VLAN-tagged and the protocol is IP then
return true
else
return false过滤器:arp
if the frame is not VLAN-tagged and the protocol is ARP then
return true
else
return false过滤器:rarp
if the frame is not VLAN-tagged and the protocol is RARP then
return true
else
return false过滤器:ip or arp or rarp
if the frame is not VLAN-tagged and the protocol is either IP, ARP or RARP then
return true
else
return false2.将ip与vlan相结合的揭示了搜索标记的顺序很重要:
你的第一个过滤器是ip or vlan。其伪码如下:
if either the frame is not VLAN-tagged and the protocol is IP
or the frame is VLAN-tagged
then
return true
else
return false对于过滤器vlan or ip,我们希望看到以下内容:
if either the frame is VLAN-tagged
or the frame is not VLAN-tagged and the protocol is IP
then
return true
else
return false 这意味着相同,这是可以的,因为A or B的意思应该与B or A相同,不是吗?但我们明白了:
(000) ldh [12]
(001) jeq #0x8100 jt 4 jf 2
(002) ldh [16]
(003) jeq #0x800 jt 4 jf 5
(004) ret #65535
(005) ret #0这意味着类似于以下伪代码:
if either the frame is VLAN-tagged
or the frame is not VLAN-tagged but it has an EtherType field shifted 4 bytes right, which says the protocol is IP
then
return true
else
return false 这没道理。行(002)是不必要的。说明应如下所示:
(000) ldh [12]
(001) jeq #0x8100 jt 3 jf 2
(002) jeq #0x800 jt 3 jf 4
(003) ret #65535
(004) ret #0也许我会因为这么说而被杀,但我认为这是libpcap中的错误。上线(002) ldh [16]从何而来?如果过滤器是vlan and ip,那么在偏移量16处检查字节就有意义了:现在我们想要找到包含IP数据包的VLAN标记帧。在这样的帧中,有两个EtherType字段:第一个(偏移量为12)包含EtherType值(0x8100),第二个(偏移量为16)包含IP协议的EtherType (#0x800):
(000) ldh [12]
(001) jeq #0x8100 jt 2 jf 5
(002) ldh [16]
(003) jeq #0x800 jt 4 jf 5
(004) ret #65535
(005) ret #03.为什么你的过滤器没有找到ARP和RARP数据包?
你的过滤器是ip or vlan or arp or rarp。该文件汇编为:
(000) ldh [12]
(001) jeq #0x800 jt 6 jf 2
(002) jeq #0x8100 jt 6 jf 3
(003) ldh [16]
(004) jeq #0x806 jt 6 jf 5
(005) jeq #0x8035 jt 6 jf 7
(006) ret #65535
(007) ret #0这段代码有上面的错误: libpcap试图在偏移量16处找到ARP和RARP EtherTypes。
4.解决问题的方法
您可以通过在过滤器的开头添加bug:arp or rarp or ip or vlan来避免错误。该文件汇编为:
(000) ldh [12]
(001) jeq #0x806 jt 5 jf 2
(002) jeq #0x8035 jt 5 jf 3
(003) jeq #0x800 jt 5 jf 4
(004) jeq #0x8100 jt 5 jf 6
(005) ret #65535
(006) ret #0这意味着:
if either the frame is not VLAN-tagged and the protocol is either IP, ARP or RARP,
or the frame is VLAN-tagged
then
return true
else
return falsehttps://stackoverflow.com/questions/20968983
复制相似问题