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 #if !defined(AFX_TCPPORTSCANNER_H__9827BC61_F74C_408D_8D12_A99E0FBC4A9F__INCLUDED_)
00037 #define AFX_TCPPORTSCANNER_H__9827BC61_F74C_408D_8D12_A99E0FBC4A9F__INCLUDED_
00038
00039 #if _MSC_VER > 1000
00040 #pragma once
00041 #endif // _MSC_VER > 1000
00042
00043 #pragma warning(disable:4786)
00044
00045 #include "TCPSocketAsync.h"
00046
00047 #include <deque>
00048 #include <map>
00049 #include <set>
00050
00051 KOMODIA_NAMESPACE_START
00052
00053 class CGenericCriticalSection;
00054
00055 class CTCPPortScanner : public CErrorHandler
00056 {
00057 public:
00058
00059 typedef std::set<unsigned short> PortsList;
00060 public:
00061
00062 void SetMaxSockets(int iMaxSockets);
00063 int GetMaxSockets()const;
00064
00065
00066 void SetConnectionTimeout(int iMS);
00067 int GetConnectionTimeout()const;
00068
00069
00070 const char* GetData(unsigned short usPort)const;
00071
00072
00073 char* GetDataForModify(unsigned short usPort)const;
00074
00075
00076 const PortsList& GetPortList()const;
00077
00078
00079 BOOL IsDone()const;
00080 BOOL IsError()const;
00081
00082
00083 void SetReceiveData(int iMaxDataSize,
00084 DWORD dwTimeout);
00085 DWORD GetReceiveDataTimeout()const;
00086 int GetReceiveDataSize()const;
00087
00088
00089 virtual BOOL Scan(const std::string& rDestinationAddress)=0;
00090 virtual BOOL Scan(IP aTarget)=0;
00091
00092
00093 virtual BOOL StopScan();
00094
00095
00096 void AddPorts(unsigned short usFromPort,
00097 unsigned short usToPort);
00098
00099
00100 CTCPPortScanner();
00101 virtual ~CTCPPortScanner();
00102 protected:
00103
00104
00105
00106
00107 virtual void OnPort(unsigned short usPort,
00108 BOOL bConnected);
00109
00110
00111
00112
00113
00114 virtual void OnPortData(unsigned short usPort,
00115 const char* pData,
00116 int iDataSize);
00117
00118
00119 virtual void TCPScanDone(BOOL bError);
00120
00121
00122 virtual void TCPScanDoneLib(BOOL bError);
00123
00124
00125 virtual void PortScanned(unsigned short usPort);
00126
00127
00128 void ScanDone(BOOL bError,
00129 BOOL bSpawnThread=TRUE);
00130
00131
00132 BOOL CanScan();
00133
00134
00135 int AdjustNumberOfSockets(int iIncrement);
00136
00137
00138 unsigned short GetNextPort();
00139
00140
00141 void SocketDone(unsigned short usPort,
00142 BOOL bConnected);
00143
00144
00145 void SocketDone(unsigned short usPort,
00146 char* pData,
00147 int iDataSize);
00148
00149
00150 void DeletePortsMap();
00151
00152
00153
00154 virtual CTCPSocketAsync* AllocateSocket(unsigned short usPort)const;
00155
00156
00157 virtual void DestroySocket(CTCPSocketAsync* pSocket)const;
00158
00159
00160 BOOL NewSocket();
00161
00162
00163 void SetError(BOOL bError);
00164
00165
00166 void SetDone(BOOL bDone);
00167
00168
00169 void ResetSocketCount();
00170
00171
00172 void ResetPair();
00173
00174
00175 void ResetData();
00176
00177
00178 void SetTarget(IP aTarget);
00179 IP GetTarget()const;
00180
00181
00182 BOOL IsFinished()const;
00183 private:
00184
00185 class CTCPScanner : public CTCPSocketAsync
00186 {
00187 public:
00188
00189 CTCPScanner(CTCPPortScanner* pFather,
00190 unsigned short usPort,
00191 int iDataSize,
00192 DWORD dwTimeout);
00193 virtual ~CTCPScanner();
00194 protected:
00195
00196 NO_OnSocketAccept
00197 NO_OnSocketOOB
00198 NO_OnSocketWrite
00199
00200 virtual BOOL OnSocketTimeout();
00201 virtual BOOL OnSocketConnect(int iErrorCode);
00202 virtual BOOL OnSocketClose(int iErrorCode);
00203 virtual BOOL OnSocketReceive(int iErrorCode);
00204 private:
00205
00206 CTCPPortScanner* m_pFather;
00207
00208
00209 int m_iDataSize;
00210
00211
00212 DWORD m_dwTimeout;
00213
00214
00215 unsigned short m_usPort;
00216 };
00217
00218 friend CTCPScanner;
00219 private:
00220
00221 typedef struct _ScanPair
00222 {
00223 unsigned short usFromPort;
00224 unsigned short usToPort;
00225 } ScanPair;
00226
00227
00228 typedef std::deque<ScanPair> PortsVector;
00229
00230
00231 typedef std::map<unsigned short,char*> DataMap;
00232
00233
00234 typedef struct _ThreadData
00235 {
00236 CTCPPortScanner* pScanner;
00237 BOOL bError;
00238 } ThreadData;
00239 private:
00240
00241 CTCPPortScanner(const CTCPPortScanner& rScanner);
00242
00243
00244 CTCPPortScanner& operator=(const CTCPPortScanner& rScanner);
00245
00246
00247 static DWORD DoneThread(LPVOID lpData);
00248
00249
00250 BOOL m_bDone;
00251
00252
00253 CGenericCriticalSection* m_pCSection;
00254
00255
00256 CGenericCriticalSection* m_pCSectionDone;
00257
00258
00259 PortsList m_aPortList;
00260
00261
00262 PortsList m_aPortAnswers;
00263
00264
00265 PortsVector m_aPortsToScan;
00266
00267
00268 int m_iReceiveData;
00269
00270
00271 DWORD m_dwTimeout;
00272
00273
00274 int m_iMaxSockets;
00275
00276
00277 ScanPair m_aCurrentPair;
00278
00279
00280 BOOL m_bError;
00281
00282
00283 DataMap m_aPortsData;
00284
00285
00286 int m_iActiveSockets;
00287
00288
00289 IP m_aTarget;
00290
00291
00292 int m_iConnectionTimeout;
00293
00294
00295 BOOL m_bFinished;
00296 };
00297
00298 KOMODIA_NAMESPACE_END
00299
00300 #endif // !defined(AFX_TCPPORTSCANNER_H__9827BC61_F74C_408D_8D12_A99E0FBC4A9F__INCLUDED_)