// ArpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "scan.h"
#include "scanDlg.h"
#include "deal.h"
#include "pcap.h"
#include<iostream>
#include<string>
#include <sstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define WM_RECEIVE (WM_USER + 1)
#endif
//以下是对全局变量的引用
extern int g_thred_mark;
extern pcap_if_t* g_all_dev;
extern unsigned char* g_local_mac;
extern pcap_if_t* g_cur_dev;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
END_MESSAGE_MAP()
CArpDlg::CArpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CArpDlg::IDD, pParent)
, info3(_T(""))
, info4(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CArpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//获得对各个控件的控制
DDX_Control(pDX,IDC_ALLADAPTER,all_adapter);
DDX_Control(pDX,IDC_BEGIN,begin);
DDX_Control(pDX,IDC_STOP,stop);
DDX_Control(pDX,IDC_RESULT,result);
DDX_Control(pDX,IDC_INFO,info);
DDX_Text(pDX, IDC_NOTICE, notice);
DDX_Control(pDX, IDC_EDIT1, info1);
DDX_Control(pDX, IDC_EDIT2, info2);
DDX_Text(pDX, IDC_EDIT3, info3);
DDX_Text(pDX, IDC_EDIT4, info4);
}
BEGIN_MESSAGE_MAP(CArpDlg, CDialog)
//{{AFX_MSG_MAP(CArpDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnOpenBrief)
ON_CBN_SELCHANGE(IDC_ALLADAPTER, OnSelchangeAlladapter)
ON_BN_CLICKED(IDC_BEGIN, OnBegin)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_MESSAGE(WM_RECEIVE, OnReceive)//自定义消息的映射
//}}AFX_MSG_MAP
ON_STN_CLICKED(IDC_NOTICE, &CArpDlg::OnStnClickedNotice)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_RESULT, &CArpDlg::OnLvnItemchangedResult)
END_MESSAGE_MAP()
BOOL CArpDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);
SetIcon(m_hIcon, FALSE);
initial();//初始化各个空间
return TRUE;
}
void CArpDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CArpDlg::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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CArpDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/**************************************
函数:打开程序介绍界面
参数:无
返回值:无
***************************************/
void CArpDlg::OnOpenBrief()
{
CAboutDlg introDlg;
introDlg.DoModal();
}
/************************************************************
函数:初始化程序的各个控件
参数:无
返回值:无
************************************************************/
void CArpDlg::initial()
{
result.InsertColumn(0,"源IP地址",LVCFMT_CENTER,208);
result.InsertColumn(1,"目的IP",LVCFMT_CENTER,255);
result.InsertColumn(2,"数据包",LVCFMT_CENTER,207);
begin.EnableWindow(false);
stop.EnableWindow(false);
all_device();
}
/***********************************************************
函数:获得全部的网络适配器,将网络适配器名称列表保存在全局变量中
参数:无
返回值:无
***************************************************************/
void CArpDlg::all_device()
{ int i=0;
all_adapter.ResetContent();
g_all_dev = get_all_dev();
if(g_all_dev==NULL)
{
CString error = "无法找到网络适配器!";
info.SetWindowText(error);
}
else
{
CString name;
pcap_if_t* d;
for(d=g_all_dev;d!=NULL;d=d->next)
{
name = d->description;
all_adapter.AddString(name);
i++;
}
char buffer[100];
_itoa_s(i,buffer,10);
CString str(buffer);
CString show = "已经检测到"+str+"个网卡,请选择一个!";
info.SetWindowText(show);
}
}
/***************************************************************
函数:响应选择特定的网络适配器事件,根据所选列出网络适配器的详细信息
参数:无
返回值:无
***************************************************************/
void CArpDlg::OnSelchangeAlladapter()
{
// TODO: Add your control notification handler code here
result.DeleteAllItems();
get_dev_info(all_adapter.GetCurSel()+1);
}
/***************************************************************
函数:根据所选列出网络适配器的详细信息的具体实现
参数:num:所选择网络适配器的序号
返回值:无
***************************************************************/
void CArpDlg::get_dev_info(int num)
{
CString output="";
pcap_if_t* d;
d = g_all_dev;
char zhujiming[200]; //创建变量来保存本地主机名
if( gethostname(zhujiming, 200) == 0 ) //判断是否已经得到主机名
info2.SetWindowTextA (zhujiming);
for(int i=0;i<num-1;i++)
{
d=d->next;
}
pcap_addr_t* adapterAddress;
adapterAddress = d->addresses;
while(adapterAddress)
{
//获得ip地址和子网掩码
g_local_ip = ((struct sockaddr_in*)adapterAddress->addr)->sin_addr.s_addr;
g_mask = ((struct sockaddr_in *)(adapterAddress->netmask))->sin_addr.S_un.S_addr;
output="";
output = output +ip_to_string(g_mask)+"\r\n";
info4.SetString (output);
UpdateData(false);
output="";
output = output +ip_to_string(g_local_ip)+"\r\n";
info3.SetString(output);
UpdateData(false);
adapterAddress = adapterAddress->next;
}
//获得mac地址
g_local_mac = get_self_mac(d->name,g_local_ip);
output="";
output = output +mac_to_string(g_local_mac)+"\r\n";
info1.SetWindowText(output);
begin.EnableWindow(true);
g_cur_dev = d;
}
/***************************************************************
函数:自定义时间的响应函数,响应获得一个活动主机的时间
参数:wParam: ip和mac地址匹配信息的字符串
lParam: 用来判断是否从对应的响应时间传递而来、
返回值:无意义
***************************************************************/
LRESULT CArpDlg::OnReceive(WPARAM wParam, LPARAM lParam)
{
string* received = (string*)wParam;
string ip = received->substr(0, received->find_first_of(':'));
string mac =received->substr(received->find_first_of(':') + 1, received->find_last_of(':') - received->find_first_of(':') -1);
static int index = 0;
index = result.GetItemCount();
int i =0;
notice="";
if (lParam == 0){
for (int j = 0; j < index; j++)
{
if (0 == result.GetItemText(j, 0).Compare(ip.c_str()) && 0 == result.GetItemText(j, 1).Compare(mac.c_str()))
{
return 0;
}
}
result.InsertItem(i, "");
result.SetItemText(i, 0, ip.c_str());
result.SetItemText(i, 1, mac.c_str());
i++;
char buffer[5];
itoa(result.GetItemCount(), buffer, 10);
notice +="发现 ";
notice += buffer;
notice += " 台活动主机";
}
UpdateData(FALSE);
return 0;
}
/*
定义点击开始捕获按钮的响应事件
*/
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
arp-lan.rar (38个子文件)
arp-lan
scan-lan.vcproj.SKY-20100905ZMP.Administrator.user 1KB
scan-lan.suo 8KB
scan-lan.sln 871B
scan.vcproj.骑着猪看日出-PC.Administrator.user 1KB
StdAfx.cpp 246B
resource.h 1KB
scan.vcproj 8KB
scanDlg.h 2KB
scan.ncb 18.47MB
Arp.rc 6KB
scan.sln 877B
scanDlg.cpp 9KB
scan.h 1KB
deal.cpp 7KB
Debug
scanDlg.obj 97KB
scan-lan.pch 24.06MB
scan-lan.ilk 1014KB
vc90.idb 1.02MB
Arp.res 3KB
BuildLog.htm 5KB
deal.obj 43KB
mt.dep 65B
scan-lan.exe 84KB
scanlan.obj 23KB
vc90.pdb 2.06MB
scan-lan.exe.intermediate.manifest 861B
StdAfx.obj 453KB
scan-lan.exe.embed.manifest 920B
scan-lan.pdb 3.46MB
scan-lan.exe.embed.manifest.res 984B
scan-lan.vcproj.骑着猪看日出-PC.Administrator.user 1KB
scanlan.cpp 2KB
StdAfx.h 3KB
Arp.aps 22KB
deal.h 715B
scan.suo 48KB
res
scan.rc2 395B
scan.ico 1KB
共 38 条
- 1
资源评论
- u0109164662013-06-03有错误,--------------------Configuration: deal - Win32 Debug-------------------- Compiling... deal.cpp c:\users\asus\desktop\新建文件夹 (3)\arp-lan\deal.h(5) : fatal error C1083: Cannot open include file: 'pcap.h': No such file or directory 执行 cl.exe 时出错. deal.exe - 1 error(s), 0 warning(s)
- 雪嫣2013-12-20运行有错误啊,麻烦编译下再传上来啊
- pthboy2012-11-30运行有错误,不知道是不是编译器版本问题,资料量还是很大的!!
- bodihotboy2014-09-29运行不了,有问题了
sgh3329525
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功