2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "dns-domain.h"
22 #include "resolved-dns-packet.h"
23 #include "string-table.h"
25 #include "unaligned.h"
29 #define EDNS0_OPT_DO (1<<15)
31 typedef struct DnsPacketRewinder
{
36 static void rewind_dns_packet(DnsPacketRewinder
*rewinder
) {
38 dns_packet_rewind(rewinder
->packet
, rewinder
->saved_rindex
);
41 #define INIT_REWINDER(rewinder, p) do { rewinder.packet = p; rewinder.saved_rindex = p->rindex; } while (0)
42 #define CANCEL_REWINDER(rewinder) do { rewinder.packet = NULL; } while (0)
44 int dns_packet_new(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
) {
50 if (mtu
<= UDP_PACKET_HEADER_SIZE
)
51 a
= DNS_PACKET_SIZE_START
;
53 a
= mtu
- UDP_PACKET_HEADER_SIZE
;
55 if (a
< DNS_PACKET_HEADER_SIZE
)
56 a
= DNS_PACKET_HEADER_SIZE
;
58 /* round up to next page size */
59 a
= PAGE_ALIGN(ALIGN(sizeof(DnsPacket
)) + a
) - ALIGN(sizeof(DnsPacket
));
61 /* make sure we never allocate more than useful */
62 if (a
> DNS_PACKET_SIZE_MAX
)
63 a
= DNS_PACKET_SIZE_MAX
;
65 p
= malloc0(ALIGN(sizeof(DnsPacket
)) + a
);
69 p
->size
= p
->rindex
= DNS_PACKET_HEADER_SIZE
;
71 p
->protocol
= protocol
;
72 p
->opt_start
= p
->opt_size
= (size_t) -1;
80 void dns_packet_set_flags(DnsPacket
*p
, bool dnssec_checking_disabled
, bool truncated
) {
86 h
= DNS_PACKET_HEADER(p
);
89 case DNS_PROTOCOL_LLMNR
:
92 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
103 case DNS_PROTOCOL_MDNS
:
104 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
108 0 /* rd (ask for recursion) */,
118 h
->flags
= htobe16(DNS_PACKET_MAKE_FLAGS(0 /* qr */,
122 1 /* rd (ask for recursion) */,
125 dnssec_checking_disabled
/* cd */,
130 int dns_packet_new_query(DnsPacket
**ret
, DnsProtocol protocol
, size_t mtu
, bool dnssec_checking_disabled
) {
136 r
= dns_packet_new(&p
, protocol
, mtu
);
140 /* Always set the TC bit to 0 initially.
141 * If there are multiple packets later, we'll update the bit shortly before sending.
143 dns_packet_set_flags(p
, dnssec_checking_disabled
, false);
149 DnsPacket
*dns_packet_ref(DnsPacket
*p
) {
154 assert(!p
->on_stack
);
156 assert(p
->n_ref
> 0);
161 static void dns_packet_free(DnsPacket
*p
) {
166 dns_question_unref(p
->question
);
167 dns_answer_unref(p
->answer
);
168 dns_resource_record_unref(p
->opt
);
170 while ((s
= hashmap_steal_first_key(p
->names
)))
172 hashmap_free(p
->names
);
180 DnsPacket
*dns_packet_unref(DnsPacket
*p
) {
184 assert(p
->n_ref
> 0);
186 dns_packet_unref(p
->more
);
196 int dns_packet_validate(DnsPacket
*p
) {
199 if (p
->size
< DNS_PACKET_HEADER_SIZE
)
202 if (p
->size
> DNS_PACKET_SIZE_MAX
)
208 int dns_packet_validate_reply(DnsPacket
*p
) {
213 r
= dns_packet_validate(p
);
217 if (DNS_PACKET_QR(p
) != 1)
220 if (DNS_PACKET_OPCODE(p
) != 0)
223 switch (p
->protocol
) {
225 case DNS_PROTOCOL_LLMNR
:
226 /* RFC 4795, Section 2.1.1. says to discard all replies with QDCOUNT != 1 */
227 if (DNS_PACKET_QDCOUNT(p
) != 1)
232 case DNS_PROTOCOL_MDNS
:
233 /* RFC 6762, Section 18 */
234 if (DNS_PACKET_RCODE(p
) != 0)
246 int dns_packet_validate_query(DnsPacket
*p
) {
251 r
= dns_packet_validate(p
);
255 if (DNS_PACKET_QR(p
) != 0)
258 if (DNS_PACKET_OPCODE(p
) != 0)
261 if (DNS_PACKET_TC(p
))
264 switch (p
->protocol
) {
266 case DNS_PROTOCOL_LLMNR
:
267 /* RFC 4795, Section 2.1.1. says to discard all queries with QDCOUNT != 1 */
268 if (DNS_PACKET_QDCOUNT(p
) != 1)
271 /* RFC 4795, Section 2.1.1. says to discard all queries with ANCOUNT != 0 */
272 if (DNS_PACKET_ANCOUNT(p
) > 0)
275 /* RFC 4795, Section 2.1.1. says to discard all queries with NSCOUNT != 0 */
276 if (DNS_PACKET_NSCOUNT(p
) > 0)
281 case DNS_PROTOCOL_MDNS
:
282 /* RFC 6762, Section 18 */
283 if (DNS_PACKET_AA(p
) != 0 ||
284 DNS_PACKET_RD(p
) != 0 ||
285 DNS_PACKET_RA(p
) != 0 ||
286 DNS_PACKET_AD(p
) != 0 ||
287 DNS_PACKET_CD(p
) != 0 ||
288 DNS_PACKET_RCODE(p
) != 0)
300 static int dns_packet_extend(DnsPacket
*p
, size_t add
, void **ret
, size_t *start
) {
303 if (p
->size
+ add
> p
->allocated
) {
306 a
= PAGE_ALIGN((p
->size
+ add
) * 2);
307 if (a
> DNS_PACKET_SIZE_MAX
)
308 a
= DNS_PACKET_SIZE_MAX
;
310 if (p
->size
+ add
> a
)
316 d
= realloc(p
->_data
, a
);
322 p
->_data
= malloc(a
);
326 memcpy(p
->_data
, (uint8_t*) p
+ ALIGN(sizeof(DnsPacket
)), p
->size
);
327 memzero((uint8_t*) p
->_data
+ p
->size
, a
- p
->size
);
337 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->size
;
343 void dns_packet_truncate(DnsPacket
*p
, size_t sz
) {
353 HASHMAP_FOREACH_KEY(n
, s
, p
->names
, i
) {
355 if (PTR_TO_SIZE(n
) < sz
)
358 hashmap_remove(p
->names
, s
);
365 int dns_packet_append_blob(DnsPacket
*p
, const void *d
, size_t l
, size_t *start
) {
371 r
= dns_packet_extend(p
, l
, &q
, start
);
379 int dns_packet_append_uint8(DnsPacket
*p
, uint8_t v
, size_t *start
) {
385 r
= dns_packet_extend(p
, sizeof(uint8_t), &d
, start
);
389 ((uint8_t*) d
)[0] = v
;
394 int dns_packet_append_uint16(DnsPacket
*p
, uint16_t v
, size_t *start
) {
400 r
= dns_packet_extend(p
, sizeof(uint16_t), &d
, start
);
404 unaligned_write_be16(d
, v
);
409 int dns_packet_append_uint32(DnsPacket
*p
, uint32_t v
, size_t *start
) {
415 r
= dns_packet_extend(p
, sizeof(uint32_t), &d
, start
);
419 unaligned_write_be32(d
, v
);
424 int dns_packet_append_string(DnsPacket
*p
, const char *s
, size_t *start
) {
428 return dns_packet_append_raw_string(p
, s
, strlen(s
), start
);
431 int dns_packet_append_raw_string(DnsPacket
*p
, const void *s
, size_t size
, size_t *start
) {
436 assert(s
|| size
== 0);
441 r
= dns_packet_extend(p
, 1 + size
, &d
, start
);
445 ((uint8_t*) d
)[0] = (uint8_t) size
;
447 memcpy_safe(((uint8_t*) d
) + 1, s
, size
);
452 int dns_packet_append_label(DnsPacket
*p
, const char *d
, size_t l
, bool canonical_candidate
, size_t *start
) {
456 /* Append a label to a packet. Optionally, does this in DNSSEC
457 * canonical form, if this label is marked as a candidate for
458 * it, and the canonical form logic is enabled for the
464 if (l
> DNS_LABEL_MAX
)
467 r
= dns_packet_extend(p
, 1 + l
, (void**) &w
, start
);
471 *(w
++) = (uint8_t) l
;
473 if (p
->canonical_form
&& canonical_candidate
) {
476 /* Generate in canonical form, as defined by DNSSEC
477 * RFC 4034, Section 6.2, i.e. all lower-case. */
479 for (i
= 0; i
< l
; i
++)
480 w
[i
] = (uint8_t) ascii_tolower(d
[i
]);
482 /* Otherwise, just copy the string unaltered. This is
483 * essential for DNS-SD, where the casing of labels
484 * matters and needs to be retained. */
490 int dns_packet_append_name(
493 bool allow_compression
,
494 bool canonical_candidate
,
503 if (p
->refuse_compression
)
504 allow_compression
= false;
506 saved_size
= p
->size
;
508 while (!dns_name_is_root(name
)) {
509 const char *z
= name
;
510 char label
[DNS_LABEL_MAX
];
513 if (allow_compression
)
514 n
= PTR_TO_SIZE(hashmap_get(p
->names
, name
));
519 r
= dns_packet_append_uint16(p
, 0xC000 | n
, NULL
);
527 r
= dns_label_unescape(&name
, label
, sizeof(label
));
531 r
= dns_packet_append_label(p
, label
, r
, canonical_candidate
, &n
);
535 if (allow_compression
) {
536 _cleanup_free_
char *s
= NULL
;
544 r
= hashmap_ensure_allocated(&p
->names
, &dns_name_hash_ops
);
548 r
= hashmap_put(p
->names
, s
, SIZE_TO_PTR(n
));
556 r
= dns_packet_append_uint8(p
, 0, NULL
);
567 dns_packet_truncate(p
, saved_size
);
571 int dns_packet_append_key(DnsPacket
*p
, const DnsResourceKey
*k
, size_t *start
) {
578 saved_size
= p
->size
;
580 r
= dns_packet_append_name(p
, dns_resource_key_name(k
), true, true, NULL
);
584 r
= dns_packet_append_uint16(p
, k
->type
, NULL
);
588 r
= dns_packet_append_uint16(p
, k
->class, NULL
);
598 dns_packet_truncate(p
, saved_size
);
602 static int dns_packet_append_type_window(DnsPacket
*p
, uint8_t window
, uint8_t length
, const uint8_t *types
, size_t *start
) {
610 saved_size
= p
->size
;
612 r
= dns_packet_append_uint8(p
, window
, NULL
);
616 r
= dns_packet_append_uint8(p
, length
, NULL
);
620 r
= dns_packet_append_blob(p
, types
, length
, NULL
);
629 dns_packet_truncate(p
, saved_size
);
633 static int dns_packet_append_types(DnsPacket
*p
, Bitmap
*types
, size_t *start
) {
637 uint8_t bitmaps
[32] = {};
644 saved_size
= p
->size
;
646 BITMAP_FOREACH(n
, types
, i
) {
649 if ((n
>> 8) != window
&& bitmaps
[entry
/ 8] != 0) {
650 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
660 bitmaps
[entry
/ 8] |= 1 << (7 - (entry
% 8));
663 if (bitmaps
[entry
/ 8] != 0) {
664 r
= dns_packet_append_type_window(p
, window
, entry
/ 8 + 1, bitmaps
, NULL
);
674 dns_packet_truncate(p
, saved_size
);
678 /* Append the OPT pseudo-RR described in RFC6891 */
679 int dns_packet_append_opt(DnsPacket
*p
, uint16_t max_udp_size
, bool edns0_do
, int rcode
, size_t *start
) {
684 /* we must never advertise supported packet size smaller than the legacy max */
685 assert(max_udp_size
>= DNS_PACKET_UNICAST_SIZE_MAX
);
687 assert(rcode
<= _DNS_RCODE_MAX
);
689 if (p
->opt_start
!= (size_t) -1)
692 assert(p
->opt_size
== (size_t) -1);
694 saved_size
= p
->size
;
697 r
= dns_packet_append_uint8(p
, 0, NULL
);
702 r
= dns_packet_append_uint16(p
, DNS_TYPE_OPT
, NULL
);
706 /* class: maximum udp packet that can be received */
707 r
= dns_packet_append_uint16(p
, max_udp_size
, NULL
);
711 /* extended RCODE and VERSION */
712 r
= dns_packet_append_uint16(p
, ((uint16_t) rcode
& 0x0FF0) << 4, NULL
);
716 /* flags: DNSSEC OK (DO), see RFC3225 */
717 r
= dns_packet_append_uint16(p
, edns0_do
? EDNS0_OPT_DO
: 0, NULL
);
724 /* If DO is on, also append RFC6975 Algorithm data */
726 static const uint8_t rfc6975
[] = {
728 0, 5, /* OPTION_CODE: DAU */
729 0, 6, /* LIST_LENGTH */
730 DNSSEC_ALGORITHM_RSASHA1
,
731 DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
,
732 DNSSEC_ALGORITHM_RSASHA256
,
733 DNSSEC_ALGORITHM_RSASHA512
,
734 DNSSEC_ALGORITHM_ECDSAP256SHA256
,
735 DNSSEC_ALGORITHM_ECDSAP384SHA384
,
737 0, 6, /* OPTION_CODE: DHU */
738 0, 3, /* LIST_LENGTH */
740 DNSSEC_DIGEST_SHA256
,
741 DNSSEC_DIGEST_SHA384
,
743 0, 7, /* OPTION_CODE: N3U */
744 0, 1, /* LIST_LENGTH */
745 NSEC3_ALGORITHM_SHA1
,
748 r
= dns_packet_append_uint16(p
, sizeof(rfc6975
), NULL
);
752 r
= dns_packet_append_blob(p
, rfc6975
, sizeof(rfc6975
), NULL
);
754 r
= dns_packet_append_uint16(p
, 0, NULL
);
759 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) + 1);
761 p
->opt_start
= saved_size
;
762 p
->opt_size
= p
->size
- saved_size
;
770 dns_packet_truncate(p
, saved_size
);
774 int dns_packet_truncate_opt(DnsPacket
*p
) {
777 if (p
->opt_start
== (size_t) -1) {
778 assert(p
->opt_size
== (size_t) -1);
782 assert(p
->opt_size
!= (size_t) -1);
783 assert(DNS_PACKET_ARCOUNT(p
) > 0);
785 if (p
->opt_start
+ p
->opt_size
!= p
->size
)
788 dns_packet_truncate(p
, p
->opt_start
);
789 DNS_PACKET_HEADER(p
)->arcount
= htobe16(DNS_PACKET_ARCOUNT(p
) - 1);
790 p
->opt_start
= p
->opt_size
= (size_t) -1;
795 int dns_packet_append_rr(DnsPacket
*p
, const DnsResourceRecord
*rr
, size_t *start
, size_t *rdata_start
) {
797 size_t saved_size
, rdlength_offset
, end
, rdlength
, rds
;
803 saved_size
= p
->size
;
805 r
= dns_packet_append_key(p
, rr
->key
, NULL
);
809 r
= dns_packet_append_uint32(p
, rr
->ttl
, NULL
);
813 /* Initially we write 0 here */
814 r
= dns_packet_append_uint16(p
, 0, &rdlength_offset
);
818 rds
= p
->size
- saved_size
;
820 switch (rr
->unparseable
? _DNS_TYPE_INVALID
: rr
->key
->type
) {
823 r
= dns_packet_append_uint16(p
, rr
->srv
.priority
, NULL
);
827 r
= dns_packet_append_uint16(p
, rr
->srv
.weight
, NULL
);
831 r
= dns_packet_append_uint16(p
, rr
->srv
.port
, NULL
);
835 r
= dns_packet_append_name(p
, rr
->srv
.name
, true, false, NULL
);
842 r
= dns_packet_append_name(p
, rr
->ptr
.name
, true, false, NULL
);
846 r
= dns_packet_append_string(p
, rr
->hinfo
.cpu
, NULL
);
850 r
= dns_packet_append_string(p
, rr
->hinfo
.os
, NULL
);
853 case DNS_TYPE_SPF
: /* exactly the same as TXT */
856 if (!rr
->txt
.items
) {
857 /* RFC 6763, section 6.1 suggests to generate
858 * single empty string for an empty array. */
860 r
= dns_packet_append_raw_string(p
, NULL
, 0, NULL
);
866 LIST_FOREACH(items
, i
, rr
->txt
.items
) {
867 r
= dns_packet_append_raw_string(p
, i
->data
, i
->length
, NULL
);
877 r
= dns_packet_append_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
881 r
= dns_packet_append_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
885 r
= dns_packet_append_name(p
, rr
->soa
.mname
, true, false, NULL
);
889 r
= dns_packet_append_name(p
, rr
->soa
.rname
, true, false, NULL
);
893 r
= dns_packet_append_uint32(p
, rr
->soa
.serial
, NULL
);
897 r
= dns_packet_append_uint32(p
, rr
->soa
.refresh
, NULL
);
901 r
= dns_packet_append_uint32(p
, rr
->soa
.retry
, NULL
);
905 r
= dns_packet_append_uint32(p
, rr
->soa
.expire
, NULL
);
909 r
= dns_packet_append_uint32(p
, rr
->soa
.minimum
, NULL
);
913 r
= dns_packet_append_uint16(p
, rr
->mx
.priority
, NULL
);
917 r
= dns_packet_append_name(p
, rr
->mx
.exchange
, true, false, NULL
);
921 r
= dns_packet_append_uint8(p
, rr
->loc
.version
, NULL
);
925 r
= dns_packet_append_uint8(p
, rr
->loc
.size
, NULL
);
929 r
= dns_packet_append_uint8(p
, rr
->loc
.horiz_pre
, NULL
);
933 r
= dns_packet_append_uint8(p
, rr
->loc
.vert_pre
, NULL
);
937 r
= dns_packet_append_uint32(p
, rr
->loc
.latitude
, NULL
);
941 r
= dns_packet_append_uint32(p
, rr
->loc
.longitude
, NULL
);
945 r
= dns_packet_append_uint32(p
, rr
->loc
.altitude
, NULL
);
949 r
= dns_packet_append_uint16(p
, rr
->ds
.key_tag
, NULL
);
953 r
= dns_packet_append_uint8(p
, rr
->ds
.algorithm
, NULL
);
957 r
= dns_packet_append_uint8(p
, rr
->ds
.digest_type
, NULL
);
961 r
= dns_packet_append_blob(p
, rr
->ds
.digest
, rr
->ds
.digest_size
, NULL
);
965 r
= dns_packet_append_uint8(p
, rr
->sshfp
.algorithm
, NULL
);
969 r
= dns_packet_append_uint8(p
, rr
->sshfp
.fptype
, NULL
);
973 r
= dns_packet_append_blob(p
, rr
->sshfp
.fingerprint
, rr
->sshfp
.fingerprint_size
, NULL
);
976 case DNS_TYPE_DNSKEY
:
977 r
= dns_packet_append_uint16(p
, rr
->dnskey
.flags
, NULL
);
981 r
= dns_packet_append_uint8(p
, rr
->dnskey
.protocol
, NULL
);
985 r
= dns_packet_append_uint8(p
, rr
->dnskey
.algorithm
, NULL
);
989 r
= dns_packet_append_blob(p
, rr
->dnskey
.key
, rr
->dnskey
.key_size
, NULL
);
993 r
= dns_packet_append_uint16(p
, rr
->rrsig
.type_covered
, NULL
);
997 r
= dns_packet_append_uint8(p
, rr
->rrsig
.algorithm
, NULL
);
1001 r
= dns_packet_append_uint8(p
, rr
->rrsig
.labels
, NULL
);
1005 r
= dns_packet_append_uint32(p
, rr
->rrsig
.original_ttl
, NULL
);
1009 r
= dns_packet_append_uint32(p
, rr
->rrsig
.expiration
, NULL
);
1013 r
= dns_packet_append_uint32(p
, rr
->rrsig
.inception
, NULL
);
1017 r
= dns_packet_append_uint16(p
, rr
->rrsig
.key_tag
, NULL
);
1021 r
= dns_packet_append_name(p
, rr
->rrsig
.signer
, false, true, NULL
);
1025 r
= dns_packet_append_blob(p
, rr
->rrsig
.signature
, rr
->rrsig
.signature_size
, NULL
);
1029 r
= dns_packet_append_name(p
, rr
->nsec
.next_domain_name
, false, false, NULL
);
1033 r
= dns_packet_append_types(p
, rr
->nsec
.types
, NULL
);
1039 case DNS_TYPE_NSEC3
:
1040 r
= dns_packet_append_uint8(p
, rr
->nsec3
.algorithm
, NULL
);
1044 r
= dns_packet_append_uint8(p
, rr
->nsec3
.flags
, NULL
);
1048 r
= dns_packet_append_uint16(p
, rr
->nsec3
.iterations
, NULL
);
1052 r
= dns_packet_append_uint8(p
, rr
->nsec3
.salt_size
, NULL
);
1056 r
= dns_packet_append_blob(p
, rr
->nsec3
.salt
, rr
->nsec3
.salt_size
, NULL
);
1060 r
= dns_packet_append_uint8(p
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1064 r
= dns_packet_append_blob(p
, rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, NULL
);
1068 r
= dns_packet_append_types(p
, rr
->nsec3
.types
, NULL
);
1075 r
= dns_packet_append_uint8(p
, rr
->tlsa
.cert_usage
, NULL
);
1079 r
= dns_packet_append_uint8(p
, rr
->tlsa
.selector
, NULL
);
1083 r
= dns_packet_append_uint8(p
, rr
->tlsa
.matching_type
, NULL
);
1087 r
= dns_packet_append_blob(p
, rr
->tlsa
.data
, rr
->tlsa
.data_size
, NULL
);
1091 r
= dns_packet_append_uint8(p
, rr
->caa
.flags
, NULL
);
1095 r
= dns_packet_append_string(p
, rr
->caa
.tag
, NULL
);
1099 r
= dns_packet_append_blob(p
, rr
->caa
.value
, rr
->caa
.value_size
, NULL
);
1103 case DNS_TYPE_OPENPGPKEY
:
1104 case _DNS_TYPE_INVALID
: /* unparseable */
1107 r
= dns_packet_append_blob(p
, rr
->generic
.data
, rr
->generic
.data_size
, NULL
);
1113 /* Let's calculate the actual data size and update the field */
1114 rdlength
= p
->size
- rdlength_offset
- sizeof(uint16_t);
1115 if (rdlength
> 0xFFFF) {
1121 p
->size
= rdlength_offset
;
1122 r
= dns_packet_append_uint16(p
, rdlength
, NULL
);
1128 *start
= saved_size
;
1136 dns_packet_truncate(p
, saved_size
);
1140 int dns_packet_append_question(DnsPacket
*p
, DnsQuestion
*q
) {
1141 DnsResourceKey
*key
;
1146 DNS_QUESTION_FOREACH(key
, q
) {
1147 r
= dns_packet_append_key(p
, key
, NULL
);
1155 int dns_packet_append_answer(DnsPacket
*p
, DnsAnswer
*a
) {
1156 DnsResourceRecord
*rr
;
1161 DNS_ANSWER_FOREACH(rr
, a
) {
1162 r
= dns_packet_append_rr(p
, rr
, NULL
, NULL
);
1170 int dns_packet_read(DnsPacket
*p
, size_t sz
, const void **ret
, size_t *start
) {
1173 if (p
->rindex
+ sz
> p
->size
)
1177 *ret
= (uint8_t*) DNS_PACKET_DATA(p
) + p
->rindex
;
1186 void dns_packet_rewind(DnsPacket
*p
, size_t idx
) {
1188 assert(idx
<= p
->size
);
1189 assert(idx
>= DNS_PACKET_HEADER_SIZE
);
1194 int dns_packet_read_blob(DnsPacket
*p
, void *d
, size_t sz
, size_t *start
) {
1201 r
= dns_packet_read(p
, sz
, &q
, start
);
1209 static int dns_packet_read_memdup(
1210 DnsPacket
*p
, size_t size
,
1211 void **ret
, size_t *ret_size
,
1212 size_t *ret_start
) {
1221 r
= dns_packet_read(p
, size
, &src
, &start
);
1230 copy
= memdup(src
, size
);
1245 int dns_packet_read_uint8(DnsPacket
*p
, uint8_t *ret
, size_t *start
) {
1251 r
= dns_packet_read(p
, sizeof(uint8_t), &d
, start
);
1255 *ret
= ((uint8_t*) d
)[0];
1259 int dns_packet_read_uint16(DnsPacket
*p
, uint16_t *ret
, size_t *start
) {
1265 r
= dns_packet_read(p
, sizeof(uint16_t), &d
, start
);
1269 *ret
= unaligned_read_be16(d
);
1274 int dns_packet_read_uint32(DnsPacket
*p
, uint32_t *ret
, size_t *start
) {
1280 r
= dns_packet_read(p
, sizeof(uint32_t), &d
, start
);
1284 *ret
= unaligned_read_be32(d
);
1289 int dns_packet_read_string(DnsPacket
*p
, char **ret
, size_t *start
) {
1290 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1297 INIT_REWINDER(rewinder
, p
);
1299 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1303 r
= dns_packet_read(p
, c
, &d
, NULL
);
1307 if (memchr(d
, 0, c
))
1314 if (!utf8_is_valid(t
)) {
1322 *start
= rewinder
.saved_rindex
;
1323 CANCEL_REWINDER(rewinder
);
1328 int dns_packet_read_raw_string(DnsPacket
*p
, const void **ret
, size_t *size
, size_t *start
) {
1329 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1334 INIT_REWINDER(rewinder
, p
);
1336 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1340 r
= dns_packet_read(p
, c
, ret
, NULL
);
1347 *start
= rewinder
.saved_rindex
;
1348 CANCEL_REWINDER(rewinder
);
1353 int dns_packet_read_name(
1356 bool allow_compression
,
1359 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1360 size_t after_rindex
= 0, jump_barrier
;
1361 _cleanup_free_
char *ret
= NULL
;
1362 size_t n
= 0, allocated
= 0;
1368 INIT_REWINDER(rewinder
, p
);
1369 jump_barrier
= p
->rindex
;
1371 if (p
->refuse_compression
)
1372 allow_compression
= false;
1377 r
= dns_packet_read_uint8(p
, &c
, NULL
);
1388 r
= dns_packet_read(p
, c
, (const void**) &label
, NULL
);
1392 if (!GREEDY_REALLOC(ret
, allocated
, n
+ !first
+ DNS_LABEL_ESCAPED_MAX
))
1400 r
= dns_label_escape(label
, c
, ret
+ n
, DNS_LABEL_ESCAPED_MAX
);
1406 } else if (allow_compression
&& (c
& 0xc0) == 0xc0) {
1410 r
= dns_packet_read_uint8(p
, &d
, NULL
);
1414 ptr
= (uint16_t) (c
& ~0xc0) << 8 | (uint16_t) d
;
1415 if (ptr
< DNS_PACKET_HEADER_SIZE
|| ptr
>= jump_barrier
)
1418 if (after_rindex
== 0)
1419 after_rindex
= p
->rindex
;
1421 /* Jumps are limited to a "prior occurrence" (RFC-1035 4.1.4) */
1428 if (!GREEDY_REALLOC(ret
, allocated
, n
+ 1))
1433 if (after_rindex
!= 0)
1434 p
->rindex
= after_rindex
;
1440 *start
= rewinder
.saved_rindex
;
1441 CANCEL_REWINDER(rewinder
);
1446 static int dns_packet_read_type_window(DnsPacket
*p
, Bitmap
**types
, size_t *start
) {
1449 const uint8_t *bitmap
;
1453 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1458 INIT_REWINDER(rewinder
, p
);
1460 r
= bitmap_ensure_allocated(types
);
1464 r
= dns_packet_read_uint8(p
, &window
, NULL
);
1468 r
= dns_packet_read_uint8(p
, &length
, NULL
);
1472 if (length
== 0 || length
> 32)
1475 r
= dns_packet_read(p
, length
, (const void **)&bitmap
, NULL
);
1479 for (i
= 0; i
< length
; i
++) {
1480 uint8_t bitmask
= 1 << 7;
1491 if (bitmap
[i
] & bitmask
) {
1494 n
= (uint16_t) window
<< 8 | (uint16_t) bit
;
1496 /* Ignore pseudo-types. see RFC4034 section 4.1.2 */
1497 if (dns_type_is_pseudo(n
))
1500 r
= bitmap_set(*types
, n
);
1514 *start
= rewinder
.saved_rindex
;
1515 CANCEL_REWINDER(rewinder
);
1520 static int dns_packet_read_type_windows(DnsPacket
*p
, Bitmap
**types
, size_t size
, size_t *start
) {
1521 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1524 INIT_REWINDER(rewinder
, p
);
1526 while (p
->rindex
< rewinder
.saved_rindex
+ size
) {
1527 r
= dns_packet_read_type_window(p
, types
, NULL
);
1531 /* don't read past end of current RR */
1532 if (p
->rindex
> rewinder
.saved_rindex
+ size
)
1536 if (p
->rindex
!= rewinder
.saved_rindex
+ size
)
1540 *start
= rewinder
.saved_rindex
;
1541 CANCEL_REWINDER(rewinder
);
1546 int dns_packet_read_key(DnsPacket
*p
, DnsResourceKey
**ret
, bool *ret_cache_flush
, size_t *start
) {
1547 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1548 _cleanup_free_
char *name
= NULL
;
1549 bool cache_flush
= false;
1550 uint16_t class, type
;
1551 DnsResourceKey
*key
;
1556 INIT_REWINDER(rewinder
, p
);
1558 r
= dns_packet_read_name(p
, &name
, true, NULL
);
1562 r
= dns_packet_read_uint16(p
, &type
, NULL
);
1566 r
= dns_packet_read_uint16(p
, &class, NULL
);
1570 if (p
->protocol
== DNS_PROTOCOL_MDNS
) {
1571 /* See RFC6762, Section 10.2 */
1573 if (type
!= DNS_TYPE_OPT
&& (class & MDNS_RR_CACHE_FLUSH
)) {
1574 class &= ~MDNS_RR_CACHE_FLUSH
;
1579 key
= dns_resource_key_new_consume(class, type
, name
);
1586 if (ret_cache_flush
)
1587 *ret_cache_flush
= cache_flush
;
1589 *start
= rewinder
.saved_rindex
;
1590 CANCEL_REWINDER(rewinder
);
1595 static bool loc_size_ok(uint8_t size
) {
1596 uint8_t m
= size
>> 4, e
= size
& 0xF;
1598 return m
<= 9 && e
<= 9 && (m
> 0 || e
== 0);
1601 int dns_packet_read_rr(DnsPacket
*p
, DnsResourceRecord
**ret
, bool *ret_cache_flush
, size_t *start
) {
1602 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
1603 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
1604 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
;
1613 INIT_REWINDER(rewinder
, p
);
1615 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
1619 if (!dns_class_is_valid_rr(key
->class) || !dns_type_is_valid_rr(key
->type
))
1622 rr
= dns_resource_record_new(key
);
1626 r
= dns_packet_read_uint32(p
, &rr
->ttl
, NULL
);
1630 /* RFC 2181, Section 8, suggests to
1631 * treat a TTL with the MSB set as a zero TTL. */
1632 if (rr
->ttl
& UINT32_C(0x80000000))
1635 r
= dns_packet_read_uint16(p
, &rdlength
, NULL
);
1639 if (p
->rindex
+ rdlength
> p
->size
)
1644 switch (rr
->key
->type
) {
1647 r
= dns_packet_read_uint16(p
, &rr
->srv
.priority
, NULL
);
1650 r
= dns_packet_read_uint16(p
, &rr
->srv
.weight
, NULL
);
1653 r
= dns_packet_read_uint16(p
, &rr
->srv
.port
, NULL
);
1656 r
= dns_packet_read_name(p
, &rr
->srv
.name
, true, NULL
);
1661 case DNS_TYPE_CNAME
:
1662 case DNS_TYPE_DNAME
:
1663 r
= dns_packet_read_name(p
, &rr
->ptr
.name
, true, NULL
);
1666 case DNS_TYPE_HINFO
:
1667 r
= dns_packet_read_string(p
, &rr
->hinfo
.cpu
, NULL
);
1671 r
= dns_packet_read_string(p
, &rr
->hinfo
.os
, NULL
);
1674 case DNS_TYPE_SPF
: /* exactly the same as TXT */
1676 if (rdlength
<= 0) {
1678 /* RFC 6763, section 6.1 suggests to treat
1679 * empty TXT RRs as equivalent to a TXT record
1680 * with a single empty string. */
1682 i
= malloc0(offsetof(DnsTxtItem
, data
) + 1); /* for safety reasons we add an extra NUL byte */
1688 DnsTxtItem
*last
= NULL
;
1690 while (p
->rindex
< offset
+ rdlength
) {
1695 r
= dns_packet_read_raw_string(p
, &data
, &sz
, NULL
);
1699 i
= malloc0(offsetof(DnsTxtItem
, data
) + sz
+ 1); /* extra NUL byte at the end */
1703 memcpy(i
->data
, data
, sz
);
1706 LIST_INSERT_AFTER(items
, rr
->txt
.items
, last
, i
);
1715 r
= dns_packet_read_blob(p
, &rr
->a
.in_addr
, sizeof(struct in_addr
), NULL
);
1719 r
= dns_packet_read_blob(p
, &rr
->aaaa
.in6_addr
, sizeof(struct in6_addr
), NULL
);
1723 r
= dns_packet_read_name(p
, &rr
->soa
.mname
, true, NULL
);
1727 r
= dns_packet_read_name(p
, &rr
->soa
.rname
, true, NULL
);
1731 r
= dns_packet_read_uint32(p
, &rr
->soa
.serial
, NULL
);
1735 r
= dns_packet_read_uint32(p
, &rr
->soa
.refresh
, NULL
);
1739 r
= dns_packet_read_uint32(p
, &rr
->soa
.retry
, NULL
);
1743 r
= dns_packet_read_uint32(p
, &rr
->soa
.expire
, NULL
);
1747 r
= dns_packet_read_uint32(p
, &rr
->soa
.minimum
, NULL
);
1751 r
= dns_packet_read_uint16(p
, &rr
->mx
.priority
, NULL
);
1755 r
= dns_packet_read_name(p
, &rr
->mx
.exchange
, true, NULL
);
1758 case DNS_TYPE_LOC
: {
1762 r
= dns_packet_read_uint8(p
, &t
, &pos
);
1767 rr
->loc
.version
= t
;
1769 r
= dns_packet_read_uint8(p
, &rr
->loc
.size
, NULL
);
1773 if (!loc_size_ok(rr
->loc
.size
))
1776 r
= dns_packet_read_uint8(p
, &rr
->loc
.horiz_pre
, NULL
);
1780 if (!loc_size_ok(rr
->loc
.horiz_pre
))
1783 r
= dns_packet_read_uint8(p
, &rr
->loc
.vert_pre
, NULL
);
1787 if (!loc_size_ok(rr
->loc
.vert_pre
))
1790 r
= dns_packet_read_uint32(p
, &rr
->loc
.latitude
, NULL
);
1794 r
= dns_packet_read_uint32(p
, &rr
->loc
.longitude
, NULL
);
1798 r
= dns_packet_read_uint32(p
, &rr
->loc
.altitude
, NULL
);
1804 dns_packet_rewind(p
, pos
);
1805 rr
->unparseable
= true;
1811 r
= dns_packet_read_uint16(p
, &rr
->ds
.key_tag
, NULL
);
1815 r
= dns_packet_read_uint8(p
, &rr
->ds
.algorithm
, NULL
);
1819 r
= dns_packet_read_uint8(p
, &rr
->ds
.digest_type
, NULL
);
1823 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1824 &rr
->ds
.digest
, &rr
->ds
.digest_size
,
1829 if (rr
->ds
.digest_size
<= 0)
1830 /* the accepted size depends on the algorithm, but for now
1831 just ensure that the value is greater than zero */
1836 case DNS_TYPE_SSHFP
:
1837 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.algorithm
, NULL
);
1841 r
= dns_packet_read_uint8(p
, &rr
->sshfp
.fptype
, NULL
);
1845 r
= dns_packet_read_memdup(p
, rdlength
- 2,
1846 &rr
->sshfp
.fingerprint
, &rr
->sshfp
.fingerprint_size
,
1849 if (rr
->sshfp
.fingerprint_size
<= 0)
1850 /* the accepted size depends on the algorithm, but for now
1851 just ensure that the value is greater than zero */
1856 case DNS_TYPE_DNSKEY
:
1857 r
= dns_packet_read_uint16(p
, &rr
->dnskey
.flags
, NULL
);
1861 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.protocol
, NULL
);
1865 r
= dns_packet_read_uint8(p
, &rr
->dnskey
.algorithm
, NULL
);
1869 r
= dns_packet_read_memdup(p
, rdlength
- 4,
1870 &rr
->dnskey
.key
, &rr
->dnskey
.key_size
,
1873 if (rr
->dnskey
.key_size
<= 0)
1874 /* the accepted size depends on the algorithm, but for now
1875 just ensure that the value is greater than zero */
1880 case DNS_TYPE_RRSIG
:
1881 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.type_covered
, NULL
);
1885 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.algorithm
, NULL
);
1889 r
= dns_packet_read_uint8(p
, &rr
->rrsig
.labels
, NULL
);
1893 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.original_ttl
, NULL
);
1897 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.expiration
, NULL
);
1901 r
= dns_packet_read_uint32(p
, &rr
->rrsig
.inception
, NULL
);
1905 r
= dns_packet_read_uint16(p
, &rr
->rrsig
.key_tag
, NULL
);
1909 r
= dns_packet_read_name(p
, &rr
->rrsig
.signer
, false, NULL
);
1913 r
= dns_packet_read_memdup(p
, offset
+ rdlength
- p
->rindex
,
1914 &rr
->rrsig
.signature
, &rr
->rrsig
.signature_size
,
1917 if (rr
->rrsig
.signature_size
<= 0)
1918 /* the accepted size depends on the algorithm, but for now
1919 just ensure that the value is greater than zero */
1924 case DNS_TYPE_NSEC
: {
1927 * RFC6762, section 18.14 explictly states mDNS should use name compression.
1928 * This contradicts RFC3845, section 2.1.1
1931 bool allow_compressed
= p
->protocol
== DNS_PROTOCOL_MDNS
;
1933 r
= dns_packet_read_name(p
, &rr
->nsec
.next_domain_name
, allow_compressed
, NULL
);
1937 r
= dns_packet_read_type_windows(p
, &rr
->nsec
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1939 /* We accept empty NSEC bitmaps. The bit indicating the presence of the NSEC record itself
1940 * is redundant and in e.g., RFC4956 this fact is used to define a use for NSEC records
1941 * without the NSEC bit set. */
1945 case DNS_TYPE_NSEC3
: {
1948 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.algorithm
, NULL
);
1952 r
= dns_packet_read_uint8(p
, &rr
->nsec3
.flags
, NULL
);
1956 r
= dns_packet_read_uint16(p
, &rr
->nsec3
.iterations
, NULL
);
1960 /* this may be zero */
1961 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1965 r
= dns_packet_read_memdup(p
, size
, &rr
->nsec3
.salt
, &rr
->nsec3
.salt_size
, NULL
);
1969 r
= dns_packet_read_uint8(p
, &size
, NULL
);
1976 r
= dns_packet_read_memdup(p
, size
,
1977 &rr
->nsec3
.next_hashed_name
, &rr
->nsec3
.next_hashed_name_size
,
1982 r
= dns_packet_read_type_windows(p
, &rr
->nsec3
.types
, offset
+ rdlength
- p
->rindex
, NULL
);
1984 /* empty non-terminals can have NSEC3 records, so empty bitmaps are allowed */
1990 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.cert_usage
, NULL
);
1994 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.selector
, NULL
);
1998 r
= dns_packet_read_uint8(p
, &rr
->tlsa
.matching_type
, NULL
);
2002 r
= dns_packet_read_memdup(p
, rdlength
- 3,
2003 &rr
->tlsa
.data
, &rr
->tlsa
.data_size
,
2006 if (rr
->tlsa
.data_size
<= 0)
2007 /* the accepted size depends on the algorithm, but for now
2008 just ensure that the value is greater than zero */
2014 r
= dns_packet_read_uint8(p
, &rr
->caa
.flags
, NULL
);
2018 r
= dns_packet_read_string(p
, &rr
->caa
.tag
, NULL
);
2022 r
= dns_packet_read_memdup(p
,
2023 rdlength
+ offset
- p
->rindex
,
2024 &rr
->caa
.value
, &rr
->caa
.value_size
, NULL
);
2028 case DNS_TYPE_OPT
: /* we only care about the header of OPT for now. */
2029 case DNS_TYPE_OPENPGPKEY
:
2032 r
= dns_packet_read_memdup(p
, rdlength
, &rr
->generic
.data
, &rr
->generic
.data_size
, NULL
);
2038 if (p
->rindex
!= offset
+ rdlength
)
2044 if (ret_cache_flush
)
2045 *ret_cache_flush
= cache_flush
;
2047 *start
= rewinder
.saved_rindex
;
2048 CANCEL_REWINDER(rewinder
);
2053 static bool opt_is_good(DnsResourceRecord
*rr
, bool *rfc6975
) {
2055 bool found_dau_dhu_n3u
= false;
2058 /* Checks whether the specified OPT RR is well-formed and whether it contains RFC6975 data (which is not OK in
2062 assert(rr
->key
->type
== DNS_TYPE_OPT
);
2064 /* Check that the version is 0 */
2065 if (((rr
->ttl
>> 16) & UINT32_C(0xFF)) != 0)
2069 l
= rr
->opt
.data_size
;
2071 uint16_t option_code
, option_length
;
2073 /* At least four bytes for OPTION-CODE and OPTION-LENGTH are required */
2077 option_code
= unaligned_read_be16(p
);
2078 option_length
= unaligned_read_be16(p
+ 2);
2080 if (l
< option_length
+ 4U)
2083 /* RFC 6975 DAU, DHU or N3U fields found. */
2084 if (IN_SET(option_code
, 5, 6, 7))
2085 found_dau_dhu_n3u
= true;
2087 p
+= option_length
+ 4U;
2088 l
-= option_length
+ 4U;
2091 *rfc6975
= found_dau_dhu_n3u
;
2095 int dns_packet_extract(DnsPacket
*p
) {
2096 _cleanup_(dns_question_unrefp
) DnsQuestion
*question
= NULL
;
2097 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
2098 _cleanup_(rewind_dns_packet
) DnsPacketRewinder rewinder
= {};
2105 INIT_REWINDER(rewinder
, p
);
2106 dns_packet_rewind(p
, DNS_PACKET_HEADER_SIZE
);
2108 n
= DNS_PACKET_QDCOUNT(p
);
2110 question
= dns_question_new(n
);
2114 for (i
= 0; i
< n
; i
++) {
2115 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
2118 r
= dns_packet_read_key(p
, &key
, &cache_flush
, NULL
);
2125 if (!dns_type_is_valid_query(key
->type
))
2128 r
= dns_question_add(question
, key
);
2134 n
= DNS_PACKET_RRCOUNT(p
);
2136 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*previous
= NULL
;
2137 bool bad_opt
= false;
2139 answer
= dns_answer_new(n
);
2143 for (i
= 0; i
< n
; i
++) {
2144 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
2147 r
= dns_packet_read_rr(p
, &rr
, &cache_flush
, NULL
);
2151 /* Try to reduce memory usage a bit */
2153 dns_resource_key_reduce(&rr
->key
, &previous
->key
);
2155 if (rr
->key
->type
== DNS_TYPE_OPT
) {
2158 if (p
->opt
|| bad_opt
) {
2159 /* Multiple OPT RRs? if so, let's ignore all, because there's something wrong
2160 * with the server, and if one is valid we wouldn't know which one. */
2161 log_debug("Multiple OPT RRs detected, ignoring all.");
2166 if (!dns_name_is_root(dns_resource_key_name(rr
->key
))) {
2167 /* If the OPT RR is not owned by the root domain, then it is bad, let's ignore
2169 log_debug("OPT RR is not owned by root domain, ignoring.");
2174 if (i
< DNS_PACKET_ANCOUNT(p
) + DNS_PACKET_NSCOUNT(p
)) {
2175 /* OPT RR is in the wrong section? Some Belkin routers do this. This is a hint
2176 * the EDNS implementation is borked, like the Belkin one is, hence ignore
2178 log_debug("OPT RR in wrong section, ignoring.");
2183 if (!opt_is_good(rr
, &has_rfc6975
)) {
2184 log_debug("Malformed OPT RR, ignoring.");
2190 /* If the OPT RR contains RFC6975 algorithm data, then this is indication that
2191 * the server just copied the OPT it got from us (which contained that data)
2192 * back into the reply. If so, then it doesn't properly support EDNS, as
2193 * RFC6975 makes it very clear that the algorithm data should only be contained
2194 * in questions, never in replies. Crappy Belkin routers copy the OPT data for
2195 * example, hence let's detect this so that we downgrade early. */
2196 log_debug("OPT RR contained RFC6975 data, ignoring.");
2201 p
->opt
= dns_resource_record_ref(rr
);
2204 /* According to RFC 4795, section 2.9. only the RRs from the Answer section shall be
2205 * cached. Hence mark only those RRs as cacheable by default, but not the ones from the
2206 * Additional or Authority sections. */
2208 r
= dns_answer_add(answer
, rr
, p
->ifindex
,
2209 (i
< DNS_PACKET_ANCOUNT(p
) ? DNS_ANSWER_CACHEABLE
: 0) |
2210 (p
->protocol
== DNS_PROTOCOL_MDNS
&& !cache_flush
? DNS_ANSWER_SHARED_OWNER
: 0));
2215 /* Remember this RR, so that we potentically can merge it's ->key object with the next RR. Note
2216 * that we only do this if we actually decided to keep the RR around. */
2217 dns_resource_record_unref(previous
);
2218 previous
= dns_resource_record_ref(rr
);
2222 p
->opt
= dns_resource_record_unref(p
->opt
);
2225 p
->question
= question
;
2231 p
->extracted
= true;
2233 /* no CANCEL, always rewind */
2237 int dns_packet_is_reply_for(DnsPacket
*p
, const DnsResourceKey
*key
) {
2243 /* Checks if the specified packet is a reply for the specified
2244 * key and the specified key is the only one in the question
2247 if (DNS_PACKET_QR(p
) != 1)
2250 /* Let's unpack the packet, if that hasn't happened yet. */
2251 r
= dns_packet_extract(p
);
2255 if (p
->question
->n_keys
!= 1)
2258 return dns_resource_key_equal(p
->question
->keys
[0], key
);
2261 static const char* const dns_rcode_table
[_DNS_RCODE_MAX_DEFINED
] = {
2262 [DNS_RCODE_SUCCESS
] = "SUCCESS",
2263 [DNS_RCODE_FORMERR
] = "FORMERR",
2264 [DNS_RCODE_SERVFAIL
] = "SERVFAIL",
2265 [DNS_RCODE_NXDOMAIN
] = "NXDOMAIN",
2266 [DNS_RCODE_NOTIMP
] = "NOTIMP",
2267 [DNS_RCODE_REFUSED
] = "REFUSED",
2268 [DNS_RCODE_YXDOMAIN
] = "YXDOMAIN",
2269 [DNS_RCODE_YXRRSET
] = "YRRSET",
2270 [DNS_RCODE_NXRRSET
] = "NXRRSET",
2271 [DNS_RCODE_NOTAUTH
] = "NOTAUTH",
2272 [DNS_RCODE_NOTZONE
] = "NOTZONE",
2273 [DNS_RCODE_BADVERS
] = "BADVERS",
2274 [DNS_RCODE_BADKEY
] = "BADKEY",
2275 [DNS_RCODE_BADTIME
] = "BADTIME",
2276 [DNS_RCODE_BADMODE
] = "BADMODE",
2277 [DNS_RCODE_BADNAME
] = "BADNAME",
2278 [DNS_RCODE_BADALG
] = "BADALG",
2279 [DNS_RCODE_BADTRUNC
] = "BADTRUNC",
2281 DEFINE_STRING_TABLE_LOOKUP(dns_rcode
, int);
2283 static const char* const dns_protocol_table
[_DNS_PROTOCOL_MAX
] = {
2284 [DNS_PROTOCOL_DNS
] = "dns",
2285 [DNS_PROTOCOL_MDNS
] = "mdns",
2286 [DNS_PROTOCOL_LLMNR
] = "llmnr",
2288 DEFINE_STRING_TABLE_LOOKUP(dns_protocol
, DnsProtocol
);