~nickbp/originz

ref: d94f181c5fbfdf0f62b40ea46d3f301f4ec681e5 originz/src/codec/message.rs -rw-r--r-- 27.1 KiB
d94f181cNick Parker Backport current benchmark to older code 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#![deny(warnings)]

use anyhow::{bail, Context, Result};
use std::convert::TryFrom;
use std::net::IpAddr;
use std::str;

use bytes::{BufMut, BytesMut};
use packed_struct::prelude::*;

use crate::codec::{domain_name, rdata};
use crate::fbs::dns_enums_generated::ResourceType;
use crate::fbs::dns_message_generated::*;

/// Wire protocol type for OPT records, for use in some match statements.
pub const OPT_RESOURCE_TYPE: u16 = 41;

// Header, from RFC2535 section 6.1 or RFC2929 section 2.
//
//                                 1  1  1  1  1  1
//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                      ID                       |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |QR|   Opcode  |AA|TC|RD|RA| Z|AD|CD|   RCODE   |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                    QDCOUNT                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                    ANCOUNT                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                    NSCOUNT                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                    ARCOUNT                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct HeaderBits {
    #[packed_field(bits = "0:15")]
    pub id: u16,

    #[packed_field(bits = "16:16")]
    pub is_response: bool,
    #[packed_field(bits = "17:20")]
    pub op_code: Integer<u8, packed_bits::Bits4>,
    #[packed_field(bits = "21:21")]
    pub authoritative: bool,
    #[packed_field(bits = "22:22")]
    pub truncated: bool,
    #[packed_field(bits = "23:23")]
    pub recursion_desired: bool,
    #[packed_field(bits = "24:24")]
    pub recursion_available: bool,
    #[packed_field(bits = "25:25")]
    pub reserved_9: bool,
    #[packed_field(bits = "26:26")]
    pub authentic_data: bool,
    #[packed_field(bits = "27:27")]
    pub checking_disabled: bool,
    #[packed_field(bits = "28:31")]
    pub response_code: Integer<u8, packed_bits::Bits4>,

    #[packed_field(bits = "32:47")]
    pub question_count: u16,
    #[packed_field(bits = "48:63")]
    pub answer_count: u16,
    #[packed_field(bits = "64:79")]
    pub authority_count: u16,
    #[packed_field(bits = "80:95")]
    pub additional_count: u16,
}

#[derive(Clone, Debug, PartialEq)]
pub struct RecordCounts {
    pub question: u16,
    pub answer: u16,
    pub authority: u16,
    pub additional: u16,
}

impl RecordCounts {
    pub fn new() -> RecordCounts {
        RecordCounts {
            question: 0,
            answer: 0,
            authority: 0,
            additional: 0,
        }
    }
}

/// Writes the message header to the provided buffer.
/// Allows specifying the transaction id since this is frequently overridden.
pub fn write_header_id(message: &Message, id_override: u16, buf: &mut BytesMut) -> Result<()> {
    let header = message
        .header()
        .with_context(|| "required header is missing")?;
    // Ensure that the OPT record, if any, is included in additional_count
    let opt_len = match message.opt() {
        Some(_opt) => 1,
        None => 0,
    };
    write_header_bits(
        HeaderBits {
            id: id_override,

            is_response: header.is_response(),
            op_code: Integer::from(header.op_code()),
            authoritative: header.authoritative(),
            truncated: header.truncated(),
            recursion_desired: header.recursion_desired(),
            recursion_available: header.recursion_available(),
            reserved_9: header.reserved_9(),
            authentic_data: header.authentic_data(),
            checking_disabled: header.checking_disabled(),
            response_code: Integer::from(header.response_code()),

            question_count: u16::try_from(match message.question() {
                Some(vec) => vec.len(),
                None => 0,
            })
            .with_context(|| "question length doesn't fit")?,
            answer_count: u16::try_from(match message.answer() {
                Some(vec) => vec.len(),
                None => 0,
            })
            .with_context(|| "answer length doesn't fit")?,
            authority_count: u16::try_from(match message.authority() {
                Some(vec) => vec.len(),
                None => 0,
            })
            .with_context(|| "authority length doesn't fit")?,
            additional_count: u16::try_from(match message.additional() {
                Some(vec) => vec.len() + opt_len,
                None => opt_len,
            })
            .with_context(|| "additional length doesn't fit")?,
        },
        buf,
    )
}

pub fn write_header_bits(bits: HeaderBits, buf: &mut BytesMut) -> Result<()> {
    let bits_packed = bits.pack();
    buf.reserve(bits_packed.len());
    buf.put_slice(&bits_packed);
    Ok(())
}

/// Returns the header content, along with whether it declares that the rest of the Message is truncated.
/// When this is encountered the message should be abandoned and a different connection method should be used.
/// (For example if the original request was UDP then try TCP)
/// Alternatively returns Ok(None) when there isn't enough data available to parse the header yet.
pub fn read_header<'a>(
    fb_builder: &mut flatbuffers::FlatBufferBuilder<'a>,
    buf: &[u8],
    offset: &mut usize,
) -> Result<Option<(flatbuffers::WIPOffset<Header<'a>>, RecordCounts, bool)>> {
    let bits_size = HeaderBits::packed_bytes();
    if buf.len() < *offset + bits_size {
        // Not enough bytes for the DNS header, wait for more bytes.
        return Ok(None);
    }
    let bits = HeaderBits::unpack_from_slice(&buf[*offset..*offset + bits_size])
        .with_context(|| "couldn't unpack header bits")?;

    let header = Header::create(
        fb_builder,
        &HeaderArgs {
            id: bits.id,
            is_response: bits.is_response,
            op_code: *bits.op_code,
            authoritative: bits.authoritative,
            truncated: bits.truncated,
            recursion_desired: bits.recursion_desired,
            recursion_available: bits.recursion_available,
            reserved_9: bits.reserved_9,
            authentic_data: bits.authentic_data,
            checking_disabled: bits.checking_disabled,
            response_code: *bits.response_code,
        },
    );

    let record_counts = RecordCounts {
        question: bits.question_count,
        answer: bits.answer_count,
        authority: bits.authority_count,
        additional: bits.additional_count,
    };

    *offset += bits_size;
    Ok(Some((header, record_counts, bits.truncated)))
}

#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct HeaderIDBits {
    #[packed_field(bits = "0:15")]
    id: u16,
}

/// Directly updates the request ID in a serialized Message.
/// This is a shortcut to avoid needing to reserialize a new message in the event of a retry.
pub fn update_message_id(id: u16, buf: &mut BytesMut, offset: usize) -> Result<()> {
    // The ID is (conveniently) located in the first two bytes of the message.
    let bits_size = HeaderIDBits::packed_bytes();
    let buf_len = buf.len();
    let to_update = buf.get_mut(offset..offset + bits_size).with_context(|| {
        format!(
            "Provided message to update is {} bytes, must be at least {}",
            buf_len, bits_size
        )
    })?;
    let bits = HeaderIDBits { id }.pack();
    for i in 0..bits_size {
        to_update[i] = bits[i];
    }
    Ok(())
}

/// Finds the minimum TTL across all resources in a Message, or None if there are no resources.
/// This is a utility for caching where we want to detect how much the response TTLs should be reduced.
/// For example, if the cache says that the remaining TTL is 20s, but the minimum resource TTL value is 30s,
/// then 10s has passed and all resources should be have TTLs reduced by 10s.
pub fn get_min_ttl(message: &Message) -> Option<u32> {
    let mut min_ttl: Option<u32> = None;

    // Skip header and question(s): No TTLs

    if let Some(resources) = message.answer() {
        for i in 0..resources.len() {
            let ttl = resources.get(i).ttl();
            match min_ttl {
                Some(min_val) => {
                    if ttl < min_val {
                        min_ttl = Some(ttl);
                    }
                }
                None => {
                    min_ttl = Some(ttl);
                }
            }
        }
    }

    if let Some(resources) = message.authority() {
        for i in 0..resources.len() {
            let ttl = resources.get(i).ttl();
            match min_ttl {
                Some(min_val) => {
                    if ttl < min_val {
                        min_ttl = Some(ttl);
                    }
                }
                None => {
                    min_ttl = Some(ttl);
                }
            }
        }
    }

    // Note: any OPT resource is in a separate message.opt() field. OPT resources don't have TTLs.
    if let Some(resources) = message.additional() {
        for i in 0..resources.len() {
            let ttl = resources.get(i).ttl();
            match min_ttl {
                Some(min_val) => {
                    if ttl < min_val {
                        min_ttl = Some(ttl);
                    }
                }
                None => {
                    min_ttl = Some(ttl);
                }
            }
        }
    }

    return min_ttl;
}

// Question, from RFC1035 section 4.1.2
//
//                                 1  1  1  1  1  1
//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                                               |
// /                     QNAME                     /
// /                                               /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                     QTYPE                     |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                     QCLASS                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct QuestionBits {
    // name: variable length string
    #[packed_field(bits = "0:15")]
    resource_type: u16,
    #[packed_field(bits = "16:31")]
    resource_class: u16,
}

pub fn write_question(
    question: &Question,
    buf: &mut BytesMut,
    ptr_offsets: &mut domain_name::LabelOffsets,
) -> Result<()> {
    domain_name::write(
        &question.name().with_context(|| "missing question.name")?,
        buf,
        ptr_offsets,
        "question.name",
    )?;

    let bits = QuestionBits {
        resource_type: question.resource_type(),
        resource_class: question.resource_class(),
    }
    .pack();
    buf.reserve(bits.len());
    buf.put_slice(&bits);
    Ok(())
}

pub fn read_question<'a>(
    fb_builder: &mut flatbuffers::FlatBufferBuilder<'a>,
    buf: &[u8],
    offset: &mut usize,
) -> Result<Option<flatbuffers::WIPOffset<Question<'a>>>> {
    let (name_bytes_consumed, name_str) = domain_name::read(buf, *offset, "question.name")?;

    let bits_size = QuestionBits::packed_bytes();
    if buf.len() < *offset + name_bytes_consumed + bits_size {
        // Not enough bytes for the question header, wait for more bytes.
        return Ok(None);
    }
    let bits = QuestionBits::unpack_from_slice(
        &buf[*offset + name_bytes_consumed..*offset + name_bytes_consumed + bits_size],
    )
    .with_context(|| "couldn't unpack question bits")?;

    *offset += name_bytes_consumed + bits_size;

    let args = QuestionArgs {
        name: Some(fb_builder.create_string(name_str.as_str())),
        resource_type: bits.resource_type,
        resource_class: bits.resource_class,
    };
    Ok(Some(Question::create(fb_builder, &args)))
}

// Resource, from RFC1035 section 4.1.3
//
//                                 1  1  1  1  1  1
//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                                               |
// /                      NAME                     /
// /                                               /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                      TYPE                     |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                     CLASS                     |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                      TTL                      |
// |                                               |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                   RDLENGTH                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
// /                     RDATA                     /
// /                                               /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

// Just the initial type field, for reading out and deciding if we're OPT or not.
#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct ResourceTypeBits {
    // name: variable length string
    #[packed_field(bits = "0:15")]
    resource_type: u16,
}

// The regular Class/TTL fields used by non-OPT records
#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct ResourceClassTTLBits {
    #[packed_field(bits = "0:15")]
    class: u16,
    #[packed_field(bits = "16:47")]
    ttl: u32,
}

// OPT-specific Class/TTL packing, from RFC6891 section 6.1.3:
//                                 1  1  1  1  1  1
//   0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |                   UDP-SIZE                    |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |     EXTENDED-RCODE    |        VERSION        |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |DO|                    Z                       |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// Mirrors ResourceClassTTLBits with OPT packing
// Members are public to allow access by rdata.rs
#[derive(Default, PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct ResourceClassTTLOPTBits {
    // Class field
    #[packed_field(bits = "0:15")]
    pub udp_size: u16,
    // TTL field
    #[packed_field(bits = "16:23")]
    pub response_code: u8,
    #[packed_field(bits = "24:31")]
    pub version: u8,
    #[packed_field(bits = "32:32")]
    pub dnssec_ok: bool,
    #[packed_field(bits = "33:47")]
    _reserved: ReservedZero<packed_bits::Bits15>,
}

// The RDLength field, initialized to zero then updated after writing the RData
#[derive(PackedStruct)]
#[packed_struct(endian = "msb", bit_numbering = "msb0")]
pub struct ResourceRdataLengthBits {
    #[packed_field(bits = "0:15")]
    rdata_len: u16,
}

pub enum RDataFields<'a> {
    RDATA(Option<ResourceData<'a>>),
    IP(IpAddr),
    SOA(rdata::SOAFields<'a>),
}

/// Values to be used when writing a Resource, instead of using what's in the fbs version.
/// Used when we're constructing our own custom response to something.
pub struct ResourceFields<'a> {
    pub name: &'a str,
    pub resource_type: u16,
    pub resource_class: u16,
    pub ttl: u32,
    pub rdata: RDataFields<'a>,
}

/// Encodes a DNS resource to the provided buf
pub fn write_resource(
    resource: &Resource,
    buf: &mut BytesMut,
    ptr_offsets: &mut domain_name::LabelOffsets,
) -> Result<()> {
    write_resource_fields(
        &ResourceFields {
            name: resource.name().with_context(|| "missing resource.name")?,
            resource_type: resource.resource_type(),
            resource_class: resource.resource_class(),
            ttl: resource.ttl(),
            rdata: RDataFields::RDATA(resource.rdata()),
        },
        buf,
        ptr_offsets,
    )
}

/// Encodes a resource to the provided buf, with the TTL reduced by the specified amount
pub fn write_resource_ttl_subtract(
    resource: &Resource,
    ttl_subtract: u32,
    buf: &mut BytesMut,
    ptr_offsets: &mut domain_name::LabelOffsets,
) -> Result<()> {
    write_resource_fields(
        &ResourceFields {
            name: resource.name().with_context(|| "missing resource.name")?,
            resource_type: resource.resource_type(),
            resource_class: resource.resource_class(),
            ttl: resource.ttl() - ttl_subtract,
            rdata: RDataFields::RDATA(resource.rdata()),
        },
        buf,
        ptr_offsets,
    )
}

/// Encodes a resource fields to the provided buf, with support for overriding field values
pub fn write_resource_fields(
    resource: &ResourceFields,
    buf: &mut BytesMut,
    ptr_offsets: &mut domain_name::LabelOffsets,
) -> Result<()> {
    domain_name::write(resource.name, buf, ptr_offsets, "resource.name")?;

    let type_bits = ResourceTypeBits {
        resource_type: resource.resource_type,
    }
    .pack();

    let classttl_bits = ResourceClassTTLBits {
        class: resource.resource_class,
        ttl: resource.ttl,
    }
    .pack();

    let rdlen_zero_bits = ResourceRdataLengthBits {
        rdata_len: 0 as u16, // To be updated below
    }
    .pack();

    buf.reserve(type_bits.len() + classttl_bits.len() + rdlen_zero_bits.len());
    buf.put_slice(&type_bits);
    buf.put_slice(&classttl_bits);
    let rdata_len_offset = buf.len();
    buf.put_slice(&rdlen_zero_bits);
    let rdata_offset = buf.len();

    // We write directly to 'buf' to ensure that any domain-name offsets are relative to the start
    // of the full 'buf'. Afterwards we go back and update the rdata_len to reflect the size of
    // what was written.
    match &resource.rdata {
        RDataFields::RDATA(None) => {}
        RDataFields::RDATA(Some(rdata)) => {
            rdata::write_rdata(resource.resource_type, &rdata, buf, ptr_offsets)?
        }
        RDataFields::IP(IpAddr::V4(ip)) => rdata::write_a_ip(&ip, buf)?,
        RDataFields::IP(IpAddr::V6(ip)) => rdata::write_aaaa_ip(&ip, buf)?,
        RDataFields::SOA(soa) => rdata::write_soa_fields(soa, buf, ptr_offsets)?,
    }

    if buf.len() > rdata_offset {
        // We wrote some rdata, so go back and update the rdata length
        let rdlen_updated_bits = ResourceRdataLengthBits {
            rdata_len: u16::try_from(buf.len() - rdata_offset)
                .with_context(|| "rdata.length doesn't fit")?,
        }
        .pack();
        let buf_len = buf.len();
        let bits = buf
            .get_mut(rdata_len_offset..rdata_offset)
            .with_context(|| {
                format!(
                    "failed to get bytes {}..{} from buffer with length {}",
                    rdata_len_offset, rdata_offset, buf_len
                )
            })?;
        // Overwrite the previously appended rdlen_zero_bits with rdlen_updated_bits
        for i in 0..(rdata_offset - rdata_len_offset) {
            bits[i] = rdlen_updated_bits[i];
        }
    }

    Ok(())
}

/// Reads a resource in the common case when OPT records are not expected.
pub fn read_resource_non_opt<'a>(
    fb_builder: &mut flatbuffers::FlatBufferBuilder<'a>,
    buf: &[u8],
    offset: &mut usize,
) -> Result<Option<flatbuffers::WIPOffset<Resource<'a>>>> {
    match read_resource_name_type(buf, offset).context("Failed to read non-OPT resource")? {
        Some((_name, OPT_RESOURCE_TYPE)) => bail!("Got OPT resource in unexpected part of message"),
        Some((name, resource_type)) => {
            match read_resource_remainder(fb_builder, buf, offset, name, resource_type)? {
                Some(resource) => Ok(Some(resource)),
                None => Ok(None),
            }
        }
        None => Ok(None),
    }
}

/// Reads the resource name and type, or None if not enough data is available.
pub fn read_resource_name_type<'a>(
    buf: &[u8],
    offset: &mut usize,
) -> Result<Option<(String, u16)>> {
    let (mut total_bytes_consumed, name_str) = domain_name::read(buf, *offset, "resource.name")?;

    // Get the type field first: Determine whether this is an OPT record
    let type_bits_size = ResourceTypeBits::packed_bytes();
    if buf.len() < *offset + total_bytes_consumed + type_bits_size {
        // Not enough bytes for the resource type field, wait for more bytes.
        return Ok(None);
    }
    let resource_type = ResourceTypeBits::unpack_from_slice(
        &buf[*offset + total_bytes_consumed..*offset + total_bytes_consumed + type_bits_size],
    )
    .with_context(|| "couldn't unpack resource prelude bits")?
    .resource_type;
    total_bytes_consumed += type_bits_size;

    *offset += total_bytes_consumed;

    Ok(Some((name_str, resource_type)))
}

/// Reads the rest of an OPT resource.
/// This should be called after read_resource_name_type(), which provides the type of resource being read.
pub fn read_resource_remainder_opt<'a>(
    fb_builder: &mut flatbuffers::FlatBufferBuilder<'a>,
    buf: &[u8],
    offset: &mut usize,
) -> Result<Option<flatbuffers::WIPOffset<OPT<'a>>>> {
    // OPT/EDNS(0) hax: Custom packing with OPT-specific fields
    let opt_class_ttl_bits_size = ResourceClassTTLOPTBits::packed_bytes();
    if buf.len() < *offset + opt_class_ttl_bits_size {
        // Not enough bytes for the OPT class/ttl fields, wait for more bytes.
        return Ok(None);
    }
    let opt_class_ttl_bits = ResourceClassTTLOPTBits::unpack_from_slice(
        &buf[*offset..*offset + opt_class_ttl_bits_size],
    )
    .with_context(|| "couldn't unpack OPT resource custom class/ttl bits")?;
    let mut total_bytes_consumed = opt_class_ttl_bits_size;

    let rdata_location = read_rdata_location(
        buf,
        *offset + total_bytes_consumed,
        &mut total_bytes_consumed,
    )
    .context("Failed to read OPT rdata location")?;
    match rdata_location {
        Some((rdata_offset, rdata_len)) => {
            // Pass the OPT-specific data replacing the class/TTL bits, to be stored in the OPT object
            let opt =
                rdata::read_opt(fb_builder, buf, rdata_offset, rdata_len, opt_class_ttl_bits)?;
            total_bytes_consumed += rdata_len;
            *offset += total_bytes_consumed;
            Ok(Some(opt))
        }
        None => {
            // Not enough bytes for the size info (or the expected rdata), wait for more bytes.
            Ok(None)
        }
    }
}

/// Reads the rest of a non-OPT resource.
/// This should be called after read_resource_name_type(), which provides the type of resource being read.
pub fn read_resource_remainder<'a>(
    fb_builder: &mut flatbuffers::FlatBufferBuilder<'a>,
    buf: &[u8],
    offset: &mut usize,
    name_str: String,
    resource_type: u16,
) -> Result<Option<flatbuffers::WIPOffset<Resource<'a>>>> {
    // Non-OPT resource: regular class and TTL fields
    let class_ttl_bits_size = ResourceClassTTLBits::packed_bytes();
    if buf.len() < *offset + class_ttl_bits_size {
        // Not enough bytes for the class/ttl fields, wait for more bytes.
        return Ok(None);
    }
    let class_ttl_bits =
        ResourceClassTTLBits::unpack_from_slice(&buf[*offset..*offset + class_ttl_bits_size])
            .with_context(|| "couldn't unpack resource class/ttl bits")?;
    let mut total_bytes_consumed = class_ttl_bits_size;

    let rdata_location = read_rdata_location(
        buf,
        *offset + total_bytes_consumed,
        &mut total_bytes_consumed,
    )
    .with_context(|| {
        format!(
            "Failed to read {:?} resource rdata location for {}",
            resource_type, name_str
        )
    })?;
    match rdata_location {
        Some((rdata_offset, rdata_len)) => {
            let rdata = rdata::read_rdata(fb_builder, buf, resource_type, rdata_offset, rdata_len)
                .with_context(|| {
                    format!(
                        "Failed to read {:?} resource for {}",
                        resource_type, name_str
                    )
                })?;
            total_bytes_consumed += rdata_len;
            *offset += total_bytes_consumed;
            let args = ResourceArgs {
                name: Some(fb_builder.create_string(name_str.as_str())),
                resource_type,
                resource_class: class_ttl_bits.class,
                ttl: class_ttl_bits.ttl,
                rdata: Some(ResourceData::create(fb_builder, &rdata)),
            };
            Ok(Some(Resource::create(fb_builder, &args)))
        }
        None => {
            // Not enough bytes for the size info (or the expected rdata), wait for more bytes.
            Ok(None)
        }
    }
}

/// Returns the offset and length of the rdata available in the buffer,
/// or None if the buffer is incomplete
fn read_rdata_location<'a>(
    buf: &[u8],
    offset: usize,
    total_bytes_consumed: &mut usize,
) -> Result<Option<(usize, usize)>> {
    let rdata_length_bits_size = ResourceRdataLengthBits::packed_bytes();
    let rdata_offset = offset + rdata_length_bits_size;
    if buf.len() < rdata_offset {
        // Not enough bytes for the rdata length field, wait for more bytes.
        return Ok(None);
    }
    let rdata_length_bits =
        ResourceRdataLengthBits::unpack_from_slice(&buf[offset..offset + rdata_length_bits_size])
            .with_context(|| "couldn't unpack rdata length bits")?;

    let rdata_len = rdata_length_bits.rdata_len as usize;
    if buf.len() < rdata_offset + rdata_len {
        // Not enough bytes for the rdata content, wait for more bytes.
        return Ok(None);
    }

    *total_bytes_consumed += rdata_length_bits_size;
    Ok(Some((rdata_offset, rdata_len)))
}

pub fn write_opt(opt: &OPT, udp_size_override: Option<u16>, buf: &mut BytesMut) -> Result<()> {
    domain_name::write_nopointer(".", buf, "opt.name")?;

    let type_bits = ResourceTypeBits {
        resource_type: ResourceType::TYPE_OPT as u16,
    }
    .pack();

    let classttl_bits = ResourceClassTTLOPTBits {
        udp_size: match udp_size_override {
            Some(udp_size) => udp_size,
            None => opt.udp_size(),
        },
        response_code: opt.response_code(),
        version: opt.response_code(),
        dnssec_ok: opt.dnssec_ok(),
        ..ResourceClassTTLOPTBits::default() // for setting the _reserved field
    }
    .pack();

    let rdlen_zero_bits = ResourceRdataLengthBits {
        rdata_len: 0 as u16, // To be updated below
    }
    .pack();

    buf.reserve(type_bits.len() + classttl_bits.len() + rdlen_zero_bits.len());
    buf.put_slice(&type_bits);
    buf.put_slice(&classttl_bits);
    let rdata_len_offset = buf.len();
    buf.put_slice(&rdlen_zero_bits);
    let rdata_offset = buf.len();

    rdata::write_opt(opt, buf)?;

    if buf.len() > rdata_offset {
        // We wrote some rdata, so go back and update the rdata length
        let rdlen_updated_bits = ResourceRdataLengthBits {
            rdata_len: u16::try_from(buf.len() - rdata_offset)
                .with_context(|| "rdata.length doesn't fit")?,
        }
        .pack();
        let buf_len = buf.len();
        let bits = buf
            .get_mut(rdata_len_offset..rdata_offset)
            .with_context(|| {
                format!(
                    "failed to get bytes {}..{} from buffer with length {}",
                    rdata_len_offset, rdata_offset, buf_len
                )
            })?;
        // Overwrite the previously appended rdlen_zero_bits with rdlen_updated_bits
        for i in 0..(rdata_offset - rdata_len_offset) {
            bits[i] = rdlen_updated_bits[i];
        }
    }
    Ok(())
}