Nmap Development mailing list archives

Re: [NSE][PATCH] datafiles.lua


From: Sven Klemm <sven () c3d2 de>
Date: Sat, 13 Sep 2008 09:23:03 +0200

Hi jah,

jah wrote:
So I've tested the code with your patch and fixed some bugs and a couple
of typo's, most of which were present from the start:
not returning services in the same fashion as parse_services(protocol) did;
bad logic prevented parse_file( filename, { } ) from returning an array
of lines where filename was one of Nmap's data files (as it does for
other files);
creating a table key with a value of nil when the pattern for the key
matches, but the pattern for the value doesn't - this was made most
obvious by the recent changes as it prevented them returning the correct
data and;
a few minor details.

thanks for testing and posting your opinion and your patch. I've applied both patches. I have one more patch for datafiles. Currently parse_rpc(), parse_protocols() and parse_services() don't really have any functionality on their own but just call parse_file() with the filename as argument. So I removed the common_files table and moved the parse instructions to those functions. This certainly makes parse_file() nicer as we don't have to check for common_files in parse_file() anymore.

Cheers,
Sven


--
Sven Klemm
http://cthulhu.c3d2.de/~sven/

Index: nselib/datafiles.lua
===================================================================
--- nselib/datafiles.lua        (revision 10129)
+++ nselib/datafiles.lua        (working copy)
@@ -10,20 +10,6 @@
 
 
 ---
--- Holds tables containing captures for common data files, indexed by filename.
--- @type table
--- @name common_files
-local common_files = {
-    ["nmap-rpc"]       = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)" ) ) end] = 
"^%s*([^%s#]+)%s+%d+" },
-    ["nmap-protocols"] = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)" ) ) end] = 
"^%s*([^%s#]+)%s+%d+" },
-    ["nmap-services"]  = { ["tcp"] = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)/tcp" ) ) end] = 
"^%s*([^%s#]+)%s+%d+/tcp" },
-                           ["udp"] = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)/udp" ) ) end] = 
"^%s*([^%s#]+)%s+%d+/udp" }
-    }
-
-}
-
-
----
 -- This function reads and parses Nmap's nmap-protocols file.
 -- bool is a Boolean value indicating success. If bool is true, then the
 -- second returned value is a table with protocol numbers indexing the
@@ -32,12 +18,7 @@
 -- @return bool, table|err
 -- @see parse_file
 parse_protocols = function()
-  local status, protocols_table = parse_file("nmap-protocols")
-  if not status then
-    return false, "Error parsing nmap-protocols"
-  end
-
-  return true, protocols_table
+  return parse_file("nmap-protocols", { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)" ) ) end] = 
"^%s*([^%s#]+)%s+%d+" })
 end
 
 
@@ -50,12 +31,7 @@
 -- @return bool, table|err
 -- @see parse_file
 parse_rpc = function()
-  local status, rpc_table = parse_file("nmap-rpc")
-  if not status then
-    return false, "Error parsing nmap-rpc"
-  end
-
-  return true, rpc_table
+  return parse_file( "nmap-rpc", { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)" ) ) end] = 
"^%s*([^%s#]+)%s+%d+" })
 end
 
 
@@ -72,16 +48,14 @@
 -- @return bool, table|err
 -- @see parse_file
 parse_services = function(protocol)
-  if protocol and protocol ~= "tcp" and protocol ~= "udp" then
-    return false, "Bad protocol for nmap-services: use tcp or udp"
-  end
+  local parse = { ["tcp"] = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)/tcp" ) ) end] = 
"^%s*([^%s#]+)%s+%d+/tcp" },
+                  ["udp"] = { [function(ln) return tonumber( ln:match( "^%s*[^%s#]+%s+(%d+)/udp" ) ) end] = 
"^%s*([^%s#]+)%s+%d+/udp" } }
 
-  local status, services_table = parse_file("nmap-services", protocol)
-  if not status then
-    return false, "Error parsing nmap-services"
+  if protocol and not parse[protocol] then
+    return false, "Bad protocol for nmap-services: use tcp or udp"
   end
 
-  return true, services_table
+  return parse_file("nmap-services", parse[protocol] or parse)
 end
 
 
@@ -100,16 +74,10 @@
     return false, "Error in datafiles.parse_file: No file to parse."
   end
 
-  -- is filename a member of common_files? is second parameter a key in common_files or is it a table?
-  if common_files[filename] and type( arg[1] ) == "string" and common_files[filename][arg[1]] then
-    data_struct = { common_files[filename][arg[1]] }
-  elseif common_files[filename] and #arg == 0 then
-    data_struct = { common_files[filename] }
-  elseif type( arg[1] ) == "table" then
-    data_struct = arg
-  elseif type( arg[1] ) ~= "table" then
+  if type( arg[1] ) ~= "table" then
     return false, "Error in datafiles.parse_file: Expected second parameter as table."
   end
+  data_struct = arg
 
   if type( data_struct ) == "table" then
     for i, struc in ipairs( data_struct ) do

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

Current thread: