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 "SpoofSocket.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040 #include "Array_ptr.h"
00041
00042 #include <memory>
00043
00044 #ifdef _MEMORY_DEBUG
00045 #define new DEBUG_NEW
00046 #define malloc DEBUG_MALLOC
00047 static char THIS_FILE[] = __FILE__;
00048 #endif
00049
00050 KOMODIA_NAMESPACE_START
00051
00052
00053
00054 #define CIPOptions_LOGNAME "CIPOptions"
00055
00056 CIPOptions::CIPOptions() : CSpoofBase(),
00057 m_pBuffer(NULL),
00058 m_bAutoPAD(TRUE)
00059 {
00060 try
00061 {
00062
00063 SetName(CIPOptions_LOGNAME);
00064
00065
00066 m_pBuffer=new char[IPOption_SIZE];
00067
00068
00069 Reset();
00070 }
00071 ERROR_HANDLER("CIPOptions")
00072 }
00073
00074 CIPOptions::CIPOptions(const CIPOptions& rIP) : CSpoofBase(rIP),
00075 m_pBuffer(NULL),
00076 m_bAutoPAD(rIP.m_bAutoPAD)
00077 {
00078 try
00079 {
00080
00081 SetName(CIPOptions_LOGNAME);
00082
00083
00084 m_pBuffer=new char[IPOption_SIZE];
00085
00086
00087 Reset();
00088
00089
00090 memcpy(m_pBuffer,
00091 rIP.m_pBuffer,
00092 IPOption_SIZE);
00093
00094
00095 m_iBufferLength=rIP.m_iBufferLength;
00096 }
00097 ERROR_HANDLER("CIPOptions")
00098 }
00099
00100 CIPOptions::~CIPOptions()
00101 {
00102 try
00103 {
00104 delete [] m_pBuffer;
00105 }
00106 ERROR_HANDLER("~CIPOptions")
00107 }
00108
00109 void CIPOptions::AddOption_Nothing()
00110 {
00111 try
00112 {
00113
00114 OptionType aOptionType;
00115
00116
00117 aOptionType=GetOption(IPOption_DONT_COPY,
00118 IPOption_CONTROL,
00119 IPOption_NO_OPERATION);
00120
00121
00122 AddToBuffer((char*)&aOptionType,sizeof(aOptionType));
00123 }
00124 ERROR_HANDLER("AddOption_Nothing")
00125 }
00126
00127 CIPOptions::OptionType CIPOptions::GetOption(unsigned char ucCopyFlag,
00128 unsigned char ucClassFlag,
00129 unsigned char ucTypeFlag)
00130 {
00131
00132 return ucCopyFlag |
00133 ucClassFlag |
00134 ucTypeFlag;
00135 }
00136
00137 void CIPOptions::AddToBuffer(const char* pBuffer,
00138 int iBufLength)
00139 {
00140 if (m_iBufferLength<IPOption_SIZE)
00141 {
00142
00143 memcpy(m_pBuffer+m_iBufferLength,
00144 pBuffer,
00145 iBufLength);
00146
00147
00148 m_iBufferLength+=iBufLength;
00149 }
00150 }
00151
00152 const char* CIPOptions::GetBuffer()const
00153 {
00154 return m_pBuffer;
00155 }
00156
00157 int CIPOptions::GetBufferLength()const
00158 {
00159 try
00160 {
00161
00162 if (m_bAutoPAD)
00163 if (m_iBufferLength/IPOption_WRAPSIZE==(m_iBufferLength/IPOption_WRAPSIZE)*IPOption_WRAPSIZE && m_iBufferLength>=IPOption_WRAPSIZE)
00164 return m_iBufferLength;
00165 else
00166 return int((float)m_iBufferLength/IPOption_WRAPSIZE+1)*IPOption_WRAPSIZE;
00167 else
00168 return m_iBufferLength;
00169 }
00170 ERROR_HANDLER_RETURN("GetBufferLength",0)
00171 }
00172
00173 void CIPOptions::AddOption_ENDLIST()
00174 {
00175 try
00176 {
00177
00178 OptionType aOptionType;
00179
00180
00181 aOptionType=GetOption(IPOption_DONT_COPY,
00182 IPOption_CONTROL,
00183 IPOption_END_OPTION);
00184
00185
00186 AddToBuffer((char*)&aOptionType,sizeof(aOptionType));
00187 }
00188 ERROR_HANDLER("AddOption_ENDLIST")
00189 }
00190
00191 void CIPOptions::SetAutoPad(BOOL bAutoPAD)
00192 {
00193 m_bAutoPAD=bAutoPAD;
00194 }
00195
00196 CIPOptions* CSpoofSocket::GetOptions()const
00197 {
00198 return m_pIPOptions;
00199 }
00200
00201 void CIPOptions::Reset()
00202 {
00203 try
00204 {
00205
00206 memset(m_pBuffer,
00207 0,
00208 IPOption_SIZE);
00209
00210
00211 m_iBufferLength=0;
00212 }
00213 ERROR_HANDLER("Reset")
00214 }
00215
00216 void CIPOptions::AddOption_Security(unsigned short usType)
00217 {
00218 try
00219 {
00220
00221 OptionType aOptionType;
00222
00223
00224 aOptionType=GetOption(IPOption_COPY,
00225 IPOption_CONTROL,
00226 IPOption_SECURITY);
00227
00228
00229 AddToBuffer((char*)&aOptionType,
00230 sizeof(aOptionType));
00231
00232
00233 aOptionType=IPOption_SECURITY_LENGTH;
00234 AddToBuffer((char*)&aOptionType,
00235 sizeof(aOptionType));
00236
00237
00238 AddToBuffer((char*)&usType,
00239 sizeof(usType));
00240
00241
00242 unsigned short usZeros;
00243 usZeros=0;
00244 unsigned char ucZeros;
00245 ucZeros=0;
00246
00247
00248
00249 AddToBuffer((char*)&usZeros,
00250 sizeof(usZeros));
00251
00252
00253 AddToBuffer((char*)&usZeros,
00254 sizeof(usZeros));
00255
00256
00257 AddToBuffer((char*)&usZeros,
00258 sizeof(usZeros));
00259 AddToBuffer((char*)&ucZeros,
00260 sizeof(ucZeros));
00261
00262
00263 }
00264 ERROR_HANDLER("AddOption_Security")
00265 }
00266
00267 void CIPOptions::AddOption_Stream(unsigned short usStreamID)
00268 {
00269 try
00270 {
00271
00272 OptionType aOptionType;
00273
00274
00275 aOptionType=GetOption(IPOption_COPY,
00276 IPOption_CONTROL,
00277 IPOption_STREAM);
00278
00279
00280 AddToBuffer((char*)&aOptionType,
00281 sizeof(aOptionType));
00282
00283
00284 aOptionType=IPOption_STREAM_LENGTH;
00285 AddToBuffer((char*)&aOptionType,
00286 sizeof(aOptionType));
00287
00288
00289 unsigned short usnStreamID;
00290 usnStreamID=htons(usStreamID);
00291
00292
00293 AddToBuffer((char*)&usnStreamID,
00294 sizeof(usnStreamID));
00295 }
00296 ERROR_HANDLER("AddOption_Stream")
00297 }
00298
00299 void CIPOptions::AddOption_StrictRoute(Routing tRoute)
00300 {
00301 try
00302 {
00303 AddOption_Route(IPOption_STRICT_ROUTING,tRoute);
00304 }
00305 ERROR_HANDLER("AddOption_StrictRoute")
00306 }
00307
00308 void CIPOptions::AddOption_RecordRoute(int iMaxRoutes)
00309 {
00310 try
00311 {
00312
00313
00314 OptionType aOptionType;
00315
00316
00317 aOptionType=GetOption(IPOption_DONT_COPY,
00318 IPOption_CONTROL,
00319 IPOption_RECORD_ROUTE);
00320
00321
00322 AddToBuffer((char*)&aOptionType,
00323 sizeof(aOptionType));
00324
00325
00326 aOptionType=iMaxRoutes*4+IPOption_STRICT_ROUTING_LENGTH;
00327 AddToBuffer((char*)&aOptionType,
00328 sizeof(aOptionType));
00329
00330
00331 aOptionType=IPOption_STRICT_ROUTING_POINTER;
00332 AddToBuffer((char*)&aOptionType,
00333 sizeof(aOptionType));
00334
00335 char cNothing[IPOption_SIZE]="";
00336 AddToBuffer(cNothing,
00337 iMaxRoutes*4);
00338 }
00339 ERROR_HANDLER("AddOption_RecordRoute")
00340 }
00341
00342 void CIPOptions::AddOption_Route(OptionType aRouteType,
00343 Routing aRoute)
00344 {
00345 try
00346 {
00347
00348
00349 OptionType aOptionType;
00350
00351
00352 aOptionType=GetOption(IPOption_COPY,
00353 IPOption_CONTROL,
00354 aRouteType);
00355
00356
00357 AddToBuffer((char*)&aOptionType,
00358 sizeof(aOptionType));
00359
00360
00361 aOptionType=aRoute.iRoutes*4+IPOption_STRICT_ROUTING_LENGTH;
00362 AddToBuffer((char*)&aOptionType,
00363 sizeof(aOptionType));
00364
00365
00366 aOptionType=IPOption_STRICT_ROUTING_POINTER;
00367 AddToBuffer((char*)&aOptionType,
00368 sizeof(aOptionType));
00369
00370
00371 AddToBuffer((char*)aRoute.ulRoutes,
00372 aRoute.iRoutes*4);
00373 }
00374 ERROR_HANDLER("AddOption_Route")
00375 }
00376
00377 void CIPOptions::AddOption_LooseRoute(Routing tRoute)
00378 {
00379 try
00380 {
00381 AddOption_Route(IPOption_LOOSE_ROUTING,tRoute);
00382 }
00383 ERROR_HANDLER("AddOption_LooseRoute")
00384 }
00385
00386 void CIPOptions::AddOption_Timestamp(OptionType tFlags, int iMaxStamps)
00387 {
00388 try
00389 {
00390
00391 OptionType aOptionType;
00392
00393
00394 aOptionType=GetOption(IPOption_DONT_COPY,
00395 IPOption_DEBUGGING,
00396 IPOption_TIMESTAMP);
00397
00398
00399 AddToBuffer((char*)&aOptionType,
00400 sizeof(aOptionType));
00401
00402
00403 aOptionType=iMaxStamps*IPOption_TIMESTAMP_SIZE+IPOption_TIMESTAMP_LENGTH-1;
00404 AddToBuffer((char*)&aOptionType,
00405 sizeof(aOptionType));
00406
00407
00408 aOptionType=IPOption_TIMESTAMP_LENGTH;
00409 AddToBuffer((char*)&aOptionType,
00410 sizeof(aOptionType));
00411
00412
00413 AddToBuffer((char*)&tFlags,
00414 sizeof(tFlags));
00415
00416
00417 char cNothing[IPOption_SIZE]="";
00418 AddToBuffer(cNothing,
00419 iMaxStamps*IPOption_TIMESTAMP_SIZE);
00420 }
00421 ERROR_HANDLER("AddOption_Timestamp")
00422 }
00423
00424
00425
00426 #define CSpoofSocket_LOGNAME "CSpoofSocket"
00427
00428 CSpoofSocket::CSpoofSocket(BOOL bRawSocket) : CSpoofBase(),
00429 m_bRaw(bRawSocket)
00430 {
00431 try
00432 {
00433
00434 SetName(CSpoofSocket_LOGNAME);
00435
00436
00437 InitializeIP();
00438 }
00439 ERROR_HANDLER("CSpoofSocket")
00440 }
00441
00442 CSpoofSocket::CSpoofSocket(SOCKET aSocket) : CSpoofBase()
00443 {
00444
00445 if (aSocket!=INVALID_SOCKET)
00446 ReportError("CSpoofSocket","Received invalid socket!");
00447 else
00448 try
00449 {
00450
00451 SetName(CSpoofSocket_LOGNAME);
00452
00453
00454 InitializeIP();
00455
00456
00457 AssignSocket(aSocket);
00458 }
00459 ERROR_HANDLER("CSpoofSocket")
00460 }
00461
00462 CSpoofSocket::~CSpoofSocket()
00463 {
00464 try
00465 {
00466
00467 SetOptions(FALSE);
00468
00469
00470 Close();
00471 }
00472 ERROR_HANDLER("~CSpoofSocket")
00473 }
00474
00475 BOOL CSpoofSocket::Create(int iProtocol)
00476 {
00477
00478 if (ValidSocket())
00479 Close();
00480
00481 try
00482 {
00483
00484 if (!m_bOverlapped)
00485 {
00486
00487 if (m_bRaw || iProtocol==IPPROTO_ICMP)
00488 m_aSpoofSocket=socket(AF_INET,
00489 SOCK_RAW,
00490 iProtocol);
00491 else
00492 if (iProtocol==IPPROTO_TCP)
00493 m_aSpoofSocket=socket(AF_INET,
00494 SOCK_STREAM,
00495 iProtocol);
00496 else if (iProtocol==IPPROTO_UDP)
00497 m_aSpoofSocket=socket(AF_INET,
00498 SOCK_DGRAM,
00499 iProtocol);
00500 }
00501 else
00502 {
00503
00504 if (m_bRaw || iProtocol==IPPROTO_ICMP)
00505 m_aSpoofSocket=WSASocket(AF_INET,
00506 SOCK_RAW,
00507 iProtocol,
00508 NULL,
00509 NULL,
00510 WSA_FLAG_OVERLAPPED);
00511 else
00512 if (iProtocol==IPPROTO_TCP)
00513 m_aSpoofSocket=WSASocket(AF_INET,
00514 SOCK_STREAM,
00515 iProtocol,
00516 NULL,
00517 NULL,
00518 WSA_FLAG_OVERLAPPED);
00519 else if (iProtocol==IPPROTO_UDP)
00520 m_aSpoofSocket=WSASocket(AF_INET,
00521 SOCK_DGRAM,
00522 iProtocol,
00523 NULL,
00524 NULL,
00525 WSA_FLAG_OVERLAPPED);
00526 }
00527
00528
00529 if (m_aSpoofSocket==INVALID_SOCKET)
00530 {
00531
00532 SetLastError("Create");
00533
00534
00535 return FALSE;
00536 }
00537
00538 if (m_bRaw)
00539 {
00540
00541 unsigned int iTrue=1;
00542
00543 if(setsockopt(m_aSpoofSocket,
00544 IPPROTO_IP,
00545 IP_HDRINCL,
00546 (char*)&iTrue,
00547 sizeof(iTrue))==GetErrorCode())
00548 {
00549
00550 SetLastError("Create");
00551
00552
00553 return FALSE;
00554 }
00555 }
00556
00557
00558 return TRUE;
00559 }
00560 ERROR_HANDLER_RETURN("Create",FALSE)
00561 }
00562
00563 int CSpoofSocket::SendRawBuffer(IP aDestinationAddress,
00564 const char* pBuffer,
00565 unsigned long ulBufferLength,
00566 unsigned short usDestinationPort)
00567 {
00568 try
00569 {
00570
00571
00572 if (m_aSpoofSocket==INVALID_SOCKET ||
00573 !m_bRaw)
00574 return FALSE;
00575
00576
00577 sockaddr_in m_TargetAddress;
00578
00579 m_TargetAddress.sin_family=AF_INET;
00580 m_TargetAddress.sin_addr.s_addr=aDestinationAddress;
00581 m_TargetAddress.sin_port=htons(usDestinationPort);
00582 memset(&m_TargetAddress.sin_zero,0,sizeof(m_TargetAddress.sin_zero));
00583
00584
00585
00586 int iResult;
00587 iResult=sendto(GetHandle(),
00588 pBuffer,
00589 ulBufferLength,
00590 0,
00591 (sockaddr*)&m_TargetAddress,
00592 sizeof(m_TargetAddress));
00593
00594 if (iResult==GetErrorCode())
00595
00596 SetLastError("SendRawBuffer");
00597
00598
00599 return iResult;
00600 }
00601 ERROR_HANDLER_RETURN("SendRawBuffer",GetErrorCode())
00602 }
00603
00604 int CSpoofSocket::Send(IP aDestinationAddress,
00605 const char* pBuffer,
00606 unsigned long ulBufferLength,
00607 unsigned short usDestinationPort)
00608 {
00609 try
00610 {
00611
00612 if (!CheckSocketValid())
00613 return GetErrorCode();
00614
00615
00616 if (ulBufferLength &&
00617 !pBuffer)
00618 return GetErrorCode();
00619
00620
00621 sockaddr_in aTargetAddress;
00622 memset(&aTargetAddress,
00623 0,
00624 sizeof(aTargetAddress));
00625
00626 aTargetAddress.sin_family=AF_INET;
00627 aTargetAddress.sin_addr.s_addr=aDestinationAddress;
00628 aTargetAddress.sin_port=htons(usDestinationPort);
00629
00630
00631 int iResult;
00632
00633
00634 if (m_bRaw)
00635 {
00636
00637 unsigned char ucHeaderLength;
00638 ucHeaderLength=IpHeaderLength;
00639
00640
00641 if (m_bOptions)
00642 ucHeaderLength+=m_pIPOptions->GetBufferLength();
00643
00644
00645 LPIpHeader lpHead=ConstructIPHeader(m_ucProtocol,
00646 IpFragFlag_DONT_FRAG,
00647 m_ucTTL,
00648 (unsigned short)GetCurrentProcessId(),
00649 ucHeaderLength);
00650
00651
00652 std::auto_ptr<IpHeader> pProtection(lpHead);
00653
00654
00655 SetIPHeaderAddress(lpHead,
00656 m_ulSourceAddress,
00657 aDestinationAddress);
00658
00659
00660 int iTotalLength;
00661 iTotalLength=ucHeaderLength+ulBufferLength;
00662
00663
00664 lpHead->usTotalLength=htons(iTotalLength);
00665
00666
00667 char* pNewBuffer;
00668 pNewBuffer=new char[iTotalLength];
00669
00670
00671 CArray_ptr<char> pBufferProtection(pNewBuffer);
00672
00673
00674 memcpy(pNewBuffer,
00675 lpHead,
00676 IpHeaderLength);
00677
00678
00679 if (m_bOptions)
00680 memcpy(pNewBuffer+IpHeaderLength,
00681 m_pIPOptions->GetBuffer(),
00682 m_pIPOptions->GetBufferLength());
00683
00684
00685 if (pBuffer)
00686 memcpy(pNewBuffer+ucHeaderLength,
00687 pBuffer,
00688 ulBufferLength);
00689
00690
00691 lpHead->usChecksum=CalculateChecksum((unsigned short*)pNewBuffer,
00692 ucHeaderLength);
00693
00694
00695 FinalIPHeader(lpHead);
00696
00697
00698 memcpy(pNewBuffer,
00699 lpHead,
00700 IpHeaderLength);
00701
00702
00703 iResult=sendto(GetHandle(),
00704 (const char*)pNewBuffer,
00705 iTotalLength,
00706 0,
00707 (sockaddr*)&aTargetAddress,
00708 sizeof(aTargetAddress));
00709
00710
00711 if (iResult==GetErrorCode() &&
00712 GetSystemLastError()!=WSAEWOULDBLOCK)
00713
00714 SetLastError("Send - Raw");
00715 }
00716 else
00717 {
00718
00719 iResult=!GetErrorCode();
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 if (iResult!=GetErrorCode())
00738
00739 iResult=sendto(GetHandle(),
00740 (const char*)pBuffer,
00741 ulBufferLength,
00742 0,
00743 (sockaddr*)&aTargetAddress,
00744 sizeof(aTargetAddress));
00745
00746
00747 if (iResult==GetErrorCode() &&
00748 GetSystemLastError()!=WSAEWOULDBLOCK)
00749
00750 SetLastError("Send");
00751 }
00752
00753
00754 return iResult;
00755 }
00756 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00757 }
00758
00759 int CSpoofSocket::Send(const std::string& rDestinationAddress,
00760 const char* pBuffer,
00761 unsigned long ulBufferLength,
00762 unsigned short usDestinationPort)
00763 {
00764 try
00765 {
00766
00767 if (!CheckSocketValid())
00768 return GetErrorCode();
00769
00770 return Send(inet_addr(rDestinationAddress.c_str()),
00771 pBuffer,
00772 ulBufferLength,
00773 usDestinationPort);
00774 }
00775 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00776 }
00777
00778 LPIpHeader CSpoofSocket::ConstructIPHeader(unsigned char ucProtocol,
00779 unsigned short usFragmentationFlags,
00780 unsigned char ucTTL,
00781 unsigned short usIdentification,
00782 unsigned char ucHeaderLength)const
00783 {
00784 return ConstructStaticIPHeader(ucProtocol,
00785 usFragmentationFlags,
00786 ucTTL,
00787 usIdentification,
00788 ucHeaderLength);
00789 }
00790
00791 void CSpoofSocket::SetIPHeaderAddress(LPIpHeader lpHead,
00792 IP aSourceAddress,
00793 IP aDestinationAddress)const
00794 {
00795 try
00796 {
00797
00798
00799 if (!aSourceAddress || !aDestinationAddress)
00800 {
00801
00802 ReportError("SetIPHeaderAddress","Recieved empty source or destination address!");
00803
00804
00805 return;
00806 }
00807
00808
00809 lpHead->ulSourceAddress=aSourceAddress;
00810
00811
00812 lpHead->ulDestinationAddress=aDestinationAddress;
00813
00814
00815 }
00816 ERROR_HANDLER("SetIPHeaderAddress")
00817 }
00818
00819 BOOL CSpoofSocket::ValidSocket() const
00820 {
00821 return m_aSpoofSocket!=INVALID_SOCKET;
00822 }
00823
00824 unsigned short CSpoofSocket::CalculateChecksum(const unsigned short *pBuffer,
00825 int iSize)
00826 {
00827 try
00828 {
00829 unsigned long usChksum=0;
00830
00831
00832 while (iSize>1)
00833 {
00834 usChksum+=*pBuffer++;
00835 iSize-=sizeof(unsigned short);
00836 }
00837
00838
00839 if (iSize)
00840 usChksum+=*(unsigned char*)pBuffer;
00841
00842
00843 usChksum=(usChksum >> 16) + (usChksum & 0xffff);
00844 usChksum+=(usChksum >> 16);
00845
00846
00847 return (unsigned short)(~usChksum);
00848 }
00849 ERROR_HANDLER_STATIC_RETURN(CSpoofSocket_LOGNAME,"CalculateChecksum",0)
00850 }
00851
00852 BOOL CSpoofSocket::Bind(IP aSourceAddress,
00853 unsigned short usPort)
00854 {
00855 try
00856 {
00857
00858 if (!CheckSocketValid())
00859 return FALSE;
00860
00861
00862 sockaddr_in soSrc;
00863
00864
00865 memset(&soSrc,0,sizeof(soSrc));
00866 soSrc.sin_family=AF_INET;
00867
00868
00869 if (aSourceAddress)
00870 soSrc.sin_addr.s_addr=aSourceAddress;
00871 else
00872 soSrc.sin_addr.s_addr=ADDR_ANY ;
00873
00874 soSrc.sin_port=htons(usPort);
00875
00876
00877 if (bind(GetHandle(),
00878 (sockaddr*)&soSrc,
00879 sizeof(soSrc)))
00880 {
00881
00882 SetLastError("Bind");
00883
00884
00885 return FALSE;
00886 }
00887 else
00888
00889 m_aConnectedTo=soSrc;
00890
00891
00892 if (!m_ulSourceAddress)
00893
00894 m_ulSourceAddress=aSourceAddress;
00895
00896 return TRUE;
00897 }
00898 ERROR_HANDLER_RETURN("Bind",FALSE)
00899 }
00900
00901 BOOL CSpoofSocket::Bind(const std::string& rSourceAddress,
00902 unsigned short usPort)
00903 {
00904 try
00905 {
00906
00907 if (!CheckSocketValid())
00908 return FALSE;
00909
00910 if (rSourceAddress.empty())
00911 return Bind((IP)0,
00912 usPort);
00913 else
00914 return Bind(inet_addr(rSourceAddress.c_str()),
00915 usPort);
00916 }
00917 ERROR_HANDLER_RETURN("Bind",FALSE)
00918 }
00919
00920 SOCKET CSpoofSocket::GetHandle() const
00921 {
00922 return m_aSpoofSocket;
00923 }
00924
00925 BOOL CSpoofSocket::CheckSocketValid() const
00926 {
00927 try
00928 {
00929
00930 if (!ValidSocket())
00931 {
00932
00933 ReportError("CheckSocketValid","Operation made on non existant socket!");
00934
00935
00936 return FALSE;
00937 }
00938
00939
00940 return TRUE;
00941 }
00942 ERROR_HANDLER_RETURN("CheckSocketValid",FALSE)
00943 }
00944
00945 BOOL CSpoofSocket::Close()
00946 {
00947 try
00948 {
00949
00950
00951 if (!ValidSocket())
00952 return FALSE;
00953
00954
00955 if (closesocket(GetHandle())==GetErrorCode())
00956 {
00957
00958 SetLastError("Close");
00959
00960
00961 return FALSE;
00962 }
00963
00964
00965 m_aSpoofSocket=INVALID_SOCKET;
00966
00967
00968 return TRUE;
00969 }
00970 ERROR_HANDLER_RETURN("Close",FALSE)
00971 }
00972
00973
00974 void CSpoofSocket::SetProtocol(unsigned char ucProtocol)
00975 {
00976 m_ucProtocol=ucProtocol;
00977 }
00978
00979 void CSpoofSocket::SetSourceAddress(IP aSourceAddress)
00980 {
00981 m_ulSourceAddress=aSourceAddress;
00982 }
00983
00984 void CSpoofSocket::SetSourceAddress(const std::string& rSourceAddress)
00985 {
00986 try
00987 {
00988
00989 if (rSourceAddress.empty())
00990 m_ulSourceAddress=0;
00991 else
00992 m_ulSourceAddress=inet_addr(rSourceAddress.c_str());
00993 }
00994 ERROR_HANDLER("SetSourceAddress")
00995 }
00996
00997 unsigned short CSpoofSocket::CalculatePseudoChecksum(const char* pBuffer,
00998 int iBufferLength,
00999 IP aDestinationAddress,
01000 int iPacketLength)const
01001 {
01002 try
01003 {
01004
01005 LPPseudoHeader lpPseudo;
01006 lpPseudo=new PseudoHeader;
01007
01008
01009 std::auto_ptr<PseudoHeader> pProtection(lpPseudo);
01010
01011
01012 lpPseudo->ulDestinationAddress=aDestinationAddress;
01013 lpPseudo->ulSourceAddress=m_ulSourceAddress;
01014 lpPseudo->ucZeros=0;
01015 lpPseudo->ucPTCL=m_ucProtocol;
01016 lpPseudo->usLength=htons(iPacketLength);
01017
01018
01019 int iTotalLength;
01020 iTotalLength=PseudoHeaderLength+iBufferLength;
01021
01022
01023 char* pNewBuffer;
01024 pNewBuffer=new char[iTotalLength];
01025
01026
01027 CArray_ptr<char> pBufferProtection(pNewBuffer);
01028
01029
01030 memcpy(pNewBuffer,lpPseudo,PseudoHeaderLength);
01031
01032
01033 memcpy(pNewBuffer+PseudoHeaderLength,
01034 pBuffer,
01035 iBufferLength);
01036
01037
01038 unsigned short usChecksum;
01039 usChecksum=CalculateChecksum((unsigned short*)pNewBuffer,iTotalLength);
01040
01041
01042 return usChecksum;
01043 }
01044 ERROR_HANDLER_RETURN("CalculatePseudoChecksum",0)
01045 }
01046
01047 void CSpoofSocket::SetTTL(unsigned char ucTTL)
01048 {
01049 try
01050 {
01051
01052 if (!CheckSocketValid())
01053 return;
01054
01055 if (m_bRaw)
01056
01057 m_ucTTL=ucTTL;
01058 else if(setsockopt(GetHandle(),
01059 IPPROTO_IP,
01060 IP_TTL,
01061 (const char*)&ucTTL,
01062 sizeof(ucTTL)))
01063 SetLastError("SetTTL");
01064 }
01065 ERROR_HANDLER("SetTTL")
01066 }
01067
01068 void CSpoofSocket::SetOptions(BOOL bOptions)
01069 {
01070 try
01071 {
01072
01073 m_bOptions=bOptions;
01074
01075
01076 if (m_pIPOptions)
01077 {
01078 delete m_pIPOptions;
01079 m_pIPOptions=NULL;
01080 }
01081
01082
01083 if (bOptions)
01084 m_pIPOptions=new CIPOptions;
01085 }
01086 ERROR_HANDLER("SetOptions")
01087 }
01088
01089 BOOL CSpoofSocket::IsRaw() const
01090 {
01091 return m_bRaw;
01092 }
01093
01094 void CSpoofSocket::InitializeIP()
01095 {
01096 try
01097 {
01098
01099 m_aSpoofSocket=INVALID_SOCKET;
01100
01101
01102 m_ulSourceAddress=0;
01103
01104
01105 m_ucTTL=IP_DEF_TTL;
01106
01107
01108 m_pIPOptions=NULL;
01109
01110
01111 m_bSniffing=FALSE;
01112
01113
01114 m_bOverlapped=FALSE;
01115
01116
01117 memset(&m_aConnectedTo,
01118 0,
01119 sizeof(m_aConnectedTo));
01120
01121
01122 SetOptions(FALSE);
01123 }
01124 ERROR_HANDLER("InitializeIP")
01125 }
01126
01127 void CSpoofSocket::AssignSocket(SOCKET aNewSocket,unsigned char ucProtocol)
01128 {
01129 try
01130 {
01131
01132 m_ucProtocol=ucProtocol;
01133
01134
01135 m_aSpoofSocket=aNewSocket;
01136 }
01137 ERROR_HANDLER("AssignSocket")
01138 }
01139
01140 int CSpoofSocket::ReceiveFrom(char* pBuffer,
01141 unsigned long ulBufferLength,
01142 IP& rIP,
01143 unsigned short& rSourcePort)
01144 {
01145 try
01146 {
01147 if (!ValidSocket() ||
01148 !pBuffer ||
01149 !ulBufferLength)
01150 return GetErrorCode();
01151
01152
01153 int iResult;
01154
01155
01156 if (m_ucProtocol!=IPPROTO_TCP &&
01157 !m_bSniffing)
01158 {
01159
01160 sockaddr saConnected;
01161
01162 int iTmp;
01163 iTmp=sizeof(saConnected);
01164
01165
01166 iResult=recvfrom(GetHandle(),
01167 pBuffer,
01168 ulBufferLength,
01169 NULL,
01170 &saConnected,
01171 &iTmp);
01172
01173
01174 if (iResult!=GetErrorCode())
01175 {
01176
01177 rIP=((sockaddr_in*)&saConnected)->sin_addr.S_un.S_addr;
01178
01179
01180 rSourcePort=htons(((sockaddr_in*)&saConnected)->sin_port);
01181
01182
01183 return iResult;
01184 }
01185 else
01186 {
01187
01188 SetLastError("Receive");
01189
01190
01191 rIP=0;
01192 rSourcePort=0;
01193
01194
01195 return iResult;
01196 }
01197 }
01198 else
01199 {
01200
01201 ReportError("Receive","Can't run on TCP socket!");
01202
01203
01204 return GetErrorCode();
01205 }
01206 }
01207 ERROR_HANDLER_RETURN("Receive",GetErrorCode())
01208 }
01209
01210 int CSpoofSocket::Receive(char *pBuffer,
01211 unsigned long ulBufferLength)
01212 {
01213 try
01214 {
01215 if (!ValidSocket() ||
01216 !pBuffer ||
01217 !ulBufferLength)
01218 return GetErrorCode();
01219
01220
01221 int iResult;
01222
01223
01224 if (m_ucProtocol!=IPPROTO_TCP &&
01225 !m_bSniffing)
01226 {
01227
01228 sockaddr saConnected;
01229
01230 int iTmp;
01231 iTmp=sizeof(saConnected);
01232
01233
01234 iResult=recvfrom(GetHandle(),
01235 pBuffer,
01236 ulBufferLength,
01237 NULL,
01238 &saConnected,
01239 &iTmp);
01240
01241
01242 if (iResult!=GetErrorCode())
01243
01244 memcpy(&m_aConnectedTo,
01245 &saConnected,
01246 sizeof(saConnected));
01247 }
01248 else
01249
01250 iResult=recv(GetHandle(),
01251 pBuffer,
01252 ulBufferLength,
01253 NULL);
01254
01255
01256 if (iResult==GetErrorCode() &&
01257 GetSystemLastError()!=WSAEWOULDBLOCK)
01258
01259 SetLastError("Receive");
01260
01261
01262 return iResult;
01263 }
01264 ERROR_HANDLER_RETURN("Receive",GetErrorCode())
01265 }
01266
01267 BOOL CSpoofSocket::ValidAddress(const std::string& rAddress)
01268 {
01269 try
01270 {
01271 return inet_addr(rAddress.c_str())!=INADDR_NONE;
01272 }
01273 ERROR_HANDLER_STATIC_RETURN(CSpoofSocket_LOGNAME,"ValidAddress",FALSE)
01274 }
01275
01276 sockaddr_in CSpoofSocket::InternalResolveDNS(const std::string& rAddress)
01277 {
01278
01279 sockaddr_in aAddr;
01280 memset(&aAddr,0,sizeof(aAddr));
01281
01282 try
01283 {
01284
01285 hostent* pData;
01286 pData=gethostbyname(rAddress.c_str());
01287
01288
01289 if (pData)
01290
01291 memcpy(&aAddr.sin_addr,
01292 pData->h_addr,
01293 pData->h_length);
01294
01295 return aAddr;
01296 }
01297 ERROR_HANDLER_STATIC_RETURN(CSpoofSocket_LOGNAME,"InternalResolveDNS",aAddr)
01298 }
01299
01300 long CSpoofSocket::ResolveDNS(const std::string& rAddress)
01301 {
01302 try
01303 {
01304
01305 sockaddr_in aAddr;
01306 aAddr=InternalResolveDNS(rAddress.c_str());
01307
01308
01309 if (aAddr.sin_addr.S_un.S_addr==0)
01310
01311 return 0;
01312 else
01313 return aAddr.sin_addr.S_un.S_addr;
01314 }
01315 ERROR_HANDLER_STATIC_RETURN(CSpoofSocket_LOGNAME,"ResolveDNS",0)
01316 }
01317
01318 BOOL CSpoofSocket::Sniff(BOOL bSniff)
01319 {
01320
01321 if (!ValidSocket())
01322 return FALSE;
01323
01324 try
01325 {
01326 #ifdef WIN32
01327 unsigned long ulBytes;
01328 if (WSAIoctl(GetHandle(),SIO_RCVALL,&bSniff,sizeof(bSniff),NULL,0,&ulBytes,NULL,NULL))
01329 {
01330
01331 SetLastError("Sniff");
01332
01333
01334 return FALSE;
01335 }
01336
01337
01338 m_bSniffing=bSniff;
01339
01340
01341 return TRUE;
01342 #else
01343 return FALSE;
01344 #endif
01345 }
01346 ERROR_HANDLER_RETURN("Sniff",FALSE)
01347 }
01348
01349 long CSpoofSocket::GetPeerAddress() const
01350 {
01351
01352 return m_aConnectedTo.sin_addr.S_un.S_addr;
01353 }
01354
01355 BOOL CSpoofSocket::Shutdown(SocketShutdown eHow)
01356 {
01357 if (!CheckSocketValid())
01358 return FALSE;
01359
01360 try
01361 {
01362 int iHow;
01363
01364
01365 if (eHow==ssReceive)
01366 iHow=SD_RECEIVE;
01367 else if (eHow==ssSend)
01368 iHow=SD_SEND;
01369 else
01370 iHow=SD_BOTH;
01371
01372
01373 if (shutdown(GetHandle(),iHow))
01374 {
01375
01376 SetLastError("Shutdown");
01377
01378
01379 return FALSE;
01380 }
01381
01382
01383 return TRUE;
01384 }
01385 ERROR_HANDLER_RETURN("Shutdown",FALSE)
01386 }
01387
01388 unsigned short CSpoofSocket::GetPeerPort() const
01389 {
01390 return htons(m_aConnectedTo.sin_port);
01391 }
01392
01393 void CSpoofSocket::FinalIPHeader(LPIpHeader lpHead)const
01394 {
01395
01396 }
01397
01398 LPIpHeader CSpoofSocket::ConstructStaticIPHeader(unsigned char ucProtocol,
01399 unsigned short usFragmentationFlags,
01400 unsigned char ucTTL,
01401 unsigned short usIdentification,
01402 unsigned char ucHeaderLength)
01403 {
01404 try
01405 {
01406
01407 LPIpHeader lpHead=new _IpHeader;
01408
01409
01410 lpHead->ucHeaderLength_Version=(ucHeaderLength >> 2) |
01411 (IpVersion << 4);
01412
01413
01414 lpHead->ucProtocol=ucProtocol;
01415
01416
01417 lpHead->usFragmentationFlags=htons(usFragmentationFlags);
01418
01419
01420 lpHead->ucTTL=ucTTL;
01421
01422
01423 lpHead->usChecksum=0;
01424
01425
01426 lpHead->usIdentification=htons(usIdentification);
01427
01428
01429 lpHead->ucTypeOfService=IpService_ROUTINE;
01430
01431
01432 return lpHead;
01433 }
01434 ERROR_HANDLER_STATIC_RETURN(CSpoofSocket_LOGNAME,"ConstructIPHeader",NULL)
01435 }
01436
01437 BOOL CSpoofSocket::CanRead()const
01438 {
01439 try
01440 {
01441
01442 if (!CheckSocketValid())
01443 return FALSE;
01444
01445
01446 fd_set aDescriptor;
01447 FD_ZERO(&aDescriptor);
01448
01449
01450 FD_SET(GetHandle(),&aDescriptor);
01451
01452
01453 timeval aTime;
01454 aTime.tv_sec=0;
01455 aTime.tv_usec=0;
01456
01457
01458 int iRetVal;
01459 iRetVal=select(NULL,&aDescriptor,NULL,NULL,&aTime);
01460
01461
01462 if (iRetVal==GetErrorCode())
01463 {
01464
01465 SetLastError("CanRead");
01466
01467
01468 return FALSE;
01469 }
01470 else
01471
01472 return FD_ISSET(GetHandle(),&aDescriptor);
01473 }
01474 ERROR_HANDLER_RETURN("CanRead",FALSE)
01475 }
01476
01477 BOOL CSpoofSocket::CanWrite()const
01478 {
01479 try
01480 {
01481
01482 if (!CheckSocketValid())
01483 return FALSE;
01484
01485
01486 fd_set aDescriptor;
01487 FD_ZERO(&aDescriptor);
01488
01489
01490 FD_SET(GetHandle(),&aDescriptor);
01491
01492
01493 timeval aTime;
01494 aTime.tv_sec=0;
01495 aTime.tv_usec=0;
01496
01497
01498 int iRetVal;
01499 iRetVal=select(NULL,NULL,&aDescriptor,NULL,&aTime);
01500
01501
01502 if (iRetVal==GetErrorCode())
01503 {
01504
01505 SetLastError("CanWrite");
01506
01507
01508 return FALSE;
01509 }
01510 else
01511
01512 return FD_ISSET(GetHandle(),&aDescriptor);
01513 }
01514 ERROR_HANDLER_RETURN("CanWrite",FALSE)
01515 }
01516
01517 BOOL CSpoofSocket::IsCreated()const
01518 {
01519 return ValidSocket();
01520 }
01521
01522 BOOL CSpoofSocket::SetBroadcast(BOOL bBroadcast)
01523 {
01524 try
01525 {
01526
01527 if (!CheckSocketValid())
01528 return FALSE;
01529
01530
01531 if(setsockopt(GetHandle(),
01532 SOL_SOCKET,
01533 SO_BROADCAST,
01534 (char*)&bBroadcast,
01535 sizeof(bBroadcast))==GetErrorCode())
01536 {
01537
01538 SetLastError("SetBroadcast");
01539
01540
01541 return FALSE;
01542 }
01543
01544 return TRUE;
01545 }
01546 ERROR_HANDLER_RETURN("SetBroadcast",FALSE)
01547 }
01548
01549 unsigned char CSpoofSocket::GetTTL()const
01550 {
01551 return m_ucTTL;
01552 }
01553
01554 IP CSpoofSocket::GetSourceAddress()const
01555 {
01556 return m_ulSourceAddress;
01557 }
01558
01559 BOOL CSpoofSocket::HasOptions()const
01560 {
01561 return m_bOptions;
01562 }
01563
01564 unsigned char CSpoofSocket::GetProtocol()const
01565 {
01566 return m_ucProtocol;
01567 }
01568
01569 BOOL CSpoofSocket::SetReceiveTimeout(unsigned long ulMS)
01570 {
01571 try
01572 {
01573
01574 if (!CheckSocketValid())
01575 return FALSE;
01576
01577
01578 if(setsockopt(GetHandle(),
01579 SOL_SOCKET,
01580 SO_RCVTIMEO,
01581 (char*)&ulMS,
01582 sizeof(ulMS))==GetErrorCode())
01583 {
01584
01585 SetLastError("SetReceiveTimeout");
01586
01587
01588 return FALSE;
01589 }
01590 else
01591 return TRUE;
01592 }
01593 ERROR_HANDLER_RETURN("SetReceiveTimeout",FALSE)
01594 }
01595
01596 bool CSpoofSocket::operator==(const CSpoofSocket& rSocket)const
01597 {
01598 return m_aSpoofSocket==rSocket.m_aSpoofSocket;
01599 }
01600
01601 bool CSpoofSocket::operator<(const CSpoofSocket& rSocket)const
01602 {
01603 return m_aSpoofSocket<rSocket.m_aSpoofSocket;
01604 }
01605
01606 void CSpoofSocket::SetConnectedTo(const sockaddr_in& rAddress)
01607 {
01608 m_aConnectedTo=rAddress;
01609 }
01610
01611 KOMODIA_NAMESPACE_END
01612