1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-29 10:59:02 +00:00

Replace "buffered socket" classes with socket.receive_buf()

The match library has a helpful function for doing exactly this, with
less copy-and-pasted code:
http://nmap.org/nsedoc/lib/match.html#numbytes
This commit is contained in:
dmiller
2014-02-10 17:27:09 +00:00
parent 21b7e3818d
commit 4ea4977a37
8 changed files with 95 additions and 673 deletions

View File

@@ -25,9 +25,6 @@
-- - Contains the "native" NCP functions sending the actual request to the
-- server.
--
-- * Socket
-- - A buffered socket implementation
--
-- * Helper
-- - The prefered script interface to the library containing functions
-- that wrap functions from the NCP class using more descriptive names
@@ -56,6 +53,7 @@
local bin = require "bin"
local bit = require "bit"
local ipOps = require "ipOps"
local match = require "match"
local nmap = require "nmap"
local stdnse = require "stdnse"
local string = require "string"
@@ -622,7 +620,7 @@ Response = {
-- @param socket socket connected to server and ready to receive data
-- @return Response containing a new Response instance
fromSocket = function(socket)
local status, header = socket:recv(16)
local status, header = socket:receive_buf(match.numbytes(16), true)
if ( not(status) ) then return false, "Failed to receive data" end
local pos, sig, len = bin.unpack(">II", header)
@@ -631,7 +629,7 @@ Response = {
local data
if ( 0 < len - 16 ) then
status, data = socket:recv(len - 16)
status, data = socket:receive_buf(match.numbytes(len - 16), true)
if ( not(status) ) then return false, "Failed to receive data" end
end
return true, Response:new(header, data)
@@ -977,7 +975,8 @@ Helper = {
--- Connect the socket and creates a NCP connection
-- @return true on success false on failure
connect = function(self)
self.socket = Socket:new()
self.socket = nmap.new_socket()
self.socket:set_timeout(5000)
local status, err = self.socket:connect(self.host, self.port)
if ( not(status) ) then return status, err end
@@ -1077,76 +1076,6 @@ Helper = {
end,
}
Socket =
{
new = function(self)
local o = {}
setmetatable(o, self)
self.__index = self
o.Socket = nmap.new_socket()
o.Buffer = nil
return o
end,
--- Sets the socket timeout (@see nmap.set_timeout)
-- @param tm number containing the socket timeout in ms
set_timeout = function(self, tm) self.Socket:set_timeout(tm) end,
--- Establishes a connection.
--
-- @param hostid Hostname or IP address.
-- @param port Port number.
-- @param protocol <code>"tcp"</code>, <code>"udp"</code>, or
-- @return Status (true or false).
-- @return Error code (if status is false).
connect = function( self, hostid, port, protocol )
self.Socket:set_timeout(5000)
return self.Socket:connect( hostid, port, protocol )
end,
--- Closes an open connection.
--
-- @return Status (true or false).
-- @return Error code (if status is false).
close = function( self )
return self.Socket:close()
end,
--- Opposed to the <code>socket:receive_bytes</code> function, that returns
-- at least x bytes, this function returns the amount of bytes requested.
--
-- @param count of bytes to read
-- @return true on success, false on failure
-- @return data containing bytes read from the socket
-- err containing error message if status is false
recv = function( self, count )
local status, data
self.Buffer = self.Buffer or ""
if ( #self.Buffer < count ) then
status, data = self.Socket:receive_bytes( count - #self.Buffer )
if ( not(status) or #data < count - #self.Buffer ) then
return false, data
end
self.Buffer = self.Buffer .. data
end
data = self.Buffer:sub( 1, count )
self.Buffer = self.Buffer:sub( count + 1)
return true, data
end,
--- Sends data over the socket
--
-- @return Status (true or false).
-- @return Error code (if status is false).
send = function( self, data )
return self.Socket:send( data )
end,
}
--- "static" Utility class containing mostly conversion functions
Util =
{