chiark / gitweb /
resolved: rework logic so that we can share transactions between queries of different...
[elogind.git] / src / resolve / resolved-dns-rr.c
index c8f7cf4..c9b564b 100644 (file)
 #include "resolved-dns-domain.h"
 #include "resolved-dns-rr.h"
 
 #include "resolved-dns-domain.h"
 #include "resolved-dns-rr.h"
 
-void dns_resource_key_free(DnsResourceKey *key) {
-        if (!key)
-                return;
+DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
+        DnsResourceKey *k;
+        size_t l;
 
 
-        free(key->name);
-        zero(*key);
+        assert(name);
+
+        l = strlen(name);
+        k = malloc0(sizeof(DnsResourceKey) + l + 1);
+        if (!k)
+                return NULL;
+
+        k->n_ref = 1;
+        k->class = class;
+        k->type = type;
+
+        strcpy((char*) k + sizeof(DnsResourceKey), name);
+
+        return k;
+}
+
+DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
+        DnsResourceKey *k;
+
+        assert(name);
+
+        k = new0(DnsResourceKey, 1);
+        if (!k)
+                return NULL;
+
+        k->n_ref = 1;
+        k->class = class;
+        k->type = type;
+        k->_name = name;
+
+        return k;
+}
+
+DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
+
+        if (!k)
+                return NULL;
+
+        assert(k->n_ref > 0);
+        k->n_ref++;
+
+        return k;
+}
+
+DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
+        if (!k)
+                return NULL;
+
+        assert(k->n_ref > 0);
+
+        if (k->n_ref == 1) {
+                free(k->_name);
+                free(k);
+        } else
+                k->n_ref--;
+
+        return NULL;
+}
+
+int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
+        int r;
+
+        r = dns_name_equal(DNS_RESOURCE_KEY_NAME(a), DNS_RESOURCE_KEY_NAME(b));
+        if (r <= 0)
+                return r;
+
+        if (a->class != b->class)
+                return 0;
+
+        if (a->type != b->type)
+                return 0;
+
+        return 1;
+}
+
+int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr) {
+        assert(key);
+        assert(rr);
+
+        if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
+                return 0;
+
+        if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
+                return 0;
+
+        return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
+}
+
+int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr) {
+        assert(key);
+        assert(rr);
+
+        if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
+                return 0;
+
+        if (rr->key->type != DNS_TYPE_CNAME)
+                return 0;
+
+        return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key));
 }
 
 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
         const DnsResourceKey *k = i;
         unsigned long ul;
 
 }
 
 unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) {
         const DnsResourceKey *k = i;
         unsigned long ul;
 
-        ul = dns_name_hash_func(k->name, hash_key);
+        ul = dns_name_hash_func(DNS_RESOURCE_KEY_NAME(k), hash_key);
         ul = ul * hash_key[0] + ul + k->class;
         ul = ul * hash_key[1] + ul + k->type;
 
         ul = ul * hash_key[0] + ul + k->class;
         ul = ul * hash_key[1] + ul + k->type;
 
@@ -45,7 +142,7 @@ int dns_resource_key_compare_func(const void *a, const void *b) {
         const DnsResourceKey *x = a, *y = b;
         int ret;
 
         const DnsResourceKey *x = a, *y = b;
         int ret;
 
-        ret = dns_name_compare_func(x->name, y->name);
+        ret = dns_name_compare_func(DNS_RESOURCE_KEY_NAME(x), DNS_RESOURCE_KEY_NAME(y));
         if (ret != 0)
                 return ret;
 
         if (ret != 0)
                 return ret;
 
@@ -62,7 +159,7 @@ int dns_resource_key_compare_func(const void *a, const void *b) {
         return 0;
 }
 
         return 0;
 }
 
-DnsResourceRecord* dns_resource_record_new(void) {
+DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
         DnsResourceRecord *rr;
 
         rr = new0(DnsResourceRecord, 1);
         DnsResourceRecord *rr;
 
         rr = new0(DnsResourceRecord, 1);
@@ -70,6 +167,8 @@ DnsResourceRecord* dns_resource_record_new(void) {
                 return NULL;
 
         rr->n_ref = 1;
                 return NULL;
 
         rr->n_ref = 1;
+        rr->key = dns_resource_key_ref(key);
+
         return rr;
 }
 
         return rr;
 }
 
@@ -94,29 +193,20 @@ DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr) {
                 return NULL;
         }
 
                 return NULL;
         }
 
-        if (IN_SET(rr->key.type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
-                free(rr->ptr.name);
-        else if (rr->key.type == DNS_TYPE_HINFO) {
-                free(rr->hinfo.cpu);
-                free(rr->hinfo.os);
-        } else if (!IN_SET(rr->key.type, DNS_TYPE_A, DNS_TYPE_AAAA))
-                free(rr->generic.data);
-
-        dns_resource_key_free(&rr->key);
-        free(rr);
-
-        return NULL;
-}
+        if (rr->key) {
+                if (IN_SET(rr->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
+                        free(rr->ptr.name);
+                else if (rr->key->type == DNS_TYPE_HINFO) {
+                        free(rr->hinfo.cpu);
+                        free(rr->hinfo.os);
+                } else if (!IN_SET(rr->key->type, DNS_TYPE_A, DNS_TYPE_AAAA))
+                        free(rr->generic.data);
 
 
-DnsResourceRecord** dns_resource_record_freev(DnsResourceRecord **rrs, unsigned n) {
-        unsigned i;
-
-        assert(n == 0 || rrs);
+                dns_resource_key_unref(rr->key);
+        }
 
 
-        for (i = 0; i < n; i++)
-                dns_resource_record_unref(rrs[i]);
+        free(rr);
 
 
-        free(rrs);
         return NULL;
 }
 
         return NULL;
 }
 
@@ -126,24 +216,18 @@ int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecor
         assert(a);
         assert(b);
 
         assert(a);
         assert(b);
 
-        r = dns_name_equal(a->key.name, b->key.name);
+        r = dns_resource_key_equal(a->key, b->key);
         if (r <= 0)
                 return r;
 
         if (r <= 0)
                 return r;
 
-        if (a->key.class != b->key.class)
-                return 0;
-
-        if (a->key.type != b->key.type)
-                return 0;
-
-        if (IN_SET(a->key.type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
+        if (IN_SET(a->key->type, DNS_TYPE_PTR, DNS_TYPE_NS, DNS_TYPE_CNAME))
                 return dns_name_equal(a->ptr.name, b->ptr.name);
                 return dns_name_equal(a->ptr.name, b->ptr.name);
-        else if (a->key.type == DNS_TYPE_HINFO)
+        else if (a->key->type == DNS_TYPE_HINFO)
                 return strcasecmp(a->hinfo.cpu, b->hinfo.cpu) == 0 &&
                        strcasecmp(a->hinfo.os, b->hinfo.os) == 0;
                 return strcasecmp(a->hinfo.cpu, b->hinfo.cpu) == 0 &&
                        strcasecmp(a->hinfo.os, b->hinfo.os) == 0;
-        else if (a->key.type == DNS_TYPE_A)
+        else if (a->key->type == DNS_TYPE_A)
                 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
                 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
-        else if (a->key.type == DNS_TYPE_AAAA)
+        else if (a->key->type == DNS_TYPE_AAAA)
                 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
         else
                 return a->generic.size == b->generic.size &&
                 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
         else
                 return a->generic.size == b->generic.size &&