Nmap Development mailing list archives

Re: [nmap-svn] r27929 - in nmap-exp/patrick/nse-lua52: . nselib


From: Patrick Donnelly <batrick () batbytes com>
Date: Fri, 3 Feb 2012 17:16:54 -0500

On Mon, Jan 30, 2012 at 5:35 PM, David Fifield <david () bamsoftware com> wrote:
On Thu, Jan 26, 2012 at 11:06:35PM -0800, commit-mailer () insecure org wrote:
Author: batrick
Date: Thu Jan 26 23:06:34 2012
New Revision: 27929

Log:
Remove stdnse.c submodule for binding nsock_sleep. The function is now available
in the nmap.socket module. We provide an alias in stdnse.lua for compatibility.

Why nmap.socket.sleep? What does sleeping have to do with sockets?

Well, nothing.

The reason I moved the function into the socket library is because it
uses the nsock pool upvalue. It is therefore difficult to export the
function outside of the module cleanly.

The reason the nsock pool is an upvalue is mostly due to ideological
concerns. There were 3 strategies for handling library data in Lua
5.1:

1) Putting data in the Lua Registry (LUA_REGISTRYINDEX pseudo-index)
2) Putting data in the library environment (LUA_ENVIRONINDEX
pseudo-index). (most common)
3) Putting data in the library closure upvalues (rarely used).

In Lua 5.2, function environments are removed. Instead, everything is
an upvalue or local ( (_ENV is a main chunk's first and only upvalue).
The new system pushes for an upvalue based design where library data
is stored in the upvalues for all library closures. See for example
lua_setfuncs [1].

As part of the upgrade to Lua 5.2, I decided to uniformly change our
libraries to use only upvalues for library data. This includes things
like userdata metatables and the nsock pool (previously stored in the
Lua registry) and the various caching mechanisms in nsock and dnet
(previously stored in the LUA_ENVIRONINDEX).

One drawback is that functions like the sleep function and the loop
function must be accessed as a regular Lua closure, not as a C
function. So another library such as stdnse.c cannot create a sleep
function closure because it does not have access to the nsock pool
upvalue. I argue this is a better design as it reduces coupling
between our libraries by localizing data access. [Nothing stops us
from having aliases though.]

It's also worth noting that for example that the luasocket library
also has a sleep function but it is just a binding to sleep AFAIK.

[1] http://www.lua.org/manual/5.2/manual.html#luaL_setfuncs

-- 
- Patrick Donnelly
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


Current thread: