收集並統計網絡流量


      這一節要講述WinPcap中另外一個高級特性:收集並統計網絡流量。統計引擎利用了內核級的數據包過濾器,來有效地為收集到的數據包進行分類。為了使用這個特性,編程人員必須打開一個適配器並將其設置成統計模式。在WinPcap中可以利用pcap_setmode()來達到目的。需要注意的是,必須使用MODE_STAT來作為這個函數的mode參數。在統計模式下,編寫一個用於監聽TCP網絡流量的程序並不復雜,代碼也不多。下面給出一個實例來展示如何實現這個程序。

/*
* Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
* Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Politecnico di Torino, CACE Technologies
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include <stdlib.h>
#include <stdio.h>
#define HAVE_REMOTE
#include <pcap.h>

void usage();

void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);

int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
u_int netmask;
struct bpf_program fcode;

/* 檢查命令行參數的合法性 */
if (argc != 2)
{
usage();
return -1;
}

/* 打開輸出適配器 */
if ( (fp= pcap_open(argv[1], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
return -1;
}

/* 不用關心掩碼,在這個過濾器中,它不會被使用 */
netmask=0xffffff;

// 編譯過濾器
if (pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 )
{
fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
/* 釋放設備列表 */
return -1;
}

//設置過濾器
if (pcap_setfilter(fp, &fcode)<0)
{
fprintf(stderr,"\nError setting the filter.\n");
pcap_close(fp);
/* 釋放設備列表 */
return -1;
}

/* 將接口設置為統計模式 */
if (pcap_setmode(fp, MODE_STAT)<0)
{
fprintf(stderr,"\nError setting the mode.\n");
pcap_close(fp);
/* 釋放設備列表 */
return -1;
}


printf("TCP traffic summary:\n");

/* 開始主循環 */
pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);

pcap_close(fp);
return 0;
}

void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct timeval *old_ts = (struct timeval *)state;
u_int delay;
LARGE_INTEGER Bps,Pps;
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;

/* 以微妙計算上一次采樣的延遲時間 */
/* 這個值通過采樣到的時間戳獲得 */
delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
/* 獲取每秒的比特數b/s */
Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
/* ^ ^
| |
| |
| |
將字節轉換成比特 -- |
|
延時是以微妙表示的 --
*/

/* 得到每秒的數據包數量 */
Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));

/* 將時間戳轉化為可識別的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

/* 打印時間戳*/
printf("%s ", timestr);

/* 打印采樣結果 */
printf("BPS=%I64u ", Bps.QuadPart);
printf("PPS=%I64u\n", Pps.QuadPart);

//存儲當前的時間戳
old_ts->tv_sec=header->ts.tv_sec;
old_ts->tv_usec=header->ts.tv_usec;
}


void usage()
{

printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
printf("\nUsage:\n");
printf("\t tcptop adapter\n");
printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");

exit(0);
}

      在啟用統計模式前,用戶需要設置一個過濾器。如果沒有設置過濾器,那么所有的數據流量多將會被監聽。因此,這個程序的實現過程大致按這樣的流程:設置過濾器->調用pcap_setmode()->回調函數通過pcap_loop()被啟動。接口描述符開始在統計模式下工作。正如我們在前面的章節所提到的,pcap_open()的第四個參數是設置的讀取超時時間read_timeout。回調函數在每一個read_timeout時間,收到由驅動發來的計算好的采樣數據。這些采樣數據會通過回調函數的第2個和第3個參數來進行傳遞,如下圖所示:

      它提供了兩個64位的計數器,分別記錄在最后一個時間間隔內收到的數據包的數量和字節總數。
      在這個程序中,適配器打開后設置的超時時間為1000毫秒,這就意味着dispatcher_handler()每隔一秒就會被調用一次。這里的過濾器設置成為只監聽TCP包。接着,pcap_setmode()和pcap_loop()相繼被調用。注意,一個指向timeval結構的指針,作為user參數傳遞給函數pcap_loop(),這個結構體會被用來存儲時間戳,以便計算兩次采樣的時間間隔。回調函數會使用這個時間間隔來計算每秒的比特數和數據包數量,並將計算的結果輸出到屏幕上。

      最后需要強調的是,這個范例程序比傳統的捕獲和統計流量的程序都要高效,因為傳統的程序都運行在用戶層。而在統計模式下,只需要更小的數據包拷貝和上下文切換,因此CPU的性能會更優,而且內存的需求量也會更小。最后看一下運行的結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM