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 "UDPSocket.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 #define CUDPSocket_Class "CUDPSocket"
00053
00054 CUDPSocket::CUDPSocket(BOOL bRawSocket) : CSpoofSocket(bRawSocket)
00055 {
00056 try
00057 {
00058
00059 SetName(CUDPSocket_Class);
00060 }
00061 ERROR_HANDLER("CUDPSocket")
00062 }
00063
00064 CUDPSocket::~CUDPSocket()
00065 {
00066 }
00067
00068 BOOL CUDPSocket::Create()
00069 {
00070 try
00071 {
00072
00073 SetProtocol(IPPROTO_UDP);
00074
00075
00076 return CSpoofSocket::Create(IPPROTO_UDP);
00077 }
00078 ERROR_HANDLER_RETURN("Create",FALSE)
00079 }
00080
00081 BOOL CUDPSocket::Send(unsigned short usSourcePort,
00082 IP aDestinationAddress,
00083 unsigned short usDestinationPort,
00084 const char* pBuffer,
00085 unsigned short usBufferLength)
00086 {
00087 try
00088 {
00089
00090 if (!CheckSocketValid())
00091 return FALSE;
00092
00093
00094 if (IsRaw())
00095 {
00096
00097 UDPHeader aHeader;
00098
00099
00100 aHeader.usSourcePort=htons(usSourcePort);
00101 aHeader.usDestinationPort=htons(usDestinationPort);
00102
00103
00104 aHeader.usLength=htons(UDPHeaderLength);
00105
00106
00107 aHeader.usChecksum=0;
00108
00109
00110 int iResult;
00111
00112
00113 if (usBufferLength)
00114 {
00115
00116 unsigned long ulTotalLength;
00117 ulTotalLength=UDPHeaderLength+usBufferLength;
00118
00119
00120 char* pNewBuffer;
00121 pNewBuffer=new char[ulTotalLength];
00122
00123
00124 CArray_ptr<char> pProtection(pNewBuffer);
00125
00126
00127 aHeader.usLength=htons(ulTotalLength);
00128
00129
00130 memcpy(pNewBuffer,
00131 &aHeader,
00132 UDPHeaderLength);
00133
00134
00135 memcpy(pNewBuffer+UDPHeaderLength,
00136 pBuffer,
00137 usBufferLength);
00138
00139
00140 aHeader.usChecksum=CalculatePseudoChecksum(pNewBuffer,
00141 ulTotalLength,
00142 aDestinationAddress,
00143 ulTotalLength);
00144
00145
00146 FinalUDPHeader(&aHeader);
00147
00148
00149 memcpy(pNewBuffer,
00150 &aHeader,
00151 UDPHeaderLength);
00152
00153
00154 iResult=CSpoofSocket::Send(aDestinationAddress,
00155 pNewBuffer,
00156 ulTotalLength,
00157 usDestinationPort);
00158 }
00159 else
00160 {
00161
00162 aHeader.usChecksum=CalculatePseudoChecksum((char*)&aHeader,
00163 UDPHeaderLength,
00164 aDestinationAddress,
00165 UDPHeaderLength);
00166
00167
00168 FinalUDPHeader(&aHeader);
00169
00170
00171 iResult=CSpoofSocket::Send(aDestinationAddress,
00172 (char*)&aHeader,
00173 UDPHeaderLength,
00174 usDestinationPort);
00175 }
00176
00177
00178 return iResult;
00179 }
00180 else
00181
00182 return CSpoofSocket::Send(aDestinationAddress,
00183 pBuffer,
00184 usBufferLength,
00185 usDestinationPort);
00186 }
00187 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00188 }
00189
00190 BOOL CUDPSocket::Send(unsigned short usSourcePort,
00191 const std::string& rDestinationAddress,
00192 unsigned short usDestinationPort,
00193 const char* pBuffer,
00194 unsigned short usBufferLength)
00195 {
00196 try
00197 {
00198
00199 return Send(usSourcePort,
00200 StringToLong(rDestinationAddress),
00201 usDestinationPort,
00202 pBuffer,
00203 usBufferLength);
00204 }
00205 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00206 }
00207
00208 BOOL CUDPSocket::Send(IP aDestinationAddress,
00209 unsigned short usDestinationPort,
00210 const char* pBuffer,
00211 unsigned short usBufferLength)
00212 {
00213 try
00214 {
00215
00216 return Send(0,
00217 aDestinationAddress,
00218 usDestinationPort,
00219 pBuffer,
00220 usBufferLength);
00221 }
00222 ERROR_HANDLER_RETURN("Send",FALSE)
00223 }
00224
00225 BOOL CUDPSocket::Send(const std::string& rDestinationAddress,
00226 unsigned short usDestinationPort,
00227 const char* pBuffer,
00228 unsigned short usBufferLength)
00229 {
00230 try
00231 {
00232
00233 return Send(0,
00234 rDestinationAddress,
00235 usDestinationPort,
00236 pBuffer,
00237 usBufferLength);
00238 }
00239 ERROR_HANDLER_RETURN("Send",FALSE)
00240 }
00241
00242 void CUDPSocket::FinalUDPHeader(LPUDPHeader lpHeader)const
00243 {
00244 }
00245
00246 int CUDPSocket::Receive(char* pBuffer,
00247 unsigned long ulBufferLength)
00248 {
00249 try
00250 {
00251
00252 return CSpoofSocket::Receive(pBuffer,
00253 ulBufferLength);
00254 }
00255 ERROR_HANDLER_RETURN("Receive",GetErrorCode())
00256 }
00257
00258 int CUDPSocket::Receive(char* pBuffer,
00259 unsigned long ulBufferLength,
00260 IP& rIP,
00261 unsigned short& rSourcePort)
00262 {
00263 try
00264 {
00265
00266 return ReceiveFrom(pBuffer,
00267 ulBufferLength,
00268 rIP,
00269 rSourcePort);
00270 }
00271 ERROR_HANDLER_RETURN("Receive",GetErrorCode())
00272 }
00273
00274 KOMODIA_NAMESPACE_END