/*
* Copyright (c) 1999, 2000
* Politecnico di Torino. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the Politecnico
* di Torino, and its contributors.'' Neither the name of
* the University 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>
#include "stdafx.h"
#include "netmeter.h"
#include "netmeterDoc.h"
#include "netmeterView.h"
#include "console.h"
#include "selectadapter.h"
#include "..\..\include\pcap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
pcap_t *fp;
pcap_dumper_t *dumpfile;
struct _timeb OldTime;
/////////////////////////////////////////////////////////////////////////////
// CNetmeterView
IMPLEMENT_DYNCREATE(CNetmeterView, CView)
BEGIN_MESSAGE_MAP(CNetmeterView, CView)
//{{AFX_MSG_MAP(CNetmeterView)
ON_COMMAND(seladapter, OnSelAdapter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNetmeterView construction/destruction
CNetmeterView::CNetmeterView()
{
//init the values
time=0;
TimeSlice=1000;
delta=5;
lastval1=0;
lastval2=0;
Adapter="";
BytesCaptured=0;
gridpen.CreatePen(PS_SOLID,0,RGB(0,150,0));
diagrampen1.CreatePen(PS_SOLID,0,RGB(255,255,0));
diagrampen2.CreatePen(PS_SOLID,0,RGB(0,255,255));
InitializeCriticalSection(&Crit);
//begin the capture
CString result;
if ((result=StartCapture())!=""){
AfxMessageBox("Error initializing the capture:\n"+result);
exit(0);
}
}
CNetmeterView::~CNetmeterView()
{
//stop the capture and close libpcap
StopCapture();
}
BOOL CNetmeterView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// function that starts the capture
CString CNetmeterView::StartCapture()
{
char *ebuf;
char *myAdapter;
CCapPars *pars=new CCapPars;
pars->prg=this;
ebuf=(char*)malloc(PCAP_ERRBUF_SIZE);
if (Adapter==""){
//If no adapter is defined choose the first
myAdapter=(char*)pcap_lookupdev(ebuf);
DWORD dwVersion=GetVersion(); //get the OS version
DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)// Windows '95
Adapter=myAdapter;
else //winNT
Adapter=(WCHAR *)myAdapter;
//open the adapter
//note: the snaplen is 1 byte because we don't need the data
//and the header of the packet, but only the length.
//Snaplen is not set to 0 because with this value the filter
//drops all the packet without copying them. Snaplen=0 can be
//used to count the packets with the minimun overhead on the system.
if ( (fp= pcap_open_live((char*)myAdapter, 1, 1, 1000, ebuf) ) == NULL)
{
return "PCAP error: Error opening the adapter";
}
}
else{
if ( (fp= pcap_open_live(Adapter.GetBuffer(0), 1, 1, 1000, ebuf) ) == NULL)
{
return "PCAP error: Error opening the adapter";
}
}
//this example shows a correct diagram only on 10Mbit Ethernets
if(pcap_datalink(fp)!=1/*Ether10*/){
AfxMessageBox("Warning: the nework adapter '"+Adapter+"' is not supported correctly by netmeter.\nNetmeter works correcly only with 10Mbit ethernet adapters.");
}
//set mode 1 (statistics mode).
//When set in this mode, the capture driver DOES'T capture anything.
//It only counts the packets and the number of bytes that satisfy the BPF filter,
//and returns to the application this values every Mon.delay milliseconds.
if(pcap_setmode(fp,MODE_STAT)==-1){
pcap_close(fp);
return "PCAP error setting count mode";
}
//set a 0 Kb driver buffer
//We don't need any kernel buffer, because the count operations
//are made in the driver without using memory
if(pcap_setbuff(fp,0)<0){
pcap_close(fp);
return "Not enough memory to allocate the capture buffer\nTry to set a smaller buffer.";
}
pars->fp=fp;
//start the capture thread
thr=AfxBeginThread(MyThreadProc, pars);
if(!thr) {
pcap_close(fp);
return "Error launching the capture thread.";
}
//get the time of the beginning of the capture
_ftime( &OldTime );
return "";
}
/////////////////////////////////////////////////////////////////////////////
// function that stops the capture
void CNetmeterView::StopCapture()
{
HANDLE thread=thr->m_hThread;
//kill the capture thread.
BOOL res=TerminateThread(thread,0);
if(res==false){
AfxMessageBox("Fatal Error: cannot terminate the capture thread");
exit(0);
}
//close the adapter
pcap_close(fp);
//wait the end of the capture thread
WaitForSingleObject(thread,INFINITE);
}
/////////////////////////////////////////////////////////////////////////////
// Create a new blackboard
void CNetmeterView::CreateBoard(CDC* pDC,CDC *DrawBuff,RECT rett)
{
int i;
CDC dc;
DrawBuff->CreateCompatibleDC(pDC);
hBitmap=CreateCompatibleBitmap(*pDC,rett.right-rett.left,rett.bottom-rett.top);
DrawBuff->SelectObject(hBitmap);
DrawBuff->FillSolidRect(0,0,rett.right-rett.left,rett.bottom-rett.top,0);
DrawBuff->SelectObject(gridpen);
for(i=1;i<10;i++){
DrawBuff->MoveTo(0,(rett.bottom-rett.top)*i/10);
DrawBuff->LineTo(rett.right-rett.left,(rett.bottom-rett.top)*i/10);
}
for(i=1;i<=10;i++){
DrawBuff->MoveTo((rett.right-rett.left)*i/10-1,0);
DrawBuff->LineTo((rett.right-rett.left)*i/10-1,rett.bottom-rett.top);
}
DrawBuff->LineTo(rett.right-rett.left,rett.bottom-rett.top);
DrawBoard(DrawBuff,rett,0,0);
pDC->BitBlt( rett.left, rett.top, rett.right-rett.left, rett.bottom-rett.top, DrawBuff, 0, 0, SRCCOPY);
}
/////////////////////////////////////////////////////////////////////////////
// Scroll the blackboard and insert a new value
void CNetmeterView::DrawBoard(CDC* pDC,RECT rett,int height1,int height2)
{
int i;
int x,y;
pDC->SelectObject(gridpen);
pDC->BitBlt( 0, 0, rett.right-rett.left, rett.bottom-rett.top, pDC, delta, 0, SRCCOPY);
pDC->FillSolidRect(rett.right-delta,0,rett.right,rett.bottom-rett.top,0);
for(i=1;i<10;i++){
pDC->MoveTo(rett.right-rett.left-delta,(rett.bottom-rett.top)*i/10);
pDC->LineTo(rett.right-rett.left,(rett.bottom-rett.top)*i/10);
}
if(((time)*delta)/((rett.right-rett.left)/10) > ((time-1)*delta)/((rett.right-rett.left)/10))
{
pDC->MoveTo((rett.right-rett.left)*i/10-1,0);
pDC->LineTo((rett.right-rett.left)*i/10-1,rett.bottom-rett.top);
}
// draw the line for the new PPS
pDC->SelectObject(diagrampen2);
x=rett.right-rett.left-delta-1;
y=rett.bottom-rett.top-lastval2*(rett.bottom-rett.top)/100-1;
pDC->MoveTo(x,y);
pDC->SetPixelV(x,y,RGB(255,255,255));
x=rett.right-rett.left-1;
y=rett.bottom-rett.top-height2*(rett.bottom-rett.top)/100-1;
pDC->LineTo(x,y);
pDC->SetPixelV(x,y,RGB(255,255,255));
lastval2=height2;
// draw the line for the new BPS
pDC->SelectObject(diagrampen1);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
winpcap案例源代码 (626个子文件)
libwpcap.a 37KB
libpacket.a 20KB
Packet.a 16KB
listbmp.bmp 1KB
cap_wizi.bmp 598B
bitmap2.bmp 214B
BMP00001.BMP 214B
bitmap1.bmp 214B
Testapp.c 7KB
Tg.c 6KB
udpdump.c 6KB
tcptop.c 4KB
pcap_filter.c 4KB
sendcap.c 4KB
pktdump_ex.c 4KB
smp_1.c 4KB
kdump.c 4KB
iflist.c 3KB
savedump.c 3KB
basic_dump_ex.c 2KB
basic_dump.c 2KB
readfile.c 1KB
readfile_ex.c 1002B
sendpack.c 971B
netmeterView.cpp 11KB
SelectAdapter.cpp 7KB
NETMETER.CPP 4KB
CaptureThread.cpp 3KB
netmeterDoc.cpp 2KB
LineColl.cpp 2KB
MainFrm.cpp 2KB
StdAfx.cpp 1KB
style.css 8KB
style.css 8KB
doc-style.css 2KB
tabs.css 2KB
tabs.css 2KB
netmeter.dsp 6KB
UDPdump.dsp 4KB
pcap_filter.dsp 4KB
iflist.dsp 4KB
pktdump_ex.dsp 4KB
TG.DSP 4KB
sendcap.dsp 4KB
tcptop.dsp 4KB
kdump.dsp 4KB
TestApp.dsp 4KB
smp_1.dsp 4KB
pcap_filter.dsw 545B
pktdump_ex.dsw 543B
netmeter.dsw 539B
UDPdump.dsw 537B
TestApp.dsw 537B
sencap.dsw 537B
iflist.dsw 535B
tcptop.dsw 535B
smp_1.dsw 533B
kdump.dsw 533B
TG.DSW 527B
WinPcap_4_0_2.exe 538KB
NETMETER.EXE 44KB
smp_1.exe 44KB
TG.EXE 44KB
UDPdump.exe 40KB
tcptop.exe 36KB
TestApp.exe 36KB
pktdump_ex.exe 32KB
kdump.exe 32KB
sendcap.exe 32KB
iflist.exe 28KB
pf.exe 28KB
winpcap.gif 10KB
winpcap.gif 10KB
winpcap.gif 10KB
npf.gif 8KB
npf-npf.gif 8KB
npf-npf.gif 8KB
dump.gif 6KB
dump.gif 6KB
dump.gif 6KB
stats.gif 5KB
stats.gif 5KB
stats.gif 5KB
stats_wpcap.gif 5KB
stats_wpcap.gif 5KB
stats_wpcap.gif 5KB
npf_dump.gif 5KB
npf-dump.gif 5KB
npf-dump.gif 5KB
arch.gif 5KB
internals-arch.gif 5KB
internals-arch.gif 5KB
ndis.gif 3KB
npf-ndis.gif 3KB
npf-ndis.gif 3KB
encoding.gif 3KB
encoding.gif 3KB
encoding.gif 3KB
tab_r.gif 3KB
tab_r.gif 3KB
共 626 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- xyxlyq2012-11-01我需要的是案例,这明明是安装包,。,。
- allen91722012-09-20明明是安装包嘛,哪有案例?
tp135bai
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功