local coroutine, io, math, string, table =
coroutine, io, math, string, table;
-local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select =
- ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select;
+local ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type=
+ ipairs, next, pairs, print, setmetatable, tostring, assert, error, unpack, select, type;
local ztact = { -- public domain 20080404 lua@ztact.com
get = function(parent, ...)
resolver.timeout = default_timeout;
-local SRV_tostring;
+local function default_rr_tostring(rr)
+ local rr_val = rr.type and rr[rr.type:lower()];
+ if type(rr_val) ~= "string" then
+ return "<UNKNOWN RDATA TYPE>";
+ end
+ return rr_val;
+end
+local special_tostrings = {
+ LOC = resolver.LOC_tostring;
+ MX = function (rr)
+ return string.format('%2i %s', rr.pref, rr.mx);
+ end;
+ SRV = function (rr)
+ local s = rr.srv;
+ return string.format('%5d %5d %5d %s', s.priority, s.weight, s.port, s.target);
+ end;
+};
local rr_metatable = {}; -- - - - - - - - - - - - - - - - - - - rr_metatable
function rr_metatable.__tostring(rr)
- local s0 = string.format('%2s %-5s %6i %-28s', rr.class, rr.type, rr.ttl, rr.name);
- local s1 = '';
- if rr.type == 'A' then
- s1 = ' '..rr.a;
- elseif rr.type == 'MX' then
- s1 = string.format(' %2i %s', rr.pref, rr.mx);
- elseif rr.type == 'CNAME' then
- s1 = ' '..rr.cname;
- elseif rr.type == 'LOC' then
- s1 = ' '..resolver.LOC_tostring(rr);
- elseif rr.type == 'NS' then
- s1 = ' '..rr.ns;
- elseif rr.type == 'SRV' then
- s1 = ' '..SRV_tostring(rr);
- elseif rr.type == 'TXT' then
- s1 = ' '..rr.txt;
- else
- s1 = ' <UNKNOWN RDATA TYPE>';
- end
- return s0..s1;
+ local rr_string = (special_tostrings[rr.type] or default_rr_tostring)(rr);
+ return string.format('%2s %-5s %6i %-28s %s', rr.class, rr.type, rr.ttl, rr.name, rr_string);
end
local remember, pointers = nil, 0;
local len = self:byte();
local n = {};
+ if len == 0 then return "." end -- Root label
while len > 0 do
if len >= 0xc0 then -- name is "compressed"
pointers = pointers + 1;
rr.a = string.format('%i.%i.%i.%i', b1, b2, b3, b4);
end
+function resolver:AAAA(rr)
+ local addr = {};
+ for i = 1, rr.rdlength, 2 do
+ local b1, b2 = self:byte(2);
+ table.insert(addr, ("%02x%02x"):format(b1, b2));
+ end
+ addr = table.concat(addr, ":"):gsub("%f[%x]0+(%x)","%1");
+ local zeros = {};
+ for item in addr:gmatch(":[0:]+:") do
+ table.insert(zeros, item)
+ end
+ if #zeros == 0 then
+ rr.aaaa = addr;
+ return
+ elseif #zeros > 1 then
+ table.sort(zeros, function(a, b) return #a > #b end);
+ end
+ rr.aaaa = addr:gsub(zeros[1], "::", 1):gsub("^0::", "::"):gsub("::0$", "::");
+end
function resolver:CNAME(rr) -- - - - - - - - - - - - - - - - - - - - CNAME
rr.cname = self:name();
rr.ptr = self:name();
end
-function SRV_tostring(rr) -- - - - - - - - - - - - - - - - - - SRV_tostring
- local s = rr.srv;
- return string.format( '%5d %5d %5d %s', s.priority, s.weight, s.port, s.target );
-end
-
-
function resolver:TXT(rr) -- - - - - - - - - - - - - - - - - - - - - - TXT
- rr.txt = self:sub (rr.rdlength);
+ rr.txt = self:sub (self:byte());
end
local sock = self.socket[servernum];
if sock then return sock; end
- sock = socket.udp();
+ local err;
+ sock, err = socket.udp();
+ if not sock then
+ return nil, err;
+ end
if self.socket_wrapper then sock = self.socket_wrapper(sock, self); end
sock:settimeout(0);
-- todo: attempt to use a random port, fallback to 0
end
end
end
- else self.cache = {}; end
+ else self.cache = setmetatable({}, cache_metatable); end
end
--set(self.yielded, co, qclass, qtype, qname, true);
end
- local conn = self:getsocket(o.server)
+ local conn, err = self:getsocket(o.server)
+ if not conn then
+ return nil, err;
+ end
conn:send (o.packet)
if timer and self.timeout then
i = i + 1;
self:servfail(conn);
o.server = self.best_server;
- conn = self:getsocket(o.server);
- conn:send(o.packet);
- return self.timeout;
- else
- -- Tried everything, failed
- self:cancel(qclass, qtype, qname, co, true);
+ conn, err = self:getsocket(o.server);
+ if conn then
+ conn:send(o.packet);
+ return self.timeout;
+ end
end
+ -- Tried everything, failed
+ self:cancel(qclass, qtype, qname, co, true);
end
end)
end
+ return true;
end
function resolver:servfail(sock)