Changeset 1a5449e in nscp


Ignore:
Timestamp:
03/26/05 23:29:52 (8 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
452fd41
Parents:
c4f6204
Message:

+ NRPE Support (very basic, no encryption, and nothing fancy)

  • Socket classes rewritten + Added NSCModuleHelper::getSettingsSection to the API
Files:
26 edited

Legend:

Unmodified
Added
Removed
  • NSClient++.cpp

    rc4f6204 r1a5449e  
    7070#ifdef _DEBUG 
    7171      strEx::run_test_getToken(); 
     72      strEx::run_test_replace(); 
    7273      charEx::run_test_getToken(); 
    7374      arrayBuffer::run_testArrayBuffer(); 
    7475#endif 
     76 
    7577      g_bConsoleLog = true; 
    7678      mainClient.InitiateService(); 
     
    406408  return mainClient.injectRAW(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); 
    407409} 
     410NSCAPI::errorReturn NSAPIGetSettingsSection(const char* section, char*** aBuffer, unsigned int * bufLen) { 
     411  unsigned int len = 0; 
     412  *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len); 
     413  *bufLen = len; 
     414  return NSCAPI::isSuccess; 
     415} 
     416 
    408417 
    409418LPVOID NSAPILoader(char*buffer) { 
     
    412421  if (stricmp(buffer, "NSAPIGetApplicationVersionStr") == 0) 
    413422    return &NSAPIGetApplicationVersionStr; 
     423  if (stricmp(buffer, "NSAPIGetSettingsSection") == 0) 
     424    return &NSAPIGetSettingsSection; 
    414425  if (stricmp(buffer, "NSAPIGetSettingsString") == 0) 
    415426    return &NSAPIGetSettingsString; 
  • NSClient++.h

    r1eef1ee r1a5449e  
    9191void NSAPIStopServer(void); 
    9292NSCAPI::nagiosReturn NSAPIInject(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen); 
    93  
     93NSCAPI::errorReturn NSAPIGetSettingsSection(const char*, char***, unsigned int *); 
    9494////////////////////////////////////////////////////////////////////////// 
    9595// Log macros to simplify logging 
  • NSClient++.vcproj

    r2a94f3f r1a5449e  
    151151      Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"> 
    152152      <File 
     153        RelativePath=".\include\arrayBuffer.cpp"> 
     154      </File> 
     155      <File 
    153156        RelativePath=".\include\NSCHelper.cpp"> 
    154157      </File> 
     
    190193      Filter="h;hpp;hxx;hm;inl"> 
    191194      <File 
     195        RelativePath=".\include\arrayBuffer.h"> 
     196      </File> 
     197      <File 
    192198        RelativePath=".\include\charEx.h"> 
    193199      </File> 
  • changelog

    rc4f6204 r1a5449e  
     12005-03-26 MickeM 
     2 + NRPE Support (very basic, no encryption, and nothing fancy) 
     3 * Socket classes rewritten 
     4 + Added NSCModuleHelper::getSettingsSection to the API 
     5 
    162005-03-22 MickeM 
    27 * Fixed NSClientListener return codes for basic commands 
     
    813 * Fixed BUG in inject command (now things should "work" again, but far from stable) 
    914 + Added NRPE return code 
    10  * Fixed some return states to NSClientListener (not verified yet, but soon they wil lbe back in order :) 
     15 * Fixed some return states to NSClientListener (not verified yet, but soon they will be back in order :) 
    1116 
    12172005-03-20 MickeM 
  • include/NSCHelper.cpp

    rc4f6204 r1a5449e  
    7777  lpNSAPIGetApplicationName fNSAPIGetApplicationName = NULL; 
    7878  lpNSAPIGetApplicationVersionStr fNSAPIGetApplicationVersionStr = NULL; 
     79  lpNSAPIGetSettingsSection fNSAPIGetSettingsSection = NULL; 
    7980  lpNSAPIGetSettingsString fNSAPIGetSettingsString = NULL; 
    8081  lpNSAPIGetSettingsInt fNSAPIGetSettingsInt = NULL; 
     
    212213  return ret; 
    213214} 
     215std::list<std::string> NSCModuleHelper::getSettingsSection(std::string section) { 
     216  if (!fNSAPIGetSettingsSection) 
     217    throw NSCMHExcpetion("NSCore has not been initiated..."); 
     218  char ** aBuffer = NULL; 
     219  unsigned int argLen = 0; 
     220  if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) { 
     221    throw NSCMHExcpetion("Settings could not be retrieved."); 
     222  } 
     223  std::list<std::string> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer); 
     224  arrayBuffer::destroyArrayBuffer(aBuffer, argLen); 
     225  return ret; 
     226} 
    214227/** 
    215228 * Retrieve an int from the settings subsystem (INI-file) 
     
    227240  return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); 
    228241} 
     242 
     243 
     244/*************************************************************************** 
     245* max_state(STATE_x, STATE_y) 
     246* compares STATE_x to  STATE_y and returns result based on the following 
     247* STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL 
     248* 
     249* Note that numerically the above does not hold 
     250****************************************************************************/ 
     251NSCAPI::nagiosReturn NSCHelper::maxState(NSCAPI::nagiosReturn a, NSCAPI::nagiosReturn b) 
     252{ 
     253  if (a == NSCAPI::returnCRIT || b == NSCAPI::returnCRIT) 
     254    return NSCAPI::returnCRIT; 
     255  else if (a == NSCAPI::returnWARN || b == NSCAPI::returnWARN) 
     256    return NSCAPI::returnWARN; 
     257  else if (a == NSCAPI::returnOK || b == NSCAPI::returnOK) 
     258    return NSCAPI::returnOK; 
     259  else if (a == NSCAPI::returnUNKNOWN || b == NSCAPI::returnUNKNOWN) 
     260    return NSCAPI::returnUNKNOWN; 
     261  /* 
     262  else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) 
     263  return STATE_DEPENDENT; 
     264  */ 
     265  //    else 
     266  throw "undefined state"; 
     267  //      return max (a, b); 
     268} 
     269 
     270 
    229271/** 
    230272 * Retrieve the application name (in human readable format) from the core. 
     
    321363  NSCModuleHelper::fNSAPIGetSettingsInt = (NSCModuleHelper::lpNSAPIGetSettingsInt)f("NSAPIGetSettingsInt"); 
    322364  NSCModuleHelper::fNSAPIGetSettingsString = (NSCModuleHelper::lpNSAPIGetSettingsString)f("NSAPIGetSettingsString"); 
     365  NSCModuleHelper::fNSAPIGetSettingsSection = (NSCModuleHelper::lpNSAPIGetSettingsSection)f("NSAPIGetSettingsSection"); 
    323366  NSCModuleHelper::fNSAPIMessage = (NSCModuleHelper::lpNSAPIMessage)f("NSAPIMessage"); 
    324367  NSCModuleHelper::fNSAPIStopServer = (NSCModuleHelper::lpNSAPIStopServer)f("NSAPIStopServer"); 
  • include/NSCHelper.h

    rc4f6204 r1a5449e  
    1818  std::string translateMessageType(NSCAPI::messageTypes msgType); 
    1919  std::string translateReturn(NSCAPI::nagiosReturn returnCode); 
    20  
    21   /* 
    22   / * ************************************************************************** 
    23   * max_state(STATE_x, STATE_y) 
    24   * compares STATE_x to  STATE_y and returns result based on the following 
    25   * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL 
    26   * 
    27   * Note that numerically the above does not hold 
    28   **************************************************************************** / 
    29  
    30   int 
    31     max_state (int a, int b) 
    32   { 
    33     if (a == STATE_CRITICAL || b == STATE_CRITICAL) 
    34       return STATE_CRITICAL; 
    35     else if (a == STATE_WARNING || b == STATE_WARNING) 
    36       return STATE_WARNING; 
    37     else if (a == STATE_OK || b == STATE_OK) 
    38       return STATE_OK; 
    39     else if (a == STATE_UNKNOWN || b == STATE_UNKNOWN) 
    40       return STATE_UNKNOWN; 
    41     else if (a == STATE_DEPENDENT || b == STATE_DEPENDENT) 
    42       return STATE_DEPENDENT; 
    43     else 
    44       return max (a, b); 
    45   } 
    46   @bug Use this scheme instead!! 
    47 */ 
    48  
    49  
     20  NSCAPI::nagiosReturn maxState(NSCAPI::nagiosReturn a, NSCAPI::nagiosReturn b); 
     21 
     22#ifdef DEBUG 
     23  inline NSCAPI::nagiosReturn int2nagios(int code) { 
     24    if (code == 0) 
     25      return NSCAPI::returnOK; 
     26    if (code == 1) 
     27      return NSCAPI::returnWARN; 
     28    if (code == 2) 
     29      return NSCAPI::returnCRIT; 
     30    if (code == 4) 
     31      return NSCAPI::returnUNKNOWN; 
     32    throw "@fixme bad code"; 
     33  } 
     34  inline int nagios2int(NSCAPI::nagiosReturn code) { 
     35    if (code == NSCAPI::returnOK) 
     36      return 0; 
     37    if (code == NSCAPI::returnWARN) 
     38      return 1; 
     39    if (code == NSCAPI::returnCRIT) 
     40      return 2; 
     41    if (code == NSCAPI::returnUNKNOWN) 
     42      return 4; 
     43    throw "@fixme bad code"; 
     44  } 
     45#else 
     46  inline NSCAPI::nagiosReturn int2nagios(int code) { 
     47    return code; 
     48  } 
     49  inline int nagios2int(NSCAPI::nagiosReturn code) { 
     50    return code; 
     51  } 
     52#endif 
     53 
     54/* 
    5055  inline void escalteReturnCode(NSCAPI::nagiosReturn &currentReturnCode, NSCAPI::nagiosReturn newReturnCode) { 
    5156    if (newReturnCode == NSCAPI::returnCRIT) 
     
    5863      currentReturnCode = NSCAPI::returnUNKNOWN; 
    5964  } 
     65  */ 
    6066  inline void escalteReturnCodeToCRIT(NSCAPI::nagiosReturn &currentReturnCode) { 
    6167    currentReturnCode = NSCAPI::returnCRIT; 
     
    6571      currentReturnCode = NSCAPI::returnWARN; 
    6672  } 
    67  
    6873}; 
    6974 
     
    8186  typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsString)(const char*,const char*,const char*,char*,unsigned int); 
    8287  typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsInt)(const char*, const char*, int); 
     88  typedef NSCAPI::errorReturn (*lpNSAPIGetSettingsSection)(const char*, char***, unsigned int *); 
    8389  typedef void (*lpNSAPIMessage)(int, const char*, const int, const char*); 
    8490  typedef NSCAPI::errorReturn (*lpNSAPIStopServer)(void); 
     
    8995  std::string getApplicationName(void); 
    9096  std::string getApplicationVersionString(void); 
     97  std::list<std::string> getSettingsSection(std::string section); 
    9198  std::string getSettingsString(std::string section, std::string key, std::string defaultValue); 
    9299  int getSettingsInt(std::string section, std::string key, int defaultValue); 
  • include/Socket.cpp

    rc6e008c r1a5449e  
    33 
    44 
    5 SimpleSocketListsner::~SimpleSocketListsner() { 
     5simpleSocket::Listener::~Listener() { 
    66  // @todo: Force cleanup here 
    77} 
     
    1414* @todo This needs to be reworked, possibly completely redone ? 
    1515*/ 
    16 DWORD SimpleSocketListsner::threadProc(LPVOID lpParameter) 
     16DWORD simpleSocket::Listener::ListenerThread::threadProc(LPVOID lpParameter) 
    1717{ 
     18  unsigned long NoBlock = 1; 
     19  Listener *core = reinterpret_cast<Listener*>(lpParameter); 
     20 
    1821  hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); 
    1922  if (!hStopEvent) { 
     
    2225  } 
    2326 
    24   WSADATA wsaData; 
    25   sockaddr_in local; 
    26   int wsaret=WSAStartup(0x101,&wsaData); 
    27   if(wsaret!=0) { 
    28     NSC_LOG_ERROR_STD("WSA Startup failed: " + strEx::itos(wsaret)); 
    29     return 0; 
     27  try { 
     28    core->socket(AF_INET,SOCK_STREAM,0); 
     29    core->setAddr(AF_INET, INADDR_ANY, htons(core->port_)); 
     30    core->bind(); 
     31    core->listen(10); 
     32    core->ioctlsocket(FIONBIO, &NoBlock); 
     33 
     34    NSC_DEBUG_MSG_STD("Currently listeneing to: " + strEx::itos(core->port_)); 
     35 
     36    while (!(WaitForSingleObject(hStopEvent, 100) == WAIT_OBJECT_0)) { 
     37      Socket client; 
     38      if (core->accept(client)) 
     39        core->onAccept(client); 
     40    } 
     41  } catch (SocketException e) { 
     42    NSC_LOG_ERROR_STD(e.getMessage()); 
    3043  } 
    3144 
    32   local.sin_family=AF_INET; 
    33   local.sin_addr.s_addr=INADDR_ANY; 
    34   local.sin_port=htons(port_); 
    35   server=socket(AF_INET,SOCK_STREAM,0); 
    36   if(server==INVALID_SOCKET) { 
    37     WSACleanup(); 
    38     NSC_LOG_ERROR_STD("Could not create listening socket: " + strEx::itos(GetLastError())); 
    39     return 0; 
    40   } 
    41  
    42   if(bind(server,(sockaddr*)&local,sizeof(local))!=0) { 
    43     closesocket(server); 
    44     WSACleanup(); 
    45     NSC_LOG_ERROR_STD("Could not bind socket: " + strEx::itos(GetLastError())); 
    46     return 0; 
    47   } 
    48  
    49   if(listen(server,10)!=0) { 
    50     closesocket(server); 
    51     WSACleanup(); 
    52     NSC_LOG_ERROR_STD("Could not open socket: " + strEx::itos(GetLastError())); 
    53     return 0; 
    54   } 
    55  
    56   SOCKET client; 
    57   sockaddr_in from; 
    58   int fromlen=sizeof(from); 
    59   while (!(WaitForSingleObject(hStopEvent, 100) == WAIT_OBJECT_0)) { 
    60     client=accept(server, (struct sockaddr*)&from,&fromlen); 
    61     if (client != INVALID_SOCKET) 
    62       onAccept(client); 
    63   } 
    64   closesocket(server); 
    65   WSACleanup(); 
     45  CloseHandle(hStopEvent); 
    6646  NSC_DEBUG_MSG("Socket closed!"); 
    6747  return 0; 
     
    7353* The thread manager is responsible for waiting for the actual termination of the thread. 
    7454*/ 
    75 void SimpleSocketListsner::exitThread(void) { 
     55void simpleSocket::Listener::ListenerThread::exitThread(void) { 
    7656  NSC_DEBUG_MSG("Requesting Socket shutdown!"); 
    7757  if (!SetEvent(hStopEvent)) { 
     
    8060} 
    8161 
     62void simpleSocket::Socket::readAll(DataBuffer &buffer, unsigned int tmpBufferLength /* = 1024*/) { 
     63  // @todo this could be optimized a bit if we want to 
     64  // If only one buffer is needed we could "reuse" the buffer instead of copying it. 
     65  char *tmpBuffer = new char[tmpBufferLength+1]; 
     66  int n=recv(socket_,tmpBuffer,tmpBufferLength,0); 
     67  while ((n!=SOCKET_ERROR )||(n!=0)) { 
     68    if (n == tmpBufferLength) { 
     69      // We filled the buffer (There is more to get) 
     70      buffer.append(tmpBuffer, n); 
     71      n=recv(socket_,tmpBuffer,tmpBufferLength,0); 
     72 
     73    } else { 
     74      // Buffer not full, we got it "all" 
     75      buffer.append(tmpBuffer, n); 
     76      break; 
     77    } 
     78  } 
     79  delete [] tmpBuffer; 
     80} 
     81 
     82void simpleSocket::Listener::StartListen(int port) { 
     83  port_ = port; 
     84  threadManager_.createThread(this); 
     85} 
     86void simpleSocket::Listener::close() { 
     87  threadManager_.exitThread(); 
     88  Socket::close(); 
     89} 
    8290 
    8391 
    8492 
    8593 
    86  
  • include/Socket.h

    rc4f6204 r1a5449e  
    44#include <Mutex.h> 
    55#include <WinSock2.h> 
    6 /** 
    7  * @ingroup NSClient++ 
    8  * Socket responder class. 
    9  * This is a background thread that listens to the socket and executes incoming commands. 
    10  * 
    11  * @version 1.0 
    12  * first version 
    13  * 
    14  * @date 02-12-2005 
    15  * 
    16  * @author mickem 
    17  * 
    18  * @par license 
    19  * This code is absolutely free to use and modify. The code is provided "as is" with 
    20  * no expressed or implied warranty. The author accepts no liability if it causes 
    21  * any damage to your computer, causes your pet to fall ill, increases baldness 
    22  * or makes your car start emitting strange noises when you start it up. 
    23  * This code has no bugs, just undocumented features! 
    24  *  
    25  * @todo This is not very well written and should probably be reworked. 
    26  * 
    27  * @bug  
    28  * 
    29  */ 
    30 class SimpleSocketListsner { 
    31 private: 
    32   MutexHandler mutexHandler; 
    33   SOCKET server; 
    34   HANDLE hStopEvent; 
    35   u_short port_; 
    366 
    37 public: 
    38   SimpleSocketListsner(u_short port) : port_(port), hStopEvent(NULL) {}; 
    39   virtual ~SimpleSocketListsner(); 
    40   DWORD threadProc(LPVOID lpParameter); 
    41   void exitThread(void); 
    42 #define RECV_BUFFER_LEN 1024 
    43   typedef std::pair<char*,unsigned int> readAllDataBlock; 
    44   static readAllDataBlock readAll(SOCKET socket) { 
    45     // @bug Is this even working ? 
    46     // @todo Nedds *alot* more work... 
    47     unsigned int buffLen = RECV_BUFFER_LEN; 
    48     char *retBuf = NULL; 
    49     char *buff = new char[buffLen]; 
    50     int n=recv(socket,buff,RECV_BUFFER_LEN,0); 
    51     while ((n!=SOCKET_ERROR )||(n!=0)) { 
    52       if (n == RECV_BUFFER_LEN) { 
    53         char* newBuf = new char[buffLen+RECV_BUFFER_LEN]; 
    54         memcpy(newBuf, buff, buffLen); 
    55         n = recv(socket, buff, RECV_BUFFER_LEN, 0); 
    56         if ((n!=SOCKET_ERROR )&&(n!=0)) { 
    57           memcpy(&newBuf[buffLen], buff, n); 
    58           buffLen += n; 
    59         } 
    60         if (retBuf) 
    61           delete [] retBuf; 
    62         retBuf = newBuf; 
    63       } else { 
    64         buffLen = n; 
    65         break; 
     7namespace simpleSocket { 
     8  class SocketException { 
     9  private: 
     10    std::string error_; 
     11  public: 
     12    SocketException(std::string error) : error_(error) {} 
     13    SocketException(std::string error, int errorCode) : error_(error) { 
     14      error_ += strEx::itos(errorCode); 
     15    } 
     16    std::string getMessage() const { 
     17      return error_; 
     18    } 
     19     
     20  }; 
     21  class DataBuffer { 
     22  private: 
     23    char *buffer_; 
     24    unsigned int length_; 
     25  public: 
     26    DataBuffer() : buffer_(NULL), length_(0){ 
     27    } 
     28    DataBuffer(const DataBuffer &other) { 
     29      buffer_ = new char[other.getLength()]; 
     30      memcpy(buffer_, other.getBuffer(), other.getLength()); 
     31      length_ = other.getLength(); 
     32    } 
     33    virtual ~DataBuffer() { 
     34      delete [] buffer_; 
     35      length_ = 0; 
     36    } 
     37    void append(const char* buffer, const unsigned int length) { 
     38      char *tBuf = new char[length_+length+1]; 
     39      memcpy(tBuf, buffer_, length_); 
     40      memcpy(&tBuf[length_], buffer, length); 
     41      delete [] buffer_; 
     42      buffer_ = tBuf; 
     43      length_ += length; 
     44    } 
     45    const char * getBuffer() const { 
     46      return buffer_; 
     47    } 
     48    unsigned int getLength() const { 
     49      return length_; 
     50    } 
     51  }; 
     52 
     53  class Socket { 
     54  private: 
     55    SOCKET socket_; 
     56    sockaddr_in from_; 
     57 
     58  public: 
     59    Socket() : socket_(NULL) { 
     60    } 
     61    Socket(SOCKET socket) : socket_(socket) { 
     62    } 
     63    Socket(Socket &other) { 
     64      socket_ = other.socket_; 
     65      other.socket_ = NULL; 
     66    } 
     67    virtual ~Socket() { 
     68      if (socket_) 
     69        closesocket(socket_); 
     70      socket_ = NULL; 
     71    } 
     72    SOCKET getSocket() const { 
     73      return socket_; 
     74    } 
     75    virtual void close() { 
     76      assert(socket_); 
     77      closesocket(socket_); 
     78      socket_ = NULL; 
     79    } 
     80    void readAll(DataBuffer &buffer, unsigned int tmpBufferLength = 1024); 
     81 
     82    void socket(int af, int type, int protocol ) { 
     83      socket_ = ::socket(af, type, protocol); 
     84      assert(socket_ != INVALID_SOCKET); 
     85    } 
     86    void bind() { 
     87      int fromlen=sizeof(from_); 
     88      if (::bind(socket_, (sockaddr*)&from_, fromlen) == SOCKET_ERROR) 
     89        throw SocketException("bind failed: ", ::WSAGetLastError()); 
     90    } 
     91    void listen(int backlog = 0) { 
     92      if (::listen(socket_, backlog) == SOCKET_ERROR) 
     93        throw SocketException("listen failed: ", ::WSAGetLastError()); 
     94    } 
     95    bool accept(Socket &client) { 
     96      int fromlen=sizeof(client.from_); 
     97      client.socket_ = ::accept(socket_, (sockaddr*)&client.from_, &fromlen); 
     98      if(client.socket_ == INVALID_SOCKET) { 
     99        int err = ::WSAGetLastError(); 
     100        if (err == WSAEWOULDBLOCK) 
     101          return false; 
     102        throw SocketException("accept failed: ", ::WSAGetLastError()); 
    66103      } 
     104      return true; 
    67105    } 
    68     if (retBuf) { 
    69       delete [] buff; 
    70       return readAllDataBlock(retBuf,buffLen); 
     106    void setAddr(short family, u_long addr, u_short port) { 
     107      from_.sin_family=family; 
     108      from_.sin_addr.s_addr=addr; 
     109      from_.sin_port=port; 
    71110    } 
    72     return readAllDataBlock(buff,buffLen); 
    73   } 
     111    int send(const char * buf, unsigned int len, int flags ) { 
     112      return ::send(socket_, buf, len, flags); 
     113    } 
     114    void ioctlsocket(long cmd, u_long *argp) { 
     115      if (::ioctlsocket(socket_, cmd, argp) == SOCKET_ERROR) 
     116        throw SocketException("ioctlsocket failed: ", ::WSAGetLastError()); 
     117    } 
     118 
     119    static WSADATA WSAStartup(WORD wVersionRequested = 0x202) { 
     120      WSADATA wsaData; 
     121      int wsaret=::WSAStartup(wVersionRequested,&wsaData); 
     122      if(wsaret != 0) 
     123        throw SocketException("WSAStartup failed: " + strEx::itos(wsaret)); 
     124      return wsaData; 
     125    } 
     126    static void WSACleanup() { 
     127      if (::WSACleanup() != 0) 
     128        throw SocketException("WSACleanup failed: ", ::WSAGetLastError()); 
     129    } 
    74130 
    75131 
    76 private: 
    77   virtual void onAccept(SOCKET client) {} 
    78 }; 
     132 
     133  }; 
    79134 
    80135 
     136  /** 
     137   * @ingroup NSClient++ 
     138   * Socket responder class. 
     139   * This is a background thread that listens to the socket and executes incoming commands. 
     140   * 
     141   * @version 1.0 
     142   * first version 
     143   * 
     144   * @date 02-12-2005 
     145   * 
     146   * @author mickem 
     147   * 
     148   * @par license 
     149   * This code is absolutely free to use and modify. The code is provided "as is" with 
     150   * no expressed or implied warranty. The author accepts no liability if it causes 
     151   * any damage to your computer, causes your pet to fall ill, increases baldness 
     152   * or makes your car start emitting strange noises when you start it up. 
     153   * This code has no bugs, just undocumented features! 
     154   *  
     155   * @todo This is not very well written and should probably be reworked. 
     156   * 
     157   * @bug  
     158   * 
     159   */ 
     160  class Listener : public Socket { 
     161  public: 
     162    class ListenerThread { 
     163    private: 
     164      HANDLE hStopEvent; 
     165    public: 
     166      ListenerThread() : hStopEvent(NULL) { 
     167      } 
     168      DWORD threadProc(LPVOID lpParameter); 
     169      void exitThread(void); 
     170    }; 
     171 
     172  private: 
     173    MutexHandler mutexHandler; 
     174    u_short port_; 
     175    typedef Thread<ListenerThread> listenThreadManager; 
     176    listenThreadManager threadManager_; 
     177 
     178  public: 
     179    Listener() {}; 
     180    virtual ~Listener(); 
     181 
     182    void StartListen(int port); 
     183    virtual void close(); 
     184 
     185  private: 
     186    virtual void onAccept(Socket client) = 0; 
     187 
     188  }; 
     189} 
     190 
  • include/arrayBuffer.cpp

    rc4f6204 r1a5449e  
    99* @return Argument wrapped as a list 
    1010*/ 
    11 std::list<std::string> arrayBuffer::arrayBuffer2list(const unsigned int argLen, char *argument[]) { 
    12   std::list<std::string> ret; 
     11arrayBuffer::arrayList arrayBuffer::arrayBuffer2list(const unsigned int argLen, char *argument[]) { 
     12  arrayList ret; 
    1313  int i=0; 
    1414  for (unsigned int i=0;i<argLen;i++) { 
     
    2727* @return A pointer that is managed by the caller. 
    2828*/ 
    29 char ** arrayBuffer::list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen) { 
     29char ** arrayBuffer::list2arrayBuffer(const arrayList lst, unsigned int &argLen) { 
    3030  argLen = static_cast<unsigned int>(lst.size()); 
    3131  char **arrayBuffer = new char*[argLen]; 
    32   std::list<std::string>::const_iterator it = lst.begin(); 
     32  arrayList::const_iterator it = lst.begin(); 
    3333  for (int i=0;it!=lst.end();++it,i++) { 
    3434    std::string::size_type alen = (*it).size(); 
     
    121121    strncpy(arrayBuffer[i], inBuf.substr(l,p).c_str(), len); 
    122122    arrayBuffer[i][len] = 0; 
     123    l = ++p; 
    123124    p = inBuf.find(splitChar, p); 
    124125  } 
  • include/arrayBuffer.h

    rc4f6204 r1a5449e  
    44namespace arrayBuffer { 
    55 
     6  typedef std::list<std::string> arrayList; 
    67 
    7   std::list<std::string> arrayBuffer2list(const unsigned int argLen, char **argument); 
    8   char ** list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen); 
     8  arrayList arrayBuffer2list(const unsigned int argLen, char **argument); 
     9  char ** list2arrayBuffer(const arrayList lst, unsigned int &argLen); 
    910  char ** split2arrayBuffer(const char* buffer, char splitChar, unsigned int &argLen); 
    1011  char ** split2arrayBuffer(const std::string buffer, char splitChar, unsigned int &argLen); 
  • include/strEx.h

    rc4f6204 r1a5449e  
    77namespace strEx { 
    88 
     9  inline void replace(std::string &string, std::string replace, std::string with) { 
     10    std::string::size_type pos = string.find(replace); 
     11    std::string::size_type len = replace.length(); 
     12    while (pos != std::string::npos) { 
     13      string = string.substr(0,pos)+with+string.substr(pos+len); 
     14      pos = string.find(replace, pos+1); 
     15    } 
     16  } 
     17  inline std::string itos(unsigned int i) { 
     18    std::stringstream ss; 
     19    ss << i; 
     20    return ss.str(); 
     21  } 
    922  inline std::string itos(int i) { 
    1023    std::stringstream ss; 
     
    8295    test_getToken("foo&bar&test", '&', "foo", "bar&test"); 
    8396  } 
     97 
     98  inline void test_replace(std::string source, std::string replace, std::string with, std::string out) { 
     99    std::cout << "strEx::test_replace(" << source << ", " << replace << ", " << with << ") : "; 
     100    std::string s = source; 
     101    strEx::replace(s, replace, with); 
     102    if (s == out) 
     103      std::cout << "Succeeded" << std::endl; 
     104    else 
     105      std::cout << "Failed [" << s << "=" << out << "]" << std::endl; 
     106  } 
     107  inline void run_test_replace() { 
     108    test_replace("", "", "", ""); 
     109    test_replace("foo", "", "", "foo"); 
     110    test_replace("foobar", "foo", "", "bar"); 
     111    test_replace("foobar", "foo", "bar", "barbar"); 
     112  } 
     113 
    84114#endif 
    85115} 
  • modules/CheckDisk/CheckDisk.cpp

    r1eef1ee r1a5449e  
    109109  NSC_DEBUG_MSG("CheckFileSize"); 
    110110  NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 
    111   std::list<std::string> args = NSCHelper::arrayBuffer2list(argLen, char_args); 
     111  std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
    112112  if (args.empty()) { 
    113113    message = "Missing argument(s)."; 
  • modules/CheckDisk/CheckDisk.vcproj

    r36c340d r1a5449e  
    219219      Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" 
    220220      UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
     221      <File 
     222        RelativePath="..\..\include\arrayBuffer.cpp"> 
     223      </File> 
    221224      <File 
    222225        RelativePath=".\CheckDisk.cpp"> 
  • modules/CheckEventLog/CheckEventLog.cpp

    r1eef1ee r1a5449e  
    300300    return NSCAPI::returnIgnored; 
    301301  NSCAPI::nagiosReturn rCode = NSCAPI::returnOK; 
    302   std::list<std::string> args = NSCHelper::arrayBuffer2list(argLen, char_args); 
     302  std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
    303303  if (args.size() < 2) { 
    304304    message = "Missing argument"; 
    305305    return NSCAPI::returnCRIT; 
     306  } 
     307 
     308  std::list<std::string>::iterator it = args.begin(); 
     309  for (;it != args.end();it++) { 
     310    NSC_DEBUG_MSG_STD("Arguments: " + (*it)); 
    306311  } 
    307312  std::string ret; 
     
    400405 
    401406  CloseEventLog(hLog); 
    402   if (critical) 
     407  if (critical) { 
    403408    ret = "CRITICAL: " + ret; 
    404   else if (!ret.empty()) 
     409    rCode = NSCAPI::returnCRIT; 
     410  } 
     411  else if (!ret.empty()) { 
    405412    ret = "WARNING: " + ret; 
    406   else  
     413    rCode = NSCAPI::returnWARN; 
     414  } else  
    407415    ret = "OK: No errors/warnings in event log."; 
    408416  if (query.truncate != 0) 
  • modules/CheckEventLog/CheckEventLog.vcproj

    r2a94f3f r1a5449e  
    122122      UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
    123123      <File 
     124        RelativePath="..\..\include\arrayBuffer.cpp"> 
     125      </File> 
     126      <File 
    124127        RelativePath=".\CheckEventLog.cpp"> 
    125128      </File> 
  • modules/FileLogger/FileLogger.vcproj

    r2a94f3f r1a5449e  
    120120      UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
    121121      <File 
     122        RelativePath="..\..\include\arrayBuffer.cpp"> 
     123      </File> 
     124      <File 
    122125        RelativePath=".\FileLogger.cpp"> 
    123126      </File> 
  • modules/NRPEListener/NRPEListener.cpp

    r1eef1ee r1a5449e  
    2020} 
    2121 
     22#define DEFAULT_NRPE_PORT 5666 
     23 
    2224 
    2325bool NRPEListener::loadModule() { 
    24   socketThreadManager.createThread(NULL); 
     26  timeout = NSCModuleHelper::getSettingsInt("NRPE", "commandTimeout", 60); 
     27  std::list<std::string> commands = NSCModuleHelper::getSettingsSection("NRPE Handlers"); 
     28  std::list<std::string>::iterator it; 
     29  for (it = commands.begin(); it != commands.end(); it++) { 
     30    strEx::token t = strEx::getToken(*it, '='); 
     31    if (t.first.substr(0,7) == "command") { 
     32      strEx::token t2 = strEx::getToken(t.first, '['); 
     33      t2 = strEx::getToken(t2.second, ']'); 
     34      t.first = t2.first; 
     35    } 
     36    if (t.first.empty() || t.second.empty()) { 
     37      NSC_LOG_ERROR_STD("Invalid command definition: " + (*it)); 
     38    } else 
     39      addCommand(t.first, t.second); 
     40  } 
     41 
     42  simpleSocket::Socket::WSAStartup(); 
     43  socket.StartListen(NSCModuleHelper::getSettingsInt("NRPE", "port", DEFAULT_NRPE_PORT)); 
    2544  return true; 
    2645} 
    2746bool NRPEListener::unloadModule() { 
    28   socketThreadManager.exitThread(); 
     47  socket.close(); 
     48  simpleSocket::Socket::WSACleanup(); 
    2949  return true; 
    3050} 
    3151 
    3252std::string NRPEListener::getModuleName() { 
    33   return "CheckDisk Various Disk related checks."; 
     53  return "NRPE module."; 
    3454} 
    3555NSCModuleWrapper::module_version NRPEListener::getModuleVersion() { 
     
    4767 
    4868NSCAPI::nagiosReturn NRPEListener::handleCommand(const std::string command, const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { 
    49   return NSCAPI::returnIgnored; 
     69  commandList::iterator it = commands.find(command); 
     70  if (it == commands.end()) 
     71    return NSCAPI::returnIgnored; 
     72 
     73  std::string str = (*it).second; 
     74  if (NSCModuleHelper::getSettingsInt("NRPE", "AllowArguments", 0) == 0) { 
     75    arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args); 
     76    arrayBuffer::arrayList::const_iterator cit = arr.begin(); 
     77    int i=0; 
     78 
     79    for (;cit!=arr.end();it++,i++) { 
     80      strEx::replace(str, "ARG" + strEx::itos(i), (*cit)); 
     81    } 
     82  } 
     83 
     84  if (NSCModuleHelper::getSettingsInt("NRPE", "AllowNastyMetaChars", 0) == 0) { 
     85    if (str.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     86      NSC_LOG_ERROR("Request command contained illegal metachars!"); 
     87      return NSCAPI::returnIgnored; 
     88    } 
     89  } 
     90 
     91  return executeNRPECommand(str, message, perf); 
     92} 
     93#define MAX_INPUT_BUFFER 1024 
     94 
     95int NRPEListener::executeNRPECommand(std::string command, std::string &msg, std::string &perf) 
     96{ 
     97  NSCAPI::nagiosReturn result; 
     98  PROCESS_INFORMATION pi; 
     99  STARTUPINFO si; 
     100  HANDLE hChildOutR, hChildOutW, hChildInR, hChildInW; 
     101  SECURITY_ATTRIBUTES sec; 
     102  DWORD dwstate, dwexitcode; 
     103  int retval; 
     104 
     105 
     106  // Set up members of SECURITY_ATTRIBUTES structure.  
     107 
     108  sec.nLength = sizeof(SECURITY_ATTRIBUTES); 
     109  sec.bInheritHandle = TRUE; 
     110  sec.lpSecurityDescriptor = NULL; 
     111 
     112  // Create Pipes 
     113  CreatePipe(&hChildInR, &hChildInW, &sec, 0); 
     114  CreatePipe(&hChildOutR, &hChildOutW, &sec, 0); 
     115 
     116  // Set up members of STARTUPINFO structure.  
     117 
     118  ZeroMemory(&si, sizeof(STARTUPINFO)); 
     119  si.cb = sizeof(STARTUPINFO); 
     120  si.dwFlags = STARTF_USESTDHANDLES; 
     121  si.hStdInput = hChildInR; 
     122  si.hStdOutput = hChildOutW; 
     123  si.hStdError = hChildOutW; 
     124 
     125 
     126  // CreateProcess doesn't work with a const command 
     127  char *cmd = new char[command.length()+1]; 
     128  strncpy(cmd, command.c_str(), command.length()); 
     129  cmd[command.length()] = 0; 
     130 
     131  // Create the child process.  
     132  BOOL processOK = CreateProcess(NULL, cmd,        // command line  
     133    NULL, // process security attributes  
     134    NULL, // primary thread security attributes  
     135    TRUE, // handles are inherited  
     136    0,    // creation flags  
     137    NULL, // use parent's environment  
     138    NULL, // use parent's current directory  
     139    &si,  // STARTUPINFO pointer  
     140    &pi); // receives PROCESS_INFORMATION  
     141  delete [] cmd; 
     142 
     143  if (processOK) { 
     144    dwstate = WaitForSingleObject(pi.hProcess, 1000*timeout); 
     145    CloseHandle(hChildInR); 
     146    CloseHandle(hChildInW); 
     147    CloseHandle(hChildOutW); 
     148 
     149    if (dwstate == WAIT_TIMEOUT) { 
     150      TerminateProcess(pi.hProcess, 5); 
     151      msg = "The check didn't respond within the timeout period!"; 
     152      result = NSCAPI::returnUNKNOWN; 
     153    } else { 
     154      DWORD dwread; 
     155      char *buf = new char[MAX_INPUT_BUFFER+1]; 
     156      retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER, &dwread, NULL); 
     157      if (!retval || dwread == 0) { 
     158        msg = "No output available from command..."; 
     159      } else { 
     160        buf[dwread] = 0; 
     161        msg = buf; 
     162        strEx::token t = strEx::getToken(msg, '\n'); 
     163        t = strEx::getToken(t.first, '|'); 
     164        msg = t.first; 
     165        perf = t.second; 
     166      } 
     167      delete [] buf; 
     168      result = NSCHelper::int2nagios(GetExitCodeProcess(pi.hProcess, &dwexitcode)); 
     169    } 
     170    CloseHandle(pi.hThread); 
     171    CloseHandle(pi.hProcess); 
     172    CloseHandle(hChildOutR); 
     173  } 
     174  else { 
     175    msg = "NRPE_NT failed to create process, exiting..."; 
     176    result = NSCAPI::returnUNKNOWN; 
     177    CloseHandle(hChildInR); 
     178    CloseHandle(hChildInW); 
     179    CloseHandle(hChildOutW); 
     180    CloseHandle(pi.hThread); 
     181    CloseHandle(pi.hProcess); 
     182    CloseHandle(hChildOutR); 
     183  } 
     184  return result; 
    50185} 
    51186 
  • modules/NRPEListener/NRPEListener.h

    r1eef1ee r1a5449e  
    44#include "NRPESocket.h" 
    55#include <Socket.h> 
     6#include <map> 
    67 
    78class NRPEListener { 
    89private: 
    9   NRPESocketThread socketThreadManager; 
     10  NRPESocket socket; 
     11  typedef std::map<std::string, std::string> commandList; 
     12  commandList commands; 
     13  unsigned int timeout; 
    1014 
    1115public: 
     
    2024  bool hasMessageHandler(); 
    2125  NSCAPI::nagiosReturn handleCommand(const std::string command, const unsigned int argLen, char **char_args, std::string &message, std::string &perf); 
     26 
     27private: 
     28  int executeNRPECommand(std::string command, std::string &msg, std::string &perf); 
     29  void addCommand(std::string key, std::string args) { 
     30    commands[key] = args; 
     31  } 
    2232}; 
    2333 
  • modules/NRPEListener/NRPEListener.vcproj

    r1eef1ee r1a5449e  
    122122      UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
    123123      <File 
     124        RelativePath="..\..\include\arrayBuffer.cpp"> 
     125      </File> 
     126      <File 
    124127        RelativePath=".\NRPEListener.cpp"> 
    125128      </File> 
  • modules/NRPEListener/NRPESocket.cpp

    rd4f294a r1a5449e  
    66 * Default c-tor 
    77 */ 
    8 NRPESocket::NRPESocket(): SimpleSocketListsner(DEFAULT_NRPE_PORT) { 
     8NRPESocket::NRPESocket() { 
    99} 
    1010 
     
    1212} 
    1313 
     14 
     15 
    1416typedef short int16_t; 
    1517typedef unsigned long u_int32_t; 
    1618 
    17 typedef struct packet_struct{ 
    18   int16_t   packet_version; 
    19   int16_t   packet_type; 
    20   u_int32_t crc32_value; 
    21   int16_t   result_code; 
    22   char      buffer[1024]; 
    23 }packet; 
    24  
    2519static unsigned long crc32_table[256]; 
    26  
    27 /* build the crc table - must be called before calculating the crc value */ 
     20static bool hascrc32 = false; 
    2821void generate_crc32_table(void){ 
    2922  unsigned long crc, poly; 
    3023  int i, j; 
    31  
    3224  poly=0xEDB88320L; 
    3325  for(i=0;i<256;i++){ 
     
    4133    crc32_table[i]=crc; 
    4234  } 
    43  
    44   return; 
     35  hascrc32 = true; 
    4536} 
    46  
    47 /* calculates the CRC 32 value for a buffer */ 
    48 unsigned long calculate_crc32(char *buffer, int buffer_size){ 
     37unsigned long calculate_crc32(const char *buffer, int buffer_size){ 
     38  if (!hascrc32) 
     39    generate_crc32_table(); 
    4940  register unsigned long crc; 
    5041  int this_char; 
     
    6152} 
    6253 
    63 void NRPESocket::onAccept(SOCKET client) { 
    64   NSC_DEBUG_MSG("Accepting connection from remote host"); 
    6554 
    66   SimpleSocketListsner::readAllDataBlock block = SimpleSocketListsner::readAll(client); 
    67   packet *p = reinterpret_cast<packet*>(block.first); 
    68   // @todo Verify versions and stuff, and ofcource add SSL (but thats in the future :) 
    69   NSC_DEBUG_MSG_STD("Incoming data: " + p->buffer); 
     55class NRPEPacket { 
     56public: 
     57  static const short queryPacket = 1; 
     58  static const short responsePacket = 2; 
     59  static const short version2 = 2; 
     60private: 
     61  typedef struct packet { 
     62    int16_t   packet_version; 
     63    int16_t   packet_type; 
     64    u_int32_t crc32_value; 
     65    int16_t   result_code; 
     66    char      buffer[1024]; 
     67  } packet; 
     68  std::string payload_; 
     69  short type_; 
     70  short version_; 
     71  NSCAPI::nagiosReturn result_; 
     72  unsigned int crc32_; 
     73  unsigned int calculatedCRC32_; 
     74  char *tmpBuffer; 
     75public: 
     76  NRPEPacket(const char *buffer) : tmpBuffer(NULL) { 
     77    const packet *p = reinterpret_cast<const packet*>(buffer); 
     78    type_ = ntohs(p->packet_type); 
     79    assert( (type_ == queryPacket)||(type_ == responsePacket)); 
     80    version_ = ntohs(p->packet_version); 
     81    assert(version_ == version2); 
     82    crc32_ = ntohl(p->crc32_value); 
     83    // Verify CRC32 
     84    // @todo Fix this, currently we need a const buffer so we cannot change the crc to 0. 
     85    char * tb = new char[getBufferLength()]; 
     86    memcpy(tb, buffer, getBufferLength()); 
     87    packet *p2 = reinterpret_cast<packet*>(tb); 
     88    p2->crc32_value = 0; 
     89    calculatedCRC32_ = calculate_crc32(tb, getBufferLength()); 
     90    delete [] tb; 
     91    // Verify CRC32 end 
     92    result_ = NSCHelper::int2nagios(ntohs(p->result_code)); 
     93    payload_ = std::string(p->buffer); 
     94  } 
     95  NRPEPacket(short type, short version, NSCAPI::nagiosReturn result, std::string payLoad)  
     96    : tmpBuffer(NULL)  
     97    ,type_(type) 
     98    ,version_(version) 
     99    ,result_(result) 
     100    ,payload_(payLoad) 
     101  { 
     102  } 
     103  ~NRPEPacket() { 
     104    delete [] tmpBuffer; 
     105  } 
     106  unsigned short getVersion() const { return version_; } 
     107  unsigned short getType() const { return type_; } 
     108  unsigned short getResult() const { return result_; } 
     109  std::string getPayload() const { return payload_; } 
     110  const char* getBuffer() { 
     111    delete [] tmpBuffer; 
     112    tmpBuffer = new char[getBufferLength()]; 
     113    packet *p = reinterpret_cast<packet*>(tmpBuffer); 
     114    p->result_code = htons(NSCHelper::nagios2int(result_)); 
     115    p->packet_type = htons(type_); 
     116    p->packet_version = htons(version_); 
     117    p->crc32_value = 0; 
     118    strncpy(p->buffer, payload_.c_str(), 1023); 
     119    p->buffer[1024] = 0; 
     120    p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 
     121    return tmpBuffer; 
     122  } 
     123  bool verifyCRC() { 
     124    return calculatedCRC32_ == crc32_; 
     125  } 
     126  const unsigned int getBufferLength() const { 
     127    return sizeof(packet); 
     128  } 
     129}; 
    70130 
    71   charEx::token cmd = charEx::getToken(p->buffer, '!'); 
     131void NRPESocket::onAccept(simpleSocket::Socket client) { 
     132  simpleSocket::DataBuffer block; 
     133  client.readAll(block); 
     134  NRPEPacket p(block.getBuffer()); 
     135  if (p.getType() != NRPEPacket::queryPacket) { 
     136    NSC_LOG_ERROR("Request is not a query."); 
     137    client.close(); 
     138    return; 
     139  } 
     140  if (p.getVersion() != NRPEPacket::version2) { 
     141    NSC_LOG_ERROR("Request had unsupported version."); 
     142    client.close(); 
     143    return; 
     144  } 
     145  if (!p.verifyCRC()) { 
     146    NSC_LOG_ERROR("Request had invalid checksum."); 
     147    client.close(); 
     148    return; 
     149  } 
     150  strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
    72151  std::string msg, perf; 
    73   NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first.c_str(), cmd.second, '!', msg, perf); 
    74 //QUERY_PACKET 
    75   packet p2; 
    76   strncpy(p2.buffer, (msg+"|"+perf).c_str(), 1023); 
    77   p2.buffer[1023] = 0; 
    78   p2.packet_type = htons(2); 
    79   p2.packet_version = htons(2); 
    80   p2.result_code = htons(static_cast<int>(ret)); 
     152  NSC_DEBUG_MSG_STD("Command: " + cmd.first); 
     153  NSC_DEBUG_MSG_STD("Arguments: " + cmd.second); 
    81154 
    82   generate_crc32_table(); 
    83   p2.crc32_value = 0; 
    84   p2.crc32_value = htonl(calculate_crc32(reinterpret_cast<char*>(&p2),sizeof(p2))); 
    85   send(client, reinterpret_cast<char*>(&p2), sizeof(p2), 0); 
     155  if (NSCModuleHelper::getSettingsInt("NRPE", "AllowArguments", 0) == 0) { 
     156    if (!cmd.second.empty()) { 
     157      NSC_LOG_ERROR("Request contained arguments (not currently allowed)."); 
     158      client.close(); 
     159      return; 
     160    } 
     161  } 
     162  if (NSCModuleHelper::getSettingsInt("NRPE", "AllowNastyMetaChars", 0) == 0) { 
     163    if (cmd.first.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     164      NSC_LOG_ERROR("Request command contained illegal metachars!"); 
     165      client.close(); 
     166      return; 
     167    } 
     168    if (cmd.second.find_first_of(NASTY_METACHARS) != std::string::npos) { 
     169      NSC_LOG_ERROR("Request arguments contained illegal metachars!"); 
     170      client.close(); 
     171      return; 
     172    } 
     173  } 
    86174 
    87   delete [] block.first; 
    88   closesocket(client); 
     175  NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
     176  if (perf.empty()) { 
     177    NRPEPacket p2(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); 
     178    client.send(p2.getBuffer(), p2.getBufferLength(), 0); 
     179  } else { 
     180    NRPEPacket p2(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + "|" + perf); 
     181    client.send(p2.getBuffer(), p2.getBufferLength(), 0); 
     182  } 
     183  client.close(); 
    89184} 
    90185 
  • modules/NRPEListener/NRPESocket.h

    r1eef1ee r1a5449e  
    55#include <WinSock2.h> 
    66#include <Socket.h> 
     7#include <string.h> 
    78/** 
    89 * @ingroup NSClient++ 
     
    3031 */ 
    3132 
    32 #define DEFAULT_NRPE_PORT 5666 
    3333 
     34#define NASTY_METACHARS         "|`&><'\"\\[]{}"        /* This may need to be modified for windows directory seperator */ 
    3435 
    35 class NRPESocket : public SimpleSocketListsner { 
     36class NRPESocket : public simpleSocket::Listener { 
    3637private: 
    3738 
     
    4142 
    4243private: 
    43   virtual void onAccept(SOCKET client); 
     44  virtual void onAccept(simpleSocket::Socket client); 
    4445}; 
    4546 
    4647 
    47 typedef Thread<NRPESocket> NRPESocketThread; // Thread manager 
    4848 
    4949 
  • modules/NSClientListener/NSClientListener.cpp

    r2a94f3f r1a5449e  
    2020} 
    2121 
     22#define DEFAULT_TCP_PORT 12489 
    2223 
    2324bool NSClientListener::loadModule() { 
    24   socketThreadManager.createThread(NULL); 
     25  socket.StartListen(NSCModuleHelper::getSettingsInt("NSClient", "port", DEFAULT_TCP_PORT)); 
    2526  return true; 
    2627} 
    2728bool NSClientListener::unloadModule() { 
    28   socketThreadManager.exitThread(); 
     29  socket.close(); 
    2930  return true; 
    3031} 
  • modules/NSClientListener/NSClientListener.h

    r2a94f3f r1a5449e  
    55class NSClientListener { 
    66private: 
    7   NSClientSocketThread socketThreadManager; 
     7  NSClientSocket socket; 
    88 
    99public: 
  • modules/NSClientListener/NSClientSocket.cpp

    rc4f6204 r1a5449e  
    66 * Default c-tor 
    77 */ 
    8 NSClientSocket::NSClientSocket() : SimpleSocketListsner(DEFAULT_TCP_PORT) { 
     8NSClientSocket::NSClientSocket() { 
    99} 
    1010 
     
    4848} 
    4949 
    50 void NSClientSocket::onAccept(SOCKET client) { 
    51  
    52   readAllDataBlock rdb = readAll(client); 
    53   if (rdb.second > 0) { 
    54     NSC_DEBUG_MSG_STD("Incoming data length: " + strEx::itos((int)rdb.second)); 
    55     std::string incoming((char*)rdb.first, (unsigned int)rdb.second); 
     50void NSClientSocket::onAccept(simpleSocket::Socket client) { 
     51  simpleSocket::DataBuffer db; 
     52  client.readAll(db); 
     53  if (db.getLength() > 0) { 
     54    NSC_DEBUG_MSG_STD("Incoming data length: " + strEx::itos(db.getLength())); 
     55    std::string incoming(db.getBuffer(), db.getLength()); 
    5656    NSC_DEBUG_MSG_STD("Incoming data: " + incoming); 
    5757    std::string response = parseRequest(incoming); 
    5858    NSC_DEBUG_MSG("Outgoing data: " + response); 
    59     send(client, response.c_str(), static_cast<int>(response.length()), 0); 
     59    client.send(response.c_str(), static_cast<int>(response.length()), 0); 
    6060  } 
    61   delete [] rdb.first; 
    62   closesocket(client); 
     61  client.close(); 
    6362} 
  • modules/NSClientListener/NSClientSocket.h

    rc4f6204 r1a5449e  
    3131 * 
    3232 */ 
    33 class NSClientSocket : public SimpleSocketListsner { 
     33class NSClientSocket : public simpleSocket::Listener { 
    3434private: 
    3535 
     
    3939 
    4040private: 
    41   virtual void onAccept(SOCKET client); 
     41  virtual void onAccept(simpleSocket::Socket client); 
    4242  std::string parseRequest(std::string buffer); 
    4343}; 
    4444 
    45 #define DEFAULT_TCP_PORT 12489 
    4645 
    4746#define REQ_CLIENTVERSION 1 // Works fine! 
     
    5756 
    5857 
    59 typedef Thread<NSClientSocket> NSClientSocketThread; // Thread manager 
    60  
    61  
  • modules/SysTray/SysTray.vcproj

    r2a94f3f r1a5449e  
    117117      UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> 
    118118      <File 
     119        RelativePath="..\..\include\arrayBuffer.cpp"> 
     120      </File> 
     121      <File 
    119122        RelativePath="..\..\include\NSCHelper.cpp"> 
    120123      </File> 
Note: See TracChangeset for help on using the changeset viewer.