2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "dns-domain.h"
25 #include "resolved-dns-answer.h"
26 #include "resolved-dns-cache.h"
27 #include "resolved-dns-packet.h"
28 #include "string-util.h"
30 /* Never cache more than 4K entries. RFC 1536, Section 5 suggests to
31 * leave DNS caches unbounded, but that's crazy. */
32 #define CACHE_MAX 4096
34 /* We never keep any item longer than 2h in our cache */
35 #define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR)
37 /* How long to cache strange rcodes, i.e. rcodes != SUCCESS and != NXDOMAIN (specifically: that's only SERVFAIL for
39 #define CACHE_TTL_STRANGE_RCODE_USEC (30 * USEC_PER_SEC)
41 typedef enum DnsCacheItemType DnsCacheItemType
;
42 typedef struct DnsCacheItem DnsCacheItem
;
44 enum DnsCacheItemType
{
48 DNS_CACHE_RCODE
, /* "strange" RCODE (effective only SERVFAIL for now) */
52 DnsCacheItemType type
;
54 DnsResourceRecord
*rr
;
63 union in_addr_union owner_address
;
66 LIST_FIELDS(DnsCacheItem
, by_key
);
69 static const char *dns_cache_item_type_to_string(DnsCacheItem
*item
) {
74 case DNS_CACHE_POSITIVE
:
77 case DNS_CACHE_NODATA
:
80 case DNS_CACHE_NXDOMAIN
:
84 return dns_rcode_to_string(item
->rcode
);
90 static void dns_cache_item_free(DnsCacheItem
*i
) {
94 dns_resource_record_unref(i
->rr
);
95 dns_resource_key_unref(i
->key
);
99 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
101 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
109 first
= hashmap_get(c
->by_key
, i
->key
);
110 LIST_REMOVE(by_key
, first
, i
);
113 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
115 hashmap_remove(c
->by_key
, i
->key
);
117 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
119 dns_cache_item_free(i
);
122 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
123 DnsCacheItem
*first
, *i
;
126 first
= hashmap_get(c
->by_key
, rr
->key
);
127 LIST_FOREACH(by_key
, i
, first
) {
128 r
= dns_resource_record_equal(i
->rr
, rr
);
132 dns_cache_item_unlink_and_free(c
, i
);
140 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
141 DnsCacheItem
*first
, *i
, *n
;
146 first
= hashmap_remove(c
->by_key
, key
);
150 LIST_FOREACH_SAFE(by_key
, i
, n
, first
) {
151 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
152 dns_cache_item_free(i
);
158 void dns_cache_flush(DnsCache
*c
) {
163 while ((key
= hashmap_first_key(c
->by_key
)))
164 dns_cache_remove_by_key(c
, key
);
166 assert(hashmap_size(c
->by_key
) == 0);
167 assert(prioq_size(c
->by_expiry
) == 0);
169 c
->by_key
= hashmap_free(c
->by_key
);
170 c
->by_expiry
= prioq_free(c
->by_expiry
);
173 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
179 /* Makes space for n new entries. Note that we actually allow
180 * the cache to grow beyond CACHE_MAX, but only when we shall
181 * add more RRs to the cache than CACHE_MAX at once. In that
182 * case the cache will be emptied completely otherwise. */
185 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
188 if (prioq_size(c
->by_expiry
) <= 0)
191 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
194 i
= prioq_peek(c
->by_expiry
);
197 /* Take an extra reference to the key so that it
198 * doesn't go away in the middle of the remove call */
199 key
= dns_resource_key_ref(i
->key
);
200 dns_cache_remove_by_key(c
, key
);
204 void dns_cache_prune(DnsCache
*c
) {
209 /* Remove all entries that are past their TTL */
213 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
215 i
= prioq_peek(c
->by_expiry
);
220 t
= now(clock_boottime_or_monotonic());
225 /* Depending whether this is an mDNS shared entry
226 * either remove only this one RR or the whole RRset */
227 log_debug("Removing %scache entry for %s (expired "USEC_FMT
"s ago)",
228 i
->shared_owner
? "shared " : "",
229 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
230 (t
- i
->until
) / USEC_PER_SEC
);
233 dns_cache_item_unlink_and_free(c
, i
);
235 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
237 /* Take an extra reference to the key so that it
238 * doesn't go away in the middle of the remove call */
239 key
= dns_resource_key_ref(i
->key
);
240 dns_cache_remove_by_key(c
, key
);
245 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
246 const DnsCacheItem
*x
= a
, *y
= b
;
248 if (x
->until
< y
->until
)
250 if (x
->until
> y
->until
)
255 static int dns_cache_init(DnsCache
*c
) {
260 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
264 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
271 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
278 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
282 first
= hashmap_get(c
->by_key
, i
->key
);
284 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
286 /* Keep a reference to the original key, while we manipulate the list. */
287 k
= dns_resource_key_ref(first
->key
);
289 /* Now, try to reduce the number of keys we keep */
290 dns_resource_key_reduce(&first
->key
, &i
->key
);
293 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
295 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
297 LIST_PREPEND(by_key
, first
, i
);
298 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
300 r
= hashmap_put(c
->by_key
, i
->key
, i
);
302 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
310 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
316 LIST_FOREACH(by_key
, i
, hashmap_get(c
->by_key
, rr
->key
))
317 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
323 static usec_t
calculate_until(DnsResourceRecord
*rr
, uint32_t nsec_ttl
, usec_t timestamp
, bool use_soa_minimum
) {
329 ttl
= MIN(rr
->ttl
, nsec_ttl
);
330 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
331 /* If this is a SOA RR, and it is requested, clamp to
332 * the SOA's minimum field. This is used when we do
333 * negative caching, to determine the TTL for the
334 * negative caching entry. See RFC 2308, Section
337 if (ttl
> rr
->soa
.minimum
)
338 ttl
= rr
->soa
.minimum
;
341 u
= ttl
* USEC_PER_SEC
;
342 if (u
> CACHE_TTL_MAX_USEC
)
343 u
= CACHE_TTL_MAX_USEC
;
345 if (rr
->expiry
!= USEC_INFINITY
) {
348 /* Make use of the DNSSEC RRSIG expiry info, if we
351 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
356 return timestamp
+ u
;
359 static void dns_cache_item_update_positive(
362 DnsResourceRecord
*rr
,
368 const union in_addr_union
*owner_address
) {
373 assert(owner_address
);
375 i
->type
= DNS_CACHE_POSITIVE
;
378 /* We are the first item in the list, we need to
379 * update the key used in the hashmap */
381 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
383 dns_resource_record_ref(rr
);
384 dns_resource_record_unref(i
->rr
);
387 dns_resource_key_unref(i
->key
);
388 i
->key
= dns_resource_key_ref(rr
->key
);
390 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
391 i
->authenticated
= authenticated
;
392 i
->shared_owner
= shared_owner
;
394 i
->ifindex
= ifindex
;
396 i
->owner_family
= owner_family
;
397 i
->owner_address
= *owner_address
;
399 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
402 static int dns_cache_put_positive(
404 DnsResourceRecord
*rr
,
410 const union in_addr_union
*owner_address
) {
412 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
413 DnsCacheItem
*existing
;
414 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
], ifname
[IF_NAMESIZE
];
419 assert(owner_address
);
421 /* Never cache pseudo RRs */
422 if (dns_class_is_pseudo(rr
->key
->class))
424 if (dns_type_is_pseudo(rr
->key
->type
))
427 /* New TTL is 0? Delete this specific entry... */
429 k
= dns_cache_remove_by_rr(c
, rr
);
431 k
> 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry",
432 dns_resource_key_to_string(rr
->key
, key_str
, sizeof key_str
));
436 /* Entry exists already? Update TTL, timestamp and owner */
437 existing
= dns_cache_get(c
, rr
);
439 dns_cache_item_update_positive(
452 /* Otherwise, add the new RR */
453 r
= dns_cache_init(c
);
457 dns_cache_make_space(c
, 1);
459 i
= new0(DnsCacheItem
, 1);
463 i
->type
= DNS_CACHE_POSITIVE
;
464 i
->key
= dns_resource_key_ref(rr
->key
);
465 i
->rr
= dns_resource_record_ref(rr
);
466 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
467 i
->authenticated
= authenticated
;
468 i
->shared_owner
= shared_owner
;
469 i
->ifindex
= ifindex
;
470 i
->owner_family
= owner_family
;
471 i
->owner_address
= *owner_address
;
472 i
->prioq_idx
= PRIOQ_IDX_NULL
;
474 r
= dns_cache_link_item(c
, i
);
478 if (log_get_max_level() >= LOG_DEBUG
) {
479 _cleanup_free_
char *t
= NULL
;
481 (void) in_addr_to_string(i
->owner_family
, &i
->owner_address
, &t
);
483 log_debug("Added positive %s%s cache entry for %s "USEC_FMT
"s on %s/%s/%s",
484 i
->authenticated
? "authenticated" : "unauthenticated",
485 i
->shared_owner
? " shared" : "",
486 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
487 (i
->until
- timestamp
) / USEC_PER_SEC
,
488 i
->ifindex
== 0 ? "*" : strna(if_indextoname(i
->ifindex
, ifname
)),
489 af_to_name_short(i
->owner_family
),
497 static int dns_cache_put_negative(
504 DnsResourceRecord
*soa
,
506 const union in_addr_union
*owner_address
) {
508 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
509 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
514 assert(owner_address
);
516 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
517 * important to filter out as we use this as a pseudo-type for
518 * NXDOMAIN entries */
519 if (dns_class_is_pseudo(key
->class))
521 if (dns_type_is_pseudo(key
->type
))
524 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
528 /* For negative replies, check if we have a TTL of a SOA */
529 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
530 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s",
531 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
534 } else if (rcode
!= DNS_RCODE_SERVFAIL
)
537 r
= dns_cache_init(c
);
541 dns_cache_make_space(c
, 1);
543 i
= new0(DnsCacheItem
, 1);
548 rcode
== DNS_RCODE_SUCCESS
? DNS_CACHE_NODATA
:
549 rcode
== DNS_RCODE_NXDOMAIN
? DNS_CACHE_NXDOMAIN
: DNS_CACHE_RCODE
;
551 i
->type
== DNS_CACHE_RCODE
? timestamp
+ CACHE_TTL_STRANGE_RCODE_USEC
:
552 calculate_until(soa
, nsec_ttl
, timestamp
, true);
553 i
->authenticated
= authenticated
;
554 i
->owner_family
= owner_family
;
555 i
->owner_address
= *owner_address
;
556 i
->prioq_idx
= PRIOQ_IDX_NULL
;
559 if (i
->type
== DNS_CACHE_NXDOMAIN
) {
560 /* NXDOMAIN entries should apply equally to all types, so we use ANY as
561 * a pseudo type for this purpose here. */
562 i
->key
= dns_resource_key_new(key
->class, DNS_TYPE_ANY
, dns_resource_key_name(key
));
566 /* Make sure to remove any previous entry for this
567 * specific ANY key. (For non-ANY keys the cache data
568 * is already cleared by the caller.) Note that we
569 * don't bother removing positive or NODATA cache
570 * items in this case, because it would either be slow
571 * or require explicit indexing by name */
572 dns_cache_remove_by_key(c
, key
);
574 i
->key
= dns_resource_key_ref(key
);
576 r
= dns_cache_link_item(c
, i
);
580 log_debug("Added %s cache entry for %s "USEC_FMT
"s",
581 dns_cache_item_type_to_string(i
),
582 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
583 (i
->until
- timestamp
) / USEC_PER_SEC
);
589 static void dns_cache_remove_previous(
594 DnsResourceRecord
*rr
;
595 DnsAnswerFlags flags
;
599 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
600 * not on mDNS), delete all matching old RRs, so that we only
601 * keep complete by_key in place. */
603 dns_cache_remove_by_key(c
, key
);
605 /* Second, flush all entries matching the answer, unless this
606 * is an RR that is explicitly marked to be "shared" between
607 * peers (i.e. mDNS RRs without the flush-cache bit set). */
608 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
609 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
612 if (flags
& DNS_ANSWER_SHARED_OWNER
)
615 dns_cache_remove_by_key(c
, rr
->key
);
619 static bool rr_eligible(DnsResourceRecord
*rr
) {
622 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
623 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
624 * existence from any cached NSEC/NSEC3, but that should be fine. */
626 switch (rr
->key
->type
) {
629 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
630 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
633 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
634 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
650 const union in_addr_union
*owner_address
) {
652 DnsResourceRecord
*soa
= NULL
, *rr
;
653 bool weird_rcode
= false;
654 DnsAnswerFlags flags
;
659 assert(owner_address
);
661 dns_cache_remove_previous(c
, key
, answer
);
663 /* We only care for positive replies and NXDOMAINs, on all other replies we will simply flush the respective
664 * entries, and that's it. (Well, with one further exception: since some DNS zones (akamai!) return SERVFAIL
665 * consistently for some lookups, and forwarders tend to propagate that we'll cache that too, but only for a
668 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
670 if (dns_answer_size(answer
) <= 0) {
671 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
673 log_debug("Not caching negative entry without a SOA record: %s",
674 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
679 /* Only cache SERVFAIL as "weird" rcode for now. We can add more later, should that turn out to be
681 if (rcode
!= DNS_RCODE_SERVFAIL
)
687 cache_keys
= dns_answer_size(answer
);
691 /* Make some space for our new entries */
692 dns_cache_make_space(c
, cache_keys
);
695 timestamp
= now(clock_boottime_or_monotonic());
697 /* Second, add in positive entries for all contained RRs */
698 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
699 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
708 r
= dns_cache_put_positive(
711 flags
& DNS_ANSWER_AUTHENTICATED
,
712 flags
& DNS_ANSWER_SHARED_OWNER
,
715 owner_family
, owner_address
);
720 if (!key
) /* mDNS doesn't know negative caching, really */
723 /* Third, add in negative entries if the key has no RR */
724 r
= dns_answer_match_key(answer
, key
, NULL
);
730 /* But not if it has a matching CNAME/DNAME (the negative
731 * caching will be done on the canonical name, not on the
733 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
739 /* See https://tools.ietf.org/html/rfc2308, which say that a matching SOA record in the packet is used to
740 * enable negative caching. We apply one exception though: if we are about to cache a weird rcode we do so
741 * regardless of a SOA. */
742 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
745 if (r
== 0 && !weird_rcode
)
748 /* Refuse using the SOA data if it is unsigned, but the key is
750 if (authenticated
&& (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
754 r
= dns_cache_put_negative(
762 owner_family
, owner_address
);
769 /* Adding all RRs failed. Let's clean up what we already
770 * added, just in case */
773 dns_cache_remove_by_key(c
, key
);
775 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
776 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
779 dns_cache_remove_by_key(c
, rr
->key
);
785 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(DnsCache
*c
, DnsResourceKey
*k
) {
793 /* If we hit some OOM error, or suchlike, we don't care too
794 * much, after all this is just a cache */
796 i
= hashmap_get(c
->by_key
, k
);
800 n
= dns_resource_key_name(k
);
802 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
803 * the pseudo-type ANY for NXDOMAIN cache items. */
804 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
805 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
808 if (dns_type_may_redirect(k
->type
)) {
809 /* Check if we have a CNAME record instead */
810 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
814 /* OK, let's look for cached DNAME records. */
819 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
823 /* Jump one label ahead */
824 r
= dns_name_parent(&n
);
830 if (k
->type
!= DNS_TYPE_NSEC
) {
831 /* Check if we have an NSEC record instead for the name. */
832 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
840 int dns_cache_lookup(DnsCache
*c
, DnsResourceKey
*key
, bool clamp_ttl
, int *rcode
, DnsAnswer
**ret
, bool *authenticated
) {
841 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
842 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
845 bool nxdomain
= false;
846 DnsCacheItem
*j
, *first
, *nsec
= NULL
;
847 bool have_authenticated
= false, have_non_authenticated
= false;
849 int found_rcode
= -1;
855 assert(authenticated
);
857 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
858 /* If we have ANY lookups we don't use the cache, so
859 * that the caller refreshes via the network. */
861 log_debug("Ignoring cache for ANY lookup: %s",
862 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
867 *rcode
= DNS_RCODE_SUCCESS
;
868 *authenticated
= false;
873 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
);
875 /* If one question cannot be answered we need to refresh */
877 log_debug("Cache miss for %s",
878 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
883 *rcode
= DNS_RCODE_SUCCESS
;
884 *authenticated
= false;
889 LIST_FOREACH(by_key
, j
, first
) {
891 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
895 } else if (j
->type
== DNS_CACHE_NXDOMAIN
)
897 else if (j
->type
== DNS_CACHE_RCODE
)
898 found_rcode
= j
->rcode
;
900 if (j
->authenticated
)
901 have_authenticated
= true;
903 have_non_authenticated
= true;
906 if (found_rcode
>= 0) {
907 log_debug("RCODE %s cache hit for %s",
908 dns_rcode_to_string(found_rcode
),
909 dns_resource_key_to_string(key
, key_str
, sizeof(key_str
)));
912 *rcode
= found_rcode
;
913 *authenticated
= false;
919 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
920 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC RRs from
921 * the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
923 log_debug("NSEC NODATA cache hit for %s",
924 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
926 /* We only found an NSEC record that matches our name.
927 * If it says the type doesn't exist report
928 * NODATA. Otherwise report a cache miss. */
931 *rcode
= DNS_RCODE_SUCCESS
;
932 *authenticated
= nsec
->authenticated
;
934 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
935 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
936 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
945 log_debug("%s cache hit for %s",
947 nxdomain
? "NXDOMAIN" : "NODATA",
948 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
954 *rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
955 *authenticated
= have_authenticated
&& !have_non_authenticated
;
959 answer
= dns_answer_new(n
);
964 current
= now(clock_boottime_or_monotonic());
966 LIST_FOREACH(by_key
, j
, first
) {
967 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
973 rr
= dns_resource_record_ref(j
->rr
);
975 r
= dns_resource_record_clamp_ttl(&rr
, LESS_BY(j
->until
, current
) / USEC_PER_SEC
);
980 r
= dns_answer_add(answer
, rr
?: j
->rr
, j
->ifindex
, j
->authenticated
? DNS_ANSWER_AUTHENTICATED
: 0);
988 *rcode
= DNS_RCODE_SUCCESS
;
989 *authenticated
= have_authenticated
&& !have_non_authenticated
;
995 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
996 DnsCacheItem
*i
, *first
;
997 bool same_owner
= true;
1002 dns_cache_prune(cache
);
1004 /* See if there's a cache entry for the same key. If there
1005 * isn't there's no conflict */
1006 first
= hashmap_get(cache
->by_key
, rr
->key
);
1010 /* See if the RR key is owned by the same owner, if so, there
1011 * isn't a conflict either */
1012 LIST_FOREACH(by_key
, i
, first
) {
1013 if (i
->owner_family
!= owner_family
||
1014 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
1022 /* See if there's the exact same RR in the cache. If yes, then
1023 * there's no conflict. */
1024 if (dns_cache_get(cache
, rr
))
1027 /* There's a conflict */
1031 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
) {
1032 unsigned ancount
= 0;
1040 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1043 LIST_FOREACH(by_key
, j
, i
) {
1047 if (!j
->shared_owner
)
1050 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1051 if (r
== -EMSGSIZE
&& p
->protocol
== DNS_PROTOCOL_MDNS
) {
1052 /* For mDNS, if we're unable to stuff all known answers into the given packet,
1053 * allocate a new one, push the RR into that one and link it to the current one.
1056 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1059 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1063 /* continue with new packet */
1065 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1075 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1080 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1090 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1093 LIST_FOREACH(by_key
, j
, i
) {
1099 t
= dns_resource_record_to_string(j
->rr
);
1108 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1110 fputs(dns_resource_key_to_string(j
->key
, key_str
, sizeof key_str
), f
);
1112 fputs(dns_cache_item_type_to_string(j
), f
);
1119 bool dns_cache_is_empty(DnsCache
*cache
) {
1123 return hashmap_isempty(cache
->by_key
);
1126 unsigned dns_cache_size(DnsCache
*cache
) {
1130 return hashmap_size(cache
->by_key
);