📄 snifferdlg.cpp
字号:
// snifferDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sniffer.h"
#include "snifferDlg.h"
//#include "ParseProtocol.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
void CSnifferDlg::DecodeIPHeader(WSABUF *wsabuf)
{
BYTE *hdr = (BYTE *)wsabuf->buf,
*nexthdr = NULL;
unsigned short shortval;
m_ip.ip_version = HI_WORD(*hdr);
m_ip.ip_hdr_len = LOW_WORD(*hdr) * 4;
nexthdr = (BYTE *)(wsabuf->buf + m_ip.ip_hdr_len);
hdr++;
m_ip.ip_tos = *hdr;
hdr++;
memcpy(&shortval, hdr, 2);
m_ip.ip_total_len = ntohs(shortval);
hdr += 2;
memcpy(&shortval, hdr, 2);
m_ip.ip_id = ntohs(shortval);
hdr += 2;
m_ip.ip_flags = ((*hdr) >> 5);
memcpy(&shortval, hdr, 2);
m_ip.ip_frag_offset = ((ntohs(shortval)) & 0x1FFF);
hdr += 2;
m_ip.ip_ttl = *hdr;
hdr++;
m_ip.ip_proto = *hdr;
hdr++;
memcpy(&shortval, hdr, 2);
m_ip.ip_hdr_chksum = ntohs(shortval);
hdr += 2;
memcpy(&m_psd_srcaddr.sin_addr.s_addr, hdr, 4);
m_ip.ip_src = ntohl(m_psd_srcaddr.sin_addr.s_addr);
hdr += 4;
memcpy(&m_psd_destaddr.sin_addr.s_addr, hdr, 4);
m_ip.ip_dest = ntohl(m_psd_destaddr.sin_addr.s_addr);
hdr += 4;
AddNewParse();
}
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)
//}}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
bstart=FALSE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
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_MESSAGE(WM_REV,OnRecv)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
//}}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
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::OnOK()
{
DWORD dwValue;
int ret;
unsigned int optval;
if (!bstart)
{
//初始化 Raw Socket
sock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
if (sock == INVALID_SOCKET)
{
AfxMessageBox("Socket init failure!");
}
optval = 1;
//设置IP头操作选项
ret=setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&optval, sizeof(optval));
if (ret== SOCKET_ERROR)
{
AfxMessageBox("Socket option set failure!");
}
ret=gethostname((char*)LocalName, sizeof(LocalName)-1);
if (ret== SOCKET_ERROR)
{
AfxMessageBox("gethostname failure!");
}
//获取本地 IP 地址
pHost = gethostbyname((char*)LocalName);
if (pHost== NULL)
{
AfxMessageBox("gethostbyname failure!");
}
addr_in.sin_addr = *(in_addr *)pHost->h_addr_list[0]; //IP
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(0);
//把 sock 绑定到本地地址上
ret=bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in));
if (ret== SOCKET_ERROR)
{
AfxMessageBox("Socket addr bind failure!");
}
//设置 SOCK_RAW 为SIO_RCVALL,以便接收所有的IP包
ret=ioctlsocket(sock, SIO_RCVALL, &dwValue);
if (ret== SOCKET_ERROR)
{
AfxMessageBox("socket io set failure!");
return;
}
ret=WSAAsyncSelect(sock,m_hWnd,WM_REV,FD_READ);
if(ret== SOCKET_ERROR)
{
AfxMessageBox("WSAAsyncSelect Error!");
return;
}
CButton *pButton=(CButton*)GetDlgItem(IDOK);
pButton->SetWindowText("stop");
bstart = TRUE;
}
else
{
dwValue = 0;
int cbRet;
//设置SOCK_RAW为SIO_RCVALL,停止接收
if (WSAIoctl(sock,SIO_RCVALL,&dwValue,sizeof(dwValue),NULL,0,(unsigned long*)&cbRet,NULL,NULL)!=0)
{
AfxMessageBox("Socket io set failure!");
return;
}
closesocket(sock);
CButton *pButton=(CButton*)GetDlgItem(IDOK);
pButton->SetWindowText("start");
bstart = FALSE;
}
}
void CSnifferDlg::OnRecv(WPARAM wParam,LPARAM lParam)
{
int ret;
CString str;
switch(WSAGETSELECTEVENT(lParam))
{
case FD_READ :
{
char rcvbuf[65535];
WSABUF wbuf;
wbuf.len = 65535;
wbuf.buf = rcvbuf;
DWORD dwFlags = 0;
DWORD dwBytesRet;
ret = WSARecv(sock, &wbuf, 1, &dwBytesRet, &dwFlags, NULL, NULL);
if (ret == SOCKET_ERROR)
{
printf("WSARecv() failed: %d\n", WSAGetLastError());
return ;
}
DecodeIPHeader(&wbuf);
}
break;
case FD_CLOSE:
AfxMessageBox("The monitor will Be Closed!");
break;
}
}
int CSnifferDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CRect rect;
rect.left = 30;
rect.top = 15;
rect.right = 450;
rect.bottom = 240;
if (!m_ListCtrl.Create(WS_CHILD |WS_VISIBLE|LVS_REPORT,
rect, this, IDC_LIST1))
{
AfxMessageBox("Failed to create ListCtrl!");
return -1;
}
m_ListCtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE);
static struct
{
LPSTR psztext;
int ui;
}columns[]={
_T("编号"),LVCFMT_CENTER,
_T("版本号"),LVCFMT_CENTER,
_T("IP头长"),LVCFMT_CENTER,
_T("IP总长"),LVCFMT_CENTER,
_T("TTL"),LVCFMT_CENTER,
_T("效验和"),LVCFMT_CENTER,
_T("协议名称"),LVCFMT_CENTER,
_T("源地址"),LVCFMT_CENTER,
_T("目的地址"),LVCFMT_CENTER,
_T("扑获时间"),LVCFMT_CENTER,
};
m_ListCtrl.InsertColumn(0,columns[0].psztext,columns[0].ui,40);
m_ListCtrl.InsertColumn(1,columns[1].psztext,columns[1].ui,50);
m_ListCtrl.InsertColumn(2,columns[2].psztext,columns[2].ui,50);
m_ListCtrl.InsertColumn(3,columns[3].psztext,columns[3].ui,50);
m_ListCtrl.InsertColumn(4,columns[4].psztext,columns[4].ui,60);
m_ListCtrl.InsertColumn(5,columns[5].psztext,columns[5].ui,80);
m_ListCtrl.InsertColumn(6,columns[6].psztext,columns[6].ui,60);
m_ListCtrl.InsertColumn(7,columns[7].psztext,columns[7].ui,80);
m_ListCtrl.InsertColumn(8,columns[8].psztext,columns[8].ui,60);
m_ListCtrl.InsertColumn(9,columns[9].psztext,columns[9].ui,140);
m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
return 0;
}
void CSnifferDlg::AddNewParse()
{
LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_IMAGE;
lvi.iItem = m_ListCtrl.GetItemCount();
lvi.iSubItem = 0;
lvi.pszText = "";
lvi.cchTextMax = 64;
m_ListCtrl.InsertItem(&lvi);
//捕捉编号
CString str;
str.Format("%d",m_ListCtrl.GetItemCount());
m_ListCtrl.SetItemText(lvi.iItem,0,str);
str.Empty();
//版本号
str.Format("%d",m_ip.ip_version);
m_ListCtrl.SetItemText(lvi.iItem,1,str);
str.Empty();
//ip头长
str.Format("%d",m_ip.ip_hdr_len);
m_ListCtrl.SetItemText(lvi.iItem,2,str);
str.Empty();
//ip总长
str.Format("%d",m_ip.ip_total_len);
m_ListCtrl.SetItemText(lvi.iItem,3,str);
str.Empty();
//TTL
str.Format("%d",m_ip.ip_ttl);
m_ListCtrl.SetItemText(lvi.iItem,4,str);
str.Empty();
//效验和
str.Format("%d",m_ip.ip_hdr_chksum);
m_ListCtrl.SetItemText(lvi.iItem,5,str);
str.Empty();
//协议名称
str.Format("IP");
m_ListCtrl.SetItemText(lvi.iItem,6,str);
str.Empty();
//源地址
str.Format("%-15s",inet_ntoa(m_psd_srcaddr.sin_addr));
m_ListCtrl.SetItemText(lvi.iItem,7,str);
str.Empty();
//目的地址
str.Format("%-15s",inet_ntoa(m_psd_destaddr.sin_addr));
m_ListCtrl.SetItemText(lvi.iItem,8,str);
str.Empty();
//捕获时间
CTime time = CTime::GetCurrentTime();
str=time.Format("%Y-%m-%d %H:%M:%S");
m_ListCtrl.SetItemText(lvi.iItem,9,str);
str.Empty();
m_ListCtrl.EnsureVisible(lvi.iItem,true);
m_ListCtrl.Update(lvi.iItem);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -