From 1117a96087b18023727192574a7d43be9344fc23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 12 May 2022 16:45:49 +0200 Subject: [PATCH] resolved: add DNS_ANSWER_REPLACE C.f. ce913e0ec4c97651c7c1509b72fb81ee61d80c6a. --- src/resolve/resolved-dns-answer.c | 7 ++----- src/resolve/resolved-dns-answer.h | 8 ++++++++ src/resolve/resolved-dns-cache.c | 4 +--- src/resolve/resolved-dns-query.c | 7 ++----- src/resolve/resolved-dns-transaction.c | 6 ++---- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/resolve/resolved-dns-answer.c b/src/resolve/resolved-dns-answer.c index 0394c3ec84b..93632733e97 100644 --- a/src/resolve/resolved-dns-answer.c +++ b/src/resolve/resolved-dns-answer.c @@ -490,9 +490,7 @@ int dns_answer_extend(DnsAnswer **a, DnsAnswer *b) { if (r < 0) return r; - dns_answer_unref(*a); - *a = merged; - + DNS_ANSWER_REPLACE(*a, merged); return 0; } @@ -714,8 +712,7 @@ int dns_answer_reserve_or_clone(DnsAnswer **a, size_t n_free) { if (r < 0) return r; - dns_answer_unref(*a); - *a = TAKE_PTR(n); + DNS_ANSWER_REPLACE(*a, TAKE_PTR(n)); return 0; } diff --git a/src/resolve/resolved-dns-answer.h b/src/resolve/resolved-dns-answer.h index 414c03192a4..a249f062738 100644 --- a/src/resolve/resolved-dns-answer.h +++ b/src/resolve/resolved-dns-answer.h @@ -45,6 +45,14 @@ DnsAnswer *dns_answer_new(size_t n); DnsAnswer *dns_answer_ref(DnsAnswer *a); DnsAnswer *dns_answer_unref(DnsAnswer *a); +#define DNS_ANSWER_REPLACE(a, b) \ + do { \ + typeof(a)* _a = &(a); \ + typeof(b) _b = (b); \ + dns_answer_unref(*_a); \ + *_a = _b; \ + } while(0) + int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags, DnsResourceRecord *rrsig); int dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags, DnsResourceRecord *rrsig); int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex); diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 45bcbfd9d78..08d9dd3e24e 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -385,9 +385,7 @@ static void dns_cache_item_update_positive( dns_resource_key_unref(i->key); i->key = dns_resource_key_ref(rr->key); - dns_answer_ref(answer); - dns_answer_unref(i->answer); - i->answer = answer; + DNS_ANSWER_REPLACE(i->answer, dns_answer_ref(answer)); dns_packet_ref(full_packet); dns_packet_unref(i->full_packet); diff --git a/src/resolve/resolved-dns-query.c b/src/resolve/resolved-dns-query.c index b62c5c349f5..32859174716 100644 --- a/src/resolve/resolved-dns-query.c +++ b/src/resolve/resolved-dns-query.c @@ -850,9 +850,7 @@ static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) { q->answer_query_flags |= dns_transaction_source_to_query_flags(t->answer_source); } else { /* Override non-successful previous answers */ - dns_answer_unref(q->answer); - q->answer = dns_answer_ref(t->answer); - + DNS_ANSWER_REPLACE(q->answer, dns_answer_ref(t->answer)); q->answer_query_flags = dns_transaction_source_to_query_flags(t->answer_source); } @@ -896,8 +894,7 @@ static void dns_query_accept(DnsQuery *q, DnsQueryCandidate *c) { !FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) continue; - dns_answer_unref(q->answer); - q->answer = dns_answer_ref(t->answer); + DNS_ANSWER_REPLACE(q->answer, dns_answer_ref(t->answer)); q->answer_rcode = t->answer_rcode; q->answer_dnssec_result = t->answer_dnssec_result; q->answer_query_flags = t->answer_query_flags | dns_transaction_source_to_query_flags(t->answer_source); diff --git a/src/resolve/resolved-dns-transaction.c b/src/resolve/resolved-dns-transaction.c index fc909551427..55626d06e39 100644 --- a/src/resolve/resolved-dns-transaction.c +++ b/src/resolve/resolved-dns-transaction.c @@ -1364,8 +1364,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt * field is later replaced by the DNSSEC validated subset. The 'answer_auxiliary' field carries the * original complete record set, including RRSIG and friends. We use this when passing data to * clients that ask for DNSSEC metadata. */ - dns_answer_unref(t->answer); - t->answer = dns_answer_ref(p->answer); + DNS_ANSWER_REPLACE(t->answer, dns_answer_ref(p->answer)); t->answer_rcode = DNS_PACKET_RCODE(p); t->answer_dnssec_result = _DNSSEC_RESULT_INVALID; SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); @@ -3452,8 +3451,7 @@ int dns_transaction_validate_dnssec(DnsTransaction *t) { break; } - dns_answer_unref(t->answer); - t->answer = TAKE_PTR(validated); + DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated)); /* At this point the answer only contains validated * RRsets. Now, let's see if it actually answers the question