收集并统计网络流量

      这一节要讲述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的性能会更优,而且内存的需求量也会更小。最后看一下运行的结果:

转载于:https://www.cnblogs.com/blacksword/archive/2012/03/19/2405573.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值