]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolved: cache - handle CNAME redirection
authorTom Gundersen <teg@jklm.no>
Mon, 17 Aug 2015 05:56:57 +0000 (07:56 +0200)
committerTom Gundersen <teg@jklm.no>
Wed, 16 Sep 2015 15:03:17 +0000 (17:03 +0200)
CNAME records are special in the way they are treated by DNS servers,
and our cache should mimic that behavior: In case a domain name has an
alias, its CNAME record is returned in place of any other.

Our cache was not doing this despite caching the CNAME records, this
entailed needless lookups to re-resolve the CNAME.

src/resolve/resolved-bus.c
src/resolve/resolved-dns-cache.c

index 19db781ac48ae3b1fd8c8952b0ade44c4d855858..bf1b7c8ab449b965f5eea69ab07775682cca0d4d 100644 (file)
@@ -220,8 +220,6 @@ static void bus_method_resolve_hostname_complete(DnsQuery *q) {
                         added++;
                 }
 
-                // what about the cache?
-
                 /* If we didn't find anything, then let's restart the
                  * query, this time with the cname */
                 if (added <= 0) {
index 478ad29bbfe146528eebeb596b3534a3f4e875b5..93b98f3727eb8173db5b80794bc0986110e662e1 100644 (file)
@@ -493,6 +493,29 @@ fail:
         return r;
 }
 
+static DnsCacheItem *dns_cache_get_by_key_follow_cname(DnsCache *c, DnsResourceKey *k) {
+        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *cname_key = NULL;
+        DnsCacheItem *i, *j;
+
+        assert(c);
+        assert(k);
+
+        i = hashmap_get(c->by_key, k);
+        if (i || k->type == DNS_TYPE_CNAME)
+                return i;
+
+        /* check if we have a CNAME record instead */
+        cname_key = dns_resource_key_new_cname(k);
+        if (!cname_key)
+                return NULL;
+
+        j = hashmap_get(c->by_key, cname_key);
+        if (j)
+                return j;
+
+        return i;
+}
+
 int dns_cache_lookup(DnsCache *c, DnsResourceKey *key, int *rcode, DnsAnswer **ret) {
         _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
         unsigned n = 0;
@@ -522,7 +545,7 @@ int dns_cache_lookup(DnsCache *c, DnsResourceKey *key, int *rcode, DnsAnswer **r
                 return 0;
         }
 
-        first = hashmap_get(c->by_key, key);
+        first = dns_cache_get_by_key_follow_cname(c, key);
         if (!first) {
                 /* If one question cannot be answered we need to refresh */