📄 stcview.cpp
字号:
// StcView.cpp : implementation file
//
#include "stdafx.h"
#include "SnifferPro.h"
#include "StcView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStcView
IMPLEMENT_DYNCREATE(CStcView, CFormView)
CStcView::CStcView()
: CFormView(CStcView::IDD)
{
//{{AFX_DATA_INIT(CStcView)
buf=NULL;
buflen=0;
isReassembly=FALSE;
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CStcView::~CStcView()
{
}
void CStcView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStcView)
DDX_Control(pDX, IDC_STC_LIST, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStcView, CFormView)
//{{AFX_MSG_MAP(CStcView)
ON_MESSAGE(WM_MESSAGE_PACKET_SELECT,OnPacketSelect)
ON_MESSAGE(WM_MESSAGE_PACKET_SPECIFY,OnPacketSpecify)
ON_MESSAGE(WM_MESSAGE_PACKET_REASSEMBLY,OnPacketReassembly)
ON_COMMAND(ID_FILE_REASSEMBLY, OnFileReassembly)
ON_UPDATE_COMMAND_UI(ID_FILE_REASSEMBLY, OnUpdateFileReassembly)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStcView diagnostics
#ifdef _DEBUG
void CStcView::AssertValid() const
{
CFormView::AssertValid();
}
void CStcView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStcView message handlers
void CStcView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
((CMainFrame *)AfxGetApp()->GetMainWnd())->stcView=this;
// TODO: Add your specialized code here and/or call the base class
CListCtrl &ctrl=this->m_list;
//初始化
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 CStcView::OnFileReassembly()
{
// TODO: Add your command handler code here
}
void CStcView::OnUpdateFileReassembly(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
}
void CStcView::OnPacketSelect(const struct pcap_pkthdr *pkt_header,const u_char *pkt_data)
{
//清空上次内容
if(buf!=NULL){
delete[] buf;
buf=NULL;
buflen=0;
}
int len;//总长(字节)
int indexRow;//当前行号
int indexColumn;//当前列号
int nRow;//总行数
int nColumn;//总列数
int current;//当前位置
int type;//类型:IP,ARP,RARP
char strAddr[5];//每行字节起始地址
char strByte[3];//每个字节的十六进制表示
char strContext[20];//每16个字节作为字符串输出
unsigned char *pos;//指向当前位置
//Init
CListCtrl &ctrl=this->m_list;
this->isReassembly=FALSE;
pos=(unsigned char *)(pkt_data+12);
type=(*pos)*0x100+(*(pos+1));
if(type==0x0800){//IP
pos=(unsigned char *)(pkt_data+16);
len=(*pos)*0x100+(*(pos+1))+14;
}
else{//ARP of RARP
len=28+14;
}
buf=new unsigned char[len];
memcpy(buf,pkt_data,len);
buflen=len;
nRow=len/16;
if(len%16>0)
nRow++;
nColumn=18;
//Init Column Position
ctrl.DeleteAllItems();
m_list.totallen=buflen;
m_list.index=0;
for(indexRow=0,current=0;indexRow<nRow;indexRow++){
sprintf(strAddr,"%0004X:",current);//显示段偏移
ctrl.InsertItem(indexRow,"",0);
ctrl.SetItemText(indexRow,0,strAddr);
//Init Column 0~F
strcpy(strContext,"");
for(indexColumn=1,pos=buf+16*indexRow;indexColumn<=16 && indexRow*16+indexColumn<=len;indexColumn++){
sprintf(strByte,"%02XF",*pos);
ctrl.SetItemText(indexRow,pos-(buf+16*indexRow)+1,strByte);//设置相应字节显示的内容,buf数据包基址,16*index为当前行偏移量,pos当前字节,则pos-(buf+16*indexRow)+1为列号
sprintf(strByte,"%c",*pos);
strcat(strContext,strByte);//将一行16字节连成字符串
//m_list.flags.Add(FALSE);
pos++;
}
ctrl.SetItemText(indexRow,17,strContext);
current+=16;//当前位置增加16,到下一行
}
}
void CStcView::OnPacketSpecify(int start,int len)//显示报文某字段对应位置,使其高亮
{
int indexRow;
int indexColumn;
char str[5];
if(this->isReassembly){
MessageBox("处于重组状态,不予显示报文对应字段!");
return;
}
//将原来高亮显示内容取消
m_list.index=0;
indexRow=0;
indexColumn=1;
for(int i=0;i<(int)buflen;i++){
this->m_list.GetItemText(indexRow,indexColumn,str,10);
str[2]='F';//标志位,表示该SubItem不高亮(False)
str[3]=0;
this->m_list.SetItemText(indexRow,indexColumn,str);
if(indexColumn==16){//列号超16则进入下一行,列号回0,行号加1
indexColumn=1;
indexRow++;
}
else
indexColumn++;
}
//设置新的高亮内容
indexRow=start/16;
indexColumn=start%16+1;
for(i=0;i<len;i++){//从start开始的len个字节设为高亮
this->m_list.GetItemText(indexRow,indexColumn,str,10);
str[2]='T';
str[3]=0;
this->m_list.SetItemText(indexRow,indexColumn,str);
if(indexColumn==16){//列号超16则进入下一行,列号回0,行号加1
indexColumn=1;
indexRow++;
}
else
indexColumn++;
}
}
void CStcView::OnPacketReassembly()
{
//清空上次内容
if(buf!=NULL){
delete[] buf;
buf=NULL;
buflen=0;
}
int indexRow;//当前行号
int indexColumn;//当前列号
int nRow;//总行数
int nColumn;//总列数
int current;//当前位置
int count;//定位器数量
char strAddr[5];//每行字节起始地址
char strByte[3];//每个字节的十六进制表示
char strContext[20];//每16个字节作为字符串输出
int total=0;
unsigned char *pos;
int index=0;
CMyListCtrl &ctrl=this->m_list;
this->isReassembly=TRUE;
count=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetSize();
for(int i=0;i<count;i++){
total+=((CMainFrame *)AfxGetApp()->GetMainWnd())->founders.GetAt(i).len;
}
buf=new unsigned char[total];
buflen=total;
m_list.totallen=buflen;
m_list.index=0;
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,"%02XF",*pos);
ctrl.SetItemText(indexRow,pos-(buf+16*indexRow)+1,strByte);//设置相应字节显示的内容,buf数据包基址,16*index为当前行偏移量,pos当前字节,则pos-(buf+16*indexRow)+1为列号
sprintf(strByte,"%c",*pos);
strcat(strContext,strByte);//将一行16字节连成字符串
pos++;
}
ctrl.SetItemText(indexRow,17,strContext);
current+=16;
}
}//if total<100K
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -