📄 snifferdlg.cpp
字号:
// snifferDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sniffer.h"
#include "snifferDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSnifferDlg dialog
CSnifferDlg::CSnifferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSnifferDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSnifferDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
//初试化套接字变量
m_sk=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CSnifferDlg::~CSnifferDlg()
{
//关闭套接字变量
if(m_sk)
closesocket(m_sk);
}
void CSnifferDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSnifferDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSnifferDlg, CDialog)
//{{AFX_MSG_MAP(CSnifferDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_RNET,OnRNet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSnifferDlg message handlers
BOOL CSnifferDlg::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
// TODO: Add extra initialization here
//设置List控件的报表风格
CListCtrl *pmyListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
DWORD dwStyle=GetWindowLong(pmyListCtrl->m_hWnd,GWL_STYLE);
SetWindowLong(pmyListCtrl->m_hWnd,GWL_STYLE,dwStyle|LVS_REPORT);
pmyListCtrl->InsertColumn(0,"服务类型",LVCFMT_LEFT,70);
pmyListCtrl->InsertColumn(1,"总长",LVCFMT_LEFT,40);
pmyListCtrl->InsertColumn(2,"标识",LVCFMT_LEFT,50);
pmyListCtrl->InsertColumn(3,"标志与片偏移",LVCFMT_LEFT,100);
pmyListCtrl->InsertColumn(4,"生存时间",LVCFMT_LEFT,80);
pmyListCtrl->InsertColumn(5,"协议标识",LVCFMT_LEFT,80);
pmyListCtrl->InsertColumn(6,"源地址",LVCFMT_LEFT,120);
pmyListCtrl->InsertColumn(7,"目的地址",LVCFMT_LEFT,120);
pmyListCtrl->InsertColumn(8,"ICMP类型",LVCFMT_LEFT,40);
pmyListCtrl->InsertColumn(9,"ICMP代码",LVCFMT_LEFT,40);
pmyListCtrl->InsertColumn(10,"源端口",LVCFMT_LEFT,40); //可以为TCP或UDP的端口号
pmyListCtrl->InsertColumn(11,"目的端口",LVCFMT_LEFT,40);
pmyListCtrl->InsertColumn(12,"序列号",LVCFMT_LEFT,50);
pmyListCtrl->InsertColumn(13,"应答号",LVCFMT_LEFT,50);
pmyListCtrl->InsertColumn(14,"标志",LVCFMT_LEFT,50);
pmyListCtrl->InsertColumn(15,"紧急事件标志",LVCFMT_LEFT,50);
pmyListCtrl->InsertColumn(16,"数据",LVCFMT_LEFT,40);
//调用初试化函数
Initialize();
return TRUE; // return TRUE unless you set the focus to a control
}
void CSnifferDlg::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 CSnifferDlg::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 CSnifferDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSnifferDlg::Initialize()
{
//得到本地机器名
char name[20];
int retval;
retval=gethostname(name,20);
if(retval==SOCKET_ERROR)
{
MessageBox("无法获得本地的机器名");
return ;
}
//通过机器名得到本地的IP地址
struct hostent *pHostent;
pHostent=gethostbyname(name);
if(pHostent==NULL)
{
MessageBox("无法得到本地的IP地址");
return;
}
char *tmpip,*ip;
tmpip=pHostent->h_addr;
ip=inet_ntoa(*((struct in_addr*)tmpip));//获得本地的IP地址
//建立原始套接字
m_sk=socket(AF_INET,SOCK_RAW,IPPROTO_IP);
if(INVALID_SOCKET ==m_sk)
{
MessageBox("建立套接字失败!");
return;
}
struct sockaddr_in localaddr;//本地地址
memset(&localaddr,0,sizeof(sockaddr_in));
localaddr.sin_family=AF_INET;
//必须指定本地的网卡的地址,不能用INADDR_ANY来指定
localaddr.sin_addr.s_addr=inet_addr(ip);
//原始套接字与本地地址绑定
retval=bind(m_sk,(struct sockaddr*)&localaddr,sizeof(sockaddr));
if(SOCKET_ERROR==retval)
{
MessageBox("绑定套接字失败!");
return;
}
//设置混杂模式
unsigned long flag=1;//允许或禁止标记
//必须要先绑定本地的地址,然后设置套接字可以接受所有IP包
if(SOCKET_ERROR==ioctlsocket(m_sk,SIO_RCVALL, &flag))
{
MessageBox("设置套接字失败");
return;
}
/*设置网络读为非阻塞事件,WSAAsyncSelect()函数为套接字请求基于
Windows消息的事件通知,它请求WinSock DLL在检测到有FD_READ读事件
时,向对话框发送WM_RNET消息
*/
retval=WSAAsyncSelect(m_sk,this->m_hWnd,WM_RNET,FD_READ);
if(SOCKET_ERROR==retval)
{
MessageBox("设置非阻塞方式失败!");
return;
}
}
void CSnifferDlg::OnRNet(WPARAM wParam,LPARAM lParam)
{
//参数wParam为套接字,lParam的低字节为发生的网络事件
struct sockaddr_in addrfrom; //目的地址
int fromlen=sizeof(struct sockaddr_in);
char buf[2000];//接受的数据
int retval; //返回值
struct IP_Head *iph;
struct ICMP_Head *icmph;
struct UDP_Head *udph;
struct TCP_Head *tcph;
CString str[17];
switch(LOWORD(lParam))
{
case FD_READ:
memset(buf,0,2000);
retval=recvfrom((SOCKET)wParam,buf,2000,0,(struct sockaddr*)&addrfrom,&fromlen);
if(retval==SOCKET_ERROR)
break;
iph=(struct IP_Head*)buf;//取IP报头
if(1==iph->protocol) //ICMP协议
{
icmph=(ICMP_Head*)(buf+sizeof(struct IP_Head)); //取ICMP报头
str[0].Format("%d",iph->tos);
str[1].Format("%d",ntohs(iph->total_length));
str[2].Format("%d",ntohs(iph->id));
str[3].Format("%d",ntohs(iph->flagoff));
str[4].Format("%d",iph->ttl);
str[5]="ICMP";
str[6]=inet_ntoa(*((in_addr*)&iph->source));
str[7]=inet_ntoa(*((in_addr*)&iph->dest));
str[8].Format("%d",icmph->type);
str[9].Format("%d",icmph->code);
str[16].Format("%s",(buf+iph->length*4+8));
WriteData(str,17);
break;
}
/*
if(6==iph->protocol) //TCP协议
{
tcph=(TCP_Head*)(buf+sizeof(struct IP_Head));//取TCP报头
str[0].Format("%d",iph->tos);
str[1].Format("%d",ntohs(iph->total_length));
str[2].Format("%d",ntohs(iph->id));
str[3].Format("%d",ntohs(iph->flagoff));
str[4].Format("%d",iph->ttl);
str[5]="TCP";
str[6]=inet_ntoa(*((in_addr*)&iph->source));
str[7]=inet_ntoa(*((in_addr*)&iph->dest));
str[8].Format("%d",iph->length*4); //测试,IP头长度
str[9].Format("%d",tcph->len*4); //TCP头长度
str[10].Format("%d",ntohs(tcph->source_port));
str[11].Format("%d",ntohs(tcph->dest_port));
str[12].Format("%u",ntohl(tcph->seqno));
str[13].Format("%u",ntohl(tcph->ackno));
str[14].Format("%x",tcph->flags);
str[15].Format("%d",ntohs(tcph->urgent));
str[16].Format("%s",(buf+iph->length*4+tcph->len*4));
WriteData(str,17);
break;
}
if(17==iph->protocol) //UDP协议
{
udph=(UDP_Head*)(buf+sizeof(struct IP_Head));//取UDP报头
str[0].Format("%d",iph->tos);
str[1].Format("%d",ntohs(iph->total_length));
str[2].Format("%d",ntohs(iph->id));
str[3].Format("%d",ntohs(iph->flagoff));
str[4].Format("%d",iph->ttl);
str[5]="UDP";
str[6]=inet_ntoa(*((in_addr*)&iph->source));
str[7]=inet_ntoa(*((in_addr*)&iph->dest));
str[10].Format("%d",ntohs(udph->source_port));
str[11].Format("%d",ntohs(udph->dest_port));
str[16].Format("%s",(buf+iph->length*4+8));
WriteData(str,17);
break;
}
*/
}
return;
}
void CSnifferDlg::WriteData(CString str[], int n)
{
static int i=0;
//获得ListControl控件的指针
CListCtrl *pmyListCtrl=(CListCtrl*)GetDlgItem(IDC_LIST1);
pmyListCtrl->InsertItem(i,str[0]); //新增一行
for(int j=1;j<n;j++)
pmyListCtrl->SetItemText(i,j,str[j]); //写入列
i++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -