1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "dns-domain.h"
10 #include "memory-util.h"
11 #include "resolved-dns-dnssec.h"
12 #include "resolved-dns-packet.h"
13 #include "resolved-dns-rr.h"
14 #include "string-table.h"
15 #include "string-util.h"
17 #include "terminal-util.h"
19 DnsResourceKey
* dns_resource_key_new(uint16_t class, uint16_t type
, const char *name
) {
26 k
= malloc0(sizeof(DnsResourceKey
) + l
+ 1);
34 strcpy((char*) k
+ sizeof(DnsResourceKey
), name
);
39 DnsResourceKey
* dns_resource_key_new_redirect(const DnsResourceKey
*key
, const DnsResourceRecord
*cname
) {
45 assert(IN_SET(cname
->key
->type
, DNS_TYPE_CNAME
, DNS_TYPE_DNAME
));
47 if (cname
->key
->type
== DNS_TYPE_CNAME
)
48 return dns_resource_key_new(key
->class, key
->type
, cname
->cname
.name
);
51 char *destination
= NULL
;
53 r
= dns_name_change_suffix(dns_resource_key_name(key
), dns_resource_key_name(cname
->key
), cname
->dname
.name
, &destination
);
57 return dns_resource_key_ref((DnsResourceKey
*) key
);
59 k
= dns_resource_key_new_consume(key
->class, key
->type
, destination
);
61 return mfree(destination
);
67 int dns_resource_key_new_append_suffix(DnsResourceKey
**ret
, DnsResourceKey
*key
, char *name
) {
68 DnsResourceKey
*new_key
;
76 if (dns_name_is_root(name
)) {
77 *ret
= dns_resource_key_ref(key
);
81 r
= dns_name_concat(dns_resource_key_name(key
), name
, 0, &joined
);
85 new_key
= dns_resource_key_new_consume(key
->class, key
->type
, joined
);
95 DnsResourceKey
* dns_resource_key_new_consume(uint16_t class, uint16_t type
, char *name
) {
100 k
= new(DnsResourceKey
, 1);
104 *k
= (DnsResourceKey
) {
114 DnsResourceKey
* dns_resource_key_ref(DnsResourceKey
*k
) {
119 /* Static/const keys created with DNS_RESOURCE_KEY_CONST will
120 * set this to -1, they should not be reffed/unreffed */
121 assert(k
->n_ref
!= (unsigned) -1);
123 assert(k
->n_ref
> 0);
129 DnsResourceKey
* dns_resource_key_unref(DnsResourceKey
*k
) {
133 assert(k
->n_ref
!= (unsigned) -1);
134 assert(k
->n_ref
> 0);
145 const char* dns_resource_key_name(const DnsResourceKey
*key
) {
154 name
= (char*) key
+ sizeof(DnsResourceKey
);
156 if (dns_name_is_root(name
))
162 bool dns_resource_key_is_address(const DnsResourceKey
*key
) {
165 /* Check if this is an A or AAAA resource key */
167 return key
->class == DNS_CLASS_IN
&& IN_SET(key
->type
, DNS_TYPE_A
, DNS_TYPE_AAAA
);
170 bool dns_resource_key_is_dnssd_ptr(const DnsResourceKey
*key
) {
173 /* Check if this is a PTR resource key used in
174 Service Instance Enumeration as described in RFC6763 p4.1. */
176 if (key
->type
!= DNS_TYPE_PTR
)
179 return dns_name_endswith(dns_resource_key_name(key
), "_tcp.local") ||
180 dns_name_endswith(dns_resource_key_name(key
), "_udp.local");
183 int dns_resource_key_equal(const DnsResourceKey
*a
, const DnsResourceKey
*b
) {
189 r
= dns_name_equal(dns_resource_key_name(a
), dns_resource_key_name(b
));
193 if (a
->class != b
->class)
196 if (a
->type
!= b
->type
)
202 int dns_resource_key_match_rr(const DnsResourceKey
*key
, DnsResourceRecord
*rr
, const char *search_domain
) {
211 /* Checks if an rr matches the specified key. If a search
212 * domain is specified, it will also be checked if the key
213 * with the search domain suffixed might match the RR. */
215 if (rr
->key
->class != key
->class && key
->class != DNS_CLASS_ANY
)
218 if (rr
->key
->type
!= key
->type
&& key
->type
!= DNS_TYPE_ANY
)
221 r
= dns_name_equal(dns_resource_key_name(rr
->key
), dns_resource_key_name(key
));
226 _cleanup_free_
char *joined
= NULL
;
228 r
= dns_name_concat(dns_resource_key_name(key
), search_domain
, 0, &joined
);
232 return dns_name_equal(dns_resource_key_name(rr
->key
), joined
);
238 int dns_resource_key_match_cname_or_dname(const DnsResourceKey
*key
, const DnsResourceKey
*cname
, const char *search_domain
) {
244 if (cname
->class != key
->class && key
->class != DNS_CLASS_ANY
)
247 if (cname
->type
== DNS_TYPE_CNAME
)
248 r
= dns_name_equal(dns_resource_key_name(key
), dns_resource_key_name(cname
));
249 else if (cname
->type
== DNS_TYPE_DNAME
)
250 r
= dns_name_endswith(dns_resource_key_name(key
), dns_resource_key_name(cname
));
258 _cleanup_free_
char *joined
= NULL
;
260 r
= dns_name_concat(dns_resource_key_name(key
), search_domain
, 0, &joined
);
264 if (cname
->type
== DNS_TYPE_CNAME
)
265 return dns_name_equal(joined
, dns_resource_key_name(cname
));
266 else if (cname
->type
== DNS_TYPE_DNAME
)
267 return dns_name_endswith(joined
, dns_resource_key_name(cname
));
273 int dns_resource_key_match_soa(const DnsResourceKey
*key
, const DnsResourceKey
*soa
) {
277 /* Checks whether 'soa' is a SOA record for the specified key. */
279 if (soa
->class != key
->class)
282 if (soa
->type
!= DNS_TYPE_SOA
)
285 return dns_name_endswith(dns_resource_key_name(key
), dns_resource_key_name(soa
));
288 static void dns_resource_key_hash_func(const DnsResourceKey
*k
, struct siphash
*state
) {
291 dns_name_hash_func(dns_resource_key_name(k
), state
);
292 siphash24_compress(&k
->class, sizeof(k
->class), state
);
293 siphash24_compress(&k
->type
, sizeof(k
->type
), state
);
296 static int dns_resource_key_compare_func(const DnsResourceKey
*x
, const DnsResourceKey
*y
) {
299 r
= dns_name_compare_func(dns_resource_key_name(x
), dns_resource_key_name(y
));
303 r
= CMP(x
->type
, y
->type
);
307 return CMP(x
->class, y
->class);
310 DEFINE_HASH_OPS(dns_resource_key_hash_ops
, DnsResourceKey
, dns_resource_key_hash_func
, dns_resource_key_compare_func
);
312 char* dns_resource_key_to_string(const DnsResourceKey
*key
, char *buf
, size_t buf_size
) {
316 /* If we cannot convert the CLASS/TYPE into a known string,
317 use the format recommended by RFC 3597, Section 5. */
319 c
= dns_class_to_string(key
->class);
320 t
= dns_type_to_string(key
->type
);
322 snprintf(buf
, buf_size
, "%s %s%s%.0u %s%s%.0u",
323 dns_resource_key_name(key
),
324 strempty(c
), c
? "" : "CLASS", c
? 0 : key
->class,
325 strempty(t
), t
? "" : "TYPE", t
? 0 : key
->type
);
330 bool dns_resource_key_reduce(DnsResourceKey
**a
, DnsResourceKey
**b
) {
334 /* Try to replace one RR key by another if they are identical, thus saving a bit of memory. Note that we do
335 * this only for RR keys, not for RRs themselves, as they carry a lot of additional metadata (where they come
336 * from, validity data, and suchlike), and cannot be replaced so easily by other RRs that have the same
337 * superficial data. */
344 /* We refuse merging const keys */
345 if ((*a
)->n_ref
== (unsigned) -1)
347 if ((*b
)->n_ref
== (unsigned) -1)
350 /* Already the same? */
354 /* Are they really identical? */
355 if (dns_resource_key_equal(*a
, *b
) <= 0)
358 /* Keep the one which already has more references. */
359 if ((*a
)->n_ref
> (*b
)->n_ref
) {
360 dns_resource_key_unref(*b
);
361 *b
= dns_resource_key_ref(*a
);
363 dns_resource_key_unref(*a
);
364 *a
= dns_resource_key_ref(*b
);
370 DnsResourceRecord
* dns_resource_record_new(DnsResourceKey
*key
) {
371 DnsResourceRecord
*rr
;
373 rr
= new(DnsResourceRecord
, 1);
377 *rr
= (DnsResourceRecord
) {
379 .key
= dns_resource_key_ref(key
),
380 .expiry
= USEC_INFINITY
,
381 .n_skip_labels_signer
= (unsigned) -1,
382 .n_skip_labels_source
= (unsigned) -1,
388 DnsResourceRecord
* dns_resource_record_new_full(uint16_t class, uint16_t type
, const char *name
) {
389 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
391 key
= dns_resource_key_new(class, type
, name
);
395 return dns_resource_record_new(key
);
398 static DnsResourceRecord
* dns_resource_record_free(DnsResourceRecord
*rr
) {
402 switch(rr
->key
->type
) {
422 dns_txt_item_free_all(rr
->txt
.items
);
431 free(rr
->mx
.exchange
);
439 free(rr
->sshfp
.fingerprint
);
442 case DNS_TYPE_DNSKEY
:
443 free(rr
->dnskey
.key
);
447 free(rr
->rrsig
.signer
);
448 free(rr
->rrsig
.signature
);
452 free(rr
->nsec
.next_domain_name
);
453 bitmap_free(rr
->nsec
.types
);
457 free(rr
->nsec3
.next_hashed_name
);
458 free(rr
->nsec3
.salt
);
459 bitmap_free(rr
->nsec3
.types
);
476 case DNS_TYPE_OPENPGPKEY
:
479 free(rr
->generic
.data
);
483 free(rr
->generic
.data
);
485 free(rr
->wire_format
);
486 dns_resource_key_unref(rr
->key
);
493 DEFINE_TRIVIAL_REF_UNREF_FUNC(DnsResourceRecord
, dns_resource_record
, dns_resource_record_free
);
495 int dns_resource_record_new_reverse(DnsResourceRecord
**ret
, int family
, const union in_addr_union
*address
, const char *hostname
) {
496 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
497 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
498 _cleanup_free_
char *ptr
= NULL
;
505 r
= dns_name_reverse(family
, address
, &ptr
);
509 key
= dns_resource_key_new_consume(DNS_CLASS_IN
, DNS_TYPE_PTR
, ptr
);
515 rr
= dns_resource_record_new(key
);
519 rr
->ptr
.name
= strdup(hostname
);
528 int dns_resource_record_new_address(DnsResourceRecord
**ret
, int family
, const union in_addr_union
*address
, const char *name
) {
529 DnsResourceRecord
*rr
;
535 if (family
== AF_INET
) {
537 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_A
, name
);
541 rr
->a
.in_addr
= address
->in
;
543 } else if (family
== AF_INET6
) {
545 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_AAAA
, name
);
549 rr
->aaaa
.in6_addr
= address
->in6
;
551 return -EAFNOSUPPORT
;
558 #define FIELD_EQUAL(a, b, field) \
559 ((a).field ## _size == (b).field ## _size && \
560 memcmp_safe((a).field, (b).field, (a).field ## _size) == 0)
562 int dns_resource_record_payload_equal(const DnsResourceRecord
*a
, const DnsResourceRecord
*b
) {
565 /* Check if a and b are the same, but don't look at their keys */
567 if (a
->unparsable
!= b
->unparsable
)
570 switch (a
->unparsable
? _DNS_TYPE_INVALID
: a
->key
->type
) {
573 r
= dns_name_equal(a
->srv
.name
, b
->srv
.name
);
577 return a
->srv
.priority
== b
->srv
.priority
&&
578 a
->srv
.weight
== b
->srv
.weight
&&
579 a
->srv
.port
== b
->srv
.port
;
585 return dns_name_equal(a
->ptr
.name
, b
->ptr
.name
);
588 return strcaseeq(a
->hinfo
.cpu
, b
->hinfo
.cpu
) &&
589 strcaseeq(a
->hinfo
.os
, b
->hinfo
.os
);
591 case DNS_TYPE_SPF
: /* exactly the same as TXT */
593 return dns_txt_item_equal(a
->txt
.items
, b
->txt
.items
);
596 return memcmp(&a
->a
.in_addr
, &b
->a
.in_addr
, sizeof(struct in_addr
)) == 0;
599 return memcmp(&a
->aaaa
.in6_addr
, &b
->aaaa
.in6_addr
, sizeof(struct in6_addr
)) == 0;
602 r
= dns_name_equal(a
->soa
.mname
, b
->soa
.mname
);
605 r
= dns_name_equal(a
->soa
.rname
, b
->soa
.rname
);
609 return a
->soa
.serial
== b
->soa
.serial
&&
610 a
->soa
.refresh
== b
->soa
.refresh
&&
611 a
->soa
.retry
== b
->soa
.retry
&&
612 a
->soa
.expire
== b
->soa
.expire
&&
613 a
->soa
.minimum
== b
->soa
.minimum
;
616 if (a
->mx
.priority
!= b
->mx
.priority
)
619 return dns_name_equal(a
->mx
.exchange
, b
->mx
.exchange
);
622 assert(a
->loc
.version
== b
->loc
.version
);
624 return a
->loc
.size
== b
->loc
.size
&&
625 a
->loc
.horiz_pre
== b
->loc
.horiz_pre
&&
626 a
->loc
.vert_pre
== b
->loc
.vert_pre
&&
627 a
->loc
.latitude
== b
->loc
.latitude
&&
628 a
->loc
.longitude
== b
->loc
.longitude
&&
629 a
->loc
.altitude
== b
->loc
.altitude
;
632 return a
->ds
.key_tag
== b
->ds
.key_tag
&&
633 a
->ds
.algorithm
== b
->ds
.algorithm
&&
634 a
->ds
.digest_type
== b
->ds
.digest_type
&&
635 FIELD_EQUAL(a
->ds
, b
->ds
, digest
);
638 return a
->sshfp
.algorithm
== b
->sshfp
.algorithm
&&
639 a
->sshfp
.fptype
== b
->sshfp
.fptype
&&
640 FIELD_EQUAL(a
->sshfp
, b
->sshfp
, fingerprint
);
642 case DNS_TYPE_DNSKEY
:
643 return a
->dnskey
.flags
== b
->dnskey
.flags
&&
644 a
->dnskey
.protocol
== b
->dnskey
.protocol
&&
645 a
->dnskey
.algorithm
== b
->dnskey
.algorithm
&&
646 FIELD_EQUAL(a
->dnskey
, b
->dnskey
, key
);
649 /* do the fast comparisons first */
650 return a
->rrsig
.type_covered
== b
->rrsig
.type_covered
&&
651 a
->rrsig
.algorithm
== b
->rrsig
.algorithm
&&
652 a
->rrsig
.labels
== b
->rrsig
.labels
&&
653 a
->rrsig
.original_ttl
== b
->rrsig
.original_ttl
&&
654 a
->rrsig
.expiration
== b
->rrsig
.expiration
&&
655 a
->rrsig
.inception
== b
->rrsig
.inception
&&
656 a
->rrsig
.key_tag
== b
->rrsig
.key_tag
&&
657 FIELD_EQUAL(a
->rrsig
, b
->rrsig
, signature
) &&
658 dns_name_equal(a
->rrsig
.signer
, b
->rrsig
.signer
);
661 return dns_name_equal(a
->nsec
.next_domain_name
, b
->nsec
.next_domain_name
) &&
662 bitmap_equal(a
->nsec
.types
, b
->nsec
.types
);
665 return a
->nsec3
.algorithm
== b
->nsec3
.algorithm
&&
666 a
->nsec3
.flags
== b
->nsec3
.flags
&&
667 a
->nsec3
.iterations
== b
->nsec3
.iterations
&&
668 FIELD_EQUAL(a
->nsec3
, b
->nsec3
, salt
) &&
669 FIELD_EQUAL(a
->nsec3
, b
->nsec3
, next_hashed_name
) &&
670 bitmap_equal(a
->nsec3
.types
, b
->nsec3
.types
);
673 return a
->tlsa
.cert_usage
== b
->tlsa
.cert_usage
&&
674 a
->tlsa
.selector
== b
->tlsa
.selector
&&
675 a
->tlsa
.matching_type
== b
->tlsa
.matching_type
&&
676 FIELD_EQUAL(a
->tlsa
, b
->tlsa
, data
);
679 return a
->caa
.flags
== b
->caa
.flags
&&
680 streq(a
->caa
.tag
, b
->caa
.tag
) &&
681 FIELD_EQUAL(a
->caa
, b
->caa
, value
);
683 case DNS_TYPE_OPENPGPKEY
:
685 return FIELD_EQUAL(a
->generic
, b
->generic
, data
);
689 int dns_resource_record_equal(const DnsResourceRecord
*a
, const DnsResourceRecord
*b
) {
698 r
= dns_resource_key_equal(a
->key
, b
->key
);
702 return dns_resource_record_payload_equal(a
, b
);
705 static char* format_location(uint32_t latitude
, uint32_t longitude
, uint32_t altitude
,
706 uint8_t size
, uint8_t horiz_pre
, uint8_t vert_pre
) {
708 char NS
= latitude
>= 1U<<31 ? 'N' : 'S';
709 char EW
= longitude
>= 1U<<31 ? 'E' : 'W';
711 int lat
= latitude
>= 1U<<31 ? (int) (latitude
- (1U<<31)) : (int) ((1U<<31) - latitude
);
712 int lon
= longitude
>= 1U<<31 ? (int) (longitude
- (1U<<31)) : (int) ((1U<<31) - longitude
);
713 double alt
= altitude
>= 10000000u ? altitude
- 10000000u : -(double)(10000000u - altitude
);
714 double siz
= (size
>> 4) * exp10((double) (size
& 0xF));
715 double hor
= (horiz_pre
>> 4) * exp10((double) (horiz_pre
& 0xF));
716 double ver
= (vert_pre
>> 4) * exp10((double) (vert_pre
& 0xF));
718 if (asprintf(&s
, "%d %d %.3f %c %d %d %.3f %c %.2fm %.2fm %.2fm %.2fm",
721 (lat
% 60000) / 1000.,
725 (lon
% 60000) / 1000.,
736 static int format_timestamp_dns(char *buf
, size_t l
, time_t sec
) {
740 assert(l
> STRLEN("YYYYMMDDHHmmSS"));
742 if (!gmtime_r(&sec
, &tm
))
745 if (strftime(buf
, l
, "%Y%m%d%H%M%S", &tm
) <= 0)
751 static char *format_types(Bitmap
*types
) {
752 _cleanup_strv_free_
char **strv
= NULL
;
753 _cleanup_free_
char *str
= NULL
;
757 BITMAP_FOREACH(type
, types
) {
758 if (dns_type_to_string(type
)) {
759 r
= strv_extend(&strv
, dns_type_to_string(type
));
765 r
= asprintf(&t
, "TYPE%u", type
);
769 r
= strv_consume(&strv
, t
);
775 str
= strv_join(strv
, " ");
779 return strjoin("( ", str
, " )");
782 static char *format_txt(DnsTxtItem
*first
) {
787 LIST_FOREACH(items
, i
, first
)
788 c
+= i
->length
* 4 + 3;
790 p
= s
= new(char, c
);
794 LIST_FOREACH(items
, i
, first
) {
802 for (j
= 0; j
< i
->length
; j
++) {
803 if (i
->data
[j
] < ' ' || i
->data
[j
] == '"' || i
->data
[j
] >= 127) {
805 *(p
++) = '0' + (i
->data
[j
] / 100);
806 *(p
++) = '0' + ((i
->data
[j
] / 10) % 10);
807 *(p
++) = '0' + (i
->data
[j
] % 10);
819 const char *dns_resource_record_to_string(DnsResourceRecord
*rr
) {
820 _cleanup_free_
char *t
= NULL
;
821 char *s
, k
[DNS_RESOURCE_KEY_STRING_MAX
];
827 return rr
->to_string
;
829 dns_resource_key_to_string(rr
->key
, k
, sizeof(k
));
831 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
834 r
= asprintf(&s
, "%s %u %u %u %s",
839 strna(rr
->srv
.name
));
848 s
= strjoin(k
, " ", rr
->ptr
.name
);
855 s
= strjoin(k
, " ", rr
->hinfo
.cpu
, " ", rr
->hinfo
.os
);
860 case DNS_TYPE_SPF
: /* exactly the same as TXT */
862 t
= format_txt(rr
->txt
.items
);
866 s
= strjoin(k
, " ", t
);
872 _cleanup_free_
char *x
= NULL
;
874 r
= in_addr_to_string(AF_INET
, (const union in_addr_union
*) &rr
->a
.in_addr
, &x
);
878 s
= strjoin(k
, " ", x
);
885 r
= in_addr_to_string(AF_INET6
, (const union in_addr_union
*) &rr
->aaaa
.in6_addr
, &t
);
889 s
= strjoin(k
, " ", t
);
895 r
= asprintf(&s
, "%s %s %s %u %u %u %u %u",
897 strna(rr
->soa
.mname
),
898 strna(rr
->soa
.rname
),
909 r
= asprintf(&s
, "%s %u %s",
918 assert(rr
->loc
.version
== 0);
920 t
= format_location(rr
->loc
.latitude
,
929 s
= strjoin(k
, " ", t
);
935 t
= hexmem(rr
->ds
.digest
, rr
->ds
.digest_size
);
939 r
= asprintf(&s
, "%s %u %u %u %s",
950 t
= hexmem(rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
);
954 r
= asprintf(&s
, "%s %u %u %s",
963 case DNS_TYPE_DNSKEY
: {
964 _cleanup_free_
char *alg
= NULL
;
968 key_tag
= dnssec_keytag(rr
, true);
970 r
= dnssec_algorithm_to_string_alloc(rr
->dnskey
.algorithm
, &alg
);
974 r
= asprintf(&s
, "%s %u %u %s",
982 r
= base64_append(&s
, r
,
983 rr
->dnskey
.key
, rr
->dnskey
.key_size
,
988 r
= asprintf(&ss
, "%s\n"
992 rr
->dnskey
.flags
& DNSKEY_FLAG_SEP
? " SEP" : "",
993 rr
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
? " REVOKE" : "",
994 rr
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
? " ZONE_KEY" : "",
1004 case DNS_TYPE_RRSIG
: {
1005 _cleanup_free_
char *alg
= NULL
;
1006 char expiration
[STRLEN("YYYYMMDDHHmmSS") + 1], inception
[STRLEN("YYYYMMDDHHmmSS") + 1];
1009 type
= dns_type_to_string(rr
->rrsig
.type_covered
);
1011 r
= dnssec_algorithm_to_string_alloc(rr
->rrsig
.algorithm
, &alg
);
1015 r
= format_timestamp_dns(expiration
, sizeof(expiration
), rr
->rrsig
.expiration
);
1019 r
= format_timestamp_dns(inception
, sizeof(inception
), rr
->rrsig
.inception
);
1024 * http://tools.ietf.org/html/rfc3597#section-5 */
1026 r
= asprintf(&s
, "%s %s%.*u %s %u %u %s %s %u %s",
1029 type
? 0 : 1, type
? 0u : (unsigned) rr
->rrsig
.type_covered
,
1032 rr
->rrsig
.original_ttl
,
1040 r
= base64_append(&s
, r
,
1041 rr
->rrsig
.signature
, rr
->rrsig
.signature_size
,
1050 t
= format_types(rr
->nsec
.types
);
1054 r
= asprintf(&s
, "%s %s %s",
1056 rr
->nsec
.next_domain_name
,
1062 case DNS_TYPE_NSEC3
: {
1063 _cleanup_free_
char *salt
= NULL
, *hash
= NULL
;
1065 if (rr
->nsec3
.salt_size
> 0) {
1066 salt
= hexmem(rr
->nsec3
.salt
, rr
->nsec3
.salt_size
);
1071 hash
= base32hexmem(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, false);
1075 t
= format_types(rr
->nsec3
.types
);
1079 r
= asprintf(&s
, "%s %"PRIu8
" %"PRIu8
" %"PRIu16
" %s %s %s",
1081 rr
->nsec3
.algorithm
,
1083 rr
->nsec3
.iterations
,
1084 rr
->nsec3
.salt_size
> 0 ? salt
: "-",
1093 case DNS_TYPE_TLSA
: {
1094 const char *cert_usage
, *selector
, *matching_type
;
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
);
1100 t
= hexmem(rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
);
1106 " -- Cert. usage: %s\n"
1107 " -- Selector: %s\n"
1108 " -- Matching type: %s",
1110 rr
->tlsa
.cert_usage
,
1112 rr
->tlsa
.matching_type
,
1123 case DNS_TYPE_CAA
: {
1124 _cleanup_free_
char *value
;
1126 value
= octescape(rr
->caa
.value
, rr
->caa
.value_size
);
1130 r
= asprintf(&s
, "%s %u %s \"%s\"%s%s%s%.0u",
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
);
1145 case DNS_TYPE_OPENPGPKEY
: {
1146 r
= asprintf(&s
, "%s", k
);
1150 r
= base64_append(&s
, r
,
1151 rr
->generic
.data
, rr
->generic
.data_size
,
1159 t
= hexmem(rr
->generic
.data
, rr
->generic
.data_size
);
1163 /* Format as documented in RFC 3597, Section 5 */
1164 r
= asprintf(&s
, "%s \\# %zu %s", k
, rr
->generic
.data_size
, t
);
1174 ssize_t
dns_resource_record_payload(DnsResourceRecord
*rr
, void **out
) {
1178 switch(rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
1182 case DNS_TYPE_CNAME
:
1183 case DNS_TYPE_DNAME
:
1184 case DNS_TYPE_HINFO
:
1193 case DNS_TYPE_DNSKEY
:
1194 case DNS_TYPE_RRSIG
:
1196 case DNS_TYPE_NSEC3
:
1199 case DNS_TYPE_SSHFP
:
1200 *out
= rr
->sshfp
.fingerprint
;
1201 return rr
->sshfp
.fingerprint_size
;
1204 *out
= rr
->tlsa
.data
;
1205 return rr
->tlsa
.data_size
;
1207 case DNS_TYPE_OPENPGPKEY
:
1209 *out
= rr
->generic
.data
;
1210 return rr
->generic
.data_size
;
1214 int dns_resource_record_to_wire_format(DnsResourceRecord
*rr
, bool canonical
) {
1216 DnsPacket packet
= {
1218 .protocol
= DNS_PROTOCOL_DNS
,
1220 .refuse_compression
= true,
1221 .canonical_form
= canonical
,
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. */
1237 if (rr
->wire_format
&& rr
->wire_format_canonical
== canonical
)
1240 r
= dns_packet_append_rr(&packet
, rr
, 0, &start
, &rds
);
1245 assert(packet
._data
);
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
;
1253 packet
._data
= NULL
;
1254 dns_packet_unref(&packet
);
1259 int dns_resource_record_signer(DnsResourceRecord
*rr
, const char **ret
) {
1266 /* Returns the RRset's signer, if it is known. */
1268 if (rr
->n_skip_labels_signer
== (unsigned) -1)
1271 n
= dns_resource_key_name(rr
->key
);
1272 r
= dns_name_skip(n
, rr
->n_skip_labels_signer
, &n
);
1282 int dns_resource_record_source(DnsResourceRecord
*rr
, const char **ret
) {
1289 /* Returns the RRset's synthesizing source, if it is known. */
1291 if (rr
->n_skip_labels_source
== (unsigned) -1)
1294 n
= dns_resource_key_name(rr
->key
);
1295 r
= dns_name_skip(n
, rr
->n_skip_labels_source
, &n
);
1305 int dns_resource_record_is_signer(DnsResourceRecord
*rr
, const char *zone
) {
1311 r
= dns_resource_record_signer(rr
, &signer
);
1315 return dns_name_equal(zone
, signer
);
1318 int dns_resource_record_is_synthetic(DnsResourceRecord
*rr
) {
1323 /* Returns > 0 if the RR is generated from a wildcard, and is not the asterisk name itself */
1325 if (rr
->n_skip_labels_source
== (unsigned) -1)
1328 if (rr
->n_skip_labels_source
== 0)
1331 if (rr
->n_skip_labels_source
> 1)
1334 r
= dns_name_startswith(dns_resource_key_name(rr
->key
), "*");
1341 void dns_resource_record_hash_func(const DnsResourceRecord
*rr
, struct siphash
*state
) {
1344 dns_resource_key_hash_func(rr
->key
, state
);
1346 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
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
);
1357 case DNS_TYPE_CNAME
:
1358 case DNS_TYPE_DNAME
:
1359 dns_name_hash_func(rr
->ptr
.name
, state
);
1362 case DNS_TYPE_HINFO
:
1363 string_hash_func(rr
->hinfo
.cpu
, state
);
1364 string_hash_func(rr
->hinfo
.os
, state
);
1368 case DNS_TYPE_SPF
: {
1371 LIST_FOREACH(items
, j
, rr
->txt
.items
) {
1372 siphash24_compress_safe(j
->data
, j
->length
, state
);
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
);
1382 siphash24_compress(&rr
->a
.in_addr
, sizeof(rr
->a
.in_addr
), state
);
1386 siphash24_compress(&rr
->aaaa
.in6_addr
, sizeof(rr
->aaaa
.in6_addr
), state
);
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
);
1400 siphash24_compress(&rr
->mx
.priority
, sizeof(rr
->mx
.priority
), state
);
1401 dns_name_hash_func(rr
->mx
.exchange
, state
);
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
);
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_safe(rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, state
);
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_safe(rr
->dnskey
.key
, rr
->dnskey
.key_size
, state
);
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_safe(rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, state
);
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
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_safe(rr
->ds
.digest
, rr
->ds
.digest_size
, state
);
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_safe(rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, state
);
1458 siphash24_compress_safe(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, state
);
1459 /* FIXME: We leave the bitmaps out */
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
);
1466 siphash24_compress_safe(rr
->tlsa
.data
, rr
->tlsa
.data_size
, state
);
1470 siphash24_compress(&rr
->caa
.flags
, sizeof(rr
->caa
.flags
), state
);
1471 string_hash_func(rr
->caa
.tag
, state
);
1472 siphash24_compress_safe(rr
->caa
.value
, rr
->caa
.value_size
, state
);
1475 case DNS_TYPE_OPENPGPKEY
:
1477 siphash24_compress_safe(rr
->generic
.data
, rr
->generic
.data_size
, state
);
1482 int dns_resource_record_compare_func(const DnsResourceRecord
*x
, const DnsResourceRecord
*y
) {
1485 r
= dns_resource_key_compare_func(x
->key
, y
->key
);
1489 if (dns_resource_record_payload_equal(x
, y
) > 0)
1492 /* We still use CMP() here, even though don't implement proper
1493 * ordering, since the hashtable doesn't need ordering anyway. */
1497 DEFINE_HASH_OPS(dns_resource_record_hash_ops
, DnsResourceRecord
, dns_resource_record_hash_func
, dns_resource_record_compare_func
);
1499 DnsResourceRecord
*dns_resource_record_copy(DnsResourceRecord
*rr
) {
1500 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*copy
= NULL
;
1501 DnsResourceRecord
*t
;
1505 copy
= dns_resource_record_new(rr
->key
);
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
->unparsable
= rr
->unparsable
;
1515 switch (rr
->unparsable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
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
)
1528 case DNS_TYPE_CNAME
:
1529 case DNS_TYPE_DNAME
:
1530 copy
->ptr
.name
= strdup(rr
->ptr
.name
);
1531 if (!copy
->ptr
.name
)
1535 case DNS_TYPE_HINFO
:
1536 copy
->hinfo
.cpu
= strdup(rr
->hinfo
.cpu
);
1537 if (!copy
->hinfo
.cpu
)
1540 copy
->hinfo
.os
= strdup(rr
->hinfo
.os
);
1541 if (!copy
->hinfo
.os
)
1547 copy
->txt
.items
= dns_txt_item_copy(rr
->txt
.items
);
1548 if (!copy
->txt
.items
)
1557 copy
->aaaa
= rr
->aaaa
;
1561 copy
->soa
.mname
= strdup(rr
->soa
.mname
);
1562 if (!copy
->soa
.mname
)
1564 copy
->soa
.rname
= strdup(rr
->soa
.rname
);
1565 if (!copy
->soa
.rname
)
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
;
1575 copy
->mx
.priority
= rr
->mx
.priority
;
1576 copy
->mx
.exchange
= strdup(rr
->mx
.exchange
);
1577 if (!copy
->mx
.exchange
)
1582 copy
->loc
= rr
->loc
;
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
)
1591 copy
->sshfp
.fingerprint_size
= rr
->sshfp
.fingerprint_size
;
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
)
1601 copy
->dnskey
.key_size
= rr
->dnskey
.key_size
;
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
)
1615 copy
->rrsig
.signature
= memdup(rr
->rrsig
.signature
, rr
->rrsig
.signature_size
);
1616 if (!copy
->rrsig
.signature
)
1618 copy
->rrsig
.signature_size
= rr
->rrsig
.signature_size
;
1622 copy
->nsec
.next_domain_name
= strdup(rr
->nsec
.next_domain_name
);
1623 if (!copy
->nsec
.next_domain_name
)
1625 copy
->nsec
.types
= bitmap_copy(rr
->nsec
.types
);
1626 if (!copy
->nsec
.types
)
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
)
1637 copy
->ds
.digest_size
= rr
->ds
.digest_size
;
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
)
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
)
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
)
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
)
1664 copy
->tlsa
.data_size
= rr
->tlsa
.data_size
;
1668 copy
->caa
.flags
= rr
->caa
.flags
;
1669 copy
->caa
.tag
= strdup(rr
->caa
.tag
);
1672 copy
->caa
.value
= memdup(rr
->caa
.value
, rr
->caa
.value_size
);
1673 if (!copy
->caa
.value
)
1675 copy
->caa
.value_size
= rr
->caa
.value_size
;
1680 copy
->generic
.data
= memdup(rr
->generic
.data
, rr
->generic
.data_size
);
1681 if (!copy
->generic
.data
)
1683 copy
->generic
.data_size
= rr
->generic
.data_size
;
1692 int dns_resource_record_clamp_ttl(DnsResourceRecord
**rr
, uint32_t max_ttl
) {
1693 DnsResourceRecord
*old_rr
, *new_rr
;
1699 if (old_rr
->key
->type
== DNS_TYPE_OPT
)
1702 new_ttl
= MIN(old_rr
->ttl
, max_ttl
);
1703 if (new_ttl
== old_rr
->ttl
)
1706 if (old_rr
->n_ref
== 1) {
1707 /* Patch in place */
1708 old_rr
->ttl
= new_ttl
;
1712 new_rr
= dns_resource_record_copy(old_rr
);
1716 new_rr
->ttl
= new_ttl
;
1718 dns_resource_record_unref(*rr
);
1724 bool dns_resource_record_is_link_local_address(DnsResourceRecord
*rr
) {
1727 if (rr
->key
->class != DNS_CLASS_IN
)
1730 if (rr
->key
->type
== DNS_TYPE_A
)
1731 return in4_addr_is_link_local(&rr
->a
.in_addr
);
1733 if (rr
->key
->type
== DNS_TYPE_AAAA
)
1734 return in6_addr_is_link_local(&rr
->aaaa
.in6_addr
);
1739 int dns_resource_record_get_cname_target(DnsResourceKey
*key
, DnsResourceRecord
*cname
, char **ret
) {
1740 _cleanup_free_
char *d
= NULL
;
1746 if (key
->class != cname
->key
->class && key
->class != DNS_CLASS_ANY
)
1749 if (cname
->key
->type
== DNS_TYPE_CNAME
) {
1750 r
= dns_name_equal(dns_resource_key_name(key
),
1751 dns_resource_key_name(cname
->key
));
1755 return -EUNATCH
; /* CNAME RR key doesn't actually match the original key */
1757 d
= strdup(cname
->cname
.name
);
1761 } else if (cname
->key
->type
== DNS_TYPE_DNAME
) {
1763 r
= dns_name_change_suffix(
1764 dns_resource_key_name(key
),
1765 dns_resource_key_name(cname
->key
),
1771 return -EUNATCH
; /* DNAME RR key doesn't actually match the original key */
1774 return -EUNATCH
; /* Not a CNAME/DNAME RR, hence doesn't match the proposition either */
1780 DnsTxtItem
*dns_txt_item_free_all(DnsTxtItem
*i
) {
1789 return dns_txt_item_free_all(n
);
1792 bool dns_txt_item_equal(DnsTxtItem
*a
, DnsTxtItem
*b
) {
1803 if (a
->length
!= b
->length
)
1806 if (memcmp(a
->data
, b
->data
, a
->length
) != 0)
1809 return dns_txt_item_equal(a
->items_next
, b
->items_next
);
1812 DnsTxtItem
*dns_txt_item_copy(DnsTxtItem
*first
) {
1813 DnsTxtItem
*i
, *copy
= NULL
, *end
= NULL
;
1815 LIST_FOREACH(items
, i
, first
) {
1818 j
= memdup(i
, offsetof(DnsTxtItem
, data
) + i
->length
+ 1);
1820 dns_txt_item_free_all(copy
);
1824 LIST_INSERT_AFTER(items
, copy
, end
, j
);
1831 int dns_txt_item_new_empty(DnsTxtItem
**ret
) {
1834 /* RFC 6763, section 6.1 suggests to treat
1835 * empty TXT RRs as equivalent to a TXT record
1836 * with a single empty string. */
1838 i
= malloc0(offsetof(DnsTxtItem
, data
) + 1); /* for safety reasons we add an extra NUL byte */
1847 static const char* const dnssec_algorithm_table
[_DNSSEC_ALGORITHM_MAX_DEFINED
] = {
1848 /* Mnemonics as listed on https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml */
1849 [DNSSEC_ALGORITHM_RSAMD5
] = "RSAMD5",
1850 [DNSSEC_ALGORITHM_DH
] = "DH",
1851 [DNSSEC_ALGORITHM_DSA
] = "DSA",
1852 [DNSSEC_ALGORITHM_ECC
] = "ECC",
1853 [DNSSEC_ALGORITHM_RSASHA1
] = "RSASHA1",
1854 [DNSSEC_ALGORITHM_DSA_NSEC3_SHA1
] = "DSA-NSEC3-SHA1",
1855 [DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
] = "RSASHA1-NSEC3-SHA1",
1856 [DNSSEC_ALGORITHM_RSASHA256
] = "RSASHA256",
1857 [DNSSEC_ALGORITHM_RSASHA512
] = "RSASHA512",
1858 [DNSSEC_ALGORITHM_ECC_GOST
] = "ECC-GOST",
1859 [DNSSEC_ALGORITHM_ECDSAP256SHA256
] = "ECDSAP256SHA256",
1860 [DNSSEC_ALGORITHM_ECDSAP384SHA384
] = "ECDSAP384SHA384",
1861 [DNSSEC_ALGORITHM_ED25519
] = "ED25519",
1862 [DNSSEC_ALGORITHM_ED448
] = "ED448",
1863 [DNSSEC_ALGORITHM_INDIRECT
] = "INDIRECT",
1864 [DNSSEC_ALGORITHM_PRIVATEDNS
] = "PRIVATEDNS",
1865 [DNSSEC_ALGORITHM_PRIVATEOID
] = "PRIVATEOID",
1867 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_algorithm
, int, 255);
1869 static const char* const dnssec_digest_table
[_DNSSEC_DIGEST_MAX_DEFINED
] = {
1870 /* Names as listed on https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml */
1871 [DNSSEC_DIGEST_SHA1
] = "SHA-1",
1872 [DNSSEC_DIGEST_SHA256
] = "SHA-256",
1873 [DNSSEC_DIGEST_GOST_R_34_11_94
] = "GOST_R_34.11-94",
1874 [DNSSEC_DIGEST_SHA384
] = "SHA-384",
1876 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(dnssec_digest
, int, 255);