diff --git a/lib/nsca.rb b/lib/nsca.rb index a60ffc6..d3cb622 100644 --- a/lib/nsca.rb +++ b/lib/nsca.rb @@ -67,15 +67,18 @@ module NSCA end # Builds a null terminated, null padded string of length maxlen - def str2cstr( str, maxlen = nil) + def str2cstr str, maxlen = nil str = str.to_s str = str.to_s[0..(maxlen-2)] if maxlen "#{str}\x00" end - def str2nstr( str, maxlen = nil) - str = str.to_s.gsub( ' ', "\x00") + def rand_padding( str, maxlen) str + SecureRandom.random_bytes( maxlen - str.length) end + def str2cstr_rand_padding( str, maxlen = nil) rand_padding str2cstr( str, maxlen), maxlen end + def str2nstr str, maxlen = nil + str = str.to_s.gsub ' ', "\x00" "#{str} " end + def str2nstr_rand_padding( str, maxlen = nil) rand_padding str2nstr( str, maxlen), maxlen end def cstr2str( str, maxlen = nil) str[ 0, str.index( ?\0) || ((maxlen||str.length+1)-1)] end def nstr2str( str, maxlen = nil) str[ 0, str.index( ' ') || ((maxlen||str.length+1)-1)].gsub( "\x00", ' ') end end diff --git a/lib/nsca/packet.rb b/lib/nsca/packet.rb index 204863f..ccbbc87 100644 --- a/lib/nsca/packet.rb +++ b/lib/nsca/packet.rb @@ -60,9 +60,9 @@ module NSCA 0, # crc32 (unknown yet) (timestamp || Time.now).to_i, return_code.to_i, - NSCA::str2cstr( hostname || `hostname -f`, cl::HOSTNAME_LENGTH), - NSCA::str2cstr( service, cl::SERVICE_LENGTH), - NSCA::str2cstr( status, cl::PLUGIN_OUTPUT_LENGTH) # incl perfdata + NSCA::str2cstr_rand_padding( hostname || `hostname -f`, cl::HOSTNAME_LENGTH), + NSCA::str2cstr_rand_padding( service, cl::SERVICE_LENGTH), + NSCA::str2cstr_rand_padding( status, cl::PLUGIN_OUTPUT_LENGTH) # incl perfdata ] # generate crc32 and put it at entry[2...6] entry[1] = NSCA::crc32 entry.pack( cl::PACK_STRING)