VC++6.0MFC编译没问题一到某处就返回这个,求大神帮帮忙

YIGE_YIGE 2015-01-16 04:57:57
,没变异到此处就出现问题//处理数据
BOOL result = LoadFromData(m_CapData,nReadAll,m_fileHeader,fullFilePath);//此处正常返回后,莫名中断
return result;
}

//读入去除文件头的数据
BOOL CPCAPData::LoadFromData(const char* pData,int nLen,pcap_file_header pfheader,const char *filePath)
{
if (pData == NULL || nLen <= sizeof(pcap_pkthdr))
{
return FALSE;
}
if (m_pPackets == NULL)
{
m_pPackets = new pcap_packet[MAX_PACKET_COUNT];//实例容器,存放pcap文件中的包
}
if (m_pPackets == NULL)
{
return FALSE;
}
pcap_pkthdr *ph; //声明,初始化
mac_header *mh;
ip_header *ih;
tcp_header *th;
unsigned int ip_header_len = 20,tcp_header_len = 20,tcp_data_len = 0;
ph = (pcap_pkthdr *)malloc(sizeof(pcap_pkthdr));
mh = (mac_header *)malloc(sizeof(mac_header));
ih = (ip_header *)malloc(sizeof(ip_header));
th = (tcp_header *)malloc(sizeof(tcp_header));
char fileName[100],sort[100];//存储变化的文件名,用于生成文件

int nProcess = 0;
pcap_pkthdr packet_header;
const char *pLocalData = pData;//去文件头之后的数据开始
m_nPackets = 0; //TCP包个数
BOOL bRead = TRUE;
while (nProcess < m_nCapDataLen)
{
if (m_nCapDataLen - nProcess >= sizeof(pcap_pkthdr))
{
memcpy(&packet_header,pLocalData,sizeof(pcap_pkthdr));
// pLocalData += sizeof(pcap_pkthdr);
nProcess += sizeof(pcap_pkthdr);
}
else
{
bRead = FALSE;
break;
}
if (m_nCapDataLen - nProcess >= (int)packet_header.caplen)
{
memcpy(mh,pLocalData+sizeof(pcap_pkthdr),sizeof(mac_header));
if(mh->FrameType!=8)
{
pLocalData += sizeof(pcap_pkthdr) + packet_header.caplen;
nProcess += packet_header.caplen;
continue;
}
memcpy(ih,pLocalData+sizeof(pcap_pkthdr)+sizeof(mac_header),sizeof(ip_header));
if(ih->Protocol!=6)//过滤出所有的TCP包
{
pLocalData += sizeof(pcap_pkthdr) + packet_header.caplen;
nProcess += packet_header.caplen;
continue;
}
else
{
//数据包长度
m_pPackets[m_nPackets].nDataLen = packet_header.caplen+16;
//段个数
m_pPackets[m_nPackets].nSection = 1;
//段起始
m_pPackets[m_nPackets].nSecStart[0] = 0;
//段结束
m_pPackets[m_nPackets].nSecEnd[0] = packet_header.caplen;
//数据指针
m_pPackets[m_nPackets].pData = pLocalData;
m_nPackets++;
}
pLocalData += sizeof(pcap_pkthdr) + packet_header.caplen;
nProcess += packet_header.caplen;
}
else
{
bRead = FALSE;
break;
}
}///////////////////////////////////////////////////////////TCP会话包分离完成,开始排序,文件存储
CFile outFile;//输出重组后的pcap文件,保存到打开文件目录(strpath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)
CFileFind sortFile;
char *src_ip,*dst_ip;
unsigned short dst_port,src_port;
unsigned long preHashCode=0,currentHashCode=0;
struct in_addr addr1,addr2;
CString tempstr;
sprintf(sort,"%s%s",path,"[TCP][3096][222.20.7.87][80][119.75.217.56].pcap");
BOOL fileFlag = FALSE;
int fileID = 1,i = 0,currentSessionLen = 0;
RemoveFileInDir(path,0);
while(i < m_nPackets)
{
memcpy(ph,m_pPackets[i].pData,sizeof(pcap_pkthdr)); //ph->caplen
memcpy(mh,m_pPackets[i].pData+sizeof(pcap_pkthdr),sizeof(mac_header));//mh->FrameType
memcpy(ih,m_pPackets[i].pData+sizeof(pcap_pkthdr)+sizeof(mac_header),sizeof(ip_header));//ih->TotalLen
ip_header_len = (ih->Ver_HLen) == 69 ? ip_header_len : (ih->Ver_HLen-64) * 4; //ip_header_length
memcpy(th,m_pPackets[i].pData+sizeof(pcap_pkthdr)+sizeof(mac_header)+ip_header_len,sizeof(tcp_header));//tcp_header
memmove(&addr1,(void *)&(ih->SrcIP),4);
memmove(&addr2,(void *)&(ih->DstIP),4);
src_ip=inet_ntoa(addr1);
tempstr=src_ip;
dst_ip=inet_ntoa(addr2);
dst_port=ntohs(th->DstPort);
src_port=ntohs(th->SrcPort);
preHashCode = currentHashCode;
currentHashCode = MakeHashCode(ih->DstIP,ih->SrcIP,th->DstPort,th->SrcPort);
if (preHashCode != currentHashCode)
{
if(fileFlag)
{
outFile.Close();
}
sprintf(fileName,"%s[TCP][%u][%s][%u][%s]%s",path,src_port,tempstr,dst_port,dst_ip,".pcap");
if (sortFile.FindFile(fileName))
{
if (!outFile.Open(fileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite,NULL))
{
printf("open file error!");
}
else
{
outFile.Seek(0,CFile::end);
outFile.Write(m_pPackets[i].pData,m_pPackets[i].nDataLen);
i++;
continue;
}
}
sprintf(fileName,"%s[TCP][%u][%s][%u][%s]%s",path,dst_port,dst_ip,src_port,tempstr,".pcap");
if (sortFile.FindFile(fileName))
{
if (!outFile.Open(fileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite,NULL))
{
printf("open file error!");
}
else
{
outFile.Seek(0,CFile::end);
outFile.Write(m_pPackets[i].pData,m_pPackets[i].nDataLen);
i++;
continue;
}
}
else
{
sprintf(fileName,"%s[TCP][%u][%s][%u][%s]%s",path,src_port,tempstr,dst_port,dst_ip,".pcap");
if(!outFile.Open(fileName,CFile::modeWrite|CFile::modeNoTruncate|CFile::modeCreate,NULL))
{
printf("open file error!");
}
else
{
CArchive ar(&outFile,CArchive::Mode::store);
outFile.Seek(0,CFile::end);
fileFlag = TRUE;
outFile.Write((void *)&pfheader,sizeof(pcap_file_header));
outFile.Write(m_pPackets[i].pData,m_pPackets[i].nDataLen);
}
}
}
else
{
outFile.Write(m_pPackets[i].pData,m_pPackets[i].nDataLen);
}
i++;
}
sortFile.Close();
outFile.Close();
RemoveFileInDir(path,1);
return bRead;
}
int CPCAPData::MakeHashCode(u_int32 des_ip,u_int32 src_ip,u_int16 des_port,u_int16 src_port)//计算五元组的hash值
{
return (des_ip^src_ip)^(ntohs(des_port)*ntohs(src_port));
}
void CPCAPData::RemoveFileInDir(const char * dir,int flag)
{
char buff[256],pcapname[50];
sprintf(buff,"%s\\*.*",dir);
int fileID = 1;
CString name,path;
CFileFind find; BOOL bFind = find.FindFile(buff);
try{
while(bFind)
{
bFind = find.FindNextFile();
name = find.GetFileName();
if(find.IsDirectory() || name.Compare(".") == 0 || name.Compare("..") == 0)
continue;

path = find.GetFilePath();
if (flag)
{
CFile sort;
if (!sort.Open(path,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite,NULL))
{
printf("error!");
}
else
{
CString name = sort.GetFileName();
u_int32 len = sort.GetLength();
int index = name.ReverseFind('.');
CString bfixx = name.Mid(index);
CString pfixx = name.Mid(0,index);
sprintf(pcapname,"%s%s[%u]_%u%s",dir,pfixx,len,fileID,bfixx);
sort.Close();
CFile::Rename(path,pcapname);
fileID++;
}
}
else
{
CFile::Remove(path);
}
}
}
catch(CFileException &e){
find.Close();
e.GetErrorMessage(buff,256);
}
}
...全文
195 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
智能卡_Snooper 2015-01-17
  • 打赏
  • 举报
回复
这种函数,别人没有环境,基本上无法回答,这种问题,说穿了就是跟踪一下的事情。
worldy 2015-01-16
  • 打赏
  • 举报
回复
遇到断言失败错误了,按重试,然后看代码停止在什么位置,然后分析

16,372

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧