int dns_cache_export_shared_to_packet(DnsCache *cache, DnsPacket *p) {
unsigned ancount = 0;
DnsCacheItem *i;
+ usec_t t;
int r;
assert(cache);
assert(p);
+ t = now(CLOCK_BOOTTIME);
+
HASHMAP_FOREACH(i, cache->by_key)
LIST_FOREACH(by_key, j, i) {
if (!j->rr)
if (!j->shared_owner)
continue;
+ /* RFC6762 7.1: Don't append records with less than half the TTL remaining
+ * as known answers. */
+ if (usec_sub_unsigned(j->until, t) < j->rr->ttl * USEC_PER_SEC / 2)
+ continue;
+
r = dns_packet_append_rr(p, j->rr, 0, NULL, NULL);
if (r == -EMSGSIZE && p->protocol == DNS_PROTOCOL_MDNS) {
/* For mDNS, if we're unable to stuff all known answers into the given packet,