1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "dns-domain.h"
5 #include "resolved-dns-answer.h"
6 #include "resolved-dns-dnssec.h"
7 #include "string-util.h"
9 DnsAnswer
*dns_answer_new(size_t n
) {
12 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
22 static void dns_answer_flush(DnsAnswer
*a
) {
23 DnsResourceRecord
*rr
;
28 DNS_ANSWER_FOREACH(rr
, a
)
29 dns_resource_record_unref(rr
);
34 static DnsAnswer
*dns_answer_free(DnsAnswer
*a
) {
41 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer
, dns_answer
, dns_answer_free
);
43 static int dns_answer_add_raw(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
49 if (a
->n_rrs
>= a
->n_allocated
)
52 a
->items
[a
->n_rrs
++] = (DnsAnswerItem
) {
53 .rr
= dns_resource_record_ref(rr
),
61 static int dns_answer_add_raw_all(DnsAnswer
*a
, DnsAnswer
*source
) {
62 DnsResourceRecord
*rr
;
66 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, source
) {
67 r
= dns_answer_add_raw(a
, rr
, ifindex
, flags
);
75 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
86 for (i
= 0; i
< a
->n_rrs
; i
++) {
87 if (a
->items
[i
].ifindex
!= ifindex
)
90 r
= dns_resource_record_equal(a
->items
[i
].rr
, rr
);
94 /* Don't mix contradicting TTLs (see below) */
95 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
98 /* Entry already exists, keep the entry with
100 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
101 dns_resource_record_ref(rr
);
102 dns_resource_record_unref(a
->items
[i
].rr
);
106 a
->items
[i
].flags
|= flags
;
110 r
= dns_resource_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
114 /* There's already an RR of the same RRset in
115 * place! Let's see if the TTLs more or less
116 * match. We don't really care if they match
117 * precisely, but we do care whether one is 0
118 * and the other is not. See RFC 2181, Section
121 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
126 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
129 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
130 DnsResourceRecord
*rr
;
131 DnsAnswerFlags flags
;
134 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
135 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
143 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
149 r
= dns_answer_reserve_or_clone(a
, 1);
153 return dns_answer_add(*a
, rr
, ifindex
, flags
);
156 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
157 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
159 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
165 soa
->soa
.mname
= strdup(name
);
169 soa
->soa
.rname
= strappend("root.", name
);
174 soa
->soa
.refresh
= 1;
177 soa
->soa
.minimum
= ttl
;
179 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
182 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
183 DnsAnswerFlags flags
= 0, i_flags
;
184 DnsResourceRecord
*i
;
190 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
191 r
= dns_resource_key_match_rr(key
, i
, NULL
);
214 int dns_answer_contains_rr(DnsAnswer
*a
, DnsResourceRecord
*rr
, DnsAnswerFlags
*ret_flags
) {
215 DnsAnswerFlags flags
= 0, i_flags
;
216 DnsResourceRecord
*i
;
222 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
223 r
= dns_resource_record_equal(i
, rr
);
246 int dns_answer_contains_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
247 DnsAnswerFlags flags
= 0, i_flags
;
248 DnsResourceRecord
*i
;
254 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
255 r
= dns_resource_key_equal(i
->key
, key
);
278 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
279 DnsResourceRecord
*i
;
281 DNS_ANSWER_FOREACH(i
, a
) {
282 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
289 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
290 DnsResourceRecord
*rr
;
293 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
295 DNS_ANSWER_FOREACH(rr
, answer
) {
298 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
301 p
= dns_resource_key_name(rr
->key
);
302 r
= dns_name_parent(&p
);
308 r
= dns_name_equal(p
, zone
);
316 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
317 DnsResourceRecord
*rr
, *soa
= NULL
;
318 DnsAnswerFlags rr_flags
, soa_flags
= 0;
323 /* For a SOA record we can never find a matching SOA record */
324 if (key
->type
== DNS_TYPE_SOA
)
327 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
328 r
= dns_resource_key_match_soa(key
, rr
->key
);
334 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
342 soa_flags
= rr_flags
;
357 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
358 DnsResourceRecord
*rr
;
359 DnsAnswerFlags rr_flags
;
364 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
365 if (!dns_type_may_redirect(key
->type
))
368 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
369 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
384 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
385 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
390 if (dns_answer_size(a
) <= 0) {
391 *ret
= dns_answer_ref(b
);
395 if (dns_answer_size(b
) <= 0) {
396 *ret
= dns_answer_ref(a
);
400 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
404 r
= dns_answer_add_raw_all(k
, a
);
408 r
= dns_answer_add_all(k
, b
);
417 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
423 r
= dns_answer_merge(*a
, b
, &merged
);
427 dns_answer_unref(*a
);
433 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
434 bool found
= false, other
= false;
435 DnsResourceRecord
*rr
;
442 /* Remove all entries matching the specified key from *a */
444 DNS_ANSWER_FOREACH(rr
, *a
) {
445 r
= dns_resource_key_equal(rr
->key
, key
);
461 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
465 if ((*a
)->n_ref
> 1) {
466 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
467 DnsAnswerFlags flags
;
470 copy
= dns_answer_new((*a
)->n_rrs
);
474 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
475 r
= dns_resource_key_equal(rr
->key
, key
);
481 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
486 dns_answer_unref(*a
);
492 /* Only a single reference, edit in-place */
496 if (i
>= (*a
)->n_rrs
)
499 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
503 /* Kill this entry */
505 dns_resource_record_unref((*a
)->items
[i
].rr
);
506 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
511 /* Keep this entry */
518 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
519 bool found
= false, other
= false;
520 DnsResourceRecord
*rr
;
527 /* Remove all entries matching the specified RR from *a */
529 DNS_ANSWER_FOREACH(rr
, *a
) {
530 r
= dns_resource_record_equal(rr
, rm
);
546 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
550 if ((*a
)->n_ref
> 1) {
551 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
552 DnsAnswerFlags flags
;
555 copy
= dns_answer_new((*a
)->n_rrs
);
559 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
560 r
= dns_resource_record_equal(rr
, rm
);
566 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
571 dns_answer_unref(*a
);
577 /* Only a single reference, edit in-place */
581 if (i
>= (*a
)->n_rrs
)
584 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
588 /* Kill this entry */
590 dns_resource_record_unref((*a
)->items
[i
].rr
);
591 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
596 /* Keep this entry */
603 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
604 DnsResourceRecord
*rr_source
;
605 int ifindex_source
, r
;
606 DnsAnswerFlags flags_source
;
611 /* Copy all RRs matching the specified key from source into *a */
613 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
615 r
= dns_resource_key_equal(rr_source
->key
, key
);
621 /* Make space for at least one entry */
622 r
= dns_answer_reserve_or_clone(a
, 1);
626 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
634 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
641 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
645 return dns_answer_remove_by_key(from
, key
);
648 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
649 DnsAnswerItem
*items
;
650 size_t i
, start
, end
;
661 /* RFC 4795, Section 2.6 suggests we should order entries
662 * depending on whether the sender is a link-local address. */
664 items
= newa(DnsAnswerItem
, a
->n_rrs
);
665 for (i
= 0; i
< a
->n_rrs
; i
++) {
667 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
668 ((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
) ||
669 (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
)))
670 /* Order address records that are not preferred to the end of the array */
671 items
[end
--] = a
->items
[i
];
673 /* Order all other records to the beginning of the array */
674 items
[start
++] = a
->items
[i
];
677 assert(start
== end
+1);
678 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
681 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
695 ns
= (*a
)->n_rrs
+ n_free
;
697 if ((*a
)->n_allocated
>= ns
)
700 /* Allocate more than we need */
703 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
709 n
= dns_answer_new(n_free
);
718 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
719 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
724 /* Tries to extend the DnsAnswer object. And if that's not
725 * possible, since we are not the sole owner, then allocate a
726 * new, appropriately sized one. Either way, after this call
727 * the object will only have a single reference, and has room
728 * for at least the specified number of RRs. */
730 r
= dns_answer_reserve(a
, n_free
);
736 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
740 r
= dns_answer_add_raw_all(n
, *a
);
744 dns_answer_unref(*a
);
750 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
751 DnsResourceRecord
*rr
;
752 DnsAnswerFlags flags
;
758 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
763 t
= dns_resource_record_to_string(rr
);
771 if (ifindex
!= 0 || flags
& (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SHARED_OWNER
))
775 printf(" ifindex=%i", ifindex
);
776 if (flags
& DNS_ANSWER_AUTHENTICATED
)
777 fputs(" authenticated", f
);
778 if (flags
& DNS_ANSWER_CACHEABLE
)
779 fputs(" cachable", f
);
780 if (flags
& DNS_ANSWER_SHARED_OWNER
)
781 fputs(" shared-owner", f
);
787 int dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
788 DnsResourceRecord
*rr
;
793 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
794 * synthesized from it */
796 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
799 DNS_ANSWER_FOREACH(rr
, a
) {
800 _cleanup_free_
char *n
= NULL
;
802 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
804 if (rr
->key
->class != cname
->key
->class)
807 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
813 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));