Changeset 945c381 in nscp
- Timestamp:
- 04/28/05 20:17:32 (8 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- e655f61
- Parents:
- d656933
- Files:
-
- 2 added
- 13 edited
-
NSClient++.cpp (modified) (2 diffs)
-
include/EnumProcess.cpp (added)
-
include/EnumProcess.h (added)
-
include/Mutex.h (modified) (1 diff)
-
include/NSCHelper.cpp (modified) (1 diff)
-
include/SSLSocket.cpp (modified) (4 diffs)
-
include/SSLSocket.h (modified) (11 diffs)
-
include/Socket.cpp (modified) (1 diff)
-
include/Socket.h (modified) (14 diffs)
-
include/config.h (modified) (3 diffs)
-
modules/CheckSystem/CheckSystem.cpp (modified) (6 diffs)
-
modules/CheckSystem/CheckSystem.h (modified) (1 diff)
-
modules/CheckSystem/CheckSystem.vcproj (modified) (1 diff)
-
modules/NRPEListener/NRPEListener.cpp (modified) (3 diffs)
-
modules/NSClientListener/NSClientListener.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
NSClient++.cpp
rcea178b r945c381 267 267 MutexLock lock(pluginMutex); 268 268 if (!lock.hasMutex()) { 269 LOG_ERROR("Failed to get mutex, command ignored..."); 269 LOG_ERROR_STD("Failed to get mutex (" + strEx::itos(lock.getWaitResult()) + "), command ignored..."); 270 return NSCAPI::returnUNKNOWN; 270 271 } 271 272 … … 278 279 return c; 279 280 case NSCAPI::returnIgnored: 280 LOG_DEBUG("A module ignored this message");281 281 break; 282 282 case NSCAPI::returnOK: -
include/Mutex.h
r8223547 r945c381 134 134 */ 135 135 virtual ~MutexLock() { 136 ReleaseMutex(hMutex_); 136 if (bHasMutex) 137 ReleaseMutex(hMutex_); 137 138 bHasMutex = false; 138 139 } -
include/NSCHelper.cpp
r1a5449e r945c381 99 99 */ 100 100 void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) { 101 if (!fNSAPIMessage) 102 throw NSCMHExcpetion("NSCore has not been initiated..."); 103 return fNSAPIMessage(msgType, file.c_str(), line, message.c_str()); 101 if (fNSAPIMessage) 102 return fNSAPIMessage(msgType, file.c_str(), line, message.c_str()); 103 else 104 std::cout << "NSCore not loaded..." << std::endl << message << std::endl; 104 105 } 105 106 /** -
include/SSLSocket.cpp
rd656933 r945c381 30 30 } 31 31 32 void simpleSSL::count_socket(bool add) { 33 static int count = 0; 34 if (add) { 35 count++; 36 std::cout << "+++SSSL::Socket" << count << std::endl; 37 } else { 38 count--; 39 std::cout << "---SSSL::Socket" << count << std::endl; 40 } 41 } 32 42 33 43 34 void simpleSSL:: SSL::readAll(simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/) {44 void simpleSSL::sSSL::readAll(simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/) { 35 45 // @todo this could be optimized a bit if we want to 36 46 // If only one buffer is needed we could "reuse" the buffer instead of copying it. … … 39 49 create(); 40 50 int n= SSL_read(ssl_,tmpBuffer,tmpBufferLength); 51 if (n > 0) { 52 buffer.append(tmpBuffer, n); 53 } 54 /* 41 55 while (n>0) { 42 56 if (n == tmpBufferLength) { 43 57 // We filled the buffer (There is more to get) 44 buffer.append(tmpBuffer, n);58 // buffer.append(tmpBuffer, n); 45 59 n=SSL_read(ssl_,tmpBuffer,tmpBufferLength); 60 break; 46 61 } else { 47 62 // Buffer not full, we got it "all" 48 buffer.append(tmpBuffer, n);63 // buffer.append(tmpBuffer, n); 49 64 break; 50 65 } 51 66 } 67 */ 52 68 delete [] tmpBuffer; 69 /* 53 70 if (n <= 0) { 54 71 int rc = getError(n); … … 56 73 throw SSLException("Socket read failed: ", n, rc); 57 74 } 75 */ 58 76 } 59 void simpleSSL::SSL::send(const char * buf, unsigned int len) { 60 std::cout << "sending data..." << std::endl; 77 void simpleSSL::sSSL::send(const char * buf, unsigned int len) { 61 78 if (!ssl_) 62 79 create(); … … 66 83 } 67 84 68 bool simpleSSL::Listener::accept(tSocket *client) { 69 assert(client); 70 client->setContext(context); 85 bool simpleSSL::Listener::accept(tSocket &client) { 86 client.setContext(context); 71 87 return simpleSocket::Socket::accept(client); 72 88 } -
include/SSLSocket.h
rd656933 r945c381 7 7 8 8 namespace simpleSSL { 9 void count_socket(bool add); 9 10 10 11 class SSLException { … … 78 79 SSL_CTX *ctx_; 79 80 public: 80 Context() : ctx_(NULL){} 81 Context() : ctx_(NULL){ 82 } 81 83 // @todo Need to make this RAII! (smart pointers ?) 82 84 Context(Context &other) { … … 114 116 }; 115 117 116 class SSL {118 class sSSL { 117 119 private: 118 120 ::SSL *ssl_; … … 120 122 121 123 public: 122 SSL() : ssl_(NULL) {} 123 ~SSL() { 124 if (ssl_) 125 free(); 124 sSSL() : ssl_(NULL) { 125 } 126 sSSL(sSSL &other) : ssl_(NULL) { 127 ssl_ = other.ssl_; 128 other.ssl_ = NULL; 129 context_ = other.context_; 130 } 131 ~sSSL() { 132 free(); 126 133 } 127 134 void free() { … … 152 159 if (!ssl_) 153 160 create(); 161 /**/ 154 162 int rc = 0; 155 163 int i = 0; … … 166 174 } 167 175 } 176 /**/ 168 177 } 169 178 void shutdown() { 170 if ( !ssl_)179 if (ssl_ == NULL) 171 180 return; 172 181 int i = 0; 173 182 int rc = 0; 183 // @bug This will break but I don't know how to fix it... 184 // SSL_shutdown(ssl_); @bug this leaks memory! 185 /* 174 186 while ((rc = SSL_shutdown(ssl_)) != 1) { 175 187 if (++i >= 100) { … … 184 196 } 185 197 } 198 */ 186 199 } 187 200 int set_fd(int fd) { … … 200 213 private: 201 214 typedef simpleSocket::Socket tBase; 202 simpleSSL::SSL ssl; 203 public: 204 Socket() {} 205 ~Socket() { 206 try { 207 ssl.shutdown(); 208 } catch (SSLException e) { 209 NSC_LOG_ERROR_STD(e.getMessage()); 210 } 215 simpleSSL::sSSL ssl; 216 public: 217 Socket() { 218 } 219 Socket(Socket &other) : tBase(other), ssl(other.ssl) { 220 } 221 virtual ~Socket() { 222 ssl.shutdown(); 211 223 ssl.free(); 212 tBase::~Socket();213 224 } 214 225 void attach(SOCKET s) { … … 241 252 } 242 253 virtual void close() { 254 ssl.shutdown(); 255 ssl.free(); 243 256 /* @todo 244 257 try { … … 267 280 public: 268 281 269 virtual bool accept(tBase::tBase *client);282 virtual bool accept(tBase::tBase &client); 270 283 271 284 void setContext(Context c) { … … 274 287 virtual void StartListener(int port); 275 288 virtual void StopListener(); 276 /*277 virtual void onAccept(simpleSocket::Socket& client) = 0;278 virtual void onAccept(tBase::tBase &client) {279 onAccept(static_cast<simpleSocket::Socket&>(client));280 }281 */282 289 }; 283 290 -
include/Socket.cpp
rcea178b r945c381 2 2 #include <Socket.h> 3 3 #include <NSCHelper.h> 4 5 4 6 5 7 -
include/Socket.h
rd656933 r945c381 4 4 #include <Mutex.h> 5 5 #include <WinSock2.h> 6 6 7 7 8 namespace simpleSocket { … … 53 54 buffer_ = new char[length+1]; 54 55 memcpy(buffer_, buffer, length); 56 length_ = length; 55 57 } 56 58 }; … … 62 64 63 65 public: 64 Socket() : socket_(NULL) {} 65 Socket(SOCKET socket) : socket_(socket) {} 66 Socket() : socket_(NULL) { 67 } 68 Socket(SOCKET socket) : socket_(socket) { 69 } 66 70 Socket(Socket &other) { 67 71 socket_ = other.socket_; … … 114 118 throw SocketException("listen failed: ", ::WSAGetLastError()); 115 119 } 116 virtual bool accept(Socket *client) { 117 assert(client); 118 int fromlen=sizeof(client->from_); 119 SOCKET s = ::accept(socket_, (sockaddr*)&client->from_, &fromlen); 120 virtual bool accept(Socket &client) { 121 int fromlen=sizeof(client.from_); 122 SOCKET s = ::accept(socket_, (sockaddr*)&client.from_, &fromlen); 120 123 if(s == INVALID_SOCKET) { 121 124 int err = ::WSAGetLastError(); … … 124 127 throw SocketException("accept failed: ", ::WSAGetLastError()); 125 128 } 126 client ->attach(s);129 client.attach(s); 127 130 return true; 128 131 } … … 189 192 struct simpleResponderBundle { 190 193 HANDLE hThread; 194 unsigned dwThreadID; 191 195 }; 192 196 typedef std::list<simpleResponderBundle> socketResponses; … … 198 202 listenThreadManager threadManager_; 199 203 socketResponses responderList_; 204 MutexHandler responderMutex_; 200 205 201 206 public: … … 221 226 Listener() : pHandler_(NULL) {}; 222 227 virtual ~Listener() { 228 std::cout << "Stale process count: " << responderList_.size() << std::endl; 223 229 // @todo check if we have stale processes here (if so log an error) 224 230 }; … … 242 248 pHandler_ = NULL; 243 249 } 244 static void socketResponceProc(LPVOIDlpParameter);250 static unsigned __stdcall socketResponceProc(void* lpParameter); 245 251 struct srp_data { 246 252 Listener *pCore; 247 tSocket * pClient;253 tSocket *client; 248 254 }; 249 255 void addResponder(tSocket *client) { … … 252 258 srp_data *lpData = new srp_data; 253 259 lpData->pCore = this; 254 lpData->pClient = client; 255 data.hThread = reinterpret_cast<HANDLE>(::_beginthread(socketResponceProc, 0, lpData)); 260 lpData->client = client; 261 262 MutexLock lock(responderMutex_); 263 if (!lock.hasMutex()) { 264 printError("Failed to get responder mutex."); 265 return; 266 } 267 data.hThread = reinterpret_cast<HANDLE>(::_beginthreadex( NULL, 0, &socketResponceProc, lpData, 0, &data.dwThreadID)); 256 268 responderList_.push_back(data); 257 // @todo protect 258 } 259 bool removeResponder(HANDLE h) { 260 // @todo protect 269 } 270 bool removeResponder(DWORD dwThreadID) { 271 MutexLock lock(responderMutex_); 272 if (!lock.hasMutex()) { 273 printError("Failed to get responder mutex when trying to free thread."); 274 return false; 275 } 261 276 for (socketResponses::iterator it = responderList_.begin(); it != responderList_.end(); ++it) { 262 if ( (*it). hThread == h) {277 if ( (*it).dwThreadID == dwThreadID) { 263 278 responderList_.erase(it); 264 279 return true; 265 280 } 266 281 } 267 // @todo protect268 282 return false; 269 283 } … … 279 293 pHandler_->onClose(); 280 294 } 281 virtual bool accept(tSocket *client) {295 virtual bool accept(tSocket &client) { 282 296 return tBase::accept(client); 283 297 } … … 290 304 291 305 template <class TListenerType, class TSocketType> 292 void simpleSocket::Listener<TListenerType, TSocketType>::socketResponceProc(LPVOIDlpParameter)306 unsigned simpleSocket::Listener<TListenerType, TSocketType>::socketResponceProc(void* lpParameter) 293 307 { 294 308 // @todo make sure this terminates after X seconds! … … 296 310 srp_data *data = reinterpret_cast<srp_data*>(lpParameter); 297 311 Listener *pCore = data->pCore; 298 tSocket * pClient = data->pClient;312 tSocket *client = data->client; 299 313 delete data; 300 314 try { 301 pCore->onAccept( pClient);315 pCore->onAccept(client); 302 316 } catch (SocketException e) { 303 317 pCore->printError(e.getMessage() + " killing socket..."); 304 318 } 305 delete pClient; 306 pCore->removeResponder(GetCurrentThread()); 307 _endthread(); 319 client->close(); 320 delete client; 321 if (!pCore->removeResponder(GetCurrentThreadId())) { 322 pCore->printError("Could not remove thread: " + strEx::itos(GetCurrentThreadId())); 323 } 324 _endthreadex(0); 325 return 0; 308 326 } 309 327 … … 328 346 while (!(WaitForSingleObject(hStopEvent_, 100) == WAIT_OBJECT_0)) { 329 347 try { 330 tSocket *client = new tSocket; 331 if (core->accept(client)) 332 core->addResponder(client); 333 else 334 delete client; 348 tSocket client; 349 if (core->accept(client)) { 350 core->addResponder(new tSocket(client)); 351 } 335 352 } catch (SocketException e) { 336 353 core->printError(e.getMessage() + ", attempting to resume..."); -
include/config.h
rcea178b r945c381 16 16 #define SZDEPENDENCIES "" 17 17 18 // Buffer size of incoming data (not eice this is the maximum request length!)18 // Buffer size of incoming data (notice this is the maximum request length!) 19 19 #define RECV_BUFFER_LEN 1024 20 20 … … 22 22 23 23 24 // Default Argument string (for consistency)24 // Default Argument string (for consistency) 25 25 #define SHOW_ALL "ShowAll" 26 26 #define SHOW_FAIL "ShowFail" … … 66 66 #define C_SYSTEM_CPU "CounterCPU" 67 67 #define C_SYSTEM_MEM_CPU_DEFAULT "\\\\.\\Processor(_total)\\% Processor Time" 68 #define C_SYSTEM_ENUMPROC_METHOD_PSAPI "PSAPI" 69 #define C_SYSTEM_ENUMPROC_METHOD_THELP "TOOLHELP" 70 #define C_SYSTEM_ENUMPROC_METHOD "ProcessEnumerationMethod" 71 #define C_SYSTEM_ENUMPROC_METHOD_DEFAULT C_SYSTEM_ENUMPROC_METHOD_THELP 68 72 69 73 // Log to File Settings -
modules/CheckSystem/CheckSystem.cpp
rd656933 r945c381 7 7 #include <tlhelp32.h> 8 8 #include <EnumNtSrv.h> 9 #include <EnumProcess.h> 9 10 10 11 CheckSystem gNSClientCompat; … … 27 28 * @return 28 29 */ 29 CheckSystem::CheckSystem() {}30 CheckSystem::CheckSystem() : processMethod_(0) {} 30 31 /** 31 32 * Default d-tor … … 40 41 bool CheckSystem::loadModule() { 41 42 pdhThread.createThread(); 43 44 std::string wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT); 45 46 CEnumProcess tmp; 47 int method = tmp.GetAvailableMethods(); 48 49 if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 50 if (method == (method|ENUM_METHOD::PSAPI)) { 51 processMethod_ = ENUM_METHOD::PSAPI; 52 } else { 53 NSC_LOG_ERROR_STD("PSAPI method not avalible, check " C_SYSTEM_ENUMPROC_METHOD " option."); 54 } 55 } else { 56 if (method == (method|ENUM_METHOD::TOOLHELP)) { 57 processMethod_ = ENUM_METHOD::TOOLHELP; 58 } else { 59 NSC_LOG_ERROR_STD("TOOLHELP method not avalible, check " C_SYSTEM_ENUMPROC_METHOD " option."); 60 } 61 } 42 62 return true; 43 63 } … … 109 129 return checkCounter(command, argLen, char_args, msg, perf); 110 130 } 111 /*112 case REQ_PROCSTATE:113 rb = NSCommands::procState(arrayBuffer::arrayBuffer2list(argLen, char_args));114 msg = rb.msg_;115 perf = rb.perf_;116 return rb.code_;117 */118 131 return NSCAPI::returnIgnored; 119 132 } … … 390 403 * @return a hash_map with all running processes 391 404 */ 392 NSPROCLST GetProcessList(void) 393 { 394 HANDLE hProcessSnap; 395 PROCESSENTRY32 pe32; 405 NSPROCLST GetProcessList(int processMethod) 406 { 396 407 NSPROCLST ret; 397 398 // Take a snapshot of all processes in the system. 399 hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); 400 if( hProcessSnap == INVALID_HANDLE_VALUE ) 401 throw "CreateToolhelp32Snapshot (of processes) failed"; 402 403 // Set the size of the structure before using it. 404 pe32.dwSize = sizeof( PROCESSENTRY32 ); 405 406 // Retrieve information about the first process, 407 // and exit if unsuccessful 408 if( !Process32First( hProcessSnap, &pe32 ) ) { 409 CloseHandle( hProcessSnap ); // Must clean up the snapshot object! 410 throw "Process32First failed!"; 411 } 412 413 // Now walk the snapshot of processes, and 414 // display information about each process in turn 415 do { 416 ret[pe32.szExeFile] = pe32.th32ProcessID; 417 } while( Process32Next( hProcessSnap, &pe32 ) ); 418 419 // Don't forget to clean up the snapshot object! 420 CloseHandle( hProcessSnap ); 408 if (processMethod == 0) { 409 NSC_LOG_ERROR_STD("ProcessMethod not defined or not available."); 410 return ret; 411 } 412 CEnumProcess enumeration; 413 enumeration.SetMethod(processMethod); 414 CEnumProcess::CProcessEntry entry; 415 for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) { 416 ret[entry.lpFilename] = entry.dwPID; 417 } 421 418 return ret; 422 419 } … … 452 449 NSPROCLST runningProcs; 453 450 try { 454 runningProcs = GetProcessList( );451 runningProcs = GetProcessList(processMethod_); 455 452 } catch (char *c) { 456 453 NSC_LOG_ERROR_STD("ERROR: " + c); -
modules/CheckSystem/CheckSystem.h
rd656933 r945c381 6 6 class CheckSystem { 7 7 private: 8 int processMethod_; 8 9 PDHCollectorThread pdhThread; 9 10 -
modules/CheckSystem/CheckSystem.vcproj
rcea178b r945c381 181 181 </File> 182 182 <File 183 RelativePath="..\..\include\EnumProcess.cpp"> 184 </File> 185 <File 183 186 RelativePath="..\..\include\NSCHelper.cpp"> 184 187 </File> -
modules/NRPEListener/NRPEListener.cpp
rd656933 r945c381 223 223 void NRPEListener::onAccept(simpleSocket::Socket *client) 224 224 { 225 assert(client);226 225 if (!allowedHosts.inAllowedHosts(client->getAddrString())) { 227 226 NSC_LOG_ERROR("Unothorized access from: " + client->getAddrString()); … … 233 232 234 233 for (int i=0;i<100;i++) { 235 client->readAll(block );234 client->readAll(block, 1048); 236 235 if (block.getLength() >= NRPEPacket::getBufferLength()) 237 236 break; … … 243 242 return; 244 243 } 245 246 244 if (block.getLength() == NRPEPacket::getBufferLength()) { 247 245 try { -
modules/NSClientListener/NSClientListener.cpp
rd656933 r945c381 144 144 145 145 void NSClientListener::onAccept(simpleSocket::Socket *client) { 146 assert(client);147 146 if (!allowedHosts.inAllowedHosts(client->getAddrString())) { 148 147 NSC_LOG_ERROR("Unothorized access from: " + client->getAddrString());
Note: See TracChangeset
for help on using the changeset viewer.








