📄 ipfluxdlg.cpp
字号:
// IPFluxDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IPFlux.h"
#include "IPFluxDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 声明两个线程函数
UINT _tFuncReceiver(void *pData);
UINT _tFuncSender(void *pData);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 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)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIPFluxDlg dialog
CIPFluxDlg::CIPFluxDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIPFluxDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CIPFluxDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
m_bStop = FALSE;
memset ((void*)m_mac, 0, sizeof(m_mac));
}
CIPFluxDlg::~CIPFluxDlg()
{
CloseHandle (m_hEvent);
// CloseHandle (m_hSender);
// CloseHandle (m_hSniffer);
}
void CIPFluxDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIPFluxDlg)
DDX_Control(pDX, IDC_IP_LOCAL, m_IPLocal);
DDX_Control(pDX, IDC_BTN_START, m_btnStart);
DDX_Control(pDX, IDC_IP_LAST, m_IPLast);
DDX_Control(pDX, IDC_IP_FIRST, m_IPFirst);
DDX_Control(pDX, IDC_LIST_IP, m_lstIP);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CIPFluxDlg, CDialog)
//{{AFX_MSG_MAP(CIPFluxDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIPFluxDlg message handlers
BOOL CIPFluxDlg::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
InitListView();
// MessageBox ("Trap1");
CString strIP, strMAC;
sockaddr_in ipLocal;
ULONG ipaddr;
// {获得并显示本机IP地址,MAC地址
wpcap.GetNetInfo(ipLocal, strMAC, m_mac);
ipaddr = ntohl ((ULONG)(ipLocal.sin_addr.S_un.S_addr));
m_ipMine = ipaddr;
m_IPLocal.SetAddress(ipaddr);
SetDlgItemText(IDC_ST_MAC, (LPCTSTR)strMAC);
// }
BYTE p1, p2, p3, p4;
m_IPLocal.GetAddress(p1, p2, p3, p4);
p4 = 0;
m_IPFirst.SetAddress(p1, p2, p3, p4);
p4 = 255;
m_IPLast.SetAddress(p1, p2, p3, p4);
return TRUE; // return TRUE unless you set the focus to a control
}
void CIPFluxDlg::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 CIPFluxDlg::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 CIPFluxDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CIPFluxDlg::InitListView()
{
int res = 0;
m_lstIP.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
res = m_lstIP.InsertColumn(0, "NO.", LVCFMT_LEFT, 30);
res = m_lstIP.InsertColumn(1, "IP地址", LVCFMT_LEFT, 120);
res = m_lstIP.InsertColumn(2, "MAC地址", LVCFMT_LEFT, 120);
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CString str;
GetDlgItemText (IDC_ST_VERSION, str);
str = str + "\nWinpcap Lib version" + PacketGetVersion();
SetDlgItemText (IDC_ST_VERSION, str);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CIPFluxDlg::OnBtnStart()
{
int nTotal = 0;
ULONG ipFirst, ipLast;
ULONG ulStartIP = 0;
m_bStop = FALSE;
static BOOL bInit = FALSE;
m_IPFirst.GetAddress(ipFirst);
m_IPLast.GetAddress(ipLast);
nTotal = ipLast - ipFirst + 1;
m_lstIP.DeleteAllItems();
GetDlgItem (IDC_BTN_START)->EnableWindow (FALSE);
// 生成一次监听线程
if (bInit == FALSE)
{
m_hSniffer = AfxBeginThread(_tFuncReceiver, (void*)this);
bInit = TRUE;
}
Sleep (300);
m_IPFirst.GetAddress(ulStartIP);
for (int i = 0; i < nTotal; i++)
{
m_ipCur = ulStartIP + i;
m_hSender = AfxBeginThread(_tFuncSender, (void*)this);
// 等待上一个线程结束
WaitForSingleObject (m_hSender, INFINITE);
}
m_bStop = TRUE;
GetDlgItem (IDC_BTN_START)->EnableWindow (TRUE);
}
UINT _tFuncSender(void *pData)
{
try{
CIPFluxDlg *pdlg = (CIPFluxDlg *)pData;
pdlg->Sender(pdlg->m_ipCur, pdlg->m_ipMine, pdlg->m_mac);
}
catch (...) {
}
return 0;
}
UINT _tFuncReceiver(void *pData)
{
CIPFluxDlg *pdlg = (CIPFluxDlg *)pData;
try{
pdlg->Receiver();
}
catch(...)
{
}
return 0;
}
void CIPFluxDlg::Sender(ULONG ipCur, ULONG ipMine, BYTE mac[])
{
wpcap.Sender(ipCur, ipMine, mac);
}
void CIPFluxDlg::Receiver()
{
wpcap.Sniff();
}
void CIPFluxDlg::ShowSearch(CString strIP, CString strMAC)
{
int iItem;
CString str;
CListCtrl *pList = (CListCtrl *)GetDlgItem (IDC_LIST_IP);
iItem = pList->GetItemCount();
str.Format ("%03d", iItem + 1);
iItem = pList->InsertItem(iItem, str);
pList->SetItemText(iItem, 1, strIP);
pList->SetItemText(iItem, 2, strMAC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -