/
SiriusView.cpp
438 lines (360 loc) · 11.8 KB
/
SiriusView.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
// SiriusView.cpp : implementation of the CSiriusView class
//
#include "stdafx.h"
#include "Sirius.h"
#include "SiriusDoc.h"
#include "SiriusView.h"
#include "PacketCapture.h"
#include "TrafficList.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TIMER_ID (1)
// global file vars
static int s_iSortColumn;
static BOOL s_bSortAscending;
static LVFINDINFO fi;
int CALLBACK CompareFunction(LPARAM lParam1,LPARAM lParam2,LPARAM lParamData);
/////////////////////////////////////////////////////////////////////////////
// data for the list view control
#define NUM_COLUMNS 8
static _TCHAR *_gszColumnLabel[NUM_COLUMNS] =
{
_T("Source"), _T("src port"), _T("Destination"), _T("dest port"), _T("Proto"), _T("Traffic"), _T("bytes/sec"), _T("Average b/s")
};
static int _gnColumnFmt[NUM_COLUMNS] =
{
LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_RIGHT, LVCFMT_RIGHT, LVCFMT_RIGHT, LVCFMT_RIGHT
};
static int _gnColumnWidth[NUM_COLUMNS] =
{
120, 80, 120, 80, 50, 100, 80, 80
};
static BOOL _gbColumnNumber[NUM_COLUMNS] =
{
FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE
};
/////////////////////////////////////////////////////////////////////////////
// CSiriusView
IMPLEMENT_DYNCREATE(CSiriusView, CListView)
BEGIN_MESSAGE_MAP(CSiriusView, CListView)
//{{AFX_MSG_MAP(CSiriusView)
ON_UPDATE_COMMAND_UI(ID_COMMAND_START, OnUpdateCommandStart)
ON_UPDATE_COMMAND_UI(ID_COMMAND_STOP, OnUpdateCommandStop)
ON_WM_PAINT()
ON_COMMAND(ID_COMMAND_START, OnCommandStart)
ON_COMMAND(ID_COMMAND_STOP, OnCommandStop)
ON_WM_DESTROY()
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnClick)
ON_UPDATE_COMMAND_UI(ID_FILTER_REMOVESORTING, OnUpdateFilterRemoveSorting)
ON_COMMAND(ID_FILTER_REMOVESORTING, OnFilterRemoveSorting)
ON_UPDATE_COMMAND_UI(ID_FILTER_TCP, OnUpdateFilterTcp)
ON_UPDATE_COMMAND_UI(ID_FILTER_UDP, OnUpdateFilterUdp)
ON_UPDATE_COMMAND_UI(ID_FILTER_ICMP, OnUpdateFilterIcmp)
ON_COMMAND(ID_FILTER_TCP, OnFilterTcp)
ON_COMMAND(ID_FILTER_UDP, OnFilterUdp)
ON_COMMAND(ID_FILTER_ICMP, OnFilterIcmp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSiriusView construction/destruction
CSiriusView::CSiriusView()
{
m_hAdapterMutex = NULL;
memset(&fi, 0, sizeof(LVFINDINFO));
fi.flags = LVFI_PARAM;
s_iSortColumn = -1;
}
CSiriusView::~CSiriusView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CSiriusView drawing
void CSiriusView::OnDraw(CDC* pDC)
{
CSiriusDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CSiriusView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// my code
_gnColumnWidth[0] = g_Settings.m_iListSrcAddr;
_gnColumnWidth[2] = g_Settings.m_iListDestAddr;
_gnColumnWidth[1] = g_Settings.m_iListSrcPort;
_gnColumnWidth[3] = g_Settings.m_iListDestPort;
_gnColumnWidth[4] = g_Settings.m_iListProto;
_gnColumnWidth[5] = g_Settings.m_iListTraffic;
_gnColumnWidth[6] = g_Settings.m_iListBS;
_gnColumnWidth[7] = g_Settings.m_iListABS;
CListCtrl& lc_AddrList = GetListCtrl();
// set report style
long lStyle = GetWindowLong(lc_AddrList.m_hWnd, GWL_STYLE);
lStyle &= ~(LVS_TYPEMASK); // turn off all the style (view mode) bits
lStyle |= LVS_REPORT; // Set the new style for the control
SetWindowLong(lc_AddrList.m_hWnd, GWL_STYLE, lStyle);
// set other styles
DWORD dwExStyle = lc_AddrList.GetExtendedStyle();
lc_AddrList.SetExtendedStyle(dwExStyle |
LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);
// insert columns
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for(int iCounter = 0; iCounter < NUM_COLUMNS; iCounter++) {
lvc.iSubItem = iCounter;
lvc.pszText = _gszColumnLabel[iCounter];
lvc.cx = _gnColumnWidth[iCounter];
lvc.fmt = _gnColumnFmt[iCounter];
lc_AddrList.InsertColumn(iCounter, &lvc);
}
g_pwndTraffic = &GetListCtrl();
VERIFY(m_ctlHeader.SubclassWindow(lc_AddrList.GetHeaderCtrl()->GetSafeHwnd()));
}
/////////////////////////////////////////////////////////////////////////////
// CSiriusView diagnostics
#ifdef _DEBUG
void CSiriusView::AssertValid() const
{
CListView::AssertValid();
}
void CSiriusView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CSiriusDoc* CSiriusView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSiriusDoc)));
return (CSiriusDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSiriusView message handlers
void CSiriusView::OnUpdateCommandStart(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!g_bStarted);
}
void CSiriusView::OnUpdateCommandStop(CCmdUI* pCmdUI)
{
pCmdUI->Enable(g_bStarted);
}
void CSiriusView::OnPaint()
{
if (GetListCtrl().GetItemCount() == 0) { // show empty list message
CPaintDC dc( this );
int nSavedDC = dc.SaveDC();
CRect rc;
GetClientRect( &rc );
CHeaderCtrl* pHC;
pHC = GetListCtrl().GetHeaderCtrl();
if (pHC != NULL) {
CRect rcH;
pHC->GetItemRect( 0, &rcH );
rc.top += rcH.bottom;
}
rc.top += 10;
CString strText;
if (g_bStarted)
strText.LoadString(IDS_NODATA);
else
strText.LoadString(IDS_BEGIN);
dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
dc.SetBkColor( ::GetSysColor( COLOR_WINDOW ) );
dc.SelectStockObject( ANSI_VAR_FONT );
dc.DrawText( strText, rc, DT_CENTER | DT_WORDBREAK | DT_NOPREFIX | DT_NOCLIP );
dc.RestoreDC(nSavedDC);
}
else {
Default();
}
}
void CSiriusView::CreateMutexName(const char *szInput, char *szOutput)
{
strcpy(szOutput, "st_"); // mutex name begin (st comes from Sirius)
strcat(szOutput, szInput); // adds Input as the rest of the mutex name
int iLen = strlen(szOutput);
// replace all '\\' with '_'
for (int i = 0; i < iLen; i++)
if ('\\' == szOutput[i])
szOutput[i] = '_';
}
void CSiriusView::OnCommandStart()
{
// get selected adapter
char szAdapter[512], szMutexName[512];
szAdapter[0] = 0;
g_pwndAdapter->GetWindowText(szAdapter, 512);
if (0 == szAdapter[0])
return;
// remove ip and mask
char *pszIpMask = strstr(szAdapter, " (ip:");
if (pszIpMask)
*pszIpMask = 0;
// check if we have such adapter already started
CreateMutexName(szAdapter, szMutexName);
m_hAdapterMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, szMutexName);
if (m_hAdapterMutex != NULL) {
MessageBox("This adapter is already opened\n\rin other instance of Show Traffic",
g_szError, MB_OK | MB_ICONSTOP);
CloseHandle(m_hAdapterMutex);
return;
}
m_hAdapterMutex = CreateMutex(NULL, FALSE, szMutexName);
if (0 != StartCapture(szAdapter))
return;
g_bStarted = TRUE;
g_pwndTraffic->Invalidate();
::SetTimer(m_hWnd, TIMER_ID, 1000, &(CSiriusView::MyTimerProc)); // init statistics timer
// update application title
g_pwndAdapter->GetWindowText(szAdapter, 512);
CString sTitle;
sTitle.LoadString(AFX_IDS_APP_TITLE);
sTitle += " - [";
sTitle += szAdapter;
sTitle += "]";
GetParentFrame()->SetWindowText(sTitle);
}
void CSiriusView::OnCommandStop()
{
EndCapture();
g_bStarted = FALSE;
KillTimer(TIMER_ID); // kill statistics timer
CloseHandle(m_hAdapterMutex);
// update application title
CString sTitle;
sTitle.LoadString(AFX_IDS_APP_TITLE);
GetParentFrame()->SetWindowText(sTitle);
}
void CSiriusView::OnDestroy()
{
HDITEM hdi;
// store columns width
CHeaderCtrl *pHdr = GetListCtrl().GetHeaderCtrl();
hdi.mask = HDI_WIDTH;
pHdr->GetItem(0, &hdi);
g_Settings.m_iListSrcAddr = hdi.cxy;
pHdr->GetItem(2, &hdi);
g_Settings.m_iListDestAddr = hdi.cxy;
pHdr->GetItem(1, &hdi);
g_Settings.m_iListSrcPort = hdi.cxy;
pHdr->GetItem(3, &hdi);
g_Settings.m_iListDestPort = hdi.cxy;
pHdr->GetItem(4, &hdi);
g_Settings.m_iListProto = hdi.cxy;
pHdr->GetItem(5, &hdi);
g_Settings.m_iListTraffic = hdi.cxy;
pHdr->GetItem(6, &hdi);
g_Settings.m_iListBS = hdi.cxy;
pHdr->GetItem(7, &hdi);
g_Settings.m_iListABS = hdi.cxy;
if (g_bStarted) {
OnCommandStop();
g_bStarted = FALSE;
}
CListView::OnDestroy();
}
void CALLBACK CSiriusView::MyTimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
TRACE("Timer called\n");
if (TIMER_ID == nIDEvent) {
g_bTimerEvent = TRUE;
char szNum[300];
sprintf(szNum, "b/s: %u avr b/s: %u", g_dwBPS, g_dwABS);
g_pwndStatusBar->SetPaneText(1, szNum, FALSE);
}
}
void CSiriusView::OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
const int iColumn = pNMListView->iSubItem;
// if it's a second click on the same column then reverse the sort order,
// otherwise sort the new column in ascending order.
Sort( iColumn, iColumn == s_iSortColumn ? !s_bSortAscending : TRUE );
*pResult = 0;
}
void CSiriusView::Sort(int iColumn, BOOL bAscending)
{
s_iSortColumn = iColumn;
s_bSortAscending = bAscending;
// show the appropriate arrow in the header control.
m_ctlHeader.SetSortArrow(s_iSortColumn, s_bSortAscending );
g_pwndTraffic->SortItems(CompareFunction, (DWORD)g_pwndTraffic);
}
BOOL IsNumber(LPCTSTR pszText)
{
for( int i = 0; i < lstrlen( pszText ); i++ )
if( !_istdigit( pszText[ i ] ) )
return false;
return true;
}
int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
const int iNumber1 = atoi( pszNumber1 );
const int iNumber2 = atoi( pszNumber2 );
if( iNumber1 < iNumber2 )
return -1;
if( iNumber1 > iNumber2 )
return 1;
return 0;
}
int CALLBACK CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )
{
CListCtrl *pListCtrl = (CListCtrl *)lParamData;
fi.lParam = lParam1;
int iItem1 = pListCtrl->FindItem(&fi);
fi.lParam = lParam2;
int iItem2 = pListCtrl->FindItem(&fi);
if ((-1 == iItem1) || (-1 == iItem2))
return 0;
CString strItem1 = pListCtrl->GetItemText(iItem1, s_iSortColumn);
CString strItem2 = pListCtrl->GetItemText(iItem2, s_iSortColumn);
if (_gbColumnNumber[s_iSortColumn])
return s_bSortAscending ? NumberCompare( strItem1, strItem2 ) : NumberCompare( strItem2, strItem1 );
else // text.
return s_bSortAscending ? lstrcmp( strItem1, strItem2 ) : lstrcmp( strItem2, strItem1 );
}
void CSiriusView::OnUpdateFilterRemoveSorting(CCmdUI* pCmdUI)
{
pCmdUI->Enable((0 <= s_iSortColumn) ? TRUE : FALSE);
}
void CSiriusView::OnFilterRemoveSorting()
{
s_iSortColumn = -1;
m_ctlHeader.SetSortArrow(s_iSortColumn, s_bSortAscending );
}
void CSiriusView::OnUpdateFilterTcp(CCmdUI* pCmdUI)
{
pCmdUI->Enable();
pCmdUI->SetCheck(g_Settings.m_bFilterTCP);
}
void CSiriusView::OnUpdateFilterUdp(CCmdUI* pCmdUI)
{
pCmdUI->Enable();
pCmdUI->SetCheck(g_Settings.m_bFilterUDP);
}
void CSiriusView::OnUpdateFilterIcmp(CCmdUI* pCmdUI)
{
pCmdUI->Enable();
pCmdUI->SetCheck(g_Settings.m_bFilterICMP);
}
void CSiriusView::OnFilterTcp()
{
g_Settings.m_bFilterTCP = !g_Settings.m_bFilterTCP;
if (g_Settings.m_bFilterTCP) // we are about to remove all filtered
g_TrafficList.FilterProto(6); // 6 = TCP
}
void CSiriusView::OnFilterUdp()
{
g_Settings.m_bFilterUDP = !g_Settings.m_bFilterUDP;
if (g_Settings.m_bFilterUDP) // we are about to remove all filtered
g_TrafficList.FilterProto(17); // 17 = UDP
}
void CSiriusView::OnFilterIcmp()
{
g_Settings.m_bFilterICMP = !g_Settings.m_bFilterICMP;
if (g_Settings.m_bFilterICMP) // we are about to remove all filtered
g_TrafficList.FilterProto(1); // 1 = ICMP
}