travis: turn on nonnull-attribute on Fuzzit
[thirdparty/systemd.git] / src / resolve / resolved-dns-rr.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
74b2466e 2
0dae31d4
ZJS
3#include <math.h>
4
b5efdb8a 5#include "alloc-util.h"
4ad7f276 6#include "dns-domain.h"
7263f724 7#include "dns-type.h"
95052df3 8#include "escape.h"
e4e73a63 9#include "hexdecoct.h"
9bd2422a 10#include "memory-util.h"
fc8eec10 11#include "resolved-dns-dnssec.h"
07630cea 12#include "resolved-dns-packet.h"
e4e73a63 13#include "resolved-dns-rr.h"
8730bccf 14#include "string-table.h"
07630cea
LP
15#include "string-util.h"
16#include "strv.h"
d7671a3e 17#include "terminal-util.h"
74b2466e 18
faa133f3
LP
19DnsResourceKey* dns_resource_key_new(uint16_t class, uint16_t type, const char *name) {
20 DnsResourceKey *k;
21 size_t l;
74b2466e 22
faa133f3
LP
23 assert(name);
24
25 l = strlen(name);
26 k = malloc0(sizeof(DnsResourceKey) + l + 1);
27 if (!k)
28 return NULL;
29
30 k->n_ref = 1;
31 k->class = class;
32 k->type = type;
33
34 strcpy((char*) k + sizeof(DnsResourceKey), name);
35
36 return k;
37}
38
36d9205d 39DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const DnsResourceRecord *cname) {
58db254a
LP
40 int r;
41
36d9205d
TG
42 assert(key);
43 assert(cname);
44
58db254a
LP
45 assert(IN_SET(cname->key->type, DNS_TYPE_CNAME, DNS_TYPE_DNAME));
46
47 if (cname->key->type == DNS_TYPE_CNAME)
48 return dns_resource_key_new(key->class, key->type, cname->cname.name);
49 else {
50 DnsResourceKey *k;
51 char *destination = NULL;
52
1c02e7ba 53 r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
58db254a
LP
54 if (r < 0)
55 return NULL;
56 if (r == 0)
57 return dns_resource_key_ref((DnsResourceKey*) key);
58
59 k = dns_resource_key_new_consume(key->class, key->type, destination);
6b430fdb
ZJS
60 if (!k)
61 return mfree(destination);
58db254a
LP
62
63 return k;
64 }
36d9205d
TG
65}
66
801ad6a6
LP
67int dns_resource_key_new_append_suffix(DnsResourceKey **ret, DnsResourceKey *key, char *name) {
68 DnsResourceKey *new_key;
69 char *joined;
70 int r;
71
72 assert(ret);
73 assert(key);
74 assert(name);
75
dc477e73 76 if (dns_name_is_root(name)) {
801ad6a6
LP
77 *ret = dns_resource_key_ref(key);
78 return 0;
79 }
80
7470cc4c 81 r = dns_name_concat(dns_resource_key_name(key), name, 0, &joined);
801ad6a6
LP
82 if (r < 0)
83 return r;
84
85 new_key = dns_resource_key_new_consume(key->class, key->type, joined);
86 if (!new_key) {
87 free(joined);
88 return -ENOMEM;
89 }
90
91 *ret = new_key;
92 return 0;
93}
94
faa133f3
LP
95DnsResourceKey* dns_resource_key_new_consume(uint16_t class, uint16_t type, char *name) {
96 DnsResourceKey *k;
97
98 assert(name);
99
100 k = new0(DnsResourceKey, 1);
101 if (!k)
102 return NULL;
103
104 k->n_ref = 1;
105 k->class = class;
106 k->type = type;
107 k->_name = name;
108
109 return k;
110}
111
112DnsResourceKey* dns_resource_key_ref(DnsResourceKey *k) {
113
114 if (!k)
115 return NULL;
116
1b4f6e79
LP
117 /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
118 * set this to -1, they should not be reffed/unreffed */
119 assert(k->n_ref != (unsigned) -1);
120
faa133f3
LP
121 assert(k->n_ref > 0);
122 k->n_ref++;
123
124 return k;
125}
126
127DnsResourceKey* dns_resource_key_unref(DnsResourceKey *k) {
128 if (!k)
129 return NULL;
130
1b4f6e79 131 assert(k->n_ref != (unsigned) -1);
faa133f3
LP
132 assert(k->n_ref > 0);
133
134 if (k->n_ref == 1) {
135 free(k->_name);
136 free(k);
137 } else
138 k->n_ref--;
139
140 return NULL;
141}
142
1c02e7ba
ZJS
143const char* dns_resource_key_name(const DnsResourceKey *key) {
144 const char *name;
145
146 if (!key)
147 return NULL;
148
149 if (key->_name)
150 name = key->_name;
151 else
152 name = (char*) key + sizeof(DnsResourceKey);
153
154 if (dns_name_is_root(name))
155 return ".";
156 else
157 return name;
158}
159
28b9b764
LP
160bool dns_resource_key_is_address(const DnsResourceKey *key) {
161 assert(key);
162
163 /* Check if this is an A or AAAA resource key */
164
165 return key->class == DNS_CLASS_IN && IN_SET(key->type, DNS_TYPE_A, DNS_TYPE_AAAA);
166}
167
a2bf8a19
DR
168bool dns_resource_key_is_dnssd_ptr(const DnsResourceKey *key) {
169 assert(key);
170
171 /* Check if this is a PTR resource key used in
172 Service Instance Enumeration as described in RFC6763 p4.1. */
173
174 if (key->type != DNS_TYPE_PTR)
175 return false;
176
177 return dns_name_endswith(dns_resource_key_name(key), "_tcp.local") ||
178 dns_name_endswith(dns_resource_key_name(key), "_udp.local");
179}
180
faa133f3
LP
181int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b) {
182 int r;
183
4d247a6c
LP
184 if (a == b)
185 return 1;
186
1c02e7ba 187 r = dns_name_equal(dns_resource_key_name(a), dns_resource_key_name(b));
faa133f3
LP
188 if (r <= 0)
189 return r;
190
191 if (a->class != b->class)
192 return 0;
193
194 if (a->type != b->type)
195 return 0;
196
197 return 1;
198}
199
105e1512 200int dns_resource_key_match_rr(const DnsResourceKey *key, DnsResourceRecord *rr, const char *search_domain) {
801ad6a6
LP
201 int r;
202
faa133f3
LP
203 assert(key);
204 assert(rr);
205
4d247a6c
LP
206 if (key == rr->key)
207 return 1;
208
801ad6a6
LP
209 /* Checks if an rr matches the specified key. If a search
210 * domain is specified, it will also be checked if the key
211 * with the search domain suffixed might match the RR. */
212
faa133f3
LP
213 if (rr->key->class != key->class && key->class != DNS_CLASS_ANY)
214 return 0;
215
216 if (rr->key->type != key->type && key->type != DNS_TYPE_ANY)
217 return 0;
218
1c02e7ba 219 r = dns_name_equal(dns_resource_key_name(rr->key), dns_resource_key_name(key));
801ad6a6
LP
220 if (r != 0)
221 return r;
222
223 if (search_domain) {
224 _cleanup_free_ char *joined = NULL;
225
7470cc4c 226 r = dns_name_concat(dns_resource_key_name(key), search_domain, 0, &joined);
801ad6a6
LP
227 if (r < 0)
228 return r;
229
1c02e7ba 230 return dns_name_equal(dns_resource_key_name(rr->key), joined);
801ad6a6
LP
231 }
232
233 return 0;
faa133f3
LP
234}
235
5d27351f 236int dns_resource_key_match_cname_or_dname(const DnsResourceKey *key, const DnsResourceKey *cname, const char *search_domain) {
801ad6a6
LP
237 int r;
238
faa133f3 239 assert(key);
5d27351f 240 assert(cname);
faa133f3 241
5d27351f 242 if (cname->class != key->class && key->class != DNS_CLASS_ANY)
faa133f3
LP
243 return 0;
244
5d27351f 245 if (cname->type == DNS_TYPE_CNAME)
1c02e7ba 246 r = dns_name_equal(dns_resource_key_name(key), dns_resource_key_name(cname));
5d27351f 247 else if (cname->type == DNS_TYPE_DNAME)
1c02e7ba 248 r = dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(cname));
58db254a 249 else
faa133f3 250 return 0;
801ad6a6
LP
251
252 if (r != 0)
253 return r;
254
255 if (search_domain) {
256 _cleanup_free_ char *joined = NULL;
257
7470cc4c 258 r = dns_name_concat(dns_resource_key_name(key), search_domain, 0, &joined);
801ad6a6
LP
259 if (r < 0)
260 return r;
261
5d27351f 262 if (cname->type == DNS_TYPE_CNAME)
1c02e7ba 263 return dns_name_equal(joined, dns_resource_key_name(cname));
5d27351f 264 else if (cname->type == DNS_TYPE_DNAME)
1c02e7ba 265 return dns_name_endswith(joined, dns_resource_key_name(cname));
801ad6a6
LP
266 }
267
268 return 0;
547973de
LP
269}
270
271int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *soa) {
272 assert(soa);
273 assert(key);
274
275 /* Checks whether 'soa' is a SOA record for the specified key. */
276
65b200e7 277 if (soa->class != key->class)
547973de 278 return 0;
801ad6a6 279
547973de
LP
280 if (soa->type != DNS_TYPE_SOA)
281 return 0;
282
1c02e7ba 283 return dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(soa));
74b2466e
LP
284}
285
7a08d314 286static void dns_resource_key_hash_func(const DnsResourceKey *k, struct siphash *state) {
b826ab58 287 assert(k);
322345fd 288
1c02e7ba 289 dns_name_hash_func(dns_resource_key_name(k), state);
b826ab58
TG
290 siphash24_compress(&k->class, sizeof(k->class), state);
291 siphash24_compress(&k->type, sizeof(k->type), state);
322345fd
LP
292}
293
7a08d314 294static int dns_resource_key_compare_func(const DnsResourceKey *x, const DnsResourceKey *y) {
322345fd
LP
295 int ret;
296
1c02e7ba 297 ret = dns_name_compare_func(dns_resource_key_name(x), dns_resource_key_name(y));
322345fd
LP
298 if (ret != 0)
299 return ret;
300
a0edd02e
FB
301 ret = CMP(x->type, y->type);
302 if (ret != 0)
303 return ret;
322345fd 304
a0edd02e
FB
305 ret = CMP(x->class, y->class);
306 if (ret != 0)
307 return ret;
322345fd
LP
308
309 return 0;
310}
311
7a08d314 312DEFINE_HASH_OPS(dns_resource_key_hash_ops, DnsResourceKey, dns_resource_key_hash_func, dns_resource_key_compare_func);
d5099efc 313
202b76ae 314char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size) {
1c02e7ba 315 const char *c, *t;
202b76ae 316 char *ans = buf;
2d4c5cbc 317
6af47493
LP
318 /* If we cannot convert the CLASS/TYPE into a known string,
319 use the format recommended by RFC 3597, Section 5. */
320
2d4c5cbc 321 c = dns_class_to_string(key->class);
2d4c5cbc 322 t = dns_type_to_string(key->type);
2d4c5cbc 323
202b76ae
ZJS
324 snprintf(buf, buf_size, "%s %s%s%.0u %s%s%.0u",
325 dns_resource_key_name(key),
87e4e28d 326 strempty(c), c ? "" : "CLASS", c ? 0 : key->class,
d149a404 327 strempty(t), t ? "" : "TYPE", t ? 0 : key->type);
2d4c5cbc 328
202b76ae 329 return ans;
2d4c5cbc
LP
330}
331
f57e3cd5
LP
332bool dns_resource_key_reduce(DnsResourceKey **a, DnsResourceKey **b) {
333 assert(a);
334 assert(b);
335
336 /* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
337 * this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
338 * from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
339 * superficial data. */
340
341 if (!*a)
342 return false;
343 if (!*b)
344 return false;
345
346 /* We refuse merging const keys */
347 if ((*a)->n_ref == (unsigned) -1)
348 return false;
349 if ((*b)->n_ref == (unsigned) -1)
350 return false;
351
352 /* Already the same? */
353 if (*a == *b)
354 return true;
355
356 /* Are they really identical? */
357 if (dns_resource_key_equal(*a, *b) <= 0)
358 return false;
359
360 /* Keep the one which already has more references. */
361 if ((*a)->n_ref > (*b)->n_ref) {
362 dns_resource_key_unref(*b);
363 *b = dns_resource_key_ref(*a);
364 } else {
365 dns_resource_key_unref(*a);
366 *a = dns_resource_key_ref(*b);
367 }
368
369 return true;
370}
371
faa133f3 372DnsResourceRecord* dns_resource_record_new(DnsResourceKey *key) {
74b2466e
LP
373 DnsResourceRecord *rr;
374
375 rr = new0(DnsResourceRecord, 1);
376 if (!rr)
377 return NULL;
378
379 rr->n_ref = 1;
faa133f3 380 rr->key = dns_resource_key_ref(key);
ee3d6aff 381 rr->expiry = USEC_INFINITY;
97c67192 382 rr->n_skip_labels_signer = rr->n_skip_labels_source = (unsigned) -1;
faa133f3 383
74b2466e
LP
384 return rr;
385}
386
8bf52d3d
LP
387DnsResourceRecord* dns_resource_record_new_full(uint16_t class, uint16_t type, const char *name) {
388 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
389
390 key = dns_resource_key_new(class, type, name);
391 if (!key)
392 return NULL;
393
394 return dns_resource_record_new(key);
395}
396
8301aa0b
YW
397static DnsResourceRecord* dns_resource_record_free(DnsResourceRecord *rr) {
398 assert(rr);
74b2466e 399
faa133f3 400 if (rr->key) {
9de3e329 401 switch(rr->key->type) {
9c92ce6d
LP
402
403 case DNS_TYPE_SRV:
404 free(rr->srv.name);
405 break;
406
9de3e329
ZJS
407 case DNS_TYPE_PTR:
408 case DNS_TYPE_NS:
409 case DNS_TYPE_CNAME:
8ac4e9e1 410 case DNS_TYPE_DNAME:
faa133f3 411 free(rr->ptr.name);
9de3e329 412 break;
9c92ce6d 413
9de3e329 414 case DNS_TYPE_HINFO:
faa133f3
LP
415 free(rr->hinfo.cpu);
416 free(rr->hinfo.os);
9de3e329 417 break;
9c92ce6d 418
9de3e329 419 case DNS_TYPE_TXT:
9c92ce6d 420 case DNS_TYPE_SPF:
2001c805 421 dns_txt_item_free_all(rr->txt.items);
9de3e329 422 break;
9c92ce6d 423
9de3e329 424 case DNS_TYPE_SOA:
7e8e0422
LP
425 free(rr->soa.mname);
426 free(rr->soa.rname);
9de3e329 427 break;
9c92ce6d 428
9de3e329 429 case DNS_TYPE_MX:
946c7094 430 free(rr->mx.exchange);
9de3e329 431 break;
9c92ce6d 432
abf126a3
TG
433 case DNS_TYPE_DS:
434 free(rr->ds.digest);
435 break;
436
42cc2eeb 437 case DNS_TYPE_SSHFP:
549c1a25 438 free(rr->sshfp.fingerprint);
42cc2eeb
LP
439 break;
440
8db0d2f5
ZJS
441 case DNS_TYPE_DNSKEY:
442 free(rr->dnskey.key);
443 break;
444
151226ab
ZJS
445 case DNS_TYPE_RRSIG:
446 free(rr->rrsig.signer);
447 free(rr->rrsig.signature);
448 break;
449
50f1e641
TG
450 case DNS_TYPE_NSEC:
451 free(rr->nsec.next_domain_name);
452 bitmap_free(rr->nsec.types);
453 break;
454
5d45a880
TG
455 case DNS_TYPE_NSEC3:
456 free(rr->nsec3.next_hashed_name);
457 free(rr->nsec3.salt);
458 bitmap_free(rr->nsec3.types);
459 break;
460
0dae31d4 461 case DNS_TYPE_LOC:
9de3e329
ZJS
462 case DNS_TYPE_A:
463 case DNS_TYPE_AAAA:
464 break;
9c92ce6d 465
48d45d2b
ZJS
466 case DNS_TYPE_TLSA:
467 free(rr->tlsa.data);
468 break;
469
95052df3
ZJS
470 case DNS_TYPE_CAA:
471 free(rr->caa.tag);
472 free(rr->caa.value);
473 break;
474
d93a16b8 475 case DNS_TYPE_OPENPGPKEY:
9de3e329 476 default:
bfc1d734
ZJS
477 if (!rr->unparseable)
478 free(rr->generic.data);
9de3e329 479 }
322345fd 480
bfc1d734
ZJS
481 if (rr->unparseable)
482 free(rr->generic.data);
483
a8812dd7 484 free(rr->wire_format);
faa133f3
LP
485 dns_resource_key_unref(rr->key);
486 }
322345fd 487
7b50eb2e 488 free(rr->to_string);
6b430fdb 489 return mfree(rr);
322345fd
LP
490}
491
8301aa0b
YW
492DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsResourceRecord, dns_resource_record, dns_resource_record_free);
493
623a4c97
LP
494int dns_resource_record_new_reverse(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *hostname) {
495 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
496 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
497 _cleanup_free_ char *ptr = NULL;
498 int r;
499
500 assert(ret);
501 assert(address);
502 assert(hostname);
503
504 r = dns_name_reverse(family, address, &ptr);
505 if (r < 0)
506 return r;
507
508 key = dns_resource_key_new_consume(DNS_CLASS_IN, DNS_TYPE_PTR, ptr);
509 if (!key)
510 return -ENOMEM;
511
512 ptr = NULL;
513
514 rr = dns_resource_record_new(key);
515 if (!rr)
516 return -ENOMEM;
517
518 rr->ptr.name = strdup(hostname);
519 if (!rr->ptr.name)
520 return -ENOMEM;
521
1cc6c93a 522 *ret = TAKE_PTR(rr);
623a4c97
LP
523
524 return 0;
525}
526
78c6a153
LP
527int dns_resource_record_new_address(DnsResourceRecord **ret, int family, const union in_addr_union *address, const char *name) {
528 DnsResourceRecord *rr;
529
530 assert(ret);
531 assert(address);
532 assert(family);
533
534 if (family == AF_INET) {
535
536 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, name);
537 if (!rr)
538 return -ENOMEM;
539
540 rr->a.in_addr = address->in;
541
542 } else if (family == AF_INET6) {
543
544 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_AAAA, name);
545 if (!rr)
546 return -ENOMEM;
547
548 rr->aaaa.in6_addr = address->in6;
549 } else
550 return -EAFNOSUPPORT;
551
552 *ret = rr;
553
554 return 0;
555}
556
a43a068a
ZJS
557#define FIELD_EQUAL(a, b, field) \
558 ((a).field ## _size == (b).field ## _size && \
9bd2422a 559 memcmp_safe((a).field, (b).field, (a).field ## _size) == 0)
a43a068a 560
51969a58 561int dns_resource_record_payload_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
322345fd
LP
562 int r;
563
51969a58 564 /* Check if a and b are the same, but don't look at their keys */
322345fd 565
fd0b4602
LP
566 if (a->unparseable != b->unparseable)
567 return 0;
568
569 switch (a->unparseable ? _DNS_TYPE_INVALID : a->key->type) {
2d4c5cbc 570
9c92ce6d
LP
571 case DNS_TYPE_SRV:
572 r = dns_name_equal(a->srv.name, b->srv.name);
573 if (r <= 0)
574 return r;
575
576 return a->srv.priority == b->srv.priority &&
577 a->srv.weight == b->srv.weight &&
578 a->srv.port == b->srv.port;
579
2d4c5cbc
LP
580 case DNS_TYPE_PTR:
581 case DNS_TYPE_NS:
582 case DNS_TYPE_CNAME:
8ac4e9e1 583 case DNS_TYPE_DNAME:
322345fd 584 return dns_name_equal(a->ptr.name, b->ptr.name);
2d4c5cbc
LP
585
586 case DNS_TYPE_HINFO:
587 return strcaseeq(a->hinfo.cpu, b->hinfo.cpu) &&
588 strcaseeq(a->hinfo.os, b->hinfo.os);
589
9de3e329 590 case DNS_TYPE_SPF: /* exactly the same as TXT */
0f84a72e 591 case DNS_TYPE_TXT:
2001c805 592 return dns_txt_item_equal(a->txt.items, b->txt.items);
2e276efc 593
2d4c5cbc 594 case DNS_TYPE_A:
322345fd 595 return memcmp(&a->a.in_addr, &b->a.in_addr, sizeof(struct in_addr)) == 0;
2d4c5cbc
LP
596
597 case DNS_TYPE_AAAA:
322345fd 598 return memcmp(&a->aaaa.in6_addr, &b->aaaa.in6_addr, sizeof(struct in6_addr)) == 0;
2d4c5cbc
LP
599
600 case DNS_TYPE_SOA:
7e8e0422
LP
601 r = dns_name_equal(a->soa.mname, b->soa.mname);
602 if (r <= 0)
603 return r;
604 r = dns_name_equal(a->soa.rname, b->soa.rname);
605 if (r <= 0)
606 return r;
607
608 return a->soa.serial == b->soa.serial &&
609 a->soa.refresh == b->soa.refresh &&
610 a->soa.retry == b->soa.retry &&
611 a->soa.expire == b->soa.expire &&
612 a->soa.minimum == b->soa.minimum;
9c92ce6d 613
946c7094
ZJS
614 case DNS_TYPE_MX:
615 if (a->mx.priority != b->mx.priority)
616 return 0;
617
618 return dns_name_equal(a->mx.exchange, b->mx.exchange);
619
0dae31d4
ZJS
620 case DNS_TYPE_LOC:
621 assert(a->loc.version == b->loc.version);
622
623 return a->loc.size == b->loc.size &&
624 a->loc.horiz_pre == b->loc.horiz_pre &&
625 a->loc.vert_pre == b->loc.vert_pre &&
626 a->loc.latitude == b->loc.latitude &&
627 a->loc.longitude == b->loc.longitude &&
628 a->loc.altitude == b->loc.altitude;
629
abf126a3
TG
630 case DNS_TYPE_DS:
631 return a->ds.key_tag == b->ds.key_tag &&
632 a->ds.algorithm == b->ds.algorithm &&
633 a->ds.digest_type == b->ds.digest_type &&
a43a068a 634 FIELD_EQUAL(a->ds, b->ds, digest);
abf126a3 635
42cc2eeb
LP
636 case DNS_TYPE_SSHFP:
637 return a->sshfp.algorithm == b->sshfp.algorithm &&
638 a->sshfp.fptype == b->sshfp.fptype &&
a43a068a 639 FIELD_EQUAL(a->sshfp, b->sshfp, fingerprint);
42cc2eeb 640
8db0d2f5 641 case DNS_TYPE_DNSKEY:
f91dc240
LP
642 return a->dnskey.flags == b->dnskey.flags &&
643 a->dnskey.protocol == b->dnskey.protocol &&
8db0d2f5 644 a->dnskey.algorithm == b->dnskey.algorithm &&
a43a068a 645 FIELD_EQUAL(a->dnskey, b->dnskey, key);
8db0d2f5 646
151226ab
ZJS
647 case DNS_TYPE_RRSIG:
648 /* do the fast comparisons first */
a43a068a
ZJS
649 return a->rrsig.type_covered == b->rrsig.type_covered &&
650 a->rrsig.algorithm == b->rrsig.algorithm &&
651 a->rrsig.labels == b->rrsig.labels &&
652 a->rrsig.original_ttl == b->rrsig.original_ttl &&
653 a->rrsig.expiration == b->rrsig.expiration &&
654 a->rrsig.inception == b->rrsig.inception &&
655 a->rrsig.key_tag == b->rrsig.key_tag &&
656 FIELD_EQUAL(a->rrsig, b->rrsig, signature) &&
657 dns_name_equal(a->rrsig.signer, b->rrsig.signer);
151226ab 658
50f1e641
TG
659 case DNS_TYPE_NSEC:
660 return dns_name_equal(a->nsec.next_domain_name, b->nsec.next_domain_name) &&
661 bitmap_equal(a->nsec.types, b->nsec.types);
662
5d45a880
TG
663 case DNS_TYPE_NSEC3:
664 return a->nsec3.algorithm == b->nsec3.algorithm &&
a43a068a
ZJS
665 a->nsec3.flags == b->nsec3.flags &&
666 a->nsec3.iterations == b->nsec3.iterations &&
667 FIELD_EQUAL(a->nsec3, b->nsec3, salt) &&
668 FIELD_EQUAL(a->nsec3, b->nsec3, next_hashed_name) &&
669 bitmap_equal(a->nsec3.types, b->nsec3.types);
5d45a880 670
48d45d2b
ZJS
671 case DNS_TYPE_TLSA:
672 return a->tlsa.cert_usage == b->tlsa.cert_usage &&
673 a->tlsa.selector == b->tlsa.selector &&
674 a->tlsa.matching_type == b->tlsa.matching_type &&
a43a068a 675 FIELD_EQUAL(a->tlsa, b->tlsa, data);
48d45d2b 676
95052df3
ZJS
677 case DNS_TYPE_CAA:
678 return a->caa.flags == b->caa.flags &&
679 streq(a->caa.tag, b->caa.tag) &&
680 FIELD_EQUAL(a->caa, b->caa, value);
681
682 case DNS_TYPE_OPENPGPKEY:
2d4c5cbc 683 default:
a43a068a 684 return FIELD_EQUAL(a->generic, b->generic, data);
2d4c5cbc 685 }
322345fd
LP
686}
687
51969a58
ZJS
688int dns_resource_record_equal(const DnsResourceRecord *a, const DnsResourceRecord *b) {
689 int r;
690
691 assert(a);
692 assert(b);
693
694 if (a == b)
695 return 1;
696
697 r = dns_resource_key_equal(a->key, b->key);
698 if (r <= 0)
699 return r;
700
701 return dns_resource_record_payload_equal(a, b);
702}
703
0dae31d4
ZJS
704static char* format_location(uint32_t latitude, uint32_t longitude, uint32_t altitude,
705 uint8_t size, uint8_t horiz_pre, uint8_t vert_pre) {
706 char *s;
707 char NS = latitude >= 1U<<31 ? 'N' : 'S';
708 char EW = longitude >= 1U<<31 ? 'E' : 'W';
709
710 int lat = latitude >= 1U<<31 ? (int) (latitude - (1U<<31)) : (int) ((1U<<31) - latitude);
711 int lon = longitude >= 1U<<31 ? (int) (longitude - (1U<<31)) : (int) ((1U<<31) - longitude);
712 double alt = altitude >= 10000000u ? altitude - 10000000u : -(double)(10000000u - altitude);
713 double siz = (size >> 4) * exp10((double) (size & 0xF));
714 double hor = (horiz_pre >> 4) * exp10((double) (horiz_pre & 0xF));
715 double ver = (vert_pre >> 4) * exp10((double) (vert_pre & 0xF));
716
717 if (asprintf(&s, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
718 (lat / 60000 / 60),
719 (lat / 60000) % 60,
720 (lat % 60000) / 1000.,
721 NS,
722 (lon / 60000 / 60),
723 (lon / 60000) % 60,
724 (lon % 60000) / 1000.,
725 EW,
726 alt / 100.,
727 siz / 100.,
728 hor / 100.,
729 ver / 100.) < 0)
730 return NULL;
731
732 return s;
733}
734
7c6423e1
TG
735static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
736 struct tm tm;
737
738 assert(buf);
fbd0b64f 739 assert(l > STRLEN("YYYYMMDDHHmmSS"));
7c6423e1
TG
740
741 if (!gmtime_r(&sec, &tm))
742 return -EINVAL;
743
744 if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
745 return -EINVAL;
746
747 return 0;
748}
749
50f1e641
TG
750static char *format_types(Bitmap *types) {
751 _cleanup_strv_free_ char **strv = NULL;
752 _cleanup_free_ char *str = NULL;
cb57dd41 753 Iterator i;
50f1e641
TG
754 unsigned type;
755 int r;
756
cb57dd41 757 BITMAP_FOREACH(type, types, i) {
50f1e641 758 if (dns_type_to_string(type)) {
2c1fb4f7 759 r = strv_extend(&strv, dns_type_to_string(type));
50f1e641
TG
760 if (r < 0)
761 return NULL;
762 } else {
763 char *t;
764
765 r = asprintf(&t, "TYPE%u", type);
766 if (r < 0)
767 return NULL;
768
2c1fb4f7 769 r = strv_consume(&strv, t);
50f1e641
TG
770 if (r < 0)
771 return NULL;
772 }
773 }
774
775 str = strv_join(strv, " ");
776 if (!str)
777 return NULL;
778
605405c6 779 return strjoin("( ", str, " )");
50f1e641
TG
780}
781
2001c805
LP
782static char *format_txt(DnsTxtItem *first) {
783 DnsTxtItem *i;
784 size_t c = 1;
785 char *p, *s;
786
787 LIST_FOREACH(items, i, first)
788 c += i->length * 4 + 3;
789
790 p = s = new(char, c);
791 if (!s)
792 return NULL;
793
794 LIST_FOREACH(items, i, first) {
795 size_t j;
796
797 if (i != first)
798 *(p++) = ' ';
799
800 *(p++) = '"';
801
802 for (j = 0; j < i->length; j++) {
803 if (i->data[j] < ' ' || i->data[j] == '"' || i->data[j] >= 127) {
804 *(p++) = '\\';
805 *(p++) = '0' + (i->data[j] / 100);
806 *(p++) = '0' + ((i->data[j] / 10) % 10);
807 *(p++) = '0' + (i->data[j] % 10);
808 } else
809 *(p++) = i->data[j];
810 }
811
812 *(p++) = '"';
813 }
814
815 *p = 0;
816 return s;
817}
818
7b50eb2e 819const char *dns_resource_record_to_string(DnsResourceRecord *rr) {
202b76ae
ZJS
820 _cleanup_free_ char *t = NULL;
821 char *s, k[DNS_RESOURCE_KEY_STRING_MAX];
2d4c5cbc 822 int r;
322345fd 823
2d4c5cbc 824 assert(rr);
322345fd 825
7b50eb2e
LP
826 if (rr->to_string)
827 return rr->to_string;
828
202b76ae 829 dns_resource_key_to_string(rr->key, k, sizeof(k));
322345fd 830
0dae31d4 831 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
322345fd 832
9c92ce6d
LP
833 case DNS_TYPE_SRV:
834 r = asprintf(&s, "%s %u %u %u %s",
835 k,
836 rr->srv.priority,
837 rr->srv.weight,
838 rr->srv.port,
839 strna(rr->srv.name));
840 if (r < 0)
7b50eb2e 841 return NULL;
9c92ce6d
LP
842 break;
843
2d4c5cbc
LP
844 case DNS_TYPE_PTR:
845 case DNS_TYPE_NS:
846 case DNS_TYPE_CNAME:
8ac4e9e1 847 case DNS_TYPE_DNAME:
605405c6 848 s = strjoin(k, " ", rr->ptr.name);
2d4c5cbc 849 if (!s)
7b50eb2e 850 return NULL;
322345fd 851
2d4c5cbc 852 break;
322345fd 853
2d4c5cbc 854 case DNS_TYPE_HINFO:
605405c6 855 s = strjoin(k, " ", rr->hinfo.cpu, " ", rr->hinfo.os);
2d4c5cbc 856 if (!s)
7b50eb2e 857 return NULL;
2d4c5cbc 858 break;
322345fd 859
9de3e329 860 case DNS_TYPE_SPF: /* exactly the same as TXT */
8db0d2f5 861 case DNS_TYPE_TXT:
2001c805 862 t = format_txt(rr->txt.items);
2e276efc 863 if (!t)
7b50eb2e 864 return NULL;
2e276efc 865
605405c6 866 s = strjoin(k, " ", t);
2e276efc 867 if (!s)
7b50eb2e 868 return NULL;
2e276efc 869 break;
2e276efc 870
2d4c5cbc
LP
871 case DNS_TYPE_A: {
872 _cleanup_free_ char *x = NULL;
322345fd 873
2d4c5cbc
LP
874 r = in_addr_to_string(AF_INET, (const union in_addr_union*) &rr->a.in_addr, &x);
875 if (r < 0)
7b50eb2e 876 return NULL;
322345fd 877
605405c6 878 s = strjoin(k, " ", x);
2d4c5cbc 879 if (!s)
7b50eb2e 880 return NULL;
2d4c5cbc
LP
881 break;
882 }
322345fd 883
8db0d2f5
ZJS
884 case DNS_TYPE_AAAA:
885 r = in_addr_to_string(AF_INET6, (const union in_addr_union*) &rr->aaaa.in6_addr, &t);
2d4c5cbc 886 if (r < 0)
7b50eb2e 887 return NULL;
322345fd 888
605405c6 889 s = strjoin(k, " ", t);
2d4c5cbc 890 if (!s)
7b50eb2e 891 return NULL;
2d4c5cbc 892 break;
322345fd 893
2d4c5cbc
LP
894 case DNS_TYPE_SOA:
895 r = asprintf(&s, "%s %s %s %u %u %u %u %u",
896 k,
897 strna(rr->soa.mname),
898 strna(rr->soa.rname),
899 rr->soa.serial,
900 rr->soa.refresh,
901 rr->soa.retry,
902 rr->soa.expire,
903 rr->soa.minimum);
904 if (r < 0)
7b50eb2e 905 return NULL;
2d4c5cbc
LP
906 break;
907
946c7094
ZJS
908 case DNS_TYPE_MX:
909 r = asprintf(&s, "%s %u %s",
910 k,
911 rr->mx.priority,
912 rr->mx.exchange);
913 if (r < 0)
7b50eb2e 914 return NULL;
946c7094
ZJS
915 break;
916
8db0d2f5 917 case DNS_TYPE_LOC:
0dae31d4
ZJS
918 assert(rr->loc.version == 0);
919
8db0d2f5
ZJS
920 t = format_location(rr->loc.latitude,
921 rr->loc.longitude,
922 rr->loc.altitude,
923 rr->loc.size,
924 rr->loc.horiz_pre,
925 rr->loc.vert_pre);
926 if (!t)
7b50eb2e 927 return NULL;
0dae31d4 928
605405c6 929 s = strjoin(k, " ", t);
0dae31d4 930 if (!s)
7b50eb2e 931 return NULL;
0dae31d4 932 break;
0dae31d4 933
abf126a3
TG
934 case DNS_TYPE_DS:
935 t = hexmem(rr->ds.digest, rr->ds.digest_size);
936 if (!t)
7b50eb2e 937 return NULL;
abf126a3
TG
938
939 r = asprintf(&s, "%s %u %u %u %s",
940 k,
941 rr->ds.key_tag,
942 rr->ds.algorithm,
943 rr->ds.digest_type,
944 t);
945 if (r < 0)
7b50eb2e 946 return NULL;
abf126a3
TG
947 break;
948
8db0d2f5 949 case DNS_TYPE_SSHFP:
549c1a25 950 t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
8db0d2f5 951 if (!t)
7b50eb2e 952 return NULL;
42cc2eeb
LP
953
954 r = asprintf(&s, "%s %u %u %s",
955 k,
956 rr->sshfp.algorithm,
957 rr->sshfp.fptype,
8db0d2f5 958 t);
42cc2eeb 959 if (r < 0)
7b50eb2e 960 return NULL;
42cc2eeb 961 break;
42cc2eeb 962
ff3d6560 963 case DNS_TYPE_DNSKEY: {
8e54f5d9 964 _cleanup_free_ char *alg = NULL;
99e5ca6d 965 char *ss;
fc8eec10
ZJS
966 uint16_t key_tag;
967
968 key_tag = dnssec_keytag(rr, true);
ff3d6560 969
8e54f5d9
LP
970 r = dnssec_algorithm_to_string_alloc(rr->dnskey.algorithm, &alg);
971 if (r < 0)
972 return NULL;
ff3d6560 973
4bb91a95 974 r = asprintf(&s, "%s %u %u %s",
8db0d2f5 975 k,
f91dc240
LP
976 rr->dnskey.flags,
977 rr->dnskey.protocol,
4bb91a95 978 alg);
8db0d2f5 979 if (r < 0)
7b50eb2e 980 return NULL;
d7671a3e 981
4bb91a95 982 r = base64_append(&s, r,
d7671a3e
ZJS
983 rr->dnskey.key, rr->dnskey.key_size,
984 8, columns());
985 if (r < 0)
986 return NULL;
987
99e5ca6d 988 r = asprintf(&ss, "%s\n"
718af59e
ZJS
989 " -- Flags:%s%s%s\n"
990 " -- Key tag: %u",
99e5ca6d 991 s,
99e5ca6d
ZJS
992 rr->dnskey.flags & DNSKEY_FLAG_SEP ? " SEP" : "",
993 rr->dnskey.flags & DNSKEY_FLAG_REVOKE ? " REVOKE" : "",
fc8eec10 994 rr->dnskey.flags & DNSKEY_FLAG_ZONE_KEY ? " ZONE_KEY" : "",
fc8eec10 995 key_tag);
99e5ca6d
ZJS
996 if (r < 0)
997 return NULL;
998 free(s);
999 s = ss;
1000
8db0d2f5 1001 break;
ff3d6560 1002 }
2d4c5cbc 1003
151226ab 1004 case DNS_TYPE_RRSIG: {
8e54f5d9 1005 _cleanup_free_ char *alg = NULL;
fbd0b64f 1006 char expiration[STRLEN("YYYYMMDDHHmmSS") + 1], inception[STRLEN("YYYYMMDDHHmmSS") + 1];
8e54f5d9 1007 const char *type;
151226ab
ZJS
1008
1009 type = dns_type_to_string(rr->rrsig.type_covered);
8e54f5d9
LP
1010
1011 r = dnssec_algorithm_to_string_alloc(rr->rrsig.algorithm, &alg);
1012 if (r < 0)
1013 return NULL;
151226ab 1014
7c6423e1
TG
1015 r = format_timestamp_dns(expiration, sizeof(expiration), rr->rrsig.expiration);
1016 if (r < 0)
7b50eb2e 1017 return NULL;
7c6423e1
TG
1018
1019 r = format_timestamp_dns(inception, sizeof(inception), rr->rrsig.inception);
1020 if (r < 0)
7b50eb2e 1021 return NULL;
7c6423e1 1022
151226ab
ZJS
1023 /* TYPE?? follows
1024 * http://tools.ietf.org/html/rfc3597#section-5 */
1025
4bb91a95 1026 r = asprintf(&s, "%s %s%.*u %s %u %u %s %s %u %s",
151226ab
ZJS
1027 k,
1028 type ?: "TYPE",
1029 type ? 0 : 1, type ? 0u : (unsigned) rr->rrsig.type_covered,
8e54f5d9 1030 alg,
151226ab
ZJS
1031 rr->rrsig.labels,
1032 rr->rrsig.original_ttl,
7c6423e1
TG
1033 expiration,
1034 inception,
151226ab 1035 rr->rrsig.key_tag,
4bb91a95 1036 rr->rrsig.signer);
151226ab 1037 if (r < 0)
7b50eb2e 1038 return NULL;
d7671a3e 1039
4bb91a95 1040 r = base64_append(&s, r,
d7671a3e
ZJS
1041 rr->rrsig.signature, rr->rrsig.signature_size,
1042 8, columns());
1043 if (r < 0)
1044 return NULL;
1045
151226ab
ZJS
1046 break;
1047 }
1048
50f1e641
TG
1049 case DNS_TYPE_NSEC:
1050 t = format_types(rr->nsec.types);
1051 if (!t)
7b50eb2e 1052 return NULL;
50f1e641
TG
1053
1054 r = asprintf(&s, "%s %s %s",
1055 k,
1056 rr->nsec.next_domain_name,
1057 t);
1058 if (r < 0)
7b50eb2e 1059 return NULL;
50f1e641
TG
1060 break;
1061
5d45a880
TG
1062 case DNS_TYPE_NSEC3: {
1063 _cleanup_free_ char *salt = NULL, *hash = NULL;
1064
f5430a3e 1065 if (rr->nsec3.salt_size > 0) {
5d45a880
TG
1066 salt = hexmem(rr->nsec3.salt, rr->nsec3.salt_size);
1067 if (!salt)
7b50eb2e 1068 return NULL;
5d45a880
TG
1069 }
1070
1071 hash = base32hexmem(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, false);
1072 if (!hash)
7b50eb2e 1073 return NULL;
5d45a880
TG
1074
1075 t = format_types(rr->nsec3.types);
1076 if (!t)
7b50eb2e 1077 return NULL;
5d45a880
TG
1078
1079 r = asprintf(&s, "%s %"PRIu8" %"PRIu8" %"PRIu16" %s %s %s",
1080 k,
1081 rr->nsec3.algorithm,
1082 rr->nsec3.flags,
1083 rr->nsec3.iterations,
f5430a3e 1084 rr->nsec3.salt_size > 0 ? salt : "-",
5d45a880
TG
1085 hash,
1086 t);
1087 if (r < 0)
7b50eb2e 1088 return NULL;
5d45a880
TG
1089
1090 break;
1091 }
1092
48d45d2b 1093 case DNS_TYPE_TLSA: {
cfb90da3 1094 const char *cert_usage, *selector, *matching_type;
48d45d2b 1095
cfb90da3
ZJS
1096 cert_usage = tlsa_cert_usage_to_string(rr->tlsa.cert_usage);
1097 selector = tlsa_selector_to_string(rr->tlsa.selector);
1098 matching_type = tlsa_matching_type_to_string(rr->tlsa.matching_type);
1099
236d312b
ZJS
1100 t = hexmem(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
1101 if (!t)
48d45d2b 1102 return NULL;
cfb90da3 1103
236d312b
ZJS
1104 r = asprintf(&s,
1105 "%s %u %u %u %s\n"
718af59e
ZJS
1106 " -- Cert. usage: %s\n"
1107 " -- Selector: %s\n"
1108 " -- Matching type: %s",
236d312b
ZJS
1109 k,
1110 rr->tlsa.cert_usage,
1111 rr->tlsa.selector,
1112 rr->tlsa.matching_type,
1113 t,
718af59e
ZJS
1114 cert_usage,
1115 selector,
1116 matching_type);
cfb90da3
ZJS
1117 if (r < 0)
1118 return NULL;
cfb90da3 1119
48d45d2b
ZJS
1120 break;
1121 }
1122
95052df3
ZJS
1123 case DNS_TYPE_CAA: {
1124 _cleanup_free_ char *value;
1125
1126 value = octescape(rr->caa.value, rr->caa.value_size);
1127 if (!value)
1128 return NULL;
1129
718af59e 1130 r = asprintf(&s, "%s %u %s \"%s\"%s%s%s%.0u",
95052df3
ZJS
1131 k,
1132 rr->caa.flags,
1133 rr->caa.tag,
718af59e
ZJS
1134 value,
1135 rr->caa.flags ? "\n -- Flags:" : "",
1136 rr->caa.flags & CAA_FLAG_CRITICAL ? " critical" : "",
1137 rr->caa.flags & ~CAA_FLAG_CRITICAL ? " " : "",
1138 rr->caa.flags & ~CAA_FLAG_CRITICAL);
95052df3
ZJS
1139 if (r < 0)
1140 return NULL;
1141
1142 break;
1143 }
1144
d93a16b8 1145 case DNS_TYPE_OPENPGPKEY: {
4bb91a95 1146 r = asprintf(&s, "%s", k);
d93a16b8
ZJS
1147 if (r < 0)
1148 return NULL;
1149
4bb91a95 1150 r = base64_append(&s, r,
a43a068a 1151 rr->generic.data, rr->generic.data_size,
d93a16b8
ZJS
1152 8, columns());
1153 if (r < 0)
1154 return NULL;
1155 break;
1156 }
1157
8db0d2f5 1158 default:
a43a068a 1159 t = hexmem(rr->generic.data, rr->generic.data_size);
8db0d2f5 1160 if (!t)
7b50eb2e 1161 return NULL;
8db0d2f5 1162
6af47493 1163 /* Format as documented in RFC 3597, Section 5 */
a43a068a 1164 r = asprintf(&s, "%s \\# %zu %s", k, rr->generic.data_size, t);
d23a27a9 1165 if (r < 0)
7b50eb2e 1166 return NULL;
2d4c5cbc 1167 break;
8db0d2f5 1168 }
2d4c5cbc 1169
7b50eb2e
LP
1170 rr->to_string = s;
1171 return s;
2d4c5cbc 1172}
322345fd 1173
2e74028a
ZJS
1174ssize_t dns_resource_record_payload(DnsResourceRecord *rr, void **out) {
1175 assert(rr);
1176 assert(out);
1177
1178 switch(rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1179 case DNS_TYPE_SRV:
1180 case DNS_TYPE_PTR:
1181 case DNS_TYPE_NS:
1182 case DNS_TYPE_CNAME:
1183 case DNS_TYPE_DNAME:
1184 case DNS_TYPE_HINFO:
1185 case DNS_TYPE_SPF:
1186 case DNS_TYPE_TXT:
1187 case DNS_TYPE_A:
1188 case DNS_TYPE_AAAA:
1189 case DNS_TYPE_SOA:
1190 case DNS_TYPE_MX:
1191 case DNS_TYPE_LOC:
1192 case DNS_TYPE_DS:
2e74028a
ZJS
1193 case DNS_TYPE_DNSKEY:
1194 case DNS_TYPE_RRSIG:
1195 case DNS_TYPE_NSEC:
1196 case DNS_TYPE_NSEC3:
1197 return -EINVAL;
1198
e1caa6e0
ZJS
1199 case DNS_TYPE_SSHFP:
1200 *out = rr->sshfp.fingerprint;
1201 return rr->sshfp.fingerprint_size;
1202
2e74028a
ZJS
1203 case DNS_TYPE_TLSA:
1204 *out = rr->tlsa.data;
1205 return rr->tlsa.data_size;
1206
2e74028a
ZJS
1207 case DNS_TYPE_OPENPGPKEY:
1208 default:
1209 *out = rr->generic.data;
1210 return rr->generic.data_size;
1211 }
1212}
1213
a8812dd7
LP
1214int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
1215
1216 DnsPacket packet = {
1217 .n_ref = 1,
1218 .protocol = DNS_PROTOCOL_DNS,
1219 .on_stack = true,
1220 .refuse_compression = true,
1221 .canonical_form = canonical,
1222 };
1223
1224 size_t start, rds;
1225 int r;
1226
1227 assert(rr);
1228
1229 /* Generates the RR in wire-format, optionally in the
1230 * canonical form as discussed in the DNSSEC RFC 4034, Section
1231 * 6.2. We allocate a throw-away DnsPacket object on the stack
1232 * here, because we need some book-keeping for memory
1233 * management, and can reuse the DnsPacket serializer, that
1234 * can generate the canonical form, too, but also knows label
1235 * compression and suchlike. */
1236
1237 if (rr->wire_format && rr->wire_format_canonical == canonical)
1238 return 0;
1239
58ab31d5 1240 r = dns_packet_append_rr(&packet, rr, 0, &start, &rds);
a8812dd7
LP
1241 if (r < 0)
1242 return r;
1243
1244 assert(start == 0);
1245 assert(packet._data);
1246
1247 free(rr->wire_format);
1248 rr->wire_format = packet._data;
1249 rr->wire_format_size = packet.size;
1250 rr->wire_format_rdata_offset = rds;
1251 rr->wire_format_canonical = canonical;
1252
1253 packet._data = NULL;
1254 dns_packet_unref(&packet);
1255
1256 return 0;
1257}
1258
97c67192
LP
1259int dns_resource_record_signer(DnsResourceRecord *rr, const char **ret) {
1260 const char *n;
1261 int r;
1262
1263 assert(rr);
1264 assert(ret);
1265
1266 /* Returns the RRset's signer, if it is known. */
1267
1268 if (rr->n_skip_labels_signer == (unsigned) -1)
1269 return -ENODATA;
1270
1c02e7ba 1271 n = dns_resource_key_name(rr->key);
97c67192
LP
1272 r = dns_name_skip(n, rr->n_skip_labels_signer, &n);
1273 if (r < 0)
1274 return r;
1275 if (r == 0)
1276 return -EINVAL;
1277
1278 *ret = n;
1279 return 0;
1280}
1281
1282int dns_resource_record_source(DnsResourceRecord *rr, const char **ret) {
1283 const char *n;
1284 int r;
1285
1286 assert(rr);
1287 assert(ret);
1288
1289 /* Returns the RRset's synthesizing source, if it is known. */
1290
1291 if (rr->n_skip_labels_source == (unsigned) -1)
1292 return -ENODATA;
1293
1c02e7ba 1294 n = dns_resource_key_name(rr->key);
97c67192
LP
1295 r = dns_name_skip(n, rr->n_skip_labels_source, &n);
1296 if (r < 0)
1297 return r;
1298 if (r == 0)
1299 return -EINVAL;
1300
1301 *ret = n;
1302 return 0;
1303}
1304
1305int dns_resource_record_is_signer(DnsResourceRecord *rr, const char *zone) {
1306 const char *signer;
1307 int r;
1308
ab481675
LP
1309 assert(rr);
1310
97c67192
LP
1311 r = dns_resource_record_signer(rr, &signer);
1312 if (r < 0)
1313 return r;
1314
1315 return dns_name_equal(zone, signer);
1316}
1317
ab481675
LP
1318int dns_resource_record_is_synthetic(DnsResourceRecord *rr) {
1319 int r;
1320
1321 assert(rr);
1322
1323 /* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
1324
1325 if (rr->n_skip_labels_source == (unsigned) -1)
1326 return -ENODATA;
1327
1328 if (rr->n_skip_labels_source == 0)
1329 return 0;
1330
1331 if (rr->n_skip_labels_source > 1)
1332 return 1;
1333
1c02e7ba 1334 r = dns_name_startswith(dns_resource_key_name(rr->key), "*");
ab481675
LP
1335 if (r < 0)
1336 return r;
1337
1338 return !r;
1339}
1340
7a08d314 1341void dns_resource_record_hash_func(const DnsResourceRecord *rr, struct siphash *state) {
c9c72065
LP
1342 assert(rr);
1343
1344 dns_resource_key_hash_func(rr->key, state);
1345
1346 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1347
1348 case DNS_TYPE_SRV:
1349 siphash24_compress(&rr->srv.priority, sizeof(rr->srv.priority), state);
1350 siphash24_compress(&rr->srv.weight, sizeof(rr->srv.weight), state);
1351 siphash24_compress(&rr->srv.port, sizeof(rr->srv.port), state);
1352 dns_name_hash_func(rr->srv.name, state);
1353 break;
1354
1355 case DNS_TYPE_PTR:
1356 case DNS_TYPE_NS:
1357 case DNS_TYPE_CNAME:
1358 case DNS_TYPE_DNAME:
1359 dns_name_hash_func(rr->ptr.name, state);
1360 break;
1361
1362 case DNS_TYPE_HINFO:
1363 string_hash_func(rr->hinfo.cpu, state);
1364 string_hash_func(rr->hinfo.os, state);
1365 break;
1366
1367 case DNS_TYPE_TXT:
1368 case DNS_TYPE_SPF: {
1369 DnsTxtItem *j;
1370
1371 LIST_FOREACH(items, j, rr->txt.items) {
1372 siphash24_compress(j->data, j->length, state);
1373
d5115566
LP
1374 /* Add an extra NUL byte, so that "a" followed by "b" doesn't result in the same hash as "ab"
1375 * followed by "". */
1376 siphash24_compress_byte(0, state);
c9c72065
LP
1377 }
1378 break;
1379 }
1380
1381 case DNS_TYPE_A:
1382 siphash24_compress(&rr->a.in_addr, sizeof(rr->a.in_addr), state);
1383 break;
1384
1385 case DNS_TYPE_AAAA:
1386 siphash24_compress(&rr->aaaa.in6_addr, sizeof(rr->aaaa.in6_addr), state);
1387 break;
1388
1389 case DNS_TYPE_SOA:
1390 dns_name_hash_func(rr->soa.mname, state);
1391 dns_name_hash_func(rr->soa.rname, state);
1392 siphash24_compress(&rr->soa.serial, sizeof(rr->soa.serial), state);
1393 siphash24_compress(&rr->soa.refresh, sizeof(rr->soa.refresh), state);
1394 siphash24_compress(&rr->soa.retry, sizeof(rr->soa.retry), state);
1395 siphash24_compress(&rr->soa.expire, sizeof(rr->soa.expire), state);
1396 siphash24_compress(&rr->soa.minimum, sizeof(rr->soa.minimum), state);
1397 break;
1398
1399 case DNS_TYPE_MX:
1400 siphash24_compress(&rr->mx.priority, sizeof(rr->mx.priority), state);
1401 dns_name_hash_func(rr->mx.exchange, state);
1402 break;
1403
1404 case DNS_TYPE_LOC:
1405 siphash24_compress(&rr->loc.version, sizeof(rr->loc.version), state);
1406 siphash24_compress(&rr->loc.size, sizeof(rr->loc.size), state);
1407 siphash24_compress(&rr->loc.horiz_pre, sizeof(rr->loc.horiz_pre), state);
1408 siphash24_compress(&rr->loc.vert_pre, sizeof(rr->loc.vert_pre), state);
1409 siphash24_compress(&rr->loc.latitude, sizeof(rr->loc.latitude), state);
1410 siphash24_compress(&rr->loc.longitude, sizeof(rr->loc.longitude), state);
1411 siphash24_compress(&rr->loc.altitude, sizeof(rr->loc.altitude), state);
1412 break;
1413
1414 case DNS_TYPE_SSHFP:
1415 siphash24_compress(&rr->sshfp.algorithm, sizeof(rr->sshfp.algorithm), state);
1416 siphash24_compress(&rr->sshfp.fptype, sizeof(rr->sshfp.fptype), state);
1417 siphash24_compress(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size, state);
1418 break;
1419
1420 case DNS_TYPE_DNSKEY:
1421 siphash24_compress(&rr->dnskey.flags, sizeof(rr->dnskey.flags), state);
1422 siphash24_compress(&rr->dnskey.protocol, sizeof(rr->dnskey.protocol), state);
1423 siphash24_compress(&rr->dnskey.algorithm, sizeof(rr->dnskey.algorithm), state);
1424 siphash24_compress(rr->dnskey.key, rr->dnskey.key_size, state);
1425 break;
1426
1427 case DNS_TYPE_RRSIG:
1428 siphash24_compress(&rr->rrsig.type_covered, sizeof(rr->rrsig.type_covered), state);
1429 siphash24_compress(&rr->rrsig.algorithm, sizeof(rr->rrsig.algorithm), state);
1430 siphash24_compress(&rr->rrsig.labels, sizeof(rr->rrsig.labels), state);
1431 siphash24_compress(&rr->rrsig.original_ttl, sizeof(rr->rrsig.original_ttl), state);
1432 siphash24_compress(&rr->rrsig.expiration, sizeof(rr->rrsig.expiration), state);
1433 siphash24_compress(&rr->rrsig.inception, sizeof(rr->rrsig.inception), state);
1434 siphash24_compress(&rr->rrsig.key_tag, sizeof(rr->rrsig.key_tag), state);
1435 dns_name_hash_func(rr->rrsig.signer, state);
1436 siphash24_compress(rr->rrsig.signature, rr->rrsig.signature_size, state);
1437 break;
1438
1439 case DNS_TYPE_NSEC:
1440 dns_name_hash_func(rr->nsec.next_domain_name, state);
1441 /* FIXME: we leave out the type bitmap here. Hash
1442 * would be better if we'd take it into account
1443 * too. */
1444 break;
1445
1446 case DNS_TYPE_DS:
1447 siphash24_compress(&rr->ds.key_tag, sizeof(rr->ds.key_tag), state);
1448 siphash24_compress(&rr->ds.algorithm, sizeof(rr->ds.algorithm), state);
1449 siphash24_compress(&rr->ds.digest_type, sizeof(rr->ds.digest_type), state);
1450 siphash24_compress(rr->ds.digest, rr->ds.digest_size, state);
1451 break;
1452
1453 case DNS_TYPE_NSEC3:
1454 siphash24_compress(&rr->nsec3.algorithm, sizeof(rr->nsec3.algorithm), state);
1455 siphash24_compress(&rr->nsec3.flags, sizeof(rr->nsec3.flags), state);
1456 siphash24_compress(&rr->nsec3.iterations, sizeof(rr->nsec3.iterations), state);
1457 siphash24_compress(rr->nsec3.salt, rr->nsec3.salt_size, state);
1458 siphash24_compress(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, state);
1459 /* FIXME: We leave the bitmaps out */
1460 break;
48d45d2b
ZJS
1461
1462 case DNS_TYPE_TLSA:
1463 siphash24_compress(&rr->tlsa.cert_usage, sizeof(rr->tlsa.cert_usage), state);
1464 siphash24_compress(&rr->tlsa.selector, sizeof(rr->tlsa.selector), state);
1465 siphash24_compress(&rr->tlsa.matching_type, sizeof(rr->tlsa.matching_type), state);
fa45182e 1466 siphash24_compress(rr->tlsa.data, rr->tlsa.data_size, state);
48d45d2b 1467 break;
c9c72065 1468
95052df3
ZJS
1469 case DNS_TYPE_CAA:
1470 siphash24_compress(&rr->caa.flags, sizeof(rr->caa.flags), state);
1471 string_hash_func(rr->caa.tag, state);
1472 siphash24_compress(rr->caa.value, rr->caa.value_size, state);
48d45d2b 1473 break;
c9c72065 1474
d93a16b8 1475 case DNS_TYPE_OPENPGPKEY:
c9c72065 1476 default:
a43a068a 1477 siphash24_compress(rr->generic.data, rr->generic.data_size, state);
c9c72065
LP
1478 break;
1479 }
1480}
1481
7a08d314
YW
1482static int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResourceRecord *y) {
1483 int r;
c9c72065 1484
7a08d314
YW
1485 r = dns_resource_key_compare_func(x->key, y->key);
1486 if (r != 0)
1487 return r;
c9c72065
LP
1488
1489 if (dns_resource_record_equal(x, y))
1490 return 0;
1491
53c5797f
FB
1492 /* We still use CMP() here, even though don't implement proper
1493 * ordering, since the hashtable doesn't need ordering anyway. */
1494 return CMP(x, y);
c9c72065
LP
1495}
1496
7a08d314 1497DEFINE_HASH_OPS(dns_resource_record_hash_ops, DnsResourceRecord, dns_resource_record_hash_func, dns_resource_record_compare_func);
c9c72065 1498
17c8de63
LP
1499DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr) {
1500 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;
1501 DnsResourceRecord *t;
1502
1503 assert(rr);
1504
1505 copy = dns_resource_record_new(rr->key);
1506 if (!copy)
1507 return NULL;
1508
1509 copy->ttl = rr->ttl;
1510 copy->expiry = rr->expiry;
1511 copy->n_skip_labels_signer = rr->n_skip_labels_signer;
1512 copy->n_skip_labels_source = rr->n_skip_labels_source;
1513 copy->unparseable = rr->unparseable;
1514
1515 switch (rr->unparseable ? _DNS_TYPE_INVALID : rr->key->type) {
1516
1517 case DNS_TYPE_SRV:
1518 copy->srv.priority = rr->srv.priority;
1519 copy->srv.weight = rr->srv.weight;
1520 copy->srv.port = rr->srv.port;
1521 copy->srv.name = strdup(rr->srv.name);
1522 if (!copy->srv.name)
1523 return NULL;
1524 break;
1525
1526 case DNS_TYPE_PTR:
1527 case DNS_TYPE_NS:
1528 case DNS_TYPE_CNAME:
1529 case DNS_TYPE_DNAME:
1530 copy->ptr.name = strdup(rr->ptr.name);
1531 if (!copy->ptr.name)
1532 return NULL;
1533 break;
1534
1535 case DNS_TYPE_HINFO:
1536 copy->hinfo.cpu = strdup(rr->hinfo.cpu);
1537 if (!copy->hinfo.cpu)
1538 return NULL;
1539
1540 copy->hinfo.os = strdup(rr->hinfo.os);
508f63b4 1541 if (!copy->hinfo.os)
17c8de63
LP
1542 return NULL;
1543 break;
1544
1545 case DNS_TYPE_TXT:
1546 case DNS_TYPE_SPF:
1547 copy->txt.items = dns_txt_item_copy(rr->txt.items);
1548 if (!copy->txt.items)
1549 return NULL;
1550 break;
1551
1552 case DNS_TYPE_A:
1553 copy->a = rr->a;
1554 break;
1555
1556 case DNS_TYPE_AAAA:
1557 copy->aaaa = rr->aaaa;
1558 break;
1559
1560 case DNS_TYPE_SOA:
1561 copy->soa.mname = strdup(rr->soa.mname);
1562 if (!copy->soa.mname)
1563 return NULL;
1564 copy->soa.rname = strdup(rr->soa.rname);
1565 if (!copy->soa.rname)
1566 return NULL;
1567 copy->soa.serial = rr->soa.serial;
1568 copy->soa.refresh = rr->soa.refresh;
1569 copy->soa.retry = rr->soa.retry;
1570 copy->soa.expire = rr->soa.expire;
1571 copy->soa.minimum = rr->soa.minimum;
1572 break;
1573
1574 case DNS_TYPE_MX:
1575 copy->mx.priority = rr->mx.priority;
1576 copy->mx.exchange = strdup(rr->mx.exchange);
1577 if (!copy->mx.exchange)
1578 return NULL;
1579 break;
1580
1581 case DNS_TYPE_LOC:
1582 copy->loc = rr->loc;
1583 break;
1584
1585 case DNS_TYPE_SSHFP:
1586 copy->sshfp.algorithm = rr->sshfp.algorithm;
1587 copy->sshfp.fptype = rr->sshfp.fptype;
1588 copy->sshfp.fingerprint = memdup(rr->sshfp.fingerprint, rr->sshfp.fingerprint_size);
1589 if (!copy->sshfp.fingerprint)
1590 return NULL;
1591 copy->sshfp.fingerprint_size = rr->sshfp.fingerprint_size;
1592 break;
1593
1594 case DNS_TYPE_DNSKEY:
1595 copy->dnskey.flags = rr->dnskey.flags;
1596 copy->dnskey.protocol = rr->dnskey.protocol;
1597 copy->dnskey.algorithm = rr->dnskey.algorithm;
1598 copy->dnskey.key = memdup(rr->dnskey.key, rr->dnskey.key_size);
1599 if (!copy->dnskey.key)
1600 return NULL;
1601 copy->dnskey.key_size = rr->dnskey.key_size;
1602 break;
1603
1604 case DNS_TYPE_RRSIG:
1605 copy->rrsig.type_covered = rr->rrsig.type_covered;
1606 copy->rrsig.algorithm = rr->rrsig.algorithm;
1607 copy->rrsig.labels = rr->rrsig.labels;
1608 copy->rrsig.original_ttl = rr->rrsig.original_ttl;
1609 copy->rrsig.expiration = rr->rrsig.expiration;
1610 copy->rrsig.inception = rr->rrsig.inception;
1611 copy->rrsig.key_tag = rr->rrsig.key_tag;
1612 copy->rrsig.signer = strdup(rr->rrsig.signer);
1613 if (!copy->rrsig.signer)
1614 return NULL;
1615 copy->rrsig.signature = memdup(rr->rrsig.signature, rr->rrsig.signature_size);
1616 if (!copy->rrsig.signature)
1617 return NULL;
1618 copy->rrsig.signature_size = rr->rrsig.signature_size;
1619 break;
1620
1621 case DNS_TYPE_NSEC:
1622 copy->nsec.next_domain_name = strdup(rr->nsec.next_domain_name);
1623 if (!copy->nsec.next_domain_name)
1624 return NULL;
1625 copy->nsec.types = bitmap_copy(rr->nsec.types);
1626 if (!copy->nsec.types)
1627 return NULL;
1628 break;
1629
1630 case DNS_TYPE_DS:
1631 copy->ds.key_tag = rr->ds.key_tag;
1632 copy->ds.algorithm = rr->ds.algorithm;
1633 copy->ds.digest_type = rr->ds.digest_type;
1634 copy->ds.digest = memdup(rr->ds.digest, rr->ds.digest_size);
1635 if (!copy->ds.digest)
1636 return NULL;
1637 copy->ds.digest_size = rr->ds.digest_size;
1638 break;
1639
1640 case DNS_TYPE_NSEC3:
1641 copy->nsec3.algorithm = rr->nsec3.algorithm;
1642 copy->nsec3.flags = rr->nsec3.flags;
1643 copy->nsec3.iterations = rr->nsec3.iterations;
1644 copy->nsec3.salt = memdup(rr->nsec3.salt, rr->nsec3.salt_size);
1645 if (!copy->nsec3.salt)
1646 return NULL;
1647 copy->nsec3.salt_size = rr->nsec3.salt_size;
1648 copy->nsec3.next_hashed_name = memdup(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size);
1649 if (!copy->nsec3.next_hashed_name_size)
1650 return NULL;
1651 copy->nsec3.next_hashed_name_size = rr->nsec3.next_hashed_name_size;
1652 copy->nsec3.types = bitmap_copy(rr->nsec3.types);
1653 if (!copy->nsec3.types)
1654 return NULL;
1655 break;
1656
1657 case DNS_TYPE_TLSA:
1658 copy->tlsa.cert_usage = rr->tlsa.cert_usage;
1659 copy->tlsa.selector = rr->tlsa.selector;
1660 copy->tlsa.matching_type = rr->tlsa.matching_type;
1661 copy->tlsa.data = memdup(rr->tlsa.data, rr->tlsa.data_size);
1662 if (!copy->tlsa.data)
1663 return NULL;
1664 copy->tlsa.data_size = rr->tlsa.data_size;
1665 break;
1666
1667 case DNS_TYPE_CAA:
1668 copy->caa.flags = rr->caa.flags;
1669 copy->caa.tag = strdup(rr->caa.tag);
1670 if (!copy->caa.tag)
1671 return NULL;
1672 copy->caa.value = memdup(rr->caa.value, rr->caa.value_size);
1673 if (!copy->caa.value)
1674 return NULL;
1675 copy->caa.value_size = rr->caa.value_size;
1676 break;
1677
1678 case DNS_TYPE_OPT:
1679 default:
1680 copy->generic.data = memdup(rr->generic.data, rr->generic.data_size);
1681 if (!copy->generic.data)
1682 return NULL;
1683 copy->generic.data_size = rr->generic.data_size;
1684 break;
1685 }
1686
1cc6c93a 1687 t = TAKE_PTR(copy);
17c8de63
LP
1688
1689 return t;
1690}
1691
1692int dns_resource_record_clamp_ttl(DnsResourceRecord **rr, uint32_t max_ttl) {
1693 DnsResourceRecord *old_rr, *new_rr;
1694 uint32_t new_ttl;
1695
1696 assert(rr);
1697 old_rr = *rr;
1698
1699 if (old_rr->key->type == DNS_TYPE_OPT)
1700 return -EINVAL;
1701
1702 new_ttl = MIN(old_rr->ttl, max_ttl);
1703 if (new_ttl == old_rr->ttl)
1704 return 0;
1705
1706 if (old_rr->n_ref == 1) {
1707 /* Patch in place */
1708 old_rr->ttl = new_ttl;
1709 return 1;
1710 }
1711
1712 new_rr = dns_resource_record_copy(old_rr);
1713 if (!new_rr)
1714 return -ENOMEM;
1715
1716 new_rr->ttl = new_ttl;
1717
1718 dns_resource_record_unref(*rr);
1719 *rr = new_rr;
1720
1721 return 1;
1722}
1723
2001c805
LP
1724DnsTxtItem *dns_txt_item_free_all(DnsTxtItem *i) {
1725 DnsTxtItem *n;
1726
1727 if (!i)
1728 return NULL;
1729
1730 n = i->items_next;
1731
1732 free(i);
1733 return dns_txt_item_free_all(n);
1734}
1735
1736bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b) {
1737
4d247a6c
LP
1738 if (a == b)
1739 return true;
1740
2001c805
LP
1741 if (!a != !b)
1742 return false;
1743
1744 if (!a)
1745 return true;
1746
1747 if (a->length != b->length)
1748 return false;
1749
1750 if (memcmp(a->data, b->data, a->length) != 0)
1751 return false;
1752
1753 return dns_txt_item_equal(a->items_next, b->items_next);
1754}
8730bccf 1755
17c8de63
LP
1756DnsTxtItem *dns_txt_item_copy(DnsTxtItem *first) {
1757 DnsTxtItem *i, *copy = NULL, *end = NULL;
1758
1759 LIST_FOREACH(items, i, first) {
1760 DnsTxtItem *j;
1761
1762 j = memdup(i, offsetof(DnsTxtItem, data) + i->length + 1);
1763 if (!j) {
1764 dns_txt_item_free_all(copy);
1765 return NULL;
1766 }
1767
1768 LIST_INSERT_AFTER(items, copy, end, j);
1769 end = j;
1770 }
1771
1772 return copy;
1773}
1774
ebb779dc
DR
1775int dns_txt_item_new_empty(DnsTxtItem **ret) {
1776 DnsTxtItem *i;
1777
1778 /* RFC 6763, section 6.1 suggests to treat
1779 * empty TXT RRs as equivalent to a TXT record
1780 * with a single empty string. */
1781
1782 i = malloc0(offsetof(DnsTxtItem, data) + 1); /* for safety reasons we add an extra NUL byte */
1783 if (!i)
1784 return -ENOMEM;
1785
1786 *ret = i;
1787
1788 return 0;
1789}
1790
8730bccf 1791static const char* const dnssec_algorithm_table[_DNSSEC_ALGORITHM_MAX_DEFINED] = {
6f717d08 1792 /* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
8730bccf
LP
1793 [DNSSEC_ALGORITHM_RSAMD5] = "RSAMD5",
1794 [DNSSEC_ALGORITHM_DH] = "DH",
1795 [DNSSEC_ALGORITHM_DSA] = "DSA",
1796 [DNSSEC_ALGORITHM_ECC] = "ECC",
1797 [DNSSEC_ALGORITHM_RSASHA1] = "RSASHA1",
1798 [DNSSEC_ALGORITHM_DSA_NSEC3_SHA1] = "DSA-NSEC3-SHA1",
1799 [DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1] = "RSASHA1-NSEC3-SHA1",
1800 [DNSSEC_ALGORITHM_RSASHA256] = "RSASHA256",
1801 [DNSSEC_ALGORITHM_RSASHA512] = "RSASHA512",
6f717d08
LP
1802 [DNSSEC_ALGORITHM_ECC_GOST] = "ECC-GOST",
1803 [DNSSEC_ALGORITHM_ECDSAP256SHA256] = "ECDSAP256SHA256",
1804 [DNSSEC_ALGORITHM_ECDSAP384SHA384] = "ECDSAP384SHA384",
cb9eeb06
MCO
1805 [DNSSEC_ALGORITHM_ED25519] = "ED25519",
1806 [DNSSEC_ALGORITHM_ED448] = "ED448",
8730bccf
LP
1807 [DNSSEC_ALGORITHM_INDIRECT] = "INDIRECT",
1808 [DNSSEC_ALGORITHM_PRIVATEDNS] = "PRIVATEDNS",
1809 [DNSSEC_ALGORITHM_PRIVATEOID] = "PRIVATEOID",
1810};
8e54f5d9 1811DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm, int, 255);
8730bccf
LP
1812
1813static const char* const dnssec_digest_table[_DNSSEC_DIGEST_MAX_DEFINED] = {
6f717d08
LP
1814 /* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1815 [DNSSEC_DIGEST_SHA1] = "SHA-1",
1816 [DNSSEC_DIGEST_SHA256] = "SHA-256",
1817 [DNSSEC_DIGEST_GOST_R_34_11_94] = "GOST_R_34.11-94",
1818 [DNSSEC_DIGEST_SHA384] = "SHA-384",
8730bccf 1819};
8e54f5d9 1820DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest, int, 255);