Merge 0.10->trunk
[prosody.git] / net / dns.lua
index bd5c260ec2f1a638843c662544139e1b2eb6e920..2b03caf605af685408cd909d95592e7be6ee6d28 100644 (file)
@@ -135,17 +135,19 @@ end
 
 local function prune(rrs, time, soft)    -- - - - - - - - - - - - - - -  prune
        time = time or socket.gettime();
-       for i,rr in pairs(rrs) do
+       for i,rr in ipairs(rrs) do
                if rr.tod then
                        -- rr.tod = rr.tod - 50    -- accelerated decripitude
                        rr.ttl = math.floor(rr.tod - time);
                        if rr.ttl <= 0 then
+                               rrs[rr[rr.type:lower()]] = nil;
                                table.remove(rrs, i);
                                return prune(rrs, time, soft); -- Re-iterate
                        end
                elseif soft == 'soft' then    -- What is this?  I forget!
                        assert(rr.ttl == 0);
-                       rrs[i] = nil;
+                       rrs[rr[rr.type:lower()]] = nil;
+                       table.remove(rrs, i);
                end
        end
 end
@@ -188,7 +190,7 @@ end
 local rrs_metatable = {};    -- - - - - - - - - - - - - - - - - -  rrs_metatable
 function rrs_metatable.__tostring(rrs)
        local t = {};
-       for i,rr in pairs(rrs) do
+       for i,rr in ipairs(rrs) do
                append(t, tostring(rr)..'\n');
        end
        return table.concat(t);
@@ -681,7 +683,10 @@ function resolver:remember(rr, type)    -- - - - - - - - - - - - - -  remember
        self.cache = self.cache or setmetatable({}, cache_metatable);
        local rrs = get(self.cache, qclass, type, qname) or
                set(self.cache, qclass, type, qname, setmetatable({}, rrs_metatable));
-       append(rrs, rr);
+       if not rrs[rr[qtype:lower()]] then
+               rrs[rr[qtype:lower()]] = true;
+               append(rrs, rr);
+       end
 
        if type == 'MX' then self.unsorted[rrs] = true; end
 end
@@ -722,6 +727,14 @@ end
 function resolver:query(qname, qtype, qclass)    -- - - - - - - - - - -- query
        qname, qtype, qclass = standardize(qname, qtype, qclass)
 
+       local co = coroutine.running();
+       local q = get(self.wanted, qclass, qtype, qname);
+       if co and q then
+               -- We are already waiting for a reply to an identical query.
+               set(self.wanted, qclass, qtype, qname, co, true);
+               return true;
+       end
+
        if not self.server then self:adddefaultnameservers(); end
 
        local question = encodeQuestion(qname, qtype, qclass);
@@ -742,10 +755,8 @@ function resolver:query(qname, qtype, qclass)    -- - - - - - - - - - -- query
        self.active[id][question] = o;
 
        -- remember which coroutine wants the answer
-       local co = coroutine.running();
        if co then
                set(self.wanted, qclass, qtype, qname, co, true);
-               --set(self.yielded, co, qclass, qtype, qname, true);
        end
 
        local conn, err = self:getsocket(o.server)
@@ -770,7 +781,7 @@ function resolver:query(qname, qtype, qclass)    -- - - - - - - - - - -- query
                                        end
                                end
                                -- Tried everything, failed
-                               self:cancel(qclass, qtype, qname, co, true);
+                               self:cancel(qclass, qtype, qname);
                        end
                end)
        end
@@ -858,7 +869,6 @@ function resolver:receive(rset)    -- - - - - - - - - - - - - - - - -  receive
                                        local cos = get(self.wanted, q.class, q.type, q.name);
                                        if cos then
                                                for co in pairs(cos) do
-                                                       set(self.yielded, co, q.class, q.type, q.name, nil);
                                                        if coroutine.status(co) == "suspended" then coroutine.resume(co); end
                                                end
                                                set(self.wanted, q.class, q.type, q.name, nil);
@@ -899,7 +909,6 @@ function resolver:feed(sock, packet, force)
                        local cos = get(self.wanted, q.class, q.type, q.name);
                        if cos then
                                for co in pairs(cos) do
-                                       set(self.yielded, co, q.class, q.type, q.name, nil);
                                        if coroutine.status(co) == "suspended" then coroutine.resume(co); end
                                end
                                set(self.wanted, q.class, q.type, q.name, nil);
@@ -910,13 +919,13 @@ function resolver:feed(sock, packet, force)
        return response;
 end
 
-function resolver:cancel(qclass, qtype, qname, co, call_handler)
+function resolver:cancel(qclass, qtype, qname)
        local cos = get(self.wanted, qclass, qtype, qname);
        if cos then
-               if call_handler then
-                       coroutine.resume(co);
+               for co in pairs(cos) do
+                       if coroutine.status(co) == "suspended" then coroutine.resume(co); end
                end
-               cos[co] = nil;
+               set(self.wanted, qclass, qtype, qname, nil);
        end
 end
 
@@ -1037,7 +1046,7 @@ end
 function dns.resolver ()    -- - - - - - - - - - - - - - - - - - - - - resolver
        -- this function seems to be redundant with resolver.new ()
 
-       local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, yielded = {}, best_server = 1 };
+       local r = { active = {}, cache = {}, unsorted = {}, wanted = {}, best_server = 1 };
        setmetatable (r, resolver);
        setmetatable (r.cache, cache_metatable);
        setmetatable (r.unsorted, { __mode = 'kv' });