From a173fe6ce11e04d83afa75e48d6790cbe4c4b8fa Mon Sep 17 00:00:00 2001 From: david Date: Mon, 23 Feb 2009 23:57:39 +0000 Subject: [PATCH] Add the stdnse.sleep function. --- nse_init.cc | 1 + nse_nmaplib.cc | 14 ++++++++++++++ nse_nmaplib.h | 1 + nse_nsock.cc | 19 +++++++++++++++++++ nse_nsock.h | 1 + nselib/stdnse.lua | 21 +++++++++++++++++++++ 6 files changed, 57 insertions(+) diff --git a/nse_init.cc b/nse_init.cc index 06778d24a..75be8c851 100644 --- a/nse_init.cc +++ b/nse_init.cc @@ -252,6 +252,7 @@ int init_lua (lua_State *L) #ifdef HAVE_OPENSSL {OPENSSLLIBNAME, luaopen_openssl}, // openssl bindings #endif + {"stdnse.c", luaopen_stdnse_c}, }; luaL_openlibs(L); // opens all standard libraries diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc index df425a0b8..a6902437a 100644 --- a/nse_nmaplib.cc +++ b/nse_nmaplib.cc @@ -562,3 +562,17 @@ int luaopen_nmap (lua_State *L) return 1; } + +/* Register C functions that belong in the stdnse namespace. They are loaded + from here in stdnse.lua. */ +int luaopen_stdnse_c (lua_State *L) +{ + static const luaL_reg stdnse_clib [] = { + {"sleep", l_nsock_sleep}, + {NULL, NULL} + }; + + luaL_register(L, "stdnse.c", stdnse_clib); + + return 1; +} diff --git a/nse_nmaplib.h b/nse_nmaplib.h index 661993d25..39d991630 100644 --- a/nse_nmaplib.h +++ b/nse_nmaplib.h @@ -11,6 +11,7 @@ class Target; class Port; int luaopen_nmap(lua_State* l); +int luaopen_stdnse_c (lua_State *L); void set_hostinfo(lua_State* l, Target* currenths); void set_portinfo(lua_State* l, Port* port); diff --git a/nse_nsock.cc b/nse_nsock.cc index ff4175397..7ab69a42c 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -950,6 +950,25 @@ void l_nsock_clear_buf(lua_State *L, l_nsock_udata* udata){ udata->bufused=0; } +static void l_nsock_sleep_handler(nsock_pool nsp, nsock_event nse, void *udata) { + lua_State *L = (lua_State*) udata; + assert(nse_status(nse) == NSE_STATUS_SUCCESS); + process_waiting2running(L, 0); +} + +int l_nsock_sleep(lua_State *L) { + double secs = luaL_checknumber(L, 1); + int msecs; + + if (secs < 0) + luaL_error(L, "Argument to sleep (%f) must not be negative\n", secs); + /* Convert to milliseconds for nsock_timer_create. */ + msecs = (int) (secs * 1000 + 0.5); + nsock_timer_create(nsp, l_nsock_sleep_handler, msecs, L); + + return lua_yield(L, 0); +} + /****************** NCAP_SOCKET ***********************************************/ #ifdef WIN32 /* From tcpip.cc. Gets pcap device name from dnet name. */ diff --git a/nse_nsock.h b/nse_nsock.h index fa67d2e69..7ab3718a9 100644 --- a/nse_nsock.h +++ b/nse_nsock.h @@ -10,6 +10,7 @@ extern "C" { int luaopen_nsock(lua_State *); int l_nsock_new(lua_State *); int l_nsock_loop(int tout); +int l_nsock_sleep(lua_State *L); int l_dnet_new(lua_State *); int l_dnet_open(lua_State *); diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 68bf264c3..0eb257eaf 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -17,10 +17,31 @@ local concat = table.concat; local nmap = require "nmap"; +local c_funcs = require "stdnse.c"; + local EMPTY = {}; -- Empty constant table module(... or "stdnse"); +-- Load C functions from stdnse.c into this namespace. +for k, v in pairs(c_funcs) do + _M[k] = v +end +-- Remove visibility of the stdnse.c table. +c = nil + +--- Sleeps for a given amount of time. +-- +-- This causes the program to yield control and not regain it until the time +-- period has elapsed. The time may have a fractional part. Internally, the +-- timer provides millisecond resolution. +-- @name sleep +-- @class function +-- @param t Time to sleep, in seconds. +-- @usage stdnse.sleep(1.5) + +-- sleep is a C function defined in nse_nmaplib.cc. + --- Prints a formatted debug message if the current verbosity level is greater -- than or equal to a given level. --