📄 jiemiandlg.cpp
字号:
// JIEMIANDlg.cpp : implementation file
//
#include "stdafx.h"
#include "JIEMIAN.h"
#include "JIEMIANDlg.h"
#include "Ping.h"
#include "SYNPing.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//Global Various
//Thread
int nThreadCount;
int m_nCounter;
CString strCurIP;
HWND hDlg;
bool bIsStopPing;
//Check Box
BOOL bIsScanByhand;
bool bIsSYN;
bool bIsShowResHost;
//Tree
CImageList m_ImageList;
CTreeCtrl *m_GbTree;
HTREEITEM m_hRoot;
HTREEITEM m_hCurrentItem;
bool bIsFocused;
//IP
DWORD dStartIP,dEndIP,dCurIP;
CString dFileIP[1000];
int nLineCount;
BOOL bIsSpecScan;
//Port
int nStartPort,nEndPort;
//Radio
int nIpWay; //0代表单IP扫描,1代表多IP扫描,2代表文件导入IP扫描
BOOL bIsConnect; //TRUE for Connect Method ,FALSE for SYN
BOOL bIsSinglePort; //True for Single port ,FALSE for multi ports
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)
//}}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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJIEMIANDlg dialog
CJIEMIANDlg::CJIEMIANDlg(CWnd* pParent /*=NULL*/)
: CDialog(CJIEMIANDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CJIEMIANDlg)
m_iThCount = 0;
m_bPingOnly = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CJIEMIANDlg::~CJIEMIANDlg()
{
WSACleanup();
}
void CJIEMIANDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CJIEMIANDlg)
DDX_Control(pDX, IDC_STOP_SCAN, m_stopScan);
DDX_Control(pDX, IDC_START_SCAN, m_startScan);
DDX_Control(pDX, IDC_SCANPORT, m_scanPort);
DDX_Control(pDX, IDC_SCANIP, m_scanIP);
DDX_Control(pDX, IDC_PROGRESS1, m_progressScan);
DDX_Control(pDX, IDC_PINGIP, m_pingIP);
DDX_Control(pDX, IDC_IPADDRESS_FROM, m_sIP);
DDX_Control(pDX, IDC_IPADDRESS_TO, m_eIP);
DDX_Control(pDX, IDC_IPADDRESS1, m_SingleIP);
DDX_Control(pDX, IDC_TREE1, m_Tree);
DDX_Control(pDX, IDC_SLIDER2, m_Slider);
DDX_Text(pDX, IDC_THREAD_COUNT, m_iThCount);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CJIEMIANDlg, CDialog)
//{{AFX_MSG_MAP(CJIEMIANDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_MULTI_IP, OnMultiIp)
ON_BN_CLICKED(IDC_SINGLE_IP, OnSingleIp)
ON_BN_CLICKED(IDC_FILE_IP, OnFileIp)
ON_BN_CLICKED(IDC_OPENFILE, OnOpenfile)
ON_BN_CLICKED(IDC_SINGLE_PORT, OnSinglePort)
ON_BN_CLICKED(IDC_MULTI_PORT, OnMultiPort)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
ON_BN_CLICKED(IDC_SCAN_BYHAND, OnScanByhand)
ON_BN_CLICKED(IDC_START_SCAN, OnStartScan)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_NOTIFY(NM_SETFOCUS, IDC_TREE1, OnSetfocusTree1)
ON_NOTIFY(NM_KILLFOCUS, IDC_TREE1, OnKillfocusTree1)
ON_BN_CLICKED(IDC_SCAN_SELECTED, OnScanSelected)
ON_BN_CLICKED(IDC_WAYSYN, OnWaysyn)
ON_BN_CLICKED(IDC_WAYCONN, OnWayconn)
ON_BN_CLICKED(IDC_STOP_SCAN, OnStopScan)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_SAVESCAN, OnSavescan)
ON_BN_CLICKED(IDC_SHOW_RESHOST, OnShowReshost)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJIEMIANDlg message handlers
BOOL CJIEMIANDlg::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
StateInital();
return TRUE; // return TRUE unless you set the focus to a control
}
void CJIEMIANDlg::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 CJIEMIANDlg::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 CJIEMIANDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CJIEMIANDlg::OnMultiIp()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(FALSE);
GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(TRUE);
GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(TRUE);
GetDlgItem(IDC_OPENFILE)->EnableWindow(FALSE);
nIpWay = 1;
}
void CJIEMIANDlg::OnSingleIp()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(TRUE);
GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(FALSE);
GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(FALSE);
GetDlgItem(IDC_OPENFILE)->EnableWindow(FALSE);
nIpWay = 0;
// GetDlgItem(IDC_IPADDRESS1)->SetAddress()
}
void CJIEMIANDlg::OnFileIp()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_IPADDRESS1)->EnableWindow(FALSE);
GetDlgItem(IDC_IPADDRESS_FROM)->EnableWindow(FALSE);
GetDlgItem(IDC_IPADDRESS_TO)->EnableWindow(FALSE);
GetDlgItem(IDC_OPENFILE)->EnableWindow(TRUE);
nIpWay =2;
}
void CJIEMIANDlg::OnOpenfile()
{
// TODO: Add your control notification handler code here
CFileDialog IPFile(TRUE,"*.txt",NULL,OFN_ALLOWMULTISELECT,"IPfile(*.txt)|*.txt|All(*.*)|*.*||,NULL");
if(IPFile.DoModal()==IDOK)
{
SetDlgItemText(IDC_IPFILE,IPFile.GetPathName());
}
}
void CJIEMIANDlg::OnSinglePort()
{
// TODO: Add your control notification handler code here
((CButton *)GetDlgItem(IDC_SINGLE_PORT))->SetCheck(TRUE);
GetDlgItem(IDC_SPORT)->EnableWindow(false);
GetDlgItem(IDC_EPORT)->EnableWindow(false);
GetDlgItem(IDC_PORT)->EnableWindow(TRUE);
CString temp;
GetDlgItem(IDC_PORT)->GetWindowText(temp);
if (temp == "")
SetDlgItemInt(IDC_PORT,0,TRUE);
GetDlgItem(IDC_PORT)->SetFocus();
}
void CJIEMIANDlg::OnMultiPort()
{
// TODO: Add your control notification handler code here
((CButton *)GetDlgItem(IDC_MULTI_PORT))->SetCheck(TRUE);
GetDlgItem(IDC_SPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_EPORT)->EnableWindow(TRUE);
GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
CString temp;
GetDlgItem(IDC_SPORT)->GetWindowText(temp);
if (temp == "")
SetDlgItemInt(IDC_SPORT,0,TRUE);
GetDlgItem(IDC_EPORT)->GetWindowText(temp);
if (temp == "")
SetDlgItemInt(IDC_EPORT,0,TRUE);
GetDlgItem(IDC_SPORT)->SetFocus();
}
void CJIEMIANDlg::OnCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int pos;
pos = m_Slider.GetPos();
m_iThCount =201 - pos;
UpdateData(FALSE);
nThreadCount = m_iThCount;
*pResult = 0;
}
//************************************************************
// 函数名:StateInital
// 功能描述:对界面中用到的变量,函数等进行初始化
// 参数:无
// 返回:无
//************************************************************
void CJIEMIANDlg::StateInital()
{
//Net
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
AfxMessageBox("WSAStartup() failed");
return ;
}
//Thread
hDlg = GetSafeHwnd();
dCurIP = 0;
bIsStopPing = false;
//Slider
nThreadCount = m_iThCount = 50;
m_Slider.SetRange(1,200);
m_Slider.SetTicFreq(20);
m_Slider.SetPos(201 - m_iThCount);
//RadioBox
((CButton *)GetDlgItem(IDC_WAYCONN))->SetCheck(TRUE);
((CButton *)GetDlgItem(IDC_SINGLE_IP))->SetCheck(true);
OnSingleIp();
OnSinglePort();
//CheckBox
bIsScanByhand = FALSE;
bIsShowResHost = false;
bIsSYN = false;
//Tree
m_ImageList.Create(16, 16, ILC_COLORDDB | ILC_MASK, 0, 1);
CBitmap bitHost,bitPort,bitLiveHost,bitDeadHost;
bitHost.LoadBitmap(IDB_HOST);
bitPort.LoadBitmap(IDB_LIVEPORT);
bitLiveHost.LoadBitmap(IDB_LIVEHOST);
bitDeadHost.LoadBitmap(IDB_DEADHOST);
m_ImageList.Add(&bitHost, RGB(192,192,192));
m_ImageList.Add(&bitPort , RGB(192,192,192));
m_ImageList.Add(&bitLiveHost , RGB(192,192,192));
m_ImageList.Add(&bitDeadHost , RGB(192,192,192));
m_Tree.SetImageList(&m_ImageList ,TVSIL_NORMAL);
m_hCurrentItem = m_hRoot = m_Tree.InsertItem("主机IP",0,0);
m_GbTree = &m_Tree;
bIsFocused = false;
//Ip
nLineCount = 0;
bIsSpecScan =FALSE;
m_nCounter = -1;
//ProgressCtrl
}
//
//************************************************************
// 函数名:OnScanByhand
// 功能描述:用全局布尔变量控制
// 参数:无
// 返回:无
//************************************************************
void CJIEMIANDlg::OnScanByhand()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_SHOW_RESHOST)->EnableWindow(bIsScanByhand);
GetDlgItem(IDC_SCAN_SELECTED)->EnableWindow(!bIsScanByhand);
bIsScanByhand=!bIsScanByhand; //每选一次,取反
if (!bIsScanByhand)
bIsSpecScan = false;
}
//************************************************************
// 函数名:OnStartScan
// 功能描述:开始扫描
// 参数:无
// 返回:无
//************************************************************
void CJIEMIANDlg::OnStartScan()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_START_SCAN)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP_SCAN)->EnableWindow(true);
if (!GetIP())
return;
if (!GetPort())
return;
nThreadCount = m_iThCount;
m_nCounter = nStartPort;
bIsSpecScan = false;
bIsStopPing = false;
m_GbTree->DeleteAllItems();
m_hRoot = m_Tree.InsertItem("主机IP",0,0);
m_progressScan.SetRange32(0,nEndPort - nStartPort + 1);
m_progressScan.SetPos(0);
m_progressScan.SetStep(1);
AfxBeginThread(ThreadManager,GetSafeHwnd());
}
//************************************************************
// 函数名:GetIP
// 功能描述:获取IP,并将初始IP分别保留在dStartIP与dEndIP
// 如果是从文件导入IP则保存在dFileIP
// 参数:无
// 返回:bool
//************************************************************
bool CJIEMIANDlg::GetIP()
{
//单IP
if(nIpWay == 0)
{
m_SingleIP.GetAddress(dStartIP);
return true;
}
//多IP
if(nIpWay == 1)
{
m_sIP.GetAddress(dStartIP);
m_eIP.GetAddress(dEndIP);
if(dEndIP<dStartIP)
{
MessageBox("请正确填写的IP地址",_T("错误"),MB_ICONWARNING|MB_OK);
GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
return false;
}
return true;
}
//文件读取
if(nIpWay == 2)
{
CStdioFile m_file;
CString strIP;
CString f[4];
// char temp;
//int nLen,nSeg=0;
CString FileName;
GetDlgItemText(IDC_IPFILE,FileName);
if(!m_file.Open(FileName,CFile::modeRead,NULL))
{
MessageBox("打不开文件",_T("错误"),MB_ICONWARNING|MB_OK);
GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
return false;
}
nLineCount = 0;
while(m_file.ReadString( strIP ))
{
strIP.TrimLeft();
strIP.TrimRight();
if(strIP.IsEmpty())
continue;
int index1 = strIP.Find('.');
int index2 = strIP.Find('.',index1+1);
int index3 = strIP.Find('.',index2+1);
if ((index1 == -1)||(index2 == -1)||(index3 == -1))
{
AfxMessageBox("IP格式有错!");
GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
return false;
}
f[0] = strIP.Mid(0,index1);
f[1] = strIP.Mid(index1+1,index2 - index1 - 1);
f[2] = strIP.Mid(index2+1,index3 - index2 - 1);
f[3] = strIP.Mid(index3+1,strIP.GetLength() - index3);
//对每个IP验证
for(int i = 0 ; i<4 ;i++)
{
if(atoi(f[i])>255 || atoi(f[i])<0)
{
MessageBox("文件IP数据错误",_T("错误"),MB_ICONWARNING|MB_OK);
GetDlgItem(IDC_START_SCAN)->EnableWindow(TRUE);
return false;
}
else
{
if(i != 3)
{
dFileIP[nLineCount] += f[i];
dFileIP[nLineCount] += ".";
}
else
{
dFileIP[nLineCount] += f[i];
}
}
}
nLineCount++;
}
return true;
}
return true;
}
//************************************************************
// 函数名:IPChange
// 功能描述:可以将DWORD的IP转换成CString型的IP
// 参数:DWORD IP
// 返回:CString str
//************************************************************
CString IPChange(DWORD IP)
{
CString str;
str.Format("%d.%d.%d.%d",
HIBYTE(HIWORD(IP)),LOBYTE(HIWORD(IP)),
HIBYTE(LOWORD(IP)),LOBYTE(LOWORD(IP)));
return str;
}
//************************************************************
// 函数名:ThreadScan
// 功能描述:扫描单个IP的端口的线程函数
// 参数:LPVOID pParam
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -