Alex_McAvoy

想要成为渔夫的猎手

Sniffer 消息处理函数

【概述】

至此,程序基本完成,只剩下控制控件更新的消息处理函数的编写

其可分为开始按钮、结束按钮、保存按钮、读取按钮、列表更新、列表项颜色变换这六个部分

【开始按钮】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//开始按钮
void CSnifferDlg::OnBnClickedButton1() {
//若已有数据,提示保存数据
if (this->m_localDataList.IsEmpty() == FALSE)
if (MessageBox("确认不保存数据?", "警告", MB_YESNO) == IDNO)
this->Sniffer_saveFile();

//清空数据
this->packetNum = 1; //重新计数
this->m_localDataList.RemoveAll();
this->m_netDataList.RemoveAll();
memset(&(this->packetCount), 0, sizeof(struct packet_count));
this->Sniffer_updatePacket();

if (this->Sniffer_startCap() < 0)
return;
this->m_listCtrl.DeleteAllItems();
this->m_treeCtrl.DeleteAllItems();
this->m_edit.SetWindowText("");
this->m_buttonStart.EnableWindow(FALSE);
this->m_buttonStop.EnableWindow(TRUE);
this->m_buttonSave.EnableWindow(FALSE);
}

【结束按钮】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//结束按钮
void CSnifferDlg::OnBnClickedButton2() {

if (this->m_ThreadHandle == NULL)
return;
if (TerminateThread(this->m_ThreadHandle, -1) == 0) {
MessageBox("线程关闭错误,请稍后重试");
return;
}
this->m_ThreadHandle = NULL;
this->m_buttonStart.EnableWindow(TRUE);
this->m_buttonStop.EnableWindow(FALSE);
this->m_buttonSave.EnableWindow(TRUE);
}

【保存按钮】

1
2
3
4
5
//保存按钮
void CSnifferDlg::OnBnClickedButton3() {
if (this->Sniffer_saveFile() < 0)
return;
}

【读取按钮】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//读取按钮
void CSnifferDlg::OnBnClickedButton4() {

//清空数据
this->m_listCtrl.DeleteAllItems();
this->packetNum = 1;
this->m_localDataList.RemoveAll();
this->m_netDataList.RemoveAll();
memset(&(this->packetCount), 0, sizeof(struct packet_count));

//打开文件对话框
char szFilter[] = "lix文件(*.lix)|*.lix||";
CFileDialog FileDlg(TRUE, ".lix", 0, 0, szFilter);
FileDlg.m_ofn.lpstrInitialDir = "D:\\";
if (FileDlg.DoModal() == IDOK) {
int ret = this->Sniffer_readFile(FileDlg.GetPathName());
if (ret < 0)
return;
}
}

【列表更新】

1
2
3
4
5
6
7
8
9
10
11
//列表更新
void CSnifferDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult) {
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
POSITION pos = m_listCtrl.GetFirstSelectedItemPosition();
int index = m_listCtrl.GetNextSelectedItem(pos); //获取列表控件当前选择的行号
if (index != -1) {
this->Sniffer_updateEdit(index);//更新对应行的编辑框
this->Sniffer_updateTree(index);//更新对应行的树形框
}
*pResult = 0;
}

【列表项颜色变换】

考虑到抓包过程中协议众多,在列表控件中无法直观看出协议,因此考虑将不同协议的包的列表项设置为不同的颜色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//列表项颜色变换
void CSnifferDlg::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult) {
LPNMLVCUSTOMDRAW pNMCD = (LPNMLVCUSTOMDRAW)pNMHDR;
*pResult = 0;

if (CDDS_PREPAINT == pNMCD->nmcd.dwDrawStage)
*pResult = CDRF_NOTIFYITEMDRAW;
else if(CDDS_ITEMPREPAINT == pNMCD->nmcd.dwDrawStage) {
POSITION pos = this->m_localDataList.FindIndex(pNMCD->nmcd.dwItemSpec);
struct data_packet * localData = (struct data_packet *)this->m_localDataList.GetAt(pos);

char buffer[10];
memset(buffer, 0, sizeof(buffer));
strcpy(buffer, localData->type);

COLORREF crText;
if (!strcmp(buffer, "ARP"))
crText = RGB(226, 238, 227);
if (!strcmp(buffer, "IPv4"))
crText = RGB(255, 182, 193);
if (!strcmp(buffer, "IPv6"))
crText = RGB(111, 224, 254);
if(!strcmp(buffer, "UDP"))
crText = RGB(194, 195, 252);
if(!strcmp(buffer, "TCP"))
crText = RGB(230, 230, 230);
if(!strcmp(buffer, "ICMPv4"))
crText = RGB(49, 164, 238);
if(!strcmp(buffer, "ICMPv6"))
crText = RGB(189, 254, 76);
if (!strcmp(buffer, "HTTP"))
crText = RGB(238, 232, 180);

pNMCD->clrTextBk = crText;
*pResult = CDRF_DODEFAULT;
}
}
感谢您对我的支持,让我继续努力分享有用的技术与知识点!