ip数据包的捕获与解析代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//PackCaptureDlg.h:headerfile#defineIPV4_WERSION4#defineIPV6_WERSION6#defineICMP_PACKET1#defineIGMP_PACKET2#defineTCP_PACKET6#defineEGP_PACKET8#defineUDP_packet17#defineOSPF_PACKET89classCPackCaptureDlg:publicCDialog{public://{{AFX_DATA(CFindHostDlg)enum{IDO=IDO_PACKCAPTURE_DIALOG};intm_Count;CStringm_Packet;//}}AFX_DATAprotected://{{AFX_MSG(CFindHostDlg)afx_msgvoidOnCapture();//}}AFX_MSGprivate:typedefstructIP_HEAD//IP头部结构{union{unsignedcharVersion;//版本(字节前四位)unsignedcharHeadLen;//头部长度(字节后四位)};unsignedcharServiceType;//服务类型unsignedshortTotalLen;//总长度unsignedshortIdentifier;//标识符union{unsignedshortFlags;//标志位(字前三位)unsignedshortFragOffset;//片偏移(字后13位)};unsignedcharTimeToLive;//生存周期unsignedcharProtocol;//协议unsignedshortHeadChecksum;//头部校验和unsignedintSourceAddr;//源IP地址unsignedintDestinAddr;//目的IP地址}ip_head;typedefstructICMP_HEAD//ICMP头部结构{unsignedcharType;//类型unsignedcharCode;//代码unsignedshortHeadChecksum;//头部校验和unsignedshortIdentifior;//标识符unsignedshortSequence;//序号}icmp_head;};//PackCaptureDlg.cpp:implementationfile#includestdafx.h#includePackCaptureDlg.h#includePackCapture.h#includewinsock2.h#includews2tcpip.h#pragmacomment(lib,ws2_32)#defineIO_RCVALL_WSAIOW(IOC_VENDOR,1)voidCPackCaptureDlg::OnCapture(){WSADATAWSAData;//建立与Socket库绑定if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0){MessageBox(WSAStartup初始化失败!);return;}SOCKETSocket;//创建原始SocketSocket=socket(AF_INET,SOCK_RAW,IPPROTO_IP);if(Socket==INVALID_SOCKET){MessageBox(创建Socket失败!);WSACleanup();return;}intRecvTime=5000;//设置Socket操作选项if(setsockopt(Socket,SOL_SOCKET,SO_RCVTIMEO,(char*)&RecvTime,sizeof(RecvTime))==SOCKET_ERROR){MessageBox(设置Socket选项失败!);closesocket(Socket);WSACleanup();return;}charHostName[128];//获得本地主机名称if(gethostname(HostName,128)==SOCKET_ERROR){MessageBox(获得主机名失败!);closesocket(Socket);WSACleanup();return;}hostent*pHostent;//获得本地主机IP地址pHostent=gethostbyname(HostNmae);if(pHostent==NULL){MessageBox(获得主机地址失败!);closesocket(Socket);WSACleanup();return;}sockaddr_inHostAddr;//定义Socket地址结构memset(&HostAddr,0,sizeof(HostAddr));HostAddr.sin_family=AF_INET;HostAddr.sin_addr.s_addr=(*(in_addr*)pHostent-h_addr).s_addr;intnBind;//绑定Socket与网卡nBind=bind(Socket,(PSOCKADDR)&HostAddr,sizeof(HostAddr));if(nBind==SOCKET_ERROR){MessageBox(绑定Socket失败!);closesocket(Socket);WSACleanup();return;}DWORDdwValue=1;//设置Socket接受模式if(ioctlsocket(Socket,IO_RCVALL,&dwValue)==SOCKET_ERROR){MessageBox(设置Socket接收器失败!);closesocket(Socket);WSACleanup();return;}UpdateData(true);m_Packet=版本总长度标志位片偏移协议源地址目的地址\r\n;for(inti=0;im_Count;i++){charRecvBuf[65535];memset(RecvBuf,0,sizeof(RecvBuf));intnRecv;//依次接受IP数据包nRecv=recv(Socket,RecvBuf,sizeof(RecvBuf),0);if(nRecv==SOCKET_ERROR){MessageBox(接受IP数据包失败!);closesocket(Socket);WSACleanup();return;}else{ip_headIpHead=*(ip_head*)RecvBuf;CStringstr;if((IpHead.Version4)==IPV4_VERSION)STR.Format(IPv4);if((IpHead.Version4)==IPV6_VERSION)STR.Format(IPv6);m_Packet+=str;str.Format(%u,ntohs(IpHead.TotalLen));m_Packet+=str;str.Format(D=%uM=%u,((ntohs(IpHead.Flags))14)&0x01,((btohs(IpHead.Flags))13)&0x01);m_Packet+=str;str.Format(%u,ntohs(IpHead.Fragoffset)&0x1fff);m_Packet+=str;switch(IpHead.Protocol){caseICMP_PACKET:str.Format(ICMP);break;caseIGMP_PACKET:str.Format(IGMP);break;caseTCP_PACKET:str.Format(TCP);break;caseEGP_PACKET:str.Format(EGP);break;caseUDP_PACKET:str.Format(UDP);break;caseOSPF_PACKET:str.Format(OSPF);break;};m_Packet+=str;str.Format(%s,inet_ntoa(*(in_addr*)&IpHead.SourceAddr));m_Packet+=str;str.Format(%s,inet_ntoa(*(in_addr*)&IpHead.DestinAddr));m_Packet+=str;m_Packet+=\r\n;}}UpdateData(false);closesocket(Socket);WSACLeanup();//解除与Socket库绑定}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功