00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "ICMPSocket.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #include <memory>
00042
00043 #ifdef _MEMORY_DEBUG
00044 #define new DEBUG_NEW
00045 #define malloc DEBUG_MALLOC
00046 static char THIS_FILE[] = __FILE__;
00047 #endif
00048
00049 KOMODIA_NAMESPACE_START
00050
00051 #define CICMPSocket_Class "CICMPSocket"
00052
00053 CICMPSocket::CICMPSocket(BOOL bRawSocket) : CSpoofSocket(bRawSocket),
00054 m_pICMPHeader(NULL),
00055 m_pIPHeader(NULL),
00056 m_pICMPIPHeader(NULL),
00057 m_pSendBuffer(NULL),
00058 m_pICMPICMPHeader(NULL),
00059 m_pICMPReceiveData(NULL)
00060 {
00061 try
00062 {
00063
00064 SetName(CICMPSocket_Class);
00065 }
00066 ERROR_HANDLER("CICMPSocket")
00067 }
00068
00069 CICMPSocket::~CICMPSocket()
00070 {
00071 try
00072 {
00073
00074 delete m_pIPHeader;
00075 delete m_pICMPHeader;
00076 delete m_pICMPIPHeader;
00077 delete m_pICMPICMPHeader;
00078
00079
00080 delete [] m_pICMPReceiveData;
00081 }
00082 ERROR_HANDLER("~CICMPSocket")
00083 }
00084
00085 BOOL CICMPSocket::Create()
00086 {
00087 try
00088 {
00089
00090 SetProtocol(IPPROTO_ICMP);
00091
00092
00093 return CSpoofSocket::Create(IPPROTO_ICMP);
00094 }
00095 ERROR_HANDLER_RETURN("Create",FALSE)
00096 }
00097
00098 BOOL CICMPSocket::SendUnreachable(const std::string& rDestinationAddress,
00099 unsigned char ucType)
00100 {
00101 try
00102 {
00103 return SendUnreachable(StringToLong(rDestinationAddress),
00104 ucType);
00105 }
00106 ERROR_HANDLER_RETURN("SendUnreachable",FALSE)
00107 }
00108
00109 BOOL CICMPSocket::SendUnreachable(IP aDestinationAddress,
00110 unsigned char ucType)
00111 {
00112 try
00113 {
00114 return Send(aDestinationAddress,
00115 ICMP_Unreachable,
00116 ucType);
00117 }
00118 ERROR_HANDLER_RETURN("SendUnreachable",FALSE)
00119 }
00120
00121 LPICMPHeader CICMPSocket::ConstructICMP() const
00122 {
00123 try
00124 {
00125
00126 LPICMPHeader lpHead;
00127 lpHead=new ICMPHeader;
00128
00129
00130 memset(lpHead,
00131 0,
00132 ICMPHeaderLength);
00133
00134
00135 lpHead->ulICMP_Originate_Timestamp=GetTickCount();
00136
00137
00138 return lpHead;
00139 }
00140 ERROR_HANDLER_RETURN("ConstructICMP",FALSE)
00141 }
00142
00143 BOOL CICMPSocket::SendTime(const std::string& rDestinationAddress,
00144 unsigned char ucType)
00145 {
00146 try
00147 {
00148 return SendTime(StringToLong(rDestinationAddress),
00149 ucType);
00150 }
00151 ERROR_HANDLER_RETURN("SendTime",FALSE)
00152 }
00153
00154 BOOL CICMPSocket::SendTime(IP aDestinationAddress,
00155 unsigned char ucType)
00156 {
00157 try
00158 {
00159 return Send(aDestinationAddress,
00160 ICMP_Time,
00161 ucType);
00162 }
00163 ERROR_HANDLER_RETURN("SendTime",FALSE)
00164 }
00165
00166 BOOL CICMPSocket::Send(IP aDestinationAddress,
00167 unsigned char ucICMP,
00168 unsigned char ucType)
00169 {
00170 try
00171 {
00172
00173 LPICMPHeader lpHead;
00174 lpHead=ConstructICMP();
00175
00176 if (!lpHead)
00177 {
00178
00179 ReportError("Send","Failed to construct ICMP header!");
00180
00181
00182 return FALSE;
00183 }
00184
00185
00186 std::auto_ptr<ICMPHeader> pProtection(lpHead);
00187
00188
00189 lpHead->ucICMPType=ucICMP;
00190 lpHead->ucICMPCode=ucType;
00191
00192
00193 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,ICMPHeaderLength);
00194
00195
00196 BOOL bSend;
00197 bSend=SendICMP(lpHead,
00198 aDestinationAddress);
00199
00200
00201 return bSend;
00202 }
00203 ERROR_HANDLER_RETURN("Send",FALSE)
00204 }
00205
00206 BOOL CICMPSocket::SendParameter(const std::string& rDestinationAddress,
00207 unsigned char ucError)
00208 {
00209 try
00210 {
00211 return SendParameter(StringToLong(rDestinationAddress),
00212 ucError);
00213 }
00214 ERROR_HANDLER_RETURN("SendParameter",FALSE)
00215 }
00216
00217 BOOL CICMPSocket::SendParameter(IP aDestinationAddress,
00218 unsigned char ucError)
00219 {
00220 try
00221 {
00222
00223 LPICMPHeader lpHead;
00224 lpHead=ConstructICMP();
00225
00226 if (!lpHead)
00227 {
00228
00229 ReportError("SendParameter","Failed to construct ICMP header!");
00230
00231
00232 return FALSE;
00233 }
00234
00235
00236 std::auto_ptr<ICMPHeader> pProtection(lpHead);
00237
00238
00239 lpHead->ucICMPType=ICMP_Parameter;
00240 lpHead->ucICMPCode=ICMP_Parameter_ERROR;
00241 lpHead->sICMP.sUC.uc1=ucError;
00242
00243
00244 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,ICMPHeaderLength);
00245
00246
00247 BOOL bSend;
00248 bSend=SendICMP(lpHead,
00249 aDestinationAddress);
00250
00251
00252 return bSend;
00253 }
00254 ERROR_HANDLER_RETURN("SendParameter",FALSE)
00255 }
00256
00257 BOOL CICMPSocket::SendQuench(const std::string& rDestinationAddress)
00258 {
00259 try
00260 {
00261 return SendQuench(StringToLong(rDestinationAddress));
00262 }
00263 ERROR_HANDLER_RETURN("SendQuench",FALSE)
00264 }
00265
00266 BOOL CICMPSocket::SendQuench(IP aDestinationAddress)
00267 {
00268 try
00269 {
00270 return Send(aDestinationAddress,
00271 ICMP_Quench,
00272 0);
00273 }
00274 ERROR_HANDLER_RETURN("SendQuench",FALSE)
00275 }
00276
00277 BOOL CICMPSocket::SendRedirect(const std::string& rDestinationAddress,
00278 unsigned char ucType,
00279 const std::string& rGatewayAddress)
00280 {
00281 try
00282 {
00283 return SendRedirect(StringToLong(rDestinationAddress),
00284 ucType,
00285 StringToLong(rGatewayAddress));
00286 }
00287 ERROR_HANDLER_RETURN("SendRedirect",FALSE)
00288 }
00289
00290 BOOL CICMPSocket::SendRedirect(IP aDestinationAddress,
00291 unsigned char ucType,
00292 IP aGatewayAddress)
00293 {
00294 try
00295 {
00296 LPICMPHeader lpHead;
00297 lpHead=ConstructICMP();
00298
00299 if (!lpHead)
00300 {
00301
00302 ReportError("SendRedirect","Failed to construct ICMP header!");
00303
00304
00305 return FALSE;
00306 }
00307
00308
00309 std::auto_ptr<ICMPHeader> pProtection(lpHead);
00310
00311
00312 lpHead->ucICMPType=ICMP_Redirect;
00313 lpHead->ucICMPCode=ucType;
00314 lpHead->sICMP.sUL=aGatewayAddress;
00315
00316
00317 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,ICMPHeaderLength);
00318
00319
00320 BOOL bSend;
00321 bSend=SendICMP(lpHead,
00322 aDestinationAddress);
00323
00324
00325 return bSend;
00326 }
00327 ERROR_HANDLER_RETURN("SendRedirect",FALSE)
00328 }
00329
00330 BOOL CICMPSocket::SendEcho(const std::string& rDestinationAddress,
00331 BOOL bReply,
00332 unsigned short usIdentifier,
00333 unsigned short usSequence,
00334 unsigned long ulData)
00335 {
00336 try
00337 {
00338 return SendEcho(StringToLong(rDestinationAddress),
00339 bReply,
00340 usIdentifier,
00341 usSequence,
00342 ulData);
00343 }
00344 ERROR_HANDLER_RETURN("SendEcho",FALSE)
00345 }
00346
00347 BOOL CICMPSocket::SendEcho(IP aDestinationAddress,
00348 BOOL bReply,
00349 unsigned short usIdentifier,
00350 unsigned short usSequence,
00351 unsigned long ulData)
00352 {
00353 try
00354 {
00355
00356 LPICMPHeader lpHead;
00357 lpHead=ConstructICMP();
00358
00359 if (!lpHead)
00360 {
00361
00362 ReportError("SendEcho","Failed to construct ICMP header!");
00363
00364
00365 return FALSE;
00366 }
00367
00368
00369 std::auto_ptr<ICMPHeader> pProtection(lpHead);
00370
00371
00372 if (bReply)
00373 lpHead->ucICMPType=ICMP_Echo_Reply;
00374 else
00375 lpHead->ucICMPType=ICMP_Echo;
00376
00377 lpHead->ucICMPCode=0;
00378 lpHead->sICMP.sUS.us1=htons(usIdentifier);
00379 lpHead->sICMP.sUS.us2=htons(usSequence);
00380 lpHead->ulICMP_Originate_Timestamp=htonl(ulData);
00381
00382
00383 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,ICMPHeaderLength);
00384
00385
00386 BOOL bSend;
00387 bSend=CSpoofSocket::Send(aDestinationAddress,
00388 (char*)lpHead,
00389 ICMPHeaderLength);
00390
00391
00392 return bSend;
00393 }
00394 ERROR_HANDLER_RETURN("SendEcho",FALSE)
00395 }
00396
00397 BOOL CICMPSocket::SendTimestamp(const std::string& rDestinationAddress,
00398 BOOL bReply,
00399 unsigned short usIdentifier,
00400 unsigned short usSequence,
00401 unsigned long ulOriginateTimestamp,
00402 unsigned long ulReceiveTimestamp,
00403 unsigned long ulTransmitTimestamp)
00404 {
00405 try
00406 {
00407 return SendTimestamp(StringToLong(rDestinationAddress),
00408 bReply,
00409 usIdentifier,
00410 usSequence,
00411 ulOriginateTimestamp,
00412 ulReceiveTimestamp,
00413 ulTransmitTimestamp);
00414 }
00415 ERROR_HANDLER_RETURN("SendTimestamp",FALSE)
00416 }
00417
00418 BOOL CICMPSocket::SendTimestamp(IP aDestinationAddress,
00419 BOOL bReply,
00420 unsigned short usIdentifier,
00421 unsigned short usSequence,
00422 unsigned long ulOriginateTimestamp,
00423 unsigned long ulReceiveTimestamp,
00424 unsigned long ulTransmitTimestamp)
00425 {
00426 try
00427 {
00428
00429 LPICMPHeader lpHead;
00430 lpHead=ConstructICMP();
00431
00432 if (!lpHead)
00433 {
00434
00435 ReportError("SendTimestamp","Failed to construct ICMP header!");
00436
00437
00438 return FALSE;
00439 }
00440
00441
00442 std::auto_ptr<ICMPHeader> pProtection(lpHead);
00443
00444
00445 if (bReply)
00446 lpHead->ucICMPType=ICMP_Timestamp_Reply;
00447 else
00448 lpHead->ucICMPType=ICMP_Timestamp;
00449
00450 lpHead->ucICMPCode=0;
00451 lpHead->sICMP.sUS.us1=htons(usIdentifier);
00452 lpHead->sICMP.sUS.us2=htons(usSequence);
00453 lpHead->ulICMP_Originate_Timestamp=htonl(ulOriginateTimestamp);
00454 lpHead->ulICMP_Receive_Timestamp=htonl(ulReceiveTimestamp);
00455 lpHead->ulICMP_Transmit_Timestamp=htonl(ulTransmitTimestamp);
00456
00457
00458 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,ICMPHeaderLength);
00459
00460
00461 BOOL bSend;
00462 bSend=SendICMP(lpHead,
00463 aDestinationAddress);
00464
00465
00466 return bSend;
00467 }
00468 ERROR_HANDLER_RETURN("SendTimestamp",FALSE)
00469 }
00470
00471 BOOL CICMPSocket::SendInformation(const std::string& rDestinationAddress,
00472 BOOL bReply,
00473 unsigned short usIdentifier,
00474 unsigned short usSequence)
00475 {
00476 try
00477 {
00478 return SendInformation(StringToLong(rDestinationAddress),
00479 bReply,
00480 usIdentifier,
00481 usSequence);
00482 }
00483 ERROR_HANDLER_RETURN("SendInformation",FALSE)
00484 }
00485
00486 BOOL CICMPSocket::SendInformation(IP aDestinationAddress,
00487 BOOL bReply,
00488 unsigned short usIdentifier,
00489 unsigned short usSequence)
00490 {
00491 try
00492 {
00493
00494 LPICMPHeader lpHead;
00495 lpHead=ConstructICMP();
00496
00497 if (!lpHead)
00498 {
00499
00500 ReportError("SendInformation","Failed to construct ICMP header!");
00501
00502
00503 return FALSE;
00504 }
00505
00506
00507 if (bReply)
00508 lpHead->ucICMPType=ICMP_Information_Reply;
00509 else
00510 lpHead->ucICMPType=ICMP_Information;
00511
00512 lpHead->ucICMPCode=0;
00513 lpHead->sICMP.sUS.us1=htons(usIdentifier);
00514 lpHead->sICMP.sUS.us2=htons(usSequence);
00515
00516
00517
00518 lpHead->usICMPChecksum=CalculateChecksum((unsigned short*)lpHead,
00519 ICMP_Information_SIZE);
00520
00521
00522 BOOL bSend;
00523 bSend=SendICMP(lpHead,
00524 aDestinationAddress);
00525
00526
00527 return bSend;
00528 }
00529 ERROR_HANDLER_RETURN("SendInformation",FALSE)
00530 }
00531
00532
00533 BOOL CICMPSocket::ProccessICMP(const char* pBuffer)
00534 {
00535 try
00536 {
00537
00538
00539 if (m_pICMPReceiveData)
00540 {
00541
00542 delete [] m_pICMPReceiveData;
00543 m_pICMPReceiveData=NULL;
00544 }
00545
00546
00547 m_usDataSize=0;
00548
00549
00550 if (!m_pIPHeader)
00551 m_pIPHeader=new IpHeader;
00552
00553 if (!m_pICMPHeader)
00554 m_pICMPHeader=new ICMPHeader;
00555
00556
00557 LPIpHeader lpHead;
00558 lpHead=m_pIPHeader;
00559
00560
00561 memcpy(lpHead,
00562 pBuffer,
00563 IpHeaderLength);
00564
00565
00566 unsigned char ucHeaderSize;
00567 ucHeaderSize=(lpHead->ucHeaderLength_Version & 15) << 2;
00568
00569
00570 unsigned short ucPacketSize;
00571 ucPacketSize=htons(lpHead->usTotalLength);
00572
00573
00574 memset(m_pICMPHeader,
00575 0,
00576 ICMPHeaderLength);
00577
00578
00579 unsigned short ucCopy;
00580 ucCopy=ucPacketSize-ucHeaderSize;
00581
00582
00583 m_usDataSize=ucCopy;
00584
00585 if (ucCopy>ICMPHeaderLength)
00586 ucCopy=ICMPHeaderLength;
00587
00588
00589 memcpy(m_pICMPHeader,
00590 pBuffer+ucHeaderSize,
00591 ucCopy);
00592
00593
00594 if (!m_pICMPIPHeader)
00595 m_pICMPIPHeader=new IpHeader;
00596
00597
00598 memcpy(m_pICMPIPHeader,
00599 pBuffer+ucHeaderSize+ICMP_DATA_SIZE,
00600 IpHeaderLength);
00601
00602
00603 unsigned char ucNewHeaderSize;
00604 ucNewHeaderSize=(m_pICMPIPHeader->ucHeaderLength_Version & 15) << 2;
00605
00606
00607 if (!(m_pICMPHeader->ucICMPType!=ICMP_Echo &&
00608 m_pICMPHeader->ucICMPType!=ICMP_Echo_Reply &&
00609 m_pICMPHeader->ucICMPType!=ICMP_Timestamp &&
00610 m_pICMPHeader->ucICMPType!=ICMP_Timestamp_Reply &&
00611 m_pICMPHeader->ucICMPType!=ICMP_Information &&
00612 m_pICMPHeader->ucICMPType!=ICMP_Information_Reply &&
00613 m_pICMPHeader->ucICMPType!=ICMP_Time))
00614 {
00615
00616
00617 if (!m_pICMPICMPHeader)
00618 m_pICMPICMPHeader=new ICMPHeader;
00619
00620 memcpy(m_pICMPICMPHeader,
00621 pBuffer+ucHeaderSize+ICMP_DATA_SIZE+ucNewHeaderSize,
00622 ICMP_DATA_SIZE);
00623
00624
00625 ReverseHeader(m_pICMPICMPHeader);
00626
00627
00628 unsigned short usRemainingSize;
00629 usRemainingSize=ucHeaderSize+
00630 ICMP_DATA_SIZE+
00631 ucNewHeaderSize+
00632 ICMP_DATA_SIZE;
00633
00634
00635 if (usRemainingSize<ucPacketSize)
00636 {
00637
00638 m_usDataSize=ucPacketSize-usRemainingSize;
00639
00640
00641 m_pICMPReceiveData=new char[m_usDataSize];
00642
00643
00644 memcpy(m_pICMPReceiveData,
00645 pBuffer+usRemainingSize,
00646 m_usDataSize);
00647 }
00648 }
00649 else
00650 {
00651
00652 if (m_pICMPICMPHeader)
00653 memset(m_pICMPICMPHeader,
00654 0,
00655 ICMPHeaderLength);
00656
00657
00658 unsigned short usRemainingSize;
00659 usRemainingSize=ucHeaderSize+
00660 ICMP_DATA_SIZE+
00661 ucNewHeaderSize;
00662
00663
00664 if (usRemainingSize<ucPacketSize)
00665 {
00666
00667 m_usDataSize=ucPacketSize-usRemainingSize;
00668
00669
00670 m_pICMPReceiveData=new char[m_usDataSize];
00671
00672
00673 memcpy(m_pICMPReceiveData,
00674 pBuffer+usRemainingSize,
00675 m_usDataSize);
00676 }
00677 }
00678
00679
00680 ReverseHeader(m_pICMPHeader);
00681
00682
00683 return TRUE;
00684 }
00685 ERROR_HANDLER_RETURN("ProccessICMP",FALSE)
00686 }
00687
00688 const LPICMPHeader CICMPSocket::GetLastICMPHeader() const
00689 {
00690
00691 return m_pICMPHeader;
00692 }
00693
00694 const LPIpHeader CICMPSocket::GetLastIPHeader() const
00695 {
00696 return m_pIPHeader;
00697 }
00698
00699 unsigned long CICMPSocket::GetLastDataSize()
00700 {
00701 return m_usDataSize;
00702 }
00703
00704 void CICMPSocket::ReverseHeader(LPICMPHeader pHeader)
00705 {
00706 try
00707 {
00708
00709 if (pHeader->ucICMPType==ICMP_Timestamp || pHeader->ucICMPType==ICMP_Timestamp_Reply)
00710 {
00711 pHeader->ulICMP_Originate_Timestamp=htonl(pHeader->ulICMP_Originate_Timestamp);
00712 pHeader->ulICMP_Receive_Timestamp=htonl(pHeader->ulICMP_Receive_Timestamp);
00713 pHeader->ulICMP_Transmit_Timestamp=htonl(pHeader->ulICMP_Transmit_Timestamp);
00714 }
00715
00716
00717
00718 if (pHeader->ucICMPType==ICMP_Echo ||
00719 pHeader->ucICMPType==ICMP_Echo_Reply ||
00720 pHeader->ucICMPType==ICMP_Time)
00721 {
00722 pHeader->sICMP.sUS.us1=htons(pHeader->sICMP.sUS.us1);
00723 pHeader->sICMP.sUS.us2=htons(pHeader->sICMP.sUS.us2);
00724 }
00725 }
00726 ERROR_HANDLER("ReverseHeader")
00727 }
00728
00729 const LPIpHeader CICMPSocket::GetLastICMPIPHeader() const
00730 {
00731
00732 return m_pICMPIPHeader;
00733 }
00734
00735 const LPICMPHeader CICMPSocket::GetLastICMPICMPHeader()const
00736 {
00737 return m_pICMPICMPHeader;
00738 }
00739
00740 BOOL CICMPSocket::SendICMP(LPICMPHeader lpHead,
00741 const std::string& rDestinationAddress)
00742 {
00743 try
00744 {
00745 return SendICMP(lpHead,
00746 StringToLong(rDestinationAddress));
00747 }
00748 ERROR_HANDLER_RETURN("SendICMP",FALSE)
00749 }
00750
00751 BOOL CICMPSocket::SendICMP(LPICMPHeader lpHead,
00752 IP aDestinationAddress)
00753 {
00754 try
00755 {
00756
00757 char* pSendBuffer;
00758
00759
00760 int iSendSize;
00761 iSendSize=ICMPHeaderLength;
00762
00763
00764 if (!m_pSendBuffer)
00765 pSendBuffer=(char*)lpHead;
00766 else
00767 {
00768 iSendSize+=m_usBufferSendSize;
00769 pSendBuffer=new char[m_usBufferSendSize];
00770
00771
00772 memcpy(pSendBuffer,
00773 (char*)lpHead,
00774 ICMPHeaderLength);
00775 memcpy(pSendBuffer+ICMPHeaderLength,
00776 m_pSendBuffer,
00777 m_usBufferSendSize);
00778 }
00779
00780
00781 FinalICMPHeader((LPICMPHeader)pSendBuffer);
00782
00783
00784 int iResult;
00785 iResult=CSpoofSocket::Send(aDestinationAddress,
00786 pSendBuffer,
00787 iSendSize);
00788
00789 if (m_pSendBuffer)
00790 delete [] pSendBuffer;
00791
00792
00793 return iResult>0;
00794 }
00795 ERROR_HANDLER_RETURN("SendICMP",FALSE)
00796 }
00797
00798 void CICMPSocket::SetSendBuffer(const char *pBuffer,
00799 unsigned short usSendSize)
00800 {
00801
00802 m_pSendBuffer=pBuffer;
00803 m_usBufferSendSize=usSendSize;
00804 }
00805
00806 void CICMPSocket::FinalICMPHeader(LPICMPHeader lpHead)const
00807 {
00808 }
00809
00810 const char* CICMPSocket::GetSendBuffer()const
00811 {
00812 return m_pSendBuffer;
00813 }
00814
00815 int CICMPSocket::GetBufferSize()const
00816 {
00817 return m_usBufferSendSize;
00818 }
00819
00820 const char* CICMPSocket::GetLastData()const
00821 {
00822 return m_pICMPReceiveData;
00823 }
00824
00825 KOMODIA_NAMESPACE_END