libpcap/tcpdump—3—抓包結論(3 packets captured,3 packets received by filter,0 packets dropped by kernel)

每次在退出tcpdump的時候,終端上都會顯示上圖這樣的3行信息。本篇文章就是想講解這3個數值的相關信息。
我會盡量寫的詳細,但能力有限,核心地方無法點到本質。

這條信息是tcpdump.cinfo(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)系統調用實現。幾十行的代碼上百行的註釋,包含了各種情況詳細的註釋,堪稱典範!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章