summaryrefslogtreecommitdiffstats
path: root/src/resolve/resolved-dns-question.c
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@gmail.com>2021-02-20 15:32:24 +0100
committerSergey Bugaev <bugaevc@gmail.com>2021-03-31 11:54:05 +0200
commitab715ddbaf9f45f352c4c018a648454df60b468b (patch)
tree91e979ef7c8f40b27a6b38489aa10a2b11a4bee5 /src/resolve/resolved-dns-question.c
parentportabled: add --extension parameter for layered images support (diff)
downloadsystemd-ab715ddbaf9f45f352c4c018a648454df60b468b.tar.xz
systemd-ab715ddbaf9f45f352c4c018a648454df60b468b.zip
resolved: add flags to DnsQuestion items
This adds DnsQuestionItem, which is to DnsQuestion what DnsAnswerItem is to DnsAnswer. No functional change.
Diffstat (limited to 'src/resolve/resolved-dns-question.c')
-rw-r--r--src/resolve/resolved-dns-question.c103
1 files changed, 63 insertions, 40 deletions
diff --git a/src/resolve/resolved-dns-question.c b/src/resolve/resolved-dns-question.c
index ef40932630..aefdaa0eeb 100644
--- a/src/resolve/resolved-dns-question.c
+++ b/src/resolve/resolved-dns-question.c
@@ -11,7 +11,7 @@ DnsQuestion *dns_question_new(size_t n) {
if (n > UINT16_MAX) /* We can only place 64K key in an question section at max */
n = UINT16_MAX;
- q = malloc0(offsetof(DnsQuestion, keys) + sizeof(DnsResourceKey*) * n);
+ q = malloc0(offsetof(DnsQuestion, items) + sizeof(DnsQuestionItem) * n);
if (!q)
return NULL;
@@ -22,18 +22,19 @@ DnsQuestion *dns_question_new(size_t n) {
}
static DnsQuestion *dns_question_free(DnsQuestion *q) {
- size_t i;
+ DnsResourceKey *key;
assert(q);
- for (i = 0; i < q->n_keys; i++)
- dns_resource_key_unref(q->keys[i]);
+ DNS_QUESTION_FOREACH(key, q)
+ dns_resource_key_unref(key);
+
return mfree(q);
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsQuestion, dns_question, dns_question_free);
-int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key) {
+int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags) {
/* Insert without checking for duplicates. */
assert(key);
@@ -42,11 +43,15 @@ int dns_question_add_raw(DnsQuestion *q, DnsResourceKey *key) {
if (q->n_keys >= q->n_allocated)
return -ENOSPC;
- q->keys[q->n_keys++] = dns_resource_key_ref(key);
+ q->items[q->n_keys++] = (DnsQuestionItem) {
+ .key = dns_resource_key_ref(key),
+ .flags = flags,
+ };
return 0;
}
-int dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
+int dns_question_add(DnsQuestion *q, DnsResourceKey *key, DnsQuestionFlags flags) {
+ DnsQuestionItem *item;
int r;
assert(key);
@@ -54,19 +59,20 @@ int dns_question_add(DnsQuestion *q, DnsResourceKey *key) {
if (!q)
return -ENOSPC;
- for (size_t i = 0; i < q->n_keys; i++) {
- r = dns_resource_key_equal(q->keys[i], key);
+
+ DNS_QUESTION_FOREACH_ITEM(item, q) {
+ r = dns_resource_key_equal(item->key, key);
if (r < 0)
return r;
- if (r > 0)
+ if (r > 0 && item->flags == flags)
return 0;
}
- return dns_question_add_raw(q, key);
+ return dns_question_add_raw(q, key, flags);
}
int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
- size_t i;
+ DnsResourceKey *key;
int r;
assert(rr);
@@ -74,8 +80,8 @@ int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *s
if (!q)
return 0;
- for (i = 0; i < q->n_keys; i++) {
- r = dns_resource_key_match_rr(q->keys[i], rr, search_domain);
+ DNS_QUESTION_FOREACH(key, q) {
+ r = dns_resource_key_match_rr(key, rr, search_domain);
if (r != 0)
return r;
}
@@ -84,7 +90,7 @@ int dns_question_matches_rr(DnsQuestion *q, DnsResourceRecord *rr, const char *s
}
int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, const char *search_domain) {
- size_t i;
+ DnsResourceKey *key;
int r;
assert(rr);
@@ -95,12 +101,12 @@ int dns_question_matches_cname_or_dname(DnsQuestion *q, DnsResourceRecord *rr, c
if (!IN_SET(rr->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME))
return 0;
- for (i = 0; i < q->n_keys; i++) {
+ DNS_QUESTION_FOREACH(key, q) {
/* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
- if (!dns_type_may_redirect(q->keys[i]->type))
+ if (!dns_type_may_redirect(key->type))
return 0;
- r = dns_resource_key_match_cname_or_dname(q->keys[i], rr->key, search_domain);
+ r = dns_resource_key_match_cname_or_dname(key, rr->key, search_domain);
if (r != 0)
return r;
}
@@ -122,38 +128,39 @@ int dns_question_is_valid_for_query(DnsQuestion *q) {
if (q->n_keys > 65535)
return 0;
- name = dns_resource_key_name(q->keys[0]);
+ name = dns_resource_key_name(q->items[0].key);
if (!name)
return 0;
/* Check that all keys in this question bear the same name */
for (i = 0; i < q->n_keys; i++) {
- assert(q->keys[i]);
+ assert(q->items[i].key);
if (i > 0) {
- r = dns_name_equal(dns_resource_key_name(q->keys[i]), name);
+ r = dns_name_equal(dns_resource_key_name(q->items[i].key), name);
if (r <= 0)
return r;
}
- if (!dns_type_is_valid_query(q->keys[i]->type))
+ if (!dns_type_is_valid_query(q->items[i].key->type))
return 0;
}
return 1;
}
-int dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) {
+int dns_question_contains_key(DnsQuestion *q, const DnsResourceKey *k) {
size_t j;
int r;
assert(k);
- if (!a)
+ if (!q)
return 0;
- for (j = 0; j < a->n_keys; j++) {
- r = dns_resource_key_equal(a->keys[j], k);
+
+ for (j = 0; j < q->n_keys; j++) {
+ r = dns_resource_key_equal(q->items[j].key, k);
if (r != 0)
return r;
}
@@ -161,8 +168,25 @@ int dns_question_contains(DnsQuestion *a, const DnsResourceKey *k) {
return 0;
}
+static int dns_question_contains_item(DnsQuestion *q, const DnsQuestionItem *i) {
+ DnsQuestionItem *item;
+ int r;
+
+ assert(i);
+
+ DNS_QUESTION_FOREACH_ITEM(item, q) {
+ if (item->flags != i->flags)
+ continue;
+ r = dns_resource_key_equal(item->key, i->key);
+ if (r != 0)
+ return r;
+ }
+
+ return false;
+}
+
int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
- size_t j;
+ DnsQuestionItem *item;
int r;
if (a == b)
@@ -173,16 +197,15 @@ int dns_question_is_equal(DnsQuestion *a, DnsQuestion *b) {
if (!b)
return a->n_keys == 0;
- /* Checks if all keys in a are also contained b, and vice versa */
+ /* Checks if all items in a are also contained b, and vice versa */
- for (j = 0; j < a->n_keys; j++) {
- r = dns_question_contains(b, a->keys[j]);
+ DNS_QUESTION_FOREACH_ITEM(item, a) {
+ r = dns_question_contains_item(b, item);
if (r <= 0)
return r;
}
-
- for (j = 0; j < b->n_keys; j++) {
- r = dns_question_contains(a, b->keys[j]);
+ DNS_QUESTION_FOREACH_ITEM(item, b) {
+ r = dns_question_contains_item(a, item);
if (r <= 0)
return r;
}
@@ -249,7 +272,7 @@ int dns_question_cname_redirect(DnsQuestion *q, const DnsResourceRecord *cname,
if (!k)
return -ENOMEM;
- r = dns_question_add(n, k);
+ r = dns_question_add(n, k, 0);
if (r < 0)
return r;
}
@@ -267,7 +290,7 @@ const char *dns_question_first_name(DnsQuestion *q) {
if (q->n_keys < 1)
return NULL;
- return dns_resource_key_name(q->keys[0]);
+ return dns_resource_key_name(q->items[0].key);
}
int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bool convert_idna) {
@@ -306,7 +329,7 @@ int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bo
if (!key)
return -ENOMEM;
- r = dns_question_add(q, key);
+ r = dns_question_add(q, key, 0);
if (r < 0)
return r;
}
@@ -318,7 +341,7 @@ int dns_question_new_address(DnsQuestion **ret, int family, const char *name, bo
if (!key)
return -ENOMEM;
- r = dns_question_add(q, key);
+ r = dns_question_add(q, key, 0);
if (r < 0)
return r;
}
@@ -354,7 +377,7 @@ int dns_question_new_reverse(DnsQuestion **ret, int family, const union in_addr_
reverse = NULL;
- r = dns_question_add(q, key);
+ r = dns_question_add(q, key, 0);
if (r < 0)
return r;
@@ -426,7 +449,7 @@ int dns_question_new_service(
if (!key)
return -ENOMEM;
- r = dns_question_add(q, key);
+ r = dns_question_add(q, key, 0);
if (r < 0)
return r;
@@ -436,7 +459,7 @@ int dns_question_new_service(
if (!key)
return -ENOMEM;
- r = dns_question_add(q, key);
+ r = dns_question_add(q, key, 0);
if (r < 0)
return r;
}