From 964006b223eec621df15378b4a1be87c7e0d3baa Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 29 Aug 2014 16:34:56 +0000 Subject: [PATCH] Adjust default timeout for rpc.lua Reported: http://seclists.org/nmap-dev/2014/q3/378 Changes the default timeout for rpc.lua from 30s to 5 times Nmap's calculated host timeout or 10s if no timeout info is available. --- nselib/rpc.lua | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/nselib/rpc.lua b/nselib/rpc.lua index 284a134ea..5288d664f 100644 --- a/nselib/rpc.lua +++ b/nselib/rpc.lua @@ -150,9 +150,10 @@ Comm = { -- -- @param host table -- @param port table + -- @param timeout [optional] socket timeout in ms -- @return status boolean true on success, false on failure -- @return string containing error message (if status is false) - Connect = function(self, host, port) + Connect = function(self, host, port, timeout) local status, err, socket status, err = self:ChkProgram() if (not(status)) then @@ -162,12 +163,23 @@ Comm = { if (not(status)) then return status, err end + timeout = timeout or ( + -- Use host timeout value * 5 if available + (type(host) == "table" and host.times and host.times.timeout * 5) + or 10 -- default 10 seconds + ) * 1000 -- convert to ms + stdnse.debug1("Timeout: %d", timeout) + local new_socket = function(...) + local socket = nmap.new_socket(...) + socket:set_timeout(timeout) + return socket + end if ( port.protocol == "tcp" ) then if nmap.is_privileged() then -- Try to bind to a reserved port for i = 1, 10, 1 do local resvport = math.random(1, 1024) - socket = nmap.new_socket() + socket = new_socket() status, err = socket:bind(nil, resvport) if status then status, err = socket:connect(host, port) @@ -176,7 +188,7 @@ Comm = { end end else - socket = nmap.new_socket() + socket = new_socket() status, err = socket:connect(host, port) end else @@ -184,7 +196,7 @@ Comm = { -- Try to bind to a reserved port for i = 1, 10, 1 do local resvport = math.random(1, 1024) - socket = nmap.new_socket("udp") + socket = new_socket("udp") status, err = socket:bind(nil, resvport) if status then status, err = socket:connect(host, port) @@ -193,7 +205,7 @@ Comm = { end end else - socket = nmap.new_socket("udp") + socket = new_socket("udp") status, err = socket:connect(host, port) end end