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

📄 snifferdlg.cpp

📁 VC++编程实现网络嗅探器在调试状态下,在Output窗口中输出监测信息
💻 CPP
字号:
// SnifferDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Sniffer.h"
#include "SnifferDlg.h"
#include "ws2tcpip.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSnifferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSnifferDlg)
	DDX_Control(pDX, IDC_PACKET, m_Packet);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSnifferDlg, CDialog)
	//{{AFX_MSG_MAP(CSnifferDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnifferDlg message handlers

BOOL CSnifferDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	WSADATA WSAData;
    BOOL	flag = true;
    int     nTimeout = 1000;
    char    LocalName[16];
    struct  hostent *pHost;

    // 检查 Winsock 版本号
    if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
		return false;

    // 初始化 Raw Socket
    if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == INVALID_SOCKET)
		return false;

    // 设置IP头操作选项
    if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR)
		return false;

    // 获取本机名
    if (gethostname((char*)LocalName, sizeof(LocalName)-1) == SOCKET_ERROR)
		return false;

    // 获取本地 IP 地址
    if ((pHost = gethostbyname((char*)LocalName)) == NULL)
		return false;
	
    addr_in.sin_addr    = *(in_addr *)pHost->h_addr_list[0]; //IP
    addr_in.sin_family  = AF_INET;
    addr_in.sin_port    = htons(57274);

    // 把 sock 绑定到本地地址上
    if (bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in)) == SOCKET_ERROR)
		return false;

	DWORD dwValue = 1;
    
	// 设置 SOCK_RAW 为SIO_RCVALL,以便接收所有的IP包
	if (ioctlsocket(sock, SIO_RCVALL, &dwValue) != 0)
		return false;

//	CFile file;
//	file.Open("E:\\Record.dat", CFile::modeCreate|CFile::modeReadWrite);
//	file.Close();
	while (true)
    {
		int ret = recv(sock, RecvBuf, BUFFER_SIZE, 0);
        if (ret > 0)
        {
//			file.Open("E:\\Record.txt", CFile::modeReadWrite);
//			file.SeekToEnd();
//			file.Write(RecvBuf, ret);
//			file.Close();
			
			ip  = *(IP*)RecvBuf;
            tcp = *(TCP*)(RecvBuf + ip.HdrLen);

			TRACE("协议:			%s\r\n",GetProtocolTxt(ip.Protocol));
			TRACE("IP源地址:		%s\r\n",inet_ntoa(*(in_addr*)&ip.SrcAddr));
			TRACE("IP目标地址:		%s\r\n",inet_ntoa(*(in_addr*)&ip.DstAddr));
			TRACE("TCP源端口号:	%d\r\n",tcp.SrcPort);
			TRACE("TCP目标端口号:	%d\r\n",tcp.DstPort);
			TRACE("数据包长度:	%d\r\n\r\n\r\n",ntohs(ip.TotalLen));
		}
    }

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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;
}

BOOL CSnifferDlg::DestroyWindow() 
{
	WSACleanup();
	return CDialog::DestroyWindow();
}

CString CSnifferDlg::GetProtocolTxt(int Protocol)
{
	switch (Protocol)
    {
        case IPPROTO_ICMP :           //1               /* control message protocol */
            return PROTOCOL_STRING_ICMP_TXT;
        case IPPROTO_TCP  :           //6               /* tcp */
            return PROTOCOL_STRING_TCP_TXT;
        case IPPROTO_UDP  :           //17              /* user datagram protocol */
            return PROTOCOL_STRING_UDP_TXT;
        default:
            return PROTOCOL_STRING_UNKNOW_TXT;
    }
}

⌨️ 快捷键说明

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