📄 testprohibitdlg.cpp
字号:
// testProhibitDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testProhibit.h"
#include "testProhibitDlg.h"
#include "Common.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
pcap_t* g_pNetCtrlHandle;
DWORD g_MyIP=0;
DWORD g_GateWayIP=0;
PacttiveIpwithMac g_pMyAddr=NULL;
PacttiveIpwithMac g_pGateAddr=NULL;
PacttiveIpwithMac g_pPreventAddr=NULL;
PacttiveIpwithMac g_pAddrList=NULL;
unsigned int _stdcall sendpackettogetallacttiveIpwithMac(void *x); //向局域网内发arp请求包扫描
unsigned int _stdcall recvpackettogetallacttiveIpwithMac(void *x); //接受并存储局域网的arp响应信息
int g_nNotifyThreadExit=0;
int g_nNotifyResum=0;
unsigned int _stdcall preventinteractionwithother(void*pParam);
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTestProhibitDlg dialog
CTestProhibitDlg::CTestProhibitDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestProhibitDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestProhibitDlg)
// 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);
}
CTestProhibitDlg::~CTestProhibitDlg()
{
FreeAddressList(g_pAddrList);
if (g_pGateAddr!=NULL)
{
delete g_pGateAddr;
g_pGateAddr=NULL;
}
if (g_pMyAddr!=NULL)
{
delete g_pMyAddr;
g_pMyAddr=NULL;
}
if (g_pPreventAddr!=NULL)
{
delete g_pPreventAddr;
g_pPreventAddr=NULL;
}
}
void CTestProhibitDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestProhibitDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestProhibitDlg, CDialog)
//{{AFX_MSG_MAP(CTestProhibitDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_RESUM, OnButtonResum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestProhibitDlg message handlers
BOOL CTestProhibitDlg::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
//xiongwen add 2007-11-13
g_pMyAddr=new acttiveIpwithMac;
g_pMyAddr->next=NULL;
g_pGateAddr=new acttiveIpwithMac;
g_pGateAddr->next=NULL;
g_pPreventAddr=new acttiveIpwithMac;
g_pPreventAddr->next=NULL;
g_pNetCtrlHandle=GetNetCardCtrlHandle(&g_MyIP);
g_GateWayIP=GetGateWayIP(g_MyIP);
g_pMyAddr->ip=g_MyIP;
g_pGateAddr->ip=g_GateWayIP;
//xiongwen add 2007-11-13
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestProhibitDlg::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 CTestProhibitDlg::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 CTestProhibitDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//PacttiveIpwithMac g_pMyAddr=NULL;
//PacttiveIpwithMac g_pGateAddr=NULL;
//PacttiveIpwithMac g_pPreventAddr=NULL;
//
//
void CTestProhibitDlg::OnButtonStart()
{
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_IPADDRESS);
CString str;
pEdit->GetWindowText(str);
g_pPreventAddr->ip=inet_addr(str);
GetLocalHostMac(g_pNetCtrlHandle,g_pMyAddr);
GetMacByIP(g_pNetCtrlHandle,g_pMyAddr,g_pGateAddr);
GetMacByIP(g_pNetCtrlHandle,g_pMyAddr,g_pPreventAddr);
unsigned long sendarphd,recvarphd;
unsigned int sendarpid,recvarpid;
/*扫描局域网内活动主机*/
sendarphd=_beginthreadex(NULL,0,sendpackettogetallacttiveIpwithMac,0,0,&sendarpid);//开启arp扫描包线程
recvarphd=_beginthreadex(NULL,0,recvpackettogetallacttiveIpwithMac,0,0,&recvarpid);//开启接收arp响应包线程
WaitForSingleObject((HANDLE)sendarphd,INFINITE);
CloseHandle((HANDLE)sendarphd);
Sleep(3000);
g_nNotifyThreadExit=1;
WaitForSingleObject((HANDLE)recvarphd,INFINITE);
CloseHandle((HANDLE)sendarphd);
unsigned long prevenhd;
unsigned int preventid;
prevenhd=_beginthreadex(NULL,0,preventinteractionwithother,0,0,&preventid);
}
unsigned int _stdcall preventinteractionwithother(void*pParam)
{
PacttiveIpwithMac pObject;
while (TRUE)
{
for (pObject=g_pAddrList;pObject->next!=NULL;pObject=pObject->next)
{
}
Sleep(10000);
if (g_nNotifyResum)
break;
}
return 1;
}
/*
unsigned int _stdcall preventinteractionwithother(void*pParam)
{
unsigned char szSendEvery[42]={0};
unsigned char szReSendEvery[42]={0};
ETHDR eth;
ARPHDR arp;
PacttiveIpwithMac pObject;
int k=0;
while (!g_nNotifyResum)
{
eth.eh_type=htons(ETH_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_REPLY);
//arp.arp_spa=spip->ip;
//arp.arp_tpa=gateip->ip;
for (pObject=g_pAddrList;pObject->next!=NULL;pObject=pObject->next)
{
char szMsg[100]={0};
sprintf(szMsg,"Send ARP To %s \n",IPToString(pObject->ip));
OutputDebugString(szMsg);
for(k=0;k<6;k++)
{
eth.eh_dst[k]=pObject->mac[k];
eth.eh_src[k]=g_pMyAddr->mac[k];
arp.arp_sha[k]=0x01;
arp.arp_tha[k]=pObject->mac[k];
}
arp.arp_spa=g_pPreventAddr->ip;
arp.arp_tpa=pObject->ip;
memset(szSendEvery,0,sizeof(szSendEvery));
memcpy(szSendEvery,ð,sizeof(eth));
memcpy(szSendEvery+sizeof(eth),&arp,sizeof(arp));
if(pObject->ip!=g_pPreventAddr->ip&&pcap_sendpacket(g_pNetCtrlHandle,szSendEvery,42)!=0)
{
char szMsg[100]={0};
sprintf(szMsg,"Send To %s ErrorCode=%d\n",IPToString(pObject->ip),GetLastError());
OutputDebugString(szMsg);
}
}
Sleep(10000);
}
return 1;
}
*/
unsigned int _stdcall sendpackettogetallacttiveIpwithMac(void *x)
{
ULONG tip,subnetsta,subnetend;
unsigned char sendbuf[42];
int k;
ETHDR eth;
ARPHDR arp;
unsigned long mynetmask=0x00ffffff;
subnetsta=htonl(g_pMyAddr->ip&mynetmask); //计算内网ip起点
subnetend=htonl(htonl(subnetsta)|(~mynetmask)); //计算内网ip结束
for(k=0;k<6;k++)
{
eth.eh_dst[k]=0xff;
eth.eh_src[k]=g_pMyAddr->mac[k];
arp.arp_sha[k]=g_pMyAddr->mac[k];
arp.arp_tha[k]=0x00;
}
eth.eh_type=htons(ETH_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);
arp.arp_spa=g_pMyAddr->ip;
memset(sendbuf,0,sizeof(sendbuf));
memcpy(sendbuf,ð,sizeof(eth));
for(tip=subnetsta;tip<=subnetend;tip++)
{
arp.arp_tpa=htonl(tip);
memcpy(sendbuf+sizeof(eth),&arp,sizeof(arp));
if(pcap_sendpacket(g_pNetCtrlHandle,sendbuf,42)!=0)
{
printf("Getallactive ip PacketSendPacket in getmine Error: %d\n",GetLastError());
return 0;
}
}
return 1;
}
unsigned int _stdcall recvpackettogetallacttiveIpwithMac(void *x)
{
struct pcap_pkthdr * pkt_header;
u_char * pkt_data;
PacttiveIpwithMac p,q;
int i;
while((pcap_next_ex(g_pNetCtrlHandle,&pkt_header,(const u_char**)&pkt_data))>0)
{
if(*(unsigned short *)(pkt_data+12)==htons(ETH_ARP)&&*(unsigned short*)(pkt_data+20)==htons(ARP_REPLY)&&*(unsigned long*)(pkt_data+38)==g_pMyAddr->ip)
{
p=new acttiveIpwithMac;
p->next=NULL;
p->ip=*(unsigned long*)(pkt_data+28);
for(i=0;i<6;i++)
{
p->mac[i]=*(unsigned char*)(pkt_data+22+i);
}
if(g_pAddrList==NULL)
{
g_pAddrList=p;
q=p;
}
else
{
q->next=p;
q=p;
}
if (g_nNotifyThreadExit)
{
break;
}
}
}
PacttiveIpwithMac z,k,j;
if(g_pAddrList!=NULL)
{
/*去处重复的活动主机*/
for(k=g_pAddrList;k->next!=NULL;k=k->next)
for(z=k;z->next!=NULL;)
{
if((k->ip==z->next->ip)&&(macequal(k,z->next)))
{
j=z->next;
z->next=j->next;
delete j;
}
else
z=z->next;
}
/*输出列表*/
for(z=g_pAddrList,i=1;z!=NULL;z=z->next,i++)
{
char szMsg[200]={0};
sprintf(szMsg,"%-3d ip=%-20s mac=%02x:%02x:%02x:%02x:%02x:%02x\n",
i,IPToString(z->ip),z->mac[0],z->mac[1],z->mac[2],z->mac[3],z->mac[4],z->mac[5]);
myDebugPrint(szMsg);
}
}
return 1;
}
void CTestProhibitDlg::OnButtonResum()
{
// TODO: Add your control notification handler code here
g_nNotifyResum=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -