1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 typedef struct DnsAnswer DnsAnswer;
5 typedef struct DnsAnswerItem DnsAnswerItem;
6
7 #include "macro.h"
8 #include "ordered-set.h"
9 #include "resolved-dns-rr.h"
10
11 /* A simple array of resource records. We keep track of the originating ifindex for each RR where that makes
12 * sense, so that we can qualify A and AAAA RRs referring to a local link with the right ifindex.
13 *
14 * Note that we usually encode the empty DnsAnswer object as a simple NULL. */
15
16 typedef enum DnsAnswerFlags {
17 DNS_ANSWER_AUTHENTICATED = 1 << 0, /* Item has been authenticated */
18 DNS_ANSWER_CACHEABLE = 1 << 1, /* Item is subject to caching */
19 DNS_ANSWER_SHARED_OWNER = 1 << 2, /* For mDNS: RRset may be owner by multiple peers */
20 DNS_ANSWER_CACHE_FLUSH = 1 << 3, /* For mDNS: sets cache-flush bit in the rrclass of response records */
21 DNS_ANSWER_GOODBYE = 1 << 4, /* For mDNS: item is subject to disappear */
22 DNS_ANSWER_SECTION_ANSWER = 1 << 5, /* When parsing: RR originates from answer section */
23 DNS_ANSWER_SECTION_AUTHORITY = 1 << 6, /* When parsing: RR originates from authority section */
24 DNS_ANSWER_SECTION_ADDITIONAL = 1 << 7, /* When parsing: RR originates from additional section */
25
26 DNS_ANSWER_MASK_SECTIONS = DNS_ANSWER_SECTION_ANSWER|
27 DNS_ANSWER_SECTION_AUTHORITY|
28 DNS_ANSWER_SECTION_ADDITIONAL,
29 } DnsAnswerFlags;
30
31 struct DnsAnswerItem {
32 unsigned n_ref;
33 DnsResourceRecord *rr;
34 DnsResourceRecord *rrsig; /* Optionally, also store RRSIG RR that successfully validates this item */
35 int ifindex;
36 DnsAnswerFlags flags;
37 };
38
39 struct DnsAnswer {
40 unsigned n_ref;
41 OrderedSet *items;
42 };
43
44 DnsAnswer *dns_answer_new(size_t n);
45 DnsAnswer *dns_answer_ref(DnsAnswer *a);
46 DnsAnswer *dns_answer_unref(DnsAnswer *a);
47
48 #define DNS_ANSWER_REPLACE(a, b) \
49 do { \
50 typeof(a)* _a = &(a); \
51 typeof(b) _b = (b); \
52 dns_answer_unref(*_a); \
53 *_a = _b; \
54 } while(0)
55
56 int dns_answer_add(DnsAnswer *a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags, DnsResourceRecord *rrsig);
57 int dns_answer_add_extend(DnsAnswer **a, DnsResourceRecord *rr, int ifindex, DnsAnswerFlags flags, DnsResourceRecord *rrsig);
58 int dns_answer_add_soa(DnsAnswer *a, const char *name, uint32_t ttl, int ifindex);
59
60 int dns_answer_match_key(DnsAnswer *a, const DnsResourceKey *key, DnsAnswerFlags *ret_flags);
61 bool dns_answer_contains_nsec_or_nsec3(DnsAnswer *a);
62 int dns_answer_contains_zone_nsec3(DnsAnswer *answer, const char *zone);
63 bool dns_answer_contains(DnsAnswer *answer, DnsResourceRecord *rr);
64
65 int dns_answer_find_soa(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *ret_flags);
66 int dns_answer_find_cname_or_dname(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord **ret, DnsAnswerFlags *ret_flags);
67
68 int dns_answer_merge(DnsAnswer *a, DnsAnswer *b, DnsAnswer **ret);
69 int dns_answer_extend(DnsAnswer **a, DnsAnswer *b);
70
71 void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local);
72
73 int dns_answer_reserve(DnsAnswer **a, size_t n_free);
74 int dns_answer_reserve_or_clone(DnsAnswer **a, size_t n_free);
75
76 int dns_answer_remove_by_key(DnsAnswer **a, const DnsResourceKey *key);
77 int dns_answer_remove_by_rr(DnsAnswer **a, DnsResourceRecord *rr);
78 int dns_answer_remove_by_answer_keys(DnsAnswer **a, DnsAnswer *b);
79
80 int dns_answer_copy_by_key(DnsAnswer **a, DnsAnswer *source, const DnsResourceKey *key, DnsAnswerFlags or_flags, DnsResourceRecord *rrsig);
81 int dns_answer_move_by_key(DnsAnswer **to, DnsAnswer **from, const DnsResourceKey *key, DnsAnswerFlags or_flags, DnsResourceRecord *rrsig);
82
83 int dns_answer_has_dname_for_cname(DnsAnswer *a, DnsResourceRecord *cname);
84
dns_answer_size(DnsAnswer * a)85 static inline size_t dns_answer_size(DnsAnswer *a) {
86 return a ? ordered_set_size(a->items) : 0;
87 }
88
dns_answer_isempty(DnsAnswer * a)89 static inline bool dns_answer_isempty(DnsAnswer *a) {
90 return dns_answer_size(a) <= 0;
91 }
92
93 void dns_answer_dump(DnsAnswer *answer, FILE *f);
94
95 void dns_answer_randomize(DnsAnswer *a);
96
97 uint32_t dns_answer_min_ttl(DnsAnswer *a);
98
99 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsAnswer*, dns_answer_unref);
100
101 typedef struct DnsAnswerIterator {
102 Iterator iterator;
103 DnsAnswer *answer;
104 DnsAnswerItem *item;
105 } DnsAnswerIterator;
106
107 #define _DNS_ANSWER_FOREACH(kk, a, i) \
108 for (DnsAnswerIterator i = { .iterator = ITERATOR_FIRST, .answer = (a) }; \
109 i.answer && \
110 ordered_set_iterate(i.answer->items, &i.iterator, (void**) &(i.item)) && \
111 (kk = i.item->rr, true); )
112
113 #define DNS_ANSWER_FOREACH(rr, a) _DNS_ANSWER_FOREACH(rr, a, UNIQ_T(i, UNIQ))
114
115 #define _DNS_ANSWER_FOREACH_IFINDEX(kk, ifi, a, i) \
116 for (DnsAnswerIterator i = { .iterator = ITERATOR_FIRST, .answer = (a) }; \
117 i.answer && \
118 ordered_set_iterate(i.answer->items, &i.iterator, (void**) &(i.item)) && \
119 (kk = i.item->rr, ifi = i.item->ifindex, true); )
120
121 #define DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, a) _DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, a, UNIQ_T(i, UNIQ))
122
123 #define _DNS_ANSWER_FOREACH_FLAGS(kk, fl, a, i) \
124 for (DnsAnswerIterator i = { .iterator = ITERATOR_FIRST, .answer = (a) }; \
125 i.answer && \
126 ordered_set_iterate(i.answer->items, &i.iterator, (void**) &(i.item)) && \
127 (kk = i.item->rr, fl = i.item->flags, true); )
128
129 #define DNS_ANSWER_FOREACH_FLAGS(rr, flags, a) _DNS_ANSWER_FOREACH_FLAGS(rr, flags, a, UNIQ_T(i, UNIQ))
130
131 #define _DNS_ANSWER_FOREACH_ITEM(item, a, i) \
132 for (DnsAnswerIterator i = { .iterator = ITERATOR_FIRST, .answer = (a) }; \
133 i.answer && \
134 ordered_set_iterate(i.answer->items, &i.iterator, (void**) &(i.item)) && \
135 (item = i.item, true); )
136
137 #define DNS_ANSWER_FOREACH_ITEM(item, a) _DNS_ANSWER_FOREACH_ITEM(item, a, UNIQ_T(i, UNIQ))
138