1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 00:51:24 +03:00

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.
This commit is contained in:
Lennart Poettering 2020-11-04 20:51:15 +01:00
parent b17b6a7401
commit 9c5fcb8ac7
3 changed files with 57 additions and 64 deletions

View File

@ -55,20 +55,19 @@ DnsAnswer *dns_answer_new(size_t n) {
a->n_ref = 1; a->n_ref = 1;
a->n_allocated = n; a->n_allocated = n;
a->set_items = TAKE_PTR(s); a->set_items = TAKE_PTR(s);
return a; return a;
} }
static void dns_answer_flush(DnsAnswer *a) { static void dns_answer_flush(DnsAnswer *a) {
DnsResourceRecord *rr; DnsAnswerItem *item;
if (!a) if (!a)
return; return;
a->set_items = set_free(a->set_items); a->set_items = set_free(a->set_items);
DNS_ANSWER_FOREACH(rr, a) DNS_ANSWER_FOREACH_ITEM(item, a)
dns_resource_record_unref(rr); dns_resource_record_unref(item->rr);
a->n_rrs = 0; 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) { static int dns_answer_add_raw_all(DnsAnswer *a, DnsAnswer *source) {
DnsResourceRecord *rr; DnsAnswerItem *item;
DnsAnswerFlags flags; int r;
int ifindex, r;
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, source) { DNS_ANSWER_FOREACH_ITEM(item, source) {
r = dns_answer_add_raw(a, rr, ifindex, flags); r = dns_answer_add_raw(
a,
item->rr,
item->ifindex,
item->flags);
if (r < 0) if (r < 0)
return r; 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) { static int dns_answer_add_all(DnsAnswer *a, DnsAnswer *b) {
DnsResourceRecord *rr; DnsAnswerItem *item;
DnsAnswerFlags flags; int r;
int ifindex, r;
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, b) { DNS_ANSWER_FOREACH_ITEM(item, b) {
r = dns_answer_add(a, rr, ifindex, flags); r = dns_answer_add(a, item->rr, item->ifindex, item->flags);
if (r < 0) if (r < 0)
return r; return r;
} }
@ -472,21 +473,20 @@ int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key) {
if ((*a)->n_ref > 1) { if ((*a)->n_ref > 1) {
_cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL; _cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL;
DnsAnswerFlags flags; DnsAnswerItem *item;
int ifindex;
copy = dns_answer_new((*a)->n_rrs); copy = dns_answer_new((*a)->n_rrs);
if (!copy) if (!copy)
return -ENOMEM; return -ENOMEM;
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) { DNS_ANSWER_FOREACH_ITEM(item, *a) {
r = dns_resource_key_equal(rr->key, key); r = dns_resource_key_equal(item->rr->key, key);
if (r < 0) if (r < 0)
return r; return r;
if (r > 0) if (r > 0)
continue; 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) if (r < 0)
return r; return r;
} }
@ -557,21 +557,20 @@ int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rm) {
if ((*a)->n_ref > 1) { if ((*a)->n_ref > 1) {
_cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL; _cleanup_(dns_answer_unrefp) DnsAnswer *copy = NULL;
DnsAnswerFlags flags; DnsAnswerItem *item;
int ifindex;
copy = dns_answer_new((*a)->n_rrs); copy = dns_answer_new((*a)->n_rrs);
if (!copy) if (!copy)
return -ENOMEM; return -ENOMEM;
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, *a) { DNS_ANSWER_FOREACH_ITEM(item, *a) {
r = dns_resource_record_equal(rr, rm); r = dns_resource_record_equal(item->rr, rm);
if (r < 0) if (r < 0)
return r; return r;
if (r > 0) if (r > 0)
continue; 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) if (r < 0)
return r; 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) { int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags) {
DnsResourceRecord *rr_source; DnsAnswerItem *item;
int ifindex_source, r; int r;
DnsAnswerFlags flags_source;
assert(a); assert(a);
assert(key); assert(key);
/* Copy all RRs matching the specified key from source into *a */ /* 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) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
@ -631,7 +629,7 @@ int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKe
if (r < 0) if (r < 0)
return r; 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) if (r < 0)
return r; 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. * 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) { void dns_answer_dump(DnsAnswer *answer, FILE *f) {
DnsResourceRecord *rr; DnsAnswerItem *item;
DnsAnswerFlags flags;
int ifindex;
if (!f) if (!f)
f = stdout; f = stdout;
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, answer) { DNS_ANSWER_FOREACH_ITEM(item, answer) {
const char *t; const char *t;
fputc('\t', f); fputc('\t', f);
t = dns_resource_record_to_string(rr); t = dns_resource_record_to_string(item->rr);
if (!t) { if (!t) {
log_oom(); log_oom();
continue; continue;
@ -796,20 +792,20 @@ void dns_answer_dump(DnsAnswer *answer, FILE *f) {
fputs(t, f); fputs(t, f);
if (ifindex != 0 || flags != 0) if (item->ifindex != 0 || item->flags != 0)
fputs("\t;", f); fputs("\t;", f);
if (ifindex != 0) if (item->ifindex != 0)
fprintf(f, " ifindex=%i", ifindex); fprintf(f, " ifindex=%i", item->ifindex);
if (flags & DNS_ANSWER_AUTHENTICATED) if (item->flags & DNS_ANSWER_AUTHENTICATED)
fputs(" authenticated", f); fputs(" authenticated", f);
if (flags & DNS_ANSWER_CACHEABLE) if (item->flags & DNS_ANSWER_CACHEABLE)
fputs(" cacheable", f); fputs(" cacheable", f);
if (flags & DNS_ANSWER_SHARED_OWNER) if (item->flags & DNS_ANSWER_SHARED_OWNER)
fputs(" shared-owner", f); fputs(" shared-owner", f);
if (flags & DNS_ANSWER_CACHE_FLUSH) if (item->flags & DNS_ANSWER_CACHE_FLUSH)
fputs(" cache-flush", f); fputs(" cache-flush", f);
if (flags & DNS_ANSWER_GOODBYE) if (item->flags & DNS_ANSWER_GOODBYE)
fputs(" goodbye", f); fputs(" goodbye", f);
fputc('\n', f); fputc('\n', f);

View File

@ -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_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) = ({ \ for (size_t UNIQ_T(i, q) = ({ \
(kk) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].rr : NULL; \ (item) = dns_answer_isempty(a) ? NULL : (a)->items; \
(ifi) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].ifindex : 0; \
(fl) = ((a) && (a)->n_rrs > 0) ? (a)->items[0].flags : 0; \
0; \ 0; \
}); \ }); \
(a) && (UNIQ_T(i, q) < (a)->n_rrs); \ UNIQ_T(i, q) < dns_answer_size(a); \
UNIQ_T(i, q)++, \ UNIQ_T(i, q)++, \
(kk) = ((UNIQ_T(i, q) < (a)->n_rrs) ? (a)->items[UNIQ_T(i, q)].rr : NULL), \ (item) = ((UNIQ_T(i, q) < dns_answer_size(a)) ? (a)->items + UNIQ_T(i, q) : 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))
#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)

View File

@ -636,11 +636,12 @@ int dns_cache_put(
int owner_family, int owner_family,
const union in_addr_union *owner_address) { const union in_addr_union *owner_address) {
DnsResourceRecord *soa = NULL, *rr; DnsResourceRecord *soa = NULL;
bool weird_rcode = false; bool weird_rcode = false;
DnsAnswerItem *item;
DnsAnswerFlags flags; DnsAnswerFlags flags;
unsigned cache_keys; unsigned cache_keys;
int r, ifindex; int r;
assert(c); assert(c);
assert(owner_address); assert(owner_address);
@ -683,18 +684,18 @@ int dns_cache_put(
timestamp = now(clock_boottime_or_monotonic()); timestamp = now(clock_boottime_or_monotonic());
/* Second, add in positive entries for all contained RRs */ /* Second, add in positive entries for all contained RRs */
DNS_ANSWER_FOREACH_FULL(rr, ifindex, flags, answer) { DNS_ANSWER_FOREACH_ITEM(item, answer) {
if ((flags & DNS_ANSWER_CACHEABLE) == 0 || if ((item->flags & DNS_ANSWER_CACHEABLE) == 0 ||
!rr_eligible(rr)) !rr_eligible(item->rr))
continue; continue;
r = dns_cache_put_positive( r = dns_cache_put_positive(
c, c,
rr, item->rr,
flags & DNS_ANSWER_AUTHENTICATED, item->flags & DNS_ANSWER_AUTHENTICATED,
flags & DNS_ANSWER_SHARED_OWNER, item->flags & DNS_ANSWER_SHARED_OWNER,
timestamp, timestamp,
ifindex, item->ifindex,
owner_family, owner_address); owner_family, owner_address);
if (r < 0) if (r < 0)
goto fail; goto fail;
@ -762,11 +763,11 @@ fail:
if (key) if (key)
dns_cache_remove_by_key(c, key); dns_cache_remove_by_key(c, key);
DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) { DNS_ANSWER_FOREACH_ITEM(item, answer) {
if ((flags & DNS_ANSWER_CACHEABLE) == 0) if ((item->flags & DNS_ANSWER_CACHEABLE) == 0)
continue; continue;
dns_cache_remove_by_key(c, rr->key); dns_cache_remove_by_key(c, item->rr->key);
} }
return r; return r;