📄 portscanerdlg.cpp
字号:
// PortScanerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PortScaner.h"
#include "PortScanerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CPortScanerDlg dialog
CPortScanerDlg::CPortScanerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPortScanerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPortScanerDlg)
m_portlist = _T("");
m_delay = 5000;
m_speed = 100;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
portlist=NULL;
IsScaning=FALSE;
}
void CPortScanerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPortScanerDlg)
DDX_Control(pDX, IDC_TREE, m_tree);
DDX_Control(pDX, IDOK, m_ok);
DDX_Control(pDX, IDC_IPADDRESS2, m_ip2);
DDX_Control(pDX, IDC_IPADDRESS1, m_ip1);
DDX_Text(pDX, IDC_EDIT_PORT, m_portlist);
DDX_Text(pDX, IDC_EDIT_DELAG, m_delay);
DDX_Slider(pDX, IDC_SLIDER, m_speed);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPortScanerDlg, CDialog)
//{{AFX_MSG_MAP(CPortScanerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPortScanerDlg message handlers
BOOL CPortScanerDlg::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
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,2),&WSAData))
{
return FALSE;
}
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CPortScanerDlg::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 CPortScanerDlg::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 CPortScanerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPortScanerDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
speed=100-m_speed+1; //for safety
if(IsScaning)
{
AfxMessageBox("Scaning ...");
return;
}
if(!CheckIP()||!CheckPort())
return;
hSend=AfxBeginThread(SendThread,0);
hRecv=AfxBeginThread(RecvThread,0);
m_tree.DeleteAllItems();
m_tree.InsertItem("Result");
IsScaning=TRUE;
// CDialog::OnOK();
}
BOOL CPortScanerDlg::CheckIP()
{
UpdateData();
if(m_ip1.IsBlank()||m_ip2.IsBlank())
{
MessageBox("Please input IP range !");
return FALSE;
}
m_ip1.GetAddress(IP[0],IP[1],IP[2],IP[3]);
m_ip2.GetAddress(IP[4],IP[5],IP[6],IP[7]);
if(IP[4]>IP[0])
{
MessageBox("Too many IP !");
return FALSE;
}
if(IP[4]<IP[0])
{
AfxMessageBox("No Work to Do !");
return FALSE;
}
if(IP[5]>IP[1])
{
MessageBox("Too many IP !");
return FALSE;
}
if(IP[5]<IP[1])
{
AfxMessageBox("No Work to Do !");
return FALSE;
}
if(IP[6]<IP[2])
{
AfxMessageBox("No Work to Do !");
return FALSE;
}
if(IP[6]==IP[2]&&IP[7]<IP[3])
{
AfxMessageBox("No Work to Do !");
return FALSE;
}
return TRUE;
}
BOOL CPortScanerDlg::CheckPort()
{
UpdateData();
int n=m_portlist.GetLength();
char *portstr=new char[n];
this->nPort=0;
int i=0,j=0,k=0;
if(this->portlist)
delete[] this->portlist;
for(i=0;i<n;i++)
{
portstr[i]=m_portlist[i];
if(m_portlist[i]==',')
this->nPort++;
}
this->portlist=new int[this->nPort+1];
for(i=0;i<n;)
{
if(portstr[i]>'9'||portstr[i]<'0')
{
i++;
continue;
}
portlist[j]=atoi(portstr+i);
//掠过已读进的数字
do
{
k=portstr[i++];
}
while(k<='9'&&k>='0');
if(portlist[j]<0||portlist[j]>65000)
{
AfxMessageBox("Error Port Setting");
return FALSE;
}
else
j++;
}
nPort=j;
if(j==0)
{
AfxMessageBox("Error Port Setting");
return FALSE;
}
return TRUE;
}
void CPortScanerDlg::AddPort(in_addr addr, int port)
{
for(int i=0;i<nPort;i++)
{
if(port==portlist[i])
goto goon;
}
return;
goon:
CString strIP,strPort;
strIP=inet_ntoa(addr);
strPort.Format("%d",port);
HTREEITEM root=m_tree.GetRootItem();
HTREEITEM hCurrent=m_tree.GetChildItem(root);
while (hCurrent != NULL)
{
if(!strnicmp(strIP,m_tree.GetItemText(hCurrent),strIP.GetLength())) //same ip
{
HTREEITEM hChildItem=m_tree.GetChildItem(hCurrent);
while (hChildItem != NULL)
{
if(strPort==m_tree.GetItemText(hChildItem)) //same port
return;
hChildItem=m_tree.GetNextSiblingItem(hChildItem);
}
//new port
m_tree.InsertItem(strPort,hCurrent);
return;
}
hCurrent =m_tree.GetNextSiblingItem(hCurrent);
}
// new ip
hCurrent=m_tree.InsertItem(strIP,root);
m_tree.InsertItem(strPort,hCurrent);
return;
}
void CPortScanerDlg::SendOver()
{
SetTimer(30,m_delay,0);
return;
}
void CPortScanerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(30==nIDEvent&&IsScaning)
{
KillTimer(30);
if(hRecv)
TerminateThread(hRecv->m_hThread,1);
AfxMessageBox("Done!");
this->SetWindowText("PortScaner");
IsScaning =FALSE;
}
CDialog::OnTimer(nIDEvent);
}
void CPortScanerDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(!IsScaning)
{
AfxMessageBox("Already stopped");
return;
}
TerminateThread(hSend->m_hThread,1);
TerminateThread(hRecv->m_hThread,1);
IsScaning =FALSE;
SetWindowText("PortScaner");
}
void CPortScanerDlg::OnButton4()
{
// TODO: Add your control notification handler code here
m_tree.DeleteAllItems();
}
void CPortScanerDlg::ExpandNode(HTREEITEM hti)
{
if(m_tree.ItemHasChildren(hti))
{
m_tree.Expand(hti,TVE_EXPAND);
hti=m_tree.GetChildItem(hti);
do ExpandNode(hti);
while((hti=m_tree.GetNextSiblingItem(hti)));
}
m_tree.EnsureVisible(hti);
}
void CPortScanerDlg::CollapesNode(HTREEITEM hti)
{
if(m_tree.ItemHasChildren(hti))
{
m_tree.Expand(hti,TVE_COLLAPSE);
hti=m_tree.GetChildItem(hti);
do CollapesNode(hti);
while((hti=m_tree.GetNextSiblingItem(hti)));
}
}
void CPortScanerDlg::OnButton2()
{
// TODO: Add your control notification handler code here
ExpandNode(m_tree.GetRootItem());
}
void CPortScanerDlg::OnButton3()
{
// TODO: Add your control notification handler code here
CollapesNode(m_tree.GetRootItem());
}
void CPortScanerDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
CFileDialog cfd(FALSE,".txt","*.txt");
if(IDOK!=cfd.DoModal())
return;
FILE *fp=fopen(cfd.GetPathName(),"w");
if(!fp)
{
MessageBox("Error Create Log File !");
return;
}
HTREEITEM root=m_tree.GetRootItem();
HTREEITEM hCurrent=m_tree.GetChildItem(root);
HTREEITEM hChildItem;
CString msg;
while (hCurrent != NULL)
{
msg=m_tree.GetItemText(hCurrent);
fputc('\n',fp);
fputs(msg,fp);
fputc('\n',fp);
hChildItem=m_tree.GetChildItem(hCurrent);
while (hChildItem != NULL)
{
msg=m_tree.GetItemText(hChildItem);
fputc('\t',fp);
fputs(msg,fp);
fputc('\n',fp);
hChildItem=m_tree.GetNextSiblingItem(hChildItem);
}
hCurrent =m_tree.GetNextSiblingItem(hCurrent);
}
fclose(fp);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -