循序渐进学习使用WINPCAP(九)

来源:互联网 发布:数据库账号密码 编辑:程序博客网 时间:2024/05/20 08:01

收集统计网络流量

这一章将展示WinPcap的另一项高级功能:收集网络流量的统计信息。WinPcap的统计引擎在内核层级上对到来的数据进行分类。如果你想了解更多的细节请,可以查看NPF驱动指南。

为了利用这个功能来监视网络,程序必须打开一个适配器并用pcap_setmode()将其设置为统计模式。注意pcap_setmode()要用MODE_STAT来将网卡设置为统计模式。

在统计模式下,编写一个程序来监测TCP流量只是几行代码的事情。下面的例子说明了如何来实现该功能。

在设置为统计模式之前,用户可以选择设置一个过滤器来指定要监测的网络流量的子集。细节可见过滤器表达式语法一节。如果没有设置过滤器,所有的流量都将被监测。

一旦:

  1. 过滤器被设置
  2. pcap_setmode()被调用
  3. 用pcap_loop()循环调用

网卡的描述者开始在统计模式下工作。需要指出的是pcap_open()的第四个参数(to_ms):它定义了采样的间隔。回调函数每隔一定的时间就获取一次采样统计。这些采样被封装入回调函数的第二和第三个参数,如下图所示:

stats_wpcap

两个64位的计数器分别记录最近一次间隔数据报数量和字节数量。

本例子中,网卡打开时设置超时为1000 ms。也就是说dispatcher_handler()每隔1秒就会被调用一次。此时,过滤器也被编译并设置为只接受TCP包。然后pcap_setmode()和pcap_loop()被调用。注意一个指向timeval结构的指针作为用户参数被传递给pcap_loop()。这个结构将用来存储一个时间戳以计算两次采样的时间间隔。dispatch_handler()用这个时间间隔得到每秒的比特数和数据报数,并且将这两个数据打印在屏幕上。

最后指出的是目前的这个例子是比任何一个利用传统方法捕获包并在用户层进行统计的程序都高效。统计模式需要最小数量的数据拷贝和上下文切换,因此CPU的利用达到最优。另外,内存也只需很少一部分。

附原文:

This lesson shows another advanced feature of WinPcap: the ability to collect statistics about network traffic. The statistical engine makes use of the kernel-level packet filter to efficiently classify the incoming packet. You can take a look at the NPF driver internals manual if you want to know more details.

In order to use this feature, the programmer must open an adapter and put it in statistical mode. This can be done with pcap_setmode(). In particular, MODE_STAT must be used as the mode argument of this function.

With statistical mode, making an application that monitors the TCP traffic load is a matter of few lines of code. The following sample shows how to do it.

/* codes */

Before enabling statistical mode, the user has the option to set a filter that defines the subset of network traffic that will be monitored. See the paragraph on the Filtering expression syntax for details. If no filter has been set, all of the traffic will be monitored.

Once

  1. the filter is set
  2. pcap_setmode() is called
  3. callback invocation is enabled with pcap_loop()

the interface descriptor starts to work in statistical mode. Notice the fourth parameter (to_ms) of pcap_open(): it defines the interval among the statistical samples. The callback function receives the samples calculated by the driver every to_ms milliseconds. These samples are encapsulated in the second and third parameters of the callback function, as shown in the following figure:

figure is above

Two 64-bit counters are provided: the number of packets and the amount of bytes received during the last interval.

In the example, the adapter is opened with a timeout of 1000 ms. This means that dispatcher_handler() is called once per second. At this point a filter that keeps only tcp packets is compiled and set. Then pcap_setmode() and pcap_loop() are called. Note that a struct timeval pointer is passed to pcap_loop() as the user parameter. This structure will be used to store a timestamp in order to calculate the interval between two samples. dispatcher_handler()uses this interval to obtain the bits per second and the packets per second and then prints these values on the screen.

Note finally that this example is by far more efficient than a program that captures the packets in the traditional way and calculates statistics at user-level. Statistical mode requires the minumum amount of data copies and context switches and therefore the CPU is optimized. Moreover, a very small amount of memory is required.

#end

原创粉丝点击