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 typedef enum DnsCacheItemType DnsCacheItemType
;
38 typedef struct DnsCacheItem DnsCacheItem
;
40 enum DnsCacheItemType
{
47 DnsCacheItemType type
;
49 DnsResourceRecord
*rr
;
57 union in_addr_union owner_address
;
60 LIST_FIELDS(DnsCacheItem
, by_key
);
63 static void dns_cache_item_free(DnsCacheItem
*i
) {
67 dns_resource_record_unref(i
->rr
);
68 dns_resource_key_unref(i
->key
);
72 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
74 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
82 first
= hashmap_get(c
->by_key
, i
->key
);
83 LIST_REMOVE(by_key
, first
, i
);
86 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
88 hashmap_remove(c
->by_key
, i
->key
);
90 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
92 dns_cache_item_free(i
);
95 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
96 DnsCacheItem
*first
, *i
;
99 first
= hashmap_get(c
->by_key
, rr
->key
);
100 LIST_FOREACH(by_key
, i
, first
) {
101 r
= dns_resource_record_equal(i
->rr
, rr
);
105 dns_cache_item_unlink_and_free(c
, i
);
113 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
114 DnsCacheItem
*first
, *i
, *n
;
119 first
= hashmap_remove(c
->by_key
, key
);
123 LIST_FOREACH_SAFE(by_key
, i
, n
, first
) {
124 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
125 dns_cache_item_free(i
);
131 void dns_cache_flush(DnsCache
*c
) {
136 while ((key
= hashmap_first_key(c
->by_key
)))
137 dns_cache_remove_by_key(c
, key
);
139 assert(hashmap_size(c
->by_key
) == 0);
140 assert(prioq_size(c
->by_expiry
) == 0);
142 c
->by_key
= hashmap_free(c
->by_key
);
143 c
->by_expiry
= prioq_free(c
->by_expiry
);
146 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
152 /* Makes space for n new entries. Note that we actually allow
153 * the cache to grow beyond CACHE_MAX, but only when we shall
154 * add more RRs to the cache than CACHE_MAX at once. In that
155 * case the cache will be emptied completely otherwise. */
158 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
161 if (prioq_size(c
->by_expiry
) <= 0)
164 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
167 i
= prioq_peek(c
->by_expiry
);
170 /* Take an extra reference to the key so that it
171 * doesn't go away in the middle of the remove call */
172 key
= dns_resource_key_ref(i
->key
);
173 dns_cache_remove_by_key(c
, key
);
177 void dns_cache_prune(DnsCache
*c
) {
182 /* Remove all entries that are past their TTL */
186 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
188 i
= prioq_peek(c
->by_expiry
);
193 t
= now(clock_boottime_or_monotonic());
198 /* Depending whether this is an mDNS shared entry
199 * either remove only this one RR or the whole RRset */
200 log_debug("Removing %scache entry for %s (expired "USEC_FMT
"s ago)",
201 i
->shared_owner
? "shared " : "",
202 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
203 (t
- i
->until
) / USEC_PER_SEC
);
206 dns_cache_item_unlink_and_free(c
, i
);
208 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
210 /* Take an extra reference to the key so that it
211 * doesn't go away in the middle of the remove call */
212 key
= dns_resource_key_ref(i
->key
);
213 dns_cache_remove_by_key(c
, key
);
218 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
219 const DnsCacheItem
*x
= a
, *y
= b
;
221 if (x
->until
< y
->until
)
223 if (x
->until
> y
->until
)
228 static int dns_cache_init(DnsCache
*c
) {
233 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
237 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
244 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
251 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
255 first
= hashmap_get(c
->by_key
, i
->key
);
257 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
259 /* Keep a reference to the original key, while we manipulate the list. */
260 k
= dns_resource_key_ref(first
->key
);
262 /* Now, try to reduce the number of keys we keep */
263 dns_resource_key_reduce(&first
->key
, &i
->key
);
266 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
268 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
270 LIST_PREPEND(by_key
, first
, i
);
271 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
273 r
= hashmap_put(c
->by_key
, i
->key
, i
);
275 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
283 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
289 LIST_FOREACH(by_key
, i
, hashmap_get(c
->by_key
, rr
->key
))
290 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
296 static usec_t
calculate_until(DnsResourceRecord
*rr
, uint32_t nsec_ttl
, usec_t timestamp
, bool use_soa_minimum
) {
302 ttl
= MIN(rr
->ttl
, nsec_ttl
);
303 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
304 /* If this is a SOA RR, and it is requested, clamp to
305 * the SOA's minimum field. This is used when we do
306 * negative caching, to determine the TTL for the
307 * negative caching entry. See RFC 2308, Section
310 if (ttl
> rr
->soa
.minimum
)
311 ttl
= rr
->soa
.minimum
;
314 u
= ttl
* USEC_PER_SEC
;
315 if (u
> CACHE_TTL_MAX_USEC
)
316 u
= CACHE_TTL_MAX_USEC
;
318 if (rr
->expiry
!= USEC_INFINITY
) {
321 /* Make use of the DNSSEC RRSIG expiry info, if we
324 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
329 return timestamp
+ u
;
332 static void dns_cache_item_update_positive(
335 DnsResourceRecord
*rr
,
341 const union in_addr_union
*owner_address
) {
346 assert(owner_address
);
348 i
->type
= DNS_CACHE_POSITIVE
;
351 /* We are the first item in the list, we need to
352 * update the key used in the hashmap */
354 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
356 dns_resource_record_ref(rr
);
357 dns_resource_record_unref(i
->rr
);
360 dns_resource_key_unref(i
->key
);
361 i
->key
= dns_resource_key_ref(rr
->key
);
363 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
364 i
->authenticated
= authenticated
;
365 i
->shared_owner
= shared_owner
;
367 i
->ifindex
= ifindex
;
369 i
->owner_family
= owner_family
;
370 i
->owner_address
= *owner_address
;
372 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
375 static int dns_cache_put_positive(
377 DnsResourceRecord
*rr
,
383 const union in_addr_union
*owner_address
) {
385 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
386 DnsCacheItem
*existing
;
387 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
], ifname
[IF_NAMESIZE
];
392 assert(owner_address
);
394 /* Never cache pseudo RRs */
395 if (dns_class_is_pseudo(rr
->key
->class))
397 if (dns_type_is_pseudo(rr
->key
->type
))
400 /* New TTL is 0? Delete this specific entry... */
402 k
= dns_cache_remove_by_rr(c
, rr
);
404 k
> 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry",
405 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
));
409 /* Entry exists already? Update TTL, timestamp and owner*/
410 existing
= dns_cache_get(c
, rr
);
412 dns_cache_item_update_positive(
425 /* Otherwise, add the new RR */
426 r
= dns_cache_init(c
);
430 dns_cache_make_space(c
, 1);
432 i
= new0(DnsCacheItem
, 1);
436 i
->type
= DNS_CACHE_POSITIVE
;
437 i
->key
= dns_resource_key_ref(rr
->key
);
438 i
->rr
= dns_resource_record_ref(rr
);
439 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
440 i
->authenticated
= authenticated
;
441 i
->shared_owner
= shared_owner
;
442 i
->ifindex
= ifindex
;
443 i
->owner_family
= owner_family
;
444 i
->owner_address
= *owner_address
;
445 i
->prioq_idx
= PRIOQ_IDX_NULL
;
447 r
= dns_cache_link_item(c
, i
);
451 if (log_get_max_level() >= LOG_DEBUG
) {
452 _cleanup_free_
char *t
= NULL
;
454 (void) in_addr_to_string(i
->owner_family
, &i
->owner_address
, &t
);
456 log_debug("Added positive %s%s cache entry for %s "USEC_FMT
"s on %s/%s/%s",
457 i
->authenticated
? "authenticated" : "unauthenticated",
458 i
->shared_owner
? " shared" : "",
459 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
460 (i
->until
- timestamp
) / USEC_PER_SEC
,
461 i
->ifindex
== 0 ? "*" : strna(if_indextoname(i
->ifindex
, ifname
)),
462 af_to_name_short(i
->owner_family
),
470 static int dns_cache_put_negative(
477 DnsResourceRecord
*soa
,
479 const union in_addr_union
*owner_address
) {
481 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
482 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
488 assert(owner_address
);
490 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
491 * important to filter out as we use this as a pseudo-type for
492 * NXDOMAIN entries */
493 if (dns_class_is_pseudo(key
->class))
495 if (dns_type_is_pseudo(key
->type
))
498 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
499 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s",
500 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
));
504 if (!IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
))
507 r
= dns_cache_init(c
);
511 dns_cache_make_space(c
, 1);
513 i
= new0(DnsCacheItem
, 1);
517 i
->type
= rcode
== DNS_RCODE_SUCCESS
? DNS_CACHE_NODATA
: DNS_CACHE_NXDOMAIN
;
518 i
->until
= calculate_until(soa
, nsec_ttl
, timestamp
, true);
519 i
->authenticated
= authenticated
;
520 i
->owner_family
= owner_family
;
521 i
->owner_address
= *owner_address
;
522 i
->prioq_idx
= PRIOQ_IDX_NULL
;
524 if (i
->type
== DNS_CACHE_NXDOMAIN
) {
525 /* NXDOMAIN entries should apply equally to all types, so we use ANY as
526 * a pseudo type for this purpose here. */
527 i
->key
= dns_resource_key_new(key
->class, DNS_TYPE_ANY
, dns_resource_key_name(key
));
531 /* Make sure to remove any previous entry for this
532 * specific ANY key. (For non-ANY keys the cache data
533 * is already cleared by the caller.) Note that we
534 * don't bother removing positive or NODATA cache
535 * items in this case, because it would either be slow
536 * or require explicit indexing by name */
537 dns_cache_remove_by_key(c
, key
);
539 i
->key
= dns_resource_key_ref(key
);
541 r
= dns_cache_link_item(c
, i
);
545 log_debug("Added %s cache entry for %s "USEC_FMT
"s",
546 i
->type
== DNS_CACHE_NODATA
? "NODATA" : "NXDOMAIN",
547 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
548 (i
->until
- timestamp
) / USEC_PER_SEC
);
554 static void dns_cache_remove_previous(
559 DnsResourceRecord
*rr
;
560 DnsAnswerFlags flags
;
564 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
565 * not on mDNS), delete all matching old RRs, so that we only
566 * keep complete by_key in place. */
568 dns_cache_remove_by_key(c
, key
);
570 /* Second, flush all entries matching the answer, unless this
571 * is an RR that is explicitly marked to be "shared" between
572 * peers (i.e. mDNS RRs without the flush-cache bit set). */
573 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
574 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
577 if (flags
& DNS_ANSWER_SHARED_OWNER
)
580 dns_cache_remove_by_key(c
, rr
->key
);
584 static bool rr_eligible(DnsResourceRecord
*rr
) {
587 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
588 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
589 * existence from any cached NSEC/NSEC3, but that should be fine. */
591 switch (rr
->key
->type
) {
594 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
595 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
598 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
599 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
615 const union in_addr_union
*owner_address
) {
617 DnsResourceRecord
*soa
= NULL
, *rr
;
618 DnsAnswerFlags flags
;
623 assert(owner_address
);
625 dns_cache_remove_previous(c
, key
, answer
);
627 if (dns_answer_size(answer
) <= 0) {
628 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
630 log_debug("Not caching negative entry without a SOA record: %s",
631 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
635 /* We only care for positive replies and NXDOMAINs, on all
636 * other replies we will simply flush the respective entries,
638 if (!IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
))
641 cache_keys
= dns_answer_size(answer
);
645 /* Make some space for our new entries */
646 dns_cache_make_space(c
, cache_keys
);
649 timestamp
= now(clock_boottime_or_monotonic());
651 /* Second, add in positive entries for all contained RRs */
652 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
653 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
662 r
= dns_cache_put_positive(
665 flags
& DNS_ANSWER_AUTHENTICATED
,
666 flags
& DNS_ANSWER_SHARED_OWNER
,
669 owner_family
, owner_address
);
674 if (!key
) /* mDNS doesn't know negative caching, really */
677 /* Third, add in negative entries if the key has no RR */
678 r
= dns_answer_match_key(answer
, key
, NULL
);
684 /* But not if it has a matching CNAME/DNAME (the negative
685 * caching will be done on the canonical name, not on the
687 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
693 /* See https://tools.ietf.org/html/rfc2308, which say that a
694 * matching SOA record in the packet is used to to enable
695 * negative caching. */
696 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
702 /* Refuse using the SOA data if it is unsigned, but the key is
704 if (authenticated
&& (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
707 r
= dns_cache_put_negative(
715 owner_family
, owner_address
);
722 /* Adding all RRs failed. Let's clean up what we already
723 * added, just in case */
726 dns_cache_remove_by_key(c
, key
);
728 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
729 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
732 dns_cache_remove_by_key(c
, rr
->key
);
738 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(DnsCache
*c
, DnsResourceKey
*k
) {
746 /* If we hit some OOM error, or suchlike, we don't care too
747 * much, after all this is just a cache */
749 i
= hashmap_get(c
->by_key
, k
);
753 n
= dns_resource_key_name(k
);
755 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
756 * the pseudo-type ANY for NXDOMAIN cache items. */
757 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
758 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
761 if (dns_type_may_redirect(k
->type
)) {
762 /* Check if we have a CNAME record instead */
763 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
767 /* OK, let's look for cached DNAME records. */
772 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
776 /* Jump one label ahead */
777 r
= dns_name_parent(&n
);
783 if (k
->type
!= DNS_TYPE_NSEC
) {
784 /* Check if we have an NSEC record instead for the name. */
785 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
793 int dns_cache_lookup(DnsCache
*c
, DnsResourceKey
*key
, int *rcode
, DnsAnswer
**ret
, bool *authenticated
) {
794 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
795 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
798 bool nxdomain
= false;
799 DnsCacheItem
*j
, *first
, *nsec
= NULL
;
800 bool have_authenticated
= false, have_non_authenticated
= false;
806 assert(authenticated
);
808 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
809 /* If we have ANY lookups we don't use the cache, so
810 * that the caller refreshes via the network. */
812 log_debug("Ignoring cache for ANY lookup: %s",
813 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
818 *rcode
= DNS_RCODE_SUCCESS
;
822 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
);
824 /* If one question cannot be answered we need to refresh */
826 log_debug("Cache miss for %s",
827 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
832 *rcode
= DNS_RCODE_SUCCESS
;
836 LIST_FOREACH(by_key
, j
, first
) {
838 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
842 } else if (j
->type
== DNS_CACHE_NXDOMAIN
)
845 if (j
->authenticated
)
846 have_authenticated
= true;
848 have_non_authenticated
= true;
851 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
852 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC RRs from
853 * the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
855 log_debug("NSEC NODATA cache hit for %s",
856 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
858 /* We only found an NSEC record that matches our name.
859 * If it says the type doesn't exist report
860 * NODATA. Otherwise report a cache miss. */
863 *rcode
= DNS_RCODE_SUCCESS
;
864 *authenticated
= nsec
->authenticated
;
866 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
867 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
868 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
877 log_debug("%s cache hit for %s",
879 nxdomain
? "NXDOMAIN" : "NODATA",
880 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
886 *rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
887 *authenticated
= have_authenticated
&& !have_non_authenticated
;
891 answer
= dns_answer_new(n
);
895 LIST_FOREACH(by_key
, j
, first
) {
899 r
= dns_answer_add(answer
, j
->rr
, j
->ifindex
, j
->authenticated
? DNS_ANSWER_AUTHENTICATED
: 0);
907 *rcode
= DNS_RCODE_SUCCESS
;
908 *authenticated
= have_authenticated
&& !have_non_authenticated
;
914 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
915 DnsCacheItem
*i
, *first
;
916 bool same_owner
= true;
921 dns_cache_prune(cache
);
923 /* See if there's a cache entry for the same key. If there
924 * isn't there's no conflict */
925 first
= hashmap_get(cache
->by_key
, rr
->key
);
929 /* See if the RR key is owned by the same owner, if so, there
930 * isn't a conflict either */
931 LIST_FOREACH(by_key
, i
, first
) {
932 if (i
->owner_family
!= owner_family
||
933 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
941 /* See if there's the exact same RR in the cache. If yes, then
942 * there's no conflict. */
943 if (dns_cache_get(cache
, rr
))
946 /* There's a conflict */
950 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
) {
951 unsigned ancount
= 0;
959 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
962 LIST_FOREACH(by_key
, j
, i
) {
966 if (!j
->shared_owner
)
969 r
= dns_packet_append_rr(p
, j
->rr
, NULL
, NULL
);
970 if (r
== -EMSGSIZE
&& p
->protocol
== DNS_PROTOCOL_MDNS
) {
971 /* For mDNS, if we're unable to stuff all known answers into the given packet,
972 * allocate a new one, push the RR into that one and link it to the current one.
975 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
978 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
982 /* continue with new packet */
984 r
= dns_packet_append_rr(p
, j
->rr
, NULL
, NULL
);
994 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
999 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1009 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1012 LIST_FOREACH(by_key
, j
, i
) {
1018 t
= dns_resource_record_to_string(j
->rr
);
1027 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1029 fputs(dns_resource_key_to_string(j
->key
, key_str
, sizeof key_str
), f
);
1031 fputs(j
->type
== DNS_CACHE_NODATA
? "NODATA" : "NXDOMAIN", f
);
1038 bool dns_cache_is_empty(DnsCache
*cache
) {
1042 return hashmap_isempty(cache
->by_key
);
1045 unsigned dns_cache_size(DnsCache
*cache
) {
1049 return hashmap_size(cache
->by_key
);