-
Notifications
You must be signed in to change notification settings - Fork 1
/
kendoUIDlg.cpp
3020 lines (2594 loc) · 86.5 KB
/
kendoUIDlg.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// kendoUIDlg.cpp : implementation file
//
#include "stdafx.h"
#include "kendoUI.h"
#include "kendoUIDlg.h"
#include "ThreadParam.h"
#include "Global.h"
#include "PacketCatcher.h"
#include <vector>
#include "ShortCutDialog.h"
#define HAVE_REMOTE
#include "pcap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma comment(lib, "version.lib") // CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
BOOL OnInitDialog();
//void OnShowWindow(BOOL bShow, UINT nStatus);
//CString CAboutDlg::GetAppVersion(CString *AppName);
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult);
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
/*Get program path*/
WCHAR l_wcaAppPath[MAX_PATH];//Save application path
::GetModuleFileName(NULL, (LPSTR)l_wcaAppPath, MAX_PATH);
/* Get version information size */
UINT l_uiVersionInfoSize;//The overall size of the saved version information
TCHAR * l_ptcVersionInfo;
l_uiVersionInfoSize = ::GetFileVersionInfoSize((LPSTR)l_wcaAppPath, 0);//get size
l_ptcVersionInfo = new TCHAR[l_uiVersionInfoSize];//Apply for space
/* This structure is used to obtain language information of version information */
struct VersionLanguage
{
WORD m_wLanguage;
WORD m_wCcodePage;
};
VersionLanguage * l_ptVersionLanguage;
UINT l_uiSize;
if (::GetFileVersionInfo((LPSTR)l_wcaAppPath, 0, l_uiVersionInfoSize, l_ptcVersionInfo) != 0)//Get version information
{
if (::VerQueryValue(l_ptcVersionInfo, _T("\\VarFileInfo\\Translation"), reinterpret_cast<LPVOID*>(&l_ptVersionLanguage), &l_uiSize))//Query language information and save
{
/* Generate query information formatter */
CString l_cstrSubBlock;
l_cstrSubBlock.Format(_T("\\StringFileInfo\\%04x%04x\\ProductVersion"), l_ptVersionLanguage->m_wLanguage, l_ptVersionLanguage->m_wCcodePage);
LPVOID * l_pvResult;
/* Query specified information */
if (::VerQueryValue(static_cast<LPVOID>(l_ptcVersionInfo), l_cstrSubBlock.GetBuffer(), reinterpret_cast<LPVOID*>(&l_pvResult), &l_uiSize))
{
CString l_cstrProductVersion(reinterpret_cast<TCHAR *>(l_pvResult));// Get version information
GetDlgItem(IDC_STATIC_VERSION)->SetWindowTextA("ver "+ l_cstrProductVersion);// Version information is printed to the About window
}
}
}
delete[] l_ptcVersionInfo;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
ON_NOTIFY(NM_CLICK, IDC_SYSLINK1, &CAboutDlg::OnNMClickSyslink1)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CkendoUIDlg dialog
CkendoUIDlg::CkendoUIDlg(CWnd* pParent /*=NULL*/)
: CDialog(CkendoUIDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CkendoUIDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_catcher.setPool(&m_pool); // catcher initialization
//m_dumper.setPool(&m_pool); // dumper initialization
/* Flag initialization */
m_pktCaptureFlag = false;
m_fileOpenFlag = false;
}
void CkendoUIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CkendoUIDlg)
DDX_Control(pDX, IDC_LIST1, m_listCtrlPacketList);
DDX_Control(pDX, IDC_TREE1, m_treeCtrlPacketDetails);
DDX_Control(pDX, IDC_EDIT1, m_editCtrlPacketBytes);
//DDX_Control(pDX, IDC_RICHEDIT21, richEditCtrlFilterList_);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CkendoUIDlg, CDialog)
//{{AFX_MSG_MAP(CkendoUIDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickedList1)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CkendoUIDlg::OnCustomdrawList1)
ON_MESSAGE(WM_PKTCATCH, &CkendoUIDlg::OnPktCatchMessage)
ON_MESSAGE(WM_TEXIT, &CkendoUIDlg::OnTExitMessage)
//}}AFX_MSG_MAP
ON_NOTIFY(LVN_KEYDOWN, IDC_LIST1, &CkendoUIDlg::OnKeydownList1)
ON_COMMAND(ID_MENU_FILE_OPEN, &CkendoUIDlg::OnMenuFileOpen)
ON_COMMAND(ID_MENU_FILE_CLOSE, &CkendoUIDlg::OnMenuFileClose)
ON_COMMAND(ID_MENU_FILE_CLEAR_CACHE, &CkendoUIDlg::OnMenuFileClearCache)
ON_COMMAND(ID_MENU_FILE_SAVEAS, &CkendoUIDlg::OnMenuFileSaveAs)
ON_COMMAND(ID_MENU_FILE_EXIT, &CkendoUIDlg::OnMenuFileExit)
ON_COMMAND(ID_MENU_HELP_ABOUT, &CkendoUIDlg::OnMenuHelpAbout)
ON_COMMAND(ID_MENU_HELP_SHORTCUT, &CkendoUIDlg::OnMenuHelpShortCut)
//ON_UPDATE_COMMAND_UI(ID_INDICATOR_STATUS, &CkendoUIDlg::OnUpdateStatus)
ON_NOTIFY_EX_RANGE(TTN_NEEDTEXT, 0, 0xffff, OnToolTipText)
ON_COMMAND(ID_TOOLBARBTN_START, &CkendoUIDlg::OnClickedStart)
ON_COMMAND(ID_TOOLBARBTN_STOP, &CkendoUIDlg::OnClickedStop)
ON_COMMAND(ID_TOOLBARBTN_CLEAR, &CkendoUIDlg::OnClickedClear)
ON_COMMAND(ID_TOOLBARBTN_FILTER, &CkendoUIDlg::OnClickedFilter)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CkendoUIDlg message handlers
/**
* @brief UI interface initialization
* @param -
* @return -
*/
BOOL CkendoUIDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
initialAccelerator(); // Shortcut key initialization
initialMenuBar(); // Menu bar initialization
initialToolBar(); // Toolbar initialization
initialComboBoxDevList(); // Network card list initialization
initialComboBoxFilterList(); // Filter list initialization
initialListCtrlPacketList(); // List control (packet list) initialization
initialTreeCtrlPacketDetails(); // Tree control (packet details) initialization
initialEditCtrlPacketBytes(); // Edit control (packet byte stream) initialization
initialStatusBar(); // Status bar initialization¯
createDirectory(".\\tmp"); // Determine whether the tmp folder exists, create it if it does not exist
return TRUE; // return TRUE unless you set the focus to a control
}
void CkendoUIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CkendoUIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CkendoUIDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*************************************************************
*
* Button event implementation
*
*************************************************************/
/**
* @brief Press the start button to start capturing packets
* @param -
* @return -
*/
void CkendoUIDlg::OnClickedStart()
{
// Get current time
time_t tt = time(NULL); // This sentence returns only a timestamp
localtime(&tt);
CTime currentTime(tt);
/* If the network card is not selected, a prompt message will be reported; otherwise, a thread will be created to capture packets. */
int selItemIndex = m_comboBoxDevList.GetCurSel();
if (selItemIndex <= 0)
{
AfxMessageBox(_T("Please select network card"), MB_OK);
return;
}
if (m_catcher.openAdapter(selItemIndex, currentTime))
{
CString status = "capturing:" + m_catcher.getDevName();
/* Modify control enable status */
m_comboBoxDevList.EnableWindow(FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_TOOLBARBTN_START, FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_TOOLBARBTN_STOP, TRUE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_MENU_FILE_OPEN, FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_MENU_FILE_SAVEAS, FALSE);
m_menu.EnableMenuItem(ID_MENU_FILE_OPEN, MF_GRAYED); // Disable menu item "Open"
m_menu.EnableMenuItem(ID_MENU_FILE_CLOSE, MF_GRAYED); // Disable menu item "Close"
m_menu.EnableMenuItem(ID_MENU_FILE_OPEN, MF_GRAYED); // Disable menu item "Save As"
/* Çå¿Õ¿Ø¼þÏÔʾÄÚÈÝ */
m_listCtrlPacketList.DeleteAllItems();
m_treeCtrlPacketDetails.DeleteAllItems();
m_editCtrlPacketBytes.SetWindowTextA("");
AfxGetMainWnd()->SetWindowText(status);
/* Clear the in-memory packet pool */
m_pool.clear();
/* Update status bar */
updateStatusBar(status, m_pool.getSize(), m_listCtrlPacketList.GetItemCount());
CString fileName = "kendoUI_" + currentTime.Format("%Y%m%d%H%M%S") + ".pcap";
m_pktDumper.setPath(".\\tmp\\" + fileName);
m_catcher.startCapture(MODE_CAPTURE_LIVE);
m_pktCaptureFlag = true;
m_openFileName = fileName;
m_fileOpenFlag = true;
}
}
/**
* @brief Press the end button to stop packet capture, delete the printed packet-related information, clear the packet list, and restart packet capture.
* @param -
* @return -
*/
void CkendoUIDlg::OnClickedStop()
{
CString status = "Capture ends:" + m_catcher.getDevName();
AfxGetMainWnd()->SetWindowText(m_pktDumper.getPath()); // Modify title bar
m_comboBoxDevList.EnableWindow(TRUE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_TOOLBARBTN_START, TRUE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_TOOLBARBTN_STOP, FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_MENU_FILE_OPEN, TRUE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_MENU_FILE_SAVEAS, TRUE);
m_menu.EnableMenuItem(ID_MENU_FILE_OPEN, MF_ENABLED); // Enable menu item "Open"
m_menu.EnableMenuItem(ID_MENU_FILE_CLOSE, MF_GRAYED); // Disable menu item "Close"
m_menu.EnableMenuItem(ID_MENU_FILE_SAVEAS, MF_ENABLED); // Enable menu item "Save as"
m_statusBar.SetPaneText(0, status, true); // Modify status
m_catcher.stopCapture();
m_pktCaptureFlag = false;
//m_catcher.closeAdapter();
}
/**
* @brief Press the filter button to filter packets based on the protocol name entered in the filter
* @param -
* @return -
*/
void CkendoUIDlg::OnClickedFilter()
{
int selIndex = m_comboBoxFilterList.GetCurSel();
if (selIndex <= 0)
return;
CString strFilter;
m_comboBoxFilterList.GetLBText(selIndex, strFilter);
m_listCtrlPacketList.DeleteAllItems();
m_treeCtrlPacketDetails.DeleteAllItems();
m_editCtrlPacketBytes.SetWindowTextA("");
printListCtrlPacketList(m_pool, strFilter);
updateStatusBar(CString(""), m_pool.getSize(), m_listCtrlPacketList.GetItemCount());
}
/**
* @brief Press the clear button to clear the filter and display all packets
* @param -
* @return -
*/
void CkendoUIDlg::OnClickedClear()
{
m_comboBoxFilterList.SetCurSel(0);
m_listCtrlPacketList.DeleteAllItems();
m_treeCtrlPacketDetails.DeleteAllItems();
m_editCtrlPacketBytes.SetWindowTextA("");
printListCtrlPacketList(m_pool);
updateStatusBar(CString(""), m_pool.getSize(), m_listCtrlPacketList.GetItemCount());
}
/*************************************************************
*
* Control initialization
*
*************************************************************/
/**
* @brief Shortcut key initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialAccelerator()
{
m_hAccelMenu = ::LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MENU1)); // ¼ÓÔز˵¥¿ì½Ý¼ü×ÊÔ´
m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
}
/**
* @brief Menu bar initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialMenuBar()
{
m_menu.LoadMenu(IDR_MENU1);
SetMenu(&m_menu);
/* Menu item disabled */
// CMenu* pMenu = this->GetMenu();
if (m_menu)
{
m_menu.EnableMenuItem(ID_MENU_FILE_CLOSE, MF_GRAYED); // Disable menu item "Close"
m_menu.EnableMenuItem(ID_MENU_FILE_SAVEAS, MF_GRAYED); // Disable menu item "Save As"
}
}
/**
* @brief Toolbar initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialToolBar()
{
// Main toolbar creation
if (!m_toolBarMain.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS | CBRS_GRIPPER | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_toolBarMain.LoadToolBar(IDR_TOOLBAR1))
{
AfxMessageBox(_T("Failed to create main tool bar\n"));
return;
}
// Create combo box (list of network cards) on main toolbar button
//Create a combo box on the button, the button position determines the position of the combo box
int index = m_toolBarMain.CommandToIndex(ID_TOOLBARBTN_DEVLIST);
m_toolBarMain.SetButtonInfo(index, ID_TOOLBARBTN_DEVLIST, TBBS_SEPARATOR, 300);//ÉèÖÃ×éºÏ¿òµÄID£¬ÀàÐÍ£¨ÕâÀïÊÇ·Ö¸ôÀ¸£©£¬300ÊÇÖ¸·Ö¸ôÀ¸¿í¶È
// Create a combo box based on the size rect of the separator
CRect rect;
m_toolBarMain.GetItemRect(index, &rect);
rect.left += 10;
rect.top += 3;
m_comboBoxDevList.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, rect, &m_toolBarMain, ID_TOOLBARBTN_DEVLIST);
// Read the main toolbar button icon, store it in ImageList, and the toolbar reads ImageList
m_imageListMain.Create(BITMAP_WIDTH, BITMAP_HEIGHT, ILC_COLOR24 | ILC_MASK, 0, 0);
for (int i = 0; i < BITMAP_LIST_MAIN_SIZE; ++i)
{
m_bitmapListMain[i].LoadBitmapA(IDB_BITMAP_DEV + i);
m_imageListMain.Add(&m_bitmapListMain[i], RGB(0, 0, 0));
}
m_toolBarMain.GetToolBarCtrl().SetImageList(&m_imageListMain);
// Disable buttons on the main toolbar
//m_toolBarMain.GetToolBarCtrl().EnableButton(IDC_DROPDOWNBTN_DEVLIST, FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_TOOLBARBTN_STOP, FALSE);
m_toolBarMain.GetToolBarCtrl().EnableButton(ID_MENU_FILE_SAVEAS, FALSE);
// Filter toolbar creation
if (!m_toolBarFilter.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS | CBRS_GRIPPER | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_toolBarFilter.LoadToolBar(IDR_TOOLBAR2))
{
AfxMessageBox(_T("Failed to create filter tool bar\n"));
return;
}
// Create combobox (filter list) on filter toolbar button
index = m_toolBarFilter.CommandToIndex(ID_TOOLBARBTN_FILTERLIST);
m_toolBarFilter.SetButtonInfo(index, ID_TOOLBARBTN_FILTERLIST, TBBS_SEPARATOR, 300);//ÉèÖÃ×éºÏ¿òµÄID£¬ÀàÐÍ£¨ÕâÀïÊÇ·Ö¸ôÀ¸£©£¬300ÊÇÖ¸·Ö¸ôÀ¸¿í¶È
// Create a combo box based on the size rect of the separator
m_toolBarFilter.GetItemRect(index, &rect);
rect.left += 10;
rect.top += 3;
m_comboBoxFilterList.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST, rect, &m_toolBarFilter, ID_TOOLBARBTN_FILTERLIST);
// Read the filter toolbar button icon, store it in ImageList, and the toolbar reads ImageList
m_imageListFilter.Create(BITMAP_WIDTH, BITMAP_HEIGHT, ILC_COLOR24 | ILC_MASK, 0, 0);
for (int i = 0; i < BITMAP_LIST_FILTER_SIZE; ++i)
{
m_bitmapListFilter[i].LoadBitmapA(IDB_BITMAP_DEV + BITMAP_LIST_MAIN_SIZE + i);
m_imageListFilter.Add(&m_bitmapListFilter[i], RGB(0, 0, 0));
}
m_toolBarFilter.GetToolBarCtrl().SetImageList(&m_imageListFilter);
// Set drop-down list font
m_comboFont.CreateFontA(12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "ÐÂËÎÌå");
m_comboBoxDevList.SetFont(&m_comboFont);
m_comboBoxFilterList.SetFont(&m_comboFont);
// Set drop-down list height
m_comboBoxDevList.SetItemHeight(-1, 18);
m_comboBoxFilterList.SetItemHeight(-1, 18);
//Control strip positioning
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
}
/**
* @brief Get the local machine network card list and print the network card description to the drop-down list
* @param -
* @return -
*/
void CkendoUIDlg::initialComboBoxDevList()
{
m_comboBoxDevList.AddString("Select network card");
m_comboBoxDevList.SetCurSel(0);
pcap_if_t *dev = NULL;
pcap_if_t *allDevs = NULL;
char errbuf[PCAP_ERRBUF_SIZE + 1];
if (pcap_findalldevs(&allDevs, errbuf) == -1)
{
AfxMessageBox(_T("pcap_findalldevs wrong!"), MB_OK);
return;
}
for (dev = allDevs; dev != NULL; dev = dev->next)
{
if (dev->description != NULL)
m_comboBoxDevList.AddString(dev->description);
}
m_catcher.setDevList(allDevs);
//pcap_freealldevs(allDevs);
}
/**
* @brief Filter list initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialComboBoxFilterList()
{
std::vector<CString> filterList;
filterList.push_back("Ethernet");
filterList.push_back("IP");
filterList.push_back("ARP");
filterList.push_back("ICMP");
filterList.push_back("TCP");
filterList.push_back("UDP");
filterList.push_back("DNS");
filterList.push_back("DHCP");
filterList.push_back("HTTP");
m_comboBoxFilterList.AddString("Select filter(optional)");
m_comboBoxFilterList.SetCurSel(0);
for(int i = 0; i < filterList.size(); ++i)
m_comboBoxFilterList.AddString(filterList[i]);
}
/**
* @brief Tree control (packet details) initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialListCtrlPacketList()
{
// Adjust list control (packet list) position based on filter toolbar position
CRect rect;
m_toolBarFilter.GetWindowRect(&rect);
ScreenToClient(&rect);
GetDlgItem(IDC_LIST1)->SetWindowPos(NULL, rect.left, rect.bottom + 5, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
DWORD dwStyle = m_listCtrlPacketList.GetExtendedStyle(); // Ìí¼ÓÁбí¿Ø¼þµÄÍø¸ñÏß
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
m_listCtrlPacketList.SetExtendedStyle(dwStyle);
m_listCtrlPacketList.GetWindowRect(&rect);
ScreenToClient(&rect);
int index = 0;
m_listCtrlPacketList.InsertColumn(index, "number", LVCFMT_CENTER, rect.Width() * 0.05);
m_listCtrlPacketList.InsertColumn(++index, "time", LVCFMT_CENTER, rect.Width() * 0.15);
m_listCtrlPacketList.InsertColumn(++index, "agreement", LVCFMT_CENTER, rect.Width() * 0.05);
m_listCtrlPacketList.InsertColumn(++index, "length", LVCFMT_CENTER, rect.Width() * 0.05);
m_listCtrlPacketList.InsertColumn(++index, "MAC address·", LVCFMT_CENTER, rect.Width() * 0.175);
m_listCtrlPacketList.InsertColumn(++index, "destination MAC address", LVCFMT_CENTER, rect.Width() * 0.175);
m_listCtrlPacketList.InsertColumn(++index, "Source IP address", LVCFMT_CENTER, rect.Width() * 0.175);
m_listCtrlPacketList.InsertColumn(++index, "destination IP address", LVCFMT_CENTER, rect.Width() * 0.175);
}
/**
* @brief Tree control (packet details) initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialTreeCtrlPacketDetails()
{
// Adjust the position of the tree control (packet details) based on the position of the list control (packet list)
CRect rect, winRect;
m_listCtrlPacketList.GetWindowRect(&rect);
ScreenToClient(&rect);
GetDlgItem(IDC_TREE1)->SetWindowPos(NULL, rect.left, rect.bottom + 5, rect.Width() * 0.5, rect.Height() + 125, SWP_NOZORDER);
}
/**
* @brief Edit control (packet byte stream) initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialEditCtrlPacketBytes()
{
// Adjust the position of the edit control (packet byte stream) based on the position of the tree control control (packet details)
CRect rect;
m_treeCtrlPacketDetails.GetWindowRect(&rect);
ScreenToClient(&rect);
GetDlgItem(IDC_EDIT1)->SetWindowPos(NULL, rect.right + 5, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
}
/**
* @brief Status bar initialization
* @param -
* @return -
*/
void CkendoUIDlg::initialStatusBar()
{
if (m_statusBar.Create(this)) // Create menu bar
{
static UINT indicators[] =
{
ID_INDICATOR_STATUS,
ID_INDICATOR_PKT_TOTAL_NUM,
ID_INDICATOR_PKT_DISPLAY_NUM
};
int indicatorsSize = sizeof(indicators) / sizeof(UINT);
m_statusBar.SetIndicators(indicators, indicatorsSize);
CRect rect;
GetClientRect(rect);
int index = 0;
m_statusBar.SetPaneInfo(index, ID_INDICATOR_STATUS, SBPS_STRETCH, rect.Width() * 0.6);
m_statusBar.SetPaneInfo(++index, ID_INDICATOR_PKT_TOTAL_NUM, SBPS_NORMAL, rect.Width() * 0.2);
m_statusBar.SetPaneInfo(++index, ID_INDICATOR_PKT_DISPLAY_NUM, SBPS_NORMAL, rect.Width() * 0.15);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); // Show status bar
}
}
/**
* @brief Update status bar
* @param [in] status state
* @param [in] pktTotalNum Total number of packets. Update this word when the value is non-negative.
* @param [in] pktDisplayNum Display number of data packets. Update this field when the value is non-negative.
* @return -
*/
void CkendoUIDlg::updateStatusBar(const CString & status, int pktTotalNum, int pktDisplayNum)
{
if (!status.IsEmpty())
{
int index = m_statusBar.CommandToIndex(ID_INDICATOR_STATUS);
m_statusBar.SetPaneText(index, status, TRUE);
}
if (pktTotalNum >= 0)
{
int index = m_statusBar.CommandToIndex(ID_INDICATOR_PKT_TOTAL_NUM);
CString text;
text.Format("data package", pktTotalNum);
m_statusBar.SetPaneText(index, text, TRUE);
}
if (pktDisplayNum >= 0)
{
int index = m_statusBar.CommandToIndex(ID_INDICATOR_PKT_DISPLAY_NUM);
CString text;
double percentage = (pktDisplayNum == 0 || pktTotalNum == 0)?
0.0 : ((double)pktDisplayNum / pktTotalNum * 100);
text.Format("already shown: %d (%.1f%%)", pktDisplayNum, percentage);
m_statusBar.SetPaneText(index, text, TRUE);
}
}
/**
* @brief Create a folder at the specified path
* @param [in] dirPath folder path
* @return true Creation successful false Creation failed (folder already exists)
*/
bool CkendoUIDlg::createDirectory(const CString& dirPath)
{
if (!PathIsDirectory(dirPath.GetString())) // Is there a folder with the same name?
{
::CreateDirectory(dirPath.GetString(), 0);
return true;
}
return false;
}
/**
* @brief Clear all files in the specified folder
* @param [in] dirPath folder path
* @return true if the clearing is successful false if the clearing fails
*/
bool CkendoUIDlg::clearDirectory(const CString& dirPath)
{
CFileFind finder;
CString path(dirPath);
path += _T("\\*.*");
BOOL isFound = finder.FindFile(path);
if (!isFound)
{
return false;
}
while (isFound)
{
isFound = finder.FindNextFile();
// Skip . and .. ; otherwise you will get stuck in an infinite loop
if (finder.IsDots())
continue;
// If it is a directory, enter the search (recursively)
if (finder.IsDirectory())
{
CString subDirPath = dirPath + finder.GetFileName();
clearDirectory(subDirPath); // Delete files in a folder
RemoveDirectory(subDirPath); // Remove empty files
}
else
{
CString filePath = dirPath + finder.GetFileName();
DeleteFile(filePath);
}
}
finder.Close();
return true;
}
/**
* @brief Print packet summary information to a list control
* @param data pack
* @return 0 Printing successful -1 Printing failed
*/
int CkendoUIDlg::printListCtrlPacketList(const Packet &pkt)
{
if (pkt.isEmpty())
return -1;
int row = 0; // Line number
int col = 0; // Column number
/* Print number */
CString strNum;
strNum.Format("%d", pkt.num);
UINT mask = LVIF_PARAM | LVIF_TEXT;
// The protocol field is used in OnCustomdrawList1()
row = m_listCtrlPacketList.InsertItem(mask, m_listCtrlPacketList.GetItemCount(), strNum, 0, 0, 0, (LPARAM)&(pkt.protocol));
/* Print Time */
CTime pktArrivalTime( (time_t)(pkt.header->ts.tv_sec) ) ;
CString strPktArrivalTime = pktArrivalTime.Format("%Y/%m/%d %H:%M:%S");
m_listCtrlPacketList.SetItemText(row, ++col, strPktArrivalTime);
/* Print agreement */
if (!pkt.protocol.IsEmpty())
m_listCtrlPacketList.SetItemText(row, ++col, pkt.protocol);
else
++col;
/* Print length */
CString strCaplen;
strCaplen.Format("%d", pkt.header->caplen);
m_listCtrlPacketList.SetItemText(row, ++col, strCaplen);
/* Print source and destination MAC addresses */
if (pkt.ethh != NULL)
{
CString strSrcMAC = MACAddr2CString(pkt.ethh->srcaddr);
CString strDstMAC = MACAddr2CString(pkt.ethh->dstaddr);
m_listCtrlPacketList.SetItemText(row, ++col, strSrcMAC);
m_listCtrlPacketList.SetItemText(row, ++col, strDstMAC);
}
else
{
col += 2;
}
/* Print source and destination IP addresses */
if (pkt.iph != NULL)
{
CString strSrcIP = IPAddr2CString(pkt.iph->srcaddr);
CString strDstIP = IPAddr2CString(pkt.iph->dstaddr);
m_listCtrlPacketList.SetItemText(row, ++col, strSrcIP);
m_listCtrlPacketList.SetItemText(row, ++col, strDstIP);
}
else
{
col += 2;
}
return 0;
}
/**
* @brief Print packet summary information to a list control
* @param pool packet pool
* @return >=0 Number of packets in the packet pool -1 Printing failed
*/
int CkendoUIDlg::printListCtrlPacketList(PacketPool &pool)
{
if (pool.isEmpty())
return -1;
int pktNum = pool.getSize();
for (int i = 1; i <= pktNum; ++i)
printListCtrlPacketList(pool.get(i));
return pktNum;
}
/**
* @brief Traverse the data packet list and print the data packets to the list control according to the filter name
* @param packetLinkList packet link list
* @param filter filter name
* @return >=0 Number of filtered data packets -1 Printing failed
*/
int CkendoUIDlg::printListCtrlPacketList(PacketPool &pool, const CString &filter)
{
if (pool.isEmpty() || filter.IsEmpty())
return -1;
int pktNum = pool.getSize();
int filterPktNum = 0;
for (int i = 0; i < pktNum; ++i)
{
const Packet &pkt = pool.get(i);// BUG: May be Here
if (pkt.protocol == filter)
{
printListCtrlPacketList(pkt);
++filterPktNum;
}
}
return filterPktNum;
}
/**
* @brief Print the packet byte stream to the edit box (hexadecimal data area)
* @param pkt data pack
* @return 0 Printing successful -1 Printing failed
*/
int CkendoUIDlg::printEditCtrlPacketBytes(const Packet & pkt)
{
if (pkt.isEmpty())
{
return -1;
}
CString strPacketBytes, strTmp;
u_char* pHexPacketBytes = pkt.pkt_data;
u_char* pASCIIPacketBytes = pkt.pkt_data;
for (int byteCount = 0, byteCount16=0, offset = 0; byteCount < pkt.header->caplen && pHexPacketBytes != NULL; ++byteCount)
{
/* If the current byte is the beginning of the line, print the offset of the beginning of the line */
if (byteCount % 16 == 0)
{
strTmp.Format("%04X:", offset);
strPacketBytes += strTmp + " ";
}
/* Print hexadecimal bytes */
strTmp.Format("%02X", *pHexPacketBytes);
strPacketBytes += strTmp + " ";
++pHexPacketBytes;
++byteCount16;
switch (byteCount16)
{
case 8:
{
/* Print a tab every 8 bytes read */
strPacketBytes += "\t";
//strPacketBytes += "#";
}
break;
case 16:
{
/* Every 16 bytes read, the ASCII character of the corresponding byte is printed. Only alphanumeric characters are printed. */
if (byteCount16 == 16)
{
strPacketBytes += " ";
for (int charCount = 0; charCount < 16; ++charCount, ++pASCIIPacketBytes)
{
strTmp.Format("%c", isalnum(*pASCIIPacketBytes) ? *pASCIIPacketBytes : '.');
strPacketBytes += strTmp;
}
strPacketBytes += "\r\n";
offset += 16;
byteCount16 = 0;
}
}
break;
default:break;
}
}
/* If the total length of the data packet is not aligned to 16 bytes, print the ASCII character corresponding to the last line of bytes. */
if (pkt.header->caplen % 16 != 0)
{
/* Space padding ensures 16-byte alignment of the byte stream */
for (int spaceCount = 0, byteCount16 = (pkt.header->caplen % 16); spaceCount < 16 - (pkt.header->caplen % 16); ++spaceCount)
{
strPacketBytes += " ";
strPacketBytes += " ";
++byteCount16;
if (byteCount16 == 8)
{
strPacketBytes += "\t";
//strPacketBytes += "#";
}
}
strPacketBytes += " ";
/* Print the ASCII characters corresponding to the last line of bytes */
for (int charCount = 0; charCount < (pkt.header->caplen % 16); ++charCount, ++pASCIIPacketBytes)
{
strTmp.Format("%c", isalnum(*pASCIIPacketBytes) ? *pASCIIPacketBytes : '.');
strPacketBytes += strTmp;
}
strPacketBytes += "\r\n";
}
m_editCtrlPacketBytes.SetWindowTextA(strPacketBytes);
return 0;
}
/**
* @brief Print the packet header parsing results to the tree control
* @param pkt data pack
* @return 0 Print successfully -1 print
*/
int CkendoUIDlg::printTreeCtrlPacketDetails(const Packet &pkt)
{
if (pkt.isEmpty())
return -1;
m_treeCtrlPacketDetails.DeleteAllItems();
/* ½¨Á¢±àºÅ½áµã */
CString strText;
CTime pktArrivalTime((time_t)(pkt.header->ts.tv_sec));
CString strPktArrivalTime = pktArrivalTime.Format("%Y/%m/%d %H:%M:%S");
strText.Format("%d data package(%s, %hu byte, capture %hu byte)", pkt.num, strPktArrivalTime, pkt.header->len, pkt.header->caplen);
HTREEITEM rootNode = m_treeCtrlPacketDetails.InsertItem(strText, TVI_ROOT);
if (pkt.ethh != NULL)
{
printEthernet2TreeCtrl(pkt, rootNode);
}
m_treeCtrlPacketDetails.Expand(rootNode, TVE_EXPAND);
return 0;
}
/**
* @brief Print Ethernet frame header to tree control
* @param pkt data pack
* @param parentNode parent node
* @return 0 Insertion successful -1 Insert
*/
int CkendoUIDlg::printEthernet2TreeCtrl(const Packet &pkt, HTREEITEM &parentNode)
{
if (pkt.isEmpty() || pkt.ethh == NULL || parentNode == NULL)
{
return -1;
}
/* Get the source and destination MAC addresses */
CString strSrcMAC = MACAddr2CString(pkt.ethh->srcaddr);
CString strDstMAC = MACAddr2CString(pkt.ethh->dstaddr);
CString strEthType;
strEthType.Format("0x%04X", ntohs(pkt.ethh->eth_type));
HTREEITEM EthNode = m_treeCtrlPacketDetails.InsertItem("Ethernet (" + strSrcMAC + " -> " + strDstMAC + ") ", parentNode, 0);
m_treeCtrlPacketDetails.InsertItem("destination MAC address: " + strDstMAC, EthNode, 0);
m_treeCtrlPacketDetails.InsertItem("MAC address:" + strSrcMAC, EthNode, 0);
m_treeCtrlPacketDetails.InsertItem("Type:" + strEthType, EthNode, 0);