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 "TCPPortScannerMultithreaded.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040 #include "ThreadPool.h"
00041
00042 #ifdef _MEMORY_DEBUG
00043 #define new DEBUG_NEW
00044 #define malloc DEBUG_MALLOC
00045 static char THIS_FILE[] = __FILE__;
00046 #endif
00047
00048 KOMODIA_NAMESPACE_START
00049
00050 #define CTCPPortScannerMultithreaded_Class "CTCPPortScannerMultithreaded"
00051
00052 CTCPPortScannerMultithreaded::CTCPPortScannerMultithreaded() : CTCPPortScanner(),
00053 m_pThreadPool(NULL),
00054 m_iMaxThreads(10)
00055 {
00056 try
00057 {
00058
00059 SetName(CTCPPortScannerMultithreaded_Class);
00060 }
00061 ERROR_HANDLER("CTCPPortScannerMultithreaded")
00062 }
00063
00064 CTCPPortScannerMultithreaded::~CTCPPortScannerMultithreaded()
00065 {
00066 try
00067 {
00068
00069 if (m_pThreadPool &&
00070 m_bOwners)
00071 delete m_pThreadPool;
00072 }
00073 ERROR_HANDLER("~CTCPPortScannerMultithreaded")
00074 }
00075
00076 BOOL CTCPPortScannerMultithreaded::Scan(const std::string& rDestinationAddress)
00077 {
00078 try
00079 {
00080 return Scan(CSpoofBase::StringToLong(rDestinationAddress));
00081 }
00082 ERROR_HANDLER_RETURN("Scan",FALSE)
00083 }
00084
00085 BOOL CTCPPortScannerMultithreaded::Scan(IP aTarget)
00086 {
00087
00088 if (GetMaxSockets()<=0 ||
00089 !IsDone() ||
00090 !aTarget ||
00091 (m_iMaxThreads<=0 &&
00092 !m_pThreadPool))
00093 {
00094
00095 ReportError("Scan","Invalid parameters!");
00096
00097
00098 return FALSE;
00099 }
00100
00101 try
00102 {
00103
00104 ResetPair();
00105
00106
00107 if (!CanScan())
00108 {
00109
00110 ReportError("Scan","No ports given!");
00111
00112
00113 SetError(TRUE);
00114
00115
00116 return FALSE;
00117 }
00118
00119
00120 SetError(FALSE);
00121 SetDone(FALSE);
00122
00123
00124 ResetSocketCount();
00125
00126
00127 DeletePortsMap();
00128
00129
00130 ResetData();
00131
00132
00133 SetTarget(aTarget);
00134
00135
00136 if (m_bOwners &&
00137 m_pThreadPool &&
00138 m_iMaxThreads>0 &&
00139 m_pThreadPool->GetMaxThreads()!=m_iMaxThreads)
00140 {
00141
00142 delete m_pThreadPool;
00143 m_pThreadPool=NULL;
00144 }
00145
00146
00147 if (!m_pThreadPool)
00148 {
00149
00150 m_pThreadPool=new CThreadPool(m_iMaxThreads);
00151
00152
00153 m_bOwners=TRUE;
00154 }
00155
00156
00157 for (int iCounter=0;
00158 iCounter<GetMaxSockets();
00159 ++iCounter)
00160
00161 m_pThreadPool->SubmitJob(SocketProc,
00162 (LPVOID)this);
00163
00164
00165 return TRUE;
00166 }
00167 ERROR_HANDLER_RETURN("Scan",FALSE)
00168 }
00169
00170 void CTCPPortScannerMultithreaded::SocketProc(LPVOID lpData)
00171 {
00172 try
00173 {
00174
00175 CTCPPortScannerMultithreaded* pClass;
00176 pClass=(CTCPPortScannerMultithreaded*)lpData;
00177
00178
00179 BOOL bQuit;
00180 bQuit=FALSE;
00181
00182
00183 while (!bQuit)
00184 {
00185
00186 pClass->AdjustNumberOfSockets(1);
00187
00188
00189 if (!(bQuit=pClass->NewSocket()))
00190 ReportStaticError(CTCPPortScannerMultithreaded_Class,"SocketProc","Error creating the socket!");
00191 }
00192 }
00193 ERROR_HANDLER_STATIC(CTCPPortScannerMultithreaded_Class,"SocketProc")
00194 }
00195
00196 void CTCPPortScannerMultithreaded::SetMultithreadedPool(CThreadPool* pPool)
00197 {
00198 try
00199 {
00200
00201 if (m_pThreadPool &&
00202 m_bOwners)
00203 delete m_pThreadPool;
00204
00205
00206 m_pThreadPool=pPool;
00207
00208
00209 m_bOwners=FALSE;
00210 }
00211 ERROR_HANDLER("SetMultithreadedPool")
00212 }
00213
00214 void CTCPPortScannerMultithreaded::SetMaxThreads(int iThreads)
00215 {
00216 m_iMaxThreads=iThreads;
00217 }
00218
00219 int CTCPPortScannerMultithreaded::GetMaxThreads()const
00220 {
00221 return m_iMaxThreads;
00222 }
00223
00224 KOMODIA_NAMESPACE_END