1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-10 00:19:02 +00:00

Improved and changed the make_buffer function.

It now returns the line as its first argument always, otherwise nil, errmsg

I updated scripts that use make_buffer.

When Lua 5.2 comes out, we may be able to use a function I've added
called stdnse.lines (commented for now). It cannot be used yet because
we cannot yield inside generic for loop. The function is built for
generic for loops. It will work as such:
for line in stdnse.lines(socket) do
  ...
end
This commit is contained in:
batrick
2008-06-20 13:50:50 +00:00
parent dffb1d9423
commit 0bc91f9db2
2 changed files with 26 additions and 63 deletions

View File

@@ -4,6 +4,7 @@ local assert = assert;
local tonumber = tonumber;
local concat = table.concat;
local nmap = require"nmap";
local print = print
module(... or "stdnse");
@@ -62,80 +63,44 @@ end
--
-- -Doug, June, 2007
--[[ function make_buffer(socket, sep)
local point, pattern, buffer = 1, "([^"..sep.."]-)"..sep.."?";
local function self(lines)
lines = lines or 1;
if not buffer then
local status, str = socket:receive_lines(lines);
function make_buffer(socket, sep)
local point, left, buffer, done, msg = 1, "";
local function self()
if done then
return nil, msg; -- must be nil for stdnse.lines (below)
elseif not buffer then
local status, str = socket:receive_lines(1);
if not status then
if buffer then
return buffer:sub(point);
if #left > 0 then
done, msg = not status, str;
return left;
else
return status, str;
end
else
buffer = str;
return self(lines);
buffer = left..str;
return self();
end
else
local _, j, str = buffer:find(pattern, point);
if j then
local i, j = buffer:find(sep, point);
if i then
local ret = buffer:sub(point, i-1);
point = j + 1;
return str;
return ret;
else
point, buffer = 1, nil;
return self(lines);
point, left, buffer = 1, buffer:sub(point), nil;
return self();
end
end
end
return self;
end --]]
make_buffer = function(sd, sep)
local self, result
local buf = ""
self = function()
local i, j, status, value
i, j = buf:find(sep)
if i then
if i == 1 then -- empty line
buf = buf:sub(j+1, -1)
--return self() -- skip empty, tail
return true, "" -- return empty
else
value = buf:sub(1, i-1)
buf = buf:sub(j+1, -1)
return true, value
end
end
if result then
if #buf > 0 then -- left over data with no terminating pattern
value = buf
buf = ""
return true, value
end
return nil, result
end
status, value = sd:receive()
if status then
buf = buf .. value
else
result = value
end
return self() -- tail
end
return self
end
--[[ This function may be usable in Lua 5.2
function lines(socket)
return make_buffer(socket, "\r?\n"), nil, nil;
end --]]
do
local t = {
["0"] = "0000",

View File

@@ -69,8 +69,6 @@ end
action = function(host, port)
local status = 0
local line = ""
local sd = nmap.new_socket()
local curr_nick = random_nick()
local sver, shost, susers, sservers, schans, sircops, slusers, slservers, sup, serr
@@ -121,8 +119,8 @@ action = function(host, port)
buf = stdnse.make_buffer(sd, "\r?\n")
while true do
status, line = buf()
if (not status) then break end
local line = buf()
if (not line) then break end
-- This one lets us know we've connected, pre-PONGed, and got a NICK
s, e, t = nmap.registry.ircserverinfo_375:exec(line, 0, 0)