1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "dns-domain.h"
7 #include "resolved-dns-answer.h"
8 #include "resolved-dns-dnssec.h"
9 #include "string-util.h"
11 DnsAnswer
*dns_answer_new(size_t n
) {
14 if (n
> UINT16_MAX
) /* We can only place 64K RRs in an answer at max */
17 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
27 static void dns_answer_flush(DnsAnswer
*a
) {
28 DnsResourceRecord
*rr
;
33 DNS_ANSWER_FOREACH(rr
, a
)
34 dns_resource_record_unref(rr
);
39 static DnsAnswer
*dns_answer_free(DnsAnswer
*a
) {
46 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer
, dns_answer
, dns_answer_free
);
48 static int dns_answer_add_raw(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
54 if (a
->n_rrs
>= a
->n_allocated
)
57 a
->items
[a
->n_rrs
++] = (DnsAnswerItem
) {
58 .rr
= dns_resource_record_ref(rr
),
66 static int dns_answer_add_raw_all(DnsAnswer
*a
, DnsAnswer
*source
) {
67 DnsResourceRecord
*rr
;
71 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, source
) {
72 r
= dns_answer_add_raw(a
, rr
, ifindex
, flags
);
80 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
91 for (i
= 0; i
< a
->n_rrs
; i
++) {
92 if (a
->items
[i
].ifindex
!= ifindex
)
95 r
= dns_resource_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
101 /* There's already an RR of the same RRset in place! Let's see if the TTLs more or less
102 * match. We don't really care if they match precisely, but we do care whether one is 0 and
103 * the other is not. See RFC 2181, Section 5.2. */
104 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
107 r
= dns_resource_record_payload_equal(a
->items
[i
].rr
, rr
);
113 /* Entry already exists, keep the entry with the higher RR. */
114 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
115 dns_resource_record_ref(rr
);
116 dns_resource_record_unref(a
->items
[i
].rr
);
120 a
->items
[i
].flags
|= flags
;
124 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
127 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
128 DnsResourceRecord
*rr
;
129 DnsAnswerFlags flags
;
132 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
133 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
141 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
147 r
= dns_answer_reserve_or_clone(a
, 1);
151 return dns_answer_add(*a
, rr
, ifindex
, flags
);
154 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
155 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
157 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
163 soa
->soa
.mname
= strdup(name
);
167 soa
->soa
.rname
= strjoin("root.", name
);
172 soa
->soa
.refresh
= 1;
175 soa
->soa
.minimum
= ttl
;
177 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
180 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
181 DnsAnswerFlags flags
= 0, i_flags
;
182 DnsResourceRecord
*i
;
188 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
189 r
= dns_resource_key_match_rr(key
, i
, NULL
);
212 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
213 DnsResourceRecord
*i
;
215 DNS_ANSWER_FOREACH(i
, a
) {
216 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
223 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
224 DnsResourceRecord
*rr
;
227 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
229 DNS_ANSWER_FOREACH(rr
, answer
) {
232 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
235 p
= dns_resource_key_name(rr
->key
);
236 r
= dns_name_parent(&p
);
242 r
= dns_name_equal(p
, zone
);
250 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
251 DnsResourceRecord
*rr
, *soa
= NULL
;
252 DnsAnswerFlags rr_flags
, soa_flags
= 0;
257 /* For a SOA record we can never find a matching SOA record */
258 if (key
->type
== DNS_TYPE_SOA
)
261 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
262 r
= dns_resource_key_match_soa(key
, rr
->key
);
268 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
276 soa_flags
= rr_flags
;
291 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
292 DnsResourceRecord
*rr
;
293 DnsAnswerFlags rr_flags
;
298 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
299 if (!dns_type_may_redirect(key
->type
))
302 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
303 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
318 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
319 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
325 *ret
= dns_answer_ref(a
);
329 if (dns_answer_size(a
) <= 0) {
330 *ret
= dns_answer_ref(b
);
334 if (dns_answer_size(b
) <= 0) {
335 *ret
= dns_answer_ref(a
);
339 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
343 r
= dns_answer_add_raw_all(k
, a
);
347 r
= dns_answer_add_all(k
, b
);
356 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
362 r
= dns_answer_merge(*a
, b
, &merged
);
366 dns_answer_unref(*a
);
372 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
373 bool found
= false, other
= false;
374 DnsResourceRecord
*rr
;
381 /* Remove all entries matching the specified key from *a */
383 DNS_ANSWER_FOREACH(rr
, *a
) {
384 r
= dns_resource_key_equal(rr
->key
, key
);
400 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
404 if ((*a
)->n_ref
> 1) {
405 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
406 DnsAnswerFlags flags
;
409 copy
= dns_answer_new((*a
)->n_rrs
);
413 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
414 r
= dns_resource_key_equal(rr
->key
, key
);
420 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
425 dns_answer_unref(*a
);
431 /* Only a single reference, edit in-place */
435 if (i
>= (*a
)->n_rrs
)
438 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
442 /* Kill this entry */
444 dns_resource_record_unref((*a
)->items
[i
].rr
);
445 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
450 /* Keep this entry */
457 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
458 bool found
= false, other
= false;
459 DnsResourceRecord
*rr
;
466 /* Remove all entries matching the specified RR from *a */
468 DNS_ANSWER_FOREACH(rr
, *a
) {
469 r
= dns_resource_record_equal(rr
, rm
);
485 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
489 if ((*a
)->n_ref
> 1) {
490 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
491 DnsAnswerFlags flags
;
494 copy
= dns_answer_new((*a
)->n_rrs
);
498 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
499 r
= dns_resource_record_equal(rr
, rm
);
505 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
510 dns_answer_unref(*a
);
516 /* Only a single reference, edit in-place */
520 if (i
>= (*a
)->n_rrs
)
523 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
527 /* Kill this entry */
529 dns_resource_record_unref((*a
)->items
[i
].rr
);
530 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
535 /* Keep this entry */
542 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
543 DnsResourceRecord
*rr_source
;
544 int ifindex_source
, r
;
545 DnsAnswerFlags flags_source
;
550 /* Copy all RRs matching the specified key from source into *a */
552 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
554 r
= dns_resource_key_equal(rr_source
->key
, key
);
560 /* Make space for at least one entry */
561 r
= dns_answer_reserve_or_clone(a
, 1);
565 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
573 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
580 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
584 return dns_answer_remove_by_key(from
, key
);
587 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
588 DnsAnswerItem
*items
;
589 size_t i
, start
, end
;
600 /* RFC 4795, Section 2.6 suggests we should order entries
601 * depending on whether the sender is a link-local address. */
603 items
= newa(DnsAnswerItem
, a
->n_rrs
);
604 for (i
= 0; i
< a
->n_rrs
; i
++) {
606 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
607 ((a
->items
[i
].rr
->key
->type
== DNS_TYPE_A
&& in_addr_is_link_local(AF_INET
, (union in_addr_union
*) &a
->items
[i
].rr
->a
.in_addr
) != prefer_link_local
) ||
608 (a
->items
[i
].rr
->key
->type
== DNS_TYPE_AAAA
&& in_addr_is_link_local(AF_INET6
, (union in_addr_union
*) &a
->items
[i
].rr
->aaaa
.in6_addr
) != prefer_link_local
)))
609 /* Order address records that are not preferred to the end of the array */
610 items
[end
--] = a
->items
[i
];
612 /* Order all other records to the beginning of the array */
613 items
[start
++] = a
->items
[i
];
616 assert(start
== end
+1);
617 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
620 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
634 ns
= (*a
)->n_rrs
+ n_free
;
635 if (ns
> UINT16_MAX
) /* Maximum number of RRs we can stick into a DNS packet section */
638 if ((*a
)->n_allocated
>= ns
)
641 /* Allocate more than we need */
646 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
652 n
= dns_answer_new(n_free
);
661 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
662 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
667 /* Tries to extend the DnsAnswer object. And if that's not
668 * possible, since we are not the sole owner, then allocate a
669 * new, appropriately sized one. Either way, after this call
670 * the object will only have a single reference, and has room
671 * for at least the specified number of RRs. */
673 r
= dns_answer_reserve(a
, n_free
);
679 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
683 r
= dns_answer_add_raw_all(n
, *a
);
687 dns_answer_unref(*a
);
694 * This function is not used in the code base, but is useful when debugging. Do not delete.
696 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
697 DnsResourceRecord
*rr
;
698 DnsAnswerFlags flags
;
704 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
709 t
= dns_resource_record_to_string(rr
);
717 if (ifindex
!= 0 || flags
!= 0)
721 fprintf(f
, " ifindex=%i", ifindex
);
722 if (flags
& DNS_ANSWER_AUTHENTICATED
)
723 fputs(" authenticated", f
);
724 if (flags
& DNS_ANSWER_CACHEABLE
)
725 fputs(" cacheable", f
);
726 if (flags
& DNS_ANSWER_SHARED_OWNER
)
727 fputs(" shared-owner", f
);
728 if (flags
& DNS_ANSWER_CACHE_FLUSH
)
729 fputs(" cache-flush", f
);
730 if (flags
& DNS_ANSWER_GOODBYE
)
731 fputs(" goodbye", f
);
737 int dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
738 DnsResourceRecord
*rr
;
743 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
744 * synthesized from it */
746 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
749 DNS_ANSWER_FOREACH(rr
, a
) {
750 _cleanup_free_
char *n
= NULL
;
752 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
754 if (rr
->key
->class != cname
->key
->class)
757 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
763 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));