首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置libpcap以获取arp数据包

如何设置libpcap以获取arp数据包
EN

Stack Overflow用户
提问于 2014-01-07 10:15:16
回答 1查看 1.1K关注 0票数 1

我有filter="ip或vlan“,我像下面这样将它传递给libpcap,它在我的应用程序中工作了很长时间,没有任何问题。

代码语言:javascript
复制
pcap_set_filter(pdesc->pd, filter, 0, netp);

现在我也被要求解析arp贸易。所以我设置了我的过滤器

ip或vlan或arp或rarp

。但是我的回调函数并不是对arp数据包调用,即使对于ip数据包,我的函数仍然被调用。

总之,我的问题是如何正确设置libpcap来从系统中获取arp数据包?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-07 12:42:18

我检查了BPF指令WireShark,以了解发生了什么。以下是各种过滤器的BPF筛选程序:

1.简单案例:

过滤器:vlan

代码语言:javascript
复制
if the frame is VLAN-tagged then 
  return true 
else 
  return false

过滤器:ip

代码语言:javascript
复制
if the frame is not VLAN-tagged and the protocol is IP then 
  return true 
else 
  return false

过滤器:arp

代码语言:javascript
复制
if the frame is not VLAN-tagged and the protocol is ARP then 
  return true 
else 
  return false

过滤器:rarp

代码语言:javascript
复制
if the frame is not VLAN-tagged and the protocol is RARP then 
  return true 
else 
  return false

过滤器:ip or arp or rarp

代码语言:javascript
复制
if the frame is not VLAN-tagged and the protocol is either IP, ARP or RARP then 
  return true 
else 
  return false

2.将ipvlan相结合的揭示了搜索标记的顺序很重要:

你的第一个过滤器是ip or vlan。其伪码如下:

代码语言:javascript
复制
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,我们希望看到以下内容:

代码语言:javascript
复制
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相同,不是吗?但我们明白了:

代码语言:javascript
复制
(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

这意味着类似于以下伪代码:

代码语言:javascript
复制
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)是不必要的。说明应如下所示:

代码语言:javascript
复制
(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):

代码语言:javascript
复制
(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      #0

3.为什么你的过滤器没有找到ARP和RARP数据包?

你的过滤器是ip or vlan or arp or rarp。该文件汇编为:

代码语言:javascript
复制
(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来避免错误。该文件汇编为:

代码语言:javascript
复制
(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

这意味着:

代码语言:javascript
复制
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 false
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20968983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档