1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "resolved-dns-answer.h"
9 #include "resolved-dns-dnssec.h"
10 #include "string-util.h"
12 DnsAnswer
*dns_answer_new(size_t n
) {
15 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
25 DnsAnswer
*dns_answer_ref(DnsAnswer
*a
) {
34 static void dns_answer_flush(DnsAnswer
*a
) {
35 DnsResourceRecord
*rr
;
40 DNS_ANSWER_FOREACH(rr
, a
)
41 dns_resource_record_unref(rr
);
46 DnsAnswer
*dns_answer_unref(DnsAnswer
*a
) {
61 static int dns_answer_add_raw(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
67 if (a
->n_rrs
>= a
->n_allocated
)
70 a
->items
[a
->n_rrs
++] = (DnsAnswerItem
) {
71 .rr
= dns_resource_record_ref(rr
),
79 static int dns_answer_add_raw_all(DnsAnswer
*a
, DnsAnswer
*source
) {
80 DnsResourceRecord
*rr
;
84 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, source
) {
85 r
= dns_answer_add_raw(a
, rr
, ifindex
, flags
);
93 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
104 for (i
= 0; i
< a
->n_rrs
; i
++) {
105 if (a
->items
[i
].ifindex
!= ifindex
)
108 r
= dns_resource_record_equal(a
->items
[i
].rr
, rr
);
112 /* Don't mix contradicting TTLs (see below) */
113 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
116 /* Entry already exists, keep the entry with
118 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
119 dns_resource_record_ref(rr
);
120 dns_resource_record_unref(a
->items
[i
].rr
);
124 a
->items
[i
].flags
|= flags
;
128 r
= dns_resource_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
132 /* There's already an RR of the same RRset in
133 * place! Let's see if the TTLs more or less
134 * match. We don't really care if they match
135 * precisely, but we do care whether one is 0
136 * and the other is not. See RFC 2181, Section
139 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
144 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
147 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
148 DnsResourceRecord
*rr
;
149 DnsAnswerFlags flags
;
152 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
153 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
161 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
167 r
= dns_answer_reserve_or_clone(a
, 1);
171 return dns_answer_add(*a
, rr
, ifindex
, flags
);
174 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
175 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
177 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
183 soa
->soa
.mname
= strdup(name
);
187 soa
->soa
.rname
= strappend("root.", name
);
192 soa
->soa
.refresh
= 1;
195 soa
->soa
.minimum
= ttl
;
197 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
200 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
201 DnsAnswerFlags flags
= 0, i_flags
;
202 DnsResourceRecord
*i
;
208 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
209 r
= dns_resource_key_match_rr(key
, i
, NULL
);
232 int dns_answer_contains_rr(DnsAnswer
*a
, DnsResourceRecord
*rr
, DnsAnswerFlags
*ret_flags
) {
233 DnsAnswerFlags flags
= 0, i_flags
;
234 DnsResourceRecord
*i
;
240 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
241 r
= dns_resource_record_equal(i
, rr
);
264 int dns_answer_contains_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
265 DnsAnswerFlags flags
= 0, i_flags
;
266 DnsResourceRecord
*i
;
272 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
273 r
= dns_resource_key_equal(i
->key
, key
);
296 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
297 DnsResourceRecord
*i
;
299 DNS_ANSWER_FOREACH(i
, a
) {
300 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
307 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
308 DnsResourceRecord
*rr
;
311 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
313 DNS_ANSWER_FOREACH(rr
, answer
) {
316 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
319 p
= dns_resource_key_name(rr
->key
);
320 r
= dns_name_parent(&p
);
326 r
= dns_name_equal(p
, zone
);
334 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
335 DnsResourceRecord
*rr
, *soa
= NULL
;
336 DnsAnswerFlags rr_flags
, soa_flags
= 0;
341 /* For a SOA record we can never find a matching SOA record */
342 if (key
->type
== DNS_TYPE_SOA
)
345 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
346 r
= dns_resource_key_match_soa(key
, rr
->key
);
352 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
360 soa_flags
= rr_flags
;
375 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
376 DnsResourceRecord
*rr
;
377 DnsAnswerFlags rr_flags
;
382 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
383 if (!dns_type_may_redirect(key
->type
))
386 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
387 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
402 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
403 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
408 if (dns_answer_size(a
) <= 0) {
409 *ret
= dns_answer_ref(b
);
413 if (dns_answer_size(b
) <= 0) {
414 *ret
= dns_answer_ref(a
);
418 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
422 r
= dns_answer_add_raw_all(k
, a
);
426 r
= dns_answer_add_all(k
, b
);
435 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
441 r
= dns_answer_merge(*a
, b
, &merged
);
445 dns_answer_unref(*a
);
451 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
452 bool found
= false, other
= false;
453 DnsResourceRecord
*rr
;
460 /* Remove all entries matching the specified key from *a */
462 DNS_ANSWER_FOREACH(rr
, *a
) {
463 r
= dns_resource_key_equal(rr
->key
, key
);
479 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
483 if ((*a
)->n_ref
> 1) {
484 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
485 DnsAnswerFlags flags
;
488 copy
= dns_answer_new((*a
)->n_rrs
);
492 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
493 r
= dns_resource_key_equal(rr
->key
, key
);
499 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
504 dns_answer_unref(*a
);
510 /* Only a single reference, edit in-place */
514 if (i
>= (*a
)->n_rrs
)
517 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
521 /* Kill this entry */
523 dns_resource_record_unref((*a
)->items
[i
].rr
);
524 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
529 /* Keep this entry */
536 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
537 bool found
= false, other
= false;
538 DnsResourceRecord
*rr
;
545 /* Remove all entries matching the specified RR from *a */
547 DNS_ANSWER_FOREACH(rr
, *a
) {
548 r
= dns_resource_record_equal(rr
, rm
);
564 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
568 if ((*a
)->n_ref
> 1) {
569 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
570 DnsAnswerFlags flags
;
573 copy
= dns_answer_new((*a
)->n_rrs
);
577 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
578 r
= dns_resource_record_equal(rr
, rm
);
584 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
589 dns_answer_unref(*a
);
595 /* Only a single reference, edit in-place */
599 if (i
>= (*a
)->n_rrs
)
602 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
606 /* Kill this entry */
608 dns_resource_record_unref((*a
)->items
[i
].rr
);
609 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
614 /* Keep this entry */
621 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
622 DnsResourceRecord
*rr_source
;
623 int ifindex_source
, r
;
624 DnsAnswerFlags flags_source
;
629 /* Copy all RRs matching the specified key from source into *a */
631 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
633 r
= dns_resource_key_equal(rr_source
->key
, key
);
639 /* Make space for at least one entry */
640 r
= dns_answer_reserve_or_clone(a
, 1);
644 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
652 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
659 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
663 return dns_answer_remove_by_key(from
, key
);
666 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
667 DnsAnswerItem
*items
;
668 size_t i
, start
, end
;
679 /* RFC 4795, Section 2.6 suggests we should order entries
680 * depending on whether the sender is a link-local address. */
682 items
= newa(DnsAnswerItem
, a
->n_rrs
);
683 for (i
= 0; i
< a
->n_rrs
; i
++) {
685 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
686 ((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
) ||
687 (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
)))
688 /* Order address records that are not preferred to the end of the array */
689 items
[end
--] = a
->items
[i
];
691 /* Order all other records to the beginning of the array */
692 items
[start
++] = a
->items
[i
];
695 assert(start
== end
+1);
696 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
699 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
713 ns
= (*a
)->n_rrs
+ n_free
;
715 if ((*a
)->n_allocated
>= ns
)
718 /* Allocate more than we need */
721 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
727 n
= dns_answer_new(n_free
);
736 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
737 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
742 /* Tries to extend the DnsAnswer object. And if that's not
743 * possible, since we are not the sole owner, then allocate a
744 * new, appropriately sized one. Either way, after this call
745 * the object will only have a single reference, and has room
746 * for at least the specified number of RRs. */
748 r
= dns_answer_reserve(a
, n_free
);
754 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
758 r
= dns_answer_add_raw_all(n
, *a
);
762 dns_answer_unref(*a
);
768 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
769 DnsResourceRecord
*rr
;
770 DnsAnswerFlags flags
;
776 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
781 t
= dns_resource_record_to_string(rr
);
789 if (ifindex
!= 0 || flags
& (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SHARED_OWNER
))
793 printf(" ifindex=%i", ifindex
);
794 if (flags
& DNS_ANSWER_AUTHENTICATED
)
795 fputs(" authenticated", f
);
796 if (flags
& DNS_ANSWER_CACHEABLE
)
797 fputs(" cachable", f
);
798 if (flags
& DNS_ANSWER_SHARED_OWNER
)
799 fputs(" shared-owner", f
);
805 bool dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
806 DnsResourceRecord
*rr
;
811 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
812 * synthesized from it */
814 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
817 DNS_ANSWER_FOREACH(rr
, a
) {
818 _cleanup_free_
char *n
= NULL
;
820 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
822 if (rr
->key
->class != cname
->key
->class)
825 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
831 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));