Changeset be0202f in nscp
- Timestamp:
- 02/13/08 22:43:47 (5 years ago)
- Children:
- fc7cdcf
- Parents:
- 9da54aa
- Location:
- trunk
- Files:
-
- 13 edited
-
AutoBuild.h (modified) (1 diff)
-
NSClient++-2005.vcproj (modified) (1 diff)
-
NSClient++.cpp (modified) (7 diffs)
-
NSClient++.h (modified) (1 diff)
-
changelog (modified) (1 diff)
-
include/NSCHelper.cpp (modified) (10 diffs)
-
include/NSCHelper.h (modified) (1 diff)
-
include/config.h (modified) (2 diffs)
-
modules/CheckSystem/PDHCollector.cpp (modified) (1 diff)
-
modules/NRPEListener/NRPEListener.cpp (modified) (10 diffs)
-
modules/NRPEListener/NRPEListener.h (modified) (1 diff)
-
modules/NRPEListener/NRPEPacket.cpp (modified) (4 diffs)
-
modules/NRPEListener/NRPEPacket.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/AutoBuild.h
r9da54aa rbe0202f 3 3 // change the FALSE to TRUE for autoincrement of build number 4 4 #define INCREMENT_VERSION TRUE 5 #define FILEVER 0,3,0,5 06 #define PRODUCTVER 0,3,0,5 07 #define STRFILEVER _T("0.3.0.5 0")8 #define STRPRODUCTVER _T("0.3.0.5 0")9 #define STRPRODUCTDATE _T("2008-02-1 1")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") 10 10 #endif // AUTOBUILD_H -
trunk/NSClient++-2005.vcproj
r5ee4e31 rbe0202f 3123 3123 > 3124 3124 </File> 3125 <File 3126 RelativePath=".\scripts\test.lua" 3127 > 3128 </File> 3125 3129 </Files> 3126 3130 <Globals> -
trunk/NSClient++.cpp
r9da54aa rbe0202f 446 446 } 447 447 448 unsigned 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 448 464 NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { 449 465 unsigned int aLen = 0; 450 466 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); 454 471 arrayBuffer::destroyArrayBuffer(aBuf, aLen); 455 472 if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { … … 599 616 if (!basePath.empty()) 600 617 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); 603 621 std::wstring path = buffer; 604 622 std::wstring::size_type pos = path.rfind('\\'); … … 615 633 616 634 NSCAPI::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 } 618 641 } 619 642 int NSAPIGetSettingsInt(const TCHAR* section, const TCHAR* key, int defaultValue) { … … 644 667 } 645 668 NSCAPI::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 } 650 678 } 651 679 NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(TCHAR*** aBuffer, unsigned int * bufLen) { … … 770 798 771 799 NSCAPI::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 } 773 806 return NSCAPI::isSuccess; 774 807 } 775 808 NSCAPI::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 } 777 815 return NSCAPI::isSuccess; 778 816 } … … 786 824 Settings::getInstance()->write(); 787 825 } 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")); 789 830 return NSCAPI::hasFailed; 790 831 } … … 800 841 Settings::getInstance()->read(); 801 842 } 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")); 803 847 return NSCAPI::hasFailed; 804 848 } -
trunk/NSClient++.h
r442eaf2 rbe0202f 102 102 std::list<std::wstring> getAllCommandNames(); 103 103 void registerCommand(std::wstring cmd, std::wstring desc); 104 unsigned int getBufferLength(); 104 105 105 106 bool logDebug(); -
trunk/changelog
r9da54aa rbe0202f 5 5 * Add module for relaying events 6 6 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 7 8 2008-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 13 2008-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) 7 17 8 18 2008-02-11 MickeM -
trunk/include/NSCHelper.cpp
r9741fa5 rbe0202f 23 23 #include <assert.h> 24 24 #include <msvc_wrappers.h> 25 26 #define BUFF_LEN 4096 27 25 #include <config.h> 28 26 29 27 #ifdef DEBUG … … 141 139 lpNSAPIReleaseAllCommandNamessBuffer fNSAPIReleaseAllCommandNamessBuffer= NULL; 142 140 lpNSAPIRegisterCommand fNSAPIRegisterCommand= NULL; 141 unsigned int buffer_length; 143 142 144 143 } … … 208 207 if (!fNSAPIInject) 209 208 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]; 212 212 msgBuffer[0] = 0; 213 213 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); 216 215 switch (retC) { 217 216 case NSCAPI::returnIgnored: … … 219 218 break; 220 219 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.")); 222 221 break; 223 222 case NSCAPI::returnOK: … … 303 302 if (!fNSAPIGetSettingsString) 304 303 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) { 307 307 delete [] buffer; 308 308 throw NSCMHExcpetion(_T("Settings could not be retrieved.")); … … 374 374 if (!fNSAPIGetApplicationName) 375 375 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) { 378 379 delete [] buffer; 379 380 throw NSCMHExcpetion(_T("Application name could not be retrieved")); … … 391 392 if (!fNSAPIGetBasePath) 392 393 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) { 395 397 delete [] buffer; 396 398 throw NSCMHExcpetion(_T("Base path could not be retrieved")); … … 399 401 delete [] buffer; 400 402 return ret; 403 } 404 405 unsigned 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; 401 411 } 402 412 … … 490 500 if (!fNSAPIDescribeCommand) 491 501 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) { 494 505 delete [] buffer; 495 506 throw NSCMHExcpetion(_T("Base path could not be retrieved")); … … 519 530 if (!fNSAPIGetApplicationVersionStr) 520 531 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 } 523 538 std::wstring ret = buffer; 524 539 delete [] buffer; -
trunk/include/NSCHelper.h
r9741fa5 rbe0202f 149 149 std::wstring describeCommand(std::wstring command); 150 150 void registerCommand(std::wstring command, std::wstring description); 151 unsigned int getBufferLength(); 151 152 }; 152 153 -
trunk/include/config.h
r9741fa5 rbe0202f 104 104 #define NRPE_SETTINGS_SCRIPTDIR _T("script_dir") 105 105 #define NRPE_SETTINGS_SCRIPTDIR_DEFAULT _T("") 106 #define NRPE_SETTINGS_STRLEN _T("string_length") 107 #define NRPE_SETTINGS_STRLEN_DEFAULT 1024 106 108 107 109 // External Script Settings headlines … … 193 195 #define MAIN_ALLOWED_HOSTS_CACHE _T("cache_allowed_hosts") 194 196 #define MAIN_ALLOWED_HOSTS_CACHE_DEFAULT 1 197 #define MAIN_STRING_LENGTH _T("string_length") 198 #define MAIN_STRING_LENGTH_DEFAULT 4096 195 199 196 200 -
trunk/modules/CheckSystem/PDHCollector.cpp
r9741fa5 rbe0202f 179 179 bool first = true; 180 180 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 } 193 196 } 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 } 196 202 } while (((waitStatus = WaitForSingleObject(hStopEvent_, checkIntervall_*100)) == WAIT_TIMEOUT)); 197 203 } else { -
trunk/modules/NRPEListener/NRPEListener.cpp
r442eaf2 rbe0202f 35 35 } 36 36 37 NRPEListener::NRPEListener() : noPerfData_(false) {37 NRPEListener::NRPEListener() : noPerfData_(false), buffer_length_(0) { 38 38 } 39 39 NRPEListener::~NRPEListener() { … … 81 81 socketTimeout_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_READ_TIMEOUT ,NRPE_SETTINGS_READ_TIMEOUT_DEFAULT); 82 82 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); 83 84 std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(NRPE_HANDLER_SECTION_TITLE); 84 85 std::list<std::wstring>::const_iterator it; … … 238 239 239 240 } 240 #define MAX_INPUT_BUFFER 1024241 242 241 int NRPEListener::executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf) 243 242 { … … 302 301 } else { 303 302 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()) { 309 317 msg = _T("No output available from command..."); 310 318 } 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); 315 320 strEx::token t = strEx::getToken(msg, '|'); 316 321 msg = t.first; … … 322 327 msg = msg.substr(0,pos+1); 323 328 } 324 //if (msg[msg.size()-1] == '\n')325 329 perf = t.second; 326 330 } 327 delete [] buf;328 331 if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { 329 332 NSC_LOG_ERROR(_T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error()); … … 375 378 return; 376 379 } 377 if (block.getLength() >= NRPEPacket::getBufferLength( ))380 if (block.getLength() >= NRPEPacket::getBufferLength(buffer_length_)) 378 381 break; 379 382 if (!lastReadHasMore) { … … 388 391 return; 389 392 } 390 if (block.getLength() == NRPEPacket::getBufferLength( )) {393 if (block.getLength() == NRPEPacket::getBufferLength(buffer_length_)) { 391 394 try { 392 NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength() ));395 NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength(), buffer_length_)); 393 396 block.copyFrom(out.getBuffer(), out.getBufferLength()); 394 397 } catch (NRPEPacket::NRPEPacketException e) { … … 422 425 strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 423 426 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_); 425 428 } 426 429 std::wstring msg, perf; … … 447 450 ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 448 451 } 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_); 450 453 } 451 454 switch (ret) { … … 467 470 ret = NSCAPI::returnUNKNOWN; 468 471 } 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 } 469 476 if (perf.empty()||noPerfData_) { 470 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg );477 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg, buffer_length_); 471 478 } 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_); 473 480 } 474 481 } -
trunk/modules/NRPEListener/NRPEListener.h
raeadfda rbe0202f 47 47 bool noPerfData_; 48 48 std::wstring scriptDirectory_; 49 unsigned int buffer_length_; 49 50 50 51 public: -
trunk/modules/NRPEListener/NRPEPacket.cpp
raeadfda rbe0202f 26 26 const char* NRPEPacket::getBuffer() { 27 27 delete [] tmpBuffer; 28 tmpBuffer = new char[getBufferLength() ];28 tmpBuffer = new char[getBufferLength()+1]; 29 29 packet *p = reinterpret_cast<packet*>(tmpBuffer); 30 30 p->result_code = htons(NSCHelper::nagios2int(result_)); … … 32 32 p->packet_version = htons(version_); 33 33 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; 36 38 p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 37 39 return tmpBuffer; … … 41 43 if (buffer == NULL) 42 44 throw NRPEPacketException(_T("No buffer.")); 43 if (length != sizeof(packet))45 if (length != getBufferLength()) 44 46 throw NRPEPacketException(_T("Invalid length.")); 45 47 const packet *p = reinterpret_cast<const packet*>(buffer); … … 53 55 // Verify CRC32 54 56 // @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]; 56 58 memcpy(tb, buffer, getBufferLength()); 57 59 packet *p2 = reinterpret_cast<packet*>(tb); 58 60 p2->crc32_value = 0; 59 61 calculatedCRC32_ = calculate_crc32(tb, getBufferLength()); 62 p2->buffer[buffer_length_] = 0; 60 63 delete [] tb; 61 64 // Verify CRC32 end -
trunk/modules/NRPEListener/NRPEPacket.h
raeadfda rbe0202f 46 46 u_int32_t crc32_value; 47 47 int16_t result_code; 48 char buffer[ 1024];48 char buffer[]; 49 49 } packet; 50 50 std::wstring payload_; … … 55 55 unsigned int calculatedCRC32_; 56 56 char *tmpBuffer; 57 unsigned int buffer_length_; 57 58 public: 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) { 60 61 readFrom(buffer, length); 61 62 }; 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) 63 64 : tmpBuffer(NULL) 64 65 ,type_(type) … … 66 67 ,result_(result) 67 68 ,payload_(payLoad) 69 ,buffer_length_(buffer_length) 68 70 { 69 71 } … … 75 77 crc32_ = other.crc32_; 76 78 calculatedCRC32_ = other.calculatedCRC32_; 79 buffer_length_ = other.buffer_length_; 77 80 } 78 81 ~NRPEPacket() { … … 88 91 return calculatedCRC32_ == crc32_; 89 92 } 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); 92 98 } 93 99 };
Note: See TracChangeset
for help on using the changeset viewer.








