add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / dox / wpcap_tut9.txt
blobc46737499dd9337b58c45a1ba90a00779dac4cfb
1 /** @ingroup wpcap_tut\r
2  */\r
3 \r
4 /** @defgroup wpcap_tut9 Gathering Statistics on the network traffic\r
5  *  @{\r
6 \r
7 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.\r
8 You can take a look at the \ref NPF if you want to know more details.\r
9 \r
10 In order to use this feature, the programmer must open an adapter and put it in \e statistical \e mode. \r
11 This can be done with pcap_setmode(). In particular, MODE_STAT must be used as the \e mode argument of this function.\r
13 With statistical mode, making an application that monitors the TCP traffic load is a matter of few lines of code. \r
14 The following sample shows how to do it.\r
16 \include tcptop/tcptop.c\r
18 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 \ref language for details. \r
19 If no filter has been set, all of the traffic will be monitored.\r
21 Once\r
23 - the filter is set\r
24 - pcap_setmode() is called\r
25 - callback invocation is enabled with pcap_loop()\r
27 the interface descriptor starts to work in statistical mode.\r
28 Notice the fourth parameter (\e to_ms) of pcap_open(): it defines the interval among the statistical samples. \r
29 The callback function receives the samples calculated by the driver every \e to_ms milliseconds. These samples are encapsulated in the second and third parameters of the callback function, as shown in the following figure:\r
31 \image html stats_wpcap.gif\r
34 Two 64-bit counters are provided: the number of packets and the amount of bytes received during the last interval.\r
36 In the example, the adapter is opened with a timeout of 1000 ms. \r
37 This means that dispatcher_handler() is called once per second.\r
38 At this point a filter that keeps only tcp packets is compiled and set. \r
39 Then pcap_setmode() and pcap_loop() are called. \r
40 Note that a struct timeval pointer is passed to pcap_loop() as the \e user parameter. \r
41 This structure will be used to store a timestamp in order to calculate the interval between two samples.\r
42 dispatcher_handler()uses this interval to obtain the bits per second and the \r
43 packets per second and then prints these values on the screen.\r
45 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.  \r
46 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.\r
48 \ref wpcap_tut8 "<<< Previous"\r
50 @}*/\r