Nmap Development mailing list archives

[NSE][PATCH] add an eof option to comm.lua


From: Sven Klemm <sven () c3d2 de>
Date: Wed, 01 Oct 2008 12:03:53 +0200

Hi everyone,

currently comm.lua stops after reading the first chunk returned by receive_lines() or receive_bytes() but in some circumstances it might be nice to read until EOF to get everything sent. The patch adds an eof option to the comm library. If this option is set it will continue receiving data until it gets EOF and in this case true and all data will be returned other error cases will still be handled normally.

Usage:
  comm.exchange( host, port, data, {eof=true} )

I think this functionality is very useful and with this option the http library and rpcinfo.nse could be changed to use the comm library again.

Cheers,
Sven

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

Index: nselib/comm.lua
===================================================================
--- nselib/comm.lua     (revision 10425)
+++ nselib/comm.lua     (working copy)
@@ -37,6 +37,8 @@
 --
 --   bytes: Specifies the minimum amount of bytes are to be read from the host
 --   lines: Specifies the minimum amount of lines are to be read from the host
+--   eof: Read until an EOF error occurs. Returns true as status and the data 
+--        received in case of an EOF error
 --   proto: Specifies the protocol to be used with the connect() call
 --   timeout: Sets the socket's timeout with nmap.set_timeout()
 --
@@ -97,10 +99,30 @@
 
        if opts.lines then
                status, response = sock:receive_lines(opts.lines)
-               return status, response
+       elseif opts.eof then
+    -- read until we get an EOF error
+    -- return true and the returned data in this case
+               local part
+               status, response = sock:receive_bytes(1)
+               if status then
+                       while status do
+                               status, part = sock:receive_bytes(opts.bytes)
+                               if status then
+                                       response = response .. part
+                               end
+                       end
+                       if part == "EOF" then
+                               status = true
+                       else
+                               response = part
+                       end
+    elseif response == "EOF" then
+      status, response = true, ""
+               end
+       else 
+         status, response = sock:receive_bytes(opts.bytes)
        end
 
-       status, response = sock:receive_bytes(opts.bytes)
        return status, response
 end
 

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

Current thread: