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 "DNSTCPSocket.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #include "DNSSocket.h"
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 namespace KomodiaDNS
00050 {
00051
00052 #define CDNSTCPSocket_Class "CDNSTCPSocket"
00053
00054 #define BUFFER_SIZE 2000
00055
00056 CDNSTCPSocket::CDNSTCPSocket(CDNSSocket* pSocket) : CTCPSocketAsync(FALSE),
00057 m_pDNSSocket(pSocket),
00058 m_iBufferPosition(0),
00059 m_pBuffer(NULL)
00060 {
00061 try
00062 {
00063
00064 SetName(CDNSTCPSocket_Class);
00065
00066
00067 m_pBuffer=new char[BUFFER_SIZE];
00068 }
00069 ERROR_HANDLER("CDNSTCPSocket")
00070 }
00071
00072 CDNSTCPSocket::~CDNSTCPSocket()
00073 {
00074 try
00075 {
00076
00077 delete [] m_pBuffer;
00078 }
00079 ERROR_HANDLER("~CDNSTCPSocket")
00080 }
00081
00082 BOOL CDNSTCPSocket::OnSocketClose(int iErrorCode)
00083 {
00084 try
00085 {
00086
00087 m_pDNSSocket->SocketClosed();
00088
00089
00090 return TRUE;
00091 }
00092 ERROR_HANDLER_RETURN("OnSocketClose",TRUE)
00093 }
00094
00095 BOOL CDNSTCPSocket::OnSocketReceive(int iErrorCode)
00096 {
00097 try
00098 {
00099
00100 if (!iErrorCode)
00101 {
00102
00103 char* pBuffer;
00104 pBuffer=new char[BUFFER_SIZE];
00105
00106
00107 if (m_iBufferPosition)
00108
00109 memcpy(pBuffer,
00110 m_pBuffer,
00111 m_iBufferPosition);
00112
00113
00114
00115 int iReceived=0;
00116
00117
00118 iReceived=Receive(pBuffer+m_iBufferPosition,
00119 BUFFER_SIZE-m_iBufferPosition);
00120
00121
00122 if (iReceived==CSpoofBase::GetErrorCode())
00123 {
00124
00125 m_pDNSSocket->SocketError(GetSystemLastError());
00126
00127
00128 delete [] pBuffer;
00129
00130
00131 m_iBufferPosition=0;
00132
00133
00134 return TRUE;
00135 }
00136
00137
00138 iReceived+=m_iBufferPosition;
00139 m_iBufferPosition=0;
00140
00141
00142 int iPosition;
00143 iPosition=0;
00144
00145 BOOL bQuit;
00146 bQuit=FALSE;
00147
00148
00149 while (!bQuit)
00150 {
00151
00152 unsigned short usFragSize;
00153 usFragSize=(((unsigned short)pBuffer[iPosition]) << 8) +
00154 (unsigned char)pBuffer[iPosition+1]+2;
00155
00156
00157 if (iPosition+usFragSize>iReceived)
00158 bQuit=TRUE;
00159 else if ((iPosition+usFragSize)==iReceived)
00160 {
00161
00162 iPosition+=usFragSize;
00163
00164
00165 bQuit=TRUE;
00166 }
00167 else
00168 iPosition+=usFragSize;
00169 }
00170
00171
00172 if (iPosition<iReceived)
00173 {
00174
00175 m_iBufferPosition=iReceived-iPosition;
00176
00177
00178 memcpy(m_pBuffer,
00179 pBuffer+iPosition,
00180 m_iBufferPosition);
00181
00182
00183 iReceived-=m_iBufferPosition;
00184 }
00185
00186
00187 if (iReceived)
00188
00189 m_pDNSSocket->SocketReceive(pBuffer,
00190 iReceived,
00191 TRUE);
00192 }
00193 else
00194
00195 m_pDNSSocket->SocketError(iErrorCode);
00196
00197
00198 return TRUE;
00199 }
00200 ERROR_HANDLER_RETURN("OnSocketReceive",TRUE)
00201 }
00202
00203 BOOL CDNSTCPSocket::OnSocketConnect(int iErrorCode)
00204 {
00205 try
00206 {
00207
00208 m_pDNSSocket->SocketConnected(iErrorCode);
00209
00210
00211 return TRUE;
00212 }
00213 ERROR_HANDLER_RETURN("OnSocketConnect",FALSE)
00214 }
00215
00216
00217 }