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