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

📄 protocol.cpp

📁 转上传一个
💻 CPP
📖 第 1 页 / 共 2 页
字号:
   1. // Protocol.cpp: implementation of the Protocol class.   
   2. //   
   3. //////////////////////////////////////////////////////////////////////   
   4.    
   5. #include "stdafx.h"   
   6. #include "PppMessager.h"   
   7. #include "Protocol.h"   
   8.    
   9. #ifdef _DEBUG   
  10. #undef THIS_FILE   
  11. static char THIS_FILE[]=__FILE__;   
  12. #define new DEBUG_NEW   
  13. #endif   
  14.    
  15. //////////////////////////////////////////////////////////////////////   
  16. // Construction/Destruction   
  17. //////////////////////////////////////////////////////////////////////   
  18.    
  19. Protocol::Protocol()   
  20. {      
  21.     //Init setings.   
  22.     m_rawSocket = NULL;   
  23.    
  24.     m_nSendTimeOut = 500;   
  25.     m_nRecvTimeOut = 500;   
  26.    
  27.     m_pucSendBuffer = NULL;   
  28.     m_pucRecvBuffer = NULL;   
  29.        
  30.     m_pucSendBuffer = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY \   
  31.         , ICMP_PACKET_MAX_SIZE);   
  32.     m_pucRecvBuffer = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY \   
  33.         , ICMP_PACKET_MAX_SIZE);   
  34.        
  35.     m_DestSocketAddr.sin_family = AF_INET;   
  36.     m_DestSocketAddr.sin_addr.s_addr = 0;   
  37. }   
  38.    
  39. Protocol::~Protocol()   
  40. {      
  41.     m_rawSocket = NULL;   
  42.    
  43.     m_nSendTimeOut = 500;   
  44.     m_nRecvTimeOut = 500;   
  45.        
  46.     if(m_pucSendBuffer != NULL)   
  47.     {   
  48.         HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, m_pucSendBuffer);   
  49.     }   
  50.     if(m_pucRecvBuffer != NULL)   
  51.     {   
  52.         HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, m_pucRecvBuffer);   
  53.     }   
  54.    
  55.     m_pucSendBuffer = NULL;   
  56.     m_pucRecvBuffer = NULL;   
  57.        
  58.     m_DestSocketAddr.sin_family = AF_INET;   
  59.     m_DestSocketAddr.sin_addr.s_addr = 0;   
  60. }   
  61.    
  62.    
  63.    
  64. ////////////////////////////////////////////////////////////////////////////////   
  65. //   
  66. //   
  67. //   
  68. //   
  69. //   
  70. //   
  71. //   
  72. //   
  73. ////////////////////////////////////////////////////////////////////////////////   
  74. BOOL Protocol::InitProtocol(void)   
  75. {      
  76.     WSADATA wsaData;   
  77.     int nResult;   
  78.     sockaddr_in LocalAddr;   
  79.    
  80.     //Request winsock version.   
  81.     nResult = WSAStartup(MAKEWORD(2, 2), &wsaData);   
  82.     if(nResult != 0)   
  83.     {   
  84.         MessageBox(NULL,"In InitProtocol, call WSAStartup failure.", "warning", MB_OK);   
  85.         return FALSE;   
  86.     }   
  87.        
  88.     //Create icmp raw socket.   
  89.     m_rawSocket = WSASocket (AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0, WSA_FLAG_OVERLAPPED);   
  90.     if(INVALID_SOCKET == m_rawSocket)   
  91.     {   
  92.         MessageBox(NULL, "In InitProtocol, call WSASocket failure.", "warning", MB_OK);   
  93.         return FALSE;   
  94.     }   
  95.    
  96.     //Set receive timeout.   
  97.     nResult = setsockopt(m_rawSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&m_nRecvTimeOut \   
  98.         , sizeof(m_nRecvTimeOut));   
  99.    
 100.     if(SOCKET_ERROR == nResult)   
 101.     {   
 102.         MessageBox(NULL,"In InitProtocol, call setsockopt(SO_RCVTIMEO) failure." \   
 103.             , "warning", MB_OK);   
 104.         return FALSE;   
 105.     }   
 106.    
 107.     //Set send timeout.   
 108.     nResult = setsockopt(m_rawSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&m_nSendTimeOut \   
 109.         , sizeof(m_nSendTimeOut));   
 110.    
 111.     if(SOCKET_ERROR == nResult)   
 112.     {   
 113.         MessageBox(NULL,"In InitProtocol, call setsockopt(SO_SNDTIMEO) failure." \   
 114.             , "warning", MB_OK);   
 115.         return FALSE;   
 116.     }   
 117.        
 118.     //Bind raw socket to locol nic.   
 119.     LocalAddr.sin_family = AF_INET;   
 120.     LocalAddr.sin_addr.s_addr = INADDR_ANY ;// Not that inet_addr("192.168.1.107") is not   
 121.                                             // corrective, but we can find reason.;   
 122.    
 123.     nResult = bind(m_rawSocket, (sockaddr *)&LocalAddr, sizeof(LocalAddr));   
 124.     if(nResult != 0)   
 125.     {   
 126.         MessageBox(NULL, "In InitProtocol, call bind failure.", "warning", MB_OK);   
 127.         return FALSE;   
 128.     }   
 129.        
 130.     return TRUE;   
 131. }   
 132.    
 133.    
 134.    
 135. ////////////////////////////////////////////////////////////////////////////////   
 136. //   
 137. //   
 138. //   
 139. //   
 140. //   
 141. //   
 142. //   
 143. //   
 144. ////////////////////////////////////////////////////////////////////////////////   
 145. BOOL Protocol::CleanProtocol(void)   
 146. {     
 147.     //Clean resouce and set default setings.   
 148.     m_rawSocket = NULL;   
 149.    
 150.     m_nSendTimeOut = 500;   
 151.     m_nRecvTimeOut = 500;   
 152.        
 153.     if(m_pucSendBuffer != NULL)   
 154.     {   
 155.         HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, m_pucSendBuffer);   
 156.     }   
 157.     if(m_pucRecvBuffer != NULL)   
 158.     {   
 159.         HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, m_pucRecvBuffer);   
 160.     }   
 161.    
 162.     m_pucSendBuffer = NULL;   
 163.     m_pucRecvBuffer = NULL;   
 164.        
 165.     m_DestSocketAddr.sin_family = AF_INET;   
 166.     m_DestSocketAddr.sin_addr.s_addr = 0;   
 167.        
 168.     return TRUE;   
 169. }   
 170.    
 171.    
 172.    
 173. ////////////////////////////////////////////////////////////////////////////////   
 174. //   
 175. //   
 176. //   
 177. //   
 178. //   
 179. //   
 180. //   
 181. //   
 182. ////////////////////////////////////////////////////////////////////////////////   
 183. BOOL Protocol::SetTimeOut(int nOption, int nValue)   
 184. {      
 185.     if(nValue < 0)   
 186.     {   
 187.         nValue = 500;//Set default setings.   
 188.     }   
 189.    
 190.     if(SEND_TIMEOUT == nOption)   
 191.     {   
 192.         m_nSendTimeOut = nValue;   
 193.     }   
 194.     else if(RECV_TIMEOUT == nOption)   
 195.     {   
 196.         m_nRecvTimeOut = nValue;   
 197.     }   
 198.     else   
 199.     {   
 200.         //Do nothing.   
 201.     }   
 202.     return TRUE;   
 203. }   
 204.    
 205.    
 206.    
 207. ////////////////////////////////////////////////////////////////////////////////   
 208. //   
 209. //   
 210. //   
 211. //   
 212. //   
 213. //   
 214. //   
 215. //   
 216. ////////////////////////////////////////////////////////////////////////////////   
 217. int Protocol::SendIcmpPacket(char * pcDataBuffer, int nBuffLen, int nOption \   
 218.                              , char * pcDestIpAddr)   
 219. {   
 220.     int nBytes;   
 221.     BOOL bResult;   
 222.        
 223.        
 224.     //Create icmp packet.   
 225.     bResult = CreateIcmpPacket(pcDataBuffer, nBuffLen, nOption);   
 226.     if(FALSE == bResult)   
 227.     {   
 228.         MessageBox(NULL, "In SendIcmpPacket, call CreateIcmpPacket failure.", "warning" \   
 229.             , MB_OK);   
 230.         return -1;   
 231.     }   
 232.    
 233.     //test.   
 234.     RecordPacket(nBuffLen + sizeof(ICMP_HEADER) + sizeof(COMMAND_HEADER));   
 235.    
 236.     //Set dest address.   
 237.     m_DestSocketAddr.sin_family = AF_INET;   
 238.     m_DestSocketAddr.sin_addr.s_addr = inet_addr(pcDestIpAddr);   
 239.        
 240.     //Send icmp packet.   
 241.     nBytes = sendto(m_rawSocket, (char *)m_pucSendBuffer, sizeof(ICMP_HEADER) \   
 242.         + sizeof(COMMAND_HEADER) + nBuffLen, 0 \   
 243.     , (sockaddr *)&m_DestSocketAddr, sizeof(m_DestSocketAddr));   
 244.    
 245.     if(-1 == nBytes)   
 246.     {   
 247.         MessageBox(NULL, "In SendIcmpPacket, call sendto failure.", "warning", MB_OK);   
 248.         return -1;   
 249.     }   
 250.    
 251.     //test   
 252.    
 253.     nBytes = nBytes  - sizeof(ICMP_HEADER) - sizeof(COMMAND_HEADER);   
 254.    
 255.     return nBytes;   
 256. }   
 257.    
 258.    
 259.    
 260. ////////////////////////////////////////////////////////////////////////////////   
 261. //   
 262. //   
 263. //   
 264. // Not handle nOption argument.   
 265. //   
 266. // We should let users receive info about icmp header and user defined headers   
 267. // followed by icmp header.   
 268. //   
 269. ////////////////////////////////////////////////////////////////////////////////   
 270. int Protocol::RecvIcmpPacket(char * pcDataBuffer, int nBuffLen, int nOption)   
 271. {   
 272.     int nBytes;   
 273.     int nLen;   
 274.     sockaddr_in SrcSocketAddr;   
 275.    
 276.     //Check if arguments are valid.   
 277.     if(NULL == pcDataBuffer)   
 278.     {   
 279.         MessageBox(NULL, "In RecvIcmpPacket, pcDataBuffer == NULL.", "warning", MB_OK);   
 280.         return -1;   
 281.     }   
 282.     if(nBuffLen <= 0)   
 283.     {   
 284.         MessageBox(NULL, "In RecvIcmpPacket, nBufferLen <= 0.", "warning", MB_OK);   
 285.         return -1;   
 286.     }   
 287.    
 288.     //Receive packet from icmp raw socket.   
 289.     if(NULL == m_pucRecvBuffer)   
 290.     {   
 291.         MessageBox(NULL, "In RecvIcmpPacket, m_pucRecvBuffer == NULL.", "warning", MB_OK);   
 292.         return -1;   
 293.     }   
 294.    
 295.     nLen = sizeof(SrcSocketAddr);   
 296.    
 297.     nBytes = recvfrom(m_rawSocket, (char *)m_pucRecvBuffer, ICMP_PACKET_MAX_SIZE, 0 \   
 298.         , (sockaddr *)&SrcSocketAddr , &nLen);   
 299.     if(SOCKET_ERROR  == nBytes)   
 300.     {      
 301.         switch(WSAGetLastError())   
 302.         {   
 303.         case WSANOTINITIALISED:   
 304.             MessageBox(NULL,"In RecvIcmpPacket,WSAGetLastError() == WSANOTINITIALISED." \   
 305.                 , "warning", MB_OK);   
 306.             break;   

⌨️ 快捷键说明

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