1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-10 09:49:05 +00:00

Change rpc's GetAdditionalBytes to error on a short read without an extra timeout

This commit is contained in:
dmiller
2016-06-28 02:19:44 +00:00
parent 9e3c7d526d
commit bd99365851

View File

@@ -287,8 +287,11 @@ Comm = {
self.program_id = progid
end,
--- Checks if data contains enough bytes to read the <code>needed</code> amount
-- If it doesn't it attempts to read the remaining amount of bytes from the socket
--- Checks if <code>data</code> contains enough bytes to read the <code>needed</code> amount
--
-- If it doesn't it attempts to read the remaining amount of bytes from the
-- socket. Unlike <code>socket.receive_bytes</code>, reading less than
-- <code>needed</code> is treated as an error.
--
-- @param data string containing the current buffer
-- @param pos number containing the current offset into the buffer
@@ -296,16 +299,19 @@ Comm = {
-- @return status success or failure
-- @return data string containing the data passed to the function and the additional data appended to it or error message on failure
GetAdditionalBytes = function( self, data, pos, needed )
local status, tmp
if data:len() - pos + 1 < needed then
local toread = needed - ( data:len() - pos + 1 )
status, tmp = self.socket:receive_bytes( toread )
-- Do the loop ourselves instead of receive_bytes. Pathological case:
-- * read less than needed and timeout
-- * receive_bytes returns short but we don't know if it's eof or timeout
-- * Try again. If it was timeout, we've doubled the timeout waiting for bytes that aren't coming.
while toread > 0 do
local status, tmp = self.socket:receive()
if status then
toread = toread - #tmp
data = data .. tmp
else
return false, string.format("getAdditionalBytes() failed to read: %d bytes from the socket",
needed - ( data:len() - pos ) )
return false, string.format("getAdditionalBytes read %d bytes before error: %s",
needed - toread, tmp)
end
end
return true, data