From 6d519cd447614f9b2ae24806c4dc0a0221aa3e17 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 24 Oct 2023 21:05:35 +0200 Subject: [PATCH] pick proper TTL for non existent DNS entries --- .../de/gultsch/minidns/AndroidDNSClient.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/gultsch/minidns/AndroidDNSClient.java b/src/main/java/de/gultsch/minidns/AndroidDNSClient.java index 047799815..2c3731871 100644 --- a/src/main/java/de/gultsch/minidns/AndroidDNSClient.java +++ b/src/main/java/de/gultsch/minidns/AndroidDNSClient.java @@ -16,6 +16,8 @@ import com.google.common.collect.ImmutableList; import de.measite.minidns.AbstractDNSClient; import de.measite.minidns.DNSMessage; +import de.measite.minidns.Record; +import de.measite.minidns.record.Data; import eu.siacs.conversations.Config; @@ -27,6 +29,8 @@ import java.util.List; public class AndroidDNSClient extends AbstractDNSClient { + private static final long DNS_MAX_TTL = 86_400L; + private static final LruCache QUERY_CACHE = new LruCache<>(1024); private final Context context; @@ -168,10 +172,23 @@ public class AndroidDNSClient extends AbstractDNSClient { } } + private static long ttl(final DNSMessage dnsMessage) { + final List> answerSection = dnsMessage.answerSection; + if (answerSection == null || answerSection.isEmpty()) { + final List> authoritySection = dnsMessage.authoritySection; + if (authoritySection == null || authoritySection.isEmpty()) { + return 0; + } else { + return Collections.min(Collections2.transform(authoritySection, d -> d.ttl)); + } + + } else { + return Collections.min(Collections2.transform(answerSection, d -> d.ttl)); + } + } + private static long expiresAt(final DNSMessage dnsMessage) { - return dnsMessage.receiveTimestamp - + (Collections.min(Collections2.transform(dnsMessage.answerSection, d -> d.ttl)) - * 1000L); + return dnsMessage.receiveTimestamp + (Math.min(DNS_MAX_TTL, ttl(dnsMessage)) * 1000L); } private static long expiresIn(final DNSMessage dnsMessage) {