From: Yu Watanabe Date: Sat, 9 Jul 2022 06:56:50 +0000 (+0900) Subject: resolve: introduce FORMAT_DNS_RCODE() macro X-Git-Tag: v252-rc1~689 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0d609349ba7e4df07c548c1cfe5127b431de7554;p=thirdparty%2Fsystemd.git resolve: introduce FORMAT_DNS_RCODE() macro Fixes #23958. --- diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c index 9e7d1cc380c..1304965d4e2 100644 --- a/src/resolve/resolved-bus.c +++ b/src/resolve/resolved-bus.c @@ -180,14 +180,8 @@ static int reply_query_state(DnsQuery *q) { sd_bus_error_setf(&error, _BUS_ERROR_DNS "NXDOMAIN", "'%s' not found", dns_query_string(q)); else { const char *rc, *n; - char p[DECIMAL_STR_MAX(q->answer_rcode)]; - - rc = dns_rcode_to_string(q->answer_rcode); - if (!rc) { - xsprintf(p, "%i", q->answer_rcode); - rc = p; - } + rc = FORMAT_DNS_RCODE(q->answer_rcode); n = strjoina(_BUS_ERROR_DNS, rc); sd_bus_error_setf(&error, n, "Could not resolve '%s', server or network returned error %s", dns_query_string(q), rc); } diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 0856976d3ef..0f084b56fc6 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -1099,7 +1099,7 @@ int dns_cache_lookup( if (found_rcode >= 0) { log_debug("RCODE %s cache hit for %s", - dns_rcode_to_string(found_rcode), + FORMAT_DNS_RCODE(found_rcode), dns_resource_key_to_string(key, key_str, sizeof(key_str))); if (ret_rcode) diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index 97809439a58..88c52e411b3 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -9,6 +9,7 @@ #include "memory-util.h" #include "resolved-dns-packet.h" #include "set.h" +#include "stdio-util.h" #include "string-table.h" #include "strv.h" #include "unaligned.h" @@ -2649,6 +2650,14 @@ static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = { }; DEFINE_STRING_TABLE_LOOKUP(dns_rcode, int); +const char *format_dns_rcode(int i, char buf[static DECIMAL_STR_MAX(int)]) { + const char *p = dns_rcode_to_string(i); + if (p) + return p; + + return snprintf_ok(buf, DECIMAL_STR_MAX(int), "%i", i); +} + static const char* const dns_protocol_table[_DNS_PROTOCOL_MAX] = { [DNS_PROTOCOL_DNS] = "dns", [DNS_PROTOCOL_MDNS] = "mdns", diff --git a/src/resolve/resolved-dns-packet.h b/src/resolve/resolved-dns-packet.h index 95b0b506ea6..505e3e7ba94 100644 --- a/src/resolve/resolved-dns-packet.h +++ b/src/resolve/resolved-dns-packet.h @@ -283,6 +283,8 @@ enum { const char* dns_rcode_to_string(int i) _const_; int dns_rcode_from_string(const char *s) _pure_; +const char *format_dns_rcode(int i, char buf[static DECIMAL_STR_MAX(int)]); +#define FORMAT_DNS_RCODE(i) format_dns_rcode(i, (char [DECIMAL_STR_MAX(int)]) {}) const char* dns_protocol_to_string(DnsProtocol p) _const_; DnsProtocol dns_protocol_from_string(const char *s) _pure_; diff --git a/src/resolve/resolved-dns-transaction.c b/src/resolve/resolved-dns-transaction.c index 6acf251d60c..27685a90a98 100644 --- a/src/resolve/resolved-dns-transaction.c +++ b/src/resolve/resolved-dns-transaction.c @@ -864,7 +864,7 @@ static int dns_transaction_dnssec_ready(DnsTransaction *t) { case DNS_TRANSACTION_RCODE_FAILURE: if (!IN_SET(dt->answer_rcode, DNS_RCODE_NXDOMAIN, DNS_RCODE_SERVFAIL)) { - log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", dns_rcode_to_string(dt->answer_rcode)); + log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", FORMAT_DNS_RCODE(dt->answer_rcode)); goto fail; } @@ -1049,7 +1049,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt log_debug("Processing incoming packet of size %zu on transaction %" PRIu16" (rcode=%s).", p->size, - t->id, dns_rcode_to_string(DNS_PACKET_RCODE(p))); + t->id, FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); switch (t->scope->protocol) { @@ -1137,7 +1137,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt return; /* Give up, accept the rcode */ - log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p))); + log_debug("Server returned error: %s", FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); break; } @@ -1151,7 +1151,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt t->clamp_feature_level_servfail < 0) { t->clamp_feature_level_servfail = t->current_feature_level; log_debug("Server returned error %s, retrying transaction.", - dns_rcode_to_string(DNS_PACKET_RCODE(p))); + FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); } else { /* Reduce this feature level by one and try again. */ switch (t->current_feature_level) { @@ -1167,7 +1167,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt } log_debug("Server returned error %s, retrying transaction with reduced feature level %s.", - dns_rcode_to_string(DNS_PACKET_RCODE(p)), + FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), dns_server_feature_level_to_string(t->clamp_feature_level_servfail)); } @@ -1308,7 +1308,7 @@ void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypt t->clamp_feature_level_nxdomain = DNS_SERVER_FEATURE_LEVEL_UDP; log_debug("Server returned error %s in EDNS0 mode, retrying transaction with reduced feature level %s (DVE-2018-0001 mitigation)", - dns_rcode_to_string(DNS_PACKET_RCODE(p)), + FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), dns_server_feature_level_to_string(t->clamp_feature_level_nxdomain)); dns_transaction_retry(t, false /* use the same server */);