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/>.
20 #include "alloc-util.h"
21 #include "dns-domain.h"
22 #include "resolved-dns-answer.h"
23 #include "resolved-dns-cache.h"
24 #include "resolved-dns-packet.h"
25 #include "string-util.h"
27 /* Never cache more than 4K entries. RFC 1536, Section 5 suggests to
28 * leave DNS caches unbounded, but that's crazy. */
29 #define CACHE_MAX 4096
31 /* We never keep any item longer than 2h in our cache */
32 #define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR)
34 typedef enum DnsCacheItemType DnsCacheItemType
;
35 typedef struct DnsCacheItem DnsCacheItem
;
37 enum DnsCacheItemType
{
44 DnsCacheItemType type
;
46 DnsResourceRecord
*rr
;
54 union in_addr_union owner_address
;
57 LIST_FIELDS(DnsCacheItem
, by_key
);
60 static void dns_cache_item_free(DnsCacheItem
*i
) {
64 dns_resource_record_unref(i
->rr
);
65 dns_resource_key_unref(i
->key
);
69 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
71 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
79 first
= hashmap_get(c
->by_key
, i
->key
);
80 LIST_REMOVE(by_key
, first
, i
);
83 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
85 hashmap_remove(c
->by_key
, i
->key
);
87 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
89 dns_cache_item_free(i
);
92 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
93 DnsCacheItem
*first
, *i
;
96 first
= hashmap_get(c
->by_key
, rr
->key
);
97 LIST_FOREACH(by_key
, i
, first
) {
98 r
= dns_resource_record_equal(i
->rr
, rr
);
102 dns_cache_item_unlink_and_free(c
, i
);
110 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
111 DnsCacheItem
*first
, *i
, *n
;
116 first
= hashmap_remove(c
->by_key
, key
);
120 LIST_FOREACH_SAFE(by_key
, i
, n
, first
) {
121 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
122 dns_cache_item_free(i
);
128 void dns_cache_flush(DnsCache
*c
) {
133 while ((key
= hashmap_first_key(c
->by_key
)))
134 dns_cache_remove_by_key(c
, key
);
136 assert(hashmap_size(c
->by_key
) == 0);
137 assert(prioq_size(c
->by_expiry
) == 0);
139 c
->by_key
= hashmap_free(c
->by_key
);
140 c
->by_expiry
= prioq_free(c
->by_expiry
);
143 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
149 /* Makes space for n new entries. Note that we actually allow
150 * the cache to grow beyond CACHE_MAX, but only when we shall
151 * add more RRs to the cache than CACHE_MAX at once. In that
152 * case the cache will be emptied completely otherwise. */
155 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
158 if (prioq_size(c
->by_expiry
) <= 0)
161 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
164 i
= prioq_peek(c
->by_expiry
);
167 /* Take an extra reference to the key so that it
168 * doesn't go away in the middle of the remove call */
169 key
= dns_resource_key_ref(i
->key
);
170 dns_cache_remove_by_key(c
, key
);
174 void dns_cache_prune(DnsCache
*c
) {
179 /* Remove all entries that are past their TTL */
184 i
= prioq_peek(c
->by_expiry
);
189 t
= now(clock_boottime_or_monotonic());
194 /* Depending whether this is an mDNS shared entry
195 * either remove only this one RR or the whole
198 dns_cache_item_unlink_and_free(c
, i
);
200 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
202 /* Take an extra reference to the key so that it
203 * doesn't go away in the middle of the remove call */
204 key
= dns_resource_key_ref(i
->key
);
205 dns_cache_remove_by_key(c
, key
);
210 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
211 const DnsCacheItem
*x
= a
, *y
= b
;
213 if (x
->until
< y
->until
)
215 if (x
->until
> y
->until
)
220 static int dns_cache_init(DnsCache
*c
) {
225 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
229 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
236 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
243 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
247 first
= hashmap_get(c
->by_key
, i
->key
);
249 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
251 /* Keep a reference to the original key, while we manipulate the list. */
252 k
= dns_resource_key_ref(first
->key
);
254 /* Now, try to reduce the number of keys we keep */
255 dns_resource_key_reduce(&first
->key
, &i
->key
);
258 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
260 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
262 LIST_PREPEND(by_key
, first
, i
);
263 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
265 r
= hashmap_put(c
->by_key
, i
->key
, i
);
267 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
275 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
281 LIST_FOREACH(by_key
, i
, hashmap_get(c
->by_key
, rr
->key
))
282 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
288 static usec_t
calculate_until(DnsResourceRecord
*rr
, uint32_t nsec_ttl
, usec_t timestamp
, bool use_soa_minimum
) {
294 ttl
= MIN(rr
->ttl
, nsec_ttl
);
295 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
296 /* If this is a SOA RR, and it is requested, clamp to
297 * the SOA's minimum field. This is used when we do
298 * negative caching, to determine the TTL for the
299 * negative caching entry. See RFC 2308, Section
302 if (ttl
> rr
->soa
.minimum
)
303 ttl
= rr
->soa
.minimum
;
306 u
= ttl
* USEC_PER_SEC
;
307 if (u
> CACHE_TTL_MAX_USEC
)
308 u
= CACHE_TTL_MAX_USEC
;
310 if (rr
->expiry
!= USEC_INFINITY
) {
313 /* Make use of the DNSSEC RRSIG expiry info, if we
316 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
321 return timestamp
+ u
;
324 static void dns_cache_item_update_positive(
327 DnsResourceRecord
*rr
,
333 const union in_addr_union
*owner_address
) {
338 assert(owner_address
);
340 i
->type
= DNS_CACHE_POSITIVE
;
343 /* We are the first item in the list, we need to
344 * update the key used in the hashmap */
346 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
348 dns_resource_record_ref(rr
);
349 dns_resource_record_unref(i
->rr
);
352 dns_resource_key_unref(i
->key
);
353 i
->key
= dns_resource_key_ref(rr
->key
);
355 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
356 i
->authenticated
= authenticated
;
357 i
->shared_owner
= shared_owner
;
359 i
->ifindex
= ifindex
;
361 i
->owner_family
= owner_family
;
362 i
->owner_address
= *owner_address
;
364 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
367 static int dns_cache_put_positive(
369 DnsResourceRecord
*rr
,
375 const union in_addr_union
*owner_address
) {
377 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
378 _cleanup_free_
char *key_str
= NULL
;
379 DnsCacheItem
*existing
;
384 assert(owner_address
);
386 /* Never cache pseudo RRs */
387 if (dns_class_is_pseudo(rr
->key
->class))
389 if (dns_type_is_pseudo(rr
->key
->type
))
392 /* New TTL is 0? Delete this specific entry... */
394 k
= dns_cache_remove_by_rr(c
, rr
);
396 if (log_get_max_level() >= LOG_DEBUG
) {
397 r
= dns_resource_key_to_string(rr
->key
, &key_str
);
402 log_debug("Removed zero TTL entry from cache: %s", key_str
);
404 log_debug("Not caching zero TTL cache entry: %s", key_str
);
410 /* Entry exists already? Update TTL, timestamp and owner*/
411 existing
= dns_cache_get(c
, rr
);
413 dns_cache_item_update_positive(
426 /* Otherwise, add the new RR */
427 r
= dns_cache_init(c
);
431 dns_cache_make_space(c
, 1);
433 i
= new0(DnsCacheItem
, 1);
437 i
->type
= DNS_CACHE_POSITIVE
;
438 i
->key
= dns_resource_key_ref(rr
->key
);
439 i
->rr
= dns_resource_record_ref(rr
);
440 i
->until
= calculate_until(rr
, (uint32_t) -1, timestamp
, false);
441 i
->authenticated
= authenticated
;
442 i
->shared_owner
= shared_owner
;
443 i
->ifindex
= ifindex
;
444 i
->owner_family
= owner_family
;
445 i
->owner_address
= *owner_address
;
446 i
->prioq_idx
= PRIOQ_IDX_NULL
;
448 r
= dns_cache_link_item(c
, i
);
452 if (log_get_max_level() >= LOG_DEBUG
) {
453 r
= dns_resource_key_to_string(i
->key
, &key_str
);
457 log_debug("Added positive cache entry for %s", key_str
);
464 static int dns_cache_put_negative(
471 DnsResourceRecord
*soa
,
473 const union in_addr_union
*owner_address
) {
475 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
476 _cleanup_free_
char *key_str
= NULL
;
482 assert(owner_address
);
484 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
485 * important to filter out as we use this as a pseudo-type for
486 * NXDOMAIN entries */
487 if (dns_class_is_pseudo(key
->class))
489 if (dns_type_is_pseudo(key
->type
))
492 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
493 if (log_get_max_level() >= LOG_DEBUG
) {
494 r
= dns_resource_key_to_string(key
, &key_str
);
498 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s", 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 if (log_get_max_level() >= LOG_DEBUG
) {
546 r
= dns_resource_key_to_string(i
->key
, &key_str
);
550 log_debug("Added %s cache entry for %s", i
->type
== DNS_CACHE_NODATA
? "NODATA" : "NXDOMAIN", key_str
);
557 static void dns_cache_remove_previous(
562 DnsResourceRecord
*rr
;
563 DnsAnswerFlags flags
;
567 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
568 * not on mDNS), delete all matching old RRs, so that we only
569 * keep complete by_key in place. */
571 dns_cache_remove_by_key(c
, key
);
573 /* Second, flush all entries matching the answer, unless this
574 * is an RR that is explicitly marked to be "shared" between
575 * peers (i.e. mDNS RRs without the flush-cache bit set). */
576 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
577 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
580 if (flags
& DNS_ANSWER_SHARED_OWNER
)
583 dns_cache_remove_by_key(c
, rr
->key
);
587 static bool rr_eligible(DnsResourceRecord
*rr
) {
590 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
591 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
592 * existence from any cached NSEC/NSEC3, but that should be fine. */
594 switch (rr
->key
->type
) {
597 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
598 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
601 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
602 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
618 const union in_addr_union
*owner_address
) {
620 DnsResourceRecord
*soa
= NULL
, *rr
;
621 DnsAnswerFlags flags
;
626 assert(owner_address
);
628 dns_cache_remove_previous(c
, key
, answer
);
630 if (dns_answer_size(answer
) <= 0) {
631 if (log_get_max_level() >= LOG_DEBUG
) {
632 _cleanup_free_
char *key_str
= NULL
;
634 r
= dns_resource_key_to_string(key
, &key_str
);
638 log_debug("Not caching negative entry without a SOA record: %s", key_str
);
644 /* We only care for positive replies and NXDOMAINs, on all
645 * other replies we will simply flush the respective entries,
647 if (!IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
))
650 cache_keys
= dns_answer_size(answer
);
654 /* Make some space for our new entries */
655 dns_cache_make_space(c
, cache_keys
);
658 timestamp
= now(clock_boottime_or_monotonic());
660 /* Second, add in positive entries for all contained RRs */
661 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
662 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
671 r
= dns_cache_put_positive(
674 flags
& DNS_ANSWER_AUTHENTICATED
,
675 flags
& DNS_ANSWER_SHARED_OWNER
,
678 owner_family
, owner_address
);
683 if (!key
) /* mDNS doesn't know negative caching, really */
686 /* Third, add in negative entries if the key has no RR */
687 r
= dns_answer_match_key(answer
, key
, NULL
);
693 /* But not if it has a matching CNAME/DNAME (the negative
694 * caching will be done on the canonical name, not on the
696 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
702 /* See https://tools.ietf.org/html/rfc2308, which say that a
703 * matching SOA record in the packet is used to to enable
704 * negative caching. */
705 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
711 /* Refuse using the SOA data if it is unsigned, but the key is
713 if (authenticated
&& (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
716 r
= dns_cache_put_negative(
724 owner_family
, owner_address
);
731 /* Adding all RRs failed. Let's clean up what we already
732 * added, just in case */
735 dns_cache_remove_by_key(c
, key
);
737 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
738 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
741 dns_cache_remove_by_key(c
, rr
->key
);
747 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(DnsCache
*c
, DnsResourceKey
*k
) {
755 /* If we hit some OOM error, or suchlike, we don't care too
756 * much, after all this is just a cache */
758 i
= hashmap_get(c
->by_key
, k
);
762 n
= DNS_RESOURCE_KEY_NAME(k
);
764 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
765 * the pseudo-type ANY for NXDOMAIN cache items. */
766 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
767 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
770 if (dns_type_may_redirect(k
->type
)) {
771 /* Check if we have a CNAME record instead */
772 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
776 /* OK, let's look for cached DNAME records. */
781 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
785 /* Jump one label ahead */
786 r
= dns_name_parent(&n
);
792 if (k
->type
!= DNS_TYPE_NSEC
) {
793 /* Check if we have an NSEC record instead for the name. */
794 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
802 int dns_cache_lookup(DnsCache
*c
, DnsResourceKey
*key
, int *rcode
, DnsAnswer
**ret
, bool *authenticated
) {
803 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
806 bool nxdomain
= false;
807 _cleanup_free_
char *key_str
= NULL
;
808 DnsCacheItem
*j
, *first
, *nsec
= NULL
;
809 bool have_authenticated
= false, have_non_authenticated
= false;
815 assert(authenticated
);
817 if (key
->type
== DNS_TYPE_ANY
||
818 key
->class == DNS_CLASS_ANY
) {
820 /* If we have ANY lookups we don't use the cache, so
821 * that the caller refreshes via the network. */
823 if (log_get_max_level() >= LOG_DEBUG
) {
824 r
= dns_resource_key_to_string(key
, &key_str
);
828 log_debug("Ignoring cache for ANY lookup: %s", key_str
);
834 *rcode
= DNS_RCODE_SUCCESS
;
838 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
);
840 /* If one question cannot be answered we need to refresh */
842 if (log_get_max_level() >= LOG_DEBUG
) {
843 r
= dns_resource_key_to_string(key
, &key_str
);
847 log_debug("Cache miss for %s", key_str
);
853 *rcode
= DNS_RCODE_SUCCESS
;
857 LIST_FOREACH(by_key
, j
, first
) {
859 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
863 } else if (j
->type
== DNS_CACHE_NXDOMAIN
)
866 if (j
->authenticated
)
867 have_authenticated
= true;
869 have_non_authenticated
= true;
872 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
873 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC RRs from
874 * the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
876 if (log_get_max_level() >= LOG_DEBUG
) {
877 r
= dns_resource_key_to_string(key
, &key_str
);
881 log_debug("NSEC NODATA cache hit for %s", key_str
);
884 /* We only found an NSEC record that matches our name.
885 * If it says the type doesn't exist report
886 * NODATA. Otherwise report a cache miss. */
889 *rcode
= DNS_RCODE_SUCCESS
;
890 *authenticated
= nsec
->authenticated
;
892 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
893 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
894 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
903 if (log_get_max_level() >= LOG_DEBUG
) {
904 r
= dns_resource_key_to_string(key
, &key_str
);
908 log_debug("%s cache hit for %s",
910 nxdomain
? "NXDOMAIN" : "NODATA",
918 *rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
919 *authenticated
= have_authenticated
&& !have_non_authenticated
;
923 answer
= dns_answer_new(n
);
927 LIST_FOREACH(by_key
, j
, first
) {
931 r
= dns_answer_add(answer
, j
->rr
, j
->ifindex
, j
->authenticated
? DNS_ANSWER_AUTHENTICATED
: 0);
939 *rcode
= DNS_RCODE_SUCCESS
;
940 *authenticated
= have_authenticated
&& !have_non_authenticated
;
946 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
947 DnsCacheItem
*i
, *first
;
948 bool same_owner
= true;
953 dns_cache_prune(cache
);
955 /* See if there's a cache entry for the same key. If there
956 * isn't there's no conflict */
957 first
= hashmap_get(cache
->by_key
, rr
->key
);
961 /* See if the RR key is owned by the same owner, if so, there
962 * isn't a conflict either */
963 LIST_FOREACH(by_key
, i
, first
) {
964 if (i
->owner_family
!= owner_family
||
965 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
973 /* See if there's the exact same RR in the cache. If yes, then
974 * there's no conflict. */
975 if (dns_cache_get(cache
, rr
))
978 /* There's a conflict */
982 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
) {
983 unsigned ancount
= 0;
991 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
994 LIST_FOREACH(by_key
, j
, i
) {
998 if (!j
->shared_owner
)
1001 r
= dns_packet_append_rr(p
, j
->rr
, NULL
, NULL
);
1002 if (r
== -EMSGSIZE
&& p
->protocol
== DNS_PROTOCOL_MDNS
) {
1003 /* For mDNS, if we're unable to stuff all known answers into the given packet,
1004 * allocate a new one, push the RR into that one and link it to the current one.
1007 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1010 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1014 /* continue with new packet */
1016 r
= dns_packet_append_rr(p
, j
->rr
, NULL
, NULL
);
1026 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1031 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1042 HASHMAP_FOREACH(i
, cache
->by_key
, iterator
) {
1045 LIST_FOREACH(by_key
, j
, i
) {
1051 t
= dns_resource_record_to_string(j
->rr
);
1060 _cleanup_free_
char *z
= NULL
;
1061 r
= dns_resource_key_to_string(j
->key
, &z
);
1069 fputs(j
->type
== DNS_CACHE_NODATA
? "NODATA" : "NXDOMAIN", f
);
1076 bool dns_cache_is_empty(DnsCache
*cache
) {
1080 return hashmap_isempty(cache
->by_key
);
1083 unsigned dns_cache_size(DnsCache
*cache
) {
1087 return hashmap_size(cache
->by_key
);