add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / winpcap / Examples / NETMETER / CaptureThread.cpp
blob4d857183734de254c5166745c8d9963e06a615a5
1 /*
2 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
3 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino, CACE Technologies
16 * nor the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "stdafx.h"
35 #include "resource.h"
36 #include "capdll.h"
37 #include "console.h"
39 HANDLE in,out,err;
41 DWORD d;
42 CCapPars* pObject;
43 CRITICAL_SECTION Crit;
45 // callback routine called by libpcap for every incoming packet
46 void dispatcher_handler(u_char *pParam,const struct pcap_pkthdr *header, const u_char *pkt_data)
48 UINT delay;
49 LARGE_INTEGER Bps,Pps;
50 CCapPars* pObject;
52 pObject=(CCapPars*)pParam;
53 //Calculate the delay in microseconds from the last sample.
54 //This value is obtained from the timestamp that the capture driver
55 //associates to the sample.
56 delay=(header->ts.tv_sec-pObject->lasttime.tv_sec)*1000000-pObject->lasttime.tv_usec+header->ts.tv_usec;
57 //get the number of Bits per second
58 Bps.QuadPart=(((LONGLONG)(*(LONGLONG*)(pkt_data+8))*80)/(delay));
59 //get the number of Packets per second
60 Pps.QuadPart=(((LONGLONG)(*(LONGLONG*)(pkt_data))*100000000)/((LONGLONG)delay*14880));
62 //store current timestamp
63 pObject->lasttime.tv_sec=header->ts.tv_sec;
64 pObject->lasttime.tv_usec=header->ts.tv_usec;
66 pObject->prg->DrawBoard(&(pObject->prg->DrawBuffer),pObject->prg->wrett,Bps.LowPart,Pps.LowPart);
69 //main thread procedure: launches the capture and wait
70 UINT MyThreadProc( LPVOID pParam )
72 int i;
74 if (pParam == NULL)
75 return -1; // illegal parameter
76 pObject=(CCapPars*)pParam;
78 //reset the timer
79 pObject->lasttime.tv_sec=0;
80 pObject->lasttime.tv_usec=0;
82 //start the capture loop
83 i = pcap_loop(pObject->fp, 0, dispatcher_handler, (PUCHAR)pParam);
85 Sleep(INFINITE);
87 return 0;