每次在退出tcpdump的时候,终端上都会显示上图这样的3行信息。本篇文章就是想讲解这3个数值的相关信息。
我会尽量写的详细,但能力有限,核心地方无法点到本质。
这条信息是tcpdump.c中info(register int verbose)接口打印的信息。部分代码如下所示:
其中packets_captured表示捕获到的包的数量,如果指定了-w选项也就是写入到文件中的包数,下一篇文章会详细
介绍。stats的类型是struct pcap_stat,在libpcap中的pcap.h文件中定义,包含3个成员,如下图所示。
/*
* As returned by the pcap_stats()
*/
struct pcap_stat {
u_int ps_recv; /* number of packets received */
u_int ps_drop; /* number of packets dropped */
u_int ps_ifdrop; /* drops by interface -- only supported on some platforms */
};
static u_int packets_captured
packets_captured是在tcpdump.c中定义的一个static的全局unsigned int变量,在dump_packet接口中递增。(写
文件也是在这里)
struct pcap_stat stats
info()中调用pcap_stats()获取到stats的值。pcap.c中定义pcap_stats()指向pcap-linux.c中的pcap_stats_linux(),在里面你会发现大部分系统是通过getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, &kstats, &len)系统调用实现。几十行的代码上百行的注释,包含了各种情况详细的注释,堪称典范!