Changeset be0202f in nscp


Ignore:
Timestamp:
02/13/08 22:43:47 (5 years ago)
Author:
Michael Medin <michael@…>
Children:
fc7cdcf
Parents:
9da54aa
Message:

2008-02-13 MickeM

+ Added string_length to [Settings] as well (used internally) for all "injected" buffers.

  • Fixed issue with scripts result truncated efter 1024 chars

(now they return "all" output and thus you can use the NRPE settings I adde yesterday :)

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/AutoBuild.h

    r9da54aa rbe0202f  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,0,50 
    6 #define PRODUCTVER     0,3,0,50 
    7 #define STRFILEVER     _T("0.3.0.50") 
    8 #define STRPRODUCTVER  _T("0.3.0.50") 
    9 #define STRPRODUCTDATE  _T("2008-02-11") 
     5#define FILEVER        0,3,0,54 
     6#define PRODUCTVER     0,3,0,54 
     7#define STRFILEVER     _T("0.3.0.54") 
     8#define STRPRODUCTVER  _T("0.3.0.54") 
     9#define STRPRODUCTDATE  _T("2008-02-12") 
    1010#endif // AUTOBUILD_H 
  • trunk/NSClient++-2005.vcproj

    r5ee4e31 rbe0202f  
    31233123      > 
    31243124    </File> 
     3125    <File 
     3126      RelativePath=".\scripts\test.lua" 
     3127      > 
     3128    </File> 
    31253129  </Files> 
    31263130  <Globals> 
  • trunk/NSClient++.cpp

    r9da54aa rbe0202f  
    446446} 
    447447 
     448unsigned int NSClientT::getBufferLength() { 
     449  static unsigned int len = 0; 
     450  if (len == 0) { 
     451    try { 
     452      len = Settings::getInstance()->getInt(MAIN_SECTION_TITLE, MAIN_STRING_LENGTH, MAIN_STRING_LENGTH_DEFAULT); 
     453    } catch (SettingsException &e) { 
     454      NSC_DEBUG_MSG(_T("Failed to get length: ") + e.getMessage()); 
     455      return MAIN_STRING_LENGTH_DEFAULT; 
     456    } catch (...) { 
     457      NSC_LOG_ERROR(_T("Failed to get length: :(")); 
     458      return MAIN_STRING_LENGTH_DEFAULT; 
     459    } 
     460  } 
     461  return len; 
     462} 
     463 
    448464NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 
    449465  unsigned int aLen = 0; 
    450466  TCHAR ** aBuf = arrayBuffer::split2arrayBuffer(arguments, splitter, aLen, escape); 
    451   TCHAR * mBuf = new TCHAR[1024]; mBuf[0] = '\0'; 
    452   TCHAR * pBuf = new TCHAR[1024]; pBuf[0] = '\0'; 
    453   NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), aLen, aBuf, mBuf, 1023, pBuf, 1023); 
     467  unsigned int buf_len = getBufferLength(); 
     468  TCHAR * mBuf = new TCHAR[buf_len+1]; mBuf[0] = '\0'; 
     469  TCHAR * pBuf = new TCHAR[buf_len+1]; pBuf[0] = '\0'; 
     470  NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), aLen, aBuf, mBuf, buf_len, pBuf, buf_len); 
    454471  arrayBuffer::destroyArrayBuffer(aBuf, aLen); 
    455472  if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { 
     
    599616  if (!basePath.empty()) 
    600617    return basePath; 
    601   TCHAR* buffer = new TCHAR[1024]; 
    602   GetModuleFileName(NULL, buffer, 1023); 
     618  unsigned int buf_len = 4096; 
     619  TCHAR* buffer = new TCHAR[buf_len+1]; 
     620  GetModuleFileName(NULL, buffer, buf_len); 
    603621  std::wstring path = buffer; 
    604622  std::wstring::size_type pos = path.rfind('\\'); 
     
    615633 
    616634NSCAPI::errorReturn NSAPIGetSettingsString(const TCHAR* section, const TCHAR* key, const TCHAR* defaultValue, TCHAR* buffer, unsigned int bufLen) { 
    617   return NSCHelper::wrapReturnString(buffer, bufLen, Settings::getInstance()->getString(section, key, defaultValue), NSCAPI::isSuccess); 
     635  try { 
     636    return NSCHelper::wrapReturnString(buffer, bufLen, Settings::getInstance()->getString(section, key, defaultValue), NSCAPI::isSuccess); 
     637  } catch (...) { 
     638    NSC_LOG_ERROR_STD(_T("Failed to getString: ") + key); 
     639    return NSCAPI::hasFailed; 
     640  } 
    618641} 
    619642int NSAPIGetSettingsInt(const TCHAR* section, const TCHAR* key, int defaultValue) { 
     
    644667} 
    645668NSCAPI::errorReturn NSAPIGetSettingsSection(const TCHAR* section, TCHAR*** aBuffer, unsigned int * bufLen) { 
    646   unsigned int len = 0; 
    647   *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len); 
    648   *bufLen = len; 
    649   return NSCAPI::isSuccess; 
     669  try { 
     670    unsigned int len = 0; 
     671    *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len); 
     672    *bufLen = len; 
     673    return NSCAPI::isSuccess; 
     674  } catch (...) { 
     675    NSC_LOG_ERROR_STD(_T("Failed to getSection: ") + section); 
     676    return NSCAPI::hasFailed; 
     677  } 
    650678} 
    651679NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(TCHAR*** aBuffer, unsigned int * bufLen) { 
     
    770798 
    771799NSCAPI::errorReturn NSAPISetSettingsString(const TCHAR* section, const TCHAR* key, const TCHAR* value) { 
    772   Settings::getInstance()->setString(section, key, value); 
     800  try { 
     801    Settings::getInstance()->setString(section, key, value); 
     802  } catch (...) { 
     803    NSC_LOG_ERROR_STD(_T("Failed to setString: ") + key); 
     804    return NSCAPI::hasFailed; 
     805  } 
    773806  return NSCAPI::isSuccess; 
    774807} 
    775808NSCAPI::errorReturn NSAPISetSettingsInt(const TCHAR* section, const TCHAR* key, int value) { 
    776   Settings::getInstance()->setInt(section, key, value); 
     809  try { 
     810    Settings::getInstance()->setInt(section, key, value); 
     811  } catch (...) { 
     812    NSC_LOG_ERROR_STD(_T("Failed to setInt: ") + key); 
     813    return NSCAPI::hasFailed; 
     814  } 
    777815  return NSCAPI::isSuccess; 
    778816} 
     
    786824      Settings::getInstance()->write(); 
    787825  } catch (SettingsException e) { 
    788     LOG_ERROR_STD(e.getMessage()); 
     826    LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage()); 
     827    return NSCAPI::hasFailed; 
     828  } catch (...) { 
     829    NSC_LOG_ERROR_STD(_T("Failed to write settings")); 
    789830    return NSCAPI::hasFailed; 
    790831  } 
     
    800841      Settings::getInstance()->read(); 
    801842  } catch (SettingsException e) { 
    802     LOG_ERROR_STD(e.getMessage()); 
     843    LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage()); 
     844    return NSCAPI::hasFailed; 
     845  } catch (...) { 
     846    NSC_LOG_ERROR_STD(_T("Failed to read settings")); 
    803847    return NSCAPI::hasFailed; 
    804848  } 
  • trunk/NSClient++.h

    r442eaf2 rbe0202f  
    102102  std::list<std::wstring> getAllCommandNames(); 
    103103  void registerCommand(std::wstring cmd, std::wstring desc); 
     104  unsigned int getBufferLength(); 
    104105 
    105106  bool logDebug(); 
  • trunk/changelog

    r9da54aa rbe0202f  
    55 * Add module for relaying events 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 
     82008-02-13 MickeM 
     9 + Added string_length to [Settings] as well (used internally) for all "injected" buffers. 
     10 * Fixed issue with scripts result truncated efter 1024 chars  
     11    (now they return "all" output and thus you can use the NRPE settings I adde yesterday :) 
     12 
     132008-02-12 MickeM 
     14 + Added new option for the [NRPE] section string_length which is the length of the NRPE strings  
     15   (notice you need to recompile the check_nrpe to match this value) 
     16 * Improved exception handling in the PDH collector (hopefully less deadlocks) 
    717 
    8182008-02-11 MickeM 
  • trunk/include/NSCHelper.cpp

    r9741fa5 rbe0202f  
    2323#include <assert.h> 
    2424#include <msvc_wrappers.h> 
    25  
    26 #define BUFF_LEN 4096 
    27  
     25#include <config.h> 
    2826 
    2927#ifdef DEBUG 
     
    141139  lpNSAPIReleaseAllCommandNamessBuffer fNSAPIReleaseAllCommandNamessBuffer= NULL; 
    142140  lpNSAPIRegisterCommand fNSAPIRegisterCommand= NULL; 
     141  unsigned int buffer_length; 
    143142 
    144143} 
     
    208207  if (!fNSAPIInject) 
    209208    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    210   TCHAR *msgBuffer = new TCHAR[BUFF_LEN+1]; 
    211   TCHAR *perfBuffer = new TCHAR[BUFF_LEN+1]; 
     209  unsigned int buf_len = getBufferLength(); 
     210  TCHAR *msgBuffer = new TCHAR[buf_len+1]; 
     211  TCHAR *perfBuffer = new TCHAR[buf_len+1]; 
    212212  msgBuffer[0] = 0; 
    213213  perfBuffer[0] = 0; 
    214   // @todo message here ! 
    215   NSCAPI::nagiosReturn retC = InjectCommandRAW(command, argLen, argument, msgBuffer, BUFF_LEN, perfBuffer, BUFF_LEN); 
     214  NSCAPI::nagiosReturn retC = InjectCommandRAW(command, argLen, argument, msgBuffer, buf_len, perfBuffer, buf_len); 
    216215  switch (retC) { 
    217216    case NSCAPI::returnIgnored: 
     
    219218      break; 
    220219    case NSCAPI::returnInvalidBufferLen: 
    221       NSC_LOG_ERROR(_T("Inject command resulted in an invalid buffer size.")); 
     220      NSC_LOG_ERROR(_T("Inject buffer to small, increase the value of: string_length.")); 
    222221      break; 
    223222    case NSCAPI::returnOK: 
     
    303302  if (!fNSAPIGetSettingsString) 
    304303    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    305   TCHAR *buffer = new TCHAR[BUFF_LEN+1]; 
    306   if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN) != NSCAPI::isSuccess) { 
     304  unsigned int buf_len = getBufferLength(); 
     305  TCHAR *buffer = new TCHAR[buf_len+1]; 
     306  if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, buf_len) != NSCAPI::isSuccess) { 
    307307    delete [] buffer; 
    308308    throw NSCMHExcpetion(_T("Settings could not be retrieved.")); 
     
    374374  if (!fNSAPIGetApplicationName) 
    375375    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    376   TCHAR *buffer = new TCHAR[BUFF_LEN+1]; 
    377   if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::isSuccess) { 
     376  unsigned int buf_len = getBufferLength(); 
     377  TCHAR *buffer = new TCHAR[buf_len+1]; 
     378  if (fNSAPIGetApplicationName(buffer, buf_len) != NSCAPI::isSuccess) { 
    378379    delete [] buffer; 
    379380    throw NSCMHExcpetion(_T("Application name could not be retrieved")); 
     
    391392  if (!fNSAPIGetBasePath) 
    392393    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    393   TCHAR *buffer = new TCHAR[BUFF_LEN+1]; 
    394   if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::isSuccess) { 
     394  unsigned int buf_len = getBufferLength(); 
     395  TCHAR *buffer = new TCHAR[buf_len+1]; 
     396  if (fNSAPIGetBasePath(buffer, buf_len) != NSCAPI::isSuccess) { 
    395397    delete [] buffer; 
    396398    throw NSCMHExcpetion(_T("Base path could not be retrieved")); 
     
    399401  delete [] buffer; 
    400402  return ret; 
     403} 
     404 
     405unsigned int NSCModuleHelper::getBufferLength() { 
     406  static unsigned int len = 0; 
     407  if (len == 0) { 
     408    len = getSettingsInt(MAIN_SECTION_TITLE, MAIN_STRING_LENGTH, MAIN_STRING_LENGTH_DEFAULT); 
     409  } 
     410  return len; 
    401411} 
    402412 
     
    490500  if (!fNSAPIDescribeCommand) 
    491501    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    492   TCHAR *buffer = new TCHAR[BUFF_LEN+1]; 
    493   if (fNSAPIDescribeCommand(command.c_str(), buffer, BUFF_LEN) != NSCAPI::isSuccess) { 
     502  unsigned int buf_len = getBufferLength(); 
     503  TCHAR *buffer = new TCHAR[buf_len+1]; 
     504  if (fNSAPIDescribeCommand(command.c_str(), buffer, buf_len) != NSCAPI::isSuccess) { 
    494505    delete [] buffer; 
    495506    throw NSCMHExcpetion(_T("Base path could not be retrieved")); 
     
    519530  if (!fNSAPIGetApplicationVersionStr) 
    520531    throw NSCMHExcpetion(_T("NSCore has not been initiated...")); 
    521   TCHAR *buffer = new TCHAR[BUFF_LEN+1]; 
    522   int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN); 
     532  unsigned int buf_len = getBufferLength(); 
     533  TCHAR *buffer = new TCHAR[buf_len+1]; 
     534  if (fNSAPIGetApplicationVersionStr(buffer, buf_len) != NSCAPI::isSuccess) { 
     535    delete [] buffer; 
     536    return _T(""); 
     537  } 
    523538  std::wstring ret = buffer; 
    524539  delete [] buffer; 
  • trunk/include/NSCHelper.h

    r9741fa5 rbe0202f  
    149149  std::wstring describeCommand(std::wstring command); 
    150150  void registerCommand(std::wstring command, std::wstring description); 
     151  unsigned int getBufferLength(); 
    151152}; 
    152153 
  • trunk/include/config.h

    r9741fa5 rbe0202f  
    104104#define NRPE_SETTINGS_SCRIPTDIR _T("script_dir") 
    105105#define NRPE_SETTINGS_SCRIPTDIR_DEFAULT _T("") 
     106#define NRPE_SETTINGS_STRLEN _T("string_length") 
     107#define NRPE_SETTINGS_STRLEN_DEFAULT 1024 
    106108 
    107109// External Script Settings headlines 
     
    193195#define MAIN_ALLOWED_HOSTS_CACHE _T("cache_allowed_hosts") 
    194196#define MAIN_ALLOWED_HOSTS_CACHE_DEFAULT 1 
     197#define MAIN_STRING_LENGTH _T("string_length") 
     198#define MAIN_STRING_LENGTH_DEFAULT 4096 
    195199 
    196200 
  • trunk/modules/CheckSystem/PDHCollector.cpp

    r9741fa5 rbe0202f  
    179179    bool first = true; 
    180180    do { 
    181       MutexLock mutex(mutexHandler); 
    182       if (!mutex.hasMutex())  
    183         NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    184       else { 
    185         try { 
    186           pdh.gatherData(); 
    187         } catch (const PDH::PDHException &e) { 
    188           if (first) {  // If this is the first run an error will be thrown since the data is not yet avalible 
    189             // This is "ok" but perhaps another solution would be better, but this works :) 
    190             first = false; 
    191           } else { 
    192             NSC_LOG_ERROR_STD(_T("Failed to query performance counters: ") + e.getError()); 
     181      std::list<std::wstring> errors; 
     182      { 
     183        MutexLock mutex(mutexHandler); 
     184        if (!mutex.hasMutex())  
     185          NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
     186        else { 
     187          try { 
     188            pdh.gatherData(); 
     189          } catch (const PDH::PDHException &e) { 
     190            if (first) {  // If this is the first run an error will be thrown since the data is not yet avalible 
     191              // This is "ok" but perhaps another solution would be better, but this works :) 
     192              first = false; 
     193            } else { 
     194              errors.push_back(_T("Failed to query performance counters: ") + e.getError()); 
     195            } 
    193196          } 
    194         } 
    195       }  
     197        }  
     198      } 
     199      for (std::list<std::wstring>::const_iterator cit = errors.begin(); cit != errors.end(); ++cit) { 
     200        NSC_LOG_ERROR_STD(*cit); 
     201      } 
    196202    } while (((waitStatus = WaitForSingleObject(hStopEvent_, checkIntervall_*100)) == WAIT_TIMEOUT)); 
    197203  } else { 
  • trunk/modules/NRPEListener/NRPEListener.cpp

    r442eaf2 rbe0202f  
    3535} 
    3636 
    37 NRPEListener::NRPEListener() : noPerfData_(false) { 
     37NRPEListener::NRPEListener() : noPerfData_(false), buffer_length_(0) { 
    3838} 
    3939NRPEListener::~NRPEListener() { 
     
    8181  socketTimeout_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_READ_TIMEOUT ,NRPE_SETTINGS_READ_TIMEOUT_DEFAULT); 
    8282  scriptDirectory_ = NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, NRPE_SETTINGS_SCRIPTDIR ,NRPE_SETTINGS_SCRIPTDIR_DEFAULT); 
     83  buffer_length_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_STRLEN, NRPE_SETTINGS_STRLEN_DEFAULT); 
    8384  std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(NRPE_HANDLER_SECTION_TITLE); 
    8485  std::list<std::wstring>::const_iterator it; 
     
    238239 
    239240} 
    240 #define MAX_INPUT_BUFFER 1024 
    241  
    242241int NRPEListener::executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf) 
    243242{ 
     
    302301    } else { 
    303302      DWORD dwread; 
    304       //TCHAR *buf = new TCHAR[MAX_INPUT_BUFFER+1]; 
    305       char *buf = new char[MAX_INPUT_BUFFER+1]; 
    306       //retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(WCHAR), &dwread, NULL); 
    307       retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(char), &dwread, NULL); 
    308       if (!retval || dwread == 0) { 
     303      std::string str; 
     304#define BUFF_SIZE 4096 
     305      char *buf = new char[BUFF_SIZE+1]; 
     306      do { 
     307        retval = ReadFile(hChildOutR, buf, BUFF_SIZE, &dwread, NULL); 
     308        if (retval == 0) 
     309          break; 
     310        if (dwread > BUFF_SIZE) 
     311          break; 
     312        buf[dwread] = 0; 
     313        str += buf; 
     314      } while (dwread == BUFF_SIZE); 
     315      delete [] buf; 
     316      if (str.empty()) { 
    309317        msg = _T("No output available from command..."); 
    310318      } else { 
    311         buf[dwread] = 0; 
    312         msg = strEx::string_to_wstring(buf); 
    313         //msg = buf; 
    314         //strEx::token t = strEx::getToken(msg, '\n'); 
     319        msg = strEx::string_to_wstring(str); 
    315320        strEx::token t = strEx::getToken(msg, '|'); 
    316321        msg = t.first; 
     
    322327            msg = msg.substr(0,pos+1); 
    323328        } 
    324         //if (msg[msg.size()-1] == '\n') 
    325329        perf = t.second; 
    326330      } 
    327       delete [] buf; 
    328331      if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { 
    329332        NSC_LOG_ERROR(_T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error()); 
     
    375378        return; 
    376379      } 
    377       if (block.getLength() >= NRPEPacket::getBufferLength()) 
     380      if (block.getLength() >= NRPEPacket::getBufferLength(buffer_length_)) 
    378381        break; 
    379382      if (!lastReadHasMore) { 
     
    388391      return; 
    389392    } 
    390     if (block.getLength() == NRPEPacket::getBufferLength()) { 
     393    if (block.getLength() == NRPEPacket::getBufferLength(buffer_length_)) { 
    391394      try { 
    392         NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength())); 
     395        NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength(), buffer_length_)); 
    393396        block.copyFrom(out.getBuffer(), out.getBufferLength()); 
    394397      } catch (NRPEPacket::NRPEPacketException e) { 
     
    422425  strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 
    423426  if (cmd.first == _T("_NRPE_CHECK")) { 
    424     return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") SZVERSION _T(") seem to be doing fine...")); 
     427    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") SZVERSION _T(") seem to be doing fine..."), buffer_length_); 
    425428  } 
    426429  std::wstring msg, perf; 
     
    447450    ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 
    448451  } catch (...) { 
    449     return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception")); 
     452    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception"), buffer_length_); 
    450453  } 
    451454  switch (ret) { 
     
    467470      ret = NSCAPI::returnUNKNOWN; 
    468471  } 
     472  if (msg.length() > buffer_length_) { 
     473    NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 
     474    msg = msg.substr(0,buffer_length_-1); 
     475  } 
    469476  if (perf.empty()||noPerfData_) { 
    470     return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); 
     477    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg, buffer_length_); 
    471478  } else { 
    472     return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf); 
     479    return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf, buffer_length_); 
    473480  } 
    474481} 
  • trunk/modules/NRPEListener/NRPEListener.h

    raeadfda rbe0202f  
    4747  bool noPerfData_; 
    4848  std::wstring scriptDirectory_; 
     49  unsigned int buffer_length_; 
    4950 
    5051public: 
  • trunk/modules/NRPEListener/NRPEPacket.cpp

    raeadfda rbe0202f  
    2626const char* NRPEPacket::getBuffer() { 
    2727  delete [] tmpBuffer; 
    28   tmpBuffer = new char[getBufferLength()]; 
     28  tmpBuffer = new char[getBufferLength()+1]; 
    2929  packet *p = reinterpret_cast<packet*>(tmpBuffer); 
    3030  p->result_code = htons(NSCHelper::nagios2int(result_)); 
     
    3232  p->packet_version = htons(version_); 
    3333  p->crc32_value = 0; 
    34   strncpy_s(p->buffer, 1024, strEx::wstring_to_string(payload_).c_str(), 1023); 
    35   p->buffer[1024] = 0; 
     34  if (payload_.length() >= buffer_length_) 
     35    throw NRPEPacketException(_T("To much data cant create return packet (truncate datat)")); 
     36  strncpy_s(p->buffer, buffer_length_, strEx::wstring_to_string(payload_).c_str(), buffer_length_); 
     37  p->buffer[buffer_length_] = 0; 
    3638  p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 
    3739  return tmpBuffer; 
     
    4143  if (buffer == NULL) 
    4244    throw NRPEPacketException(_T("No buffer.")); 
    43   if (length != sizeof(packet)) 
     45  if (length != getBufferLength()) 
    4446    throw NRPEPacketException(_T("Invalid length.")); 
    4547  const packet *p = reinterpret_cast<const packet*>(buffer); 
     
    5355  // Verify CRC32 
    5456  // @todo Fix this, currently we need a const buffer so we cannot change the CRC to 0. 
    55   char * tb = new char[getBufferLength()]; 
     57  char * tb = new char[getBufferLength()+1]; 
    5658  memcpy(tb, buffer, getBufferLength()); 
    5759  packet *p2 = reinterpret_cast<packet*>(tb); 
    5860  p2->crc32_value = 0; 
    5961  calculatedCRC32_ = calculate_crc32(tb, getBufferLength()); 
     62  p2->buffer[buffer_length_] = 0; 
    6063  delete [] tb; 
    6164  // Verify CRC32 end 
  • trunk/modules/NRPEListener/NRPEPacket.h

    raeadfda rbe0202f  
    4646    u_int32_t crc32_value; 
    4747    int16_t   result_code; 
    48     char      buffer[1024]; 
     48    char      buffer[]; 
    4949  } packet; 
    5050  std::wstring payload_; 
     
    5555  unsigned int calculatedCRC32_; 
    5656  char *tmpBuffer; 
     57  unsigned int buffer_length_; 
    5758public: 
    58   NRPEPacket() : tmpBuffer(NULL) {}; 
    59   NRPEPacket(const char *buffer, unsigned int length) : tmpBuffer(NULL) { 
     59  NRPEPacket(unsigned int buffer_length) : tmpBuffer(NULL), buffer_length_(buffer_length) {}; 
     60  NRPEPacket(const char *buffer, unsigned int length, unsigned int buffer_length) : tmpBuffer(NULL), buffer_length_(buffer_length) { 
    6061    readFrom(buffer, length); 
    6162  }; 
    62   NRPEPacket(short type, short version, NSCAPI::nagiosReturn result, std::wstring payLoad)  
     63  NRPEPacket(short type, short version, NSCAPI::nagiosReturn result, std::wstring payLoad, unsigned int buffer_length)  
    6364    : tmpBuffer(NULL)  
    6465    ,type_(type) 
     
    6667    ,result_(result) 
    6768    ,payload_(payLoad) 
     69    ,buffer_length_(buffer_length) 
    6870  { 
    6971  } 
     
    7577    crc32_ = other.crc32_; 
    7678    calculatedCRC32_ = other.calculatedCRC32_; 
     79    buffer_length_ = other.buffer_length_; 
    7780  } 
    7881  ~NRPEPacket() { 
     
    8891    return calculatedCRC32_ == crc32_; 
    8992  } 
    90   static unsigned int getBufferLength() { 
    91     return sizeof(packet); 
     93  unsigned int getBufferLength() const { 
     94    return getBufferLength(buffer_length_); 
     95  } 
     96  static unsigned int getBufferLength(unsigned int buffer_length) { 
     97    return sizeof(packet)+buffer_length*sizeof(char); 
    9298  } 
    9399}; 
Note: See TracChangeset for help on using the changeset viewer.