1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "resolved-dns-answer.h"
9 #include "resolved-dns-cache.h"
10 #include "resolved-dns-packet.h"
11 #include "string-util.h"
13 /* Never cache more than 4K entries. RFC 1536, Section 5 suggests to
14 * leave DNS caches unbounded, but that's crazy. */
15 #define CACHE_MAX 4096
17 /* We never keep any item longer than 2h in our cache */
18 #define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR)
20 /* How long to cache strange rcodes, i.e. rcodes != SUCCESS and != NXDOMAIN (specifically: that's only SERVFAIL for
22 #define CACHE_TTL_STRANGE_RCODE_USEC (30 * USEC_PER_SEC)
24 typedef enum DnsCacheItemType DnsCacheItemType
;
25 typedef struct DnsCacheItem DnsCacheItem
;
27 enum DnsCacheItemType
{
31 DNS_CACHE_RCODE
, /* "strange" RCODE (effective only SERVFAIL for now) */
35 DnsCacheItemType type
;
37 DnsResourceRecord
*rr
;
46 union in_addr_union owner_address
;
49 LIST_FIELDS(DnsCacheItem
, by_key
);
52 static const char *dns_cache_item_type_to_string(DnsCacheItem
*item
) {
57 case DNS_CACHE_POSITIVE
:
60 case DNS_CACHE_NODATA
:
63 case DNS_CACHE_NXDOMAIN
:
67 return dns_rcode_to_string(item
->rcode
);
73 static void dns_cache_item_free(DnsCacheItem
*i
) {
77 dns_resource_record_unref(i
->rr
);
78 dns_resource_key_unref(i
->key
);
82 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
84 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
92 first
= hashmap_get(c
->by_key
, i
->key
);
93 LIST_REMOVE(by_key
, first
, i
);
96 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
98 hashmap_remove(c
->by_key
, i
->key
);
100 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
102 dns_cache_item_free(i
);
105 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
106 DnsCacheItem
*first
, *i
;
109 first
= hashmap_get(c
->by_key
, rr
->key
);
110 LIST_FOREACH(by_key
, i
, first
) {
111 r
= dns_resource_record_equal(i
->rr
, rr
);
115 dns_cache_item_unlink_and_free(c
, i
);
123 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
124 DnsCacheItem
*first
, *i
, *n
;
129 first
= hashmap_remove(c
->by_key
, key
);
133 LIST_FOREACH_SAFE(by_key
, i
, n
, first
) {
134 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
135 dns_cache_item_free(i
);
141 void dns_cache_flush(DnsCache
*c
) {
146 while ((key
= hashmap_first_key(c
->by_key
)))
147 dns_cache_remove_by_key(c
, key
);
149 assert(hashmap_size(c
->by_key
) == 0);
150 assert(prioq_size(c
->by_expiry
) == 0);
152 c
->by_key
= hashmap_free(c
->by_key
);
153 c
->by_expiry
= prioq_free(c
->by_expiry
);
156 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
162 /* Makes space for n new entries. Note that we actually allow
163 * the cache to grow beyond CACHE_MAX, but only when we shall
164 * add more RRs to the cache than CACHE_MAX at once. In that
165 * case the cache will be emptied completely otherwise. */
168 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
171 if (prioq_size(c
->by_expiry
) <= 0)
174 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
177 i
= prioq_peek(c
->by_expiry
);
180 /* Take an extra reference to the key so that it
181 * doesn't go away in the middle of the remove call */
182 key
= dns_resource_key_ref(i
->key
);
183 dns_cache_remove_by_key(c
, key
);
187 void dns_cache_prune(DnsCache
*c
) {
192 /* Remove all entries that are past their TTL */
196 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
198 i
= prioq_peek(c
->by_expiry
);
203 t
= now(clock_boottime_or_monotonic());
208 /* Depending whether this is an mDNS shared entry
209 * either remove only this one RR or the whole RRset */
210 log_debug("Removing %scache entry for %s (expired "USEC_FMT
"s ago)",
211 i
->shared_owner
? "shared " : "",
212 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
213 (t
- i
->until
) / USEC_PER_SEC
);
216 dns_cache_item_unlink_and_free(c
, i
);
218 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
220 /* Take an extra reference to the key so that it
221 * doesn't go away in the middle of the remove call */
222 key
= dns_resource_key_ref(i
->key
);
223 dns_cache_remove_by_key(c
, key
);
228 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
229 const DnsCacheItem
*x
= a
, *y
= b
;
231 return CMP(x
->until
, y
->until
);
234 static int dns_cache_init(DnsCache
*c
) {
239 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
243 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
250 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
257 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
261 first
= hashmap_get(c
->by_key
, i
->key
);
263 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
265 /* Keep a reference to the original key, while we manipulate the list. */
266 k
= dns_resource_key_ref(first
->key
);
268 /* Now, try to reduce the number of keys we keep */
269 dns_resource_key_reduce(&first
->key
, &i
->key
);
272 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
274 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
276 LIST_PREPEND(by_key
, first
, i
);
277 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
279 r
= hashmap_put(c
->by_key
, i
->key
, i
);
281 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
289 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
295 LIST_FOREACH(by_key
, i
, hashmap_get(c
->by_key
, rr
->key
))
296 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
302 static usec_t
calculate_until(DnsResourceRecord
*rr
, uint32_t nsec_ttl
, usec_t timestamp
, bool use_soa_minimum
) {
308 ttl
= MIN(rr
->ttl
, nsec_ttl
);
309 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
310 /* If this is a SOA RR, and it is requested, clamp to
311 * the SOA's minimum field. This is used when we do
312 * negative caching, to determine the TTL for the
313 * negative caching entry. See RFC 2308, Section
316 if (ttl
> rr
->soa
.minimum
)
317 ttl
= rr
->soa
.minimum
;
320 u
= ttl
* USEC_PER_SEC
;
321 if (u
> CACHE_TTL_MAX_USEC
)
322 u
= CACHE_TTL_MAX_USEC
;
324 if (rr
->expiry
!= USEC_INFINITY
) {
327 /* Make use of the DNSSEC RRSIG expiry info, if we
330 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
335 return timestamp
+ u
;
338 static void dns_cache_item_update_positive(
341 DnsResourceRecord
*rr
,
347 const union in_addr_union
*owner_address
) {
352 assert(owner_address
);
354 i
->type
= DNS_CACHE_POSITIVE
;
357 /* We are the first item in the list, we need to
358 * update the key used in the hashmap */
360 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
362 dns_resource_record_ref(rr
);
363 dns_resource_record_unref(i
->rr
);
366 dns_resource_key_unref(i
->key
);
367 i
->key
= dns_resource_key_ref(rr
->key
);
369 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
370 i
->authenticated
= authenticated
;
371 i
->shared_owner
= shared_owner
;
373 i
->ifindex
= ifindex
;
375 i
->owner_family
= owner_family
;
376 i
->owner_address
= *owner_address
;
378 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
381 static int dns_cache_put_positive(
383 DnsResourceRecord
*rr
,
389 const union in_addr_union
*owner_address
) {
391 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
392 DnsCacheItem
*existing
;
393 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
], ifname
[IF_NAMESIZE
];
398 assert(owner_address
);
400 /* Never cache pseudo RRs */
401 if (dns_class_is_pseudo(rr
->key
->class))
403 if (dns_type_is_pseudo(rr
->key
->type
))
406 /* New TTL is 0? Delete this specific entry... */
408 k
= dns_cache_remove_by_rr(c
, rr
);
410 k
> 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry",
411 dns_resource_key_to_string(rr
->key
, key_str
, sizeof key_str
));
415 /* Entry exists already? Update TTL, timestamp and owner */
416 existing
= dns_cache_get(c
, rr
);
418 dns_cache_item_update_positive(
431 /* Otherwise, add the new RR */
432 r
= dns_cache_init(c
);
436 dns_cache_make_space(c
, 1);
438 i
= new0(DnsCacheItem
, 1);
442 i
->type
= DNS_CACHE_POSITIVE
;
443 i
->key
= dns_resource_key_ref(rr
->key
);
444 i
->rr
= dns_resource_record_ref(rr
);
445 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
446 i
->authenticated
= authenticated
;
447 i
->shared_owner
= shared_owner
;
448 i
->ifindex
= ifindex
;
449 i
->owner_family
= owner_family
;
450 i
->owner_address
= *owner_address
;
451 i
->prioq_idx
= PRIOQ_IDX_NULL
;
453 r
= dns_cache_link_item(c
, i
);
458 _cleanup_free_
char *t
= NULL
;
460 (void) in_addr_to_string(i
->owner_family
, &i
->owner_address
, &t
);
462 log_debug("Added positive %s%s cache entry for %s "USEC_FMT
"s on %s/%s/%s",
463 i
->authenticated
? "authenticated" : "unauthenticated",
464 i
->shared_owner
? " shared" : "",
465 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
466 (i
->until
- timestamp
) / USEC_PER_SEC
,
467 i
->ifindex
== 0 ? "*" : strna(if_indextoname(i
->ifindex
, ifname
)),
468 af_to_name_short(i
->owner_family
),
476 static int dns_cache_put_negative(
483 DnsResourceRecord
*soa
,
485 const union in_addr_union
*owner_address
) {
487 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
488 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
493 assert(owner_address
);
495 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
496 * important to filter out as we use this as a pseudo-type for
497 * NXDOMAIN entries */
498 if (dns_class_is_pseudo(key
->class))
500 if (dns_type_is_pseudo(key
->type
))
503 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
507 /* For negative replies, check if we have a TTL of a SOA */
508 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
509 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s",
510 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
513 } else if (rcode
!= DNS_RCODE_SERVFAIL
)
516 r
= dns_cache_init(c
);
520 dns_cache_make_space(c
, 1);
522 i
= new0(DnsCacheItem
, 1);
527 rcode
== DNS_RCODE_SUCCESS
? DNS_CACHE_NODATA
:
528 rcode
== DNS_RCODE_NXDOMAIN
? DNS_CACHE_NXDOMAIN
: DNS_CACHE_RCODE
;
530 i
->type
== DNS_CACHE_RCODE
? timestamp
+ CACHE_TTL_STRANGE_RCODE_USEC
:
531 calculate_until(soa
, nsec_ttl
, timestamp
, true);
532 i
->authenticated
= authenticated
;
533 i
->owner_family
= owner_family
;
534 i
->owner_address
= *owner_address
;
535 i
->prioq_idx
= PRIOQ_IDX_NULL
;
538 if (i
->type
== DNS_CACHE_NXDOMAIN
) {
539 /* NXDOMAIN entries should apply equally to all types, so we use ANY as
540 * a pseudo type for this purpose here. */
541 i
->key
= dns_resource_key_new(key
->class, DNS_TYPE_ANY
, dns_resource_key_name(key
));
545 /* Make sure to remove any previous entry for this
546 * specific ANY key. (For non-ANY keys the cache data
547 * is already cleared by the caller.) Note that we
548 * don't bother removing positive or NODATA cache
549 * items in this case, because it would either be slow
550 * or require explicit indexing by name */
551 dns_cache_remove_by_key(c
, key
);
553 i
->key
= dns_resource_key_ref(key
);
555 r
= dns_cache_link_item(c
, i
);
559 log_debug("Added %s cache entry for %s "USEC_FMT
"s",
560 dns_cache_item_type_to_string(i
),
561 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
562 (i
->until
- timestamp
) / USEC_PER_SEC
);
568 static void dns_cache_remove_previous(
573 DnsResourceRecord
*rr
;
574 DnsAnswerFlags flags
;
578 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
579 * not on mDNS), delete all matching old RRs, so that we only
580 * keep complete by_key in place. */
582 dns_cache_remove_by_key(c
, key
);
584 /* Second, flush all entries matching the answer, unless this
585 * is an RR that is explicitly marked to be "shared" between
586 * peers (i.e. mDNS RRs without the flush-cache bit set). */
587 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
588 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
591 if (flags
& DNS_ANSWER_SHARED_OWNER
)
594 dns_cache_remove_by_key(c
, rr
->key
);
598 static bool rr_eligible(DnsResourceRecord
*rr
) {
601 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
602 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
603 * existence from any cached NSEC/NSEC3, but that should be fine. */
605 switch (rr
->key
->type
) {
608 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
609 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
612 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
613 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
629 const union in_addr_union
*owner_address
) {
631 DnsResourceRecord
*soa
= NULL
, *rr
;
632 bool weird_rcode
= false;
633 DnsAnswerFlags flags
;
638 assert(owner_address
);
640 dns_cache_remove_previous(c
, key
, answer
);
642 /* We only care for positive replies and NXDOMAINs, on all other replies we will simply flush the respective
643 * entries, and that's it. (Well, with one further exception: since some DNS zones (akamai!) return SERVFAIL
644 * consistently for some lookups, and forwarders tend to propagate that we'll cache that too, but only for a
647 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
648 if (dns_answer_size(answer
) <= 0) {
650 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
652 log_debug("Not caching negative entry without a SOA record: %s",
653 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
659 /* Only cache SERVFAIL as "weird" rcode for now. We can add more later, should that turn out to be
661 if (rcode
!= DNS_RCODE_SERVFAIL
)
667 cache_keys
= dns_answer_size(answer
);
671 /* Make some space for our new entries */
672 dns_cache_make_space(c
, cache_keys
);
675 timestamp
= now(clock_boottime_or_monotonic());
677 /* Second, add in positive entries for all contained RRs */
678 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
679 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0 ||
683 r
= dns_cache_put_positive(
686 flags
& DNS_ANSWER_AUTHENTICATED
,
687 flags
& DNS_ANSWER_SHARED_OWNER
,
690 owner_family
, owner_address
);
695 if (!key
) /* mDNS doesn't know negative caching, really */
698 /* Third, add in negative entries if the key has no RR */
699 r
= dns_answer_match_key(answer
, key
, NULL
);
705 /* But not if it has a matching CNAME/DNAME (the negative
706 * caching will be done on the canonical name, not on the
708 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
714 /* See https://tools.ietf.org/html/rfc2308, which say that a matching SOA record in the packet is used to
715 * enable negative caching. We apply one exception though: if we are about to cache a weird rcode we do so
716 * regardless of a SOA. */
717 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
720 if (r
== 0 && !weird_rcode
)
723 /* Refuse using the SOA data if it is unsigned, but the key is
725 if (authenticated
&& (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
729 r
= dns_cache_put_negative(
737 owner_family
, owner_address
);
744 /* Adding all RRs failed. Let's clean up what we already
745 * added, just in case */
748 dns_cache_remove_by_key(c
, key
);
750 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
751 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
754 dns_cache_remove_by_key(c
, rr
->key
);
760 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(DnsCache
*c
, DnsResourceKey
*k
) {
768 /* If we hit some OOM error, or suchlike, we don't care too
769 * much, after all this is just a cache */
771 i
= hashmap_get(c
->by_key
, k
);
775 n
= dns_resource_key_name(k
);
777 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
778 * the pseudo-type ANY for NXDOMAIN cache items. */
779 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
780 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
783 if (dns_type_may_redirect(k
->type
)) {
784 /* Check if we have a CNAME record instead */
785 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
789 /* OK, let's look for cached DNAME records. */
794 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
798 /* Jump one label ahead */
799 r
= dns_name_parent(&n
);
805 if (k
->type
!= DNS_TYPE_NSEC
) {
806 /* Check if we have an NSEC record instead for the name. */
807 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
815 int dns_cache_lookup(DnsCache
*c
, DnsResourceKey
*key
, bool clamp_ttl
, int *rcode
, DnsAnswer
**ret
, bool *authenticated
) {
816 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
817 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
820 bool nxdomain
= false;
821 DnsCacheItem
*j
, *first
, *nsec
= NULL
;
822 bool have_authenticated
= false, have_non_authenticated
= false;
824 int found_rcode
= -1;
830 assert(authenticated
);
832 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
833 /* If we have ANY lookups we don't use the cache, so
834 * that the caller refreshes via the network. */
836 log_debug("Ignoring cache for ANY lookup: %s",
837 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
842 *rcode
= DNS_RCODE_SUCCESS
;
843 *authenticated
= false;
848 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
);
850 /* If one question cannot be answered we need to refresh */
852 log_debug("Cache miss for %s",
853 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
858 *rcode
= DNS_RCODE_SUCCESS
;
859 *authenticated
= false;
864 LIST_FOREACH(by_key
, j
, first
) {
866 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
870 } else if (j
->type
== DNS_CACHE_NXDOMAIN
)
872 else if (j
->type
== DNS_CACHE_RCODE
)
873 found_rcode
= j
->rcode
;
875 if (j
->authenticated
)
876 have_authenticated
= true;
878 have_non_authenticated
= true;
881 if (found_rcode
>= 0) {
882 log_debug("RCODE %s cache hit for %s",
883 dns_rcode_to_string(found_rcode
),
884 dns_resource_key_to_string(key
, key_str
, sizeof(key_str
)));
887 *rcode
= found_rcode
;
888 *authenticated
= false;
894 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
895 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC RRs from
896 * the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
898 log_debug("NSEC NODATA cache hit for %s",
899 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
901 /* We only found an NSEC record that matches our name.
902 * If it says the type doesn't exist report
903 * NODATA. Otherwise report a cache miss. */
906 *rcode
= DNS_RCODE_SUCCESS
;
907 *authenticated
= nsec
->authenticated
;
909 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
910 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
911 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
920 log_debug("%s cache hit for %s",
922 nxdomain
? "NXDOMAIN" : "NODATA",
923 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
929 *rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
930 *authenticated
= have_authenticated
&& !have_non_authenticated
;
934 answer
= dns_answer_new(n
);
939 current
= now(clock_boottime_or_monotonic());
941 LIST_FOREACH(by_key
, j
, first
) {
942 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
948 rr
= dns_resource_record_ref(j
->rr
);
950 r
= dns_resource_record_clamp_ttl(&rr
, LESS_BY(j
->until
, current
) / USEC_PER_SEC
);
955 r
= dns_answer_add(answer
, rr
?: j
->rr
, j
->ifindex
, j
->authenticated
? DNS_ANSWER_AUTHENTICATED
: 0);
963 *rcode
= DNS_RCODE_SUCCESS
;
964 *authenticated
= have_authenticated
&& !have_non_authenticated
;
970 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
971 DnsCacheItem
*i
, *first
;
972 bool same_owner
= true;
977 dns_cache_prune(cache
);
979 /* See if there's a cache entry for the same key. If there
980 * isn't there's no conflict */
981 first
= hashmap_get(cache
->by_key
, rr
->key
);
985 /* See if the RR key is owned by the same owner, if so, there
986 * isn't a conflict either */
987 LIST_FOREACH(by_key
, i
, first
) {
988 if (i
->owner_family
!= owner_family
||
989 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
997 /* See if there's the exact same RR in the cache. If yes, then
998 * there's no conflict. */
999 if (dns_cache_get(cache
, rr
))
1002 /* There's a conflict */
1006 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
) {
1007 unsigned ancount
= 0;
1015 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1018 LIST_FOREACH(by_key
, j
, i
) {
1022 if (!j
->shared_owner
)
1025 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1026 if (r
== -EMSGSIZE
&& p
->protocol
== DNS_PROTOCOL_MDNS
) {
1027 /* For mDNS, if we're unable to stuff all known answers into the given packet,
1028 * allocate a new one, push the RR into that one and link it to the current one.
1031 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1034 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1038 /* continue with new packet */
1040 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1050 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1055 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1065 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1068 LIST_FOREACH(by_key
, j
, i
) {
1074 t
= dns_resource_record_to_string(j
->rr
);
1083 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1085 fputs(dns_resource_key_to_string(j
->key
, key_str
, sizeof key_str
), f
);
1087 fputs(dns_cache_item_type_to_string(j
), f
);
1094 bool dns_cache_is_empty(DnsCache
*cache
) {
1098 return hashmap_isempty(cache
->by_key
);
1101 unsigned dns_cache_size(DnsCache
*cache
) {
1105 return hashmap_size(cache
->by_key
);