1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 a
= malloc0(offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * n
);
24 static void dns_answer_flush(DnsAnswer
*a
) {
25 DnsResourceRecord
*rr
;
30 DNS_ANSWER_FOREACH(rr
, a
)
31 dns_resource_record_unref(rr
);
36 static DnsAnswer
*dns_answer_free(DnsAnswer
*a
) {
43 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsAnswer
, dns_answer
, dns_answer_free
);
45 static int dns_answer_add_raw(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
51 if (a
->n_rrs
>= a
->n_allocated
)
54 a
->items
[a
->n_rrs
++] = (DnsAnswerItem
) {
55 .rr
= dns_resource_record_ref(rr
),
63 static int dns_answer_add_raw_all(DnsAnswer
*a
, DnsAnswer
*source
) {
64 DnsResourceRecord
*rr
;
68 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, source
) {
69 r
= dns_answer_add_raw(a
, rr
, ifindex
, flags
);
77 int dns_answer_add(DnsAnswer
*a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
88 for (i
= 0; i
< a
->n_rrs
; i
++) {
89 if (a
->items
[i
].ifindex
!= ifindex
)
92 r
= dns_resource_key_equal(a
->items
[i
].rr
->key
, rr
->key
);
98 /* There's already an RR of the same RRset in place! Let's see if the TTLs more or less
99 * match. We don't really care if they match precisely, but we do care whether one is 0 and
100 * the other is not. See RFC 2181, Section 5.2. */
101 if ((rr
->ttl
== 0) != (a
->items
[i
].rr
->ttl
== 0))
104 r
= dns_resource_record_payload_equal(a
->items
[i
].rr
, rr
);
110 /* Entry already exists, keep the entry with the higher RR. */
111 if (rr
->ttl
> a
->items
[i
].rr
->ttl
) {
112 dns_resource_record_ref(rr
);
113 dns_resource_record_unref(a
->items
[i
].rr
);
117 a
->items
[i
].flags
|= flags
;
121 return dns_answer_add_raw(a
, rr
, ifindex
, flags
);
124 static int dns_answer_add_all(DnsAnswer
*a
, DnsAnswer
*b
) {
125 DnsResourceRecord
*rr
;
126 DnsAnswerFlags flags
;
129 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, b
) {
130 r
= dns_answer_add(a
, rr
, ifindex
, flags
);
138 int dns_answer_add_extend(DnsAnswer
**a
, DnsResourceRecord
*rr
, int ifindex
, DnsAnswerFlags flags
) {
144 r
= dns_answer_reserve_or_clone(a
, 1);
148 return dns_answer_add(*a
, rr
, ifindex
, flags
);
151 int dns_answer_add_soa(DnsAnswer
*a
, const char *name
, uint32_t ttl
, int ifindex
) {
152 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*soa
= NULL
;
154 soa
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_SOA
, name
);
160 soa
->soa
.mname
= strdup(name
);
164 soa
->soa
.rname
= strappend("root.", name
);
169 soa
->soa
.refresh
= 1;
172 soa
->soa
.minimum
= ttl
;
174 return dns_answer_add(a
, soa
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
177 int dns_answer_match_key(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsAnswerFlags
*ret_flags
) {
178 DnsAnswerFlags flags
= 0, i_flags
;
179 DnsResourceRecord
*i
;
185 DNS_ANSWER_FOREACH_FLAGS(i
, i_flags
, a
) {
186 r
= dns_resource_key_match_rr(key
, i
, NULL
);
209 int dns_answer_contains_nsec_or_nsec3(DnsAnswer
*a
) {
210 DnsResourceRecord
*i
;
212 DNS_ANSWER_FOREACH(i
, a
) {
213 if (IN_SET(i
->key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_NSEC3
))
220 int dns_answer_contains_zone_nsec3(DnsAnswer
*answer
, const char *zone
) {
221 DnsResourceRecord
*rr
;
224 /* Checks whether the specified answer contains at least one NSEC3 RR in the specified zone */
226 DNS_ANSWER_FOREACH(rr
, answer
) {
229 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
232 p
= dns_resource_key_name(rr
->key
);
233 r
= dns_name_parent(&p
);
239 r
= dns_name_equal(p
, zone
);
247 int dns_answer_find_soa(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
248 DnsResourceRecord
*rr
, *soa
= NULL
;
249 DnsAnswerFlags rr_flags
, soa_flags
= 0;
254 /* For a SOA record we can never find a matching SOA record */
255 if (key
->type
== DNS_TYPE_SOA
)
258 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
259 r
= dns_resource_key_match_soa(key
, rr
->key
);
265 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(soa
->key
));
273 soa_flags
= rr_flags
;
288 int dns_answer_find_cname_or_dname(DnsAnswer
*a
, const DnsResourceKey
*key
, DnsResourceRecord
**ret
, DnsAnswerFlags
*flags
) {
289 DnsResourceRecord
*rr
;
290 DnsAnswerFlags rr_flags
;
295 /* For a {C,D}NAME record we can never find a matching {C,D}NAME record */
296 if (!dns_type_may_redirect(key
->type
))
299 DNS_ANSWER_FOREACH_FLAGS(rr
, rr_flags
, a
) {
300 r
= dns_resource_key_match_cname_or_dname(key
, rr
->key
, NULL
);
315 int dns_answer_merge(DnsAnswer
*a
, DnsAnswer
*b
, DnsAnswer
**ret
) {
316 _cleanup_(dns_answer_unrefp
) DnsAnswer
*k
= NULL
;
321 if (dns_answer_size(a
) <= 0) {
322 *ret
= dns_answer_ref(b
);
326 if (dns_answer_size(b
) <= 0) {
327 *ret
= dns_answer_ref(a
);
331 k
= dns_answer_new(a
->n_rrs
+ b
->n_rrs
);
335 r
= dns_answer_add_raw_all(k
, a
);
339 r
= dns_answer_add_all(k
, b
);
348 int dns_answer_extend(DnsAnswer
**a
, DnsAnswer
*b
) {
354 r
= dns_answer_merge(*a
, b
, &merged
);
358 dns_answer_unref(*a
);
364 int dns_answer_remove_by_key(DnsAnswer
**a
, const DnsResourceKey
*key
) {
365 bool found
= false, other
= false;
366 DnsResourceRecord
*rr
;
373 /* Remove all entries matching the specified key from *a */
375 DNS_ANSWER_FOREACH(rr
, *a
) {
376 r
= dns_resource_key_equal(rr
->key
, key
);
392 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
396 if ((*a
)->n_ref
> 1) {
397 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
398 DnsAnswerFlags flags
;
401 copy
= dns_answer_new((*a
)->n_rrs
);
405 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
406 r
= dns_resource_key_equal(rr
->key
, key
);
412 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
417 dns_answer_unref(*a
);
423 /* Only a single reference, edit in-place */
427 if (i
>= (*a
)->n_rrs
)
430 r
= dns_resource_key_equal((*a
)->items
[i
].rr
->key
, key
);
434 /* Kill this entry */
436 dns_resource_record_unref((*a
)->items
[i
].rr
);
437 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
442 /* Keep this entry */
449 int dns_answer_remove_by_rr(DnsAnswer
**a
, DnsResourceRecord
*rm
) {
450 bool found
= false, other
= false;
451 DnsResourceRecord
*rr
;
458 /* Remove all entries matching the specified RR from *a */
460 DNS_ANSWER_FOREACH(rr
, *a
) {
461 r
= dns_resource_record_equal(rr
, rm
);
477 *a
= dns_answer_unref(*a
); /* Return NULL for the empty answer */
481 if ((*a
)->n_ref
> 1) {
482 _cleanup_(dns_answer_unrefp
) DnsAnswer
*copy
= NULL
;
483 DnsAnswerFlags flags
;
486 copy
= dns_answer_new((*a
)->n_rrs
);
490 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, *a
) {
491 r
= dns_resource_record_equal(rr
, rm
);
497 r
= dns_answer_add_raw(copy
, rr
, ifindex
, flags
);
502 dns_answer_unref(*a
);
508 /* Only a single reference, edit in-place */
512 if (i
>= (*a
)->n_rrs
)
515 r
= dns_resource_record_equal((*a
)->items
[i
].rr
, rm
);
519 /* Kill this entry */
521 dns_resource_record_unref((*a
)->items
[i
].rr
);
522 memmove((*a
)->items
+ i
, (*a
)->items
+ i
+ 1, sizeof(DnsAnswerItem
) * ((*a
)->n_rrs
- i
- 1));
527 /* Keep this entry */
534 int dns_answer_copy_by_key(DnsAnswer
**a
, DnsAnswer
*source
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
535 DnsResourceRecord
*rr_source
;
536 int ifindex_source
, r
;
537 DnsAnswerFlags flags_source
;
542 /* Copy all RRs matching the specified key from source into *a */
544 DNS_ANSWER_FOREACH_FULL(rr_source
, ifindex_source
, flags_source
, source
) {
546 r
= dns_resource_key_equal(rr_source
->key
, key
);
552 /* Make space for at least one entry */
553 r
= dns_answer_reserve_or_clone(a
, 1);
557 r
= dns_answer_add(*a
, rr_source
, ifindex_source
, flags_source
|or_flags
);
565 int dns_answer_move_by_key(DnsAnswer
**to
, DnsAnswer
**from
, const DnsResourceKey
*key
, DnsAnswerFlags or_flags
) {
572 r
= dns_answer_copy_by_key(to
, *from
, key
, or_flags
);
576 return dns_answer_remove_by_key(from
, key
);
579 void dns_answer_order_by_scope(DnsAnswer
*a
, bool prefer_link_local
) {
580 DnsAnswerItem
*items
;
581 size_t i
, start
, end
;
592 /* RFC 4795, Section 2.6 suggests we should order entries
593 * depending on whether the sender is a link-local address. */
595 items
= newa(DnsAnswerItem
, a
->n_rrs
);
596 for (i
= 0; i
< a
->n_rrs
; i
++) {
598 if (a
->items
[i
].rr
->key
->class == DNS_CLASS_IN
&&
599 ((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
) ||
600 (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
)))
601 /* Order address records that are not preferred to the end of the array */
602 items
[end
--] = a
->items
[i
];
604 /* Order all other records to the beginning of the array */
605 items
[start
++] = a
->items
[i
];
608 assert(start
== end
+1);
609 memcpy(a
->items
, items
, sizeof(DnsAnswerItem
) * a
->n_rrs
);
612 int dns_answer_reserve(DnsAnswer
**a
, size_t n_free
) {
626 ns
= (*a
)->n_rrs
+ n_free
;
628 if ((*a
)->n_allocated
>= ns
)
631 /* Allocate more than we need */
634 n
= realloc(*a
, offsetof(DnsAnswer
, items
) + sizeof(DnsAnswerItem
) * ns
);
640 n
= dns_answer_new(n_free
);
649 int dns_answer_reserve_or_clone(DnsAnswer
**a
, size_t n_free
) {
650 _cleanup_(dns_answer_unrefp
) DnsAnswer
*n
= NULL
;
655 /* Tries to extend the DnsAnswer object. And if that's not
656 * possible, since we are not the sole owner, then allocate a
657 * new, appropriately sized one. Either way, after this call
658 * the object will only have a single reference, and has room
659 * for at least the specified number of RRs. */
661 r
= dns_answer_reserve(a
, n_free
);
667 n
= dns_answer_new(((*a
)->n_rrs
+ n_free
) * 2);
671 r
= dns_answer_add_raw_all(n
, *a
);
675 dns_answer_unref(*a
);
681 void dns_answer_dump(DnsAnswer
*answer
, FILE *f
) {
682 DnsResourceRecord
*rr
;
683 DnsAnswerFlags flags
;
689 DNS_ANSWER_FOREACH_FULL(rr
, ifindex
, flags
, answer
) {
694 t
= dns_resource_record_to_string(rr
);
702 if (ifindex
!= 0 || flags
& (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
|DNS_ANSWER_SHARED_OWNER
))
706 printf(" ifindex=%i", ifindex
);
707 if (flags
& DNS_ANSWER_AUTHENTICATED
)
708 fputs(" authenticated", f
);
709 if (flags
& DNS_ANSWER_CACHEABLE
)
710 fputs(" cachable", f
);
711 if (flags
& DNS_ANSWER_SHARED_OWNER
)
712 fputs(" shared-owner", f
);
718 int dns_answer_has_dname_for_cname(DnsAnswer
*a
, DnsResourceRecord
*cname
) {
719 DnsResourceRecord
*rr
;
724 /* Checks whether the answer contains a DNAME record that indicates that the specified CNAME record is
725 * synthesized from it */
727 if (cname
->key
->type
!= DNS_TYPE_CNAME
)
730 DNS_ANSWER_FOREACH(rr
, a
) {
731 _cleanup_free_
char *n
= NULL
;
733 if (rr
->key
->type
!= DNS_TYPE_DNAME
)
735 if (rr
->key
->class != cname
->key
->class)
738 r
= dns_name_change_suffix(cname
->cname
.name
, rr
->dname
.name
, dns_resource_key_name(rr
->key
), &n
);
744 r
= dns_name_equal(n
, dns_resource_key_name(cname
->key
));