📄 ipscandlg.cpp
字号:
// IPScanDlg.cpp : implementation file
//
#include "stdafx.h"
#include "IPScan.h"
#include "IPScanDlg.h"
#include "packet32.h"
#include "ntddndis.h"
#include "Process.h"
#include "DataStruct.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern WCHAR AdapterList[][256];
extern UCHAR MACAddress[];
extern PARASEND pmSend;
extern int iHostCount;
extern BOOL WideCharToANSI(char *pDst, unsigned short *pSrc);
extern void ULToString(TCHAR *pDst, ULONG* pUL);
extern BOOL SetLocalIP(WCHAR *pAdapterName, ULONG ulIP, ULONG ulMask);
extern void GetMacFromString(UCHAR *pDst, TCHAR* pSrc);
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CIPScanDlg dialog
CIPScanDlg::CIPScanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIPScanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CIPScanDlg)
m_strMyIP = _T("");
m_strMask = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CIPScanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIPScanDlg)
DDX_Control(pDX, IDC_IPLIST, m_lcIPList);
DDX_Text(pDX, IDC_LOCALIP, m_strMyIP);
DDX_Text(pDX, IDC_MASK, m_strMask);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CIPScanDlg, CDialog)
//{{AFX_MSG_MAP(CIPScanDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEARCH, OnSearch)
ON_MESSAGE(WM_REFRESH, OnRefresh)
ON_MESSAGE(WM_ENDSEARCH, OnEndSearch)
ON_BN_CLICKED(IDC_SET, OnSet)
ON_NOTIFY(NM_CLICK, IDC_IPLIST, OnClickIPlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIPScanDlg message handlers
BOOL CIPScanDlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// 得到网卡的相关配置
GetNetInfo();
// 设置列表框的样式
m_lcIPList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_lcIPList.InsertColumn(0, _T("IP地址"), LVCFMT_CENTER, 130);
m_lcIPList.InsertColumn(1, _T("MAC地址"), LVCFMT_CENTER, 200);
m_lcIPList.InsertColumn(2, _T("状态"), LVCFMT_CENTER, 100);
iCurSel = -1;
return TRUE; // return TRUE unless you set the focus to a control
}
void CIPScanDlg::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 CIPScanDlg::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();
}
}
HCURSOR CIPScanDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT WINAPI SearchIP(PVOID pvParams)
{
ETHDR eth;
ARPHDR arp;
char sendbuf[42];
ULONG ulDstIP;
BOOL bRecvAllEcho;
LPADAPTER lpadapter = 0;
LPPACKET lppackets = 0;
int iOpen = ((CIPScanApp *)AfxGetApp())->iOpen;
lpadapter = PacketOpenAdapter((TCHAR *)(AdapterList[iOpen]));
if ((lppackets=PacketAllocatePacket()) == FALSE)
{
AfxMessageBox(_T("PacketAllocatePacket error!"), MB_ICONSTOP);
return 1;
}
while (pmSend.bContinue)
{
bRecvAllEcho = TRUE;
for (ulDstIP = pmSend.ulStartIP; ulDstIP != pmSend.ulEndIP; ulDstIP += 0x01000000)
{
// 向从ulStartup到ulEndIP的网段发送ARP request
// 判断ulStartup标识的主机是否已经发来ARP reply,若是,该轮循环可免
if (pmSend.pStatus[ntohl(ulDstIP) - ntohl(pmSend.ulStartIP)])
continue;
// 初始化帧头部
memset(eth.eh_dst, 0xff, 6);
memcpy(eth.eh_src, MACAddress, 6);
eth.eh_type = htons(ETH_ARP);
// 填充即将发送的ARP包体
arp.arp_hdr=htons(ARP_HARDWARE);
arp.arp_pro=htons(ETH_IP);
arp.arp_hln=6;
arp.arp_pln=4;
arp.arp_opt=htons(ARP_REQUEST);
memcpy(arp.arp_sha, MACAddress, 6);
arp.arp_spa = pmSend.ulMyIP;
memset(arp.arp_tha, 0xff, 6);
arp.arp_tpa = ulDstIP;
memcpy(sendbuf, ð, sizeof(eth));
memcpy(sendbuf + sizeof(eth), &arp, sizeof(arp));
// 发送arp包
PacketInitPacket(lppackets,sendbuf,sizeof(eth)+sizeof(arp));
if (PacketSendPacket(lpadapter,lppackets,TRUE)==FALSE)
{
AfxMessageBox(_T("PacketSendPacket error!"), MB_ICONSTOP);
return 1;
}
bRecvAllEcho = FALSE;
}
if (bRecvAllEcho)
break;
}
PacketFreePacket(lppackets);
PacketCloseAdapter(lpadapter);
return 0;
}
// 数据包分析函数,若接收到ARP-REPLY包,返回TRUE,否则返回FALSE
BOOL ProcessPacket(LPPACKET lpPacket)
{
ULONG ulBytesReceived = lpPacket->ulBytesReceived;
char *pChar, *buf = (char *)lpPacket->Buffer;
UINT tlen, off=0;
bpf_hdr *hdr;
PETHDR pEth;
PARPHDR pArp;
BOOL bResult = FALSE;
while (off < ulBytesReceived)
{
hdr = (struct bpf_hdr *)(buf + off);
tlen = hdr->bh_caplen;
off += hdr->bh_hdrlen; // off向下偏移包头长度
pChar = (char *)(buf + off); // pChar指向数据包体
off = Packet_WORDALIGN(off + tlen); // off再向下挪动数据包体大小的长度,指向下一个数据包
// 由pChar指向的,长度为tlen的缓冲区即为一个数据包
// 现对其进行分析
pEth = (PETHDR)pChar;
pArp = (PARPHDR)(pChar + sizeof(ETHDR));
if (pEth->eh_type != ntohs(ETH_ARP) || pArp->arp_opt != ntohs(ARP_REPLY))
continue; // 不是ARP Reply,放弃该包
if (ntohl(pArp->arp_spa) >= ntohl(pmSend.ulStartIP) &&
ntohl(pArp->arp_spa) < ntohl(pmSend.ulEndIP))
{
// 接收到一个ARP_REPLY
PARARFR paraRFR;
paraRFR.iIndex = ntohl(pArp->arp_spa) - ntohl(pmSend.ulStartIP);
pmSend.pStatus[paraRFR.iIndex] = 1;
// 取得MAC地址
_stprintf(paraRFR.sMac, _T("%.2x-%.2x-%.2x-%.2x-%.2x-%.2x"),
pEth->eh_src[0],
pEth->eh_src[1],
pEth->eh_src[2],
pEth->eh_src[3],
pEth->eh_src[4],
pEth->eh_src[5]);
SendMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_REFRESH, NULL, (LPARAM)¶RFR);
bResult = TRUE;
}
}
return bResult;
}
UINT WINAPI PackRecv(PVOID pvParams)
{
LPADAPTER lpadapter = 0;
LPPACKET lppacketr = 0;
char recvbuf[16*1024];
DWORD dwRecvRecent = GetTickCount();
int iOpen = ((CIPScanApp *)AfxGetApp())->iOpen;
lpadapter = PacketOpenAdapter((TCHAR *)(AdapterList[iOpen]));
if (PacketSetHwFilter(lpadapter, NDIS_PACKET_TYPE_DIRECTED) == FALSE)
{
AfxMessageBox(_T("PacketSetHwFilter failed!"), MB_ICONSTOP);
return 1;
}
if (PacketSetBuff(lpadapter,256*1024)==FALSE)
{
AfxMessageBox(_T("PacketSetBuff error!"), MB_ICONSTOP);
return 1;
}
if (PacketSetReadTimeout(lpadapter,1000)==FALSE)
{
AfxMessageBox(_T("Warning: Unable to set the timeout!"), MB_ICONSTOP);
}
if ((lppacketr=PacketAllocatePacket())==FALSE)
{
AfxMessageBox(_T("PacketAllocatePacket receive error!"), MB_ICONSTOP);
return 1;
}
PacketInitPacket(lppacketr,(char *)recvbuf,sizeof(recvbuf));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -