resolved: cache - only allow putting a single question key at a time
authorTom Gundersen <teg@jklm.no>
Thu, 3 Sep 2015 23:01:27 +0000 (01:01 +0200)
committerTom Gundersen <teg@jklm.no>
Wed, 16 Sep 2015 15:03:17 +0000 (17:03 +0200)
Only one key is allowed per transaction now, so let's simplify things and only allow putting
one question key into the cache at a time.

src/resolve/resolved-dns-cache.c
src/resolve/resolved-dns-cache.h
src/resolve/resolved-dns-transaction.c

index 05518c3..478ad29 100644 (file)
@@ -403,7 +403,7 @@ static int dns_cache_put_negative(
 
 int dns_cache_put(
                 DnsCache *c,
-                DnsQuestion *q,
+                DnsResourceKey *key,
                 int rcode,
                 DnsAnswer *answer,
                 unsigned max_rrs,
@@ -411,16 +411,16 @@ int dns_cache_put(
                 int owner_family,
                 const union in_addr_union *owner_address) {
 
+        DnsResourceRecord *soa = NULL;
         unsigned cache_keys, i;
         int r;
 
         assert(c);
 
-        if (q) {
-                /* First, if we were passed a question, delete all matching old RRs,
+        if (key) {
+                /* First, if we were passed a key, 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]);
+                dns_cache_remove(c, key);
         }
 
         if (!answer)
@@ -438,8 +438,8 @@ int dns_cache_put(
 
         cache_keys = answer->n_rrs;
 
-        if (q)
-                cache_keys += q->n_keys;
+        if (key)
+                cache_keys ++;
 
         /* Make some space for our new entries */
         dns_cache_make_space(c, cache_keys);
@@ -454,33 +454,29 @@ int dns_cache_put(
                         goto fail;
         }
 
-        if (!q)
+        if (!key)
                 return 0;
 
-        /* Third, add in negative entries for all keys with no RR */
-        for (i = 0; i < q->n_keys; i++) {
-                DnsResourceRecord *soa = NULL;
-
-                r = dns_answer_contains(answer, q->keys[i]);
-                if (r < 0)
-                        goto fail;
-                if (r > 0)
-                        continue;
+        /* Third, add in negative entries if the key has no RR */
+        r = dns_answer_contains(answer, key);
+        if (r < 0)
+                goto fail;
+        if (r > 0)
+                return 0;
 
-                /* See https://tools.ietf.org/html/rfc2308, which
-                 * say that a matching SOA record in the packet
-                 * is used to to enable negative caching. */
+        /* See https://tools.ietf.org/html/rfc2308, which
+         * say that a matching SOA record in the packet
+         * is used to to enable negative caching. */
 
-                r = dns_answer_find_soa(answer, q->keys[i], &soa);
-                if (r < 0)
-                        goto fail;
-                if (r == 0)
-                        continue;
+        r = dns_answer_find_soa(answer, key, &soa);
+        if (r < 0)
+                goto fail;
+        if (r == 0)
+                return 0;
 
-                r = dns_cache_put_negative(c, q->keys[i], rcode, timestamp, MIN(soa->soa.minimum, soa->ttl), owner_family, owner_address);
-                if (r < 0)
-                        goto fail;
-        }
+        r = dns_cache_put_negative(c, key, rcode, timestamp, MIN(soa->soa.minimum, soa->ttl), owner_family, owner_address);
+        if (r < 0)
+                goto fail;
 
         return 0;
 
@@ -488,10 +484,8 @@ fail:
         /* Adding all RRs failed. Let's clean up what we already
          * added, just in case */
 
-        if (q) {
-                for (i = 0; i < q->n_keys; i++)
-                        dns_cache_remove(c, q->keys[i]);
-        }
+        if (key)
+                dns_cache_remove(c, key);
 
         for (i = 0; i < answer->n_rrs; i++)
                 dns_cache_remove(c, answer->items[i].rr->key);
index 1225e58..60cf6a4 100644 (file)
@@ -39,7 +39,7 @@ typedef struct DnsCache {
 void dns_cache_flush(DnsCache *c);
 void dns_cache_prune(DnsCache *c);
 
-int dns_cache_put(DnsCache *c, DnsQuestion *q, int rcode, DnsAnswer *answer, unsigned max_rrs, usec_t timestamp, int owner_family, const union in_addr_union *owner_address);
+int dns_cache_put(DnsCache *c, DnsResourceKey *key, int rcode, DnsAnswer *answer, unsigned max_rrs, usec_t timestamp, int owner_family, const union in_addr_union *owner_address);
 int dns_cache_lookup(DnsCache *c, DnsResourceKey *key, int *rcode, DnsAnswer **answer);
 
 int dns_cache_check_conflicts(DnsCache *cache, DnsResourceRecord *rr, int owner_family, const union in_addr_union *owner_address);
index 8092bb5..b30473d 100644 (file)
@@ -458,7 +458,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) {
         }
 
         /* According to RFC 4795, section 2.9. only the RRs from the answer section shall be cached */
-        dns_cache_put(&t->scope->cache, p->question, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender);
+        dns_cache_put(&t->scope->cache, t->key, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender);
 
         if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS)
                 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);