summaryrefslogtreecommitdiff
path: root/src/resolve
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-11-04 20:51:15 +0100
committerLennart Poettering <lennart@poettering.net>2021-02-09 17:52:49 +0100
commit9c5fcb8ac7afb9c958a222760ab6edb529ba49a5 (patch)
tree8ae1693b75fcd5c082eb33f3cfef618a6af28043 /src/resolve
parentb17b6a74015de4a56f7fc5fd786e88fdd376f356 (diff)
downloadsystemd-9c5fcb8ac7afb9c958a222760ab6edb529ba49a5.tar.gz
resolved: replace DNS_ANSWER_FOREACH_FULL() iterator macro with DNS_ANSWER_FOREACH_ITEM()
The more fields DnsAnswerItem gains the less sense it makes to pass every field of it as separate parameter to an iterator macro. Let's simplify things here, in preparation of adding more fields to the structure later on: let's just return the structure itself in the loop, rather than the individual fields.
Diffstat (limited to 'src/resolve')
-rw-r--r--src/resolve/resolved-dns-answer.c82
-rw-r--r--src/resolve/resolved-dns-answer.h14
-rw-r--r--src/resolve/resolved-dns-cache.c25
3 files changed, 57 insertions, 64 deletions
diff --git a/src/resolve/resolved-dns-answer.c b/src/resolve/resolved-dns-answer.c
index f2ed877b53..ca79f66c78 100644
--- a/src/resolve/resolved-dns-answer.c
+++ b/src/resolve/resolved-dns-answer.c
@@ -55,20 +55,19 @@ DnsAnswer *dns_answer_new(size_t n) {
a->n_ref = 1;
a->n_allocated = n;
a->set_items = TAKE_PTR(s);
-
return a;
}
static void dns_answer_flush(DnsAnswer *a) {
- DnsResourceRecord *rr;
+ DnsAnswerItem *item;
if (!a)
return;
a->set_items = set_free(a->set_items);
- DNS_ANSWER_FOREACH(rr, a)
- dns_resource_record_unref(rr);
+ DNS_ANSWER_FOREACH_ITEM(item, a)
+ dns_resource_record_unref(item->rr);
a->n_rrs = 0;
}
@@ -112,12 +111,15 @@ static int dns_answer_add_raw(DnsAnswer *a, DnsResourceRecord *rr, int ifindex,
}
static int dns_answer_add_raw_all(DnsAnswer *a, DnsAnswer *source) {
- DnsResourceRecord *rr;
- DnsAnswerFlags flags;
- int ifindex, r;
+ DnsAnswerItem *item;
+ int r;
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, source) {
- r = dns_answer_add_raw(a, rr, ifindex, flags);
+ DNS_ANSWER_FOREACH_ITEM(item, source) {
+ r = dns_answer_add_raw(
+ a,
+ item->rr,
+ item->ifindex,
+ item->flags);
if (r < 0)
return r;
}
@@ -162,12 +164,11 @@ int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFl
}
static int dns_answer_add_all(DnsAnswer *a, DnsAnswer *b) {
- DnsResourceRecord *rr;
- DnsAnswerFlags flags;
- int ifindex, r;
+ DnsAnswerItem *item;
+ int r;
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, b) {
- r = dns_answer_add(a, rr, ifindex, flags);
+ DNS_ANSWER_FOREACH_ITEM(item, b) {
+ r = dns_answer_add(a, item->rr, item->ifindex, item->flags);
if (r < 0)
return r;
}
@@ -472,21 +473,20 @@ int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key) {
if ((*a)->n_ref > 1) {
_cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL;
- DnsAnswerFlags flags;
- int ifindex;
+ DnsAnswerItem *item;
copy = dns_answer_new((*a)->n_rrs);
if (!copy)
return -ENOMEM;
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) {
- r = dns_resource_key_equal(rr->key, key);
+ DNS_ANSWER_FOREACH_ITEM(item, *a) {
+ r = dns_resource_key_equal(item->rr->key, key);
if (r < 0)
return r;
if (r > 0)
continue;
- r = dns_answer_add_raw(copy, rr, ifindex, flags);
+ r = dns_answer_add_raw(copy, item->rr, item->ifindex, item->flags);
if (r < 0)
return r;
}
@@ -557,21 +557,20 @@ int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rm) {
if ((*a)->n_ref > 1) {
_cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL;
- DnsAnswerFlags flags;
- int ifindex;
+ DnsAnswerItem *item;
copy = dns_answer_new((*a)->n_rrs);
if (!copy)
return -ENOMEM;
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) {
- r = dns_resource_record_equal(rr, rm);
+ DNS_ANSWER_FOREACH_ITEM(item, *a) {
+ r = dns_resource_record_equal(item->rr, rm);
if (r < 0)
return r;
if (r > 0)
continue;
- r = dns_answer_add_raw(copy, rr, ifindex, flags);
+ r = dns_answer_add_raw(copy, item->rr, item->ifindex, item->flags);
if (r < 0)
return r;
}
@@ -609,18 +608,17 @@ int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rm) {
}
int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags) {
- DnsResourceRecord *rr_source;
- int ifindex_source, r;
- DnsAnswerFlags flags_source;
+ DnsAnswerItem *item;
+ int r;
assert(a);
assert(key);
/* Copy all RRs matching the specified key from source into *a */
- DNS_ANSWER_FOREACH_FULL(rr_source, ifindex_source, flags_source, source) {
+ DNS_ANSWER_FOREACH_ITEM(item, source) {
- r = dns_resource_key_equal(rr_source->key, key);
+ r = dns_resource_key_equal(item->rr->key, key);
if (r < 0)
return r;
if (r == 0)
@@ -631,7 +629,7 @@ int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKe
if (r < 0)
return r;
- r = dns_answer_add(*a, rr_source, ifindex_source, flags_source|or_flags);
+ r = dns_answer_add(*a, item->rr, item->ifindex, item->flags|or_flags);
if (r < 0)
return r;
}
@@ -776,19 +774,17 @@ int dns_answer_reserve_or_clone(DnsAnswer **a, size_t n_free) {
* This function is not used in the code base, but is useful when debugging. Do not delete.
*/
void dns_answer_dump(DnsAnswer *answer, FILE *f) {
- DnsResourceRecord *rr;
- DnsAnswerFlags flags;
- int ifindex;
+ DnsAnswerItem *item;
if (!f)
f = stdout;
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, answer) {
+ DNS_ANSWER_FOREACH_ITEM(item, answer) {
const char *t;
fputc('\t', f);
- t = dns_resource_record_to_string(rr);
+ t = dns_resource_record_to_string(item->rr);
if (!t) {
log_oom();
continue;
@@ -796,20 +792,20 @@ void dns_answer_dump(DnsAnswer *answer, FILE *f) {
fputs(t, f);
- if (ifindex != 0 || flags != 0)
+ if (item->ifindex != 0 || item->flags != 0)
fputs("\t;", f);
- if (ifindex != 0)
- fprintf(f, " ifindex=%i", ifindex);
- if (flags & DNS_ANSWER_AUTHENTICATED)
+ if (item->ifindex != 0)
+ fprintf(f, " ifindex=%i", item->ifindex);
+ if (item->flags & DNS_ANSWER_AUTHENTICATED)
fputs(" authenticated", f);
- if (flags & DNS_ANSWER_CACHEABLE)
+ if (item->flags & DNS_ANSWER_CACHEABLE)
fputs(" cacheable", f);
- if (flags & DNS_ANSWER_SHARED_OWNER)
+ if (item->flags & DNS_ANSWER_SHARED_OWNER)
fputs(" shared-owner", f);
- if (flags & DNS_ANSWER_CACHE_FLUSH)
+ if (item->flags & DNS_ANSWER_CACHE_FLUSH)
fputs(" cache-flush", f);
- if (flags & DNS_ANSWER_GOODBYE)
+ if (item->flags & DNS_ANSWER_GOODBYE)
fputs(" goodbye", f);
fputc('\n', f);
diff --git a/src/resolve/resolved-dns-answer.h b/src/resolve/resolved-dns-answer.h
index 1aff4f8942..8f91a307b6 100644
--- a/src/resolve/resolved-dns-answer.h
+++ b/src/resolve/resolved-dns-answer.h
@@ -116,17 +116,13 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
#define DNS_ANSWER_FOREACH_FLAGS(kk, flags, a) _DNS_ANSWER_FOREACH_FLAGS(UNIQ, kk, flags, a)
-#define _DNS_ANSWER_FOREACH_FULL(q, kk, ifi, fl, a) \
+#define _DNS_ANSWER_FOREACH_ITEM(q, item, a) \
for (size_t UNIQ_T(i, q) = ({ \
- (kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \
- (ifi) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].ifindex : 0; \
- (fl) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].flags : 0; \
+ (item) = dns_answer_isempty(a) ? NULL : (a)->items; \
0; \
}); \
- (a) && (UNIQ_T(i, q) < (a)->n_rrs); \
+ UNIQ_T(i, q) < dns_answer_size(a); \
UNIQ_T(i, q)++, \
- (kk) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].rr : NULL), \
- (ifi) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].ifindex : 0), \
- (fl) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].flags : 0))
+ (item) = ((UNIQ_T(i, q) < dns_answer_size(a)) ? (a)->items + UNIQ_T(i, q) : NULL))
-#define DNS_ANSWER_FOREACH_FULL(kk, ifindex, flags, a) _DNS_ANSWER_FOREACH_FULL(UNIQ, kk, ifindex, flags, a)
+#define DNS_ANSWER_FOREACH_ITEM(item, a) _DNS_ANSWER_FOREACH_ITEM(UNIQ, item, a)
diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c
index 75f1ccb649..6cdf010309 100644
--- a/src/resolve/resolved-dns-cache.c
+++ b/src/resolve/resolved-dns-cache.c
@@ -636,11 +636,12 @@ int dns_cache_put(
int owner_family,
const union in_addr_union *owner_address) {
- DnsResourceRecord *soa = NULL, *rr;
+ DnsResourceRecord *soa = NULL;
bool weird_rcode = false;
+ DnsAnswerItem *item;
DnsAnswerFlags flags;
unsigned cache_keys;
- int r, ifindex;
+ int r;
assert(c);
assert(owner_address);
@@ -683,18 +684,18 @@ int dns_cache_put(
timestamp = now(clock_boottime_or_monotonic());
/* Second, add in positive entries for all contained RRs */
- DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, answer) {
- if ((flags & DNS_ANSWER_CACHEABLE) == 0 ||
- !rr_eligible(rr))
+ DNS_ANSWER_FOREACH_ITEM(item, answer) {
+ if ((item->flags & DNS_ANSWER_CACHEABLE) == 0 ||
+ !rr_eligible(item->rr))
continue;
r = dns_cache_put_positive(
c,
- rr,
- flags & DNS_ANSWER_AUTHENTICATED,
- flags & DNS_ANSWER_SHARED_OWNER,
+ item->rr,
+ item->flags & DNS_ANSWER_AUTHENTICATED,
+ item->flags & DNS_ANSWER_SHARED_OWNER,
timestamp,
- ifindex,
+ item->ifindex,
owner_family, owner_address);
if (r < 0)
goto fail;
@@ -762,11 +763,11 @@ fail:
if (key)
dns_cache_remove_by_key(c, key);
- DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) {
- if ((flags & DNS_ANSWER_CACHEABLE) == 0)
+ DNS_ANSWER_FOREACH_ITEM(item, answer) {
+ if ((item->flags & DNS_ANSWER_CACHEABLE) == 0)
continue;
- dns_cache_remove_by_key(c, rr->key);
+ dns_cache_remove_by_key(c, item->rr->key);
}
return r;