您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于socket套接字的IP数据包的抓取
基于socket套接字的IP数据包的抓取SOCKETsock;WSADATAwsData;ofstreamofs(ip.log,ios::app);if(WSAStartup(MAKEWORD(2,2),&wsData)!=0){printf(WSAStartupfailed!\n);return-1;}if((sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==INVALID_SOCKET){printf(createsocketfailed\n);return-1;}BOOLflag=true;if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char*)&flag,sizeof(flag))==SOCKET_ERROR){printf(setsockoptfailed!\n);return-1;}charhostName[128];if(gethostname(hostName,100)==SOCKET_ERROR){printf(gethostnamefailed!\n);return-1;}hostent*pHostIP;if((pHostIP=gethostbyname(hostName))==NULL){printf(gethostbynamefailed\n);return-1;}sockaddr_inaddr_in;addr_in.sin_addr=*(in_addr*)pHostIP-h_addr_list[0];addr_in.sin_family=AF_INET;addr_in.sin_port=htons(6000);if(bind(sock,(PSOCKADDR)&addr_in,sizeof(addr_in))==SOCKET_ERROR){printf(bindfailed\n);return-1;}#defineIO_RCVALL_WSAIOW(IOC_VENDOR,1)DWORDdwBufferLen[10];DWORDdwBufferInLen=1;DWORDdwBytesReturned=0;charbuffer[100];if(WSAIoctl(sock,IO_RCVALL,&dwBufferInLen,sizeof(dwBufferInLen),&dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL)==SOCKET_ERROR){printf(ioctlsocketfaild\n);return-1;}printf(AnalysisIPPacket!\n\n);strings;char*p;charbuffer1[100];while(true){intsize=recv(sock,buffer,sizeof(buffer),0);IP_HEADERip=*(IP_HEADER*)buffer;cout-----------------------endl;s+=\n-----------------------\n;coutVersion:(ip.Version4)endl;_itoa(ip.Version4,buffer1,10);s+=Version:;s+=buffer1;s+=\n;coutIHL:((ip.HdrLen&0x0f)*4)endl;s+=IHL:;_itoa((ip.HdrLen&0x0f)*4,buffer1,10);s+=buffer1;s+=\n;coutTypeofservice:Priority(ip.ServiceType5),Service((ip.ServiceType1)&0x0f)endl;s+=Typeofservice:Priority;_itoa(ip.ServiceType5,buffer1,10);s+=buffer1;s+=,Service;_itoa((ip.ServiceType1)&0x0f,buffer1,10);s+=buffer1;s+=\n;coutTOtalLength:ip.TotalLenendl;s+=TOtalLength:;_itoa(ip.TotalLen,buffer1,10);s+=buffer1;s+=\n;coutIdentifcation:ip.IDendl;s+=Identifcation:;_itoa(ip.ID,buffer1,10);s+=buffer1;s+=\n;coutsign:((ip.Flags15)&0x01),DF=((ip.Flags14)&0x01),Mf=((ip.Flags13)&0x01)endl;s+=sign:;_itoa((ip.Flags15)&0x01,buffer1,10);s+=buffer1;s+=,DF=;_itoa((ip.Flags14)&0x01,buffer1,10);s+=buffer1;s+=,Mf=;_itoa((ip.Flags13)&0x01,buffer1,10);s+=buffer1;s+=\n;coutFragmentoffset:(ip.Fragoff&0x1fff)endl;s+=Fragmentoffset:;_itoa(ip.Fragoff&0x1fff,buffer1,10);s+=buffer1;s+=\n;coutTimetolive:(int)ip.TimeToLiveendl;s+=Timetolive:;_itoa((int)ip.TimeToLive,buffer1,10);s+=buffer1;s+=\n;coutProtocol:(int)ip.Protocolendl;s+=Protocol:;_itoa((int)ip.Protocol,buffer1,10);s+=buffer1;s+=\n;coutHeaderChecksum:ip.HdrChksumendl;s+=HeaderChecksum:;_itoa(ip.HdrChksum,buffer1,10);s+=buffer1;s+=\n;coutSourceaddress:inet_ntoa(*(in_addr*)&ip.SrcAddr)endl;s+=Sourceaddress:;s+=inet_ntoa(*(in_addr*)&ip.SrcAddr);s+=\n;coutDestinationaddress:inet_ntoa(*(in_addr*)&ip.DstAddr)endl;s+=Destinationaddress:;s+=inet_ntoa(*(in_addr*)&ip.DstAddr);s+=\n;
本文标题:基于socket套接字的IP数据包的抓取
链接地址:https://www.777doc.com/doc-1895418 .html