net.server_select: Make sure there is a server to pause when hitting maxfd (Thanks...
[prosody.git] / net / dns.lua
index c905f56c5d612565d416a2fcd85321cc64f58b7a..89b50255811b606af3ff2a53e0a91312bbe5e974 100644 (file)
@@ -223,7 +223,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
@@ -358,6 +358,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;
@@ -389,6 +390,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();
@@ -479,7 +499,7 @@ function resolver:PTR(rr)
 end
 
 function resolver:TXT(rr)    -- - - - - - - - - - - - - - - - - - - - - -  TXT
-       rr.txt = self:sub (rr.rdlength);
+       rr.txt = self:sub (self:byte());
 end
 
 
@@ -529,6 +549,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
@@ -622,6 +643,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
@@ -686,7 +708,7 @@ function resolver:purge(soft)    -- - - - - - - - - - - - - - - - - - -  purge
                                end
                        end
                end
-       else self.cache = {}; end
+       else self.cache = setmetatable({}, cache_metatable); end
 end
 
 
@@ -776,6 +798,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
@@ -832,6 +857,7 @@ function resolver:receive(rset)    -- - - - - - - - - - - - - - - - -  receive
                                                set(self.wanted, q.class, q.type, q.name, nil);
                                        end
                                end
+                               
                        end
                end
        end
@@ -1046,6 +1072,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