Skip to content

Commit

Permalink
Just use srand on NSE startup in nse_main.cc. This removes the need
Browse files Browse the repository at this point in the history
for a binding which will probably not be useful for script writers
when they have OpenSSL's PRNG already available.
  • Loading branch information
batrick committed May 24, 2011
1 parent a5cd01a commit a26f606
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 25 deletions.
8 changes: 8 additions & 0 deletions nse_main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "nmap.h"
#include "nbase.h"
#include "nmap_error.h"
#include "portlist.h"
#include "nsock.h"
Expand Down Expand Up @@ -613,6 +614,13 @@ void script_scan (std::vector<Target *> &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);
Expand Down
6 changes: 0 additions & 6 deletions nse_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ local open = io.open;

local math = require "math";
local max = math.max;
local randomseed = math.randomseed;

local package = require "package";

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions nse_nmaplib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ extern "C" {
#include <math.h>

#include "nmap.h"
#include "nbase.h"
#include "nmap_error.h"
#include "NmapOps.h"
#include "Target.h"
Expand Down Expand Up @@ -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 [] = {
Expand Down Expand Up @@ -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}
};

Expand Down
11 changes: 0 additions & 11 deletions nselib/nmap.luadoc
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ function get_interface_info(interface_name)
-- @return String representing the full path to the file or <code>nil</code>.
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 <code>0</code> to <code>5</code>,
Expand Down

0 comments on commit a26f606

Please sign in to comment.