Changeset

5568:c463558bed45

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 10 May 2013 10:46:27 +0100
parents 5560:cdb057244e44 (current diff) 5567:c4ab25b35a55 (diff)
children 5570:f592409f5fcd
files
diffstat 4 files changed, 205 insertions(+), 197 deletions(-) [+]
line wrap: on
line diff
--- a/net/dns.lua	Wed May 01 13:57:11 2013 +0100
+++ b/net/dns.lua	Fri May 10 10:46:27 2013 +0100
@@ -1072,6 +1072,10 @@
 	return _resolver:settimeout(...);
 end
 
+function dns.cache()
+	return _resolver.cache;
+end
+
 function dns.socket_wrapper_set(...)    -- - - - - - - - -  socket_wrapper_set
 	return _resolver:socket_wrapper_set(...);
 end
--- a/plugins/mod_admin_telnet.lua	Wed May 01 13:57:11 2013 +0100
+++ b/plugins/mod_admin_telnet.lua	Fri May 10 10:46:27 2013 +0100
@@ -207,6 +207,7 @@
 		print [[user - Commands to create and delete users, and change their passwords]]
 		print [[server - Uptime, version, shutting down, etc.]]
 		print [[port - Commands to manage ports the server is listening on]]
+		print [[dns - Commands to manage and inspect the internal DNS resolver]]
 		print [[config - Reloading the configuration, etc.]]
 		print [[console - Help regarding the console itself]]
 	elseif section == "c2s" then
@@ -239,6 +240,12 @@
 	elseif section == "port" then
 		print [[port:list() - Lists all network ports prosody currently listens on]]
 		print [[port:close(port, interface) - Close a port]]
+	elseif section == "dns" then
+		print [[dns:lookup(name, type, class) - Do a DNS lookup]]
+		print [[dns:addnameserver(nameserver) - Add a nameserver to the list]]
+		print [[dns:setnameserver(nameserver) - Replace the list of name servers with the supplied one]]
+		print [[dns:purge() - Clear the DNS cache]]
+		print [[dns:cache() - Show cached records]]
 	elseif section == "config" then
 		print [[config:reload() - Reload the server configuration. Modules may need to be reloaded for changes to take effect.]]
 	elseif section == "console" then
@@ -1001,6 +1008,40 @@
 	end
 end
 
+def_env.dns = {};
+local adns = require"net.adns";
+local dns = require"net.dns";
+
+function def_env.dns:lookup(name, typ, class)
+	local ret = "Query sent";
+	local print = self.session.print;
+	local function handler(...)
+		ret = "Got response";
+		print(...);
+	end
+	adns.lookup(handler, name, typ, class);
+	return true, ret;
+end
+
+function def_env.dns:addnameserver(...)
+	dns.addnameserver(...)
+	return true
+end
+
+function def_env.dns:setnameserver(...)
+	dns.setnameserver(...)
+	return true
+end
+
+function def_env.dns:purge()
+	dns.purge()
+	return true
+end
+
+function def_env.dns:cache()
+	return true, "Cache:\n"..tostring(dns.cache())
+end
+
 -------------
 
 function printbanner(session)
--- a/util/array.lua	Wed May 01 13:57:11 2013 +0100
+++ b/util/array.lua	Fri May 10 10:46:27 2013 +0100
@@ -17,7 +17,7 @@
 local array = {};
 local array_base = {};
 local array_methods = {};
-local array_mt = { __index = array_methods, __tostring = function (array) return array:concat(", "); end };
+local array_mt = { __index = array_methods, __tostring = function (array) return "{"..array:concat(", ").."}"; end };
 
 local function new_array(self, t, _s, _var)
 	if type(t) == "function" then -- Assume iterator
--- a/util/json.lua	Wed May 01 13:57:11 2013 +0100
+++ b/util/json.lua	Fri May 10 10:46:27 2013 +0100
@@ -13,11 +13,11 @@
 local pairs, ipairs = pairs, ipairs;
 local next = next;
 local error = error;
-local newproxy, getmetatable = newproxy, getmetatable;
+local newproxy, getmetatable, setmetatable = newproxy, getmetatable, setmetatable;
 local print = print;
 
 local has_array, array = pcall(require, "util.array");
-local array_mt = hasarray and getmetatable(array()) or {};
+local array_mt = has_array and getmetatable(array()) or {};
 
 --module("json")
 local json = {};
@@ -185,214 +185,177 @@
 -----------------------------------
 
 
-function json.decode(json)
-	json = json.." "; -- appending a space ensures valid json wouldn't touch EOF
-	local pos = 1;
-	local current = {};
-	local stack = {};
-	local ch, peek;
-	local function next()
-		ch = json:sub(pos, pos);
-		if ch == "" then error("Unexpected EOF"); end
-		pos = pos+1;
-		peek = json:sub(pos, pos);
-		return ch;
-	end
-	
-	local function skipwhitespace()
-		while ch and (ch == "\r" or ch == "\n" or ch == "\t" or ch == " ") do
-			next();
+local function _skip_whitespace(json, index)
+	return json:find("[^ \t\r\n]", index) or index; -- no need to check \r\n, we converted those to \t
+end
+local function _fixobject(obj)
+	local __array = obj.__array;
+	if __array then
+		obj.__array = nil;
+		for i,v in ipairs(__array) do
+			t_insert(obj, v);
 		end
 	end
-	local function skiplinecomment()
-		repeat next(); until not(ch) or ch == "\r" or ch == "\n";
-		skipwhitespace();
-	end
-	local function skipstarcomment()
-		next(); next(); -- skip '/', '*'
-		while peek and ch ~= "*" and peek ~= "/" do next(); end
-		if not peek then error("eof in star comment") end
-		next(); next(); -- skip '*', '/'
-		skipwhitespace();
-	end
-	local function skipstuff()
-		while true do
-			skipwhitespace();
-			if ch == "/" and peek == "*" then
-				skipstarcomment();
-			elseif ch == "/" and peek == "/" then
-				skiplinecomment();
+	local __hash = obj.__hash;
+	if __hash then
+		obj.__hash = nil;
+		local k;
+		for i,v in ipairs(__hash) do
+			if k ~= nil then
+				obj[k] = v; k = nil;
 			else
-				return;
+				k = v;
 			end
 		end
 	end
-	
-	local readvalue;
-	local function readarray()
-		local t = setmetatable({}, array_mt);
-		next(); -- skip '['
-		skipstuff();
-		if ch == "]" then next(); return t; end
-		t_insert(t, readvalue());
-		while true do
-			skipstuff();
-			if ch == "]" then next(); return t; end
-			if not ch then error("eof while reading array");
-			elseif ch == "," then next();
-			elseif ch then error("unexpected character in array, comma expected"); end
-			if not ch then error("eof while reading array"); end
-			t_insert(t, readvalue());
+	return obj;
+end
+local _readvalue, _readstring;
+local function _readobject(json, index)
+	local o = {};
+	while true do
+		local key, val;
+		index = _skip_whitespace(json, index + 1);
+		if json:byte(index) ~= 0x22 then -- "\""
+			if json:byte(index) == 0x7d then return o, index + 1; end -- "}"
+			return nil, "key expected";
 		end
-	end
-	
-	local function checkandskip(c)
-		local x = ch or "eof";
-		if x ~= c then error("unexpected "..x..", '"..c.."' expected"); end
-		next();
-	end
-	local function readliteral(lit, val)
-		for c in lit:gmatch(".") do
-			checkandskip(c);
-		end
-		return val;
+		key, index = _readstring(json, index);
+		if key == nil then return nil, index; end
+		index = _skip_whitespace(json, index);
+		if json:byte(index) ~= 0x3a then return nil, "colon expected"; end -- ":"
+		val, index = _readvalue(json, index + 1);
+		if val == nil then return nil, index; end
+		o[key] = val;
+		index = _skip_whitespace(json, index);
+		local b = json:byte(index);
+		if b == 0x7d then return _fixobject(o), index + 1; end -- "}"
+		if b ~= 0x2c then return nil, "object eof"; end -- ","
 	end
-	local function readstring()
-		local s = "";
-		checkandskip("\"");
-		while ch do
-			while ch and ch ~= "\\" and ch ~= "\"" do
-				s = s..ch; next();
-			end
-			if ch == "\\" then
-				next();
-				if unescapes[ch] then
-					s = s..unescapes[ch];
-					next();
-				elseif ch == "u" then
-					local seq = "";
-					for i=1,4 do
-						next();
-						if not ch then error("unexpected eof in string"); end
-						if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end
-						seq = seq..ch;
-					end
-					s = s..codepoint_to_utf8(tonumber(seq, 16));
-					next();
-				else error("invalid escape sequence in string"); end
-			end
-			if ch == "\"" then
-				next();
-				return s;
-			end
-		end
-		error("eof while reading string");
-	end
-	local function readnumber()
-		local s = "";
-		if ch == "-" then
-			s = s..ch; next();
-			if not ch:match("[0-9]") then error("number format error"); end
-		end
-		if ch == "0" then
-			s = s..ch; next();
-			if ch:match("[0-9]") then error("number format error"); end
-		else
-			while ch and ch:match("[0-9]") do
-				s = s..ch; next();
-			end
+end
+local function _readarray(json, index)
+	local a = {};
+	local oindex = index;
+	while true do
+		local val;
+		val, index = _readvalue(json, index + 1);
+		if val == nil then
+			if json:byte(oindex + 1) == 0x5d then return setmetatable(a, array_mt), oindex + 2; end -- "]"
+			return val, index;
 		end
-		if ch == "." then
-			s = s..ch; next();
-			if not ch:match("[0-9]") then error("number format error"); end
-			while ch and ch:match("[0-9]") do
-				s = s..ch; next();
-			end
-			if ch == "e" or ch == "E" then
-				s = s..ch; next();
-				if ch == "+" or ch == "-" then
-					s = s..ch; next();
-					if not ch:match("[0-9]") then error("number format error"); end
-					while ch and ch:match("[0-9]") do
-						s = s..ch; next();
-					end
-				end
-			end
-		end
-		return tonumber(s);
+		t_insert(a, val);
+		index = _skip_whitespace(json, index);
+		local b = json:byte(index);
+		if b == 0x5d then return setmetatable(a, array_mt), index + 1; end -- "]"
+		if b ~= 0x2c then return nil, "array eof"; end -- ","
+	end
+end
+local _unescape_error;
+local function _unescape_surrogate_func(x)
+	local lead, trail = tonumber(x:sub(3, 6), 16), tonumber(x:sub(9, 12), 16);
+	local codepoint = lead * 0x400 + trail - 0x35FDC00;
+	local a = codepoint % 64;
+	codepoint = (codepoint - a) / 64;
+	local b = codepoint % 64;
+	codepoint = (codepoint - b) / 64;
+	local c = codepoint % 64;
+	codepoint = (codepoint - c) / 64;
+	return s_char(0xF0 + codepoint, 0x80 + c, 0x80 + b, 0x80 + a);
+end
+local function _unescape_func(x)
+	x = x:match("%x%x%x%x", 3);
+	if x then
+		--if x >= 0xD800 and x <= 0xDFFF then _unescape_error = true; end -- bad surrogate pair
+		return codepoint_to_utf8(tonumber(x, 16));
 	end
-	local function readmember(t)
-		skipstuff();
-		local k = readstring();
-		skipstuff();
-		checkandskip(":");
-		t[k] = readvalue();
+	_unescape_error = true;
+end
+function _readstring(json, index)
+	index = index + 1;
+	local endindex = json:find("\"", index, true);
+	if endindex then
+		local s = json:sub(index, endindex - 1);
+		--if s:find("[%z-\31]") then return nil, "control char in string"; end
+		-- FIXME handle control characters
+		_unescape_error = nil;
+		--s = s:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x", _unescape_surrogate_func);
+		-- FIXME handle escapes beyond BMP
+		s = s:gsub("\\u.?.?.?.?", _unescape_func);
+		if _unescape_error then return nil, "invalid escape"; end
+		return s, endindex + 1;
+	end
+	return nil, "string eof";
+end
+local function _readnumber(json, index)
+	local m = json:match("[0-9%.%-eE%+]+", index); -- FIXME do strict checking
+	return tonumber(m), index + #m;
+end
+local function _readnull(json, index)
+	local a, b, c = json:byte(index + 1, index + 3);
+	if a == 0x75 and b == 0x6c and c == 0x6c then
+		return null, index + 4;
 	end
-	local function fixobject(obj)
-		local __array = obj.__array;
-		if __array then
-			obj.__array = nil;
-			for i,v in ipairs(__array) do
-				t_insert(obj, v);
-			end
-		end
-		local __hash = obj.__hash;
-		if __hash then
-			obj.__hash = nil;
-			local k;
-			for i,v in ipairs(__hash) do
-				if k ~= nil then
-					obj[k] = v; k = nil;
-				else
-					k = v;
-				end
-			end
-		end
-		return obj;
+	return nil, "null parse failed";
+end
+local function _readtrue(json, index)
+	local a, b, c = json:byte(index + 1, index + 3);
+	if a == 0x72 and b == 0x75 and c == 0x65 then
+		return true, index + 4;
+	end
+	return nil, "true parse failed";
+end
+local function _readfalse(json, index)
+	local a, b, c, d = json:byte(index + 1, index + 4);
+	if a == 0x61 and b == 0x6c and c == 0x73 and d == 0x65 then
+		return false, index + 5;
 	end
-	local function readobject()
-		local t = {};
-		next(); -- skip '{'
-		skipstuff();
-		if ch == "}" then next(); return t; end
-		if not ch then error("eof while reading object"); end
-		readmember(t);
-		while true do
-			skipstuff();
-			if ch == "}" then next(); return fixobject(t); end
-			if not ch then error("eof while reading object");
-			elseif ch == "," then next();
-			elseif ch then error("unexpected character in object, comma expected"); end
-			if not ch then error("eof while reading object"); end
-			readmember(t);
-		end
+	return nil, "false parse failed";
+end
+function _readvalue(json, index)
+	index = _skip_whitespace(json, index);
+	local b = json:byte(index);
+	-- TODO try table lookup instead of if-else?
+	if b == 0x7B then -- "{"
+		return _readobject(json, index);
+	elseif b == 0x5B then -- "["
+		return _readarray(json, index);
+	elseif b == 0x22 then -- "\""
+		return _readstring(json, index);
+	elseif b ~= nil and b >= 0x30 and b <= 0x39 or b == 0x2d then -- "0"-"9" or "-"
+		return _readnumber(json, index);
+	elseif b == 0x6e then -- "n"
+		return _readnull(json, index);
+	elseif b == 0x74 then -- "t"
+		return _readtrue(json, index);
+	elseif b == 0x66 then -- "f"
+		return _readfalse(json, index);
+	else
+		return nil, "value expected";
 	end
+end
+local first_escape = {
+	["\\\""] = "\\u0022";
+	["\\\\"] = "\\u005c";
+	["\\/" ] = "\\u002f";
+	["\\b" ] = "\\u0008";
+	["\\f" ] = "\\u000C";
+	["\\n" ] = "\\u000A";
+	["\\r" ] = "\\u000D";
+	["\\t" ] = "\\u0009";
+	["\\u" ] = "\\u";
+};
+
+function json.decode(json)
+	json = json:gsub("\\.", first_escape) -- get rid of all escapes except \uXXXX, making string parsing much simpler
+		--:gsub("[\r\n]", "\t"); -- \r\n\t are equivalent, we care about none of them, and none of them can be in strings
 	
-	function readvalue()
-		skipstuff();
-		while ch do
-			if ch == "{" then
-				return readobject();
-			elseif ch == "[" then
-				return readarray();
-			elseif ch == "\"" then
-				return readstring();
-			elseif ch:match("[%-0-9%.]") then
-				return readnumber();
-			elseif ch == "n" then
-				return readliteral("null", null);
-			elseif ch == "t" then
-				return readliteral("true", true);
-			elseif ch == "f" then
-				return readliteral("false", false);
-			else
-				error("invalid character at value start: "..ch);
-			end
-		end
-		error("eof while reading value");
-	end
-	next();
-	return readvalue();
+	-- TODO do encoding verification
+	
+	local val, index = _readvalue(json, 1);
+	if val == nil then return val, index; end
+	if json:find("[^ \t\r\n]", index) then return nil, "garbage at eof"; end
+
+	return val;
 end
 
 function json.test(object)