From a1acc6e332b05f6a5167bf9d0bc0657794e1342c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 15 Mar 2021 21:18:52 +0100 Subject: [PATCH] resolved: let's tweak how we calculate TTL left MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When responding from DNS cache, let's slightly tweak how the TTL is lowered: as before let's round down when converting from our internal µs to the external seconds. (This is preferable, since records should better be cached too short instead of too long.) Let's avoid rounding down to zero though, since that has special semantics in many cases (in particular mDNS). Let's just use 1s in that case. --- src/resolve/resolved-dns-cache.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index db2361ae363..9b2e7115c0a 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -937,9 +937,18 @@ static int answer_add_clamp_ttl( assert(rr); if (FLAGS_SET(query_flags, SD_RESOLVED_CLAMP_TTL)) { + uint32_t left_ttl; + + /* Let's determine how much time is left for this cache entry. Note that we round down, but + * clamp this to be 1s at minimum, since we usually want records to remain cached better too + * short a time than too long a time, but otoh don't want to return 0 ever, since that has + * special semantics in various contexts — in particular in mDNS */ + + left_ttl = MAX(1U, LESS_BY(until, current) / USEC_PER_SEC); + patched = dns_resource_record_ref(rr); - r = dns_resource_record_clamp_ttl(&patched, LESS_BY(until, current) / USEC_PER_SEC); + r = dns_resource_record_clamp_ttl(&patched, left_ttl); if (r < 0) return r; @@ -947,7 +956,7 @@ static int answer_add_clamp_ttl( if (rrsig) { patched_rrsig = dns_resource_record_ref(rrsig); - r = dns_resource_record_clamp_ttl(&patched_rrsig, LESS_BY(until, current) / USEC_PER_SEC); + r = dns_resource_record_clamp_ttl(&patched_rrsig, left_ttl); if (r < 0) return r; -- 2.47.3