From a26f606ecd493a247f25e50d1949ae345d9e83c1 Mon Sep 17 00:00:00 2001 From: batrick Date: Tue, 24 May 2011 02:48:39 +0000 Subject: [PATCH] Just use srand on NSE startup in nse_main.cc. This removes the need for a binding which will probably not be useful for script writers when they have OpenSSL's PRNG already available. --- nse_main.cc | 8 ++++++++ nse_main.lua | 6 ------ nse_nmaplib.cc | 8 -------- nselib/nmap.luadoc | 11 ----------- 4 files changed, 8 insertions(+), 25 deletions(-) diff --git a/nse_main.cc b/nse_main.cc index 101c0ecdb..e06f77bbb 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -1,4 +1,5 @@ #include "nmap.h" +#include "nbase.h" #include "nmap_error.h" #include "portlist.h" #include "nsock.h" @@ -613,6 +614,13 @@ void script_scan (std::vector &targets, stype scantype) assert(L_NSE != NULL); lua_settop(L_NSE, 0); /* clear the stack */ + /* + Set the random seed value on behalf of scripts. Since Lua uses the C rand + and srand functions, which have a static seed for the entire program, we + don't want scripts doing this themselves. + */ + srand(get_random_uint()); + #if 0 /* Lua 5.2 */ lua_pushcfunction(L_NSE, run_main); diff --git a/nse_main.lua b/nse_main.lua index 1b0da269b..421ebfc2b 100644 --- a/nse_main.lua +++ b/nse_main.lua @@ -95,7 +95,6 @@ local open = io.open; local math = require "math"; local max = math.max; -local randomseed = math.randomseed; local package = require "package"; @@ -124,11 +123,6 @@ do -- Append the nselib directory to the Lua search path package.path = path.."?.lua;"..package.path; end --- Set the math.randomseed value in nse_main.lua on behalf of scripts. --- Since Lua uses the C rand and srand functions, which have a static --- seed for the entire program, we don't want scripts doing this themselves. -math.randomseed(nmap.get_random_uint()); - local script_database_type, script_database_path = cnse.fetchfile_absolute(cnse.script_dbpath); local script_database_update = cnse.scriptupdatedb; diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc index cf0be7432..31333845b 100644 --- a/nse_nmaplib.cc +++ b/nse_nmaplib.cc @@ -8,7 +8,6 @@ extern "C" { #include #include "nmap.h" -#include "nbase.h" #include "nmap_error.h" #include "NmapOps.h" #include "Target.h" @@ -754,12 +753,6 @@ static int l_get_interface (lua_State *L) return 1; } -static int l_get_random_uint (lua_State *L) -{ - lua_pushnumber(L, (lua_Number) get_random_uint()); - return 1; -} - int luaopen_nmap (lua_State *L) { static const luaL_reg nmaplib [] = { @@ -787,7 +780,6 @@ int luaopen_nmap (lua_State *L) {"address_family", l_address_family}, {"get_interface", l_get_interface}, {"get_interface_info", l_dnet_get_interface_info}, - {"get_random_uint", l_get_random_uint}, {NULL, NULL} }; diff --git a/nselib/nmap.luadoc b/nselib/nmap.luadoc index 72c4f978c..487bdd95c 100644 --- a/nselib/nmap.luadoc +++ b/nselib/nmap.luadoc @@ -120,17 +120,6 @@ function get_interface_info(interface_name) -- @return String representing the full path to the file or nil. function fetchfile(filename) - ---- Generates a random unsigned integer. --- --- This function is generally useful for seeding a random number generator. --- It is not efficient enough to be used as a random number generator. --- --- @usage --- nmap.get_rand_uint() --> 1234567890 --- @return Number A random unsigned integer. -function get_random_uint() - --- Returns the timing level as a non-negative integer. -- -- Possible return values vary from 0 to 5,