Changeset 945c381 in nscp


Ignore:
Timestamp:
04/28/05 20:17:32 (8 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
e655f61
Parents:
d656933
Message:
  • Fixed multitasking socket listsner (had a hard-to-find memory leak)
  • Changed ProcessEnumeration? class to one that should work on NT4 New option to set this in the config file. + Added some more options to the config file.
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • NSClient++.cpp

    rcea178b r945c381  
    267267  MutexLock lock(pluginMutex); 
    268268  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; 
    270271  } 
    271272 
     
    278279          return c; 
    279280        case NSCAPI::returnIgnored: 
    280           LOG_DEBUG("A module ignored this message"); 
    281281          break; 
    282282        case NSCAPI::returnOK: 
  • include/Mutex.h

    r8223547 r945c381  
    134134   */ 
    135135  virtual ~MutexLock() { 
    136         ReleaseMutex(hMutex_); 
     136    if (bHasMutex) 
     137          ReleaseMutex(hMutex_); 
    137138    bHasMutex = false; 
    138139  } 
  • include/NSCHelper.cpp

    r1a5449e r945c381  
    9999 */ 
    100100void 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; 
    104105} 
    105106/** 
  • include/SSLSocket.cpp

    rd656933 r945c381  
    3030} 
    3131 
     32void 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} 
    3242 
    3343 
    34 void simpleSSL::SSL::readAll(simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/) { 
     44void simpleSSL::sSSL::readAll(simpleSocket::DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/) { 
    3545  // @todo this could be optimized a bit if we want to 
    3646  // If only one buffer is needed we could "reuse" the buffer instead of copying it. 
     
    3949    create(); 
    4050  int n= SSL_read(ssl_,tmpBuffer,tmpBufferLength); 
     51  if (n > 0) { 
     52    buffer.append(tmpBuffer, n); 
     53  } 
     54  /* 
    4155  while (n>0) { 
    4256    if (n == tmpBufferLength) { 
    4357      // We filled the buffer (There is more to get) 
    44       buffer.append(tmpBuffer, n); 
     58//      buffer.append(tmpBuffer, n); 
    4559      n=SSL_read(ssl_,tmpBuffer,tmpBufferLength); 
     60      break; 
    4661    } else { 
    4762      // Buffer not full, we got it "all" 
    48       buffer.append(tmpBuffer, n); 
     63//      buffer.append(tmpBuffer, n); 
    4964      break; 
    5065    } 
    5166  } 
     67  */ 
    5268  delete [] tmpBuffer; 
     69  /* 
    5370  if (n <= 0) { 
    5471    int rc = getError(n); 
     
    5673      throw SSLException("Socket read failed: ", n, rc); 
    5774  } 
     75  */ 
    5876} 
    59 void simpleSSL::SSL::send(const char * buf, unsigned int len) { 
    60   std::cout << "sending data..." << std::endl; 
     77void simpleSSL::sSSL::send(const char * buf, unsigned int len) { 
    6178  if (!ssl_) 
    6279    create(); 
     
    6683} 
    6784 
    68 bool simpleSSL::Listener::accept(tSocket *client) { 
    69   assert(client); 
    70   client->setContext(context); 
     85bool simpleSSL::Listener::accept(tSocket &client) { 
     86  client.setContext(context); 
    7187  return simpleSocket::Socket::accept(client); 
    7288} 
  • include/SSLSocket.h

    rd656933 r945c381  
    77 
    88namespace simpleSSL { 
     9  void count_socket(bool add); 
    910 
    1011  class SSLException { 
     
    7879    SSL_CTX *ctx_; 
    7980  public: 
    80     Context() : ctx_(NULL){} 
     81    Context() : ctx_(NULL){ 
     82    } 
    8183    // @todo Need to make this RAII! (smart pointers ?) 
    8284    Context(Context &other) { 
     
    114116  }; 
    115117 
    116   class SSL { 
     118  class sSSL { 
    117119  private: 
    118120    ::SSL *ssl_; 
     
    120122 
    121123  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(); 
    126133    } 
    127134    void free() { 
     
    152159      if (!ssl_) 
    153160        create(); 
     161      /**/ 
    154162      int rc = 0; 
    155163      int i = 0; 
     
    166174        } 
    167175      } 
     176      /**/ 
    168177    } 
    169178    void shutdown() { 
    170       if (!ssl_) 
     179      if (ssl_ == NULL) 
    171180        return; 
    172181      int i = 0; 
    173182      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      /* 
    174186      while ((rc = SSL_shutdown(ssl_)) != 1) { 
    175187        if (++i >= 100) { 
     
    184196        } 
    185197      } 
     198      */ 
    186199    } 
    187200    int set_fd(int fd) { 
     
    200213  private: 
    201214    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(); 
    211223      ssl.free(); 
    212       tBase::~Socket(); 
    213224    } 
    214225    void attach(SOCKET s) { 
     
    241252    } 
    242253    virtual void close() { 
     254      ssl.shutdown(); 
     255      ssl.free(); 
    243256      /* @todo 
    244257      try { 
     
    267280  public: 
    268281 
    269     virtual bool accept(tBase::tBase *client); 
     282    virtual bool accept(tBase::tBase &client); 
    270283 
    271284    void setContext(Context c) { 
     
    274287    virtual void StartListener(int port); 
    275288    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     */ 
    282289  }; 
    283290 
  • include/Socket.cpp

    rcea178b r945c381  
    22#include <Socket.h> 
    33#include <NSCHelper.h> 
     4 
     5 
    46 
    57 
  • include/Socket.h

    rd656933 r945c381  
    44#include <Mutex.h> 
    55#include <WinSock2.h> 
     6 
    67 
    78namespace simpleSocket { 
     
    5354      buffer_ = new char[length+1]; 
    5455      memcpy(buffer_, buffer, length); 
     56      length_ = length; 
    5557    } 
    5658  }; 
     
    6264 
    6365  public: 
    64     Socket() : socket_(NULL) {} 
    65     Socket(SOCKET socket) : socket_(socket) {} 
     66    Socket() : socket_(NULL) { 
     67    } 
     68    Socket(SOCKET socket) : socket_(socket) { 
     69    } 
    6670    Socket(Socket &other) { 
    6771      socket_ = other.socket_; 
     
    114118        throw SocketException("listen failed: ", ::WSAGetLastError()); 
    115119    } 
    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); 
    120123      if(s == INVALID_SOCKET) { 
    121124        int err = ::WSAGetLastError(); 
     
    124127        throw SocketException("accept failed: ", ::WSAGetLastError()); 
    125128      } 
    126       client->attach(s); 
     129      client.attach(s); 
    127130      return true; 
    128131    } 
     
    189192    struct simpleResponderBundle { 
    190193      HANDLE hThread; 
     194      unsigned dwThreadID; 
    191195    }; 
    192196    typedef std::list<simpleResponderBundle> socketResponses; 
     
    198202    listenThreadManager threadManager_; 
    199203    socketResponses responderList_; 
     204    MutexHandler responderMutex_; 
    200205 
    201206  public: 
     
    221226    Listener() : pHandler_(NULL) {}; 
    222227    virtual ~Listener() { 
     228      std::cout << "Stale process count: " << responderList_.size() << std::endl; 
    223229      // @todo check if we have stale processes here (if so log an error) 
    224230    }; 
     
    242248      pHandler_ = NULL; 
    243249    } 
    244     static void socketResponceProc(LPVOID lpParameter); 
     250    static unsigned __stdcall socketResponceProc(void* lpParameter); 
    245251    struct srp_data { 
    246252      Listener *pCore; 
    247       tSocket *pClient; 
     253      tSocket *client; 
    248254    }; 
    249255    void addResponder(tSocket *client) { 
     
    252258      srp_data *lpData = new srp_data; 
    253259      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)); 
    256268      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      } 
    261276      for (socketResponses::iterator it = responderList_.begin(); it != responderList_.end(); ++it) { 
    262         if ( (*it).hThread == h) { 
     277        if ( (*it).dwThreadID == dwThreadID) { 
    263278          responderList_.erase(it); 
    264279          return true; 
    265280        } 
    266281      } 
    267       // @todo protect 
    268282      return false; 
    269283    } 
     
    279293        pHandler_->onClose(); 
    280294    } 
    281     virtual bool accept(tSocket *client) { 
     295    virtual bool accept(tSocket &client) { 
    282296      return tBase::accept(client); 
    283297    } 
     
    290304 
    291305template <class TListenerType, class TSocketType> 
    292 void simpleSocket::Listener<TListenerType, TSocketType>::socketResponceProc(LPVOID lpParameter) 
     306unsigned simpleSocket::Listener<TListenerType, TSocketType>::socketResponceProc(void* lpParameter) 
    293307{ 
    294308  // @todo make sure this terminates after X seconds! 
     
    296310  srp_data *data = reinterpret_cast<srp_data*>(lpParameter); 
    297311  Listener *pCore = data->pCore; 
    298   tSocket *pClient = data->pClient; 
     312  tSocket *client = data->client; 
    299313  delete data; 
    300314  try { 
    301     pCore->onAccept(pClient); 
     315    pCore->onAccept(client); 
    302316  } catch (SocketException e) { 
    303317    pCore->printError(e.getMessage() + " killing socket..."); 
    304318  } 
    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; 
    308326} 
    309327 
     
    328346    while (!(WaitForSingleObject(hStopEvent_, 100) == WAIT_OBJECT_0)) { 
    329347      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        } 
    335352      } catch (SocketException e) { 
    336353        core->printError(e.getMessage() + ", attempting to resume..."); 
  • include/config.h

    rcea178b r945c381  
    1616#define SZDEPENDENCIES       "" 
    1717 
    18 // Buffer size of incoming data (noteice this is the maximum request length!) 
     18// Buffer size of incoming data (notice this is the maximum request length!) 
    1919#define RECV_BUFFER_LEN   1024 
    2020 
     
    2222 
    2323 
    24 // Default Argumentstring (for consistency) 
     24// Default Argument string (for consistency) 
    2525#define SHOW_ALL "ShowAll" 
    2626#define SHOW_FAIL "ShowFail" 
     
    6666#define C_SYSTEM_CPU "CounterCPU" 
    6767#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 
    6872 
    6973// Log to File Settings 
  • modules/CheckSystem/CheckSystem.cpp

    rd656933 r945c381  
    77#include <tlhelp32.h> 
    88#include <EnumNtSrv.h> 
     9#include <EnumProcess.h> 
    910 
    1011CheckSystem gNSClientCompat; 
     
    2728 * @return  
    2829 */ 
    29 CheckSystem::CheckSystem() {} 
     30CheckSystem::CheckSystem() : processMethod_(0) {} 
    3031/** 
    3132 * Default d-tor 
     
    4041bool CheckSystem::loadModule() { 
    4142  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  } 
    4262  return true; 
    4363} 
     
    109129    return checkCounter(command, argLen, char_args, msg, perf); 
    110130  } 
    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   */ 
    118131  return NSCAPI::returnIgnored; 
    119132} 
     
    390403* @return a hash_map with all running processes 
    391404*/ 
    392 NSPROCLST GetProcessList(void) 
    393 { 
    394   HANDLE hProcessSnap; 
    395   PROCESSENTRY32 pe32; 
     405NSPROCLST GetProcessList(int processMethod) 
     406{ 
    396407  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  } 
    421418  return ret; 
    422419} 
     
    452449  NSPROCLST runningProcs; 
    453450  try { 
    454     runningProcs = GetProcessList(); 
     451    runningProcs = GetProcessList(processMethod_); 
    455452  } catch (char *c) { 
    456453    NSC_LOG_ERROR_STD("ERROR: " + c); 
  • modules/CheckSystem/CheckSystem.h

    rd656933 r945c381  
    66class CheckSystem { 
    77private: 
     8  int processMethod_; 
    89  PDHCollectorThread pdhThread; 
    910 
  • modules/CheckSystem/CheckSystem.vcproj

    rcea178b r945c381  
    181181      </File> 
    182182      <File 
     183        RelativePath="..\..\include\EnumProcess.cpp"> 
     184      </File> 
     185      <File 
    183186        RelativePath="..\..\include\NSCHelper.cpp"> 
    184187      </File> 
  • modules/NRPEListener/NRPEListener.cpp

    rd656933 r945c381  
    223223void NRPEListener::onAccept(simpleSocket::Socket *client)  
    224224{ 
    225   assert(client); 
    226225  if (!allowedHosts.inAllowedHosts(client->getAddrString())) { 
    227226    NSC_LOG_ERROR("Unothorized access from: " + client->getAddrString()); 
     
    233232 
    234233    for (int i=0;i<100;i++) { 
    235       client->readAll(block); 
     234      client->readAll(block, 1048); 
    236235      if (block.getLength() >= NRPEPacket::getBufferLength()) 
    237236        break; 
     
    243242      return; 
    244243    } 
    245  
    246244    if (block.getLength() == NRPEPacket::getBufferLength()) { 
    247245      try { 
  • modules/NSClientListener/NSClientListener.cpp

    rd656933 r945c381  
    144144 
    145145void NSClientListener::onAccept(simpleSocket::Socket *client) { 
    146   assert(client); 
    147146  if (!allowedHosts.inAllowedHosts(client->getAddrString())) { 
    148147    NSC_LOG_ERROR("Unothorized access from: " + client->getAddrString()); 
Note: See TracChangeset for help on using the changeset viewer.