【发布时间】:2012-02-26 18:48:24
【问题描述】:
我正在编写网络分析器,我需要过滤保存在文件中的数据包,我已经编写了一些代码来过滤 http 数据包,但我不确定它是否可以正常工作,因为当我在 pcap 转储上使用我的代码时结果是 5 个数据包,但在 Wireshark 中,在过滤器中写入 http 会给我 2 个数据包,如果我使用:
tcpdump port http -r trace-1.pcap
它给了我 11 个数据包。
嗯,3 个不同的结果,这有点令人困惑。
me代码中的过滤器和数据包处理是:
...
if (pcap_compile(handle, &fcode, "tcp port 80", 1, netmask) < 0)
...
while ((packet = pcap_next(handle,&header))) {
u_char *pkt_ptr = (u_char *)packet;
//parse the first (ethernet) header, grabbing the type field
int ether_type = ((int)(pkt_ptr[12]) << 8) | (int)pkt_ptr[13];
int ether_offset = 0;
if (ether_type == ETHER_TYPE_IP) // ethernet II
ether_offset = 14;
else if (ether_type == ETHER_TYPE_8021Q) // 802
ether_offset = 18;
else
fprintf(stderr, "Unknown ethernet type, %04X, skipping...\n", ether_type);
//parse the IP header
pkt_ptr += ether_offset; //skip past the Ethernet II header
struct ip_header *ip_hdr = (struct ip_header *)pkt_ptr;
int packet_length = ntohs(ip_hdr->tlen);
printf("\n%d - packet length: %d, and the capture lenght: %d\n", cnt++,packet_length, header.caplen);
}
我的问题是为什么过滤http时有3种不同的结果?和/或者如果我过滤错了,那么我该如何正确处理,还有没有办法使用端口号以外的其他方式过滤 http(或 ssh、ftp、telnet ...)数据包?
谢谢
【问题讨论】:
-
你比较了差异还是结果?或者能否附上pcap文件链接,不然很难说原因。