KomodiaDNS::CDNSSocket Class Reference#include <DNSSocket.h>
Inheritance diagram for KomodiaDNS::CDNSSocket:
![]()
Collaboration diagram for KomodiaDNS::CDNSSocket:
![]()
Detailed DescriptionDefinition at line 63 of file DNSSocket.h. Member Typedef Documentation
Member Enumeration Documentation
Definition at line 53 of file ErrorHandler.h. 00054 { 00055 lpDebug, 00056 lpMessage, 00057 lpCritical, 00058 lpError 00059 } LogPriority;
Constructor & Destructor Documentation
Definition at line 62 of file DNSSocket.cpp. 00066 : CErrorHandler(), 00067 m_pFather(pFather), 00068 m_bUseTCP(bTCP), 00069 m_bAsync(bAsync), 00070 m_strDNSServer(strDNSServer), 00071 m_pThreadManager(NULL), 00072 m_pTCP(NULL), 00073 m_pUDP(NULL), 00074 m_pCSection(NULL), 00075 m_bOwners(FALSE), 00076 m_bAsyncConnect(bAsyncConnect), 00077 m_aConnectionStatus(csNone), 00078 m_pCSectionDeque(NULL) 00079 { 00080 try 00081 { 00082 //Set our name 00083 SetName(CDNSSocket_Class); 00084 00085 //The timeout 00086 m_iTimeout=INITIAL_TIMEOUT; 00087 00088 //Create the CS 00089 m_pCSection=COSManager::CreateCriticalSection(); 00090 m_pCSectionDeque=COSManager::CreateCriticalSection(); 00091 } 00092 ERROR_HANDLER("CDNSSocket") 00093 }
Definition at line 95 of file DNSSocket.cpp. 00096 { 00097 try 00098 { 00099 //Deletet the data 00100 std::for_each(m_aPendingData.begin(), 00101 m_aPendingData.end(), 00102 DeleteProc); 00103 00104 //Clear it 00105 m_aPendingData.clear(); 00106 00107 //Delete the sockets 00108 //Do we have these sockets ? 00109 if (m_pTCP) 00110 m_pTCP->DeleteSocketFromThread(); 00111 00112 if (m_pUDP) 00113 m_pUDP->DeleteSocketFromThread(); 00114 00115 if (m_bOwners) 00116 //Delete the thread manager 00117 delete m_pThreadManager; 00118 00119 //Delete the CS 00120 delete m_pCSection; 00121 delete m_pCSectionDeque; 00122 } 00123 ERROR_HANDLER("~CDNSSocket") 00124 }
Member Function Documentation
Definition at line 122 of file ErrorHandler.cpp. 00123 { 00124 try 00125 { 00126 //Get the error string 00127 LPVOID lpMsgBuf; 00128 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 00129 FORMAT_MESSAGE_FROM_SYSTEM | 00130 FORMAT_MESSAGE_IGNORE_INSERTS, 00131 NULL, 00132 dwErrorCode, 00133 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 00134 (LPTSTR) &lpMsgBuf, 00135 0, 00136 NULL); 00137 00138 //Save it 00139 std::string sMessage; 00140 sMessage+=(char*)lpMsgBuf; 00141 00142 //Release the buffer 00143 LocalFree(lpMsgBuf); 00144 00145 //Done 00146 return sMessage; 00147 } 00148 catch (...) 00149 { 00150 return "Unknown"; 00151 } 00152 }
Definition at line 521 of file ErrorHandler.cpp. 00522 { 00523 try 00524 { 00525 //Our string 00526 std::string sDate; 00527 00528 //Our tmp buf 00529 char cTmp[128]; 00530 00531 //Get date 00532 _strdate(cTmp); 00533 sDate=cTmp; 00534 sDate+=' '; 00535 00536 //Get time 00537 _strtime(cTmp); 00538 sDate+=cTmp; 00539 00540 //Done 00541 return sDate; 00542 } 00543 ERROR_HANDLER_STATIC_RETURN(CErrorHandler_Class,"GetCurrentDateTime","") 00544 }
Definition at line 135 of file DNSSocket.cpp. 00136 { 00137 try 00138 { 00139 //Initialize our sockets 00140 //if TCP 00141 if (m_bUseTCP) 00142 { 00143 //Do we have the socket 00144 if (m_pTCP) 00145 delete m_pTCP; 00146 00147 //Create the socket 00148 m_pTCP=new CDNSTCPSocket(this); 00149 00150 //Try to create it 00151 if (!m_pTCP->Create()) 00152 { 00153 //Write the error 00154 ReportError("Initialize","Failed to create socket!"); 00155 00156 //Exit 00157 return FALSE; 00158 } 00159 00160 //Set the blocking mode 00161 if (!m_bAsync) 00162 if (!m_pTCP->ReBlock()) 00163 { 00164 //Write an error 00165 ReportError("Initialize","Failed to switch socket to blocking mode!"); 00166 00167 //And exit 00168 return FALSE; 00169 } 00170 else 00171 ; 00172 else 00173 //Set it to have buffer 00174 m_pTCP->AllowBlockedBuffer(TRUE); 00175 } 00176 else 00177 { 00178 //Do we have it 00179 if (m_pUDP) 00180 delete m_pUDP; 00181 00182 //Create the socket 00183 m_pUDP=new CDNSUDPSocket(this); 00184 00185 //Try to create it 00186 if (!m_pUDP->Create()) 00187 { 00188 //Write the error 00189 ReportError("Initialize","Failed to create socket!"); 00190 00191 //Exit 00192 return FALSE; 00193 } 00194 00195 //Set the blocking mode 00196 if (!m_bAsync) 00197 if (!m_pUDP->ReBlock()) 00198 { 00199 //Write an error 00200 ReportError("Initialize","Failed to switch socket to blocking mode!"); 00201 00202 //And exit 00203 return FALSE; 00204 } 00205 else 00206 ; 00207 else 00208 if (!m_pUDP->Listen()) 00209 { 00210 //Write an error 00211 ReportError("Initialize","Failed to listen on socket to blocking mode!"); 00212 00213 //And exit 00214 return FALSE; 00215 } 00216 } 00217 00218 //Done 00219 return TRUE; 00220 } 00221 ERROR_HANDLER_RETURN("Initialize",FALSE) 00222 }
Definition at line 556 of file DNSSocket.cpp. 00561 { 00562 try 00563 { 00564 //Check if we have a thread manager 00565 if (m_pThreadManager && 00566 !bFromThread) 00567 { 00568 //Check the buf len 00569 if (iBufferLengh<=0) 00570 { 00571 //Report it 00572 ReportError("ParseAnswer","Invalid buffer length!"); 00573 00574 //Exit 00575 return NULL; 00576 } 00577 00578 //Create a job 00579 ParseThreadData* pData; 00580 pData=new ParseThreadData; 00581 00582 //Set the data 00583 pData->pData=pBuffer; 00584 pData->pOriginalData=pOriginalBuffer; 00585 pData->iDataSize=iBufferLengh; 00586 pData->pSocket=this; 00587 pData->bTCP=bTCP; 00588 00589 //Submit 00590 m_pThreadManager->SubmitJob(ThreadProc, 00591 (LPVOID)pData); 00592 00593 //Exit 00594 return NULL; 00595 } 00596 00597 //Parse the answer 00598 CDNSAnswers* pAnswers; 00599 pAnswers=new CDNSAnswers; 00600 00601 //Try to parse 00602 int iResult; 00603 iResult=pAnswers->Parse(pBuffer, 00604 iBufferLengh); 00605 00606 if (!iResult) 00607 { 00608 //Delete it, and set to null 00609 delete pAnswers; 00610 pAnswers=NULL; 00611 00612 //Write the error 00613 ReportError("ParseAnswer","Failed to parse answers"); 00614 00615 //Delete the memory 00616 delete [] pOriginalBuffer; 00617 00618 //And exit 00619 return NULL; 00620 } 00621 00622 if (m_bAsync) 00623 //Inform the manager 00624 m_pFather->OnDNSReceive(pAnswers); 00625 00626 if (iResult<iBufferLengh) 00627 { 00628 //Check if TCP 00629 if (bTCP) 00630 iResult+=2; 00631 00632 //Change the data and do again 00633 iBufferLengh-=iResult; 00634 pBuffer+=iResult; 00635 00636 //And parse again 00637 return ParseAnswer(pBuffer, 00638 pOriginalBuffer, 00639 iBufferLengh, 00640 TRUE, 00641 bTCP); 00642 } 00643 else 00644 delete [] pOriginalBuffer; 00645 00646 //Done 00647 return pAnswers; 00648 } 00649 ERROR_HANDLER_RETURN("ParseAnswer",NULL) 00650 }
Definition at line 500 of file DNSSocket.cpp. 00501 { 00502 if (m_bAsync) 00503 return NULL; //We will receive it later 00504 00505 try 00506 { 00507 //Here we initialize the receive of the DNS 00508 char* pBuffer; 00509 pBuffer=new char[2000]; 00510 00511 //How much data did we receive 00512 int iReceived=0; 00513 00514 if (m_bUseTCP) 00515 iReceived=m_pTCP->Receive(pBuffer, 00516 2000); 00517 else 00518 iReceived=m_pUDP->Receive(pBuffer, 00519 2000); 00520 00521 //Check if we had errors 00522 if (iReceived==CSpoofBase::GetErrorCode() || 00523 !iReceived) 00524 { 00525 //Delete the memory 00526 delete [] pBuffer; 00527 00528 //Done 00529 return FALSE; 00530 } 00531 00532 //Parse it 00533 return SocketReceive(pBuffer, 00534 iReceived, 00535 m_bUseTCP); 00536 } 00537 ERROR_HANDLER_RETURN("Receive",NULL) 00538 }
Definition at line 154 of file ErrorHandler.cpp. 00157 { 00158 if (!GetLog()) 00159 return; 00160 00161 try 00162 { 00163 //Get the log 00164 CErrorLog* pLog; 00165 pLog=GetLog(); 00166 00167 //Convert the error code 00168 char aTmp[11]; 00169 sprintf(aTmp,"%d",iErrorCode); 00170 00171 //Get the string for it 00172 std::string sError; 00173 sError=rMessage; 00174 sError+=", and Socket error: "; 00175 sError+=aTmp; 00176 sError+=", "; 00177 sError+=ErrorCodeToString(iErrorCode); 00178 00179 //Report to the log 00180 pLog->ReportError(m_sClassName, 00181 rMethod, 00182 sError); 00183 } 00184 ERROR_UNKNOWN("ReportError") 00185 }
Definition at line 187 of file ErrorHandler.cpp. 00189 { 00190 if (!GetLog()) 00191 return; 00192 00193 try 00194 { 00195 //Get the log 00196 CErrorLog* pLog; 00197 pLog=GetLog(); 00198 00199 //Convert the error code 00200 char aTmp[11]; 00201 sprintf(aTmp,"%d",iErrorCode); 00202 00203 //Get the string for it 00204 std::string sError; 00205 sError="Socket error: "; 00206 sError+=aTmp; 00207 sError+=", "; 00208 sError+=ErrorCodeToString(iErrorCode); 00209 00210 //Report to the log 00211 pLog->ReportError(m_sClassName, 00212 rMethod, 00213 sError); 00214 } 00215 ERROR_UNKNOWN("ReportError") 00216 }
Definition at line 275 of file ErrorHandler.cpp. 00278 { 00279 if (!GetLog()) 00280 return; 00281 00282 try 00283 { 00284 //Get the log 00285 CErrorLog* pLog; 00286 pLog=GetLog(); 00287 00288 //Convert the number 00289 char aTmp[11]; 00290 ltoa(dwAdditionalData,aTmp,10); 00291 00292 //Create the new message 00293 std::string sNewMessage(rMessage); 00294 sNewMessage+="Additional data: "; 00295 sNewMessage+=aTmp; 00296 00297 //Report to the log 00298 pLog->ReportError(m_sClassName, 00299 rMethod, 00300 sNewMessage); 00301 } 00302 ERROR_UNKNOWN("ReportError") 00303 }
Definition at line 218 of file ErrorHandler.cpp. 00220 { 00221 if (!GetLog()) 00222 return; 00223 00224 try 00225 { 00226 CErrorLog* pLog; 00227 pLog=GetLog(); 00228 00229 //Report to the log 00230 pLog->ReportError(m_sClassName, 00231 rMethod, 00232 rMessage); 00233 } 00234 ERROR_UNKNOWN("ReportError") 00235 }
Definition at line 311 of file ErrorHandler.cpp. 00312 { 00313 if (!GetLog()) 00314 return; 00315 00316 try 00317 { 00318 #ifdef WIN32 00319 //Get the last error 00320 DWORD dwLastError; 00321 dwLastError=GetLastError(); 00322 00323 //Report the error 00324 GetLog()->ReportError(m_sClassName, 00325 rMethod, 00326 ErrorCodeToString(dwLastError)); 00327 00328 #else 00329 GetLog()->ReportCatchError(m_sClassName, 00330 rMethod, 00331 "Unknown error!"); 00332 #endif 00333 } 00334 ERROR_UNKNOWN("ReportError") 00335 }
Definition at line 237 of file ErrorHandler.cpp. 00239 { 00240 if (!GetLog()) 00241 return; 00242 00243 try 00244 { 00245 //Get the last error 00246 DWORD dwLastError; 00247 dwLastError=GetLastError(); 00248 00249 //Format the message 00250 std::string sMessage; 00251 sMessage=rMessage; 00252 sMessage+=", with error code: "; 00253 00254 //Convert the error code 00255 char aTmp[11]; 00256 itoa(dwLastError,aTmp,10); 00257 00258 //Add it again 00259 sMessage+=aTmp; 00260 sMessage+=" "; 00261 sMessage+=ErrorCodeToString(dwLastError); 00262 00263 //Get the log 00264 CErrorLog* pLog; 00265 pLog=GetLog(); 00266 00267 //Report to the log 00268 pLog->ReportError(m_sClassName, 00269 rMethod, 00270 sMessage); 00271 } 00272 ERROR_UNKNOWN("ReportErrorOS") 00273 }
Definition at line 489 of file ErrorHandler.cpp. 00493 { 00494 if (!m_pLog) 00495 return; 00496 00497 try 00498 { 00499 //Convert the number 00500 char aTmp[11]; 00501 ltoa(dwAdditionalData,aTmp,10); 00502 00503 //Create the new message 00504 std::string sNewMessage(rMessage); 00505 sNewMessage+="Additional data: "; 00506 sNewMessage+=aTmp; 00507 00508 //Report to the log 00509 m_pLog->ReportError(rClass, 00510 rMethod, 00511 sNewMessage); 00512 } 00513 ERROR_UNKNOWN("ReportStaticError") 00514 }
Definition at line 472 of file ErrorHandler.cpp. 00475 { 00476 if (!m_pLog) 00477 return; 00478 00479 try 00480 { 00481 //Report to the log 00482 m_pLog->ReportError(rClass, 00483 rMethod, 00484 rMessage); 00485 } 00486 ERROR_UNKNOWN("ReportStaticError") 00487 }
Definition at line 446 of file ErrorHandler.cpp. 00448 { 00449 if (!m_pLog) 00450 return; 00451 00452 try 00453 { 00454 #ifdef WIN32 00455 //Get the last error 00456 DWORD dwLastError; 00457 dwLastError=GetLastError(); 00458 00459 //Report the error 00460 m_pLog->ReportError(rClass, 00461 rMethod, 00462 ErrorCodeToString(dwLastError)); 00463 #else 00464 m_pLog->ReportError(rClass, 00465 rMethod, 00466 "Unknown error!"); 00467 #endif 00468 } 00469 ERROR_UNKNOWN("ReportStaticError") 00470 }
Definition at line 242 of file DNSSocket.cpp. 00245 { 00246 try 00247 { 00248 //Error indicator 00249 int iError; 00250 iError=0; 00251 00252 //Are we TCP 00253 if (m_bUseTCP) 00254 { 00255 //Reconnection status 00256 BOOL bReconnect; 00257 bReconnect=FALSE; 00258 00259 //Enter the CS 00260 CCriticalAutoRelease aRelease(m_pCSection); 00261 00262 //Are we disconnected 00263 if (m_aConnectionStatus==csDisconnected) 00264 //Try to reinitialize 00265 if (!Initialize()) 00266 { 00267 //Write the error 00268 ReportError("Send","Failed to reinitialize!"); 00269 00270 //Done 00271 return FALSE; 00272 } 00273 else 00274 m_aConnectionStatus=csNone; 00275 00276 //What kind of use ? 00277 if (m_aConnectionStatus==csNone) 00278 { 00279 //Set connection to pending 00280 SetConnectionStatus(csPending); 00281 00282 //Exit the CS 00283 aRelease.Exit(); 00284 00285 //connect 00286 BOOL bResult; 00287 bResult=m_pTCP->Connect(0, 00288 m_strDNSServer.c_str(), 00289 53, 00290 !m_bAsyncConnect || 00291 bReconnect); 00292 00293 //Check for error 00294 if (!bResult) 00295 { 00296 //Set the connection status 00297 SetConnectionStatus(csNone); 00298 00299 //Write the error 00300 ReportError("Send","Failed to connect!"); 00301 00302 //Get the error 00303 iError=m_pTCP->GetSystemLastError(); 00304 } 00305 else if (!m_bAsyncConnect || 00306 !m_bAsync || 00307 bReconnect) 00308 //Set the connection status 00309 SetConnectionStatus(csConnected); 00310 } 00311 else 00312 aRelease.Exit(); 00313 00314 //Do we need to add to the pool 00315 if (m_aConnectionStatus!=csConnected && 00316 !iError) 00317 //Add to queue 00318 AddData(pBuffer, 00319 bufLen, 00320 lpLocalIDData); 00321 //Can we send the data 00322 else if (!iError && 00323 m_aConnectionStatus==csConnected) 00324 { 00325 //Try to flush the data 00326 if (!Flush()) 00327 { 00328 //Write the error 00329 ReportError("Send","Failed to flush!"); 00330 00331 //Get the error 00332 iError=1; 00333 } 00334 else if (!m_pTCP->Send(pBuffer,bufLen)) 00335 { 00336 //Write the error 00337 ReportError("Send","Failed to send!"); 00338 00339 //Get the error 00340 iError=m_pTCP->GetSystemLastError(); 00341 } 00342 } 00343 } 00344 else if (m_pUDP->Send(0, 00345 m_strDNSServer.c_str(), 00346 53, 00347 pBuffer, 00348 bufLen)==CSpoofBase::GetErrorCode()) 00349 { 00350 //Write the error 00351 ReportError("Send","Failed to send UDP packet!"); 00352 00353 //Get the error 00354 iError=m_pUDP->GetSystemLastError(); 00355 } 00356 00357 //Did we have an error ? 00358 if (iError) 00359 //Exit 00360 return FALSE; 00361 else 00362 //Parse the data 00363 return TRUE; 00364 } 00365 ERROR_HANDLER_RETURN("Send",FALSE) 00366 }
Definition at line 540 of file DNSSocket.cpp. 00541 { 00542 try 00543 { 00544 //Save it 00545 m_iTimeout=iMS; 00546 00547 //Which socket we use 00548 if (m_bUseTCP) 00549 return m_pTCP->SetConnectionTimeout(iMS); 00550 else 00551 return TRUE; 00552 } 00553 ERROR_HANDLER_RETURN("SetTimeout",FALSE) 00554 }
Definition at line 723 of file DNSSocket.cpp. 00724 { 00725 try 00726 { 00727 //Delete an old manager, if we have it 00728 if (m_pThreadManager) 00729 delete m_pThreadManager; 00730 00731 //Save it 00732 m_pThreadManager=pThreadManager; 00733 00734 //We don't own the thread manager 00735 m_bOwners=FALSE; 00736 } 00737 ERROR_HANDLER("SetMultithreaded") 00738 }
Definition at line 706 of file DNSSocket.cpp. 00707 { 00708 try 00709 { 00710 //Do we have a thread manager 00711 if (m_pThreadManager) 00712 return; 00713 00714 //Create it 00715 m_pThreadManager=new CThreadPool(iThreadNumber); 00716 00717 //We own the thread manager 00718 m_bOwners=TRUE; 00719 } 00720 ERROR_HANDLER("SetMultithreaded") 00721 }
Definition at line 673 of file DNSSocket.cpp. 00676 { 00677 try 00678 { 00679 //Save the buffer 00680 char* pOriginalBuffer; 00681 pOriginalBuffer=pBuffer; 00682 00683 //Check is it TCP 00684 if (bTCP) 00685 { 00686 //Adjust the buffer 00687 pBuffer+=2; 00688 iBufferLengh-=2; 00689 00690 //Try to flush, if we have any ? 00691 if (!Flush()) 00692 //Report it 00693 ReportError("SocketReceive","Failed to flush!"); 00694 } 00695 00696 //Parse the answer 00697 return ParseAnswer(pBuffer, 00698 pOriginalBuffer, 00699 iBufferLengh, 00700 !m_bAsync, 00701 bTCP); 00702 } 00703 ERROR_HANDLER_RETURN("SocketReceive",NULL) 00704 }
Definition at line 355 of file ErrorHandler.cpp. 00359 { 00360 if (!GetLog()) 00361 return; 00362 00363 try 00364 { 00365 //Convert the number 00366 char aTmp[11]; 00367 ltoa(dwAdditionalData,aTmp,10); 00368 00369 //Create the new message 00370 std::string sNewMessage(rMessage); 00371 sNewMessage+="Additional data: "; 00372 sNewMessage+=aTmp; 00373 00374 //Delegate the call 00375 GetLog()->WriteMessage(m_sClassName, 00376 rMethod, 00377 sNewMessage, 00378 aPriority); 00379 } 00380 ERROR_UNKNOWN("WriteMessage") 00381 }
Definition at line 337 of file ErrorHandler.cpp. 00340 { 00341 if (!GetLog()) 00342 return; 00343 00344 try 00345 { 00346 //Delegate the call 00347 GetLog()->WriteMessage(m_sClassName, 00348 rMethod, 00349 rMessage, 00350 aPriority); 00351 } 00352 ERROR_UNKNOWN("WriteMessage") 00353 }
Definition at line 403 of file ErrorHandler.cpp. 00408 { 00409 if (!m_pLog) 00410 return; 00411 00412 try 00413 { 00414 //Convert the number 00415 char aTmp[11]; 00416 ltoa(dwAdditionalData,aTmp,10); 00417 00418 //Create the new message 00419 std::string sNewMessage(rMessage); 00420 sNewMessage+="Additional data: "; 00421 sNewMessage+=aTmp; 00422 00423 //Delegate the call 00424 m_pLog->WriteMessage(rClass, 00425 rMethod, 00426 sNewMessage, 00427 aPriority); 00428 } 00429 ERROR_UNKNOWN("WriteStaticMessage") 00430 }
Definition at line 383 of file ErrorHandler.cpp. 00388 { 00389 if (!m_pLog) 00390 return; 00391 00392 try 00393 { 00394 //Delegate the call 00395 m_pLog->WriteMessage(rClass, 00396 rMethod, 00397 rMessage, 00398 aPriority); 00399 } 00400 ERROR_UNKNOWN("WriteStaticMessage") 00401 }
Friends And Related Function Documentation
Definition at line 65 of file DNSSocket.h.
Definition at line 66 of file DNSSocket.h.
The documentation for this class was generated from the following files: |