⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contentview.cpp

📁 这是一个嗅探器
💻 CPP
字号:
// ContentView.cpp : implementation file
//

#include "stdafx.h"
#include "snifferpro.h"
#include "ContentView.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CContentView

IMPLEMENT_DYNCREATE(CContentView, CListView)

CContentView::CContentView()
{
	buf=NULL;
	buflen=0;
}

CContentView::~CContentView()
{
}


BEGIN_MESSAGE_MAP(CContentView, CListView)
	//{{AFX_MSG_MAP(CContentView)
	ON_MESSAGE(WM_MESSAGE_PACKET_SPECIFY,OnPacketSpecify)
	ON_MESSAGE(WM_MESSAGE_PACKET_SELECT,OnPacketSelect)
	ON_MESSAGE(WM_MESSAGE_PACKET_REASSEMBLY,OnPacketReassembly)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContentView drawing

void CContentView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CContentView diagnostics

#ifdef _DEBUG
void CContentView::AssertValid() const
{
	CListView::AssertValid();
}

void CContentView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CContentView message handlers

void CContentView::OnInitialUpdate() 
{
	CListView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	((CMainFrame *)AfxGetApp()->GetMainWnd())->conView=this;
	CListCtrl &ctrl=this->GetListCtrl();

	DWORD log = GetWindowLong(ctrl.GetSafeHwnd(),GWL_STYLE);
	log |= LVS_REPORT|LVS_SINGLESEL|LVS_SHOWSELALWAYS;
	SetWindowLong(ctrl.GetSafeHwnd(),GWL_STYLE,log);
	ctrl.InsertColumn(0,"Address",LVCFMT_LEFT,60);
	ctrl.InsertColumn(1,"0",LVCFMT_LEFT,30);
	ctrl.InsertColumn(2,"1",LVCFMT_LEFT,30);
	ctrl.InsertColumn(3,"2",LVCFMT_LEFT,30);
	ctrl.InsertColumn(4,"3",LVCFMT_LEFT,30);
	ctrl.InsertColumn(5,"4",LVCFMT_LEFT,30);
	ctrl.InsertColumn(6,"5",LVCFMT_LEFT,30);
	ctrl.InsertColumn(7,"6",LVCFMT_LEFT,30);
	ctrl.InsertColumn(8,"7",LVCFMT_LEFT,30);
	ctrl.InsertColumn(9,"8",LVCFMT_LEFT,30);
	ctrl.InsertColumn(10,"9",LVCFMT_LEFT,30);
	ctrl.InsertColumn(11,"A",LVCFMT_LEFT,30);
	ctrl.InsertColumn(12,"B",LVCFMT_LEFT,30);
	ctrl.InsertColumn(13,"C",LVCFMT_LEFT,30);
	ctrl.InsertColumn(14,"D",LVCFMT_LEFT,30);
	ctrl.InsertColumn(15,"E",LVCFMT_LEFT,30);
	ctrl.InsertColumn(16,"F",LVCFMT_LEFT,30);
	ctrl.InsertColumn(17,"Context",LVCFMT_LEFT,150);
}

void CContentView::OnPacketSelect(const struct pcap_pkthdr *pkt_header,const u_char *pkt_data)
{
	if(buf!=NULL){
		delete[] buf;
		buf=NULL;
		buflen=0;
	}
	int len=pkt_header->len;
	int indexRow;
	int indexColumn;
	int nRow;
	int nColumn;
	int current;
	char strAddr[5];
	char strByte[4];
	char strContext[20];
	unsigned char *pos;
	//unsigned char *buf;
	CListCtrl &ctrl=this->GetListCtrl();
 
	
	buf=new unsigned char[pkt_header->len];
	memcpy(buf,pkt_data,pkt_header->len);
	buflen=pkt_header->len;

	nRow=len/16;
	if(len%16>0)
		nRow++;
	nColumn=18;
	//Init Column Position
	ctrl.DeleteAllItems();
	for(indexRow=0,current=0;indexRow<nRow;indexRow++){
		sprintf(strAddr,"%0004X:",current);
		ctrl.InsertItem(indexRow,strAddr,0);
		//Init Column 0~F
		strcpy(strContext,"");
		for(indexColumn=1,pos=buf+16*indexRow;indexColumn<=16 && indexRow*16+indexColumn<=len;indexColumn++){
			sprintf(strByte,"%02X",*pos);
			//strcat(strByte,"F");
			ctrl.SetItemText(indexRow,pos-(buf+16*indexRow)+1,strByte);
			sprintf(strByte,"%c",*pos);
			strcat(strContext,strByte);
			pos++;
		}
		ctrl.SetItemText(indexRow,17,strContext);
		current+=16;
	}
	//delete[] buf;
}

void CContentView::OnPacketSpecify(unsigned int start,unsigned int len)
{
	int indexRow;
	int indexColumn;
	CListCtrl &ctrl=this->GetListCtrl();
	char str[10];
	sprintf(str,"%d,%d",start,len);
	MessageBox(str);
	indexRow=start/16;
	indexColumn=start%16+1;
	//BOOL SetItemState( int nItem, LVITEM* pItem ); 
	UINT mask=ctrl.GetItemState(indexRow,LVIS_SELECTED);
	ctrl.SetItemState(indexRow,LVIS_SELECTED,mask);
}

void CContentView::OnPacketReassembly()
{
	if(buf!=NULL){
		delete[] buf;
		buf=NULL;
		buflen=0;
	}
	
	int count=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetSize();
	int total=0;
	//unsigned char *buf;
	unsigned char *pos;
	int index=0;
	int indexRow;
	int indexColumn;
	int nRow;
	int nColumn;
	int current;
	char strAddr[5];
	char strByte[3];
	char strContext[20];
	CListCtrl &ctrl=this->GetListCtrl();


	for(int i=0;i<count;i++){
		total+=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetAt(i).len;
	}
	buf=new unsigned char[total];
	buflen=total;
	for(i=0;i<count;i++){
		int position=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetAt(i).index;
		int len=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetAt(i).len;
		int start=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetAt(i).start;
		memcpy(buf+index,(unsigned char *)(((CMainFrame *)AfxGetApp()->GetMainWnd())->mulPackView->pkt_datas.GetAt(position)+start),len);
		index+=len;
	}

	nRow=total/16;
	if(total%16>0)
		nRow++;
	nColumn=18;
	//Init Column Position
	if(total<102400){//total<100K
	ctrl.DeleteAllItems();
	for(indexRow=0,current=0;indexRow<nRow;indexRow++){
		sprintf(strAddr,"%0004X:",current);
		ctrl.InsertItem(indexRow,strAddr,0);
		//Init Column 0~F
		strcpy(strContext,"");
		for(indexColumn=1,pos=buf+16*indexRow;indexColumn<=16;indexColumn++){
			sprintf(strByte,"%02X",*pos);
			ctrl.SetItemText(indexRow,pos-(buf+16*indexRow)+1,strByte);
			sprintf(strByte,"%c",*pos);
			strcat(strContext,strByte);
			pos++;
		}
		ctrl.SetItemText(indexRow,17,strContext);
		current+=16;
	}
	}//if total<100K
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -