1:255.16-alt1
[systemd_ALT.git] / src / resolve / resolved-dns-rr.h
blob1a12933b01e7b823a982402da24ea354e82e7bcb
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
4 #include <netinet/in.h>
6 #include "bitmap.h"
7 #include "dns-def.h"
8 #include "dns-type.h"
9 #include "hashmap.h"
10 #include "in-addr-util.h"
11 #include "json.h"
12 #include "list.h"
13 #include "string-util.h"
14 #include "time-util.h"
16 typedef struct DnsResourceKey DnsResourceKey;
17 typedef struct DnsResourceRecord DnsResourceRecord;
18 typedef struct DnsTxtItem DnsTxtItem;
20 /* DNSKEY RR flags */
21 #define DNSKEY_FLAG_SEP (UINT16_C(1) << 0)
22 #define DNSKEY_FLAG_REVOKE (UINT16_C(1) << 7)
23 #define DNSKEY_FLAG_ZONE_KEY (UINT16_C(1) << 8)
25 /* mDNS RR flags */
26 #define MDNS_RR_CACHE_FLUSH_OR_QU (UINT16_C(1) << 15)
28 /* DNSSEC algorithm identifiers, see
29 * http://tools.ietf.org/html/rfc4034#appendix-A.1 and
30 * https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
31 enum {
32 DNSSEC_ALGORITHM_RSAMD5 = 1,
33 DNSSEC_ALGORITHM_DH,
34 DNSSEC_ALGORITHM_DSA,
35 DNSSEC_ALGORITHM_ECC,
36 DNSSEC_ALGORITHM_RSASHA1,
37 DNSSEC_ALGORITHM_DSA_NSEC3_SHA1,
38 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1,
39 DNSSEC_ALGORITHM_RSASHA256 = 8, /* RFC 5702 */
40 DNSSEC_ALGORITHM_RSASHA512 = 10, /* RFC 5702 */
41 DNSSEC_ALGORITHM_ECC_GOST = 12, /* RFC 5933 */
42 DNSSEC_ALGORITHM_ECDSAP256SHA256 = 13, /* RFC 6605 */
43 DNSSEC_ALGORITHM_ECDSAP384SHA384 = 14, /* RFC 6605 */
44 DNSSEC_ALGORITHM_ED25519 = 15, /* RFC 8080 */
45 DNSSEC_ALGORITHM_ED448 = 16, /* RFC 8080 */
46 DNSSEC_ALGORITHM_INDIRECT = 252,
47 DNSSEC_ALGORITHM_PRIVATEDNS,
48 DNSSEC_ALGORITHM_PRIVATEOID,
49 _DNSSEC_ALGORITHM_MAX_DEFINED
52 /* DNSSEC digest identifiers, see
53 * https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
54 enum {
55 DNSSEC_DIGEST_SHA1 = 1,
56 DNSSEC_DIGEST_SHA256 = 2, /* RFC 4509 */
57 DNSSEC_DIGEST_GOST_R_34_11_94 = 3, /* RFC 5933 */
58 DNSSEC_DIGEST_SHA384 = 4, /* RFC 6605 */
59 _DNSSEC_DIGEST_MAX_DEFINED
62 /* DNSSEC NSEC3 hash algorithms, see
63 * https://www.iana.org/assignments/dnssec-nsec3-parameters/dnssec-nsec3-parameters.xhtml */
64 enum {
65 NSEC3_ALGORITHM_SHA1 = 1,
66 _NSEC3_ALGORITHM_MAX_DEFINED
69 struct DnsResourceKey {
70 unsigned n_ref; /* (unsigned -1) for const keys, see below */
71 uint16_t class, type;
72 char *_name; /* don't access directly, use dns_resource_key_name()! */
75 /* Creates a temporary resource key. This is only useful to quickly
76 * look up something, without allocating a full DnsResourceKey object
77 * for it. Note that it is not OK to take references to this kind of
78 * resource key object. */
79 #define DNS_RESOURCE_KEY_CONST(c, t, n) \
80 ((DnsResourceKey) { \
81 .n_ref = UINT_MAX, \
82 .class = c, \
83 .type = t, \
84 ._name = (char*) n, \
87 struct DnsTxtItem {
88 size_t length;
89 LIST_FIELDS(DnsTxtItem, items);
90 uint8_t data[];
93 struct DnsResourceRecord {
94 unsigned n_ref;
95 uint32_t ttl;
96 usec_t expiry; /* RRSIG signature expiry */
98 DnsResourceKey *key;
100 char *to_string;
102 /* How many labels to strip to determine "signer" of the RRSIG (aka, the zone). -1 if not signed. */
103 uint8_t n_skip_labels_signer;
104 /* How many labels to strip to determine "synthesizing source" of this RR, i.e. the wildcard's immediate parent. -1 if not signed. */
105 uint8_t n_skip_labels_source;
107 bool unparsable;
108 bool wire_format_canonical;
110 void *wire_format;
111 size_t wire_format_size;
112 size_t wire_format_rdata_offset;
114 union {
115 struct {
116 void *data;
117 size_t data_size;
118 } generic, opt;
120 struct {
121 char *name;
122 uint16_t priority;
123 uint16_t weight;
124 uint16_t port;
125 } srv;
127 struct {
128 char *name;
129 } ptr, ns, cname, dname;
131 struct {
132 char *cpu;
133 char *os;
134 } hinfo;
136 struct {
137 DnsTxtItem *items;
138 } txt, spf;
140 struct {
141 struct in_addr in_addr;
142 } a;
144 struct {
145 struct in6_addr in6_addr;
146 } aaaa;
148 struct {
149 char *mname;
150 char *rname;
151 uint32_t serial;
152 uint32_t refresh;
153 uint32_t retry;
154 uint32_t expire;
155 uint32_t minimum;
156 } soa;
158 struct {
159 char *exchange;
160 uint16_t priority;
161 } mx;
163 /* https://tools.ietf.org/html/rfc1876 */
164 struct {
165 uint8_t version;
166 uint8_t size;
167 uint8_t horiz_pre;
168 uint8_t vert_pre;
169 uint32_t latitude;
170 uint32_t longitude;
171 uint32_t altitude;
172 } loc;
174 /* https://tools.ietf.org/html/rfc4255#section-3.1 */
175 struct {
176 void *fingerprint;
177 size_t fingerprint_size;
179 uint8_t algorithm;
180 uint8_t fptype;
181 } sshfp;
183 /* http://tools.ietf.org/html/rfc4034#section-2.1 */
184 struct {
185 void* key;
186 size_t key_size;
188 uint16_t flags;
189 uint8_t protocol;
190 uint8_t algorithm;
191 } dnskey;
193 /* http://tools.ietf.org/html/rfc4034#section-3.1 */
194 struct {
195 char *signer;
196 void *signature;
197 size_t signature_size;
199 uint16_t type_covered;
200 uint8_t algorithm;
201 uint8_t labels;
202 uint32_t original_ttl;
203 uint32_t expiration;
204 uint32_t inception;
205 uint16_t key_tag;
206 } rrsig;
208 /* https://tools.ietf.org/html/rfc4034#section-4.1 */
209 struct {
210 char *next_domain_name;
211 Bitmap *types;
212 } nsec;
214 /* https://tools.ietf.org/html/rfc4034#section-5.1 */
215 struct {
216 void *digest;
217 size_t digest_size;
219 uint16_t key_tag;
220 uint8_t algorithm;
221 uint8_t digest_type;
222 } ds;
224 struct {
225 Bitmap *types;
226 void *salt;
227 size_t salt_size;
228 void *next_hashed_name;
229 size_t next_hashed_name_size;
231 uint8_t algorithm;
232 uint8_t flags;
233 uint16_t iterations;
234 } nsec3;
236 /* https://tools.ietf.org/html/draft-ietf-dane-protocol-23 */
237 struct {
238 void *data;
239 size_t data_size;
241 uint8_t cert_usage;
242 uint8_t selector;
243 uint8_t matching_type;
244 } tlsa;
246 /* https://tools.ietf.org/html/rfc6844 */
247 struct {
248 char *tag;
249 void *value;
250 size_t value_size;
252 uint8_t flags;
253 } caa;
256 /* Note: fields should be ordered to minimize alignment gaps. Use pahole! */
259 /* We use uint8_t for label counts above, and UINT8_MAX/-1 has special meaning. */
260 assert_cc(DNS_N_LABELS_MAX < UINT8_MAX);
262 static inline const void* DNS_RESOURCE_RECORD_RDATA(const DnsResourceRecord *rr) {
263 if (!rr)
264 return NULL;
266 if (!rr->wire_format)
267 return NULL;
269 assert(rr->wire_format_rdata_offset <= rr->wire_format_size);
270 return (uint8_t*) rr->wire_format + rr->wire_format_rdata_offset;
273 static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(const DnsResourceRecord *rr) {
274 if (!rr)
275 return 0;
276 if (!rr->wire_format)
277 return 0;
279 assert(rr->wire_format_rdata_offset <= rr->wire_format_size);
280 return rr->wire_format_size - rr->wire_format_rdata_offset;
283 static inline uint8_t DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(const DnsResourceRecord *rr) {
284 assert(rr);
285 assert(rr->key->type == DNS_TYPE_OPT);
287 return ((rr->ttl >> 16) & 0xFF) == 0;
290 DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name);
291 DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname);
292 int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name);
293 DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name);
294 DnsResourceKey* dns_resource_key_ref(DnsResourceKey *key);
295 DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key);
297 #define DNS_RESOURCE_KEY_REPLACE(a, b) \
298 do { \
299 typeof(a)* _a = &(a); \
300 typeof(b) _b = (b); \
301 dns_resource_key_unref(*_a); \
302 *_a = _b; \
303 } while(0)
305 const char* dns_resource_key_name(const DnsResourceKey *key);
306 bool dns_resource_key_is_address(const DnsResourceKey *key);
307 bool dns_resource_key_is_dnssd_ptr(const DnsResourceKey *key);
308 bool dns_resource_key_is_dnssd_two_label_ptr(const DnsResourceKey *key);
309 int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b);
310 int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain);
311 int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain);
312 int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa);
314 /* _DNS_{CLASS,TYPE}_STRING_MAX include one byte for NUL, which we use for space instead below.
315 * DNS_HOSTNAME_MAX does not include the NUL byte, so we need to add 1. */
316 #define DNS_RESOURCE_KEY_STRING_MAX (_DNS_CLASS_STRING_MAX + _DNS_TYPE_STRING_MAX + DNS_HOSTNAME_MAX + 1)
318 char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size);
319 ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out);
321 #define DNS_RESOURCE_KEY_TO_STRING(key) \
322 dns_resource_key_to_string(key, (char[DNS_RESOURCE_KEY_STRING_MAX]) {}, DNS_RESOURCE_KEY_STRING_MAX)
324 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref);
326 static inline bool dns_key_is_shared(const DnsResourceKey *key) {
327 return key->type == DNS_TYPE_PTR;
330 bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b);
332 DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key);
333 DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name);
334 DnsResourceRecord* dns_resource_record_ref(DnsResourceRecord *rr);
335 DnsResourceRecord* dns_resource_record_unref(DnsResourceRecord *rr);
337 #define DNS_RR_REPLACE(a, b) \
338 do { \
339 typeof(a)* _a = &(a); \
340 typeof(b) _b = (b); \
341 dns_resource_record_unref(*_a); \
342 *_a = _b; \
343 } while(0)
345 int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
346 int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name);
347 int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b);
348 int dns_resource_record_payload_equal(const DnsResourceRecord *a, const DnsResourceRecord *b);
350 const char* dns_resource_record_to_string(DnsResourceRecord *rr);
351 DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr);
352 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref);
354 int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical);
356 int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret);
357 int dns_resource_record_source(DnsResourceRecord *rr, const char **ret);
358 int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone);
359 int dns_resource_record_is_synthetic(DnsResourceRecord *rr);
361 int dns_resource_record_clamp_ttl(DnsResourceRecord **rr, uint32_t max_ttl);
363 bool dns_resource_record_is_link_local_address(DnsResourceRecord *rr);
365 int dns_resource_record_get_cname_target(DnsResourceKey *key, DnsResourceRecord *cname, char **ret);
367 DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i);
368 bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b);
369 DnsTxtItem *dns_txt_item_copy(DnsTxtItem *i);
370 int dns_txt_item_new_empty(DnsTxtItem **ret);
372 int dns_resource_record_new_from_raw(DnsResourceRecord **ret, const void *data, size_t size);
374 int dns_resource_key_to_json(DnsResourceKey *key, JsonVariant **ret);
375 int dns_resource_key_from_json(JsonVariant *v, DnsResourceKey **ret);
376 int dns_resource_record_to_json(DnsResourceRecord *rr, JsonVariant **ret);
378 void dns_resource_record_hash_func(const DnsResourceRecord *i, struct siphash *state);
379 int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResourceRecord *y);
381 extern const struct hash_ops dns_resource_key_hash_ops;
382 extern const struct hash_ops dns_resource_record_hash_ops;
384 int dnssec_algorithm_to_string_alloc(int i, char **ret);
385 int dnssec_algorithm_from_string(const char *s) _pure_;
387 int dnssec_digest_to_string_alloc(int i, char **ret);
388 int dnssec_digest_from_string(const char *s) _pure_;