⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logresult.cpp

📁 ARP test mode. According to the idea we design the arithmetic for the key part, first the system sen
💻 CPP
字号:
// LOGResult.cpp : implementation file
//

#include "stdafx.h"
#include "LANDetect.h"
#include "LOGResult.h"
#include "Log.h"
#include <win32/libnet.h>
#include "ARP.h"
#include   <lm.h>   
#include   <lmmsg.h> 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLOGResult dialog
HWND result_hnd0;
BOOL pflag0;
CLOGResult::CLOGResult(CWnd* pParent /*=NULL*/)
	: CDialog(CLOGResult::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLOGResult)
	m_send = _T("");
	//}}AFX_DATA_INIT
}


void CLOGResult::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLOGResult)
	DDX_Control(pDX, IDC_LIST_LOG_RESULT, m_lresult);
	DDX_Text(pDX, IDC_EDIT_SEND, m_send);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLOGResult, CDialog)
	//{{AFX_MSG_MAP(CLOGResult)
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_SENDALL, OnButtonSendall)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLOGResult message handlers

BOOL CLOGResult::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//按钮设置
	GetDlgItem(IDC_BUTTON_START)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(false);
	GetDlgItem(IDCANCEL)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_SENDALL)->EnableWindow(false);
	//设置Control List控件
	DWORD dwStyle=GetWindowLong(m_lresult.GetSafeHwnd(),GWL_STYLE);
	dwStyle&=~LVS_TYPEMASK;
	dwStyle|=LVS_REPORT;
	SetWindowLong(m_lresult.GetSafeHwnd(),GWL_STYLE,dwStyle);

    m_lresult.InsertColumn(0,"IP地址",LVCFMT_LEFT,120);
	m_lresult.InsertColumn(1,"主机MAC地址",LVCFMT_LEFT,180);
	m_lresult.InsertColumn(2,"时间",LVCFMT_LEFT,180);
    
	m_lresult.SetExtendedStyle(LVS_EX_GRIDLINES);
    ::SendMessage(m_lresult.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
      LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);	

	result_hnd0=GetDlgItem(IDC_LIST_LOG_RESULT)->m_hWnd;    //获得控件的句柄
	m_send="请停止监测,并尽快与管理员取得联系,否则将停止你的网络访问权限!";
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CLOGResult::Insert(CString ip,CString mac_str,CString time)
{
	m_lresult.m_hWnd=result_hnd0;
	m_lresult.InsertItem(0,ip,0);
	m_lresult.SetItemText(0,1,mac_str);
	m_lresult.SetItemText(0,2,time);
	if(m_db.IsOpen())
		m_db.Close();
	CString sql;
	sql="select * from Record";   
	m_db.Open(CRecordset::dynaset,_T(sql));
	m_db.AddNew();
	//m_db.m_Sno=1;
	m_db.m_DIP=ip;
	m_db.m_DMAC=mac_str;
	m_db.m_DTimes=time;
	m_db.Update();
	m_db.Requery();
}
/*
=======================================================================================================================
下面是实现ARP协议分析的函数,函数类型与回调函数相同
=======================================================================================================================
 */
void arp_protocol_packet_callback0(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)
{
    struct arp_header *arp_protocol;
    u_short operation_code;
    struct in_addr source_ip_address;
	u_char *mac_string;
    arp_protocol = (struct arp_header*)(packet_content + 14);
    operation_code = ntohs(arp_protocol->arp_operation_code);
	CString filter_ip;
	filter_ip.Format("%d.%d.%d.%d",m1[0],m1[1],m1[2],1);
	memcpy((void*) &source_ip_address, (void*) &arp_protocol->arp_source_ip_address, sizeof(struct in_addr));
    CString ip,time;
	ip=inet_ntoa(source_ip_address);
	mac_string = arp_protocol->arp_source_ethernet_address;
	CString mac_str;
	mac_str.Format("%02x-%02x-%02x-%02x-%02x-%02x", *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5));
	if(operation_code == 2&&filter_ip!=ip)
	{
		SYSTEMTIME st = {0}; 
		GetLocalTime(&st);//获得当前本地时间 
		time.Format("%d-%d-%d %d:%d:%d",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
		CLOGResult p;
		p.Insert(ip,mac_str,time);
		Sleep(500);
	}
	
}

UINT ReceivePacket0(LPVOID param)
{
    pcap_t *pcap_handle;
    /* Winpcap句柄 */
	char error_content[PCAP_ERRBUF_SIZE];
    /* 存储错误信息 */
    struct bpf_program bpf_filter;	
    /* BPF过滤规则 */
    char *bpf_filter_string;
	CString filt = "dst host "+myIP+" and arp";
	bpf_filter_string=(char *)(LPCSTR)filt;
    /* 过滤规则字符串 */
   // bpf_u_int32 net_mask;
    /* 掩码 */
    bpf_u_int32 net_ip;
    /* 网路地址 */
    //pcap_handle = pcap_open_live(net_interface, BUFSIZ, 1, 1, error_content);
	if((pcap_handle=pcap_open_live(dev->name,65536,1,1000,error_content))==NULL){//若网卡无法绑定
		AfxMessageBox("网卡打开失败!");
		return -1;
	}
    /* 打开网路接口,设成混杂模式 */
    pcap_compile(pcap_handle, &bpf_filter, bpf_filter_string, 0, net_ip);
    /* 编译BPF过滤规则 */
    pcap_setfilter(pcap_handle, &bpf_filter);
    /* 设置过滤规则 */
    if (pcap_datalink(pcap_handle) != DLT_EN10MB)
        return 0;
    do{
	pcap_loop(pcap_handle, 1, arp_protocol_packet_callback0, NULL);
	}
	while(!pflag0);
    /* 注册回调函数,循环捕获网络数据包,利用回调函数来处理每个数据包 */
	pcap_close(pcap_handle);
    /* 关闭Winpcap操作 */
	return 0;
}

UINT SendPacket0(LPVOID param)       //发送报文
{
	libnet_t *l;
	libnet_ptag_t protocol_tag;
	char *device=NULL;
	char error_information[LIBNET_ERRBUF_SIZE];
	char *destination_ip_str;           //目的IP地址
	char *source_ip_str=(char *)(LPCSTR)myIP;
	u_char hardware_source[6]={0x00,0x1A,0x6B,0x5D,0x43,0x70};
	u_char hardware_destination[6];
	u_long destination_ip;
	u_long source_ip;
	for(int i=1;i<=6;i++)      //获取MAC地址
	{
		hardware_destination[i-1]=GetMAC(i,bmac);
	}
	source_ip=libnet_name2addr4(l,source_ip_str,LIBNET_RESOLVE);
	for(i=0;i<Lognum;i++)
	{
		l=libnet_init(
		LIBNET_LINK_ADV,
		device,
		error_information);
		destination_ip_str=(char *)(LPCSTR)LIP[i];
		destination_ip=libnet_name2addr4(l,destination_ip_str,LIBNET_RESOLVE);
		protocol_tag=libnet_build_arp(
			ARPHRD_ETHER,
			ETHERTYPE_IP,
			6,
			4,
			ARPOP_REQUEST,
			hardware_source,
			(u_int8_t *)&source_ip,
			hardware_destination,
			(u_int8_t *)&destination_ip,
			NULL,
			0,
			l,
			0
			);
		protocol_tag=libnet_autobuild_ethernet(
			hardware_destination,
			ETHERTYPE_ARP,
			l
			);
		libnet_write(l);
		libnet_destroy(l);
	}
		Sleep(1000);
	//SetEnd();
	return 0;
}

CWinThread *r0,*s0;
void CLOGResult::OnButtonStart() 
{
	//按钮设置
	GetDlgItem(IDC_BUTTON_START)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(true);
	GetDlgItem(IDCANCEL)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_SENDALL)->EnableWindow(false);
	//发送探测报文,并接收响应报文
	pflag0=false;
	m_lresult.DeleteAllItems();
	r0=AfxBeginThread(AFX_THREADPROC(ReceivePacket0),(LPVOID)this);//启动收包线程
	if(frequency==0)
		s0=AfxBeginThread(AFX_THREADPROC(SendPacket0),(LPVOID)this);
	else
	{
		s0=AfxBeginThread(AFX_THREADPROC(SendPacket0),(LPVOID)this);
		if(!pflag0)
			SetTimer(1,sec*1000,NULL);
		else
			return;
	}
}

void CLOGResult::OnButtonStop() 
{
	pflag0=true;
	GetDlgItem(IDC_BUTTON_START)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow(false);
	GetDlgItem(IDCANCEL)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_SENDALL)->EnableWindow(true);
	if(r0!=NULL)
	{
	TerminateThread(r0->m_hThread,0);
	CloseHandle(r0->m_hThread);
	}
	if(s0!=NULL)
	{
	TerminateThread(s0->m_hThread,0);
	CloseHandle(s0->m_hThread);
	}
}

void CLOGResult::OnCancel() 
{	
	if(r0!=NULL)
	{
	TerminateThread(r0->m_hThread,0);
	CloseHandle(r0->m_hThread);
	}
	if(s0!=NULL)
	{
	TerminateThread(s0->m_hThread,0);
	CloseHandle(s0->m_hThread);
	}
	CDialog::OnCancel();
}

void CLOGResult::OnButtonSend()       //发送警告信息
{
	POSITION pos=m_lresult.GetFirstSelectedItemPosition();
	if(pos==NULL)
	{
		AfxMessageBox("请从监测结果列表中选择一项!");
		return;
	}
    int SelIndex = m_lresult.GetNextSelectedItem(pos);
	CString str;     //记录要发送的主机IP
	str=m_lresult.GetItemText(SelIndex,0);
	UpdateData(true);
	wchar_t   wszbuf[500];
	wchar_t   wsztarget[20];
	int   len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,NULL,0);   
    MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,wsztarget,len);   
    len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,m_send,-1,NULL,0);   
    MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,m_send,-1,wszbuf,len);   
    NetMessageBufferSend(NULL,wsztarget,NULL,(LPBYTE)wszbuf,m_send.GetLength()); 
	AfxMessageBox("发送成功!");
}

void CLOGResult::OnButtonSendall() 
{
	UpdateData(true);
	if(m_lresult.GetItemCount()==0)
	{
		AfxMessageBox("列表中无待发送主机!");
		return;
	}
	wchar_t   wszbuf[500];
	wchar_t   wsztarget[20];  
    int len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,m_send,-1,NULL,0);   
    MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,m_send,-1,wszbuf,len); 
	for(int i=0;i<m_lresult.GetItemCount();i++)
	{
		CString str;    //获取IP
		str=m_lresult.GetItemText(i,0);
		len=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,NULL,0);   
        MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,wsztarget,len); 
		NetMessageBufferSend(NULL,wsztarget,NULL,(LPBYTE)wszbuf,m_send.GetLength());
	}
	AfxMessageBox("发送成功!");
}

void CLOGResult::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	s0=AfxBeginThread(AFX_THREADPROC(SendPacket0),(LPVOID)this);
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -