libpcap-从所有接口捕获数据包 [英] libpcap - capture packets from all interfaces

查看:99
本文介绍了libpcap-从所有接口捕获数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Linux机器上的所有网络接口捕获数据包.为此,我打算使用 pcap_open_live() API并将"any"作为设备参数传递.

I need to capture packets from all network interfaces on Linux machine. In order to do it I'm planning to use pcap_open_live() API and pass "any" as a device argument.

我有不同类型的端口:以太网端口(例如eth0)和GRE隧道(例如tun0)来自不同类型接口的数据包具有不同的标头格式:

I have different types of ports: Ethernet ports (say eth0) and GRE tunnels (say tun0) The packets that coming from different types of interfaces has different headers format:

  1. 来自以太网端口的数据包具有MAC报头
  2. 来自隧道的数据包带有Linux煮熟的"捕获封装(16字节)标头

如何查看 pcap_loop()回调处理程序我得到了哪种类型的数据包头?

How can I check into pcap_loop() callback handler what type of packet header I got?

推荐答案

您收到的所有数据包都具有相同类型的数据包头;这是在pcap_t上调用 pcap_datalink()时得到的类型. pcap_datalink()返回的值是 DLT _ 值,如链接层标题类型页面.

All packets you receive get the same type of packet header; that's the type you get when you call pcap_datalink() on the pcap_t. The values that pcap_datalink() returns are the DLT_ values as shown in the Link-Layer Header Types page on the tcpdump.org site.

如果您打开了 any 设备,则 pcap_datalink()将返回 DLT_LINUX_SLL ,表示 ALL 您捕获的数据包将具有煮熟的"捕获标头-甚至是 eth0 中的那些标头!您必须在 eth0 上捕获而不是在 any 上捕获这些数据包的以太网标头.

If you've opened the any device, pcap_datalink() will return DLT_LINUX_SLL, meaning that ALL packets you capture will have the "cooked" capture header - even the ones from eth0! You'd have to capture on eth0, rather than any, to get Ethernet headers for those packets.

这篇关于libpcap-从所有接口捕获数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆