aboutsummaryrefslogtreecommitdiffstats
path: root/net/dns.lua
diff options
context:
space:
mode:
Diffstat (limited to 'net/dns.lua')
-rw-r--r--net/dns.lua78
1 files changed, 56 insertions, 22 deletions
diff --git a/net/dns.lua b/net/dns.lua
index 61fb62e8..bd5c260e 100644
--- a/net/dns.lua
+++ b/net/dns.lua
@@ -14,6 +14,7 @@
local socket = require "socket";
local timer = require "util.timer";
+local new_ip = require "util.ip".new_ip;
local _, windows = pcall(require, "util.windows");
local is_windows = (_ and windows) or os.getenv("WINDIR");
@@ -158,8 +159,6 @@ resolver.__index = resolver;
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
@@ -170,8 +169,13 @@ end
local special_tostrings = {
LOC = resolver.LOC_tostring;
- MX = function (rr) return string.format('%2i %s', rr.pref, rr.mx); end;
- SRV = SRV_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
@@ -220,7 +224,7 @@ end
function dns.random(...) -- - - - - - - - - - - - - - - - - - - dns.random
- math.randomseed(math.floor(10000*socket.gettime()));
+ math.randomseed(math.floor(10000*socket.gettime()) % 0x100000000);
dns.random = math.random;
return dns.random(...);
end
@@ -355,6 +359,7 @@ function resolver:name() -- - - - - - - - - - - - - - - - - - - - - - name
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;
@@ -386,6 +391,25 @@ function resolver:A(rr) -- - - - - - - - - - - - - - - - - - - - - - - - A
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();
@@ -475,14 +499,8 @@ function resolver:PTR(rr)
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
@@ -532,6 +550,7 @@ function resolver:decode(packet, force) -- - - - - - - - - - - - - - decode
if not force then
if not self.active[response.header.id] or not self.active[response.header.id][response.question.raw] then
+ self.active[response.header.id] = nil;
return nil;
end
end
@@ -579,11 +598,12 @@ function resolver:adddefaultnameservers() -- - - - - adddefaultnameservers
if resolv_conf then
for line in resolv_conf:lines() do
line = line:gsub("#.*$", "")
- :match('^%s*nameserver%s+(.*)%s*$');
+ :match('^%s*nameserver%s+([%x:%.]*)%s*$');
if line then
- line:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]", function (address)
- self:addnameserver(address)
- end);
+ local ip = new_ip(line);
+ if ip then
+ self:addnameserver(ip.addr);
+ end
end
end
end
@@ -603,15 +623,20 @@ function resolver:getsocket(servernum) -- - - - - - - - - - - - - getsocket
if sock then return sock; end
local err;
- sock, err = socket.udp();
+ local peer = self.server[servernum];
+ if peer:find(":") then
+ sock, err = socket.udp6();
+ else
+ sock, err = socket.udp();
+ end
+ if sock and self.socket_wrapper then sock, err = self.socket_wrapper(sock, self); end
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
sock:setsockname('*', 0);
- sock:setpeername(self.server[servernum], 53);
+ sock:setpeername(peer, 53);
self.socket[servernum] = sock;
self.socketset[sock] = servernum;
return sock;
@@ -625,6 +650,7 @@ function resolver:voidsocket(sock)
self.socket[self.socketset[sock]] = nil;
self.socketset[sock] = nil;
end
+ sock:close();
end
function resolver:socket_wrapper_set(func) -- - - - - - - socket_wrapper_set
@@ -689,7 +715,7 @@ function resolver:purge(soft) -- - - - - - - - - - - - - - - - - - - purge
end
end
end
- else self.cache = {}; end
+ else self.cache = setmetatable({}, cache_metatable); end
end
@@ -727,7 +753,7 @@ function resolver:query(qname, qtype, qclass) -- - - - - - - - - - -- query
return nil, err;
end
conn:send (o.packet)
-
+
if timer and self.timeout then
local num_servers = #self.server;
local i = 1;
@@ -779,6 +805,9 @@ function resolver:servfail(sock)
end
end
end
+ if next(queries) == nil then
+ self.active[id] = nil;
+ end
end
if num == self.best_server then
@@ -820,7 +849,7 @@ function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
-- retire the query
local queries = self.active[response.header.id];
queries[response.question.raw] = nil;
-
+
if not next(queries) then self.active[response.header.id] = nil; end
if not next(self.active) then self:closeall(); end
@@ -835,6 +864,7 @@ function resolver:receive(rset) -- - - - - - - - - - - - - - - - - receive
set(self.wanted, q.class, q.type, q.name, nil);
end
end
+
end
end
end
@@ -1049,6 +1079,10 @@ function dns.settimeout(...)
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