resolved: allow dns_cache_put() without a question
authorDaniel Mack <daniel@zonque.org>
Tue, 4 Aug 2015 11:53:02 +0000 (13:53 +0200)
committerDaniel Mack <daniel@zonque.org>
Tue, 25 Aug 2015 12:26:18 +0000 (14:26 +0200)
Currently, dns_cache_put() does a number of things:

1) It unconditionally removes all keys contained in the passed
   question before adding keys from the newly arrived answers.

2) It puts positive entries into the cache for all RRs contained
   in the answer.

3) It creates negative entries in the cache for all keys in the
   question that are not answered.

Allow passing q = NULL in the parameters and skip 1) and 3), so
we can use that function for mDNS responses. In this case, the
question is irrelevant, we are interested in all answers we got.

src/resolve/resolved-dns-cache.c

index efc407d..7ee0983 100644 (file)
@@ -411,16 +411,17 @@ int dns_cache_put(
                 int owner_family,
                 const union in_addr_union *owner_address) {
 
-        unsigned i;
+        unsigned cache_keys, i;
         int r;
 
         assert(c);
-        assert(q);
 
-        /* First, delete all matching old RRs, so that we only keep
-         * complete by_key in place. */
-        for (i = 0; i < q->n_keys; i++)
-                dns_cache_remove(c, q->keys[i]);
+        if (q) {
+                /* First, if we were passed a question, delete all matching old RRs,
+                 * so that we only keep complete by_key in place. */
+                for (i = 0; i < q->n_keys; i++)
+                        dns_cache_remove(c, q->keys[i]);
+        }
 
         if (!answer)
                 return 0;
@@ -435,8 +436,13 @@ int dns_cache_put(
         if (!IN_SET(rcode, DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN))
                 return 0;
 
+        cache_keys = answer->n_rrs;
+
+        if (q)
+                cache_keys += q->n_keys;
+
         /* Make some space for our new entries */
-        dns_cache_make_space(c, answer->n_rrs + q->n_keys);
+        dns_cache_make_space(c, cache_keys);
 
         if (timestamp <= 0)
                 timestamp = now(clock_boottime_or_monotonic());
@@ -448,6 +454,9 @@ int dns_cache_put(
                         goto fail;
         }
 
+        if (!q)
+                return 0;
+
         /* Third, add in negative entries for all keys with no RR */
         for (i = 0; i < q->n_keys; i++) {
                 DnsResourceRecord *soa = NULL;
@@ -479,8 +488,11 @@ fail:
         /* Adding all RRs failed. Let's clean up what we already
          * added, just in case */
 
-        for (i = 0; i < q->n_keys; i++)
-                dns_cache_remove(c, q->keys[i]);
+        if (q) {
+                for (i = 0; i < q->n_keys; i++)
+                        dns_cache_remove(c, q->keys[i]);
+        }
+
         for (i = 0; i < answer->n_rrs; i++)
                 dns_cache_remove(c, answer->items[i].rr->key);