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_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
96 /* There's already an RR of the same RRset in place! Let's see if the TTLs more or less
97 * match. We don't really care if they match precisely, but we do care whether one is 0 and
98 * the other is not. See RFC 2181, Section 5.2. */
99 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
102 r
= dns_resource_record_payload_equal(a
->items
[i
].rr
, rr
);
108 /* Entry already exists, keep the entry with the higher RR. */
109 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
110 dns_resource_record_ref(rr
);
111 dns_resource_record_unref(a
->items
[i
].rr
);
115 a
->items
[i
].flags
|= flags
;
119 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
122 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
123 DnsResourceRecord
*rr
;
124 DnsAnswerFlags flags
;
127 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
128 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
136 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
142 r
= dns_answer_reserve_or_clone(a
, 1);
146 return dns_answer_add(*a
, rr
, ifindex
, flags
);
149 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
150 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
152 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
158 soa
->soa
.mname
= strdup(name
);
162 soa
->soa
.rname
= strappend("root.", name
);
167 soa
->soa
.refresh
= 1;
170 soa
->soa
.minimum
= ttl
;
172 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
175 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
176 DnsAnswerFlags flags
= 0, i_flags
;
177 DnsResourceRecord
*i
;
183 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
184 r
= dns_resource_key_match_rr(key
, i
, NULL
);
207 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
208 DnsResourceRecord
*i
;
210 DNS_ANSWER_FOREACH(i
, a
) {
211 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
218 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
219 DnsResourceRecord
*rr
;
222 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
224 DNS_ANSWER_FOREACH(rr
, answer
) {
227 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
230 p
= dns_resource_key_name(rr
->key
);
231 r
= dns_name_parent(&p
);
237 r
= dns_name_equal(p
, zone
);
245 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
246 DnsResourceRecord
*rr
, *soa
= NULL
;
247 DnsAnswerFlags rr_flags
, soa_flags
= 0;
252 /* For a SOA record we can never find a matching SOA record */
253 if (key
->type
== DNS_TYPE_SOA
)
256 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
257 r
= dns_resource_key_match_soa(key
, rr
->key
);
263 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
271 soa_flags
= rr_flags
;
286 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
287 DnsResourceRecord
*rr
;
288 DnsAnswerFlags rr_flags
;
293 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
294 if (!dns_type_may_redirect(key
->type
))
297 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
298 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
313 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
314 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
319 if (dns_answer_size(a
) <= 0) {
320 *ret
= dns_answer_ref(b
);
324 if (dns_answer_size(b
) <= 0) {
325 *ret
= dns_answer_ref(a
);
329 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
333 r
= dns_answer_add_raw_all(k
, a
);
337 r
= dns_answer_add_all(k
, b
);
346 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
352 r
= dns_answer_merge(*a
, b
, &merged
);
356 dns_answer_unref(*a
);
362 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
363 bool found
= false, other
= false;
364 DnsResourceRecord
*rr
;
371 /* Remove all entries matching the specified key from *a */
373 DNS_ANSWER_FOREACH(rr
, *a
) {
374 r
= dns_resource_key_equal(rr
->key
, key
);
390 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
394 if ((*a
)->n_ref
> 1) {
395 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
396 DnsAnswerFlags flags
;
399 copy
= dns_answer_new((*a
)->n_rrs
);
403 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
404 r
= dns_resource_key_equal(rr
->key
, key
);
410 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
415 dns_answer_unref(*a
);
421 /* Only a single reference, edit in-place */
425 if (i
>= (*a
)->n_rrs
)
428 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
432 /* Kill this entry */
434 dns_resource_record_unref((*a
)->items
[i
].rr
);
435 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
440 /* Keep this entry */
447 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
448 bool found
= false, other
= false;
449 DnsResourceRecord
*rr
;
456 /* Remove all entries matching the specified RR from *a */
458 DNS_ANSWER_FOREACH(rr
, *a
) {
459 r
= dns_resource_record_equal(rr
, rm
);
475 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
479 if ((*a
)->n_ref
> 1) {
480 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
481 DnsAnswerFlags flags
;
484 copy
= dns_answer_new((*a
)->n_rrs
);
488 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
489 r
= dns_resource_record_equal(rr
, rm
);
495 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
500 dns_answer_unref(*a
);
506 /* Only a single reference, edit in-place */
510 if (i
>= (*a
)->n_rrs
)
513 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
517 /* Kill this entry */
519 dns_resource_record_unref((*a
)->items
[i
].rr
);
520 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
525 /* Keep this entry */
532 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
533 DnsResourceRecord
*rr_source
;
534 int ifindex_source
, r
;
535 DnsAnswerFlags flags_source
;
540 /* Copy all RRs matching the specified key from source into *a */
542 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
544 r
= dns_resource_key_equal(rr_source
->key
, key
);
550 /* Make space for at least one entry */
551 r
= dns_answer_reserve_or_clone(a
, 1);
555 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
563 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
570 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
574 return dns_answer_remove_by_key(from
, key
);
577 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
578 DnsAnswerItem
*items
;
579 size_t i
, start
, end
;
590 /* RFC 4795, Section 2.6 suggests we should order entries
591 * depending on whether the sender is a link-local address. */
593 items
= newa(DnsAnswerItem
, a
->n_rrs
);
594 for (i
= 0; i
< a
->n_rrs
; i
++) {
596 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
597 ((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
) ||
598 (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
)))
599 /* Order address records that are not preferred to the end of the array */
600 items
[end
--] = a
->items
[i
];
602 /* Order all other records to the beginning of the array */
603 items
[start
++] = a
->items
[i
];
606 assert(start
== end
+1);
607 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
610 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
624 ns
= (*a
)->n_rrs
+ n_free
;
626 if ((*a
)->n_allocated
>= ns
)
629 /* Allocate more than we need */
632 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
638 n
= dns_answer_new(n_free
);
647 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
648 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
653 /* Tries to extend the DnsAnswer object. And if that's not
654 * possible, since we are not the sole owner, then allocate a
655 * new, appropriately sized one. Either way, after this call
656 * the object will only have a single reference, and has room
657 * for at least the specified number of RRs. */
659 r
= dns_answer_reserve(a
, n_free
);
665 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
669 r
= dns_answer_add_raw_all(n
, *a
);
673 dns_answer_unref(*a
);
679 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
680 DnsResourceRecord
*rr
;
681 DnsAnswerFlags flags
;
687 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
692 t
= dns_resource_record_to_string(rr
);
700 if (ifindex
!= 0 || flags
& (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SHARED_OWNER
))
704 printf(" ifindex=%i", ifindex
);
705 if (flags
& DNS_ANSWER_AUTHENTICATED
)
706 fputs(" authenticated", f
);
707 if (flags
& DNS_ANSWER_CACHEABLE
)
708 fputs(" cachable", f
);
709 if (flags
& DNS_ANSWER_SHARED_OWNER
)
710 fputs(" shared-owner", f
);
716 int dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
717 DnsResourceRecord
*rr
;
722 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
723 * synthesized from it */
725 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
728 DNS_ANSWER_FOREACH(rr
, a
) {
729 _cleanup_free_
char *n
= NULL
;
731 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
733 if (rr
->key
->class != cname
->key
->class)
736 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
742 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));