当我用
创建一个数据包过滤器(例如只针对tcp流量)tcpdump -dd tcp
包过滤器的输出是
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 2, 0x000086dd },
{ 0x30, 0, 0, 0x00000014 },
{ 0x15, 3, 4, 0x00000006 },
{ 0x15, 0, 3, 0x00000800 },
{ 0x30, 0, 0, 0x00000017 },
{ 0x15, 0, 1, 0x00000006 },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
但是当我以编程方式执行相同操作时;
pcap_compile_nopcap(1500, DLT_EN10MB, &fcode, "tcp", 1, 0);
struct bpf_insn *insn = fcode.bf_insns;
for (i = 0; i < fcode.bf_len; ++insn, ++i)
{
printf("{ 0x%x, %d, %d, 0x%08x },\n",
insn->code, insn->jt, insn->jf, insn->k);
}
我得到以下数据包过滤器输出:
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 5, 0x000086dd },
{ 0x30, 0, 0, 0x00000014 },
{ 0x15, 6, 0, 0x00000006 },
{ 0x15, 0, 6, 0x0000002c },
{ 0x30, 0, 0, 0x00000036 },
{ 0x15, 3, 4, 0x00000006 },
{ 0x15, 0, 3, 0x00000800 },
{ 0x30, 0, 0, 0x00000017 },
{ 0x15, 0, 1, 0x00000006 },
{ 0x6, 0, 0, 0x000005dc },
{ 0x6, 0, 0, 0x00000000 },
为什么这两个数据包过滤器不同?
最佳答案
可能是因为您系统上的 tcpdump 是使用比您的程序更旧的 libpcap 版本构建的。您系统上的 tcpdump 可能使用的是 libpcap 而没有进行此更改:
commit 58275c05a5cf9c3512bcbb1192ff351d32ccccbd
Author: Guy Harris <guy@alum.mit.edu>
Date: Thu Sep 1 22:21:45 2011 -0700
Handle some amount of IPv6 fragmentation.
If we're checking for a particular protocol running on top of IPv6, and
we're not doing full protocol-chain chasing for all "running on top of
IPv6" tests, at least check for a fragmentation header before the header
for the protocol.
并且您的程序可能正在使用具有该更改的 libpcap。该更改在 libpcap 1.3.x 时间范围内的某个地方进入了 libpcap。
关于linux - tcpdump -dd 输出与 pcap_compile_nopcap 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13496574/