监听发往本机和从本机发出的数据包
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
//head.h #ifndef _TCP_HEAD_ #define _TCP_HEAD_ typedef struct _tcphdr { unsigned short tcp_sport; unsigned short tcp_dport; unsigned int tcp_seq; unsigned int tcp_ack; unsigned char tcp_lenres; unsigned char tcp_flag; unsigned short tcp_win; unsigned short tcp_sum; unsigned short tcp_urp; } TCP_HEADER,*pTCP_HEADER; #endif #ifndef _UDP_HEAD_ #define _UDP_HEAD_ typedef struct _udphdr { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_len; unsigned short uh_sum; }UDP_HEADER,*pUDP_HEADER; #endif #ifndef _IP_HEAD_ #define _IP_HEAD_ typedef struct _iphdr { unsigned char h_verlen; unsigned char tos; unsigned short total_len; unsigned short ident; unsigned short frag_and_flags; unsigned char ttl; unsigned char proto; unsigned short checksum; unsigned int sourceIP; unsigned int destIP; }IP_HEADER,*pIP_HEADER; #endif //stdafx.h #pragma once #include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include "head.h" #include <winsock2.h> #include <iostream> #include <memory.h> #include <time.h> #include <windows.h> #include <Ws2tcpip.h> #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) //socket.cpp // socket.cpp // #include "stdafx.h" #include <string> #pragma comment (lib,"Ws2_32") void Decodepkt(char* buff,int len); std::string CheckProto(int pro); void DecodeUDP(char* buff); void DecodeTCP(char* buff); using namespace std; int main(int argc, char* argv[]) { WSADATA wsaData; struct sockaddr_in SourceAddr; char recvbuff[65535]={'\0'}; SOCKET ListenSocket; bool flag=true; int err=WSAStartup(MAKEWORD(2,2),&wsaData); if(err!=0){ cout<<"failed in WSAStartup()"<<endl; goto error; } ListenSocket=socket(AF_INET, SOCK_RAW,IPPROTO_IP); if(ListenSocket==INVALID_SOCKET) { cout<<"failed in socket()"<<WSAGetLastError()<<endl; goto error; } if(setsockopt(ListenSocket,IPPROTO_IP,IP_HDRINCL,(char*)&flag,sizeof(int))==SOCKET_ERROR) { cout<<"failed in setsockopt(IPP)"<<WSAGetLastError()<<endl; goto error; } struct hostent* phost; char hostname[100]={'\0'}; gethostname(hostname,100); phost=gethostbyname(hostname); int Sourcelen=sizeof(SourceAddr); SourceAddr.sin_family=AF_INET; SourceAddr.sin_port=htons(10000); memcpy(&SourceAddr.sin_addr.s_addr,phost->h_addr_list[0],phost->h_length); if(bind(ListenSocket,(struct sockaddr*)&SourceAddr,Sourcelen)!=0) { cout<<"failed in bind()"<<endl; goto error; } DWORD dwBuffIn=1; DWORD dwBuffOut[10]; DWORD dwBytesReturned =0; if(WSAIoctl(ListenSocket,SIO_RCVALL,&dwBuffIn,sizeof(dwBuffIn),dwBuffOut,sizeof(dwBuffOut),&dwBytesReturned,NULL,NULL)<0){ cout<<"failed in WSAIoctl()"<<endl; goto error; } int bytes_recv=0; while(true) { memset(recvbuff,0x00,sizeof(recvbuff)); bytes_recv=recv(ListenSocket,recvbuff,sizeof(recvbuff),0); if(bytes_recv==0) { continue; cout<<"empty"<<endl; } Decodepkt(recvbuff,sizeof(recvbuff)); } error: WSACleanup(); system("pause"); return 0; } void Decodepkt(char* buff,int len) { IP_HEADER* ip; ip=(pIP_HEADER)buff; string pro=CheckProto(ip->proto); struct in_addr source,dest; source.s_addr=(u_long)ip->sourceIP; dest.s_addr=(u_long)ip->destIP; cout<<"Network:"<<endl; cout<<"Src IP: "<<inet_ntoa(source)<<endl; cout<<"Dest IP: "<<inet_ntoa(dest)<<endl; cout<<"protocal:"<<pro<<endl; int iplen=(ip->h_verlen&0x0f)<<2; if(pro=="TCP") { DecodeTCP(buff+iplen); } else if(pro=="UDP") { DecodeUDP(buff+iplen); } } std::string CheckProto(int pro) { std::string result=""; switch(pro) { case 1: result="ICMP"; break; case 2: result="IGMP"; break; case 3: result="GGP"; break; case 4: result="IP"; break; case 6: result="TCP"; break; case 17: result="UDP"; break; default: result="Invalid protocol"; break; } return result; } void DecodeUDP(char* buff) { pUDP_HEADER udp=(pUDP_HEADER)buff; cout<<"Src Port:"<<ntohs(udp->uh_sport)<<endl; cout<<"Dest Port:"<<ntohs(udp->uh_dport)<<endl; char* data=buff+sizeof(UDP_HEADER); cout<<"Data:"<<data<<endl; } void DecodeTCP(char* buff) { pTCP_HEADER tcp=(pTCP_HEADER)buff; cout<<"Src Port:"<<ntohs(tcp->tcp_sport)<<endl; cout<<"Dest Port:"<<ntohs(tcp->tcp_dport)<<endl; char* data=buff+sizeof(TCP_HEADER); cout<<"Data:"<<data<<endl; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=335