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

📄 capfilereader.cpp

📁 众所周知
💻 CPP
字号:
#include "CapFileReader.h"

CapFileReader::CapFileReader(void) :
	m_fp(NULL),
	m_CurPos(0),
	m_NextPos(0),
	m_IsOpen(false),
	m_DataLength(0),
	m_StartTime(0),
	m_MicroSecond(0),
	m_Protocol(0)
{

}

CapFileReader::~CapFileReader(void)
{
	Close();
}

bool CapFileReader::Open(const char* CapFile)
{
	if (m_IsOpen)
	{
		return true;
	}
	m_CurPos = 0;
	m_NextPos = 0;
	assert(NULL!=CapFile);
	assert(NULL==m_fp);
	m_fp = fopen(CapFile, "rb");
	if (NULL == m_fp)
	{
		DEBUG_WRITE1("打开文件错误!%s\n", CapFile);
		return false;
	}
	//读取时间
	int nResult = fseek(m_fp, 0x0C, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(&m_StartTime, sizeof(m_StartTime), 1, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	m_IsOpen = true;
	m_NextPos = FILE_HEADER;
	return true;
}

bool CapFileReader::Read()
{
	if (!m_IsOpen)
	{
		return false;
	}
	int nResult = fseek(m_fp, m_NextPos, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	//获得微妙数
	nResult = (int)fread(&m_MicroSecond, sizeof(m_MicroSecond), 1, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	//获得长度
	nResult = fseek(m_fp, 4, SEEK_CUR);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(&m_DataLength, sizeof(m_DataLength), 1, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	int temp = m_NextPos + SPLIT_SEG_LEN + 12;
	nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(&m_Protocol, sizeof(m_Protocol), 1, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	m_CurPos = m_NextPos;
	m_NextPos += SPLIT_SEG_LEN + m_DataLength;
	return true;
}

void CapFileReader::Close()
{
	m_IsOpen = false;
	if (NULL != m_fp)
	{
		fclose(m_fp);
		m_fp = NULL;
	}
}

bool CapFileReader::GetSourceMacAddr(char* Buffer, int Length)
{
	assert(NULL!=Buffer);
	assert(Length>0);
	int temp = m_CurPos + SPLIT_SEG_LEN + 6;
	int nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	if (Length>6)
	{
		Length = 6;
	}
	nResult = (int)fread(Buffer, 1, Length, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	return true;
}

bool CapFileReader::GetDestMacAddr(char* Buffer, int Length)
{
	assert(NULL!=Buffer);
	assert(Length>0);
	int temp = m_CurPos + SPLIT_SEG_LEN;
	int nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	if (Length>6)
	{
		Length = 6;
	}
	nResult = (int)fread(Buffer, 1, Length, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	return true;
}

bool CapFileReader::GetIPHeader(IP_HEADER& ip)
{
	int temp = m_CurPos + SPLIT_SEG_LEN + 14;
	int nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(&ip, sizeof(IP_HEADER), 1, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	return true;
}

bool CapFileReader::GetIPData(int HeadLen, char* Buffer, int Length)
{
	assert(HeadLen>=(int)sizeof(IP_HEADER));
	assert(NULL!=Buffer);
	assert(Length>0);
	int temp = m_CurPos + SPLIT_SEG_LEN + 14 + HeadLen;
	int nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(Buffer, 1, Length, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	return true;
}

bool CapFileReader::GetInternetData(char* Buffer, int Length)
{
	assert(NULL!=Buffer);
	assert(Length>0);
	int temp = m_CurPos + SPLIT_SEG_LEN + 14;
	int nResult = fseek(m_fp, temp, SEEK_SET);
	if (0!=nResult)
	{
		Close();
		return false;
	}
	nResult = (int)fread(Buffer, 1, Length, m_fp);
	if (0==nResult)
	{
		Close();
		return false;
	}
	return true;
}

⌨️ 快捷键说明

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