1
0
mirror of https://github.com/nmap/nmap.git synced 2026-02-02 11:39:03 +00:00

New convenience function stdnse.get_timeout

This commit is contained in:
dmiller
2014-09-02 18:23:09 +00:00
parent 2692746c42
commit c615bee688
3 changed files with 40 additions and 10 deletions

View File

@@ -10,7 +10,7 @@
-- * <code>bytes</code> - minimum number of bytes to read.
-- * <code>lines</code> - minimum number of lines to read.
-- * <code>proto</code> - string, protocol to use. Default <code>"tcp"</code>
-- * <code>timeout</code> - socket timeout in milliseconds. Default 8000
-- * <code>timeout</code> - socket timeout in milliseconds. Default: same as <code>stdnse.get_timeout</code>
-- * <code>connect_timeout</code> - override timeout for connection
-- * <code>request_timeout</code> - override timeout for requests
-- * <code>recv_before</code> - boolean, receive data before sending first payload
@@ -169,7 +169,7 @@ end
-- request_timeout: specific timeout for requests
-- recv_before: receive data before sending first payload
--
-- Default timeout is set to 8000.
-- Default timeout is result of stdnse.get_timeout
--
-- @param host The destination host IP
-- @param port The destination host port
@@ -189,7 +189,7 @@ local function opencon(host, port, protocol, data, opts)
elseif opts and opts.timeout then
sd:set_timeout(opts.timeout)
else
sd:set_timeout(8000)
sd:set_timeout(stdnse.get_timeout(host))
end
local status = sd:connect(host, port, protocol)
@@ -205,7 +205,7 @@ local function opencon(host, port, protocol, data, opts)
elseif opts and opts.timeout then
sd:set_timeout(opts.timeout)
else
sd:set_timeout(8000)
sd:set_timeout(stdnse.get_timeout(host))
end
local response, early_resp;

View File

@@ -163,12 +163,7 @@ 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)
timeout = timeout or stdnse.get_timeout(host, 10000)
local new_socket = function(...)
local socket = nmap.new_socket(...)
socket:set_timeout(timeout)

View File

@@ -1363,5 +1363,40 @@ function contains(tab, item)
return false, nil
end
--- Returns a conservative timeout for a host
--
-- If the host parameter is a NSE host table with a <code>times.timeout</code>
-- attribute, then the return value is the host timeout scaled according to the
-- max_timeout. The scaling factor is defined by a linear formula such that
-- (max_timeout=8000, scale=2) and (max_timeout=1000, scale=1)
--
-- @param host The host object to base the timeout on. If this is anything but
-- a host table, the max_timeout is returned.
-- @param max_timeout The maximum timeout in milliseconds. This is the default
-- timeout used if there is no host.times.timeout. Default: 8000
-- @param min_timeout The minimum timeout in milliseconds that will be
-- returned. Default: 1000
-- @return The timeout in milliseconds, suitable for passing to set_timeout
-- @usage
-- assert(host.times.timeout == 1.3)
-- assert(get_timeout() == 8000)
-- assert(get_timeout(nil, 5000) == 5000)
-- assert(get_timeout(host) == 2600)
-- assert(get_timeout(host, 10000, 3000) == 3000)
function get_timeout(host, max_timeout, min_timeout)
max_timeout = max_timeout or 8000
local t = type(host) == "table" and host.times and host.times.timeout
if not t then
return max_timeout
end
t = t * (max_timeout + 6000) / 7
min_timeout = min_timeout or 1000
if t < min_timeout then
return min_timeout
elseif t > max_timeout then
return max_timeout
end
return t
end
return _ENV;